Skip to content
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

configs: common: enable c++11 thread support via c11 threads #774

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented Aug 11, 2024

Notes for Revision 2

Note

This is the second revision of this change, the first being in #735.

This revision adds a weak default implementations of C11 <threads.h> API functions, so that Zephyr applications can continue to build and link with default settings (i.e. without enabling the options below):

CONFIG_COMMON_LIBC_THRD=y
CONFIG_POSIX_API=y
CONFIG_DYNAMIC_THREAD=y
CONFIG_THREAD_STACK_INFO=y

Overview

This change allows Zephyr developers to use most or all of the C++ facilities in gcc that depend on gthread support.

Those facilities include (but are likely not limited to)

  • std::thread
  • std::this_thread
  • std::mutex
  • std::recursive_mutex
  • std::shared_mutex
  • std::condition_variable
  • std::binary_semaphore
  • std::counting_semaphore
  • std::lock_guard
  • std::unique_lock
  • std::scoped_lock
  • std::shared_lock
  • std::syncstream
  • std::future
  • std::async

Associated Zephyr SDK PRs

Associated Zephyr PRs

Zephyr SDK Toolchain Snapshots

Testing branch

Test are in review at zephyrproject-rtos/zephyr#43729 .

The test suites can be run with e.g.

west build -p auto -b qemu_riscv64 -t run tests/lib/cpp/std/thread \
  -- \
  -DCONFIG_PICOLIBC=n \
  -DCONFIG_NEWLIB_LIBC=y

They should run via picolibc eventually as well, but IIRC, there is an error about a missing .spec file. The workaround is to copy the spec file from a separate Zephyr SDK installation.

Help Wanted

Updating Picolibc

The few changes required to newlib might also need to be applied to picolibc in order to expose similar functionality.

Additional Testsuites

It would be helpful to add test suites for the following

Additional testsuites can be added in parallel. They should not be considered blockers by any means, since they do not depend on anything specific to Zephyr.

Technically, not dependent on threading, coroutine support in Zephyr might Just Work (TM). However, coroutines have the potential to be extremely impactful in Zephyr as they are far more memory efficient than threads (important for resource-constrained devices).

Building, Testing, and Uploading Additional SDK Toolchains

"Zephyr SDK Toolchain Snapshots" lists a number of unchecked boxes and unlinked toolchains without links. To check the box and populate the link, the following steps should be taken:

  1. Build the toolchain
sh ./sdk-ng/contrib/linux_build_toolchain.sh -o /tmp/sdk-build -c $ARCH-zephyr-elf
  1. Back up the existing toolchain (one-time only)
rsync -avr /opt/zephyr/zephyr-sdk-0.16.8/$ARCH-zephyr-elf{,_orig}/
  1. Overwrite the old toolchain with the new
rsync -avr --delete {/tmp/sdk-build/output,/opt/zephyr/zephyr-sdk-0.16.8}/arc64-zephyr-elf/
  1. Ensure the following testsuites build, run, and pass
west build -p auto -b $BOARD -t run tests/lib/cpp/std/condition_variable/ -- -DCONFIG_NEWLIB_LIBC=y -DCONFIG_PICOLIBC=n
west build -p auto -b $BOARD -t run tests/lib/cpp/std/mutex/ -- -DCONFIG_NEWLIB_LIBC=y -DCONFIG_PICOLIBC=n
west build -p auto -b $BOARD -t run tests/lib/cpp/std/thread/ -- -DCONFIG_NEWLIB_LIBC=y -DCONFIG_PICOLIBC=n
  1. Compress the toolchain
cd /opt/zephyr/zephyr-sdk-0.16.8
tar cpJf ~/$ARCH-zephyr-elf.tar.xz $ARCH-zephyr-elf
  1. Upload file to Google Drive
  2. Update Link

LLVM Toolchain Integration

Currently, all of this work is being done against Zephyr's GCC-based SDK toolchains. It's possible that LLVM toolchain integration will be much easier.

Our testsuites should run successfully with both GCC-based toolchains as well as with LLVM.

Add support for ISO C++11 threads, mutexes, condition variables,
semaphores, locks, scoped locks, synchronized streams, and
futures (async programming) via ISO C11 threads.

Signed-off-by: Christopher Friedt <[email protected]>
@cfriedt cfriedt force-pushed the enable-std-thread-iso-only-v2 branch from d388e25 to f2d16be Compare September 15, 2024 13:57
@cfriedt
Copy link
Member Author

cfriedt commented Sep 15, 2024

@stephanosio - any idea what's happening with CI in sdk-ng? This PR has been blocked on failing CI for a while.

@stephanosio
Copy link
Member

@stephanosio - any idea what's happening with CI in sdk-ng? This PR has been blocked on failing CI for a while.

@cfriedt The main CI has been fixed. Please rebase to pull in the fixes.

@cfriedt
Copy link
Member Author

cfriedt commented Sep 18, 2024

Thanks @stephanosio 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants