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