Files
react-native/React/Fabric/Utils/MainRunLoopEventBeat.mm
T
Valentin Shergin c80192c2ab Fabric: EventBeat::Owner to help with crashes
Summary:
The purpose of `EventBeat` is handling an asynchronous callback to itself which is being delivered on some different thread. That brings a challenge of ensuring that the `EventBeat` object stays valid during the timeframe of callback execution. The concept of Owner helps with that.
The owner is a shared pointer that retains (probably indirectly) the `EventBeat` object. To ensure the correctness of the call, `EventBeat` retains the owner (practically creating a retain cycle) during executing the callback. In case if the pointer to the owner already null, `EventBeat` skips executing the callback.
It's impossible to retain itself directly or refer to the shared pointer to itself from a constructor. `OwnerBox` is designed to work around this issue; it allows to store the pointer later, right after the creation of some other object that owns an `EventBeat`.

Reviewed By: JoshuaGross

Differential Revision: D17128549

fbshipit-source-id: 7ed34fd865430975157fd362f51c4a3d64214430
2019-08-30 18:24:27 -07:00

87 lines
2.0 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.
#import "MainRunLoopEventBeat.h"
#import <React/RCTUtils.h>
#import <mutex>
namespace facebook {
namespace react {
MainRunLoopEventBeat::MainRunLoopEventBeat(EventBeat::SharedOwnerBox const &ownerBox, RuntimeExecutor runtimeExecutor)
: EventBeat(ownerBox), runtimeExecutor_(std::move(runtimeExecutor))
{
mainRunLoopObserver_ = CFRunLoopObserverCreateWithHandler(
NULL /* allocator */,
kCFRunLoopBeforeWaiting /* activities */,
true /* repeats */,
0 /* order */,
^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
if (!this->isRequested_) {
return;
}
this->lockExecutorAndBeat();
});
assert(mainRunLoopObserver_);
CFRunLoopAddObserver(CFRunLoopGetMain(), mainRunLoopObserver_, kCFRunLoopCommonModes);
}
MainRunLoopEventBeat::~MainRunLoopEventBeat()
{
CFRunLoopRemoveObserver(CFRunLoopGetMain(), mainRunLoopObserver_, kCFRunLoopCommonModes);
CFRelease(mainRunLoopObserver_);
}
void MainRunLoopEventBeat::induce() const
{
if (!this->isRequested_) {
return;
}
RCTExecuteOnMainQueue(^{
this->lockExecutorAndBeat();
});
}
void MainRunLoopEventBeat::lockExecutorAndBeat() const
{
auto owner = ownerBox_->owner.lock();
if (!owner) {
return;
}
// Note: We need the third mutex to get back to the main thread before
// the lambda is finished (because all mutexes are allocated on the stack).
std::mutex mutex1;
std::mutex mutex2;
std::mutex mutex3;
mutex1.lock();
mutex2.lock();
mutex3.lock();
jsi::Runtime *runtimePtr;
runtimeExecutor_([&](jsi::Runtime &runtime) {
runtimePtr = &runtime;
mutex1.unlock();
// `beat` is called somewhere here.
mutex2.lock();
mutex3.unlock();
});
mutex1.lock();
beat(*runtimePtr);
mutex2.unlock();
mutex3.lock();
}
} // namespace react
} // namespace facebook