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}
This commit is contained in:
Niels Möller 2019-05-20 11:15:24 +02:00 committed by Commit Bot
parent e8602067db
commit 0f78c6b28d
10 changed files with 30 additions and 49 deletions

View File

@ -15,10 +15,6 @@ if (is_android) {
import("//build/config/android/rules.gni") import("//build/config/android/rules.gni")
} }
config("rtc_base_chromium_config") {
defines = [ "NO_MAIN_THREAD_WRAPPING" ]
}
config("rtc_base_all_dependent_config") { config("rtc_base_all_dependent_config") {
if (is_ios) { if (is_ios) {
libs = [ libs = [
@ -903,7 +899,6 @@ rtc_static_library("rtc_base") {
if (build_with_chromium) { if (build_with_chromium) {
include_dirs = [ "../../boringssl/src/include" ] include_dirs = [ "../../boringssl/src/include" ]
public_configs += [ ":rtc_base_chromium_config" ]
} else { } else {
sources += [ sources += [
"callback.h", "callback.h",

View File

@ -121,20 +121,6 @@ TEST_F(MessageQueueTest, DiposeHandlerWithPostedMessagePending) {
EXPECT_TRUE(deleted); 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 // Ensure that ProcessAllMessageQueues does its essential function; process
// all messages (both delayed and non delayed) up until the current time, on // all messages (both delayed and non delayed) up until the current time, on
// all registered message queues. // all registered message queues.

View File

@ -78,19 +78,11 @@ Thread* Thread::Current() {
ThreadManager* manager = ThreadManager::Instance(); ThreadManager* manager = ThreadManager::Instance();
Thread* thread = manager->CurrentThread(); 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; return thread;
} }
#if defined(WEBRTC_POSIX) #if defined(WEBRTC_POSIX)
ThreadManager::ThreadManager() : main_thread_ref_(CurrentThreadRef()) { ThreadManager::ThreadManager() {
#if defined(WEBRTC_MAC) #if defined(WEBRTC_MAC)
InitCocoaMultiThreading(); InitCocoaMultiThreading();
#endif #endif
@ -112,8 +104,7 @@ void ThreadManager::SetCurrentThread(Thread* thread) {
#endif #endif
#if defined(WEBRTC_WIN) #if defined(WEBRTC_WIN)
ThreadManager::ThreadManager() ThreadManager::ThreadManager() : key_(TlsAlloc()) {}
: key_(TlsAlloc()), main_thread_ref_(CurrentThreadRef()) {}
Thread* ThreadManager::CurrentThread() { Thread* ThreadManager::CurrentThread() {
return static_cast<Thread*>(TlsGetValue(key_)); return static_cast<Thread*>(TlsGetValue(key_));
@ -142,10 +133,6 @@ void ThreadManager::UnwrapCurrentThread() {
} }
} }
bool ThreadManager::IsMainThread() {
return IsThreadRefEqual(CurrentThreadRef(), main_thread_ref_);
}
Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls()
: thread_(Thread::Current()), : thread_(Thread::Current()),
previous_state_(thread_->SetAllowBlockingCalls(false)) {} previous_state_(thread_->SetAllowBlockingCalls(false)) {}
@ -574,8 +561,11 @@ bool Thread::IsRunning() {
AutoThread::AutoThread() AutoThread::AutoThread()
: Thread(SocketServer::CreateDefault(), /*do_init=*/false) { : Thread(SocketServer::CreateDefault(), /*do_init=*/false) {
DoInit();
if (!ThreadManager::Instance()->CurrentThread()) { 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); ThreadManager::Instance()->SetCurrentThread(this);
} }
} }

View File

@ -102,8 +102,6 @@ class ThreadManager {
Thread* WrapCurrentThread(); Thread* WrapCurrentThread();
void UnwrapCurrentThread(); void UnwrapCurrentThread();
bool IsMainThread();
private: private:
ThreadManager(); ThreadManager();
~ThreadManager(); ~ThreadManager();
@ -116,9 +114,6 @@ class ThreadManager {
const DWORD key_; const DWORD key_;
#endif #endif
// The thread to potentially autowrap.
const PlatformThreadRef main_thread_ref_;
RTC_DISALLOW_COPY_AND_ASSIGN(ThreadManager); RTC_DISALLOW_COPY_AND_ASSIGN(ThreadManager);
}; };

View File

@ -267,15 +267,18 @@ TEST(ThreadTest, Names) {
TEST(ThreadTest, Wrap) { TEST(ThreadTest, Wrap) {
Thread* current_thread = Thread::Current(); Thread* current_thread = Thread::Current();
current_thread->UnwrapCurrent(); ThreadManager::Instance()->SetCurrentThread(nullptr);
CustomThread* cthread = new CustomThread();
EXPECT_TRUE(cthread->WrapCurrent()); {
EXPECT_TRUE(cthread->RunningForTest()); CustomThread cthread;
EXPECT_FALSE(cthread->IsOwned()); EXPECT_TRUE(cthread.WrapCurrent());
cthread->UnwrapCurrent(); EXPECT_EQ(&cthread, Thread::Current());
EXPECT_FALSE(cthread->RunningForTest()); EXPECT_TRUE(cthread.RunningForTest());
delete cthread; EXPECT_FALSE(cthread.IsOwned());
current_thread->WrapCurrent(); cthread.UnwrapCurrent();
EXPECT_FALSE(cthread.RunningForTest());
}
ThreadManager::Instance()->SetCurrentThread(current_thread);
} }
TEST(ThreadTest, Invoke) { TEST(ThreadTest, Invoke) {

View File

@ -18,6 +18,7 @@
#include "rtc_base/logging.h" #include "rtc_base/logging.h"
#include "rtc_base/ssl_adapter.h" #include "rtc_base/ssl_adapter.h"
#include "rtc_base/ssl_stream_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/field_trial.h"
#include "system_wrappers/include/metrics.h" #include "system_wrappers/include/metrics.h"
#include "test/field_trial.h" #include "test/field_trial.h"
@ -123,6 +124,8 @@ int main(int argc, char* argv[]) {
rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, false); rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, false);
rtc::test::RunTestsFromIOSApp(); rtc::test::RunTestsFromIOSApp();
#endif #endif
rtc::AutoThread main_thread;
const int res = RUN_ALL_TESTS(); const int res = RUN_ALL_TESTS();
rtc::CleanupSSL(); rtc::CleanupSSL();

View File

@ -1118,6 +1118,7 @@ if (is_ios || is_mac) {
":peerconnectionfactory_base_objc", ":peerconnectionfactory_base_objc",
":sdk_unittests_bundle_data", ":sdk_unittests_bundle_data",
":sdk_unittests_sources", ":sdk_unittests_sources",
"../rtc_base",
"//test:test_support", "//test:test_support",
] ]
ldflags = [ "-all_load" ] ldflags = [ "-all_load" ]
@ -1136,6 +1137,7 @@ if (is_ios || is_mac) {
deps = [ deps = [
":framework_objc+link", ":framework_objc+link",
":ios_framework_bundle", ":ios_framework_bundle",
"../rtc_base",
"//test:test_support", "//test:test_support",
] ]
} }

View File

@ -10,11 +10,14 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#include "rtc_base/thread.h"
#include "test/ios/coverage_util_ios.h" #include "test/ios/coverage_util_ios.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
rtc::test::ConfigureCoverageReportPath(); rtc::test::ConfigureCoverageReportPath();
rtc::AutoThread main_thread;
@autoreleasepool { @autoreleasepool {
return UIApplicationMain(argc, argv, nil, nil); return UIApplicationMain(argc, argv, nil, nil);
} }

View File

@ -181,6 +181,7 @@ if (is_ios) {
] ]
deps = [ deps = [
":perf_test", ":perf_test",
"../rtc_base",
"../sdk:helpers_objc", "../sdk:helpers_objc",
] ]
configs += [ ":test_support_objc_config" ] configs += [ ":test_support_objc_config" ]

View File

@ -10,6 +10,7 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#include "rtc_base/thread.h"
#include "test/ios/coverage_util_ios.h" #include "test/ios/coverage_util_ios.h"
#include "test/ios/test_support.h" #include "test/ios/test_support.h"
#include "test/testsupport/perf_test.h" #include "test/testsupport/perf_test.h"
@ -73,6 +74,8 @@ static bool g_save_chartjson_result;
- (void)runTests { - (void)runTests {
rtc::test::ConfigureCoverageReportPath(); rtc::test::ConfigureCoverageReportPath();
rtc::AutoThread main_thread;
int exitStatus = g_test_suite(); int exitStatus = g_test_suite();
if (g_save_chartjson_result) { if (g_save_chartjson_result) {