Chromium Code Reviews| OLD | NEW |
|---|---|
| 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> CreateComInitializerWithType( | |
|
alexeypa (please no reviews)
2012/11/26 19:16:13
nit: CreateComInitializer()
Wez
2012/11/27 02:22:27
Done.
| |
| 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; |
| 44 #if defined(OS_WIN) | |
| 45 ComInitType com_init_type; | |
| 46 #endif | |
|
Wez
2012/11/22 21:57:15
note-to-self: Remove this.
Wez
2012/11/27 02:22:27
Done.
| |
| 21 | 47 |
| 22 // Used to receive the AutoThreadTaskRunner for the thread. | 48 // Used to receive the AutoThreadTaskRunner for the thread. |
| 23 scoped_refptr<AutoThreadTaskRunner> task_runner; | 49 scoped_refptr<AutoThreadTaskRunner> task_runner; |
| 24 | 50 |
| 25 // Used to synchronize thread startup. | 51 // Used to synchronize thread startup. |
| 26 base::WaitableEvent event; | 52 base::WaitableEvent event; |
| 27 | 53 |
| 28 explicit StartupData(MessageLoop::Type type) | 54 explicit StartupData(MessageLoop::Type type) |
| 29 : loop_type(type), | 55 : loop_type(type), |
| 30 event(false, false) {} | 56 event(false, false) {} |
| 31 }; | 57 }; |
| 32 | 58 |
| 33 // static | 59 // static |
| 34 scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithType( | 60 scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithType( |
| 35 const char* name, | 61 const char* name, |
| 36 scoped_refptr<AutoThreadTaskRunner> joiner, | 62 scoped_refptr<AutoThreadTaskRunner> joiner, |
| 37 MessageLoop::Type type) { | 63 MessageLoop::Type type) { |
| 38 AutoThread* thread = new AutoThread(name, joiner); | 64 AutoThread* thread = new AutoThread(name, joiner); |
| 39 scoped_refptr<AutoThreadTaskRunner> task_runner = thread->StartWithType(type); | 65 scoped_refptr<AutoThreadTaskRunner> task_runner = thread->StartWithType(type); |
| 40 if (task_runner.get()) { | 66 if (!task_runner) |
| 41 return task_runner; | |
| 42 } else { | |
| 43 delete thread; | 67 delete thread; |
| 44 return NULL; | 68 return task_runner; |
| 45 } | |
| 46 } | 69 } |
| 47 | 70 |
| 48 // static | 71 // static |
| 49 scoped_refptr<AutoThreadTaskRunner> AutoThread::Create( | 72 scoped_refptr<AutoThreadTaskRunner> AutoThread::Create( |
| 50 const char* name, scoped_refptr<AutoThreadTaskRunner> joiner) { | 73 const char* name, scoped_refptr<AutoThreadTaskRunner> joiner) { |
| 51 return CreateWithType(name, joiner, MessageLoop::TYPE_DEFAULT); | 74 return CreateWithType(name, joiner, MessageLoop::TYPE_DEFAULT); |
| 52 } | 75 } |
| 53 | 76 |
| 77 #if defined(OS_WIN) | |
| 78 // static | |
| 79 scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithLoopAndComInitTypes( | |
| 80 const char* name, | |
| 81 scoped_refptr<AutoThreadTaskRunner> joiner, | |
| 82 MessageLoop::Type loop_type, | |
| 83 ComInitType com_init_type) { | |
| 84 AutoThread* thread = new AutoThread(name, joiner); | |
| 85 thread->SetComInitType(com_init_type); | |
| 86 scoped_refptr<AutoThreadTaskRunner> task_runner = | |
| 87 thread->StartWithType(loop_type); | |
| 88 if (!task_runner) | |
| 89 delete thread; | |
| 90 return task_runner; | |
| 91 } | |
| 92 #endif | |
| 93 | |
| 54 AutoThread::AutoThread(const char* name) | 94 AutoThread::AutoThread(const char* name) |
| 55 : startup_data_(NULL), | 95 : startup_data_(NULL), |
|
alexeypa (please no reviews)
2012/11/26 19:16:13
Initialize |com_init_type_| here.
Wez
2012/11/27 02:22:27
Done.
| |
| 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) |
|
alexeypa (please no reviews)
2012/11/26 19:16:13
Initialize |com_init_type_| here.
Wez
2012/11/27 02:22:27
Done.
| |
| 62 : startup_data_(NULL), | 102 : startup_data_(NULL), |
| 63 thread_(0), | 103 thread_(0), |
| 64 name_(name), | 104 name_(name), |
| 65 was_quit_properly_(false), | 105 was_quit_properly_(false), |
| 66 joiner_(joiner) { | 106 joiner_(joiner) { |
| 67 } | 107 } |
| 68 | 108 |
| 69 AutoThread::~AutoThread() { | 109 AutoThread::~AutoThread() { |
| 70 DCHECK(!startup_data_); | 110 DCHECK(!startup_data_); |
| 71 | 111 |
| 72 // Wait for the thread to exit. | 112 // Wait for the thread to exit. |
| 73 if (thread_) { | 113 if (thread_) { |
| 74 base::PlatformThread::Join(thread_); | 114 base::PlatformThread::Join(thread_); |
| 75 } | 115 } |
| 76 } | 116 } |
| 77 | 117 |
| 78 scoped_refptr<AutoThreadTaskRunner> | 118 scoped_refptr<AutoThreadTaskRunner> |
| 79 AutoThread::StartWithType(MessageLoop::Type type) { | 119 AutoThread::StartWithType(MessageLoop::Type type) { |
| 80 DCHECK(!thread_); | 120 DCHECK(!thread_); |
| 121 #if defined(OS_WIN) | |
| 122 DCHECK(com_init_type_ != COM_INIT_STA || type == MessageLoop::TYPE_UI); | |
| 123 #endif | |
| 81 | 124 |
| 82 StartupData startup_data(type); | 125 StartupData startup_data(type); |
| 83 startup_data_ = &startup_data; | 126 startup_data_ = &startup_data; |
| 84 | 127 |
| 85 if (!base::PlatformThread::Create(0, this, &thread_)) { | 128 if (!base::PlatformThread::Create(0, this, &thread_)) { |
| 86 DLOG(ERROR) << "failed to create thread"; | 129 DLOG(ERROR) << "failed to create thread"; |
| 87 startup_data_ = NULL; | 130 startup_data_ = NULL; |
| 88 return NULL; | 131 return NULL; |
| 89 } | 132 } |
| 90 | 133 |
| 91 // Wait for the thread to start and initialize message_loop_ | 134 // Wait for the thread to start and initialize message_loop_ |
| 92 // TODO(wez): Since at this point we know the MessageLoop _will_ run, and | 135 // TODO(wez): Since at this point we know the MessageLoop _will_ run, and |
| 93 // the thread lifetime is controlled by the AutoThreadTaskRunner, we would | 136 // the thread lifetime is controlled by the AutoThreadTaskRunner, we would |
| 94 // ideally return the AutoThreadTaskRunner to the caller without waiting for | 137 // ideally return the AutoThreadTaskRunner to the caller without waiting for |
| 95 // the thread to signal us. | 138 // the thread to signal us. |
| 96 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 139 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 97 startup_data.event.Wait(); | 140 startup_data.event.Wait(); |
| 98 | 141 |
| 99 // set it to NULL so we don't keep a pointer to some object on the stack. | 142 // set it to NULL so we don't keep a pointer to some object on the stack. |
| 100 startup_data_ = NULL; | 143 startup_data_ = NULL; |
| 101 | 144 |
| 102 DCHECK(startup_data.task_runner.get()); | 145 DCHECK(startup_data.task_runner.get()); |
| 103 return startup_data.task_runner; | 146 return startup_data.task_runner; |
| 104 } | 147 } |
| 105 | 148 |
| 106 scoped_refptr<AutoThreadTaskRunner> AutoThread::Start() { | 149 #if defined(OS_WIN) |
| 107 return StartWithType(MessageLoop::TYPE_DEFAULT); | 150 void AutoThread::SetComInitType(ComInitType com_init_type) { |
| 151 com_init_type_ = com_init_type; | |
|
alexeypa (please no reviews)
2012/11/26 19:16:13
nit: Add DCHECK_EQ(com_init_type_, COM_INIT_NONE)
Wez
2012/11/27 02:22:27
Done.
| |
| 108 } | 152 } |
| 153 #endif | |
| 109 | 154 |
| 110 void AutoThread::QuitThread( | 155 void AutoThread::QuitThread( |
| 111 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 156 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 112 if (!task_runner->BelongsToCurrentThread()) { | 157 if (!task_runner->BelongsToCurrentThread()) { |
| 113 task_runner->PostTask(FROM_HERE, base::Bind(&AutoThread::QuitThread, | 158 task_runner->PostTask(FROM_HERE, base::Bind(&AutoThread::QuitThread, |
| 114 base::Unretained(this), | 159 base::Unretained(this), |
| 115 task_runner)); | 160 task_runner)); |
| 116 return; | 161 return; |
| 117 } | 162 } |
| 118 | 163 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 143 startup_data_->task_runner = | 188 startup_data_->task_runner = |
| 144 new AutoThreadTaskRunner(message_loop.message_loop_proxy(), | 189 new AutoThreadTaskRunner(message_loop.message_loop_proxy(), |
| 145 base::Bind(&AutoThread::QuitThread, | 190 base::Bind(&AutoThread::QuitThread, |
| 146 base::Unretained(this), | 191 base::Unretained(this), |
| 147 message_loop.message_loop_proxy())); | 192 message_loop.message_loop_proxy())); |
| 148 | 193 |
| 149 startup_data_->event.Signal(); | 194 startup_data_->event.Signal(); |
| 150 // startup_data_ can't be touched anymore since the starting thread is now | 195 // startup_data_ can't be touched anymore since the starting thread is now |
| 151 // unlocked. | 196 // unlocked. |
| 152 | 197 |
| 198 #if defined(OS_WIN) | |
| 199 // Initialize COM on the thread, if requested. | |
| 200 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer( | |
| 201 CreateComInitializerWithType(com_init_type_)); | |
| 202 #endif | |
| 203 | |
| 153 message_loop.Run(); | 204 message_loop.Run(); |
| 154 | 205 |
| 155 // Assert that MessageLoop::Quit was called by AutoThread::QuitThread. | 206 // Assert that MessageLoop::Quit was called by AutoThread::QuitThread. |
| 156 DCHECK(was_quit_properly_); | 207 DCHECK(was_quit_properly_); |
| 157 } | 208 } |
| 158 | 209 |
| 159 } // namespace base | 210 } // namespace base |
| OLD | NEW |