Zephyr Project
A Scalable Open Source RTOS for IoT Embedded Devices

◆ arch_switch()

static void arch_switch ( void *  switch_to,
void **  switched_from 

#include <kernel/include/kernel_arch_interface.h>

Cooperatively context switch

Architectures have considerable leeway on what the specific semantics of the switch handles are, but optimal implementations should do the following if possible:

1) Push all thread state relevant to the context switch to the current stack 2) Update the switched_from parameter to contain the current stack pointer, after all context has been saved. switched_from is used as an output- only parameter and its current value is ignored (and can be NULL, see below). 3) Set the stack pointer to the value provided in switch_to 4) Pop off all thread state from the stack we switched to and return.

Some arches may implement thread->switch handle as a pointer to the thread itself, and save context somewhere in thread->arch. In this case, on initial context switch from the dummy thread, thread->switch handle for the outgoing thread is NULL. Instead of dereferencing switched_from all the way to get the thread pointer, subtract ___thread_t_switch_handle_OFFSET to obtain the thread pointer instead. That is, such a scheme would have behavior like (in C pseudocode):

void arch_switch(void *switch_to, void **switched_from) { struct k_thread *new = switch_to; struct k_thread *old = CONTAINER_OF(switched_from, struct k_thread, switch_handle);

// save old context... *switched_from = old; // restore new context... }

Note that, regardless of the underlying handle representation, the incoming switched_from pointer MUST be written through with a non-NULL value after all relevant thread state has been saved. The kernel uses this as a synchronization signal to be able to wait for switch completion from another CPU.

switch_toIncoming thread's switch handle
switched_fromPointer to outgoing thread's switch handle storage location, which may be updated.