OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 5 #ifndef BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
6 #define BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 6 #define BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 | 10 |
11 #if defined(OS_WIN) | 11 #if defined(OS_WIN) |
12 #include "base/win/object_watcher.h" | 12 #include "base/win/object_watcher.h" |
13 #else | 13 #else |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
17 #endif | 17 #endif |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 | 20 |
21 class Flag; | 21 class Flag; |
22 class AsyncWaiter; | 22 class AsyncWaiter; |
23 class AsyncCallbackTask; | 23 class AsyncCallbackTask; |
24 class WaitableEvent; | 24 class WaitableEvent; |
25 | 25 |
| 26 // ----------------------------------------------------------------------------- |
26 // This class provides a way to wait on a WaitableEvent asynchronously. | 27 // This class provides a way to wait on a WaitableEvent asynchronously. |
27 // | 28 // |
28 // Each instance of this object can be waiting on a single WaitableEvent. When | 29 // 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 // 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. | 31 // MessageLoop. This callback can be deleted by deleting the waiter. |
31 // | 32 // |
32 // Typical usage: | 33 // Typical usage: |
33 // | 34 // |
34 // class MyClass { | 35 // class MyClass : public base::WaitableEventWatcher::Delegate { |
35 // public: | 36 // public: |
36 // void DoStuffWhenSignaled(WaitableEvent *waitable_event) { | 37 // void DoStuffWhenSignaled(WaitableEvent *waitable_event) { |
37 // watcher_.StartWatching(waitable_event, | 38 // watcher_.StartWatching(waitable_event, this); |
38 // base::Bind(&MyClass::OnWaitableEventSignaled, this); | |
39 // } | 39 // } |
40 // void OnWaitableEventSignaled(WaitableEvent* waitable_event) { | 40 // virtual void OnWaitableEventSignaled(WaitableEvent* waitable_event) { |
41 // // OK, time to do stuff! | 41 // // OK, time to do stuff! |
42 // } | 42 // } |
43 // private: | 43 // private: |
44 // base::WaitableEventWatcher watcher_; | 44 // base::WaitableEventWatcher watcher_; |
45 // }; | 45 // }; |
46 // | 46 // |
47 // In the above example, MyClass wants to "do stuff" when waitable_event | 47 // In the above example, MyClass wants to "do stuff" when waitable_event |
48 // becomes signaled. WaitableEventWatcher makes this task easy. When MyClass | 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 | 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 | 50 // worry about OnWaitableEventSignaled being called on a deleted MyClass |
51 // pointer. | 51 // pointer. |
52 // | 52 // |
53 // BEWARE: With automatically reset WaitableEvents, a signal may be lost if it | 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 | 54 // occurs just before a WaitableEventWatcher is deleted. There is currently no |
55 // safe way to stop watching an automatic reset WaitableEvent without possibly | 55 // safe way to stop watching an automatic reset WaitableEvent without possibly |
56 // missing a signal. | 56 // missing a signal. |
57 // | 57 // |
58 // NOTE: you /are/ allowed to delete the WaitableEvent while still waiting on | 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. | 59 // it with a Watcher. It will act as if the event was never signaled. |
| 60 // ----------------------------------------------------------------------------- |
60 | 61 |
61 class BASE_EXPORT WaitableEventWatcher | 62 class BASE_EXPORT WaitableEventWatcher |
62 #if defined(OS_WIN) | 63 #if !defined(OS_WIN) |
63 : public win::ObjectWatcher::Delegate { | 64 : public MessageLoop::DestructionObserver |
64 #else | |
65 : public MessageLoop::DestructionObserver { | |
66 #endif | 65 #endif |
| 66 { |
67 public: | 67 public: |
68 typedef Callback<void(WaitableEvent*)> EventCallback; | 68 |
69 WaitableEventWatcher(); | 69 WaitableEventWatcher(); |
70 virtual ~WaitableEventWatcher(); | 70 virtual ~WaitableEventWatcher(); |
71 | 71 |
72 // When @event is signaled, the given callback is called on the thread of the | 72 class BASE_EXPORT Delegate { |
73 // current message loop when StartWatching is called. | 73 public: |
74 bool StartWatching(WaitableEvent* event, const EventCallback& callback); | 74 // ------------------------------------------------------------------------- |
| 75 // This is called on the MessageLoop thread when WaitableEvent has been |
| 76 // signaled. |
| 77 // |
| 78 // Note: the event may not be signaled by the time that this function is |
| 79 // called. This indicates only that it has been signaled at some point in |
| 80 // the past. |
| 81 // ------------------------------------------------------------------------- |
| 82 virtual void OnWaitableEventSignaled(WaitableEvent* waitable_event) = 0; |
75 | 83 |
| 84 protected: |
| 85 virtual ~Delegate() { } |
| 86 }; |
| 87 |
| 88 // --------------------------------------------------------------------------- |
| 89 // When @event is signaled, the given delegate is called on the thread of the |
| 90 // current message loop when StartWatching is called. The delegate is not |
| 91 // deleted. |
| 92 // --------------------------------------------------------------------------- |
| 93 bool StartWatching(WaitableEvent* event, Delegate* delegate); |
| 94 |
| 95 // --------------------------------------------------------------------------- |
76 // Cancel the current watch. Must be called from the same thread which | 96 // Cancel the current watch. Must be called from the same thread which |
77 // started the watch. | 97 // started the watch. |
78 // | 98 // |
79 // Does nothing if no event is being watched, nor if the watch has completed. | 99 // 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 | 100 // The delegate will *not* be called for the current watch after this |
81 // function returns. Since the callback runs on the same thread as this | 101 // function returns. Since the delegate runs on the same thread as this |
82 // function, it cannot be called during this function either. | 102 // function, it cannot be called during this function either. |
| 103 // --------------------------------------------------------------------------- |
83 void StopWatching(); | 104 void StopWatching(); |
84 | 105 |
| 106 // --------------------------------------------------------------------------- |
85 // Return the currently watched event, or NULL if no object is currently being | 107 // Return the currently watched event, or NULL if no object is currently being |
86 // watched. | 108 // watched. |
| 109 // --------------------------------------------------------------------------- |
87 WaitableEvent* GetWatchedEvent(); | 110 WaitableEvent* GetWatchedEvent(); |
88 | 111 |
89 // Return the callback that will be invoked when the event is | 112 // --------------------------------------------------------------------------- |
90 // signaled. | 113 // Return the delegate, or NULL if there is no delegate. |
91 const EventCallback& callback() const { return callback_; } | 114 // --------------------------------------------------------------------------- |
| 115 Delegate* delegate() { |
| 116 return delegate_; |
| 117 } |
92 | 118 |
93 private: | 119 private: |
94 #if defined(OS_WIN) | 120 #if defined(OS_WIN) |
95 virtual void OnObjectSignaled(HANDLE h) OVERRIDE; | 121 // --------------------------------------------------------------------------- |
| 122 // The helper class exists because, if WaitableEventWatcher were to inherit |
| 123 // from ObjectWatcher::Delegate, then it couldn't also have an inner class |
| 124 // called Delegate (at least on Windows). Thus this object exists to proxy |
| 125 // the callback function |
| 126 // --------------------------------------------------------------------------- |
| 127 class ObjectWatcherHelper : public win::ObjectWatcher::Delegate { |
| 128 public: |
| 129 ObjectWatcherHelper(WaitableEventWatcher* watcher); |
| 130 |
| 131 // ------------------------------------------------------------------------- |
| 132 // Implementation of ObjectWatcher::Delegate |
| 133 // ------------------------------------------------------------------------- |
| 134 void OnObjectSignaled(HANDLE h); |
| 135 |
| 136 private: |
| 137 WaitableEventWatcher *const watcher_; |
| 138 }; |
| 139 |
| 140 void OnObjectSignaled(); |
| 141 |
| 142 ObjectWatcherHelper helper_; |
96 win::ObjectWatcher watcher_; | 143 win::ObjectWatcher watcher_; |
97 #else | 144 #else |
| 145 // --------------------------------------------------------------------------- |
98 // Implementation of MessageLoop::DestructionObserver | 146 // Implementation of MessageLoop::DestructionObserver |
| 147 // --------------------------------------------------------------------------- |
99 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 148 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
100 | 149 |
101 MessageLoop* message_loop_; | 150 MessageLoop* message_loop_; |
102 scoped_refptr<Flag> cancel_flag_; | 151 scoped_refptr<Flag> cancel_flag_; |
103 AsyncWaiter* waiter_; | 152 AsyncWaiter* waiter_; |
104 base::Closure internal_callback_; | 153 base::Closure callback_; |
105 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; | 154 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; |
106 #endif | 155 #endif |
107 | 156 |
108 WaitableEvent* event_; | 157 WaitableEvent* event_; |
109 EventCallback callback_; | 158 |
| 159 Delegate* delegate_; |
110 }; | 160 }; |
111 | 161 |
112 } // namespace base | 162 } // namespace base |
113 | 163 |
114 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 164 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
OLD | NEW |