blob: b5be475fb38891aeade314acf3be7eb0dddd3265 [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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#if defined(WEBRTC_WIN)
14#include <comdef.h>
15#elif defined(WEBRTC_POSIX)
16#include <time.h>
17#endif
18
nisseede5da42017-01-12 05:15:36 -080019#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020#include "webrtc/base/logging.h"
danilchapbebf54c2016-04-28 01:32:48 -070021#include "webrtc/base/nullsocketserver.h"
Tommiea14f0a2015-05-18 13:51:06 +020022#include "webrtc/base/platform_thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023#include "webrtc/base/stringutils.h"
24#include "webrtc/base/timeutils.h"
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070025#include "webrtc/base/trace_event.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027namespace rtc {
28
29ThreadManager* ThreadManager::Instance() {
Andrew MacDonald469c2c02015-05-22 17:50:26 -070030 RTC_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031 return &thread_manager;
32}
33
34// static
35Thread* Thread::Current() {
36 return ThreadManager::Instance()->CurrentThread();
37}
38
39#if defined(WEBRTC_POSIX)
kthelgason61abe152017-03-29 02:32:36 -070040#if !defined(WEBRTC_MAC)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041ThreadManager::ThreadManager() {
deadbeef37f5ecf2017-02-27 14:06:41 -080042 pthread_key_create(&key_, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043#ifndef NO_MAIN_THREAD_WRAPPING
44 WrapCurrentThread();
45#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046}
47
48ThreadManager::~ThreadManager() {
kthelgasonde6adbe2017-02-22 00:42:11 -080049 UnwrapCurrentThread();
50 pthread_key_delete(key_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051}
kthelgason61abe152017-03-29 02:32:36 -070052#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053
54Thread *ThreadManager::CurrentThread() {
55 return static_cast<Thread *>(pthread_getspecific(key_));
56}
57
58void ThreadManager::SetCurrentThread(Thread *thread) {
59 pthread_setspecific(key_, thread);
60}
61#endif
62
63#if defined(WEBRTC_WIN)
64ThreadManager::ThreadManager() {
65 key_ = TlsAlloc();
66#ifndef NO_MAIN_THREAD_WRAPPING
67 WrapCurrentThread();
68#endif
69}
70
71ThreadManager::~ThreadManager() {
72 UnwrapCurrentThread();
73 TlsFree(key_);
74}
75
76Thread *ThreadManager::CurrentThread() {
77 return static_cast<Thread *>(TlsGetValue(key_));
78}
79
80void ThreadManager::SetCurrentThread(Thread *thread) {
81 TlsSetValue(key_, thread);
82}
83#endif
84
85Thread *ThreadManager::WrapCurrentThread() {
86 Thread* result = CurrentThread();
deadbeef37f5ecf2017-02-27 14:06:41 -080087 if (nullptr == result) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088 result = new Thread();
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +000089 result->WrapCurrentWithThreadManager(this, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090 }
91 return result;
92}
93
94void ThreadManager::UnwrapCurrentThread() {
95 Thread* t = CurrentThread();
96 if (t && !(t->IsOwned())) {
97 t->UnwrapCurrent();
98 delete t;
99 }
100}
101
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000102Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls()
103 : thread_(Thread::Current()),
104 previous_state_(thread_->SetAllowBlockingCalls(false)) {
105}
106
107Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() {
nisseede5da42017-01-12 05:15:36 -0800108 RTC_DCHECK(thread_->IsCurrent());
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000109 thread_->SetAllowBlockingCalls(previous_state_);
110}
111
danilchapbebf54c2016-04-28 01:32:48 -0700112Thread::Thread() : Thread(SocketServer::CreateDefault()) {}
113
114Thread::Thread(SocketServer* ss)
jbauch25d1f282016-02-05 00:25:02 -0800115 : MessageQueue(ss, false),
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000116 running_(true, false),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117#if defined(WEBRTC_WIN)
deadbeef37f5ecf2017-02-27 14:06:41 -0800118 thread_(nullptr),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119 thread_id_(0),
120#endif
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000121 owned_(true),
122 blocking_calls_allowed_(true) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 SetName("Thread", this); // default name
danilchapbebf54c2016-04-28 01:32:48 -0700124 DoInit();
125}
126
127Thread::Thread(std::unique_ptr<SocketServer> ss)
128 : MessageQueue(std::move(ss), false),
129 running_(true, false),
130#if defined(WEBRTC_WIN)
deadbeef37f5ecf2017-02-27 14:06:41 -0800131 thread_(nullptr),
danilchapbebf54c2016-04-28 01:32:48 -0700132 thread_id_(0),
133#endif
134 owned_(true),
135 blocking_calls_allowed_(true) {
136 SetName("Thread", this); // default name
137 DoInit();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138}
139
140Thread::~Thread() {
141 Stop();
jbauch25d1f282016-02-05 00:25:02 -0800142 DoDestroy();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143}
144
danilchapbebf54c2016-04-28 01:32:48 -0700145std::unique_ptr<Thread> Thread::CreateWithSocketServer() {
146 return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault()));
147}
148
149std::unique_ptr<Thread> Thread::Create() {
150 return std::unique_ptr<Thread>(
151 new Thread(std::unique_ptr<SocketServer>(new NullSocketServer())));
152}
153
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154bool Thread::SleepMs(int milliseconds) {
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000155 AssertBlockingIsAllowedOnCurrentThread();
156
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157#if defined(WEBRTC_WIN)
158 ::Sleep(milliseconds);
159 return true;
160#else
161 // POSIX has both a usleep() and a nanosleep(), but the former is deprecated,
162 // so we use nanosleep() even though it has greater precision than necessary.
163 struct timespec ts;
164 ts.tv_sec = milliseconds / 1000;
165 ts.tv_nsec = (milliseconds % 1000) * 1000000;
deadbeef37f5ecf2017-02-27 14:06:41 -0800166 int ret = nanosleep(&ts, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167 if (ret != 0) {
168 LOG_ERR(LS_WARNING) << "nanosleep() returning early";
169 return false;
170 }
171 return true;
172#endif
173}
174
175bool Thread::SetName(const std::string& name, const void* obj) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000176 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177 name_ = name;
178 if (obj) {
179 char buf[16];
180 sprintfn(buf, sizeof(buf), " 0x%p", obj);
181 name_ += buf;
182 }
183 return true;
184}
185
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000186bool Thread::Start(Runnable* runnable) {
nisseede5da42017-01-12 05:15:36 -0800187 RTC_DCHECK(owned_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000188 if (!owned_) return false;
nisseede5da42017-01-12 05:15:36 -0800189 RTC_DCHECK(!running());
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000190 if (running()) return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191
André Susano Pinto02a57972016-07-22 13:30:05 +0200192 Restart(); // reset IsQuitting() if the thread is being restarted
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000193
194 // Make sure that ThreadManager is created on the main thread before
195 // we start a new thread.
196 ThreadManager::Instance();
197
198 ThreadInit* init = new ThreadInit;
199 init->thread = this;
200 init->runnable = runnable;
201#if defined(WEBRTC_WIN)
deadbeef37f5ecf2017-02-27 14:06:41 -0800202 thread_ = CreateThread(nullptr, 0, PreRun, init, 0, &thread_id_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000203 if (thread_) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000204 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000205 } else {
206 return false;
207 }
208#elif defined(WEBRTC_POSIX)
209 pthread_attr_t attr;
210 pthread_attr_init(&attr);
211
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000212 int error_code = pthread_create(&thread_, &attr, PreRun, init);
213 if (0 != error_code) {
214 LOG(LS_ERROR) << "Unable to create pthread, error " << error_code;
215 return false;
216 }
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000217 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000218#endif
219 return true;
220}
221
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000222bool Thread::WrapCurrent() {
223 return WrapCurrentWithThreadManager(ThreadManager::Instance(), true);
224}
225
226void Thread::UnwrapCurrent() {
227 // Clears the platform-specific thread-specific storage.
deadbeef37f5ecf2017-02-27 14:06:41 -0800228 ThreadManager::Instance()->SetCurrentThread(nullptr);
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000229#if defined(WEBRTC_WIN)
deadbeef37f5ecf2017-02-27 14:06:41 -0800230 if (thread_ != nullptr) {
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000231 if (!CloseHandle(thread_)) {
232 LOG_GLE(LS_ERROR) << "When unwrapping thread, failed to close handle.";
233 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800234 thread_ = nullptr;
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000235 }
236#endif
237 running_.Reset();
238}
239
240void Thread::SafeWrapCurrent() {
241 WrapCurrentWithThreadManager(ThreadManager::Instance(), false);
242}
243
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000244void Thread::Join() {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000245 if (running()) {
nisseede5da42017-01-12 05:15:36 -0800246 RTC_DCHECK(!IsCurrent());
jiayl@webrtc.org1fd362c2014-09-26 16:57:07 +0000247 if (Current() && !Current()->blocking_calls_allowed_) {
248 LOG(LS_WARNING) << "Waiting for the thread to join, "
249 << "but blocking calls have been disallowed";
250 }
251
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000252#if defined(WEBRTC_WIN)
deadbeef37f5ecf2017-02-27 14:06:41 -0800253 RTC_DCHECK(thread_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 WaitForSingleObject(thread_, INFINITE);
255 CloseHandle(thread_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800256 thread_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000257 thread_id_ = 0;
258#elif defined(WEBRTC_POSIX)
259 void *pv;
260 pthread_join(thread_, &pv);
261#endif
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000262 running_.Reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263 }
264}
265
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000266bool Thread::SetAllowBlockingCalls(bool allow) {
nisseede5da42017-01-12 05:15:36 -0800267 RTC_DCHECK(IsCurrent());
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000268 bool previous = blocking_calls_allowed_;
269 blocking_calls_allowed_ = allow;
270 return previous;
271}
272
273// static
274void Thread::AssertBlockingIsAllowedOnCurrentThread() {
tfarinaa41ab932015-10-30 16:08:48 -0700275#if !defined(NDEBUG)
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000276 Thread* current = Thread::Current();
nisseede5da42017-01-12 05:15:36 -0800277 RTC_DCHECK(!current || current->blocking_calls_allowed_);
henrike@webrtc.org92a9bac2014-07-14 22:03:57 +0000278#endif
279}
280
deadbeefdc20e262017-01-31 15:10:44 -0800281// static
kthelgason61abe152017-03-29 02:32:36 -0700282#if !defined(WEBRTC_MAC)
deadbeefdc20e262017-01-31 15:10:44 -0800283#if defined(WEBRTC_WIN)
284DWORD WINAPI Thread::PreRun(LPVOID pv) {
285#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286void* Thread::PreRun(void* pv) {
deadbeefdc20e262017-01-31 15:10:44 -0800287#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288 ThreadInit* init = static_cast<ThreadInit*>(pv);
289 ThreadManager::Instance()->SetCurrentThread(init->thread);
Tommiea14f0a2015-05-18 13:51:06 +0200290 rtc::SetCurrentThreadName(init->thread->name_.c_str());
kthelgasonde6adbe2017-02-22 00:42:11 -0800291 if (init->runnable) {
292 init->runnable->Run(init->thread);
293 } else {
294 init->thread->Run();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 }
kthelgasonde6adbe2017-02-22 00:42:11 -0800296 delete init;
297#ifdef WEBRTC_WIN
298 return 0;
299#else
300 return nullptr;
301#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302}
kthelgason61abe152017-03-29 02:32:36 -0700303#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304
305void Thread::Run() {
306 ProcessMessages(kForever);
307}
308
309bool Thread::IsOwned() {
310 return owned_;
311}
312
313void Thread::Stop() {
314 MessageQueue::Quit();
315 Join();
316}
317
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700318void Thread::Send(const Location& posted_from,
319 MessageHandler* phandler,
320 uint32_t id,
321 MessageData* pdata) {
André Susano Pinto02a57972016-07-22 13:30:05 +0200322 if (IsQuitting())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000323 return;
324
325 // Sent messages are sent to the MessageHandler directly, in the context
326 // of "thread", like Win32 SendMessage. If in the right context,
327 // call the handler directly.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000328 Message msg;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700329 msg.posted_from = posted_from;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000330 msg.phandler = phandler;
331 msg.message_id = id;
332 msg.pdata = pdata;
333 if (IsCurrent()) {
334 phandler->OnMessage(&msg);
335 return;
336 }
337
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000338 AssertBlockingIsAllowedOnCurrentThread();
339
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000340 AutoThread thread;
341 Thread *current_thread = Thread::Current();
deadbeef37f5ecf2017-02-27 14:06:41 -0800342 RTC_DCHECK(current_thread != nullptr); // AutoThread ensures this
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343
344 bool ready = false;
345 {
346 CritScope cs(&crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000347 _SendMessage smsg;
348 smsg.thread = current_thread;
349 smsg.msg = msg;
350 smsg.ready = &ready;
351 sendlist_.push_back(smsg);
352 }
353
354 // Wait for a reply
jbauch9ccedc32016-02-25 01:14:56 -0800355 WakeUpSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000356
357 bool waited = false;
358 crit_.Enter();
359 while (!ready) {
360 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000361 // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary
362 // thread invoking calls on the current thread.
363 current_thread->ReceiveSendsFromThread(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000364 current_thread->socketserver()->Wait(kForever, false);
365 waited = true;
366 crit_.Enter();
367 }
368 crit_.Leave();
369
370 // Our Wait loop above may have consumed some WakeUp events for this
371 // MessageQueue, that weren't relevant to this Send. Losing these WakeUps can
372 // cause problems for some SocketServers.
373 //
374 // Concrete example:
375 // Win32SocketServer on thread A calls Send on thread B. While processing the
376 // message, thread B Posts a message to A. We consume the wakeup for that
377 // Post while waiting for the Send to complete, which means that when we exit
378 // this loop, we need to issue another WakeUp, or else the Posted message
379 // won't be processed in a timely manner.
380
381 if (waited) {
382 current_thread->socketserver()->WakeUp();
383 }
384}
385
386void Thread::ReceiveSends() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800387 ReceiveSendsFromThread(nullptr);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000388}
389
390void Thread::ReceiveSendsFromThread(const Thread* source) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000391 // Receive a sent message. Cleanup scenarios:
392 // - thread sending exits: We don't allow this, since thread can exit
393 // only via Join, so Send must complete.
394 // - thread receiving exits: Wakeup/set ready in Thread::Clear()
395 // - object target cleared: Wakeup/set ready in Thread::Clear()
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000396 _SendMessage smsg;
397
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000398 crit_.Enter();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000399 while (PopSendMessageFromThread(source, &smsg)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000400 crit_.Leave();
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000401
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402 smsg.msg.phandler->OnMessage(&smsg.msg);
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000403
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000404 crit_.Enter();
405 *smsg.ready = true;
406 smsg.thread->socketserver()->WakeUp();
407 }
408 crit_.Leave();
409}
410
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000411bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) {
412 for (std::list<_SendMessage>::iterator it = sendlist_.begin();
413 it != sendlist_.end(); ++it) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800414 if (it->thread == source || source == nullptr) {
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000415 *msg = *it;
416 sendlist_.erase(it);
417 return true;
418 }
419 }
420 return false;
421}
422
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700423void Thread::InvokeInternal(const Location& posted_from,
424 MessageHandler* handler) {
425 TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line",
426 posted_from.file_and_line(), "src_func",
427 posted_from.function_name());
428 Send(posted_from, handler);
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +0000429}
430
Peter Boström0c4e06b2015-10-07 12:23:21 +0200431void Thread::Clear(MessageHandler* phandler,
432 uint32_t id,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000433 MessageList* removed) {
434 CritScope cs(&crit_);
435
436 // Remove messages on sendlist_ with phandler
437 // Object target cleared: remove from send list, wakeup/set ready
deadbeef37f5ecf2017-02-27 14:06:41 -0800438 // if sender not null.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000439
440 std::list<_SendMessage>::iterator iter = sendlist_.begin();
441 while (iter != sendlist_.end()) {
442 _SendMessage smsg = *iter;
443 if (smsg.msg.Match(phandler, id)) {
444 if (removed) {
445 removed->push_back(smsg.msg);
446 } else {
447 delete smsg.msg.pdata;
448 }
449 iter = sendlist_.erase(iter);
450 *smsg.ready = true;
451 smsg.thread->socketserver()->WakeUp();
452 continue;
453 }
454 ++iter;
455 }
456
457 MessageQueue::Clear(phandler, id, removed);
458}
459
kthelgason61abe152017-03-29 02:32:36 -0700460#if !defined(WEBRTC_MAC)
461// Note that these methods have a separate implementation for mac and ios
462// defined in webrtc/base/thread_darwin.mm.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000463bool Thread::ProcessMessages(int cmsLoop) {
Honghai Zhang82d78622016-05-06 11:29:15 -0700464 int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000465 int cmsNext = cmsLoop;
466
467 while (true) {
kthelgasonde6adbe2017-02-22 00:42:11 -0800468 Message msg;
469 if (!Get(&msg, cmsNext))
470 return !IsQuitting();
471 Dispatch(&msg);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000472
kthelgasonde6adbe2017-02-22 00:42:11 -0800473 if (cmsLoop != kForever) {
474 cmsNext = static_cast<int>(TimeUntil(msEnd));
475 if (cmsNext < 0)
476 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000477 }
478 }
479}
kthelgason61abe152017-03-29 02:32:36 -0700480#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000481
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000482bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager,
483 bool need_synchronize_access) {
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000484 if (running())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000485 return false;
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000486
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000487#if defined(WEBRTC_WIN)
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000488 if (need_synchronize_access) {
489 // We explicitly ask for no rights other than synchronization.
490 // This gives us the best chance of succeeding.
491 thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId());
492 if (!thread_) {
493 LOG_GLE(LS_ERROR) << "Unable to get handle to thread.";
494 return false;
495 }
496 thread_id_ = GetCurrentThreadId();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000497 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000498#elif defined(WEBRTC_POSIX)
499 thread_ = pthread_self();
500#endif
jiayl@webrtc.orgba737cb2014-09-18 16:45:21 +0000501
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000502 owned_ = false;
fischman@webrtc.orge5063b12014-05-23 17:28:50 +0000503 running_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000504 thread_manager->SetCurrentThread(this);
505 return true;
506}
507
danilchapbebf54c2016-04-28 01:32:48 -0700508AutoThread::AutoThread() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000509 if (!ThreadManager::Instance()->CurrentThread()) {
510 ThreadManager::Instance()->SetCurrentThread(this);
511 }
512}
513
514AutoThread::~AutoThread() {
515 Stop();
516 if (ThreadManager::Instance()->CurrentThread() == this) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800517 ThreadManager::Instance()->SetCurrentThread(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000518 }
519}
520
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000521} // namespace rtc