Fabric: Using RuntimeExecutor in concrete EventBeats

Summary: Now we use RuntimeExecutor instead of MessageQueue; that's more clear and remove a dependency from Bridge.

Reviewed By: sahrens

Differential Revision: D12837226

fbshipit-source-id: 0ea3782ce2f49c7f3a91425880863e3b3ea37712
This commit is contained in:
Valentin Shergin
2018-11-06 10:58:49 -08:00
committed by Facebook Github Bot
parent 0a39cda39a
commit 98685e8960
5 changed files with 29 additions and 27 deletions

View File

@@ -30,7 +30,7 @@
#import <fabric/uimanager/ContextContainer.h>
#import "MainRunLoopEventBeat.h"
#import "MessageQueueEventBeat.h"
#import "RuntimeEventBeat.h"
#import "RCTConversions.h"
using namespace facebook::react;
@@ -165,12 +165,12 @@ using namespace facebook::react;
});
};
EventBeatFactory synchronousBeatFactory = [messageQueueThread]() {
return std::make_unique<MainRunLoopEventBeat>(messageQueueThread);
EventBeatFactory synchronousBeatFactory = [runtimeExecutor]() {
return std::make_unique<MainRunLoopEventBeat>(runtimeExecutor);
};
EventBeatFactory asynchronousBeatFactory = [messageQueueThread]() {
return std::make_unique<MessageQueueEventBeat>(messageQueueThread);
EventBeatFactory asynchronousBeatFactory = [runtimeExecutor]() {
return std::make_unique<RuntimeEventBeat>(runtimeExecutor);
};
contextContainer->registerInstance<EventBeatFactory>(synchronousBeatFactory, "synchronous");

View File

@@ -7,7 +7,7 @@
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFRunLoop.h>
#include <cxxreact/MessageQueueThread.h>
#include <fabric/uimanager/FabricUIManager.h>
#include <fabric/events/EventBeat.h>
namespace facebook {
@@ -21,15 +21,15 @@ class MainRunLoopEventBeat final:
public EventBeat {
public:
MainRunLoopEventBeat(std::shared_ptr<MessageQueueThread> messageQueueThread);
MainRunLoopEventBeat(RuntimeExecutor runtimeExecutor);
~MainRunLoopEventBeat();
void induce() const override;
private:
void blockMessageQueueAndThenBeat() const;
void lockExecutorAndBeat() const;
std::shared_ptr<MessageQueueThread> messageQueueThread_;
const RuntimeExecutor runtimeExecutor_;
CFRunLoopObserverRef mainRunLoopObserver_;
};

View File

@@ -11,8 +11,8 @@
namespace facebook {
namespace react {
MainRunLoopEventBeat::MainRunLoopEventBeat(std::shared_ptr<MessageQueueThread> messageQueueThread):
messageQueueThread_(std::move(messageQueueThread)) {
MainRunLoopEventBeat::MainRunLoopEventBeat(RuntimeExecutor runtimeExecutor):
runtimeExecutor_(std::move(runtimeExecutor)) {
mainRunLoopObserver_ =
CFRunLoopObserverCreateWithHandler(
@@ -25,7 +25,7 @@ MainRunLoopEventBeat::MainRunLoopEventBeat(std::shared_ptr<MessageQueueThread> m
return;
}
this->blockMessageQueueAndThenBeat();
this->lockExecutorAndBeat();
}
);
@@ -45,11 +45,11 @@ void MainRunLoopEventBeat::induce() const {
}
RCTExecuteOnMainQueue(^{
this->blockMessageQueueAndThenBeat();
this->lockExecutorAndBeat();
});
}
void MainRunLoopEventBeat::blockMessageQueueAndThenBeat() const {
void MainRunLoopEventBeat::lockExecutorAndBeat() const {
// 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).
@@ -61,7 +61,7 @@ void MainRunLoopEventBeat::blockMessageQueueAndThenBeat() const {
mutex2.lock();
mutex3.lock();
messageQueueThread_->runOnQueue([&]() {
runtimeExecutor_([&](jsi::Runtime &runtime) {
mutex1.unlock();
mutex2.lock();
mutex3.unlock();

View File

@@ -7,26 +7,28 @@
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFRunLoop.h>
#include <cxxreact/MessageQueueThread.h>
#include <fabric/uimanager/FabricUIManager.h>
#include <fabric/events/EventBeat.h>
namespace facebook {
namespace react {
/*
* Event beat that associated with MessageQueueThread.
* Event beat associated with JavaScript runtime.
* The beat is called on `RuntimeExecutor`'s thread induced by the main thread
* event loop.
*/
class MessageQueueEventBeat:
class RuntimeEventBeat:
public EventBeat {
public:
MessageQueueEventBeat(const std::shared_ptr<MessageQueueThread> &messageQueueThread);
~MessageQueueEventBeat();
RuntimeEventBeat(RuntimeExecutor runtimeExecutor);
~RuntimeEventBeat();
void induce() const override;
private:
const std::shared_ptr<MessageQueueThread> messageQueueThread_;
const RuntimeExecutor runtimeExecutor_;
CFRunLoopObserverRef mainRunLoopObserver_;
mutable std::atomic<bool> isBusy_ {false};
};

View File

@@ -5,13 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/
#include "MessageQueueEventBeat.h"
#include "RuntimeEventBeat.h"
namespace facebook {
namespace react {
MessageQueueEventBeat::MessageQueueEventBeat(const std::shared_ptr<MessageQueueThread> &messageQueueThread):
messageQueueThread_(messageQueueThread) {
RuntimeEventBeat::RuntimeEventBeat(RuntimeExecutor runtimeExecutor):
runtimeExecutor_(std::move(runtimeExecutor)) {
mainRunLoopObserver_ =
CFRunLoopObserverCreateWithHandler(
@@ -31,12 +31,12 @@ MessageQueueEventBeat::MessageQueueEventBeat(const std::shared_ptr<MessageQueueT
CFRunLoopAddObserver(CFRunLoopGetMain(), mainRunLoopObserver_, kCFRunLoopCommonModes);
}
MessageQueueEventBeat::~MessageQueueEventBeat() {
RuntimeEventBeat::~RuntimeEventBeat() {
CFRunLoopRemoveObserver(CFRunLoopGetMain(), mainRunLoopObserver_, kCFRunLoopCommonModes);
CFRelease(mainRunLoopObserver_);
}
void MessageQueueEventBeat::induce() const {
void RuntimeEventBeat::induce() const {
if (!isRequested_ || isBusy_) {
return;
}
@@ -58,7 +58,7 @@ void MessageQueueEventBeat::induce() const {
#endif
isBusy_ = true;
messageQueueThread_->runOnQueue([=]() mutable {
runtimeExecutor_([=](jsi::Runtime &runtime) mutable {
this->beat();
isBusy_ = false;
#ifndef NDEBUG