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" | |
12 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
14 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
15 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
16 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "mojo/common/environment_data.h" | |
17 #include "mojo/common/message_pump_mojo.h" | 17 #include "mojo/common/message_pump_mojo.h" |
18 #include "mojo/common/message_pump_mojo_handler.h" | 18 #include "mojo/common/message_pump_mojo_handler.h" |
19 #include "mojo/common/time_helper.h" | 19 #include "mojo/common/time_helper.h" |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 namespace common { | 22 namespace common { |
23 | 23 |
24 typedef int WatcherID; | 24 typedef int WatcherID; |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 const char kWatcherThreadName[] = "handle-watcher-thread"; | 28 const char kWatcherThreadName[] = "handle-watcher-thread"; |
29 | 29 |
30 const char kWatcherThreadManagerKey[] = "watcher-thread-manager"; | |
31 | |
30 // TODO(sky): this should be unnecessary once MessageLoop has been refactored. | 32 // TODO(sky): this should be unnecessary once MessageLoop has been refactored. |
31 MessagePumpMojo* message_pump_mojo = NULL; | 33 MessagePumpMojo* message_pump_mojo = NULL; |
32 | 34 |
33 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() { | 35 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() { |
34 message_pump_mojo = new MessagePumpMojo; | 36 message_pump_mojo = new MessagePumpMojo; |
35 return scoped_ptr<base::MessagePump>(message_pump_mojo).Pass(); | 37 return scoped_ptr<base::MessagePump>(message_pump_mojo).Pass(); |
36 } | 38 } |
37 | 39 |
38 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) { | 40 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) { |
39 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() : | 41 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() : |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 void WatcherBackend::OnHandleError(const Handle& handle, MojoResult result) { | 148 void WatcherBackend::OnHandleError(const Handle& handle, MojoResult result) { |
147 RemoveAndNotify(handle, result); | 149 RemoveAndNotify(handle, result); |
148 } | 150 } |
149 | 151 |
150 // WatcherThreadManager -------------------------------------------------------- | 152 // WatcherThreadManager -------------------------------------------------------- |
151 | 153 |
152 // WatcherThreadManager manages the background thread that listens for handles | 154 // WatcherThreadManager manages the background thread that listens for handles |
153 // to be ready. All requests are handled by WatcherBackend. | 155 // to be ready. All requests are handled by WatcherBackend. |
154 class WatcherThreadManager { | 156 class WatcherThreadManager { |
155 public: | 157 public: |
158 ~WatcherThreadManager(); | |
159 | |
156 // Returns the shared instance. | 160 // Returns the shared instance. |
157 static WatcherThreadManager* GetInstance(); | 161 static WatcherThreadManager* GetInstance(); |
158 | 162 |
159 // Starts watching the requested handle. Returns a unique ID that is used to | 163 // Starts watching the requested handle. Returns a unique ID that is used to |
160 // stop watching the handle. When the handle is ready |callback| is notified | 164 // stop watching the handle. When the handle is ready |callback| is notified |
161 // on the thread StartWatching() was invoked on. | 165 // on the thread StartWatching() was invoked on. |
162 // This may be invoked on any thread. | 166 // This may be invoked on any thread. |
163 WatcherID StartWatching(const Handle& handle, | 167 WatcherID StartWatching(const Handle& handle, |
164 MojoWaitFlags wait_flags, | 168 MojoWaitFlags wait_flags, |
165 base::TimeTicks deadline, | 169 base::TimeTicks deadline, |
166 const base::Callback<void(MojoResult)>& callback); | 170 const base::Callback<void(MojoResult)>& callback); |
167 | 171 |
168 // Stops watching a handle. | 172 // Stops watching a handle. |
169 // This may be invoked on any thread. | 173 // This may be invoked on any thread. |
170 void StopWatching(WatcherID watcher_id); | 174 void StopWatching(WatcherID watcher_id); |
171 | 175 |
172 private: | 176 private: |
173 friend struct base::DefaultLazyInstanceTraits<WatcherThreadManager>; | |
174 | |
175 WatcherThreadManager(); | 177 WatcherThreadManager(); |
176 ~WatcherThreadManager(); | |
177 | 178 |
178 base::Thread thread_; | 179 base::Thread thread_; |
179 | 180 |
180 base::AtomicSequenceNumber watcher_id_generator_; | 181 base::AtomicSequenceNumber watcher_id_generator_; |
181 | 182 |
182 WatcherBackend backend_; | 183 WatcherBackend backend_; |
183 | 184 |
184 DISALLOW_COPY_AND_ASSIGN(WatcherThreadManager); | 185 DISALLOW_COPY_AND_ASSIGN(WatcherThreadManager); |
185 }; | 186 }; |
186 | 187 |
188 struct WatcherThreadManagerData : EnvironmentData::Data { | |
189 scoped_ptr<WatcherThreadManager> thread_manager; | |
190 }; | |
191 | |
192 WatcherThreadManager::~WatcherThreadManager() { | |
193 thread_.Stop(); | |
194 } | |
195 | |
187 WatcherThreadManager* WatcherThreadManager::GetInstance() { | 196 WatcherThreadManager* WatcherThreadManager::GetInstance() { |
188 static base::LazyInstance<WatcherThreadManager> instance = | 197 WatcherThreadManagerData* data = static_cast<WatcherThreadManagerData*>( |
darin (slow to review)
2014/03/31 23:21:55
it looks like you have to worry about multiple thr
sky
2014/03/31 23:54:40
Done.
| |
189 LAZY_INSTANCE_INITIALIZER; | 198 EnvironmentData::GetInstance()->GetData(kWatcherThreadManagerKey)); |
190 return &instance.Get(); | 199 if (!data) { |
200 data = new WatcherThreadManagerData; | |
201 data->thread_manager.reset(new WatcherThreadManager); | |
202 EnvironmentData::GetInstance()->SetData( | |
203 kWatcherThreadManagerKey, | |
204 scoped_ptr<EnvironmentData::Data>(data)); | |
205 } | |
206 return data->thread_manager.get(); | |
191 } | 207 } |
192 | 208 |
193 WatcherID WatcherThreadManager::StartWatching( | 209 WatcherID WatcherThreadManager::StartWatching( |
194 const Handle& handle, | 210 const Handle& handle, |
195 MojoWaitFlags wait_flags, | 211 MojoWaitFlags wait_flags, |
196 base::TimeTicks deadline, | 212 base::TimeTicks deadline, |
197 const base::Callback<void(MojoResult)>& callback) { | 213 const base::Callback<void(MojoResult)>& callback) { |
198 WatchData data; | 214 WatchData data; |
199 data.id = watcher_id_generator_.GetNext(); | 215 data.id = watcher_id_generator_.GetNext(); |
200 data.handle = handle; | 216 data.handle = handle; |
(...skipping 21 matching lines...) Expand all Loading... | |
222 watcher_id)); | 238 watcher_id)); |
223 } | 239 } |
224 | 240 |
225 WatcherThreadManager::WatcherThreadManager() | 241 WatcherThreadManager::WatcherThreadManager() |
226 : thread_(kWatcherThreadName) { | 242 : thread_(kWatcherThreadName) { |
227 base::Thread::Options thread_options; | 243 base::Thread::Options thread_options; |
228 thread_options.message_pump_factory = base::Bind(&CreateMessagePumpMojo); | 244 thread_options.message_pump_factory = base::Bind(&CreateMessagePumpMojo); |
229 thread_.StartWithOptions(thread_options); | 245 thread_.StartWithOptions(thread_options); |
230 } | 246 } |
231 | 247 |
232 WatcherThreadManager::~WatcherThreadManager() { | |
233 thread_.Stop(); | |
234 } | |
235 | |
236 } // namespace | 248 } // namespace |
237 | 249 |
238 // HandleWatcher::StartState --------------------------------------------------- | 250 // HandleWatcher::StartState --------------------------------------------------- |
239 | 251 |
240 // Contains the information passed to Start(). | 252 // Contains the information passed to Start(). |
241 struct HandleWatcher::StartState { | 253 struct HandleWatcher::StartState { |
242 explicit StartState(HandleWatcher* watcher) : weak_factory(watcher) { | 254 explicit StartState(HandleWatcher* watcher) : weak_factory(watcher) { |
243 } | 255 } |
244 | 256 |
245 ~StartState() { | 257 ~StartState() { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 void HandleWatcher::OnHandleReady(MojoResult result) { | 312 void HandleWatcher::OnHandleReady(MojoResult result) { |
301 DCHECK(start_state_.get()); | 313 DCHECK(start_state_.get()); |
302 scoped_ptr<StartState> old_state(start_state_.Pass()); | 314 scoped_ptr<StartState> old_state(start_state_.Pass()); |
303 old_state->callback.Run(result); | 315 old_state->callback.Run(result); |
304 | 316 |
305 // NOTE: We may have been deleted during callback execution. | 317 // NOTE: We may have been deleted during callback execution. |
306 } | 318 } |
307 | 319 |
308 } // namespace common | 320 } // namespace common |
309 } // namespace mojo | 321 } // namespace mojo |
OLD | NEW |