forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. Fixes zephyrproject-rtos#25569 Signed-off-by: Christopher Friedt <[email protected]>
- Loading branch information
Showing
4 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
add_subdirectory(abi) | ||
|
||
add_subdirectory(std) | ||
add_subdirectory_ifdef(CONFIG_MINIMAL_LIBCPP minimal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
zephyr_sources_ifdef(CONFIG_CPP_STD_THREAD cpp_std_thread.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2020 Friedt Professional Engineering Services, Inc | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/kernel.h> | ||
|
||
#include <system_error> | ||
#include <thread> | ||
|
||
namespace std | ||
{ | ||
void thread::detach() | ||
{ | ||
int err; | ||
thread::id tid; | ||
::pthread_t pth; | ||
|
||
tid = this->get_id(); | ||
pth = (::pthread_t)tid._M_thread; | ||
|
||
err = ::pthread_detach(pth); | ||
if (err) { | ||
__throw_system_error(err); | ||
} | ||
} | ||
|
||
void thread::join() | ||
{ | ||
int err; | ||
thread::id tid; | ||
::pthread_t pth; | ||
void *unused; | ||
|
||
tid = this->get_id(); | ||
pth = (::pthread_t)tid._M_thread; | ||
|
||
err = ::pthread_join(pth, &unused); | ||
if (err) { | ||
__throw_system_error(err); | ||
} | ||
} | ||
|
||
thread::_State::~_State() | ||
{ | ||
} | ||
|
||
unsigned int thread::hardware_concurrency() noexcept | ||
{ | ||
return CONFIG_MP_NUM_CPUS; | ||
} | ||
|
||
extern "C" { | ||
static void *execute_native_thread_routine(void *__p) | ||
{ | ||
thread::_State_ptr __t{static_cast<thread::_State *>(__p)}; | ||
__t->_M_run(); | ||
return nullptr; | ||
} | ||
} | ||
|
||
void thread::_M_start_thread(_State_ptr state, void (*)()) | ||
{ | ||
const int err = | ||
__gthread_create(&_M_id._M_thread, &execute_native_thread_routine, state.get()); | ||
|
||
if (err) { | ||
__throw_system_error(err); | ||
} | ||
state.release(); | ||
} | ||
|
||
} /* namespace std */ |