notification.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef ABSL_SYNCHRONIZATION_NOTIFICATION_H_
  50. #define ABSL_SYNCHRONIZATION_NOTIFICATION_H_
  51. #include <atomic>
  52. #include "absl/base/attributes.h"
  53. #include "absl/synchronization/mutex.h"
  54. #include "absl/time/time.h"
  55. namespace absl {
  56. ABSL_NAMESPACE_BEGIN
  57. // -----------------------------------------------------------------------------
  58. // Notification
  59. // -----------------------------------------------------------------------------
  60. class Notification {
  61. public:
  62. // Initializes the "notified" state to unnotified.
  63. Notification() : notified_yet_(false) {}
  64. explicit Notification(bool prenotify) : notified_yet_(prenotify) {}
  65. Notification(const Notification&) = delete;
  66. Notification& operator=(const Notification&) = delete;
  67. ~Notification();
  68. // Notification::HasBeenNotified()
  69. //
  70. // Returns the value of the notification's internal "notified" state.
  71. ABSL_MUST_USE_RESULT bool HasBeenNotified() const {
  72. return HasBeenNotifiedInternal(&this->notified_yet_);
  73. }
  74. // Notification::WaitForNotification()
  75. //
  76. // Blocks the calling thread until the notification's "notified" state is
  77. // `true`. Note that if `Notify()` has been previously called on this
  78. // notification, this function will immediately return.
  79. void WaitForNotification() const;
  80. // Notification::WaitForNotificationWithTimeout()
  81. //
  82. // Blocks until either the notification's "notified" state is `true` (which
  83. // may occur immediately) or the timeout has elapsed, returning the value of
  84. // its "notified" state in either case.
  85. bool WaitForNotificationWithTimeout(absl::Duration timeout) const;
  86. // Notification::WaitForNotificationWithDeadline()
  87. //
  88. // Blocks until either the notification's "notified" state is `true` (which
  89. // may occur immediately) or the deadline has expired, returning the value of
  90. // its "notified" state in either case.
  91. bool WaitForNotificationWithDeadline(absl::Time deadline) const;
  92. // Notification::Notify()
  93. //
  94. // Sets the "notified" state of this notification to `true` and wakes waiting
  95. // threads. Note: do not call `Notify()` multiple times on the same
  96. // `Notification`; calling `Notify()` more than once on the same notification
  97. // results in undefined behavior.
  98. void Notify();
  99. private:
  100. static inline bool HasBeenNotifiedInternal(
  101. const std::atomic<bool>* notified_yet) {
  102. return notified_yet->load(std::memory_order_acquire);
  103. }
  104. mutable Mutex mutex_;
  105. std::atomic<bool> notified_yet_; // written under mutex_
  106. };
  107. ABSL_NAMESPACE_END
  108. } // namespace absl
  109. #endif // ABSL_SYNCHRONIZATION_NOTIFICATION_H_