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
This commit is contained in:
Stefan Ekenberg 2015-06-03 15:59:57 +02:00 committed by Matthias Clasen
parent 6c43b6a21a
commit 338741fff5

View File

@ -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;
}