Revert "Delete NO_MAIN_THREAD_WRAPPING preprocessor define."
This reverts commit 0f78c6b28dbc0c9caa555ce89ce91b0f08c510ea.
Reason for revert: Breaks downstream tests.
Original change's description:
> Delete NO_MAIN_THREAD_WRAPPING preprocessor define.
>
> Since many tests rely on rtc::Thread::Current(), add an
> explicit rtc::AutoThread in the main() function used by tests.
>
> Bug: webrtc:9714
> Change-Id: Id82121967c9621fe1c2945846009c48139fa57da
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/39680
> Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> Commit-Queue: Niels Moller <nisse@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#28000}
TBR=kwiberg@webrtc.org,nisse@webrtc.org,kthelgason@webrtc.org
Change-Id: Iff939bb0d5ad0ea01b953321993733bb56c9070b
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: webrtc:9714
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/137512
Reviewed-by: Niels Moller <nisse@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28001}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index 418af60..72b7229 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -15,6 +15,10 @@
import("//build/config/android/rules.gni")
}
+config("rtc_base_chromium_config") {
+ defines = [ "NO_MAIN_THREAD_WRAPPING" ]
+}
+
config("rtc_base_all_dependent_config") {
if (is_ios) {
libs = [
@@ -899,6 +903,7 @@
if (build_with_chromium) {
include_dirs = [ "../../boringssl/src/include" ]
+ public_configs += [ ":rtc_base_chromium_config" ]
} else {
sources += [
"callback.h",
diff --git a/rtc_base/message_queue_unittest.cc b/rtc_base/message_queue_unittest.cc
index b31ea6c..b7c32e3 100644
--- a/rtc_base/message_queue_unittest.cc
+++ b/rtc_base/message_queue_unittest.cc
@@ -121,6 +121,20 @@
EXPECT_TRUE(deleted);
}
+struct UnwrapMainThreadScope {
+ UnwrapMainThreadScope() : rewrap_(Thread::Current() != nullptr) {
+ if (rewrap_)
+ ThreadManager::Instance()->UnwrapCurrentThread();
+ }
+ ~UnwrapMainThreadScope() {
+ if (rewrap_)
+ ThreadManager::Instance()->WrapCurrentThread();
+ }
+
+ private:
+ bool rewrap_;
+};
+
// Ensure that ProcessAllMessageQueues does its essential function; process
// all messages (both delayed and non delayed) up until the current time, on
// all registered message queues.
diff --git a/rtc_base/thread.cc b/rtc_base/thread.cc
index 0caa856..be0d15d 100644
--- a/rtc_base/thread.cc
+++ b/rtc_base/thread.cc
@@ -78,11 +78,19 @@
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(SocketServer::CreateDefault());
+ thread->WrapCurrentWithThreadManager(manager, true);
+ }
+#endif
+
return thread;
}
#if defined(WEBRTC_POSIX)
-ThreadManager::ThreadManager() {
+ThreadManager::ThreadManager() : main_thread_ref_(CurrentThreadRef()) {
#if defined(WEBRTC_MAC)
InitCocoaMultiThreading();
#endif
@@ -104,7 +112,8 @@
#endif
#if defined(WEBRTC_WIN)
-ThreadManager::ThreadManager() : key_(TlsAlloc()) {}
+ThreadManager::ThreadManager()
+ : key_(TlsAlloc()), main_thread_ref_(CurrentThreadRef()) {}
Thread* ThreadManager::CurrentThread() {
return static_cast<Thread*>(TlsGetValue(key_));
@@ -133,6 +142,10 @@
}
}
+bool ThreadManager::IsMainThread() {
+ return IsThreadRefEqual(CurrentThreadRef(), main_thread_ref_);
+}
+
Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls()
: thread_(Thread::Current()),
previous_state_(thread_->SetAllowBlockingCalls(false)) {}
@@ -561,11 +574,8 @@
AutoThread::AutoThread()
: Thread(SocketServer::CreateDefault(), /*do_init=*/false) {
+ DoInit();
if (!ThreadManager::Instance()->CurrentThread()) {
- // DoInit registers with MessageQueueManager. Do that only if we intend to
- // be rtc::Thread::Current(), otherwise ProcessAllMessageQueuesInternal will
- // post a message to a queue that no running thread is serving.
- DoInit();
ThreadManager::Instance()->SetCurrentThread(this);
}
}
diff --git a/rtc_base/thread.h b/rtc_base/thread.h
index 6b5bdf4..2b4d020 100644
--- a/rtc_base/thread.h
+++ b/rtc_base/thread.h
@@ -102,6 +102,8 @@
Thread* WrapCurrentThread();
void UnwrapCurrentThread();
+ bool IsMainThread();
+
private:
ThreadManager();
~ThreadManager();
@@ -114,6 +116,9 @@
const DWORD key_;
#endif
+ // The thread to potentially autowrap.
+ const PlatformThreadRef main_thread_ref_;
+
RTC_DISALLOW_COPY_AND_ASSIGN(ThreadManager);
};
diff --git a/rtc_base/thread_unittest.cc b/rtc_base/thread_unittest.cc
index 55a0e2d..c4b6143 100644
--- a/rtc_base/thread_unittest.cc
+++ b/rtc_base/thread_unittest.cc
@@ -267,18 +267,15 @@
TEST(ThreadTest, Wrap) {
Thread* current_thread = Thread::Current();
- ThreadManager::Instance()->SetCurrentThread(nullptr);
-
- {
- CustomThread cthread;
- EXPECT_TRUE(cthread.WrapCurrent());
- EXPECT_EQ(&cthread, Thread::Current());
- EXPECT_TRUE(cthread.RunningForTest());
- EXPECT_FALSE(cthread.IsOwned());
- cthread.UnwrapCurrent();
- EXPECT_FALSE(cthread.RunningForTest());
- }
- ThreadManager::Instance()->SetCurrentThread(current_thread);
+ current_thread->UnwrapCurrent();
+ CustomThread* cthread = new CustomThread();
+ EXPECT_TRUE(cthread->WrapCurrent());
+ EXPECT_TRUE(cthread->RunningForTest());
+ EXPECT_FALSE(cthread->IsOwned());
+ cthread->UnwrapCurrent();
+ EXPECT_FALSE(cthread->RunningForTest());
+ delete cthread;
+ current_thread->WrapCurrent();
}
TEST(ThreadTest, Invoke) {
diff --git a/rtc_base/unittest_main.cc b/rtc_base/unittest_main.cc
index 8cc1e96..fe3e9e4 100644
--- a/rtc_base/unittest_main.cc
+++ b/rtc_base/unittest_main.cc
@@ -18,7 +18,6 @@
#include "rtc_base/logging.h"
#include "rtc_base/ssl_adapter.h"
#include "rtc_base/ssl_stream_adapter.h"
-#include "rtc_base/thread.h"
#include "system_wrappers/include/field_trial.h"
#include "system_wrappers/include/metrics.h"
#include "test/field_trial.h"
@@ -124,8 +123,6 @@
rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, false);
rtc::test::RunTestsFromIOSApp();
#endif
-
- rtc::AutoThread main_thread;
const int res = RUN_ALL_TESTS();
rtc::CleanupSSL();
diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn
index c694ec6..4cd3783 100644
--- a/sdk/BUILD.gn
+++ b/sdk/BUILD.gn
@@ -1118,7 +1118,6 @@
":peerconnectionfactory_base_objc",
":sdk_unittests_bundle_data",
":sdk_unittests_sources",
- "../rtc_base",
"//test:test_support",
]
ldflags = [ "-all_load" ]
@@ -1137,7 +1136,6 @@
deps = [
":framework_objc+link",
":ios_framework_bundle",
- "../rtc_base",
"//test:test_support",
]
}
diff --git a/sdk/objc/unittests/main.mm b/sdk/objc/unittests/main.mm
index 9c51376..77a88a6 100644
--- a/sdk/objc/unittests/main.mm
+++ b/sdk/objc/unittests/main.mm
@@ -10,14 +10,11 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
-#include "rtc_base/thread.h"
#include "test/ios/coverage_util_ios.h"
int main(int argc, char* argv[]) {
rtc::test::ConfigureCoverageReportPath();
- rtc::AutoThread main_thread;
-
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, nil);
}
diff --git a/test/BUILD.gn b/test/BUILD.gn
index ee3c57c..d4706b8 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -181,7 +181,6 @@
]
deps = [
":perf_test",
- "../rtc_base",
"../sdk:helpers_objc",
]
configs += [ ":test_support_objc_config" ]
diff --git a/test/ios/test_support.mm b/test/ios/test_support.mm
index d034bfe..8600597 100644
--- a/test/ios/test_support.mm
+++ b/test/ios/test_support.mm
@@ -10,7 +10,6 @@
#import <UIKit/UIKit.h>
-#include "rtc_base/thread.h"
#include "test/ios/coverage_util_ios.h"
#include "test/ios/test_support.h"
#include "test/testsupport/perf_test.h"
@@ -74,8 +73,6 @@
- (void)runTests {
rtc::test::ConfigureCoverageReportPath();
- rtc::AutoThread main_thread;
-
int exitStatus = g_test_suite();
if (g_save_chartjson_result) {