add conditional variable API newapi

Wed, 23 Oct 2024 15:57:59 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 23 Oct 2024 15:57:59 +0200
branch
newapi
changeset 347
6f0d0b0d97f5
parent 346
a20213cb3d2f
child 348
a4fdec2fc353

add conditional variable API

application/main.c file | annotate | diff | comparison | revisions
ui/common/objs.mk file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Wed Oct 23 15:05:20 2024 +0200
+++ b/application/main.c	Wed Oct 23 15:57:59 2024 +0200
@@ -51,6 +51,35 @@
 
 UIWIDGET tabview;
 
+static UiCondVar *cond;
+static int thr_end = 0;
+static int thr_started = 0;
+
+int threadfunc(void *data) {
+    printf("thr wait for data...\n");
+    ui_condvar_wait(cond);
+    printf("thr data received: {%s} [%d]\n", cond->data, cond->intdata);
+    ui_condvar_destroy(cond);
+    cond = NULL;
+    
+    return 0;
+}
+
+void action_start_thread(UiEvent *event, void *data) {
+    if(!thr_started) {
+        cond = ui_condvar_create();
+        ui_job(event->obj, threadfunc, NULL, NULL, NULL);
+        thr_started = 1;
+    }
+}
+
+void action_notify_thread(UiEvent *event, void *data) {
+    if(!thr_end) {
+        ui_condvar_signal(cond, "hello thread", 123);
+        thr_end = 1;
+    }
+}
+
 void action_menu(UiEvent *event, void *userdata) {
     
 }
@@ -252,8 +281,8 @@
             }
         }
         ui_tab(obj, "Tab 2") {
-            ui_button(obj, .label = "Button 1", .onclick=action_tab2_button);
-            ui_button(obj, .label = "Button 2", .onclick=action_tab2_button);
+            ui_button(obj, .label = "Button 1 Start Thread", .onclick=action_start_thread);
+            ui_button(obj, .label = "Button 2 Notify Thread", .onclick=action_notify_thread);
             ui_button(obj, .label = "Button 3", .onclick=action_tab2_button);
             ui_button(obj, .label = "Button 4", .onclick=action_tab2_button);
             ui_button(obj, .label = "Button 5", .onclick=action_tab2_button);
--- a/ui/common/objs.mk	Wed Oct 23 15:05:20 2024 +0200
+++ b/ui/common/objs.mk	Wed Oct 23 15:57:59 2024 +0200
@@ -39,6 +39,7 @@
 COMMON_OBJ += toolbar.o
 COMMON_OBJ += ucx_properties.o
 COMMON_OBJ += threadpool.o
+COMMON_OBJ += condvar.o
 
 TOOLKITOBJS += $(COMMON_OBJ:%=$(COMMON_OBJPRE)%)
 TOOLKITSOURCE += $(COMMON_OBJ:%.o=common/%.c)
--- a/ui/ui/toolkit.h	Wed Oct 23 15:05:20 2024 +0200
+++ b/ui/ui/toolkit.h	Wed Oct 23 15:57:59 2024 +0200
@@ -402,6 +402,11 @@
     size_t nfiles;
 };
 
+typedef struct UiCondVar {
+    void *data;
+    int intdata;
+} UiCondVar;
+
 
 UIEXPORT void ui_init(const char *appname, int argc, char **argv);
 UIEXPORT const char* ui_appname();
@@ -539,6 +544,11 @@
 UIEXPORT char* ui_getappdir();
 UIEXPORT char* ui_configfile(char *name);
 
+UIEXPORT UiCondVar* ui_condvar_create(void);
+UIEXPORT void ui_condvar_wait(UiCondVar *var);
+UIEXPORT void ui_condvar_signal(UiCondVar *var, void *data, int intdata);
+UIEXPORT void ui_condvar_destroy(UiCondVar *var);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial