Skip to content

Commit

Permalink
sys/posix/pthread: newlib compatibility
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gschorcht committed Mar 2, 2022
1 parent 52116e1 commit bf6a823
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions sys/posix/pthread/include/pthread_once.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ extern "C" {
/**
* @brief Datatype to supply to pthread_once().
*/
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.
*/
#define PTHREAD_ONCE_INIT 0
#define PTHREAD_ONCE_INIT { 1, 0 }

/**
* @brief Helper function that ensures that `init_routine` is called at once.
Expand Down
4 changes: 2 additions & 2 deletions sys/posix/pthread/pthread_once.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit bf6a823

Please sign in to comment.