| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/child_process.h" | 5 #include "content/common/child_process.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
| 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/process_util.h" | 13 #include "base/process_util.h" |
| 13 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "base/threading/thread_local.h" |
| 15 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 16 #include "content/common/child_thread.h" | 18 #include "content/common/child_thread.h" |
| 17 | 19 |
| 18 #if defined(OS_POSIX) | 20 #if defined(OS_POSIX) |
| 19 static void SigUSR1Handler(int signal) { } | 21 static void SigUSR1Handler(int signal) { } |
| 20 #endif | 22 #endif |
| 21 | 23 |
| 22 ChildProcess* ChildProcess::child_process_; | 24 namespace { |
| 25 static base::LazyInstance<base::ThreadLocalPointer<ChildProcess> > lazy_tls( |
| 26 base::LINKER_INITIALIZED); |
| 27 } |
| 23 | 28 |
| 24 ChildProcess::ChildProcess() | 29 ChildProcess::ChildProcess() |
| 25 : ref_count_(0), | 30 : ref_count_(0), |
| 26 shutdown_event_(true, false), | 31 shutdown_event_(true, false), |
| 27 io_thread_("Chrome_ChildIOThread") { | 32 io_thread_("Chrome_ChildIOThread") { |
| 28 DCHECK(!child_process_); | 33 DCHECK(!lazy_tls.Pointer()->Get()); |
| 29 child_process_ = this; | 34 lazy_tls.Pointer()->Set(this); |
| 30 | 35 |
| 31 io_thread_.StartWithOptions(base::Thread::Options(MessageLoop::TYPE_IO, 0)); | 36 io_thread_.StartWithOptions(base::Thread::Options(MessageLoop::TYPE_IO, 0)); |
| 32 } | 37 } |
| 33 | 38 |
| 34 ChildProcess::~ChildProcess() { | 39 ChildProcess::~ChildProcess() { |
| 35 DCHECK(child_process_ == this); | 40 DCHECK(lazy_tls.Pointer()->Get() == this); |
| 36 | 41 |
| 37 // Signal this event before destroying the child process. That way all | 42 // Signal this event before destroying the child process. That way all |
| 38 // background threads can cleanup. | 43 // background threads can cleanup. |
| 39 // For example, in the renderer the RenderThread instances will be able to | 44 // For example, in the renderer the RenderThread instances will be able to |
| 40 // notice shutdown before the render process begins waiting for them to exit. | 45 // notice shutdown before the render process begins waiting for them to exit. |
| 41 shutdown_event_.Signal(); | 46 shutdown_event_.Signal(); |
| 42 | 47 |
| 43 // Kill the main thread object before nulling child_process_, since | 48 // Kill the main thread object before nulling lazy_tls, since |
| 44 // destruction code might depend on it. | 49 // destruction code might depend on it. |
| 45 main_thread_.reset(); | 50 main_thread_.reset(); |
| 46 | 51 |
| 47 child_process_ = NULL; | 52 lazy_tls.Pointer()->Set(NULL); |
| 53 } |
| 54 |
| 55 ChildProcess* ChildProcess::current() { |
| 56 return lazy_tls.Pointer()->Get(); |
| 48 } | 57 } |
| 49 | 58 |
| 50 ChildThread* ChildProcess::main_thread() { | 59 ChildThread* ChildProcess::main_thread() { |
| 51 return main_thread_.get(); | 60 return main_thread_.get(); |
| 52 } | 61 } |
| 53 | 62 |
| 54 void ChildProcess::set_main_thread(ChildThread* thread) { | 63 void ChildProcess::set_main_thread(ChildThread* thread) { |
| 55 main_thread_.reset(thread); | 64 main_thread_.reset(thread); |
| 56 } | 65 } |
| 57 | 66 |
| 58 void ChildProcess::AddRefProcess() { | 67 void ChildProcess::AddRefProcess() { |
| 59 DCHECK(!main_thread_.get() || // null in unittests. | 68 DCHECK(!main_thread_.get() || // null in unittests. |
| 60 MessageLoop::current() == main_thread_->message_loop()); | 69 MessageLoop::current() == main_thread_->message_loop()); |
| 61 ref_count_++; | 70 ref_count_++; |
| 62 } | 71 } |
| 63 | 72 |
| 64 void ChildProcess::ReleaseProcess() { | 73 void ChildProcess::ReleaseProcess() { |
| 65 DCHECK(!main_thread_.get() || // null in unittests. | 74 DCHECK(!main_thread_.get() || // null in unittests. |
| 66 MessageLoop::current() == main_thread_->message_loop()); | 75 MessageLoop::current() == main_thread_->message_loop()); |
| 67 DCHECK(ref_count_); | 76 DCHECK(ref_count_); |
| 68 DCHECK(child_process_); | |
| 69 if (--ref_count_) | 77 if (--ref_count_) |
| 70 return; | 78 return; |
| 71 | 79 |
| 72 if (main_thread_.get()) // null in unittests. | 80 if (main_thread_.get()) // null in unittests. |
| 73 main_thread_->OnProcessFinalRelease(); | 81 main_thread_->OnProcessFinalRelease(); |
| 74 } | 82 } |
| 75 | 83 |
| 76 base::WaitableEvent* ChildProcess::GetShutDownEvent() { | 84 base::WaitableEvent* ChildProcess::GetShutDownEvent() { |
| 77 DCHECK(child_process_); | 85 return &lazy_tls.Pointer()->Get()->shutdown_event_; |
| 78 return &child_process_->shutdown_event_; | |
| 79 } | 86 } |
| 80 | 87 |
| 81 void ChildProcess::WaitForDebugger(const std::string& label) { | 88 void ChildProcess::WaitForDebugger(const std::string& label) { |
| 82 #if defined(OS_WIN) | 89 #if defined(OS_WIN) |
| 83 #if defined(GOOGLE_CHROME_BUILD) | 90 #if defined(GOOGLE_CHROME_BUILD) |
| 84 std::string title = "Google Chrome"; | 91 std::string title = "Google Chrome"; |
| 85 #else // CHROMIUM_BUILD | 92 #else // CHROMIUM_BUILD |
| 86 std::string title = "Chromium"; | 93 std::string title = "Chromium"; |
| 87 #endif // CHROMIUM_BUILD | 94 #endif // CHROMIUM_BUILD |
| 88 title += " "; | 95 title += " "; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 102 << ") paused waiting for debugger to attach @ pid"; | 109 << ") paused waiting for debugger to attach @ pid"; |
| 103 // Install a signal handler so that pause can be woken. | 110 // Install a signal handler so that pause can be woken. |
| 104 struct sigaction sa; | 111 struct sigaction sa; |
| 105 memset(&sa, 0, sizeof(sa)); | 112 memset(&sa, 0, sizeof(sa)); |
| 106 sa.sa_handler = SigUSR1Handler; | 113 sa.sa_handler = SigUSR1Handler; |
| 107 sigaction(SIGUSR1, &sa, NULL); | 114 sigaction(SIGUSR1, &sa, NULL); |
| 108 | 115 |
| 109 pause(); | 116 pause(); |
| 110 #endif // defined(OS_POSIX) | 117 #endif // defined(OS_POSIX) |
| 111 } | 118 } |
| OLD | NEW |