Skip to content

Commit

Permalink
Revert D64287526: batch sync events based on event beats
Browse files Browse the repository at this point in the history
Differential Revision:
D64287526

Original commit changeset: a5dc3b643ef1

Original Phabricator Diff: D64287526

fbshipit-source-id: 9c967bd2573dc69bfbde69a7a79046e9d58fcb21
  • Loading branch information
Cody Pizzaia authored and facebook-github-bot committed Oct 24, 2024
1 parent 538bff7 commit 020e1b6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ void EventBeat::request() const {
isRequested_ = true;
}

void EventBeat::requestSynchronous() const {
isSynchronousRequested_ = true;
request();
}

void EventBeat::setBeatCallback(BeatCallback beatCallback) {
beatCallback_ = std::move(beatCallback);
}
Expand All @@ -38,7 +33,7 @@ void EventBeat::induce() const {
isRequested_ = false;
isBeatCallbackScheduled_ = true;

auto beat = std::function<void(jsi::Runtime&)>(
runtimeScheduler_.scheduleWork(
[this, ownerBox = ownerBox_](jsi::Runtime& runtime) {
auto owner = ownerBox->owner.lock();
if (!owner) {
Expand All @@ -50,13 +45,6 @@ void EventBeat::induce() const {
beatCallback_(runtime);
}
});

if (isSynchronousRequested_) {
isSynchronousRequested_ = false;
runtimeScheduler_.executeNowOnTheSameThread(std::move(beat));
} else {
runtimeScheduler_.scheduleWork(std::move(beat));
}
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ class EventBeat {
*/
virtual void request() const;

/*
* Communicates to the Beat that a consumer is waiting synchronously for the
* coming beat.
*/
virtual void requestSynchronous() const;

/*
* The callback is must be called on the proper thread.
*/
Expand All @@ -99,7 +93,6 @@ class EventBeat {
private:
RuntimeScheduler& runtimeScheduler_;
mutable std::atomic<bool> isBeatCallbackScheduled_{false};
mutable std::atomic<bool> isSynchronousRequested_{false};
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ namespace facebook::react {
EventDispatcher::EventDispatcher(
const EventQueueProcessor& eventProcessor,
std::unique_ptr<EventBeat> eventBeat,
RuntimeScheduler& runtimeScheduler,
StatePipe statePipe,
std::weak_ptr<EventLogger> eventLogger)
: eventQueue_(EventQueue(eventProcessor, std::move(eventBeat))),
: eventQueue_(
EventQueue(eventProcessor, std::move(eventBeat), runtimeScheduler)),
statePipe_(std::move(statePipe)),
eventLogger_(std::move(eventLogger)) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class EventDispatcher {
EventDispatcher(
const EventQueueProcessor& eventProcessor,
std::unique_ptr<EventBeat> eventBeat,
RuntimeScheduler& runtimeScheduler,
StatePipe statePipe,
std::weak_ptr<EventLogger> eventLogger);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ namespace facebook::react {

EventQueue::EventQueue(
EventQueueProcessor eventProcessor,
std::unique_ptr<EventBeat> eventBeat)
std::unique_ptr<EventBeat> eventBeat,
RuntimeScheduler& runtimeScheduler)
: eventProcessor_(std::move(eventProcessor)),
eventBeat_(std::move(eventBeat)) {
eventBeat_(std::move(eventBeat)),
runtimeScheduler_(&runtimeScheduler) {
eventBeat_->setBeatCallback(
[this](jsi::Runtime& runtime) { onBeat(runtime); });
}
Expand Down Expand Up @@ -77,14 +79,26 @@ void EventQueue::enqueueStateUpdate(StateUpdate&& stateUpdate) const {
}

void EventQueue::onEnqueue() const {
if (synchronousAccessRequested_) {
// Sync flush has been scheduled, no need to request access to the runtime.
return;
}
eventBeat_->request();
}

void EventQueue::experimental_flushSync() const {
eventBeat_->requestSynchronous();
synchronousAccessRequested_ = true;
runtimeScheduler_->executeNowOnTheSameThread([this](jsi::Runtime& runtime) {
synchronousAccessRequested_ = false;
onBeat(runtime);
});
}

void EventQueue::onBeat(jsi::Runtime& runtime) const {
if (synchronousAccessRequested_) {
// Sync flush has been scheduled, let's yield to it.
return;
}
flushStateUpdates();
flushEvents(runtime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class EventQueue {
public:
EventQueue(
EventQueueProcessor eventProcessor,
std::unique_ptr<EventBeat> eventBeat);
std::unique_ptr<EventBeat> eventBeat,
RuntimeScheduler& runtimeScheduler);

/*
* Enqueues and (probably later) dispatch a given event.
Expand Down Expand Up @@ -74,6 +75,10 @@ class EventQueue {
mutable std::vector<RawEvent> eventQueue_;
mutable std::vector<StateUpdate> stateUpdateQueue_;
mutable std::mutex queueMutex_;

// TODO: T183075253
RuntimeScheduler* runtimeScheduler_;
mutable std::atomic_bool synchronousAccessRequested_{false};
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Scheduler::Scheduler(
EventQueueProcessor(
eventPipe, eventPipeConclusion, statePipe, eventPerformanceLogger_),
std::move(eventBeat),
*runtimeScheduler,
statePipe,
eventPerformanceLogger_);

Expand Down

0 comments on commit 020e1b6

Please sign in to comment.