The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * kernel timing: the tick count and wait queues with a deadline.
5 *
6 * the PIT interrupts TIMER_HZ times a second; each interrupt is one tick.
7 * a timed queue is a wait queue that a waiter can also leave when a given
8 * tick arrives. the timer interrupt wakes every waiter of a queue when the
9 * earliest deadline on it has passed; the waiters then check their own
10 * deadline and condition, and sleep again if they still have to wait.
11 */
12
13#include "common/stdint.h"
14#include "drivers/pit/pit.h"
16
17#define TIMER_HZ PIT_FREQUENCY_HZ
18
19// a deadline that never comes
20#define TIMER_NEVER UINT64_MAX
21
22/**
23 * a wait queue with a deadline.
24 */
25typedef struct timed_queue
26{
28 volatile uint64_t deadline; // earliest deadline of a waiter (TIMER_NEVER: none)
29 struct timed_queue *next; // the list timer_tick walks
31
32/**
33 * set up a queue and add it to the ones timer_tick checks. call once per
34 * queue (queues live as long as the kernel).
35 * @param queue the queue.
36 */
38
39/**
40 * sleep on a queue until it is woken or the deadline tick has come. for a
41 * wait on time only: a wake between the caller's check and the sleep is
42 * lost. the caller re-checks the deadline after waking.
43 * @param queue the queue.
44 * @param deadline the tick to wake at, or TIMER_NEVER.
45 */
47
48/**
49 * like timed_queue_block, but a signal for the process also wakes it.
50 */
52
53/**
54 * sleep on a queue until it is woken or the deadline tick has come, with
55 * a spinlock released while asleep and held again on return. the caller
56 * tests its condition with the lock held, and the waker takes the lock to
57 * change it, so a wake between the test and the sleep is not lost.
58 * @param queue the queue.
59 * @param deadline the tick to wake at, or TIMER_NEVER.
60 * @param lock a lock taken with spin_lock_irqsave() by the caller.
61 */
63
64/**
65 * like timed_queue_block_locked, but a signal for the process also wakes
66 * it.
67 */
69 spinlock_t *lock);
70
71/**
72 * sleep on a queue while a counter has the value the caller saw, until it
73 * is woken, the deadline tick comes or a signal arrives. a waker changes
74 * the counter, then wakes the queue: a change before the sleep is seen, a
75 * later one finds the process asleep. for waits on many things at once
76 * (poll), where no one lock guards the condition.
77 * @param queue the queue.
78 * @param deadline the tick to wake at, or TIMER_NEVER.
79 * @param word the counter.
80 * @param seen the value the caller read before it tested its condition.
81 */
82void timed_queue_wait_word(timed_queue_t *queue, uint64_t deadline, const volatile uint32_t *word,
83 uint32_t seen);
84
85/**
86 * wake every waiter of a queue (they re-check their condition).
87 * @param queue the queue.
88 */
90
91/**
92 * called from the timer interrupt on every tick: wake the queues whose
93 * earliest deadline has passed.
94 * @param now the current tick.
95 */
96void timer_tick(uint64_t now);
97
98#define TIMER_POLL_HOOKS 6
99
100/** a function the timer interrupt calls on every tick (now: the tick count). */
101typedef void (*timer_poll_fn)(uint64_t now);
102
103/**
104 * call fn on every tick, in the timer interrupt (interrupts off). for
105 * drivers that poll their device, such as the USB keyboard. there is room
106 * for TIMER_POLL_HOOKS functions; more are ignored.
107 */
109
110/**
111 * the current tick.
112 */
113uint64_t timer_now(void);
114
115/**
116 * the number of ticks that covers at least a time span (rounded up).
117 * @param ms milliseconds.
118 * @return ticks.
119 */
unsigned int uint32_t
Definition fb.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
kernel locking rules:
Definition spinlock.h:67
a wait queue with a deadline.
Definition timer.h:26
struct timed_queue * next
Definition timer.h:29
volatile uint64_t deadline
Definition timer.h:28
wait_queue_t waiters
Definition timer.h:27
a queue of blocked processes, linked through process_t::wait_next.
Definition wait_queue.h:35
uint64_t timer_ms_to_ticks(uint32_t ms)
the number of ticks that covers at least a time span (rounded up).
Definition timer.c:147
void timed_queue_block_interruptible(timed_queue_t *queue, uint64_t deadline)
like timed_queue_block, but a signal for the process also wakes it.
Definition timer.c:71
uint64_t timer_now(void)
the current tick.
Definition timer.c:137
void timed_queue_init(timed_queue_t *queue)
set up a queue and add it to the ones timer_tick checks.
Definition timer.c:14
void timer_add_poll_hook(timer_poll_fn fn)
call fn on every tick, in the timer interrupt (interrupts off).
Definition timer.c:102
void timed_queue_wake_all(timed_queue_t *queue)
wake every waiter of a queue (they re-check their condition).
Definition timer.c:95
void timed_queue_wait_word(timed_queue_t *queue, uint64_t deadline, const volatile uint32_t *word, uint32_t seen)
sleep on a queue while a counter has the value the caller saw, until it is woken, the deadline tick c...
Definition timer.c:89
void timed_queue_block_locked_interruptible(timed_queue_t *queue, uint64_t deadline, spinlock_t *lock)
like timed_queue_block_locked, but a signal for the process also wakes it.
Definition timer.c:82
void timed_queue_block_locked(timed_queue_t *queue, uint64_t deadline, spinlock_t *lock)
sleep on a queue until it is woken or the deadline tick has come, with a spinlock released while asle...
Definition timer.c:76
void timed_queue_block(timed_queue_t *queue, uint64_t deadline)
sleep on a queue until it is woken or the deadline tick has come.
Definition timer.c:66
void timer_tick(uint64_t now)
called from the timer interrupt on every tick: wake the queues whose earliest deadline has passed.
Definition timer.c:114
struct timed_queue timed_queue_t
a wait queue with a deadline.
void(*) timer_poll_fn(uint64_t now)
a function the timer interrupt calls on every tick (now: the tick count).
Definition timer.h:101