xref: /titanic_44/usr/src/lib/libbc/libc/gen/common/ualarm.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*7c478bd9Sstevel@tonic-gate 	  /* from UCB 5.1 85/06/05 */
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate /*
5*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1985 Regents of the University of California.
6*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
7*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
8*7c478bd9Sstevel@tonic-gate  */
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate #define	USPS	1000000		/* # of microseconds in a second */
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate /*
15*7c478bd9Sstevel@tonic-gate  * Generate a SIGALRM signal in ``usecs'' microseconds.
16*7c478bd9Sstevel@tonic-gate  * If ``reload'' is non-zero, keep generating SIGALRM
17*7c478bd9Sstevel@tonic-gate  * every ``reload'' microseconds after the first signal.
18*7c478bd9Sstevel@tonic-gate  */
19*7c478bd9Sstevel@tonic-gate unsigned
ualarm(usecs,reload)20*7c478bd9Sstevel@tonic-gate ualarm(usecs, reload)
21*7c478bd9Sstevel@tonic-gate 	register unsigned usecs;
22*7c478bd9Sstevel@tonic-gate 	register unsigned reload;
23*7c478bd9Sstevel@tonic-gate {
24*7c478bd9Sstevel@tonic-gate 	struct itimerval new, old;
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate 	new.it_interval.tv_usec = reload % USPS;
27*7c478bd9Sstevel@tonic-gate 	new.it_interval.tv_sec = reload / USPS;
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 	new.it_value.tv_usec = usecs % USPS;
30*7c478bd9Sstevel@tonic-gate 	new.it_value.tv_sec = usecs / USPS;
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 	if (setitimer(ITIMER_REAL, &new, &old) == 0)
33*7c478bd9Sstevel@tonic-gate 		return (old.it_value.tv_sec * USPS + old.it_value.tv_usec);
34*7c478bd9Sstevel@tonic-gate 	/* else */
35*7c478bd9Sstevel@tonic-gate 		return (-1);
36*7c478bd9Sstevel@tonic-gate }
37