Chromium Code Reviews| 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.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 static const char* const g_browser_thread_names[BrowserThread::ID_COUNT] = { | 33 static const char* const g_browser_thread_names[BrowserThread::ID_COUNT] = { |
| 34 "", // UI (name assembled in browser_main.cc). | 34 "", // UI (name assembled in browser_main.cc). |
| 35 "Chrome_DBThread", // DB | 35 "Chrome_DBThread", // DB |
| 36 "Chrome_FileThread", // FILE | 36 "Chrome_FileThread", // FILE |
| 37 "Chrome_FileUserBlockingThread", // FILE_USER_BLOCKING | 37 "Chrome_FileUserBlockingThread", // FILE_USER_BLOCKING |
| 38 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER | 38 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER |
| 39 "Chrome_CacheThread", // CACHE | 39 "Chrome_CacheThread", // CACHE |
| 40 "Chrome_IOThread", // IO | 40 "Chrome_IOThread", // IO |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 static const char* GetThreadName(BrowserThread::ID thread) { | |
| 44 if (BrowserThread::UI < thread && thread < BrowserThread::ID_COUNT) | |
| 45 return g_browser_thread_names[thread]; | |
| 46 if (thread == BrowserThread::UI) | |
| 47 return "Chrome_UIThread"; | |
| 48 return "Unknown Thread"; | |
| 49 } | |
| 50 | |
| 43 // An implementation of SingleThreadTaskRunner to be used in conjunction | 51 // An implementation of SingleThreadTaskRunner to be used in conjunction |
| 44 // with BrowserThread. | 52 // with BrowserThread. |
| 45 class BrowserThreadTaskRunner : public base::SingleThreadTaskRunner { | 53 class BrowserThreadTaskRunner : public base::SingleThreadTaskRunner { |
| 46 public: | 54 public: |
| 47 explicit BrowserThreadTaskRunner(BrowserThread::ID identifier) | 55 explicit BrowserThreadTaskRunner(BrowserThread::ID identifier) |
| 48 : id_(identifier) {} | 56 : id_(identifier) {} |
| 49 | 57 |
| 50 // SingleThreadTaskRunner implementation. | 58 // SingleThreadTaskRunner implementation. |
| 51 bool PostDelayedTask(const tracked_objects::Location& from_here, | 59 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 52 const base::Closure& task, | 60 const base::Closure& task, |
| 53 base::TimeDelta delay) override { | 61 base::TimeDelta delay) override { |
| 54 return BrowserThread::PostDelayedTask(id_, from_here, task, delay); | 62 return BrowserThread::PostDelayedTask(id_, from_here, task, delay); |
| 55 } | 63 } |
| 56 | 64 |
| 57 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 65 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 58 const base::Closure& task, | 66 const base::Closure& task, |
| 59 base::TimeDelta delay) override { | 67 base::TimeDelta delay) override { |
| 60 return BrowserThread::PostNonNestableDelayedTask(id_, from_here, task, | 68 return BrowserThread::PostNonNestableDelayedTask(id_, from_here, task, |
| 61 delay); | 69 delay); |
| 62 } | 70 } |
| 63 | 71 |
| 64 bool RunsTasksOnCurrentThread() const override { | 72 bool RunsTasksOnCurrentThread() const override { |
| 65 return BrowserThread::CurrentlyOn(id_); | 73 return BrowserThread::CurrentlyOn(id_); |
| 66 } | 74 } |
| 67 | 75 |
| 76 std::string GetThreadName() const override { | |
| 77 return content::GetThreadName(id_); | |
|
gab
2016/05/12 15:50:57
content:: is redundant here
| |
| 78 } | |
| 79 | |
| 68 protected: | 80 protected: |
| 69 ~BrowserThreadTaskRunner() override {} | 81 ~BrowserThreadTaskRunner() override {} |
| 70 | 82 |
| 71 private: | 83 private: |
| 72 BrowserThread::ID id_; | 84 BrowserThread::ID id_; |
| 73 DISALLOW_COPY_AND_ASSIGN(BrowserThreadTaskRunner); | 85 DISALLOW_COPY_AND_ASSIGN(BrowserThreadTaskRunner); |
| 74 }; | 86 }; |
| 75 | 87 |
| 76 // A separate helper is used just for the task runners, in order to avoid | 88 // A separate helper is used just for the task runners, in order to avoid |
| 77 // needing to initialize the globals to create a task runner. | 89 // needing to initialize the globals to create a task runner. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 | 125 |
| 114 const scoped_refptr<base::SequencedWorkerPool> blocking_pool; | 126 const scoped_refptr<base::SequencedWorkerPool> blocking_pool; |
| 115 }; | 127 }; |
| 116 | 128 |
| 117 base::LazyInstance<BrowserThreadGlobals>::Leaky | 129 base::LazyInstance<BrowserThreadGlobals>::Leaky |
| 118 g_globals = LAZY_INSTANCE_INITIALIZER; | 130 g_globals = LAZY_INSTANCE_INITIALIZER; |
| 119 | 131 |
| 120 } // namespace | 132 } // namespace |
| 121 | 133 |
| 122 BrowserThreadImpl::BrowserThreadImpl(ID identifier) | 134 BrowserThreadImpl::BrowserThreadImpl(ID identifier) |
| 123 : Thread(g_browser_thread_names[identifier]), | 135 : Thread(GetThreadName(identifier)), identifier_(identifier) { |
| 124 identifier_(identifier) { | |
| 125 Initialize(); | 136 Initialize(); |
| 126 } | 137 } |
| 127 | 138 |
| 128 BrowserThreadImpl::BrowserThreadImpl(ID identifier, | 139 BrowserThreadImpl::BrowserThreadImpl(ID identifier, |
| 129 base::MessageLoop* message_loop) | 140 base::MessageLoop* message_loop) |
| 130 : Thread(message_loop->thread_name()), identifier_(identifier) { | 141 : Thread(GetThreadName(identifier)), identifier_(identifier) { |
| 131 set_message_loop(message_loop); | 142 set_message_loop(message_loop); |
| 132 Initialize(); | 143 Initialize(); |
| 133 } | 144 } |
| 134 | 145 |
| 135 // static | 146 // static |
| 136 void BrowserThreadImpl::ShutdownThreadPool() { | 147 void BrowserThreadImpl::ShutdownThreadPool() { |
| 137 // The goal is to make it impossible for chrome to 'infinite loop' during | 148 // The goal is to make it impossible for chrome to 'infinite loop' during |
| 138 // shutdown, but to reasonably expect that all BLOCKING_SHUTDOWN tasks queued | 149 // shutdown, but to reasonably expect that all BLOCKING_SHUTDOWN tasks queued |
| 139 // during shutdown get run. There's nothing particularly scientific about the | 150 // during shutdown get run. There's nothing particularly scientific about the |
| 140 // number chosen. | 151 // number chosen. |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 410 // static | 421 // static |
| 411 bool BrowserThread::CurrentlyOn(ID identifier) { | 422 bool BrowserThread::CurrentlyOn(ID identifier) { |
| 412 BrowserThreadGlobals& globals = g_globals.Get(); | 423 BrowserThreadGlobals& globals = g_globals.Get(); |
| 413 base::AutoLock lock(globals.lock); | 424 base::AutoLock lock(globals.lock); |
| 414 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 425 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
| 415 return globals.threads[identifier] && | 426 return globals.threads[identifier] && |
| 416 globals.threads[identifier]->message_loop() == | 427 globals.threads[identifier]->message_loop() == |
| 417 base::MessageLoop::current(); | 428 base::MessageLoop::current(); |
| 418 } | 429 } |
| 419 | 430 |
| 420 static const char* GetThreadName(BrowserThread::ID thread) { | |
| 421 if (BrowserThread::UI < thread && thread < BrowserThread::ID_COUNT) | |
| 422 return g_browser_thread_names[thread]; | |
| 423 if (thread == BrowserThread::UI) | |
| 424 return "Chrome_UIThread"; | |
| 425 return "Unknown Thread"; | |
| 426 } | |
| 427 | |
| 428 // static | 431 // static |
| 429 std::string BrowserThread::GetDCheckCurrentlyOnErrorMessage(ID expected) { | 432 std::string BrowserThread::GetDCheckCurrentlyOnErrorMessage(ID expected) { |
| 430 const base::MessageLoop* message_loop = base::MessageLoop::current(); | 433 base::MessageLoop* message_loop = base::MessageLoop::current(); |
| 431 ID actual_browser_thread; | 434 ID actual_browser_thread; |
| 432 const char* actual_name = "Unknown Thread"; | 435 std::string actual_name("Unknown Thread"); |
|
gab
2016/05/12 15:50:57
435-440 can all be replaced by:
std::string actua
| |
| 433 if (message_loop && !message_loop->thread_name().empty()) { | 436 if (message_loop) { |
| 434 actual_name = message_loop->thread_name().c_str(); | 437 actual_name = message_loop->task_runner()->GetThreadName(); |
| 435 } else if (GetCurrentThreadIdentifier(&actual_browser_thread)) { | 438 } else if (GetCurrentThreadIdentifier(&actual_browser_thread)) { |
| 436 actual_name = GetThreadName(actual_browser_thread); | 439 actual_name = GetThreadName(actual_browser_thread); |
| 437 } | 440 } |
| 438 std::string result = "Must be called on "; | 441 std::string result = "Must be called on "; |
| 439 result += GetThreadName(expected); | 442 result += GetThreadName(expected); |
| 440 result += "; actually called on "; | 443 result += "; actually called on "; |
| 441 result += actual_name; | 444 result += actual_name; |
| 442 result += "."; | 445 result += "."; |
| 443 return result; | 446 return result; |
| 444 } | 447 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 552 AtomicWord* storage = reinterpret_cast<AtomicWord*>( | 555 AtomicWord* storage = reinterpret_cast<AtomicWord*>( |
| 553 &globals.thread_delegates[identifier]); | 556 &globals.thread_delegates[identifier]); |
| 554 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( | 557 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( |
| 555 storage, reinterpret_cast<AtomicWord>(delegate)); | 558 storage, reinterpret_cast<AtomicWord>(delegate)); |
| 556 | 559 |
| 557 // This catches registration when previously registered. | 560 // This catches registration when previously registered. |
| 558 DCHECK(!delegate || !old_pointer); | 561 DCHECK(!delegate || !old_pointer); |
| 559 } | 562 } |
| 560 | 563 |
| 561 } // namespace content | 564 } // namespace content |
| OLD | NEW |