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

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: Add missing OS checks. Created 8 years 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
« no previous file with comments | « remoting/base/auto_thread.h ('k') | remoting/base/auto_thread_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #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());
32 }
33 return initializer.Pass();
34 }
35 #endif
36
37 }
38
17 // Used to pass data to ThreadMain. This structure is allocated on the stack 39 // Used to pass data to ThreadMain. This structure is allocated on the stack
18 // from within StartWithType. 40 // from within StartWithType.
19 struct AutoThread::StartupData { 41 struct AutoThread::StartupData {
42 // Fields describing the desired thread behaviour.
20 MessageLoop::Type loop_type; 43 MessageLoop::Type loop_type;
21 44
22 // Used to receive the AutoThreadTaskRunner for the thread. 45 // Used to receive the AutoThreadTaskRunner for the thread.
23 scoped_refptr<AutoThreadTaskRunner> task_runner; 46 scoped_refptr<AutoThreadTaskRunner> task_runner;
24 47
25 // Used to synchronize thread startup. 48 // Used to synchronize thread startup.
26 base::WaitableEvent event; 49 base::WaitableEvent event;
27 50
28 explicit StartupData(MessageLoop::Type type) 51 explicit StartupData(MessageLoop::Type type)
29 : loop_type(type), 52 : loop_type(type),
30 event(false, false) {} 53 event(false, false) {}
31 }; 54 };
32 55
33 // static 56 // static
34 scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithType( 57 scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithType(
35 const char* name, 58 const char* name,
36 scoped_refptr<AutoThreadTaskRunner> joiner, 59 scoped_refptr<AutoThreadTaskRunner> joiner,
37 MessageLoop::Type type) { 60 MessageLoop::Type type) {
38 AutoThread* thread = new AutoThread(name, joiner); 61 AutoThread* thread = new AutoThread(name, joiner);
39 scoped_refptr<AutoThreadTaskRunner> task_runner = thread->StartWithType(type); 62 scoped_refptr<AutoThreadTaskRunner> task_runner = thread->StartWithType(type);
40 if (task_runner.get()) { 63 if (!task_runner)
41 return task_runner;
42 } else {
43 delete thread; 64 delete thread;
44 return NULL; 65 return task_runner;
45 }
46 } 66 }
47 67
48 // static 68 // static
49 scoped_refptr<AutoThreadTaskRunner> AutoThread::Create( 69 scoped_refptr<AutoThreadTaskRunner> AutoThread::Create(
50 const char* name, scoped_refptr<AutoThreadTaskRunner> joiner) { 70 const char* name, scoped_refptr<AutoThreadTaskRunner> joiner) {
51 return CreateWithType(name, joiner, MessageLoop::TYPE_DEFAULT); 71 return CreateWithType(name, joiner, MessageLoop::TYPE_DEFAULT);
52 } 72 }
53 73
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;
88 }
89 #endif
90
54 AutoThread::AutoThread(const char* name) 91 AutoThread::AutoThread(const char* name)
55 : startup_data_(NULL), 92 : startup_data_(NULL),
93 #if defined(OS_WIN)
94 com_init_type_(COM_INIT_NONE),
95 #endif
56 thread_(0), 96 thread_(0),
57 name_(name), 97 name_(name),
58 was_quit_properly_(false) { 98 was_quit_properly_(false) {
59 } 99 }
60 100
61 AutoThread::AutoThread(const char* name, AutoThreadTaskRunner* joiner) 101 AutoThread::AutoThread(const char* name, AutoThreadTaskRunner* joiner)
62 : startup_data_(NULL), 102 : startup_data_(NULL),
103 #if defined(OS_WIN)
104 com_init_type_(COM_INIT_NONE),
105 #endif
63 thread_(0), 106 thread_(0),
64 name_(name), 107 name_(name),
65 was_quit_properly_(false), 108 was_quit_properly_(false),
66 joiner_(joiner) { 109 joiner_(joiner) {
67 } 110 }
68 111
69 AutoThread::~AutoThread() { 112 AutoThread::~AutoThread() {
70 DCHECK(!startup_data_); 113 DCHECK(!startup_data_);
71 114
72 // Wait for the thread to exit. 115 // Wait for the thread to exit.
73 if (thread_) { 116 if (thread_) {
74 base::PlatformThread::Join(thread_); 117 base::PlatformThread::Join(thread_);
75 } 118 }
76 } 119 }
77 120
78 scoped_refptr<AutoThreadTaskRunner> 121 scoped_refptr<AutoThreadTaskRunner>
79 AutoThread::StartWithType(MessageLoop::Type type) { 122 AutoThread::StartWithType(MessageLoop::Type type) {
80 DCHECK(!thread_); 123 DCHECK(!thread_);
124 #if defined(OS_WIN)
125 DCHECK(com_init_type_ != COM_INIT_STA || type == MessageLoop::TYPE_UI);
126 #endif
81 127
82 StartupData startup_data(type); 128 StartupData startup_data(type);
83 startup_data_ = &startup_data; 129 startup_data_ = &startup_data;
84 130
85 if (!base::PlatformThread::Create(0, this, &thread_)) { 131 if (!base::PlatformThread::Create(0, this, &thread_)) {
86 DLOG(ERROR) << "failed to create thread"; 132 DLOG(ERROR) << "failed to create thread";
87 startup_data_ = NULL; 133 startup_data_ = NULL;
88 return NULL; 134 return NULL;
89 } 135 }
90 136
91 // Wait for the thread to start and initialize message_loop_ 137 // Wait for the thread to start and initialize message_loop_
92 // TODO(wez): Since at this point we know the MessageLoop _will_ run, and 138 // TODO(wez): Since at this point we know the MessageLoop _will_ run, and
93 // the thread lifetime is controlled by the AutoThreadTaskRunner, we would 139 // the thread lifetime is controlled by the AutoThreadTaskRunner, we would
94 // ideally return the AutoThreadTaskRunner to the caller without waiting for 140 // ideally return the AutoThreadTaskRunner to the caller without waiting for
95 // the thread to signal us. 141 // the thread to signal us.
96 base::ThreadRestrictions::ScopedAllowWait allow_wait; 142 base::ThreadRestrictions::ScopedAllowWait allow_wait;
97 startup_data.event.Wait(); 143 startup_data.event.Wait();
98 144
99 // set it to NULL so we don't keep a pointer to some object on the stack. 145 // set it to NULL so we don't keep a pointer to some object on the stack.
100 startup_data_ = NULL; 146 startup_data_ = NULL;
101 147
102 DCHECK(startup_data.task_runner.get()); 148 DCHECK(startup_data.task_runner.get());
103 return startup_data.task_runner; 149 return startup_data.task_runner;
104 } 150 }
105 151
106 scoped_refptr<AutoThreadTaskRunner> AutoThread::Start() { 152 #if defined(OS_WIN)
107 return StartWithType(MessageLoop::TYPE_DEFAULT); 153 void AutoThread::SetComInitType(ComInitType com_init_type) {
154 DCHECK_EQ(com_init_type_, COM_INIT_NONE);
155 com_init_type_ = com_init_type;
108 } 156 }
157 #endif
109 158
110 void AutoThread::QuitThread( 159 void AutoThread::QuitThread(
111 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 160 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
112 if (!task_runner->BelongsToCurrentThread()) { 161 if (!task_runner->BelongsToCurrentThread()) {
113 task_runner->PostTask(FROM_HERE, base::Bind(&AutoThread::QuitThread, 162 task_runner->PostTask(FROM_HERE, base::Bind(&AutoThread::QuitThread,
114 base::Unretained(this), 163 base::Unretained(this),
115 task_runner)); 164 task_runner));
116 return; 165 return;
117 } 166 }
118 167
(...skipping 24 matching lines...) Expand all
143 startup_data_->task_runner = 192 startup_data_->task_runner =
144 new AutoThreadTaskRunner(message_loop.message_loop_proxy(), 193 new AutoThreadTaskRunner(message_loop.message_loop_proxy(),
145 base::Bind(&AutoThread::QuitThread, 194 base::Bind(&AutoThread::QuitThread,
146 base::Unretained(this), 195 base::Unretained(this),
147 message_loop.message_loop_proxy())); 196 message_loop.message_loop_proxy()));
148 197
149 startup_data_->event.Signal(); 198 startup_data_->event.Signal();
150 // startup_data_ can't be touched anymore since the starting thread is now 199 // startup_data_ can't be touched anymore since the starting thread is now
151 // unlocked. 200 // unlocked.
152 201
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
207
153 message_loop.Run(); 208 message_loop.Run();
154 209
155 // Assert that MessageLoop::Quit was called by AutoThread::QuitThread. 210 // Assert that MessageLoop::Quit was called by AutoThread::QuitThread.
156 DCHECK(was_quit_properly_); 211 DCHECK(was_quit_properly_);
157 } 212 }
158 213
159 } // namespace base 214 } // namespace base
OLDNEW
« no previous file with comments | « remoting/base/auto_thread.h ('k') | remoting/base/auto_thread_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698