OMNeT++ Simulation Library  6.0.3
cCoroutine Class Reference

#include <ccoroutine.h>

Description

Low-level coroutine library. Coroutines are used by cSimpleModule.

cCoroutine has platform-dependent implementation:

On Windows, it uses the Win32 Fiber API.

On Unix-like systems, it uses POSIX coroutines (setcontext()/switchcontext()) if they are available.

Otherwise, it uses a portable coroutine library first described by Stig Kofoed ("Portable coroutines", see the Manual for a better reference). It creates all coroutine stacks within the main stack, and uses setjmp()/longjmp() for context switching. This implies that the maximum stack space allowed by the operating system for the OMNeT++ process must be sufficiently high (several, maybe several hundred megabytes), otherwise a segmentation fault will occur.

Public Member Functions

Constructor, destructor
bool setup (CoroutineFnp fnp, void *arg, unsigned stackSize)
 
 cCoroutine ()
 
virtual ~cCoroutine ()
 
Coroutine statistics
virtual bool hasStackOverflow () const
 
virtual unsigned getStackSize () const
 
virtual unsigned getStackUsage () const
 

Static Public Member Functions

Coroutine control
static void init (unsigned totalStack, unsigned mainStack)
 
static void switchTo (cCoroutine *cor)
 
static void switchToMain ()
 

Constructor & Destructor Documentation

◆ cCoroutine()

Constructor.

◆ ~cCoroutine()

virtual ~cCoroutine ( )
virtual

Destructor.

Member Function Documentation

◆ init()

static void init ( unsigned  totalStack,
unsigned  mainStack 
)
static

Initializes the coroutine library. This function has to be called exactly once in a program, possibly at the top of main().

◆ switchTo()

static void switchTo ( cCoroutine cor)
static

Switch to another coroutine. The execution of the current coroutine is suspended and the other coroutine is resumed from the point it last left off.

◆ switchToMain()

static void switchToMain ( )
static

Switch to the main coroutine (the one main() runs in).

◆ setup()

bool setup ( CoroutineFnp  fnp,
void *  arg,
unsigned  stackSize 
)

Sets up a coroutine. The arguments are the function that should be run in the coroutine, a pointer that is passed to the coroutine function, and the stack size.

◆ hasStackOverflow()

virtual bool hasStackOverflow ( ) const
virtual

Returns true if there was a stack overflow during execution of the coroutine.

Windows/Fiber API, POSIX coroutines: Not implemented: always returns false.

Portable coroutines: it checks the intactness of a predefined byte pattern (0xdeadbeef) at the stack boundary, and report stack overflow if it was overwritten. The mechanism usually works fine, but occasionally it can be fooled by large uninitialized local variables (e.g. char buffer[256]): if the byte pattern happens to fall in the middle of such a local variable, it may be preserved intact and stack violation is not detected.

◆ getStackSize()

virtual unsigned getStackSize ( ) const
virtual

Returns the stack size of the coroutine.

◆ getStackUsage()

virtual unsigned getStackUsage ( ) const
virtual

Returns the amount of stack actually used by the coroutine.

Windows/Fiber API, POSIX coroutines: Not implemented, always returns 0.

Portable coroutines: It works by checking the intactness of predefined byte patterns (0xdeadbeef) placed in the stack.


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