- Setcontext
setcontext is one of a family of C library functions (the others being getcontext, makecontext and swapcontext) used for context control. The
setcontext
family allows the implementation in C of advancedcontrol flow patterns such asiterator s, fibers, andcoroutine s. They may be viewed as an advanced version ofsetjmp/longjmp ; whereas the latter allows only a single non-local jump up the stack,setcontext
allows the creation of multiple cooperative threads of control, each with its own stack.pecification
setcontext
is specified inPOSIX .1-2001 and theSingle Unix Specification , version 2, but not allUnix-like operating system s provide them. The functions and associated types are defined in theucontext.h
systemheader file . This includes theucontext_t
type, with which all four functions operate:uc_link
points to the context which will be resumed when the current context exits, if the context was created withmakecontext
(a secondary context).uc_sigmask
is used to store the set of signals blocked in the context, anduc_stack
is the stack used by the context.uc_mcontext
stores execution state, including allregister s and CPU flags, theinstruction pointer , and thestack pointer ;mcontext_t
is an opaque type.The functions are:
*int setcontext(const ucontext_t *ucp)
:This function transfers control to the context inucp
. Execution continues from the point at which the context was stored inucp
.setcontext
does not return.
*int getcontext(ucontext_t *ucp)
:Saves current context intoucp
. This function returns in two possible cases: after the initial call, or when a thread switches to the context inucp
viasetcontext
orswapcontext
. Thegetcontext
function does not provide areturn value to distinguish the cases (its return value is used solely to signal error), so the programmer must use an explicit flag variable, which must not be a register variable and must be declared volatile to avoidconstant propagation or othercompiler optimisation s.
*void makecontext(ucontext_t *ucp, void *func(), int argc, ...)
:Themakecontext
function sets up an alternate thread of control inucp
, which has previously been initialised usinggetcontext
. Theucp.uc_stack
member should be pointed to an appropriately sized stack; the constantSIGSTKSZ
is commonly used. Whenucp
is jumped to usingsetcontext
orswapcontext
, execution will begin at theentry point to the function pointed to byfunc
, withargc
arguments as specified. Whenfunc
terminates, control is returned toucp.uc_link
.
*int swapcontext(ucontext_t *oucp, ucontext_t *ucp)
:Transfers control toucp
and saves the current execution state intooucp
.Example
The example below demonstrates an iterator using
setcontext
. This form of example is unlikely to be widely seen; assetcontext
is somewhat cumbersome to use effectively, programmers writing cooperatively multitasked applications often choose to use a wrapper library such asGNU Portable Threads . Most code usingsetcontext
appears in such wrapper libraries, inhigh-level programming language implementations, or inemulator s.External links
* [http://www.gnu.org/software/libc/manual/html_mono/libc.html#System-V-contexts System V Contexts] - The
GNU C Library Manual
*
* [http://www.freebsd.org/cgi/man.cgi?query=setcontext&manpath=FreeBSD+7.0-RELEASE setcontext - get/set current user context] FreeBSD man page.
Wikimedia Foundation. 2010.