blob: 2657bbeb4ee24a7e562dd712e233e4243d50fbd5 [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
nisseede5da42017-01-12 05:15:36 -080023#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024#include "webrtc/base/common.h"
25#include "webrtc/base/logging.h"
danilchapbebf54c2016-04-28 01:32:48 -070026#include "webrtc/base/nullsocketserver.h"
Tommiea14f0a2015-05-18 13:51:06 +020027#include "webrtc/base/platform_thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028#include "webrtc/base/stringutils.h"
29#include "webrtc/base/timeutils.h"
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070030#include "webrtc/base/trace_event.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031
32#if !__has_feature(objc_arc) && (defined(WEBRTC_MAC))
33#include "webrtc/base/maccocoathreadhelper.h"
34#include "webrtc/base/scoped_autorelease_pool.h"
35#endif
36
37namespace rtc {
38
39ThreadManager* ThreadManager::Instance() {
Andrew MacDonald469c2c02015-05-22 17:50:26 -070040 RTC_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 return &thread_manager;
42}
43
44// static
45Thread* Thread::Current() {
46 return ThreadManager::Instance()->CurrentThread();
47}
48
49#if defined(WEBRTC_POSIX)
50ThreadManager::ThreadManager() {
51 pthread_key_create(&key_, NULL);
52#ifndef NO_MAIN_THREAD_WRAPPING
53 WrapCurrentThread();
54#endif
55#if !__has_feature(objc_arc) && (defined(WEBRTC_MAC))
56 // Under Automatic Reference Counting (ARC), you cannot use autorelease pools
57 // directly. Instead, you use @autoreleasepool blocks instead. Also, we are
58 // maintaining thread safety using immutability within context of GCD dispatch
59 // queues in this case.
60 InitCocoaMultiThreading();
61#endif
62}
63
64ThreadManager::~ThreadManager() {
65#if __has_feature(objc_arc)
66 @autoreleasepool
67#elif defined(WEBRTC_MAC)
68 // This is called during exit, at which point apparently no NSAutoreleasePools
69 // are available; but we might still need them to do cleanup (or we get the
70 // "no autoreleasepool in place, just leaking" warning when exiting).
71 ScopedAutoreleasePool pool;
72#endif
73 {
74 UnwrapCurrentThread();
75 pthread_key_delete(key_);
76 }
77}
78
79Thread *ThreadManager::CurrentThread() {
80 return static_cast<Thread *>(pthread_getspecific(key_));
81}
82
83void ThreadManager::SetCurrentThread(Thread *thread) {
84 pthread_setspecific(key_, thread);
85}
86#endif
87
88#if defined(WEBRTC_WIN)
89ThreadManager::ThreadManager() {
90 key_ = TlsAlloc();
91#ifndef NO_MAIN_THREAD_WRAPPING
92 WrapCurrentThread();
93#endif
94}
95
96ThreadManager::~ThreadManager() {
97 UnwrapCurrentThread();
98 TlsFree(key_);
99}
100
101Thread *ThreadManager::CurrentThread() {
102 return static_cast<Thread *>(TlsGetValue(key_));
103}
104
105void ThreadManager::SetCurrentThread(Thread *thread) {
106 TlsSetValue(key_, thread);
107}
108#endif
109
110Thread *ThreadManager::WrapCurrentThread() {
111 Thread* result = CurrentThread();
112 if (NULL == result) {
113 result = new Thread();
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000114 result->WrapCurrentWithThreadManager(this, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115 }
116 return result;
117}
118
119void ThreadManager::UnwrapCurrentThread() {
120 Thread* t = CurrentThread();
121 if (t && !(t->IsOwned())) {
122 t->UnwrapCurrent();
123 delete t;
124 }
125}
126
127struct ThreadInit {
128 Thread* thread;
129 Runnable* runnable;
130};
131
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000132Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls()
133 : thread_(Thread::Current()),
134 previous_state_(thread_->SetAllowBlockingCalls(false)) {
135}
136
137Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() {
nisseede5da42017-01-12 05:15:36 -0800138 RTC_DCHECK(thread_->IsCurrent());
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000139 thread_->SetAllowBlockingCalls(previous_state_);
140}
141
danilchapbebf54c2016-04-28 01:32:48 -0700142Thread::Thread() : Thread(SocketServer::CreateDefault()) {}
143
144Thread::Thread(SocketServer* ss)
jbauch25d1f282016-02-05 00:25:02 -0800145 : MessageQueue(ss, false),
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000146 running_(true, false),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147#if defined(WEBRTC_WIN)
148 thread_(NULL),
149 thread_id_(0),
150#endif
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000151 owned_(true),
152 blocking_calls_allowed_(true) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153 SetName("Thread", this); // default name
danilchapbebf54c2016-04-28 01:32:48 -0700154 DoInit();
155}
156
157Thread::Thread(std::unique_ptr<SocketServer> ss)
158 : MessageQueue(std::move(ss), false),
159 running_(true, false),
160#if defined(WEBRTC_WIN)
161 thread_(NULL),
162 thread_id_(0),
163#endif
164 owned_(true),
165 blocking_calls_allowed_(true) {
166 SetName("Thread", this); // default name
167 DoInit();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168}
169
170Thread::~Thread() {
171 Stop();
jbauch25d1f282016-02-05 00:25:02 -0800172 DoDestroy();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173}
174
danilchapbebf54c2016-04-28 01:32:48 -0700175std::unique_ptr<Thread> Thread::CreateWithSocketServer() {
176 return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault()));
177}
178
179std::unique_ptr<Thread> Thread::Create() {
180 return std::unique_ptr<Thread>(
181 new Thread(std::unique_ptr<SocketServer>(new NullSocketServer())));
182}
183
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000184bool Thread::SleepMs(int milliseconds) {
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000185 AssertBlockingIsAllowedOnCurrentThread();
186
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187#if defined(WEBRTC_WIN)
188 ::Sleep(milliseconds);
189 return true;
190#else
191 // POSIX has both a usleep() and a nanosleep(), but the former is deprecated,
192 // so we use nanosleep() even though it has greater precision than necessary.
193 struct timespec ts;
194 ts.tv_sec = milliseconds / 1000;
195 ts.tv_nsec = (milliseconds % 1000) * 1000000;
196 int ret = nanosleep(&ts, NULL);
197 if (ret != 0) {
198 LOG_ERR(LS_WARNING) << "nanosleep() returning early";
199 return false;
200 }
201 return true;
202#endif
203}
204
205bool Thread::SetName(const std::string& name, const void* obj) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000206 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000207 name_ = name;
208 if (obj) {
209 char buf[16];
210 sprintfn(buf, sizeof(buf), " 0x%p", obj);
211 name_ += buf;
212 }
213 return true;
214}
215
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000216bool Thread::Start(Runnable* runnable) {
nisseede5da42017-01-12 05:15:36 -0800217 RTC_DCHECK(owned_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000218 if (!owned_) return false;
nisseede5da42017-01-12 05:15:36 -0800219 RTC_DCHECK(!running());
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000220 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221
André Susano Pinto02a57972016-07-22 13:30:05 +0200222 Restart(); // reset IsQuitting() if the thread is being restarted
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000223
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)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100232 thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PreRun, init, 0,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000233 &thread_id_);
234 if (thread_) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000235 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236 } else {
237 return false;
238 }
239#elif defined(WEBRTC_POSIX)
240 pthread_attr_t attr;
241 pthread_attr_init(&attr);
242
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000243 int error_code = pthread_create(&thread_, &attr, PreRun, init);
244 if (0 != error_code) {
245 LOG(LS_ERROR) << "Unable to create pthread, error " << error_code;
246 return false;
247 }
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000248 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249#endif
250 return true;
251}
252
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000253bool Thread::WrapCurrent() {
254 return WrapCurrentWithThreadManager(ThreadManager::Instance(), true);
255}
256
257void Thread::UnwrapCurrent() {
258 // Clears the platform-specific thread-specific storage.
259 ThreadManager::Instance()->SetCurrentThread(NULL);
260#if defined(WEBRTC_WIN)
261 if (thread_ != NULL) {
262 if (!CloseHandle(thread_)) {
263 LOG_GLE(LS_ERROR) << "When unwrapping thread, failed to close handle.";
264 }
265 thread_ = NULL;
266 }
267#endif
268 running_.Reset();
269}
270
271void Thread::SafeWrapCurrent() {
272 WrapCurrentWithThreadManager(ThreadManager::Instance(), false);
273}
274
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000275void Thread::Join() {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000276 if (running()) {
nisseede5da42017-01-12 05:15:36 -0800277 RTC_DCHECK(!IsCurrent());
jiayl@webrtc.org1fd362c2014-09-26 16:57:07 +0000278 if (Current() && !Current()->blocking_calls_allowed_) {
279 LOG(LS_WARNING) << "Waiting for the thread to join, "
280 << "but blocking calls have been disallowed";
281 }
282
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283#if defined(WEBRTC_WIN)
nisseede5da42017-01-12 05:15:36 -0800284 RTC_DCHECK(thread_ != NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 WaitForSingleObject(thread_, INFINITE);
286 CloseHandle(thread_);
287 thread_ = NULL;
288 thread_id_ = 0;
289#elif defined(WEBRTC_POSIX)
290 void *pv;
291 pthread_join(thread_, &pv);
292#endif
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000293 running_.Reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000294 }
295}
296
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000297bool Thread::SetAllowBlockingCalls(bool allow) {
nisseede5da42017-01-12 05:15:36 -0800298 RTC_DCHECK(IsCurrent());
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000299 bool previous = blocking_calls_allowed_;
300 blocking_calls_allowed_ = allow;
301 return previous;
302}
303
304// static
305void Thread::AssertBlockingIsAllowedOnCurrentThread() {
tfarinaa41ab932015-10-30 16:08:48 -0700306#if !defined(NDEBUG)
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000307 Thread* current = Thread::Current();
nisseede5da42017-01-12 05:15:36 -0800308 RTC_DCHECK(!current || current->blocking_calls_allowed_);
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000309#endif
310}
311
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000312void* Thread::PreRun(void* pv) {
313 ThreadInit* init = static_cast<ThreadInit*>(pv);
314 ThreadManager::Instance()->SetCurrentThread(init->thread);
Tommiea14f0a2015-05-18 13:51:06 +0200315 rtc::SetCurrentThreadName(init->thread->name_.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316#if __has_feature(objc_arc)
317 @autoreleasepool
318#elif defined(WEBRTC_MAC)
319 // Make sure the new thread has an autoreleasepool
320 ScopedAutoreleasePool pool;
321#endif
322 {
323 if (init->runnable) {
324 init->runnable->Run(init->thread);
325 } else {
326 init->thread->Run();
327 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000328 delete init;
329 return NULL;
330 }
331}
332
333void Thread::Run() {
334 ProcessMessages(kForever);
335}
336
337bool Thread::IsOwned() {
338 return owned_;
339}
340
341void Thread::Stop() {
342 MessageQueue::Quit();
343 Join();
344}
345
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700346void Thread::Send(const Location& posted_from,
347 MessageHandler* phandler,
348 uint32_t id,
349 MessageData* pdata) {
André Susano Pinto02a57972016-07-22 13:30:05 +0200350 if (IsQuitting())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000351 return;
352
353 // Sent messages are sent to the MessageHandler directly, in the context
354 // of "thread", like Win32 SendMessage. If in the right context,
355 // call the handler directly.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000356 Message msg;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700357 msg.posted_from = posted_from;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000358 msg.phandler = phandler;
359 msg.message_id = id;
360 msg.pdata = pdata;
361 if (IsCurrent()) {
362 phandler->OnMessage(&msg);
363 return;
364 }
365
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000366 AssertBlockingIsAllowedOnCurrentThread();
367
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000368 AutoThread thread;
369 Thread *current_thread = Thread::Current();
nisseede5da42017-01-12 05:15:36 -0800370 RTC_DCHECK(current_thread != NULL); // AutoThread ensures this
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000371
372 bool ready = false;
373 {
374 CritScope cs(&crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000375 _SendMessage smsg;
376 smsg.thread = current_thread;
377 smsg.msg = msg;
378 smsg.ready = &ready;
379 sendlist_.push_back(smsg);
380 }
381
382 // Wait for a reply
jbauch9ccedc32016-02-25 01:14:56 -0800383 WakeUpSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000384
385 bool waited = false;
386 crit_.Enter();
387 while (!ready) {
388 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000389 // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary
390 // thread invoking calls on the current thread.
391 current_thread->ReceiveSendsFromThread(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000392 current_thread->socketserver()->Wait(kForever, false);
393 waited = true;
394 crit_.Enter();
395 }
396 crit_.Leave();
397
398 // Our Wait loop above may have consumed some WakeUp events for this
399 // MessageQueue, that weren't relevant to this Send. Losing these WakeUps can
400 // cause problems for some SocketServers.
401 //
402 // Concrete example:
403 // Win32SocketServer on thread A calls Send on thread B. While processing the
404 // message, thread B Posts a message to A. We consume the wakeup for that
405 // Post while waiting for the Send to complete, which means that when we exit
406 // this loop, we need to issue another WakeUp, or else the Posted message
407 // won't be processed in a timely manner.
408
409 if (waited) {
410 current_thread->socketserver()->WakeUp();
411 }
412}
413
414void Thread::ReceiveSends() {
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000415 ReceiveSendsFromThread(NULL);
416}
417
418void Thread::ReceiveSendsFromThread(const Thread* source) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000419 // Receive a sent message. Cleanup scenarios:
420 // - thread sending exits: We don't allow this, since thread can exit
421 // only via Join, so Send must complete.
422 // - thread receiving exits: Wakeup/set ready in Thread::Clear()
423 // - object target cleared: Wakeup/set ready in Thread::Clear()
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000424 _SendMessage smsg;
425
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000426 crit_.Enter();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000427 while (PopSendMessageFromThread(source, &smsg)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000428 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000429
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000430 smsg.msg.phandler->OnMessage(&smsg.msg);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000431
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000432 crit_.Enter();
433 *smsg.ready = true;
434 smsg.thread->socketserver()->WakeUp();
435 }
436 crit_.Leave();
437}
438
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000439bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) {
440 for (std::list<_SendMessage>::iterator it = sendlist_.begin();
441 it != sendlist_.end(); ++it) {
442 if (it->thread == source || source == NULL) {
443 *msg = *it;
444 sendlist_.erase(it);
445 return true;
446 }
447 }
448 return false;
449}
450
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700451void Thread::InvokeInternal(const Location& posted_from,
452 MessageHandler* handler) {
453 TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line",
454 posted_from.file_and_line(), "src_func",
455 posted_from.function_name());
456 Send(posted_from, handler);
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +0000457}
458
Peter Boström0c4e06b2015-10-07 12:23:21 +0200459void Thread::Clear(MessageHandler* phandler,
460 uint32_t id,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000461 MessageList* removed) {
462 CritScope cs(&crit_);
463
464 // Remove messages on sendlist_ with phandler
465 // Object target cleared: remove from send list, wakeup/set ready
466 // if sender not NULL.
467
468 std::list<_SendMessage>::iterator iter = sendlist_.begin();
469 while (iter != sendlist_.end()) {
470 _SendMessage smsg = *iter;
471 if (smsg.msg.Match(phandler, id)) {
472 if (removed) {
473 removed->push_back(smsg.msg);
474 } else {
475 delete smsg.msg.pdata;
476 }
477 iter = sendlist_.erase(iter);
478 *smsg.ready = true;
479 smsg.thread->socketserver()->WakeUp();
480 continue;
481 }
482 ++iter;
483 }
484
485 MessageQueue::Clear(phandler, id, removed);
486}
487
488bool Thread::ProcessMessages(int cmsLoop) {
Honghai Zhang82d78622016-05-06 11:29:15 -0700489 int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000490 int cmsNext = cmsLoop;
491
492 while (true) {
493#if __has_feature(objc_arc)
494 @autoreleasepool
495#elif defined(WEBRTC_MAC)
496 // see: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html
497 // Each thread is supposed to have an autorelease pool. Also for event loops
498 // like this, autorelease pool needs to be created and drained/released
499 // for each cycle.
500 ScopedAutoreleasePool pool;
501#endif
502 {
503 Message msg;
504 if (!Get(&msg, cmsNext))
505 return !IsQuitting();
506 Dispatch(&msg);
507
508 if (cmsLoop != kForever) {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700509 cmsNext = static_cast<int>(TimeUntil(msEnd));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000510 if (cmsNext < 0)
511 return true;
512 }
513 }
514 }
515}
516
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000517bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager,
518 bool need_synchronize_access) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000519 if (running())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000520 return false;
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000521
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000522#if defined(WEBRTC_WIN)
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000523 if (need_synchronize_access) {
524 // We explicitly ask for no rights other than synchronization.
525 // This gives us the best chance of succeeding.
526 thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId());
527 if (!thread_) {
528 LOG_GLE(LS_ERROR) << "Unable to get handle to thread.";
529 return false;
530 }
531 thread_id_ = GetCurrentThreadId();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000532 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000533#elif defined(WEBRTC_POSIX)
534 thread_ = pthread_self();
535#endif
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000536
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000537 owned_ = false;
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000538 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000539 thread_manager->SetCurrentThread(this);
540 return true;
541}
542
danilchapbebf54c2016-04-28 01:32:48 -0700543AutoThread::AutoThread() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000544 if (!ThreadManager::Instance()->CurrentThread()) {
545 ThreadManager::Instance()->SetCurrentThread(this);
546 }
547}
548
549AutoThread::~AutoThread() {
550 Stop();
551 if (ThreadManager::Instance()->CurrentThread() == this) {
552 ThreadManager::Instance()->SetCurrentThread(NULL);
553 }
554}
555
556#if defined(WEBRTC_WIN)
557void ComThread::Run() {
558 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
nisseede5da42017-01-12 05:15:36 -0800559 RTC_DCHECK(SUCCEEDED(hr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000560 if (SUCCEEDED(hr)) {
561 Thread::Run();
562 CoUninitialize();
563 } else {
564 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr;
565 }
566}
567#endif
568
569} // namespace rtc