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 NXMUTEX_INITIALIZER NXSEM_INITIALIZER(1, PRIOINHERIT_FLAGS_ENABLE)
#define NXRMUTEX_INITIALIZER {NXSEM_INITIALIZER(1, PRIOINHERIT_FLAGS_ENABLE), \
#define NXMUTEX_INITIALIZER NXSEM_INITIALIZER(1, SEM_PRIO_INHERIT)
#define NXRMUTEX_INITIALIZER {NXSEM_INITIALIZER(1, SEM_PRIO_INHERIT), \
NXRMUTEX_NO_HOLDER, 0}
/****************************************************************************

View file

@ -41,17 +41,12 @@
#define SEM_PRIO_NONE 0
#define SEM_PRIO_INHERIT 1
#define SEM_PRIO_PROTECT 2
#define SEM_PRIO_MASK 3
/* Value returned by sem_open() in the event of a failure. */
#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
****************************************************************************/

View file

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

View file

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

View file

@ -76,25 +76,18 @@ int nxsem_set_protocol(FAR sem_t *sem, int protocol)
{
DEBUGASSERT(sem != NULL);
switch (protocol)
switch (protocol & SEM_PRIO_MASK)
{
case SEM_PRIO_NONE:
/* Disable priority inheritance */
sem->flags &= ~PRIOINHERIT_FLAGS_ENABLE;
/* Remove any current holders */
nxsem_destroyholder(sem);
return OK;
break;
case SEM_PRIO_INHERIT:
/* Enable priority inheritance (dangerous) */
sem->flags |= PRIOINHERIT_FLAGS_ENABLE;
return OK;
break;
case SEM_PRIO_PROTECT:
@ -103,10 +96,11 @@ int nxsem_set_protocol(FAR sem_t *sem, int protocol)
return -ENOTSUP;
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
{
#ifdef CONFIG_PRIORITY_INHERITANCE
uint8_t prioinherit = sem->flags & PRIOINHERIT_FLAGS_ENABLE;
uint8_t prioinherit = sem->flags & SEM_PRIO_MASK;
#endif
/* 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
if (prioinherit != 0)
if (prioinherit == SEM_PRIO_INHERIT)
{
/* Disable context switching. The following operations must be
* atomic with regard to the scheduler.