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