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" | 9 #include "base/macros.h" |
10 #include "base/sequence_checker.h" | |
11 #include "build/build_config.h" | 10 #include "build/build_config.h" |
12 | 11 |
13 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
14 #include "base/win/object_watcher.h" | 13 #include "base/win/object_watcher.h" |
| 14 #include "base/win/scoped_handle.h" |
15 #else | 15 #else |
16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/sequence_checker.h" |
17 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
18 #endif | 19 #endif |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 | 22 |
22 class Flag; | 23 class Flag; |
23 class AsyncWaiter; | 24 class AsyncWaiter; |
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. |
(...skipping 22 matching lines...) Expand all Loading... |
49 // goes out of scope, the watcher_ will be destroyed, and there is no need to | 50 // 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 | 51 // worry about OnWaitableEventSignaled being called on a deleted MyClass |
51 // pointer. | 52 // pointer. |
52 // | 53 // |
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. But pay attention: if the event was signaled and deleted |
| 61 // right after, the callback may be called with deleted WaitableEvent pointer. |
60 | 62 |
61 class BASE_EXPORT WaitableEventWatcher | 63 class BASE_EXPORT WaitableEventWatcher |
62 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
63 : public win::ObjectWatcher::Delegate | 65 : public win::ObjectWatcher::Delegate |
64 #endif | 66 #endif |
65 { | 67 { |
66 public: | 68 public: |
67 using EventCallback = OnceCallback<void(WaitableEvent*)>; | 69 using EventCallback = OnceCallback<void(WaitableEvent*)>; |
68 WaitableEventWatcher(); | 70 WaitableEventWatcher(); |
69 | 71 |
(...skipping 13 matching lines...) Expand all Loading... |
83 // Does nothing if no event is being watched, nor if the watch has completed. | 85 // Does nothing if no event is being watched, nor if the watch has completed. |
84 // The callback will *not* be called for the current watch after this | 86 // The callback will *not* be called for the current watch after this |
85 // function returns. Since the callback runs on the same sequence as this | 87 // function returns. Since the callback runs on the same sequence as this |
86 // function, it cannot be called during this function either. | 88 // function, it cannot be called during this function either. |
87 void StopWatching(); | 89 void StopWatching(); |
88 | 90 |
89 private: | 91 private: |
90 #if defined(OS_WIN) | 92 #if defined(OS_WIN) |
91 void OnObjectSignaled(HANDLE h) override; | 93 void OnObjectSignaled(HANDLE h) override; |
92 | 94 |
| 95 // Duplicated handle of the event passed to StartWatching(). |
| 96 win::ScopedHandle duplicated_event_handle_; |
| 97 |
| 98 // A watcher for |duplicated_event_handle_|. The handle MUST outlive |
| 99 // |watcher_|. |
93 win::ObjectWatcher watcher_; | 100 win::ObjectWatcher watcher_; |
| 101 |
94 EventCallback callback_; | 102 EventCallback callback_; |
95 WaitableEvent* event_ = nullptr; | 103 WaitableEvent* event_ = nullptr; |
96 #else | 104 #else |
97 // Instantiated in StartWatching(). Set before the callback runs. Reset in | 105 // Instantiated in StartWatching(). Set before the callback runs. Reset in |
98 // StopWatching() or StartWatching(). | 106 // StopWatching() or StartWatching(). |
99 scoped_refptr<Flag> cancel_flag_; | 107 scoped_refptr<Flag> cancel_flag_; |
100 | 108 |
101 // Enqueued in the wait list of the watched WaitableEvent. | 109 // Enqueued in the wait list of the watched WaitableEvent. |
102 AsyncWaiter* waiter_ = nullptr; | 110 AsyncWaiter* waiter_ = nullptr; |
103 | 111 |
104 // Kernel of the watched WaitableEvent. | 112 // Kernel of the watched WaitableEvent. |
105 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; | 113 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; |
106 | 114 |
107 // Ensures that StartWatching() and StopWatching() are called on the same | 115 // Ensures that StartWatching() and StopWatching() are called on the same |
108 // sequence. | 116 // sequence. |
109 SequenceChecker sequence_checker_; | 117 SequenceChecker sequence_checker_; |
110 #endif | 118 #endif |
111 | 119 |
112 DISALLOW_COPY_AND_ASSIGN(WaitableEventWatcher); | 120 DISALLOW_COPY_AND_ASSIGN(WaitableEventWatcher); |
113 }; | 121 }; |
114 | 122 |
115 } // namespace base | 123 } // namespace base |
116 | 124 |
117 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 125 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
OLD | NEW |