semaphore: move param check to sem_xx level

for the speed improve

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd 2022-10-21 00:00:08 +08:00 committed by Xiang Xiao
parent a4b378a583
commit a9c647d418
6 changed files with 255 additions and 246 deletions

View File

@ -25,6 +25,7 @@
#include <nuttx/config.h> #include <nuttx/config.h>
#include <sys/types.h> #include <sys/types.h>
#include <assert.h>
#include <limits.h> #include <limits.h>
#include <errno.h> #include <errno.h>
@ -63,12 +64,8 @@ int nxsem_init(FAR sem_t *sem, int pshared, unsigned int value)
{ {
UNUSED(pshared); UNUSED(pshared);
/* Verify that a semaphore was provided and the count is within the valid DEBUGASSERT(sem != NULL && value <= SEM_VALUE_MAX);
* range.
*/
if (sem != NULL && value <= SEM_VALUE_MAX)
{
/* Initialize the semaphore count */ /* Initialize the semaphore count */
sem->semcount = (int16_t)value; sem->semcount = (int16_t)value;
@ -91,9 +88,6 @@ int nxsem_init(FAR sem_t *sem, int pshared, unsigned int value)
return OK; return OK;
} }
return -EINVAL;
}
/**************************************************************************** /****************************************************************************
* Name: sem_init * Name: sem_init
* *
@ -122,6 +116,16 @@ int sem_init(FAR sem_t *sem, int pshared, unsigned int value)
{ {
int ret; int ret;
/* Verify that a semaphore was provided and the count is within the valid
* range.
*/
if (sem == NULL || value > SEM_VALUE_MAX)
{
set_errno(EINVAL);
return ERROR;
}
ret = nxsem_init(sem, pshared, value); ret = nxsem_init(sem, pshared, value);
if (ret < 0) if (ret < 0)
{ {

View File

@ -97,19 +97,9 @@ int nxsem_clockwait(FAR sem_t *sem, clockid_t clockid,
int status; int status;
int ret = ERROR; int ret = ERROR;
DEBUGASSERT(sem != NULL && abstime != NULL);
DEBUGASSERT(up_interrupt_context() == false); DEBUGASSERT(up_interrupt_context() == false);
/* Verify the input parameters and, in case of an error, set
* errno appropriately.
*/
#ifdef CONFIG_DEBUG_FEATURES
if (!abstime || !sem)
{
return -EINVAL;
}
#endif
/* We will disable interrupts until we have completed the semaphore /* We will disable interrupts until we have completed the semaphore
* wait. We need to do this (as opposed to just disabling pre-emption) * wait. We need to do this (as opposed to just disabling pre-emption)
* because there could be interrupt handlers that are asynchronously * because there could be interrupt handlers that are asynchronously
@ -134,11 +124,13 @@ int nxsem_clockwait(FAR sem_t *sem, clockid_t clockid,
* with a valid timeout. * with a valid timeout.
*/ */
#ifdef CONFIG_DEBUG_FEATURES
if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
{ {
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
#endif
/* Convert the timespec to clock ticks. We must have interrupts /* Convert the timespec to clock ticks. We must have interrupts
* disabled here so that this time stays valid until the wait begins. * disabled here so that this time stays valid until the wait begins.
@ -268,6 +260,16 @@ int sem_clockwait(FAR sem_t *sem, clockid_t clockid,
{ {
int ret; int ret;
/* Verify the input parameters and, in case of an error, set
* errno appropriately.
*/
if (sem == NULL || abstime == NULL)
{
set_errno(EINVAL);
return ERROR;
}
/* sem_timedwait() is a cancellation point */ /* sem_timedwait() is a cancellation point */
enter_cancellation_point(); enter_cancellation_point();

View File

@ -58,13 +58,11 @@
int nxsem_destroy(FAR sem_t *sem) int nxsem_destroy(FAR sem_t *sem)
{ {
/* Assure a valid semaphore is specified */ DEBUGASSERT(sem != NULL);
if (sem != NULL)
{
/* There is really no particular action that we need /* There is really no particular action that we need
* take to destroy a semaphore. We will just reset * take to destroy a semaphore. We will just reset
* the count to some reasonable value (1) and release * the count to some reasonable value (0) and release
* ownership. * ownership.
* *
* Check if other threads are waiting on the semaphore. * Check if other threads are waiting on the semaphore.
@ -83,9 +81,6 @@ int nxsem_destroy (FAR sem_t *sem)
return OK; return OK;
} }
return -EINVAL;
}
/**************************************************************************** /****************************************************************************
* Name: sem_destroy * Name: sem_destroy
* *
@ -114,6 +109,14 @@ int sem_destroy (FAR sem_t *sem)
{ {
int ret; int ret;
/* Assure a valid semaphore is specified */
if (sem == NULL)
{
set_errno(EINVAL);
return ERROR;
}
ret = nxsem_destroy(sem); ret = nxsem_destroy(sem);
if (ret < 0) if (ret < 0)
{ {

View File

@ -72,12 +72,9 @@ int nxsem_post(FAR sem_t *sem)
FAR struct tcb_s *stcb = NULL; FAR struct tcb_s *stcb = NULL;
irqstate_t flags; irqstate_t flags;
int16_t sem_count; int16_t sem_count;
int ret = -EINVAL;
/* Make sure we were supplied with a valid semaphore. */ DEBUGASSERT(sem != NULL);
if (sem != NULL)
{
/* The following operations must be performed with interrupts /* The following operations must be performed with interrupts
* disabled because sem_post() may be called from an interrupt * disabled because sem_post() may be called from an interrupt
* handler. * handler.
@ -89,11 +86,7 @@ int nxsem_post(FAR sem_t *sem)
/* Check the maximum allowable value */ /* Check the maximum allowable value */
if (sem_count >= SEM_VALUE_MAX) DEBUGASSERT(sem_count < SEM_VALUE_MAX);
{
leave_critical_section(flags);
return -EOVERFLOW;
}
/* Perform the semaphore unlock operation, releasing this task as a /* Perform the semaphore unlock operation, releasing this task as a
* holder then also incrementing the count on the semaphore. * holder then also incrementing the count on the semaphore.
@ -179,14 +172,12 @@ int nxsem_post(FAR sem_t *sem)
nxsem_restore_baseprio(stcb, sem); nxsem_restore_baseprio(stcb, sem);
sched_unlock(); sched_unlock();
#endif #endif
ret = OK;
/* Interrupts may now be enabled. */ /* Interrupts may now be enabled. */
leave_critical_section(flags); leave_critical_section(flags);
}
return ret; return OK;
} }
/**************************************************************************** /****************************************************************************
@ -222,6 +213,14 @@ int sem_post(FAR sem_t *sem)
{ {
int ret; int ret;
/* Make sure we were supplied with a valid semaphore. */
if (sem == NULL)
{
set_errno(EINVAL);
return ERROR;
}
ret = nxsem_post(sem); ret = nxsem_post(sem);
if (ret < 0) if (ret < 0)
{ {

View File

@ -75,8 +75,6 @@ int nxsem_trywait(FAR sem_t *sem)
DEBUGASSERT(sem != NULL && up_interrupt_context() == false); DEBUGASSERT(sem != NULL && up_interrupt_context() == false);
DEBUGASSERT(!OSINIT_IDLELOOP() || !sched_idletask()); DEBUGASSERT(!OSINIT_IDLELOOP() || !sched_idletask());
if (sem != NULL)
{
/* The following operations must be performed with interrupts disabled /* The following operations must be performed with interrupts disabled
* because sem_post() may be called from an interrupt handler. * because sem_post() may be called from an interrupt handler.
*/ */
@ -104,12 +102,6 @@ int nxsem_trywait(FAR sem_t *sem)
/* Interrupts may now be enabled. */ /* Interrupts may now be enabled. */
leave_critical_section(flags); leave_critical_section(flags);
}
else
{
ret = -EINVAL;
}
return ret; return ret;
} }
@ -138,6 +130,12 @@ int sem_trywait(FAR sem_t *sem)
{ {
int ret; int ret;
if (sem == NULL)
{
set_errno(EINVAL);
return ERROR;
}
/* Let nxsem_trywait do the real work */ /* Let nxsem_trywait do the real work */
ret = nxsem_trywait(sem); ret = nxsem_trywait(sem);

View File

@ -72,7 +72,7 @@ int nxsem_wait(FAR sem_t *sem)
{ {
FAR struct tcb_s *rtcb = this_task(); FAR struct tcb_s *rtcb = this_task();
irqstate_t flags; irqstate_t flags;
int ret = -EINVAL; int ret;
/* This API should not be called from interrupt handlers & idleloop */ /* This API should not be called from interrupt handlers & idleloop */
@ -88,8 +88,6 @@ int nxsem_wait(FAR sem_t *sem)
/* Make sure we were supplied with a valid semaphore. */ /* Make sure we were supplied with a valid semaphore. */
if (sem != NULL)
{
/* Check if the lock is available */ /* Check if the lock is available */
if (sem->semcount > 0) if (sem->semcount > 0)
@ -190,7 +188,6 @@ int nxsem_wait(FAR sem_t *sem)
sched_unlock(); sched_unlock();
#endif #endif
} }
}
leave_critical_section(flags); leave_critical_section(flags);
return ret; return ret;
@ -254,6 +251,12 @@ int sem_wait(FAR sem_t *sem)
int errcode; int errcode;
int ret; int ret;
if (sem == NULL)
{
set_errno(EINVAL);
return ERROR;
}
/* sem_wait() is a cancellation point */ /* sem_wait() is a cancellation point */
if (enter_cancellation_point()) if (enter_cancellation_point())