[glib] g_get_monotonic_time: fix race condition
- From: Dan Winship <danw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] g_get_monotonic_time: fix race condition
- Date: Wed, 31 Aug 2011 01:16:10 +0000 (UTC)
commit 2955981569808304399d16847712f0ef97c6355b
Author: Dan Winship <danw gnome org>
Date: Tue Aug 16 19:12:59 2011 -0400
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.
https://bugzilla.gnome.org/show_bug.cgi?id=655129
glib/gmain.c | 31 +++++++++++++++----------------
1 files changed, 15 insertions(+), 16 deletions(-)
---
diff --git a/glib/gmain.c b/glib/gmain.c
index 75c2c0e..a7bcc0f 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -2042,25 +2042,24 @@ g_get_monotonic_time (void)
#ifdef HAVE_CLOCK_GETTIME
/* librt clock_gettime() is our first choice */
{
- static int clockid = CLOCK_REALTIME;
+#ifdef HAVE_MONOTONIC_CLOCK
+ static volatile gsize clockid = 0;
+#else
+ static clockid_t clockid = CLOCK_REALTIME;
+#endif
struct timespec ts;
#ifdef HAVE_MONOTONIC_CLOCK
- /* 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)
- clockid = CLOCK_MONOTONIC;
- checked = TRUE;
- }
- }
+ if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
+ best_clockid = CLOCK_MONOTONIC;
+ else
+ best_clockid = CLOCK_REALTIME;
+ g_once_init_leave (&clockid, (gsize)best_clockid);
+ }
#endif
clock_gettime (clockid, &ts);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]