Skip to content

Commit

Permalink
Prevent race condition in g_io_condition_get_type
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Stefan Ekenberg authored and Matthias Clasen committed Jun 5, 2015
1 parent 6c43b6a commit 338741f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions gobject/gsourceclosure.c
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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;
}
Expand Down

0 comments on commit 338741f

Please sign in to comment.