You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

services.h 2.6 kB

6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef DATASET_UTIL_SERVICES_H_
  17. #define DATASET_UTIL_SERVICES_H_
  18. #include <memory>
  19. #include <mutex>
  20. #include <string>
  21. #include "dataset/util/memory_pool.h"
  22. #include "dataset/util/allocator.h"
  23. #include "dataset/util/service.h"
  24. #define UNIQUEID_LEN 36
  25. namespace mindspore {
  26. namespace dataset {
  27. class TaskManager;
  28. class Services {
  29. public:
  30. static Status CreateInstance() {
  31. std::call_once(init_instance_flag_, [&]() -> Status {
  32. instance_.reset(new Services());
  33. return (instance_->CreateAllInstances());
  34. });
  35. if (instance_ == nullptr) {
  36. instance_.reset(new Services());
  37. return (instance_->CreateAllInstances());
  38. }
  39. return Status::OK();
  40. }
  41. static Services &GetInstance() {
  42. if (instance_ == nullptr) {
  43. if (!CreateInstance()) {
  44. std::terminate();
  45. }
  46. }
  47. return *instance_;
  48. }
  49. Services(const Services &) = delete;
  50. Services &operator=(const Services &) = delete;
  51. ~Services() noexcept;
  52. static TaskManager &getTaskMgrInstance();
  53. std::shared_ptr<MemoryPool> GetServiceMemPool() { return pool_; }
  54. #if !defined(_WIN32) && !defined(_WIN64)
  55. static std::string GetUserName();
  56. static std::string GetHostName();
  57. static int GetLWP();
  58. #endif
  59. static std::string GetUniqueID();
  60. template <typename T>
  61. static Allocator<T> GetAllocator() {
  62. return Allocator<T>(Services::GetInstance().GetServiceMemPool());
  63. }
  64. private:
  65. static std::once_flag init_instance_flag_;
  66. static std::unique_ptr<Services> instance_;
  67. // A small pool used for small objects that last until the
  68. // Services Manager shuts down. Used by all sub-services.
  69. std::shared_ptr<MemoryPool> pool_;
  70. // We use pointers here instead of unique_ptr because we
  71. // want to have ultimate control on the order of
  72. // construction and destruction.
  73. static constexpr int kNumServices_ = 1;
  74. Service *sa_[kNumServices_];
  75. Services();
  76. Status CreateAllInstances();
  77. };
  78. } // namespace dataset
  79. } // namespace mindspore
  80. #endif // DATASET_UTIL_SERVICES_H_