Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Unified Diff: webrtc/base/thread.cc

Issue 2833993003: Move autowrap from ThreadManager constructor to Thread::Current. (Closed)
Patch Set: Limit autowrap to main thread only. New method ThreadManager::IsMainThread. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/thread.h ('k') | webrtc/base/thread_darwin.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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()));
}
« no previous file with comments | « webrtc/base/thread.h ('k') | webrtc/base/thread_darwin.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698