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/string_util.h"
9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
10 #include "base/thread_local.h"
11 #include "base/waitable_event.h"
15 // This task is used to trigger the message loop to exit.
16 class ThreadQuitTask
: public Task
{
19 MessageLoop::current()->Quit();
20 Thread::SetThreadWasQuitProperly(true);
24 // Used to pass data to ThreadMain. This structure is allocated on the stack
25 // from within StartWithOptions.
26 struct Thread::StartupData
{
27 // We get away with a const reference here because of how we are allocated.
28 const Thread::Options
& options
;
30 // Used to synchronize thread startup.
33 explicit StartupData(const Options
& opt
)
35 event(false, false) {}
38 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";
88 startup_data_
= NULL
; // Record that we failed to start.
92 // Wait for the thread to start and initialize message_loop_
93 startup_data
.event
.Wait();
95 DCHECK(message_loop_
);
100 if (!thread_was_started())
105 // Wait for the thread to exit.
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.
110 PlatformThread::Join(thread_
);
112 // The thread should NULL message_loop_ on exit.
113 DCHECK(!message_loop_
);
115 // The thread no longer needs to be joined.
116 startup_data_
= NULL
;
121 void Thread::StopSoon() {
122 // We should only be called on the same thread that started us.
124 // Reading thread_id_ without a lock can lead to a benign data race
125 // with ThreadMain, so we annotate it to stay silent under ThreadSanitizer.
126 DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_
), PlatformThread::CurrentId());
128 if (stopping_
|| !message_loop_
)
132 message_loop_
->PostTask(FROM_HERE
, new ThreadQuitTask());
135 void Thread::Run(MessageLoop
* message_loop
) {
139 void Thread::ThreadMain() {
141 // The message loop for this thread.
142 MessageLoop
message_loop(startup_data_
->options
.message_loop_type
);
144 // Complete the initialization of our Thread object.
145 thread_id_
= PlatformThread::CurrentId();
146 PlatformThread::SetName(name_
.c_str());
147 ANNOTATE_THREAD_NAME(name_
.c_str()); // Tell the name to race detector.
148 message_loop
.set_thread_name(name_
);
149 message_loop_
= &message_loop
;
150 message_loop_proxy_
= MessageLoopProxy::CreateForCurrentThread();
152 // Let the thread do extra initialization.
153 // Let's do this before signaling we are started.
156 startup_data_
->event
.Signal();
157 // startup_data_ can't be touched anymore since the starting thread is now
162 // Let the thread do extra cleanup.
165 // Assert that MessageLoop::Quit was called by ThreadQuitTask.
166 DCHECK(GetThreadWasQuitProperly());
168 // We can't receive messages anymore.
169 message_loop_
= NULL
;
170 message_loop_proxy_
= NULL
;
172 CleanUpAfterMessageLoopDestruction();