SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_C_TIME_POSIX_H_
11#define STK_C_TIME_POSIX_H_
12
32
33/* Same newlib guard as in stk_c_pthread.h: <sys/types.h> must not define its own pthread types, the shim provides
34 them (needed here because this header may be included before stk_c_pthread.h). */
35#ifndef _SYS__PTHREADTYPES_H_
36 #define _SYS__PTHREADTYPES_H_
37 #define STK_C_PTHREADTYPES_GUARD
38#endif
39
40#ifndef STK_C_SYSTEM_TIME_H
41 #if defined(__GNUC__) || defined(__clang__)
42 #include_next <time.h>
43 #else
44 #error "Define STK_C_SYSTEM_TIME_H to the toolchain's time.h (e.g. <../inc/c/time.h>) for this compiler"
45 #endif
46#else
47 #include STK_C_SYSTEM_TIME_H
48#endif
49
50#include <stdint.h>
51#include <stddef.h>
52#include <sys/types.h> /* useconds_t */
53
54/* Toolchains which already ship the POSIX timer types: newlib (arm-none-eabi-gcc) defines timer_t in <sys/types.h> and
55 struct itimerspec in <sys/timespec.h>, glibc has both too. Detected here, after the toolchain's headers were read,
56 so that the types are not declared twice. Other toolchains: define the STK_C_NO_* macros below by hand. */
57#if defined(__NEWLIB__) || defined(__GLIBC__)
58 #ifndef STK_C_NO_TIMER_T
59 #define STK_C_NO_TIMER_T
60 #endif
61 #ifndef STK_C_NO_ITIMERSPEC
62 #define STK_C_NO_ITIMERSPEC
63 #endif
64 /* union sigval / struct sigevent come from the toolchain's <signal.h> (use the layer's signal.h) */
65 #ifndef STK_C_DEFINE_SIGEVENT
66 #define STK_C_DEFINE_SIGEVENT (0)
67 #endif
68#endif
69#if defined(_SYS_TIMESPEC_H) && !defined(STK_C_NO_ITIMERSPEC)
70 #define STK_C_NO_ITIMERSPEC
71#endif
72
73#ifdef __cplusplus
74extern "C" {
75#endif
76
87
88#ifndef CLOCK_REALTIME
89 #define CLOCK_REALTIME (0)
90#endif
91#ifndef CLOCK_MONOTONIC
92 #define CLOCK_MONOTONIC (1)
93#endif
94#ifndef TIMER_ABSTIME
95 #define TIMER_ABSTIME (1)
96#endif
97#ifndef TIME_UTC
98 #define TIME_UTC (1)
99#endif
100
101#ifndef STK_C_NO_TIME_DECLS
102
108int clock_gettime(clockid_t clk_id, struct timespec *ts);
109
114int clock_getres(clockid_t clk_id, struct timespec *res);
115
119int timespec_get(struct timespec *ts, int base);
120
125int nanosleep(const struct timespec *req, struct timespec *rem);
126
130int clock_nanosleep(clockid_t clk_id, int flags, const struct timespec *req, struct timespec *rem);
131
132#endif /* STK_C_NO_TIME_DECLS */
133
134#ifndef STK_C_NO_SLEEP_DECLS
135
139int usleep(useconds_t usec);
140
144unsigned int sleep(unsigned int seconds);
145
146#endif /* STK_C_NO_SLEEP_DECLS */
147
148/* --------------------------------------------------------------------------------------------------------------- */
149/* POSIX timers */
150/* --------------------------------------------------------------------------------------------------------------- */
151
156#ifndef STK_C_NO_TIMER_T
157 typedef void *timer_t;
158#endif
159
165#ifndef STK_C_DEFINE_SIGEVENT
166 #define STK_C_DEFINE_SIGEVENT (1)
167#endif
168
169#if STK_C_DEFINE_SIGEVENT && !defined(SIGEV_NONE) /* <signal.h> was included first: it already provides them */
170 union sigval
171 {
174 };
175
184
185 #define SIGEV_NONE (0)
186 #define SIGEV_SIGNAL (1)
187 #define SIGEV_THREAD (2)
188#endif
189
190#if !STK_C_DEFINE_SIGEVENT
191 /* complete type: include <signal.h> (the layer's signal.h makes struct sigevent usable with SIGEV_THREAD) */
192 struct sigevent;
193#endif
194
195#ifndef STK_C_NO_ITIMERSPEC
197{
198 struct timespec it_interval;
199 struct timespec it_value;
200};
201#endif
202
206#ifndef STK_C_TIMERS_MAX
207 #define STK_C_TIMERS_MAX (8U)
208#endif
209
215void stk_posix_set_wall_time(int64_t unix_seconds);
216
223void stk_posix_timer_bind_host(void *timer_host);
224
225#ifndef STK_C_NO_TIMER_DECLS
226
232int timer_create(clockid_t clk_id, struct sigevent *sevp, timer_t *timerid);
233
237int timer_delete(timer_t timerid);
238
246int timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);
247
251int timer_gettime(timer_t timerid, struct itimerspec *curr_value);
252
256int timer_getoverrun(timer_t timerid);
257
258#endif /* STK_C_NO_TIMER_DECLS */
259
260#ifdef __cplusplus
261}
262#endif
263
264#endif /* STK_C_TIME_POSIX_H_ */
int timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value, struct itimerspec *old_value)
Arm (it_value != 0) or disarm (it_value == 0) a timer.
int nanosleep(const struct timespec *req, struct timespec *rem)
Suspend the calling task for a relative time (rounded UP to whole ticks). Cancellation point.
void stk_posix_set_wall_time(int64_t unix_seconds)
Set the current wall time used by time() and gettimeofday() (see sys/time.h).
void * timer_t
Definition time.h:157
int clock_nanosleep(clockid_t clk_id, int flags, const struct timespec *req, struct timespec *rem)
Suspend for a relative time or until an absolute time (flags = TIMER_ABSTIME). Cancellation point.
int clock_getres(clockid_t clk_id, struct timespec *res)
Get the resolution (one kernel tick) of a clock.
unsigned int sleep(unsigned int seconds)
Suspend for seconds seconds. Cancellation point.
int timespec_get(struct timespec *ts, int base)
C11 timespec_get(): base must be TIME_UTC (reports the same STK timeline).
void stk_posix_timer_bind_host(void *timer_host)
Provide the timer host which runs POSIX timers.
int timer_create(clockid_t clk_id, struct sigevent *sevp, timer_t *timerid)
Create a timer.
int clock_gettime(clockid_t clk_id, struct timespec *ts)
Get the current time of a clock.
int timer_gettime(timer_t timerid, struct itimerspec *curr_value)
Get the time until the next expiration and the period.
int usleep(useconds_t usec)
Suspend for usec microseconds. Cancellation point.
int timer_delete(timer_t timerid)
Delete a timer (waits until a running notification has returned).
int timer_getoverrun(timer_t timerid)
Get the overrun count. STK does not track overruns.
Definition time.h:171
void * sival_ptr
Definition time.h:173
int sival_int
Definition time.h:172
void * sigev_notify_attributes
Definition time.h:182
void(* sigev_notify_function)(union sigval)
Definition time.h:181
int sigev_signo
Definition time.h:179
union sigval sigev_value
Definition time.h:180
int sigev_notify
Definition time.h:178
struct timespec it_interval
Definition time.h:198
struct timespec it_value
Definition time.h:199