Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Reanimated runtime only from UI thread #6770

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <reanimated/Tools/CollectionUtils.h>
#include <reanimated/Tools/FeaturesConfig.h>
#include <unordered_map>
#include <mutex>
#include <condition_variable>

#ifdef RCT_NEW_ARCH_ENABLED
#include <reanimated/Fabric/ReanimatedCommitShadowNode.h>
Expand Down Expand Up @@ -220,10 +222,55 @@
}

jsi::Value ReanimatedModuleProxy::executeOnUIRuntimeSync(
jsi::Runtime &rt,
const jsi::Value &worklet) {
return uiWorkletRuntime_->executeSync(rt, worklet);
}
jsi::Runtime &rt,
const jsi::Value &worklet) {

// Ensure that locking is supported
assert(
supportsLocking_ &&

Check failure on line 230 in packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp

View workflow job for this annotation

GitHub Actions / build (apps/fabric-example)

use of undeclared identifier 'supportsLocking_'

Check failure on line 230 in packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp

View workflow job for this annotation

GitHub Actions / build (apps/paper-example)

use of undeclared identifier 'supportsLocking_'
("[Reanimated] Runtime \"" + name_ + "\" doesn't support locking.")
.c_str());

// Extract the shareable worklet, throwing an error if invalid
auto shareableWorklet = extractShareableOrThrow<ShareableWorklet>(
rt,
worklet,
"[Reanimated] Only worklets can be executed synchronously on the UI runtime.");

// Synchronization primitives
std::mutex mutex;
std::condition_variable cv;
bool taskCompleted = false;
std::shared_ptr<Shareable> shareableResult;

// Schedule the worklet on the UI thread
uiScheduler_->scheduleOnUI([&] {

// Execute the worklet within the UI runtime

auto result = uiWorkletRuntime_->runGuarded(shareableWorklet);
shareableResult = extractShareableOrThrow(uiWorkletRuntime_->getJSIRuntime(), result);


// Lock the mutex, store the result, and notify the waiting thread
{
std::lock_guard<std::mutex> lock(mutex);
taskCompleted = true;
}
cv.notify_one();
});

// Wait for the UI thread to complete the task
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&]() { return taskCompleted; });
}

jsi::Value workletResult = shareableResult->toJSValue(rt);

// Return the result to the calling thread
return workletResult;
}

jsi::Value ReanimatedModuleProxy::createWorkletRuntime(
jsi::Runtime &rt,
Expand Down
Loading