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

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