Files
react-native/ReactCommon/fabric/events/EventBeatBasedExecutor.cpp
Valentin Shergin b91f6d1e93 Fabric: Using EventBeatBasedExecutor to ensure threading during installing UIManager
Summary: This is the last step before making JSIUIManagerInstaller a direct dependency of UIManager (and making UIManager installation process completely seamless/platform-agnostic).

Reviewed By: mdvacca

Differential Revision: D9995781

fbshipit-source-id: 6f8c7177495b01ebaac1dbe330f49dce2e2a552c
2018-09-26 10:18:38 -07:00

80 lines
1.5 KiB
C++

/**
* 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 <cassert>
#include "EventBeatBasedExecutor.h"
#include <cassert>
namespace facebook {
namespace react {
using Mode = EventBeatBasedExecutor::Mode;
EventBeatBasedExecutor::EventBeatBasedExecutor(std::unique_ptr<EventBeat> eventBeat):
eventBeat_(std::move(eventBeat)) {
eventBeat_->setBeatCallback(std::bind(&EventBeatBasedExecutor::onBeat, this));
}
void EventBeatBasedExecutor::operator()(Routine routine, Mode mode) const {
if (mode == Mode::Asynchronous) {
execute({.routine = std::move(routine)});
return;
}
std::mutex mutex;
mutex.lock();
execute({
.routine = std::move(routine),
.callback = [&mutex]() {
mutex.unlock();
}
});
mutex.lock();
}
void EventBeatBasedExecutor::execute(Task task) const {
{
std::lock_guard<std::mutex> lock(mutex_);
tasks_.push_back(std::move(task));
}
eventBeat_->request();
eventBeat_->induce();
}
void EventBeatBasedExecutor::onBeat() const {
std::vector<Task> tasks;
{
std::lock_guard<std::mutex> lock(mutex_);
if (tasks_.size() == 0) {
return;
}
tasks = std::move(tasks_);
assert(tasks_.size() == 0);
}
for (const auto task : tasks) {
task.routine();
if (task.callback) {
task.callback();
}
}
}
} // namespace react
} // namespace facebook