Chromium Code Reviews| 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 typedef Callback<void(WaitableEvent*)> WaitableEventCallback; | |
|
brettw
2013/01/28 20:56:59
I think the typedef is fine. I usually try to keep
teravest
2013/01/28 21:25:09
Done.
| |
| 27 | |
| 26 // ----------------------------------------------------------------------------- | 28 // ----------------------------------------------------------------------------- |
| 27 // This class provides a way to wait on a WaitableEvent asynchronously. | 29 // This class provides a way to wait on a WaitableEvent asynchronously. |
| 28 // | 30 // |
| 29 // Each instance of this object can be waiting on a single WaitableEvent. When | 31 // Each instance of this object can be waiting on a single WaitableEvent. When |
| 30 // the waitable event is signaled, a callback is made in the thread of a given | 32 // the waitable event is signaled, a callback is made in the thread of a given |
| 31 // MessageLoop. This callback can be deleted by deleting the waiter. | 33 // MessageLoop. This callback can be deleted by deleting the waiter. |
| 32 // | 34 // |
| 33 // Typical usage: | 35 // Typical usage: |
| 34 // | 36 // |
| 35 // class MyClass : public base::WaitableEventWatcher::Delegate { | 37 // class MyClass { |
| 36 // public: | 38 // public: |
| 37 // void DoStuffWhenSignaled(WaitableEvent *waitable_event) { | 39 // void DoStuffWhenSignaled(WaitableEvent *waitable_event) { |
| 38 // watcher_.StartWatching(waitable_event, this); | 40 // base::WaitableEventCallback watcher_callback = |
| 41 // base::Bind(&MyClass::OnWaitableEventSignaled, this); | |
| 42 // watcher_.StartWatching(waitable_event, watcher_callback); | |
| 39 // } | 43 // } |
| 40 // virtual void OnWaitableEventSignaled(WaitableEvent* waitable_event) { | 44 // void OnWaitableEventSignaled(WaitableEvent* waitable_event) { |
| 41 // // OK, time to do stuff! | 45 // // OK, time to do stuff! |
| 42 // } | 46 // } |
| 43 // private: | 47 // private: |
| 44 // base::WaitableEventWatcher watcher_; | 48 // base::WaitableEventWatcher watcher_; |
| 45 // }; | 49 // }; |
| 46 // | 50 // |
| 47 // In the above example, MyClass wants to "do stuff" when waitable_event | 51 // In the above example, MyClass wants to "do stuff" when waitable_event |
| 48 // becomes signaled. WaitableEventWatcher makes this task easy. When MyClass | 52 // 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 | 53 // 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 | 54 // worry about OnWaitableEventSignaled being called on a deleted MyClass |
| 51 // pointer. | 55 // pointer. |
| 52 // | 56 // |
| 53 // BEWARE: With automatically reset WaitableEvents, a signal may be lost if it | 57 // BEWARE: With automatically reset WaitableEvents, a signal may be lost if it |
| 54 // occurs just before a WaitableEventWatcher is deleted. There is currently no | 58 // occurs just before a WaitableEventWatcher is deleted. There is currently no |
| 55 // safe way to stop watching an automatic reset WaitableEvent without possibly | 59 // safe way to stop watching an automatic reset WaitableEvent without possibly |
| 56 // missing a signal. | 60 // missing a signal. |
| 57 // | 61 // |
| 58 // NOTE: you /are/ allowed to delete the WaitableEvent while still waiting on | 62 // 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. | 63 // it with a Watcher. It will act as if the event was never signaled. |
| 60 // ----------------------------------------------------------------------------- | 64 // ----------------------------------------------------------------------------- |
| 61 | 65 |
| 62 class BASE_EXPORT WaitableEventWatcher | 66 class BASE_EXPORT WaitableEventWatcher |
| 63 #if !defined(OS_WIN) | 67 #if defined(OS_WIN) |
|
tfarina
2013/01/28 21:06:10
for a possible future CL. Just an idea. We should
dmichael (off chromium)
2013/01/28 21:47:27
That's what we have, but the header is shared. Tha
| |
| 64 : public MessageLoop::DestructionObserver | 68 : public win::ObjectWatcher::Delegate { |
| 69 #else | |
| 70 : public MessageLoop::DestructionObserver { | |
| 65 #endif | 71 #endif |
| 66 { | |
| 67 public: | 72 public: |
| 68 | 73 |
| 69 WaitableEventWatcher(); | 74 WaitableEventWatcher(); |
| 70 virtual ~WaitableEventWatcher(); | 75 virtual ~WaitableEventWatcher(); |
| 71 | 76 |
| 72 class BASE_EXPORT Delegate { | |
| 73 public: | |
| 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; | |
| 83 | |
| 84 protected: | |
| 85 virtual ~Delegate() { } | |
| 86 }; | |
| 87 | |
| 88 // --------------------------------------------------------------------------- | 77 // --------------------------------------------------------------------------- |
|
brettw
2013/01/28 20:56:59
Whoever wrote this kind of went crazy with the hor
teravest
2013/01/28 21:25:09
Done.
| |
| 89 // When @event is signaled, the given delegate is called on the thread of the | 78 // When @event is signaled, the given callback is called on the thread of the |
| 90 // current message loop when StartWatching is called. The delegate is not | 79 // current message loop when StartWatching is called. |
| 91 // deleted. | |
| 92 // --------------------------------------------------------------------------- | 80 // --------------------------------------------------------------------------- |
| 93 bool StartWatching(WaitableEvent* event, Delegate* delegate); | 81 bool StartWatching(WaitableEvent* event, |
| 82 const WaitableEventCallback& callback); | |
| 94 | 83 |
| 95 // --------------------------------------------------------------------------- | 84 // --------------------------------------------------------------------------- |
| 96 // Cancel the current watch. Must be called from the same thread which | 85 // Cancel the current watch. Must be called from the same thread which |
| 97 // started the watch. | 86 // started the watch. |
| 98 // | 87 // |
| 99 // Does nothing if no event is being watched, nor if the watch has completed. | 88 // Does nothing if no event is being watched, nor if the watch has completed. |
| 100 // The delegate will *not* be called for the current watch after this | 89 // The callback will *not* be called for the current watch after this |
| 101 // function returns. Since the delegate runs on the same thread as this | 90 // function returns. Since the callback runs on the same thread as this |
| 102 // function, it cannot be called during this function either. | 91 // function, it cannot be called during this function either. |
| 103 // --------------------------------------------------------------------------- | 92 // --------------------------------------------------------------------------- |
| 104 void StopWatching(); | 93 void StopWatching(); |
| 105 | 94 |
| 106 // --------------------------------------------------------------------------- | 95 // --------------------------------------------------------------------------- |
| 107 // Return the currently watched event, or NULL if no object is currently being | 96 // Return the currently watched event, or NULL if no object is currently being |
| 108 // watched. | 97 // watched. |
| 109 // --------------------------------------------------------------------------- | 98 // --------------------------------------------------------------------------- |
| 110 WaitableEvent* GetWatchedEvent(); | 99 WaitableEvent* GetWatchedEvent(); |
| 111 | 100 |
| 112 // --------------------------------------------------------------------------- | 101 // --------------------------------------------------------------------------- |
| 113 // Return the delegate, or NULL if there is no delegate. | 102 // Return the callback that will be invoked when the event is |
| 103 // signaled. | |
| 114 // --------------------------------------------------------------------------- | 104 // --------------------------------------------------------------------------- |
| 115 Delegate* delegate() { | 105 const WaitableEventCallback& callback() const { return callback_; } |
| 116 return delegate_; | |
| 117 } | |
| 118 | 106 |
| 119 private: | 107 private: |
| 120 #if defined(OS_WIN) | 108 #if defined(OS_WIN) |
| 121 // --------------------------------------------------------------------------- | 109 virtual void OnObjectSignaled(HANDLE h) OVERRIDE; |
| 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_; | |
| 143 win::ObjectWatcher watcher_; | 110 win::ObjectWatcher watcher_; |
| 144 #else | 111 #else |
| 145 // --------------------------------------------------------------------------- | 112 // --------------------------------------------------------------------------- |
| 146 // Implementation of MessageLoop::DestructionObserver | 113 // Implementation of MessageLoop::DestructionObserver |
| 147 // --------------------------------------------------------------------------- | 114 // --------------------------------------------------------------------------- |
| 148 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 115 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 149 | 116 |
| 150 MessageLoop* message_loop_; | 117 MessageLoop* message_loop_; |
| 151 scoped_refptr<Flag> cancel_flag_; | 118 scoped_refptr<Flag> cancel_flag_; |
| 152 AsyncWaiter* waiter_; | 119 AsyncWaiter* waiter_; |
| 153 base::Closure callback_; | 120 base::Closure internal_callback_; |
| 154 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; | 121 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; |
| 155 #endif | 122 #endif |
| 156 | 123 |
| 157 WaitableEvent* event_; | 124 WaitableEvent* event_; |
| 158 | 125 WaitableEventCallback callback_; |
| 159 Delegate* delegate_; | |
| 160 }; | 126 }; |
| 161 | 127 |
| 162 } // namespace base | 128 } // namespace base |
| 163 | 129 |
| 164 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 130 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
| OLD | NEW |