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
|
|
|
*
|
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-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
|
|
|
|
2022-02-21 11:14:10 +01:00
|
|
|
#include <nuttx/init.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
|
|
|
|
2023-08-10 20:47:04 +02:00
|
|
|
/* This API should not be called from the idleloop */
|
2007-03-10 01:17:29 +01:00
|
|
|
|
2023-08-10 20:47:04 +02:00
|
|
|
DEBUGASSERT(sem != NULL);
|
|
|
|
DEBUGASSERT(!OSINIT_IDLELOOP() || !sched_idletask() ||
|
|
|
|
up_interrupt_context());
|
2007-03-10 01:17:29 +01:00
|
|
|
|
2022-10-20 18:00:08 +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
|
|
|
|
2022-10-20 18:00:08 +02:00
|
|
|
flags = enter_critical_section();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-10-20 18:00:08 +02:00
|
|
|
/* If the semaphore is available, give it to the requesting task */
|
2016-11-09 14:01:49 +01:00
|
|
|
|
2022-10-20 18:00:08 +02:00
|
|
|
if (sem->semcount > 0)
|
|
|
|
{
|
|
|
|
/* It is, let the task take the semaphore */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-08-22 04:24:30 +02:00
|
|
|
ret = nxsem_protect_wait(sem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
leave_critical_section(flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-20 18:00:08 +02:00
|
|
|
sem->semcount--;
|
|
|
|
nxsem_add_holder(sem);
|
|
|
|
rtcb->waitobj = NULL;
|
|
|
|
ret = OK;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2016-11-09 14:01:49 +01:00
|
|
|
else
|
|
|
|
{
|
2022-10-20 18:00:08 +02:00
|
|
|
/* Semaphore is not available */
|
|
|
|
|
|
|
|
ret = -EAGAIN;
|
2017-10-05 15:59:06 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 18:00:08 +02:00
|
|
|
/* Interrupts may now be enabled. */
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
2017-10-05 15:59:06 +02:00
|
|
|
return ret;
|
|
|
|
}
|