From d8ebe98c6ceb582c2fbf14325c7d8bd3faba6236 Mon Sep 17 00:00:00 2001 From: ligd Date: Fri, 22 Jul 2022 12:07:27 +0800 Subject: [PATCH] pm: use rmutex_xx API for recursive lock Signed-off-by: ligd --- drivers/power/pm.h | 8 ++--- drivers/power/pm_initialize.c | 5 +-- drivers/power/pm_lock.c | 60 ++--------------------------------- 3 files changed, 5 insertions(+), 68 deletions(-) diff --git a/drivers/power/pm.h b/drivers/power/pm.h index a01dd5a22d..435c00787e 100644 --- a/drivers/power/pm.h +++ b/drivers/power/pm.h @@ -79,13 +79,9 @@ struct pm_domain_s FAR const struct pm_governor_s *governor; - /* This semaphore manages mutually exclusive access to the domain state. - * It must be initialized to the value 1. - */ + /* Recursive lock for race condition */ - sem_t sem; - pid_t holder; - unsigned int count; + rmutex_t lock; }; /* This structure encapsulates all of the global data used by the PM system */ diff --git a/drivers/power/pm_initialize.c b/drivers/power/pm_initialize.c index ecdfbf9f24..839cec0bbf 100644 --- a/drivers/power/pm_initialize.c +++ b/drivers/power/pm_initialize.c @@ -92,10 +92,7 @@ void pm_initialize(void) #endif pm_set_governor(i, gov); - nxsem_init(&g_pmglobals.domain[i].sem, 0, 1); - - g_pmglobals.domain[i].holder = INVALID_PROCESS_ID; - g_pmglobals.domain[i].count = 0; + nxrmutex_init(&g_pmglobals.domain[i].lock); } } diff --git a/drivers/power/pm_lock.c b/drivers/power/pm_lock.c index 7e87fe56a7..4c0c2ed1fa 100644 --- a/drivers/power/pm_lock.c +++ b/drivers/power/pm_lock.c @@ -33,62 +33,6 @@ #if defined(CONFIG_PM) -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -static int pm_recursive_lock(struct pm_domain_s *pm) -{ - pid_t me = gettid(); - int ret = OK; - - /* Does this thread already hold the semaphore? */ - - if (pm->holder == me) - { - /* Yes.. just increment the reference count */ - - pm->count++; - } - else - { - /* No.. take the semaphore (perhaps waiting) */ - - ret = nxsem_wait_uninterruptible(&pm->sem); - if (ret >= 0) - { - /* Now this thread holds the semaphore */ - - pm->holder = me; - pm->count = 1; - } - } - - return ret; -} - -static void pm_recursive_unlock(struct pm_domain_s *pm) -{ - DEBUGASSERT(pm->holder == getpid() && pm->count > 0); - - /* If the count would go to zero, then release the semaphore */ - - if (pm->count == 1) - { - /* We no longer hold the semaphore */ - - pm->holder = INVALID_PROCESS_ID; - pm->count = 0; - nxsem_post(&pm->sem); - } - else - { - /* We still hold the semaphore. Just decrement the count */ - - pm->count--; - } -} - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -108,7 +52,7 @@ irqstate_t pm_lock(int domain) { if (!up_interrupt_context() && !sched_idletask()) { - pm_recursive_lock(&g_pmglobals.domain[domain]); + nxrmutex_lock(&g_pmglobals.domain[domain].lock); } return enter_critical_section(); @@ -131,7 +75,7 @@ void pm_unlock(int domain, irqstate_t flags) if (!up_interrupt_context() && !sched_idletask()) { - pm_recursive_unlock(&g_pmglobals.domain[domain]); + nxrmutex_unlock(&g_pmglobals.domain[domain].lock); } }