| 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 13 matching lines...) Expand all Loading... |
| 24 #include "webrtc/base/timeutils.h" | 24 #include "webrtc/base/timeutils.h" |
| 25 #include "webrtc/base/trace_event.h" | 25 #include "webrtc/base/trace_event.h" |
| 26 | 26 |
| 27 namespace rtc { | 27 namespace rtc { |
| 28 | 28 |
| 29 ThreadManager* ThreadManager::Instance() { | 29 ThreadManager* ThreadManager::Instance() { |
| 30 RTC_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ()); | 30 RTC_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ()); |
| 31 return &thread_manager; | 31 return &thread_manager; |
| 32 } | 32 } |
| 33 | 33 |
| 34 ThreadManager::~ThreadManager() { |
| 35 // By above RTC_DEFINE_STATIC_LOCAL. |
| 36 RTC_NOTREACHED() << "ThreadManager should never be destructed."; |
| 37 } |
| 38 |
| 34 // static | 39 // static |
| 35 Thread* Thread::Current() { | 40 Thread* Thread::Current() { |
| 36 return ThreadManager::Instance()->CurrentThread(); | 41 ThreadManager* manager = ThreadManager::Instance(); |
| 42 Thread* thread = manager->CurrentThread(); |
| 43 |
| 44 #ifndef NO_MAIN_THREAD_WRAPPING |
| 45 // Only autowrap the thread which instantiated the ThreadManager. |
| 46 if (!thread && manager->IsMainThread()) { |
| 47 thread = new Thread(); |
| 48 thread->WrapCurrentWithThreadManager(manager, true); |
| 49 } |
| 50 #endif |
| 51 |
| 52 return thread; |
| 37 } | 53 } |
| 38 | 54 |
| 39 #if defined(WEBRTC_POSIX) | 55 #if defined(WEBRTC_POSIX) |
| 40 #if !defined(WEBRTC_MAC) | 56 #if !defined(WEBRTC_MAC) |
| 41 ThreadManager::ThreadManager() { | 57 ThreadManager::ThreadManager() { |
| 58 main_thread_ref_ = CurrentThreadRef(); |
| 42 pthread_key_create(&key_, nullptr); | 59 pthread_key_create(&key_, nullptr); |
| 43 #ifndef NO_MAIN_THREAD_WRAPPING | |
| 44 WrapCurrentThread(); | |
| 45 #endif | |
| 46 } | |
| 47 | |
| 48 ThreadManager::~ThreadManager() { | |
| 49 UnwrapCurrentThread(); | |
| 50 pthread_key_delete(key_); | |
| 51 } | 60 } |
| 52 #endif | 61 #endif |
| 53 | 62 |
| 54 Thread *ThreadManager::CurrentThread() { | 63 Thread *ThreadManager::CurrentThread() { |
| 55 return static_cast<Thread *>(pthread_getspecific(key_)); | 64 return static_cast<Thread *>(pthread_getspecific(key_)); |
| 56 } | 65 } |
| 57 | 66 |
| 58 void ThreadManager::SetCurrentThread(Thread *thread) { | 67 void ThreadManager::SetCurrentThread(Thread *thread) { |
| 59 pthread_setspecific(key_, thread); | 68 pthread_setspecific(key_, thread); |
| 60 } | 69 } |
| 61 #endif | 70 #endif |
| 62 | 71 |
| 63 #if defined(WEBRTC_WIN) | 72 #if defined(WEBRTC_WIN) |
| 64 ThreadManager::ThreadManager() { | 73 ThreadManager::ThreadManager() { |
| 74 main_thread_ref_ = CurrentThreadRef(); |
| 65 key_ = TlsAlloc(); | 75 key_ = TlsAlloc(); |
| 66 #ifndef NO_MAIN_THREAD_WRAPPING | |
| 67 WrapCurrentThread(); | |
| 68 #endif | |
| 69 } | |
| 70 | |
| 71 ThreadManager::~ThreadManager() { | |
| 72 UnwrapCurrentThread(); | |
| 73 TlsFree(key_); | |
| 74 } | 76 } |
| 75 | 77 |
| 76 Thread *ThreadManager::CurrentThread() { | 78 Thread *ThreadManager::CurrentThread() { |
| 77 return static_cast<Thread *>(TlsGetValue(key_)); | 79 return static_cast<Thread *>(TlsGetValue(key_)); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void ThreadManager::SetCurrentThread(Thread *thread) { | 82 void ThreadManager::SetCurrentThread(Thread *thread) { |
| 81 TlsSetValue(key_, thread); | 83 TlsSetValue(key_, thread); |
| 82 } | 84 } |
| 83 #endif | 85 #endif |
| 84 | 86 |
| 85 Thread *ThreadManager::WrapCurrentThread() { | 87 Thread *ThreadManager::WrapCurrentThread() { |
| 86 Thread* result = CurrentThread(); | 88 Thread* result = CurrentThread(); |
| 87 if (nullptr == result) { | 89 if (nullptr == result) { |
| 88 result = new Thread(); | 90 result = new Thread(); |
| 89 result->WrapCurrentWithThreadManager(this, true); | 91 result->WrapCurrentWithThreadManager(this, true); |
| 90 } | 92 } |
| 91 return result; | 93 return result; |
| 92 } | 94 } |
| 93 | 95 |
| 94 void ThreadManager::UnwrapCurrentThread() { | 96 void ThreadManager::UnwrapCurrentThread() { |
| 95 Thread* t = CurrentThread(); | 97 Thread* t = CurrentThread(); |
| 96 if (t && !(t->IsOwned())) { | 98 if (t && !(t->IsOwned())) { |
| 97 t->UnwrapCurrent(); | 99 t->UnwrapCurrent(); |
| 98 delete t; | 100 delete t; |
| 99 } | 101 } |
| 100 } | 102 } |
| 101 | 103 |
| 104 bool ThreadManager::IsMainThread() { |
| 105 return IsThreadRefEqual(CurrentThreadRef(), main_thread_ref_); |
| 106 } |
| 107 |
| 102 Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() | 108 Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() |
| 103 : thread_(Thread::Current()), | 109 : thread_(Thread::Current()), |
| 104 previous_state_(thread_->SetAllowBlockingCalls(false)) { | 110 previous_state_(thread_->SetAllowBlockingCalls(false)) { |
| 105 } | 111 } |
| 106 | 112 |
| 107 Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { | 113 Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { |
| 108 RTC_DCHECK(thread_->IsCurrent()); | 114 RTC_DCHECK(thread_->IsCurrent()); |
| 109 thread_->SetAllowBlockingCalls(previous_state_); | 115 thread_->SetAllowBlockingCalls(previous_state_); |
| 110 } | 116 } |
| 111 | 117 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 135 blocking_calls_allowed_(true) { | 141 blocking_calls_allowed_(true) { |
| 136 SetName("Thread", this); // default name | 142 SetName("Thread", this); // default name |
| 137 DoInit(); | 143 DoInit(); |
| 138 } | 144 } |
| 139 | 145 |
| 140 Thread::~Thread() { | 146 Thread::~Thread() { |
| 141 Stop(); | 147 Stop(); |
| 142 DoDestroy(); | 148 DoDestroy(); |
| 143 } | 149 } |
| 144 | 150 |
| 151 bool Thread::IsCurrent() const { |
| 152 return ThreadManager::Instance()->CurrentThread() == this; |
| 153 } |
| 154 |
| 145 std::unique_ptr<Thread> Thread::CreateWithSocketServer() { | 155 std::unique_ptr<Thread> Thread::CreateWithSocketServer() { |
| 146 return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault())); | 156 return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault())); |
| 147 } | 157 } |
| 148 | 158 |
| 149 std::unique_ptr<Thread> Thread::Create() { | 159 std::unique_ptr<Thread> Thread::Create() { |
| 150 return std::unique_ptr<Thread>( | 160 return std::unique_ptr<Thread>( |
| 151 new Thread(std::unique_ptr<SocketServer>(new NullSocketServer()))); | 161 new Thread(std::unique_ptr<SocketServer>(new NullSocketServer()))); |
| 152 } | 162 } |
| 153 | 163 |
| 154 bool Thread::SleepMs(int milliseconds) { | 164 bool Thread::SleepMs(int milliseconds) { |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 } | 522 } |
| 513 | 523 |
| 514 AutoThread::~AutoThread() { | 524 AutoThread::~AutoThread() { |
| 515 Stop(); | 525 Stop(); |
| 516 if (ThreadManager::Instance()->CurrentThread() == this) { | 526 if (ThreadManager::Instance()->CurrentThread() == this) { |
| 517 ThreadManager::Instance()->SetCurrentThread(nullptr); | 527 ThreadManager::Instance()->SetCurrentThread(nullptr); |
| 518 } | 528 } |
| 519 } | 529 } |
| 520 | 530 |
| 521 } // namespace rtc | 531 } // namespace rtc |
| OLD | NEW |