Zth (libzth)
Loading...
Searching...
No Matches
zth::impl::ContextBase< Impl > Class Template Reference

Base class of the Context. More...

#include <context.h>

Inheritance diagram for zth::impl::ContextBase< Impl >:
zth::impl::ContextArch< Context > zth::impl::ContextArch< Context > zth::impl::ContextArch< Impl > zth::impl::ContextArch< Impl > zth::Context zth::Context zth::Context zth::Context zth::Context zth::Context zth::Context zth::Context

Classes

struct  Stack
 Stack information. More...
 

Public Member Functions

ContextAttrattr () noexcept
 Return the context attributes, requested by the user.
 
ContextAttr const & attr () const noexcept
 Return the context attributes, requested by the user.
 
int create () noexcept
 Create context.
 
void destroy () noexcept
 Destroy and cleanup context.
 
int initStack (Stack &stack, Stack &usable) noexcept
 Allocate and initialize stack.
 
void deinitStack (Stack &stack) noexcept
 Deinit and free stack.
 
Stack const & stack () const noexcept
 Return the stack address.
 
Stack const & stackUsable () const noexcept
 Return the start of the actual usable stack.
 
size_t calcStackSize (size_t size) noexcept
 Compute the stack size, given the requested user size and current configuration.
 
void * allocStack (size_t size) noexcept
 Allocate requested size of stack memory.
 
void deallocStack (Stack &stack) noexcept
 Frees the previously allocated stack.
 
void stackAlign (Stack &stack) noexcept
 Compute and modify the stack alignment and size, within the allocated space.
 
void valgrindRegister () noexcept
 Register the current stack to valgrind.
 
void valgrindDeregister () noexcept
 Deregister the current stack from valgrind.
 
int stackGuardInit () noexcept
 Initialize guards around the stack memory.
 
void stackGuardDeinit () noexcept
 Release the guards around the memory.
 
void stackGuard () noexcept
 Configure the guard for the current fiber.
 
void stackGuard (Stack const &stack) noexcept
 Configure the guard for the given stack.
 
void * stackGuard (void *p) noexcept
 Configure the guard for the address.
 
void context_switch (Context &to) noexcept
 Perform a context switch.
 
void context_push_regs () noexcept
 Pre-sjlj context saving.
 
void context_pop_regs () noexcept
 Post-sjlj context restoring.
 
void context_prepare_jmp (Impl &to, jmp_buf &env) noexcept
 Pre-sjlj jump.
 
void die () noexcept
 Flag fiber as died after it returned from context_entry().
 
bool alive () const noexcept
 Check if fiber is still running.
 
void * stack_switch (void *stack, size_t size, void *(*f)(void *) noexcept, void *arg) noexcept
 

Static Public Member Functions

static int init () noexcept
 One-time system initialization.
 
static void deinit () noexcept
 Final system cleanup.
 
static size_t pageSize () noexcept
 Get system's page size.
 
static bool stackGrowsDown (void const *reference)
 Checks if the stack grows down or up.
 
static void ** sp (Stack const &stack) noexcept
 Get the initial stack pointer for the given stack.
 
static void stack_push (void **&sp, void *p) noexcept
 Push data into the stack.
 
static void set_sp (jmp_buf &env, void **sp) noexcept
 Set the stack pointer in a jmp_buf.
 
static void set_pc (jmp_buf &env, void *sp) noexcept
 Set the program counter in a jmp_buf.
 
static void context_trampoline_from_jmp_buf () noexcept
 Entry point to jump to from a (sig)jmp_buf.
 

Protected Member Functions

constexpr ContextBase (ContextAttr const &attr) noexcept
 
 ~ContextBase ()
 Dtor.
 
Impl & impl () noexcept
 Return Impl this.
 
Impl const & impl () const noexcept
 Return Impl this.
 

Detailed Description

template<typename Impl>
class zth::impl::ContextBase< Impl >

Base class of the Context.

The Context consists of three layers:

Although the classes inherit each other and override methods, they are not virtual. All implementation is done in the header. The lowest subclass is passed as template parameter to the parents, which allows invoking the most specific methods. This allows fully inlining and optimizing the functions.

Definition at line 68 of file context.h.

Constructor & Destructor Documentation

◆ ContextBase()

template<typename Impl >
constexpr zth::impl::ContextBase< Impl >::ContextBase ( ContextAttr const &  attr)
inlineexplicitconstexprprotectednoexcept

