bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnulib] Re: nanosleep module and mingw32


From: Paul Eggert
Subject: Re: [bug-gnulib] Re: nanosleep module and mingw32
Date: Fri, 19 May 2006 11:01:49 -0700
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

Bruno Haible <address@hidden> writes:

> Since gnulib's policy is to let the programs write code in POSIX
> syntax, I vote for a module that creates a <sys/select.h> file in
> the build directory.

Yes, that would make sense, to properly declare 'select'.  For
nanosleep I suppose we'd also need a substitute time.h that declares
nanosleep.

There's one other thing about this particular problem that may save us
here, though.  The current code tests that nanosleep works at runtime,
not merely whether it links.  I suspect this is overkill, at least
nowadays.  And, since the original poster is talking about a
cross-compilation environment, where all runtime tests fail, maybe
changing the test to check only that nanosleep links will solve his
problem, as the replacement code won't be compiled at all.

I installed the following patch along these lines.  If it doesn't
suffice (or maybe even if it does) we can continue along the
header-replacement route.

2006-05-19  Paul Eggert  <address@hidden>

        * nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
        Use the usual Autoconf way to include <time.h> and/or sys/time.h.
        (my_usleep): Don't mishandle maximum value.
        * m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Rename cache variables to use
        gl_ rather than jm_.  Link, don't run, so that cross-compiles are
        allowed.  Check that resulting type is arithmetic.
        Check for sys/select.h.

--- lib/nanosleep.c     23 Sep 2005 04:15:13 -0000      1.17
+++ lib/nanosleep.c     19 May 2006 17:45:21 -0000
@@ -1,5 +1,7 @@
 /* Provide a replacement for the POSIX nanosleep function.
-   Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
+
+   Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006 Free Software
+   Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -27,9 +29,23 @@
 
 #include <stdbool.h>
 #include <stdio.h>
+#if HAVE_SYS_SELECT_H
+# include <sys/select.h>
+#endif
 #include <sys/types.h>
 #include <signal.h>
 
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 #include <errno.h>
 
 #include <unistd.h>
@@ -57,7 +73,7 @@ sighandler (int sig)
   suspended = 1;
 }
 
-/* FIXME: comment */
+/* Suspend execution for at least *TS_DELAY seconds.  */
 
 static void
 my_usleep (const struct timespec *ts_delay)
@@ -67,13 +83,20 @@ my_usleep (const struct timespec *ts_del
   tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
   if (tv_delay.tv_usec == 1000000)
     {
-      tv_delay.tv_sec++;
-      tv_delay.tv_usec = 0;
+      time_t t1 = tv_delay.tv_sec + 1;
+      if (t1 < tv_delay.tv_sec)
+       tv_delay.tv_usec = 1000000 - 1; /* close enough */
+      else
+       {
+         tv_delay.tv_sec = t1;
+         tv_delay.tv_usec = 0;
+       }
     }
   select (0, NULL, NULL, NULL, &tv_delay);
 }
 
-/* FIXME: comment */
+/* Suspend execution for at least *REQUESTED_DELAY seconds.  The
+   *REMAINING_DELAY part isn't implemented yet.  */
 
 int
 rpl_nanosleep (const struct timespec *requested_delay,
--- m4/nanosleep.m4     24 Apr 2006 07:35:24 -0000      1.23
+++ m4/nanosleep.m4     19 May 2006 17:45:24 -0000
@@ -1,4 +1,4 @@
-#serial 16
+#serial 17
 
 dnl From Jim Meyering.
 dnl Check for the nanosleep function.
@@ -16,6 +16,12 @@ AC_DEFUN([gl_FUNC_NANOSLEEP],
 [
  AC_LIBSOURCES([nanosleep.c])
 
+ dnl Persuade glibc and Solaris <time.h> to declare nanosleep.
+ AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+
+ AC_REQUIRE([AC_HEADER_TIME])
+ AC_CHECK_HEADERS_ONCE(sys/time.h)
+
  nanosleep_save_libs=$LIBS
 
  # Solaris 2.5.1 needs -lposix4 to get the nanosleep function.
@@ -25,15 +31,10 @@ AC_DEFUN([gl_FUNC_NANOSLEEP],
                 LIB_NANOSLEEP=$ac_cv_search_nanosleep])
  AC_SUBST([LIB_NANOSLEEP])
 
- AC_CACHE_CHECK([whether nanosleep works],
-  jm_cv_func_nanosleep_works,
+ AC_CACHE_CHECK([for nanosleep],
+  [gl_cv_func_nanosleep],
   [
-   dnl Persuade glibc and Solaris <time.h> to declare nanosleep.
-   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
-
-   AC_REQUIRE([AC_HEADER_TIME])
-   AC_CHECK_HEADERS_ONCE(sys/time.h)
-   AC_TRY_RUN([
+   AC_LINK_IFELSE([AC_LANG_SOURCE([[
 #   if TIME_WITH_SYS_TIME
 #    include <sys/time.h>
 #    include <time.h>
@@ -51,15 +52,13 @@ AC_DEFUN([gl_FUNC_NANOSLEEP],
       struct timespec ts_sleep, ts_remaining;
       ts_sleep.tv_sec = 0;
       ts_sleep.tv_nsec = 1;
-      return nanosleep (&ts_sleep, &ts_remaining) != 0;
+      return nanosleep (&ts_sleep, &ts_remaining) < 0;
     }
-         ],
-        jm_cv_func_nanosleep_works=yes,
-        jm_cv_func_nanosleep_works=no,
-        dnl When crosscompiling, assume the worst.
-        jm_cv_func_nanosleep_works=no)
+      ]])],
+     [gl_cv_func_nanosleep=yes],
+     [gl_cv_func_nanosleep=no])
   ])
-  if test $jm_cv_func_nanosleep_works = no; then
+  if test $gl_cv_func_nanosleep = no; then
     AC_LIBOBJ(nanosleep)
     AC_DEFINE(nanosleep, rpl_nanosleep,
       [Define to rpl_nanosleep if the replacement function should be used.])
@@ -73,4 +72,5 @@ AC_DEFUN([gl_FUNC_NANOSLEEP],
 AC_DEFUN([gl_PREREQ_NANOSLEEP],
 [
   AC_CHECK_FUNCS_ONCE(siginterrupt)
+  AC_CHECK_HEADERS_ONCE(sys/select.h)
 ])




reply via email to

[Prev in Thread] Current Thread [Next in Thread]