Low-level coroutine library. More...
#include <ccoroutine.h>
Public Member Functions | |
Constructor, destructor | |
bool | setup (CoroutineFnp fnp, void *arg, unsigned stack_size) |
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 total_stack, unsigned main_stack) |
static void | switchTo (cCoroutine *cor) |
static void | switchToMain () |
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.
virtual unsigned cCoroutine::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.
virtual bool cCoroutine::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.
static void cCoroutine::init | ( | unsigned | total_stack, | |
unsigned | main_stack | |||
) | [static] |
Initializes the coroutine library.
This function has to be called exactly once in a program, possibly at the top of main().
bool cCoroutine::setup | ( | CoroutineFnp | fnp, | |
void * | arg, | |||
unsigned | stack_size | |||
) |
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.
static void cCoroutine::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.