[glib] Prevent race condition in g_io_condition_get_type



commit 338741fff5381d1a8d11b8f62c9e208af8b016fa
Author: Stefan Ekenberg <stefeg axis com>
Date:   Wed Jun 3 15:59:57 2015 +0200

    Prevent race condition in g_io_condition_get_type
    
    Prevents race condition in function g_io_condition_get_type by ensuring
    that the initialization section for 'etype' is executed only once
    during a program's life time, and that concurrent threads are blocked
    until initialization completes. This changes solves the problem that
    concurrent threads could execute the check 'etype == 0' before any of
    them had initialized it, which in turn meant that multiple threads
    would then attempt to register the "GIOCondition" type.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=750386

 gobject/gsourceclosure.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
---
diff --git a/gobject/gsourceclosure.c b/gobject/gsourceclosure.c
index b74bdb6..910b6b2 100644
--- a/gobject/gsourceclosure.c
+++ b/gobject/gsourceclosure.c
@@ -32,8 +32,9 @@ G_DEFINE_BOXED_TYPE (GIOChannel, g_io_channel, g_io_channel_ref, g_io_channel_un
 GType
 g_io_condition_get_type (void)
 {
-  static GType etype = 0;
-  if (etype == 0)
+  static volatile GType etype = 0;
+
+  if (g_once_init_enter (&etype))
     {
       static const GFlagsValue values[] = {
        { G_IO_IN,   "G_IO_IN",   "in" },
@@ -44,7 +45,8 @@ g_io_condition_get_type (void)
        { G_IO_NVAL, "G_IO_NVAL", "nval" },
        { 0, NULL, NULL }
       };
-      etype = g_flags_register_static ("GIOCondition", values);
+      GType type_id = g_flags_register_static ("GIOCondition", values);
+      g_once_init_leave (&etype, type_id);
     }
   return etype;
 }


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