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.
This commit is contained in:
Willy Tarreau 2019-06-06 16:50:39 +02:00
parent 237f8aef41
commit 43091ed161

View File

@ -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> ms to <from>, set the result to <tv> and returns a pointer <tv>
@ -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;
}