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.

cond_var.cc 2.6 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/cond_var.h"
  17. #include <exception>
  18. #include "minddata/dataset/util/services.h"
  19. #include "minddata/dataset/util/task_manager.h"
  20. namespace mindspore {
  21. namespace dataset {
  22. CondVar::CondVar() : svc_(nullptr), my_name_(Services::GetUniqueID()) {}
  23. Status CondVar::Wait(std::unique_lock<std::mutex> *lck, const std::function<bool()> &pred) {
  24. try {
  25. if (svc_ != nullptr) {
  26. // If this cv registers with a global resource tracking, then wait unconditionally.
  27. auto f = [this, &pred]() -> bool { return (pred() || this->Interrupted()); };
  28. cv_.wait(*lck, f);
  29. // If we are interrupted, override the return value if this is the master thread.
  30. // Master thread is being interrupted mostly because of some thread is reporting error.
  31. RETURN_IF_NOT_OK(Task::OverrideInterruptRc(this->GetInterruptStatus()));
  32. } else {
  33. // Otherwise we wake up once a while to check for interrupt (for this thread).
  34. auto f = [&pred]() -> bool { return (pred() || this_thread::is_interrupted()); };
  35. while (!f()) {
  36. (void)cv_.wait_for(*lck, std::chrono::milliseconds(1));
  37. }
  38. RETURN_IF_INTERRUPTED();
  39. }
  40. } catch (const std::exception &e) {
  41. RETURN_STATUS_UNEXPECTED(e.what());
  42. }
  43. return Status::OK();
  44. }
  45. CondVar::~CondVar() noexcept {
  46. if (svc_ != nullptr) {
  47. (void)svc_->Deregister(my_name_);
  48. svc_ = nullptr;
  49. }
  50. }
  51. void CondVar::NotifyOne() noexcept { cv_.notify_one(); }
  52. void CondVar::NotifyAll() noexcept { cv_.notify_all(); }
  53. Status CondVar::Register(std::shared_ptr<IntrpService> svc) {
  54. Status rc = svc->Register(my_name_, this);
  55. if (rc.IsOk()) {
  56. svc_ = svc;
  57. }
  58. return rc;
  59. }
  60. void CondVar::Interrupt() {
  61. IntrpResource::Interrupt();
  62. cv_.notify_all();
  63. }
  64. std::string CondVar::my_name() const { return my_name_; }
  65. Status CondVar::Deregister() {
  66. if (svc_) {
  67. Status rc = svc_->Deregister(my_name_);
  68. svc_ = nullptr;
  69. return rc;
  70. }
  71. return Status::OK();
  72. }
  73. } // namespace dataset
  74. } // namespace mindspore