bug-gnulib
[Top][All Lists]
Advanced

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

usleep (was: pending patches?)


From: Eric Blake
Subject: usleep (was: pending patches?)
Date: Tue, 17 Nov 2009 22:38:20 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Eric Blake on 11/14/2009 6:18 AM:
> I'm also thinking of adding a usleep module to guarantee Linux semantics
> (usleep(1000000) waits a second, rather than failing instantly with
> EINVAL).  That will clean up its use somewhat.

I'm pushing this.  Most systems these days still support usleep, even
though POSIX no longer requires it; even mingw has it; so the few
platforms where this implementation rounds up to the ceiling of the next
second should be rare (still correct behavior, just slow).  And there is
still a benefit to using usleep instead of select or nanosleep - fewer
dependencies on external libraries.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAksDiEwACgkQ84KuGfSFAYBUoACfZnXo+SByXSHSfOvvn1ZsosXB
lEEAoIv2BZUS+rOWjgwKIIhHa4y7VMk/
=lCtE
-----END PGP SIGNATURE-----
>From e8eecfd3f7d37692a7b87779b69d6fdc22d88d4d Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Tue, 17 Nov 2009 09:24:56 -0700
Subject: [PATCH 1/2] usleep: new module

mingw usleep(1000000) failed with EINVAL, as allowed by POSIX,
but contrary to GNU usage.  Rather than implement an accurate
usleep based on select or nanosleep, both of which drag in
dependencies on external libraries, this version intentionally
takes the ceiling in seconds if usleep() is missing.

* modules/usleep: New file.
* m4/usleep.m4 (gl_FUNC_USLEEP): Likewise.
* lib/usleep.c (usleep): Likewise.
* m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Add defaults.
* modules/unistd (Makefile.am): Substitute witnesses.
* lib/unistd.in.h (usleep): Add declaration.
* doc/pastposix-functions/usleep.texi (usleep): Document this.
* MODULES.html.sh (Date and time): Likewise.
* modules/usleep-tests (Depends-on): New test.
* tests/test-usleep.c: New file.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog                           |   12 +++++++
 MODULES.html.sh                     |    3 +-
 doc/pastposix-functions/usleep.texi |   18 ++++++-----
 lib/unistd.in.h                     |   21 ++++++++++++
 lib/usleep.c                        |   58 +++++++++++++++++++++++++++++++++++
 m4/unistd_h.m4                      |    5 ++-
 m4/usleep.m4                        |   37 ++++++++++++++++++++++
 modules/unistd                      |    3 ++
 modules/usleep                      |   26 +++++++++++++++
 modules/usleep-tests                |   11 ++++++
 tests/test-usleep.c                 |   49 +++++++++++++++++++++++++++++
 11 files changed, 233 insertions(+), 10 deletions(-)
 create mode 100644 lib/usleep.c
 create mode 100644 m4/usleep.m4
 create mode 100644 modules/usleep
 create mode 100644 modules/usleep-tests
 create mode 100644 tests/test-usleep.c

diff --git a/ChangeLog b/ChangeLog
index 599a944..cf14b4b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2009-11-17  Eric Blake  <address@hidden>

+       usleep: new module
+       * modules/usleep: New file.
+       * m4/usleep.m4 (gl_FUNC_USLEEP): Likewise.
+       * lib/usleep.c (usleep): Likewise.
+       * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Add defaults.
+       * modules/unistd (Makefile.am): Substitute witnesses.
+       * lib/unistd.in.h (usleep): Add declaration.
+       * doc/pastposix-functions/usleep.texi (usleep): Document this.
+       * MODULES.html.sh (Date and time): Likewise.
+       * modules/usleep-tests (Depends-on): New test.
+       * tests/test-usleep.c: New file.
+
        chown: work around OpenBSD bug
        * lib/chown.c (rpl_chown): Work around the bug.
        * lib/lchown.c (rpl_lchown): Attempt to do likewise.
