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

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