Hi all,
I’m rather new to using RIOT and have been hunting around for some thorough documentation on how the scheduler works, but haven’t really been able to find anything that helps to explain what I am generally seeing in most ISR definitions, which tend to end with the following bit of code:
if (sched_context_switch_request) { thread_yield(); }
My first questions are: What are the general sources for context switch requests and what does it mean to call thread_yield in an ISR? I would have imagined that ISRs are called outside of the context of any thread, so calling yield within them didn’t immediately make sense to me. Is this just a general pattern for triggering the scheduler to run, if necessary, after the ISR has completed?
I walked through the code in thread_yield a bit and (for the Cortex-M platform specifically) reached this partial understanding:
void thread_yield(void) { // Disable IRQs and store the PRIMASK unsigned old_state = irq_disable();
// Get the currently active thread (In an ISR, I assume this is the interrupted thread) thread_t *me = (thread_t *)sched_active_thread;
// If the thread is currently running or pending if (me->status >= STATUS_ON_RUNQUEUE) { // Advance the next runnable thread at the current thread’s priority level clist_lpoprpush(&sched_runqueues[me->priority]); }
// Re-enable the original PRIMASK irq_restore(old_state);
// Trigger the PendSV interrupt to run the scheduler thread_yield_higher(); }
I’m having a bit of difficulty understanding the flow of events that would lead you to call thread_yield inside of an ISR, as I imagine that it causes the thread that was running before the ISR was executed to be the one that yields. Why would it be necessary to cause that thread to yield to other threads at the same level of priority simply because a potentially unrelated ISR was run? I predict that the reasoning for that is explained by how sched_context_switch_request operates.
Perhaps this is designed for the case where actions are taken inside the ISR which signal other threads (like sending IPC messages), but I’m still not clear on why it would need to reschedule the previously executing thread relative to other running or pending threads of equal priority.
Any clarification or direction towards more indepth documentation of the scheduling system is much appreciated.
Thanks much, Charles