Chromium Code Reviews| Index: content/browser/browser_thread_impl.cc |
| diff --git a/content/browser/browser_thread_impl.cc b/content/browser/browser_thread_impl.cc |
| index b86643970e599c88c4bb044765f5e5d15ed0a160..2792bb943a2b7721c462c5eb4be002f2ca5cdc1f 100644 |
| --- a/content/browser/browser_thread_impl.cc |
| +++ b/content/browser/browser_thread_impl.cc |
| @@ -15,7 +15,7 @@ |
| #include "base/macros.h" |
| #include "base/single_thread_task_runner.h" |
| #include "base/threading/sequenced_worker_pool.h" |
| -#include "base/threading/thread_restrictions.h" |
| +#include "base/threading/thread_local.h" |
| #include "build/build_config.h" |
| #include "content/public/browser/browser_thread_delegate.h" |
| #include "content/public/browser/content_browser_client.h" |
| @@ -117,6 +117,9 @@ struct BrowserThreadGlobals { |
| base::LazyInstance<BrowserThreadGlobals>::Leaky |
| g_globals = LAZY_INSTANCE_INITIALIZER; |
| +base::LazyInstance<base::ThreadLocalPointer<BrowserThreadImpl>>::Leaky |
| + g_browser_thread = LAZY_INSTANCE_INITIALIZER; |
| + |
| } // namespace |
| BrowserThreadImpl::BrowserThreadImpl(ID identifier) |
| @@ -130,6 +133,13 @@ BrowserThreadImpl::BrowserThreadImpl(ID identifier, |
| : Thread(message_loop->thread_name()), identifier_(identifier) { |
| set_message_loop(message_loop); |
| Initialize(); |
| + |
| + if (message_loop == base::MessageLoop::current()) { |
| + // In tests, it's possible for the same current MessageLoop to be assigned |
| + // to multiple BrowserThreadImpls. |
| + if (!g_browser_thread.Get().Get()) |
| + g_browser_thread.Get().Set(this); |
| + } |
| } |
| // static |
| @@ -229,11 +239,11 @@ void BrowserThreadImpl::Run(base::MessageLoop* message_loop) { |
| } |
| #endif |
| - BrowserThread::ID thread_id = ID_COUNT; |
| - if (!GetCurrentThreadIdentifier(&thread_id)) |
| - return Thread::Run(message_loop); |
| + DCHECK(!g_browser_thread.Get().Get()); |
| + g_browser_thread.Get().Set(this); |
| + CHECK_EQ(Thread::message_loop(), message_loop); |
| - switch (thread_id) { |
| + switch (identifier_) { |
| case BrowserThread::UI: |
| return UIThreadRun(message_loop); |
| case BrowserThread::DB: |
| @@ -249,10 +259,10 @@ void BrowserThreadImpl::Run(base::MessageLoop* message_loop) { |
| case BrowserThread::IO: |
| return IOThreadRun(message_loop); |
| case BrowserThread::ID_COUNT: |
| + default: |
|
kinuko
2016/01/08 08:43:32
Let's avoid having default here, which makes it ha
Anand Mistry (off Chromium)
2016/01/08 09:52:46
Done.
|
| CHECK(false); // This shouldn't actually be reached! |
| break; |
| } |
| - Thread::Run(message_loop); |
| } |
| void BrowserThreadImpl::CleanUp() { |
| @@ -294,6 +304,9 @@ BrowserThreadImpl::~BrowserThreadImpl() { |
| "Threads must be listed in the reverse order that they die"; |
| } |
| #endif |
| + |
| + if (g_browser_thread.Get().Get() == this) |
| + g_browser_thread.Get().Set(nullptr); |
| } |
| bool BrowserThreadImpl::StartWithOptions(const Options& options) { |
| @@ -398,11 +411,6 @@ bool BrowserThread::IsThreadInitialized(ID identifier) { |
| // static |
| bool BrowserThread::CurrentlyOn(ID identifier) { |
| - // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
| - // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
| - // function. |
| - // http://crbug.com/63678 |
| - base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
| BrowserThreadGlobals& globals = g_globals.Get(); |
| base::AutoLock lock(globals.lock); |
| DCHECK(identifier >= 0 && identifier < ID_COUNT); |
| @@ -501,22 +509,12 @@ bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) { |
| if (g_globals == NULL) |
| return false; |
| - // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
| - // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
| - // function. |
| - // http://crbug.com/63678 |
| - base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
| - base::MessageLoop* cur_message_loop = base::MessageLoop::current(); |
| - BrowserThreadGlobals& globals = g_globals.Get(); |
| - for (int i = 0; i < ID_COUNT; ++i) { |
| - if (globals.threads[i] && |
| - globals.threads[i]->message_loop() == cur_message_loop) { |
| - *identifier = globals.threads[i]->identifier_; |
| - return true; |
| - } |
| - } |
| + BrowserThreadImpl* browser_thread = g_browser_thread.Get().Get(); |
| + if (!browser_thread) |
| + return false; |
| - return false; |
| + *identifier = browser_thread->identifier_; |
| + return true; |
| } |
| // static |