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