From f5b6e7710fae76f031b899f24710b82e9c5602de Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 11 May 2023 09:50:22 -0400 Subject: xtime: Make lazy tzset() call thread-safe --- src/xtime.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'src/xtime.c') diff --git a/src/xtime.c b/src/xtime.c index 406d694..79dafad 100644 --- a/src/xtime.c +++ b/src/xtime.c @@ -2,6 +2,7 @@ // SPDX-License-Identifier: 0BSD #include "xtime.h" +#include "atomic.h" #include "config.h" #include #include @@ -10,15 +11,19 @@ #include #include -/** Whether tzset() has been called. */ -static bool tz_is_set = false; +/** Call tzset() if necessary. */ +static void xtzset(void) { + static atomic bool is_set = false; -int xlocaltime(const time_t *timep, struct tm *result) { - // Should be called before localtime_r() according to POSIX.1-2004 - if (!tz_is_set) { + if (!load(&is_set, relaxed)) { tzset(); - tz_is_set = true; + store(&is_set, true, relaxed); } +} + +int xlocaltime(const time_t *timep, struct tm *result) { + // Should be called before localtime_r() according to POSIX.1-2004 + xtzset(); if (localtime_r(timep, result)) { return 0; @@ -29,10 +34,7 @@ int xlocaltime(const time_t *timep, struct tm *result) { int xgmtime(const time_t *timep, struct tm *result) { // Should be called before gmtime_r() according to POSIX.1-2004 - if (!tz_is_set) { - tzset(); - tz_is_set = true; - } + xtzset(); if (gmtime_r(timep, result)) { return 0; -- cgit v1.2.3