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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "dataset/util/intrp_service.h"
  17. #include <sstream>
  18. #include "common/utils.h"
  19. #include "dataset/util/services.h"
  20. #include "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. (void)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::kInterrupted, __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. (void)all_intrp_resources_.emplace(name, res);
  47. high_water_mark_++;
  48. } catch (std::exception &e) {
  49. RETURN_STATUS_UNEXPECTED(e.what());
  50. }
  51. }
  52. return Status::OK();
  53. }
  54. Status IntrpService::Deregister(const std::string &name) noexcept {
  55. std::lock_guard<std::mutex> lck(mutex_);
  56. try {
  57. std::ostringstream ss;
  58. ss << this_thread::get_id();
  59. MS_LOG(DEBUG) << "De-register resource with name " << name << ". Thread ID is " << ss.str() << ".";
  60. auto it = all_intrp_resources_.find(name);
  61. if (it != all_intrp_resources_.end()) {
  62. (void)all_intrp_resources_.erase(it);
  63. } else {
  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. Status IntrpService::InterruptAll() noexcept {
  72. Status rc;
  73. for (auto const &it : all_intrp_resources_) {
  74. std::string kName = it.first;
  75. try {
  76. Status rc2 = it.second->Interrupt();
  77. if (rc2.IsError()) {
  78. rc = rc2;
  79. }
  80. } catch (const std::exception &e) {
  81. // continue the clean up.
  82. }
  83. }
  84. return rc;
  85. }
  86. } // namespace dataset
  87. } // namespace mindspore