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