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/memory/singleton.h" |
12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
14 #include "base/message_loop/message_loop_proxy.h" | 15 #include "base/message_loop/message_loop_proxy.h" |
15 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "mojo/common/environment_data.h" | |
19 #include "mojo/common/message_pump_mojo.h" | 19 #include "mojo/common/message_pump_mojo.h" |
20 #include "mojo/common/message_pump_mojo_handler.h" | 20 #include "mojo/common/message_pump_mojo_handler.h" |
21 #include "mojo/common/time_helper.h" | 21 #include "mojo/common/time_helper.h" |
22 | 22 |
23 namespace mojo { | 23 namespace mojo { |
24 namespace common { | 24 namespace common { |
25 | 25 |
26 typedef int WatcherID; | 26 typedef int WatcherID; |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 const char kWatcherThreadName[] = "handle-watcher-thread"; | 30 const char kWatcherThreadName[] = "handle-watcher-thread"; |
31 | 31 |
32 const char kWatcherThreadManagerKey[] = "watcher-thread-manager"; | |
33 | |
34 // TODO(sky): this should be unnecessary once MessageLoop has been refactored. | 32 // TODO(sky): this should be unnecessary once MessageLoop has been refactored. |
35 MessagePumpMojo* message_pump_mojo = NULL; | 33 MessagePumpMojo* message_pump_mojo = NULL; |
36 | 34 |
37 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() { | 35 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() { |
38 message_pump_mojo = new MessagePumpMojo; | 36 message_pump_mojo = new MessagePumpMojo; |
39 return scoped_ptr<base::MessagePump>(message_pump_mojo).Pass(); | 37 return scoped_ptr<base::MessagePump>(message_pump_mojo).Pass(); |
40 } | 38 } |
41 | 39 |
42 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) { | 40 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) { |
43 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() : | 41 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() : |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 WatcherID StartWatching(const Handle& handle, | 167 WatcherID StartWatching(const Handle& handle, |
170 MojoWaitFlags wait_flags, | 168 MojoWaitFlags wait_flags, |
171 base::TimeTicks deadline, | 169 base::TimeTicks deadline, |
172 const base::Callback<void(MojoResult)>& callback); | 170 const base::Callback<void(MojoResult)>& callback); |
173 | 171 |
174 // Stops watching a handle. | 172 // Stops watching a handle. |
175 // This may be invoked on any thread. | 173 // This may be invoked on any thread. |
176 void StopWatching(WatcherID watcher_id); | 174 void StopWatching(WatcherID watcher_id); |
177 | 175 |
178 private: | 176 private: |
| 177 friend struct DefaultSingletonTraits<WatcherThreadManager>; |
179 WatcherThreadManager(); | 178 WatcherThreadManager(); |
180 | 179 |
181 base::Thread thread_; | 180 base::Thread thread_; |
182 | 181 |
183 base::AtomicSequenceNumber watcher_id_generator_; | 182 base::AtomicSequenceNumber watcher_id_generator_; |
184 | 183 |
185 WatcherBackend backend_; | 184 WatcherBackend backend_; |
186 | 185 |
187 DISALLOW_COPY_AND_ASSIGN(WatcherThreadManager); | 186 DISALLOW_COPY_AND_ASSIGN(WatcherThreadManager); |
188 }; | 187 }; |
189 | 188 |
190 struct WatcherThreadManagerData : EnvironmentData::Data { | |
191 scoped_ptr<WatcherThreadManager> thread_manager; | |
192 }; | |
193 | |
194 WatcherThreadManager::~WatcherThreadManager() { | 189 WatcherThreadManager::~WatcherThreadManager() { |
195 thread_.Stop(); | 190 thread_.Stop(); |
196 } | 191 } |
197 | 192 |
198 static base::LazyInstance<base::Lock> thread_lookup_lock = | |
199 LAZY_INSTANCE_INITIALIZER; | |
200 | |
201 WatcherThreadManager* WatcherThreadManager::GetInstance() { | 193 WatcherThreadManager* WatcherThreadManager::GetInstance() { |
202 base::AutoLock auto_lock(thread_lookup_lock.Get()); | 194 return Singleton<WatcherThreadManager>::get(); |
203 WatcherThreadManagerData* data = static_cast<WatcherThreadManagerData*>( | |
204 EnvironmentData::GetInstance()->GetData(kWatcherThreadManagerKey)); | |
205 if (!data) { | |
206 data = new WatcherThreadManagerData; | |
207 data->thread_manager.reset(new WatcherThreadManager); | |
208 EnvironmentData::GetInstance()->SetData( | |
209 kWatcherThreadManagerKey, | |
210 scoped_ptr<EnvironmentData::Data>(data)); | |
211 } | |
212 return data->thread_manager.get(); | |
213 } | 195 } |
214 | 196 |
215 WatcherID WatcherThreadManager::StartWatching( | 197 WatcherID WatcherThreadManager::StartWatching( |
216 const Handle& handle, | 198 const Handle& handle, |
217 MojoWaitFlags wait_flags, | 199 MojoWaitFlags wait_flags, |
218 base::TimeTicks deadline, | 200 base::TimeTicks deadline, |
219 const base::Callback<void(MojoResult)>& callback) { | 201 const base::Callback<void(MojoResult)>& callback) { |
220 WatchData data; | 202 WatchData data; |
221 data.id = watcher_id_generator_.GetNext(); | 203 data.id = watcher_id_generator_.GetNext(); |
222 data.handle = handle; | 204 data.handle = handle; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 303 |
322 state_.reset(new State(this, handle, wait_flags, deadline, callback)); | 304 state_.reset(new State(this, handle, wait_flags, deadline, callback)); |
323 } | 305 } |
324 | 306 |
325 void HandleWatcher::Stop() { | 307 void HandleWatcher::Stop() { |
326 state_.reset(); | 308 state_.reset(); |
327 } | 309 } |
328 | 310 |
329 } // namespace common | 311 } // namespace common |
330 } // namespace mojo | 312 } // namespace mojo |
OLD | NEW |