diff --git a/include/nuttx/mutex.h b/include/nuttx/mutex.h index 70c15db7df..b790db839e 100644 --- a/include/nuttx/mutex.h +++ b/include/nuttx/mutex.h @@ -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} /**************************************************************************** diff --git a/include/semaphore.h b/include/semaphore.h index bf3a27c2ca..8ba1906a44 100644 --- a/include/semaphore.h +++ b/include/semaphore.h @@ -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 ****************************************************************************/ diff --git a/libs/libc/semaphore/sem_getprotocol.c b/libs/libc/semaphore/sem_getprotocol.c index 6339d736ed..2e1f63fbc8 100644 --- a/libs/libc/semaphore/sem_getprotocol.c +++ b/libs/libc/semaphore/sem_getprotocol.c @@ -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 diff --git a/sched/semaphore/sem_holder.c b/sched/semaphore/sem_holder.c index 181a4d32dc..b4d0336c18 100644 --- a/sched/semaphore/sem_holder.c +++ b/sched/semaphore/sem_holder.c @@ -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 */ diff --git a/sched/semaphore/sem_post.c b/sched/semaphore/sem_post.c index a883cc6f02..2a0d72987d 100644 --- a/sched/semaphore/sem_post.c +++ b/sched/semaphore/sem_post.c @@ -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(); diff --git a/sched/semaphore/sem_setprotocol.c b/sched/semaphore/sem_setprotocol.c index ca8042afa1..74f864bcc9 100644 --- a/sched/semaphore/sem_setprotocol.c +++ b/sched/semaphore/sem_setprotocol.c @@ -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; } /**************************************************************************** diff --git a/sched/semaphore/sem_wait.c b/sched/semaphore/sem_wait.c index 9c78a2d1b1..004f52c068 100644 --- a/sched/semaphore/sem_wait.c +++ b/sched/semaphore/sem_wait.c @@ -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.