Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: mojo/message_pump/handle_watcher.cc

Issue 1841863002: Update monet. (Closed) Base URL: https://github.com/domokit/monet.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/message_pump/handle_watcher.h ('k') | mojo/message_pump/handle_watcher_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/message_pump/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"
12 #include "base/logging.h" 11 #include "base/logging.h"
13 #include "base/macros.h" 12 #include "base/macros.h"
14 #include "base/memory/singleton.h" 13 #include "base/memory/singleton.h"
15 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
17 #include "base/message_loop/message_loop_proxy.h"
18 #include "base/synchronization/lock.h" 16 #include "base/synchronization/lock.h"
19 #include "base/synchronization/waitable_event.h" 17 #include "base/synchronization/waitable_event.h"
18 #include "base/thread_task_runner_handle.h"
20 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
21 #include "base/threading/thread_restrictions.h" 20 #include "base/threading/thread_restrictions.h"
22 #include "base/time/time.h" 21 #include "base/time/time.h"
23 #include "mojo/common/message_pump_mojo.h" 22 #include "mojo/message_pump/message_pump_mojo.h"
24 #include "mojo/common/message_pump_mojo_handler.h" 23 #include "mojo/message_pump/message_pump_mojo_handler.h"
25 #include "mojo/common/time_helper.h" 24 #include "mojo/message_pump/time_helper.h"
26 25
27 namespace mojo { 26 namespace mojo {
28 namespace common { 27 namespace common {
29 28
30 typedef int WatcherID; 29 typedef int WatcherID;
31 30
32 namespace { 31 namespace {
33 32
34 const char kWatcherThreadName[] = "handle-watcher-thread"; 33 const char kWatcherThreadName[] = "handle-watcher-thread";
35 34
36 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) { 35 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) {
37 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() : 36 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() :
38 internal::NowTicks() + base::TimeDelta::FromMicroseconds(deadline); 37 internal::NowTicks() + base::TimeDelta::FromMicroseconds(deadline);
39 } 38 }
40 39
41 // Tracks the data for a single call to Start(). 40 // Tracks the data for a single call to Start().
42 struct WatchData { 41 struct WatchData {
43 WatchData() 42 WatchData()
44 : id(0), 43 : id(0),
45 handle_signals(MOJO_HANDLE_SIGNAL_NONE), 44 handle_signals(MOJO_HANDLE_SIGNAL_NONE),
46 message_loop(NULL) {} 45 task_runner(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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 } 115 }
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 data.task_runner->PostTask(FROM_HERE, base::Bind(data.callback, result));
127 data.message_loop->PostTask(FROM_HERE, base::Bind(data.callback, result));
128 } 126 }
129 127
130 bool WatcherBackend::GetMojoHandleByWatcherID(WatcherID watcher_id, 128 bool WatcherBackend::GetMojoHandleByWatcherID(WatcherID watcher_id,
131 Handle* handle) const { 129 Handle* handle) const {
132 for (HandleToWatchDataMap::const_iterator i = handle_to_data_.begin(); 130 for (HandleToWatchDataMap::const_iterator i = handle_to_data_.begin();
133 i != handle_to_data_.end(); ++i) { 131 i != handle_to_data_.end(); ++i) {
134 if (i->second.id == watcher_id) { 132 if (i->second.id == watcher_id) {
135 *handle = i->second.handle; 133 *handle = i->second.handle;
136 return true; 134 return true;
137 } 135 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 MojoHandleSignals handle_signals, 229 MojoHandleSignals handle_signals,
232 base::TimeTicks deadline, 230 base::TimeTicks deadline,
233 const base::Callback<void(MojoResult)>& callback) { 231 const base::Callback<void(MojoResult)>& callback) {
234 RequestData request_data; 232 RequestData request_data;
235 request_data.type = REQUEST_START; 233 request_data.type = REQUEST_START;
236 request_data.start_data.id = watcher_id_generator_.GetNext(); 234 request_data.start_data.id = watcher_id_generator_.GetNext();
237 request_data.start_data.handle = handle; 235 request_data.start_data.handle = handle;
238 request_data.start_data.callback = callback; 236 request_data.start_data.callback = callback;
239 request_data.start_data.handle_signals = handle_signals; 237 request_data.start_data.handle_signals = handle_signals;
240 request_data.start_data.deadline = deadline; 238 request_data.start_data.deadline = deadline;
241 request_data.start_data.message_loop = base::MessageLoopProxy::current(); 239 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); 240 AddRequest(request_data);
245 return request_data.start_data.id; 241 return request_data.start_data.id;
246 } 242 }
247 243
248 void WatcherThreadManager::StopWatching(WatcherID watcher_id) { 244 void WatcherThreadManager::StopWatching(WatcherID watcher_id) {
249 // Handle the case of StartWatching() followed by StopWatching() before 245 // Handle the case of StartWatching() followed by StopWatching() before
250 // |thread_| woke up. 246 // |thread_| woke up.
251 { 247 {
252 base::AutoLock auto_lock(lock_); 248 base::AutoLock auto_lock(lock_);
253 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { 249 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) {
(...skipping 19 matching lines...) Expand all
273 269
274 void WatcherThreadManager::AddRequest(const RequestData& data) { 270 void WatcherThreadManager::AddRequest(const RequestData& data) {
275 { 271 {
276 base::AutoLock auto_lock(lock_); 272 base::AutoLock auto_lock(lock_);
277 const bool was_empty = requests_.empty(); 273 const bool was_empty = requests_.empty();
278 requests_.push_back(data); 274 requests_.push_back(data);
279 if (!was_empty) 275 if (!was_empty)
280 return; 276 return;
281 } 277 }
282 // We own |thread_|, so it's safe to use Unretained() here. 278 // We own |thread_|, so it's safe to use Unretained() here.
283 thread_.message_loop()->PostTask( 279 thread_.task_runner()->PostTask(
284 FROM_HERE, 280 FROM_HERE,
285 base::Bind(&WatcherThreadManager::ProcessRequestsOnBackendThread, 281 base::Bind(&WatcherThreadManager::ProcessRequestsOnBackendThread,
286 base::Unretained(this))); 282 base::Unretained(this)));
287 } 283 }
288 284
289 void WatcherThreadManager::ProcessRequestsOnBackendThread() { 285 void WatcherThreadManager::ProcessRequestsOnBackendThread() {
290 DCHECK_EQ(thread_.message_loop(), base::MessageLoop::current()); 286 DCHECK_EQ(thread_.message_loop(), base::MessageLoop::current());
291 287
292 Requests requests; 288 Requests requests;
293 { 289 {
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 this, handle, handle_signals, deadline, callback)); 465 this, handle, handle_signals, deadline, callback));
470 } 466 }
471 } 467 }
472 468
473 void HandleWatcher::Stop() { 469 void HandleWatcher::Stop() {
474 state_.reset(); 470 state_.reset();
475 } 471 }
476 472
477 } // namespace common 473 } // namespace common
478 } // namespace mojo 474 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/message_pump/handle_watcher.h ('k') | mojo/message_pump/handle_watcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698