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