Remove an unused variable in keyboard overlay.
[chromium-blink-merge.git] / remoting / base / auto_thread.cc
blob9f513783bff3e9c304eca079f2eef7af8638b558
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"
7 #include "base/bind.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"
15 #if defined(OS_WIN)
16 #include "base/win/scoped_com_initializer.h"
17 #endif
19 namespace remoting {
21 namespace {
23 #if defined(OS_WIN)
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();
35 #endif
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)
52 : loop_type(type),
53 event(false, false) {}
56 // static
57 scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithType(
58 const char* name,
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);
63 if (!task_runner)
64 delete thread;
65 return task_runner;
68 // static
69 scoped_refptr<AutoThreadTaskRunner> AutoThread::Create(
70 const char* name, scoped_refptr<AutoThreadTaskRunner> joiner) {
71 return CreateWithType(name, joiner, MessageLoop::TYPE_DEFAULT);
74 #if defined(OS_WIN)
75 // static
76 scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithLoopAndComInitTypes(
77 const char* name,
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);
85 if (!task_runner)
86 delete thread;
87 return task_runner;
89 #endif
91 AutoThread::AutoThread(const char* name)
92 : startup_data_(NULL),
93 #if defined(OS_WIN)
94 com_init_type_(COM_INIT_NONE),
95 #endif
96 thread_(0),
97 name_(name),
98 was_quit_properly_(false) {
101 AutoThread::AutoThread(const char* name, AutoThreadTaskRunner* joiner)
102 : startup_data_(NULL),
103 #if defined(OS_WIN)
104 com_init_type_(COM_INIT_NONE),
105 #endif
106 thread_(0),
107 name_(name),
108 was_quit_properly_(false),
109 joiner_(joiner) {
112 AutoThread::~AutoThread() {
113 DCHECK(!startup_data_);
115 // Wait for the thread to exit.
116 if (thread_) {
117 base::PlatformThread::Join(thread_);
121 scoped_refptr<AutoThreadTaskRunner>
122 AutoThread::StartWithType(MessageLoop::Type type) {
123 DCHECK(!thread_);
124 #if defined(OS_WIN)
125 DCHECK(com_init_type_ != COM_INIT_STA || type == MessageLoop::TYPE_UI);
126 #endif
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;
134 return 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;
152 #if defined(OS_WIN)
153 void AutoThread::SetComInitType(ComInitType com_init_type) {
154 DCHECK_EQ(com_init_type_, COM_INIT_NONE);
155 com_init_type_ = com_init_type;
157 #endif
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),
164 task_runner));
165 return;
168 MessageLoop::current()->Quit();
169 was_quit_properly_ = true;
171 if (joiner_) {
172 joiner_->PostTask(FROM_HERE, base::Bind(&AutoThread::JoinAndDeleteThread,
173 base::Unretained(this)));
177 void AutoThread::JoinAndDeleteThread() {
178 delete this;
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
200 // unlocked.
202 #if defined(OS_WIN)
203 // Initialize COM on the thread, if requested.
204 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer(
205 CreateComInitializer(com_init_type_));
206 #endif
208 message_loop.Run();
210 // Assert that MessageLoop::Quit was called by AutoThread::QuitThread.
211 DCHECK(was_quit_properly_);
214 } // namespace base