Skip to content

Commit 74cc8a5

Browse files
committed
cpp: thread: support for std::thread and std::this_thread
This commit adds support for std::thread and std::this_thread when the configured C++ standard is >= C++11. The implementation uses POSIX threads under the hood. Signed-off-by: Christopher Friedt <[email protected]>
1 parent a747d1a commit 74cc8a5

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

lib/cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
add_subdirectory(abi)
4-
4+
add_subdirectory(std)
55
add_subdirectory_ifdef(CONFIG_MINIMAL_LIBCPP minimal)

lib/cpp/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ config CPP_RTTI
143143

144144
endif # !MINIMAL_LIBCPP
145145

146+
config CPP_STD_THREAD
147+
bool "Support for std::thread"
148+
default y if !STD_CPP98
149+
depends on !STD_CPP98
150+
select POSIX_API
151+
help
152+
This option enables support for std::thread and std::this_thread.
153+
146154
config CPP_STATIC_INIT_GNU
147155
bool
148156
select STATIC_INIT_GNU

lib/cpp/std/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
zephyr_sources_ifdef(CONFIG_CPP_STD_THREAD
2+
thread.cpp
3+
)

lib/cpp/std/thread.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2020 Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define _GLIBCXX_VISIBILITY(x)
8+
#define _GLIBCXX_THREAD_ABI_COMPAT 1
9+
10+
#include <threads.h>
11+
12+
#include <thread>
13+
14+
#include <zephyr/init.h>
15+
#include <zephyr/kernel.h>
16+
17+
static void __z_cpp_thread_terminate() FUNC_NORETURN;
18+
static void __z_cpp_thread_terminate()
19+
{
20+
thrd_exit(0);
21+
}
22+
23+
static int __z_cpp_thread_init(void)
24+
{
25+
/* use thrd_exit(0) instead of std::abort() to avoid kernel panic */
26+
std::set_terminate(__z_cpp_thread_terminate);
27+
return 0;
28+
}
29+
SYS_INIT(__z_cpp_thread_init, PRE_KERNEL_1, 0);

0 commit comments

Comments
 (0)