diff --git a/drivers/power/pm_register.c b/drivers/power/pm_register.c index 3c68f31b2f50b..fa95b8efa34ca 100644 --- a/drivers/power/pm_register.c +++ b/drivers/power/pm_register.c @@ -62,19 +62,11 @@ int pm_register(FAR struct pm_callback_s *callbacks) /* Add the new entry to the end of the list of registered callbacks */ - if (OSINIT_OS_READY()) - { - ret = pm_lock(); - if (ret == OK) - { - dq_addlast(&callbacks->entry, &g_pmglobals.registry); - pm_unlock(); - } - } - else + ret = pm_lock(); + if (ret == OK) { dq_addlast(&callbacks->entry, &g_pmglobals.registry); - ret = OK; + pm_unlock(); } return ret; diff --git a/include/nuttx/init.h b/include/nuttx/init.h index 1d73ba7aee295..3bdc67f6ec41e 100644 --- a/include/nuttx/init.h +++ b/include/nuttx/init.h @@ -41,6 +41,7 @@ #define OSINIT_MM_READY() (g_nx_initstate >= OSINIT_MEMORY) #define OSINIT_HW_READY() (g_nx_initstate >= OSINIT_HARDWARE) #define OSINIT_OS_READY() (g_nx_initstate >= OSINIT_OSREADY) +#define OSINIT_IDLELOOP() (g_nx_initstate >= OSINIT_IDLELOOP) #define OSINIT_OS_INITIALIZING() (g_nx_initstate < OSINIT_OSREADY) /**************************************************************************** @@ -65,8 +66,9 @@ enum nx_initstate_e * to support the hardware are also available but * the OS has not yet completed its full * initialization. */ - OSINIT_OSREADY = 5 /* The OS is fully initialized and multi-tasking is + OSINIT_OSREADY = 5, /* The OS is fully initialized and multi-tasking is * active. */ + OSINIT_IDLELOOP = 6 /* The OS enter idle loop */ }; /**************************************************************************** diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c index d7894640e9292..74473e8f7a378 100644 --- a/sched/init/nx_start.c +++ b/sched/init/nx_start.c @@ -776,6 +776,10 @@ void nx_start(void) DEBUGVERIFY(nx_bringup()); + /* Enter to idleloop */ + + g_nx_initstate = OSINIT_IDLELOOP; + /* Let other threads have access to the memory manager */ sched_unlock(); diff --git a/sched/semaphore/sem_trywait.c b/sched/semaphore/sem_trywait.c index 2da213907dc0c..1083dd97f5574 100644 --- a/sched/semaphore/sem_trywait.c +++ b/sched/semaphore/sem_trywait.c @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -69,9 +70,10 @@ int nxsem_trywait(FAR sem_t *sem) irqstate_t flags; int ret; - /* This API should not be called from interrupt handlers */ + /* This API should not be called from interrupt handlers & idleloop */ DEBUGASSERT(sem != NULL && up_interrupt_context() == false); + DEBUGASSERT(OSINIT_IDLELOOP() && !sched_idletask()); if (sem != NULL) { diff --git a/sched/semaphore/sem_wait.c b/sched/semaphore/sem_wait.c index eec67723d77ba..0e1acc4886be6 100644 --- a/sched/semaphore/sem_wait.c +++ b/sched/semaphore/sem_wait.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -73,9 +74,10 @@ int nxsem_wait(FAR sem_t *sem) irqstate_t flags; int ret = -EINVAL; - /* This API should not be called from interrupt handlers */ + /* This API should not be called from interrupt handlers & idleloop */ DEBUGASSERT(sem != NULL && up_interrupt_context() == false); + DEBUGASSERT(OSINIT_IDLELOOP() && !sched_idletask()); /* The following operations must be performed with interrupts * disabled because nxsem_post() may be called from an interrupt