diff --git a/include/nuttx/init.h b/include/nuttx/init.h index 1d73ba7aee2..3bdc67f6ec4 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 d7894640e92..74473e8f7a3 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 2da213907dc..4e9cc0ea015 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 eec67723d77..29580d72ccc 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