2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2014-08-08 21:53:29 +02:00
|
|
|
* sched/semaphore/sem_trywait.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-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <stdbool.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sched.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
2016-02-14 15:17:46 +01:00
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/arch.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 21:53:29 +02:00
|
|
|
#include "semaphore/semaphore.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2017-10-05 15:59:06 +02:00
|
|
|
* Name: nxsem_trywait
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This function locks the specified semaphore only if the semaphore is
|
2020-03-16 20:42:34 +01:00
|
|
|
* currently not locked. In either case, the call returns without
|
|
|
|
* blocking.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* sem - the semaphore descriptor
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-10-05 15:59:06 +02:00
|
|
|
* This is an internal OS interface and should not be used by applications.
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
|
|
|
* Possible returned errors:
|
2012-07-14 21:30:31 +02:00
|
|
|
*
|
2017-10-05 15:59:06 +02:00
|
|
|
* EINVAL - Invalid attempt to get the semaphore
|
|
|
|
* EAGAIN - The semaphore is not available.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-05 15:59:06 +02:00
|
|
|
int nxsem_trywait(FAR sem_t *sem)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-02-07 00:44:41 +01:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
2016-02-14 15:17:46 +01:00
|
|
|
irqstate_t flags;
|
2017-10-05 15:59:06 +02:00
|
|
|
int ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-02-22 09:33:14 +01:00
|
|
|
/* This API should not be called from interrupt handlers */
|
2007-03-10 01:17:29 +01:00
|
|
|
|
2016-11-09 14:01:49 +01:00
|
|
|
DEBUGASSERT(sem != NULL && up_interrupt_context() == false);
|
2007-03-10 01:17:29 +01:00
|
|
|
|
2016-11-09 14:01:49 +01:00
|
|
|
if (sem != NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2012-07-14 21:30:31 +02:00
|
|
|
/* The following operations must be performed with interrupts disabled
|
|
|
|
* because sem_post() may be called from an interrupt handler.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* If the semaphore is available, give it to the requesting task */
|
|
|
|
|
|
|
|
if (sem->semcount > 0)
|
|
|
|
{
|
|
|
|
/* It is, let the task take the semaphore */
|
|
|
|
|
|
|
|
sem->semcount--;
|
2021-12-17 04:21:04 +01:00
|
|
|
nxsem_add_holder(sem);
|
2007-02-18 00:21:28 +01:00
|
|
|
rtcb->waitsem = NULL;
|
|
|
|
ret = OK;
|
|
|
|
}
|
2016-11-09 14:01:49 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Semaphore is not available */
|
|
|
|
|
2017-10-05 15:59:06 +02:00
|
|
|
ret = -EAGAIN;
|
2016-11-09 14:01:49 +01:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Interrupts may now be enabled. */
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2016-11-09 14:01:49 +01:00
|
|
|
else
|
|
|
|
{
|
2017-10-05 15:59:06 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sem_trywait
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function locks the specified semaphore only if the semaphore is
|
2020-03-16 20:42:34 +01:00
|
|
|
* currently not locked. In either case, the call returns without
|
|
|
|
* blocking.
|
2017-10-05 15:59:06 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-10-05 15:59:06 +02:00
|
|
|
* sem - the semaphore descriptor
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-10-05 15:59:06 +02:00
|
|
|
* Zero (OK) on success or -1 (ERROR) if unsuccessful. If this function
|
|
|
|
* returns -1(ERROR), then the cause of the failure will be reported in
|
|
|
|
* errno variable as:
|
|
|
|
*
|
|
|
|
* EINVAL - Invalid attempt to get the semaphore
|
|
|
|
* EAGAIN - The semaphore is not available.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int sem_trywait(FAR sem_t *sem)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Let nxsem_trywait do the real work */
|
|
|
|
|
|
|
|
ret = nxsem_trywait(sem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
2016-11-09 14:01:49 +01:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|