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