Skip to content

Commit

Permalink
Fabric: Using RuntimeExecutor in concrete EventBeats
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shergin authored and facebook-github-bot committed Nov 6, 2018
1 parent 0a39cda commit 98685e8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
10 changes: 5 additions & 5 deletions React/Fabric/RCTSurfacePresenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,12 +165,12 @@ - (RCTScheduler *)_scheduler
});
};

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");
Expand Down
8 changes: 4 additions & 4 deletions React/Fabric/Utils/MainRunLoopEventBeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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_;
};

Expand Down
12 changes: 6 additions & 6 deletions React/Fabric/Utils/MainRunLoopEventBeat.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -25,7 +25,7 @@
return;
}

this->blockMessageQueueAndThenBeat();
this->lockExecutorAndBeat();
}
);

Expand All @@ -45,11 +45,11 @@
}

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).

Expand All @@ -61,7 +61,7 @@
mutex2.lock();
mutex3.lock();

messageQueueThread_->runOnQueue([&]() {
runtimeExecutor_([&](jsi::Runtime &runtime) {
mutex1.unlock();
mutex2.lock();
mutex3.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -31,12 +31,12 @@
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;
}
Expand All @@ -58,7 +58,7 @@
#endif

isBusy_ = true;
messageQueueThread_->runOnQueue([=]() mutable {
runtimeExecutor_([=](jsi::Runtime &runtime) mutable {
this->beat();
isBusy_ = false;
#ifndef NDEBUG
Expand Down

0 comments on commit 98685e8

Please sign in to comment.