Definition at line 71 of file context.h.

◆ ~ContextBase()

template<typename Impl >
zth::impl::ContextBase< Impl >::~ContextBase ( )
inlineprotected

Dtor.

Definition at line 81 of file context.h.

Member Function Documentation

◆ alive()

template<typename Impl >
bool zth::impl::ContextBase< Impl >::alive ( ) const
inlinenoexcept

Check if fiber is still running.

Definition at line 520 of file context.h.

◆ allocStack()

template<typename Impl >
void * zth::impl::ContextBase< Impl >::allocStack ( size_t  size)
inlinenoexcept

Allocate requested size of stack memory.

Alignment does not have to be enforced.

Parameters
sizethe requested size, which is a multiple of pageSize().

Definition at line 287 of file context.h.

◆ attr() [1/2]

template<typename Impl >
ContextAttr const & zth::impl::ContextBase< Impl >::attr ( ) const
inlinenoexcept

Return the context attributes, requested by the user.

Definition at line 151 of file context.h.

◆ attr() [2/2]

template<typename Impl >
ContextAttr & zth::impl::ContextBase< Impl >::attr ( )
inlinenoexcept

Return the context attributes, requested by the user.

Definition at line 143 of file context.h.

◆ calcStackSize()

template<typename Impl >
size_t zth::impl::ContextBase< Impl >::calcStackSize ( size_t  size)
inlinenoexcept

Compute the stack size, given the requested user size and current configuration.

Definition at line 265 of file context.h.

◆ context_pop_regs()

template<typename Impl >
void zth::impl::ContextBase< Impl >::context_pop_regs ( )
inlinenoexcept

Post-sjlj context restoring.

Definition at line 499 of file context.h.

◆ context_prepare_jmp()

template<typename Impl >
void zth::impl::ContextBase< Impl >::context_prepare_jmp ( Impl &  to,
jmp_buf &  env 
)
inlinenoexcept

Pre-sjlj jump.

Definition at line 505 of file context.h.

◆ context_push_regs()

template<typename Impl >
void zth::impl::ContextBase< Impl >::context_push_regs ( )
inlinenoexcept

Pre-sjlj context saving.

Definition at line 494 of file context.h.

◆ context_switch()

template<typename Impl >
void zth::impl::ContextBase< Impl >::context_switch ( Context to)
noexcept

Perform a context switch.

◆ context_trampoline_from_jmp_buf()

template<typename Impl >
static void zth::impl::ContextBase< Impl >::context_trampoline_from_jmp_buf ( )
staticnoexcept

Entry point to jump to from a (sig)jmp_buf.

As the pc is probably set via set_pc(), which is not really supported by the standard libary, you may also have to implement this function a bit more careful.

It should call context_entry in the end.

◆ create()

template<typename Impl >
int zth::impl::ContextBase< Impl >::create ( )
inlinenoexcept

Create context.

Definition at line 159 of file context.h.

◆ deallocStack()

template<typename Impl >
void zth::impl::ContextBase< Impl >::deallocStack ( Stack stack)
inlinenoexcept

Frees the previously allocated stack.

Definition at line 312 of file context.h.

◆ deinit()

template<typename Impl >
static void zth::impl::ContextBase< Impl >::deinit ( )
inlinestaticnoexcept

Final system cleanup.

Definition at line 138 of file context.h.

◆ deinitStack()

template<typename Impl >
void zth::impl::ContextBase< Impl >::deinitStack ( Stack stack)
inlinenoexcept

Deinit and free stack.

Definition at line 227 of file context.h.

◆ destroy()

template<typename Impl >
void zth::impl::ContextBase< Impl >::destroy ( )
inlinenoexcept

Destroy and cleanup context.

Definition at line 189 of file context.h.

◆ die()

template<typename Impl >
void zth::impl::ContextBase< Impl >::die ( )
inlinenoexcept

Flag fiber as died after it returned from context_entry().

Definition at line 510 of file context.h.

◆ impl() [1/2]

template<typename Impl >
Impl const & zth::impl::ContextBase< Impl >::impl ( ) const
inlineprotectednoexcept

Return Impl this.

Definition at line 98 of file context.h.

◆ impl() [2/2]

template<typename Impl >
Impl & zth::impl::ContextBase< Impl >::impl ( )
inlineprotectednoexcept

Return Impl this.

Definition at line 90 of file context.h.

◆ init()

