1 // Copyright (c) 2011 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.
5 #include "base/threading/thread.h"
7 #include "base/lazy_instance.h"
8 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
9 #include "base/threading/thread_local.h"
10 #include "base/synchronization/waitable_event.h"
16 // We use this thread-local variable to record whether or not a thread exited
17 // because its Stop method was called. This allows us to catch cases where
18 // MessageLoop::Quit() is called directly, which is unexpected when using a
19 // Thread to setup and run a MessageLoop.
20 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool(
21 base::LINKER_INITIALIZED);
25 // This task is used to trigger the message loop to exit.
26 class ThreadQuitTask : public Task {
29 MessageLoop::current()->Quit();
30 Thread::SetThreadWasQuitProperly(true);
34 // Used to pass data to ThreadMain. This structure is allocated on the stack
35 // from within StartWithOptions.
36 struct Thread::StartupData {
37 // We get away with a const reference here because of how we are allocated.
38 const Thread::Options& options;
40 // Used to synchronize thread startup.
43 explicit StartupData(const Options& opt)
45 event(false, false) {}
48 Thread::Thread(const char* name)
54 thread_id_(kInvalidThreadId),
62 bool Thread::Start() {
63 return StartWithOptions(Options());
66 bool Thread::StartWithOptions(const Options& options) {
67 DCHECK(!message_loop_);
69 SetThreadWasQuitProperly(false);
71 StartupData startup_data(options);
72 startup_data_ = &startup_data;
74 if (!PlatformThread::Create(options.stack_size, this, &thread_)) {
75 DLOG(ERROR) << "failed to create thread";
80 // Wait for the thread to start and initialize message_loop_
81 startup_data.event.Wait();
83 // set it to NULL so we don't keep a pointer to some object on the stack.
87 DCHECK(message_loop_);
92 if (!thread_was_started())
97 // Wait for the thread to exit.
99 // TODO(darin): Unfortunately, we need to keep message_loop_ around until
100 // the thread exits. Some consumers are abusing the API. Make them stop.
102 PlatformThread::Join(thread_);
104 // The thread should NULL message_loop_ on exit.
105 DCHECK(!message_loop_);
107 // The thread no longer needs to be joined.
113 void Thread::StopSoon() {
114 // We should only be called on the same thread that started us.
116 // Reading thread_id_ without a lock can lead to a benign data race
117 // with ThreadMain, so we annotate it to stay silent under ThreadSanitizer.
118 DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_), PlatformThread::CurrentId());
120 if (stopping_ || !message_loop_)
124 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
127 void Thread::Run(MessageLoop* message_loop) {
131 void Thread::SetThreadWasQuitProperly(bool flag) {
132 lazy_tls_bool.Pointer()->Set(flag);
135 bool Thread::GetThreadWasQuitProperly() {
136 bool quit_properly = true;
138 quit_properly = lazy_tls_bool.Pointer()->Get();
140 return quit_properly;
143 void Thread::ThreadMain() {
145 // The message loop for this thread.
146 MessageLoop message_loop(startup_data_->options.message_loop_type);
148 // Complete the initialization of our Thread object.
149 thread_id_ = PlatformThread::CurrentId();
150 PlatformThread::SetName(name_.c_str());
151 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector.
152 message_loop.set_thread_name(name_);
153 message_loop_ = &message_loop;
155 // Let the thread do extra initialization.
156 // Let's do this before signaling we are started.
159 startup_data_->event.Signal();
160 // startup_data_ can't be touched anymore since the starting thread is now
165 // Let the thread do extra cleanup.
168 // Assert that MessageLoop::Quit was called by ThreadQuitTask.
169 DCHECK(GetThreadWasQuitProperly());
171 // We can't receive messages anymore.
172 message_loop_ = NULL;
174 thread_id_ = kInvalidThreadId;