blob: fd076a425d230e7486c0d23b949053dc60d226b3 [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]f886b7bf2008-09-10 10:54:067#include "base/lazy_instance.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
[email protected]f886b7bf2008-09-10 10:54:0647namespace {
48
initial.commitd7cae122008-07-26 21:49:3849// We use this thread-local variable to record whether or not a thread exited
50// because its Stop method was called. This allows us to catch cases where
51// MessageLoop::Quit() is called directly, which is unexpected when using a
52// Thread to setup and run a MessageLoop.
[email protected]f886b7bf2008-09-10 10:54:0653base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool(
54 base::LINKER_INITIALIZED);
[email protected]83a05dd2008-09-03 16:47:3755
56} // namespace
initial.commitd7cae122008-07-26 21:49:3857
58void Thread::SetThreadWasQuitProperly(bool flag) {
[email protected]f886b7bf2008-09-10 10:54:0659 lazy_tls_bool.Pointer()->Set(flag);
initial.commitd7cae122008-07-26 21:49:3860}
61
62bool Thread::GetThreadWasQuitProperly() {
63 bool quit_properly = true;
64#ifndef NDEBUG
[email protected]f886b7bf2008-09-10 10:54:0665 quit_properly = lazy_tls_bool.Pointer()->Get();
initial.commitd7cae122008-07-26 21:49:3866#endif
67 return quit_properly;
68}
69
initial.commitd7cae122008-07-26 21:49:3870bool Thread::Start() {
[email protected]4d9bdfaf2008-08-26 05:53:5771 return StartWithOptions(Options());
initial.commitd7cae122008-07-26 21:49:3872}
73
[email protected]4d9bdfaf2008-08-26 05:53:5774bool Thread::StartWithOptions(const Options& options) {
[email protected]e9ba26d2008-08-21 09:46:3275 DCHECK(!message_loop_);
initial.commitd7cae122008-07-26 21:49:3876
[email protected]e9ba26d2008-08-21 09:46:3277 SetThreadWasQuitProperly(false);
78
[email protected]4d9bdfaf2008-08-26 05:53:5779 StartupData startup_data(options);
80 startup_data_ = &startup_data;
[email protected]e9ba26d2008-08-21 09:46:3281
[email protected]4d9bdfaf2008-08-26 05:53:5782 if (!PlatformThread::Create(options.stack_size, this, &thread_)) {
initial.commitd7cae122008-07-26 21:49:3883 DLOG(ERROR) << "failed to create thread";
[email protected]4d9bdfaf2008-08-26 05:53:5784 startup_data_ = NULL; // Record that we failed to start.
initial.commitd7cae122008-07-26 21:49:3885 return false;
86 }
87
88 // Wait for the thread to start and initialize message_loop_
[email protected]4d9bdfaf2008-08-26 05:53:5789 startup_data.event.Wait();
[email protected]e9ba26d2008-08-21 09:46:3290
91 DCHECK(message_loop_);
initial.commitd7cae122008-07-26 21:49:3892 return true;
93}
94
95void Thread::Stop() {
[email protected]4d9bdfaf2008-08-26 05:53:5796 if (!thread_was_started())
initial.commitd7cae122008-07-26 21:49:3897 return;
98
[email protected]4d9bdfaf2008-08-26 05:53:5799 // We should only be called on the same thread that started us.
100 DCHECK_NE(thread_id_, PlatformThread::CurrentId());
initial.commitd7cae122008-07-26 21:49:38101
[email protected]4d9bdfaf2008-08-26 05:53:57102 // StopSoon may have already been called.
[email protected]e9ba26d2008-08-21 09:46:32103 if (message_loop_)
[email protected]eff4aecb2008-08-12 18:37:35104 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
105
[email protected]4d9bdfaf2008-08-26 05:53:57106 // Wait for the thread to exit. It should already have terminated but make
[email protected]eff4aecb2008-08-12 18:37:35107 // sure this assumption is valid.
[email protected]4d9bdfaf2008-08-26 05:53:57108 //
109 // TODO(darin): Unfortunately, we need to keep message_loop_ around until
110 // the thread exits. Some consumers are abusing the API. Make them stop.
111 //
[email protected]e9ba26d2008-08-21 09:46:32112 PlatformThread::Join(thread_);
[email protected]eff4aecb2008-08-12 18:37:35113
[email protected]e9ba26d2008-08-21 09:46:32114 // The thread can't receive messages anymore.
115 message_loop_ = NULL;
116
117 // The thread no longer needs to be joined.
[email protected]4d9bdfaf2008-08-26 05:53:57118 startup_data_ = NULL;
[email protected]eff4aecb2008-08-12 18:37:35119}
120
121void Thread::StopSoon() {
[email protected]e9ba26d2008-08-21 09:46:32122 if (!message_loop_)
[email protected]eff4aecb2008-08-12 18:37:35123 return;
124
[email protected]4d9bdfaf2008-08-26 05:53:57125 // We should only be called on the same thread that started us.
126 DCHECK_NE(thread_id_, PlatformThread::CurrentId());
[email protected]eff4aecb2008-08-12 18:37:35127
initial.commitd7cae122008-07-26 21:49:38128 // We had better have a message loop at this point! If we do not, then it
129 // most likely means that the thread terminated unexpectedly, probably due
130 // to someone calling Quit() on our message loop directly.
131 DCHECK(message_loop_);
132
133 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
134
[email protected]eff4aecb2008-08-12 18:37:35135 // The thread can't receive messages anymore.
[email protected]e9ba26d2008-08-21 09:46:32136 message_loop_ = NULL;
initial.commitd7cae122008-07-26 21:49:38137}
138
[email protected]e9ba26d2008-08-21 09:46:32139void Thread::ThreadMain() {
initial.commitd7cae122008-07-26 21:49:38140 // The message loop for this thread.
[email protected]4d9bdfaf2008-08-26 05:53:57141 MessageLoop message_loop(startup_data_->options.message_loop_type);
initial.commitd7cae122008-07-26 21:49:38142
[email protected]eff4aecb2008-08-12 18:37:35143 // Complete the initialization of our Thread object.
[email protected]e9ba26d2008-08-21 09:46:32144 thread_id_ = PlatformThread::CurrentId();
[email protected]7d0f94452008-08-25 13:54:18145 PlatformThread::SetName(name_.c_str());
[email protected]e9ba26d2008-08-21 09:46:32146 message_loop.set_thread_name(name_);
147 message_loop_ = &message_loop;
[email protected]e9ba26d2008-08-21 09:46:32148
[email protected]4d9bdfaf2008-08-26 05:53:57149 startup_data_->event.Signal();
150 // startup_data_ can't be touched anymore since the starting thread is now
[email protected]e9ba26d2008-08-21 09:46:32151 // unlocked.
initial.commitd7cae122008-07-26 21:49:38152
153 // Let the thread do extra initialization.
[email protected]e9ba26d2008-08-21 09:46:32154 Init();
initial.commitd7cae122008-07-26 21:49:38155
156 message_loop.Run();
157
158 // Let the thread do extra cleanup.
[email protected]e9ba26d2008-08-21 09:46:32159 CleanUp();
initial.commitd7cae122008-07-26 21:49:38160
161 // Assert that MessageLoop::Quit was called by ThreadQuitTask.
[email protected]e9ba26d2008-08-21 09:46:32162 DCHECK(GetThreadWasQuitProperly());
initial.commitd7cae122008-07-26 21:49:38163
[email protected]eff4aecb2008-08-12 18:37:35164 // We can't receive messages anymore.
[email protected]e9ba26d2008-08-21 09:46:32165 message_loop_ = NULL;
initial.commitd7cae122008-07-26 21:49:38166}
license.botbf09a502008-08-24 00:55:55167
[email protected]4d9bdfaf2008-08-26 05:53:57168} // namespace base