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), |
45 handle_signals(MOJO_HANDLE_SIGNAL_NONE), | 46 handle_signals(MOJO_HANDLE_SIGNAL_NONE), |
46 message_loop(NULL) {} | 47 task_runner(NULL) {} |
47 | 48 |
48 WatcherID id; | 49 WatcherID id; |
49 Handle handle; | 50 Handle handle; |
50 MojoHandleSignals handle_signals; | 51 MojoHandleSignals handle_signals; |
51 base::TimeTicks deadline; | 52 base::TimeTicks deadline; |
52 base::Callback<void(MojoResult)> callback; | 53 base::Callback<void(MojoResult)> callback; |
53 scoped_refptr<base::MessageLoopProxy> message_loop; | 54 scoped_refptr<base::SingleThreadTaskRunner> task_runner; |
54 }; | 55 }; |
55 | 56 |
56 // WatcherBackend -------------------------------------------------------------- | 57 // WatcherBackend -------------------------------------------------------------- |
57 | 58 |
58 // WatcherBackend is responsible for managing the requests and interacting with | 59 // WatcherBackend is responsible for managing the requests and interacting with |
59 // MessagePumpMojo. All access (outside of creation/destruction) is done on the | 60 // MessagePumpMojo. All access (outside of creation/destruction) is done on the |
60 // thread WatcherThreadManager creates. | 61 // thread WatcherThreadManager creates. |
61 class WatcherBackend : public MessagePumpMojoHandler { | 62 class WatcherBackend : public MessagePumpMojoHandler { |
62 public: | 63 public: |
63 WatcherBackend(); | 64 WatcherBackend(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 118 |
118 void WatcherBackend::RemoveAndNotify(const Handle& handle, | 119 void WatcherBackend::RemoveAndNotify(const Handle& handle, |
119 MojoResult result) { | 120 MojoResult result) { |
120 if (handle_to_data_.count(handle) == 0) | 121 if (handle_to_data_.count(handle) == 0) |
121 return; | 122 return; |
122 | 123 |
123 const WatchData data(handle_to_data_[handle]); | 124 const WatchData data(handle_to_data_[handle]); |
124 handle_to_data_.erase(handle); | 125 handle_to_data_.erase(handle); |
125 MessagePumpMojo::current()->RemoveHandler(handle); | 126 MessagePumpMojo::current()->RemoveHandler(handle); |
126 | 127 |
127 data.message_loop->PostTask(FROM_HERE, base::Bind(data.callback, result)); | 128 data.task_runner->PostTask(FROM_HERE, base::Bind(data.callback, result)); |
128 } | 129 } |
129 | 130 |
130 bool WatcherBackend::GetMojoHandleByWatcherID(WatcherID watcher_id, | 131 bool WatcherBackend::GetMojoHandleByWatcherID(WatcherID watcher_id, |
131 Handle* handle) const { | 132 Handle* handle) const { |
132 for (HandleToWatchDataMap::const_iterator i = handle_to_data_.begin(); | 133 for (HandleToWatchDataMap::const_iterator i = handle_to_data_.begin(); |
133 i != handle_to_data_.end(); ++i) { | 134 i != handle_to_data_.end(); ++i) { |
134 if (i->second.id == watcher_id) { | 135 if (i->second.id == watcher_id) { |
135 *handle = i->second.handle; | 136 *handle = i->second.handle; |
136 return true; | 137 return true; |
137 } | 138 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 MojoHandleSignals handle_signals, | 232 MojoHandleSignals handle_signals, |
232 base::TimeTicks deadline, | 233 base::TimeTicks deadline, |
233 const base::Callback<void(MojoResult)>& callback) { | 234 const base::Callback<void(MojoResult)>& callback) { |
234 RequestData request_data; | 235 RequestData request_data; |
235 request_data.type = REQUEST_START; | 236 request_data.type = REQUEST_START; |
236 request_data.start_data.id = watcher_id_generator_.GetNext(); | 237 request_data.start_data.id = watcher_id_generator_.GetNext(); |
237 request_data.start_data.handle = handle; | 238 request_data.start_data.handle = handle; |
238 request_data.start_data.callback = callback; | 239 request_data.start_data.callback = callback; |
239 request_data.start_data.handle_signals = handle_signals; | 240 request_data.start_data.handle_signals = handle_signals; |
240 request_data.start_data.deadline = deadline; | 241 request_data.start_data.deadline = deadline; |
241 request_data.start_data.message_loop = base::MessageLoopProxy::current(); | 242 request_data.start_data.task_runner = base::ThreadTaskRunnerHandle::Get(); |
242 DCHECK_NE(static_cast<base::MessageLoopProxy*>(NULL), | 243 DCHECK_NE(static_cast<base::SingleThreadTaskRunner*>(NULL), |
243 request_data.start_data.message_loop.get()); | 244 request_data.start_data.task_runner.get()); |
244 AddRequest(request_data); | 245 AddRequest(request_data); |
245 return request_data.start_data.id; | 246 return request_data.start_data.id; |
246 } | 247 } |
247 | 248 |
248 void WatcherThreadManager::StopWatching(WatcherID watcher_id) { | 249 void WatcherThreadManager::StopWatching(WatcherID watcher_id) { |
249 // Handle the case of StartWatching() followed by StopWatching() before | 250 // Handle the case of StartWatching() followed by StopWatching() before |
250 // |thread_| woke up. | 251 // |thread_| woke up. |
251 { | 252 { |
252 base::AutoLock auto_lock(lock_); | 253 base::AutoLock auto_lock(lock_); |
253 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { | 254 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 this, handle, handle_signals, deadline, callback)); | 470 this, handle, handle_signals, deadline, callback)); |
470 } | 471 } |
471 } | 472 } |
472 | 473 |
473 void HandleWatcher::Stop() { | 474 void HandleWatcher::Stop() { |
474 state_.reset(); | 475 state_.reset(); |
475 } | 476 } |
476 | 477 |
477 } // namespace common | 478 } // namespace common |
478 } // namespace mojo | 479 } // namespace mojo |
OLD | NEW |