xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/usleep.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1985 Regents of the University of California.
80Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
90Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
100Sstevel@tonic-gate  */
110Sstevel@tonic-gate 
12*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
13*722Smuffin 
140Sstevel@tonic-gate #include <unistd.h>
150Sstevel@tonic-gate #include <sys/time.h>
160Sstevel@tonic-gate #include <signal.h>
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #define	USPS	1000000		/* number of microseconds in a second */
190Sstevel@tonic-gate #define	TICK	(USPS / _sysconf(_SC_CLK_TCK))
200Sstevel@tonic-gate 
210Sstevel@tonic-gate #define	setvec(vec, a) \
220Sstevel@tonic-gate 	vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
230Sstevel@tonic-gate 
240Sstevel@tonic-gate static int ringring;
250Sstevel@tonic-gate 
26*722Smuffin void
usleep(unsigned n)27*722Smuffin usleep(unsigned n)
280Sstevel@tonic-gate {
290Sstevel@tonic-gate 	static void sleepx();
300Sstevel@tonic-gate 	int omask;
310Sstevel@tonic-gate 	struct itimerval itv, oitv;
32*722Smuffin 	struct itimerval *itp = &itv;
330Sstevel@tonic-gate 	struct sigvec vec, ovec;
340Sstevel@tonic-gate 
350Sstevel@tonic-gate 	if (n == 0)
360Sstevel@tonic-gate 		return;
370Sstevel@tonic-gate 	timerclear(&itp->it_interval);
380Sstevel@tonic-gate 	timerclear(&itp->it_value);
390Sstevel@tonic-gate 	if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
400Sstevel@tonic-gate 		return;
410Sstevel@tonic-gate 	itp->it_value.tv_sec = n / USPS;
420Sstevel@tonic-gate 	itp->it_value.tv_usec = n % USPS;
430Sstevel@tonic-gate 	if (timerisset(&oitv.it_value)) {
440Sstevel@tonic-gate 		if (timercmp(&oitv.it_value, &itp->it_value, >)) {
450Sstevel@tonic-gate 			oitv.it_value.tv_sec -= itp->it_value.tv_sec;
460Sstevel@tonic-gate 			oitv.it_value.tv_usec -= itp->it_value.tv_usec;
470Sstevel@tonic-gate 			if (oitv.it_value.tv_usec < 0) {
480Sstevel@tonic-gate 				oitv.it_value.tv_usec += USPS;
490Sstevel@tonic-gate 				oitv.it_value.tv_sec--;
500Sstevel@tonic-gate 			}
510Sstevel@tonic-gate 		} else {
520Sstevel@tonic-gate 			itp->it_value = oitv.it_value;
530Sstevel@tonic-gate 			oitv.it_value.tv_sec = 0;
540Sstevel@tonic-gate 			oitv.it_value.tv_usec = 2 * TICK;
550Sstevel@tonic-gate 		}
560Sstevel@tonic-gate 	}
570Sstevel@tonic-gate 	setvec(vec, sleepx);
580Sstevel@tonic-gate 	(void) sigvec(SIGALRM, &vec, &ovec);
590Sstevel@tonic-gate 	omask = sigblock(sigmask(SIGALRM));
600Sstevel@tonic-gate 	ringring = 0;
610Sstevel@tonic-gate 	(void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0);
620Sstevel@tonic-gate 	while (!ringring)
630Sstevel@tonic-gate 		sigpause(omask &~ sigmask(SIGALRM));
640Sstevel@tonic-gate 	(void) sigvec(SIGALRM, &ovec, (struct sigvec *)0);
650Sstevel@tonic-gate 	(void) sigsetmask(omask);
660Sstevel@tonic-gate 	(void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static void
sleepx(void)70*722Smuffin sleepx(void)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	ringring = 1;
740Sstevel@tonic-gate }
75