diff --git a/MODULES.html.sh b/MODULES.html.sh
index 7d42b73..cb791b8 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2593,8 +2593,9 @@ func_all_modules ()
   func_begin_table
   func_module gethrxtime
   func_module gettime
-  func_module settime
   func_module posixtm
+  func_module settime
+  func_module usleep
   func_module xnanosleep
   func_end_table

diff --git a/doc/pastposix-functions/usleep.texi 
b/doc/pastposix-functions/usleep.texi
index bb38152..8ca0da9 100644
--- a/doc/pastposix-functions/usleep.texi
+++ b/doc/pastposix-functions/usleep.texi
@@ -4,23 +4,25 @@ usleep

 POSIX specification: @url{http://www.opengroup.org/susv3xsh/usleep.html}

-Gnulib module: ---
+Gnulib module: usleep

 Portability problems fixed by Gnulib:
 @itemize
address@hidden
+On some systems, @code{usleep} rejects attempts to sleep longer than 1
+second, as allowed by POSIX:
+mingw.
address@hidden
+This function is missing on some platforms.  However, the replacement
+is designed to be lightweight, and may round to the neareset second;
+use @code{select} or @code{nanosleep} if better resolution is needed:
+IRIX 5.3, Solaris 2.4, older mingw, BeOS.
 @end itemize

 Portability problems not fixed by Gnulib:
 @itemize
 @item
-This function is missing on some platforms:
-IRIX 5.3, Solaris 2.4, older mingw, BeOS.
address@hidden
 According to POSIX, the @code{usleep} function may interfere with the program's
 use of the @code{SIGALRM} signal.  On Linux, it doesn't; on other platforms,
 it may.
address@hidden
-On some systems, @code{usleep} rejects attempts to sleep longer than 1
-second, as allowed by POSIX:
-mingw.
 @end itemize
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index c026b71..30f7bdd 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -792,6 +792,27 @@ extern int unlinkat (int fd, char const *file, int flag);
 #endif


+#if @GNULIB_USLEEP@
+# if @REPLACE_USLEEP@
+#  undef usleep
+#  define usleep rpl_usleep
+# endif
+# if address@hidden@ || @REPLACE_USLEEP@
+/* Pause the execution of the current thread for N microseconds.
+   Returns 0 on completion, or -1 on range error.
+   See the POSIX:2001 specification
+   <http://www.opengroup.org/susv3xsh/sleep.html>.  */
+extern int usleep (useconds_t n);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef usleep
+# define usleep(n) \
+    (GL_LINK_WARNING ("usleep is unportable - " \
+                      "use gnulib module usleep for portability"), \
+     usleep (n))
+#endif
+
+
 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
    See the POSIX:2001 specification
diff --git a/lib/usleep.c b/lib/usleep.c
new file mode 100644
index 0000000..86f1fef
--- /dev/null
+++ b/lib/usleep.c
@@ -0,0 +1,58 @@
+/* Pausing execution of the current thread.
+   Copyright (C) 2009 Free Software Foundation, Inc.
+   Written by Eric Blake <address@hidden>, 2009.
+
+   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
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* This file is _intentionally_ light-weight.  Rather than using
+   select or nanosleep, both of which drag in external libraries on
+   some platforms, this merely rounds up to the nearest second if
+   usleep() does not exist.  If sub-second resolution is important,
+   then use a more powerful interface to begin with.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <unistd.h>
+
+#include <errno.h>
+
+#ifndef HAVE_USLEEP
+# define HAVE_USLEEP 0
+#endif
+
+/* Sleep for MICRO microseconds, which can be greater than 1 second.
+   Return -1 and set errno to EINVAL on range error (about 4295
+   seconds), or 0 on success.  Interaction with SIGALARM is
+   unspecified.  */
+
+int
+usleep (useconds_t micro)
+{
+  unsigned int seconds = micro / 1000000;
+  if (sizeof seconds < sizeof micro && micro / 1000000 != seconds)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  if (!HAVE_USLEEP && micro % 1000000)
+    seconds++;
+  while ((seconds = sleep (seconds)) != 0);
+
+#undef usleep
+#if !HAVE_USLEEP
+# define usleep(x) 0
+#endif
+  return usleep (micro % 1000000);
+}
diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4
index 88e60a0..25bdb59 100644
--- a/m4/unistd_h.m4
+++ b/m4/unistd_h.m4
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 34
+# unistd_h.m4 serial 35
 dnl Copyright (C) 2006-2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -66,6 +66,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   GNULIB_UNISTD_H_SIGPIPE=0; AC_SUBST([GNULIB_UNISTD_H_SIGPIPE])
   GNULIB_UNLINK=0;           AC_SUBST([GNULIB_UNLINK])
   GNULIB_UNLINKAT=0;         AC_SUBST([GNULIB_UNLINKAT])
+  GNULIB_USLEEP=0;           AC_SUBST([GNULIB_USLEEP])
   GNULIB_WRITE=0;            AC_SUBST([GNULIB_WRITE])
   dnl Assume proper GNU behavior unless another module says otherwise.
   HAVE_CHOWN=1;           AC_SUBST([HAVE_CHOWN])
@@ -96,6 +97,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   HAVE_OS_H=0;            AC_SUBST([HAVE_OS_H])
   HAVE_SYS_PARAM_H=0;     AC_SUBST([HAVE_SYS_PARAM_H])
   HAVE_UNLINKAT=1;        AC_SUBST([HAVE_UNLINKAT])
+  HAVE_USLEEP=1;          AC_SUBST([HAVE_USLEEP])
   REPLACE_CHOWN=0;        AC_SUBST([REPLACE_CHOWN])
   REPLACE_CLOSE=0;        AC_SUBST([REPLACE_CLOSE])
   REPLACE_DUP=0;          AC_SUBST([REPLACE_DUP])
@@ -114,6 +116,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   REPLACE_SYMLINK=0;      AC_SUBST([REPLACE_SYMLINK])
   REPLACE_UNLINK=0;       AC_SUBST([REPLACE_UNLINK])
   REPLACE_UNLINKAT=0;     AC_SUBST([REPLACE_UNLINKAT])
+  REPLACE_USLEEP=0;       AC_SUBST([REPLACE_USLEEP])
   REPLACE_WRITE=0;        AC_SUBST([REPLACE_WRITE])
   UNISTD_H_HAVE_WINSOCK2_H=0; AC_SUBST([UNISTD_H_HAVE_WINSOCK2_H])
   UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS=0;
diff --git a/m4/usleep.m4 b/m4/usleep.m4
new file mode 100644
index 0000000..c8e6911
--- /dev/null
+++ b/m4/usleep.m4
@@ -0,0 +1,37 @@
+# usleep.m4 serial 1
+dnl Copyright (C) 2009 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl This macro intentionally does not check for select or nanosleep;
+dnl both of those modules can require external libraries.
+AC_DEFUN([gl_FUNC_USLEEP],
+[
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  dnl usleep was required in POSIX 2001, but dropped as obsolete in
+  dnl POSIX 2008; therefore, it is not always exposed in headers.
+  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+  AC_CHECK_FUNCS_ONCE([usleep])
+  AC_CHECK_TYPE([useconds_t], [],
+    [AC_DEFINE([useconds_t], [unsigned int], [Define to an unsigned 32-bit
+      type if <sys/types.h> lacks this type.])])
+  if test $ac_cv_func_usleep = no; then
+    HAVE_USLEEP=0
+    AC_LIBOBJ([usleep])
+  else
+    dnl POSIX allows implementations to reject arguments larger than
+    dnl 999999, but GNU guarantees it will work.
+    AC_CACHE_CHECK([whether usleep allows large arguments],
+      [gl_cv_func_usleep_works],
+      [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
+#include <unistd.h>
+]], [[return !!usleep (1000000);]])],
+        [gl_cv_func_usleep_works=yes], [gl_cv_func_usleep_works=no],
+        [gl_cv_func_usleep_works="guessing no"])])
+    if test "$gl_cv_func_usleep_works" != yes; then
+      REPLACE_USLEEP=1
+      AC_LIBOBJ([usleep])
+    fi
+  fi
+])
diff --git a/modules/unistd b/modules/unistd
index 1cb3b0b..008ccdf 100644
--- a/modules/unistd
+++ b/modules/unistd
@@ -59,6 +59,7 @@ unistd.h: unistd.in.h
              -e 's|@''GNULIB_UNISTD_H_SIGPIPE''@|$(GNULIB_UNISTD_H_SIGPIPE)|g' 
