1 /*
2 * Copyright (c) 2008-2009 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #ifndef __KERNEL_TIMER_H
24 #define __KERNEL_TIMER_H
25
26 #include <compiler.h>
27 #include <list.h>
28 #include <sys/types.h>
29
30 __BEGIN_CDECLS;
31
32 void timer_init(void);
33
34 struct timer;
35 typedef enum handler_return (*timer_callback)(struct timer *, lk_time_ns_t now,
36 void *arg);
37
38 #define TIMER_MAGIC (0x74696D72) //'timr'
39
40 typedef struct timer {
41 int magic;
42 uint cpu;
43 bool running;
44 struct list_node node;
45
46 lk_time_ns_t scheduled_time;
47 lk_time_ns_t periodic_time;
48
49 timer_callback callback;
50 void *arg;
51 } timer_t;
52
53 #define TIMER_INITIAL_VALUE(t) \
54 { \
55 .magic = TIMER_MAGIC, \
56 .cpu = ~0U, \
57 .running = false, \
58 .node = LIST_INITIAL_CLEARED_VALUE, \
59 .scheduled_time = 0, \
60 .periodic_time = 0, \
61 .callback = NULL, \
62 .arg = NULL, \
63 }
64
65 /* Rules for Timers:
66 * - Timer callbacks occur from interrupt context
67 * - Timers may be programmed or canceled from interrupt or thread context.
68 * Timers canceled from interrupt context must be canceled from the same CPU
69 * that the callback runs on. Timers canceled from thread context should use
70 * timer_cancel_sync instead of timer_cancel to make sure the timer callback
71 * is not still running when the call returns.
72 * - Timers may be canceled or reprogrammed from within their callback
73 * - Timers currently are dispatched from a 10ms periodic tick
74 */
75 void timer_initialize(timer_t *);
76 void timer_set_oneshot_ns(timer_t *, lk_time_ns_t delay, timer_callback,
77 void *arg);
78 void timer_set_periodic_ns(timer_t *, lk_time_ns_t period, timer_callback,
79 void *arg);
80
81 /**
82 * timer_cancel_etc - Cancel timer and optionally wait for the callback
83 * @timer: Timer to cancel.
84 * @wait: If %true, wait for callback.
85 */
86 void timer_cancel_etc(timer_t *timer, bool wait);
87
88 /**
89 * timer_cancel - Cancel timer without waiting for callback to finish
90 * @timer: Timer to cancel.
91 *
92 * Can be called from interrupt context, including the timer callback itself.
93 * It is not safe to free the timer immediately this call returns as the timer
94 * could still be running.
95 */
timer_cancel(timer_t * timer)96 static void timer_cancel(timer_t *timer) {
97 timer_cancel_etc(timer, false);
98 }
99
100 /**
101 * timer_cancel_sync - Cancel timer and wait for callback to finish
102 * @timer: Timer to cancel.
103 *
104 * Can not be called from interrupt context.
105 */
timer_cancel_sync(timer_t * timer)106 static void timer_cancel_sync(timer_t *timer) {
107 timer_cancel_etc(timer, true);
108 }
109
110 __END_CDECLS;
111
112 #endif
113
114