1 // Copyright (c) 2006-2009 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/thread.h"
7 #include "base/lazy_instance.h"
8 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
9 #include "base/thread_local.h"
10 #include "base/waitable_event.h"
14 // This task is used to trigger the message loop to exit.
15 class ThreadQuitTask
: public Task
{
18 MessageLoop::current()->Quit();
19 Thread::SetThreadWasQuitProperly(true);
23 // Used to pass data to ThreadMain. This structure is allocated on the stack
24 // from within StartWithOptions.
25 struct Thread::StartupData
{
26 // We get away with a const reference here because of how we are allocated.
27 const Thread::Options
& options
;
29 // Used to synchronize thread startup.
32 explicit StartupData(const Options
& opt
)
34 event(false, false) {}
37 Thread::Thread(const char* name
)
53 // 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.
57 base::LazyInstance
<base::ThreadLocalBoolean
> lazy_tls_bool(
58 base::LINKER_INITIALIZED
);
62 void Thread::SetThreadWasQuitProperly(bool flag
) {
63 lazy_tls_bool
.Pointer()->Set(flag
);
66 bool Thread::GetThreadWasQuitProperly() {
67 bool quit_properly
= true;
69 quit_properly
= lazy_tls_bool
.Pointer()->Get();
74 bool Thread::Start() {
75 return StartWithOptions(Options());
78 bool Thread::StartWithOptions(const Options
& options
) {
79 DCHECK(!message_loop_
);
81 SetThreadWasQuitProperly(false);
83 StartupData
startup_data(options
);
84 startup_data_
= &startup_data
;
86 if (!PlatformThread::Create(options
.stack_size
, this, &thread_
)) {
87 DLOG(ERROR
) << "failed to create thread";
92 // Wait for the thread to start and initialize message_loop_
93 startup_data
.event
.Wait();
95 // set it to NULL so we don't keep a pointer to some object on the stack.
99 DCHECK(message_loop_
);
103 void Thread::Stop() {
104 if (!thread_was_started())
109 // Wait for the thread to exit.
111 // TODO(darin): Unfortunately, we need to keep message_loop_ around until
112 // the thread exits. Some consumers are abusing the API. Make them stop.
114 PlatformThread::Join(thread_
);
116 // The thread should NULL message_loop_ on exit.
117 DCHECK(!message_loop_
);
119 // The thread no longer needs to be joined.
125 void Thread::StopSoon() {
126 // We should only be called on the same thread that started us.
128 // Reading thread_id_ without a lock can lead to a benign data race
129 // with ThreadMain, so we annotate it to stay silent under ThreadSanitizer.
130 DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_
), PlatformThread::CurrentId());
132 if (stopping_
|| !message_loop_
)
136 message_loop_
->PostTask(FROM_HERE
, new ThreadQuitTask());
139 void Thread::Run(MessageLoop
* message_loop
) {
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
;
154 message_loop_proxy_
= MessageLoopProxy::CreateForCurrentThread();
156 // Let the thread do extra initialization.
157 // Let's do this before signaling we are started.
160 startup_data_
->event
.Signal();
161 // startup_data_ can't be touched anymore since the starting thread is now
166 // Let the thread do extra cleanup.
169 // Assert that MessageLoop::Quit was called by ThreadQuitTask.
170 DCHECK(GetThreadWasQuitProperly());
172 // We can't receive messages anymore.
173 message_loop_
= NULL
;
174 message_loop_proxy_
= NULL
;
176 CleanUpAfterMessageLoopDestruction();