blob: aa2442f371303980ecacfff6df69366f55052130 [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"
25#include "webrtc/base/stringutils.h"
26#include "webrtc/base/timeutils.h"
27
28#if !__has_feature(objc_arc) && (defined(WEBRTC_MAC))
29#include "webrtc/base/maccocoathreadhelper.h"
30#include "webrtc/base/scoped_autorelease_pool.h"
31#endif
32
33namespace rtc {
34
35ThreadManager* ThreadManager::Instance() {
36 LIBJINGLE_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ());
37 return &thread_manager;
38}
39
40// static
41Thread* Thread::Current() {
42 return ThreadManager::Instance()->CurrentThread();
43}
44
45#if defined(WEBRTC_POSIX)
46ThreadManager::ThreadManager() {
47 pthread_key_create(&key_, NULL);
48#ifndef NO_MAIN_THREAD_WRAPPING
49 WrapCurrentThread();
50#endif
51#if !__has_feature(objc_arc) && (defined(WEBRTC_MAC))
52 // Under Automatic Reference Counting (ARC), you cannot use autorelease pools
53 // directly. Instead, you use @autoreleasepool blocks instead. Also, we are
54 // maintaining thread safety using immutability within context of GCD dispatch
55 // queues in this case.
56 InitCocoaMultiThreading();
57#endif
58}
59
60ThreadManager::~ThreadManager() {
61#if __has_feature(objc_arc)
62 @autoreleasepool
63#elif defined(WEBRTC_MAC)
64 // This is called during exit, at which point apparently no NSAutoreleasePools
65 // are available; but we might still need them to do cleanup (or we get the
66 // "no autoreleasepool in place, just leaking" warning when exiting).
67 ScopedAutoreleasePool pool;
68#endif
69 {
70 UnwrapCurrentThread();
71 pthread_key_delete(key_);
72 }
73}
74
75Thread *ThreadManager::CurrentThread() {
76 return static_cast<Thread *>(pthread_getspecific(key_));
77}
78
79void ThreadManager::SetCurrentThread(Thread *thread) {
80 pthread_setspecific(key_, thread);
81}
82#endif
83
84#if defined(WEBRTC_WIN)
85ThreadManager::ThreadManager() {
86 key_ = TlsAlloc();
87#ifndef NO_MAIN_THREAD_WRAPPING
88 WrapCurrentThread();
89#endif
90}
91
92ThreadManager::~ThreadManager() {
93 UnwrapCurrentThread();
94 TlsFree(key_);
95}
96
97Thread *ThreadManager::CurrentThread() {
98 return static_cast<Thread *>(TlsGetValue(key_));
99}
100
101void ThreadManager::SetCurrentThread(Thread *thread) {
102 TlsSetValue(key_, thread);
103}
104#endif
105
106Thread *ThreadManager::WrapCurrentThread() {
107 Thread* result = CurrentThread();
108 if (NULL == result) {
109 result = new Thread();
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000110 result->WrapCurrentWithThreadManager(this, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111 }
112 return result;
113}
114
115void ThreadManager::UnwrapCurrentThread() {
116 Thread* t = CurrentThread();
117 if (t && !(t->IsOwned())) {
118 t->UnwrapCurrent();
119 delete t;
120 }
121}
122
123struct ThreadInit {
124 Thread* thread;
125 Runnable* runnable;
126};
127
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000128Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls()
129 : thread_(Thread::Current()),
130 previous_state_(thread_->SetAllowBlockingCalls(false)) {
131}
132
133Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() {
134 ASSERT(thread_->IsCurrent());
135 thread_->SetAllowBlockingCalls(previous_state_);
136}
137
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138Thread::Thread(SocketServer* ss)
139 : MessageQueue(ss),
140 priority_(PRIORITY_NORMAL),
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000141 running_(true, false),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142#if defined(WEBRTC_WIN)
143 thread_(NULL),
144 thread_id_(0),
145#endif
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000146 owned_(true),
147 blocking_calls_allowed_(true) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148 SetName("Thread", this); // default name
149}
150
151Thread::~Thread() {
152 Stop();
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000153 Clear(NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154}
155
156bool Thread::SleepMs(int milliseconds) {
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000157 AssertBlockingIsAllowedOnCurrentThread();
158
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159#if defined(WEBRTC_WIN)
160 ::Sleep(milliseconds);
161 return true;
162#else
163 // POSIX has both a usleep() and a nanosleep(), but the former is deprecated,
164 // so we use nanosleep() even though it has greater precision than necessary.
165 struct timespec ts;
166 ts.tv_sec = milliseconds / 1000;
167 ts.tv_nsec = (milliseconds % 1000) * 1000000;
168 int ret = nanosleep(&ts, NULL);
169 if (ret != 0) {
170 LOG_ERR(LS_WARNING) << "nanosleep() returning early";
171 return false;
172 }
173 return true;
174#endif
175}
176
177bool Thread::SetName(const std::string& name, const void* obj) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000178 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000179 name_ = name;
180 if (obj) {
181 char buf[16];
182 sprintfn(buf, sizeof(buf), " 0x%p", obj);
183 name_ += buf;
184 }
185 return true;
186}
187
188bool Thread::SetPriority(ThreadPriority priority) {
189#if defined(WEBRTC_WIN)
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000190 if (running()) {
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000191 ASSERT(thread_ != NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000192 BOOL ret = FALSE;
193 if (priority == PRIORITY_NORMAL) {
194 ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_NORMAL);
195 } else if (priority == PRIORITY_HIGH) {
196 ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_HIGHEST);
197 } else if (priority == PRIORITY_ABOVE_NORMAL) {
198 ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_ABOVE_NORMAL);
199 } else if (priority == PRIORITY_IDLE) {
200 ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_IDLE);
201 }
202 if (!ret) {
203 return false;
204 }
205 }
206 priority_ = priority;
207 return true;
208#else
209 // TODO: Implement for Linux/Mac if possible.
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000210 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 priority_ = priority;
212 return true;
213#endif
214}
215
216bool Thread::Start(Runnable* runnable) {
217 ASSERT(owned_);
218 if (!owned_) return false;
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000219 ASSERT(!running());
220 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221
222 Restart(); // reset fStop_ if the thread is being restarted
223
224 // Make sure that ThreadManager is created on the main thread before
225 // we start a new thread.
226 ThreadManager::Instance();
227
228 ThreadInit* init = new ThreadInit;
229 init->thread = this;
230 init->runnable = runnable;
231#if defined(WEBRTC_WIN)
232 DWORD flags = 0;
233 if (priority_ != PRIORITY_NORMAL) {
234 flags = CREATE_SUSPENDED;
235 }
236 thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PreRun, init, flags,
237 &thread_id_);
238 if (thread_) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000239 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240 if (priority_ != PRIORITY_NORMAL) {
241 SetPriority(priority_);
242 ::ResumeThread(thread_);
243 }
244 } else {
245 return false;
246 }
247#elif defined(WEBRTC_POSIX)
248 pthread_attr_t attr;
249 pthread_attr_init(&attr);
250
251 // Thread priorities are not supported in NaCl.
252#if !defined(__native_client__)
253 if (priority_ != PRIORITY_NORMAL) {
254 if (priority_ == PRIORITY_IDLE) {
255 // There is no POSIX-standard way to set a below-normal priority for an
256 // individual thread (only whole process), so let's not support it.
257 LOG(LS_WARNING) << "PRIORITY_IDLE not supported";
258 } else {
259 // Set real-time round-robin policy.
260 if (pthread_attr_setschedpolicy(&attr, SCHED_RR) != 0) {
261 LOG(LS_ERROR) << "pthread_attr_setschedpolicy";
262 }
263 struct sched_param param;
264 if (pthread_attr_getschedparam(&attr, &param) != 0) {
265 LOG(LS_ERROR) << "pthread_attr_getschedparam";
266 } else {
267 // The numbers here are arbitrary.
268 if (priority_ == PRIORITY_HIGH) {
269 param.sched_priority = 6; // 6 = HIGH
270 } else {
271 ASSERT(priority_ == PRIORITY_ABOVE_NORMAL);
272 param.sched_priority = 4; // 4 = ABOVE_NORMAL
273 }
274 if (pthread_attr_setschedparam(&attr, &param) != 0) {
275 LOG(LS_ERROR) << "pthread_attr_setschedparam";
276 }
277 }
278 }
279 }
280#endif // !defined(__native_client__)
281
282 int error_code = pthread_create(&thread_, &attr, PreRun, init);
283 if (0 != error_code) {
284 LOG(LS_ERROR) << "Unable to create pthread, error " << error_code;
285 return false;
286 }
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000287 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288#endif
289 return true;
290}
291
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000292bool Thread::WrapCurrent() {
293 return WrapCurrentWithThreadManager(ThreadManager::Instance(), true);
294}
295
296void Thread::UnwrapCurrent() {
297 // Clears the platform-specific thread-specific storage.
298 ThreadManager::Instance()->SetCurrentThread(NULL);
299#if defined(WEBRTC_WIN)
300 if (thread_ != NULL) {
301 if (!CloseHandle(thread_)) {
302 LOG_GLE(LS_ERROR) << "When unwrapping thread, failed to close handle.";
303 }
304 thread_ = NULL;
305 }
306#endif
307 running_.Reset();
308}
309
310void Thread::SafeWrapCurrent() {
311 WrapCurrentWithThreadManager(ThreadManager::Instance(), false);
312}
313
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000314void Thread::Join() {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000315 if (running()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316 ASSERT(!IsCurrent());
jiayl@webrtc.org1fd362c2014-09-26 16:57:07 +0000317 if (Current() && !Current()->blocking_calls_allowed_) {
318 LOG(LS_WARNING) << "Waiting for the thread to join, "
319 << "but blocking calls have been disallowed";
320 }
321
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322#if defined(WEBRTC_WIN)
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000323 ASSERT(thread_ != NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000324 WaitForSingleObject(thread_, INFINITE);
325 CloseHandle(thread_);
326 thread_ = NULL;
327 thread_id_ = 0;
328#elif defined(WEBRTC_POSIX)
329 void *pv;
330 pthread_join(thread_, &pv);
331#endif
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000332 running_.Reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000333 }
334}
335
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000336bool Thread::SetAllowBlockingCalls(bool allow) {
337 ASSERT(IsCurrent());
338 bool previous = blocking_calls_allowed_;
339 blocking_calls_allowed_ = allow;
340 return previous;
341}
342
343// static
344void Thread::AssertBlockingIsAllowedOnCurrentThread() {
345#ifdef _DEBUG
346 Thread* current = Thread::Current();
347 ASSERT(!current || current->blocking_calls_allowed_);
348#endif
349}
350
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000351#if defined(WEBRTC_WIN)
352// As seen on MSDN.
353// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.71).aspx
354#define MSDEV_SET_THREAD_NAME 0x406D1388
355typedef struct tagTHREADNAME_INFO {
356 DWORD dwType;
357 LPCSTR szName;
358 DWORD dwThreadID;
359 DWORD dwFlags;
360} THREADNAME_INFO;
361
362void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName) {
363 THREADNAME_INFO info;
364 info.dwType = 0x1000;
365 info.szName = szThreadName;
366 info.dwThreadID = dwThreadID;
367 info.dwFlags = 0;
368
369 __try {
370 RaiseException(MSDEV_SET_THREAD_NAME, 0, sizeof(info) / sizeof(DWORD),
371 reinterpret_cast<ULONG_PTR*>(&info));
372 }
373 __except(EXCEPTION_CONTINUE_EXECUTION) {
374 }
375}
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000376#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000377
378void* Thread::PreRun(void* pv) {
379 ThreadInit* init = static_cast<ThreadInit*>(pv);
380 ThreadManager::Instance()->SetCurrentThread(init->thread);
381#if defined(WEBRTC_WIN)
382 SetThreadName(GetCurrentThreadId(), init->thread->name_.c_str());
383#elif defined(WEBRTC_POSIX)
384 // TODO: See if naming exists for pthreads.
385#endif
386#if __has_feature(objc_arc)
387 @autoreleasepool
388#elif defined(WEBRTC_MAC)
389 // Make sure the new thread has an autoreleasepool
390 ScopedAutoreleasePool pool;
391#endif
392 {
393 if (init->runnable) {
394 init->runnable->Run(init->thread);
395 } else {
396 init->thread->Run();
397 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000398 delete init;
399 return NULL;
400 }
401}
402
403void Thread::Run() {
404 ProcessMessages(kForever);
405}
406
407bool Thread::IsOwned() {
408 return owned_;
409}
410
411void Thread::Stop() {
412 MessageQueue::Quit();
413 Join();
414}
415
416void Thread::Send(MessageHandler *phandler, uint32 id, MessageData *pdata) {
417 if (fStop_)
418 return;
419
420 // Sent messages are sent to the MessageHandler directly, in the context
421 // of "thread", like Win32 SendMessage. If in the right context,
422 // call the handler directly.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000423 Message msg;
424 msg.phandler = phandler;
425 msg.message_id = id;
426 msg.pdata = pdata;
427 if (IsCurrent()) {
428 phandler->OnMessage(&msg);
429 return;
430 }
431
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000432 AssertBlockingIsAllowedOnCurrentThread();
433
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000434 AutoThread thread;
435 Thread *current_thread = Thread::Current();
436 ASSERT(current_thread != NULL); // AutoThread ensures this
437
438 bool ready = false;
439 {
440 CritScope cs(&crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000441 _SendMessage smsg;
442 smsg.thread = current_thread;
443 smsg.msg = msg;
444 smsg.ready = &ready;
445 sendlist_.push_back(smsg);
446 }
447
448 // Wait for a reply
449
450 ss_->WakeUp();
451
452 bool waited = false;
453 crit_.Enter();
454 while (!ready) {
455 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000456 // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary
457 // thread invoking calls on the current thread.
458 current_thread->ReceiveSendsFromThread(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000459 current_thread->socketserver()->Wait(kForever, false);
460 waited = true;
461 crit_.Enter();
462 }
463 crit_.Leave();
464
465 // Our Wait loop above may have consumed some WakeUp events for this
466 // MessageQueue, that weren't relevant to this Send. Losing these WakeUps can
467 // cause problems for some SocketServers.
468 //
469 // Concrete example:
470 // Win32SocketServer on thread A calls Send on thread B. While processing the
471 // message, thread B Posts a message to A. We consume the wakeup for that
472 // Post while waiting for the Send to complete, which means that when we exit
473 // this loop, we need to issue another WakeUp, or else the Posted message
474 // won't be processed in a timely manner.
475
476 if (waited) {
477 current_thread->socketserver()->WakeUp();
478 }
479}
480
481void Thread::ReceiveSends() {
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000482 ReceiveSendsFromThread(NULL);
483}
484
485void Thread::ReceiveSendsFromThread(const Thread* source) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000486 // Receive a sent message. Cleanup scenarios:
487 // - thread sending exits: We don't allow this, since thread can exit
488 // only via Join, so Send must complete.
489 // - thread receiving exits: Wakeup/set ready in Thread::Clear()
490 // - object target cleared: Wakeup/set ready in Thread::Clear()
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000491 _SendMessage smsg;
492
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000493 crit_.Enter();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000494 while (PopSendMessageFromThread(source, &smsg)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000495 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000496
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000497 smsg.msg.phandler->OnMessage(&smsg.msg);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000498
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000499 crit_.Enter();
500 *smsg.ready = true;
501 smsg.thread->socketserver()->WakeUp();
502 }
503 crit_.Leave();
504}
505
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000506bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) {
507 for (std::list<_SendMessage>::iterator it = sendlist_.begin();
508 it != sendlist_.end(); ++it) {
509 if (it->thread == source || source == NULL) {
510 *msg = *it;
511 sendlist_.erase(it);
512 return true;
513 }
514 }
515 return false;
516}
517
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000518void Thread::Clear(MessageHandler *phandler, uint32 id,
519 MessageList* removed) {
520 CritScope cs(&crit_);
521
522 // Remove messages on sendlist_ with phandler
523 // Object target cleared: remove from send list, wakeup/set ready
524 // if sender not NULL.
525
526 std::list<_SendMessage>::iterator iter = sendlist_.begin();
527 while (iter != sendlist_.end()) {
528 _SendMessage smsg = *iter;
529 if (smsg.msg.Match(phandler, id)) {
530 if (removed) {
531 removed->push_back(smsg.msg);
532 } else {
533 delete smsg.msg.pdata;
534 }
535 iter = sendlist_.erase(iter);
536 *smsg.ready = true;
537 smsg.thread->socketserver()->WakeUp();
538 continue;
539 }
540 ++iter;
541 }
542
543 MessageQueue::Clear(phandler, id, removed);
544}
545
546bool Thread::ProcessMessages(int cmsLoop) {
547 uint32 msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop);
548 int cmsNext = cmsLoop;
549
550 while (true) {
551#if __has_feature(objc_arc)
552 @autoreleasepool
553#elif defined(WEBRTC_MAC)
554 // see: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html
555 // Each thread is supposed to have an autorelease pool. Also for event loops
556 // like this, autorelease pool needs to be created and drained/released
557 // for each cycle.
558 ScopedAutoreleasePool pool;
559#endif
560 {
561 Message msg;
562 if (!Get(&msg, cmsNext))
563 return !IsQuitting();
564 Dispatch(&msg);
565
566 if (cmsLoop != kForever) {
567 cmsNext = TimeUntil(msEnd);
568 if (cmsNext < 0)
569 return true;
570 }
571 }
572 }
573}
574
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000575bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager,
576 bool need_synchronize_access) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000577 if (running())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000578 return false;
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000579
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000580#if defined(WEBRTC_WIN)
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000581 if (need_synchronize_access) {
582 // We explicitly ask for no rights other than synchronization.
583 // This gives us the best chance of succeeding.
584 thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId());
585 if (!thread_) {
586 LOG_GLE(LS_ERROR) << "Unable to get handle to thread.";
587 return false;
588 }
589 thread_id_ = GetCurrentThreadId();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000590 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000591#elif defined(WEBRTC_POSIX)
592 thread_ = pthread_self();
593#endif
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000594
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000595 owned_ = false;
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000596 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000597 thread_manager->SetCurrentThread(this);
598 return true;
599}
600
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000601AutoThread::AutoThread(SocketServer* ss) : Thread(ss) {
602 if (!ThreadManager::Instance()->CurrentThread()) {
603 ThreadManager::Instance()->SetCurrentThread(this);
604 }
605}
606
607AutoThread::~AutoThread() {
608 Stop();
609 if (ThreadManager::Instance()->CurrentThread() == this) {
610 ThreadManager::Instance()->SetCurrentThread(NULL);
611 }
612}
613
614#if defined(WEBRTC_WIN)
615void ComThread::Run() {
616 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
617 ASSERT(SUCCEEDED(hr));
618 if (SUCCEEDED(hr)) {
619 Thread::Run();
620 CoUninitialize();
621 } else {
622 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr;
623 }
624}
625#endif
626
627} // namespace rtc