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 1 commit
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 @@ -219,11 +219,52 @@
});
}

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

Check failure on line 222 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 'NativeReanimatedModule'

Check failure on line 222 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 'NativeReanimatedModule'
Szymon20000 marked this conversation as resolved.
Show resolved Hide resolved
jsi::Runtime &rt,
const jsi::Value &worklet) {

// Ensure that locking is supported
assert(
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;
jsi::Value result;

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

// Execute the worklet within the UI runtime
jsi::Value workletResult = uiWorkletRuntime_->executeSync(rt, worklet);

// Lock the mutex, store the result, and notify the waiting thread
{
std::lock_guard<std::mutex> lock(mutex);
result = std::move(workletResult);
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; });
}

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

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