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
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 "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. CondVar::~CondVar() noexcept {
  45. if (svc_ != nullptr) {
  46. (void)svc_->Deregister(my_name_);
  47. svc_ = nullptr;
  48. }
  49. }
  50. void CondVar::NotifyOne() noexcept { cv_.notify_one(); }
  51. void CondVar::NotifyAll() noexcept { cv_.notify_all(); }
  52. Status CondVar::Register(std::shared_ptr<IntrpService> svc) {
  53. Status rc = svc->Register(my_name_, this);
  54. if (rc.IsOk()) {
  55. svc_ = svc;
  56. }
  57. return rc;
  58. }
  59. void CondVar::Interrupt() {
  60. IntrpResource::Interrupt();
  61. cv_.notify_all();
  62. }
  63. std::string CondVar::my_name() const { return my_name_; }
  64. Status CondVar::Deregister() {
  65. if (svc_) {
  66. Status rc = svc_->Deregister(my_name_);
  67. svc_ = nullptr;
  68. return rc;
  69. }
  70. return Status::OK();
  71. }
  72. } // namespace dataset
  73. } // namespace mindspore