[gjs] byteArray: Convert static initialization to g_once(), factor out



commit 58cddea1531fe98a8280dc1c73e2ce28e97273b9
Author: Colin Walters <walters verbum org>
Date:   Mon Nov 5 14:20:06 2012 -0500

    byteArray: Convert static initialization to g_once(), factor out
    
    Using g_once() helps us find static initialization places, and is
    threadsafe in case that ever matters.
    
    Also, factoring out into a separate function will help a future patch.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=685431

 gjs/byteArray.c |   29 +++++++++++++++++++++--------
 1 files changed, 21 insertions(+), 8 deletions(-)
---
diff --git a/gjs/byteArray.c b/gjs/byteArray.c
index 5b6aa1f..729c06f 100644
--- a/gjs/byteArray.c
+++ b/gjs/byteArray.c
@@ -751,24 +751,37 @@ from_array_func(JSContext *context,
     return ret;
 }
 
+/* Ensure that the module and class objects exists, and that in turn
+ * ensures that JS_InitClass has been called, causing
+ * gjs_byte_array_prototype to be valid for the later call to
+ * JS_NewObject.
+ */
+static void
+byte_array_ensure_initialized (JSContext *context)
+{
+    static gsize initialized = 0;
+
+    if (g_once_init_enter (&initialized)) {
+        jsval rval;
+        JS_EvaluateScript(context, JS_GetGlobalObject(context),
+                          "imports.byteArray.ByteArray;", 27,
+                          "<internal>", 1, &rval);
+        g_once_init_leave (&initialized, 1);
+    }
+}
+
 JSObject *
 gjs_byte_array_from_byte_array (JSContext *context,
                                 GByteArray *array)
 {
     JSObject *object;
     ByteArrayInstance *priv;
-    static gboolean init = FALSE;
 
     g_return_val_if_fail(context != NULL, NULL);
     g_return_val_if_fail(array != NULL, NULL);
 
-    if (!init) {
-        jsval rval;
-        JS_EvaluateScript(context, JS_GetGlobalObject(context),
-                          "imports.byteArray.ByteArray;", 27,
-                          "<internal>", 1, &rval);
-        init = TRUE;
-    }
+    byte_array_ensure_initialized (context);
+
     object = JS_NewObject(context, &gjs_byte_array_class,
                           gjs_byte_array_prototype, NULL);
     if (!object) {



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