1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/posix/pthread: newlib compatibility

When using a toolchain with built-in POSIX thread support, static C++ constructors use a static mutex variable which is initialized with `pthread_once` when first used. However, since RIOT's `pthread_once_t` type is different from that in newlib's `pthread`, which is assumed by GCC, RIOT crashes as soon as static constructors are used.
Changing the `pthread_once_t` type to be compatible with newlib's `pthread_once_t` type solves the problem and allows the RIOT `pthread` modules to be used even with toolchains with built-in POSIX thread support.
This commit is contained in:
Gunar Schorcht 2022-03-02 11:57:39 +01:00
parent 52116e1070
commit c09d9d87b7
2 changed files with 11 additions and 5 deletions

View File

@ -23,15 +23,21 @@ extern "C" {
/**
* @brief Datatype to supply to pthread_once().
* @details This data type must be compatible with the one defined
* in newlib's `include/sys/_pthreadtypes.h`.
*/
typedef volatile int pthread_once_t;
typedef struct {
int is_initialized; /**< initialized */
int init_executed; /**< init function executed */
} pthread_once_t;
/**
* @def PTHREAD_ONCE_INIT
* @brief Initialization for pthread_once_t.
* @details A zeroed out pthread_once_t is initialized.
* @details pthread_once_t variables are declared as initialized, but
* the init function is not yet executed.
*/
#define PTHREAD_ONCE_INIT 0
#define PTHREAD_ONCE_INIT { 1, 0 }
/**
* @brief Helper function that ensures that `init_routine` is called at once.

View File

@ -22,11 +22,11 @@
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
{
if (*once_control == PTHREAD_ONCE_INIT) {
if (!once_control->init_executed) {
init_routine();
}
*once_control = PTHREAD_ONCE_INIT + 1;
once_control->init_executed = 1;
return 0;
}