Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: remoting/base/auto_thread.cc

Issue 11348087: Add AutoThread types for Windows that initialize COM. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/base/auto_thread.h" 5 #include "remoting/base/auto_thread.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
10 #include "base/threading/thread_local.h" 10 #include "base/threading/thread_local.h"
11 #include "base/threading/thread_restrictions.h" 11 #include "base/threading/thread_restrictions.h"
12 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
13 #include "remoting/base/auto_thread_task_runner.h" 13 #include "remoting/base/auto_thread_task_runner.h"
14 14
15 #if defined(OS_WIN)
16 #include "base/win/scoped_com_initializer.h"
17 #endif
18
15 namespace remoting { 19 namespace remoting {
16 20
21 namespace {
22
23 MessageLoop::Type AutoThreadToMessageLoopType(AutoThread::Type type) {
24 switch (type) {
25 case AutoThread::TYPE_DEFAULT: return MessageLoop::TYPE_DEFAULT;
26 case AutoThread::TYPE_IO: return MessageLoop::TYPE_IO;
27 case AutoThread::TYPE_UI: return MessageLoop::TYPE_UI;
28 #if defined(OS_WIN)
29 case AutoThread::TYPE_UI_COM_STA: return MessageLoop::TYPE_UI;
30 case AutoThread::TYPE_UI_COM_MTA: return MessageLoop::TYPE_UI;
31 #endif
32 }
33 NOTREACHED();
34 return MessageLoop::TYPE_DEFAULT;
35 }
36
37 #if defined(OS_WIN)
38 scoped_ptr<base::win::ScopedCOMInitializer> CreateComInitializerForType(
39 AutoThread::Type type) {
40 scoped_ptr<base::win::ScopedCOMInitializer> initializer;
41 if (type == AutoThread::TYPE_UI_COM_STA) {
42 initializer.reset(new base::win::ScopedCOMInitializer());
43 } else if (type == AutoThread::TYPE_UI_COM_MTA) {
44 initializer.reset(new base::win::ScopedCOMInitializer(
45 base::win::ScopedCOMInitializer::kMTA));
46 }
47 return initializer.Pass();
48 }
49 #endif
50
51 }
52
17 // Used to pass data to ThreadMain. This structure is allocated on the stack 53 // Used to pass data to ThreadMain. This structure is allocated on the stack
18 // from within StartWithType. 54 // from within StartWithType.
19 struct AutoThread::StartupData { 55 struct AutoThread::StartupData {
20 MessageLoop::Type loop_type; 56 Type loop_type;
21 57
22 // Used to receive the AutoThreadTaskRunner for the thread. 58 // Used to receive the AutoThreadTaskRunner for the thread.
23 scoped_refptr<AutoThreadTaskRunner> task_runner; 59 scoped_refptr<AutoThreadTaskRunner> task_runner;
24 60
25 // Used to synchronize thread startup. 61 // Used to synchronize thread startup.
26 base::WaitableEvent event; 62 base::WaitableEvent event;
27 63
28 explicit StartupData(MessageLoop::Type type) 64 explicit StartupData(Type type)
29 : loop_type(type), 65 : loop_type(type),
30 event(false, false) {} 66 event(false, false) {}
31 }; 67 };
32 68
33 // static 69 // static
34 scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithType( 70 scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithType(
35 const char* name, 71 const char* name,
36 scoped_refptr<AutoThreadTaskRunner> joiner, 72 scoped_refptr<AutoThreadTaskRunner> joiner,
37 MessageLoop::Type type) { 73 Type type) {
38 AutoThread* thread = new AutoThread(name, joiner); 74 AutoThread* thread = new AutoThread(name, joiner);
39 scoped_refptr<AutoThreadTaskRunner> task_runner = thread->StartWithType(type); 75 scoped_refptr<AutoThreadTaskRunner> task_runner = thread->StartWithType(type);
40 if (task_runner.get()) { 76 if (task_runner.get()) {
41 return task_runner; 77 return task_runner;
42 } else { 78 } else {
43 delete thread; 79 delete thread;
44 return NULL; 80 return NULL;
45 } 81 }
46 } 82 }
47 83
48 // static 84 // static
49 scoped_refptr<AutoThreadTaskRunner> AutoThread::Create( 85 scoped_refptr<AutoThreadTaskRunner> AutoThread::Create(
50 const char* name, scoped_refptr<AutoThreadTaskRunner> joiner) { 86 const char* name, scoped_refptr<AutoThreadTaskRunner> joiner) {
51 return CreateWithType(name, joiner, MessageLoop::TYPE_DEFAULT); 87 return CreateWithType(name, joiner, TYPE_DEFAULT);
52 } 88 }
53 89
54 AutoThread::AutoThread(const char* name) 90 AutoThread::AutoThread(const char* name)
55 : startup_data_(NULL), 91 : startup_data_(NULL),
56 thread_(0), 92 thread_(0),
57 name_(name), 93 name_(name),
58 was_quit_properly_(false) { 94 was_quit_properly_(false) {
59 } 95 }
60 96
61 AutoThread::AutoThread(const char* name, AutoThreadTaskRunner* joiner) 97 AutoThread::AutoThread(const char* name, AutoThreadTaskRunner* joiner)
62 : startup_data_(NULL), 98 : startup_data_(NULL),
63 thread_(0), 99 thread_(0),
64 name_(name), 100 name_(name),
65 was_quit_properly_(false), 101 was_quit_properly_(false),
66 joiner_(joiner) { 102 joiner_(joiner) {
67 } 103 }
68 104
69 AutoThread::~AutoThread() { 105 AutoThread::~AutoThread() {
70 DCHECK(!startup_data_); 106 DCHECK(!startup_data_);
71 107
72 // Wait for the thread to exit. 108 // Wait for the thread to exit.
73 if (thread_) { 109 if (thread_) {
74 base::PlatformThread::Join(thread_); 110 base::PlatformThread::Join(thread_);
75 } 111 }
76 } 112 }
77 113
78 scoped_refptr<AutoThreadTaskRunner> 114 scoped_refptr<AutoThreadTaskRunner>
79 AutoThread::StartWithType(MessageLoop::Type type) { 115 AutoThread::StartWithType(Type type) {
80 DCHECK(!thread_); 116 DCHECK(!thread_);
81 117
82 StartupData startup_data(type); 118 StartupData startup_data(type);
83 startup_data_ = &startup_data; 119 startup_data_ = &startup_data;
84 120
85 if (!base::PlatformThread::Create(0, this, &thread_)) { 121 if (!base::PlatformThread::Create(0, this, &thread_)) {
86 DLOG(ERROR) << "failed to create thread"; 122 DLOG(ERROR) << "failed to create thread";
87 startup_data_ = NULL; 123 startup_data_ = NULL;
88 return NULL; 124 return NULL;
89 } 125 }
90 126
91 // Wait for the thread to start and initialize message_loop_ 127 // Wait for the thread to start and initialize message_loop_
92 // TODO(wez): Since at this point we know the MessageLoop _will_ run, and 128 // TODO(wez): Since at this point we know the MessageLoop _will_ run, and
93 // the thread lifetime is controlled by the AutoThreadTaskRunner, we would 129 // the thread lifetime is controlled by the AutoThreadTaskRunner, we would
94 // ideally return the AutoThreadTaskRunner to the caller without waiting for 130 // ideally return the AutoThreadTaskRunner to the caller without waiting for
95 // the thread to signal us. 131 // the thread to signal us.
96 base::ThreadRestrictions::ScopedAllowWait allow_wait; 132 base::ThreadRestrictions::ScopedAllowWait allow_wait;
97 startup_data.event.Wait(); 133 startup_data.event.Wait();
98 134
99 // set it to NULL so we don't keep a pointer to some object on the stack. 135 // set it to NULL so we don't keep a pointer to some object on the stack.
100 startup_data_ = NULL; 136 startup_data_ = NULL;
101 137
102 DCHECK(startup_data.task_runner.get()); 138 DCHECK(startup_data.task_runner.get());
103 return startup_data.task_runner; 139 return startup_data.task_runner;
104 } 140 }
105 141
106 scoped_refptr<AutoThreadTaskRunner> AutoThread::Start() { 142 scoped_refptr<AutoThreadTaskRunner> AutoThread::Start() {
107 return StartWithType(MessageLoop::TYPE_DEFAULT); 143 return StartWithType(TYPE_DEFAULT);
108 } 144 }
109 145
110 void AutoThread::QuitThread( 146 void AutoThread::QuitThread(
111 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 147 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
112 if (!task_runner->BelongsToCurrentThread()) { 148 if (!task_runner->BelongsToCurrentThread()) {
113 task_runner->PostTask(FROM_HERE, base::Bind(&AutoThread::QuitThread, 149 task_runner->PostTask(FROM_HERE, base::Bind(&AutoThread::QuitThread,
114 base::Unretained(this), 150 base::Unretained(this),
115 task_runner)); 151 task_runner));
116 return; 152 return;
117 } 153 }
118 154
119 MessageLoop::current()->Quit(); 155 MessageLoop::current()->Quit();
120 was_quit_properly_ = true; 156 was_quit_properly_ = true;
121 157
122 if (joiner_) { 158 if (joiner_) {
123 joiner_->PostTask(FROM_HERE, base::Bind(&AutoThread::JoinAndDeleteThread, 159 joiner_->PostTask(FROM_HERE, base::Bind(&AutoThread::JoinAndDeleteThread,
124 base::Unretained(this))); 160 base::Unretained(this)));
125 } 161 }
126 } 162 }
127 163
128 void AutoThread::JoinAndDeleteThread() { 164 void AutoThread::JoinAndDeleteThread() {
129 delete this; 165 delete this;
130 } 166 }
131 167
132 void AutoThread::ThreadMain() { 168 void AutoThread::ThreadMain() {
133 // The message loop for this thread. 169 // The message loop for this thread.
134 MessageLoop message_loop(startup_data_->loop_type); 170 MessageLoop message_loop(
171 AutoThreadToMessageLoopType(startup_data_->loop_type));
135 172
136 // Complete the initialization of our AutoThread object. 173 // Complete the initialization of our AutoThread object.
137 base::PlatformThread::SetName(name_.c_str()); 174 base::PlatformThread::SetName(name_.c_str());
138 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. 175 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector.
139 message_loop.set_thread_name(name_); 176 message_loop.set_thread_name(name_);
140 177
141 // Return an AutoThreadTaskRunner that will cleanly quit this thread when 178 // Return an AutoThreadTaskRunner that will cleanly quit this thread when
142 // no more references to it remain. 179 // no more references to it remain.
143 startup_data_->task_runner = 180 startup_data_->task_runner =
144 new AutoThreadTaskRunner(message_loop.message_loop_proxy(), 181 new AutoThreadTaskRunner(message_loop.message_loop_proxy(),
145 base::Bind(&AutoThread::QuitThread, 182 base::Bind(&AutoThread::QuitThread,
146 base::Unretained(this), 183 base::Unretained(this),
147 message_loop.message_loop_proxy())); 184 message_loop.message_loop_proxy()));
148 185
149 startup_data_->event.Signal(); 186 startup_data_->event.Signal();
150 // startup_data_ can't be touched anymore since the starting thread is now 187 // startup_data_ can't be touched anymore since the starting thread is now
151 // unlocked. 188 // unlocked.
152 189
190 #if defined(OS_WIN)
191 // Initialize COM on the thread, if requested.
192 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer(
193 CreateComInitializerForType(startup_data_->loop_type));
194 #endif
195
153 message_loop.Run(); 196 message_loop.Run();
154 197
155 // Assert that MessageLoop::Quit was called by AutoThread::QuitThread. 198 // Assert that MessageLoop::Quit was called by AutoThread::QuitThread.
156 DCHECK(was_quit_properly_); 199 DCHECK(was_quit_properly_);
157 } 200 }
158 201
159 } // namespace base 202 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698