\
              -e 's|@''GNULIB_UNLINK''@|$(GNULIB_UNLINK)|g' \
              -e 's|@''GNULIB_UNLINKAT''@|$(GNULIB_UNLINKAT)|g' \
+             -e 's|@''GNULIB_USLEEP''@|$(GNULIB_USLEEP)|g' \
              -e 's|@''GNULIB_WRITE''@|$(GNULIB_WRITE)|g' \
              -e 's|@''HAVE_CHOWN''@|$(HAVE_CHOWN)|g' \
              -e 's|@''HAVE_DUP2''@|$(HAVE_DUP2)|g' \
@@ -84,6 +85,7 @@ unistd.h: unistd.in.h
              -e 's|@''HAVE_SYMLINK''@|$(HAVE_SYMLINK)|g' \
              -e 's|@''HAVE_SYMLINKAT''@|$(HAVE_SYMLINKAT)|g' \
              -e 's|@''HAVE_UNLINKAT''@|$(HAVE_UNLINKAT)|g' \
+             -e 's|@''HAVE_USLEEP''@|$(HAVE_USLEEP)|g' \
              -e 's|@''HAVE_DECL_ENVIRON''@|$(HAVE_DECL_ENVIRON)|g' \
              -e 's|@''HAVE_DECL_GETLOGIN_R''@|$(HAVE_DECL_GETLOGIN_R)|g' \
              -e 's|@''HAVE_OS_H''@|$(HAVE_OS_H)|g' \
