2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2023-04-23 08:45:14 +02:00
|
|
|
* sched/pthread/pthread_barrierwait.c
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-04-11 01:45:05 +02:00
|
|
|
#include <nuttx/irq.h>
|
2023-10-24 16:16:00 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2007-03-24 00:22:22 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.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_barrier_wait
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-08-07 16:25:30 +02:00
|
|
|
* 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.
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* 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().
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* The results are undefined if this function is called with an
|
|
|
|
* uninitialized barrier.
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* 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.
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* 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.
|
2007-03-24 00:22:22 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-03-24 00:22:22 +01:00
|
|
|
* barrier - the barrier to wait on
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-03-24 00:22:22 +01:00
|
|
|
* 0 (OK) on success or EINVAL if the barrier is not valid.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-24 00:22:22 +01:00
|
|
|
|
|
|
|
int pthread_barrier_wait(FAR pthread_barrier_t *barrier)
|
|
|
|
{
|
2023-04-11 01:45:05 +02:00
|
|
|
irqstate_t flags;
|
2023-10-24 16:16:00 +02:00
|
|
|
int semcount;
|
|
|
|
int ret;
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2023-10-24 16:16:00 +02:00
|
|
|
if (barrier == NULL)
|
2007-03-24 00:22:22 +01:00
|
|
|
{
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable pre-emption throughout the following */
|
|
|
|
|
2023-04-11 01:45:05 +02:00
|
|
|
flags = enter_critical_section();
|
2007-03-24 00:22:22 +01:00
|
|
|
|
|
|
|
/* Find out how many threads are already waiting at the barrier */
|
|
|
|
|
2023-10-24 16:16:00 +02:00
|
|
|
ret = nxsem_get_value(&barrier->sem, &semcount);
|
2007-03-24 00:22:22 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2023-04-11 01:45:05 +02:00
|
|
|
leave_critical_section(flags);
|
2023-10-24 16:16:00 +02:00
|
|
|
return -ret;
|
2007-03-24 00:22:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the number of waiters would be equal to the count, then we are done */
|
|
|
|
|
2014-01-14 20:30:22 +01:00
|
|
|
if ((1 - semcount) >= (int)barrier->count)
|
2007-03-24 00:22:22 +01:00
|
|
|
{
|
|
|
|
/* Free all of the waiting threads */
|
|
|
|
|
|
|
|
while (semcount < 0)
|
|
|
|
{
|
2023-10-24 16:16:00 +02:00
|
|
|
nxsem_post(&barrier->sem);
|
|
|
|
nxsem_get_value(&barrier->sem, &semcount);
|
2007-03-24 00:22:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then return PTHREAD_BARRIER_SERIAL_THREAD to the final thread */
|
|
|
|
|
2023-04-11 01:45:05 +02:00
|
|
|
leave_critical_section(flags);
|
2007-03-24 00:22:22 +01:00
|
|
|
return PTHREAD_BARRIER_SERIAL_THREAD;
|
|
|
|
}
|
2023-10-24 16:16:00 +02:00
|
|
|
|
|
|
|
/* Otherwise, this thread must wait as well */
|
|
|
|
|
|
|
|
while ((ret = nxsem_wait(&barrier->sem)) != OK)
|
2007-03-24 00:22:22 +01:00
|
|
|
{
|
2023-10-24 16:16:00 +02:00
|
|
|
/* If the thread is awakened by a signal, just continue to wait */
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2023-10-24 16:16:00 +02:00
|
|
|
if (ret != -EINTR)
|
2007-03-24 00:22:22 +01:00
|
|
|
{
|
2023-10-24 16:16:00 +02:00
|
|
|
/* If it is awakened by some other error, then there is a
|
|
|
|
* problem
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
2007-03-24 00:22:22 +01:00
|
|
|
}
|
2023-10-24 16:16:00 +02:00
|
|
|
}
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2023-10-24 16:16:00 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2007-03-24 00:22:22 +01:00
|
|
|
|
2023-10-24 16:16:00 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
return -ret;
|
2007-03-24 00:22:22 +01:00
|
|
|
}
|