Skip to content

Commit 4eafc6c

Browse files
committed
posix: time.h: move to posix_time.h
To avoid conflicts between the C library's time.h and posix time.h, use an "override" header when necessary for C libraries that do not themselves provide POSIX definitions in time.h . This will allow non-POSIX C libraries to take advantage of Zephyr's implementation.
1 parent 7d94d10 commit 4eafc6c

File tree

24 files changed

+287
-171
lines changed

24 files changed

+287
-171
lines changed

include/zephyr/posix/mqueue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#ifndef ZEPHYR_INCLUDE_POSIX_MESSAGE_PASSING_H_
88
#define ZEPHYR_INCLUDE_POSIX_MESSAGE_PASSING_H_
99

10+
#include <time.h>
11+
1012
#include <zephyr/kernel.h>
11-
#include <zephyr/posix/time.h>
1213
#include <zephyr/posix/fcntl.h>
1314
#include <zephyr/posix/signal.h>
1415
#include <zephyr/posix/sys/stat.h>

include/zephyr/posix/posix_features.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@
5656
#define _POSIX_CLOCK_SELECTION _POSIX_VERSION
5757
#endif
5858

59-
#ifdef CONFIG_POSIX_CPUTIME
60-
#define _POSIX_CPUTIME _POSIX_VERSION
61-
#endif
62-
6359
#ifdef CONFIG_POSIX_FSYNC
6460
#define _POSIX_FSYNC _POSIX_VERSION
6561
#endif
@@ -90,10 +86,6 @@
9086
#define _POSIX_MESSAGE_PASSING _POSIX_VERSION
9187
#endif
9288

93-
#ifdef CONFIG_POSIX_MONOTONIC_CLOCK
94-
#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION
95-
#endif
96-
9789
/* #define _POSIX_PRIORITIZED_IO (-1L) */
9890

9991
#ifdef CONFIG_POSIX_PRIORITY_SCHEDULING
@@ -161,10 +153,6 @@
161153
/* #define _POSIX_THREAD_ROBUST_PRIO_INHERIT (-1L) */
162154
/* #define _POSIX_THREAD_ROBUST_PRIO_PROTECT (-1L) */
163155

164-
#ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS
165-
#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION
166-
#endif
167-
168156
/* #define _POSIX_THREAD_SPORADIC_SERVER (-1L) */
169157

170158
#ifdef CONFIG_POSIX_THREADS

include/zephyr/posix/posix_time.h

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* Copyright The Zephyr Project Contributors
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_TIME_H_
8+
#define ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_TIME_H_
9+
10+
#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__)
11+
12+
#include <stddef.h>
13+
14+
#include <zephyr/sys/clock.h>
15+
#include <zephyr/toolchain.h>
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
/* clock_t must be defined in the libc time.h */
22+
/* size_t must be defined in the libc stddef.h */
23+
/* time_t must be defined in the libc time.h */
24+
25+
#if !defined(_CLOCKID_T_DECLARED) && !defined(__clockid_t_defined)
26+
typedef unsigned long clockid_t;
27+
#define _CLOCKID_T_DECLARED
28+
#define __clockid_t_defined
29+
#endif
30+
31+
#if !defined(_TIMER_T_DECLARED) && !defined(__timer_t_defined)
32+
typedef unsigned long timer_t;
33+
#define _TIMER_T_DECLARED
34+
#define __timer_t_defined
35+
#endif
36+
37+
#if !defined(_LOCALE_T_DECLARED) && !defined(__locale_t_defined)
38+
#ifdef CONFIG_NEWLIB_LIBC
39+
struct __locale_t;
40+
typedef struct __locale_t *locale_t;
41+
#else
42+
typedef void *locale_t;
43+
#endif
44+
#define _LOCALE_T_DECLARED
45+
#define __locale_t_defined
46+
#endif
47+
48+
#if !defined(_PID_T_DECLARED) && !defined(__pid_t_defined)
49+
typedef int pid_t;
50+
#define _PID_T_DECLARED
51+
#define __pid_t_defined
52+
#endif
53+
54+
#if defined(_POSIX_REALTIME_SIGNALS)
55+
struct sigevent;
56+
#endif
57+
58+
/* struct tm must be defined in the libc time.h */
59+
60+
#if __STDC_VERSION__ >= 201112L
61+
/* struct timespec must be defined in the libc time.h */
62+
#else
63+
#if !defined(_TIMESPEC_DECLARED) && !defined(__timespec_defined)
64+
typedef struct {
65+
time_t tv_sec;
66+
long tv_nsec;
67+
} timespec_t;
68+
#define _TIMESPEC_DECLARED
69+
#define __timespec_defined
70+
#endif
71+
#endif
72+
73+
#if !defined(_ITIMERSPEC_DECLARED) && !defined(__itimerspec_defined)
74+
struct itimerspec {
75+
struct timespec it_interval;
76+
struct timespec it_value;
77+
};
78+
#define _ITIMERSPEC_DECLARED
79+
#define __itimerspec_defined
80+
#endif
81+
82+
/* NULL must be defined in the libc stddef.h */
83+
84+
#ifndef CLOCK_REALTIME
85+
#define CLOCK_REALTIME ((clockid_t)SYS_CLOCK_REALTIME)
86+
#endif
87+
88+
#ifndef CLOCKS_PER_SEC
89+
#if defined(_XOPEN_SOURCE)
90+
#define CLOCKS_PER_SEC 1000000
91+
#else
92+
#define CLOCKS_PER_SEC CONFIG_TICKS_PER_SECOND
93+
#endif
94+
#endif
95+
96+
#if defined(_POSIX_CPUTIME) || defined(__DOXYGEN__)
97+
#ifndef CLOCK_PROCESS_CPUTIME_ID
98+
#define CLOCK_PROCESS_CPUTIME_ID ((clockid_t)2)
99+
#endif
100+
#endif
101+
102+
#if defined(_POSIX_THREAD_CPUTIME) || defined(__DOXYGEN__)
103+
#ifndef CLOCK_THREAD_CPUTIME_ID
104+
#define CLOCK_THREAD_CPUTIME_ID ((clockid_t)3)
105+
#endif
106+
#endif
107+
108+
#if defined(_POSIX_MONOTONIC_CLOCK) || defined(__DOXYGEN__)
109+
#ifndef CLOCK_MONOTONIC
110+
#define CLOCK_MONOTONIC ((clockid_t)SYS_CLOCK_MONOTONIC)
111+
#endif
112+
#endif
113+
114+
#ifndef TIMER_ABSTIME
115+
#define TIMER_ABSTIME SYS_TIMER_ABSTIME
116+
#endif
117+
118+
/* asctime() must be declared in the libc time.h */
119+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__)
120+
char *asctime_r(const struct tm *ZRESTRICT tm, char *ZRESTRICT buf);
121+
#endif
122+
/* clock() must be declared in the libc time.h */
123+
#if defined(_POSIX_CPUTIME) || defined(__DOXYGEN__)
124+
int clock_getcpuclockid(pid_t pid, clockid_t *clock_id);
125+
#endif
126+
#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__)
127+
int clock_getres(clockid_t clock_id, struct timespec *ts);
128+
int clock_gettime(clockid_t clock_id, struct timespec *ts);
129+
#endif
130+
#if defined(_POSIX_CLOCK_SELECTION) || defined(__DOXYGEN__)
131+
int clock_nanosleep(clockid_t clock_id, int flags,
132+
const struct timespec *rqtp, struct timespec *rmtp);
133+
#endif
134+
#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__)
135+
int clock_settime(clockid_t clock_id, const struct timespec *ts);
136+
#endif
137+
/* ctime() must be declared in the libc time.h */
138+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__)
139+
char *ctime_r(const time_t *clock, char *buf);
140+
#endif
141+
/* difftime() must be declared in the libc time.h */
142+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
143+
struct tm *getdate(const char *string);
144+
#endif
145+
/* gmtime() must be declared in the libc time.h */
146+
#if __STDC_VERSION__ >= 202311L
147+
/* gmtime_r() must be declared in the libc time.h */
148+
#else
149+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__)
150+
struct tm *gmtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result);
151+
#endif
152+
#endif
153+
/* localtime() must be declared in the libc time.h */
154+
#if __STDC_VERSION__ >= 202311L
155+
/* localtime_r() must be declared in the libc time.h */
156+
#else
157+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__)
158+
struct tm *localtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result);
159+
#endif
160+
#endif
161+
/* mktime() must be declared in the libc time.h */
162+
#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__)
163+
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
164+
#endif
165+
/* strftime() must be declared in the libc time.h */
166+
size_t strftime_l(char *ZRESTRICT s, size_t maxsize,
167+
const char *ZRESTRICT format, const struct tm *ZRESTRICT timeptr,
168+
locale_t locale);
169+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
170+
char *strptime(const char *ZRESTRICT s, const char *ZRESTRICT format, struct tm *ZRESTRICT tm);
171+
#endif
172+
/* time() must be declared in the libc time.h */
173+
#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__)
174+
int timer_create(clockid_t clockId, struct sigevent *ZRESTRICT evp, timer_t *ZRESTRICT timerid);
175+
int timer_delete(timer_t timerid);
176+
int timer_getoverrun(timer_t timerid);
177+
int timer_gettime(timer_t timerid, struct itimerspec *its);
178+
int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
179+
struct itimerspec *ovalue);
180+
#endif
181+
182+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
183+
extern int daylight;
184+
extern long timezone;
185+
#endif
186+
187+
extern char *tzname[];
188+
189+
#ifdef __cplusplus
190+
}
191+
#endif
192+
193+
#endif /* defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) */
194+
195+
#endif /* ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_TIME_H_ */

include/zephyr/posix/pthread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
#include <stdlib.h>
1111
#include <string.h>
12+
#include <time.h>
1213

1314
#include <zephyr/kernel.h>
14-
#include <zephyr/posix/time.h>
1515
#include <zephyr/posix/unistd.h>
1616
#include <zephyr/posix/sched.h>
1717

include/zephyr/posix/semaphore.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#ifndef ZEPHYR_INCLUDE_POSIX_SEMAPHORE_H_
77
#define ZEPHYR_INCLUDE_POSIX_SEMAPHORE_H_
88

9-
#include <zephyr/posix/time.h>
9+
#include <time.h>
10+
1011
#include <zephyr/posix/posix_types.h>
1112

1213
#ifdef __cplusplus

include/zephyr/posix/signal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef ZEPHYR_INCLUDE_POSIX_SIGNAL_H_
77
#define ZEPHYR_INCLUDE_POSIX_SIGNAL_H_
88

9+
#include <time.h>
10+
911
/* include posix_types.h before posix_features.h (here) to avoid build errors against newlib */
1012
#include <zephyr/posix/posix_types.h>
1113
#include "posix_features.h"

include/zephyr/posix/time.h

Lines changed: 0 additions & 114 deletions
This file was deleted.

include/zephyr/posix/unistd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef ZEPHYR_INCLUDE_POSIX_UNISTD_H_
77
#define ZEPHYR_INCLUDE_POSIX_UNISTD_H_
88

9+
#include <time.h>
10+
911
#include <zephyr/posix/posix_types.h>
1012

1113
#ifdef CONFIG_POSIX_API

0 commit comments

Comments
 (0)