From 51b029f22c9d960f3dc184f61f63ceae0cd5e40f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kapa=C5=82a?= Date: Wed, 27 Nov 2024 19:29:02 +0100 Subject: [PATCH 1/4] Update ReanimatedModuleProxy.cpp --- .../NativeModules/ReanimatedModuleProxy.cpp | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp index 12b4b22fe8b..c6cbf5bd0de 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp @@ -219,11 +219,52 @@ void ReanimatedModuleProxy::scheduleOnUI( }); } -jsi::Value ReanimatedModuleProxy::executeOnUIRuntimeSync( - jsi::Runtime &rt, - const jsi::Value &worklet) { - return uiWorkletRuntime_->executeSync(rt, worklet); -} +jsi::Value NativeReanimatedModule::executeOnUIRuntimeSync( + 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( + 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 lock(mutex); + result = std::move(workletResult); + taskCompleted = true; + } + cv.notify_one(); + }); + + // Wait for the UI thread to complete the task + { + std::unique_lock lock(mutex); + cv.wait(lock, [&]() { return taskCompleted; }); + } + + // Return the result to the calling thread + return result; + } jsi::Value ReanimatedModuleProxy::createWorkletRuntime( jsi::Runtime &rt, From 450cecb33f1ea6754abc4983b000b83ef283babc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kapa=C5=82a?= Date: Thu, 28 Nov 2024 09:04:05 +0100 Subject: [PATCH 2/4] Update ReanimatedModuleProxy.cpp --- .../cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp index c6cbf5bd0de..8ab7bc8dcf3 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #ifdef RCT_NEW_ARCH_ENABLED #include From 0b4079d563e245d358f45eae2ead8d68a4902809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kapa=C5=82a?= Date: Thu, 28 Nov 2024 09:04:20 +0100 Subject: [PATCH 3/4] Update packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp Co-authored-by: Marc Rousavy --- .../cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp index 8ab7bc8dcf3..3f33695d084 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp @@ -221,7 +221,7 @@ void ReanimatedModuleProxy::scheduleOnUI( }); } -jsi::Value NativeReanimatedModule::executeOnUIRuntimeSync( +jsi::Value ReanimatedModuleProxy::executeOnUIRuntimeSync( jsi::Runtime &rt, const jsi::Value &worklet) { From 1961aac954a85af2dd820528af3d6feaf35a253c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kapa=C5=82a?= Date: Thu, 28 Nov 2024 09:47:42 +0100 Subject: [PATCH 4/4] Update ReanimatedModuleProxy.cpp --- .../NativeModules/ReanimatedModuleProxy.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp index 3f33695d084..f92e0fc4ec3 100644 --- a/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +++ b/packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp @@ -241,18 +241,20 @@ jsi::Value ReanimatedModuleProxy::executeOnUIRuntimeSync( std::mutex mutex; std::condition_variable cv; bool taskCompleted = false; - jsi::Value result; + std::shared_ptr shareableResult; // Schedule the worklet on the UI thread uiScheduler_->scheduleOnUI([&] { // Execute the worklet within the UI runtime - jsi::Value workletResult = uiWorkletRuntime_->executeSync(rt, worklet); + + auto result = uiWorkletRuntime_->runGuarded(shareableWorklet); + shareableResult = extractShareableOrThrow(uiWorkletRuntime_->getJSIRuntime(), result); + // Lock the mutex, store the result, and notify the waiting thread { std::lock_guard lock(mutex); - result = std::move(workletResult); taskCompleted = true; } cv.notify_one(); @@ -264,8 +266,10 @@ jsi::Value ReanimatedModuleProxy::executeOnUIRuntimeSync( cv.wait(lock, [&]() { return taskCompleted; }); } + jsi::Value workletResult = shareableResult->toJSValue(rt); + // Return the result to the calling thread - return result; + return workletResult; } jsi::Value ReanimatedModuleProxy::createWorkletRuntime(