xref: /illumos-gate/usr/src/lib/libc/port/gen/ualarm.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
17c478bd9Sstevel@tonic-gate /*
2*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1985 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
15*7257d1b4Sraf #include "lint.h"
167c478bd9Sstevel@tonic-gate #include <sys/types.h>
177c478bd9Sstevel@tonic-gate #include <sys/time.h>
187c478bd9Sstevel@tonic-gate #include <unistd.h>
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #define	USPS	1000000		/* # of microseconds in a second */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Generate a SIGALRM signal in ``usecs'' microseconds.
247c478bd9Sstevel@tonic-gate  * If ``reload'' is non-zero, keep generating SIGALRM
257c478bd9Sstevel@tonic-gate  * every ``reload'' microseconds after the first signal.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate useconds_t
ualarm(useconds_t usecs,useconds_t reload)287c478bd9Sstevel@tonic-gate ualarm(useconds_t usecs, useconds_t reload)
297c478bd9Sstevel@tonic-gate {
307c478bd9Sstevel@tonic-gate 	struct itimerval new, old;
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 	new.it_interval.tv_usec = reload % USPS;
337c478bd9Sstevel@tonic-gate 	new.it_interval.tv_sec = reload / USPS;
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate 	new.it_value.tv_usec = usecs % USPS;
367c478bd9Sstevel@tonic-gate 	new.it_value.tv_sec = usecs / USPS;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 	if (setitimer(ITIMER_REAL, &new, &old) != 0)
397c478bd9Sstevel@tonic-gate 		return (0);	/* no errors are defined */
407c478bd9Sstevel@tonic-gate 	return (old.it_value.tv_sec * USPS + old.it_value.tv_usec);
417c478bd9Sstevel@tonic-gate }
42