Skip to content

Commit

Permalink
Merge pull request #17734 from gschorcht/sys/posix/pthread_newlib_com…
Browse files Browse the repository at this point in the history
…patibility

sys/posix/pthread: newlib compatibility
  • Loading branch information
fjmolinas committed Mar 3, 2022
2 parents 2234bf4 + c09d9d8 commit 644f32f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions sys/posix/pthread/include/pthread_once.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 644f32f

Please sign in to comment.