xref: /illumos-gate/usr/src/lib/libc/port/gen/ualarm.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1985 Regents of the University of California.
11  * All rights reserved.  The Berkeley software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #include "lint.h"
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <unistd.h>
19 
20 #define	USPS	1000000		/* # of microseconds in a second */
21 
22 /*
23  * Generate a SIGALRM signal in ``usecs'' microseconds.
24  * If ``reload'' is non-zero, keep generating SIGALRM
25  * every ``reload'' microseconds after the first signal.
26  */
27 useconds_t
28 ualarm(useconds_t usecs, useconds_t reload)
29 {
30 	struct itimerval new, old;
31 
32 	new.it_interval.tv_usec = reload % USPS;
33 	new.it_interval.tv_sec = reload / USPS;
34 
35 	new.it_value.tv_usec = usecs % USPS;
36 	new.it_value.tv_sec = usecs / USPS;
37 
38 	if (setitimer(ITIMER_REAL, &new, &old) != 0)
39 		return (0);	/* no errors are defined */
40 	return (old.it_value.tv_sec * USPS + old.it_value.tv_usec);
41 }
42