Chromium Code Reviews| Index: content/browser/browser_main_loop.cc |
| diff --git a/content/browser/browser_main_loop.cc b/content/browser/browser_main_loop.cc |
| index d7553cc1012b087b98ae0f232a879be3c0c12dae..1865de755f886cb7388236b3e9b96e921ad40807 100644 |
| --- a/content/browser/browser_main_loop.cc |
| +++ b/content/browser/browser_main_loop.cc |
| @@ -44,6 +44,7 @@ |
| #include "content/public/common/content_switches.h" |
| #include "content/public/common/main_function_params.h" |
| #include "content/public/common/result_codes.h" |
| +#include "content/public/common/startup_task_runner.h" |
| #include "crypto/nss_util.h" |
| #include "media/audio/audio_manager.h" |
| #include "media/base/media.h" |
| @@ -471,8 +472,90 @@ void BrowserMainLoop::MainMessageLoopStart() { |
| } |
| } |
| -void BrowserMainLoop::CreateThreads() { |
| - TRACE_EVENT0("startup", "BrowserMainLoop::CreateThreads") |
| +void BrowserMainLoop::CreateThread(size_t thread_id) { |
| + |
| + base::Thread::Options default_options; |
| + scoped_ptr<BrowserProcessSubThread>* thread_to_start = NULL; |
| + base::Thread::Options* options = &default_options; |
| + |
| + base::Thread::Options io_message_loop_options; |
| + io_message_loop_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| + base::Thread::Options ui_message_loop_options; |
| + ui_message_loop_options.message_loop_type = base::MessageLoop::TYPE_UI; |
| + |
| + switch (thread_id) { |
| + case BrowserThread::DB: |
| + TRACE_EVENT_BEGIN1("startup", |
| + "BrowserMainLoop::CreateThread:start", |
| + "Thread", |
| + "BrowserThread::DB"); |
| + thread_to_start = &db_thread_; |
| + break; |
| + case BrowserThread::FILE_USER_BLOCKING: |
| + TRACE_EVENT_BEGIN1("startup", |
| + "BrowserMainLoop::CreateThread:start", |
| + "Thread", |
| + "BrowserThread::FILE_USER_BLOCKING"); |
| + thread_to_start = &file_user_blocking_thread_; |
| + break; |
| + case BrowserThread::FILE: |
| + TRACE_EVENT_BEGIN1("startup", |
| + "BrowserMainLoop::CreateThread:start", |
| + "Thread", |
| + "BrowserThread::FILE"); |
| + thread_to_start = &file_thread_; |
| +#if defined(OS_WIN) |
| + // On Windows, the FILE thread needs to be have a UI message loop |
| + // which pumps messages in such a way that Google Update can |
| + // communicate back to us. |
| + options = &ui_message_loop_options; |
| +#else |
| + options = &io_message_loop_options; |
| +#endif |
| + break; |
| + case BrowserThread::PROCESS_LAUNCHER: |
| + TRACE_EVENT_BEGIN1("startup", |
| + "BrowserMainLoop::CreateThread:start", |
| + "Thread", |
| + "BrowserThread::PROCESS_LAUNCHER"); |
| + thread_to_start = &process_launcher_thread_; |
| + break; |
| + case BrowserThread::CACHE: |
| + TRACE_EVENT_BEGIN1("startup", |
| + "BrowserMainLoop::CreateThread:start", |
| + "Thread", |
| + "BrowserThread::CACHE"); |
| + thread_to_start = &cache_thread_; |
| + options = &io_message_loop_options; |
| + break; |
| + case BrowserThread::IO: |
| + TRACE_EVENT_BEGIN1("startup", |
| + "BrowserMainLoop::CreateThread:start", |
| + "Thread", |
| + "BrowserThread::IO"); |
| + thread_to_start = &io_thread_; |
| + options = &io_message_loop_options; |
| + break; |
| + case BrowserThread::UI: |
| + case BrowserThread::ID_COUNT: |
| + default: |
| + NOTREACHED(); |
| + break; |
| + } |
| + |
| + BrowserThread::ID id = static_cast<BrowserThread::ID>(thread_id); |
| + |
| + if (thread_to_start) { |
| + (*thread_to_start).reset(new BrowserProcessSubThread(id)); |
| + (*thread_to_start)->StartWithOptions(*options); |
| + } else { |
| + NOTREACHED(); |
| + } |
| + |
| + TRACE_EVENT_END0("startup", "BrowserMainLoop::CreateThread:start"); |
| +} |
| + |
| +void BrowserMainLoop::PreCreateThreads() { |
| if (parts_) { |
| TRACE_EVENT0("startup", |
| @@ -497,15 +580,17 @@ void BrowserMainLoop::CreateThreads() { |
| if (parsed_command_line_.HasSwitch(switches::kSingleProcess)) |
| RenderProcessHost::SetRunRendererInProcess(true); |
| #endif |
| +} |
| - if (result_code_ > 0) |
| - return; |
| +void BrowserMainLoop::CreateThreads( |
| + const scoped_refptr<StartupTaskRunner>& task_runner) { |
| + TRACE_EVENT0("startup", "BrowserMainLoop::CreateThreads") |
| - base::Thread::Options default_options; |
| - base::Thread::Options io_message_loop_options; |
| - io_message_loop_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| - base::Thread::Options ui_message_loop_options; |
| - ui_message_loop_options.message_loop_type = base::MessageLoop::TYPE_UI; |
| + task_runner->SetProxy(base::MessageLoop::current()->message_loop_proxy()); |
| + |
| + base::Closure pre_create_threads = |
| + base::Bind(&BrowserMainLoop::PreCreateThreads, base::Unretained(this)); |
| + task_runner->AddTask(pre_create_threads); |
| // Start threads in the order they occur in the BrowserThread::ID |
| // enumeration, except for BrowserThread::UI which is the main |
| @@ -515,83 +600,23 @@ void BrowserMainLoop::CreateThreads() { |
| for (size_t thread_id = BrowserThread::UI + 1; |
| thread_id < BrowserThread::ID_COUNT; |
| ++thread_id) { |
| - scoped_ptr<BrowserProcessSubThread>* thread_to_start = NULL; |
| - base::Thread::Options* options = &default_options; |
| - |
| - switch (thread_id) { |
| - case BrowserThread::DB: |
| - TRACE_EVENT_BEGIN1("startup", |
| - "BrowserMainLoop::CreateThreads:start", |
| - "Thread", "BrowserThread::DB"); |
| - thread_to_start = &db_thread_; |
| - break; |
| - case BrowserThread::FILE_USER_BLOCKING: |
| - TRACE_EVENT_BEGIN1("startup", |
| - "BrowserMainLoop::CreateThreads:start", |
| - "Thread", "BrowserThread::FILE_USER_BLOCKING"); |
| - thread_to_start = &file_user_blocking_thread_; |
| - break; |
| - case BrowserThread::FILE: |
| - TRACE_EVENT_BEGIN1("startup", |
| - "BrowserMainLoop::CreateThreads:start", |
| - "Thread", "BrowserThread::FILE"); |
| - thread_to_start = &file_thread_; |
| -#if defined(OS_WIN) |
| - // On Windows, the FILE thread needs to be have a UI message loop |
| - // which pumps messages in such a way that Google Update can |
| - // communicate back to us. |
| - options = &ui_message_loop_options; |
| -#else |
| - options = &io_message_loop_options; |
| -#endif |
| - break; |
| - case BrowserThread::PROCESS_LAUNCHER: |
| - TRACE_EVENT_BEGIN1("startup", |
| - "BrowserMainLoop::CreateThreads:start", |
| - "Thread", "BrowserThread::PROCESS_LAUNCHER"); |
| - thread_to_start = &process_launcher_thread_; |
| - break; |
| - case BrowserThread::CACHE: |
| - TRACE_EVENT_BEGIN1("startup", |
| - "BrowserMainLoop::CreateThreads:start", |
| - "Thread", "BrowserThread::CACHE"); |
| - thread_to_start = &cache_thread_; |
| - options = &io_message_loop_options; |
| - break; |
| - case BrowserThread::IO: |
| - TRACE_EVENT_BEGIN1("startup", |
| - "BrowserMainLoop::CreateThreads:start", |
| - "Thread", "BrowserThread::IO"); |
| - thread_to_start = &io_thread_; |
| - options = &io_message_loop_options; |
| - break; |
| - case BrowserThread::UI: |
| - case BrowserThread::ID_COUNT: |
| - default: |
| - NOTREACHED(); |
| - break; |
| - } |
| - |
| - BrowserThread::ID id = static_cast<BrowserThread::ID>(thread_id); |
| - |
| - if (thread_to_start) { |
| - (*thread_to_start).reset(new BrowserProcessSubThread(id)); |
| - (*thread_to_start)->StartWithOptions(*options); |
| - } else { |
| - NOTREACHED(); |
| - } |
| - |
| - TRACE_EVENT_END0("startup", "BrowserMainLoop::CreateThreads:start"); |
| - |
| + base::Closure create_thread = base::Bind( |
| + &BrowserMainLoop::CreateThread, base::Unretained(this), thread_id); |
| + task_runner->AddTask(create_thread); |
|
Yaron
2013/07/23 02:14:03
To jam's point, the two slow thread inits that I k
aberent
2013/07/23 13:45:03
The time for actual thread creation is now trivial
|
| } |
| -#if !defined(OS_IOS) |
| - indexed_db_thread_.reset(new base::Thread("IndexedDB")); |
| - indexed_db_thread_->Start(); |
| -#endif |
| + base::Closure browser_thread_started = base::Bind( |
| + &BrowserMainLoop::BrowserThreadsStarted, base::Unretained(this)); |
| + task_runner->AddTask(browser_thread_started); |
| - BrowserThreadsStarted(); |
| + base::Closure pre_main_message_loop_run = base::Bind( |
| + &BrowserMainLoop::PreMainMessageLoopRun, base::Unretained(this)); |
| + task_runner->AddTask(pre_main_message_loop_run); |
| + task_runner->StartRunningTasks(); |
| +} |
| + |
| +void BrowserMainLoop::PreMainMessageLoopRun() { |
| if (parts_) { |
| TRACE_EVENT0("startup", |
| "BrowserMainLoop::CreateThreads:PreMainMessageLoopRun"); |
| @@ -753,6 +778,12 @@ void BrowserMainLoop::InitializeMainThread() { |
| void BrowserMainLoop::BrowserThreadsStarted() { |
| TRACE_EVENT0("startup", "BrowserMainLoop::BrowserThreadsStarted") |
| + |
| +#if !defined(OS_IOS) |
| + indexed_db_thread_.reset(new base::Thread("IndexedDB")); |
| + indexed_db_thread_->Start(); |
| +#endif |
| + |
| #if defined(OS_ANDROID) |
| // Up the priority of anything that touches with display tasks |
| // (this thread is UI thread, and io_thread_ is for IPCs). |