This shouldn't affect behavior of either Gnulib or glibc.
* lib/mktime-internal.h (__libc_lock_lock, __libc_lock_unlock)
(__tzset_unlocked) [!_LIBC]: New macros.
* lib/mktime.c (tzset) [!_LIBC]: Define this instead of __tzset,
if defining either.
(__mktime64): Simplify now that mktime-internal defines
libc-specific macros to noops when !_LIBC.
---
 ChangeLog             |  9 +++++++++
 lib/mktime-internal.h |  7 ++++++-
 lib/mktime.c          | 11 ++---------
 3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 629f1b8d3f..7870c91b31 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2025-01-04  Paul Eggert  <egg...@cs.ucla.edu>
 
+       mktime: ease merge of locking code with glibc
+       This shouldn't affect behavior of either Gnulib or glibc.
+       * lib/mktime-internal.h (__libc_lock_lock, __libc_lock_unlock)
+       (__tzset_unlocked) [!_LIBC]: New macros.
+       * lib/mktime.c (tzset) [!_LIBC]: Define this instead of __tzset,
+       if defining either.
+       (__mktime64): Simplify now that mktime-internal defines
+       libc-specific macros to noops when !_LIBC.
+
        mktime: improve tm_isdst heuristic
        * lib/mktime.c (__mktime_internal): When tm_isdst disagrees with
        what was requested, search at most a year (+ stride) from the
diff --git a/lib/mktime-internal.h b/lib/mktime-internal.h
index 1da98b4373..215be914c2 100644
--- a/lib/mktime-internal.h
+++ b/lib/mktime-internal.h
@@ -19,6 +19,9 @@
 
 #ifndef _LIBC
 # include <time.h>
+# define __libc_lock_lock(lock) ((void) 0)
+# define __libc_lock_unlock(lock) ((void) 0)
+# define __tzset_unlocked() tzset ()
 #endif
 
 /* mktime_offset_t is a signed type wide enough to hold a UTC offset
@@ -73,6 +76,8 @@ typedef int mktime_offset_t;
 /* Subroutine of mktime.  Return the time_t representation of TP and
    normalize TP, given that a struct tm * maps to a time_t.  If
    LOCAL, the mapping is performed by localtime_r, otherwise by gmtime_r.
-   Record next guess for localtime-gmtime offset in *OFFSET.  */
+   Record next guess for localtime-gmtime offset in *OFFSET.
+
+   If _LIBC, the caller must lock __tzset_lock.  */
 extern __time64_t __mktime_internal (struct tm *tp, bool local,
                                      mktime_offset_t *offset) attribute_hidden;
diff --git a/lib/mktime.c b/lib/mktime.c
index 4448a92c06..4218fca69b 100644
--- a/lib/mktime.c
+++ b/lib/mktime.c
@@ -101,8 +101,8 @@ my_tzset (void)
   tzset ();
 # endif
 }
-# undef __tzset
-# define __tzset() my_tzset ()
+# undef tzset
+# define tzset() my_tzset ()
 #endif
 
 #if defined _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL
@@ -540,12 +540,8 @@ __mktime_internal (struct tm *tp, bool local, 
mktime_offset_t *offset)
 __time64_t
 __mktime64 (struct tm *tp)
 {
-# ifdef _LIBC
   __libc_lock_lock (__tzset_lock);
   __tzset_unlocked ();
-# else
-  __tzset ();
-# endif
 
 # if defined _LIBC || NEED_MKTIME_WORKING
   static mktime_offset_t localtime_offset;
@@ -555,10 +551,7 @@ __mktime64 (struct tm *tp)
   __time64_t result = mktime (tp);
 # endif
 
-# ifdef _LIBC
   __libc_lock_unlock (__tzset_lock);
-# endif
-
   return result;
 }
 #endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS */
-- 
2.45.2


Reply via email to