xref: /onnv-gate/usr/src/lib/libbc/libc/gen/4.2/sleep.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
2*722Smuffin  * copyright (c) 1980 Regents of the University of California.
30Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
40Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
50Sstevel@tonic-gate  */
60Sstevel@tonic-gate 
7*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
8*722Smuffin 
90Sstevel@tonic-gate #include <sys/time.h>
100Sstevel@tonic-gate #include <signal.h>
110Sstevel@tonic-gate 
120Sstevel@tonic-gate #define	setvec(vec, a) \
130Sstevel@tonic-gate 	vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
140Sstevel@tonic-gate 
150Sstevel@tonic-gate static int ringring;
160Sstevel@tonic-gate 
17*722Smuffin static void	sleepx(void);
18*722Smuffin 
19*722Smuffin void
sleep(unsigned n)20*722Smuffin sleep(unsigned n)
210Sstevel@tonic-gate {
220Sstevel@tonic-gate 	int omask;
230Sstevel@tonic-gate 	struct itimerval itv, oitv;
24*722Smuffin 	struct itimerval *itp = &itv;
250Sstevel@tonic-gate 	struct sigvec vec, ovec;
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 	if (n == 0)
280Sstevel@tonic-gate 		return;
290Sstevel@tonic-gate 	timerclear(&itp->it_interval);
300Sstevel@tonic-gate 	timerclear(&itp->it_value);
310Sstevel@tonic-gate 	if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
320Sstevel@tonic-gate 		return;
330Sstevel@tonic-gate 	itp->it_value.tv_sec = n;
340Sstevel@tonic-gate 	if (timerisset(&oitv.it_value)) {
350Sstevel@tonic-gate 		if (timercmp(&oitv.it_value, &itp->it_value, >))
360Sstevel@tonic-gate 			oitv.it_value.tv_sec -= itp->it_value.tv_sec;
370Sstevel@tonic-gate 		else {
380Sstevel@tonic-gate 			itp->it_value = oitv.it_value;
390Sstevel@tonic-gate 			/*
400Sstevel@tonic-gate 			 * This is a hack, but we must have time to
410Sstevel@tonic-gate 			 * return from the setitimer after the alarm
420Sstevel@tonic-gate 			 * or else it'll be restarted.  And, anyway,
430Sstevel@tonic-gate 			 * sleep never did anything more than this before.
440Sstevel@tonic-gate 			 */
450Sstevel@tonic-gate 			oitv.it_value.tv_sec = 1;
460Sstevel@tonic-gate 			oitv.it_value.tv_usec = 0;
470Sstevel@tonic-gate 		}
480Sstevel@tonic-gate 	}
490Sstevel@tonic-gate 	setvec(vec, sleepx);
500Sstevel@tonic-gate 	(void) sigvec(SIGALRM, &vec, &ovec);
510Sstevel@tonic-gate 	omask = sigblock(sigmask(SIGALRM));
520Sstevel@tonic-gate 	ringring = 0;
530Sstevel@tonic-gate 	(void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0);
540Sstevel@tonic-gate 	while (!ringring)
550Sstevel@tonic-gate 		sigpause(omask &~ sigmask(SIGALRM));
560Sstevel@tonic-gate 	(void) sigvec(SIGALRM, &ovec, (struct sigvec *)0);
570Sstevel@tonic-gate 	(void) sigsetmask(omask);
580Sstevel@tonic-gate 	(void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
590Sstevel@tonic-gate }
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static void
sleepx(void)62*722Smuffin sleepx(void)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	ringring = 1;
660Sstevel@tonic-gate }
67