xref: /titanic_41/usr/src/lib/libbc/libc/gen/4.2/sleep.c (revision e0724c534a46ca4754330bc022bf1e2a68f5bb93)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
8 
9 #include <sys/time.h>
10 #include <signal.h>
11 
12 #define	setvec(vec, a) \
13 	vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
14 
15 static int ringring;
16 
17 static void	sleepx(void);
18 
19 void
20 sleep(unsigned n)
21 {
22 	int omask;
23 	struct itimerval itv, oitv;
24 	struct itimerval *itp = &itv;
25 	struct sigvec vec, ovec;
26 
27 	if (n == 0)
28 		return;
29 	timerclear(&itp->it_interval);
30 	timerclear(&itp->it_value);
31 	if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
32 		return;
33 	itp->it_value.tv_sec = n;
34 	if (timerisset(&oitv.it_value)) {
35 		if (timercmp(&oitv.it_value, &itp->it_value, >))
36 			oitv.it_value.tv_sec -= itp->it_value.tv_sec;
37 		else {
38 			itp->it_value = oitv.it_value;
39 			/*
40 			 * This is a hack, but we must have time to
41 			 * return from the setitimer after the alarm
42 			 * or else it'll be restarted.  And, anyway,
43 			 * sleep never did anything more than this before.
44 			 */
45 			oitv.it_value.tv_sec = 1;
46 			oitv.it_value.tv_usec = 0;
47 		}
48 	}
49 	setvec(vec, sleepx);
50 	(void) sigvec(SIGALRM, &vec, &ovec);
51 	omask = sigblock(sigmask(SIGALRM));
52 	ringring = 0;
53 	(void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0);
54 	while (!ringring)
55 		sigpause(omask &~ sigmask(SIGALRM));
56 	(void) sigvec(SIGALRM, &ovec, (struct sigvec *)0);
57 	(void) sigsetmask(omask);
58 	(void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
59 }
60 
61 static void
62 sleepx(void)
63 {
64 
65 	ringring = 1;
66 }
67