Skip to content

Commit

Permalink
Fabric: MountingCoordinator::waitForTransaction(...)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shergin authored and facebook-github-bot committed Oct 9, 2019
1 parent 86af90f commit 8cf9505
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
33 changes: 23 additions & 10 deletions ReactCommon/fabric/mounting/MountingCoordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -31,24 +33,35 @@ SurfaceId MountingCoordinator::getSurfaceId() const {
}

void MountingCoordinator::push(ShadowTreeRevision &&revision) const {
std::lock_guard<std::mutex> lock(mutex_);

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);
{
std::lock_guard<std::mutex> lock(mutex_);

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);
}
}

signal_.notify_all();
}

void MountingCoordinator::revoke() const {
std::lock_guard<std::mutex> lock(mutex_);
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_);
Expand Down
15 changes: 14 additions & 1 deletion ReactCommon/fabric/mounting/MountingCoordinator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <better/optional.h>
#include <chrono>

#include <react/mounting/MountingTransaction.h>
#include <react/mounting/ShadowTreeRevision.h>
Expand Down Expand Up @@ -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;

Expand All @@ -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_`.
Expand Down

0 comments on commit 8cf9505

Please sign in to comment.