2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/pthread/pthread_once.c
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-24 00:22:22 +01:00
|
|
|
* Included Files
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-24 00:22:22 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2021-03-30 19:04:27 +02:00
|
|
|
#include <assert.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <stdbool.h>
|
2007-03-24 00:22:22 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-24 00:22:22 +01:00
|
|
|
* Public Functions
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: pthread_once
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2021-03-30 19:04:27 +02:00
|
|
|
* The first call to pthread_once() by any thread with a given
|
2016-08-07 16:25:30 +02:00
|
|
|
* once_control, will call the init_routine with no arguments. Subsequent
|
|
|
|
* calls to pthread_once() with the same once_control will have no effect.
|
|
|
|
* On return from pthread_once(), init_routine will have completed.
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-08-07 16:25:30 +02:00
|
|
|
* once_control - Determines if init_routine should be called.
|
2021-03-30 19:04:27 +02:00
|
|
|
* once_control should be declared and initialized as follows:
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
|
|
|
* pthread_once_t once_control = PTHREAD_ONCE_INIT;
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* PTHREAD_ONCE_INIT is defined in pthread.h
|
2007-03-24 00:22:22 +01:00
|
|
|
* init_routine - The initialization routine that will be called once.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2021-03-30 19:04:27 +02:00
|
|
|
* 0 (OK) on success or an error number shall be returned to
|
|
|
|
* indicate the error.
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
int pthread_once(FAR pthread_once_t *once_control,
|
|
|
|
CODE void (*init_routine)(void))
|
2007-03-24 00:22:22 +01:00
|
|
|
{
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2021-03-30 19:04:27 +02:00
|
|
|
DEBUGASSERT(once_control != NULL);
|
|
|
|
DEBUGASSERT(init_routine != NULL);
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2021-03-30 19:04:27 +02:00
|
|
|
/* Prohibit pre-emption while we test and set the once_control. */
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2021-03-30 19:04:27 +02:00
|
|
|
sched_lock();
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2021-03-30 19:04:27 +02:00
|
|
|
if (!*once_control)
|
|
|
|
{
|
|
|
|
*once_control = true;
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2021-03-30 19:04:27 +02:00
|
|
|
/* Call the init_routine with pre-emption enabled. */
|
2007-03-24 00:22:22 +01:00
|
|
|
|
|
|
|
sched_unlock();
|
2021-03-30 19:04:27 +02:00
|
|
|
init_routine();
|
2007-03-24 00:22:22 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:04:27 +02:00
|
|
|
/* The init_routine has already been called.
|
|
|
|
* Restore pre-emption and return.
|
|
|
|
*/
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2021-03-30 19:04:27 +02:00
|
|
|
sched_unlock();
|
|
|
|
return OK;
|
2007-03-24 00:22:22 +01:00
|
|
|
}
|