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

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

Issue 218583009: Adds a way to associate key/value pairs with the environment (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: make mojo_service_manager depend on mojo_common_lib Created 6 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 | Annotate | Revision Log
« no previous file with comments | « mojo/common/environment_data.cc ('k') | mojo/common/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/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/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/message_loop/message_loop_proxy.h" 14 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/synchronization/lock.h"
15 #include "base/threading/thread.h" 16 #include "base/threading/thread.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "mojo/common/environment_data.h"
17 #include "mojo/common/message_pump_mojo.h" 19 #include "mojo/common/message_pump_mojo.h"
18 #include "mojo/common/message_pump_mojo_handler.h" 20 #include "mojo/common/message_pump_mojo_handler.h"
19 #include "mojo/common/time_helper.h" 21 #include "mojo/common/time_helper.h"
20 22
21 namespace mojo { 23 namespace mojo {
22 namespace common { 24 namespace common {
23 25
24 typedef int WatcherID; 26 typedef int WatcherID;
25 27
26 namespace { 28 namespace {
27 29
28 const char kWatcherThreadName[] = "handle-watcher-thread"; 30 const char kWatcherThreadName[] = "handle-watcher-thread";
29 31
32 const char kWatcherThreadManagerKey[] = "watcher-thread-manager";
33
30 // TODO(sky): this should be unnecessary once MessageLoop has been refactored. 34 // TODO(sky): this should be unnecessary once MessageLoop has been refactored.
31 MessagePumpMojo* message_pump_mojo = NULL; 35 MessagePumpMojo* message_pump_mojo = NULL;
32 36
33 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() { 37 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() {
34 message_pump_mojo = new MessagePumpMojo; 38 message_pump_mojo = new MessagePumpMojo;
35 return scoped_ptr<base::MessagePump>(message_pump_mojo).Pass(); 39 return scoped_ptr<base::MessagePump>(message_pump_mojo).Pass();
36 } 40 }
37 41
38 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) { 42 base::TimeTicks MojoDeadlineToTimeTicks(MojoDeadline deadline) {
39 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() : 43 return deadline == MOJO_DEADLINE_INDEFINITE ? base::TimeTicks() :
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 void WatcherBackend::OnHandleError(const Handle& handle, MojoResult result) { 150 void WatcherBackend::OnHandleError(const Handle& handle, MojoResult result) {
147 RemoveAndNotify(handle, result); 151 RemoveAndNotify(handle, result);
148 } 152 }
149 153
150 // WatcherThreadManager -------------------------------------------------------- 154 // WatcherThreadManager --------------------------------------------------------
151 155
152 // WatcherThreadManager manages the background thread that listens for handles 156 // WatcherThreadManager manages the background thread that listens for handles
153 // to be ready. All requests are handled by WatcherBackend. 157 // to be ready. All requests are handled by WatcherBackend.
154 class WatcherThreadManager { 158 class WatcherThreadManager {
155 public: 159 public:
160 ~WatcherThreadManager();
161
156 // Returns the shared instance. 162 // Returns the shared instance.
157 static WatcherThreadManager* GetInstance(); 163 static WatcherThreadManager* GetInstance();
158 164
159 // Starts watching the requested handle. Returns a unique ID that is used to 165 // 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 166 // stop watching the handle. When the handle is ready |callback| is notified
161 // on the thread StartWatching() was invoked on. 167 // on the thread StartWatching() was invoked on.
162 // This may be invoked on any thread. 168 // This may be invoked on any thread.
163 WatcherID StartWatching(const Handle& handle, 169 WatcherID StartWatching(const Handle& handle,
164 MojoWaitFlags wait_flags, 170 MojoWaitFlags wait_flags,
165 base::TimeTicks deadline, 171 base::TimeTicks deadline,
166 const base::Callback<void(MojoResult)>& callback); 172 const base::Callback<void(MojoResult)>& callback);
167 173
168 // Stops watching a handle. 174 // Stops watching a handle.
169 // This may be invoked on any thread. 175 // This may be invoked on any thread.
170 void StopWatching(WatcherID watcher_id); 176 void StopWatching(WatcherID watcher_id);
171 177
172 private: 178 private:
173 friend struct base::DefaultLazyInstanceTraits<WatcherThreadManager>;
174
175 WatcherThreadManager(); 179 WatcherThreadManager();
176 ~WatcherThreadManager();
177 180
178 base::Thread thread_; 181 base::Thread thread_;
179 182
180 base::AtomicSequenceNumber watcher_id_generator_; 183 base::AtomicSequenceNumber watcher_id_generator_;
181 184
182 WatcherBackend backend_; 185 WatcherBackend backend_;
183 186
184 DISALLOW_COPY_AND_ASSIGN(WatcherThreadManager); 187 DISALLOW_COPY_AND_ASSIGN(WatcherThreadManager);
185 }; 188 };
186 189
190 struct WatcherThreadManagerData : EnvironmentData::Data {
191 scoped_ptr<WatcherThreadManager> thread_manager;
192 };
193
194 WatcherThreadManager::~WatcherThreadManager() {
195 thread_.Stop();
196 }
197
198 static base::LazyInstance<base::Lock> thread_lookup_lock =
199 LAZY_INSTANCE_INITIALIZER;
200
187 WatcherThreadManager* WatcherThreadManager::GetInstance() { 201 WatcherThreadManager* WatcherThreadManager::GetInstance() {
188 static base::LazyInstance<WatcherThreadManager> instance = 202 base::AutoLock auto_lock(thread_lookup_lock.Get());
189 LAZY_INSTANCE_INITIALIZER; 203 WatcherThreadManagerData* data = static_cast<WatcherThreadManagerData*>(
190 return &instance.Get(); 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();
191 } 213 }
192 214
193 WatcherID WatcherThreadManager::StartWatching( 215 WatcherID WatcherThreadManager::StartWatching(
194 const Handle& handle, 216 const Handle& handle,
195 MojoWaitFlags wait_flags, 217 MojoWaitFlags wait_flags,
196 base::TimeTicks deadline, 218 base::TimeTicks deadline,
197 const base::Callback<void(MojoResult)>& callback) { 219 const base::Callback<void(MojoResult)>& callback) {
198 WatchData data; 220 WatchData data;
199 data.id = watcher_id_generator_.GetNext(); 221 data.id = watcher_id_generator_.GetNext();
200 data.handle = handle; 222 data.handle = handle;
(...skipping 21 matching lines...) Expand all
222 watcher_id)); 244 watcher_id));
223 } 245 }
224 246
225 WatcherThreadManager::WatcherThreadManager() 247 WatcherThreadManager::WatcherThreadManager()
226 : thread_(kWatcherThreadName) { 248 : thread_(kWatcherThreadName) {
227 base::Thread::Options thread_options; 249 base::Thread::Options thread_options;
228 thread_options.message_pump_factory = base::Bind(&CreateMessagePumpMojo); 250 thread_options.message_pump_factory = base::Bind(&CreateMessagePumpMojo);
229 thread_.StartWithOptions(thread_options); 251 thread_.StartWithOptions(thread_options);
230 } 252 }
231 253
232 WatcherThreadManager::~WatcherThreadManager() {
233 thread_.Stop();
234 }
235
236 } // namespace 254 } // namespace
237 255
238 // HandleWatcher::StartState --------------------------------------------------- 256 // HandleWatcher::StartState ---------------------------------------------------
239 257
240 // Contains the information passed to Start(). 258 // Contains the information passed to Start().
241 struct HandleWatcher::StartState { 259 struct HandleWatcher::StartState {
242 explicit StartState(HandleWatcher* watcher) : weak_factory(watcher) { 260 explicit StartState(HandleWatcher* watcher) : weak_factory(watcher) {
243 } 261 }
244 262
245 ~StartState() { 263 ~StartState() {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 void HandleWatcher::OnHandleReady(MojoResult result) { 318 void HandleWatcher::OnHandleReady(MojoResult result) {
301 DCHECK(start_state_.get()); 319 DCHECK(start_state_.get());
302 scoped_ptr<StartState> old_state(start_state_.Pass()); 320 scoped_ptr<StartState> old_state(start_state_.Pass());
303 old_state->callback.Run(result); 321 old_state->callback.Run(result);
304 322
305 // NOTE: We may have been deleted during callback execution. 323 // NOTE: We may have been deleted during callback execution.
306 } 324 }
307 325
308 } // namespace common 326 } // namespace common
309 } // namespace mojo 327 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/common/environment_data.cc ('k') | mojo/common/handle_watcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698