@@ -106,6 +108,7 @@ unistd.h: unistd.in.h
              -e 's|@''REPLACE_SYMLINK''@|$(REPLACE_SYMLINK)|g' \
              -e 's|@''REPLACE_UNLINK''@|$(REPLACE_UNLINK)|g' \
              -e 's|@''REPLACE_UNLINKAT''@|$(REPLACE_UNLINKAT)|g' \
+             -e 's|@''REPLACE_USLEEP''@|$(REPLACE_USLEEP)|g' \
              -e 's|@''REPLACE_WRITE''@|$(REPLACE_WRITE)|g' \
              -e 
's|@''UNISTD_H_HAVE_WINSOCK2_H''@|$(UNISTD_H_HAVE_WINSOCK2_H)|g' \
              -e 
's|@''UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS''@|$(UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS)|g'
 \
diff --git a/modules/usleep b/modules/usleep
new file mode 100644
index 0000000..c77826b
--- /dev/null
+++ b/modules/usleep
@@ -0,0 +1,26 @@
+Description:
+usleep() function: pause execution of the current thread.
+
+Files:
+lib/usleep.c
+m4/usleep.m4
+
+Depends-on:
+extensions
+sleep
+unistd
+
+configure.ac:
+gl_FUNC_USLEEP
+gl_UNISTD_MODULE_INDICATOR([usleep])
+
+Makefile.am:
+
+Include:
+<unistd.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Eric Blake
diff --git a/modules/usleep-tests b/modules/usleep-tests
new file mode 100644
index 0000000..762d644
--- /dev/null
+++ b/modules/usleep-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-usleep.c
+
+Depends-on:
+time
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-usleep
+check_PROGRAMS += test-usleep
diff --git a/tests/test-usleep.c b/tests/test-usleep.c
new file mode 100644
index 0000000..d6e62eb
--- /dev/null
+++ b/tests/test-usleep.c
@@ -0,0 +1,49 @@
+/* Test of usleep() function.
+   Copyright (C) 2009 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
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Eric Blake <address@hidden>, 2009.  */
+
+#include <config.h>
+
+#include <unistd.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define ASSERT(expr) \
+  do                                                                         \
+    {                                                                        \
+      if (!(expr))                                                           \
+        {                                                                    \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          fflush (stderr);                                                   \
+          abort ();                                                          \
+        }                                                                    \
+    }                                                                        \
+  while (0)
+
+int
+main(void)
+{
+  time_t start = time (NULL);
+  ASSERT (usleep (1000000) == 0);
+  ASSERT (start < time (NULL));
+
+  ASSERT (usleep (0) == 0);
+
+  return 0;
+}
-- 
1.6.5.rc1


