StuBS
Context Switch

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

Classes

struct  StackPointer
 Structure for saving the stack pointer when switching coroutines. More...
 

Functions

void * prepareContext (void *tos, void(*kickoff)(void *), void *param1=nullptr)
 Prepares a context for its first activation. More...
 
void context_switch (StackPointer &current, StackPointer &next)
 Executes the context switch. More...
 
void context_go (StackPointer &next)
 Executes a thread for the very first time. More...
 

Detailed Description

Low-Level functionality required for context switching.


Class Documentation

◆ StackPointer

struct StackPointer

Structure for saving the stack pointer when switching coroutines.

Class Members
void * kernel Current kernel stack pointer of the thread.

Function Documentation

◆ prepareContext()

void * prepareContext ( void *  tos,
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.

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)
kickoffPointer to the Thread::kickoff function
param1first parameter for Thread::kickoff function
Returns
Pointer to the latest data (which will be popped first) on the prepared stack

◆ context_switch()

void context_switch ( StackPointer current,
StackPointer next 
)

Executes the context switch.

For a clean context switch, the current register values must be stored to the currently active stack and the stack pointer must be stored to the current StackPointer structure. Subsequently, these values must be restored accordingly from the next stack.

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

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

◆ context_go()

void context_go ( StackPointer next)

Executes a thread for the very first time.

For a clean startup, the current register values must be restored accordingly from the next stack.

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

Parameters
nextPointer to the structure that the next stack pointer will be read from