OLD | NEW |
---|---|
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" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/message_loop/message_loop_proxy.h" | |
15 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
16 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
17 #include "content/public/browser/browser_thread_delegate.h" | 16 #include "content/public/browser/browser_thread_delegate.h" |
18 #include "content/public/browser/content_browser_client.h" | 17 #include "content/public/browser/content_browser_client.h" |
19 #include "net/disk_cache/simple/simple_backend_impl.h" | 18 #include "net/disk_cache/simple/simple_backend_impl.h" |
20 | 19 |
21 #if defined(OS_ANDROID) | 20 #if defined(OS_ANDROID) |
22 #include "base/android/jni_android.h" | 21 #include "base/android/jni_android.h" |
23 #endif | 22 #endif |
24 | 23 |
25 namespace content { | 24 namespace content { |
26 | 25 |
27 namespace { | 26 namespace { |
28 | 27 |
29 // Friendly names for the well-known threads. | 28 // Friendly names for the well-known threads. |
30 static const char* g_browser_thread_names[BrowserThread::ID_COUNT] = { | 29 static const char* g_browser_thread_names[BrowserThread::ID_COUNT] = { |
31 "", // UI (name assembled in browser_main.cc). | 30 "", // UI (name assembled in browser_main.cc). |
32 "Chrome_DBThread", // DB | 31 "Chrome_DBThread", // DB |
33 "Chrome_FileThread", // FILE | 32 "Chrome_FileThread", // FILE |
34 "Chrome_FileUserBlockingThread", // FILE_USER_BLOCKING | 33 "Chrome_FileUserBlockingThread", // FILE_USER_BLOCKING |
35 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER | 34 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER |
36 "Chrome_CacheThread", // CACHE | 35 "Chrome_CacheThread", // CACHE |
37 "Chrome_IOThread", // IO | 36 "Chrome_IOThread", // IO |
38 }; | 37 }; |
39 | 38 |
40 // An implementation of MessageLoopProxy to be used in conjunction | 39 // An implementation of SingleThreadTaskRunner to be used in conjunction |
41 // with BrowserThread. | 40 // with BrowserThread. |
42 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { | 41 class BrowserThreadTaskRunner : public base::SingleThreadTaskRunner { |
no sievers
2015/06/01 23:30:47
Not that your patch is affecting that in any way -
Sami
2015/06/03 14:55:21
I was wondering about that too. I thought maybe so
| |
43 public: | 42 public: |
44 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) | 43 explicit BrowserThreadTaskRunner(BrowserThread::ID identifier) |
45 : id_(identifier) { | 44 : id_(identifier) {} |
46 } | |
47 | 45 |
48 // MessageLoopProxy implementation. | 46 // SingleThreadTaskRunner implementation. |
49 bool PostDelayedTask(const tracked_objects::Location& from_here, | 47 bool PostDelayedTask(const tracked_objects::Location& from_here, |
50 const base::Closure& task, | 48 const base::Closure& task, |
51 base::TimeDelta delay) override { | 49 base::TimeDelta delay) override { |
52 return BrowserThread::PostDelayedTask(id_, from_here, task, delay); | 50 return BrowserThread::PostDelayedTask(id_, from_here, task, delay); |
53 } | 51 } |
54 | 52 |
55 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 53 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
56 const base::Closure& task, | 54 const base::Closure& task, |
57 base::TimeDelta delay) override { | 55 base::TimeDelta delay) override { |
58 return BrowserThread::PostNonNestableDelayedTask(id_, from_here, task, | 56 return BrowserThread::PostNonNestableDelayedTask(id_, from_here, task, |
59 delay); | 57 delay); |
60 } | 58 } |
61 | 59 |
62 bool RunsTasksOnCurrentThread() const override { | 60 bool RunsTasksOnCurrentThread() const override { |
63 return BrowserThread::CurrentlyOn(id_); | 61 return BrowserThread::CurrentlyOn(id_); |
64 } | 62 } |
65 | 63 |
66 protected: | 64 protected: |
67 ~BrowserThreadMessageLoopProxy() override {} | 65 ~BrowserThreadTaskRunner() override {} |
68 | 66 |
69 private: | 67 private: |
70 BrowserThread::ID id_; | 68 BrowserThread::ID id_; |
71 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); | 69 DISALLOW_COPY_AND_ASSIGN(BrowserThreadTaskRunner); |
72 }; | 70 }; |
73 | 71 |
74 // A separate helper is used just for the proxies, in order to avoid needing | 72 // A separate helper is used just for the task runners, in order to avoid |
75 // to initialize the globals to create a proxy. | 73 // needing to initialize the globals to create a task runner. |
76 struct BrowserThreadProxies { | 74 struct BrowserThreadTaskRunners { |
77 BrowserThreadProxies() { | 75 BrowserThreadTaskRunners() { |
78 for (int i = 0; i < BrowserThread::ID_COUNT; ++i) { | 76 for (int i = 0; i < BrowserThread::ID_COUNT; ++i) { |
79 proxies[i] = | 77 proxies[i] = |
80 new BrowserThreadMessageLoopProxy(static_cast<BrowserThread::ID>(i)); | 78 new BrowserThreadTaskRunner(static_cast<BrowserThread::ID>(i)); |
81 } | 79 } |
82 } | 80 } |
83 | 81 |
84 scoped_refptr<base::MessageLoopProxy> proxies[BrowserThread::ID_COUNT]; | 82 scoped_refptr<base::SingleThreadTaskRunner> proxies[BrowserThread::ID_COUNT]; |
85 }; | 83 }; |
86 | 84 |
87 base::LazyInstance<BrowserThreadProxies>::Leaky | 85 base::LazyInstance<BrowserThreadTaskRunners>::Leaky g_task_runners = |
88 g_proxies = LAZY_INSTANCE_INITIALIZER; | 86 LAZY_INSTANCE_INITIALIZER; |
89 | 87 |
90 struct BrowserThreadGlobals { | 88 struct BrowserThreadGlobals { |
91 BrowserThreadGlobals() | 89 BrowserThreadGlobals() |
92 : blocking_pool(new base::SequencedWorkerPool(3, "BrowserBlocking")) { | 90 : blocking_pool(new base::SequencedWorkerPool(3, "BrowserBlocking")) { |
93 memset(threads, 0, BrowserThread::ID_COUNT * sizeof(threads[0])); | 91 memset(threads, 0, BrowserThread::ID_COUNT * sizeof(threads[0])); |
94 memset(thread_delegates, 0, | 92 memset(thread_delegates, 0, |
95 BrowserThread::ID_COUNT * sizeof(thread_delegates[0])); | 93 BrowserThread::ID_COUNT * sizeof(thread_delegates[0])); |
96 } | 94 } |
97 | 95 |
98 // This lock protects |threads|. Do not read or modify that array | 96 // This lock protects |threads|. Do not read or modify that array |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 | 322 |
325 BrowserThreadGlobals& globals = g_globals.Get(); | 323 BrowserThreadGlobals& globals = g_globals.Get(); |
326 if (!target_thread_outlives_current) | 324 if (!target_thread_outlives_current) |
327 globals.lock.Acquire(); | 325 globals.lock.Acquire(); |
328 | 326 |
329 base::MessageLoop* message_loop = | 327 base::MessageLoop* message_loop = |
330 globals.threads[identifier] ? globals.threads[identifier]->message_loop() | 328 globals.threads[identifier] ? globals.threads[identifier]->message_loop() |
331 : NULL; | 329 : NULL; |
332 if (message_loop) { | 330 if (message_loop) { |
333 if (nestable) { | 331 if (nestable) { |
334 message_loop->PostDelayedTask(from_here, task, delay); | 332 message_loop->task_runner()->PostDelayedTask(from_here, task, delay); |
335 } else { | 333 } else { |
336 message_loop->PostNonNestableDelayedTask(from_here, task, delay); | 334 message_loop->task_runner()->PostNonNestableDelayedTask(from_here, task, |
335 delay); | |
337 } | 336 } |
338 } | 337 } |
339 | 338 |
340 if (!target_thread_outlives_current) | 339 if (!target_thread_outlives_current) |
341 globals.lock.Release(); | 340 globals.lock.Release(); |
342 | 341 |
343 return !!message_loop; | 342 return !!message_loop; |
344 } | 343 } |
345 | 344 |
346 // static | 345 // static |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
510 globals.threads[i]->message_loop() == cur_message_loop) { | 509 globals.threads[i]->message_loop() == cur_message_loop) { |
511 *identifier = globals.threads[i]->identifier_; | 510 *identifier = globals.threads[i]->identifier_; |
512 return true; | 511 return true; |
513 } | 512 } |
514 } | 513 } |
515 | 514 |
516 return false; | 515 return false; |
517 } | 516 } |
518 | 517 |
519 // static | 518 // static |
520 scoped_refptr<base::MessageLoopProxy> | 519 scoped_refptr<base::SingleThreadTaskRunner> |
521 BrowserThread::GetMessageLoopProxyForThread(ID identifier) { | 520 BrowserThread::GetMessageLoopProxyForThread(ID identifier) { |
522 return g_proxies.Get().proxies[identifier]; | 521 return g_task_runners.Get().proxies[identifier]; |
523 } | 522 } |
524 | 523 |
525 // static | 524 // static |
526 base::MessageLoop* BrowserThread::UnsafeGetMessageLoopForThread(ID identifier) { | 525 base::MessageLoop* BrowserThread::UnsafeGetMessageLoopForThread(ID identifier) { |
527 if (g_globals == NULL) | 526 if (g_globals == NULL) |
528 return NULL; | 527 return NULL; |
529 | 528 |
530 BrowserThreadGlobals& globals = g_globals.Get(); | 529 BrowserThreadGlobals& globals = g_globals.Get(); |
531 base::AutoLock lock(globals.lock); | 530 base::AutoLock lock(globals.lock); |
532 base::Thread* thread = globals.threads[identifier]; | 531 base::Thread* thread = globals.threads[identifier]; |
(...skipping 10 matching lines...) Expand all Loading... | |
543 AtomicWord* storage = reinterpret_cast<AtomicWord*>( | 542 AtomicWord* storage = reinterpret_cast<AtomicWord*>( |
544 &globals.thread_delegates[identifier]); | 543 &globals.thread_delegates[identifier]); |
545 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( | 544 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( |
546 storage, reinterpret_cast<AtomicWord>(delegate)); | 545 storage, reinterpret_cast<AtomicWord>(delegate)); |
547 | 546 |
548 // This catches registration when previously registered. | 547 // This catches registration when previously registered. |
549 DCHECK(!delegate || !old_pointer); | 548 DCHECK(!delegate || !old_pointer); |
550 } | 549 } |
551 | 550 |
552 } // namespace content | 551 } // namespace content |
OLD | NEW |