xref: /onnv-gate/usr/src/lib/libc/port/gen/ualarm.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
2*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
70Sstevel@tonic-gate /*	  All Rights Reserved  	*/
80Sstevel@tonic-gate 
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate  * Copyright (c) 1985 Regents of the University of California.
110Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
120Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
130Sstevel@tonic-gate  */
140Sstevel@tonic-gate 
15*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
16*6812Sraf 
17*6812Sraf #include "lint.h"
180Sstevel@tonic-gate #include <sys/types.h>
190Sstevel@tonic-gate #include <sys/time.h>
200Sstevel@tonic-gate #include <unistd.h>
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #define	USPS	1000000		/* # of microseconds in a second */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate /*
250Sstevel@tonic-gate  * Generate a SIGALRM signal in ``usecs'' microseconds.
260Sstevel@tonic-gate  * If ``reload'' is non-zero, keep generating SIGALRM
270Sstevel@tonic-gate  * every ``reload'' microseconds after the first signal.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate useconds_t
ualarm(useconds_t usecs,useconds_t reload)300Sstevel@tonic-gate ualarm(useconds_t usecs, useconds_t reload)
310Sstevel@tonic-gate {
320Sstevel@tonic-gate 	struct itimerval new, old;
330Sstevel@tonic-gate 
340Sstevel@tonic-gate 	new.it_interval.tv_usec = reload % USPS;
350Sstevel@tonic-gate 	new.it_interval.tv_sec = reload / USPS;
360Sstevel@tonic-gate 
370Sstevel@tonic-gate 	new.it_value.tv_usec = usecs % USPS;
380Sstevel@tonic-gate 	new.it_value.tv_sec = usecs / USPS;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate 	if (setitimer(ITIMER_REAL, &new, &old) != 0)
410Sstevel@tonic-gate 		return (0);	/* no errors are defined */
420Sstevel@tonic-gate 	return (old.it_value.tv_sec * USPS + old.it_value.tv_usec);
430Sstevel@tonic-gate }
44