abseil-cpp/absl/synchronization/notification.h
Go to the documentation of this file.
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 // -----------------------------------------------------------------------------
16 // notification.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header file defines a `Notification` abstraction, which allows threads
20 // to receive notification of a single occurrence of a single event.
21 //
22 // The `Notification` object maintains a private boolean "notified" state that
23 // transitions to `true` at most once. The `Notification` class provides the
24 // following primary member functions:
25 // * `HasBeenNotified()` to query its state
26 // * `WaitForNotification*()` to have threads wait until the "notified" state
27 // is `true`.
28 // * `Notify()` to set the notification's "notified" state to `true` and
29 // notify all waiting threads that the event has occurred.
30 // This method may only be called once.
31 //
32 // Note that while `Notify()` may only be called once, it is perfectly valid to
33 // call any of the `WaitForNotification*()` methods multiple times, from
34 // multiple threads -- even after the notification's "notified" state has been
35 // set -- in which case those methods will immediately return.
36 //
37 // Note that the lifetime of a `Notification` requires careful consideration;
38 // it might not be safe to destroy a notification after calling `Notify()` since
39 // it is still legal for other threads to call `WaitForNotification*()` methods
40 // on the notification. However, observers responding to a "notified" state of
41 // `true` can safely delete the notification without interfering with the call
42 // to `Notify()` in the other thread.
43 //
44 // Memory ordering: For any threads X and Y, if X calls `Notify()`, then any
45 // action taken by X before it calls `Notify()` is visible to thread Y after:
46 // * Y returns from `WaitForNotification()`, or
47 // * Y receives a `true` return value from either `HasBeenNotified()` or
48 // `WaitForNotificationWithTimeout()`.
49 
50 #ifndef ABSL_SYNCHRONIZATION_NOTIFICATION_H_
51 #define ABSL_SYNCHRONIZATION_NOTIFICATION_H_
52 
53 #include <atomic>
54 
55 #include "absl/base/attributes.h"
56 #include "absl/base/macros.h"
57 #include "absl/synchronization/mutex.h"
58 #include "absl/time/time.h"
59 
60 namespace absl {
62 
63 // -----------------------------------------------------------------------------
64 // Notification
65 // -----------------------------------------------------------------------------
66 class Notification {
67  public:
68  // Initializes the "notified" state to unnotified.
70  explicit Notification(bool prenotify) : notified_yet_(prenotify) {}
71  Notification(const Notification&) = delete;
72  Notification& operator=(const Notification&) = delete;
73  ~Notification();
74 
75  // Notification::HasBeenNotified()
76  //
77  // Returns the value of the notification's internal "notified" state.
80  }
81 
82  // Notification::WaitForNotification()
83  //
84  // Blocks the calling thread until the notification's "notified" state is
85  // `true`. Note that if `Notify()` has been previously called on this
86  // notification, this function will immediately return.
87  void WaitForNotification() const;
88 
89  // Notification::WaitForNotificationWithTimeout()
90  //
91  // Blocks until either the notification's "notified" state is `true` (which
92  // may occur immediately) or the timeout has elapsed, returning the value of
93  // its "notified" state in either case.
95 
96  // Notification::WaitForNotificationWithDeadline()
97  //
98  // Blocks until either the notification's "notified" state is `true` (which
99  // may occur immediately) or the deadline has expired, returning the value of
100  // its "notified" state in either case.
101  bool WaitForNotificationWithDeadline(absl::Time deadline) const;
102 
103  // Notification::Notify()
104  //
105  // Sets the "notified" state of this notification to `true` and wakes waiting
106  // threads. Note: do not call `Notify()` multiple times on the same
107  // `Notification`; calling `Notify()` more than once on the same notification
108  // results in undefined behavior.
109  void Notify();
110 
111  private:
112  static inline bool HasBeenNotifiedInternal(
113  const std::atomic<bool>* notified_yet) {
114  return notified_yet->load(std::memory_order_acquire);
115  }
116 
117  mutable Mutex mutex_;
118  std::atomic<bool> notified_yet_; // written under mutex_
119 };
120 
122 } // namespace absl
123 
124 #endif // ABSL_SYNCHRONIZATION_NOTIFICATION_H_
absl::Notification::notified_yet_
std::atomic< bool > notified_yet_
Definition: abseil-cpp/absl/synchronization/notification.h:118
absl::Time
Definition: third_party/abseil-cpp/absl/time/time.h:642
false
#define false
Definition: setup_once.h:323
absl::Mutex
Definition: abseil-cpp/absl/synchronization/mutex.h:131
absl::Notification::mutex_
Mutex mutex_
Definition: abseil-cpp/absl/synchronization/notification.h:117
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::Notification
Definition: abseil-cpp/absl/synchronization/notification.h:66
ABSL_MUST_USE_RESULT
#define ABSL_MUST_USE_RESULT
Definition: abseil-cpp/absl/base/attributes.h:441
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::Notification::Notify
void Notify()
Definition: abseil-cpp/absl/synchronization/notification.cc:27
absl::Notification::WaitForNotificationWithDeadline
bool WaitForNotificationWithDeadline(absl::Time deadline) const
Definition: abseil-cpp/absl/synchronization/notification.cc:67
absl::Notification::HasBeenNotifiedInternal
static bool HasBeenNotifiedInternal(const std::atomic< bool > *notified_yet)
Definition: abseil-cpp/absl/synchronization/notification.h:112
absl::Notification::HasBeenNotified
ABSL_MUST_USE_RESULT bool HasBeenNotified() const
Definition: abseil-cpp/absl/synchronization/notification.h:78
absl::Duration
Definition: third_party/abseil-cpp/absl/time/time.h:159
absl::Notification::operator=
Notification & operator=(const Notification &)=delete
absl::Notification::~Notification
~Notification()
Definition: abseil-cpp/absl/synchronization/notification.cc:42
absl::Notification::Notification
Notification(bool prenotify)
Definition: abseil-cpp/absl/synchronization/notification.h:70
absl::Notification::Notification
Notification()
Definition: abseil-cpp/absl/synchronization/notification.h:69
absl::Notification::WaitForNotificationWithTimeout
bool WaitForNotificationWithTimeout(absl::Duration timeout) const
Definition: abseil-cpp/absl/synchronization/notification.cc:56
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
absl::Notification::WaitForNotification
void WaitForNotification() const
Definition: abseil-cpp/absl/synchronization/notification.cc:48


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:32