xref: /titanic_51/usr/src/lib/libc/port/gen/ualarm.c (revision c10c16dec587a0662068f6e2991c29ed3a9db943)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include "lint.h"
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #include <unistd.h>
21 
22 #define	USPS	1000000		/* # of microseconds in a second */
23 
24 /*
25  * Generate a SIGALRM signal in ``usecs'' microseconds.
26  * If ``reload'' is non-zero, keep generating SIGALRM
27  * every ``reload'' microseconds after the first signal.
28  */
29 useconds_t
30 ualarm(useconds_t usecs, useconds_t reload)
31 {
32 	struct itimerval new, old;
33 
34 	new.it_interval.tv_usec = reload % USPS;
35 	new.it_interval.tv_sec = reload / USPS;
36 
37 	new.it_value.tv_usec = usecs % USPS;
38 	new.it_value.tv_sec = usecs / USPS;
39 
40 	if (setitimer(ITIMER_REAL, &new, &old) != 0)
41 		return (0);	/* no errors are defined */
42 	return (old.it_value.tv_sec * USPS + old.it_value.tv_usec);
43 }
44