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

Side by Side Diff: content/browser/browser_thread_impl.cc

Issue 9696034: Avoid creating BrowserThreadGlobals when we don't need to. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: revert d'tor changes, as globals are sure to exist already here Created 8 years, 9 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/browser_thread_impl.h" 5 #include "content/browser/browser_thread_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/atomicops.h" 9 #include "base/atomicops.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 sequence_token_name, from_here, task); 235 sequence_token_name, from_here, task);
236 } 236 }
237 237
238 // static 238 // static
239 base::SequencedWorkerPool* BrowserThread::GetBlockingPool() { 239 base::SequencedWorkerPool* BrowserThread::GetBlockingPool() {
240 return g_globals.Get().blocking_pool; 240 return g_globals.Get().blocking_pool;
241 } 241 }
242 242
243 // static 243 // static
244 bool BrowserThread::IsWellKnownThread(ID identifier) { 244 bool BrowserThread::IsWellKnownThread(ID identifier) {
245 if (g_globals == NULL)
246 return false;
247
245 BrowserThreadGlobals& globals = g_globals.Get(); 248 BrowserThreadGlobals& globals = g_globals.Get();
246 base::AutoLock lock(globals.lock); 249 base::AutoLock lock(globals.lock);
247 return (identifier >= 0 && identifier < ID_COUNT && 250 return (identifier >= 0 && identifier < ID_COUNT &&
248 globals.threads[identifier]); 251 globals.threads[identifier]);
249 } 252 }
250 253
251 // static 254 // static
252 bool BrowserThread::CurrentlyOn(ID identifier) { 255 bool BrowserThread::CurrentlyOn(ID identifier) {
253 // We shouldn't use MessageLoop::current() since it uses LazyInstance which 256 // We shouldn't use MessageLoop::current() since it uses LazyInstance which
254 // may be deleted by ~AtExitManager when a WorkerPool thread calls this 257 // may be deleted by ~AtExitManager when a WorkerPool thread calls this
255 // function. 258 // function.
256 // http://crbug.com/63678 259 // http://crbug.com/63678
257 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; 260 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton;
258 BrowserThreadGlobals& globals = g_globals.Get(); 261 BrowserThreadGlobals& globals = g_globals.Get();
259 base::AutoLock lock(globals.lock); 262 base::AutoLock lock(globals.lock);
260 DCHECK(identifier >= 0 && identifier < ID_COUNT); 263 DCHECK(identifier >= 0 && identifier < ID_COUNT);
261 return globals.threads[identifier] && 264 return globals.threads[identifier] &&
262 globals.threads[identifier]->message_loop() == 265 globals.threads[identifier]->message_loop() ==
263 MessageLoop::current(); 266 MessageLoop::current();
264 } 267 }
265 268
266 // static 269 // static
267 bool BrowserThread::IsMessageLoopValid(ID identifier) { 270 bool BrowserThread::IsMessageLoopValid(ID identifier) {
271 if (g_globals == NULL)
272 return false;
273
268 BrowserThreadGlobals& globals = g_globals.Get(); 274 BrowserThreadGlobals& globals = g_globals.Get();
269 base::AutoLock lock(globals.lock); 275 base::AutoLock lock(globals.lock);
270 DCHECK(identifier >= 0 && identifier < ID_COUNT); 276 DCHECK(identifier >= 0 && identifier < ID_COUNT);
271 return globals.threads[identifier] && 277 return globals.threads[identifier] &&
272 globals.threads[identifier]->message_loop(); 278 globals.threads[identifier]->message_loop();
273 } 279 }
274 280
275 // static 281 // static
276 bool BrowserThread::PostTask(ID identifier, 282 bool BrowserThread::PostTask(ID identifier,
277 const tracked_objects::Location& from_here, 283 const tracked_objects::Location& from_here,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 const tracked_objects::Location& from_here, 347 const tracked_objects::Location& from_here,
342 const base::Closure& task, 348 const base::Closure& task,
343 const base::Closure& reply) { 349 const base::Closure& reply) {
344 return GetMessageLoopProxyForThread(identifier)->PostTaskAndReply(from_here, 350 return GetMessageLoopProxyForThread(identifier)->PostTaskAndReply(from_here,
345 task, 351 task,
346 reply); 352 reply);
347 } 353 }
348 354
349 // static 355 // static
350 bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) { 356 bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) {
357 if (g_globals == NULL)
358 return false;
359
351 // We shouldn't use MessageLoop::current() since it uses LazyInstance which 360 // We shouldn't use MessageLoop::current() since it uses LazyInstance which
352 // may be deleted by ~AtExitManager when a WorkerPool thread calls this 361 // may be deleted by ~AtExitManager when a WorkerPool thread calls this
353 // function. 362 // function.
354 // http://crbug.com/63678 363 // http://crbug.com/63678
355 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; 364 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton;
356 MessageLoop* cur_message_loop = MessageLoop::current(); 365 MessageLoop* cur_message_loop = MessageLoop::current();
357 BrowserThreadGlobals& globals = g_globals.Get(); 366 BrowserThreadGlobals& globals = g_globals.Get();
358 for (int i = 0; i < ID_COUNT; ++i) { 367 for (int i = 0; i < ID_COUNT; ++i) {
359 if (globals.threads[i] && 368 if (globals.threads[i] &&
360 globals.threads[i]->message_loop() == cur_message_loop) { 369 globals.threads[i]->message_loop() == cur_message_loop) {
361 *identifier = globals.threads[i]->identifier_; 370 *identifier = globals.threads[i]->identifier_;
362 return true; 371 return true;
363 } 372 }
364 } 373 }
365 374
366 return false; 375 return false;
367 } 376 }
368 377
369 // static 378 // static
370 scoped_refptr<base::MessageLoopProxy> 379 scoped_refptr<base::MessageLoopProxy>
371 BrowserThread::GetMessageLoopProxyForThread(ID identifier) { 380 BrowserThread::GetMessageLoopProxyForThread(ID identifier) {
372 scoped_refptr<base::MessageLoopProxy> proxy( 381 scoped_refptr<base::MessageLoopProxy> proxy(
373 new BrowserThreadMessageLoopProxy(identifier)); 382 new BrowserThreadMessageLoopProxy(identifier));
374 return proxy; 383 return proxy;
375 } 384 }
376 385
377 // static 386 // static
378 MessageLoop* BrowserThread::UnsafeGetMessageLoopForThread(ID identifier) { 387 MessageLoop* BrowserThread::UnsafeGetMessageLoopForThread(ID identifier) {
388 if (g_globals == NULL)
389 return NULL;
390
379 BrowserThreadGlobals& globals = g_globals.Get(); 391 BrowserThreadGlobals& globals = g_globals.Get();
380 base::AutoLock lock(globals.lock); 392 base::AutoLock lock(globals.lock);
381 base::Thread* thread = globals.threads[identifier]; 393 base::Thread* thread = globals.threads[identifier];
382 DCHECK(thread); 394 DCHECK(thread);
383 MessageLoop* loop = thread->message_loop(); 395 MessageLoop* loop = thread->message_loop();
384 return loop; 396 return loop;
385 } 397 }
386 398
387 // static 399 // static
388 void BrowserThread::SetDelegate(ID identifier, 400 void BrowserThread::SetDelegate(ID identifier,
389 BrowserThreadDelegate* delegate) { 401 BrowserThreadDelegate* delegate) {
390 using base::subtle::AtomicWord; 402 using base::subtle::AtomicWord;
391 BrowserThreadGlobals& globals = g_globals.Get(); 403 BrowserThreadGlobals& globals = g_globals.Get();
392 AtomicWord* storage = reinterpret_cast<AtomicWord*>( 404 AtomicWord* storage = reinterpret_cast<AtomicWord*>(
393 &globals.thread_delegates[identifier]); 405 &globals.thread_delegates[identifier]);
394 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( 406 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange(
395 storage, reinterpret_cast<AtomicWord>(delegate)); 407 storage, reinterpret_cast<AtomicWord>(delegate));
396 408
397 // This catches registration when previously registered. 409 // This catches registration when previously registered.
398 DCHECK(!delegate || !old_pointer); 410 DCHECK(!delegate || !old_pointer);
399 } 411 }
400 412
401 } // namespace content 413 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698