PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
clock.h
Go to the documentation of this file.
1#pragma once
2
4#include <kernel/sync/lock.h>
5
6#include <sys/proc.h>
7#include <time.h>
8
9typedef struct cpu cpu_t;
10
11/**
12 * @brief Clock subsystem.
13 * @defgroup kernel_sched_clock Clock subsystem.
14 * @ingroup kernel_sched
15 *
16 * The clock subsystem is responsible for providing a consistent system wide time keeping.
17 *
18 * System wide time is provided via "clock sources", which are provided in modules. Each source registers itself with a estimate of its precision, the clock subsystem then chooses the two sources, one for uptime and one for unix epoch, with the best precision.
19 *
20 * @{
21 */
22
23/**
24 * @brief Maximum amount of system timer sources.
25 */
26#define CLOCK_MAX_SOURCES 8
27
28/**
29 * @brief Clock source structure.
30 * @struct clock_source_t
31 */
32typedef struct
33{
34 const char* name;
36 clock_t (*read_ns)(void);
37 time_t (*read_epoch)(void);
39
40/**
41 * @brief Register a system timer source.
42 *
43 * @param source The timer source to register.
44 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
45 * - `EINVAL`: Invalid parameters.
46 * - `ENOSPC`: No more timer sources can be registered.
47 */
49
50/**
51 * @brief Unregister a system timer source.
52 *
53 * @param source The timer source to unregister, or `NULL` for no-op.
54 */
56
57/**
58 * @brief Retrieve the time in nanoseconds since boot.
59 *
60 * @return The time in nanoseconds since boot.
61 */
63
64/**
65 * @brief Retrieve the seconds since the unix epoch.
66 *
67 * @return The amount of seconds since the unix epoch.
68 */
70
71/**
72 * @brief Wait for a specified number of nanoseconds.
73 *
74 * This function uses a busy-wait loop, making it highly CPU inefficient, but its useful during early
75 * initialization or when you are unable to block the current thread.
76 *
77 * @param nanoseconds The number of nanoseconds to wait.
78 */
79void clock_wait(clock_t nanoseconds);
80
81/** @} */
time_t clock_epoch(void)
Retrieve the seconds since the unix epoch.
Definition clock.c:119
void clock_wait(clock_t nanoseconds)
Wait for a specified number of nanoseconds.
Definition clock.c:130
clock_t clock_uptime(void)
Retrieve the time in nanoseconds since boot.
Definition clock.c:99
uint64_t clock_source_register(const clock_source_t *source)
Register a system timer source.
Definition clock.c:47
void clock_source_unregister(const clock_source_t *source)
Unregister a system timer source.
Definition clock.c:72
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static clock_source_t source
Structure to describe the HPET to the sys time subsystem.
Definition hpet.c:192
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Clock source structure.
Definition clock.h:33
const char * name
Definition clock.h:34
clock_t precision
Definition clock.h:35
CPU structure.
Definition cpu.h:122
long long unsigned time_t
Definition time_t.h:4