PatchworkOS
Loading...
Searching...
No Matches
Time subsystem

System time and timers. More...

Data Structures

struct  timer_ctx_t
 Per-CPU system time context. More...
 

Macros

#define TIMER_MAX_CALLBACK   16
 Maximum amount of timer callbacks.
 

Typedefs

typedef void(* timer_callback_t) (interrupt_frame_t *frame, cpu_t *self)
 Timer callback function type.
 

Functions

void timer_ctx_init (timer_ctx_t *ctx)
 Initialize per-CPU timer context.
 
clock_t timer_uptime (void)
 Time since boot.
 
time_t timer_unix_epoch (void)
 The unix epoch.
 
void timer_interrupt_handler (interrupt_frame_t *frame, cpu_t *self)
 Handle timer interrupt.
 
void timer_subscribe (timer_ctx_t *ctx, timer_callback_t callback)
 Subscribe to timer interrupts.
 
void timer_unsubscribe (timer_ctx_t *ctx, timer_callback_t callback)
 Unsubscribe from timer interrupts.
 
void timer_one_shot (cpu_t *self, clock_t uptime, clock_t timeout)
 Schedule a one-shot timer interrupt.
 
void timer_notify (cpu_t *cpu)
 Trigger timer interrupt on cpu.
 
void timer_notify_self (void)
 Trigger timer interrupt on self.
 

Detailed Description

System time and timers.

The timer subsystem provides kernel time management.

Macro Definition Documentation

◆ TIMER_MAX_CALLBACK

#define TIMER_MAX_CALLBACK   16

Maximum amount of timer callbacks.

Definition at line 24 of file timer.h.

Typedef Documentation

◆ timer_callback_t

typedef void(* timer_callback_t) (interrupt_frame_t *frame, cpu_t *self)

Timer callback function type.

Definition at line 29 of file timer.h.

Function Documentation

◆ timer_ctx_init()

void timer_ctx_init ( timer_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 51 of file timer.c.

References apic_timer_ticks_per_ns(), timer_ctx_t::apicTicksPerNs, timer_ctx_t::callbacks, CLOCKS_NEVER, cpu_t::id, timer_ctx_t::lock, lock_init(), LOG_INFO, timer_ctx_t::nextDeadline, NULL, smp_self_unsafe(), cpu_t::timer, and TIMER_MAX_CALLBACK.

Referenced by cpu_init().

◆ timer_interrupt_handler()

void timer_interrupt_handler ( interrupt_frame_t frame,
cpu_t self 
)

Handle timer interrupt.

Parameters
frameThe current interrupt frame.
selfThe current cpu.

Definition at line 96 of file timer.c.

References timer_ctx_t::callbacks, CLOCKS_NEVER, timer_ctx_t::lock, LOCK_SCOPE, timer_ctx_t::nextDeadline, NULL, cpu_t::timer, timer_accumulate(), and TIMER_MAX_CALLBACK.

Referenced by interrupt_handler().

◆ timer_notify()

void timer_notify ( cpu_t cpu)

Trigger timer interrupt on cpu.

Triggers the timer interrupt on the specified cpu.

Parameters
cpuThe destination cpu.

Definition at line 197 of file timer.c.

References INTERRUPT_TIMER, lapic_send_ipi(), and cpu_t::lapicId.

Referenced by sched_load_balance(), sched_push(), and sched_push_new_thread().

◆ timer_notify_self()

void timer_notify_self ( void  )

Trigger timer interrupt on self.

Triggers the timer interrupt on the current cpu.

Definition at line 202 of file timer.c.

References INTERRUPT_TIMER.

Referenced by sched_done_with_boot_thread(), SYSCALL_DEFINE(), and wait_block_commit().

◆ timer_one_shot()

void timer_one_shot ( cpu_t self,
clock_t  uptime,
clock_t  timeout 
)

Schedule a one-shot timer interrupt.

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 idea is that every system that wants timer interrupts calls the timer_one_shot() function with their desired timeout and then when the timer interrupt occurs they check if their desired time has been reached, if it has they do what they need to do, else they call the function once more respecifying their desired timeout, and we repeat the process. This does technically result in some uneeded checks but its a very simply way of effectively eliminating the need to care about timer related race conditions.

Parameters
selfThe currently running cpu.
uptimeThe time since boot, we need to specify this as an argument to avoid inconsistency in the timeout/deadline calculations.
timeoutThe desired timeout.

Definition at line 153 of file timer.c.

References apic_timer_one_shot(), APIC_TIMER_TICKS_FIXED_POINT_OFFSET, timer_ctx_t::apicTicksPerNs, CLOCKS_NEVER, INTERRUPT_TIMER, timer_ctx_t::nextDeadline, NULL, cpu_t::timer, UINT32_MAX, and uptime().

Referenced by sched_invoke(), wait_block_finalize(), and wait_timer_handler().

◆ timer_subscribe()

void timer_subscribe ( timer_ctx_t ctx,
timer_callback_t  callback 
)

Subscribe to timer interrupts.

Note that subscribing to a callback only applies to the cpu that the timer context belongs to.

Parameters
ctxThe timer context that the subscription is for.
callbackThe callback function to be called on timer interrupts.

Definition at line 111 of file timer.c.

References timer_ctx_t::callbacks, timer_ctx_t::lock, LOCK_SCOPE, LOG_DEBUG, NULL, panic(), and TIMER_MAX_CALLBACK.

Referenced by process_procfs_init(), sched_cpu_ctx_init(), and wait_cpu_ctx_init().

◆ timer_unix_epoch()

time_t timer_unix_epoch ( void  )

The unix epoch.

Returns
time_t The amount of seconds since the unix epoch.

Definition at line 86 of file timer.c.

References bootEpoch, CLOCKS_PER_SEC, initialized, timer_init(), and timer_uptime().

Referenced by inode_new(), inode_notify_access(), inode_notify_change(), inode_notify_modify(), and SYSCALL_DEFINE().

◆ timer_unsubscribe()

void timer_unsubscribe ( timer_ctx_t ctx,
timer_callback_t  callback 
)

Unsubscribe from timer interrupts.

Note that unsubscribing from a callback only applies to the cpu that the timer context belongs to.

Parameters
ctxThe timer context that the unsubscription is for.
callbackThe callback function to be removed from timer interrupts.

Definition at line 132 of file timer.c.

References timer_ctx_t::callbacks, timer_ctx_t::lock, LOCK_SCOPE, LOG_DEBUG, NULL, panic(), and TIMER_MAX_CALLBACK.

◆ timer_uptime()