Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1273)

Side by Side Diff: chrome/common/child_process.cc

Issue 155944: Switch the first thread in a child process to be the main thread... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "chrome/common/child_process.h" 5 #include "chrome/common/child_process.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "chrome/common/child_thread.h" 8 #include "chrome/common/child_thread.h"
9 9
10 ChildProcess* ChildProcess::child_process_; 10 ChildProcess* ChildProcess::child_process_;
11 11
12 ChildProcess::ChildProcess(ChildThread* child_thread) 12 ChildProcess::ChildProcess()
13 : child_thread_(child_thread), 13 : ref_count_(0),
14 ref_count_(0), 14 shutdown_event_(true, false),
15 shutdown_event_(true, false) { 15 io_thread_("Chrome_ChildIOThread") {
16 DCHECK(!child_process_); 16 DCHECK(!child_process_);
17 child_process_ = this; 17 child_process_ = this;
18 if (child_thread_.get()) // null in unittests. 18
19 child_thread_->Run(); 19 io_thread_.StartWithOptions(base::Thread::Options(MessageLoop::TYPE_IO, 0));
20 } 20 }
21 21
22 ChildProcess::~ChildProcess() { 22 ChildProcess::~ChildProcess() {
23 DCHECK(child_process_ == this); 23 DCHECK(child_process_ == this);
24 24
25 // Signal this event before destroying the child process. That way all 25 // Signal this event before destroying the child process. That way all
26 // background threads can cleanup. 26 // background threads can cleanup.
27 // For example, in the renderer the RenderThread instances will be able to 27 // For example, in the renderer the RenderThread instances will be able to
28 // notice shutdown before the render process begins waiting for them to exit. 28 // notice shutdown before the render process begins waiting for them to exit.
29 shutdown_event_.Signal(); 29 shutdown_event_.Signal();
30 30
31 if (child_thread_.get()) 31 // Kill the main thread object before nulling child_process_, since
32 child_thread_->Stop(); 32 // destruction code might depend on it.
33 main_thread_.reset();
33 34
34 child_process_ = NULL; 35 child_process_ = NULL;
35 } 36 }
36 37
37 void ChildProcess::AddRefProcess() { 38 void ChildProcess::AddRefProcess() {
38 DCHECK(!child_thread_.get() || // null in unittests. 39 DCHECK(!main_thread_.get() || // null in unittests.
39 MessageLoop::current() == child_thread_->message_loop()); 40 MessageLoop::current() == main_thread_->message_loop());
40 ref_count_++; 41 ref_count_++;
41 } 42 }
42 43
43 void ChildProcess::ReleaseProcess() { 44 void ChildProcess::ReleaseProcess() {
44 DCHECK(!child_thread_.get() || // null in unittests. 45 DCHECK(!main_thread_.get() || // null in unittests.
45 MessageLoop::current() == child_thread_->message_loop()); 46 MessageLoop::current() == main_thread_->message_loop());
46 DCHECK(ref_count_); 47 DCHECK(ref_count_);
47 DCHECK(child_process_); 48 DCHECK(child_process_);
48 if (--ref_count_) 49 if (--ref_count_)
49 return; 50 return;
50 51
51 if (child_thread_.get()) // null in unittests. 52 if (main_thread_.get()) // null in unittests.
52 child_thread_->OnProcessFinalRelease(); 53 main_thread_->OnProcessFinalRelease();
53 } 54 }
54 55
55 base::WaitableEvent* ChildProcess::GetShutDownEvent() { 56 base::WaitableEvent* ChildProcess::GetShutDownEvent() {
56 DCHECK(child_process_); 57 DCHECK(child_process_);
57 return &child_process_->shutdown_event_; 58 return &child_process_->shutdown_event_;
58 } 59 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698