1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #if defined(LIBC_SCCS) && !defined(lint) 8 static char sccsid[] = "@(#)sleep.c 5.3 (Berkeley) 09/30/87"; 9 #endif LIBC_SCCS and not lint 10 11 #include <sys/time.h> 12 #include <signal.h> 13 14 #define setvec(vec, a) \ 15 vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0 16 17 static int ringring; 18 19 sleep(n) 20 unsigned n; 21 { 22 int sleepx(); 23 long omask; 24 struct itimerval itv, oitv; 25 register struct itimerval *itp = &itv; 26 struct sigvec vec, ovec; 27 28 if (n == 0) 29 return; 30 timerclear(&itp->it_interval); 31 timerclear(&itp->it_value); 32 if (setitimer(ITIMER_REAL, itp, &oitv) < 0) 33 return; 34 itp->it_value.tv_sec = n; 35 if (timerisset(&oitv.it_value)) { 36 if (timercmp(&oitv.it_value, &itp->it_value, >)) 37 oitv.it_value.tv_sec -= itp->it_value.tv_sec; 38 else { 39 itp->it_value = oitv.it_value; 40 /* 41 * This is a hack, but we must have time to 42 * return from the setitimer after the alarm 43 * or else it'll be restarted. And, anyway, 44 * sleep never did anything more than this before. 45 */ 46 oitv.it_value.tv_sec = 1; 47 oitv.it_value.tv_usec = 0; 48 } 49 } 50 setvec(vec, sleepx); 51 (void) sigvec(SIGALRM, &vec, &ovec); 52 omask = sigblock(sigmask(SIGALRM)); 53 ringring = 0; 54 (void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0); 55 while (!ringring) 56 sigpause(omask &~ sigmask(SIGALRM)); 57 (void) sigvec(SIGALRM, &ovec, (struct sigvec *)0); 58 (void) sigsetmask(omask); 59 (void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0); 60 } 61 62 static 63 sleepx() 64 { 65 66 ringring = 1; 67 } 68