[retro-gtk] core: Port callback data handling to C



commit fe1cde9e65704bda0c7582f13823778cefb07785
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu Jul 20 14:29:23 2017 +0200

    core: Port callback data handling to C
    
    https://bugzilla.gnome.org/show_bug.cgi?id=777489

 retro-gtk/core.vala    |   54 ++----------------------------------
 retro-gtk/retro-core.c |   71 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 51 deletions(-)
---
diff --git a/retro-gtk/core.vala b/retro-gtk/core.vala
index 7da942d..0183ba1 100644
--- a/retro-gtk/core.vala
+++ b/retro-gtk/core.vala
@@ -6,11 +6,6 @@ namespace Retro {
  * Handles a Libretro module.
  */
 public class Core : Object {
-       private static RecMutex r_mutex = RecMutex ();
-       private static RecMutex w_mutex = RecMutex ();
-       private static (unowned Core)[] objects = new (unowned Core)[32];
-       private static int i = 0;
-
        public signal void video_output (uint8[] data, uint width, uint height, size_t pitch, PixelFormat 
pixel_format, float aspect_ratio);
        public signal void audio_output (int16[] frames, double sample_rate);
        public signal void log (string log_domain, LogLevelFlags log_level, string message);
@@ -23,59 +18,16 @@ public class Core : Object {
         *
         * Must be called before any call to a function from the module.
         */
-       internal void push_cb_data () {
-               w_mutex.lock ();
-               r_mutex.lock ();
-
-               if (i == objects.length) {
-                       stderr.printf ("Error: Callback data stack overflow.\n");
-
-                       r_mutex.unlock ();
-                       assert_not_reached ();
-               }
-
-               objects[i] = this;
-               i++;
-
-               r_mutex.unlock ();
-       }
+       internal extern void push_cb_data ();
 
        /**
         * Removes the Core at the head of the stack.
         *
         * Must be called after any call to {@link push_cb_data()}.
         */
-       internal static void pop_cb_data () {
-               r_mutex.lock ();
-               if (i == 0) {
-                       stderr.printf ("Error: Callback data stack underflow.\n");
-
-                       r_mutex.unlock ();
-                       w_mutex.unlock ();
-                       assert_not_reached ();
-               }
+       internal extern static void pop_cb_data ();
 
-               i--;
-               objects[i] = null;
-
-               r_mutex.unlock ();
-               w_mutex.unlock ();
-       }
-
-       internal static unowned Core get_cb_data () {
-               r_mutex.lock ();
-               if (i == 0) {
-                       stderr.printf ("Error: Callback data segmentation fault.\n");
-
-                       r_mutex.unlock ();
-                       assert_not_reached ();
-               }
-
-               unowned Core result =  objects[i - 1];
-               r_mutex.unlock ();
-
-               return result;
-       }
+       internal extern static unowned Core get_cb_data ();
 
        private extern uint get_api_version_real ();
        /**
diff --git a/retro-gtk/retro-core.c b/retro-gtk/retro-core.c
index ae0ada4..0776446 100644
--- a/retro-gtk/retro-core.c
+++ b/retro-gtk/retro-core.c
@@ -6,6 +6,77 @@
 
 /* Private */
 
+#define RETRO_CORE_OBJECTS_LENGTH 32
+
+static GRecMutex retro_core_r_mutex = { 0 };
+static GRecMutex retro_core_w_mutex = { 0 };
+static RetroCore *retro_core_objects[32];
+static gint retro_core_i = 0;
+
+// FIXME Make static as soon as possible.
+void
+retro_core_push_cb_data (RetroCore *self)
+{
+  g_return_if_fail (self != NULL);
+
+  g_rec_mutex_lock (&retro_core_w_mutex);
+  g_rec_mutex_lock (&retro_core_r_mutex);
+
+  if (G_UNLIKELY (retro_core_i == RETRO_CORE_OBJECTS_LENGTH)) {
+    g_critical ("RetroCore callback data stack overflow.");
+
+    g_rec_mutex_unlock (&retro_core_r_mutex);
+    g_assert_not_reached ();
+  }
+
+  retro_core_objects[retro_core_i] = self;
+  retro_core_i++;
+
+  g_rec_mutex_unlock (&retro_core_r_mutex);
+}
+
+// FIXME Make static as soon as possible.
+void
+retro_core_pop_cb_data (void)
+{
+  g_rec_mutex_lock (&retro_core_r_mutex);
+
+  if (G_UNLIKELY (retro_core_i == 0)) {
+    g_critical ("RetroCore callback data stack underflow.");
+
+    g_rec_mutex_unlock (&retro_core_r_mutex);
+    g_rec_mutex_unlock (&retro_core_w_mutex);
+    g_assert_not_reached ();
+  }
+  retro_core_i--;
+
+  retro_core_objects[retro_core_i] = NULL;
+
+  g_rec_mutex_unlock (&retro_core_r_mutex);
+  g_rec_mutex_unlock (&retro_core_w_mutex);
+}
+
+// FIXME Make static as soon as possible.
+RetroCore *
+retro_core_get_cb_data (void)
+{
+  RetroCore *result;
+
+  g_rec_mutex_lock (&retro_core_r_mutex);
+
+  if (retro_core_i == 0) {
+    g_critical ("RetroCore callback data segmentation fault.");
+
+    g_rec_mutex_unlock (&retro_core_r_mutex);
+    g_assert_not_reached ();
+  }
+
+  result = retro_core_objects[retro_core_i - 1];
+  g_rec_mutex_unlock (&retro_core_r_mutex);
+
+  return result;
+}
+
 static void
 init_controller_device (guint             port,
                         RetroInputDevice *device,


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