1 /*
2  * Copyright (c) 2008-2009 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #pragma once
9 
10 #include <lk/compiler.h>
11 #include <lk/list.h>
12 #include <sys/types.h>
13 
14 __BEGIN_CDECLS
15 
16 void timer_init(void);
17 
18 struct timer;
19 typedef enum handler_return (*timer_callback)(struct timer *, lk_time_t now, void *arg);
20 
21 #define TIMER_MAGIC (0x74696D72)  //'timr'
22 
23 typedef struct timer {
24     int magic;
25     struct list_node node;
26 
27     lk_time_t scheduled_time;
28     lk_time_t periodic_time;
29 
30     timer_callback callback;
31     void *arg;
32 } timer_t;
33 
34 #define TIMER_INITIAL_VALUE(t) \
35 { \
36     .magic = TIMER_MAGIC, \
37     .node = LIST_INITIAL_CLEARED_VALUE, \
38     .scheduled_time = 0, \
39     .periodic_time = 0, \
40     .callback = NULL, \
41     .arg = NULL, \
42 }
43 
44 /* Rules for Timers:
45  * - Timer callbacks occur from interrupt context
46  * - Timers may be programmed or canceled from interrupt or thread context
47  * - Timers may be canceled or reprogrammed from within their callback
48  * - Timers currently are dispatched from a 10ms periodic tick
49 */
50 void timer_initialize(timer_t *);
51 void timer_set_oneshot(timer_t *, lk_time_t delay, timer_callback, void *arg);
52 void timer_set_periodic(timer_t *, lk_time_t period, timer_callback, void *arg);
53 void timer_cancel(timer_t *);
54 
55 __END_CDECLS
56