xref: /minix3/minix/lib/libtimers/tmrs_set.c (revision cfd712b4245f67a5631cc14e950ce43b18455602)
1*cfd712b4SDavid van Moolenbroek #include <minix/timers.h>
2433d6423SLionel Sambuc 
3*cfd712b4SDavid van Moolenbroek /*
4*cfd712b4SDavid van Moolenbroek  * Activate a timer to run function 'watchdog' at absolute time 'exp_time', as
5*cfd712b4SDavid van Moolenbroek  * part of timers queue 'tmrs'. If the timer is already in use, it is first
6*cfd712b4SDavid van Moolenbroek  * removed from the timers queue.  Then, it is put in the list of active timers
7*cfd712b4SDavid van Moolenbroek  * with the first to expire in front.  The caller responsible for scheduling a
8*cfd712b4SDavid van Moolenbroek  * new alarm for the timer if needed.  To that end, the function returns three
9*cfd712b4SDavid van Moolenbroek  * values: its return value (TRUE or FALSE) indicates whether there was an old
10*cfd712b4SDavid van Moolenbroek  * head timer; if TRUE, 'old_head' (if non-NULL) is filled with the absolute
11*cfd712b4SDavid van Moolenbroek  * expiry time of the old head timer.  If 'new_head' is non-NULL, it is filled
12*cfd712b4SDavid van Moolenbroek  * with the absolute expiry time of the new head timer.
13433d6423SLionel Sambuc  */
14*cfd712b4SDavid van Moolenbroek int
tmrs_settimer(minix_timer_t ** tmrs,minix_timer_t * tp,clock_t exp_time,tmr_func_t watchdog,int arg,clock_t * old_head,clock_t * new_head)15*cfd712b4SDavid van Moolenbroek tmrs_settimer(minix_timer_t ** tmrs, minix_timer_t * tp, clock_t exp_time,
16*cfd712b4SDavid van Moolenbroek 	tmr_func_t watchdog, int arg, clock_t * old_head, clock_t * new_head)
17*cfd712b4SDavid van Moolenbroek {
18433d6423SLionel Sambuc 	minix_timer_t **atp;
19*cfd712b4SDavid van Moolenbroek 	int r;
20433d6423SLionel Sambuc 
21*cfd712b4SDavid van Moolenbroek 	if (*tmrs != NULL) {
22*cfd712b4SDavid van Moolenbroek 		if (old_head != NULL)
23*cfd712b4SDavid van Moolenbroek 			*old_head = (*tmrs)->tmr_exp_time;
24*cfd712b4SDavid van Moolenbroek 		r = TRUE;
25*cfd712b4SDavid van Moolenbroek 	} else
26*cfd712b4SDavid van Moolenbroek 		r = FALSE;
27433d6423SLionel Sambuc 
28433d6423SLionel Sambuc 	/* Set the timer's variables. */
29*cfd712b4SDavid van Moolenbroek 	if (tmr_is_set(tp))
30*cfd712b4SDavid van Moolenbroek 		(void)tmrs_clrtimer(tmrs, tp, NULL, NULL);
31433d6423SLionel Sambuc 	tp->tmr_exp_time = exp_time;
32*cfd712b4SDavid van Moolenbroek 	tp->tmr_func = watchdog;	/* set the timer object */
33*cfd712b4SDavid van Moolenbroek 	tp->tmr_arg = arg;
34433d6423SLionel Sambuc 
35*cfd712b4SDavid van Moolenbroek 	/*
36*cfd712b4SDavid van Moolenbroek 	 * Add the timer to the active timers. The next timer due is in front.
37*cfd712b4SDavid van Moolenbroek 	 */
38433d6423SLionel Sambuc 	for (atp = tmrs; *atp != NULL; atp = &(*atp)->tmr_next) {
39*cfd712b4SDavid van Moolenbroek 		if (tmr_is_first(exp_time, (*atp)->tmr_exp_time))
40*cfd712b4SDavid van Moolenbroek 			break;
41433d6423SLionel Sambuc 	}
42433d6423SLionel Sambuc 	tp->tmr_next = *atp;
43433d6423SLionel Sambuc 	*atp = tp;
44433d6423SLionel Sambuc 
45*cfd712b4SDavid van Moolenbroek 	if (new_head != NULL)
46*cfd712b4SDavid van Moolenbroek 		*new_head = (*tmrs)->tmr_exp_time;
47*cfd712b4SDavid van Moolenbroek 	return r;
48*cfd712b4SDavid van Moolenbroek }
49