]> Savannah Git Hosting - gnulib.git/commitdiff
nstrftime: tweak volatile access
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 18 Jun 2024 15:21:44 +0000 (08:21 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 18 Jun 2024 15:22:03 +0000 (08:22 -0700)
* lib/strftime.c (utc_timezone): Avoid unnecessary access to volatile.

ChangeLog
lib/strftime.c

index 0c39edd886f9da1e3e5a6ca9790beb43d46fa2c5..1d5c7437115bce818f32b98cfc28e0124997038c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2024-06-18  Paul Eggert  <eggert@cs.ucla.edu>
+
+       nstrftime: tweak volatile access
+       * lib/strftime.c (utc_timezone): Avoid unnecessary access to volatile.
+
 2024-06-18  Bruno Haible  <bruno@clisp.org>
 
        nstrftime: Fix crash on NetBSD 10.0.
index ffc1670f2384db82673167839156d31a6ca94d45..e21a356e800b2db8466dcc921422559dc52a115b 100644 (file)
@@ -405,9 +405,10 @@ c_locale (void)
 
 #if HAVE_NATIVE_TIME_Z
 
-/* Cache for the UTC time zone object.
-   Marked volatile so that different threads see the same value
-   (avoids locking).  */
+/* On NetBSD a null tz has undefined behavior, so use a non-null tz.
+   Cache the UTC time zone object in a volatile variable for improved
+   thread safety.  This is good enough in practice, although in theory
+   stdatomic.h should be used.  */
 static volatile timezone_t utc_timezone_cache;
 
 /* Return the UTC time zone object, or (timezone_t) 0 with errno set
@@ -415,9 +416,10 @@ static volatile timezone_t utc_timezone_cache;
 static timezone_t
 utc_timezone (void)
 {
-  if (!utc_timezone_cache)
-    utc_timezone_cache = tzalloc ("UTC");
-  return utc_timezone_cache;
+  timezone_t tz = utc_timezone_cache;
+  if (!tz)
+    utc_timezone_cache = tz = tzalloc ("UTC0");
+  return tz;
 }
 
 #endif