sched/mutex: add ticked lock version for mutex/rmutex

Added ticked lock version for mutex to reduce time calculation overhead

Signed-off-by: chao an <anchao.archer@bytedance.com>
This commit is contained in:
chao an
2025-04-02 17:25:11 +08:00
committed by Xiang Xiao
parent cc64c026d9
commit 0d6de4c0a7
3 changed files with 152 additions and 0 deletions

View File

@@ -215,6 +215,34 @@ int nxmutex_lock(FAR mutex_t *mutex);
int nxmutex_trylock(FAR mutex_t *mutex);
/****************************************************************************
* Name: nxmutex_ticklock
*
* Description:
* This function attempts to lock the mutex referenced by 'mutex'. If the
* mutex value is (<=) zero, then the calling task will not return until it
* successfully acquires the lock or timed out
*
* Input Parameters:
* mutex - Mutex object
* delay - Ticks to wait from the start time until the semaphore is
* posted. If ticks is zero, then this function is equivalent
* to nxmutex_trylock().
*
* Returned Value:
* OK The mutex successfully acquires
* EINVAL The mutex argument does not refer to a valid mutex. Or the
* thread would have blocked, and the abstime parameter specified
* a nanoseconds field value less than zero or greater than or
* equal to 1000 million.
* ETIMEDOUT The mutex could not be locked before the specified timeout
* expired.
* EDEADLK A deadlock condition was detected.
*
****************************************************************************/
int nxmutex_ticklock(FAR mutex_t *mutex, uint32_t delay);
/****************************************************************************
* Name: nxmutex_clocklock
*
@@ -561,6 +589,34 @@ int nxrmutex_lock(FAR rmutex_t *rmutex);
int nxrmutex_trylock(FAR rmutex_t *rmutex);
/****************************************************************************
* Name: nxrmutex_ticklock
*
* Description:
* This function attempts to lock the mutex referenced by 'mutex'. If the
* mutex value is (<=) zero, then the calling task will not return until it
* successfully acquires the lock or timed out
*
* Input Parameters:
* rmutex - Rmutex object
* delay - Ticks to wait from the start time until the semaphore is
* posted. If ticks is zero, then this function is equivalent
* to nxrmutex_trylock().
*
* Returned Value:
* OK The mutex successfully acquires
* EINVAL The mutex argument does not refer to a valid mutex. Or the
* thread would have blocked, and the abstime parameter specified
* a nanoseconds field value less than zero or greater than or
* equal to 1000 million.
* ETIMEDOUT The mutex could not be locked before the specified timeout
* expired.
* EDEADLK A deadlock condition was detected.
*
****************************************************************************/
int nxrmutex_ticklock(FAR rmutex_t *rmutex, uint32_t delay);
/****************************************************************************
* Name: nxrmutex_clocklock
*

View File

@@ -307,6 +307,56 @@ int nxmutex_trylock(FAR mutex_t *mutex)
return ret;
}
/****************************************************************************
* Name: nxmutex_ticklock
*
* Description:
* This function attempts to lock the mutex referenced by 'mutex'. If the
* mutex value is (<=) zero, then the calling task will not return until it
* successfully acquires the lock or timed out
*
* Input Parameters:
* mutex - Mutex object
* delay - Ticks to wait from the start time until the semaphore is
* posted. If ticks is zero, then this function is equivalent
* to sem_trywait().
*
* Returned Value:
* OK The mutex successfully acquires
* EINVAL The mutex argument does not refer to a valid mutex. Or the
* thread would have blocked, and the abstime parameter specified
* a nanoseconds field value less than zero or greater than or
* equal to 1000 million.
* ETIMEDOUT The mutex could not be locked before the specified timeout
* expired.
* EDEADLK A deadlock condition was detected.
*
****************************************************************************/
int nxmutex_ticklock(FAR mutex_t *mutex, uint32_t delay)
{
int ret;
/* Wait until we get the lock or until the timeout expires */
if (delay)
{
ret = nxsem_tickwait(&mutex->sem, delay);
}
else
{
ret = nxsem_trywait(&mutex->sem);
}
if (ret >= 0)
{
mutex->holder = _SCHED_GETTID();
nxmutex_add_backtrace(mutex);
}
return ret;
}
/****************************************************************************
* Name: nxmutex_clocklock
*
@@ -806,6 +856,50 @@ int nxrmutex_trylock(FAR rmutex_t *rmutex)
return ret;
}
/****************************************************************************
* Name: nxrmutex_ticklock
*
* Description:
* This function attempts to lock the mutex referenced by 'mutex'. If the
* mutex value is (<=) zero, then the calling task will not return until it
* successfully acquires the lock or timed out
*
* Input Parameters:
* rmutex - Rmutex object
* delay - Ticks to wait from the start time until the semaphore is
* posted. If ticks is zero, then this function is equivalent
* to nxrmutex_trylock().
*
* Returned Value:
* OK The mutex successfully acquires
* EINVAL The mutex argument does not refer to a valid mutex. Or the
* thread would have blocked, and the abstime parameter specified
* a nanoseconds field value less than zero or greater than or
* equal to 1000 million.
* ETIMEDOUT The mutex could not be locked before the specified timeout
* expired.
* EDEADLK A deadlock condition was detected.
*
****************************************************************************/
int nxrmutex_ticklock(FAR rmutex_t *rmutex, uint32_t delay)
{
int ret = 0;
if (!nxrmutex_is_hold(rmutex))
{
ret = nxmutex_ticklock(&rmutex->mutex, delay);
}
if (ret >= 0)
{
DEBUGASSERT(rmutex->count < UINT_MAX);
++rmutex->count;
}
return ret;
}
/****************************************************************************
* Name: nxrmutex_clocklock
*

View File

@@ -55,6 +55,7 @@
# define mutex_trylock(m) nxrmutex_trylock(m)
# define mutex_breaklock(m,v) nxrmutex_breaklock(m,v)
# define mutex_restorelock(m,v) nxrmutex_restorelock(m,v)
# define mutex_ticklock(m,t) nxrmutex_ticklock(m,t)
# define mutex_clocklock(m,t) nxrmutex_clocklock(m,CLOCK_REALTIME,t)
# define mutex_set_protocol(m,p) nxrmutex_set_protocol(m,p)
# define mutex_getprioceiling(m,p) nxrmutex_getprioceiling(m,p)
@@ -72,6 +73,7 @@
# define mutex_trylock(m) nxmutex_trylock(m)
# define mutex_breaklock(m,v) nxmutex_breaklock(m, v)
# define mutex_restorelock(m,v) nxmutex_restorelock(m, v)
# define mutex_ticklock(m,t) nxmutex_ticklock(m,t)
# define mutex_clocklock(m,t) nxmutex_clocklock(m,CLOCK_REALTIME,t)
# define mutex_set_protocol(m,p) nxmutex_set_protocol(m,p)
# define mutex_getprioceiling(m,p) nxmutex_getprioceiling(m,p)