|
| 1 | +#ifndef STDBIGOS_CLOCK |
| 2 | +#define STDBIGOS_CLOCK |
| 3 | + |
| 4 | +#include "error.h" |
| 5 | +#include "types.h" |
| 6 | + |
| 7 | +/** |
| 8 | + * @file include/stdbigos/clock.h |
| 9 | + * @brief Public periodic clock API used by the scheduler and examples. |
| 10 | + * |
| 11 | + * The periodic clock provides a monotonic time source measured in |
| 12 | + * architecture-specific ticks and supports scheduling a "next switch" |
| 13 | + * tick for the scheduler to observe. |
| 14 | + * |
| 15 | + * @addtogroup stdbigos |
| 16 | + * @{ |
| 17 | + * @addtogroup clock |
| 18 | + * @{ |
| 19 | + */ |
| 20 | + |
| 21 | +/** |
| 22 | + * @brief Initialize the periodic clock subsystem. |
| 23 | + * |
| 24 | + * Initialize internal state and program the first timer deadline using |
| 25 | + * the provided tick quantum. |
| 26 | + * |
| 27 | + * @param tick_quantum Number of hardware timer ticks per software tick. |
| 28 | + * @return error_t E_OK on success, otherwise an error code. |
| 29 | + */ |
| 30 | +error_t clock_init(u64 tick_quantum); |
| 31 | + |
| 32 | +/** |
| 33 | + * @brief Return the current time in hardware timer ticks. |
| 34 | + * |
| 35 | + * This returns a monotonic timestamp suitable for computing deadlines. |
| 36 | + * |
| 37 | + * @return Current time in hardware ticks. |
| 38 | + */ |
| 39 | +u64 clock_now(void); |
| 40 | + |
| 41 | +/** |
| 42 | + * @brief Return the number of software ticks since boot. |
| 43 | + * |
| 44 | + * Software ticks are incremented by the timer IRQ handler and advance |
| 45 | + * at a rate of one per `tick_quantum` hardware ticks. |
| 46 | + * |
| 47 | + * @return Number of elapsed software ticks. |
| 48 | + */ |
| 49 | +u64 clock_ticks_now(void); |
| 50 | + |
| 51 | +/** |
| 52 | + * @brief Request the scheduler be woken in a number of software ticks. |
| 53 | + * |
| 54 | + * Programs the next switch tick to be `ticks_from_now` software ticks |
| 55 | + * in the future. This does not block; use `clock_next_switch_tick` |
| 56 | + * or `clock_ticks_to_next_switch` to query the scheduled value. |
| 57 | + * |
| 58 | + * @param ticks_from_now Number of software ticks from now to schedule. |
| 59 | + * @return error_t E_OK on success, otherwise an error code. |
| 60 | + */ |
| 61 | +error_t clock_set_next_switch_in(u64 ticks_from_now); |
| 62 | + |
| 63 | +/** |
| 64 | + * @brief Obtain the currently scheduled next-switch absolute tick. |
| 65 | + * |
| 66 | + * Writes the absolute software tick value at which the scheduler has |
| 67 | + * requested a switch into `out_tick`. |
| 68 | + * |
| 69 | + * @param out_tick Pointer to u64 to receive the absolute tick value. |
| 70 | + * @return error_t E_OK on success, EINVAL if `out_tick` is NULL, or |
| 71 | + * other error codes on failure. |
| 72 | + */ |
| 73 | +error_t clock_next_switch_tick(u64* out_tick); |
| 74 | + |
| 75 | +/** |
| 76 | + * @brief Obtain the number of software ticks until the next switch. |
| 77 | + * |
| 78 | + * Writes into `out_ticks` the number of ticks remaining until the |
| 79 | + * scheduled next-switch tick. If a next-switch is not scheduled, |
| 80 | + * behavior is implementation-defined. |
| 81 | + * |
| 82 | + * @param out_ticks Pointer to u64 to receive remaining ticks. |
| 83 | + * @return error_t E_OK on success, EINVAL if `out_ticks` is NULL, or |
| 84 | + * other error codes on failure. |
| 85 | + */ |
| 86 | +error_t clock_ticks_to_next_switch(u64* out_ticks); |
| 87 | + |
| 88 | +/** @} */ /* end group clock */ |
| 89 | +/** @} */ /* end group stdbigos */ |
| 90 | + |
| 91 | +#endif // !STDBIGOS_CLOCK |
0 commit comments