diff --git a/native/android/app/src/cpp/GlobalDBSingletonJNIHelper.cpp b/native/android/app/src/cpp/GlobalDBSingletonJNIHelper.cpp new file mode 100644 --- /dev/null +++ b/native/android/app/src/cpp/GlobalDBSingletonJNIHelper.cpp @@ -0,0 +1,33 @@ +#include +#include + +namespace comm { +void GlobalDBSingletonJNIHelper::scheduleOrRun( + facebook::jni::alias_ref jThis, + facebook::jni::alias_ref task) { + auto globalTaskRef = facebook::jni::make_global(task); + GlobalDBSingleton::instance.scheduleOrRun( + [globalTaskRef = std::move(globalTaskRef)]() mutable { + auto runTask = [globalTaskRef = std::move(globalTaskRef)]() mutable { + globalTaskRef->run(); + globalTaskRef.release(); + }; + facebook::jni::ThreadScope::WithClassLoader(std::move(runTask)); + }); +} + +void GlobalDBSingletonJNIHelper::enableMultithreading( + facebook::jni::alias_ref jThis) { + GlobalDBSingleton::instance.enableMultithreading(); +} + +void GlobalDBSingletonJNIHelper::registerNatives() { + javaClassStatic()->registerNatives({ + makeNativeMethod( + "scheduleOrRun", GlobalDBSingletonJNIHelper::scheduleOrRun), + makeNativeMethod( + "enableMultithreading", + GlobalDBSingletonJNIHelper::enableMultithreading), + }); +} +} // namespace comm diff --git a/native/android/app/src/cpp/jsiInstaller.cpp b/native/android/app/src/cpp/jsiInstaller.cpp --- a/native/android/app/src/cpp/jsiInstaller.cpp +++ b/native/android/app/src/cpp/jsiInstaller.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -57,5 +58,6 @@ comm::GlobalNetworkSingletonJNIHelper::registerNatives(); comm::ThreadOperationsJNIHelper::registerNatives(); comm::MessageOperationsUtilitiesJNIHelper::registerNatives(); + comm::GlobalDBSingletonJNIHelper::registerNatives(); }); } diff --git a/native/android/app/src/main/java/app/comm/android/fbjni/GlobalDBSingleton.java b/native/android/app/src/main/java/app/comm/android/fbjni/GlobalDBSingleton.java new file mode 100644 --- /dev/null +++ b/native/android/app/src/main/java/app/comm/android/fbjni/GlobalDBSingleton.java @@ -0,0 +1,6 @@ +package app.comm.android.fbjni; + +public class GlobalDBSingleton { + public static native void scheduleOrRun(Runnable task); + public static native void enableMultithreading(); +} diff --git a/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingletonJNIHelper.h b/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingletonJNIHelper.h new file mode 100644 --- /dev/null +++ b/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingletonJNIHelper.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +#include + +namespace comm { +class GlobalDBSingletonJNIHelper + : public facebook::jni::JavaClass { +public: + static auto constexpr kJavaDescriptor = + "Lapp/comm/android/fbjni/GlobalDBSingleton;"; + static void scheduleOrRun( + facebook::jni::alias_ref jThis, + facebook::jni::alias_ref task); + static void enableMultithreading( + facebook::jni::alias_ref jThis); + static void registerNatives(); +}; +} // namespace comm