[riot-notifications] [RIOT-OS/RIOT] cpu/native: fix race in thread_yield_higher() (#10891)

Kaspar Schleiser notifications at github.com
Mon Jan 28 23:38:10 CET 2019

> Maybe because I don't fully understand how and when `sched_context_switch_request ` can be 0 in and when it is 1.

TL;DR ```sched_context_switch_request``` is used to request a (necessarily deferred) context switch from within an ISR.

A context switch can be triggered either from the running task or from within an ISR.
In the former case, it can be done right away: save the current context, run ```sched_run()``` to find the next thread, then restore that.

In ISRs, the context switch cannot be done right away. See e.g. this (pseudocode):

void foobar_isr() {
  if (whatever > FOO) {

In ISR context, the ISR must finish first. It is not possible to "pause" ISR context, switch to the thread, then "continue" the ISR context. ISRs must finish. So the thread that would probably be unlocked by the mutec_unlock() call is merely put on the runqueue, but we need to somehow pass along the information that a context switch might be necessary.

This is what "sched_context_switch_request" is used for: pass along the information that a context switch might be necessary after an ISR has finished.

Thing is, different platforms have different concepts.

E.g., on Cortex-M, there's PendSV, which will always be called last. Also, there are the banked ISR registers (a second set of registers), thus for serving an ISR, no manual context saving is necessary. Only when, after all ISRs have run, a different user thread should continue to run, the current context needs to be saved, the next selected and then restored, through fiddling with the user mode stack. ```sched_context_switch_request``` is used as a flag to let the architecture code, at the end of an ISR, know that this work should be done and thus PendSV should be triggered.

Cortex-M checks the flag here:


Other platforms do it differently, but all are using the flag.

You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190128/49f2ca2f/attachment-0001.html>

More information about the notifications mailing list