template<typename Impl >
static int zth::impl::ContextBase< Impl >::init ( )
inlinestaticnoexcept

One-time system initialization.

Definition at line 130 of file context.h.

◆ initStack()

template<typename Impl >
int zth::impl::ContextBase< Impl >::initStack ( Stack stack,
Stack usable 
)
inlinenoexcept

Allocate and initialize stack.

Definition at line 200 of file context.h.

◆ pageSize()

template<typename Impl >
static size_t zth::impl::ContextBase< Impl >::pageSize ( )
inlinestaticnoexcept

Get system's page size.

Definition at line 235 of file context.h.

◆ set_pc()

template<typename Impl >
static void zth::impl::ContextBase< Impl >::set_pc ( jmp_buf &  env,
void *  sp 
)
staticnoexcept

Set the program counter in a jmp_buf.

◆ set_sp()

template<typename Impl >
static void zth::impl::ContextBase< Impl >::set_sp ( jmp_buf &  env,
void **  sp 
)
staticnoexcept

Set the stack pointer in a jmp_buf.

◆ sp()

template<typename Impl >
static void ** zth::impl::ContextBase< Impl >::sp ( Stack const &  stack)
staticnoexcept

Get the initial stack pointer for the given stack.

◆ stack()

template<typename Impl >
Stack const & zth::impl::ContextBase< Impl >::stack ( ) const
inlinenoexcept

Return the stack address.

Definition at line 249 of file context.h.

◆ stack_push()

template<typename Impl >
static void zth::impl::ContextBase< Impl >::stack_push ( void **&  sp,
void *  p 
)
staticnoexcept

Push data into the stack.

◆ stack_switch()

template<typename Impl >
void * zth::impl::ContextBase< Impl >::stack_switch ( void *  stack,
size_t  size,
void *(*)(void *) noexcept  f,
void *  arg 
)
inlinenoexcept

Definition at line 529 of file context.h.

◆ stackAlign()

template<typename Impl >
void zth::impl::ContextBase< Impl >::stackAlign ( Stack stack)
inlinenoexcept

Compute and modify the stack alignment and size, within the allocated space.

Definition at line 329 of file context.h.

◆ stackGrowsDown()

template<typename Impl >
static bool zth::impl::ContextBase< Impl >::stackGrowsDown ( void const *  reference)
inlinestatic

Checks if the stack grows down or up.

Parameters
referencea pointer to some object that is at the caller's stack
Returns
true if it grows down

Definition at line 424 of file context.h.

◆ stackGuard() [1/3]

template<typename Impl >
void zth::impl::ContextBase< Impl >::stackGuard ( )
inlinenoexcept

Configure the guard for the current fiber.

This is called after every context switch.

Definition at line 435 of file context.h.

◆ stackGuard() [2/3]

template<typename Impl >
void zth::impl::ContextBase< Impl >::stackGuard ( Stack const &  stack)
inlinenoexcept

Configure the guard for the given stack.

Definition at line 443 of file context.h.

◆ stackGuard() [3/3]

template<typename Impl >
void * zth::impl::ContextBase< Impl >::stackGuard ( void *  p)
inlinenoexcept

Configure the guard for the address.

Returns
the previous guard

Definition at line 450 of file context.h.

◆ stackGuardDeinit()

template<typename Impl >
void zth::impl::ContextBase< Impl >::stackGuardDeinit ( )
inlinenoexcept

Release the guards around the memory.

Definition at line 414 of file context.h.

◆ stackGuardInit()

template<typename Impl >
int zth::impl::ContextBase< Impl >::stackGuardInit ( )
inlinenoexcept

Initialize guards around the stack memory.

Definition at line 393 of file context.h.

◆ stackUsable()

template<typename Impl >
Stack const & zth::impl::ContextBase< Impl >::stackUsable ( ) const
inlinenoexcept

Return the start of the actual usable stack.

Definition at line 257 of file context.h.

◆ valgrindDeregister()

template<typename Impl >
void zth::impl::ContextBase< Impl >::valgrindDeregister ( )
inlinenoexcept

Deregister the current stack from valgrind.

Definition at line 382 of file context.h.

◆ valgrindRegister()

template<typename Impl >
void zth::impl::ContextBase< Impl >::valgrindRegister ( )
inlinenoexcept

Register the current stack to valgrind.

Definition at line 364 of file context.h.


The documentation for this class was generated from the following file: