OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/waitable_event_watcher.h" | 5 #include "base/waitable_event_watcher.h" |
6 | 6 |
7 #include "base/condition_variable.h" | 7 #include "base/condition_variable.h" |
8 #include "base/lock.h" | 8 #include "base/lock.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/waitable_event.h" | 10 #include "base/waitable_event.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 private: | 115 private: |
116 scoped_refptr<Flag> flag_; | 116 scoped_refptr<Flag> flag_; |
117 WaitableEventWatcher::Delegate *const delegate_; | 117 WaitableEventWatcher::Delegate *const delegate_; |
118 WaitableEvent *const event_; | 118 WaitableEvent *const event_; |
119 }; | 119 }; |
120 | 120 |
121 WaitableEventWatcher::WaitableEventWatcher() | 121 WaitableEventWatcher::WaitableEventWatcher() |
122 : event_(NULL), | 122 : event_(NULL), |
123 message_loop_(NULL), | 123 message_loop_(NULL), |
124 cancel_flag_(NULL), | 124 cancel_flag_(NULL), |
125 callback_task_(NULL) { | 125 callback_task_(NULL), |
| 126 delegate_(NULL) { |
126 } | 127 } |
127 | 128 |
128 WaitableEventWatcher::~WaitableEventWatcher() { | 129 WaitableEventWatcher::~WaitableEventWatcher() { |
129 StopWatching(); | 130 StopWatching(); |
130 } | 131 } |
131 | 132 |
132 // ----------------------------------------------------------------------------- | 133 // ----------------------------------------------------------------------------- |
133 // The Handle is how the user cancels a wait. After deleting the Handle we | 134 // The Handle is how the user cancels a wait. After deleting the Handle we |
134 // insure that the delegate cannot be called. | 135 // insure that the delegate cannot be called. |
135 // ----------------------------------------------------------------------------- | 136 // ----------------------------------------------------------------------------- |
(...skipping 16 matching lines...) Expand all Loading... |
152 } | 153 } |
153 | 154 |
154 DCHECK(!cancel_flag_.get()) << "StartWatching called while still watching"; | 155 DCHECK(!cancel_flag_.get()) << "StartWatching called while still watching"; |
155 | 156 |
156 cancel_flag_ = new Flag; | 157 cancel_flag_ = new Flag; |
157 callback_task_ = new AsyncCallbackTask(cancel_flag_, delegate, event); | 158 callback_task_ = new AsyncCallbackTask(cancel_flag_, delegate, event); |
158 WaitableEvent::WaitableEventKernel* kernel = event->kernel_.get(); | 159 WaitableEvent::WaitableEventKernel* kernel = event->kernel_.get(); |
159 | 160 |
160 AutoLock locked(kernel->lock_); | 161 AutoLock locked(kernel->lock_); |
161 | 162 |
| 163 delegate_ = delegate; |
| 164 event_ = event; |
| 165 |
162 if (kernel->signaled_) { | 166 if (kernel->signaled_) { |
163 if (!kernel->manual_reset_) | 167 if (!kernel->manual_reset_) |
164 kernel->signaled_ = false; | 168 kernel->signaled_ = false; |
165 | 169 |
166 // No hairpinning - we can't call the delegate directly here. We have to | 170 // No hairpinning - we can't call the delegate directly here. We have to |
167 // enqueue a task on the MessageLoop as normal. | 171 // enqueue a task on the MessageLoop as normal. |
168 current_ml->PostTask(FROM_HERE, callback_task_); | 172 current_ml->PostTask(FROM_HERE, callback_task_); |
169 return true; | 173 return true; |
170 } | 174 } |
171 | 175 |
172 message_loop_ = current_ml; | 176 message_loop_ = current_ml; |
173 current_ml->AddDestructionObserver(this); | 177 current_ml->AddDestructionObserver(this); |
174 | 178 |
175 event_ = event; | |
176 kernel_ = kernel; | 179 kernel_ = kernel; |
177 waiter_ = new AsyncWaiter(current_ml, callback_task_, cancel_flag_); | 180 waiter_ = new AsyncWaiter(current_ml, callback_task_, cancel_flag_); |
178 event->Enqueue(waiter_); | 181 event->Enqueue(waiter_); |
179 | 182 |
180 return true; | 183 return true; |
181 } | 184 } |
182 | 185 |
183 void WaitableEventWatcher::StopWatching() { | 186 void WaitableEventWatcher::StopWatching() { |
| 187 delegate_ = NULL; |
| 188 |
184 if (message_loop_) { | 189 if (message_loop_) { |
185 message_loop_->RemoveDestructionObserver(this); | 190 message_loop_->RemoveDestructionObserver(this); |
186 message_loop_ = NULL; | 191 message_loop_ = NULL; |
187 } | 192 } |
188 | 193 |
189 if (!cancel_flag_.get()) // if not currently watching... | 194 if (!cancel_flag_.get()) // if not currently watching... |
190 return; | 195 return; |
191 | 196 |
192 if (cancel_flag_->value()) { | 197 if (cancel_flag_->value()) { |
193 // In this case, the event has fired, but we haven't figured that out yet. | 198 // In this case, the event has fired, but we haven't figured that out yet. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 // ----------------------------------------------------------------------------- | 271 // ----------------------------------------------------------------------------- |
267 // This is called when the MessageLoop which the callback will be run it is | 272 // This is called when the MessageLoop which the callback will be run it is |
268 // deleted. We need to cancel the callback as if we had been deleted, but we | 273 // deleted. We need to cancel the callback as if we had been deleted, but we |
269 // will still be deleted at some point in the future. | 274 // will still be deleted at some point in the future. |
270 // ----------------------------------------------------------------------------- | 275 // ----------------------------------------------------------------------------- |
271 void WaitableEventWatcher::WillDestroyCurrentMessageLoop() { | 276 void WaitableEventWatcher::WillDestroyCurrentMessageLoop() { |
272 StopWatching(); | 277 StopWatching(); |
273 } | 278 } |
274 | 279 |
275 } // namespace base | 280 } // namespace base |
OLD | NEW |