[/ Copyright Oliver Kowalke 2014. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt ] [section:context Struct fcontext_t] Each instance of __fcontext__ represents a context (CPU registers and stack space). Together with its related functions __jump_fcontext__ and __make_fcontext__ it provides a execution control transfer mechanism similar interface like [@http://www.kernel.org/doc/man-pages/online/pages/man2/getcontext.2.html ucontext_t]. __fcontext__ and its functions are located in __context_ns__ and the functions are declared as extern "C". [warning If __fcontext__ is used in a multi threaded application, it can migrated between threads, but must not reference __tls__.] [important The low level API is the part to port to new platforms.] [note If __fls__ is used on Windows, the user is responsible for calling __fls_alloc__, __fls_free__.] [heading Executing a context] A new context supposed to execute a __context_fn__ (returning void and accepting void * as argument) will be created on top of the stack (at 16 byte boundary) by function __make_fcontext__. // context-function void f(intptr); // creates a new stack std::size_t size = 8192; void* sp(std::malloc(size)); // context fc uses f() as context function // fcontext_t is placed on top of context stack // a pointer to fcontext_t is returned fcontext_t fc(make_fcontext(sp,size,f)); Calling __jump_fcontext__ invokes the __context_fn__ in a newly created context complete with registers, flags, stack and instruction pointers. When control should be returned to the original calling context, call __jump_fcontext__. The current context information (registers, flags, and stack and instruction pointers) is saved and the original context information is restored. Calling __jump_fcontext__ again resumes execution in the second context after saving the new state of the original context. boost::context::fcontext_t fcm,fc1,fc2; void f1(void *) { std::cout<<"f1: entered"< pair_t; void f(void * param) { pair_t* p=(pair_t*)param; p=(pair_t*)boost::context::jump_fcontext(&fc,fcm,(void *)(p->first+p->second)); boost::context::jump_fcontext(&fc,fcm,(void *)(p->first+p->second)); } std::size_t size(8192); void* sp(std::malloc(size)); pair_t p(std::make_pair(2,7)); fc=boost::context::make_fcontext(sp,size,f); int res=(int)boost::context::jump_fcontext(&fcm,fc,(void *)&p); std::cout< fcontext_t; void * jump_fcontext(fcontext_t* ofc,fcontext_t nfc,void * vp); fcontext_t make_fcontext(void* sp,std::size_t size,void(*fn)(void *)); [heading `sp`] [variablelist [[Member:] [Pointer to the beginning of the stack (depending of the architecture the stack grows downwards or upwards).]] ] [heading `size`] [variablelist [[Member:] [Size of the stack in bytes.]] ] [heading `fc_stack`] [variablelist [[Member:] [Tracks the memory for the context's stack.]] ] [heading `void * jump_fcontext(fcontext_t* ofc,fcontext_t nfc,void * p)`] [variablelist [[Effects:] [Stores the current context data (stack pointer, instruction pointer, and CPU registers) to `*ofc` and restores the context data from `nfc`, which implies jumping to execution context `nfc`. The void * argument, `p`, is passed to the current context to be returned by the most recent call to `jump_fcontext()` in the same thread.]] [[Returns:] [The third pointer argument passed to the most recent call to `jump_fcontext()`, if any.]] ] [heading `fcontext_t make_fcontext(void* sp,std::size_t size,void(*fn)(void *))`] [variablelist [[Precondition:] [Stack `sp` and function pointer `fn` are valid (depending on the architecture `sp` points to the top or bottom of the stack) and `size` > 0.]] [[Effects:] [Creates an fcontext_t on top of the stack and prepares the stack to execute the __context_fn__ `fn`.]] [[Returns:][Returns a fcontext_t which is placed on the stack.]] ] [endsect]