mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Fabric: MountingCoordinator::waitForTransaction(...)
Summary: `MountingCoordinator::waitForTransaction()` allows waiting for a new coming transaction synchronously, as efficient as waiting for a mutex. This feature can be used to implement more high-level synchronous rendering APIs. Reviewed By: mdvacca Differential Revision: D17629425 fbshipit-source-id: acd91b941e4d6d43bc4518f332a1604e14506be9
This commit is contained in:
committed by
Facebook Github Bot
parent
86af90fac5
commit
8cf9505bd2
@@ -11,6 +11,8 @@
|
||||
#include <glog/logging.h>
|
||||
#endif
|
||||
|
||||
#include <condition_variable>
|
||||
|
||||
#include <react/mounting/Differentiator.h>
|
||||
#include <react/mounting/ShadowViewMutation.h>
|
||||
#include <react/utils/TimeUtils.h>
|
||||
@@ -31,17 +33,21 @@ SurfaceId MountingCoordinator::getSurfaceId() const {
|
||||
}
|
||||
|
||||
void MountingCoordinator::push(ShadowTreeRevision &&revision) const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
assert(revision.getNumber() > baseRevision_.getNumber());
|
||||
assert(
|
||||
!lastRevision_.has_value() ||
|
||||
revision.getNumber() != lastRevision_->getNumber());
|
||||
assert(revision.getNumber() > baseRevision_.getNumber());
|
||||
assert(
|
||||
!lastRevision_.has_value() ||
|
||||
revision.getNumber() != lastRevision_->getNumber());
|
||||
|
||||
if (!lastRevision_.has_value() ||
|
||||
lastRevision_->getNumber() < revision.getNumber()) {
|
||||
lastRevision_ = std::move(revision);
|
||||
if (!lastRevision_.has_value() ||
|
||||
lastRevision_->getNumber() < revision.getNumber()) {
|
||||
lastRevision_ = std::move(revision);
|
||||
}
|
||||
}
|
||||
|
||||
signal_.notify_all();
|
||||
}
|
||||
|
||||
void MountingCoordinator::revoke() const {
|
||||
@@ -49,6 +55,13 @@ void MountingCoordinator::revoke() const {
|
||||
lastRevision_.reset();
|
||||
}
|
||||
|
||||
bool MountingCoordinator::waitForTransaction(
|
||||
std::chrono::duration<double> timeout) const {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return signal_.wait_for(
|
||||
lock, timeout, [this]() { return lastRevision_.has_value(); });
|
||||
}
|
||||
|
||||
better::optional<MountingTransaction> MountingCoordinator::pullTransaction()
|
||||
const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <better/optional.h>
|
||||
#include <chrono>
|
||||
|
||||
#include <react/mounting/MountingTransaction.h>
|
||||
#include <react/mounting/ShadowTreeRevision.h>
|
||||
@@ -48,10 +49,21 @@ class MountingCoordinator final {
|
||||
* mount.
|
||||
* The method is thread-safe and can be called from any thread.
|
||||
* However, a consumer should always call it on the same thread (e.g. on the
|
||||
* main thread) or ensure sequentiality of mount transaction separately.
|
||||
* main thread) or ensure sequentiality of mount transactions separately.
|
||||
*/
|
||||
better::optional<MountingTransaction> pullTransaction() const;
|
||||
|
||||
/*
|
||||
* Blocks the current thread until a new mounting transaction is available or
|
||||
* after the specified `timeout` duration.
|
||||
* Returns `false` if a timeout occurred before a new transaction available.
|
||||
* Call `pullTransaction` right after the method to retrieve the transaction.
|
||||
* Similarly to `pullTransaction` this method is thread-safe but the consumer
|
||||
* should call it on the same thread (e.g. on the main thread) or ensure
|
||||
* sequentiality of mount transactions separately.
|
||||
*/
|
||||
bool waitForTransaction(std::chrono::duration<double> timeout) const;
|
||||
|
||||
private:
|
||||
friend class ShadowTree;
|
||||
|
||||
@@ -76,6 +88,7 @@ class MountingCoordinator final {
|
||||
mutable ShadowTreeRevision baseRevision_;
|
||||
mutable better::optional<ShadowTreeRevision> lastRevision_{};
|
||||
mutable MountingTransaction::Number number_{0};
|
||||
mutable std::condition_variable signal_;
|
||||
|
||||
#ifdef RN_SHADOW_TREE_INTROSPECTION
|
||||
mutable StubViewTree stubViewTree_; // Protected by `mutex_`.
|
||||
|
||||
Reference in New Issue
Block a user