Skip to content

posix: implement xsi_single_process and posix_clock_selection #89068

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

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented Apr 25, 2025

  • posix: env: create z_getenv()..z_setenv() convenience functions
  • posix: env: move most implementations to env-common.c
  • posix: clock: create z_clock_gettime().. convenience functions.
  • posix: clock: move most implementations to clock_common.c
  • posix: implement the xsi_single_process option group
  • tests: posix: add test for the xsi_single_process option group
  • doc: posix: add doc for xsi_single_process option group
  • posix: implement the posix_clock_selection option group
  • tests: posix: add posix_clock_selection testsuite

Notable Changes

  • gettimeofday() is moved to the XSI_SINGLE_PROCESS Option Group (which is according to spec)
  • Remove artificial dependencies POSIX_SINGLE_PROCESS and POSIX_TIMERS from XSI_SINGLE_PROCESS
    • POSIX Option Groups should be independent
  • env functions required by XSI_SINGLE_PROCESS and POSIX_SINGLE_PROCESS moved to env-common.c
  • clock functions required by XSI_SINGLE_PROCESS, POSIX_TIMERS, and POSIX_CLOCK_SELECTION moved to clock-common.c
  • Clock base now protected by sys_sem instead of spinlock (to better support userspace)

Notable Observations

  • z_ functions will get replaced at a later date when we have resolution of Drop z_ prefix as identifier for internal/private functions and revert back to _  #58007
  • tests/lib/time should really be a part of tests/lib/c_lib since it tests the time() C89 function
    • Big problem is that it pulls in CONFIG_POSIX_TIMERS and CONFIG_XSI_SINGLE_PROCESS
    • Why? in the common libc implementationtime() uses clock_gettime(), which is maybe fine if the assumption is that one is using POSIX or running on top of a POSIX OS anyway, but that's definitely not the case in Zephyr.
      • it forms a dependency cycle at the API level
      • the z_clock_gettime() and similar functions in this PR could easily become a core part of Zephyr (i.e. promoted to k_clock_gettime() etc)
      • the dependency cycle (between libc and posix) would then be resolved via mutual dependency (Zephyr)
  • asctime_r(), ctime_r(), gmtime_r(), localtime_r() should all probably be moved to POSIX_C_LANG_SUPPORT_R since they are not part of ISO C until C23, which is not yet proposed in RFC: C11 (or C17) requirement #30105

Should be merged after #89063, #89107

@cfriedt cfriedt force-pushed the issue/88556/posix-gettimeofday-is-not-a-part-of-posix-timers-but-xsi-single-process branch 3 times, most recently from 247ce21 to d41e9e4 Compare April 25, 2025 10:39
@cfriedt cfriedt changed the title poxix: env: create z_getenv()..z_setenv() convenience functions poxix: implement xsi_single_process and posix_clock_selection Apr 25, 2025
@cfriedt cfriedt force-pushed the issue/88556/posix-gettimeofday-is-not-a-part-of-posix-timers-but-xsi-single-process branch 7 times, most recently from ff4b6b4 to 0fa03fe Compare April 25, 2025 12:53
@cfriedt cfriedt changed the title poxix: implement xsi_single_process and posix_clock_selection posix: implement xsi_single_process and posix_clock_selection Apr 25, 2025
@cfriedt cfriedt force-pushed the issue/88556/posix-gettimeofday-is-not-a-part-of-posix-timers-but-xsi-single-process branch 2 times, most recently from 84db803 to 4144abe Compare April 26, 2025 12:15
@cfriedt cfriedt force-pushed the issue/88556/posix-gettimeofday-is-not-a-part-of-posix-timers-but-xsi-single-process branch 5 times, most recently from 7f7d093 to 3f894b8 Compare May 3, 2025 09:40
@cfriedt cfriedt requested review from nashif, jukkar, ycsin and JordanYates May 3, 2025 10:34
@cfriedt cfriedt marked this pull request as ready for review May 3, 2025 10:34
@cfriedt cfriedt requested a review from carlescufi May 3, 2025 10:41
cfriedt added 9 commits May 4, 2025 20:31
To facilitate adding putenv in a separate compilation unit,
make z_getenv(), z_getenv_r(), z_setenv(), and z_unsetenv()
convenience functions.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Move most implementations to env-common.c in preparation for adding
putenv.c .

We also take this as an opportunity to switch from using k_spinlock
to sys_sem.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
To facilitate moving gettimeofday() and clock_nanosleep() to separate
compilation units, make z_clock_nanosleep(), z_clock_gettime(),
and z_clock_settime() convenience functions.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Move most implementations to clock_common.c in preparation for
moving gettimeofday() and clock_nanosleep() to different compilation
units.

We also take this as an opportunity to switch from using k_spinlock
to sys_sem.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
gettimeofday() was already implemented, but incorrectly lumped into
POSIX_TIMERS.

putenv() is really just a wrapper around setenv().

The only one left to implement was gethostid() which was relatively
trivial.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Add tests for the XSI_SINGLE_PROCESS Option Group.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Mark the XSI_SINGLE_PROCESS Option Group as supported.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Implement the POSIX_CLOCK_SELECTION Option Group.

This was mostly already done, but compiled / linked in the wrong places.

E.g. pthread_condattr_getclock() and pthread_condattr_setclock() were
in pthread.c and part of POSIX_THREADS_BASE. clock_nanosleep() was in
clock.c and part of POSIX_TIMERS.

This change builds them as part of clock_selection.c with
CONFIG_POSIX_CLOCK_SELECTION.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Add a testsuite for the POSIX_CLOCK_SELECTION Option Group which
consists of clock_nanosleep(), pthread_condattr_getclock(), and
pthread_condattr_setclock().

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
@cfriedt cfriedt force-pushed the issue/88556/posix-gettimeofday-is-not-a-part-of-posix-timers-but-xsi-single-process branch from 3f894b8 to b8c8acb Compare May 5, 2025 00:39
@cfriedt cfriedt requested a review from ycsin May 5, 2025 02:04
Copy link
Member

@ycsin ycsin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @cfriedt

@cfriedt
Copy link
Member Author

cfriedt commented May 11, 2025

@nashif, @andyross - the bot assigned his PR to you, and so I think it needs one or both if your approvals in order to be merged.

Feel free to reassign if you prefer.

@cfriedt cfriedt assigned cfriedt and unassigned nashif and andyross May 13, 2025
@nashif nashif merged commit 3c7e43f into zephyrproject-rtos:main May 14, 2025
28 checks passed
cfriedt added a commit to cfriedt/zephyr that referenced this pull request May 14, 2025
https://github.com/zephyrproject-rtos/zephyr/actions/runs/\
15014534061/job/42189411225

We were seeing a linker error of the form

```
picolibc/x86_64-zephyr-elf/lib/32/libc.a(libc_time_time.c.o): \
  in function `time':
time.c:(.text.time+0x11): undefined reference to `gettimeofday'
```

Update the dependency from CONFIG_POSIX_TIMERS to
CONFIG_XSI_SINGLE_PROCESS for gettimeofday().

Note: this is really only a workaround. The proper solution would
be to have libc functions not depend on POSIX functions.

Specifically, here

https://github.com/zephyrproject-rtos/picolibc/blob/\
51a8b32857e75345c37652a80b5cda98b28d69e5/newlib/libc/time/\
time.c#L54

and here

https://github.com/zephyrproject-rtos/zephyr/blob/\
3a4e128/lib/libc/common/\
source/time/time.c#L17

Also mentioned in zephyrproject-rtos#89068

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
cfriedt added a commit to cfriedt/zephyr that referenced this pull request May 15, 2025
https://github.com/zephyrproject-rtos/zephyr/actions/runs/\
15014534061/job/42189411225

We were seeing a linker error of the form

```
picolibc/x86_64-zephyr-elf/lib/32/libc.a(libc_time_time.c.o): \
  in function `time':
time.c:(.text.time+0x11): undefined reference to `gettimeofday'
```

Update the dependency from CONFIG_POSIX_TIMERS to
CONFIG_XSI_SINGLE_PROCESS for gettimeofday().

Note: this is really only a workaround. The proper solution would
be to have libc functions not depend on POSIX functions.

Specifically, here

https://github.com/zephyrproject-rtos/picolibc/blob/\
51a8b32857e75345c37652a80b5cda98b28d69e5/newlib/libc/time/\
time.c#L54

and here

https://github.com/zephyrproject-rtos/zephyr/blob/\
3a4e128/lib/libc/common/\
source/time/time.c#L17

Also mentioned in zephyrproject-rtos#89068

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
kartben pushed a commit that referenced this pull request May 15, 2025
https://github.com/zephyrproject-rtos/zephyr/actions/runs/\
15014534061/job/42189411225

We were seeing a linker error of the form

```
picolibc/x86_64-zephyr-elf/lib/32/libc.a(libc_time_time.c.o): \
  in function `time':
time.c:(.text.time+0x11): undefined reference to `gettimeofday'
```

Update the dependency from CONFIG_POSIX_TIMERS to
CONFIG_XSI_SINGLE_PROCESS for gettimeofday().

Note: this is really only a workaround. The proper solution would
be to have libc functions not depend on POSIX functions.

Specifically, here

https://github.com/zephyrproject-rtos/picolibc/blob/\
51a8b32857e75345c37652a80b5cda98b28d69e5/newlib/libc/time/\
time.c#L54

and here

https://github.com/zephyrproject-rtos/zephyr/blob/\
3a4e128/lib/libc/common/\
source/time/time.c#L17

Also mentioned in #89068

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
jlbwoods pushed a commit to jlbwoods/zephyr that referenced this pull request May 15, 2025
https://github.com/zephyrproject-rtos/zephyr/actions/runs/\
15014534061/job/42189411225

We were seeing a linker error of the form

```
picolibc/x86_64-zephyr-elf/lib/32/libc.a(libc_time_time.c.o): \
  in function `time':
time.c:(.text.time+0x11): undefined reference to `gettimeofday'
```

Update the dependency from CONFIG_POSIX_TIMERS to
CONFIG_XSI_SINGLE_PROCESS for gettimeofday().

Note: this is really only a workaround. The proper solution would
be to have libc functions not depend on POSIX functions.

Specifically, here

https://github.com/zephyrproject-rtos/picolibc/blob/\
51a8b32857e75345c37652a80b5cda98b28d69e5/newlib/libc/time/\
time.c#L54

and here

https://github.com/zephyrproject-rtos/zephyr/blob/\
3a4e128/lib/libc/common/\
source/time/time.c#L17

Also mentioned in zephyrproject-rtos#89068

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants