-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
posix: implement _POSIX_THREAD_SAFE_FUNCTIONS
's time functions
#74180
posix: implement _POSIX_THREAD_SAFE_FUNCTIONS
's time functions
#74180
Conversation
feca070
to
3bb69bf
Compare
78031cc
to
1b51d12
Compare
_POSIX_THREAD_SAFE_FUNCTIONS
0266adb
to
5487180
Compare
32d5924
to
8d5c07a
Compare
_POSIX_THREAD_SAFE_FUNCTIONS
_POSIX_THREAD_SAFE_FUNCTIONS
's time functions
bbeab7c
to
75e322a
Compare
lib/libc/minimal/include/time.h
Outdated
char *asctime(const struct tm *timeptr); | ||
struct tm *localtime(const time_t *timer); | ||
struct tm *localtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result); | ||
char *ctime(const time_t *clock); | ||
|
||
#if defined(CONFIG_COMMON_LIBC_ASCTIME_R) || defined(__DOXYGEN__) | ||
char *asctime_r(const struct tm *ZRESTRICT tp, char *ZRESTRICT buf); | ||
#endif /* CONFIG_COMMON_LIBC_ASCTIME_R */ | ||
|
||
#if defined(CONFIG_COMMON_LIBC_CTIME_R) || defined(__DOXYGEN__) | ||
char *ctime_r(const time_t *clock, char *buf); | ||
#endif /* CONFIG_COMMON_LIBC_CTIME_R */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the header file, this would probably work well.
char *asctime(const struct tm *timeptr); | |
struct tm *localtime(const time_t *timer); | |
struct tm *localtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result); | |
char *ctime(const time_t *clock); | |
#if defined(CONFIG_COMMON_LIBC_ASCTIME_R) || defined(__DOXYGEN__) | |
char *asctime_r(const struct tm *ZRESTRICT tp, char *ZRESTRICT buf); | |
#endif /* CONFIG_COMMON_LIBC_ASCTIME_R */ | |
#if defined(CONFIG_COMMON_LIBC_CTIME_R) || defined(__DOXYGEN__) | |
char *ctime_r(const time_t *clock, char *buf); | |
#endif /* CONFIG_COMMON_LIBC_CTIME_R */ | |
char *asctime(const struct tm *timeptr); | |
struct tm *localtime(const time_t *timer); | |
char *ctime(const time_t *clock); | |
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__) | |
char *asctime_r(const struct tm *ZRESTRICT tp, char *ZRESTRICT buf); | |
char *ctime_r(const time_t *clock, char *buf); | |
struct tm *localtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result); | |
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do I inject _POSIX_THREAD_SAFE_FUNCTIONS
into this C header? I had a look at one of the picolibc time.h
and they are not guarded
lib/posix/options/Kconfig.pthread
Outdated
config POSIX_THREAD_SAFE_FUNCTIONS | ||
bool "POSIX thread-safe functions" | ||
select COMMON_LIBC_ASCTIME_R | ||
select COMMON_LIBC_GMTIME_R | ||
select COMMON_LIBC_LOCALTIME_R_UTC | ||
select COMMON_LIBC_CTIME_R | ||
help | ||
Select 'y' here to enable POSIX thread-safe functions including asctime_r(), ctime_r(), | ||
flockfile(), ftrylockfile(), funlockfile(), getc_unlocked(), getchar_unlocked(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Darn - I just realized _POSIX_THREAD_SAFE_FUNCTIONS
is an Option, and not an Option Group :(
Based on posixoptions(7), it seems to be a combination of POSIX_C_LANG_SUPPORT_R
and POSIX_FILE_SYSTEM_R
Option Groups
For reference
https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_subprofiles.html
So likely we will need a new Kconfig.c_lang_r
like this, approximately.
config POSIX_C_LANG_SUPPORT_R
select COMMON_LIBC_ASCTIME_R
select COMMON_LIBC_CTIME_R
select COMMON_LIBC_GMTIME_R
select COMMON_LIBC_LOCALTIME_R
With that, I think there are some nuances with how POSIX_THREAD_SAFE_FUNCTIONS
should behave. Let me know if this makes sense.
config POSIX_THREAD_SAFE_FUNCTIONS | |
bool "POSIX thread-safe functions" | |
select COMMON_LIBC_ASCTIME_R | |
select COMMON_LIBC_GMTIME_R | |
select COMMON_LIBC_LOCALTIME_R_UTC | |
select COMMON_LIBC_CTIME_R | |
help | |
Select 'y' here to enable POSIX thread-safe functions including asctime_r(), ctime_r(), | |
flockfile(), ftrylockfile(), funlockfile(), getc_unlocked(), getchar_unlocked(), | |
config POSIX_THREAD_SAFE_FUNCTIONS | |
bool "POSIX thread-safe functions" | |
select POSIX_FILE_SYSTEM_R if POSIX_FILE_SYSTEM | |
select POSIX_C_LANG_SUPPORT_R | |
help | |
Select 'y' here to enable POSIX thread-safe functions including asctime_r(), ctime_r(), | |
flockfile(), ftrylockfile(), funlockfile(), getc_unlocked(), getchar_unlocked(), |
config COMMON_LIBC_GMTIME_R | ||
bool | ||
help | ||
common implementation of gmtime_r(). | ||
|
||
config COMMON_LIBC_LOCALTIME_R_UTC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this one can be abbreviated without the UTC.
From here
Unlike localtime(), the localtime_r() function is not required to set tzname. If localtime_r() sets tzname, it shall also set daylight and timezone. If localtime_r() does not set tzname, it shall not set daylight and shall not set timezone. If the tm structure member tm_zone is accessed after the value of TZ is subsequently modified, the behaviour is undefined.
So if our implementation of localtime_r()
never sets the timezone (i.e. is UTC-only), it's still compliant. No special treatment necessary. My assumption is that most would probably like to avoid bringing timezone details into Zephyr.
config COMMON_LIBC_LOCALTIME_R_UTC | |
config COMMON_LIBC_LOCALTIME_R |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
COMMON_LIBC_LOCALTIME_R_UTC
is shared by both the localtime()
& localtime_r()
, I guess that's why I added that suffix to make it obvious that both functions only work for UTC, should I still remove the _UTC
suffix? Or maybe I should create another Kconfig for localtime()
?
@@ -39,6 +39,9 @@ config MINIMAL_LIBC_TIME | |||
bool "Time functions" | |||
select COMMON_LIBC_TIME if POSIX_TIMERS | |||
select COMMON_LIBC_GMTIME_R | |||
select COMMON_LIBC_ASCTIME | |||
select COMMON_LIBC_LOCALTIME_R_UTC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
select COMMON_LIBC_LOCALTIME_R_UTC | |
select COMMON_LIBC_LOCALTIME_R |
struct tm *localtime_r(const time_t *timer, struct tm *result) | ||
{ | ||
return gmtime_r(timer, result); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct tm *localtime_r(const time_t *timer, struct tm *result) | |
{ | |
return gmtime_r(timer, result); | |
} | |
#ifdef CONFIG_LIBC_TIME_LOCALTIME_R | |
struct tm *localtime_r(const time_t *timer, struct tm *result) | |
{ | |
return gmtime_r(timer, result); | |
} | |
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
localtime_r()
isn't a C function?
Pretty close - get some sleep and we'll check in again in the morning, your time. |
@ycsin - I'm sorry that this PR (as well as several others you have made) have been delayed. I think this is partly due to false positives in Zephyr's CI. As a workaround, feel free to include this commit in your PR. |
75e322a
to
60b56a8
Compare
60b56a8
to
4832201
Compare
DNM: has commit from PR that needs to be merged first. |
`readdir_r()` belongs to the following option group POSIX_FILE_SYSTEM_R: Thread-Safe File System Create a new Kconfig `CONFIG_POSIX_FILE_SYSTEM_R` to compile it. Signed-off-by: Yong Cong Sin <ycsin@meta.com> Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
Implemented the following: - `asctime_r()` - `asctime()` - `localtime()` - `localtime_r()` - `ctime()` - `ctime_r()` Specifically: - the implementation of `localtime()` & `localtime_r()` simply wraps around the gmtime() & gmtime_r() functions, the results are always expressed as UTC. - `ctime()` is equivalent to `asctime(localtime(clock))`, it inherits the limitation of `localtime()` as well, which only supports UTC results currently. Added tests for these newly implemented functions. Signed-off-by: Yong Cong Sin <ycsin@meta.com> Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
4832201
to
12c7543
Compare
Add:
asctime
asctime_r
localtime
localtime_r
ctime
ctime_r