KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/timer.h
4 Copyright (C) 2025 Paul Cercueil
5*/
6
7/** \file kos/timer.h
8 \brief Timer functionality.
9 \ingroup timers
10
11 This file contains functions for reading the internal timer provided
12 by the architecture.
13
14 \sa arch/timer.h
15
16 \author Paul Cercueil
17*/
18#ifndef __KOS_TIMER_H
19#define __KOS_TIMER_H
20
21#include <sys/cdefs.h>
22__BEGIN_DECLS
23
24#include <arch/timer.h>
25#include <stdint.h>
26#include <time.h>
27
28typedef struct timespec timespec_t;
29
30/** \brief Get the current uptime of the system.
31 \ingroup timers
32
33 This function retrieves the number of seconds and nanoseconds since KOS was
34 started.
35
36 \return The current uptime of the system as a timespec struct.
37*/
38static inline timespec_t timer_gettime(void) {
39 return arch_timer_gettime();
40}
41
42/** \brief Get the current uptime of the system (in milliseconds).
43 \ingroup timers
44
45 This function retrieves the number of milliseconds since KOS was started. It
46 is equivalent to calling timer_ms_gettime() and combining the number of
47 seconds and milliseconds into one 64-bit value.
48
49 \return The number of milliseconds since KOS started.
50*/
51static inline uint64_t timer_ms_gettime64(void) {
53
54 return (uint64_t)time.tv_sec * 1000 + (uint64_t)(time.tv_nsec / 1000000);
55}
56
57/** \brief Get the current uptime of the system (in microseconds).
58 \ingroup timers
59
60 This function retrieves the number of microseconds since KOS was started.
61
62 \return The uptime in microseconds.
63*/
64static inline uint64_t timer_us_gettime64(void) {
66
67 return (uint64_t)time.tv_sec * 1000000 + (uint64_t)(time.tv_nsec / 1000);
68}
69
70/** \brief Get the current uptime of the system (in nanoseconds).
71 \ingroup timers
72
73 This function retrieves the number of nanoseconds since KOS was started.
74
75 \return The uptime in nanoseconds.
76*/
77static inline uint64_t timer_ns_gettime64(void) {
79
80 return (uint64_t)time.tv_sec * 1000000000 + (uint64_t)time.tv_nsec;
81}
82
83/** \brief Get the current uptime of the system (in secs and millisecs).
84 \ingroup timers
85
86 This function retrieves the number of seconds and milliseconds since KOS was
87 started.
88
89 \param secs A pointer to store the number of seconds since boot
90 into.
91 \param msecs A pointer to store the number of milliseconds past
92 a second since boot.
93 \note To get the total number of milliseconds since boot,
94 calculate (*secs * 1000) + *msecs, or use the
95 timer_ms_gettime64() function.
96*/
97static inline void timer_ms_gettime(uint32_t *secs, uint32_t *msecs) {
99
100 if(secs) *secs = time.tv_sec;
101 if(msecs) *msecs = (uint32_t)(time.tv_nsec / 1000000);
102}
103
104/** \brief Get the current uptime of the system (in secs and microsecs).
105 \ingroup timers
106
107 This function retrieves the number of seconds and microseconds since KOS was
108 started.
109
110 \note To get the total number of microseconds since boot,
111 calculate (*secs * 1000000) + *usecs, or use the
112 timer_us_gettime64() function.
113
114 \param secs A pointer to store the number of seconds since boot
115 into.
116 \param usecs A pointer to store the number of microseconds past
117 a second since boot.
118*/
119static inline void timer_us_gettime(uint32_t *secs, uint32_t *usecs) {
120 timespec_t time = timer_gettime();
121
122 if(secs) *secs = time.tv_sec;
123 if(usecs) *usecs = (uint32_t)(time.tv_nsec / 1000);
124}
125
126/** \brief Get the current uptime of the system (in secs and nanosecs).
127 \ingroup timers
128
129 This function retrieves the number of seconds and nanoseconds since KOS was
130 started.
131
132 \note To get the total number of nanoseconds since boot,
133 calculate (*secs * 1000000000) + *nsecs, or use the
134 timer_ns_gettime64() function.
135
136 \param secs A pointer to store the number of seconds since boot
137 into.
138 \param nsecs A pointer to store the number of nanoseconds past
139 a second since boot.
140*/
141static inline void timer_ns_gettime(uint32_t *secs, uint32_t *nsecs) {
142 timespec_t time = timer_gettime();
143
144 if(secs) *secs = time.tv_sec;
145 if(nsecs) *nsecs = (uint32_t)time.tv_nsec;
146}
147
148/** \brief Spin-loop delay function with microsecond granularity
149 \ingroup timers
150
151 This function is meant as a very accurate delay function, even if threading
152 and interrupts are disabled. It is a delay and not a sleep, which means that
153 the CPU will be busy-looping during that time frame. For any time frame
154 bigger than a few hundred microseconds, it is recommended to sleep instead.
155
156 Note that the parameter is 16-bit, which means that the maximum acceptable
157 value is 65535 microseconds.
158
159 \param us The number of microseconds to wait for.
160 \sa timer_spin_delay_ns, thd_sleep
161*/
162static inline void timer_spin_delay_us(unsigned short us) {
163 uint64_t timeout = timer_us_gettime64() + us;
164
165 /* Note that we don't actually care about the counter overflowing.
166 Nobody will run their Dreamcast straight for 584942 years. */
167 while(timer_us_gettime64() < timeout);
168}
169
170
171/** \brief Spin-loop delay function with nanosecond granularity
172 \ingroup timers
173
174 This function is meant as a very accurate delay function, even if threading
175 and interrupts are disabled. It is a delay and not a sleep, which means that
176 the CPU will be busy-looping during that time frame.
177
178 Note that the parameter is 16-bit, which means that the maximum acceptable
179 value is 65535 nanoseconds.
180
181 \param ns The number of nanoseconds to wait for.
182 \sa timer_spin_delay_us, thd_sleep
183*/
184static inline void timer_spin_delay_ns(unsigned short ns) {
185 uint64_t timeout = timer_ns_gettime64() + ns;
186
187 /* Note that we don't actually care about the counter overflowing.
188 Nobody will run their Dreamcast straight for 585 years. */
189 while(timer_ns_gettime64() < timeout);
190}
191
192/** \brief Spin-loop delay function with millisecond granularity
193 \ingroup timers
194
195 This function should never be used, and is only used for compatibility with
196 older code. It makes no sense to busy-wait for that long.
197
198 \param ms The number of microseconds to wait for.
199 \sa thd_sleep
200*/
201
202__depr("Do not use timer_spin_sleep(), use thd_sleep() instead")
203static inline void timer_spin_sleep(unsigned int ms) {
204 uint64_t timeout = timer_ms_gettime64() + ms;
205
206 while(timer_ms_gettime64() < timeout);
207}
208
209__END_DECLS
210
211#endif /* __KOS_TIMER_H */
static uint32_t("Please see purupuru_effect_t for modern equivalent.") PURUPURU_EFFECT2_UINTENSITY(uint8_t x)
Definition purupuru.h:96
#define inline
Definition cdefs.h:107
Do not use timer_spin_sleep()
Spin-loop delay function with millisecond granularity.
static void timer_ns_gettime(uint32_t *secs, uint32_t *nsecs)
Get the current uptime of the system (in secs and nanosecs).
Definition timer.h:141
static uint64_t timer_us_gettime64(void)
Get the current uptime of the system (in microseconds).
Definition timer.h:64
static timespec_t timer_gettime(void)
Get the current uptime of the system.
Definition timer.h:38
static uint64_t timer_ms_gettime64(void)
Get the current uptime of the system (in milliseconds).
Definition timer.h:51
static uint64_t timer_ns_gettime64(void)
Get the current uptime of the system (in nanoseconds).
Definition timer.h:77
static void timer_ms_gettime(uint32_t *secs, uint32_t *msecs)
Get the current uptime of the system (in secs and millisecs).
Definition timer.h:97
static void timer_us_gettime(uint32_t *secs, uint32_t *usecs)
Get the current uptime of the system (in secs and microsecs).
Definition timer.h:119
static void timer_spin_delay_ns(unsigned short ns)
Spin-loop delay function with nanosecond granularity.
Definition timer.h:184
static void timer_spin_delay_us(unsigned short us)
Spin-loop delay function with microsecond granularity.
Definition timer.h:162
static struct timespec arch_timer_gettime(void)
Get the current uptime of the system (in seconds and nanoseconds).
Definition timer.h:259
struct timespec timespec_t
Definition timer.h:28
Low-level timer functionality.
KOS-implementation of select C11 and POSIX extensions.