From 8b5078fc3688bbe49e7fa6ed3354b06b4ad6ce21 Mon Sep 17 00:00:00 2001 From: zhangyuan21 Date: Mon, 19 Dec 2022 14:55:12 +0800 Subject: [PATCH] sched/semaphore: check sem flags before enable priority inheritance Signed-off-by: zhangyuan21 --- sched/semaphore/sem_post.c | 17 ++++++++++++++--- sched/semaphore/sem_wait.c | 29 ++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/sched/semaphore/sem_post.c b/sched/semaphore/sem_post.c index 1f9f966116..a883cc6f02 100644 --- a/sched/semaphore/sem_post.c +++ b/sched/semaphore/sem_post.c @@ -72,6 +72,9 @@ int nxsem_post(FAR sem_t *sem) FAR struct tcb_s *stcb = NULL; irqstate_t flags; int16_t sem_count; +#ifdef CONFIG_PRIORITY_INHERITANCE + uint8_t prioinherit; +#endif DEBUGASSERT(sem != NULL); @@ -118,8 +121,13 @@ int nxsem_post(FAR sem_t *sem) * will do nothing. */ - sched_lock(); + prioinherit = sem->flags & PRIOINHERIT_FLAGS_ENABLE; + if (prioinherit != 0) + { + sched_lock(); + } #endif + /* If the result of semaphore unlock is non-positive, then * there must be some task waiting for the semaphore. */ @@ -180,8 +188,11 @@ int nxsem_post(FAR sem_t *sem) */ #ifdef CONFIG_PRIORITY_INHERITANCE - nxsem_restore_baseprio(stcb, sem); - sched_unlock(); + if (prioinherit != 0) + { + nxsem_restore_baseprio(stcb, sem); + sched_unlock(); + } #endif /* Interrupts may now be enabled. */ diff --git a/sched/semaphore/sem_wait.c b/sched/semaphore/sem_wait.c index df06d9c0f3..9c78a2d1b1 100644 --- a/sched/semaphore/sem_wait.c +++ b/sched/semaphore/sem_wait.c @@ -107,6 +107,10 @@ int nxsem_wait(FAR sem_t *sem) else { +#ifdef CONFIG_PRIORITY_INHERITANCE + uint8_t prioinherit = sem->flags & PRIOINHERIT_FLAGS_ENABLE; +#endif + /* First, verify that the task is not already waiting on a * semaphore */ @@ -126,18 +130,22 @@ int nxsem_wait(FAR sem_t *sem) */ #ifdef CONFIG_PRIORITY_INHERITANCE - /* Disable context switching. The following operations must be - * atomic with regard to the scheduler. - */ + if (prioinherit != 0) + { + /* Disable context switching. The following operations must be + * atomic with regard to the scheduler. + */ - sched_lock(); + sched_lock(); - /* Boost the priority of any threads holding a count on the - * semaphore. - */ + /* Boost the priority of any threads holding a count on the + * semaphore. + */ - nxsem_boost_priority(sem); + nxsem_boost_priority(sem); + } #endif + /* Set the errno value to zero (preserving the original errno) * value). We reuse the per-thread errno to pass information * between sem_waitirq() and this functions. @@ -201,7 +209,10 @@ int nxsem_wait(FAR sem_t *sem) ret = rtcb->errcode != OK ? -rtcb->errcode : OK; #ifdef CONFIG_PRIORITY_INHERITANCE - sched_unlock(); + if (prioinherit != 0) + { + sched_unlock(); + } #endif }