mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
42d232901c
Summary: This should fix a race condition uncovered by ASAN build that results in `m_shutdown` being referenced after destruction. This mirrors a similar fix made in react-native-macos: https://github.com/microsoft/react-native-macos/commit/ea7767a211e5d1e5b18171c79404553b712539c7 Changelog: [Internal] Reviewed By: shergin Differential Revision: D24263156 fbshipit-source-id: 5fb1e7053c107dfb7050242fce8fcf0435e5592c
115 lines
2.9 KiB
Plaintext
115 lines
2.9 KiB
Plaintext
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "RCTMessageThread.h"
|
|
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
|
|
#import <React/RCTCxxUtils.h>
|
|
#import <React/RCTUtils.h>
|
|
|
|
// A note about the implementation: This class is not used
|
|
// generically. It's a thin wrapper around a run loop which
|
|
// implements a C++ interface, for use by the C++ xplat bridge code.
|
|
// This means it can make certain non-generic assumptions. In
|
|
// particular, the sync functions are only used for bridge setup and
|
|
// teardown, and quitSynchronous is guaranteed to be called.
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
RCTMessageThread::RCTMessageThread(NSRunLoop *runLoop, RCTJavaScriptCompleteBlock errorBlock)
|
|
: m_cfRunLoop([runLoop getCFRunLoop]), m_errorBlock(errorBlock), m_shutdown(false)
|
|
{
|
|
CFRetain(m_cfRunLoop);
|
|
}
|
|
|
|
RCTMessageThread::~RCTMessageThread()
|
|
{
|
|
RCTAssert(m_shutdown, @"RCTMessageThread: quitSynchronous() not called before destructor");
|
|
CFRelease(m_cfRunLoop);
|
|
}
|
|
|
|
// This is analogous to dispatch_async
|
|
void RCTMessageThread::runAsync(std::function<void()> func)
|
|
{
|
|
CFRunLoopPerformBlock(m_cfRunLoop, kCFRunLoopCommonModes, ^{
|
|
// Create an autorelease pool each run loop to prevent memory footprint from growing too large, which can lead to
|
|
// performance problems.
|
|
@autoreleasepool {
|
|
func();
|
|
}
|
|
});
|
|
CFRunLoopWakeUp(m_cfRunLoop);
|
|
}
|
|
|
|
// This is analogous to dispatch_sync
|
|
void RCTMessageThread::runSync(std::function<void()> func)
|
|
{
|
|
if (m_cfRunLoop == CFRunLoopGetCurrent()) {
|
|
func();
|
|
return;
|
|
}
|
|
|
|
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
|
|
runAsync([func = std::make_shared<std::function<void()>>(std::move(func)), &sema] {
|
|
(*func)();
|
|
dispatch_semaphore_signal(sema);
|
|
});
|
|
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
|
|
}
|
|
|
|
void RCTMessageThread::tryFunc(const std::function<void()> &func)
|
|
{
|
|
NSError *error = tryAndReturnError(func);
|
|
if (error) {
|
|
m_errorBlock(error);
|
|
}
|
|
}
|
|
|
|
void RCTMessageThread::runOnQueue(std::function<void()> &&func)
|
|
{
|
|
if (m_shutdown) {
|
|
return;
|
|
}
|
|
runAsync([sharedThis = shared_from_this(), func = std::make_shared<std::function<void()>>(std::move(func))] {
|
|
if (!sharedThis->m_shutdown) {
|
|
sharedThis->tryFunc(*func);
|
|
}
|
|
});
|
|
}
|
|
|
|
void RCTMessageThread::runOnQueueSync(std::function<void()> &&func)
|
|
{
|
|
if (m_shutdown) {
|
|
return;
|
|
}
|
|
runSync([sharedThis = shared_from_this(), func = std::move(func)] {
|
|
if (!sharedThis->m_shutdown) {
|
|
sharedThis->tryFunc(func);
|
|
}
|
|
});
|
|
}
|
|
|
|
void RCTMessageThread::quitSynchronous()
|
|
{
|
|
m_shutdown = true;
|
|
runSync([] {});
|
|
CFRunLoopStop(m_cfRunLoop);
|
|
}
|
|
|
|
void RCTMessageThread::setRunLoop(NSRunLoop *runLoop)
|
|
{
|
|
CFRelease(m_cfRunLoop);
|
|
m_cfRunLoop = [runLoop getCFRunLoop];
|
|
CFRetain(m_cfRunLoop);
|
|
}
|
|
|
|
}
|
|
}
|