mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 00:14:22 +08:00
pthreads: Move pthread_barrier_init, pthread_barrier_destroy, and pthread_barrier_wait from sched/pthreads to libc/pthreads. This just coordinate other OS interface calls but are not a fundamental OS interfaces and, hence, do not belong within the OS.
This commit is contained in:
@@ -40,7 +40,6 @@ CSRCS += pthread_getschedparam.c pthread_setschedparam.c
|
||||
CSRCS += pthread_mutexinit.c pthread_mutexdestroy.c
|
||||
CSRCS += pthread_mutexlock.c pthread_mutextrylock.c pthread_mutexunlock.c
|
||||
CSRCS += pthread_condwait.c pthread_condsignal.c pthread_condbroadcast.c
|
||||
CSRCS += pthread_barrierinit.c pthread_barrierdestroy.c pthread_barrierwait.c
|
||||
CSRCS += pthread_cancel.c
|
||||
CSRCS += pthread_keycreate.c pthread_setspecific.c pthread_getspecific.c
|
||||
CSRCS += pthread_keydelete.c
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
/****************************************************************************
|
||||
* sched/pthread/pthread_barriedestroy.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pthread_barrier_destroy
|
||||
*
|
||||
* Description:
|
||||
* The pthread_barrier_destroy() function destroys the barrier referenced
|
||||
* by 'barrier' and releases any resources used by the barrier. The effect
|
||||
* of subsequent use of the barrier is undefined until the barrier is
|
||||
* reinitialized by another call to pthread_barrier_init(). The result
|
||||
* are undefined if pthread_barrier_destroy() is called when any thread is
|
||||
* blocked on the barrier, or if this function is called with an
|
||||
* uninitialized barrier.
|
||||
*
|
||||
* Parameters:
|
||||
* barrier - barrier to be destroyed.
|
||||
*
|
||||
* Return Value:
|
||||
* 0 (OK) on success or on of the following error numbers:
|
||||
*
|
||||
* EBUSY The implementation has detected an attempt to destroy a barrier
|
||||
* while it is in use.
|
||||
* EINVAL The value specified by barrier is invalid.
|
||||
*
|
||||
* Assumptions:
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int pthread_barrier_destroy(FAR pthread_barrier_t *barrier)
|
||||
{
|
||||
int ret = OK;
|
||||
|
||||
if (!barrier)
|
||||
{
|
||||
ret = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
sem_destroy(&barrier->sem);
|
||||
barrier->count = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
/****************************************************************************
|
||||
* sched/pthread/pthread_barrieinit.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pthread_barrier_init
|
||||
*
|
||||
* Description:
|
||||
* The pthread_barrier_init() function allocates any resources required to
|
||||
* use the barrier referenced by 'barrier' and initialized the barrier
|
||||
* with the attributes referenced by attr. If attr is NULL, the default
|
||||
* barrier attributes will be used. The results are undefined if
|
||||
* pthread_barrier_init() is called when any thread is blocked on the
|
||||
* barrier. The results are undefined if a barrier is used without first
|
||||
* being initialized. The results are undefined if pthread_barrier_init()
|
||||
* is called specifying an already initialized barrier.
|
||||
*
|
||||
* Parameters:
|
||||
* barrier - the barrier to be initialized
|
||||
* attr - barrier attributes to be used in the initialization.
|
||||
* count - the count to be associated with the barrier. The count
|
||||
* argument specifies the number of threads that must call
|
||||
* pthread_barrier_wait() before any of them successfully return from
|
||||
* the call. The value specified by count must be greater than zero.
|
||||
*
|
||||
* Return Value:
|
||||
* 0 (OK) on success or on of the following error numbers:
|
||||
*
|
||||
* EAGAIN The system lacks the necessary resources to initialize another
|
||||
* barrier. EINVAL The barrier reference is invalid, or the values
|
||||
* specified by attr are invalid, or the value specified by count
|
||||
* is equal to zero.
|
||||
* ENOMEM Insufficient memory exists to initialize the barrier.
|
||||
* EBUSY The implementation has detected an attempt to reinitialize a
|
||||
* barrier while it is in use.
|
||||
*
|
||||
* Assumptions:
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int pthread_barrier_init(FAR pthread_barrier_t *barrier,
|
||||
FAR const pthread_barrierattr_t *attr, unsigned int count)
|
||||
{
|
||||
int ret = OK;
|
||||
|
||||
if (!barrier || count == 0)
|
||||
{
|
||||
ret = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
sem_init(&barrier->sem, 0, 0);
|
||||
barrier->count = count;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
/****************************************************************************
|
||||
* sched/pthread/pthread_barrierwait.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2014 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <sched.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pthread_barrier_wait
|
||||
*
|
||||
* Description:
|
||||
* The pthread_barrier_wait() function synchronizse participating threads
|
||||
* at the barrier referenced by 'barrier'. The calling thread is blocked
|
||||
* until the required number of threads have called pthread_barrier_wait()
|
||||
* specifying the same 'barrier'. When the required number of threads
|
||||
* have called pthread_barrier_wait() specifying the 'barrier', the
|
||||
* constant PTHREAD_BARRIER_SERIAL_THREAD will be returned to one
|
||||
* unspecified thread and zero will be returned to each of the remaining
|
||||
* threads. At this point, the barrier will be reset to the state it had
|
||||
* as a result of the most recent pthread_barrier_init() function that
|
||||
* referenced it.
|
||||
*
|
||||
* The constant PTHREAD_BARRIER_SERIAL_THREAD is defined in pthread.h and
|
||||
* its value must be distinct from any other value returned by
|
||||
* pthread_barrier_wait().
|
||||
*
|
||||
* The results are undefined if this function is called with an
|
||||
* uninitialized barrier.
|
||||
*
|
||||
* If a signal is delivered to a thread blocked on a barrier, upon return
|
||||
* from the signal handler the thread will resume waiting at the barrier
|
||||
* if the barrier wait has not completed; otherwise, the thread will
|
||||
* continue as normal from the completed barrier wait. Until the thread in
|
||||
* the signal handler returns from it, it is unspecified whether other
|
||||
* threads may proceed past the barrier once they have all reached it.
|
||||
*
|
||||
* A thread that has blocked on a barrier will not prevent any unblocked
|
||||
* thread that is eligible to use the same processing resources from
|
||||
* eventually making forward progress in its execution. Eligibility for
|
||||
* processing resources will be determined by the scheduling policy.
|
||||
*
|
||||
* Parameters:
|
||||
* barrier - the barrier to wait on
|
||||
*
|
||||
* Return Value:
|
||||
* 0 (OK) on success or EINVAL if the barrier is not valid.
|
||||
*
|
||||
* Assumptions:
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int pthread_barrier_wait(FAR pthread_barrier_t *barrier)
|
||||
{
|
||||
int semcount;
|
||||
int ret = OK;
|
||||
|
||||
if (!barrier)
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Disable pre-emption throughout the following */
|
||||
|
||||
sched_lock();
|
||||
|
||||
/* Find out how many threads are already waiting at the barrier */
|
||||
|
||||
ret = sem_getvalue(&barrier->sem, &semcount);
|
||||
if (ret != OK)
|
||||
{
|
||||
sched_unlock();
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* If the number of waiters would be equal to the count, then we are done */
|
||||
|
||||
if ((1 - semcount) >= (int)barrier->count)
|
||||
{
|
||||
/* Free all of the waiting threads */
|
||||
|
||||
while (semcount < 0)
|
||||
{
|
||||
(void)sem_post(&barrier->sem);
|
||||
(void)sem_getvalue(&barrier->sem, &semcount);
|
||||
}
|
||||
|
||||
/* Then return PTHREAD_BARRIER_SERIAL_THREAD to the final thread */
|
||||
|
||||
sched_unlock();
|
||||
return PTHREAD_BARRIER_SERIAL_THREAD;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Otherwise, this thread must wait as well */
|
||||
|
||||
while (sem_wait(&barrier->sem) != OK)
|
||||
{
|
||||
/* If the thread is awakened by a signal, just continue to wait */
|
||||
|
||||
int errornumber = get_errno();
|
||||
if (errornumber != EINTR)
|
||||
{
|
||||
/* If it is awakened by some other error, then there is a
|
||||
* problem
|
||||
*/
|
||||
|
||||
sched_unlock();
|
||||
return errornumber;
|
||||
}
|
||||
}
|
||||
|
||||
/* We will only get here when we are one of the N-1 threads that were
|
||||
* waiting for the final thread at the barrier. We just need to return
|
||||
* zero.
|
||||
*/
|
||||
|
||||
sched_unlock();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user