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/child/child_process.h" | 5 #include "content/child/child_process.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 7 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 8 #include <signal.h> // For SigUSR1Handler below. | 8 #include <signal.h> // For SigUSR1Handler below. |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "base/lazy_instance.h" | |
| 11 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 12 #include "base/metrics/statistics_recorder.h" | 13 #include "base/metrics/statistics_recorder.h" |
| 13 #include "base/process_util.h" | 14 #include "base/process_util.h" |
| 14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 18 #include "base/threading/thread_local.h" | |
| 17 #include "content/child/child_thread.h" | 19 #include "content/child/child_thread.h" |
| 18 | 20 |
| 19 #if defined(OS_ANDROID) | 21 #if defined(OS_ANDROID) |
| 20 #include "base/debug/debugger.h" | 22 #include "base/debug/debugger.h" |
| 21 #endif | 23 #endif |
| 22 | 24 |
| 23 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 25 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 24 static void SigUSR1Handler(int signal) { } | 26 static void SigUSR1Handler(int signal) { } |
| 25 #endif | 27 #endif |
| 26 | 28 |
| 27 namespace content { | 29 namespace content { |
| 28 // The singleton instance for this process. | 30 |
| 29 ChildProcess* child_process = NULL; | 31 namespace { |
| 32 | |
| 33 base::LazyInstance<base::ThreadLocalPointer<ChildProcess> > g_lazy_tls = | |
| 34 LAZY_INSTANCE_INITIALIZER; | |
| 35 } | |
| 30 | 36 |
| 31 ChildProcess::ChildProcess() | 37 ChildProcess::ChildProcess() |
| 32 : ref_count_(0), | 38 : ref_count_(0), |
| 33 shutdown_event_(true, false), | 39 shutdown_event_(true, false), |
| 34 io_thread_("Chrome_ChildIOThread") { | 40 io_thread_("Chrome_ChildIOThread") { |
| 35 DCHECK(!child_process); | 41 DCHECK(!g_lazy_tls.Pointer()->Get()); |
| 36 child_process = this; | 42 g_lazy_tls.Pointer()->Set(this); |
| 37 | 43 |
| 38 base::StatisticsRecorder::Initialize(); | 44 base::StatisticsRecorder::Initialize(); |
| 39 | 45 |
| 40 // We can't recover from failing to start the IO thread. | 46 // We can't recover from failing to start the IO thread. |
| 41 CHECK(io_thread_.StartWithOptions( | 47 CHECK(io_thread_.StartWithOptions( |
| 42 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 48 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| 43 | 49 |
| 44 #if defined(OS_ANDROID) | 50 #if defined(OS_ANDROID) |
| 45 io_thread_.SetPriority(base::kThreadPriority_Display); | 51 io_thread_.SetPriority(base::kThreadPriority_Display); |
| 46 #endif | 52 #endif |
| 47 } | 53 } |
| 48 | 54 |
| 49 ChildProcess::~ChildProcess() { | 55 ChildProcess::~ChildProcess() { |
| 50 DCHECK(child_process == this); | 56 DCHECK(g_lazy_tls.Pointer()->Get() == this); |
| 51 | 57 |
| 52 // Signal this event before destroying the child process. That way all | 58 // Signal this event before destroying the child process. That way all |
| 53 // background threads can cleanup. | 59 // background threads can cleanup. |
| 54 // For example, in the renderer the RenderThread instances will be able to | 60 // For example, in the renderer the RenderThread instances will be able to |
| 55 // notice shutdown before the render process begins waiting for them to exit. | 61 // notice shutdown before the render process begins waiting for them to exit. |
| 56 shutdown_event_.Signal(); | 62 shutdown_event_.Signal(); |
| 57 | 63 |
| 58 // Kill the main thread object before nulling child_process, since | 64 // Kill the main thread object before nulling child_process, since |
| 59 // destruction code might depend on it. | 65 // destruction code might depend on it. |
| 60 if (main_thread_) { // null in unittests. | 66 if (main_thread_) { // null in unittests. |
| 61 main_thread_->Shutdown(); | 67 main_thread_->Shutdown(); |
| 62 main_thread_.reset(); | 68 main_thread_.reset(); |
| 63 } | 69 } |
| 64 | 70 |
| 65 child_process = NULL; | 71 g_lazy_tls.Pointer()->Set(NULL); |
| 66 } | 72 } |
| 67 | 73 |
| 68 ChildThread* ChildProcess::main_thread() { | 74 ChildThread* ChildProcess::main_thread() { |
| 69 return main_thread_.get(); | 75 return main_thread_.get(); |
| 70 } | 76 } |
| 71 | 77 |
| 72 void ChildProcess::set_main_thread(ChildThread* thread) { | 78 void ChildProcess::set_main_thread(ChildThread* thread) { |
| 73 main_thread_.reset(thread); | 79 main_thread_.reset(thread); |
| 74 } | 80 } |
| 75 | 81 |
| 76 void ChildProcess::AddRefProcess() { | 82 void ChildProcess::AddRefProcess() { |
| 77 DCHECK(!main_thread_.get() || // null in unittests. | 83 DCHECK(!main_thread_.get() || // null in unittests. |
| 78 base::MessageLoop::current() == main_thread_->message_loop()); | 84 base::MessageLoop::current() == main_thread_->message_loop()); |
| 79 ref_count_++; | 85 ref_count_++; |
| 80 } | 86 } |
| 81 | 87 |
| 82 void ChildProcess::ReleaseProcess() { | 88 void ChildProcess::ReleaseProcess() { |
| 83 DCHECK(!main_thread_.get() || // null in unittests. | 89 DCHECK(!main_thread_.get() || // null in unittests. |
| 84 base::MessageLoop::current() == main_thread_->message_loop()); | 90 base::MessageLoop::current() == main_thread_->message_loop()); |
| 85 DCHECK(ref_count_); | 91 DCHECK(ref_count_); |
| 86 DCHECK(child_process); | |
| 87 if (--ref_count_) | 92 if (--ref_count_) |
| 88 return; | 93 return; |
| 89 | 94 |
| 90 if (main_thread_) // null in unittests. | 95 if (main_thread_) // null in unittests. |
| 91 main_thread_->OnProcessFinalRelease(); | 96 main_thread_->OnProcessFinalRelease(); |
| 92 } | 97 } |
| 93 | 98 |
| 94 ChildProcess* ChildProcess::current() { | 99 ChildProcess* ChildProcess::current() { |
| 95 return child_process; | 100 return g_lazy_tls.Pointer()->Get(); |
|
scottmg
2013/06/26 15:21:50
is this used in any hot functions? it's probably s
jam
2013/06/26 17:23:08
Not that I know of, and would be doubtful if so.
| |
| 96 } | 101 } |
| 97 | 102 |
| 98 base::WaitableEvent* ChildProcess::GetShutDownEvent() { | 103 base::WaitableEvent* ChildProcess::GetShutDownEvent() { |
| 99 DCHECK(child_process); | 104 return &shutdown_event_; |
| 100 return &child_process->shutdown_event_; | |
| 101 } | 105 } |
| 102 | 106 |
| 103 void ChildProcess::WaitForDebugger(const std::string& label) { | 107 void ChildProcess::WaitForDebugger(const std::string& label) { |
| 104 #if defined(OS_WIN) | 108 #if defined(OS_WIN) |
| 105 #if defined(GOOGLE_CHROME_BUILD) | 109 #if defined(GOOGLE_CHROME_BUILD) |
| 106 std::string title = "Google Chrome"; | 110 std::string title = "Google Chrome"; |
| 107 #else // CHROMIUM_BUILD | 111 #else // CHROMIUM_BUILD |
| 108 std::string title = "Chromium"; | 112 std::string title = "Chromium"; |
| 109 #endif // CHROMIUM_BUILD | 113 #endif // CHROMIUM_BUILD |
| 110 title += " "; | 114 title += " "; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 133 memset(&sa, 0, sizeof(sa)); | 137 memset(&sa, 0, sizeof(sa)); |
| 134 sa.sa_handler = SigUSR1Handler; | 138 sa.sa_handler = SigUSR1Handler; |
| 135 sigaction(SIGUSR1, &sa, NULL); | 139 sigaction(SIGUSR1, &sa, NULL); |
| 136 | 140 |
| 137 pause(); | 141 pause(); |
| 138 #endif // defined(OS_ANDROID) | 142 #endif // defined(OS_ANDROID) |
| 139 #endif // defined(OS_POSIX) | 143 #endif // defined(OS_POSIX) |
| 140 } | 144 } |
| 141 | 145 |
| 142 } // namespace content | 146 } // namespace content |
| OLD | NEW |