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
This commit is contained in:
Elliott Hughes
2009-12-14 17:07:19 -08:00
parent 278d960aae
commit 99d7907611
2 changed files with 8 additions and 5 deletions

View File

@@ -145,7 +145,7 @@ extern int ftruncate(int, off_t);
extern int pause(void); extern int pause(void);
extern unsigned int alarm(unsigned int); extern unsigned int alarm(unsigned int);
extern unsigned int sleep(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 gethostname(char *, size_t);
extern int sethostname(const char *, size_t); extern int sethostname(const char *, size_t);

View File

@@ -28,7 +28,7 @@
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
void usleep(unsigned long usec) int usleep(unsigned long usec)
{ {
struct timespec ts; struct timespec ts;
@@ -43,10 +43,13 @@ void usleep(unsigned long usec)
for (;;) for (;;)
{ {
if ( nanosleep( &ts, &ts ) >= 0 ) if ( nanosleep( &ts, &ts ) == 0 )
break; 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 ) if ( errno != EINTR )
break; return -1;
} }
} }