diff options
author | Werner Koch <[email protected]> | 2016-10-31 11:20:33 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2016-10-31 11:24:45 +0000 |
commit | ad491ceec6145b3781a05dc7b4a36052abeeb4b4 (patch) | |
tree | e503edd6bf7e5627eb231d45318d27a7fb2114f9 /common/sysutils.c | |
parent | w32: Fix PKG_CONFIG_LIBDIR in --build-w32 (diff) | |
download | gnupg-ad491ceec6145b3781a05dc7b4a36052abeeb4b4.tar.gz gnupg-ad491ceec6145b3781a05dc7b4a36052abeeb4b4.zip |
common: New function gnupg_usleep.
* configure.ac (HAVE_NANOSLEEP): Test for nanosleep.
* common/sysutils.c: Always include time.h.
(gnupg_usleep): New.
--
This function has been compiled from nPth and Libassuan.
Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'common/sysutils.c')
-rw-r--r-- | common/sysutils.c | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/common/sysutils.c b/common/sysutils.c index 6eea90ed6..28d4cdee1 100644 --- a/common/sysutils.c +++ b/common/sysutils.c @@ -49,8 +49,8 @@ # include <asm/sysinfo.h> # include <asm/unistd.h> #endif +#include <time.h> #ifdef HAVE_SETRLIMIT -# include <time.h> # include <sys/time.h> # include <sys/resource.h> #endif @@ -307,6 +307,50 @@ gnupg_sleep (unsigned int seconds) } +/* Wrapper around the platforms usleep function. This one won't wake + * up before the sleep time has really elapsed. When build with nPth + * it merely calls npth_usleep and thus suspends only the current + * thread. */ +void +gnupg_usleep (unsigned int usecs) +{ +#if defined(USE_NPTH) + + npth_usleep (usecs); + +#elif defined(HAVE_W32_SYSTEM) + + Sleep ((usecs + 999) / 1000); + +#elif defined(HAVE_NANOSLEEP) + + if (usecs) + { + struct timespec req; + struct timespec rem; + + req.tv_sec = 0; + req.tv_nsec = usecs * 1000; + + while (nanosleep (&req, &rem) < 0 && errno == EINTR) + req = rem; + } + +#else /*Standard Unix*/ + + if (usecs) + { + struct timeval tv; + + tv.tv_sec = usecs / 1000000; + tv.tv_usec = usecs % 1000000; + select (0, NULL, NULL, NULL, &tv); + } + +#endif +} + + /* This function is a NOP for POSIX systems but required under Windows as the file handles as returned by OS calls (like CreateFile) are different from the libc file descriptors (like open). This function |