2008-01-31 05:59:12 +08:00
|
|
|
/****************************************************************************
|
2014-08-09 04:21:48 +08:00
|
|
|
* sched/pthread/pthread_condsignal.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-05-17 22:47:40 +08: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 07:21:28 +08:00
|
|
|
*
|
2020-05-17 22:47:40 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2020-05-17 22:47:40 +08: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 07:21:28 +08:00
|
|
|
*
|
2008-01-31 05:59:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-01-31 05:59:12 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2008-01-31 05:59:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2009-12-15 02:39:29 +08:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2009-12-15 02:39:29 +08:00
|
|
|
|
2014-08-09 02:55:02 +08:00
|
|
|
#include "pthread/pthread.h"
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-01-31 05:59:12 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Public Functions
|
2008-01-31 05:59:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-01-31 05:59:12 +08:00
|
|
|
/****************************************************************************
|
2012-07-15 03:30:31 +08:00
|
|
|
* Name: pthread_cond_signal
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A thread can signal on a condition variable.
|
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2007-02-18 07:21:28 +08:00
|
|
|
* None
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2007-02-18 07:21:28 +08:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2008-01-31 05:59:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-01-31 05:59:12 +08:00
|
|
|
int pthread_cond_signal(FAR pthread_cond_t *cond)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
int sval;
|
|
|
|
|
2022-07-01 01:34:24 +08:00
|
|
|
sinfo("cond=%p\n", cond);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
if (!cond)
|
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get the current value of the semaphore */
|
|
|
|
|
2022-10-29 20:54:04 +08:00
|
|
|
if (nxsem_get_value(&cond->sem, &sval) != OK)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-05-17 22:47:40 +08:00
|
|
|
/* One of my objectives in this design was to make
|
|
|
|
* pthread_cond_signal() usable from interrupt handlers. However,
|
|
|
|
* from interrupt handlers, you cannot take the associated mutex
|
|
|
|
* before signaling the condition. As a result, I think that
|
|
|
|
* there could be a race condition with the following logic which
|
|
|
|
* assumes that the if sval < 0 then the thread is waiting.
|
|
|
|
* Without the mutex, there is no atomic, protected operation that
|
|
|
|
* will guarantee this to be so.
|
2012-03-20 01:56:15 +08:00
|
|
|
*/
|
|
|
|
|
2016-06-12 06:42:42 +08:00
|
|
|
sinfo("sval=%d\n", sval);
|
2007-02-18 07:21:28 +08:00
|
|
|
if (sval < 0)
|
|
|
|
{
|
2016-06-12 06:42:42 +08:00
|
|
|
sinfo("Signalling...\n");
|
2024-07-05 21:22:18 +08:00
|
|
|
ret = -nxsem_post(&cond->sem);
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 06:42:42 +08:00
|
|
|
sinfo("Returning %d\n", ret);
|
2007-02-18 07:21:28 +08:00
|
|
|
return ret;
|
|
|
|
}
|