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.9 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SERVICES_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SERVICES_H_
  18. #include <memory>
  19. #include <mutex>
  20. #include <string>
  21. #include "minddata/dataset/util/memory_pool.h"
  22. #include "minddata/dataset/util/allocator.h"
  23. #include "minddata/dataset/util/service.h"
  24. #define UNIQUEID_LEN 36
  25. namespace mindspore {
  26. namespace dataset {
  27. class TaskManager;
  28. class CacheServer;
  29. class Services {
  30. public:
  31. static Status CreateInstance() {
  32. std::call_once(init_instance_flag_, [&]() -> Status {
  33. instance_.reset(new Services());
  34. return (instance_->CreateAllInstances());
  35. });
  36. if (instance_ == nullptr) {
  37. instance_.reset(new Services());
  38. return (instance_->CreateAllInstances());
  39. }
  40. return Status::OK();
  41. }
  42. static Services &GetInstance() {
  43. if (instance_ == nullptr) {
  44. if (!CreateInstance()) {
  45. std::terminate();
  46. }
  47. }
  48. return *instance_;
  49. }
  50. Services(const Services &) = delete;
  51. Services &operator=(const Services &) = delete;
  52. ~Services() noexcept;
  53. static TaskManager &getTaskMgrInstance();
  54. static CacheServer &getCacheServer();
  55. std::shared_ptr<MemoryPool> GetServiceMemPool() { return pool_; }
  56. #if !defined(_WIN32) && !defined(_WIN64)
  57. static std::string GetUserName();
  58. static std::string GetHostName();
  59. static int GetLWP();
  60. #endif
  61. static std::string GetUniqueID();
  62. template <typename T>
  63. static Allocator<T> GetAllocator() {
  64. return Allocator<T>(Services::GetInstance().GetServiceMemPool());
  65. }
  66. private:
  67. static std::once_flag init_instance_flag_;
  68. static std::unique_ptr<Services> instance_;
  69. // A small pool used for small objects that last until the
  70. // Services Manager shuts down. Used by all sub-services.
  71. std::shared_ptr<MemoryPool> pool_;
  72. // We use pointers here instead of unique_ptr because we
  73. // want to have ultimate control on the order of
  74. // construction and destruction.
  75. static constexpr int kSlotTaskMgr_ = 0;
  76. static constexpr int kSlotCacheMgr_ = 1;
  77. static constexpr int kNumServices_ = 2;
  78. Service *sa_[kNumServices_];
  79. Services();
  80. Status CreateAllInstances();
  81. };
  82. } // namespace dataset
  83. } // namespace mindspore
  84. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SERVICES_H_