>From 6038ee4b827caaf05fa37dbb2304fedb9d0cd6c7 Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Tue, 17 Nov 2009 09:58:18 -0700
Subject: [PATCH 2/2] usleep: use it to simplify tests

Blindly using usleep makes it easier to write tests.

* modules/stat-time-tests (Depends-on): Add usleep.
(configure.ac): Drop usleep check.
* modules/chown-tests (Depends-on, configure.ac): Likewise.
* modules/lchown-tests (Depends-on, configure.ac): Likewise.
* modules/fdutimensat-tests (Depends-on, configure.ac): Likewise.
* modules/futimens-tests (Depends-on, configure.ac): Likewise.
* modules/openat-tests (Depends-on, configure.ac): Likewise.
* modules/utimens-tests (Depends-on, configure.ac): Likewise.
* modules/utimensat-tests (Depends-on, configure.ac): Likewise.
* modules/pipe-filter-gi-tests (Depends-on, configure.ac):
Likewise.
* tests/test-chown.h (nap): Rely on nicer usleep semantics.
* tests/test-lchown.h (nap): Likewise.
* tests/test-pipe-filter-gi2-main.c (small_nap): Likewise.
* tests/test-stat-time.c (nap): Likewise.
* tests/test-utimens-common.h (nap): Update comments.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog                         |   18 ++++++++++++++++++
 modules/chown-tests               |    4 ++--
 modules/fdutimensat-tests         |    2 +-
 modules/futimens-tests            |    2 +-
 modules/lchown-tests              |    4 ++--
 modules/openat-tests              |    4 ++--
 modules/pipe-filter-gi-tests      |    3 +--
 modules/stat-time-tests           |    3 +--
 modules/utimens-tests             |    2 +-
 modules/utimensat-tests           |    2 +-
 tests/test-chown.h                |   17 ++++-------------
 tests/test-lchown.h               |   17 ++++-------------
 tests/test-pipe-filter-gi2-main.c |    4 ----
 tests/test-stat-time.c            |   17 ++++-------------
 tests/test-utimens-common.h       |    6 ++++--
 15 files changed, 46 insertions(+), 59 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cf14b4b..62b5423 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,23 @@
 2009-11-17  Eric Blake  <address@hidden>

