
Originally Posted by
BrianTee
But the readme doesn't say what tells the context_switch() when to run. Or for example if the timeslice is set to 10 msec and it takes 20 msec to run the code, when does it context_switch back to that thread?
The ReadMe does - the sentence after the indicated text above:
Code:
Notes on implementation
Threads take turns on the CPU and are switched by the context_switch() function, written in assembly. This function is called by the SysTick ISR. The library overrides the default systick_isr() to accomplish switching. On the Teensy by default, each tick is 1 millisecond long. By default, each thread runs for 100 ticks, or 100 milliseconds, but this can be changed by setTimeSlice().
With an allocated 10 ms timeslice the thread "A" will be idled after 10 ms for the next thread.
When other threads finish their timeslices the idled thread "A" will be restored to resume execution where it left off.
If "A" needs 20 ms to complete a task - it will be completed in two 10 ms time slices. When time slice is up - it is just frozen/idled without notice until the start of the next time slice for that thread "A".
If thread "A" is a continuous process, it will execute 10ms at a time between the time given to other threads.