2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2014-08-08 14:21:48 -06:00
|
|
|
* sched/pthread/pthread_condbroadcast.c
|
2007-02-17 23:21:28 +00:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
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-17 23:21:28 +00:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-17 23:21:28 +00: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-17 23:21:28 +00:00
|
|
|
*
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2007-02-17 23:21:28 +00:00
|
|
|
* Included Files
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2009-12-14 18:39:29 +00:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
2007-02-17 23:21:28 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <sched.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <debug.h>
|
2009-12-14 18:39:29 +00:00
|
|
|
|
2025-01-15 13:35:23 +02:00
|
|
|
#include <nuttx/atomic.h>
|
|
|
|
|
|
2014-08-08 12:55:02 -06:00
|
|
|
#include "pthread/pthread.h"
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2007-02-17 23:21:28 +00:00
|
|
|
* Public Functions
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2012-07-14 19:30:31 +00:00
|
|
|
* Name: pthread_cond_broadcast
|
2007-02-17 23:21:28 +00:00
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* A thread broadcast on a condition variable.
|
2025-01-15 13:35:23 +02:00
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2007-02-17 23:21:28 +00:00
|
|
|
* None
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2007-02-17 23:21:28 +00:00
|
|
|
* None
|
|
|
|
|
*
|
|
|
|
|
* Assumptions:
|
|
|
|
|
*
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
int pthread_cond_broadcast(FAR pthread_cond_t *cond)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
|
|
|
|
int ret = OK;
|
|
|
|
|
|
2022-06-30 14:34:24 -03:00
|
|
|
sinfo("cond=%p\n", cond);
|
2007-02-17 23:21:28 +00:00
|
|
|
|
|
|
|
|
if (!cond)
|
|
|
|
|
{
|
|
|
|
|
ret = EINVAL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-01-15 13:35:23 +02:00
|
|
|
int wcnt = atomic_read(COND_WAIT_COUNT(cond));
|
|
|
|
|
|
2024-11-14 19:19:31 +08:00
|
|
|
/* Loop until all of the waiting threads have been restarted. */
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2025-01-15 13:35:23 +02:00
|
|
|
while (wcnt > 0)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
2025-01-15 13:35:23 +02:00
|
|
|
if (atomic_cmpxchg(COND_WAIT_COUNT(cond), &wcnt, wcnt - 1))
|
|
|
|
|
{
|
|
|
|
|
/* Post the condition semaphore to wake up a waiting thread.
|
|
|
|
|
* Only the highest priority waiting thread will get to execute
|
|
|
|
|
*/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2025-01-15 13:35:23 +02:00
|
|
|
ret = -nxsem_post(&cond->sem);
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2025-01-15 13:35:23 +02:00
|
|
|
/* Decrement the waiter count */
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2025-01-15 13:35:23 +02:00
|
|
|
wcnt--;
|
|
|
|
|
}
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-11 16:42:42 -06:00
|
|
|
sinfo("Returning %d\n", ret);
|
2007-02-17 23:21:28 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|