| 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" | |
| 10 | |
| 11 #if defined(OS_WIN) | |
| 12 #include "base/win/object_watcher.h" | |
| 13 #else | |
| 14 #include "base/callback.h" | 9 #include "base/callback.h" |
| 15 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 16 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 17 #endif | 12 #include "build/build_config.h" |
| 18 | 13 |
| 19 namespace base { | 14 namespace base { |
| 20 | 15 |
| 21 class Flag; | 16 class Flag; |
| 22 class AsyncWaiter; | 17 class AsyncWaiter; |
| 23 class AsyncCallbackTask; | 18 class AsyncCallbackTask; |
| 24 class WaitableEvent; | 19 class WaitableEvent; |
| 25 | 20 |
| 26 // This class provides a way to wait on a WaitableEvent asynchronously. | 21 // This class provides a way to wait on a WaitableEvent asynchronously. |
| 27 // | 22 // |
| (...skipping 24 matching lines...) Expand all Loading... |
| 52 // | 47 // |
| 53 // BEWARE: With automatically reset WaitableEvents, a signal may be lost if it | 48 // BEWARE: With automatically reset WaitableEvents, a signal may be lost if it |
| 54 // occurs just before a WaitableEventWatcher is deleted. There is currently no | 49 // occurs just before a WaitableEventWatcher is deleted. There is currently no |
| 55 // safe way to stop watching an automatic reset WaitableEvent without possibly | 50 // safe way to stop watching an automatic reset WaitableEvent without possibly |
| 56 // missing a signal. | 51 // missing a signal. |
| 57 // | 52 // |
| 58 // NOTE: you /are/ allowed to delete the WaitableEvent while still waiting on | 53 // 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. | 54 // it with a Watcher. It will act as if the event was never signaled. |
| 60 | 55 |
| 61 class BASE_EXPORT WaitableEventWatcher | 56 class BASE_EXPORT WaitableEventWatcher |
| 62 #if defined(OS_WIN) | |
| 63 : public win::ObjectWatcher::Delegate { | |
| 64 #else | |
| 65 : public MessageLoop::DestructionObserver { | 57 : public MessageLoop::DestructionObserver { |
| 66 #endif | |
| 67 public: | 58 public: |
| 68 typedef Callback<void(WaitableEvent*)> EventCallback; | 59 typedef Callback<void(WaitableEvent*)> EventCallback; |
| 69 WaitableEventWatcher(); | 60 WaitableEventWatcher(); |
| 70 ~WaitableEventWatcher() override; | 61 ~WaitableEventWatcher() override; |
| 71 | 62 |
| 72 // When @event is signaled, the given callback is called on the thread of the | 63 // When @event is signaled, the given callback is called on the thread of the |
| 73 // current message loop when StartWatching is called. | 64 // current message loop when StartWatching is called. |
| 74 bool StartWatching(WaitableEvent* event, const EventCallback& callback); | 65 bool StartWatching(WaitableEvent* event, const EventCallback& callback); |
| 75 | 66 |
| 76 // Cancel the current watch. Must be called from the same thread which | 67 // Cancel the current watch. Must be called from the same thread which |
| 77 // started the watch. | 68 // started the watch. |
| 78 // | 69 // |
| 79 // Does nothing if no event is being watched, nor if the watch has completed. | 70 // 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 | 71 // 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 | 72 // function returns. Since the callback runs on the same thread as this |
| 82 // function, it cannot be called during this function either. | 73 // function, it cannot be called during this function either. |
| 83 void StopWatching(); | 74 void StopWatching(); |
| 84 | 75 |
| 85 // Return the currently watched event, or NULL if no object is currently being | 76 // Return the currently watched event, or NULL if no object is currently being |
| 86 // watched. | 77 // watched. |
| 87 WaitableEvent* GetWatchedEvent(); | 78 WaitableEvent* GetWatchedEvent(); |
| 88 | 79 |
| 89 // Return the callback that will be invoked when the event is | 80 // Return the callback that will be invoked when the event is |
| 90 // signaled. | 81 // signaled. |
| 91 const EventCallback& callback() const { return callback_; } | 82 const EventCallback& callback() const { return callback_; } |
| 92 | 83 |
| 93 private: | 84 private: |
| 94 #if defined(OS_WIN) | |
| 95 void OnObjectSignaled(HANDLE h) override; | |
| 96 win::ObjectWatcher watcher_; | |
| 97 #else | |
| 98 // Implementation of MessageLoop::DestructionObserver | 85 // Implementation of MessageLoop::DestructionObserver |
| 99 void WillDestroyCurrentMessageLoop() override; | 86 void WillDestroyCurrentMessageLoop() override; |
| 100 | 87 |
| 101 MessageLoop* message_loop_; | 88 MessageLoop* message_loop_; |
| 102 scoped_refptr<Flag> cancel_flag_; | 89 scoped_refptr<Flag> cancel_flag_; |
| 103 AsyncWaiter* waiter_; | 90 AsyncWaiter* waiter_; |
| 104 base::Closure internal_callback_; | 91 base::Closure internal_callback_; |
| 105 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; | 92 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; |
| 106 #endif | |
| 107 | 93 |
| 108 WaitableEvent* event_; | 94 WaitableEvent* event_; |
| 109 EventCallback callback_; | 95 EventCallback callback_; |
| 110 }; | 96 }; |
| 111 | 97 |
| 112 } // namespace base | 98 } // namespace base |
| 113 | 99 |
| 114 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 100 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
| OLD | NEW |