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 |
danakj
2017/04/27 20:01:08
ISTM that all WaitableEventWatchers' EventCallback
atuchin
2017/04/28 06:44:29
As for me it is better to remove that param at all
danakj
2017/04/28 17:54:49
I would like to do it together because the pointer
atuchin
2017/05/02 07:41:19
Unfortunately, the pointer wasn't safe before.
As
| |
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 win::ScopedHandle event_handle_; | |
93 win::ObjectWatcher watcher_; | 96 win::ObjectWatcher watcher_; |
94 EventCallback callback_; | 97 EventCallback callback_; |
95 WaitableEvent* event_ = nullptr; | 98 WaitableEvent* event_ = nullptr; |
96 #else | 99 #else |
97 // Instantiated in StartWatching(). Set before the callback runs. Reset in | 100 // Instantiated in StartWatching(). Set before the callback runs. Reset in |
98 // StopWatching() or StartWatching(). | 101 // StopWatching() or StartWatching(). |
99 scoped_refptr<Flag> cancel_flag_; | 102 scoped_refptr<Flag> cancel_flag_; |
100 | 103 |
101 // Enqueued in the wait list of the watched WaitableEvent. | 104 // Enqueued in the wait list of the watched WaitableEvent. |
102 AsyncWaiter* waiter_ = nullptr; | 105 AsyncWaiter* waiter_ = nullptr; |
103 | 106 |
104 // Kernel of the watched WaitableEvent. | 107 // Kernel of the watched WaitableEvent. |
105 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; | 108 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; |
106 | 109 |
107 // Ensures that StartWatching() and StopWatching() are called on the same | 110 // Ensures that StartWatching() and StopWatching() are called on the same |
108 // sequence. | 111 // sequence. |
109 SequenceChecker sequence_checker_; | 112 SequenceChecker sequence_checker_; |
110 #endif | 113 #endif |
111 | 114 |
112 DISALLOW_COPY_AND_ASSIGN(WaitableEventWatcher); | 115 DISALLOW_COPY_AND_ASSIGN(WaitableEventWatcher); |
113 }; | 116 }; |
114 | 117 |
115 } // namespace base | 118 } // namespace base |
116 | 119 |
117 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 120 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
OLD | NEW |