Using the reproduction steps Tim shared [1], this patch fixes the
redeclaration on MinGW:

    depbase=`echo nanosleep.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
    x86_64-w64-mingw32-gcc -DHAVE_CONFIG_H -I. -I..  -DGNULIB_STRICT_CHECKING=1 
  -g -O2 -MT nanosleep.o -MD -MP -MF $depbase.Tpo -c -o nanosleep.o nanosleep.c 
&&\
    mv -f $depbase.Tpo $depbase.Po
    nanosleep.c:97:1: error: redefinition of 'nanosleep'
       97 | nanosleep (const struct timespec *requested_delay,
          | ^~~~~~~~~
    In file included from /usr/share/mingw-w64/include/time.h:323,
                     from ./time.h:54,
                     from nanosleep.c:23:
    /usr/share/mingw-w64/include/pthread_time.h:79:39: note: previous 
definition of 'nanosleep' with type 'int(const struct timespec *, struct 
timespec *)'
       79 | WINPTHREAD_NANOSLEEP_DECL int __cdecl nanosleep(const struct 
timespec *request, struct timespec *remain)
          |                                       ^~~~~~~~~
    make[4]: *** [Makefile:2248: nanosleep.o] Error 1

I left the cross guess as "no" so the function gets replaced. This is
because MinGW does not use QueryPerformanceCounter, so I assume that
does not work as well for small durations [2].

I'm not sure if that is correct and/or if it should be documented. So I
will hold off pushing for review.

Collin

[1] https://savannah.gnu.org/bugs/?67704
[2] 
https://github.com/mingw-w64/mingw-w64/blob/master/mingw-w64-libraries/winpthreads/src/nanosleep.c

>From 863693dd36aeab6803dbb46566f066f98339a0b5 Mon Sep 17 00:00:00 2001
Message-ID: <863693dd36aeab6803dbb46566f066f98339a0b5.1763263302.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Sat, 15 Nov 2025 19:11:53 -0800
Subject: [PATCH] nanosleep: Avoid a redefinition on MinGW.

Reported by Tim Ruehsen in
<https://savannah.gnu.org/bugs/?67704>.

* m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Check for nanosleep is found in
-lpthread.
---
 ChangeLog       | 8 ++++++++
 m4/nanosleep.m4 | 5 +++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bce004b202..ce925711b0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-11-15  Collin Funk  <[email protected]>
+
+	nanosleep: Avoid a redefinition on MinGW.
+	Reported by Tim Ruehsen in
+	<https://savannah.gnu.org/bugs/?67704>.
+	* m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Check for nanosleep is found in
+	-lpthread.
+
 2025-11-15  Paul Eggert  <[email protected]>
 
 	-Wunused-const-variable started in GCC 6.1 not 4.4
diff --git a/m4/nanosleep.m4 b/m4/nanosleep.m4
index 81c45d8a31..a02ee233eb 100644
--- a/m4/nanosleep.m4
+++ b/m4/nanosleep.m4
@@ -1,5 +1,5 @@
 # nanosleep.m4
-# serial 47
+# serial 48
 dnl Copyright (C) 1999-2001, 2003-2025 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -24,9 +24,10 @@ AC_DEFUN([gl_FUNC_NANOSLEEP]
 
  # Solaris 2.5.1 needs -lposix4 to get the nanosleep function.
  # Solaris 7 prefers the library name -lrt to the obsolescent name -lposix4.
+ # MinGW uses needs -lpthread to get the nanosleep function.
  NANOSLEEP_LIB=
  AC_SUBST([NANOSLEEP_LIB])
- AC_SEARCH_LIBS([nanosleep], [rt posix4],
+ AC_SEARCH_LIBS([nanosleep], [rt posix4 pthread],
                 [test "$ac_cv_search_nanosleep" = "none required" ||
                  NANOSLEEP_LIB=$ac_cv_search_nanosleep])
  if test "x$ac_cv_search_nanosleep" != xno; then
-- 
2.51.1

Reply via email to