Get rid of thread_darwin file.

This file has been causing problems for the build. ObjC was required for
a few methods because autoreleasepools are necessary on new threads if
those threads will be running objc code.

This CL introduces a workaround by using ObjC runtime C APIs to create
and drain autoreleasepools, but this comes with the cost of relying on
an internal API that may break on future OS/clang releases.

Bug: webrtc:9838
Change-Id: I18e765020c20c096c9ef8d80dfa82375ecb202ff
Reviewed-on: https://webrtc-review.googlesource.com/c/105301
Commit-Queue: Kári Helgason <kthelgason@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25141}
This commit is contained in:
Kári Tristan Helgason
2018-10-12 12:57:49 +02:00
committed by Commit Bot
parent c34cf71d60
commit 62b1345c85
6 changed files with 97 additions and 112 deletions

View File

@ -33,6 +33,32 @@
#include "rtc_base/timeutils.h"
#include "rtc_base/trace_event.h"
#if defined(WEBRTC_MAC)
#include "rtc_base/system/cocoa_threading.h"
/*
* These are forward-declarations for methods that are part of the
* ObjC runtime. They are declared in the private header objc-internal.h.
* These calls are what clang inserts when using @autoreleasepool in ObjC,
* but here they are used directly in order to keep this file C++.
* https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support
*/
extern "C" {
void* objc_autoreleasePoolPush(void);
void objc_autoreleasePoolPop(void* pool);
}
namespace {
class ScopedAutoReleasePool {
public:
ScopedAutoReleasePool() : pool_(objc_autoreleasePoolPush()) {}
~ScopedAutoReleasePool() { objc_autoreleasePoolPop(pool_); }
private:
void* const pool_;
};
} // namespace
#endif
namespace rtc {
ThreadManager* ThreadManager::Instance() {
@ -62,11 +88,12 @@ Thread* Thread::Current() {
}
#if defined(WEBRTC_POSIX)
#if !defined(WEBRTC_MAC)
ThreadManager::ThreadManager() : main_thread_ref_(CurrentThreadRef()) {
#if defined(WEBRTC_MAC)
InitCocoaMultiThreading();
#endif
pthread_key_create(&key_, nullptr);
}
#endif
Thread* ThreadManager::CurrentThread() {
return static_cast<Thread*>(pthread_getspecific(key_));
@ -301,7 +328,6 @@ void Thread::AssertBlockingIsAllowedOnCurrentThread() {
}
// static
#if !defined(WEBRTC_MAC)
#if defined(WEBRTC_WIN)
DWORD WINAPI Thread::PreRun(LPVOID pv) {
#else
@ -310,6 +336,9 @@ void* Thread::PreRun(void* pv) {
ThreadInit* init = static_cast<ThreadInit*>(pv);
ThreadManager::Instance()->SetCurrentThread(init->thread);
rtc::SetCurrentThreadName(init->thread->name_.c_str());
#if defined(WEBRTC_MAC)
ScopedAutoReleasePool pool;
#endif
if (init->runnable) {
init->runnable->Run(init->thread);
} else {
@ -323,7 +352,6 @@ void* Thread::PreRun(void* pv) {
return nullptr;
#endif
}
#endif
void Thread::Run() {
ProcessMessages(kForever);
@ -486,9 +514,6 @@ void Thread::Clear(MessageHandler* phandler,
ClearInternal(phandler, id, removed);
}
#if !defined(WEBRTC_MAC)
// Note that these methods have a separate implementation for mac and ios
// defined in webrtc/rtc_base/thread_darwin.mm.
bool Thread::ProcessMessages(int cmsLoop) {
// Using ProcessMessages with a custom clock for testing and a time greater
// than 0 doesn't work, since it's not guaranteed to advance the custom
@ -499,6 +524,9 @@ bool Thread::ProcessMessages(int cmsLoop) {
int cmsNext = cmsLoop;
while (true) {
#if defined(WEBRTC_MAC)
ScopedAutoReleasePool pool;
#endif
Message msg;
if (!Get(&msg, cmsNext))
return !IsQuitting();
@ -511,7 +539,6 @@ bool Thread::ProcessMessages(int cmsLoop) {
}
}
}
#endif
bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager,
bool need_synchronize_access) {