1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread.h"
6*635a8641SAndroid Build Coastguard Worker
7*635a8641SAndroid Build Coastguard Worker #include "base/bind.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/bind_helpers.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/lazy_instance.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/location.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/run_loop.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/waitable_event.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_id_name_manager.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_local.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_restrictions.h"
18*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
19*635a8641SAndroid Build Coastguard Worker
20*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) && !defined(OS_NACL)
21*635a8641SAndroid Build Coastguard Worker #include "base/files/file_descriptor_watcher_posix.h"
22*635a8641SAndroid Build Coastguard Worker #endif
23*635a8641SAndroid Build Coastguard Worker
24*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
25*635a8641SAndroid Build Coastguard Worker #include "base/win/scoped_com_initializer.h"
26*635a8641SAndroid Build Coastguard Worker #endif
27*635a8641SAndroid Build Coastguard Worker
28*635a8641SAndroid Build Coastguard Worker namespace base {
29*635a8641SAndroid Build Coastguard Worker
30*635a8641SAndroid Build Coastguard Worker namespace {
31*635a8641SAndroid Build Coastguard Worker
32*635a8641SAndroid Build Coastguard Worker // We use this thread-local variable to record whether or not a thread exited
33*635a8641SAndroid Build Coastguard Worker // because its Stop method was called. This allows us to catch cases where
34*635a8641SAndroid Build Coastguard Worker // MessageLoop::QuitWhenIdle() is called directly, which is unexpected when
35*635a8641SAndroid Build Coastguard Worker // using a Thread to setup and run a MessageLoop.
36*635a8641SAndroid Build Coastguard Worker base::LazyInstance<base::ThreadLocalBoolean>::Leaky lazy_tls_bool =
37*635a8641SAndroid Build Coastguard Worker LAZY_INSTANCE_INITIALIZER;
38*635a8641SAndroid Build Coastguard Worker
39*635a8641SAndroid Build Coastguard Worker } // namespace
40*635a8641SAndroid Build Coastguard Worker
41*635a8641SAndroid Build Coastguard Worker Thread::Options::Options() = default;
42*635a8641SAndroid Build Coastguard Worker
Options(MessageLoop::Type type,size_t size)43*635a8641SAndroid Build Coastguard Worker Thread::Options::Options(MessageLoop::Type type, size_t size)
44*635a8641SAndroid Build Coastguard Worker : message_loop_type(type), stack_size(size) {}
45*635a8641SAndroid Build Coastguard Worker
46*635a8641SAndroid Build Coastguard Worker Thread::Options::Options(const Options& other) = default;
47*635a8641SAndroid Build Coastguard Worker
48*635a8641SAndroid Build Coastguard Worker Thread::Options::~Options() = default;
49*635a8641SAndroid Build Coastguard Worker
Thread(const std::string & name)50*635a8641SAndroid Build Coastguard Worker Thread::Thread(const std::string& name)
51*635a8641SAndroid Build Coastguard Worker : id_event_(WaitableEvent::ResetPolicy::MANUAL,
52*635a8641SAndroid Build Coastguard Worker WaitableEvent::InitialState::NOT_SIGNALED),
53*635a8641SAndroid Build Coastguard Worker name_(name),
54*635a8641SAndroid Build Coastguard Worker start_event_(WaitableEvent::ResetPolicy::MANUAL,
55*635a8641SAndroid Build Coastguard Worker WaitableEvent::InitialState::NOT_SIGNALED) {
56*635a8641SAndroid Build Coastguard Worker // Only bind the sequence on Start(): the state is constant between
57*635a8641SAndroid Build Coastguard Worker // construction and Start() and it's thus valid for Start() to be called on
58*635a8641SAndroid Build Coastguard Worker // another sequence as long as every other operation is then performed on that
59*635a8641SAndroid Build Coastguard Worker // sequence.
60*635a8641SAndroid Build Coastguard Worker owning_sequence_checker_.DetachFromSequence();
61*635a8641SAndroid Build Coastguard Worker }
62*635a8641SAndroid Build Coastguard Worker
~Thread()63*635a8641SAndroid Build Coastguard Worker Thread::~Thread() {
64*635a8641SAndroid Build Coastguard Worker Stop();
65*635a8641SAndroid Build Coastguard Worker }
66*635a8641SAndroid Build Coastguard Worker
Start()67*635a8641SAndroid Build Coastguard Worker bool Thread::Start() {
68*635a8641SAndroid Build Coastguard Worker DCHECK(owning_sequence_checker_.CalledOnValidSequence());
69*635a8641SAndroid Build Coastguard Worker
70*635a8641SAndroid Build Coastguard Worker Options options;
71*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
72*635a8641SAndroid Build Coastguard Worker if (com_status_ == STA)
73*635a8641SAndroid Build Coastguard Worker options.message_loop_type = MessageLoop::TYPE_UI;
74*635a8641SAndroid Build Coastguard Worker #endif
75*635a8641SAndroid Build Coastguard Worker return StartWithOptions(options);
76*635a8641SAndroid Build Coastguard Worker }
77*635a8641SAndroid Build Coastguard Worker
StartWithOptions(const Options & options)78*635a8641SAndroid Build Coastguard Worker bool Thread::StartWithOptions(const Options& options) {
79*635a8641SAndroid Build Coastguard Worker DCHECK(owning_sequence_checker_.CalledOnValidSequence());
80*635a8641SAndroid Build Coastguard Worker DCHECK(!message_loop_);
81*635a8641SAndroid Build Coastguard Worker DCHECK(!IsRunning());
82*635a8641SAndroid Build Coastguard Worker DCHECK(!stopping_) << "Starting a non-joinable thread a second time? That's "
83*635a8641SAndroid Build Coastguard Worker << "not allowed!";
84*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
85*635a8641SAndroid Build Coastguard Worker DCHECK((com_status_ != STA) ||
86*635a8641SAndroid Build Coastguard Worker (options.message_loop_type == MessageLoop::TYPE_UI));
87*635a8641SAndroid Build Coastguard Worker #endif
88*635a8641SAndroid Build Coastguard Worker
89*635a8641SAndroid Build Coastguard Worker // Reset |id_| here to support restarting the thread.
90*635a8641SAndroid Build Coastguard Worker id_event_.Reset();
91*635a8641SAndroid Build Coastguard Worker id_ = kInvalidThreadId;
92*635a8641SAndroid Build Coastguard Worker
93*635a8641SAndroid Build Coastguard Worker SetThreadWasQuitProperly(false);
94*635a8641SAndroid Build Coastguard Worker
95*635a8641SAndroid Build Coastguard Worker MessageLoop::Type type = options.message_loop_type;
96*635a8641SAndroid Build Coastguard Worker if (!options.message_pump_factory.is_null())
97*635a8641SAndroid Build Coastguard Worker type = MessageLoop::TYPE_CUSTOM;
98*635a8641SAndroid Build Coastguard Worker
99*635a8641SAndroid Build Coastguard Worker message_loop_timer_slack_ = options.timer_slack;
100*635a8641SAndroid Build Coastguard Worker std::unique_ptr<MessageLoop> message_loop_owned =
101*635a8641SAndroid Build Coastguard Worker MessageLoop::CreateUnbound(type, options.message_pump_factory);
102*635a8641SAndroid Build Coastguard Worker message_loop_ = message_loop_owned.get();
103*635a8641SAndroid Build Coastguard Worker start_event_.Reset();
104*635a8641SAndroid Build Coastguard Worker
105*635a8641SAndroid Build Coastguard Worker // Hold |thread_lock_| while starting the new thread to synchronize with
106*635a8641SAndroid Build Coastguard Worker // Stop() while it's not guaranteed to be sequenced (until crbug/629139 is
107*635a8641SAndroid Build Coastguard Worker // fixed).
108*635a8641SAndroid Build Coastguard Worker {
109*635a8641SAndroid Build Coastguard Worker AutoLock lock(thread_lock_);
110*635a8641SAndroid Build Coastguard Worker bool success =
111*635a8641SAndroid Build Coastguard Worker options.joinable
112*635a8641SAndroid Build Coastguard Worker ? PlatformThread::CreateWithPriority(options.stack_size, this,
113*635a8641SAndroid Build Coastguard Worker &thread_, options.priority)
114*635a8641SAndroid Build Coastguard Worker : PlatformThread::CreateNonJoinableWithPriority(
115*635a8641SAndroid Build Coastguard Worker options.stack_size, this, options.priority);
116*635a8641SAndroid Build Coastguard Worker if (!success) {
117*635a8641SAndroid Build Coastguard Worker DLOG(ERROR) << "failed to create thread";
118*635a8641SAndroid Build Coastguard Worker message_loop_ = nullptr;
119*635a8641SAndroid Build Coastguard Worker return false;
120*635a8641SAndroid Build Coastguard Worker }
121*635a8641SAndroid Build Coastguard Worker }
122*635a8641SAndroid Build Coastguard Worker
123*635a8641SAndroid Build Coastguard Worker joinable_ = options.joinable;
124*635a8641SAndroid Build Coastguard Worker
125*635a8641SAndroid Build Coastguard Worker // The ownership of |message_loop_| is managed by the newly created thread
126*635a8641SAndroid Build Coastguard Worker // within the ThreadMain.
127*635a8641SAndroid Build Coastguard Worker ignore_result(message_loop_owned.release());
128*635a8641SAndroid Build Coastguard Worker
129*635a8641SAndroid Build Coastguard Worker DCHECK(message_loop_);
130*635a8641SAndroid Build Coastguard Worker return true;
131*635a8641SAndroid Build Coastguard Worker }
132*635a8641SAndroid Build Coastguard Worker
StartAndWaitForTesting()133*635a8641SAndroid Build Coastguard Worker bool Thread::StartAndWaitForTesting() {
134*635a8641SAndroid Build Coastguard Worker DCHECK(owning_sequence_checker_.CalledOnValidSequence());
135*635a8641SAndroid Build Coastguard Worker bool result = Start();
136*635a8641SAndroid Build Coastguard Worker if (!result)
137*635a8641SAndroid Build Coastguard Worker return false;
138*635a8641SAndroid Build Coastguard Worker WaitUntilThreadStarted();
139*635a8641SAndroid Build Coastguard Worker return true;
140*635a8641SAndroid Build Coastguard Worker }
141*635a8641SAndroid Build Coastguard Worker
WaitUntilThreadStarted() const142*635a8641SAndroid Build Coastguard Worker bool Thread::WaitUntilThreadStarted() const {
143*635a8641SAndroid Build Coastguard Worker DCHECK(owning_sequence_checker_.CalledOnValidSequence());
144*635a8641SAndroid Build Coastguard Worker if (!message_loop_)
145*635a8641SAndroid Build Coastguard Worker return false;
146*635a8641SAndroid Build Coastguard Worker base::ThreadRestrictions::ScopedAllowWait allow_wait;
147*635a8641SAndroid Build Coastguard Worker start_event_.Wait();
148*635a8641SAndroid Build Coastguard Worker return true;
149*635a8641SAndroid Build Coastguard Worker }
150*635a8641SAndroid Build Coastguard Worker
FlushForTesting()151*635a8641SAndroid Build Coastguard Worker void Thread::FlushForTesting() {
152*635a8641SAndroid Build Coastguard Worker DCHECK(owning_sequence_checker_.CalledOnValidSequence());
153*635a8641SAndroid Build Coastguard Worker if (!message_loop_)
154*635a8641SAndroid Build Coastguard Worker return;
155*635a8641SAndroid Build Coastguard Worker
156*635a8641SAndroid Build Coastguard Worker WaitableEvent done(WaitableEvent::ResetPolicy::AUTOMATIC,
157*635a8641SAndroid Build Coastguard Worker WaitableEvent::InitialState::NOT_SIGNALED);
158*635a8641SAndroid Build Coastguard Worker task_runner()->PostTask(FROM_HERE,
159*635a8641SAndroid Build Coastguard Worker BindOnce(&WaitableEvent::Signal, Unretained(&done)));
160*635a8641SAndroid Build Coastguard Worker done.Wait();
161*635a8641SAndroid Build Coastguard Worker }
162*635a8641SAndroid Build Coastguard Worker
Stop()163*635a8641SAndroid Build Coastguard Worker void Thread::Stop() {
164*635a8641SAndroid Build Coastguard Worker DCHECK(joinable_);
165*635a8641SAndroid Build Coastguard Worker
166*635a8641SAndroid Build Coastguard Worker // TODO(gab): Fix improper usage of this API (http://crbug.com/629139) and
167*635a8641SAndroid Build Coastguard Worker // enable this check, until then synchronization with Start() via
168*635a8641SAndroid Build Coastguard Worker // |thread_lock_| is required...
169*635a8641SAndroid Build Coastguard Worker // DCHECK(owning_sequence_checker_.CalledOnValidSequence());
170*635a8641SAndroid Build Coastguard Worker AutoLock lock(thread_lock_);
171*635a8641SAndroid Build Coastguard Worker
172*635a8641SAndroid Build Coastguard Worker StopSoon();
173*635a8641SAndroid Build Coastguard Worker
174*635a8641SAndroid Build Coastguard Worker // Can't join if the |thread_| is either already gone or is non-joinable.
175*635a8641SAndroid Build Coastguard Worker if (thread_.is_null())
176*635a8641SAndroid Build Coastguard Worker return;
177*635a8641SAndroid Build Coastguard Worker
178*635a8641SAndroid Build Coastguard Worker // Wait for the thread to exit.
179*635a8641SAndroid Build Coastguard Worker //
180*635a8641SAndroid Build Coastguard Worker // TODO(darin): Unfortunately, we need to keep |message_loop_| around until
181*635a8641SAndroid Build Coastguard Worker // the thread exits. Some consumers are abusing the API. Make them stop.
182*635a8641SAndroid Build Coastguard Worker //
183*635a8641SAndroid Build Coastguard Worker PlatformThread::Join(thread_);
184*635a8641SAndroid Build Coastguard Worker thread_ = base::PlatformThreadHandle();
185*635a8641SAndroid Build Coastguard Worker
186*635a8641SAndroid Build Coastguard Worker // The thread should nullify |message_loop_| on exit (note: Join() adds an
187*635a8641SAndroid Build Coastguard Worker // implicit memory barrier and no lock is thus required for this check).
188*635a8641SAndroid Build Coastguard Worker DCHECK(!message_loop_);
189*635a8641SAndroid Build Coastguard Worker
190*635a8641SAndroid Build Coastguard Worker stopping_ = false;
191*635a8641SAndroid Build Coastguard Worker }
192*635a8641SAndroid Build Coastguard Worker
StopSoon()193*635a8641SAndroid Build Coastguard Worker void Thread::StopSoon() {
194*635a8641SAndroid Build Coastguard Worker // TODO(gab): Fix improper usage of this API (http://crbug.com/629139) and
195*635a8641SAndroid Build Coastguard Worker // enable this check.
196*635a8641SAndroid Build Coastguard Worker // DCHECK(owning_sequence_checker_.CalledOnValidSequence());
197*635a8641SAndroid Build Coastguard Worker
198*635a8641SAndroid Build Coastguard Worker if (stopping_ || !message_loop_)
199*635a8641SAndroid Build Coastguard Worker return;
200*635a8641SAndroid Build Coastguard Worker
201*635a8641SAndroid Build Coastguard Worker stopping_ = true;
202*635a8641SAndroid Build Coastguard Worker
203*635a8641SAndroid Build Coastguard Worker if (using_external_message_loop_) {
204*635a8641SAndroid Build Coastguard Worker // Setting |stopping_| to true above should have been sufficient for this
205*635a8641SAndroid Build Coastguard Worker // thread to be considered "stopped" per it having never set its |running_|
206*635a8641SAndroid Build Coastguard Worker // bit by lack of its own ThreadMain.
207*635a8641SAndroid Build Coastguard Worker DCHECK(!IsRunning());
208*635a8641SAndroid Build Coastguard Worker message_loop_ = nullptr;
209*635a8641SAndroid Build Coastguard Worker return;
210*635a8641SAndroid Build Coastguard Worker }
211*635a8641SAndroid Build Coastguard Worker
212*635a8641SAndroid Build Coastguard Worker task_runner()->PostTask(
213*635a8641SAndroid Build Coastguard Worker FROM_HERE, base::BindOnce(&Thread::ThreadQuitHelper, Unretained(this)));
214*635a8641SAndroid Build Coastguard Worker }
215*635a8641SAndroid Build Coastguard Worker
DetachFromSequence()216*635a8641SAndroid Build Coastguard Worker void Thread::DetachFromSequence() {
217*635a8641SAndroid Build Coastguard Worker DCHECK(owning_sequence_checker_.CalledOnValidSequence());
218*635a8641SAndroid Build Coastguard Worker owning_sequence_checker_.DetachFromSequence();
219*635a8641SAndroid Build Coastguard Worker }
220*635a8641SAndroid Build Coastguard Worker
GetThreadId() const221*635a8641SAndroid Build Coastguard Worker PlatformThreadId Thread::GetThreadId() const {
222*635a8641SAndroid Build Coastguard Worker // If the thread is created but not started yet, wait for |id_| being ready.
223*635a8641SAndroid Build Coastguard Worker base::ThreadRestrictions::ScopedAllowWait allow_wait;
224*635a8641SAndroid Build Coastguard Worker id_event_.Wait();
225*635a8641SAndroid Build Coastguard Worker return id_;
226*635a8641SAndroid Build Coastguard Worker }
227*635a8641SAndroid Build Coastguard Worker
GetThreadHandle() const228*635a8641SAndroid Build Coastguard Worker PlatformThreadHandle Thread::GetThreadHandle() const {
229*635a8641SAndroid Build Coastguard Worker AutoLock lock(thread_lock_);
230*635a8641SAndroid Build Coastguard Worker return thread_;
231*635a8641SAndroid Build Coastguard Worker }
232*635a8641SAndroid Build Coastguard Worker
IsRunning() const233*635a8641SAndroid Build Coastguard Worker bool Thread::IsRunning() const {
234*635a8641SAndroid Build Coastguard Worker // TODO(gab): Fix improper usage of this API (http://crbug.com/629139) and
235*635a8641SAndroid Build Coastguard Worker // enable this check.
236*635a8641SAndroid Build Coastguard Worker // DCHECK(owning_sequence_checker_.CalledOnValidSequence());
237*635a8641SAndroid Build Coastguard Worker
238*635a8641SAndroid Build Coastguard Worker // If the thread's already started (i.e. |message_loop_| is non-null) and not
239*635a8641SAndroid Build Coastguard Worker // yet requested to stop (i.e. |stopping_| is false) we can just return true.
240*635a8641SAndroid Build Coastguard Worker // (Note that |stopping_| is touched only on the same sequence that starts /
241*635a8641SAndroid Build Coastguard Worker // started the new thread so we need no locking here.)
242*635a8641SAndroid Build Coastguard Worker if (message_loop_ && !stopping_)
243*635a8641SAndroid Build Coastguard Worker return true;
244*635a8641SAndroid Build Coastguard Worker // Otherwise check the |running_| flag, which is set to true by the new thread
245*635a8641SAndroid Build Coastguard Worker // only while it is inside Run().
246*635a8641SAndroid Build Coastguard Worker AutoLock lock(running_lock_);
247*635a8641SAndroid Build Coastguard Worker return running_;
248*635a8641SAndroid Build Coastguard Worker }
249*635a8641SAndroid Build Coastguard Worker
Run(RunLoop * run_loop)250*635a8641SAndroid Build Coastguard Worker void Thread::Run(RunLoop* run_loop) {
251*635a8641SAndroid Build Coastguard Worker // Overridable protected method to be called from our |thread_| only.
252*635a8641SAndroid Build Coastguard Worker DCHECK(id_event_.IsSignaled());
253*635a8641SAndroid Build Coastguard Worker DCHECK_EQ(id_, PlatformThread::CurrentId());
254*635a8641SAndroid Build Coastguard Worker
255*635a8641SAndroid Build Coastguard Worker run_loop->Run();
256*635a8641SAndroid Build Coastguard Worker }
257*635a8641SAndroid Build Coastguard Worker
258*635a8641SAndroid Build Coastguard Worker // static
SetThreadWasQuitProperly(bool flag)259*635a8641SAndroid Build Coastguard Worker void Thread::SetThreadWasQuitProperly(bool flag) {
260*635a8641SAndroid Build Coastguard Worker lazy_tls_bool.Pointer()->Set(flag);
261*635a8641SAndroid Build Coastguard Worker }
262*635a8641SAndroid Build Coastguard Worker
263*635a8641SAndroid Build Coastguard Worker // static
GetThreadWasQuitProperly()264*635a8641SAndroid Build Coastguard Worker bool Thread::GetThreadWasQuitProperly() {
265*635a8641SAndroid Build Coastguard Worker bool quit_properly = true;
266*635a8641SAndroid Build Coastguard Worker #ifndef NDEBUG
267*635a8641SAndroid Build Coastguard Worker quit_properly = lazy_tls_bool.Pointer()->Get();
268*635a8641SAndroid Build Coastguard Worker #endif
269*635a8641SAndroid Build Coastguard Worker return quit_properly;
270*635a8641SAndroid Build Coastguard Worker }
271*635a8641SAndroid Build Coastguard Worker
SetMessageLoop(MessageLoop * message_loop)272*635a8641SAndroid Build Coastguard Worker void Thread::SetMessageLoop(MessageLoop* message_loop) {
273*635a8641SAndroid Build Coastguard Worker DCHECK(owning_sequence_checker_.CalledOnValidSequence());
274*635a8641SAndroid Build Coastguard Worker DCHECK(message_loop);
275*635a8641SAndroid Build Coastguard Worker
276*635a8641SAndroid Build Coastguard Worker // Setting |message_loop_| should suffice for this thread to be considered
277*635a8641SAndroid Build Coastguard Worker // as "running", until Stop() is invoked.
278*635a8641SAndroid Build Coastguard Worker DCHECK(!IsRunning());
279*635a8641SAndroid Build Coastguard Worker message_loop_ = message_loop;
280*635a8641SAndroid Build Coastguard Worker DCHECK(IsRunning());
281*635a8641SAndroid Build Coastguard Worker
282*635a8641SAndroid Build Coastguard Worker using_external_message_loop_ = true;
283*635a8641SAndroid Build Coastguard Worker }
284*635a8641SAndroid Build Coastguard Worker
ThreadMain()285*635a8641SAndroid Build Coastguard Worker void Thread::ThreadMain() {
286*635a8641SAndroid Build Coastguard Worker // First, make GetThreadId() available to avoid deadlocks. It could be called
287*635a8641SAndroid Build Coastguard Worker // any place in the following thread initialization code.
288*635a8641SAndroid Build Coastguard Worker DCHECK(!id_event_.IsSignaled());
289*635a8641SAndroid Build Coastguard Worker // Note: this read of |id_| while |id_event_| isn't signaled is exceptionally
290*635a8641SAndroid Build Coastguard Worker // okay because ThreadMain has a happens-after relationship with the other
291*635a8641SAndroid Build Coastguard Worker // write in StartWithOptions().
292*635a8641SAndroid Build Coastguard Worker DCHECK_EQ(kInvalidThreadId, id_);
293*635a8641SAndroid Build Coastguard Worker id_ = PlatformThread::CurrentId();
294*635a8641SAndroid Build Coastguard Worker DCHECK_NE(kInvalidThreadId, id_);
295*635a8641SAndroid Build Coastguard Worker id_event_.Signal();
296*635a8641SAndroid Build Coastguard Worker
297*635a8641SAndroid Build Coastguard Worker // Complete the initialization of our Thread object.
298*635a8641SAndroid Build Coastguard Worker PlatformThread::SetName(name_.c_str());
299*635a8641SAndroid Build Coastguard Worker ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector.
300*635a8641SAndroid Build Coastguard Worker
301*635a8641SAndroid Build Coastguard Worker // Lazily initialize the |message_loop| so that it can run on this thread.
302*635a8641SAndroid Build Coastguard Worker DCHECK(message_loop_);
303*635a8641SAndroid Build Coastguard Worker std::unique_ptr<MessageLoop> message_loop(message_loop_);
304*635a8641SAndroid Build Coastguard Worker message_loop_->BindToCurrentThread();
305*635a8641SAndroid Build Coastguard Worker message_loop_->SetTimerSlack(message_loop_timer_slack_);
306*635a8641SAndroid Build Coastguard Worker
307*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) && !defined(OS_NACL)
308*635a8641SAndroid Build Coastguard Worker // Allow threads running a MessageLoopForIO to use FileDescriptorWatcher API.
309*635a8641SAndroid Build Coastguard Worker std::unique_ptr<FileDescriptorWatcher> file_descriptor_watcher;
310*635a8641SAndroid Build Coastguard Worker if (MessageLoopForIO::IsCurrent()) {
311*635a8641SAndroid Build Coastguard Worker file_descriptor_watcher.reset(new FileDescriptorWatcher(
312*635a8641SAndroid Build Coastguard Worker static_cast<MessageLoopForIO*>(message_loop_)));
313*635a8641SAndroid Build Coastguard Worker }
314*635a8641SAndroid Build Coastguard Worker #endif
315*635a8641SAndroid Build Coastguard Worker
316*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
317*635a8641SAndroid Build Coastguard Worker std::unique_ptr<win::ScopedCOMInitializer> com_initializer;
318*635a8641SAndroid Build Coastguard Worker if (com_status_ != NONE) {
319*635a8641SAndroid Build Coastguard Worker com_initializer.reset((com_status_ == STA) ?
320*635a8641SAndroid Build Coastguard Worker new win::ScopedCOMInitializer() :
321*635a8641SAndroid Build Coastguard Worker new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA));
322*635a8641SAndroid Build Coastguard Worker }
323*635a8641SAndroid Build Coastguard Worker #endif
324*635a8641SAndroid Build Coastguard Worker
325*635a8641SAndroid Build Coastguard Worker // Let the thread do extra initialization.
326*635a8641SAndroid Build Coastguard Worker Init();
327*635a8641SAndroid Build Coastguard Worker
328*635a8641SAndroid Build Coastguard Worker {
329*635a8641SAndroid Build Coastguard Worker AutoLock lock(running_lock_);
330*635a8641SAndroid Build Coastguard Worker running_ = true;
331*635a8641SAndroid Build Coastguard Worker }
332*635a8641SAndroid Build Coastguard Worker
333*635a8641SAndroid Build Coastguard Worker start_event_.Signal();
334*635a8641SAndroid Build Coastguard Worker
335*635a8641SAndroid Build Coastguard Worker RunLoop run_loop;
336*635a8641SAndroid Build Coastguard Worker run_loop_ = &run_loop;
337*635a8641SAndroid Build Coastguard Worker Run(run_loop_);
338*635a8641SAndroid Build Coastguard Worker
339*635a8641SAndroid Build Coastguard Worker {
340*635a8641SAndroid Build Coastguard Worker AutoLock lock(running_lock_);
341*635a8641SAndroid Build Coastguard Worker running_ = false;
342*635a8641SAndroid Build Coastguard Worker }
343*635a8641SAndroid Build Coastguard Worker
344*635a8641SAndroid Build Coastguard Worker // Let the thread do extra cleanup.
345*635a8641SAndroid Build Coastguard Worker CleanUp();
346*635a8641SAndroid Build Coastguard Worker
347*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
348*635a8641SAndroid Build Coastguard Worker com_initializer.reset();
349*635a8641SAndroid Build Coastguard Worker #endif
350*635a8641SAndroid Build Coastguard Worker
351*635a8641SAndroid Build Coastguard Worker if (message_loop->type() != MessageLoop::TYPE_CUSTOM) {
352*635a8641SAndroid Build Coastguard Worker // Assert that RunLoop::QuitWhenIdle was called by ThreadQuitHelper. Don't
353*635a8641SAndroid Build Coastguard Worker // check for custom message pumps, because their shutdown might not allow
354*635a8641SAndroid Build Coastguard Worker // this.
355*635a8641SAndroid Build Coastguard Worker DCHECK(GetThreadWasQuitProperly());
356*635a8641SAndroid Build Coastguard Worker }
357*635a8641SAndroid Build Coastguard Worker
358*635a8641SAndroid Build Coastguard Worker // We can't receive messages anymore.
359*635a8641SAndroid Build Coastguard Worker // (The message loop is destructed at the end of this block)
360*635a8641SAndroid Build Coastguard Worker message_loop_ = nullptr;
361*635a8641SAndroid Build Coastguard Worker run_loop_ = nullptr;
362*635a8641SAndroid Build Coastguard Worker }
363*635a8641SAndroid Build Coastguard Worker
ThreadQuitHelper()364*635a8641SAndroid Build Coastguard Worker void Thread::ThreadQuitHelper() {
365*635a8641SAndroid Build Coastguard Worker DCHECK(run_loop_);
366*635a8641SAndroid Build Coastguard Worker run_loop_->QuitWhenIdle();
367*635a8641SAndroid Build Coastguard Worker SetThreadWasQuitProperly(true);
368*635a8641SAndroid Build Coastguard Worker }
369*635a8641SAndroid Build Coastguard Worker
370*635a8641SAndroid Build Coastguard Worker } // namespace base
371