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 3.4 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <algorithm>
  19. #include <map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <string>
  23. #include <vector>
  24. #include "minddata/dataset/util/memory_pool.h"
  25. #include "minddata/dataset/util/allocator.h"
  26. #include "minddata/dataset/util/service.h"
  27. #define UNIQUEID_LEN 36
  28. #define UNIQUEID_LIST_LIMITS 1024
  29. #define UNIQUEID_HALF_INDEX ((UNIQUEID_LIST_LIMITS) / 2)
  30. namespace mindspore {
  31. namespace dataset {
  32. class TaskManager;
  33. class Services {
  34. public:
  35. static Status CreateInstance() {
  36. std::call_once(init_instance_flag_, [&]() -> Status {
  37. instance_.reset(new Services());
  38. return (instance_->CreateAllInstances());
  39. });
  40. if (instance_ == nullptr) {
  41. instance_.reset(new Services());
  42. return (instance_->CreateAllInstances());
  43. }
  44. return Status::OK();
  45. }
  46. static Services &GetInstance() {
  47. if (instance_ == nullptr) {
  48. if (!CreateInstance()) {
  49. std::terminate();
  50. }
  51. }
  52. return *instance_;
  53. }
  54. Services(const Services &) = delete;
  55. Services &operator=(const Services &) = delete;
  56. ~Services() noexcept;
  57. std::shared_ptr<MemoryPool> GetServiceMemPool() { return pool_; }
  58. #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__APPLE__)
  59. static std::string GetUserName();
  60. static std::string GetHostName();
  61. static int GetLWP();
  62. #endif
  63. static std::string GetUniqueID();
  64. template <typename T>
  65. static Allocator<T> GetAllocator() {
  66. return Allocator<T>(Services::GetInstance().GetServiceMemPool());
  67. }
  68. /// \brief Add a new service to the start up list.
  69. /// \tparam T Class that implements Service
  70. /// \return Status object and where the service is located in the hook_ list
  71. template <typename T, typename... Args>
  72. Status AddHook(T **out, Args &&... args) {
  73. RETURN_UNEXPECTED_IF_NULL(out);
  74. try {
  75. (*out) = new T(std::forward<Args>(args)...);
  76. std::unique_ptr<T> svc(*out);
  77. hook_.push_back(std::move(svc));
  78. } catch (const std::bad_alloc &e) {
  79. return Status(StatusCode::kMDOutOfMemory);
  80. }
  81. return Status::OK();
  82. }
  83. private:
  84. static std::once_flag init_instance_flag_;
  85. static std::unique_ptr<Services> instance_;
  86. static std::map<std::string, uint64_t> unique_id_list_;
  87. static uint64_t unique_id_count_;
  88. static std::mutex unique_id_mutex_;
  89. // A small pool used for small objects that last until the
  90. // Services Manager shuts down. Used by all sub-services.
  91. std::shared_ptr<MemoryPool> pool_;
  92. std::vector<std::unique_ptr<Service>> hook_;
  93. Services();
  94. Status CreateAllInstances();
  95. };
  96. } // namespace dataset
  97. } // namespace mindspore
  98. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SERVICES_H_