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:
parent
bea6e0ddd7
commit
0109bcad8c
@ -24,10 +24,10 @@
|
|||||||
|
|
||||||
#include <nuttx/config.h>
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -38,14 +38,14 @@
|
|||||||
* Name: pthread_once
|
* Name: pthread_once
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* The first call to pthread_once() by any thread with a given
|
* The first call to pthread_once() by any thread with a given
|
||||||
* once_control, will call the init_routine with no arguments. Subsequent
|
* once_control, will call the init_routine with no arguments. Subsequent
|
||||||
* calls to pthread_once() with the same once_control will have no effect.
|
* calls to pthread_once() with the same once_control will have no effect.
|
||||||
* On return from pthread_once(), init_routine will have completed.
|
* On return from pthread_once(), init_routine will have completed.
|
||||||
*
|
*
|
||||||
* Input Parameters:
|
* Input Parameters:
|
||||||
* once_control - Determines if init_routine should be called.
|
* 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;
|
* pthread_once_t once_control = PTHREAD_ONCE_INIT;
|
||||||
*
|
*
|
||||||
@ -53,8 +53,8 @@
|
|||||||
* init_routine - The initialization routine that will be called once.
|
* init_routine - The initialization routine that will be called once.
|
||||||
*
|
*
|
||||||
* Returned Value:
|
* Returned Value:
|
||||||
* 0 (OK) on success or EINVAL if either once_control or init_routine are
|
* 0 (OK) on success or an error number shall be returned to
|
||||||
* invalid
|
* indicate the error.
|
||||||
*
|
*
|
||||||
* Assumptions:
|
* Assumptions:
|
||||||
*
|
*
|
||||||
@ -65,31 +65,28 @@ int pthread_once(FAR pthread_once_t *once_control,
|
|||||||
{
|
{
|
||||||
/* Sanity checks */
|
/* Sanity checks */
|
||||||
|
|
||||||
if (once_control && init_routine)
|
DEBUGASSERT(once_control != NULL);
|
||||||
|
DEBUGASSERT(init_routine != NULL);
|
||||||
|
|
||||||
|
/* Prohibit pre-emption while we test and set the once_control. */
|
||||||
|
|
||||||
|
sched_lock();
|
||||||
|
|
||||||
|
if (!*once_control)
|
||||||
{
|
{
|
||||||
/* Prohibit pre-emption while we test and set the once_control */
|
*once_control = true;
|
||||||
|
|
||||||
sched_lock();
|
/* Call the init_routine with pre-emption enabled. */
|
||||||
if (!*once_control)
|
|
||||||
{
|
|
||||||
*once_control = true;
|
|
||||||
|
|
||||||
/* Call the init_routine with pre-emption enabled. */
|
|
||||||
|
|
||||||
sched_unlock();
|
|
||||||
init_routine();
|
|
||||||
return OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The init_routine has already been called.
|
|
||||||
* Restore pre-emption and return
|
|
||||||
*/
|
|
||||||
|
|
||||||
sched_unlock();
|
sched_unlock();
|
||||||
|
init_routine();
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* One of the two arguments is NULL */
|
/* The init_routine has already been called.
|
||||||
|
* Restore pre-emption and return.
|
||||||
|
*/
|
||||||
|
|
||||||
return EINVAL;
|
sched_unlock();
|
||||||
|
return OK;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user