+       usleep: use it to simplify tests
+       * modules/stat-time-tests (Depends-on): Add usleep.
+       (configure.ac): Drop usleep check.
+       * modules/chown-tests (Depends-on, configure.ac): Likewise.
+       * modules/lchown-tests (Depends-on, configure.ac): Likewise.
+       * modules/fdutimensat-tests (Depends-on, configure.ac): Likewise.
+       * modules/futimens-tests (Depends-on, configure.ac): Likewise.
+       * modules/openat-tests (Depends-on, configure.ac): Likewise.
+       * modules/utimens-tests (Depends-on, configure.ac): Likewise.
+       * modules/utimensat-tests (Depends-on, configure.ac): Likewise.
+       * modules/pipe-filter-gi-tests (Depends-on, configure.ac):
+       Likewise.
+       * tests/test-chown.h (nap): Rely on nicer usleep semantics.
+       * tests/test-lchown.h (nap): Likewise.
+       * tests/test-pipe-filter-gi2-main.c (small_nap): Likewise.
+       * tests/test-stat-time.c (nap): Likewise.
+       * tests/test-utimens-common.h (nap): Update comments.
+
        usleep: new module
        * modules/usleep: New file.
        * m4/usleep.m4 (gl_FUNC_USLEEP): Likewise.
diff --git a/modules/chown-tests b/modules/chown-tests
index 74e4cfb..a5d5c3a 100644
--- a/modules/chown-tests
+++ b/modules/chown-tests
@@ -6,13 +6,13 @@ Depends-on:
 lstat
 mgetgroups
 progname
-sleep
+usleep
 stat-time
 stdbool
 symlink

 configure.ac:
-AC_CHECK_FUNCS_ONCE([getegid usleep])
+AC_CHECK_FUNCS_ONCE([getegid])

 Makefile.am:
 TESTS += test-chown
diff --git a/modules/fdutimensat-tests b/modules/fdutimensat-tests
index abd813d..75ff374 100644
--- a/modules/fdutimensat-tests
+++ b/modules/fdutimensat-tests
@@ -8,10 +8,10 @@ tests/test-fdutimensat.c
 Depends-on:
 progname
 timespec
+usleep
 utimecmp

 configure.ac:
-AC_CHECK_FUNCS_ONCE([usleep])

 Makefile.am:
 TESTS += test-fdutimensat
diff --git a/modules/futimens-tests b/modules/futimens-tests
index 3a92524..2f57c3b 100644
--- a/modules/futimens-tests
+++ b/modules/futimens-tests
@@ -5,10 +5,10 @@ tests/test-futimens.c

 Depends-on:
 timespec
+usleep
 utimecmp

 configure.ac:
-AC_CHECK_FUNCS_ONCE([usleep])

 Makefile.am:
 TESTS += test-futimens
diff --git a/modules/lchown-tests b/modules/lchown-tests
index c33103a..8b71e75 100644
--- a/modules/lchown-tests
+++ b/modules/lchown-tests
@@ -5,13 +5,13 @@ tests/test-lchown.c
 Depends-on:
 mgetgroups
 progname
-sleep
+usleep
 stat-time
 stdbool
 symlink

 configure.ac:
-AC_CHECK_FUNCS_ONCE([getegid usleep])
+AC_CHECK_FUNCS_ONCE([getegid])

 Makefile.am:
 TESTS += test-lchown
diff --git a/modules/openat-tests b/modules/openat-tests
index 62cef88..e604ca4 100644
--- a/modules/openat-tests
+++ b/modules/openat-tests
@@ -15,13 +15,13 @@ tests/test-unlinkat.c
 Depends-on:
 mgetgroups
 pathmax
-sleep
+usleep
 stat-time
 symlink
 unlinkdir

 configure.ac:
-AC_CHECK_FUNCS_ONCE([getegid usleep])
+AC_CHECK_FUNCS_ONCE([getegid])

 Makefile.am:
 TESTS += test-fchownat test-fstatat test-mkdirat test-openat test-unlinkat
diff --git a/modules/pipe-filter-gi-tests b/modules/pipe-filter-gi-tests
index a02efa8..f0b2969 100644
--- a/modules/pipe-filter-gi-tests
+++ b/modules/pipe-filter-gi-tests
@@ -11,11 +11,10 @@ binary-io
 c-ctype
 read-file
 full-write
-sleep
+usleep
 progname

 configure.ac:
-AC_CHECK_FUNCS_ONCE([usleep])

 Makefile.am:
 TESTS += test-pipe-filter-gi1.sh test-pipe-filter-gi2.sh
diff --git a/modules/stat-time-tests b/modules/stat-time-tests
index 05b2b62..0c0b66a 100644
--- a/modules/stat-time-tests
+++ b/modules/stat-time-tests
@@ -3,10 +3,9 @@ tests/test-stat-time.c

 Depends-on:
 time
-sleep
+usleep

 configure.ac:
-AC_CHECK_FUNCS_ONCE([usleep])

 Makefile.am:
 TESTS += test-stat-time
diff --git a/modules/utimens-tests b/modules/utimens-tests
index 25e7476..6a2d161 100644
--- a/modules/utimens-tests
+++ b/modules/utimens-tests
@@ -8,10 +8,10 @@ tests/test-utimens.c
 Depends-on:
 symlink
 timespec
+usleep
 utimecmp

 configure.ac:
-AC_CHECK_FUNCS_ONCE([usleep])

 Makefile.am:
 TESTS += test-utimens
diff --git a/modules/utimensat-tests b/modules/utimensat-tests
index 1f161ea..ded7f74 100644
--- a/modules/utimensat-tests
+++ b/modules/utimensat-tests
@@ -7,10 +7,10 @@ tests/test-utimensat.c
 Depends-on:
 progname
 timespec
+usleep
 utimecmp

 configure.ac:
-AC_CHECK_FUNCS_ONCE([usleep])

 Makefile.am:
 TESTS += test-utimensat
