2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2014-08-08 22:21:48 +02:00
|
|
|
* sched/pthread/pthread_condbroadcast.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +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-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +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-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2014-08-08 20:55:02 +02:00
|
|
|
#include "pthread/pthread.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: pthread_cond_broadcast
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A thread broadcast on a condition variable.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-02-18 00:21:28 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
int pthread_cond_broadcast(FAR pthread_cond_t *cond)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
int sval;
|
|
|
|
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("cond=0x%p\n", cond);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
if (!cond)
|
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-14 21:30:31 +02:00
|
|
|
/* Disable pre-emption until all of the waiting threads have been
|
|
|
|
* restarted. This is necessary to assure that the sval behaves as
|
|
|
|
* expected in the following while loop
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
/* Get the current value of the semaphore */
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
if (nxsem_get_value((FAR sem_t *)&cond->sem, &sval) != OK)
|
2012-07-14 21:30:31 +02:00
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Loop until all of the waiting threads have been restarted. */
|
|
|
|
|
|
|
|
while (sval < 0)
|
|
|
|
{
|
|
|
|
/* If the value is less than zero (meaning that one or more
|
|
|
|
* thread is waiting), then post the condition semaphore.
|
|
|
|
* Only the highest priority waiting thread will get to execute
|
|
|
|
*/
|
|
|
|
|
2017-05-29 15:05:06 +02:00
|
|
|
ret = pthread_sem_give((FAR sem_t *)&cond->sem);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Increment the semaphore count (as was done by the
|
|
|
|
* above post).
|
|
|
|
*/
|
|
|
|
|
|
|
|
sval++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we can let the restarted threads run */
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
}
|
|
|
|
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Returning %d\n", ret);
|
2007-02-18 00:21:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|