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 #include "base/synchronization/waitable_event_watcher.h" | 5 #include "base/synchronization/waitable_event_watcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
12 #include "base/synchronization/waitable_event.h" | 10 #include "base/threading/sequenced_task_runner_handle.h" |
13 | 11 |
14 namespace base { | 12 namespace base { |
15 | 13 |
16 // ----------------------------------------------------------------------------- | 14 // ----------------------------------------------------------------------------- |
17 // WaitableEventWatcher (async waits). | 15 // WaitableEventWatcher (async waits). |
18 // | 16 // |
19 // The basic design is that we add an AsyncWaiter to the wait-list of the event. | 17 // The basic design is that we add an AsyncWaiter to the wait-list of the event. |
20 // That AsyncWaiter has a pointer to MessageLoop, and a Task to be posted to it. | 18 // That AsyncWaiter has a pointer to SequencedTaskRunner, and a Task to be |
21 // The MessageLoop ends up running the task, which calls the delegate. | 19 // posted to it. The task ends up calling the callback when it runs on the |
20 // sequence. | |
22 // | 21 // |
23 // Since the wait can be canceled, we have a thread-safe Flag object which is | 22 // Since the wait can be canceled, we have a thread-safe Flag object which is |
24 // set when the wait has been canceled. At each stage in the above, we check the | 23 // set when the wait has been canceled. At each stage in the above, we check the |
25 // flag before going onto the next stage. Since the wait may only be canceled in | 24 // flag before going onto the next stage. Since the wait may only be canceled in |
26 // the MessageLoop which runs the Task, we are assured that the delegate cannot | 25 // the sequence which runs the Task, we are assured that the callback cannot be |
27 // be called after canceling... | 26 // called after canceling... |
28 | 27 |
29 // ----------------------------------------------------------------------------- | 28 // ----------------------------------------------------------------------------- |
30 // A thread-safe, reference-counted, write-once flag. | 29 // A thread-safe, reference-counted, write-once flag. |
31 // ----------------------------------------------------------------------------- | 30 // ----------------------------------------------------------------------------- |
32 class Flag : public RefCountedThreadSafe<Flag> { | 31 class Flag : public RefCountedThreadSafe<Flag> { |
33 public: | 32 public: |
34 Flag() { flag_ = false; } | 33 Flag() { flag_ = false; } |
35 | 34 |
36 void Set() { | 35 void Set() { |
37 AutoLock locked(lock_); | 36 AutoLock locked(lock_); |
38 flag_ = true; | 37 flag_ = true; |
39 } | 38 } |
40 | 39 |
41 bool value() const { | 40 bool value() const { |
42 AutoLock locked(lock_); | 41 AutoLock locked(lock_); |
43 return flag_; | 42 return flag_; |
44 } | 43 } |
45 | 44 |
46 private: | 45 private: |
47 friend class RefCountedThreadSafe<Flag>; | 46 friend class RefCountedThreadSafe<Flag>; |
48 ~Flag() {} | 47 ~Flag() {} |
49 | 48 |
50 mutable Lock lock_; | 49 mutable Lock lock_; |
51 bool flag_; | 50 bool flag_; |
52 | 51 |
53 DISALLOW_COPY_AND_ASSIGN(Flag); | 52 DISALLOW_COPY_AND_ASSIGN(Flag); |
54 }; | 53 }; |
55 | 54 |
56 // ----------------------------------------------------------------------------- | 55 // ----------------------------------------------------------------------------- |
57 // This is an asynchronous waiter which posts a task to a MessageLoop when | 56 // This is an asynchronous waiter which posts a task to a SequencedTaskRunner |
58 // fired. An AsyncWaiter may only be in a single wait-list. | 57 // when fired. An AsyncWaiter may only be in a single wait-list. |
59 // ----------------------------------------------------------------------------- | 58 // ----------------------------------------------------------------------------- |
60 class AsyncWaiter : public WaitableEvent::Waiter { | 59 class AsyncWaiter : public WaitableEvent::Waiter { |
61 public: | 60 public: |
62 AsyncWaiter(MessageLoop* message_loop, | 61 AsyncWaiter(scoped_refptr<SequencedTaskRunner> task_runner, |
63 const base::Closure& callback, | 62 const base::Closure& callback, |
64 Flag* flag) | 63 Flag* flag) |
65 : message_loop_(message_loop), | 64 : task_runner_(task_runner), callback_(callback), flag_(flag) {} |
dcheng
2016/10/03 21:24:02
Nit: std::move
fdoray
2016/10/04 12:59:11
Done.
| |
66 callback_(callback), | |
67 flag_(flag) { } | |
68 | 65 |
69 bool Fire(WaitableEvent* event) override { | 66 bool Fire(WaitableEvent* event) override { |
70 // Post the callback if we haven't been cancelled. | 67 // Post the callback if we haven't been cancelled. |
71 if (!flag_->value()) { | 68 if (!flag_->value()) |
72 message_loop_->task_runner()->PostTask(FROM_HERE, callback_); | 69 task_runner_->PostTask(FROM_HERE, callback_); |
73 } | |
74 | 70 |
75 // We are removed from the wait-list by the WaitableEvent itself. It only | 71 // We are removed from the wait-list by the WaitableEvent itself. It only |
76 // remains to delete ourselves. | 72 // remains to delete ourselves. |
77 delete this; | 73 delete this; |
78 | 74 |
79 // We can always return true because an AsyncWaiter is never in two | 75 // We can always return true because an AsyncWaiter is never in two |
80 // different wait-lists at the same time. | 76 // different wait-lists at the same time. |
81 return true; | 77 return true; |
82 } | 78 } |
83 | 79 |
84 // See StopWatching for discussion | 80 // See StopWatching for discussion |
85 bool Compare(void* tag) override { return tag == flag_.get(); } | 81 bool Compare(void* tag) override { return tag == flag_.get(); } |
86 | 82 |
87 private: | 83 private: |
88 MessageLoop *const message_loop_; | 84 const scoped_refptr<SequencedTaskRunner> task_runner_; |
89 base::Closure callback_; | 85 const base::Closure callback_; |
90 scoped_refptr<Flag> flag_; | 86 const scoped_refptr<Flag> flag_; |
91 }; | 87 }; |
92 | 88 |
93 // ----------------------------------------------------------------------------- | 89 // ----------------------------------------------------------------------------- |
94 // For async waits we need to make a callback in a MessageLoop thread. We do | 90 // For async waits we need to run a callback on a sequence. We do this by |
95 // this by posting a callback, which calls the delegate and keeps track of when | 91 // posting an AsyncCallbackHelper task, which calls the callback and keeps track |
96 // the event is canceled. | 92 // of when the event is canceled. |
97 // ----------------------------------------------------------------------------- | 93 // ----------------------------------------------------------------------------- |
98 void AsyncCallbackHelper(Flag* flag, | 94 void AsyncCallbackHelper(Flag* flag, |
99 const WaitableEventWatcher::EventCallback& callback, | 95 const WaitableEventWatcher::EventCallback& callback, |
100 WaitableEvent* event) { | 96 WaitableEvent* event) { |
101 // Runs in MessageLoop thread. | 97 // Runs on the sequence that called StartWatching(). |
102 if (!flag->value()) { | 98 if (!flag->value()) { |
103 // This is to let the WaitableEventWatcher know that the event has occured | 99 // This is to let the WaitableEventWatcher know that the event has occured. |
104 // because it needs to be able to return NULL from GetWatchedObject | |
105 flag->Set(); | 100 flag->Set(); |
106 callback.Run(event); | 101 callback.Run(event); |
107 } | 102 } |
108 } | 103 } |
109 | 104 |
110 WaitableEventWatcher::WaitableEventWatcher() | 105 WaitableEventWatcher::WaitableEventWatcher() { |
111 : message_loop_(NULL), | 106 sequence_checker_.DetachFromSequence(); |
112 cancel_flag_(NULL), | |
113 waiter_(NULL), | |
114 event_(NULL) { | |
115 } | 107 } |
116 | 108 |
117 WaitableEventWatcher::~WaitableEventWatcher() { | 109 WaitableEventWatcher::~WaitableEventWatcher() { |
118 StopWatching(); | 110 // The destructor may be called from a different sequence than StartWatching() |
111 // when there is no active watch. To avoid triggering a DCHECK in | |
112 // StopWatching(), do not call it when there is no active watch. | |
113 if (cancel_flag_ && !cancel_flag_->value()) | |
114 StopWatching(); | |
119 } | 115 } |
120 | 116 |
121 // ----------------------------------------------------------------------------- | 117 // ----------------------------------------------------------------------------- |
122 // The Handle is how the user cancels a wait. After deleting the Handle we | 118 // The Handle is how the user cancels a wait. After deleting the Handle we |
123 // insure that the delegate cannot be called. | 119 // insure that the delegate cannot be called. |
124 // ----------------------------------------------------------------------------- | 120 // ----------------------------------------------------------------------------- |
125 bool WaitableEventWatcher::StartWatching( | 121 bool WaitableEventWatcher::StartWatching( |
126 WaitableEvent* event, | 122 WaitableEvent* event, |
127 const EventCallback& callback) { | 123 const EventCallback& callback) { |
128 MessageLoop *const current_ml = MessageLoop::current(); | 124 DCHECK(sequence_checker_.CalledOnValidSequence()); |
dcheng
2016/10/03 21:24:02
Just to be sure: do you know if there's any effici
fdoray
2016/10/04 12:59:11
Changed to DCHECK(SequencedTaskRunnerHandle::IsSet
| |
129 DCHECK(current_ml) << "Cannot create WaitableEventWatcher without a " | 125 DCHECK(SequencedTaskRunnerHandle::Get()); |
130 "current MessageLoop"; | |
131 | 126 |
132 // A user may call StartWatching from within the callback function. In this | 127 // A user may call StartWatching from within the callback function. In this |
133 // case, we won't know that we have finished watching, expect that the Flag | 128 // case, we won't know that we have finished watching, expect that the Flag |
134 // will have been set in AsyncCallbackHelper(). | 129 // will have been set in AsyncCallbackHelper(). |
135 if (cancel_flag_.get() && cancel_flag_->value()) { | 130 if (cancel_flag_.get() && cancel_flag_->value()) |
136 if (message_loop_) { | 131 cancel_flag_ = nullptr; |
137 message_loop_->RemoveDestructionObserver(this); | |
138 message_loop_ = NULL; | |
139 } | |
140 | 132 |
141 cancel_flag_ = NULL; | 133 DCHECK(!cancel_flag_) << "StartWatching called while still watching"; |
142 } | |
143 | |
144 DCHECK(!cancel_flag_.get()) << "StartWatching called while still watching"; | |
145 | 134 |
146 cancel_flag_ = new Flag; | 135 cancel_flag_ = new Flag; |
147 callback_ = callback; | 136 const auto internal_callback = base::Bind( |
dcheng
2016/10/03 21:24:02
Nit: I think we can just write Closure here (to me
fdoray
2016/10/04 12:59:11
Done.
| |
148 internal_callback_ = base::Bind( | 137 &AsyncCallbackHelper, base::RetainedRef(cancel_flag_), callback, event); |
149 &AsyncCallbackHelper, base::RetainedRef(cancel_flag_), callback_, event); | |
150 WaitableEvent::WaitableEventKernel* kernel = event->kernel_.get(); | 138 WaitableEvent::WaitableEventKernel* kernel = event->kernel_.get(); |
151 | 139 |
152 AutoLock locked(kernel->lock_); | 140 AutoLock locked(kernel->lock_); |
153 | 141 |
154 event_ = event; | |
155 | |
156 if (kernel->signaled_) { | 142 if (kernel->signaled_) { |
157 if (!kernel->manual_reset_) | 143 if (!kernel->manual_reset_) |
158 kernel->signaled_ = false; | 144 kernel->signaled_ = false; |
159 | 145 |
160 // No hairpinning - we can't call the delegate directly here. We have to | 146 // No hairpinning - we can't call the delegate directly here. We have to |
161 // enqueue a task on the MessageLoop as normal. | 147 // post a task to the SequencedTaskRunnerHandle as usual. |
162 current_ml->task_runner()->PostTask(FROM_HERE, internal_callback_); | 148 SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE, internal_callback); |
163 return true; | 149 return true; |
164 } | 150 } |
165 | 151 |
166 message_loop_ = current_ml; | |
167 current_ml->AddDestructionObserver(this); | |
168 | |
169 kernel_ = kernel; | 152 kernel_ = kernel; |
170 waiter_ = new AsyncWaiter(current_ml, internal_callback_, cancel_flag_.get()); | 153 waiter_ = new AsyncWaiter(SequencedTaskRunnerHandle::Get(), internal_callback, |
154 cancel_flag_.get()); | |
171 event->Enqueue(waiter_); | 155 event->Enqueue(waiter_); |
172 | 156 |
173 return true; | 157 return true; |
174 } | 158 } |
175 | 159 |
176 void WaitableEventWatcher::StopWatching() { | 160 void WaitableEventWatcher::StopWatching() { |
177 callback_.Reset(); | 161 DCHECK(sequence_checker_.CalledOnValidSequence()); |
178 | |
179 if (message_loop_) { | |
180 message_loop_->RemoveDestructionObserver(this); | |
181 message_loop_ = NULL; | |
182 } | |
183 | 162 |
184 if (!cancel_flag_.get()) // if not currently watching... | 163 if (!cancel_flag_.get()) // if not currently watching... |
185 return; | 164 return; |
186 | 165 |
187 if (cancel_flag_->value()) { | 166 if (cancel_flag_->value()) { |
188 // In this case, the event has fired, but we haven't figured that out yet. | 167 // In this case, the event has fired, but we haven't figured that out yet. |
189 // The WaitableEvent may have been deleted too. | 168 // The WaitableEvent may have been deleted too. |
190 cancel_flag_ = NULL; | 169 cancel_flag_ = NULL; |
191 return; | 170 return; |
192 } | 171 } |
(...skipping 27 matching lines...) Expand all Loading... | |
220 // a tag which is good for the lifetime of this handle: the Flag. Since we | 199 // a tag which is good for the lifetime of this handle: the Flag. Since we |
221 // have a reference to the Flag, its memory cannot be reused while this object | 200 // have a reference to the Flag, its memory cannot be reused while this object |
222 // still exists. So if we find a waiter with the correct pointer value, and | 201 // still exists. So if we find a waiter with the correct pointer value, and |
223 // which shares a Flag pointer, we have a real match. | 202 // which shares a Flag pointer, we have a real match. |
224 if (kernel_->Dequeue(waiter_, cancel_flag_.get())) { | 203 if (kernel_->Dequeue(waiter_, cancel_flag_.get())) { |
225 // Case 2: the waiter hasn't been signaled yet; it was still on the wait | 204 // Case 2: the waiter hasn't been signaled yet; it was still on the wait |
226 // list. We've removed it, thus we can delete it and the task (which cannot | 205 // list. We've removed it, thus we can delete it and the task (which cannot |
227 // have been enqueued with the MessageLoop because the waiter was never | 206 // have been enqueued with the MessageLoop because the waiter was never |
228 // signaled) | 207 // signaled) |
229 delete waiter_; | 208 delete waiter_; |
230 internal_callback_.Reset(); | |
231 cancel_flag_ = NULL; | 209 cancel_flag_ = NULL; |
232 return; | 210 return; |
233 } | 211 } |
234 | 212 |
235 // Case 3: the waiter isn't on the wait-list, thus it was signaled. It may | 213 // Case 3: the waiter isn't on the wait-list, thus it was signaled. It may not |
236 // not have run yet, so we set the flag to tell it not to bother enqueuing the | 214 // have run yet, so we set the flag to tell it not to bother enqueuing the |
237 // task on the MessageLoop, but to delete it instead. The Waiter deletes | 215 // task on the SequencedTaskRunner, but to delete it instead. The Waiter |
238 // itself once run. | 216 // deletes itself once run. |
239 cancel_flag_->Set(); | 217 cancel_flag_->Set(); |
240 cancel_flag_ = NULL; | 218 cancel_flag_ = NULL; |
241 | 219 |
242 // If the waiter has already run then the task has been enqueued. If the Task | 220 // If the waiter has already run then the task has been enqueued. If the Task |
243 // hasn't yet run, the flag will stop the delegate from getting called. (This | 221 // hasn't yet run, the flag will stop the delegate from getting called. (This |
244 // is thread safe because one may only delete a Handle from the MessageLoop | 222 // is thread safe because one may only delete a Handle from the sequence that |
245 // thread.) | 223 // called StartWatching()). |
246 // | 224 // |
247 // If the delegate has already been called then we have nothing to do. The | 225 // If the delegate has already been called then we have nothing to do. The |
248 // task has been deleted by the MessageLoop. | 226 // task has been deleted by the MessageLoop. |
249 } | 227 } |
250 | 228 |
251 WaitableEvent* WaitableEventWatcher::GetWatchedEvent() { | |
252 if (!cancel_flag_.get()) | |
253 return NULL; | |
254 | |
255 if (cancel_flag_->value()) | |
256 return NULL; | |
257 | |
258 return event_; | |
259 } | |
260 | |
261 // ----------------------------------------------------------------------------- | |
262 // This is called when the MessageLoop which the callback will be run it is | |
263 // deleted. We need to cancel the callback as if we had been deleted, but we | |
264 // will still be deleted at some point in the future. | |
265 // ----------------------------------------------------------------------------- | |
266 void WaitableEventWatcher::WillDestroyCurrentMessageLoop() { | |
267 StopWatching(); | |
268 } | |
269 | |
270 } // namespace base | 229 } // namespace base |
OLD | NEW |