diff --git a/rtc_base/message_queue_unittest.cc b/rtc_base/message_queue_unittest.cc index b7c32e32b0..b31ea6c1b2 100644 --- a/rtc_base/message_queue_unittest.cc +++ b/rtc_base/message_queue_unittest.cc @@ -121,20 +121,6 @@ TEST_F(MessageQueueTest, DiposeHandlerWithPostedMessagePending) { 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 5c4749d974..5df5ede53a 100644 --- a/rtc_base/thread.cc +++ b/rtc_base/thread.cc @@ -567,8 +567,11 @@ bool Thread::IsRunning() { 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_unittest.cc b/rtc_base/thread_unittest.cc index c4b6143ee0..55a0e2d553 100644 --- a/rtc_base/thread_unittest.cc +++ b/rtc_base/thread_unittest.cc @@ -267,15 +267,18 @@ TEST(ThreadTest, Names) { TEST(ThreadTest, Wrap) { Thread* current_thread = Thread::Current(); - 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(); + 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); } TEST(ThreadTest, Invoke) { diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn index 60be6db860..6036a4d781 100644 --- a/sdk/BUILD.gn +++ b/sdk/BUILD.gn @@ -1125,6 +1125,7 @@ if (is_ios || is_mac) { ":peerconnectionfactory_base_objc", ":sdk_unittests_bundle_data", ":sdk_unittests_sources", + "../rtc_base", "//test:test_support", ] ldflags = [ "-all_load" ] @@ -1143,6 +1144,7 @@ if (is_ios || is_mac) { 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 77a88a64d3..9c513762c1 100644 --- a/sdk/objc/unittests/main.mm +++ b/sdk/objc/unittests/main.mm @@ -10,11 +10,14 @@ #import #import +#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); }