summaryrefslogtreecommitdiff
path: root/kern/timer.h
blob: 02b5bef70b6d67e43672bff7b1b0105184b0384c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (c) 2017 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Low resolution timer system.
 */

#ifndef KERN_TIMER_H
#define KERN_TIMER_H

#include <stdint.h>

#include <kern/init.h>

/*
 * Scheduling flags.
 */
#define TIMER_DETACHED      0x1     /* Timer completion isn't synchronized */
#define TIMER_INTR          0x2     /* Handler is run from interrupt context */
#define TIMER_HIGH_PRIO     0x4     /* Handler is run in high priority thread */

struct timer;

/*
 * Type for timer functions.
 */
typedef void (*timer_fn_t)(struct timer *);

#include <kern/timer_i.h>

/*
 * Return the absolute expiration time of the timer, in ticks.
 *
 * This function may not be called while another thread is scheduling the
 * timer.
 */
static inline uint64_t
timer_get_time(const struct timer *timer)
{
    return timer->ticks;
}

/*
 * Initialize a timer.
 *
 * Timers that are reponsible for releasing their own resources must
 * be detached.
 */
void timer_init(struct timer *timer, timer_fn_t fn, int flags);

/*
 * Schedule a timer.
 *
 * The time of expiration is an absolute time in ticks.
 *
 * Timers may safely be rescheduled after completion. Periodic timers are
 * implemented by rescheduling from the handler.
 *
 * If the timer has been canceled, this function does nothing. A
 * canceled timer must be reinitialized before being scheduled again.
 *
 * This function may safely be called in interrupt context.
 */
void timer_schedule(struct timer *timer, uint64_t ticks);

/*
 * Cancel a timer.
 *
 * The given timer must not be detached.
 *
 * If the timer has already expired, this function waits until the timer
 * function completes, or returns immediately if the function has already
 * completed.
 *
 * This function may safely be called from the timer handler, but not on
 * the current timer. Canceling a timer from the handler is achieved by
 * simply not rescheduling it.
 */
void timer_cancel(struct timer *timer);

/*
 * Report a periodic event on the current processor.
 *
 * Interrupts and preemption must be disabled when calling this function.
 */
void timer_report_periodic_event(void);

/*
 * This init operation provides :
 *  - timer initialization and scheduling
 */
INIT_OP_DECLARE(timer_bootstrap);

#endif /* KERN_TIMER_H */