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_WATCHER_H_
6 #define BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_
8 #include "base/base_export.h"
9 #include "build/build_config.h"
12 #include "base/win/object_watcher.h"
14 #include "base/callback.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/synchronization/waitable_event.h"
23 class AsyncCallbackTask
;
26 // This class provides a way to wait on a WaitableEvent asynchronously.
28 // Each instance of this object can be waiting on a single WaitableEvent. When
29 // the waitable event is signaled, a callback is made in the thread of a given
30 // MessageLoop. This callback can be deleted by deleting the waiter.
36 // void DoStuffWhenSignaled(WaitableEvent *waitable_event) {
37 // watcher_.StartWatching(waitable_event,
38 // base::Bind(&MyClass::OnWaitableEventSignaled, this);
41 // void OnWaitableEventSignaled(WaitableEvent* waitable_event) {
42 // // OK, time to do stuff!
44 // base::WaitableEventWatcher watcher_;
47 // In the above example, MyClass wants to "do stuff" when waitable_event
48 // becomes signaled. WaitableEventWatcher makes this task easy. When MyClass
49 // goes out of scope, the watcher_ will be destroyed, and there is no need to
50 // worry about OnWaitableEventSignaled being called on a deleted MyClass
53 // BEWARE: With automatically reset WaitableEvents, a signal may be lost if it
54 // occurs just before a WaitableEventWatcher is deleted. There is currently no
55 // safe way to stop watching an automatic reset WaitableEvent without possibly
58 // NOTE: you /are/ allowed to delete the WaitableEvent while still waiting on
59 // it with a Watcher. It will act as if the event was never signaled.
61 class BASE_EXPORT WaitableEventWatcher
63 : public win::ObjectWatcher::Delegate
{
65 : public MessageLoop::DestructionObserver
{
68 typedef Callback
<void(WaitableEvent
*)> EventCallback
;
69 WaitableEventWatcher();
70 ~WaitableEventWatcher() override
;
72 // When @event is signaled, the given callback is called on the thread of the
73 // current message loop when StartWatching is called.
74 bool StartWatching(WaitableEvent
* event
, const EventCallback
& callback
);
76 // Cancel the current watch. Must be called from the same thread which
79 // Does nothing if no event is being watched, nor if the watch has completed.
80 // The callback will *not* be called for the current watch after this
81 // function returns. Since the callback runs on the same thread as this
82 // function, it cannot be called during this function either.
85 // Return the currently watched event, or NULL if no object is currently being
87 WaitableEvent
* GetWatchedEvent();
89 // Return the callback that will be invoked when the event is
91 const EventCallback
& callback() const { return callback_
; }
95 virtual void OnObjectSignaled(HANDLE h
) override
;
96 win::ObjectWatcher watcher_
;
98 // Implementation of MessageLoop::DestructionObserver
99 void WillDestroyCurrentMessageLoop() override
;
101 MessageLoop
* message_loop_
;
102 scoped_refptr
<Flag
> cancel_flag_
;
103 AsyncWaiter
* waiter_
;
104 base::Closure internal_callback_
;
105 scoped_refptr
<WaitableEvent::WaitableEventKernel
> kernel_
;
108 WaitableEvent
* event_
;
109 EventCallback callback_
;
114 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_