dozniak wrote:prasoc wrote:Well the idea of the "run" variable in each thread is that when the scheduler starts (loops around the first time), it doesn't actually SAVE the registers until it's ran at least once - otherwise you would overwrite the thread's state with nonsense.
But the current thread's state is not nonsense - since it's already running it has some state that HAS to be saved to later return to.
At the beginning it is, each thread needs to run atleast once before it has a proper state, so the scheduler goes like this:
>>>>> run 1: (do not save the current registers into task 0, as it hasnt ran yet)
(increment the task counter)
set task 1 "ran" = true
load task 1 into the register and run
-
>>>>> run 2: save task 1, because "ran = true"
(increment the task counter)
set task 2 "ran" = true
load task 2 into the register and run
-
>>>>> run 3: save task 2, because "ran = true"
(increment the task counter)
set task 3 "ran" = true
load task 3 into the register and run
-
etc. etc.
it pins down the edge case of the first loop not having a "previous state".
Brendan wrote:This translates to something like:
Thank you for the example task switching code, Brendan. I will try and incorporate it into my scheduler and report back my success / failures