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