blob: 8ab381f4df8b0f22412ebba4439e098c52c4336b [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"
Tommiea14f0a2015-05-18 13:51:06 +020025#include "webrtc/base/platform_thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026#include "webrtc/base/stringutils.h"
27#include "webrtc/base/timeutils.h"
28
29#if !__has_feature(objc_arc) && (defined(WEBRTC_MAC))
30#include "webrtc/base/maccocoathreadhelper.h"
31#include "webrtc/base/scoped_autorelease_pool.h"
32#endif
33
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000034#include "webrtc/base/trace_event.h"
35
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036namespace 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141Thread::Thread(SocketServer* ss)
142 : MessageQueue(ss),
143 priority_(PRIORITY_NORMAL),
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000144 running_(true, false),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145#if defined(WEBRTC_WIN)
146 thread_(NULL),
147 thread_id_(0),
148#endif
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000149 owned_(true),
150 blocking_calls_allowed_(true) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151 SetName("Thread", this); // default name
152}
153
154Thread::~Thread() {
155 Stop();
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000156 Clear(NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157}
158
159bool Thread::SleepMs(int milliseconds) {
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000160 AssertBlockingIsAllowedOnCurrentThread();
161
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162#if defined(WEBRTC_WIN)
163 ::Sleep(milliseconds);
164 return true;
165#else
166 // POSIX has both a usleep() and a nanosleep(), but the former is deprecated,
167 // so we use nanosleep() even though it has greater precision than necessary.
168 struct timespec ts;
169 ts.tv_sec = milliseconds / 1000;
170 ts.tv_nsec = (milliseconds % 1000) * 1000000;
171 int ret = nanosleep(&ts, NULL);
172 if (ret != 0) {
173 LOG_ERR(LS_WARNING) << "nanosleep() returning early";
174 return false;
175 }
176 return true;
177#endif
178}
179
180bool Thread::SetName(const std::string& name, const void* obj) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000181 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000182 name_ = name;
183 if (obj) {
184 char buf[16];
185 sprintfn(buf, sizeof(buf), " 0x%p", obj);
186 name_ += buf;
187 }
188 return true;
189}
190
191bool Thread::SetPriority(ThreadPriority priority) {
192#if defined(WEBRTC_WIN)
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000193 if (running()) {
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000194 ASSERT(thread_ != NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195 BOOL ret = FALSE;
196 if (priority == PRIORITY_NORMAL) {
197 ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_NORMAL);
198 } else if (priority == PRIORITY_HIGH) {
199 ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_HIGHEST);
200 } else if (priority == PRIORITY_ABOVE_NORMAL) {
201 ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_ABOVE_NORMAL);
202 } else if (priority == PRIORITY_IDLE) {
203 ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_IDLE);
204 }
205 if (!ret) {
206 return false;
207 }
208 }
209 priority_ = priority;
210 return true;
211#else
212 // TODO: Implement for Linux/Mac if possible.
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000213 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000214 priority_ = priority;
215 return true;
216#endif
217}
218
219bool Thread::Start(Runnable* runnable) {
220 ASSERT(owned_);
221 if (!owned_) return false;
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000222 ASSERT(!running());
223 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224
225 Restart(); // reset fStop_ if the thread is being restarted
226
227 // Make sure that ThreadManager is created on the main thread before
228 // we start a new thread.
229 ThreadManager::Instance();
230
231 ThreadInit* init = new ThreadInit;
232 init->thread = this;
233 init->runnable = runnable;
234#if defined(WEBRTC_WIN)
235 DWORD flags = 0;
236 if (priority_ != PRIORITY_NORMAL) {
237 flags = CREATE_SUSPENDED;
238 }
239 thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PreRun, init, flags,
240 &thread_id_);
241 if (thread_) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000242 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000243 if (priority_ != PRIORITY_NORMAL) {
244 SetPriority(priority_);
245 ::ResumeThread(thread_);
246 }
247 } else {
248 return false;
249 }
250#elif defined(WEBRTC_POSIX)
251 pthread_attr_t attr;
252 pthread_attr_init(&attr);
253
254 // Thread priorities are not supported in NaCl.
255#if !defined(__native_client__)
256 if (priority_ != PRIORITY_NORMAL) {
257 if (priority_ == PRIORITY_IDLE) {
258 // There is no POSIX-standard way to set a below-normal priority for an
259 // individual thread (only whole process), so let's not support it.
260 LOG(LS_WARNING) << "PRIORITY_IDLE not supported";
261 } else {
262 // Set real-time round-robin policy.
263 if (pthread_attr_setschedpolicy(&attr, SCHED_RR) != 0) {
264 LOG(LS_ERROR) << "pthread_attr_setschedpolicy";
265 }
266 struct sched_param param;
267 if (pthread_attr_getschedparam(&attr, &param) != 0) {
268 LOG(LS_ERROR) << "pthread_attr_getschedparam";
269 } else {
270 // The numbers here are arbitrary.
271 if (priority_ == PRIORITY_HIGH) {
272 param.sched_priority = 6; // 6 = HIGH
273 } else {
274 ASSERT(priority_ == PRIORITY_ABOVE_NORMAL);
275 param.sched_priority = 4; // 4 = ABOVE_NORMAL
276 }
277 if (pthread_attr_setschedparam(&attr, &param) != 0) {
278 LOG(LS_ERROR) << "pthread_attr_setschedparam";
279 }
280 }
281 }
282 }
283#endif // !defined(__native_client__)
284
285 int error_code = pthread_create(&thread_, &attr, PreRun, init);
286 if (0 != error_code) {
287 LOG(LS_ERROR) << "Unable to create pthread, error " << error_code;
288 return false;
289 }
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000290 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291#endif
292 return true;
293}
294
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000295bool Thread::WrapCurrent() {
296 return WrapCurrentWithThreadManager(ThreadManager::Instance(), true);
297}
298
299void Thread::UnwrapCurrent() {
300 // Clears the platform-specific thread-specific storage.
301 ThreadManager::Instance()->SetCurrentThread(NULL);
302#if defined(WEBRTC_WIN)
303 if (thread_ != NULL) {
304 if (!CloseHandle(thread_)) {
305 LOG_GLE(LS_ERROR) << "When unwrapping thread, failed to close handle.";
306 }
307 thread_ = NULL;
308 }
309#endif
310 running_.Reset();
311}
312
313void Thread::SafeWrapCurrent() {
314 WrapCurrentWithThreadManager(ThreadManager::Instance(), false);
315}
316
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000317void Thread::Join() {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000318 if (running()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000319 ASSERT(!IsCurrent());
jiayl@webrtc.org1fd362c2014-09-26 16:57:07 +0000320 if (Current() && !Current()->blocking_calls_allowed_) {
321 LOG(LS_WARNING) << "Waiting for the thread to join, "
322 << "but blocking calls have been disallowed";
323 }
324
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000325#if defined(WEBRTC_WIN)
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000326 ASSERT(thread_ != NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000327 WaitForSingleObject(thread_, INFINITE);
328 CloseHandle(thread_);
329 thread_ = NULL;
330 thread_id_ = 0;
331#elif defined(WEBRTC_POSIX)
332 void *pv;
333 pthread_join(thread_, &pv);
334#endif
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000335 running_.Reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000336 }
337}
338
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000339bool Thread::SetAllowBlockingCalls(bool allow) {
340 ASSERT(IsCurrent());
341 bool previous = blocking_calls_allowed_;
342 blocking_calls_allowed_ = allow;
343 return previous;
344}
345
346// static
347void Thread::AssertBlockingIsAllowedOnCurrentThread() {
348#ifdef _DEBUG
349 Thread* current = Thread::Current();
350 ASSERT(!current || current->blocking_calls_allowed_);
351#endif
352}
353
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000354void* Thread::PreRun(void* pv) {
355 ThreadInit* init = static_cast<ThreadInit*>(pv);
356 ThreadManager::Instance()->SetCurrentThread(init->thread);
Tommiea14f0a2015-05-18 13:51:06 +0200357 rtc::SetCurrentThreadName(init->thread->name_.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000358#if __has_feature(objc_arc)
359 @autoreleasepool
360#elif defined(WEBRTC_MAC)
361 // Make sure the new thread has an autoreleasepool
362 ScopedAutoreleasePool pool;
363#endif
364 {
365 if (init->runnable) {
366 init->runnable->Run(init->thread);
367 } else {
368 init->thread->Run();
369 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000370 delete init;
371 return NULL;
372 }
373}
374
375void Thread::Run() {
376 ProcessMessages(kForever);
377}
378
379bool Thread::IsOwned() {
380 return owned_;
381}
382
383void Thread::Stop() {
384 MessageQueue::Quit();
385 Join();
386}
387
Peter Boström0c4e06b2015-10-07 12:23:21 +0200388void Thread::Send(MessageHandler* phandler, uint32_t id, MessageData* pdata) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000389 if (fStop_)
390 return;
391
392 // Sent messages are sent to the MessageHandler directly, in the context
393 // of "thread", like Win32 SendMessage. If in the right context,
394 // call the handler directly.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000395 Message msg;
396 msg.phandler = phandler;
397 msg.message_id = id;
398 msg.pdata = pdata;
399 if (IsCurrent()) {
400 phandler->OnMessage(&msg);
401 return;
402 }
403
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000404 AssertBlockingIsAllowedOnCurrentThread();
405
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000406 AutoThread thread;
407 Thread *current_thread = Thread::Current();
408 ASSERT(current_thread != NULL); // AutoThread ensures this
409
410 bool ready = false;
411 {
412 CritScope cs(&crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000413 _SendMessage smsg;
414 smsg.thread = current_thread;
415 smsg.msg = msg;
416 smsg.ready = &ready;
417 sendlist_.push_back(smsg);
418 }
419
420 // Wait for a reply
421
422 ss_->WakeUp();
423
424 bool waited = false;
425 crit_.Enter();
426 while (!ready) {
427 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000428 // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary
429 // thread invoking calls on the current thread.
430 current_thread->ReceiveSendsFromThread(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000431 current_thread->socketserver()->Wait(kForever, false);
432 waited = true;
433 crit_.Enter();
434 }
435 crit_.Leave();
436
437 // Our Wait loop above may have consumed some WakeUp events for this
438 // MessageQueue, that weren't relevant to this Send. Losing these WakeUps can
439 // cause problems for some SocketServers.
440 //
441 // Concrete example:
442 // Win32SocketServer on thread A calls Send on thread B. While processing the
443 // message, thread B Posts a message to A. We consume the wakeup for that
444 // Post while waiting for the Send to complete, which means that when we exit
445 // this loop, we need to issue another WakeUp, or else the Posted message
446 // won't be processed in a timely manner.
447
448 if (waited) {
449 current_thread->socketserver()->WakeUp();
450 }
451}
452
453void Thread::ReceiveSends() {
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000454 ReceiveSendsFromThread(NULL);
455}
456
457void Thread::ReceiveSendsFromThread(const Thread* source) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000458 // Receive a sent message. Cleanup scenarios:
459 // - thread sending exits: We don't allow this, since thread can exit
460 // only via Join, so Send must complete.
461 // - thread receiving exits: Wakeup/set ready in Thread::Clear()
462 // - object target cleared: Wakeup/set ready in Thread::Clear()
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000463 _SendMessage smsg;
464
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000465 crit_.Enter();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000466 while (PopSendMessageFromThread(source, &smsg)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000467 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000468
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000469 smsg.msg.phandler->OnMessage(&smsg.msg);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000470
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000471 crit_.Enter();
472 *smsg.ready = true;
473 smsg.thread->socketserver()->WakeUp();
474 }
475 crit_.Leave();
476}
477
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000478bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) {
479 for (std::list<_SendMessage>::iterator it = sendlist_.begin();
480 it != sendlist_.end(); ++it) {
481 if (it->thread == source || source == NULL) {
482 *msg = *it;
483 sendlist_.erase(it);
484 return true;
485 }
486 }
487 return false;
488}
489
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +0000490void Thread::InvokeBegin() {
491 TRACE_EVENT_BEGIN0("webrtc", "Thread::Invoke");
492}
493
494void Thread::InvokeEnd() {
495 TRACE_EVENT_END0("webrtc", "Thread::Invoke");
496}
497
Peter Boström0c4e06b2015-10-07 12:23:21 +0200498void Thread::Clear(MessageHandler* phandler,
499 uint32_t id,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000500 MessageList* removed) {
501 CritScope cs(&crit_);
502
503 // Remove messages on sendlist_ with phandler
504 // Object target cleared: remove from send list, wakeup/set ready
505 // if sender not NULL.
506
507 std::list<_SendMessage>::iterator iter = sendlist_.begin();
508 while (iter != sendlist_.end()) {
509 _SendMessage smsg = *iter;
510 if (smsg.msg.Match(phandler, id)) {
511 if (removed) {
512 removed->push_back(smsg.msg);
513 } else {
514 delete smsg.msg.pdata;
515 }
516 iter = sendlist_.erase(iter);
517 *smsg.ready = true;
518 smsg.thread->socketserver()->WakeUp();
519 continue;
520 }
521 ++iter;
522 }
523
524 MessageQueue::Clear(phandler, id, removed);
525}
526
527bool Thread::ProcessMessages(int cmsLoop) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200528 uint32_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000529 int cmsNext = cmsLoop;
530
531 while (true) {
532#if __has_feature(objc_arc)
533 @autoreleasepool
534#elif defined(WEBRTC_MAC)
535 // see: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html
536 // Each thread is supposed to have an autorelease pool. Also for event loops
537 // like this, autorelease pool needs to be created and drained/released
538 // for each cycle.
539 ScopedAutoreleasePool pool;
540#endif
541 {
542 Message msg;
543 if (!Get(&msg, cmsNext))
544 return !IsQuitting();
545 Dispatch(&msg);
546
547 if (cmsLoop != kForever) {
548 cmsNext = TimeUntil(msEnd);
549 if (cmsNext < 0)
550 return true;
551 }
552 }
553 }
554}
555
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000556bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager,
557 bool need_synchronize_access) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000558 if (running())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000559 return false;
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000560
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000561#if defined(WEBRTC_WIN)
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000562 if (need_synchronize_access) {
563 // We explicitly ask for no rights other than synchronization.
564 // This gives us the best chance of succeeding.
565 thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId());
566 if (!thread_) {
567 LOG_GLE(LS_ERROR) << "Unable to get handle to thread.";
568 return false;
569 }
570 thread_id_ = GetCurrentThreadId();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000571 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000572#elif defined(WEBRTC_POSIX)
573 thread_ = pthread_self();
574#endif
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000575
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000576 owned_ = false;
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000577 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000578 thread_manager->SetCurrentThread(this);
579 return true;
580}
581
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000582AutoThread::AutoThread(SocketServer* ss) : Thread(ss) {
583 if (!ThreadManager::Instance()->CurrentThread()) {
584 ThreadManager::Instance()->SetCurrentThread(this);
585 }
586}
587
588AutoThread::~AutoThread() {
589 Stop();
590 if (ThreadManager::Instance()->CurrentThread() == this) {
591 ThreadManager::Instance()->SetCurrentThread(NULL);
592 }
593}
594
595#if defined(WEBRTC_WIN)
596void ComThread::Run() {
597 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
598 ASSERT(SUCCEEDED(hr));
599 if (SUCCEEDED(hr)) {
600 Thread::Run();
601 CoUninitialize();
602 } else {
603 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr;
604 }
605}
606#endif
607
608} // namespace rtc