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/location.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 base::Closure callback_; | 90 base::Closure callback_; |
91 scoped_refptr<Flag> flag_; | 91 scoped_refptr<Flag> flag_; |
92 }; | 92 }; |
93 | 93 |
94 // ----------------------------------------------------------------------------- | 94 // ----------------------------------------------------------------------------- |
95 // For async waits we need to make a callback in a MessageLoop thread. We do | 95 // For async waits we need to make a callback in a MessageLoop thread. We do |
96 // this by posting a callback, which calls the delegate and keeps track of when | 96 // this by posting a callback, which calls the delegate and keeps track of when |
97 // the event is canceled. | 97 // the event is canceled. |
98 // ----------------------------------------------------------------------------- | 98 // ----------------------------------------------------------------------------- |
99 void AsyncCallbackHelper(Flag* flag, | 99 void AsyncCallbackHelper(Flag* flag, |
100 WaitableEventWatcher::Delegate* delegate, | 100 Callback<void(WaitableEvent*)>* callback, |
101 WaitableEvent* event) { | 101 WaitableEvent* event) { |
102 // Runs in MessageLoop thread. | 102 // Runs in MessageLoop thread. |
103 if (!flag->value()) { | 103 if (!flag->value()) { |
104 // This is to let the WaitableEventWatcher know that the event has occured | 104 // This is to let the WaitableEventWatcher know that the event has occured |
105 // because it needs to be able to return NULL from GetWatchedObject | 105 // because it needs to be able to return NULL from GetWatchedObject |
106 flag->Set(); | 106 flag->Set(); |
107 delegate->OnWaitableEventSignaled(event); | 107 callback->Run(event); |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 WaitableEventWatcher::WaitableEventWatcher() | 111 WaitableEventWatcher::WaitableEventWatcher() |
112 : message_loop_(NULL), | 112 : message_loop_(NULL), |
113 cancel_flag_(NULL), | 113 cancel_flag_(NULL), |
114 waiter_(NULL), | 114 waiter_(NULL), |
115 event_(NULL), | 115 event_(NULL) { |
116 delegate_(NULL) { | |
117 } | 116 } |
118 | 117 |
119 WaitableEventWatcher::~WaitableEventWatcher() { | 118 WaitableEventWatcher::~WaitableEventWatcher() { |
120 StopWatching(); | 119 StopWatching(); |
121 } | 120 } |
122 | 121 |
123 // ----------------------------------------------------------------------------- | 122 // ----------------------------------------------------------------------------- |
124 // The Handle is how the user cancels a wait. After deleting the Handle we | 123 // The Handle is how the user cancels a wait. After deleting the Handle we |
125 // insure that the delegate cannot be called. | 124 // insure that the delegate cannot be called. |
126 // ----------------------------------------------------------------------------- | 125 // ----------------------------------------------------------------------------- |
127 bool WaitableEventWatcher::StartWatching | 126 bool WaitableEventWatcher::StartWatching |
128 (WaitableEvent* event, WaitableEventWatcher::Delegate* delegate) { | 127 (WaitableEvent* event, Callback<void(WaitableEvent*)>* callback) { |
dmichael (off chromium)
2013/01/25 21:28:11
nit: paren should be on the previous line.
extra t
teravest
2013/01/28 17:10:55
Done.
| |
129 MessageLoop *const current_ml = MessageLoop::current(); | 128 MessageLoop *const current_ml = MessageLoop::current(); |
130 DCHECK(current_ml) << "Cannot create WaitableEventWatcher without a " | 129 DCHECK(current_ml) << "Cannot create WaitableEventWatcher without a " |
131 "current MessageLoop"; | 130 "current MessageLoop"; |
132 | 131 |
133 // A user may call StartWatching from within the callback function. In this | 132 // A user may call StartWatching from within the callback function. In this |
134 // case, we won't know that we have finished watching, expect that the Flag | 133 // case, we won't know that we have finished watching, expect that the Flag |
135 // will have been set in AsyncCallbackHelper(). | 134 // will have been set in AsyncCallbackHelper(). |
136 if (cancel_flag_.get() && cancel_flag_->value()) { | 135 if (cancel_flag_.get() && cancel_flag_->value()) { |
137 if (message_loop_) { | 136 if (message_loop_) { |
138 message_loop_->RemoveDestructionObserver(this); | 137 message_loop_->RemoveDestructionObserver(this); |
139 message_loop_ = NULL; | 138 message_loop_ = NULL; |
140 } | 139 } |
141 | 140 |
142 cancel_flag_ = NULL; | 141 cancel_flag_ = NULL; |
143 } | 142 } |
144 | 143 |
145 DCHECK(!cancel_flag_.get()) << "StartWatching called while still watching"; | 144 DCHECK(!cancel_flag_.get()) << "StartWatching called while still watching"; |
146 | 145 |
147 cancel_flag_ = new Flag; | 146 cancel_flag_ = new Flag; |
148 callback_ = base::Bind(&AsyncCallbackHelper, cancel_flag_, delegate, event); | 147 internal_callback_ = |
148 base::Bind(&AsyncCallbackHelper, cancel_flag_, callback, event); | |
149 WaitableEvent::WaitableEventKernel* kernel = event->kernel_.get(); | 149 WaitableEvent::WaitableEventKernel* kernel = event->kernel_.get(); |
150 | 150 |
151 AutoLock locked(kernel->lock_); | 151 AutoLock locked(kernel->lock_); |
152 | 152 |
153 delegate_ = delegate; | 153 callback_ = callback; |
154 event_ = event; | 154 event_ = event; |
155 | 155 |
156 if (kernel->signaled_) { | 156 if (kernel->signaled_) { |
157 if (!kernel->manual_reset_) | 157 if (!kernel->manual_reset_) |
158 kernel->signaled_ = false; | 158 kernel->signaled_ = false; |
159 | 159 |
160 // No hairpinning - we can't call the delegate directly here. We have to | 160 // No hairpinning - we can't call the delegate directly here. We have to |
161 // enqueue a task on the MessageLoop as normal. | 161 // enqueue a task on the MessageLoop as normal. |
162 current_ml->PostTask(FROM_HERE, callback_); | 162 current_ml->PostTask(FROM_HERE, internal_callback_); |
163 return true; | 163 return true; |
164 } | 164 } |
165 | 165 |
166 message_loop_ = current_ml; | 166 message_loop_ = current_ml; |
167 current_ml->AddDestructionObserver(this); | 167 current_ml->AddDestructionObserver(this); |
168 | 168 |
169 kernel_ = kernel; | 169 kernel_ = kernel; |
170 waiter_ = new AsyncWaiter(current_ml, callback_, cancel_flag_); | 170 waiter_ = new AsyncWaiter(current_ml, internal_callback_, cancel_flag_); |
171 event->Enqueue(waiter_); | 171 event->Enqueue(waiter_); |
172 | 172 |
173 return true; | 173 return true; |
174 } | 174 } |
175 | 175 |
176 void WaitableEventWatcher::StopWatching() { | 176 void WaitableEventWatcher::StopWatching() { |
177 delegate_ = NULL; | 177 callback_ = NULL; |
178 | 178 |
179 if (message_loop_) { | 179 if (message_loop_) { |
180 message_loop_->RemoveDestructionObserver(this); | 180 message_loop_->RemoveDestructionObserver(this); |
181 message_loop_ = NULL; | 181 message_loop_ = NULL; |
182 } | 182 } |
183 | 183 |
184 if (!cancel_flag_.get()) // if not currently watching... | 184 if (!cancel_flag_.get()) // if not currently watching... |
185 return; | 185 return; |
186 | 186 |
187 if (cancel_flag_->value()) { | 187 if (cancel_flag_->value()) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 // a tag which is good for the lifetime of this handle: the Flag. Since we | 220 // 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 | 221 // 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 | 222 // 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. | 223 // which shares a Flag pointer, we have a real match. |
224 if (kernel_->Dequeue(waiter_, cancel_flag_.get())) { | 224 if (kernel_->Dequeue(waiter_, cancel_flag_.get())) { |
225 // Case 2: the waiter hasn't been signaled yet; it was still on the wait | 225 // 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 | 226 // 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 | 227 // have been enqueued with the MessageLoop because the waiter was never |
228 // signaled) | 228 // signaled) |
229 delete waiter_; | 229 delete waiter_; |
230 callback_.Reset(); | 230 internal_callback_.Reset(); |
231 cancel_flag_ = NULL; | 231 cancel_flag_ = NULL; |
232 return; | 232 return; |
233 } | 233 } |
234 | 234 |
235 // Case 3: the waiter isn't on the wait-list, thus it was signaled. It may | 235 // Case 3: the waiter isn't on the wait-list, thus it was signaled. It may |
236 // not have run yet, so we set the flag to tell it not to bother enqueuing the | 236 // not 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 | 237 // task on the MessageLoop, but to delete it instead. The Waiter deletes |
238 // itself once run. | 238 // itself once run. |
239 cancel_flag_->Set(); | 239 cancel_flag_->Set(); |
240 cancel_flag_ = NULL; | 240 cancel_flag_ = NULL; |
(...skipping 20 matching lines...) Expand all Loading... | |
261 // ----------------------------------------------------------------------------- | 261 // ----------------------------------------------------------------------------- |
262 // This is called when the MessageLoop which the callback will be run it is | 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 | 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. | 264 // will still be deleted at some point in the future. |
265 // ----------------------------------------------------------------------------- | 265 // ----------------------------------------------------------------------------- |
266 void WaitableEventWatcher::WillDestroyCurrentMessageLoop() { | 266 void WaitableEventWatcher::WillDestroyCurrentMessageLoop() { |
267 StopWatching(); | 267 StopWatching(); |
268 } | 268 } |
269 | 269 |
270 } // namespace base | 270 } // namespace base |
OLD | NEW |