xref: /aosp_15_r20/external/abseil-cpp/absl/synchronization/notification.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #include "absl/synchronization/notification.h"
16*9356374aSAndroid Build Coastguard Worker 
17*9356374aSAndroid Build Coastguard Worker #include <atomic>
18*9356374aSAndroid Build Coastguard Worker 
19*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/raw_logging.h"
20*9356374aSAndroid Build Coastguard Worker #include "absl/synchronization/mutex.h"
21*9356374aSAndroid Build Coastguard Worker #include "absl/time/time.h"
22*9356374aSAndroid Build Coastguard Worker 
23*9356374aSAndroid Build Coastguard Worker namespace absl {
24*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
25*9356374aSAndroid Build Coastguard Worker 
Notify()26*9356374aSAndroid Build Coastguard Worker void Notification::Notify() {
27*9356374aSAndroid Build Coastguard Worker   MutexLock l(&this->mutex_);
28*9356374aSAndroid Build Coastguard Worker 
29*9356374aSAndroid Build Coastguard Worker #ifndef NDEBUG
30*9356374aSAndroid Build Coastguard Worker   if (ABSL_PREDICT_FALSE(notified_yet_.load(std::memory_order_relaxed))) {
31*9356374aSAndroid Build Coastguard Worker     ABSL_RAW_LOG(
32*9356374aSAndroid Build Coastguard Worker         FATAL,
33*9356374aSAndroid Build Coastguard Worker         "Notify() method called more than once for Notification object %p",
34*9356374aSAndroid Build Coastguard Worker         static_cast<void *>(this));
35*9356374aSAndroid Build Coastguard Worker   }
36*9356374aSAndroid Build Coastguard Worker #endif
37*9356374aSAndroid Build Coastguard Worker 
38*9356374aSAndroid Build Coastguard Worker   notified_yet_.store(true, std::memory_order_release);
39*9356374aSAndroid Build Coastguard Worker }
40*9356374aSAndroid Build Coastguard Worker 
~Notification()41*9356374aSAndroid Build Coastguard Worker Notification::~Notification() {
42*9356374aSAndroid Build Coastguard Worker   // Make sure that the thread running Notify() exits before the object is
43*9356374aSAndroid Build Coastguard Worker   // destructed.
44*9356374aSAndroid Build Coastguard Worker   MutexLock l(&this->mutex_);
45*9356374aSAndroid Build Coastguard Worker }
46*9356374aSAndroid Build Coastguard Worker 
WaitForNotification() const47*9356374aSAndroid Build Coastguard Worker void Notification::WaitForNotification() const {
48*9356374aSAndroid Build Coastguard Worker   if (!HasBeenNotifiedInternal(&this->notified_yet_)) {
49*9356374aSAndroid Build Coastguard Worker     this->mutex_.LockWhen(Condition(&HasBeenNotifiedInternal,
50*9356374aSAndroid Build Coastguard Worker                                     &this->notified_yet_));
51*9356374aSAndroid Build Coastguard Worker     this->mutex_.Unlock();
52*9356374aSAndroid Build Coastguard Worker   }
53*9356374aSAndroid Build Coastguard Worker }
54*9356374aSAndroid Build Coastguard Worker 
WaitForNotificationWithTimeout(absl::Duration timeout) const55*9356374aSAndroid Build Coastguard Worker bool Notification::WaitForNotificationWithTimeout(
56*9356374aSAndroid Build Coastguard Worker     absl::Duration timeout) const {
57*9356374aSAndroid Build Coastguard Worker   bool notified = HasBeenNotifiedInternal(&this->notified_yet_);
58*9356374aSAndroid Build Coastguard Worker   if (!notified) {
59*9356374aSAndroid Build Coastguard Worker     notified = this->mutex_.LockWhenWithTimeout(
60*9356374aSAndroid Build Coastguard Worker         Condition(&HasBeenNotifiedInternal, &this->notified_yet_), timeout);
61*9356374aSAndroid Build Coastguard Worker     this->mutex_.Unlock();
62*9356374aSAndroid Build Coastguard Worker   }
63*9356374aSAndroid Build Coastguard Worker   return notified;
64*9356374aSAndroid Build Coastguard Worker }
65*9356374aSAndroid Build Coastguard Worker 
WaitForNotificationWithDeadline(absl::Time deadline) const66*9356374aSAndroid Build Coastguard Worker bool Notification::WaitForNotificationWithDeadline(absl::Time deadline) const {
67*9356374aSAndroid Build Coastguard Worker   bool notified = HasBeenNotifiedInternal(&this->notified_yet_);
68*9356374aSAndroid Build Coastguard Worker   if (!notified) {
69*9356374aSAndroid Build Coastguard Worker     notified = this->mutex_.LockWhenWithDeadline(
70*9356374aSAndroid Build Coastguard Worker         Condition(&HasBeenNotifiedInternal, &this->notified_yet_), deadline);
71*9356374aSAndroid Build Coastguard Worker     this->mutex_.Unlock();
72*9356374aSAndroid Build Coastguard Worker   }
73*9356374aSAndroid Build Coastguard Worker   return notified;
74*9356374aSAndroid Build Coastguard Worker }
75*9356374aSAndroid Build Coastguard Worker 
76*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
77*9356374aSAndroid Build Coastguard Worker }  // namespace absl
78