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

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