summaryrefslogtreecommitdiffstats
path: root/include/c.h
diff options
context:
space:
mode:
authorKarel Zak2014-01-24 13:04:14 +0100
committerKarel Zak2014-01-24 13:04:14 +0100
commita5bd7939961aaa6e39f3b298e556063cfd97d75a (patch)
treec7b5425996072f268f5cbb6c5496a4a2a95a5256 /include/c.h
parentflock: Change the 'exit status' man page section to make more sense (diff)
downloadkernel-qcow2-util-linux-a5bd7939961aaa6e39f3b298e556063cfd97d75a.tar.gz
kernel-qcow2-util-linux-a5bd7939961aaa6e39f3b298e556063cfd97d75a.tar.xz
kernel-qcow2-util-linux-a5bd7939961aaa6e39f3b298e556063cfd97d75a.zip
include/c.h: prefer nanosleep() over usleep()
Let's use nanosleep() although if usleep() exists. The nanosleep function does no interact with signals and other timers. The patch introduces xusleep() as replacement to libc (or our fallback) usleep(). Yes, we don't want to use struct timespec + nanosleep() everywhere in code as nano-time resolution is useless for us. The patch also enlarges delays in some busy wait loops. It seems enough to try read/write 4x per second. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'include/c.h')
-rw-r--r--include/c.h17
1 files changed, 12 insertions, 5 deletions
diff --git a/include/c.h b/include/c.h
index 4a9bf3d42..a192fb1dd 100644
--- a/include/c.h
+++ b/include/c.h
@@ -253,20 +253,27 @@ static inline size_t get_hostname_max(void)
return 64;
}
-#ifndef HAVE_USLEEP
/*
- * This function is marked obsolete in POSIX.1-2001 and removed in
- * POSIX.1-2008. It is replaced with nanosleep().
+ * The usleep function is marked obsolete in POSIX.1-2001 and removed in
+ * POSIX.1-2008. This is replaced with nanosleep() that provides more
+ * advantages (like no interaction with signals and other timer functions.
*/
-static inline int usleep(useconds_t usec)
+#include <time.h>
+
+static inline int xusleep(useconds_t usec)
{
+#ifdef HAVE_NANOSLEEP
struct timespec waittime = {
.tv_sec = usec / 1000000L,
.tv_nsec = (usec % 1000000L) * 1000
};
return nanosleep(&waittime, NULL);
-}
+#elif defined(HAVE_USLEEP)
+ return usleep(usec);
+#else
+# error "System with usleep() or nanosleep() required!"
#endif
+}
/*
* Constant strings for usage() functions. For more info see