-
Notifications
You must be signed in to change notification settings - Fork 2.1k
AVR thread yield #5745
Description
I believe the AVR thread yield is not implemented correctly. thread_arch_yield (aka thread_yield_higher) is currently implemented as a naked function that will begin execution of the new thread at the same level as the calling point. In the atmega version of timer.c, thread_yield_higher is called in an ISR, which can result in long running functions at this level possible halting the execution of all other threads. https://github.com/RIOT-OS/RIOT/blob/master/cpu/atmega_common/periph/timer.c#L193
I discovered this when working with xtimer. It would consistently run the spin loop version instead of sleeping the thread due to running at interrupt level. https://github.com/RIOT-OS/RIOT/blob/master/sys/xtimer/xtimer.c#L45
A possible method for eliminating this issue is to use an interrupt driven thread swap like on the other platforms. For example, on the cortex platform, an interrupt is triggered and will be serviced as normal, the resulting context swap will place the new thread at the normal execution level since it will only start execution after returning the thread swapping ISR returns. Unlike the cortex platform, AVRs do not have a dedicated software driven interrupt for context swaps.
I have implemented a method for doing this by using one of the external interrupt pins, while keeping it as an output and driving it through software. master...jthacker:avr_thread_arch_isr_stack_usage
CONTEXT_SWAP_INIT, CONTEXT_SWAP_INT_VECT and CONTEXT_SWAP_TRIGGER would need to implemented by each board since this requires sacrificing a physical pin on each board, and may be dependent on the layout. Obviously this is not ideal. For this reason I have opened this as an issue and not a pull request to see if firstly there is agreement with the issue at hand and to discuss possible alternative solutions.