From 43091ed161e1013feb7efb07a50b04dda6546bf9 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 6 Jun 2019 16:50:39 +0200 Subject: [PATCH] BUG/MINOR: time: make sure only one thread sets global_now at boot All threads call tv_update_date(-1) at boot to set their own local time offset. While doing so they also overwrite global_now, which is not that much of a problem except that it's not done using an atomic write and that it will be overwritten by every there in parallel. We only need the first thread to set it anyway, so let's simply set it if not set and do it using a CAS. This should fix GH issue #111. This may be backported to 1.9. --- src/time.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/time.c b/src/time.c index 187f05142..e830f851a 100644 --- a/src/time.c +++ b/src/time.c @@ -30,7 +30,7 @@ THREAD_LOCAL struct timeval before_poll; /* system date before calling poll( THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */ static THREAD_LOCAL struct timeval tv_offset; /* per-thread time ofsset relative to global time */ -volatile unsigned long long global_now; /* common date between all threads (32:32) */ +static volatile unsigned long long global_now; /* common date between all threads (32:32) */ /* * adds ms to , set the result to and returns a pointer @@ -186,8 +186,11 @@ REGPRM2 void tv_update_date(int max_wait, int interrupted) after_poll = date; samp_time = idle_time = 0; ti->idle_pct = 100; - global_now = (((unsigned long long)adjusted.tv_sec) << 32) + - (unsigned int)adjusted.tv_usec; + old_now = global_now; + if (!old_now) { // never set + new_now = (((unsigned long long)adjusted.tv_sec) << 32) + (unsigned int)adjusted.tv_usec; + _HA_ATOMIC_CAS(&global_now, &old_now, new_now); + } goto to_ms; }