]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/c.h: prefer nanosleep() over usleep()
authorKarel Zak <kzak@redhat.com>
Fri, 24 Jan 2014 12:04:14 +0000 (13:04 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 24 Jan 2014 12:04:14 +0000 (13:04 +0100)
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>
include/all-io.h
include/c.h
libmount/src/lock.c
login-utils/sulogin.c
sys-utils/hwclock-kd.c
sys-utils/rtcwake.c
term-utils/agetty.c
text-utils/tailf.c

index 424ab7d3fc7c2f8a70d3233d6f3e5e4343975d34..2546cff1bed83c90b006cbdef5e948d44ece3aa2 100644 (file)
@@ -29,7 +29,7 @@ static inline int write_all(int fd, const void *buf, size_t count)
                } else if (errno != EINTR && errno != EAGAIN)
                        return -1;
                if (errno == EAGAIN)    /* Try later, *sigh* */
-                       usleep(10000);
+                       xusleep(250000);
        }
        return 0;
 }
@@ -49,7 +49,7 @@ static inline int fwrite_all(const void *ptr, size_t size,
                } else if (errno != EINTR && errno != EAGAIN)
                        return -1;
                if (errno == EAGAIN)    /* Try later, *sigh* */
-                       usleep(10000);
+                       xusleep(250000);
        }
        return 0;
 }
@@ -65,8 +65,10 @@ static inline ssize_t read_all(int fd, char *buf, size_t count)
                ret = read(fd, buf, count);
                if (ret <= 0) {
                        if ((errno == EAGAIN || errno == EINTR || ret == 0) &&
-                           (tries++ < 5))
+                           (tries++ < 5)) {
+                               xusleep(250000);
                                continue;
+                       }
                        return c ? c : -1;
                }
                if (ret > 0)
index 4a9bf3d42e5e99c3472056deac7b0f15a7356d3c..a192fb1dd6fd052eb77e12c78d7d8bdc142184c2 100644 (file)
@@ -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
index d9b6f59395bb469d9ccd7043eb6cd9318e424a1b..734757d81e7fb4bde45d259c37c4b46c9264bd9c 100644 (file)
@@ -650,7 +650,7 @@ int test_lock(struct libmnt_test *ts, int argc, char *argv[])
                if (synctime && synctime - tv.tv_sec > 1) {
                        usecs = ((synctime - tv.tv_sec) * 1000000UL) -
                                                (1000000UL - tv.tv_usec);
-                       usleep(usecs);
+                       xusleep(usecs);
                }
        }
 
@@ -676,7 +676,7 @@ int test_lock(struct libmnt_test *ts, int argc, char *argv[])
                 * concurrent processes happy.
                 */
                if (synctime)
-                       usleep(25000);
+                       xusleep(25000);
        }
 
        return 0;
index 4560ee0fbe1432412e91aee1f3f71d1fff382950..37d006dde40c51c0c0af4abebc8c9a799f6bc603 100644 (file)
@@ -590,7 +590,7 @@ static char *getpasswd(struct console *con)
        while (cp->eol == '\0') {
                if (read(fd, &c, 1) < 1) {
                        if (errno == EINTR || errno == EAGAIN) {
-                               usleep(1000);
+                               xusleep(250000);
                                continue;
                        }
                        ret = (char*)0;
@@ -993,7 +993,7 @@ int main(int argc, char **argv)
                        if (*usemask & (1<<con->id))
                                continue;
                        kill(con->pid, SIGHUP);
-                       usleep(5000);
+                       usleep(50000);
                        kill(con->pid, SIGKILL);
                }
        }
index 9ed83436a60391daadf6625080970fd64d3411f5..8e67009cb91065855bff363bf082675f666c9cff 100644 (file)
@@ -66,7 +66,7 @@ static int synchronize_to_clock_tick_kd(void)
                 *  A2000 RTCs and simply hangs after some time. Inserting a
                 *  sleep helps."
                 */
-               usleep(1);
+               xusleep(1);
 
                if (ioctl(con_fd, KDGHWCLK, &nowtime) == -1) {
                        warn(_("KDGHWCLK ioctl to read time failed in loop"));
index eedf78d0c0c9ca84e9154d56176a568cdfab8b8c..51ffb3c5a004698bb74c615e5696a43d9341e1bd 100644 (file)
@@ -560,7 +560,7 @@ int main(int argc, char **argv)
                                program_invocation_short_name, suspend, devname,
                                ctime(&alarm));
                fflush(stdout);
-               usleep(10 * 1000);
+               xusleep(10 * 1000);
        }
 
        if (strcmp(suspend, "no") == 0) {
index baa1b38cb88a617c82065a64ef1033845a86346d..7c0579c630efd6175b521a83e6d835a4680ca93f 100644 (file)
@@ -1589,7 +1589,7 @@ static char *get_logname(struct options *op, struct termios *tp, struct chardata
                                /* The terminal could be open with O_NONBLOCK when
                                 * -L (force CLOCAL) is specified...  */
                                if (errno == EINTR || errno == EAGAIN) {
-                                       usleep(250000);
+                                       xusleep(250000);
                                        continue;
                                }
                                switch (errno) {
index 2f611a4cb875da0338f13ca3e982e80636046528..d2366a6a009e8f6673da8485cdd753348868c910 100644 (file)
@@ -133,7 +133,7 @@ watch_file(const char *filename, off_t *size)
 {
        do {
                roll_file(filename, size);
-               usleep(250000);
+               xusleep(250000);
        } while(1);
 }