Remove "volatile" from G_DEFINE_*
Submitted by Behdad Esfahbod
Link to original bug (#683360)
Description
In code like this:
#define G_DEFINE_QUARK(QN, q_n) \
GQuark \
q_n##_quark (void) \
{ \
static volatile gsize g_define_quark__volatile = 0; \
if (g_once_init_enter (&g_define_quark__volatile)) \
{ \
GQuark g_define_quark = g_quark_from_string (#QN); \
g_once_init_leave (&g_define_quark__volatile, g_define_quark); \
} \
return g_define_quark__volatile; \
}
We shouldn't be using "volatile". What volatile means is that "the value may change at any moment". That's true when you are dealing with the same variable from multiple threads without the correct primitives. When we use g_once_init, gatomic, etc, ie. primitives designed exactly to address this case, the value is not changing unexpectedly anymore. As such, we should NOT mark them volatile.
In C, volatile really is only useful with IO ports and other hardware memory mappings. In more recent C++ volatile variables have been respec'ed to have acquire/release semantics which is more useful, as in, you can use them and get some atomicity without bothering with atomic ops.
Edited by Corentin Noël