StuBS
Context Switch

Low-Level functionality required for context switching. More...

Classes

struct  Context
 Structure for saving the CPU context when switching coroutines. More...
 

Functions

void prepareContext (void *tos, Context &context, void(*kickoff)(void *), void *param1=nullptr)
 Prepares a context for its first activation. More...
 
void context_switch (Context *current, Context *next)
 Executes the context switch. More...
 
void context_launch (Context *next)
 Launch context switching. More...
 

Detailed Description

Low-Level functionality required for context switching.

Function Documentation

◆ context_launch()

void context_launch ( Context next)

Launch context switching.

To start context switching, the current context (from the boot-routines) is thrown away and the prepared register values within the given next context replace it.

This function must be implemented in assembler in the file context.asm (why?). It must be declared as extern "C", so assembler functions are not C++ name mangled.

Parameters
nextPointer to the structure that the next context will be read from

◆ context_switch()

void context_switch ( Context current,
Context next 
)

Executes the context switch.

For a clean context switch, the current register values must be stored in the given context struct. Subsequently, these values must be restored accordingly from the next context struct.

This function must be implemented in assembler in the file context.asm (why?). It must be declared as extern "C", so assembler functions are not C++ name mangled.

Parameters
currentPointer to the structure that the current context will be stored in
nextPointer to the structure that the next context will be read from

◆ prepareContext()

void prepareContext ( void *  tos,
Context context,
void(*)(void *)  kickoff,
void *  param1 = nullptr 
)

Prepares a context for its first activation.

To allow the execution to start in Thread::kickoff during the first activation, the stack must be initialized such that it contains all the information required. This is, most importantly, the function to be called (typically the respective implementation of Thread::kickoff) and any parameters required by this function.

Additionally to the stack, the initial context is setup in a way that context_switch and context_launch can work with it.

prepareContext() can be implemented in the high-level programming language C++ (in file context.cc).

Parameters
tosPointer to the top of stack (= address of first byte beyond the memory reserved for the stack)
contextReference to the Context structure to be filled
kickoffPointer to the Thread::kickoff function
param1first parameter for Thread::kickoff function