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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <utility>
  18. #include "dataset/util/services.h"
  19. #include "dataset/util/task_manager.h"
  20. namespace mindspore {
  21. namespace dataset {
  22. CondVar::CondVar() : svc_(nullptr), my_name_(std::move(Services::GetUniqueID())) {}
  23. Status CondVar::Wait(std::unique_lock<std::mutex> *lck, const std::function<bool()> &pred) {
  24. // Append an additional condition on top of the given predicate.
  25. // We will also bail out if this cv got interrupted.
  26. auto f = [this, &pred]() -> bool { return (pred() || (CurState() == State::kInterrupted)); };
  27. // If we have interrupt service, just wait on the cv unconditionally.
  28. // Otherwise fall back to the old way of checking interrupt.
  29. if (svc_) {
  30. cv_.wait(*lck, f);
  31. if (CurState() == State::kInterrupted) {
  32. Task *my_task = TaskManager::FindMe();
  33. if (my_task->IsMasterThread() && my_task->CaughtSevereException()) {
  34. return TaskManager::GetMasterThreadRc();
  35. } else {
  36. return Status(StatusCode::kInterrupted);
  37. }
  38. }
  39. } else {
  40. RETURN_IF_NOT_OK(interruptible_wait(&cv_, lck, pred));
  41. if (CurState() == State::kInterrupted) {
  42. return Status(StatusCode::kInterrupted);
  43. }
  44. }
  45. return Status::OK();
  46. }
  47. CondVar::~CondVar() noexcept {
  48. if (svc_ != nullptr) {
  49. (void)svc_->Deregister(my_name_);
  50. svc_ = nullptr;
  51. }
  52. }
  53. void CondVar::NotifyOne() noexcept { cv_.notify_one(); }
  54. void CondVar::NotifyAll() noexcept { cv_.notify_all(); }
  55. Status CondVar::Register(std::shared_ptr<IntrpService> svc) {
  56. Status rc = svc->Register(my_name_, this);
  57. if (rc.IsOk()) {
  58. svc_ = svc;
  59. }
  60. return rc;
  61. }
  62. Status CondVar::Interrupt() {
  63. RETURN_IF_NOT_OK(IntrpResource::Interrupt());
  64. cv_.notify_all();
  65. return Status::OK();
  66. }
  67. std::string CondVar::my_name() const { return my_name_; }
  68. Status CondVar::Deregister() {
  69. if (svc_) {
  70. Status rc = svc_->Deregister(my_name_);
  71. svc_ = nullptr;
  72. return rc;
  73. }
  74. return Status::OK();
  75. }
  76. } // namespace dataset
  77. } // namespace mindspore