blob: aeaa0a18a2c8dd2bdde8492f5e0fb2c5aff5283c [file] [log] [blame]
[email protected]b026f4a2009-01-28 17:21:231// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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]94d0d3e2009-11-23 19:17:337#include "base/dynamic_annotations.h"
[email protected]f886b7bf2008-09-10 10:54:068#include "base/lazy_instance.h"
initial.commitd7cae122008-07-26 21:49:389#include "base/string_util.h"
[email protected]83a05dd2008-09-03 16:47:3710#include "base/thread_local.h"
[email protected]ffd83082008-08-11 14:35:1511#include "base/waitable_event.h"
[email protected]eff4aecb2008-08-12 18:37:3512
[email protected]4d9bdfaf2008-08-26 05:53:5713namespace base {
14
initial.commitd7cae122008-07-26 21:49:3815// This task is used to trigger the message loop to exit.
16class ThreadQuitTask : public Task {
17 public:
18 virtual void Run() {
19 MessageLoop::current()->Quit();
20 Thread::SetThreadWasQuitProperly(true);
21 }
22};
23
[email protected]4d9bdfaf2008-08-26 05:53:5724// Used to pass data to ThreadMain. This structure is allocated on the stack
25// from within StartWithOptions.
26struct Thread::StartupData {
27 // We get away with a const reference here because of how we are allocated.
28 const Thread::Options& options;
29
30 // Used to synchronize thread startup.
31 WaitableEvent event;
32
[email protected]b026f4a2009-01-28 17:21:2333 explicit StartupData(const Options& opt)
34 : options(opt),
35 event(false, false) {}
[email protected]4d9bdfaf2008-08-26 05:53:5736};
37
initial.commitd7cae122008-07-26 21:49:3838Thread::Thread(const char *name)
[email protected]88f333122009-09-14 23:47:3839 : stopping_(false),
40 startup_data_(NULL),
[email protected]65487262008-08-26 06:06:0141 thread_(0),
[email protected]4d9bdfaf2008-08-26 05:53:5742 message_loop_(NULL),
43 thread_id_(0),
44 name_(name) {
initial.commitd7cae122008-07-26 21:49:3845}
46
47Thread::~Thread() {
48 Stop();
49}
50
[email protected]f886b7bf2008-09-10 10:54:0651namespace {
52
initial.commitd7cae122008-07-26 21:49:3853// We use this thread-local variable to record whether or not a thread exited
54// because its Stop method was called. This allows us to catch cases where
55// MessageLoop::Quit() is called directly, which is unexpected when using a
56// Thread to setup and run a MessageLoop.
[email protected]f886b7bf2008-09-10 10:54:0657base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool(
58 base::LINKER_INITIALIZED);
[email protected]83a05dd2008-09-03 16:47:3759
60} // namespace
initial.commitd7cae122008-07-26 21:49:3861
62void Thread::SetThreadWasQuitProperly(bool flag) {
[email protected]f886b7bf2008-09-10 10:54:0663 lazy_tls_bool.Pointer()->Set(flag);
initial.commitd7cae122008-07-26 21:49:3864}
65
66bool Thread::GetThreadWasQuitProperly() {
67 bool quit_properly = true;
68#ifndef NDEBUG
[email protected]f886b7bf2008-09-10 10:54:0669 quit_properly = lazy_tls_bool.Pointer()->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]88f333122009-09-14 23:47:38103 StopSoon();
initial.commitd7cae122008-07-26 21:49:38104
[email protected]88f333122009-09-14 23:47:38105 // Wait for the thread to exit.
[email protected]4d9bdfaf2008-08-26 05:53:57106 //
107 // TODO(darin): Unfortunately, we need to keep message_loop_ around until
108 // the thread exits. Some consumers are abusing the API. Make them stop.
109 //
[email protected]e9ba26d2008-08-21 09:46:32110 PlatformThread::Join(thread_);
[email protected]eff4aecb2008-08-12 18:37:35111
[email protected]88f333122009-09-14 23:47:38112 // The thread should NULL message_loop_ on exit.
113 DCHECK(!message_loop_);
[email protected]e9ba26d2008-08-21 09:46:32114
115 // The thread no longer needs to be joined.
[email protected]4d9bdfaf2008-08-26 05:53:57116 startup_data_ = NULL;
[email protected]88f333122009-09-14 23:47:38117
118 stopping_ = false;
[email protected]eff4aecb2008-08-12 18:37:35119}
120
121void Thread::StopSoon() {
[email protected]4d9bdfaf2008-08-26 05:53:57122 // We should only be called on the same thread that started us.
123 DCHECK_NE(thread_id_, PlatformThread::CurrentId());
[email protected]eff4aecb2008-08-12 18:37:35124
[email protected]3c92e242009-09-21 17:03:46125 if (stopping_ || !message_loop_)
[email protected]88f333122009-09-14 23:47:38126 return;
initial.commitd7cae122008-07-26 21:49:38127
[email protected]88f333122009-09-14 23:47:38128 stopping_ = true;
initial.commitd7cae122008-07-26 21:49:38129 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
initial.commitd7cae122008-07-26 21:49:38130}
131
[email protected]828bd792009-09-10 05:00:20132void Thread::Run(MessageLoop* message_loop) {
133 message_loop->Run();
134}
135
[email protected]e9ba26d2008-08-21 09:46:32136void Thread::ThreadMain() {
initial.commitd7cae122008-07-26 21:49:38137 // The message loop for this thread.
[email protected]4d9bdfaf2008-08-26 05:53:57138 MessageLoop message_loop(startup_data_->options.message_loop_type);
initial.commitd7cae122008-07-26 21:49:38139
[email protected]eff4aecb2008-08-12 18:37:35140 // Complete the initialization of our Thread object.
[email protected]e9ba26d2008-08-21 09:46:32141 thread_id_ = PlatformThread::CurrentId();
[email protected]7d0f94452008-08-25 13:54:18142 PlatformThread::SetName(name_.c_str());
[email protected]94d0d3e2009-11-23 19:17:33143 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector.
[email protected]e9ba26d2008-08-21 09:46:32144 message_loop.set_thread_name(name_);
145 message_loop_ = &message_loop;
[email protected]e9ba26d2008-08-21 09:46:32146
[email protected]cd636d82009-01-27 01:26:16147 // Let the thread do extra initialization.
148 // Let's do this before signaling we are started.
149 Init();
150
[email protected]4d9bdfaf2008-08-26 05:53:57151 startup_data_->event.Signal();
152 // startup_data_ can't be touched anymore since the starting thread is now
[email protected]e9ba26d2008-08-21 09:46:32153 // unlocked.
initial.commitd7cae122008-07-26 21:49:38154
[email protected]828bd792009-09-10 05:00:20155 Run(message_loop_);
initial.commitd7cae122008-07-26 21:49:38156
157 // Let the thread do extra cleanup.
[email protected]e9ba26d2008-08-21 09:46:32158 CleanUp();
initial.commitd7cae122008-07-26 21:49:38159
160 // Assert that MessageLoop::Quit was called by ThreadQuitTask.
[email protected]e9ba26d2008-08-21 09:46:32161 DCHECK(GetThreadWasQuitProperly());
initial.commitd7cae122008-07-26 21:49:38162
[email protected]eff4aecb2008-08-12 18:37:35163 // We can't receive messages anymore.
[email protected]e9ba26d2008-08-21 09:46:32164 message_loop_ = NULL;
[email protected]b026f4a2009-01-28 17:21:23165 thread_id_ = 0;
initial.commitd7cae122008-07-26 21:49:38166}
license.botbf09a502008-08-24 00:55:55167
[email protected]4d9bdfaf2008-08-26 05:53:57168} // namespace base