diff --git a/tests/test-chown.h b/tests/test-chown.h
index 12082e4..ab98682 100644
--- a/tests/test-chown.h
+++ b/tests/test-chown.h
@@ -22,11 +22,6 @@
 static void
 nap (void)
 {
-#if !HAVE_USLEEP
-  /* Assume the worst case file system of FAT, which has a granularity
-     of 2 seconds.  */
-  sleep (2);
-#else /* HAVE_USLEEP */
   static long delay;
   if (!delay)
     {
@@ -38,8 +33,8 @@ nap (void)
          differ, repeat the test one more time (in case we crossed a
          quantization boundary on a file system with 1 second
          resolution).  If we can't observe a difference in only the
-         nanoseconds, then fall back to 2 seconds.  However, note that
-         usleep (2000000) is allowed to fail with EINVAL.  */
+         nanoseconds, then fall back to 1 second if the time is odd,
+         and 2 seconds (needed for FAT) if time is even.  */
       struct stat st1;
       struct stat st2;
       ASSERT (close (creat (BASE "tmp", 0600)) == 0);
@@ -61,13 +56,9 @@ nap (void)
         }
       if (! (st1.st_mtime == st2.st_mtime
              && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
-        delay = 2000000;
+        delay = (st1.st_mtime & 1) ? 1000000 : 2000000;
     }
-  if (delay == 2000000)
-    sleep (2);
-  else
-    usleep (delay);
-#endif /* HAVE_USLEEP */
+  usleep (delay);
 }

 #if !HAVE_GETEGID
diff --git a/tests/test-lchown.h b/tests/test-lchown.h
index a1e8b68..f3ddb7a 100644
--- a/tests/test-lchown.h
+++ b/tests/test-lchown.h
@@ -22,11 +22,6 @@
 static void
 nap (void)
 {
-# if !HAVE_USLEEP
-  /* Assume the worst case file system of FAT, which has a granularity
-     of 2 seconds.  */
-  sleep (2);
-# else /* HAVE_USLEEP */
   static long delay;
   if (!delay)
     {
@@ -38,8 +33,8 @@ nap (void)
          differ, repeat the test one more time (in case we crossed a
          quantization boundary on a file system with 1 second
          resolution).  If we can't observe a difference in only the
-         nanoseconds, then fall back to 2 seconds.  However, note that
-         usleep (2000000) is allowed to fail with EINVAL.  */
+         nanoseconds, then fall back to 1 second if the time is odd,
+         and 2 seconds (needed for FAT) if time is even.  */
       struct stat st1;
       struct stat st2;
       ASSERT (close (creat (BASE "tmp", 0600)) == 0);
@@ -61,13 +56,9 @@ nap (void)
         }
       if (! (st1.st_mtime == st2.st_mtime
              && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
-        delay = 2000000;
+        delay = (st1.st_mtime & 1) ? 1000000 : 2000000;
     }
-  if (delay == 2000000)
-    sleep (2);
-  else
-    usleep (delay);
-# endif /* HAVE_USLEEP */
+  usleep (delay);
 }
 #endif /* !TEST_CHOWN_NAP */

diff --git a/tests/test-pipe-filter-gi2-main.c 
b/tests/test-pipe-filter-gi2-main.c
index 7be285e..b9a0b11 100644
--- a/tests/test-pipe-filter-gi2-main.c
+++ b/tests/test-pipe-filter-gi2-main.c
@@ -45,11 +45,7 @@
 static void
 small_nap (void)
 {
-#if HAVE_USLEEP
   usleep (100000);
-#else
-  sleep (1);
-#endif
 }

 static char static_buf[5];
diff --git a/tests/test-stat-time.c b/tests/test-stat-time.c
index b88af3d..85ada87 100644
--- a/tests/test-stat-time.c
+++ b/tests/test-stat-time.c
@@ -96,11 +96,6 @@ do_stat (const char *filename, struct stat *p)
 static void
 nap (void)
 {
-#if !HAVE_USLEEP
-  /* Assume the worst case file system of FAT, which has a granularity
-     of 2 seconds.  */
-  sleep (2);
-#else /* HAVE_USLEEP */
   static long delay;
   if (!delay)
     {
@@ -112,8 +107,8 @@ nap (void)
          differ, repeat the test one more time (in case we crossed a
          quantization boundary on a file system with 1 second
          resolution).  If we can't observe a difference in only the
-         nanoseconds, then fall back to 2 seconds.  However, note that
-         usleep (2000000) is allowed to fail with EINVAL.  */
+         nanoseconds, then fall back to 1 second if the time is odd,
+         and 2 seconds (needed for FAT) if time is even.  */
       struct stat st1;
       struct stat st2;
       ASSERT (stat ("t-stt-stamp1", &st1) == 0);
@@ -133,13 +128,9 @@ nap (void)
         }
       if (! (st1.st_mtime == st2.st_mtime
              && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
-        delay = 2000000;
+        delay = (st1.st_mtime & 1) ? 1000000 : 2000000;
     }
-  if (delay == 2000000)
-    sleep (2);
-  else
-    usleep (delay);
-#endif /* HAVE_USLEEP */
+  usleep (delay);
 }

 static void
diff --git a/tests/test-utimens-common.h b/tests/test-utimens-common.h
index 1945f23..6c404cc 100644
--- a/tests/test-utimens-common.h
+++ b/tests/test-utimens-common.h
@@ -58,8 +58,10 @@ enum {
 static void
 nap (void)
 {
-  /* Systems that lack usleep also lack subsecond timestamps.  Our
-     usage of utimecmp allows equality, so we don't need to sleep.  */
+  /* Systems that lack usleep also lack subsecond timestamps, and have
+     a quantization boundary equal to the resolution.  Our usage of
+     utimecmp allows equality, so no need to waste 980 milliseconds
+     if the replacement usleep rounds to 1 second.  */
 #if HAVE_USLEEP
   usleep (20 * 1000); /* 20 milliseconds.  */
 #endif
-- 
1.6.5.rc1


reply via email to

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