Replace previous change by different test
[minix.git] / include / timers.h
blobdcb5ba4907356dcdde255c96bd0994b5e99e11eb
1 /* This library provides generic watchdog timer management functionality.
2 * The functions operate on a timer queue provided by the caller. Note that
3 * the timers must use absolute time to allow sorting. The library provides:
5 * tmrs_settimer: (re)set a new watchdog timer in the timers queue
6 * tmrs_clrtimer: remove a timer from both the timers queue
7 * tmrs_exptimers: check for expired timers and run watchdog functions
9 * Author:
10 * Jorrit N. Herder <jnherder@cs.vu.nl>
11 * Adapted from tmr_settimer and tmr_clrtimer in src/kernel/clock.c.
12 * Last modified: September 30, 2004.
15 #ifndef _TIMERS_H
16 #define _TIMERS_H
18 #include <limits.h>
20 #include <sys/types.h>
21 #include <minix/u64.h>
22 #include <minix/minlib.h>
23 #include <minix/endpoint.h>
25 struct timer;
26 typedef void (*tmr_func_t)(struct timer *tp);
27 typedef union { int ta_int; long ta_long; void *ta_ptr; } tmr_arg_t;
29 /* A timer_t variable must be declare for each distinct timer to be used.
30 * The timers watchdog function and expiration time are automatically set
31 * by the library function tmrs_settimer, but its argument is not.
33 typedef struct timer
35 struct timer *tmr_next; /* next in a timer chain */
36 clock_t tmr_exp_time; /* expiration time */
37 tmr_func_t tmr_func; /* function to call when expired */
38 tmr_arg_t tmr_arg; /* random argument */
39 } timer_t;
41 /* Used when the timer is not active. */
42 #define TMR_NEVER ((clock_t) -1 < 0) ? ((clock_t) LONG_MAX) : ((clock_t) -1)
43 #undef TMR_NEVER
44 #define TMR_NEVER ((clock_t) LONG_MAX)
46 /* These definitions can be used to set or get data from a timer variable. */
47 #define tmr_arg(tp) (&(tp)->tmr_arg)
48 #define tmr_exp_time(tp) (&(tp)->tmr_exp_time)
50 /* Timers should be initialized once before they are being used. Be careful
51 * not to reinitialize a timer that is in a list of timers, or the chain
52 * will be broken.
54 #define tmr_inittimer(tp) (void)((tp)->tmr_exp_time = TMR_NEVER, \
55 (tp)->tmr_next = NULL)
57 /* The following generic timer management functions are available. They
58 * can be used to operate on the lists of timers. Adding a timer to a list
59 * automatically takes care of removing it.
61 clock_t tmrs_clrtimer(timer_t **tmrs, timer_t *tp, clock_t *new_head);
62 void tmrs_exptimers(timer_t **tmrs, clock_t now, clock_t *new_head);
63 clock_t tmrs_settimer(timer_t **tmrs, timer_t *tp, clock_t exp_time,
64 tmr_func_t watchdog, clock_t *new_head);
66 #define PRINT_STATS(cum_spenttime, cum_instances) { \
67 if(ex64hi(cum_spenttime)) { util_stacktrace(); printf(" ( ??? %lu %lu)\n", \
68 ex64hi(cum_spenttime), ex64lo(cum_spenttime)); } \
69 printf("%s:%d,%lu,%lu\n", \
70 __FILE__, __LINE__, cum_instances, \
71 ex64lo(cum_spenttime)); \
74 #define RESET_STATS(starttime, cum_instances, cum_spenttime, cum_starttime) { \
75 cum_instances = 0; \
76 cum_starttime = starttime; \
77 cum_spenttime = make64(0,0); \
80 #define TIME_BLOCK_VAR(timed_code_block, time_interval) do { \
81 static u64_t _cum_spenttime, _cum_starttime; \
82 static int _cum_instances; \
83 u64_t _next_cum_spent, _starttime, _endtime, _dt, _cum_dt; \
84 u32_t _dt_micros; \
85 read_tsc_64(&_starttime); \
86 do { timed_code_block } while(0); \
87 read_tsc_64(&_endtime); \
88 _dt = sub64(_endtime, _starttime); \
89 if(_cum_instances == 0) { \
90 RESET_STATS(_starttime, _cum_instances, _cum_spenttime, _cum_starttime); \
91 } \
92 _next_cum_spent = add64(_cum_spenttime, _dt); \
93 if(ex64hi(_next_cum_spent)) { \
94 PRINT_STATS(_cum_spenttime, _cum_instances); \
95 RESET_STATS(_starttime, _cum_instances, _cum_spenttime, _cum_starttime); \
96 } \
97 _cum_spenttime = add64(_cum_spenttime, _dt); \
98 _cum_instances++; \
99 _cum_dt = sub64(_endtime, _cum_starttime); \
100 if(cmp64(_cum_dt, make64(0, 120)) > 0) { \
101 PRINT_STATS(_cum_spenttime, _cum_instances); \
102 RESET_STATS(_starttime, _cum_instances, _cum_spenttime, _cum_starttime); \
104 } while(0)
106 #define TIME_BLOCK(timed_code_block) TIME_BLOCK_VAR(timed_code_block, 100)
107 #define TIME_BLOCK_T(timed_code_block, t) TIME_BLOCK_VAR(timed_code_block, t)
109 #endif /* _TIMERS_H */