ui/common/threadpool.c

changeset 1048
60bba3640adb
parent 1042
f3e2811ecf3a
--- a/ui/common/threadpool.c	Mon Jan 12 20:58:41 2026 +0100
+++ b/ui/common/threadpool.c	Mon Jan 12 21:25:51 2026 +0100
@@ -42,6 +42,7 @@
 
 
 static pthread_mutex_t mc_buffer_mutex;
+static pthread_cond_t  mc_buffer_available;
 static CxList *mainthread_call_buffer;
 static volatile int mainthread_call_buffered = 0;
 
@@ -52,6 +53,7 @@
 
 void uic_init_threads(void) {
     pthread_mutex_init(&mc_buffer_mutex, NULL);
+    pthread_cond_init(&mc_buffer_available, NULL);
     mainthread_call_buffer = cxLinkedListCreate(NULL, sizeof(UiMainCall));
 }
 
@@ -65,6 +67,7 @@
     call.func = func;
     call.data = data;
     cxListAdd(mainthread_call_buffer, &call);
+    pthread_cond_signal(&mc_buffer_available);
     pthread_mutex_unlock(&mc_buffer_mutex);
 }
 
@@ -75,8 +78,8 @@
         ui_exec_buffered_mainthread_calls();
     }
 }
-void ui_exec_buffered_mainthread_calls(void) {
-    pthread_mutex_lock(&mc_buffer_mutex);
+
+static void exec_buffered_calls(void) {
     CxIterator i = cxListIterator(mainthread_call_buffer);
     cx_foreach(UiMainCall *, call, i) {
         if(call->func) {
@@ -84,10 +87,34 @@
         }
     }
     cxListClear(mainthread_call_buffer);
+}
+
+void ui_exec_buffered_mainthread_calls(void) {
+    pthread_mutex_lock(&mc_buffer_mutex);
+    exec_buffered_calls();
     pthread_mutex_unlock(&mc_buffer_mutex);
 }
 
-
+UIEXPORT void ui_exec_buffered_mainthread_calls_wait(int timeout) {
+    struct timespec ts;
+    if(timeout > 0) {
+        clock_gettime(CLOCK_REALTIME, &ts);
+        ts.tv_sec += timeout;
+    }
+    
+    pthread_mutex_lock(&mc_buffer_mutex);
+    while(cxListSize(mainthread_call_buffer) == 0) {
+        if(timeout > 0) {
+            if(pthread_cond_timedwait(&mc_buffer_available, &mc_buffer_mutex, &ts)) {
+                break;
+            }
+        } else {
+            pthread_cond_wait(&mc_buffer_available, &mc_buffer_mutex);
+        }
+    }
+    exec_buffered_calls();
+    pthread_mutex_unlock(&mc_buffer_mutex);
+}
 
 UiThreadpool* threadpool_new(int min, int max) {
     UiThreadpool *pool = malloc(sizeof(UiThreadpool));

mercurial