Index: webrtc/base/thread.cc |
diff --git a/webrtc/base/thread.cc b/webrtc/base/thread.cc |
index b5be475fb38891aeade314acf3be7eb0dddd3265..9a71c1aa3fd352130677641d54b2ff8a79ce5d96 100644 |
--- a/webrtc/base/thread.cc |
+++ b/webrtc/base/thread.cc |
@@ -31,23 +31,32 @@ ThreadManager* ThreadManager::Instance() { |
return &thread_manager; |
} |
+ThreadManager::~ThreadManager() { |
+ // By above RTC_DEFINE_STATIC_LOCAL. |
+ RTC_NOTREACHED() << "ThreadManager should never be destructed."; |
+} |
+ |
// static |
Thread* Thread::Current() { |
- return ThreadManager::Instance()->CurrentThread(); |
+ ThreadManager* manager = ThreadManager::Instance(); |
+ Thread* thread = manager->CurrentThread(); |
+ |
+#ifndef NO_MAIN_THREAD_WRAPPING |
+ // Only autowrap the thread which instantiated the ThreadManager. |
+ if (!thread && manager->IsMainThread()) { |
+ thread = new Thread(); |
+ thread->WrapCurrentWithThreadManager(manager, true); |
+ } |
+#endif |
+ |
+ return thread; |
} |
#if defined(WEBRTC_POSIX) |
#if !defined(WEBRTC_MAC) |
ThreadManager::ThreadManager() { |
+ main_thread_ref_ = CurrentThreadRef(); |
pthread_key_create(&key_, nullptr); |
-#ifndef NO_MAIN_THREAD_WRAPPING |
- WrapCurrentThread(); |
-#endif |
-} |
- |
-ThreadManager::~ThreadManager() { |
- UnwrapCurrentThread(); |
- pthread_key_delete(key_); |
} |
#endif |
@@ -62,15 +71,8 @@ void ThreadManager::SetCurrentThread(Thread *thread) { |
#if defined(WEBRTC_WIN) |
ThreadManager::ThreadManager() { |
+ main_thread_ref_ = CurrentThreadRef(); |
key_ = TlsAlloc(); |
-#ifndef NO_MAIN_THREAD_WRAPPING |
- WrapCurrentThread(); |
-#endif |
-} |
- |
-ThreadManager::~ThreadManager() { |
- UnwrapCurrentThread(); |
- TlsFree(key_); |
} |
Thread *ThreadManager::CurrentThread() { |
@@ -99,6 +101,10 @@ void ThreadManager::UnwrapCurrentThread() { |
} |
} |
+bool ThreadManager::IsMainThread() { |
+ return IsThreadRefEqual(CurrentThreadRef(), main_thread_ref_); |
+} |
+ |
Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() |
: thread_(Thread::Current()), |
previous_state_(thread_->SetAllowBlockingCalls(false)) { |
@@ -142,6 +148,10 @@ Thread::~Thread() { |
DoDestroy(); |
} |
+bool Thread::IsCurrent() const { |
+ return ThreadManager::Instance()->CurrentThread() == this; |
+} |
+ |
std::unique_ptr<Thread> Thread::CreateWithSocketServer() { |
return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault())); |
} |