| 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/message_loop.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| 11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 | 14 |
| 15 // ----------------------------------------------------------------------------- | 15 // ----------------------------------------------------------------------------- |
| 16 // WaitableEventWatcher (async waits). | 16 // WaitableEventWatcher (async waits). |
| 17 // | 17 // |
| 18 // The basic design is that we add an AsyncWaiter to the wait-list of the event. | 18 // The basic design is that we add an AsyncWaiter to the wait-list of the event. |
| 19 // That AsyncWaiter has a pointer to MessageLoop, and a Task to be posted to it. | 19 // That AsyncWaiter has a pointer to MessageLoop, and a Task to be posted to it. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 AsyncWaiter(MessageLoop* message_loop, | 61 AsyncWaiter(MessageLoop* message_loop, |
| 62 const base::Closure& callback, | 62 const base::Closure& callback, |
| 63 Flag* flag) | 63 Flag* flag) |
| 64 : message_loop_(message_loop), | 64 : message_loop_(message_loop), |
| 65 callback_(callback), | 65 callback_(callback), |
| 66 flag_(flag) { } | 66 flag_(flag) { } |
| 67 | 67 |
| 68 bool Fire(WaitableEvent* event) override { | 68 bool Fire(WaitableEvent* event) override { |
| 69 // Post the callback if we haven't been cancelled. | 69 // Post the callback if we haven't been cancelled. |
| 70 if (!flag_->value()) { | 70 if (!flag_->value()) { |
| 71 message_loop_->PostTask(FROM_HERE, callback_); | 71 message_loop_->task_runner()->PostTask(FROM_HERE, callback_); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // We are removed from the wait-list by the WaitableEvent itself. It only | 74 // We are removed from the wait-list by the WaitableEvent itself. It only |
| 75 // remains to delete ourselves. | 75 // remains to delete ourselves. |
| 76 delete this; | 76 delete this; |
| 77 | 77 |
| 78 // We can always return true because an AsyncWaiter is never in two | 78 // We can always return true because an AsyncWaiter is never in two |
| 79 // different wait-lists at the same time. | 79 // different wait-lists at the same time. |
| 80 return true; | 80 return true; |
| 81 } | 81 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 AutoLock locked(kernel->lock_); | 151 AutoLock locked(kernel->lock_); |
| 152 | 152 |
| 153 event_ = event; | 153 event_ = event; |
| 154 | 154 |
| 155 if (kernel->signaled_) { | 155 if (kernel->signaled_) { |
| 156 if (!kernel->manual_reset_) | 156 if (!kernel->manual_reset_) |
| 157 kernel->signaled_ = false; | 157 kernel->signaled_ = false; |
| 158 | 158 |
| 159 // No hairpinning - we can't call the delegate directly here. We have to | 159 // No hairpinning - we can't call the delegate directly here. We have to |
| 160 // enqueue a task on the MessageLoop as normal. | 160 // enqueue a task on the MessageLoop as normal. |
| 161 current_ml->PostTask(FROM_HERE, internal_callback_); | 161 current_ml->task_runner()->PostTask(FROM_HERE, internal_callback_); |
| 162 return true; | 162 return true; |
| 163 } | 163 } |
| 164 | 164 |
| 165 message_loop_ = current_ml; | 165 message_loop_ = current_ml; |
| 166 current_ml->AddDestructionObserver(this); | 166 current_ml->AddDestructionObserver(this); |
| 167 | 167 |
| 168 kernel_ = kernel; | 168 kernel_ = kernel; |
| 169 waiter_ = new AsyncWaiter(current_ml, internal_callback_, cancel_flag_.get()); | 169 waiter_ = new AsyncWaiter(current_ml, internal_callback_, cancel_flag_.get()); |
| 170 event->Enqueue(waiter_); | 170 event->Enqueue(waiter_); |
| 171 | 171 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 // ----------------------------------------------------------------------------- | 260 // ----------------------------------------------------------------------------- |
| 261 // This is called when the MessageLoop which the callback will be run it is | 261 // This is called when the MessageLoop which the callback will be run it is |
| 262 // deleted. We need to cancel the callback as if we had been deleted, but we | 262 // deleted. We need to cancel the callback as if we had been deleted, but we |
| 263 // will still be deleted at some point in the future. | 263 // will still be deleted at some point in the future. |
| 264 // ----------------------------------------------------------------------------- | 264 // ----------------------------------------------------------------------------- |
| 265 void WaitableEventWatcher::WillDestroyCurrentMessageLoop() { | 265 void WaitableEventWatcher::WillDestroyCurrentMessageLoop() { |
| 266 StopWatching(); | 266 StopWatching(); |
| 267 } | 267 } |
| 268 | 268 |
| 269 } // namespace base | 269 } // namespace base |
| OLD | NEW |