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

View File

@ -97,19 +97,9 @@ int nxsem_clockwait(FAR sem_t *sem, clockid_t clockid,
int status;
int ret = ERROR;
DEBUGASSERT(sem != NULL && abstime != NULL);
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
* wait. We need to do this (as opposed to just disabling pre-emption)
* 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.
*/
#ifdef CONFIG_DEBUG_FEATURES
if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
{
ret = -EINVAL;
goto out;
}
#endif
/* Convert the timespec to clock ticks. We must have interrupts
* 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;
/* 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 */
enter_cancellation_point();

View File

@ -56,15 +56,13 @@
*
****************************************************************************/
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
* 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.
*
* Check if other threads are waiting on the semaphore.
@ -81,9 +79,6 @@ int nxsem_destroy (FAR sem_t *sem)
nxsem_destroyholder(sem);
return OK;
}
return -EINVAL;
}
/****************************************************************************
@ -114,6 +109,14 @@ int sem_destroy (FAR sem_t *sem)
{
int ret;
/* Assure a valid semaphore is specified */
if (sem == NULL)
{
set_errno(EINVAL);
return ERROR;
}
ret = nxsem_destroy(sem);
if (ret < 0)
{

View File

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

View File

@ -75,8 +75,6 @@ int nxsem_trywait(FAR sem_t *sem)
DEBUGASSERT(sem != NULL && up_interrupt_context() == false);
DEBUGASSERT(!OSINIT_IDLELOOP() || !sched_idletask());
if (sem != NULL)
{
/* The following operations must be performed with interrupts disabled
* 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. */
leave_critical_section(flags);
}
else
{
ret = -EINVAL;
}
return ret;
}
@ -138,6 +130,12 @@ int sem_trywait(FAR sem_t *sem)
{
int ret;
if (sem == NULL)
{
set_errno(EINVAL);
return ERROR;
}
/* Let nxsem_trywait do the real work */
ret = nxsem_trywait(sem);

View File

@ -72,7 +72,7 @@ int nxsem_wait(FAR sem_t *sem)
{
FAR struct tcb_s *rtcb = this_task();
irqstate_t flags;
int ret = -EINVAL;
int ret;
/* 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. */
if (sem != NULL)
{
/* Check if the lock is available */
if (sem->semcount > 0)
@ -190,7 +188,6 @@ int nxsem_wait(FAR sem_t *sem)
sched_unlock();
#endif
}
}
leave_critical_section(flags);
return ret;
@ -254,6 +251,12 @@ int sem_wait(FAR sem_t *sem)
int errcode;
int ret;
if (sem == NULL)
{
set_errno(EINVAL);
return ERROR;
}
/* sem_wait() is a cancellation point */
if (enter_cancellation_point())