blob: 5278c9321eae369167fb5717d5be84c6caa16021 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/base/thread.h"
12
13#ifndef __has_feature
14#define __has_feature(x) 0 // Compatibility with non-clang or LLVM compilers.
15#endif // __has_feature
16
17#if defined(WEBRTC_WIN)
18#include <comdef.h>
19#elif defined(WEBRTC_POSIX)
20#include <time.h>
21#endif
22
23#include "webrtc/base/common.h"
24#include "webrtc/base/logging.h"
danilchapbebf54c2016-04-28 01:32:48 -070025#include "webrtc/base/nullsocketserver.h"
Tommiea14f0a2015-05-18 13:51:06 +020026#include "webrtc/base/platform_thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027#include "webrtc/base/stringutils.h"
28#include "webrtc/base/timeutils.h"
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070029#include "webrtc/base/trace_event.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030
31#if !__has_feature(objc_arc) && (defined(WEBRTC_MAC))
32#include "webrtc/base/maccocoathreadhelper.h"
33#include "webrtc/base/scoped_autorelease_pool.h"
34#endif
35
36namespace rtc {
37
38ThreadManager* ThreadManager::Instance() {
Andrew MacDonald469c2c02015-05-22 17:50:26 -070039 RTC_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 return &thread_manager;
41}
42
43// static
44Thread* Thread::Current() {
45 return ThreadManager::Instance()->CurrentThread();
46}
47
48#if defined(WEBRTC_POSIX)
49ThreadManager::ThreadManager() {
50 pthread_key_create(&key_, NULL);
51#ifndef NO_MAIN_THREAD_WRAPPING
52 WrapCurrentThread();
53#endif
54#if !__has_feature(objc_arc) && (defined(WEBRTC_MAC))
55 // Under Automatic Reference Counting (ARC), you cannot use autorelease pools
56 // directly. Instead, you use @autoreleasepool blocks instead. Also, we are
57 // maintaining thread safety using immutability within context of GCD dispatch
58 // queues in this case.
59 InitCocoaMultiThreading();
60#endif
61}
62
63ThreadManager::~ThreadManager() {
64#if __has_feature(objc_arc)
65 @autoreleasepool
66#elif defined(WEBRTC_MAC)
67 // This is called during exit, at which point apparently no NSAutoreleasePools
68 // are available; but we might still need them to do cleanup (or we get the
69 // "no autoreleasepool in place, just leaking" warning when exiting).
70 ScopedAutoreleasePool pool;
71#endif
72 {
73 UnwrapCurrentThread();
74 pthread_key_delete(key_);
75 }
76}
77
78Thread *ThreadManager::CurrentThread() {
79 return static_cast<Thread *>(pthread_getspecific(key_));
80}
81
82void ThreadManager::SetCurrentThread(Thread *thread) {
83 pthread_setspecific(key_, thread);
84}
85#endif
86
87#if defined(WEBRTC_WIN)
88ThreadManager::ThreadManager() {
89 key_ = TlsAlloc();
90#ifndef NO_MAIN_THREAD_WRAPPING
91 WrapCurrentThread();
92#endif
93}
94
95ThreadManager::~ThreadManager() {
96 UnwrapCurrentThread();
97 TlsFree(key_);
98}
99
100Thread *ThreadManager::CurrentThread() {
101 return static_cast<Thread *>(TlsGetValue(key_));
102}
103
104void ThreadManager::SetCurrentThread(Thread *thread) {
105 TlsSetValue(key_, thread);
106}
107#endif
108
109Thread *ThreadManager::WrapCurrentThread() {
110 Thread* result = CurrentThread();
111 if (NULL == result) {
112 result = new Thread();
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000113 result->WrapCurrentWithThreadManager(this, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 }
115 return result;
116}
117
118void ThreadManager::UnwrapCurrentThread() {
119 Thread* t = CurrentThread();
120 if (t && !(t->IsOwned())) {
121 t->UnwrapCurrent();
122 delete t;
123 }
124}
125
126struct ThreadInit {
127 Thread* thread;
128 Runnable* runnable;
129};
130
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000131Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls()
132 : thread_(Thread::Current()),
133 previous_state_(thread_->SetAllowBlockingCalls(false)) {
134}
135
136Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() {
137 ASSERT(thread_->IsCurrent());
138 thread_->SetAllowBlockingCalls(previous_state_);
139}
140
danilchapbebf54c2016-04-28 01:32:48 -0700141Thread::Thread() : Thread(SocketServer::CreateDefault()) {}
142
143Thread::Thread(SocketServer* ss)
jbauch25d1f282016-02-05 00:25:02 -0800144 : MessageQueue(ss, false),
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000145 running_(true, false),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146#if defined(WEBRTC_WIN)
147 thread_(NULL),
148 thread_id_(0),
149#endif
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000150 owned_(true),
151 blocking_calls_allowed_(true) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 SetName("Thread", this); // default name
danilchapbebf54c2016-04-28 01:32:48 -0700153 DoInit();
154}
155
156Thread::Thread(std::unique_ptr<SocketServer> ss)
157 : MessageQueue(std::move(ss), false),
158 running_(true, false),
159#if defined(WEBRTC_WIN)
160 thread_(NULL),
161 thread_id_(0),
162#endif
163 owned_(true),
164 blocking_calls_allowed_(true) {
165 SetName("Thread", this); // default name
166 DoInit();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167}
168
169Thread::~Thread() {
170 Stop();
jbauch25d1f282016-02-05 00:25:02 -0800171 DoDestroy();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172}
173
danilchapbebf54c2016-04-28 01:32:48 -0700174std::unique_ptr<Thread> Thread::CreateWithSocketServer() {
175 return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault()));
176}
177
178std::unique_ptr<Thread> Thread::Create() {
179 return std::unique_ptr<Thread>(
180 new Thread(std::unique_ptr<SocketServer>(new NullSocketServer())));
181}
182
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183bool Thread::SleepMs(int milliseconds) {
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000184 AssertBlockingIsAllowedOnCurrentThread();
185
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000186#if defined(WEBRTC_WIN)
187 ::Sleep(milliseconds);
188 return true;
189#else
190 // POSIX has both a usleep() and a nanosleep(), but the former is deprecated,
191 // so we use nanosleep() even though it has greater precision than necessary.
192 struct timespec ts;
193 ts.tv_sec = milliseconds / 1000;
194 ts.tv_nsec = (milliseconds % 1000) * 1000000;
195 int ret = nanosleep(&ts, NULL);
196 if (ret != 0) {
197 LOG_ERR(LS_WARNING) << "nanosleep() returning early";
198 return false;
199 }
200 return true;
201#endif
202}
203
204bool Thread::SetName(const std::string& name, const void* obj) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000205 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000206 name_ = name;
207 if (obj) {
208 char buf[16];
209 sprintfn(buf, sizeof(buf), " 0x%p", obj);
210 name_ += buf;
211 }
212 return true;
213}
214
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000215bool Thread::Start(Runnable* runnable) {
216 ASSERT(owned_);
217 if (!owned_) return false;
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000218 ASSERT(!running());
219 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220
221 Restart(); // reset fStop_ if the thread is being restarted
222
223 // Make sure that ThreadManager is created on the main thread before
224 // we start a new thread.
225 ThreadManager::Instance();
226
227 ThreadInit* init = new ThreadInit;
228 init->thread = this;
229 init->runnable = runnable;
230#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100231 thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PreRun, init, 0,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232 &thread_id_);
233 if (thread_) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000234 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235 } else {
236 return false;
237 }
238#elif defined(WEBRTC_POSIX)
239 pthread_attr_t attr;
240 pthread_attr_init(&attr);
241
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000242 int error_code = pthread_create(&thread_, &attr, PreRun, init);
243 if (0 != error_code) {
244 LOG(LS_ERROR) << "Unable to create pthread, error " << error_code;
245 return false;
246 }
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000247 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248#endif
249 return true;
250}
251
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000252bool Thread::WrapCurrent() {
253 return WrapCurrentWithThreadManager(ThreadManager::Instance(), true);
254}
255
256void Thread::UnwrapCurrent() {
257 // Clears the platform-specific thread-specific storage.
258 ThreadManager::Instance()->SetCurrentThread(NULL);
259#if defined(WEBRTC_WIN)
260 if (thread_ != NULL) {
261 if (!CloseHandle(thread_)) {
262 LOG_GLE(LS_ERROR) << "When unwrapping thread, failed to close handle.";
263 }
264 thread_ = NULL;
265 }
266#endif
267 running_.Reset();
268}
269
270void Thread::SafeWrapCurrent() {
271 WrapCurrentWithThreadManager(ThreadManager::Instance(), false);
272}
273
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000274void Thread::Join() {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000275 if (running()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000276 ASSERT(!IsCurrent());
jiayl@webrtc.org1fd362c2014-09-26 16:57:07 +0000277 if (Current() && !Current()->blocking_calls_allowed_) {
278 LOG(LS_WARNING) << "Waiting for the thread to join, "
279 << "but blocking calls have been disallowed";
280 }
281
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000282#if defined(WEBRTC_WIN)
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000283 ASSERT(thread_ != NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284 WaitForSingleObject(thread_, INFINITE);
285 CloseHandle(thread_);
286 thread_ = NULL;
287 thread_id_ = 0;
288#elif defined(WEBRTC_POSIX)
289 void *pv;
290 pthread_join(thread_, &pv);
291#endif
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000292 running_.Reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000293 }
294}
295
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000296bool Thread::SetAllowBlockingCalls(bool allow) {
297 ASSERT(IsCurrent());
298 bool previous = blocking_calls_allowed_;
299 blocking_calls_allowed_ = allow;
300 return previous;
301}
302
303// static
304void Thread::AssertBlockingIsAllowedOnCurrentThread() {
tfarinaa41ab932015-10-30 16:08:48 -0700305#if !defined(NDEBUG)
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000306 Thread* current = Thread::Current();
307 ASSERT(!current || current->blocking_calls_allowed_);
308#endif
309}
310
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000311void* Thread::PreRun(void* pv) {
312 ThreadInit* init = static_cast<ThreadInit*>(pv);
313 ThreadManager::Instance()->SetCurrentThread(init->thread);
Tommiea14f0a2015-05-18 13:51:06 +0200314 rtc::SetCurrentThreadName(init->thread->name_.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315#if __has_feature(objc_arc)
316 @autoreleasepool
317#elif defined(WEBRTC_MAC)
318 // Make sure the new thread has an autoreleasepool
319 ScopedAutoreleasePool pool;
320#endif
321 {
322 if (init->runnable) {
323 init->runnable->Run(init->thread);
324 } else {
325 init->thread->Run();
326 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000327 delete init;
328 return NULL;
329 }
330}
331
332void Thread::Run() {
333 ProcessMessages(kForever);
334}
335
336bool Thread::IsOwned() {
337 return owned_;
338}
339
340void Thread::Stop() {
341 MessageQueue::Quit();
342 Join();
343}
344
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700345void Thread::Send(const Location& posted_from,
346 MessageHandler* phandler,
347 uint32_t id,
348 MessageData* pdata) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000349 if (fStop_)
350 return;
351
352 // Sent messages are sent to the MessageHandler directly, in the context
353 // of "thread", like Win32 SendMessage. If in the right context,
354 // call the handler directly.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000355 Message msg;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700356 msg.posted_from = posted_from;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000357 msg.phandler = phandler;
358 msg.message_id = id;
359 msg.pdata = pdata;
360 if (IsCurrent()) {
361 phandler->OnMessage(&msg);
362 return;
363 }
364
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000365 AssertBlockingIsAllowedOnCurrentThread();
366
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000367 AutoThread thread;
368 Thread *current_thread = Thread::Current();
369 ASSERT(current_thread != NULL); // AutoThread ensures this
370
371 bool ready = false;
372 {
373 CritScope cs(&crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000374 _SendMessage smsg;
375 smsg.thread = current_thread;
376 smsg.msg = msg;
377 smsg.ready = &ready;
378 sendlist_.push_back(smsg);
379 }
380
381 // Wait for a reply
jbauch9ccedc32016-02-25 01:14:56 -0800382 WakeUpSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000383
384 bool waited = false;
385 crit_.Enter();
386 while (!ready) {
387 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000388 // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary
389 // thread invoking calls on the current thread.
390 current_thread->ReceiveSendsFromThread(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000391 current_thread->socketserver()->Wait(kForever, false);
392 waited = true;
393 crit_.Enter();
394 }
395 crit_.Leave();
396
397 // Our Wait loop above may have consumed some WakeUp events for this
398 // MessageQueue, that weren't relevant to this Send. Losing these WakeUps can
399 // cause problems for some SocketServers.
400 //
401 // Concrete example:
402 // Win32SocketServer on thread A calls Send on thread B. While processing the
403 // message, thread B Posts a message to A. We consume the wakeup for that
404 // Post while waiting for the Send to complete, which means that when we exit
405 // this loop, we need to issue another WakeUp, or else the Posted message
406 // won't be processed in a timely manner.
407
408 if (waited) {
409 current_thread->socketserver()->WakeUp();
410 }
411}
412
413void Thread::ReceiveSends() {
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000414 ReceiveSendsFromThread(NULL);
415}
416
417void Thread::ReceiveSendsFromThread(const Thread* source) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000418 // Receive a sent message. Cleanup scenarios:
419 // - thread sending exits: We don't allow this, since thread can exit
420 // only via Join, so Send must complete.
421 // - thread receiving exits: Wakeup/set ready in Thread::Clear()
422 // - object target cleared: Wakeup/set ready in Thread::Clear()
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000423 _SendMessage smsg;
424
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000425 crit_.Enter();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000426 while (PopSendMessageFromThread(source, &smsg)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000427 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000428
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000429 smsg.msg.phandler->OnMessage(&smsg.msg);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000430
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000431 crit_.Enter();
432 *smsg.ready = true;
433 smsg.thread->socketserver()->WakeUp();
434 }
435 crit_.Leave();
436}
437
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000438bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) {
439 for (std::list<_SendMessage>::iterator it = sendlist_.begin();
440 it != sendlist_.end(); ++it) {
441 if (it->thread == source || source == NULL) {
442 *msg = *it;
443 sendlist_.erase(it);
444 return true;
445 }
446 }
447 return false;
448}
449
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700450void Thread::InvokeInternal(const Location& posted_from,
451 MessageHandler* handler) {
452 TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line",
453 posted_from.file_and_line(), "src_func",
454 posted_from.function_name());
455 Send(posted_from, handler);
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +0000456}
457
Peter Boström0c4e06b2015-10-07 12:23:21 +0200458void Thread::Clear(MessageHandler* phandler,
459 uint32_t id,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000460 MessageList* removed) {
461 CritScope cs(&crit_);
462
463 // Remove messages on sendlist_ with phandler
464 // Object target cleared: remove from send list, wakeup/set ready
465 // if sender not NULL.
466
467 std::list<_SendMessage>::iterator iter = sendlist_.begin();
468 while (iter != sendlist_.end()) {
469 _SendMessage smsg = *iter;
470 if (smsg.msg.Match(phandler, id)) {
471 if (removed) {
472 removed->push_back(smsg.msg);
473 } else {
474 delete smsg.msg.pdata;
475 }
476 iter = sendlist_.erase(iter);
477 *smsg.ready = true;
478 smsg.thread->socketserver()->WakeUp();
479 continue;
480 }
481 ++iter;
482 }
483
484 MessageQueue::Clear(phandler, id, removed);
485}
486
487bool Thread::ProcessMessages(int cmsLoop) {
Honghai Zhang82d78622016-05-06 11:29:15 -0700488 int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000489 int cmsNext = cmsLoop;
490
491 while (true) {
492#if __has_feature(objc_arc)
493 @autoreleasepool
494#elif defined(WEBRTC_MAC)
495 // see: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html
496 // Each thread is supposed to have an autorelease pool. Also for event loops
497 // like this, autorelease pool needs to be created and drained/released
498 // for each cycle.
499 ScopedAutoreleasePool pool;
500#endif
501 {
502 Message msg;
503 if (!Get(&msg, cmsNext))
504 return !IsQuitting();
505 Dispatch(&msg);
506
507 if (cmsLoop != kForever) {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700508 cmsNext = static_cast<int>(TimeUntil(msEnd));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000509 if (cmsNext < 0)
510 return true;
511 }
512 }
513 }
514}
515
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000516bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager,
517 bool need_synchronize_access) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000518 if (running())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000519 return false;
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000520
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000521#if defined(WEBRTC_WIN)
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000522 if (need_synchronize_access) {
523 // We explicitly ask for no rights other than synchronization.
524 // This gives us the best chance of succeeding.
525 thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId());
526 if (!thread_) {
527 LOG_GLE(LS_ERROR) << "Unable to get handle to thread.";
528 return false;
529 }
530 thread_id_ = GetCurrentThreadId();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000531 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000532#elif defined(WEBRTC_POSIX)
533 thread_ = pthread_self();
534#endif
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000535
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000536 owned_ = false;
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000537 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000538 thread_manager->SetCurrentThread(this);
539 return true;
540}
541
danilchapbebf54c2016-04-28 01:32:48 -0700542AutoThread::AutoThread() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000543 if (!ThreadManager::Instance()->CurrentThread()) {
544 ThreadManager::Instance()->SetCurrentThread(this);
545 }
546}
547
548AutoThread::~AutoThread() {
549 Stop();
550 if (ThreadManager::Instance()->CurrentThread() == this) {
551 ThreadManager::Instance()->SetCurrentThread(NULL);
552 }
553}
554
555#if defined(WEBRTC_WIN)
556void ComThread::Run() {
557 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
558 ASSERT(SUCCEEDED(hr));
559 if (SUCCEEDED(hr)) {
560 Thread::Run();
561 CoUninitialize();
562 } else {
563 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr;
564 }
565}
566#endif
567
568} // namespace rtc