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