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