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