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

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <memory>
  20. #include <mutex>
  21. #include <set>
  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. namespace mindspore {
  29. namespace dataset {
  30. class TaskManager;
  31. class Services {
  32. public:
  33. static Status CreateInstance() {
  34. std::call_once(init_instance_flag_, [&]() -> Status {
  35. instance_.reset(new Services());
  36. return (instance_->CreateAllInstances());
  37. });
  38. if (instance_ == nullptr) {
  39. instance_.reset(new Services());
  40. return (instance_->CreateAllInstances());
  41. }
  42. return Status::OK();
  43. }
  44. static Services &GetInstance() {
  45. if (instance_ == nullptr) {
  46. if (!CreateInstance()) {
  47. std::terminate();
  48. }
  49. }
  50. return *instance_;
  51. }
  52. Services(const Services &) = delete;
  53. Services &operator=(const Services &) = delete;
  54. ~Services() noexcept;
  55. std::shared_ptr<MemoryPool> GetServiceMemPool() { return pool_; }
  56. #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID)
  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. /// \brief Add a new service to the start up list.
  67. /// \tparam T Class that implements Service
  68. /// \return Status object and where the service is located in the hook_ list
  69. template <typename T, typename... Args>
  70. Status AddHook(T **out, Args &&... args) {
  71. RETURN_UNEXPECTED_IF_NULL(out);
  72. try {
  73. (*out) = new T(std::forward<Args>(args)...);
  74. std::unique_ptr<T> svc(*out);
  75. hook_.push_back(std::move(svc));
  76. } catch (const std::bad_alloc &e) {
  77. return Status(StatusCode::kOutOfMemory);
  78. }
  79. return Status::OK();
  80. }
  81. private:
  82. static std::once_flag init_instance_flag_;
  83. static std::unique_ptr<Services> instance_;
  84. static std::set<std::string> unique_id_list_;
  85. static std::mutex unique_id_mutex_;
  86. // A small pool used for small objects that last until the
  87. // Services Manager shuts down. Used by all sub-services.
  88. std::shared_ptr<MemoryPool> pool_;
  89. std::vector<std::unique_ptr<Service>> hook_;
  90. Services();
  91. Status CreateAllInstances();
  92. };
  93. } // namespace dataset
  94. } // namespace mindspore
  95. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SERVICES_H_