Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F32508360
D5577.1767076491.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
D5577.1767076491.diff
View Options
diff --git a/native/android/app/src/cpp/GlobalDBSingleton.cpp b/native/android/app/src/cpp/GlobalDBSingleton.cpp
--- a/native/android/app/src/cpp/GlobalDBSingleton.cpp
+++ b/native/android/app/src/cpp/GlobalDBSingleton.cpp
@@ -14,6 +14,10 @@
this->scheduleOrRunCommonImpl(task);
}
+void GlobalDBSingleton::scheduleOrRunCancellable(const taskType task) {
+ this->scheduleOrRunCancellableCommonImpl(task);
+}
+
void GlobalDBSingleton::enableMultithreading() {
this->enableMultithreadingCommonImpl();
}
diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
--- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
+++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
@@ -19,7 +19,7 @@
jsi::Runtime &rt,
std::function<T()> task) {
std::promise<T> promise;
- GlobalDBSingleton::instance.scheduleOrRun([&promise, &task]() {
+ GlobalDBSingleton::instance.scheduleOrRunCancellable([&promise, &task]() {
try {
if constexpr (std::is_void<T>::value) {
task();
diff --git a/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingleton.h b/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingleton.h
--- a/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingleton.h
+++ b/native/cpp/CommonCpp/NativeModules/InternalModules/GlobalDBSingleton.h
@@ -5,6 +5,9 @@
#include <atomic>
namespace comm {
+
+const std::string TASK_CANCELLED_FLAG("TASK_CANCELLED");
+
class GlobalDBSingleton {
std::atomic<bool> multithreadingEnabled;
std::unique_ptr<WorkerThread> databaseThread;
@@ -20,6 +23,22 @@
task();
}
+ void scheduleOrRunCancellableCommonImpl(const taskType task) {
+ if (this->tasksCancelled.load()) {
+ throw std::runtime_error(TASK_CANCELLED_FLAG);
+ }
+ if (this->databaseThread == nullptr) {
+ task();
+ return;
+ }
+ this->databaseThread->scheduleTask([this, task]() {
+ if (this->tasksCancelled.load()) {
+ throw std::runtime_error(TASK_CANCELLED_FLAG);
+ }
+ task();
+ });
+ }
+
void enableMultithreadingCommonImpl() {
if (this->databaseThread == nullptr) {
this->databaseThread = std::make_unique<WorkerThread>("database");
@@ -30,6 +49,7 @@
public:
static GlobalDBSingleton instance;
void scheduleOrRun(const taskType task);
+ void scheduleOrRunCancellable(const taskType task);
void enableMultithreading();
void setTasksCancelled(bool tasksCancelled) {
this->tasksCancelled.store(tasksCancelled);
diff --git a/native/ios/Comm/GlobalDBSingleton.mm b/native/ios/Comm/GlobalDBSingleton.mm
--- a/native/ios/Comm/GlobalDBSingleton.mm
+++ b/native/ios/Comm/GlobalDBSingleton.mm
@@ -21,6 +21,17 @@
});
}
+void GlobalDBSingleton::scheduleOrRunCancellable(const taskType task) {
+ if (NSThread.isMainThread || this->multithreadingEnabled.load()) {
+ this->scheduleOrRunCancellableCommonImpl(task);
+ return;
+ }
+
+ dispatch_async(dispatch_get_main_queue(), ^{
+ this->scheduleOrRunCancellableCommonImpl(task);
+ });
+}
+
void GlobalDBSingleton::enableMultithreading() {
if (NSThread.isMainThread) {
this->enableMultithreadingCommonImpl();
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Dec 30, 6:34 AM (21 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5866579
Default Alt Text
D5577.1767076491.diff (3 KB)
Attached To
Mode
D5577: [native] implement method to run synchronous cancellable task in CommCoreModule
Attached
Detach File
Event Timeline
Log In to Comment