Skip to content

Commit

Permalink
OS: Fix task switch in task context
Browse files Browse the repository at this point in the history
The task switch function in task context rt_hw_context_switch
might not be switched due to higher level interrupt happened before
software interrupt to switch context, in the higher level interrupt
if it triggered switch interrupt in interrupt context, the task
context will be messed up.

want to switch TA -> TB : interrupt entered               | In TA context now
Interrupt triggered, want to switch to TC          | Still in TA context, but it assumed in TB context
software interrupt switch task, task switch TB -> TC, but actually it is still in TA context
so TA's context is actually saved to TB's stack, which is not right.

So we changed task switch here to use interrupt switch flow:

want to switch TA -> TB, interrupt entered | in TA context now
Interrupt triggered, want to switch to TC | still in TA context, but now it will do TA -> TC
software interrupt switch tasl, now do TA -> TC, which is correct now

In this case, it want to do two task switch, A->B, B->C, but accuatually
only 1 task switch is done, A->C due to higher interrupt handler entered before software interrupt,
and in higher interrupt handler, it also triggered task switch

Signed-off-by: Huaqi Fang <[email protected]>
  • Loading branch information
fanghuaqi committed Aug 13, 2021
1 parent 94996b0 commit 1a1a209
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions OS/RTThread/libcpu/risc-v/nuclei/cpuport.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to)

void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to)
{
rt_interrupt_from_thread = from;
rt_interrupt_to_thread = to;
portYIELD();
rt_hw_context_switch_interrupt(from, to);
}

/** shutdown CPU */
Expand Down

0 comments on commit 1a1a209

Please sign in to comment.