sem: Remove PRIOINHERIT_FLAGS_ENABLE and use SEM_PRIO_INHERIT instead

and refine the code to prepare the support of new flags

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2023-01-11 00:10:48 +08:00 committed by Petro Karashchenko
parent 48c9d10336
commit ef65d443ad
7 changed files with 17 additions and 35 deletions

View File

@ -37,8 +37,8 @@
****************************************************************************/ ****************************************************************************/
#define NXRMUTEX_NO_HOLDER (pid_t)-1 #define NXRMUTEX_NO_HOLDER (pid_t)-1
#define NXMUTEX_INITIALIZER NXSEM_INITIALIZER(1, PRIOINHERIT_FLAGS_ENABLE) #define NXMUTEX_INITIALIZER NXSEM_INITIALIZER(1, SEM_PRIO_INHERIT)
#define NXRMUTEX_INITIALIZER {NXSEM_INITIALIZER(1, PRIOINHERIT_FLAGS_ENABLE), \ #define NXRMUTEX_INITIALIZER {NXSEM_INITIALIZER(1, SEM_PRIO_INHERIT), \
NXRMUTEX_NO_HOLDER, 0} NXRMUTEX_NO_HOLDER, 0}
/**************************************************************************** /****************************************************************************

View File

@ -41,17 +41,12 @@
#define SEM_PRIO_NONE 0 #define SEM_PRIO_NONE 0
#define SEM_PRIO_INHERIT 1 #define SEM_PRIO_INHERIT 1
#define SEM_PRIO_PROTECT 2 #define SEM_PRIO_PROTECT 2
#define SEM_PRIO_MASK 3
/* Value returned by sem_open() in the event of a failure. */ /* Value returned by sem_open() in the event of a failure. */
#define SEM_FAILED NULL #define SEM_FAILED NULL
/* Bit definitions for the struct sem_s flags field */
#define PRIOINHERIT_FLAGS_ENABLE (1 << 0) /* Bit 0: Priority inheritance
* is enabled for this semaphore.
*/
/**************************************************************************** /****************************************************************************
* Public Type Declarations * Public Type Declarations
****************************************************************************/ ****************************************************************************/

View File

@ -55,15 +55,7 @@ int sem_getprotocol(FAR sem_t *sem, FAR int *protocol)
DEBUGASSERT(sem != NULL && protocol != NULL); DEBUGASSERT(sem != NULL && protocol != NULL);
#ifdef CONFIG_PRIORITY_INHERITANCE #ifdef CONFIG_PRIORITY_INHERITANCE
if ((sem->flags & PRIOINHERIT_FLAGS_ENABLE) != 0) *protocol = sem->flags;
{
*protocol = SEM_PRIO_INHERIT;
}
else
{
*protocol = SEM_PRIO_NONE;
}
#else #else
*protocol = SEM_PRIO_NONE; *protocol = SEM_PRIO_NONE;
#endif #endif

View File

@ -700,6 +700,7 @@ void nxsem_destroyholder(FAR sem_t *sem)
void nxsem_add_holder_tcb(FAR struct tcb_s *htcb, FAR sem_t *sem) void nxsem_add_holder_tcb(FAR struct tcb_s *htcb, FAR sem_t *sem)
{ {
FAR struct semholder_s *pholder; FAR struct semholder_s *pholder;
uint8_t prioinherit = sem->flags & SEM_PRIO_MASK;
/* If priority inheritance is disabled for this thread or it is IDLE /* If priority inheritance is disabled for this thread or it is IDLE
* thread, then do not add the holder. * thread, then do not add the holder.
@ -707,7 +708,7 @@ void nxsem_add_holder_tcb(FAR struct tcb_s *htcb, FAR sem_t *sem)
* inheritance is effectively disabled. * inheritance is effectively disabled.
*/ */
if (!is_idle_task(htcb) && (sem->flags & PRIOINHERIT_FLAGS_ENABLE) != 0) if (!is_idle_task(htcb) && prioinherit == SEM_PRIO_INHERIT)
{ {
/* Find or allocate a container for this new holder */ /* Find or allocate a container for this new holder */

View File

@ -121,8 +121,8 @@ int nxsem_post(FAR sem_t *sem)
* will do nothing. * will do nothing.
*/ */
prioinherit = sem->flags & PRIOINHERIT_FLAGS_ENABLE; prioinherit = sem->flags & SEM_PRIO_MASK;
if (prioinherit != 0) if (prioinherit == SEM_PRIO_INHERIT)
{ {
sched_lock(); sched_lock();
} }
@ -188,7 +188,7 @@ int nxsem_post(FAR sem_t *sem)
*/ */
#ifdef CONFIG_PRIORITY_INHERITANCE #ifdef CONFIG_PRIORITY_INHERITANCE
if (prioinherit != 0) if (prioinherit == SEM_PRIO_INHERIT)
{ {
nxsem_restore_baseprio(stcb, sem); nxsem_restore_baseprio(stcb, sem);
sched_unlock(); sched_unlock();

View File

@ -76,25 +76,18 @@ int nxsem_set_protocol(FAR sem_t *sem, int protocol)
{ {
DEBUGASSERT(sem != NULL); DEBUGASSERT(sem != NULL);
switch (protocol) switch (protocol & SEM_PRIO_MASK)
{ {
case SEM_PRIO_NONE: case SEM_PRIO_NONE:
/* Disable priority inheritance */
sem->flags &= ~PRIOINHERIT_FLAGS_ENABLE;
/* Remove any current holders */ /* Remove any current holders */
nxsem_destroyholder(sem); nxsem_destroyholder(sem);
return OK; break;
case SEM_PRIO_INHERIT: case SEM_PRIO_INHERIT:
/* Enable priority inheritance (dangerous) */ break;
sem->flags |= PRIOINHERIT_FLAGS_ENABLE;
return OK;
case SEM_PRIO_PROTECT: case SEM_PRIO_PROTECT:
@ -103,10 +96,11 @@ int nxsem_set_protocol(FAR sem_t *sem, int protocol)
return -ENOTSUP; return -ENOTSUP;
default: default:
break; return -EINVAL;
} }
return -EINVAL; sem->flags = protocol;
return OK;
} }
/**************************************************************************** /****************************************************************************

View File

@ -108,7 +108,7 @@ int nxsem_wait(FAR sem_t *sem)
else else
{ {
#ifdef CONFIG_PRIORITY_INHERITANCE #ifdef CONFIG_PRIORITY_INHERITANCE
uint8_t prioinherit = sem->flags & PRIOINHERIT_FLAGS_ENABLE; uint8_t prioinherit = sem->flags & SEM_PRIO_MASK;
#endif #endif
/* First, verify that the task is not already waiting on a /* First, verify that the task is not already waiting on a
@ -130,7 +130,7 @@ int nxsem_wait(FAR sem_t *sem)
*/ */
#ifdef CONFIG_PRIORITY_INHERITANCE #ifdef CONFIG_PRIORITY_INHERITANCE
if (prioinherit != 0) if (prioinherit == SEM_PRIO_INHERIT)
{ {
/* Disable context switching. The following operations must be /* Disable context switching. The following operations must be
* atomic with regard to the scheduler. * atomic with regard to the scheduler.