| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 | 38 |
| 39 // static | 39 // static |
| 40 Thread* Thread::Current() { | 40 Thread* Thread::Current() { |
| 41 ThreadManager* manager = ThreadManager::Instance(); | 41 ThreadManager* manager = ThreadManager::Instance(); |
| 42 Thread* thread = manager->CurrentThread(); | 42 Thread* thread = manager->CurrentThread(); |
| 43 | 43 |
| 44 #ifndef NO_MAIN_THREAD_WRAPPING | 44 #ifndef NO_MAIN_THREAD_WRAPPING |
| 45 // Only autowrap the thread which instantiated the ThreadManager. | 45 // Only autowrap the thread which instantiated the ThreadManager. |
| 46 if (!thread && manager->IsMainThread()) { | 46 if (!thread && manager->IsMainThread()) { |
| 47 thread = new Thread(); | 47 thread = new Thread(SocketServer::CreateDefault()); |
| 48 thread->WrapCurrentWithThreadManager(manager, true); | 48 thread->WrapCurrentWithThreadManager(manager, true); |
| 49 } | 49 } |
| 50 #endif | 50 #endif |
| 51 | 51 |
| 52 return thread; | 52 return thread; |
| 53 } | 53 } |
| 54 | 54 |
| 55 #if defined(WEBRTC_POSIX) | 55 #if defined(WEBRTC_POSIX) |
| 56 #if !defined(WEBRTC_MAC) | 56 #if !defined(WEBRTC_MAC) |
| 57 ThreadManager::ThreadManager() { | 57 ThreadManager::ThreadManager() { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 80 } | 80 } |
| 81 | 81 |
| 82 void ThreadManager::SetCurrentThread(Thread *thread) { | 82 void ThreadManager::SetCurrentThread(Thread *thread) { |
| 83 TlsSetValue(key_, thread); | 83 TlsSetValue(key_, thread); |
| 84 } | 84 } |
| 85 #endif | 85 #endif |
| 86 | 86 |
| 87 Thread *ThreadManager::WrapCurrentThread() { | 87 Thread *ThreadManager::WrapCurrentThread() { |
| 88 Thread* result = CurrentThread(); | 88 Thread* result = CurrentThread(); |
| 89 if (nullptr == result) { | 89 if (nullptr == result) { |
| 90 result = new Thread(); | 90 result = new Thread(SocketServer::CreateDefault()); |
| 91 result->WrapCurrentWithThreadManager(this, true); | 91 result->WrapCurrentWithThreadManager(this, true); |
| 92 } | 92 } |
| 93 return result; | 93 return result; |
| 94 } | 94 } |
| 95 | 95 |
| 96 void ThreadManager::UnwrapCurrentThread() { | 96 void ThreadManager::UnwrapCurrentThread() { |
| 97 Thread* t = CurrentThread(); | 97 Thread* t = CurrentThread(); |
| 98 if (t && !(t->IsOwned())) { | 98 if (t && !(t->IsOwned())) { |
| 99 t->UnwrapCurrent(); | 99 t->UnwrapCurrent(); |
| 100 delete t; | 100 delete t; |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 bool ThreadManager::IsMainThread() { | 104 bool ThreadManager::IsMainThread() { |
| 105 return IsThreadRefEqual(CurrentThreadRef(), main_thread_ref_); | 105 return IsThreadRefEqual(CurrentThreadRef(), main_thread_ref_); |
| 106 } | 106 } |
| 107 | 107 |
| 108 Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() | 108 Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() |
| 109 : thread_(Thread::Current()), | 109 : thread_(Thread::Current()), |
| 110 previous_state_(thread_->SetAllowBlockingCalls(false)) { | 110 previous_state_(thread_->SetAllowBlockingCalls(false)) { |
| 111 } | 111 } |
| 112 | 112 |
| 113 Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { | 113 Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { |
| 114 RTC_DCHECK(thread_->IsCurrent()); | 114 RTC_DCHECK(thread_->IsCurrent()); |
| 115 thread_->SetAllowBlockingCalls(previous_state_); | 115 thread_->SetAllowBlockingCalls(previous_state_); |
| 116 } | 116 } |
| 117 | 117 |
| 118 // DEPRECATED. |
| 118 Thread::Thread() : Thread(SocketServer::CreateDefault()) {} | 119 Thread::Thread() : Thread(SocketServer::CreateDefault()) {} |
| 119 | 120 |
| 120 Thread::Thread(SocketServer* ss) | 121 Thread::Thread(SocketServer* ss) |
| 121 : MessageQueue(ss, false), | 122 : MessageQueue(ss, false), |
| 122 running_(true, false), | 123 running_(true, false), |
| 123 #if defined(WEBRTC_WIN) | 124 #if defined(WEBRTC_WIN) |
| 124 thread_(nullptr), | 125 thread_(nullptr), |
| 125 thread_id_(0), | 126 thread_id_(0), |
| 126 #endif | 127 #endif |
| 127 owned_(true), | 128 owned_(true), |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 #elif defined(WEBRTC_POSIX) | 514 #elif defined(WEBRTC_POSIX) |
| 514 thread_ = pthread_self(); | 515 thread_ = pthread_self(); |
| 515 #endif | 516 #endif |
| 516 | 517 |
| 517 owned_ = false; | 518 owned_ = false; |
| 518 running_.Set(); | 519 running_.Set(); |
| 519 thread_manager->SetCurrentThread(this); | 520 thread_manager->SetCurrentThread(this); |
| 520 return true; | 521 return true; |
| 521 } | 522 } |
| 522 | 523 |
| 523 AutoThread::AutoThread() { | 524 AutoThread::AutoThread() : Thread(SocketServer::CreateDefault()) { |
| 524 if (!ThreadManager::Instance()->CurrentThread()) { | 525 if (!ThreadManager::Instance()->CurrentThread()) { |
| 525 ThreadManager::Instance()->SetCurrentThread(this); | 526 ThreadManager::Instance()->SetCurrentThread(this); |
| 526 } | 527 } |
| 527 } | 528 } |
| 528 | 529 |
| 529 AutoThread::~AutoThread() { | 530 AutoThread::~AutoThread() { |
| 530 Stop(); | 531 Stop(); |
| 531 if (ThreadManager::Instance()->CurrentThread() == this) { | 532 if (ThreadManager::Instance()->CurrentThread() == this) { |
| 532 ThreadManager::Instance()->SetCurrentThread(nullptr); | 533 ThreadManager::Instance()->SetCurrentThread(nullptr); |
| 533 } | 534 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 549 // P2PTransportChannelPingTest, relying on the message posted in | 550 // P2PTransportChannelPingTest, relying on the message posted in |
| 550 // cricket::Connection::Destroy. | 551 // cricket::Connection::Destroy. |
| 551 ProcessMessages(0); | 552 ProcessMessages(0); |
| 552 rtc::ThreadManager::Instance()->SetCurrentThread(old_thread_); | 553 rtc::ThreadManager::Instance()->SetCurrentThread(old_thread_); |
| 553 if (old_thread_) { | 554 if (old_thread_) { |
| 554 MessageQueueManager::Add(old_thread_); | 555 MessageQueueManager::Add(old_thread_); |
| 555 } | 556 } |
| 556 } | 557 } |
| 557 | 558 |
| 558 } // namespace rtc | 559 } // namespace rtc |
| OLD | NEW |