Commit 29559815 authored by Dan Winship's avatar Dan Winship
Browse files

g_get_monotonic_time: fix race condition

Since there was nothing guaranteeing synchronization of the
assignments to checked and clockid, it would be possible for one
thread to set clockid = CLOCK_MONOTONIC, and for another thread to see
checked = TRUE but still clockid = CLOCK_REALTIME.
parent c0eb77bf
......@@ -2042,25 +2042,24 @@ g_get_monotonic_time (void)
/* librt clock_gettime() is our first choice */
static int clockid = CLOCK_REALTIME;
static volatile gsize clockid = 0;
static clockid_t clockid = CLOCK_REALTIME;
struct timespec ts;
/* We have to check if we actually have monotonic clock support.
* There is no thread safety issue here since there is no harm if we
* check twice.
static gboolean checked;
if (g_once_init_enter (&clockid))
clockid_t best_clockid;
if G_UNLIKELY (!checked)
if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
checked = TRUE;
if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
best_clockid = CLOCK_MONOTONIC;
best_clockid = CLOCK_REALTIME;
g_once_init_leave (&clockid, (gsize)best_clockid);
clock_gettime (clockid, &ts);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment