diff options
author | Thorsten Lockert <tholo@cvs.openbsd.org> | 1997-04-25 04:20:43 +0000 |
---|---|---|
committer | Thorsten Lockert <tholo@cvs.openbsd.org> | 1997-04-25 04:20:43 +0000 |
commit | b7983e6188938fbbd3e8279bcb5dcb51c68e17e7 (patch) | |
tree | eb92acb0c84d992e43e3a02afd33d8c78a71c6f5 /lib/libc | |
parent | d56a8169be2d8ef87ada7d86da6f58080962c05c (diff) |
Use nanosleep(2)
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/gen/sleep.c | 91 | ||||
-rw-r--r-- | lib/libc/gen/usleep.c | 76 |
2 files changed, 15 insertions, 152 deletions
diff --git a/lib/libc/gen/sleep.c b/lib/libc/gen/sleep.c index 9245e47f914..6af594f7c25 100644 --- a/lib/libc/gen/sleep.c +++ b/lib/libc/gen/sleep.c @@ -32,98 +32,25 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: sleep.c,v 1.6 1996/09/30 12:21:30 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: sleep.c,v 1.7 1997/04/25 04:20:41 tholo Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/time.h> -#include <signal.h> -#include <unistd.h> - -static void sleephandler __P((int)); -static volatile int ringring; unsigned int sleep(seconds) unsigned int seconds; { - struct itimerval itv, oitv; - struct sigaction act, oact; - struct timeval diff; - sigset_t set, oset; - - if (!seconds) - return 0; - - sigemptyset(&set); - sigaddset(&set, SIGALRM); - sigprocmask(SIG_BLOCK, &set, &oset); - - act.sa_handler = sleephandler; - act.sa_flags = 0; - sigemptyset(&act.sa_mask); - sigaction(SIGALRM, &act, &oact); - - timerclear(&itv.it_interval); - itv.it_value.tv_sec = seconds; - itv.it_value.tv_usec = 0; - timerclear(&diff); - setitimer(ITIMER_REAL, &itv, &oitv); + struct timespec rqt, rmt; - if (timerisset(&oitv.it_value)) { - if (timercmp(&oitv.it_value, &itv.it_value, >)) { - timersub(&oitv.it_value, &itv.it_value, &oitv.it_value); - } else { - /* - * The existing timer was scheduled to fire - * before ours, so we compute the time diff - * so we can add it back in the end. - */ - timersub(&itv.it_value, &oitv.it_value, &diff); - itv.it_value = oitv.it_value; - /* - * This is a hack, but we must have time to return - * from the setitimer after the alarm or else it'll - * be restarted. And, anyway, sleep never did - * anything more than this before. - */ - oitv.it_value.tv_sec = 1; - oitv.it_value.tv_usec = 0; + if (seconds == 0) + return(0); - setitimer(ITIMER_REAL, &itv, NULL); - } - } + rqt.tv_sec = seconds; + rqt.tv_nsec = 0; - set = oset; - sigdelset(&set, SIGALRM); - ringring = 0; - (void) sigsuspend(&set); + if (nanosleep(&rqt, &rmt) < 0) + ; - if (!ringring) { - struct itimerval nulltv; - /* - * Interrupted by other signal; allow for pending - * SIGALRM to be processed before resetting handler, - * after first turning off the timer. - */ - timerclear(&nulltv.it_interval); - timerclear(&nulltv.it_value); - (void) setitimer(ITIMER_REAL, &nulltv, &itv); - } else - timerclear(&itv.it_value); - sigprocmask(SIG_SETMASK, &oset, NULL); - sigaction(SIGALRM, &oact, NULL); - (void) setitimer(ITIMER_REAL, &oitv, NULL); - - if (timerisset(&diff)) - timeradd(&itv.it_value, &diff, &itv.it_value); - - return (itv.it_value.tv_sec); -} - -/* ARGSUSED */ -static void -sleephandler(sig) - int sig; -{ - ringring = 1; + return(rmt.tv_sec); } diff --git a/lib/libc/gen/usleep.c b/lib/libc/gen/usleep.c index 6aedc44de26..eae3659a6fe 100644 --- a/lib/libc/gen/usleep.c +++ b/lib/libc/gen/usleep.c @@ -32,86 +32,22 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: usleep.c,v 1.4 1996/09/15 09:31:11 tholo Exp $"; +static char rcsid[] = "$OpenBSD: usleep.c,v 1.5 1997/04/25 04:20:42 tholo Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/time.h> -#include <signal.h> -#include <unistd.h> - -#define TICK 10000 /* system clock resolution in microseconds */ - -static void sleephandler __P((int)); -static volatile int ringring; void usleep(useconds) unsigned int useconds; { - struct itimerval itv, oitv; - struct sigaction act, oact; - sigset_t set, oset; + struct timespec rqt; - if (!useconds) + if (useconds == 0) return; - sigemptyset(&set); - sigaddset(&set, SIGALRM); - sigprocmask(SIG_BLOCK, &set, &oset); - - act.sa_handler = sleephandler; - act.sa_flags = 0; - sigemptyset(&act.sa_mask); - sigaction(SIGALRM, &act, &oact); - - timerclear(&itv.it_interval); - itv.it_value.tv_sec = useconds / 1000000; - itv.it_value.tv_usec = useconds % 1000000; - setitimer(ITIMER_REAL, &itv, &oitv); - - if (timerisset(&oitv.it_value)) { - if (timercmp(&oitv.it_value, &itv.it_value, >)) { - timersub(&oitv.it_value, &itv.it_value, &oitv.it_value); - } else { - itv.it_value = oitv.it_value; - /* - * This is a hack, but we must have time to return - * from the setitimer after the alarm or else it'll - * be restarted. And, anyway, sleep never did - * anything more than this before. - */ - oitv.it_value.tv_sec = 0; - oitv.it_value.tv_usec = 2 * TICK; + rqt.tv_sec = useconds / 1000000; + rqt.tv_nsec = (useconds % 1000000) * 1000; - setitimer(ITIMER_REAL, &itv, NULL); - } - } - - set = oset; - sigdelset(&set, SIGALRM); - ringring = 0; - (void) sigsuspend(&set); - - if (!ringring) { - struct itimerval nulltv; - /* - * Interrupted by other signal; allow for pending - * SIGALRM to be processed before resetting handler, - * after first turning off the timer. - */ - timerclear(&nulltv.it_interval); - timerclear(&nulltv.it_value); - (void) setitimer(ITIMER_REAL, &nulltv, NULL); - } - sigprocmask(SIG_SETMASK, &oset, NULL); - sigaction(SIGALRM, &oact, NULL); - (void) setitimer(ITIMER_REAL, &oitv, NULL); -} - -/* ARGSUSED */ -static void -sleephandler(sig) - int sig; -{ - ringring = 1; + nanosleep(&rqt, NULL); } |