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.
5 #ifndef BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_
6 #define BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_
8 #include "base/base_export.h"
9 #include "base/basictypes.h"
12 #include "base/win/scoped_handle.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/synchronization/lock.h"
26 // A WaitableEvent can be a useful thread synchronization tool when you want to
27 // allow one thread to wait for another thread to finish some work. For
28 // non-Windows systems, this can only be used from within a single address
31 // Use a WaitableEvent when you would otherwise use a Lock+ConditionVariable to
32 // protect a simple boolean value. However, if you find yourself using a
33 // WaitableEvent in conjunction with a Lock to wait for a more complex state
34 // change (e.g., for an item to be added to a queue), then you should probably
35 // be using a ConditionVariable instead of a WaitableEvent.
37 // NOTE: On Windows, this class provides a subset of the functionality afforded
38 // by a Windows event object. This is intentional. If you are writing Windows
39 // specific code and you need other features of a Windows event, then you might
40 // be better off just using an Windows event directly.
41 class BASE_EXPORT WaitableEvent
{
43 // If manual_reset is true, then to set the event state to non-signaled, a
44 // consumer must call the Reset method. If this parameter is false, then the
45 // system automatically resets the event state to non-signaled after a single
46 // waiting thread has been released.
47 WaitableEvent(bool manual_reset
, bool initially_signaled
);
50 // Create a WaitableEvent from an Event HANDLE which has already been
51 // created. This objects takes ownership of the HANDLE and will close it when
53 explicit WaitableEvent(win::ScopedHandle event_handle
);
58 // Put the event in the un-signaled state.
61 // Put the event in the signaled state. Causing any thread blocked on Wait
65 // Returns true if the event is in the signaled state, else false. If this
66 // is not a manual reset event, then this test will cause a reset.
69 // Wait indefinitely for the event to be signaled. Wait's return "happens
70 // after" |Signal| has completed. This means that it's safe for a
71 // WaitableEvent to synchronise its own destruction, like this:
73 // WaitableEvent *e = new WaitableEvent;
74 // SendToOtherThread(e);
79 // Wait up until max_time has passed for the event to be signaled. Returns
80 // true if the event was signaled. If this method returns false, then it
81 // does not necessarily mean that max_time was exceeded.
83 // TimedWait can synchronise its own destruction like |Wait|.
84 bool TimedWait(const TimeDelta
& max_time
);
87 HANDLE
handle() const { return handle_
.Get(); }
90 // Wait, synchronously, on multiple events.
91 // waitables: an array of WaitableEvent pointers
92 // count: the number of elements in @waitables
94 // returns: the index of a WaitableEvent which has been signaled.
96 // You MUST NOT delete any of the WaitableEvent objects while this wait is
97 // happening, however WaitMany's return "happens after" the |Signal| call
98 // that caused it has completed, like |Wait|.
99 static size_t WaitMany(WaitableEvent
** waitables
, size_t count
);
101 // For asynchronous waiting, see WaitableEventWatcher
103 // This is a private helper class. It's here because it's used by friends of
104 // this class (such as WaitableEventWatcher) to be able to enqueue elements
108 // Signal the waiter to wake up.
110 // Consider the case of a Waiter which is in multiple WaitableEvent's
111 // wait-lists. Each WaitableEvent is automatic-reset and two of them are
112 // signaled at the same time. Now, each will wake only the first waiter in
113 // the wake-list before resetting. However, if those two waiters happen to
114 // be the same object (as can happen if another thread didn't have a chance
115 // to dequeue the waiter from the other wait-list in time), two auto-resets
116 // will have happened, but only one waiter has been signaled!
118 // Because of this, a Waiter may "reject" a wake by returning false. In
119 // this case, the auto-reset WaitableEvent shouldn't act as if anything has
121 virtual bool Fire(WaitableEvent
* signaling_event
) = 0;
123 // Waiters may implement this in order to provide an extra condition for
124 // two Waiters to be considered equal. In WaitableEvent::Dequeue, if the
125 // pointers match then this function is called as a final check. See the
126 // comments in ~Handle for why.
127 virtual bool Compare(void* tag
) = 0;
134 friend class WaitableEventWatcher
;
137 win::ScopedHandle handle_
;
139 // On Windows, one can close a HANDLE which is currently being waited on. The
140 // MSDN documentation says that the resulting behaviour is 'undefined', but
141 // it doesn't crash. However, if we were to include the following members
142 // directly then, on POSIX, one couldn't use WaitableEventWatcher to watch an
143 // event which gets deleted. This mismatch has bitten us several times now,
144 // so we have a kernel of the WaitableEvent, which is reference counted.
145 // WaitableEventWatchers may then take a reference and thus match the Windows
147 struct WaitableEventKernel
:
148 public RefCountedThreadSafe
<WaitableEventKernel
> {
150 WaitableEventKernel(bool manual_reset
, bool initially_signaled
);
152 bool Dequeue(Waiter
* waiter
, void* tag
);
155 const bool manual_reset_
;
157 std::list
<Waiter
*> waiters_
;
160 friend class RefCountedThreadSafe
<WaitableEventKernel
>;
161 ~WaitableEventKernel();
164 typedef std::pair
<WaitableEvent
*, size_t> WaiterAndIndex
;
166 // When dealing with arrays of WaitableEvent*, we want to sort by the address
167 // of the WaitableEvent in order to have a globally consistent locking order.
168 // In that case we keep them, in sorted order, in an array of pairs where the
169 // second element is the index of the WaitableEvent in the original,
171 static size_t EnqueueMany(WaiterAndIndex
* waitables
,
172 size_t count
, Waiter
* waiter
);
176 void Enqueue(Waiter
* waiter
);
178 scoped_refptr
<WaitableEventKernel
> kernel_
;
181 DISALLOW_COPY_AND_ASSIGN(WaitableEvent
);
186 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_