waitable_event.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_
  5. #define BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_
  6. #include <stddef.h>
  7. #include "base/base_export.h"
  8. #include "base/macros.h"
  9. #include "build/build_config.h"
  10. #if defined(OS_WIN)
  11. #include "base/win/scoped_handle.h"
  12. #elif defined(OS_APPLE)
  13. #include <mach/mach.h>
  14. #include <list>
  15. #include <memory>
  16. #include "base/callback_forward.h"
  17. #include "base/mac/scoped_mach_port.h"
  18. #include "base/memory/ref_counted.h"
  19. #include "base/synchronization/lock.h"
  20. #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
  21. #include <list>
  22. #include <utility>
  23. #include "base/memory/ref_counted.h"
  24. #include "base/synchronization/lock.h"
  25. #endif
  26. namespace base {
  27. class TimeDelta;
  28. // A WaitableEvent can be a useful thread synchronization tool when you want to
  29. // allow one thread to wait for another thread to finish some work. For
  30. // non-Windows systems, this can only be used from within a single address
  31. // space.
  32. //
  33. // Use a WaitableEvent when you would otherwise use a Lock+ConditionVariable to
  34. // protect a simple boolean value. However, if you find yourself using a
  35. // WaitableEvent in conjunction with a Lock to wait for a more complex state
  36. // change (e.g., for an item to be added to a queue), then you should probably
  37. // be using a ConditionVariable instead of a WaitableEvent.
  38. //
  39. // NOTE: On Windows, this class provides a subset of the functionality afforded
  40. // by a Windows event object. This is intentional. If you are writing Windows
  41. // specific code and you need other features of a Windows event, then you might
  42. // be better off just using an Windows event directly.
  43. class BASE_EXPORT WaitableEvent {
  44. public:
  45. // Indicates whether a WaitableEvent should automatically reset the event
  46. // state after a single waiting thread has been released or remain signaled
  47. // until Reset() is manually invoked.
  48. enum class ResetPolicy { MANUAL, AUTOMATIC };
  49. // Indicates whether a new WaitableEvent should start in a signaled state or
  50. // not.
  51. enum class InitialState { SIGNALED, NOT_SIGNALED };
  52. // Constructs a WaitableEvent with policy and initial state as detailed in
  53. // the above enums.
  54. WaitableEvent(ResetPolicy reset_policy = ResetPolicy::MANUAL,
  55. InitialState initial_state = InitialState::NOT_SIGNALED);
  56. #if defined(OS_WIN)
  57. // Create a WaitableEvent from an Event HANDLE which has already been
  58. // created. This objects takes ownership of the HANDLE and will close it when
  59. // deleted.
  60. explicit WaitableEvent(win::ScopedHandle event_handle);
  61. #endif
  62. ~WaitableEvent();
  63. // Put the event in the un-signaled state.
  64. void Reset();
  65. // Put the event in the signaled state. Causing any thread blocked on Wait
  66. // to be woken up.
  67. void Signal();
  68. // Returns true if the event is in the signaled state, else false. If this
  69. // is not a manual reset event, then this test will cause a reset.
  70. bool IsSignaled();
  71. // Wait indefinitely for the event to be signaled. Wait's return "happens
  72. // after" |Signal| has completed. This means that it's safe for a
  73. // WaitableEvent to synchronise its own destruction, like this:
  74. //
  75. // WaitableEvent *e = new WaitableEvent;
  76. // SendToOtherThread(e);
  77. // e->Wait();
  78. // delete e;
  79. void Wait();
  80. // Wait up until wait_delta has passed for the event to be signaled
  81. // (real-time; ignores time overrides). Returns true if the event was
  82. // signaled. Handles spurious wakeups and guarantees that |wait_delta| will
  83. // have elapsed if this returns false.
  84. //
  85. // TimedWait can synchronise its own destruction like |Wait|.
  86. bool TimedWait(const TimeDelta& wait_delta);
  87. #if defined(OS_WIN)
  88. HANDLE handle() const { return handle_.Get(); }
  89. #endif
  90. // Declares that this WaitableEvent will only ever be used by a thread that is
  91. // idle at the bottom of its stack and waiting for work (in particular, it is
  92. // not synchronously waiting on this event before resuming ongoing work). This
  93. // is useful to avoid telling base-internals that this thread is "blocked"
  94. // when it's merely idle and ready to do work. As such, this is only expected
  95. // to be used by thread and thread pool impls.
  96. void declare_only_used_while_idle() { waiting_is_blocking_ = false; }
  97. // Wait, synchronously, on multiple events.
  98. // waitables: an array of WaitableEvent pointers
  99. // count: the number of elements in @waitables
  100. //
  101. // returns: the index of a WaitableEvent which has been signaled.
  102. //
  103. // You MUST NOT delete any of the WaitableEvent objects while this wait is
  104. // happening, however WaitMany's return "happens after" the |Signal| call
  105. // that caused it has completed, like |Wait|.
  106. //
  107. // If more than one WaitableEvent is signaled to unblock WaitMany, the lowest
  108. // index among them is returned.
  109. static size_t WaitMany(WaitableEvent** waitables, size_t count);
  110. // For asynchronous waiting, see WaitableEventWatcher
  111. // This is a private helper class. It's here because it's used by friends of
  112. // this class (such as WaitableEventWatcher) to be able to enqueue elements
  113. // of the wait-list
  114. class Waiter {
  115. public:
  116. // Signal the waiter to wake up.
  117. //
  118. // Consider the case of a Waiter which is in multiple WaitableEvent's
  119. // wait-lists. Each WaitableEvent is automatic-reset and two of them are
  120. // signaled at the same time. Now, each will wake only the first waiter in
  121. // the wake-list before resetting. However, if those two waiters happen to
  122. // be the same object (as can happen if another thread didn't have a chance
  123. // to dequeue the waiter from the other wait-list in time), two auto-resets
  124. // will have happened, but only one waiter has been signaled!
  125. //
  126. // Because of this, a Waiter may "reject" a wake by returning false. In
  127. // this case, the auto-reset WaitableEvent shouldn't act as if anything has
  128. // been notified.
  129. virtual bool Fire(WaitableEvent* signaling_event) = 0;
  130. // Waiters may implement this in order to provide an extra condition for
  131. // two Waiters to be considered equal. In WaitableEvent::Dequeue, if the
  132. // pointers match then this function is called as a final check. See the
  133. // comments in ~Handle for why.
  134. virtual bool Compare(void* tag) = 0;
  135. protected:
  136. virtual ~Waiter() = default;
  137. };
  138. private:
  139. friend class WaitableEventWatcher;
  140. #if defined(OS_WIN)
  141. win::ScopedHandle handle_;
  142. #elif defined(OS_APPLE)
  143. // Prior to macOS 10.12, a TYPE_MACH_RECV dispatch source may not be invoked
  144. // immediately. If a WaitableEventWatcher is used on a manual-reset event,
  145. // and another thread that is Wait()ing on the event calls Reset()
  146. // immediately after waking up, the watcher may not receive the callback.
  147. // On macOS 10.12 and higher, dispatch delivery is reliable. But for OSes
  148. // prior, a lock-protected list of callbacks is used for manual-reset event
  149. // watchers. Automatic-reset events are not prone to this issue, since the
  150. // first thread to wake will claim the event.
  151. static bool UseSlowWatchList(ResetPolicy policy);
  152. // Peeks the message queue named by |port| and returns true if a message
  153. // is present and false if not. If |dequeue| is true, the messsage will be
  154. // drained from the queue. If |dequeue| is false, the queue will only be
  155. // peeked. |port| must be a receive right.
  156. static bool PeekPort(mach_port_t port, bool dequeue);
  157. // The Mach receive right is waited on by both WaitableEvent and
  158. // WaitableEventWatcher. It is valid to signal and then delete an event, and
  159. // a watcher should still be notified. If the right were to be destroyed
  160. // immediately, the watcher would not receive the signal. Because Mach
  161. // receive rights cannot have a user refcount greater than one, the right
  162. // must be reference-counted manually.
  163. class ReceiveRight : public RefCountedThreadSafe<ReceiveRight> {
  164. public:
  165. ReceiveRight(mach_port_t name, bool create_slow_watch_list);
  166. mach_port_t Name() const { return right_.get(); }
  167. // This structure is used iff UseSlowWatchList() is true. See the comment
  168. // in Signal() for details.
  169. struct WatchList {
  170. WatchList();
  171. ~WatchList();
  172. // The lock protects a list of closures to be run when the event is
  173. // Signal()ed. The closures are invoked on the signaling thread, so they
  174. // must be safe to be called from any thread.
  175. Lock lock;
  176. std::list<OnceClosure> list;
  177. };
  178. WatchList* SlowWatchList() const { return slow_watch_list_.get(); }
  179. private:
  180. friend class RefCountedThreadSafe<ReceiveRight>;
  181. ~ReceiveRight();
  182. mac::ScopedMachReceiveRight right_;
  183. // This is allocated iff UseSlowWatchList() is true. It is created on the
  184. // heap to avoid performing initialization when not using the slow path.
  185. std::unique_ptr<WatchList> slow_watch_list_;
  186. DISALLOW_COPY_AND_ASSIGN(ReceiveRight);
  187. };
  188. const ResetPolicy policy_;
  189. // The receive right for the event.
  190. scoped_refptr<ReceiveRight> receive_right_;
  191. // The send right used to signal the event. This can be disposed of with
  192. // the event, unlike the receive right, since a deleted event cannot be
  193. // signaled.
  194. mac::ScopedMachSendRight send_right_;
  195. #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
  196. // On Windows, you must not close a HANDLE which is currently being waited on.
  197. // The MSDN documentation says that the resulting behaviour is 'undefined'.
  198. // To solve that issue each WaitableEventWatcher duplicates the given event
  199. // handle.
  200. // However, if we were to include the following members
  201. // directly then, on POSIX, one couldn't use WaitableEventWatcher to watch an
  202. // event which gets deleted. This mismatch has bitten us several times now,
  203. // so we have a kernel of the WaitableEvent, which is reference counted.
  204. // WaitableEventWatchers may then take a reference and thus match the Windows
  205. // behaviour.
  206. struct WaitableEventKernel :
  207. public RefCountedThreadSafe<WaitableEventKernel> {
  208. public:
  209. WaitableEventKernel(ResetPolicy reset_policy, InitialState initial_state);
  210. bool Dequeue(Waiter* waiter, void* tag);
  211. base::Lock lock_;
  212. const bool manual_reset_;
  213. bool signaled_;
  214. std::list<Waiter*> waiters_;
  215. private:
  216. friend class RefCountedThreadSafe<WaitableEventKernel>;
  217. ~WaitableEventKernel();
  218. };
  219. typedef std::pair<WaitableEvent*, size_t> WaiterAndIndex;
  220. // When dealing with arrays of WaitableEvent*, we want to sort by the address
  221. // of the WaitableEvent in order to have a globally consistent locking order.
  222. // In that case we keep them, in sorted order, in an array of pairs where the
  223. // second element is the index of the WaitableEvent in the original,
  224. // unsorted, array.
  225. static size_t EnqueueMany(WaiterAndIndex* waitables,
  226. size_t count, Waiter* waiter);
  227. bool SignalAll();
  228. bool SignalOne();
  229. void Enqueue(Waiter* waiter);
  230. scoped_refptr<WaitableEventKernel> kernel_;
  231. #endif
  232. // Whether a thread invoking Wait() on this WaitableEvent should be considered
  233. // blocked as opposed to idle (and potentially replaced if part of a pool).
  234. bool waiting_is_blocking_ = true;
  235. DISALLOW_COPY_AND_ASSIGN(WaitableEvent);
  236. };
  237. } // namespace base
  238. #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_