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