[gnome-remote-desktop] Add grd-utils



commit 39f9a4b913ae51588acfc9e95f87be2b0bd4d6ec
Author: Pascal Nowack <Pascal Nowack gmx de>
Date:   Sat Jan 1 12:07:34 2022 +0100

    Add grd-utils
    
    There are several situations in gnome-remote-desktop, where one thread
    has to wait on another thread to finish a certain task.
    For these situations, a combination of a GCond, a GMutex, and a boolean
    value is used.
    This signal-wait mechanism has always the same pattern.
    In order to avoid implementing it over and over again, create a helper
    struct with helper functions, which take over the mechanism.

 src/grd-utils.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/grd-utils.h | 43 ++++++++++++++++++++++++++++++++++++++
 src/meson.build |  2 ++
 3 files changed, 110 insertions(+)
---
diff --git a/src/grd-utils.c b/src/grd-utils.c
new file mode 100644
index 00000000..7f85d54b
--- /dev/null
+++ b/src/grd-utils.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2022 Pascal Nowack
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "grd-utils.h"
+
+void
+grd_sync_point_init (GrdSyncPoint *sync_point)
+{
+  g_cond_init (&sync_point->sync_cond);
+  g_mutex_init (&sync_point->sync_mutex);
+}
+
+void
+grd_sync_point_clear (GrdSyncPoint *sync_point)
+{
+  g_cond_clear (&sync_point->sync_cond);
+  g_mutex_clear (&sync_point->sync_mutex);
+}
+
+void
+grd_sync_point_complete (GrdSyncPoint *sync_point,
+                         gboolean      success)
+{
+  g_return_if_fail (!sync_point->completed);
+
+  g_mutex_lock (&sync_point->sync_mutex);
+  sync_point->success = success;
+
+  sync_point->completed = TRUE;
+  g_cond_signal (&sync_point->sync_cond);
+  g_mutex_unlock (&sync_point->sync_mutex);
+}
+
+gboolean
+grd_sync_point_wait_for_completion (GrdSyncPoint *sync_point)
+{
+  gboolean success;
+
+  g_mutex_lock (&sync_point->sync_mutex);
+  while (!sync_point->completed)
+    g_cond_wait (&sync_point->sync_cond, &sync_point->sync_mutex);
+
+  success = sync_point->success;
+  g_mutex_unlock (&sync_point->sync_mutex);
+
+  return success;
+}
diff --git a/src/grd-utils.h b/src/grd-utils.h
new file mode 100644
index 00000000..7ef4e10f
--- /dev/null
+++ b/src/grd-utils.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2022 Pascal Nowack
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#ifndef GRD_UTILS_H
+#define GRD_UTILS_H
+
+#include <gio/gio.h>
+
+typedef struct _GrdSyncPoint
+{
+  GCond sync_cond;
+  GMutex sync_mutex;
+  gboolean completed;
+
+  gboolean success;
+} GrdSyncPoint;
+
+void grd_sync_point_init (GrdSyncPoint *sync_point);
+
+void grd_sync_point_clear (GrdSyncPoint *sync_point);
+
+void grd_sync_point_complete (GrdSyncPoint *sync_point,
+                              gboolean      success);
+
+gboolean grd_sync_point_wait_for_completion (GrdSyncPoint *sync_point);
+
+#endif /* GRD_UTILS_H */
diff --git a/src/meson.build b/src/meson.build
index 7016d41d..9723d4a6 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -37,6 +37,8 @@ daemon_sources = files([
   'grd-stream.c',
   'grd-stream.h',
   'grd-types.h',
+  'grd-utils.c',
+  'grd-utils.h',
 ])
 
 if have_rdp


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]