From 99d7907611725e23b6fad3ae7acff4926504e687 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 14 Dec 2009 17:07:19 -0800 Subject: [PATCH] Fix usleep(3) return type to be POSIX-compliant. POSIX usleep(3) returns 0 on successful completion, -1 otherwise: http://www.opengroup.org/onlinepubs/007908799/xsh/usleep.html This was found by an external user porting native code: http://groups.google.com/group/android-porting/browse_thread/thread/674848f001db0292 --- libc/include/unistd.h | 2 +- libc/unistd/usleep.c | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/libc/include/unistd.h b/libc/include/unistd.h index 67cb5fe3b..954f34e2f 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -145,7 +145,7 @@ extern int ftruncate(int, off_t); extern int pause(void); extern unsigned int alarm(unsigned int); extern unsigned int sleep(unsigned int); -extern void usleep(unsigned long); +extern int usleep(unsigned long); extern int gethostname(char *, size_t); extern int sethostname(const char *, size_t); diff --git a/libc/unistd/usleep.c b/libc/unistd/usleep.c index 75458b159..19e8ee892 100644 --- a/libc/unistd/usleep.c +++ b/libc/unistd/usleep.c @@ -28,7 +28,7 @@ #include #include -void usleep(unsigned long usec) +int usleep(unsigned long usec) { struct timespec ts; @@ -43,10 +43,13 @@ void usleep(unsigned long usec) for (;;) { - if ( nanosleep( &ts, &ts ) >= 0 ) - break; + if ( nanosleep( &ts, &ts ) == 0 ) + return 0; + // We try again if the nanosleep failure is EINTR. + // The other possible failures are EINVAL (which we should pass through), + // and ENOSYS, which doesn't happen. if ( errno != EINTR ) - break; + return -1; } }