[libgweather/ebassi/gtk4: 8/10] Make enum type registration thread safe




commit b13d537877979eb4de1838b2309fb68d1ae75531
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sun Oct 10 17:26:20 2021 +0100

    Make enum type registration thread safe
    
    Change the template to use g_once_init_enter/leave pairs, like everyone
    else does.

 libgweather/gweather-enum-types.c.tmpl | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
---
diff --git a/libgweather/gweather-enum-types.c.tmpl b/libgweather/gweather-enum-types.c.tmpl
index fe1f7c4d..11386970 100644
--- a/libgweather/gweather-enum-types.c.tmpl
+++ b/libgweather/gweather-enum-types.c.tmpl
@@ -1,6 +1,10 @@
 /*** BEGIN file-header ***/
-#include <config.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "gweather-enum-types.h"
+
 #include "gweather-location.h"
 #include "gweather-enums.h"
 #include "gweather-weather.h"
@@ -14,21 +18,26 @@
 GType
 @enum_name@_get_type (void)
 {
-       static GType etype = 0;
-       if (G_UNLIKELY (etype == 0)) {
-               static const G@Type@Value values[] = {
+    static gsize g_define_enum_type;
+
+    if (g_once_init_enter (&g_define_enum_type)) {
+        static const G@Type@Value values[] = {
 /*** END value-header ***/
 
 /*** BEGIN value-production ***/
-                       { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
+            { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
 /*** END value-production ***/
 
 /*** BEGIN value-tail ***/
-                       { 0, NULL, NULL }
-               };
-               etype = g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
-       }
-       return etype;
+            { 0, NULL, NULL }
+        };
+
+        GType enum_type = g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
+
+        g_once_init_leave (&g_define_enum_type, enum_type);
+    }
+
+    return g_define_enum_type;
 }
 
 /*** END value-tail ***/


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