blob: 4f68546c2e4a31b2cf881b36da2d7dfe5bf2483f [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]eff4aecb2008-08-12 18:37:355#include "base/thread.h"
6
[email protected]83a05dd2008-09-03 16:47:377#include "base/singleton.h"
initial.commitd7cae122008-07-26 21:49:388#include "base/string_util.h"
[email protected]83a05dd2008-09-03 16:47:379#include "base/thread_local.h"
[email protected]ffd83082008-08-11 14:35:1510#include "base/waitable_event.h"
[email protected]eff4aecb2008-08-12 18:37:3511
[email protected]4d9bdfaf2008-08-26 05:53:5712namespace base {
13
initial.commitd7cae122008-07-26 21:49:3814// This task is used to trigger the message loop to exit.
15class ThreadQuitTask : public Task {
16 public:
17 virtual void Run() {
18 MessageLoop::current()->Quit();
19 Thread::SetThreadWasQuitProperly(true);
20 }
21};
22
[email protected]4d9bdfaf2008-08-26 05:53:5723// Used to pass data to ThreadMain. This structure is allocated on the stack
24// from within StartWithOptions.
25struct Thread::StartupData {
26 // We get away with a const reference here because of how we are allocated.
27 const Thread::Options& options;
28
29 // Used to synchronize thread startup.
30 WaitableEvent event;
31
32 StartupData(const Options& opt) : options(opt), event(false, false) {}
33};
34
initial.commitd7cae122008-07-26 21:49:3835Thread::Thread(const char *name)
[email protected]4d9bdfaf2008-08-26 05:53:5736 : startup_data_(NULL),
[email protected]65487262008-08-26 06:06:0137 thread_(0),
[email protected]4d9bdfaf2008-08-26 05:53:5738 message_loop_(NULL),
39 thread_id_(0),
40 name_(name) {
initial.commitd7cae122008-07-26 21:49:3841}
42
43Thread::~Thread() {
44 Stop();
45}
46
47// We use this thread-local variable to record whether or not a thread exited
48// because its Stop method was called. This allows us to catch cases where
49// MessageLoop::Quit() is called directly, which is unexpected when using a
50// Thread to setup and run a MessageLoop.
[email protected]83a05dd2008-09-03 16:47:3751namespace {
52
53// Use a differentiating type to make sure we don't share our boolean we any
54// other Singleton<ThreadLocalBoolean>'s.
55struct ThreadExitedDummyDiffType { };
56typedef Singleton<ThreadLocalBoolean,
57 DefaultSingletonTraits<ThreadLocalBoolean>,
58 ThreadExitedDummyDiffType> ThreadExitedSingleton;
59
60} // namespace
initial.commitd7cae122008-07-26 21:49:3861
62void Thread::SetThreadWasQuitProperly(bool flag) {
[email protected]83a05dd2008-09-03 16:47:3763 ThreadExitedSingleton::get()->Set(flag);
initial.commitd7cae122008-07-26 21:49:3864}
65
66bool Thread::GetThreadWasQuitProperly() {
67 bool quit_properly = true;
68#ifndef NDEBUG
[email protected]83a05dd2008-09-03 16:47:3769 quit_properly = ThreadExitedSingleton::get()->Get();
initial.commitd7cae122008-07-26 21:49:3870#endif
71 return quit_properly;
72}
73
initial.commitd7cae122008-07-26 21:49:3874bool Thread::Start() {
[email protected]4d9bdfaf2008-08-26 05:53:5775 return StartWithOptions(Options());
initial.commitd7cae122008-07-26 21:49:3876}
77
[email protected]4d9bdfaf2008-08-26 05:53:5778bool Thread::StartWithOptions(const Options& options) {
[email protected]e9ba26d2008-08-21 09:46:3279 DCHECK(!message_loop_);
initial.commitd7cae122008-07-26 21:49:3880
[email protected]e9ba26d2008-08-21 09:46:3281 SetThreadWasQuitProperly(false);
82
[email protected]4d9bdfaf2008-08-26 05:53:5783 StartupData startup_data(options);
84 startup_data_ = &startup_data;
[email protected]e9ba26d2008-08-21 09:46:3285
[email protected]4d9bdfaf2008-08-26 05:53:5786 if (!PlatformThread::Create(options.stack_size, this, &thread_)) {
initial.commitd7cae122008-07-26 21:49:3887 DLOG(ERROR) << "failed to create thread";
[email protected]4d9bdfaf2008-08-26 05:53:5788 startup_data_ = NULL; // Record that we failed to start.
initial.commitd7cae122008-07-26 21:49:3889 return false;
90 }
91
92 // Wait for the thread to start and initialize message_loop_
[email protected]4d9bdfaf2008-08-26 05:53:5793 startup_data.event.Wait();
[email protected]e9ba26d2008-08-21 09:46:3294
95 DCHECK(message_loop_);
initial.commitd7cae122008-07-26 21:49:3896 return true;
97}
98
99void Thread::Stop() {
[email protected]4d9bdfaf2008-08-26 05:53:57100 if (!thread_was_started())
initial.commitd7cae122008-07-26 21:49:38101 return;
102
[email protected]4d9bdfaf2008-08-26 05:53:57103 // We should only be called on the same thread that started us.
104 DCHECK_NE(thread_id_, PlatformThread::CurrentId());
initial.commitd7cae122008-07-26 21:49:38105
[email protected]4d9bdfaf2008-08-26 05:53:57106 // StopSoon may have already been called.
[email protected]e9ba26d2008-08-21 09:46:32107 if (message_loop_)
[email protected]eff4aecb2008-08-12 18:37:35108 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
109
[email protected]4d9bdfaf2008-08-26 05:53:57110 // Wait for the thread to exit. It should already have terminated but make
[email protected]eff4aecb2008-08-12 18:37:35111 // sure this assumption is valid.
[email protected]4d9bdfaf2008-08-26 05:53:57112 //
113 // TODO(darin): Unfortunately, we need to keep message_loop_ around until
114 // the thread exits. Some consumers are abusing the API. Make them stop.
115 //
[email protected]e9ba26d2008-08-21 09:46:32116 PlatformThread::Join(thread_);
[email protected]eff4aecb2008-08-12 18:37:35117
[email protected]e9ba26d2008-08-21 09:46:32118 // The thread can't receive messages anymore.
119 message_loop_ = NULL;
120
121 // The thread no longer needs to be joined.
[email protected]4d9bdfaf2008-08-26 05:53:57122 startup_data_ = NULL;
[email protected]eff4aecb2008-08-12 18:37:35123}
124
125void Thread::StopSoon() {
[email protected]e9ba26d2008-08-21 09:46:32126 if (!message_loop_)
[email protected]eff4aecb2008-08-12 18:37:35127 return;
128
[email protected]4d9bdfaf2008-08-26 05:53:57129 // We should only be called on the same thread that started us.
130 DCHECK_NE(thread_id_, PlatformThread::CurrentId());
[email protected]eff4aecb2008-08-12 18:37:35131
initial.commitd7cae122008-07-26 21:49:38132 // We had better have a message loop at this point! If we do not, then it
133 // most likely means that the thread terminated unexpectedly, probably due
134 // to someone calling Quit() on our message loop directly.
135 DCHECK(message_loop_);
136
137 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
138
[email protected]eff4aecb2008-08-12 18:37:35139 // The thread can't receive messages anymore.
[email protected]e9ba26d2008-08-21 09:46:32140 message_loop_ = NULL;
initial.commitd7cae122008-07-26 21:49:38141}
142
[email protected]e9ba26d2008-08-21 09:46:32143void Thread::ThreadMain() {
initial.commitd7cae122008-07-26 21:49:38144 // The message loop for this thread.
[email protected]4d9bdfaf2008-08-26 05:53:57145 MessageLoop message_loop(startup_data_->options.message_loop_type);
initial.commitd7cae122008-07-26 21:49:38146
[email protected]eff4aecb2008-08-12 18:37:35147 // Complete the initialization of our Thread object.
[email protected]e9ba26d2008-08-21 09:46:32148 thread_id_ = PlatformThread::CurrentId();
[email protected]7d0f94452008-08-25 13:54:18149 PlatformThread::SetName(name_.c_str());
[email protected]e9ba26d2008-08-21 09:46:32150 message_loop.set_thread_name(name_);
151 message_loop_ = &message_loop;
[email protected]e9ba26d2008-08-21 09:46:32152
[email protected]4d9bdfaf2008-08-26 05:53:57153 startup_data_->event.Signal();
154 // startup_data_ can't be touched anymore since the starting thread is now
[email protected]e9ba26d2008-08-21 09:46:32155 // unlocked.
initial.commitd7cae122008-07-26 21:49:38156
157 // Let the thread do extra initialization.
[email protected]e9ba26d2008-08-21 09:46:32158 Init();
initial.commitd7cae122008-07-26 21:49:38159
160 message_loop.Run();
161
162 // Let the thread do extra cleanup.
[email protected]e9ba26d2008-08-21 09:46:32163 CleanUp();
initial.commitd7cae122008-07-26 21:49:38164
165 // Assert that MessageLoop::Quit was called by ThreadQuitTask.
[email protected]e9ba26d2008-08-21 09:46:32166 DCHECK(GetThreadWasQuitProperly());
initial.commitd7cae122008-07-26 21:49:38167
[email protected]eff4aecb2008-08-12 18:37:35168 // We can't receive messages anymore.
[email protected]e9ba26d2008-08-21 09:46:32169 message_loop_ = NULL;
initial.commitd7cae122008-07-26 21:49:38170}
license.botbf09a502008-08-24 00:55:55171
[email protected]4d9bdfaf2008-08-26 05:53:57172} // namespace base