1 // Copyright (c) 2012 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 "remoting/base/auto_thread.h"
8 #include "base/lazy_instance.h"
9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
10 #include "base/threading/thread_local.h"
11 #include "base/threading/thread_restrictions.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "remoting/base/auto_thread_task_runner.h"
16 #include "base/win/scoped_com_initializer.h"
24 scoped_ptr
<base::win::ScopedCOMInitializer
> CreateComInitializer(
25 AutoThread::ComInitType type
) {
26 scoped_ptr
<base::win::ScopedCOMInitializer
> initializer
;
27 if (type
== AutoThread::COM_INIT_MTA
) {
28 initializer
.reset(new base::win::ScopedCOMInitializer(
29 base::win::ScopedCOMInitializer::kMTA
));
30 } else if (type
== AutoThread::COM_INIT_STA
) {
31 initializer
.reset(new base::win::ScopedCOMInitializer());
33 return initializer
.Pass();
39 // Used to pass data to ThreadMain. This structure is allocated on the stack
40 // from within StartWithType.
41 struct AutoThread::StartupData
{
42 // Fields describing the desired thread behaviour.
43 MessageLoop::Type loop_type
;
45 // Used to receive the AutoThreadTaskRunner for the thread.
46 scoped_refptr
<AutoThreadTaskRunner
> task_runner
;
48 // Used to synchronize thread startup.
49 base::WaitableEvent event
;
51 explicit StartupData(MessageLoop::Type type
)
53 event(false, false) {}
57 scoped_refptr
<AutoThreadTaskRunner
> AutoThread::CreateWithType(
59 scoped_refptr
<AutoThreadTaskRunner
> joiner
,
60 MessageLoop::Type type
) {
61 AutoThread
* thread
= new AutoThread(name
, joiner
);
62 scoped_refptr
<AutoThreadTaskRunner
> task_runner
= thread
->StartWithType(type
);
69 scoped_refptr
<AutoThreadTaskRunner
> AutoThread::Create(
70 const char* name
, scoped_refptr
<AutoThreadTaskRunner
> joiner
) {
71 return CreateWithType(name
, joiner
, MessageLoop::TYPE_DEFAULT
);
76 scoped_refptr
<AutoThreadTaskRunner
> AutoThread::CreateWithLoopAndComInitTypes(
78 scoped_refptr
<AutoThreadTaskRunner
> joiner
,
79 MessageLoop::Type loop_type
,
80 ComInitType com_init_type
) {
81 AutoThread
* thread
= new AutoThread(name
, joiner
);
82 thread
->SetComInitType(com_init_type
);
83 scoped_refptr
<AutoThreadTaskRunner
> task_runner
=
84 thread
->StartWithType(loop_type
);
91 AutoThread::AutoThread(const char* name
)
92 : startup_data_(NULL
),
94 com_init_type_(COM_INIT_NONE
),
98 was_quit_properly_(false) {
101 AutoThread::AutoThread(const char* name
, AutoThreadTaskRunner
* joiner
)
102 : startup_data_(NULL
),
104 com_init_type_(COM_INIT_NONE
),
108 was_quit_properly_(false),
112 AutoThread::~AutoThread() {
113 DCHECK(!startup_data_
);
115 // Wait for the thread to exit.
117 base::PlatformThread::Join(thread_
);
121 scoped_refptr
<AutoThreadTaskRunner
>
122 AutoThread::StartWithType(MessageLoop::Type type
) {
125 DCHECK(com_init_type_
!= COM_INIT_STA
|| type
== MessageLoop::TYPE_UI
);
128 StartupData
startup_data(type
);
129 startup_data_
= &startup_data
;
131 if (!base::PlatformThread::Create(0, this, &thread_
)) {
132 DLOG(ERROR
) << "failed to create thread";
133 startup_data_
= NULL
;
137 // Wait for the thread to start and initialize message_loop_
138 // TODO(wez): Since at this point we know the MessageLoop _will_ run, and
139 // the thread lifetime is controlled by the AutoThreadTaskRunner, we would
140 // ideally return the AutoThreadTaskRunner to the caller without waiting for
141 // the thread to signal us.
142 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
143 startup_data
.event
.Wait();
145 // set it to NULL so we don't keep a pointer to some object on the stack.
146 startup_data_
= NULL
;
148 DCHECK(startup_data
.task_runner
.get());
149 return startup_data
.task_runner
;
153 void AutoThread::SetComInitType(ComInitType com_init_type
) {
154 DCHECK_EQ(com_init_type_
, COM_INIT_NONE
);
155 com_init_type_
= com_init_type
;
159 void AutoThread::QuitThread(
160 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
) {
161 if (!task_runner
->BelongsToCurrentThread()) {
162 task_runner
->PostTask(FROM_HERE
, base::Bind(&AutoThread::QuitThread
,
163 base::Unretained(this),
168 MessageLoop::current()->Quit();
169 was_quit_properly_
= true;
172 joiner_
->PostTask(FROM_HERE
, base::Bind(&AutoThread::JoinAndDeleteThread
,
173 base::Unretained(this)));
177 void AutoThread::JoinAndDeleteThread() {
181 void AutoThread::ThreadMain() {
182 // The message loop for this thread.
183 MessageLoop
message_loop(startup_data_
->loop_type
);
185 // Complete the initialization of our AutoThread object.
186 base::PlatformThread::SetName(name_
.c_str());
187 ANNOTATE_THREAD_NAME(name_
.c_str()); // Tell the name to race detector.
188 message_loop
.set_thread_name(name_
);
190 // Return an AutoThreadTaskRunner that will cleanly quit this thread when
191 // no more references to it remain.
192 startup_data_
->task_runner
=
193 new AutoThreadTaskRunner(message_loop
.message_loop_proxy(),
194 base::Bind(&AutoThread::QuitThread
,
195 base::Unretained(this),
196 message_loop
.message_loop_proxy()));
198 startup_data_
->event
.Signal();
199 // startup_data_ can't be touched anymore since the starting thread is now
203 // Initialize COM on the thread, if requested.
204 scoped_ptr
<base::win::ScopedCOMInitializer
> com_initializer(
205 CreateComInitializer(com_init_type_
));
210 // Assert that MessageLoop::Quit was called by AutoThread::QuitThread.
211 DCHECK(was_quit_properly_
);