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.

intrp_service.cc 2.9 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "minddata/dataset/util/intrp_service.h"
  17. #include <sstream>
  18. #include "minddata/dataset/util/services.h"
  19. #include "minddata/dataset/util/task_manager.h"
  20. namespace mindspore {
  21. namespace dataset {
  22. IntrpService::IntrpService() : high_water_mark_(0) { (void)ServiceStart(); }
  23. IntrpService::~IntrpService() noexcept {
  24. MS_LOG(INFO) << "Number of registered resources is " << high_water_mark_ << ".";
  25. if (!all_intrp_resources_.empty()) {
  26. try {
  27. InterruptAll();
  28. } catch (const std::exception &e) {
  29. // Ignore all error as we can't throw in the destructor.
  30. }
  31. }
  32. (void)ServiceStop();
  33. }
  34. Status IntrpService::Register(const std::string &name, IntrpResource *res) {
  35. SharedLock stateLck(&state_lock_);
  36. // Now double check the state
  37. if (ServiceState() != STATE::kRunning) {
  38. return Status(StatusCode::kMDInterrupted, __LINE__, __FILE__, "Interrupt service is shutting down");
  39. } else {
  40. std::lock_guard<std::mutex> lck(mutex_);
  41. try {
  42. std::ostringstream ss;
  43. ss << this_thread::get_id();
  44. MS_LOG(DEBUG) << "Register resource with name " << name << ". Thread ID " << ss.str() << ".";
  45. auto it = all_intrp_resources_.emplace(name, res);
  46. if (it.second == false) {
  47. return Status(StatusCode::kMDDuplicateKey, __LINE__, __FILE__, name);
  48. }
  49. high_water_mark_++;
  50. } catch (std::exception &e) {
  51. RETURN_STATUS_UNEXPECTED(e.what());
  52. }
  53. }
  54. return Status::OK();
  55. }
  56. Status IntrpService::Deregister(const std::string &name) noexcept {
  57. std::lock_guard<std::mutex> lck(mutex_);
  58. try {
  59. std::ostringstream ss;
  60. ss << this_thread::get_id();
  61. MS_LOG(DEBUG) << "De-register resource with name " << name << ". Thread ID is " << ss.str() << ".";
  62. auto n = all_intrp_resources_.erase(name);
  63. if (n == 0) {
  64. MS_LOG(INFO) << "Key " << name << " not found.";
  65. }
  66. } catch (std::exception &e) {
  67. RETURN_STATUS_UNEXPECTED(e.what());
  68. }
  69. return Status::OK();
  70. }
  71. void IntrpService::InterruptAll() noexcept {
  72. std::lock_guard<std::mutex> lck(mutex_);
  73. for (auto const &it : all_intrp_resources_) {
  74. std::string kName = it.first;
  75. try {
  76. it.second->Interrupt();
  77. } catch (const std::exception &e) {
  78. // continue the clean up.
  79. }
  80. }
  81. }
  82. } // namespace dataset
  83. } // namespace mindspore