libs/libc: Update pthread_once according to the specification

OpenGroup specification was updated regarding the return value for
pthread_once, which after Issue 7 states that "the [EINVAL] error for an
uninitialized pthread_once_t object is removed; this condition results
in undefined behavior".
This commit is contained in:
Gustavo Henrique Nihei 2021-03-30 14:04:27 -03:00 committed by Xiang Xiao
parent bea6e0ddd7
commit 0109bcad8c

View File

@ -24,10 +24,10 @@
#include <nuttx/config.h>
#include <assert.h>
#include <stdbool.h>
#include <pthread.h>
#include <sched.h>
#include <errno.h>
#include <debug.h>
/****************************************************************************
@ -45,7 +45,7 @@
*
* Input Parameters:
* once_control - Determines if init_routine should be called.
* once_control should be declared and initializeed as follows:
* once_control should be declared and initialized as follows:
*
* pthread_once_t once_control = PTHREAD_ONCE_INIT;
*
@ -53,8 +53,8 @@
* init_routine - The initialization routine that will be called once.
*
* Returned Value:
* 0 (OK) on success or EINVAL if either once_control or init_routine are
* invalid
* 0 (OK) on success or an error number shall be returned to
* indicate the error.
*
* Assumptions:
*
@ -65,11 +65,13 @@ int pthread_once(FAR pthread_once_t *once_control,
{
/* Sanity checks */
if (once_control && init_routine)
{
/* Prohibit pre-emption while we test and set the once_control */
DEBUGASSERT(once_control != NULL);
DEBUGASSERT(init_routine != NULL);
/* Prohibit pre-emption while we test and set the once_control. */
sched_lock();
if (!*once_control)
{
*once_control = true;
@ -82,14 +84,9 @@ int pthread_once(FAR pthread_once_t *once_control,
}
/* The init_routine has already been called.
* Restore pre-emption and return
* Restore pre-emption and return.
*/
sched_unlock();
return OK;
}
/* One of the two arguments is NULL */
return EINVAL;
}