blob: 09f20a7417fc02535352f61b7ff74e2db9355c2d [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]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
[email protected]b026f4a2009-01-28 17:21:2332 explicit StartupData(const Options& opt)
33 : options(opt),
34 event(false, false) {}
[email protected]4d9bdfaf2008-08-26 05:53:5735};
36
initial.commitd7cae122008-07-26 21:49:3837Thread::Thread(const char *name)
[email protected]88f333122009-09-14 23:47:3838 : stopping_(false),
39 startup_data_(NULL),
[email protected]65487262008-08-26 06:06:0140 thread_(0),
[email protected]4d9bdfaf2008-08-26 05:53:5741 message_loop_(NULL),
42 thread_id_(0),
43 name_(name) {
initial.commitd7cae122008-07-26 21:49:3844}
45
46Thread::~Thread() {
47 Stop();
48}
49
[email protected]f886b7bf2008-09-10 10:54:0650namespace {
51
initial.commitd7cae122008-07-26 21:49:3852// We use this thread-local variable to record whether or not a thread exited
53// because its Stop method was called. This allows us to catch cases where
54// MessageLoop::Quit() is called directly, which is unexpected when using a
55// Thread to setup and run a MessageLoop.
[email protected]f886b7bf2008-09-10 10:54:0656base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool(
57 base::LINKER_INITIALIZED);
[email protected]83a05dd2008-09-03 16:47:3758
59} // namespace
initial.commitd7cae122008-07-26 21:49:3860
61void Thread::SetThreadWasQuitProperly(bool flag) {
[email protected]f886b7bf2008-09-10 10:54:0662 lazy_tls_bool.Pointer()->Set(flag);
initial.commitd7cae122008-07-26 21:49:3863}
64
65bool Thread::GetThreadWasQuitProperly() {
66 bool quit_properly = true;
67#ifndef NDEBUG
[email protected]f886b7bf2008-09-10 10:54:0668 quit_properly = lazy_tls_bool.Pointer()->Get();
initial.commitd7cae122008-07-26 21:49:3869#endif
70 return quit_properly;
71}
72
initial.commitd7cae122008-07-26 21:49:3873bool Thread::Start() {
[email protected]4d9bdfaf2008-08-26 05:53:5774 return StartWithOptions(Options());
initial.commitd7cae122008-07-26 21:49:3875}
76
[email protected]4d9bdfaf2008-08-26 05:53:5777bool Thread::StartWithOptions(const Options& options) {
[email protected]e9ba26d2008-08-21 09:46:3278 DCHECK(!message_loop_);
initial.commitd7cae122008-07-26 21:49:3879
[email protected]e9ba26d2008-08-21 09:46:3280 SetThreadWasQuitProperly(false);
81
[email protected]4d9bdfaf2008-08-26 05:53:5782 StartupData startup_data(options);
83 startup_data_ = &startup_data;
[email protected]e9ba26d2008-08-21 09:46:3284
[email protected]4d9bdfaf2008-08-26 05:53:5785 if (!PlatformThread::Create(options.stack_size, this, &thread_)) {
initial.commitd7cae122008-07-26 21:49:3886 DLOG(ERROR) << "failed to create thread";
[email protected]4d9bdfaf2008-08-26 05:53:5787 startup_data_ = NULL; // Record that we failed to start.
initial.commitd7cae122008-07-26 21:49:3888 return false;
89 }
90
91 // Wait for the thread to start and initialize message_loop_
[email protected]4d9bdfaf2008-08-26 05:53:5792 startup_data.event.Wait();
[email protected]e9ba26d2008-08-21 09:46:3293
94 DCHECK(message_loop_);
initial.commitd7cae122008-07-26 21:49:3895 return true;
96}
97
98void Thread::Stop() {
[email protected]4d9bdfaf2008-08-26 05:53:5799 if (!thread_was_started())
initial.commitd7cae122008-07-26 21:49:38100 return;
101
[email protected]88f333122009-09-14 23:47:38102 StopSoon();
initial.commitd7cae122008-07-26 21:49:38103
[email protected]88f333122009-09-14 23:47:38104 // Wait for the thread to exit.
[email protected]4d9bdfaf2008-08-26 05:53:57105 //
106 // TODO(darin): Unfortunately, we need to keep message_loop_ around until
107 // the thread exits. Some consumers are abusing the API. Make them stop.
108 //
[email protected]e9ba26d2008-08-21 09:46:32109 PlatformThread::Join(thread_);
[email protected]eff4aecb2008-08-12 18:37:35110
[email protected]88f333122009-09-14 23:47:38111 // The thread should NULL message_loop_ on exit.
112 DCHECK(!message_loop_);
[email protected]e9ba26d2008-08-21 09:46:32113
114 // The thread no longer needs to be joined.
[email protected]4d9bdfaf2008-08-26 05:53:57115 startup_data_ = NULL;
[email protected]88f333122009-09-14 23:47:38116
117 stopping_ = false;
[email protected]eff4aecb2008-08-12 18:37:35118}
119
120void Thread::StopSoon() {
[email protected]4d9bdfaf2008-08-26 05:53:57121 // We should only be called on the same thread that started us.
122 DCHECK_NE(thread_id_, PlatformThread::CurrentId());
[email protected]eff4aecb2008-08-12 18:37:35123
[email protected]3c92e242009-09-21 17:03:46124 if (stopping_ || !message_loop_)
[email protected]88f333122009-09-14 23:47:38125 return;
initial.commitd7cae122008-07-26 21:49:38126
[email protected]88f333122009-09-14 23:47:38127 stopping_ = true;
initial.commitd7cae122008-07-26 21:49:38128 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
initial.commitd7cae122008-07-26 21:49:38129}
130
[email protected]828bd792009-09-10 05:00:20131void Thread::Run(MessageLoop* message_loop) {
132 message_loop->Run();
133}
134
[email protected]e9ba26d2008-08-21 09:46:32135void Thread::ThreadMain() {
initial.commitd7cae122008-07-26 21:49:38136 // The message loop for this thread.
[email protected]4d9bdfaf2008-08-26 05:53:57137 MessageLoop message_loop(startup_data_->options.message_loop_type);
initial.commitd7cae122008-07-26 21:49:38138
[email protected]eff4aecb2008-08-12 18:37:35139 // Complete the initialization of our Thread object.
[email protected]e9ba26d2008-08-21 09:46:32140 thread_id_ = PlatformThread::CurrentId();
[email protected]7d0f94452008-08-25 13:54:18141 PlatformThread::SetName(name_.c_str());
[email protected]e9ba26d2008-08-21 09:46:32142 message_loop.set_thread_name(name_);
143 message_loop_ = &message_loop;
[email protected]e9ba26d2008-08-21 09:46:32144
[email protected]cd636d82009-01-27 01:26:16145 // Let the thread do extra initialization.
146 // Let's do this before signaling we are started.
147 Init();
148
[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
[email protected]828bd792009-09-10 05:00:20153 Run(message_loop_);
initial.commitd7cae122008-07-26 21:49:38154
155 // Let the thread do extra cleanup.
[email protected]e9ba26d2008-08-21 09:46:32156 CleanUp();
initial.commitd7cae122008-07-26 21:49:38157
158 // Assert that MessageLoop::Quit was called by ThreadQuitTask.
[email protected]e9ba26d2008-08-21 09:46:32159 DCHECK(GetThreadWasQuitProperly());
initial.commitd7cae122008-07-26 21:49:38160
[email protected]eff4aecb2008-08-12 18:37:35161 // We can't receive messages anymore.
[email protected]e9ba26d2008-08-21 09:46:32162 message_loop_ = NULL;
[email protected]b026f4a2009-01-28 17:21:23163 thread_id_ = 0;
initial.commitd7cae122008-07-26 21:49:38164}
license.botbf09a502008-08-24 00:55:55165
[email protected]4d9bdfaf2008-08-26 05:53:57166} // namespace base