PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches
Timer subsystem

Per-CPU timers. More...

Collaboration diagram for Timer subsystem:

Detailed Description

Per-CPU timers.

The timer subsystem is responsible for managing per-CPU timers which are responsible for generating timer interrupts. These interrupts are whats called "one-shot" interrupts, meaning that the interrupt will only occur once and then a new interrupt must be programmed.

Timer Interrupts

The way we handle timer interrupts is that each subsystem that relies on the timer calls the timer_set() function with their desired deadline and then, when the timer interrupt occurs, the timer interrupt is acknowledged and the usual interrupt handling process continues. For example, the scheduler and wait system will check if they need to do anything.

Both the scheduler and the wait system can now call timer_set() again if they need to schedule another timer interrupt or if the time they requested has not yet occurred.

This does technically result in some uneeded checks but its a very simply way of effectively eliminating timer related race conditions.

Timer Sources

The actual timer interrupts are provided by "timer sources" (timer_source_t), which are registered by modules. Each source registers itself with a estimate of its precision, the timer subsystem then chooses the source with the highest precision as the active timer source.

Data Structures

struct  timer_cpu_ctx_t
 Per-CPU system time context. More...
 
struct  timer_source_t
 Timer source structure. More...
 

Macros

#define TIMER_MAX_SOURCES   4
 Maximum amount of timer sources.
 

Functions

void timer_cpu_ctx_init (timer_cpu_ctx_t *ctx)
 Initialize per-CPU timer context.
 
void timer_ack_eoi (interrupt_frame_t *frame, cpu_t *self)
 Acknowledge a timer interrupt and send EOI.
 
uint64_t timer_source_register (const timer_source_t *source)
 Register a timer source.
 
void timer_source_unregister (const timer_source_t *source)
 Unregister a timer source.
 
uint64_t timer_source_amount (void)
 Get the amount of registered timer sources.
 
void timer_set (clock_t uptime, clock_t deadline)
 Schedule a one-shot timer interrupt on the current CPU.
 

Macro Definition Documentation

◆ TIMER_MAX_SOURCES

#define TIMER_MAX_SOURCES   4

Maximum amount of timer sources.

Definition at line 60 of file timer.h.

Function Documentation

◆ timer_cpu_ctx_init()

void timer_cpu_ctx_init ( timer_cpu_ctx_t ctx)

Initialize per-CPU timer context.

Must be called on the CPU who owns the context.

Parameters
ctxThe timer context to initialize.

Definition at line 23 of file timer.c.

Here is the caller graph for this function:

◆ timer_ack_eoi()

void timer_ack_eoi ( interrupt_frame_t frame,
cpu_t self 
)

Acknowledge a timer interrupt and send EOI.

Parameters
frameThe interrupt frame of the timer interrupt.
selfThe CPU on which the timer interrupt was received.

Definition at line 28 of file timer.c.

Here is the caller graph for this function:

◆ timer_source_register()

uint64_t timer_source_register ( const timer_source_t source)

Register a timer source.

Parameters
sourceThe timer source to register.
Returns
On success, 0. On failure, ERR and errno is set to:
  • EINVAL: Invalid parameters.
  • ENOSPC: No more timer sources can be registered.
  • Other errors as returned by irq_handler_register().

Definition at line 49 of file timer.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_source_unregister()

void timer_source_unregister ( const timer_source_t source)

Unregister a timer source.

Parameters
sourceThe timer source to unregister, or NULL for no-op.

Definition at line 88 of file timer.c.

Here is the call graph for this function:

◆ timer_source_amount()

uint64_t timer_source_amount ( void  )

Get the amount of registered timer sources.

Returns
The amount of registered timer sources.

Definition at line 126 of file timer.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_set()

void timer_set ( clock_t  uptime,
clock_t  deadline 
)

Schedule a one-shot timer interrupt on the current CPU.

Sets the per-cpu timer to generate a interrupt after the specified timeout.

Multiple calls with different timeouts will result in the timer being set for the shortest requested timeout, this will be reset after a timer interrupt.

The reason we need to specify the current uptime, is not just as a slight optimization, but also to ensure the caller knows exactly what time they are scheduling the timer for, as the uptime could change between the caller reading the time and this function setting the timer, resulting in very subtle bugs or race conditions.

Note
Will never set the timeout to be less than CONFIG_MIN_TIMER_TIMEOUT to avoid spamming the CPU with timer interrupts.
Parameters
uptimeThe time since boot, we need to specify this as an argument to avoid inconsistency in the timeout/deadline calculations.
deadlineThe desired deadline.

Definition at line 134 of file timer.c.

Here is the call graph for this function:
Here is the caller graph for this function: