OliverKowalke2014Oliver Kowalke
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)
C++ Library for swiching different user ctx
ContextOverviewBoost.Context is a foundational library that
provides a sort of cooperative multitasking on a single thread. By providing
an abstraction of the current execution state in the current thread, including
the stack (with local variables) and stack pointer, all registers and CPU flags,
and the instruction pointer, a execution_context represents
a specific point in the application's execution path. This is useful for building
higher-level abstractions, like coroutines, cooperative
threads (userland threads) or an equivalent to C#
keyword yield in C++.
execution_context provides the means to suspend the current
execution path and to transfer execution control, thereby permitting another
context to run on the current thread. This state full transfer mechanism enables
a context to suspend execution from within nested functions and, later, to
resume from where it was suspended. While the execution path represented by
a execution_context only runs on a single thread, it can
be migrated to another thread at any given time.
A context switch between threads requires system calls (involving the OS kernel),
which can cost more than thousand CPU cycles on x86 CPUs. By contrast, transferring
control among them requires only few CPU cycles because it does not involve
system calls as it is done within a single thread.
In order to use the classes and functions described here, you can either include
the specific headers specified by the descriptions of each class or function,
or include the master library header:
#include<boost/context/all.hpp>
which includes all the other headers in turn.
All functions and classes are contained in the namespace boost::context.
execution_context requires C++11!
RequirementsBoost.Context must be built for the particular
compiler(s) and CPU architecture(s)s being targeted. Boost.Context
includes assembly code and, therefore, requires GNU as and GNU preprocesspr
for supported POSIX systems, MASM for Windows/x86 systems and ARMasm for Windows/arm
systems.
MASM64 (ml64.exe) is a part of Microsoft's Windows Driver Kit.
Please note that address-model=64 must be
given to bjam command line on 64bit Windows for 64bit build; otherwise 32bit
code will be generated.
For cross-compiling the lib you must specify certain additional properties
at bjam command line: target-os, abi, binary-format,
architecture and address-model.
For safe SEH the property 'asmflags=\safeseh' must be specified at bjam command
line.
Class execution_context
(version 2)
This class is enabled per default.
Class execution_context encapsulates context switching
and manages the associated context' stack (allocation/deallocation).
execution_context allocates the context stack (using its
StackAllocator argument)
and creates a control structure on top of it. This structure is responsible
for managing context' stack. The address of the control structure is stored
in the first frame of context' stack (e.g. it can not directly accessed from
within execution_context). In contrast to execution_context
(v1) the ownership of the control structure is not shared (no member
variable to control structure in execution_context).
execution_context keeps internally a state that is moved
by a call of execution_context::operator() (*this will be
invalidated), e.g. after a calling execution_context::operator(),
*this
can not be used for an additional context switch.
execution_context is only move-constructible and move-assignable.
The moved state is assigned to a new instance of execution_context.
This object becomes the first argument of the context-function, if the context
was resumed the first time, or the first element in a tuple returned by execution_context::operator()
that has been called in the resumed context. In contrast to execution_context
(v1), the context switch is faster because no global pointer etc. is
involved.
Segmented stacks are not supported by execution_context
(v2).
On return the context-function of the current context has to specify an execution_context
to which the execution control is transferred after termination of the current
context.
If an instance with valid state goes out of scope and the context-function
has not yet returned, the stack is traversed in order to access the control
structure (address stored at the first stack frame) and context' stack is deallocated
via the StackAllocator. The stack walking makes the destruction
of execution_context slow and should be prevented if possible.
execution_context expects a context-function
with signature execution_context(execution_contextctx,Args...args). The
parameter ctx represents the
context from which this context was resumed (e.g. that has called execution_context::operator()
on *this)
and args are the data passed
to execution_context::operator(). The return value represents
the execution_context that has to be resumed, after termiantion of this context.
Benefits of execution_context (v2)
over execution_context (v1)
are: faster context switch, type-safety of passed/returned arguments.
usage
of execution_contextintn=35;ctx::execution_context<int>source([n](ctx::execution_context<int>sink,int)mutable{inta=0;intb=1;while(n-->0){autoresult=sink(a);sink=std::move(std::get<0>(result));autonext=a+b;a=b;b=next;}returnsink;});for(inti=0;i<10;++i){autoresult=source(i);source=std::move(std::get<0>(result));std::cout<<std::get<1>(result)<<" ";}output:0112358132134
This simple example demonstrates the basic usage of execution_context
as a generator. The context sink
represents the main-context (function main()
running). sink is generated
by the framework (first element of lambda's parameter list). Because the state
is invalidated (== changed) by each call of execution_context::operator(),
the new state of the execution_context, returned by execution_context::operator(),
needs to be assigned to sink
after each call.
The lambda that calculates the Fibonacci numbers is executed inside the context
represented by source. Calculated
Fibonacci numbers are transferred between the two context' via expression
sink(a) (and returned by source()).
Note that this example represents a generator thus the
value transferred into the lambda via source() is not
used. Using boost::optional<> as transferred type,
might also appropriate to express this fact.
The locale variables a, b and next
remain their values during each context switch (yield(a)).
This is possible due source
has its own stack and the stack is exchanged by each context switch.
parameter
passing
With execution_context<void> no
data will be transferred, only the context switch is executed.
boost::context::execution_context<void>ctx1([](boost::context::execution_context<void>ctx2){std::printf("inside ctx1\n");returnctx2();});ctx1();output:insidectx1ctx1()
resumes ctx1, e.g. the lambda
passed at the constructor of ctx1
is entered. Argument ctx2 represents
the context that has been suspended with the invocation of ctx1(). When the lambda returns ctx2,
context ctx1 will be terminated
while the context represented by ctx2
is resumed, hence the control of execution returns from ctx1().
The arguments passed to execution_context::operator(),
in one context, is passed as the last arguments of the context-function
if the context is started for the first time. In all following invocations
of execution_context::operator() the arguments passed
to execution_context::operator(), in one context, is returned
by execution_context::operator() in the other context.
boost::context::execution_context<int>ctx1([](boost::context::execution_context<int>ctx2,intj){std::printf("inside ctx1, j == %d\n",j);returnctx2(j+1);});inti=1;std::tie(ctx1,i)=ctx1(i);std::printf("i == %d\n",i);output:insidectx1,j==1i==2ctx1(i) enters
the lambda in context ctx1
with argument j=1. The expression ctx2(j+1) resumes the
context represented by ctx2
and transfers back an integer of j+1. On return
of ctx1(i), the variable
i contains the value of j+1.
If more than one argument has to be transferred, the signature of the context-function
is simply extended.
boost::context::execution_context<int,int>ctx1([](boost::context::execution_context<int,int>ctx2,inti,intj){std::printf("inside ctx1, i == %d j == %d\n",i,j);returnctx2(i+j,i-j);});inti=2,j=1;std::tie(ctx1,i,j)=ctx1(i,j);std::printf("i == %d j == %d\n",i,j);output:insidectx1,i==2j==1i==3j==1
For use-cases, that require to transfer data of different type in each direction,
boost::variant<> could be used.
classX{private:std::exception_ptrexcptr_;boost::context::execution_context<boost::variant<int,std::string>>ctx_;public:X():excptr_(),ctx_([=](boost::context::execution_context<boost::variant<int,std::string>>ctx,boost::variant<int,std::string>data){try{for(;;){inti=boost::get<int>(data);data=boost::lexical_cast<std::string>(i);autoresult=ctx(data);ctx=std::move(std::get<0>(result));data=std::get<1>(result);}catch(std::bad_castconst&){excptr_=std::current_exception();}returnctx;}){}std::stringoperator()(inti){boost::variant<int,std::string>data=i;autoresult=ctx_(data);ctx_=std::move(std::get<0>(result));data=std::get<1>(result);if(excptr_){std::rethrow_exception(excptr_);}returnboost::get<std::string>(data);}};Xx;std::cout<<x(7)<<std::endl;output:7
In the case of unidirectional transfer of data, boost::optional<>
or a pointer are appropriate.
exception
handling
If the function executed inside a execution_context emits
ans exception, the application is terminated by calling std::terminate().
std::exception_ptr can be used to transfer exceptions
between different execution contexts.
Do not jump from inside a catch block and then re-throw the exception in
another execution context.
Executing
function on top of a context
Sometimes it is useful to execute a new function on top of a resumed context.
For this purpose execution_context::operator() with first
argument exec_ontop_arg has
to be used. The function passed as argument must return a tuple of execution_context
and arguments.
boost::context::execution_context<int>f1(boost::context::execution_context<int>ctx,intdata){std::cout<<"f1: entered first time: "<<data<<std::endl;std::tie(ctx,data)=ctx(data+1);std::cout<<"f1: entered second time: "<<data<<std::endl;std::tie(ctx,data)=ctx(data+1);std::cout<<"f1: entered third time: "<<data<<std::endl;returnctx;}std::tuple<boost::context::execution_context<int>,int>f2(boost::context::execution_context<int>ctx,intdata){std::cout<<"f2: entered: "<<data<<std::endl;returnstd::make_tuple(std::move(ctx),-1);}intdata=0;ctx::execution_context<int>ctx(f1);std::tie(ctx,data)=ctx(data+1);std::cout<<"f1: returned first time: "<<data<<std::endl;std::tie(ctx,data)=ctx(data+1);std::cout<<"f1: returned second time: "<<data<<std::endl;std::tie(ctx,data)=ctx(ctx::exec_ontop_arg,f2,data+1);output:f1:enteredfirsttime:1f1:returnedfirsttime:2f1:enteredsecondtime:3f1:returnedsecondtime:4f2:entered:5f1:enteredthirdtime:-1
The expression ctx(ctx::exec_ontop_arg,f2,data+1) executes f2() on top of context ctx,
e.g. an additional stack frame is allocated on top of the context stack (in
front of f1()).
f2()
returns argument -1
that will returned by the second invocation of ctx(data+1) in f1().
Another option is to execute a function on top of the context that throws an
exception.
structinterrupt{boost::context::execution_context<void>ctx;interrupt(boost::context::execution_context<void>&&ctx_):ctx(std::forward<boost::context::execution_context<void>>(ctx_)){}};boost::context::execution_context<void>f1(boost::context::execution_context<void>ctx){try{for(;;){std::cout<<"f1()"<<std::endl;ctx=ctx();}}catch(interrupt&e){std::cout<<"f1(): interrupted"<<std::endl;ctx=std::move(e.ctx);}returnctx;}boost::context::execution_context<void>f2(boost::context::execution_context<void>ctx){throwinterrupt(std::move(ctx));returnctx;}boost::context::execution_context<void>ctx(f1);ctx=ctx();ctx=ctx();ctx=ctx(boost::context::exec_ontop_arg,f2);output:f1()f1()f1():interrupted
In this example f2()
is used to interrupt the for-loop
in f1().
Stack
destruction
On construction of execution_context a stack is allocated.
If the context-function returns the stack will be destructed.
If the context-function has not yet returned and the destructor
of an valid execution_context instance (e.g. execution_context::operator
bool() returns true)
is called, the stack will be destructed too.
allocating
control structures on top of stack
Allocating control structures on top of the stack requires to allocated the
stack_context and create the control structure with placement
new before execution_context is created.
The user is responsible for destructing the control structure at the top
of the stack.
// stack-allocator used for (de-)allocating stackfixedsize_stacksalloc(4048);// allocate stack spacestack_contextsctx(salloc.allocate());// reserve space for control structure on top of the stackvoid*sp=static_cast<char*>(sctx.sp)-sizeof(my_control_structure);std::size_tsize=sctx.size-sizeof(my_control_structure);// placement new creates control structure on reserved spacemy_control_structure*cs=new(sp)my_control_structure(sp,size,sctx,salloc);...// destructing the control structurecs->~my_control_structure();...structmy_control_structure{// captured contextexecution_contextcctx;template<typenameStackAllocator>my_control_structure(void*sp,std::size_tsize,stack_contextsctx,StackAllocatorsalloc):// create captured contextcctx(std::allocator_arg,preallocated(sp,size,sctx),salloc,entry_func){}...};inverting
the control flow
/*
* grammar:
* P ---> E '\0'
* E ---> T {('+'|'-') T}
* T ---> S {('*'|'/') S}
* S ---> digit | '(' E ')'
*/classParser{// implementation omitted; see examples directory};std::istringstreamis("1+1");booldone=false;std::exception_ptrexcept;// execute parser in new execution contextboost::context::execution_context<char>source([&is,&done,&except](ctx::execution_context<char>sink,char){// create parser with callback functionParserp(is,[&sink](charch){// resume main execution contextautoresult=sink(ch);sink=std::move(std::get<0>(result));});try{// start recursive parsingp.run();}catch(...){// store other exceptions in exception-pointerexcept=std::current_exception();}// set termination flagdone=true;// resume main execution contextreturnsink;});// user-code pulls parsed data from parser// invert control flowautoresult=source('\0');source=std::move(std::get<0>(result));charc=std::get<1>(result);if(except){std::rethrow_exception(except);}while(!done){printf("Parsed: %c\n",c);std::tie(source,c)=source('\0');if(except){std::rethrow_exception(except);}}output:Parsed:1Parsed:+Parsed:1
In this example a recursive descent parser uses a callback to emit a newly
passed symbol. Using execution_context the control flow
can be inverted, e.g. the user-code pulls parsed symbols from the parser -
instead to get pushed from the parser (via callback).
The data (character) is transferred between the two execution_context.
If the code executed by execution_context emits an exception,
the application is terminated. std::exception_ptr can
be used to transfer exceptions between different execution contexts.
Sometimes it is necessary to unwind the stack of an unfinished context to destroy
local stack variables so they can release allocated resources (RAII pattern).
The user is responsible for this task.
Class
execution_contextstructexec_ontop_arg_t{};constexec_ontop_arg_texec_ontop_arg{};template<typename...Args>classexecution_context{public:template<typenameFn,typename...Params>execution_context(Fn&&fn,Params&&...params);template<typenameStackAlloc,typenameFn,typename...Params>execution_context(std::allocator_arg_t,StackAllocsalloc,Fn&&fn,Params&&...params);template<typenameStackAlloc,typenameFn,typename...Params>execution_context(std::allocator_arg_t,preallocatedpalloc,StackAllocsalloc,Fn&&fn,Params&&...params);template<typenameFn,typename...Params>execution_context(std::allocator_arg_t,segemented_stack,Fn&&fn,Params&&...params)=delete;template<typenameFn,typename...Params>execution_context(std::allocator_arg_t,preallocatedpalloc,segmented,Fn&&fn,Params&&...params)=delete;~execution_context();execution_context(execution_context&&other)noexcept;execution_context&operator=(execution_context&&other)noexcept;execution_context(execution_contextconst&other)noexcept=delete;execution_context&operator=(execution_contextconst&other)noexcept=delete;explicitoperatorbool()constnoexcept;booloperator!()constnoexcept;std::tuple<execution_context,Args...>operator()(Args...args);template<typenameFn>std::tuple<execution_context,Args...>operator()(exec_ontop_arg_t,Fn&&fn,Args...args);booloperator==(execution_contextconst&other)constnoexcept;booloperator!=(execution_contextconst&other)constnoexcept;booloperator<(execution_contextconst&other)constnoexcept;booloperator>(execution_contextconst&other)constnoexcept;booloperator<=(execution_contextconst&other)constnoexcept;booloperator>=(execution_contextconst&other)constnoexcept;template<typenamecharT,classtraitsT>friendstd::basic_ostream<charT,traitsT>&operator<<(std::basic_ostream<charT,traitsT>&os,execution_contextconst&other);};
Constructor
template<typenameFn,typename...Params>execution_context(Fn&&fn,Params&&...params);template<typenameStackAlloc,typenameFn,typename...Params>execution_context(std::allocator_arg_t,StackAllocsalloc,Fn&&fn,Params&&...params);template<typenameStackAlloc,typenameFn,typename...Params>execution_context(std::allocator_arg_t,preallocatedpalloc,StackAllocsalloc,Fn&&fn,Params&&...params);Effects:
Creates a new execution context and prepares the context to execute
fn. fixedsize_stack
is used as default stack allocator (stack size == fixedsize_stack::traits::default_size()).
The constructor with argument type preallocated,
is used to create a user defined data (for
instance additional control structures) on top of the stack.
Destructor
~execution_context();Effects:
Destructs the associated stack if *this is a valid context, e.g. execution_context::operator
bool() returns true.
Throws:
Nothing.
Move
constructor
execution_context(execution_context&&other)noexcept;Effects:
Moves underlying capture record to *this.
Throws:
Nothing.
Move
assignment operator
execution_context&operator=(execution_context&&other)noexcept;Effects:
Moves the state of other
to *this
using move semantics.
Throws:
Nothing.
Member function
operator bool()
explicitoperatorbool()constnoexcept;Returns:true if *this points to a capture record.
Throws:
Nothing.
Member function
operator!()
booloperator!()constnoexcept;Returns:true if *this does not point to a capture record.
Throws:
Nothing.
Member function
operator()()
std::tuple<execution_context<Args...>,Args...>operator()(Args...args);// member of generic execution_context templateexecution_context<void>operator()();// member of execution_context< void >Effects:
Stores internally the current context data (stack pointer, instruction
pointer, and CPU registers) of the current active context and restores
the context data from *this, which implies jumping to *this's
context. The arguments, ...args, are passed to the current context
to be returned by the most recent call to execution_context::operator() in the same thread.
Returns:
The tuple of execution_context and returned arguments passed to the most
recent call to execution_context::operator(), if any and a execution_context representing
the context that has been suspended.
Note:
The returned execution_context indicates if the suspended context has
terminated (return from context-function) via booloperator().
If the returned execution_context has terminated no data are transferred
in the returned tuple.
Member
function operator()()
template<typenameFn>std::tuple<execution_context<Args...>,Args...>operator()(exec_ontop_arg_t,Fn&&fn,Args...args);// member of generic execution_contexttemplate<typenameFn>execution_context<void>operator()(exec_ontop_arg_t,Fn&&fn);// member of execution_context< void >Effects:
Same as execution_context::operator(). Additionally,
function fn is executed
in the context of *this
(e.g. the stack frame of fn
is allocated on stack of *this).
Returns:
The tuple of execution_context and returned arguments passed to the most
recent call to execution_context::operator(), if any and a execution_context representing
the context that has been suspended .
Note:
The tuple of execution_context and returned arguments from fn are passed as arguments to the context-function
of resumed context (if the context is entered the first time) or those
arguments are returned from execution_context::operator() within the resumed context.
Note:
Function fn needs to
return a tuple of execution_context and arguments (see
description).
Note:
The context calling this function must not be destroyed before the arguments,
that will be returned from fn,
are preserved at least in the stack frame of the resumed context.
Note:
The returned execution_context indicates if the suspended context has
terminated (return from context-function) via booloperator().
If the returned execution_context has terminated no data are transferred
in the returned tuple.
Member
function operator==()
booloperator==(execution_contextconst&other)constnoexcept;Returns:true if *this and other
represent the same execution context, false
otherwise.
Throws:
Nothing.
Member
function operator!=()
booloperator!=(execution_contextconst&other)constnoexcept;Returns:! (other == * this)Throws:
Nothing.
Member function
operator<()
booloperator<(execution_contextconst&other)constnoexcept;Returns:true if *this!=other is true and the implementation-defined
total order of execution_context
values places *this
before other, false otherwise.
Throws:
Nothing.
Member
function operator>()
booloperator>(execution_contextconst&other)constnoexcept;Returns:other<*thisThrows:
Nothing.
Member
function operator<=()
booloperator<=(execution_contextconst&other)constnoexcept;Returns:!(other<*this)Throws:
Nothing.
Member
function operator>=()
booloperator>=(execution_contextconst&other)constnoexcept;Returns:!(*this<other)Throws:
Nothing.
Non-member function operator<<()template<typenamecharT,classtraitsT>std::basic_ostream<charT,traitsT>&operator<<(std::basic_ostream<charT,traitsT>&os,execution_contextconst&other);Efects:
Writes the representation of other
to stream os.
Returns:osClass execution_context
(version 1)
This class is only enabled if property segmented-stacks=on
(enables segmented stacks) or compiler flag BOOST_EXECUTION_CONTEXT=1
is specified at b2-commandline.
Class execution_context encapsulates context switching
and manages the associated context' stack (allocation/deallocation).
execution_context allocates the context stack (using its
StackAllocator argument)
and creates a control structure on top of it. This structure is responsible
for managing context' stack. Instances of execution_context,
associated with a specific context, share the ownership of the control structure.
If the last reference goes out of scope, the control structure is destroyed
and the stack gets deallocated via the StackAllocator.
execution_context is copy-constructible, move-constructible,
copy-assignable and move-assignable.
execution_context maintains a static (thread-local) pointer,
accessed by execution_context::current(), pointing to
the active context. On each context switch the pointer is updated. The usage
of this global pointer makes the context switch a little bit slower (due access
of thread local storage) but has some advantages. It allows to access the control
structure of the current active context from arbitrary code paths required
in order to support segmented stacks, which require to call certain maintenance
functions (like __splitstack_getcontext() etc.) before each context switch
(each context switch exchanges the stack).
execution_context expects a function/functor with signature
void(void*vp) (vp
is the data passed at the first invocation of ecv1::operator()()).
usage
of execution_contextintn=35;boost::context::execution_contextsink(boost::context::execution_context::current());boost::context::execution_contextsource([n,&sink](void*)mutable{inta=0;intb=1;while(n-->0){sink(&a);autonext=a+b;a=b;b=next;}});for(inti=0;i<10;++i){std::cout<<*(int*)source()<<" ";}output:0112358132134
This simple example demonstrates the basic usage of execution_context.
The context sink, returned
by execution_context::current(), represents the main-context
(function main() running) and is one of the captured parameters
in the lambda expression. The lambda that calculates the Fibonacci numbers
is executed inside the context represented by source.
Calculated Fibonacci numbers are transferred between the two context' via expression
sink(&a) (and returned by source()).
The locale variables a, b and next
remain their values during each context switch (yield(a)).
This is possible because ctx
owns a stack (exchanged by context switch).
inverting
the control flow
/*
* grammar:
* P ---> E '\0'
* E ---> T {('+'|'-') T}
* T ---> S {('*'|'/') S}
* S ---> digit | '(' E ')'
*/classParser{// implementation omitted; see examples directory};std::istringstreamis("1+1");booldone=false;std::exception_ptrexcept;// create handle to main execution contextautomain_ctx(boost::context::execution_context::current());// execute parser in new execution contextboost::context::execution_contextsource([&sink,&is,&done,&except](void*){// create parser with callback functionParserp(is,[&sink](charch){// resume main execution contextsink(&ch);});try{// start recursive parsingp.run();}catch(...){// store other exceptions in exception-pointerexcept=std::current_exception();}// set termination flagdone=true;// resume main execution contextsink();});// user-code pulls parsed data from parser// invert control flowvoid*vp=source();if(except){std::rethrow_exception(except);}while(!done){printf("Parsed: %c\n",*static_cast<char*>(vp));vp=source();if(except){std::rethrow_exception(except);}}output:Parsed:1Parsed:+Parsed:1
In this example a recursive descent parser uses a callback to emit a newly
passed symbol. Using execution_context the control flow
can be inverted, e.g. the user-code pulls parsed symbols from the parser -
instead to get pushed from the parser (via callback).
The data (character) is transferred between the two execution_context.
If the code executed by execution_context emits an exception,
the application is terminated. std::exception_ptr can
be used to transfer exceptions between different execution contexts.
Sometimes it is necessary to unwind the stack of an unfinished context to destroy
local stack variables so they can release allocated resources (RAII pattern).
The user is responsible for this task.
allocating
control structures on top of stack
Allocating control structures on top of the stack requires to allocated the
stack_context and create the control structure with placement
new before execution_context is created.
The user is responsible for destructing the control structure at the top
of the stack.
// stack-allocator used for (de-)allocating stackfixedsize_stacksalloc(4048);// allocate stack spacestack_contextsctx(salloc.allocate());// reserve space for control structure on top of the stackvoid*sp=static_cast<char*>(sctx.sp)-sizeof(my_control_structure);std::size_tsize=sctx.size-sizeof(my_control_structure);// placement new creates control structure on reserved spacemy_control_structure*cs=new(sp)my_control_structure(sp,size,sctx,salloc);...// destructing the control structurecs->~my_control_structure();...structmy_control_structure{// execution contextexecution_contextectx;template<typenameStackAllocator>my_control_structure(void*sp,std::size_tsize,stack_contextsctx,StackAllocatorsalloc):// create execution contextectx(std::allocator_arg,preallocated(sp,size,sctx),salloc,entry_func){}...};exception
handling
If the function executed inside a execution_context emits
ans exception, the application is terminated by calling std::terminate().
std::exception_ptr can be used to transfer exceptions
between different execution contexts.
Do not jump from inside a catch block and then re-throw the exception in
another execution context.
parameter
passing
The void pointer argument passed to execution_context::operator(),
in one context, is passed as the last argument of the context-function
if the context is started for the first time. In all following invocations
of execution_context::operator() the void pointer passed
to execution_context::operator(), in one context, is returned
by execution_context::operator() in the other context.
classX{private:std::exception_ptrexcptr_;boost::context::execution_contextcaller_;boost::context::execution_contextcallee_;public:X():excptr_(),caller_(boost::context::execution_context::current()),callee_([=](void*vp){try{inti=*static_cast<int*>(vp);std::stringstr=boost::lexical_cast<std::string>(i);caller_(&str);}catch(std::bad_castconst&){excptr_=std::current_exception();}}){}std::stringoperator()(inti){void*ret=callee_(&i);if(excptr_){std::rethrow_exception(excptr_);}return*static_cast<std::string*>(ret);}};Xx;std::cout<<x(7)<<std::endl;output:7Class
execution_contextclassexecution_context{public:staticexecution_contextcurrent()noexcept;template<typenameFn,typename...Args>execution_context(Fn&&fn,Args&&...args);template<typenameStackAlloc,typenameFn,typename...Args>execution_context(std::allocator_arg_t,StackAllocsalloc,Fn&&fn,Args&&...args);template<typenameStackAlloc,typenameFn,typename...Args>execution_context(std::allocator_arg_t,preallocatedpalloc,StackAllocsalloc,Fn&&fn,Args&&...args);execution_context(execution_contextconst&other)noexcept;execution_context(execution_context&&other)noexcept;execution_context&operator=(execution_contextconst&other)noexcept;execution_context&operator=(execution_context&&other)noexcept;explicitoperatorbool()constnoexcept;booloperator!()constnoexcept;void*operator()(void*vp=nullptr);template<typenameFn>void*operator()(exec_ontop_arg_t,Fn&&fn,void*vp=nullptr);booloperator==(execution_contextconst&other)constnoexcept;booloperator!=(execution_contextconst&other)constnoexcept;booloperator<(execution_contextconst&other)constnoexcept;booloperator>(execution_contextconst&other)constnoexcept;booloperator<=(execution_contextconst&other)constnoexcept;booloperator>=(execution_contextconst&other)constnoexcept;template<typenamecharT,classtraitsT>friendstd::basic_ostream<charT,traitsT>&operator<<(std::basic_ostream<charT,traitsT>&os,execution_contextconst&other);};
Static member function current()
staticexecution_contextcurrent()noexcept;Returns:
Returns an instance of excution_context pointing to the active execution
context.
Throws:
Nothing.
Constructor
template<typenameFn,typename...Args>execution_context(Fn&&fn,Args&&...args);template<typenameStackAlloc,typenameFn,typename...Args>execution_context(std::allocator_arg_t,StackAllocsalloc,Fn&&fn,Args&&...args);template<typenameStackAlloc,typenameFn,typename...Args>execution_context(std::allocator_arg_t,preallocatedpalloc,StackAllocsalloc,Fn&&fn,Args&&...args);Effects:
Creates a new execution context and prepares the context to execute
fn. fixedsize_stack
is used as default stack allocator (stack size == fixedsize_stack::traits::default_size()).
The constructor with argument type preallocated,
is used to create a user defined data (for
instance additional control structures) on top of the stack.
Copy
constructor
execution_context(execution_contextconst&other)noexcept;Effects:
Copies other, e.g. underlying
control structure is shared with *this.
Throws:
Nothing.
Move
constructor
execution_context(execution_context&&other)noexcept;Effects:
Moves underlying control structure to *this.
Throws:
Nothing.
Copy
assignment operator
execution_context&operator=(execution_contextconst&other)noexcept;Effects:
Copies the state of other
to *this,
control structure is shared.
Throws:
Nothing.
Move
assignment operator
execution_context&operator=(execution_context&&other)noexcept;Effects:
Moves the control structure of other
to *this
using move semantics.
Throws:
Nothing.
Member function
operator bool()
explicitoperatorbool()constnoexcept;Returns:true if *this points to a control structure.
Throws:
Nothing.
Member function
operator!()
booloperator!()constnoexcept;Returns:true if *this does not point to a control structure.
Throws:
Nothing.
Member function
operator()()
void*operator()(void*vp=nullptr)noexcept;Effects:
Stores internally the current context data (stack pointer, instruction
pointer, and CPU registers) of the current active context and restores
the context data from *this, which implies jumping to *this's
context. The void pointer argument, vp,
is passed to the current context to be returned by the most recent call
to execution_context::operator() in the same thread. fn
is executed with arguments args
on top of the stack of this.
Note:
The behaviour is undefined if operator()() is called while execution_context::current()
returns *this
(e.g. resuming an already running context). If the top-level context
function returns, std::exit() is called.
Returns:
The void pointer argument passed to the most recent call to execution_context::operator(),
if any.
Member
function operator(exec_ontop_arg_t)()
template<typenameFn>void*operator()(exec_ontop_arg_t,Fn&&fn,void*vp=nullptr);Effects:
Same as execution_context::operator(). Additionally,
function fn is executed
with arguments vp in
the context of *this
(e.g. the stack frame of fn
is allocated on stack of *this).
Returns:
The void pointer argument passed to the most recent call to execution_context::operator(),
if any.
Member
function operator==()
booloperator==(execution_contextconst&other)constnoexcept;Returns:true if *this and other
represent the same execution context, false
otherwise.
Throws:
Nothing.
Member
function operator!=()
booloperator!=(execution_contextconst&other)constnoexcept;Returns:! (other == * this)Throws:
Nothing.
Member function
operator<()
booloperator<(execution_contextconst&other)constnoexcept;Returns:true if *this!=other is true and the implementation-defined
total order of execution_context
values places *this
before other, false otherwise.
Throws:
Nothing.
Member
function operator>()
booloperator>(execution_contextconst&other)constnoexcept;Returns:other<*thisThrows:
Nothing.
Member
function operator<=()
booloperator<=(execution_contextconst&other)constnoexcept;Returns:!(other<*this)Throws:
Nothing.
Member
function operator>=()
booloperator>=(execution_contextconst&other)constnoexcept;Returns:!(*this<other)Throws:
Nothing.
Non-member function operator<<()template<typenamecharT,classtraitsT>std::basic_ostream<charT,traitsT>&operator<<(std::basic_ostream<charT,traitsT>&os,execution_contextconst&other);Efects:
Writes the representation of other
to stream os.
Returns:osStack allocation
The memory used by the stack is allocated/deallocated via a StackAllocator
which is required to model a stack-allocator concept.
stack-allocator
concept
A StackAllocator must satisfy the stack-allocator
concept requirements shown in the following table, in which a is an object of a StackAllocator
type, sctx is a stack_context, and size
is a std::size_t:
expression
return type
notes
a(size)
creates a stack allocator
a.allocate()stack_context
creates a stack
a.deallocate(sctx)void
deallocates the stack created by a.allocate()
The implementation of allocate() might include logic to protect against
exceeding the context's available stack size rather than leaving it as undefined
behaviour.
Calling deallocate()
with a stack_context not
set by allocate()
results in undefined behaviour.
The stack is not required to be aligned; alignment takes place inside execution_context.
Depending on the architecture allocate() stores an address from the top of the stack
(growing downwards) or the bottom of the stack (growing upwards).
Class protected_fixedsizeBoost.Context provides the class protected_fixedsize_stack
which models the stack-allocator concept. It appends
a guard page at the end of each stack to protect against exceeding the stack.
If the guard page is accessed (read or write operation) a segmentation fault/access
violation is generated by the operating system.
Using protected_fixedsize_stack is expensive. That
is, launching a new coroutine with a new stack is expensive; the allocated
stack is just as efficient to use as any other stack.
The appended guardpage
is not mapped to physical memory, only
virtual addresses are used.
#include<boost/context/protected_fixedsize.hpp>template<typenametraitsT>structbasic_protected_fixedsize{typedeftraitTtraits_type;basic_protected_fixesize(std::size_tsize=traits_type::default_size());stack_contextallocate();voiddeallocate(stack_context&);}typedefbasic_protected_fixedsize<stack_traits>protected_fixedsizestack_contextallocate()Preconditions:traits_type::minimum:size()<=size
and !traits_type::is_unbounded()&&(traits_type::maximum:size()>=size).
Effects:
Allocates memory of at least size
Bytes and stores a pointer to the stack and its actual size in sctx. Depending on the architecture
(the stack grows downwards/upwards) the stored address is the highest/lowest
address of the stack.
voiddeallocate(stack_context&sctx)Preconditions:sctx.sp is valid, traits_type::minimum:size()<=sctx.size and !traits_type::is_unbounded()&&(traits_type::maximum:size()>=sctx.size).
Effects:
Deallocates the stack space.
Class pooled_fixedsize_stackBoost.Context provides the class pooled_fixedsize_stack
which models the stack-allocator concept. In contrast
to protected_fixedsize_stack it does not append a guard
page at the end of each stack. The memory is managed internally by boost::pool<>.
#include<boost/context/pooled_fixedsize_stack.hpp>template<typenametraitsT>structbasic_pooled_fixedsize_stack{typedeftraitTtraits_type;basic_pooled_fixedsize_stack(std::size_tstack_size=traits_type::default_size(),std::size_tnext_size=32,std::size_tmax_size=0);stack_contextallocate();voiddeallocate(stack_context&);}typedefbasic_pooled_fixedsize_stack<stack_traits>pooled_fixedsize_stack;basic_pooled_fixedsize_stack(std::size_tstack_size,std::size_tnext_size,std::size_tmax_size)Preconditions:!traits_type::is_unbounded()&&(traits_type::maximum:size()>=stack_size)
and 0<nest_size.
Effects:
Allocates memory of at least stack_size
Bytes and stores a pointer to the stack and its actual size in sctx. Depending on the architecture
(the stack grows downwards/upwards) the stored address is the highest/lowest
address of the stack. Argument next_size
determines the number of stacks to request from the system the first
time that *this
needs to allocate system memory. The third argument max_size
controls how many memory might be allocated for stacks - a value of
zero means no uper limit.
stack_contextallocate()Preconditions:!traits_type::is_unbounded()&&(traits_type::maximum:size()>=stack_size).
Effects:
Allocates memory of at least stack_size
Bytes and stores a pointer to the stack and its actual size in sctx. Depending on the architecture
(the stack grows downwards/upwards) the stored address is the highest/lowest
address of the stack.
voiddeallocate(stack_context&sctx)Preconditions:sctx.sp is valid, !traits_type::is_unbounded()&&(traits_type::maximum:size()>=sctx.size).
Effects:
Deallocates the stack space.
Class fixedsize_stackBoost.Context provides the class fixedsize_stack
which models the stack-allocator concept. In contrast
to protected_fixedsize_stack it does not append a guard
page at the end of each stack. The memory is simply managed by std::malloc() and std::free().
#include<boost/context/fixedsize_stack.hpp>template<typenametraitsT>structbasic_fixedsize_stack{typedeftraitTtraits_type;basic_fixesize_stack(std::size_tsize=traits_type::default_size());stack_contextallocate();voiddeallocate(stack_context&);}typedefbasic_fixedsize_stack<stack_traits>fixedsize_stack;stack_contextallocate()Preconditions:traits_type::minimum:size()<=size
and !traits_type::is_unbounded()&&(traits_type::maximum:size()>=size).
Effects:
Allocates memory of at least size
Bytes and stores a pointer to the stack and its actual size in sctx. Depending on the architecture
(the stack grows downwards/upwards) the stored address is the highest/lowest
address of the stack.
voiddeallocate(stack_context&sctx)Preconditions:sctx.sp is valid, traits_type::minimum:size()<=sctx.size and !traits_type::is_unbounded()&&(traits_type::maximum:size()>=sctx.size).
Effects:
Deallocates the stack space.
Class segmented_stackBoost.Context supports usage of a segmented_stack,
e. g. the size of the stack grows on demand. The coroutine is created with
a minimal stack size and will be increased as required. Class segmented_stack
models the stack-allocator concept. In contrast to
protected_fixedsize_stack and fixedsize_stack
it creates a stack which grows on demand.
Segmented stacks are currently only supported by gcc
from version 4.7clang
from version 3.4 onwards. In order to
use a __segmented_stack__ Boost.Context
must be built with property segmented-stacks,
e.g. toolset=gcc segmented-stacks=on at
b2/bjam command line.
#include<boost/context/segmented_stack.hpp>template<typenametraitsT>structbasic_segmented_stack{typedeftraitTtraits_type;basic_segmented_stack(std::size_tsize=traits_type::default_size());stack_contextallocate();voiddeallocate(stack_context&);}typedefbasic_segmented_stack<stack_traits>segmented_stack;stack_contextallocate()Preconditions:traits_type::minimum:size()<=size
and !traits_type::is_unbounded()&&(traits_type::maximum:size()>=size).
Effects:
Allocates memory of at least size
Bytes and stores a pointer to the stack and its actual size in sctx. Depending on the architecture
(the stack grows downwards/upwards) the stored address is the highest/lowest
address of the stack.
voiddeallocate(stack_context&sctx)Preconditions:sctx.sp is valid, traits_type::minimum:size()<=sctx.size and !traits_type::is_unbounded()&&(traits_type::maximum:size()>=sctx.size).
Effects:
Deallocates the stack space.
If the library is compiled for segmented stacks, __segmented_stack__ is
the only available stack allocator.
Class stack_traitsstack_traits models a stack-traits
providing a way to access certain properites defined by the enironment. Stack
allocators use stack-traits to allocate stacks.
#include<boost/context/stack_traits.hpp>structstack_traits{staticboolis_unbounded()noexcept;staticstd::size_tpage_size()noexcept;staticstd::size_tdefault_size()noexcept;staticstd::size_tminimum_size()noexcept;staticstd::size_tmaximum_size()noexcept;}staticboolis_unbounded()Returns:
Returns true if the environment
defines no limit for the size of a stack.
Throws:
Nothing.
staticstd::size_tpage_size()Returns:
Returns the page size in bytes.
Throws:
Nothing.
staticstd::size_tdefault_size()Returns:
Returns a default stack size, which may be platform specific. If the
stack is unbounded then the present implementation returns the maximum
of 64kB
and minimum_size().
Throws:
Nothing.
staticstd::size_tminimum_size()Returns:
Returns the minimum size in bytes of stack defined by the environment
(Win32 4kB/Win64 8kB, defined by rlimit on POSIX).
Throws:
Nothing.
staticstd::size_tmaximum_size()Preconditions:is_unbounded()
returns false.
Returns:
Returns the maximum size in bytes of stack defined by the environment.
Throws:
Nothing.
Class stack_contextBoost.Context provides the class stack_context
which will contain the stack pointer and the size of the stack. In case of
a segmented_stack, stack_context
contains some extra control structures.
structstack_context{void*sp;std::size_tsize;// might contain additional control structures// for segmented stacks}void*spValue:
Pointer to the beginning of the stack.
std::size_tsizeValue:
Actual size of the stack.
Support for valgrind
Running programs that switch stacks under valgrind causes problems. Property
(b2 command-line) valgrind=on let
valgrind treat the memory regions as stack space which suppresses the errors.
Struct preallocatedstructpreallocated{void*sp;std::size_tsize;stack_contextsctx;preallocated(void*sp,std:size_tsize,stack_allocatorsctx)noexcept;};Constructor
preallocated(void*sp,std:size_tsize,stack_allocatorsctx)noexcept;Effects:
Creates an object of preallocated.
Performance
Performance of Boost.Context was measured
on the platforms shown in the following table. Performance measurements were
taken using rdtsc and boost::chrono::high_resolution_clock,
with overhead corrections, on x86 platforms. In each case, cache warm-up was
accounted for, and the one running thread was pinned to a single CPU. The code
was compiled using the build options, 'variant = release cxxflags = -DBOOST_DISABLE_ASSERTS'.
Cross compiling
Cross compiling the library requires to specify the build properties <architecture>,
<address-model>, <binary-format> and <abi> at b2 command
line.
RationaleNo
inline-assembler
Some newer compiler (for instance MSVC 10 for x86_64 and itanium) do not support
inline assembler. MSDN article
'Inline Assembler'. Inlined assembler generates code bloating which is not welcome
on embedded systems.
fcontext_t
Boost.Context provides the low level API fcontext_t
which is implemented in assembler to provide context swapping operations. fcontext_t
is the part to port to new platforms.
Context switches do not preserve the signal mask on UNIX systems.
fcontext_t is an opaque pointer.
Other APIs setjmp()/longjmp()
C99 defines setjmp()/longjmp()
to provide non-local jumps but it does not require that longjmp()
preserves the current stack frame. Therefore, jumping into a function which
was exited via a call to longjmp() is undefined
ISO/IEC 9899:1999, 2005, 7.13.2.1:2
.
ucontext_t
Since POSIX.1-2003 ucontext_t
is deprecated and was removed in POSIX.1-2008! The function signature of
makecontext()
is:
voidmakecontext(ucontext_t*ucp,void(*func)(),intargc,...);
The third argument of makecontext() specifies the number of integer arguments
that follow which will require function pointer cast if func
will accept those arguments which is undefined in C99
ISO/IEC 9899:1999, 2005, J.2
.
The arguments in the var-arg list are required to be integers, passing pointers
in var-arg list is not guaranteed to work, especially it will fail for architectures
where pointers are larger than integers.
ucontext_t preserves signal
mask between context switches which involves system calls consuming a lot
of CPU cycles (ucontext_t is slower by perfomance_link[factor 13x] relative
to fcontext_t).
Windows
fibers
A drawback of Windows Fiber API is that CreateFiber() does not accept a pointer to user allocated
stack space preventing the reuse of stacks for other context instances. Because
the Windows Fiber API requires to call ConvertThreadToFiber() if SwitchFiber() is called for a thread which has not been
converted to a fiber. For the same reason ConvertFiberToThread() must be called after return from SwitchFiber()
if the thread was forced to be converted to a fiber before (which is inefficient).
if(!is_a_fiber()){ConvertThreadToFiber(0);SwitchToFiber(ctx);ConvertFiberToThread();}
If the condition _WIN32_WINNT>=_WIN32_WINNT_VISTA
is met function IsThreadAFiber() is provided in order to detect if the current
thread was already converted. Unfortunately Windows XP + SP 2/3 defines
_WIN32_WINNT>=_WIN32_WINNT_VISTA without providing
IsThreadAFiber().
x86 and
floating-point envi386
"The FpCsr and the MxCsr register must be saved and restored before
any call or return by any procedure that needs to modify them ..."
'Calling Conventions', Agner Fog
.
x86_64
Windows
MxCsr - "A callee that modifies any of the non-volatile fields within
MxCsr must restore them before returning to its caller. Furthermore, a caller
that has modified any of these fields must restore them to their standard
values before invoking a callee ..." MSDN
article 'MxCsr'.
FpCsr - "A callee that modifies any of the fields within FpCsr must
restore them before returning to its caller. Furthermore, a caller that has
modified any of these fields must restore them to their standard values before
invoking a callee ..." MSDN
article 'FpCsr'.
"The MMX and floating-point stack registers (MM0-MM7/ST0-ST7) are preserved
across context switches. There is no explicit calling convention for these
registers." MSDN
article 'Legacy Floating-Point Support'.
"The 64-bit Microsoft compiler does not use ST(0)-ST(7)/MM0-MM7".
'Calling Conventions', Agner Fog
.
"XMM6-XMM15 must be preserved" MSDN
article 'Register Usage'SysV
"The control bits of the MxCsr register are callee-saved (preserved
across calls), while the status bits are caller-saved (not preserved). The
x87 status word register is caller-saved, whereas the x87 control word (FpCsr)
is callee-saved."
SysV ABI AMD64 Architecture Processor Supplement Draft Version 0.99.4,
3.2.1
.
ReferenceARM
AAPCS ABI: Procedure Call Standard for the ARM Architecture
AAPCS/LINUX: ARM GNU/Linux Application Binary Interface Supplement
MIPS
O32 ABI: SYSTEM V APPLICATION BINARY INTERFACE, MIPS RISC Processor Supplement
PowerPC32
SYSV ABI: SYSTEM V APPLICATION BINARY INTERFACE PowerPC Processor Supplement
PowerPC64
SYSV ABI: PowerPC User Instruction Set Architecture, Book I
X86-32
SYSV ABI: SYSTEM V APPLICATION BINARY INTERFACE, Intel386TM Architecture
Processor Supplement
MS PE: Calling
ConventionsX86-64
SYSV ABI: System V Application Binary Interface, AMD64 Architecture Processor
Supplement
MS PE: x64
Software ConventionsAcknowledgments
I'd like to thank Adreas Fett, Artyom Beilis, Daniel Larimer, David Deakins,
Evgeny Shapovalov, Fernando Pelliccioni, Giovanni Piero Deretta, Gordon Woodhull,
Helge Bahmann, Holger Grund, Jeffrey Lee Hellrung (Jr.), Keith Jeffery, Martin
Husemann, Phil Endecott, Robert Stewart, Sergey Cheban, Steven Watanabe, Vicente
J. Botet Escriba, Wayne Piekarski.