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

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