rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 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_t now, void *arg); |
| 36 | |
| 37 | #define TIMER_MAGIC 'timr' |
| 38 | |
| 39 | typedef struct timer { |
| 40 | int magic; |
| 41 | struct list_node node; |
| 42 | |
| 43 | lk_time_t scheduled_time; |
| 44 | lk_time_t periodic_time; |
| 45 | |
| 46 | timer_callback callback; |
| 47 | void *arg; |
| 48 | } timer_t; |
| 49 | |
| 50 | #define TIMER_INITIAL_VALUE(t) \ |
| 51 | { \ |
| 52 | .magic = TIMER_MAGIC, \ |
| 53 | .node = LIST_INITIAL_CLEARED_VALUE, \ |
| 54 | .scheduled_time = 0, \ |
| 55 | .periodic_time = 0, \ |
| 56 | .callback = NULL, \ |
| 57 | .arg = NULL, \ |
| 58 | } |
| 59 | |
| 60 | /* Rules for Timers: |
| 61 | * - Timer callbacks occur from interrupt context |
| 62 | * - Timers may be programmed or canceled from interrupt or thread context |
| 63 | * - Timers may be canceled or reprogrammed from within their callback |
| 64 | * - Timers currently are dispatched from a 10ms periodic tick |
| 65 | */ |
| 66 | void timer_initialize(timer_t *); |
| 67 | void timer_set_oneshot(timer_t *, lk_time_t delay, timer_callback, void *arg); |
| 68 | void timer_set_periodic(timer_t *, lk_time_t period, timer_callback, void *arg); |
| 69 | void timer_cancel(timer_t *); |
| 70 | |
| 71 | __END_CDECLS; |
| 72 | |
| 73 | #endif |
| 74 | |