Commit 3fd3f62c3f0996bc65a9379c71362e5885b52250

Thomas de Grivel 2020-05-14T18:58:56

lock rtbuf cli run thread callbacks

diff --git a/cli/include/rtbuf/cli.h b/cli/include/rtbuf/cli.h
index 6f02ffe..9680f58 100644
--- a/cli/include/rtbuf/cli.h
+++ b/cli/include/rtbuf/cli.h
@@ -17,6 +17,10 @@
 #ifndef RTBUF_CLI_H
 #define RTBUF_CLI_H
 
+#include <pthread.h>
+
+extern pthread_t g_rtbuf_cli_run_thread;
+
 int rtbuf_cli_exit (int argc, const char *argv[]);
 void repl_init ();
 int load (const char *path);
diff --git a/gtk/rtbuf_gtk.c b/gtk/rtbuf_gtk.c
index 69eca6e..0b753c1 100644
--- a/gtk/rtbuf_gtk.c
+++ b/gtk/rtbuf_gtk.c
@@ -35,7 +35,7 @@ unsigned int g_next_id = 0;
 
 GtkBuilder *builder = NULL;
 
-pthread_t g_rtbuf_gtk_thread = 0;
+GMutex g_mutex;
 
 GtkWindow              *modular = NULL;
 GtkToolbar             *modular_toolbar = NULL;
@@ -463,9 +463,21 @@ int rtbuf_gtk_builder ()
   return 0;
 }
 
+void rtbuf_gtk_lock ()
+{
+  if (pthread_equal(pthread_self(), g_rtbuf_cli_run_thread))
+    g_mutex_lock(&g_mutex);
+}
+
+void rtbuf_gtk_unlock ()
+{
+  if (pthread_equal(pthread_self(), g_rtbuf_cli_run_thread))
+    g_mutex_unlock(&g_mutex);
+}
+
 void rtbuf_gtk_new_cb (s_rtbuf *rtbuf)
 {
-  printf("rtbuf-gtk new\n");
+  rtbuf_gtk_lock();
   if (!rtbuf->user_ptr) {
     s_rtbuf_gtk_rtbuf_info *info = rtbuf_gtk_rtbuf_info_new();
     assert(info);
@@ -474,16 +486,18 @@ void rtbuf_gtk_new_cb (s_rtbuf *rtbuf)
     rtbuf->user_ptr = info;
   }
   rtbuf_gtk_modular_layout_new(rtbuf);
+  rtbuf_gtk_unlock();
 }
 
 void rtbuf_gtk_delete_cb (s_rtbuf *rtbuf)
 {
-  printf("rtbuf-gtk delete\n");
+  rtbuf_gtk_lock();
   if (rtbuf->user_ptr) {
-    s_rtbuf_gtk_rtbuf_info *info =
-      (s_rtbuf_gtk_rtbuf_info*) rtbuf->user_ptr;
+    s_rtbuf_gtk_rtbuf_info *info;
+    info = (s_rtbuf_gtk_rtbuf_info*) rtbuf->user_ptr;
     gtk_widget_destroy(GTK_WIDGET(info->widget));
   }
+  rtbuf_gtk_unlock();
 }
 
 void rtbuf_gtk_bind_cb (s_rtbuf *src, unsigned int out,
@@ -494,7 +508,7 @@ void rtbuf_gtk_bind_cb (s_rtbuf *src, unsigned int out,
   RtbufOutputWidget *output_widget;
   RtbufInputWidget *input_widget;
   s_rtbuf_gtk_connection *connection;
-  printf("rtbuf-gtk bind\n");
+  rtbuf_gtk_lock();
   assert(src);
   assert(src->proc);
   assert(out < src->proc->out_n);
@@ -521,6 +535,7 @@ void rtbuf_gtk_bind_cb (s_rtbuf *src, unsigned int out,
   connection->input_widget = input_widget;
   rtbuf_gtk_connection_push(&modular_connections, connection);
   gtk_widget_queue_draw(GTK_WIDGET(modular_layout));
+  rtbuf_gtk_unlock();
 }
 
 void rtbuf_gtk_unbind_cb (s_rtbuf *src, unsigned int out,
@@ -531,7 +546,7 @@ void rtbuf_gtk_unbind_cb (s_rtbuf *src, unsigned int out,
   RtbufOutputWidget *output_widget;
   RtbufInputWidget *input_widget;
   s_rtbuf_gtk_connection *connection;
-  printf("rtbuf-gtk unbind\n");
+  rtbuf_gtk_lock();
   assert(src);
   assert(src->proc);
   assert(out < src->proc->out_n);
@@ -548,24 +563,27 @@ void rtbuf_gtk_unbind_cb (s_rtbuf *src, unsigned int out,
   assert(input_widget);
   connection = rtbuf_gtk_connection_find(modular_connections,
                                          output_widget, input_widget);
-  printf("connection %p\n", connection);
   if (connection)
     rtbuf_gtk_connection_remove_one(&modular_connections,
                                     connection);
   gtk_widget_queue_draw(GTK_WIDGET(modular_layout));
+  rtbuf_gtk_unlock();
 }
 
 void main_loop (void)
 {
   struct timespec timeout;
   timeout.tv_sec = 0;
-  timeout.tv_nsec = 1000000;
+  timeout.tv_nsec = 1000;
+  g_mutex_init(&g_mutex);
   while (1) {
     int sleep = 1;
     if (rtbuf_cli_do_event())
       sleep = 0;
     if (gtk_events_pending()) {
+      g_mutex_lock(&g_mutex);
       gtk_main_iteration();
+      g_mutex_unlock(&g_mutex);
       sleep = 0;
     }
     if (sleep)
diff --git a/gtk/rtbuf_gtk_cli.c b/gtk/rtbuf_gtk_cli.c
deleted file mode 100644
index 8332025..0000000
--- a/gtk/rtbuf_gtk_cli.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright 2020 Thomas de Grivel <thoxdg@gmail.com>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <assert.h>
-#include <pthread.h>
-#include <rtbuf/rtbuf.h>
-#include <rtbuf/cli.h>
-#include "rtbuf_gtk.h"
-
-pthread_t g_rtbuf_gtk_cli_thread = 0;
-
-void * rtbuf_gtk_cli_thread_proc (void *arg)
-{
-  (void) arg;
-  assert(g_rtbuf);
-  assert(g_argc);
-  assert(g_argv);
-  repl();
-  return 0;
-}
-
-void rtbuf_gtk_cli_start ()
-{
-  if (pthread_create(&g_rtbuf_gtk_cli_thread, 0,
-                     &rtbuf_gtk_cli_thread_proc, 0))
-    rtbuf_err("rtbuf_gtk_cli_start: pthread_create failed");
-}