| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/common/handle_watcher.h" | 5 #include "mojo/common/handle_watcher.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/atomic_sequence_num.h" | 9 #include "base/atomic_sequence_num.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/message_loop/message_loop_proxy.h" | |
| 18 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 19 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
| 19 #include "base/thread_task_runner_handle.h" |
| 20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
| 21 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" |
| 22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "mojo/common/message_pump_mojo.h" | 23 #include "mojo/common/message_pump_mojo.h" |
| 24 #include "mojo/common/message_pump_mojo_handler.h" | 24 #include "mojo/common/message_pump_mojo_handler.h" |
| 25 #include "mojo/common/time_helper.h" | 25 #include "mojo/common/time_helper.h" |
| 26 | 26 |
| 27 namespace mojo { | 27 namespace mojo { |
| 28 namespace common { | 28 namespace common { |
| 29 | 29 |
| 30 typedef int WatcherID; | 30 typedef int WatcherID; |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 const char kWatcherThreadName[] = "handle-watcher-thread"; | 34 const char kWatcherThreadName[] = "handle-watcher-thread"; |
| 35 | 35 |
| 36 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) { | 36 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) { |
| 37 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() : | 37 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() : |
| 38 internal::NowTicks() + base::TimeDelta::FromMicroseconds(deadline); | 38 internal::NowTicks() + base::TimeDelta::FromMicroseconds(deadline); |
| 39 } | 39 } |
| 40 | 40 |
| 41 // Tracks the data for a single call to Start(). | 41 // Tracks the data for a single call to Start(). |
| 42 struct WatchData { | 42 struct WatchData { |
| 43 WatchData() | 43 WatchData() |
| 44 : id(0), | 44 : id(0), |
| 45 handle_signals(MOJO_HANDLE_SIGNAL_NONE), | 45 handle_signals(MOJO_HANDLE_SIGNAL_NONE), |
| 46 message_loop(NULL) {} | 46 task_runner(NULL) {} |
| 47 | 47 |
| 48 WatcherID id; | 48 WatcherID id; |
| 49 Handle handle; | 49 Handle handle; |
| 50 MojoHandleSignals handle_signals; | 50 MojoHandleSignals handle_signals; |
| 51 base::TimeTicks deadline; | 51 base::TimeTicks deadline; |
| 52 base::Callback<void(MojoResult)> callback; | 52 base::Callback<void(MojoResult)> callback; |
| 53 scoped_refptr<base::MessageLoopProxy> message_loop; | 53 scoped_refptr<base::SingleThreadTaskRunner> task_runner; |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 // WatcherBackend -------------------------------------------------------------- | 56 // WatcherBackend -------------------------------------------------------------- |
| 57 | 57 |
| 58 // WatcherBackend is responsible for managing the requests and interacting with | 58 // WatcherBackend is responsible for managing the requests and interacting with |
| 59 // MessagePumpMojo. All access (outside of creation/destruction) is done on the | 59 // MessagePumpMojo. All access (outside of creation/destruction) is done on the |
| 60 // thread WatcherThreadManager creates. | 60 // thread WatcherThreadManager creates. |
| 61 class WatcherBackend : public MessagePumpMojoHandler { | 61 class WatcherBackend : public MessagePumpMojoHandler { |
| 62 public: | 62 public: |
| 63 WatcherBackend(); | 63 WatcherBackend(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 } | 116 } |
| 117 | 117 |
| 118 void WatcherBackend::RemoveAndNotify(const Handle& handle, | 118 void WatcherBackend::RemoveAndNotify(const Handle& handle, |
| 119 MojoResult result) { | 119 MojoResult result) { |
| 120 if (handle_to_data_.count(handle) == 0) | 120 if (handle_to_data_.count(handle) == 0) |
| 121 return; | 121 return; |
| 122 | 122 |
| 123 const WatchData data(handle_to_data_[handle]); | 123 const WatchData data(handle_to_data_[handle]); |
| 124 handle_to_data_.erase(handle); | 124 handle_to_data_.erase(handle); |
| 125 MessagePumpMojo::current()->RemoveHandler(handle); | 125 MessagePumpMojo::current()->RemoveHandler(handle); |
| 126 data.message_loop->PostTask(FROM_HERE, base::Bind(data.callback, result)); | 126 data.task_runner->PostTask(FROM_HERE, base::Bind(data.callback, result)); |
| 127 } | 127 } |
| 128 | 128 |
| 129 bool WatcherBackend::GetMojoHandleByWatcherID(WatcherID watcher_id, | 129 bool WatcherBackend::GetMojoHandleByWatcherID(WatcherID watcher_id, |
| 130 Handle* handle) const { | 130 Handle* handle) const { |
| 131 for (HandleToWatchDataMap::const_iterator i = handle_to_data_.begin(); | 131 for (HandleToWatchDataMap::const_iterator i = handle_to_data_.begin(); |
| 132 i != handle_to_data_.end(); ++i) { | 132 i != handle_to_data_.end(); ++i) { |
| 133 if (i->second.id == watcher_id) { | 133 if (i->second.id == watcher_id) { |
| 134 *handle = i->second.handle; | 134 *handle = i->second.handle; |
| 135 return true; | 135 return true; |
| 136 } | 136 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 MojoHandleSignals handle_signals, | 230 MojoHandleSignals handle_signals, |
| 231 base::TimeTicks deadline, | 231 base::TimeTicks deadline, |
| 232 const base::Callback<void(MojoResult)>& callback) { | 232 const base::Callback<void(MojoResult)>& callback) { |
| 233 RequestData request_data; | 233 RequestData request_data; |
| 234 request_data.type = REQUEST_START; | 234 request_data.type = REQUEST_START; |
| 235 request_data.start_data.id = watcher_id_generator_.GetNext(); | 235 request_data.start_data.id = watcher_id_generator_.GetNext(); |
| 236 request_data.start_data.handle = handle; | 236 request_data.start_data.handle = handle; |
| 237 request_data.start_data.callback = callback; | 237 request_data.start_data.callback = callback; |
| 238 request_data.start_data.handle_signals = handle_signals; | 238 request_data.start_data.handle_signals = handle_signals; |
| 239 request_data.start_data.deadline = deadline; | 239 request_data.start_data.deadline = deadline; |
| 240 request_data.start_data.message_loop = base::MessageLoopProxy::current(); | 240 request_data.start_data.task_runner = base::ThreadTaskRunnerHandle::Get(); |
| 241 DCHECK_NE(static_cast<base::MessageLoopProxy*>(NULL), | |
| 242 request_data.start_data.message_loop.get()); | |
| 243 AddRequest(request_data); | 241 AddRequest(request_data); |
| 244 return request_data.start_data.id; | 242 return request_data.start_data.id; |
| 245 } | 243 } |
| 246 | 244 |
| 247 void WatcherThreadManager::StopWatching(WatcherID watcher_id) { | 245 void WatcherThreadManager::StopWatching(WatcherID watcher_id) { |
| 248 // Handle the case of StartWatching() followed by StopWatching() before | 246 // Handle the case of StartWatching() followed by StopWatching() before |
| 249 // |thread_| woke up. | 247 // |thread_| woke up. |
| 250 { | 248 { |
| 251 base::AutoLock auto_lock(lock_); | 249 base::AutoLock auto_lock(lock_); |
| 252 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { | 250 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 272 | 270 |
| 273 void WatcherThreadManager::AddRequest(const RequestData& data) { | 271 void WatcherThreadManager::AddRequest(const RequestData& data) { |
| 274 { | 272 { |
| 275 base::AutoLock auto_lock(lock_); | 273 base::AutoLock auto_lock(lock_); |
| 276 const bool was_empty = requests_.empty(); | 274 const bool was_empty = requests_.empty(); |
| 277 requests_.push_back(data); | 275 requests_.push_back(data); |
| 278 if (!was_empty) | 276 if (!was_empty) |
| 279 return; | 277 return; |
| 280 } | 278 } |
| 281 // We own |thread_|, so it's safe to use Unretained() here. | 279 // We own |thread_|, so it's safe to use Unretained() here. |
| 282 thread_.message_loop()->PostTask( | 280 thread_.task_runner()->PostTask( |
| 283 FROM_HERE, | 281 FROM_HERE, |
| 284 base::Bind(&WatcherThreadManager::ProcessRequestsOnBackendThread, | 282 base::Bind(&WatcherThreadManager::ProcessRequestsOnBackendThread, |
| 285 base::Unretained(this))); | 283 base::Unretained(this))); |
| 286 } | 284 } |
| 287 | 285 |
| 288 void WatcherThreadManager::ProcessRequestsOnBackendThread() { | 286 void WatcherThreadManager::ProcessRequestsOnBackendThread() { |
| 289 DCHECK_EQ(thread_.message_loop(), base::MessageLoop::current()); | 287 DCHECK_EQ(thread_.message_loop(), base::MessageLoop::current()); |
| 290 | 288 |
| 291 Requests requests; | 289 Requests requests; |
| 292 { | 290 { |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 this, handle, handle_signals, deadline, callback)); | 466 this, handle, handle_signals, deadline, callback)); |
| 469 } | 467 } |
| 470 } | 468 } |
| 471 | 469 |
| 472 void HandleWatcher::Stop() { | 470 void HandleWatcher::Stop() { |
| 473 state_.reset(); | 471 state_.reset(); |
| 474 } | 472 } |
| 475 | 473 |
| 476 } // namespace common | 474 } // namespace common |
| 477 } // namespace mojo | 475 } // namespace mojo |
| OLD | NEW |