PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches
timer.c
Go to the documentation of this file.
1#include <kernel/cpu/cpu.h>
3#include <kernel/cpu/irq.h>
5#include <kernel/log/log.h>
6#include <kernel/log/panic.h>
10#include <kernel/sched/timer.h>
11
12#include <kernel/sync/rwlock.h>
13#include <stdbool.h>
14#include <stdint.h>
15#include <sys/math.h>
16#include <time.h>
17
22
27
29{
30 (void)frame; // Unused
31
33
35
36 if (bestSource != NULL)
37 {
38 if (bestSource->ack != NULL)
39 {
40 bestSource->ack(self);
41 }
42 if (bestSource->eoi != NULL)
43 {
44 bestSource->eoi(self);
45 }
46 }
47}
48
50{
51 if (source == NULL || source->set == NULL || source->precision == 0 || source->name == NULL)
52 {
53 errno = EINVAL;
54 return ERR;
55 }
56
59 {
61 errno = ENOSPC;
62 return ERR;
63 }
64
65 for (uint32_t i = 0; i < sourceCount; i++)
66 {
67 if (sources[i] == source)
68 {
70 return 0;
71 }
72 }
73
75
76 bool bestSourceUpdated = false;
78 {
80 bestSourceUpdated = true;
81 }
83
84 LOG_INFO("registered timer source '%s'%s\n", source->name, bestSourceUpdated ? " (best source)" : "");
85 return 0;
86}
87
89{
90 if (source == NULL)
91 {
92 return;
93 }
94
96 for (uint32_t i = 0; i < sourceCount; i++)
97 {
98 if (sources[i] != source)
99 {
100 continue;
101 }
102
103 memmove(&sources[i], &sources[i + 1], (sourceCount - i - 1) * sizeof(timer_source_t*));
104 sourceCount--;
105
106 if (bestSource == source)
107 {
109 for (uint32_t j = 0; j < sourceCount; j++)
110 {
111 if (bestSource == NULL || sources[j]->precision < bestSource->precision)
112 {
113 bestSource = sources[j];
114 }
115 }
116 }
117
119 LOG_INFO("unregistered timer source '%s'\n", source->name);
120 return;
121 }
122
124}
125
133
135{
136 if (deadline == CLOCKS_NEVER)
137 {
138 return;
139 }
140
142
143 deadline = MAX(deadline, uptime + CONFIG_MIN_TIMER_TIMEOUT);
144
145 cpu_t* cpu = cpu_get_unsafe();
146 if (cpu->timer.deadline <= deadline)
147 {
148 return;
149 }
150 cpu->timer.deadline = deadline;
151
152 if (bestSource != NULL)
153 {
154 bestSource->set(VECTOR_TIMER, uptime, deadline - uptime);
155 }
156}
#define CLOCKS_NEVER
Definition clock_t.h:16
@ VECTOR_TIMER
See Timer subsystem for more information.
Definition interrupt.h:128
static cpu_t * cpu_get_unsafe(void)
Gets the current CPU structure without disabling interrupts.
Definition cpu.h:289
#define LOG_INFO(format,...)
Definition log.h:106
void rwlock_write_acquire(rwlock_t *lock)
Acquires a rwlock for writing, blocking until it is available.
Definition rwlock.c:62
void rwlock_read_acquire(rwlock_t *lock)
Acquires a rwlock for reading, blocking until it is available.
Definition rwlock.c:18
#define RWLOCK_READ_SCOPE(lock)
Acquires a rwlock for reading for the reminder of the current scope.
Definition rwlock.h:29
void rwlock_read_release(rwlock_t *lock)
Releases a rwlock from reading.
Definition rwlock.c:54
void rwlock_write_release(rwlock_t *lock)
Releases a rwlock from writing.
Definition rwlock.c:109
#define RWLOCK_CREATE()
Create a rwlock initializer.
Definition rwlock.h:47
uint64_t timer_source_amount(void)
Get the amount of registered timer sources.
Definition timer.c:126
void timer_cpu_ctx_init(timer_cpu_ctx_t *ctx)
Initialize per-CPU timer context.
Definition timer.c:23
void timer_set(clock_t uptime, clock_t deadline)
Schedule a one-shot timer interrupt on the current CPU.
Definition timer.c:134
void timer_source_unregister(const timer_source_t *source)
Unregister a timer source.
Definition timer.c:88
void timer_ack_eoi(interrupt_frame_t *frame, cpu_t *self)
Acknowledge a timer interrupt and send EOI.
Definition timer.c:28
#define TIMER_MAX_SOURCES
Maximum amount of timer sources.
Definition timer.h:60
uint64_t timer_source_register(const timer_source_t *source)
Register a timer source.
Definition timer.c:49
#define CONFIG_MIN_TIMER_TIMEOUT
Minimum timer timeout configuration.
Definition config.h:89
#define ENOSPC
No space left on device.
Definition errno.h:172
#define EINVAL
Invalid argument.
Definition errno.h:142
#define errno
Error number variable.
Definition errno.h:27
#define MAX(x, y)
Definition math.h:15
clock_t uptime(void)
System call for retreving the time since boot.
Definition uptime.c:6
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static sys_time_source_t source
Structure to describe the HPET to the sys time subsystem.
Definition hpet.c:192
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC void * memmove(void *_RESTRICT s1, const void *_RESTRICT s2, size_t n)
CPU structure.
Definition cpu.h:122
timer_cpu_ctx_t timer
Definition cpu.h:132
Trap Frame Structure.
Definition interrupt.h:143
Read-Write Ticket Lock structure.
Definition rwlock.h:61
clock_t precision
Definition sys_time.h:37
const char * name
Definition sys_time.h:36
Per-CPU system time context.
Definition timer.h:46
clock_t volatile deadline
Definition timer.h:54
Timer source structure.
Definition timer.h:67
clock_t precision
Definition timer.h:69
void(* set)(irq_virt_t virt, clock_t uptime, clock_t timeout)
Should set the one-shot timer to fire after the specified timeout.
Definition timer.h:79
void(* eoi)(cpu_t *cpu)
Definition timer.h:81
void(* ack)(cpu_t *cpu)
Definition timer.h:80
static rwlock_t sourcesLock
Definition timer.c:21
static const timer_source_t * sources[TIMER_MAX_SOURCES]
Definition timer.c:18
static const timer_source_t * bestSource
Definition timer.c:20
static uint32_t sourceCount
Definition timer.c:19