<s>
setcontext	B-Operating_System
is	O
one	O
of	O
a	O
family	O
of	O
C	B-Language
library	B-Library
functions	I-Library
(	O
the	O
others	O
being	O
getcontext	B-Operating_System
,	O
makecontext	B-Operating_System
and	O
swapcontext	B-Operating_System
)	O
used	O
for	O
context	B-Operating_System
control	O
.	O
</s>
<s>
The	O
setcontext	B-Operating_System
family	O
allows	O
the	O
implementation	O
in	O
C	B-Language
of	O
advanced	O
control	O
flow	O
patterns	O
such	O
as	O
iterators	O
,	O
fibers	B-Operating_System
,	O
and	O
coroutines	B-Architecture
.	O
</s>
<s>
They	O
may	O
be	O
viewed	O
as	O
an	O
advanced	O
version	O
of	O
setjmp/longjmp	B-Language
;	O
whereas	O
the	O
latter	O
allows	O
only	O
a	O
single	O
non-local	O
jump	O
up	O
the	O
stack	B-General_Concept
,	O
setcontext	B-Operating_System
allows	O
the	O
creation	O
of	O
multiple	O
cooperative	O
threads	B-Operating_System
of	I-Operating_System
control	I-Operating_System
,	O
each	O
with	O
its	O
own	O
stack	B-General_Concept
.	O
</s>
<s>
setcontext	B-Operating_System
was	O
specified	O
in	O
POSIX.1-2001	O
and	O
the	O
Single	O
Unix	O
Specification	O
,	O
version	O
2	O
,	O
but	O
not	O
all	O
Unix-like	B-Operating_System
operating	I-Operating_System
systems	I-Operating_System
provide	O
them	O
.	O
</s>
<s>
POSIX.1-2004	O
obsoleted	O
these	O
functions	O
,	O
and	O
in	O
POSIX.1-2008	O
they	O
were	O
removed	O
,	O
with	O
POSIX	B-Operating_System
Threads	I-Operating_System
indicated	O
as	O
a	O
possible	O
replacement	O
.	O
</s>
<s>
Citing	O
IEEE	O
Std	O
1003.1	O
,	O
2004	O
Edition	O
:	O
With	O
the	O
incorporation	O
of	O
the	O
ISO/IEC	O
9899:1999	O
standard	O
into	O
this	O
specification	O
it	O
was	O
found	O
that	O
the	O
ISO	O
C	B-Language
standard	O
(	O
Subclause	O
6.11.6	O
)	O
specifies	O
that	O
the	O
use	O
of	O
function	O
declarators	O
with	O
empty	O
parentheses	O
is	O
an	O
obsolescent	O
feature	O
.	O
</s>
<s>
void	O
makecontext(ucontext_t	O
*	O
ucp	O
,	O
void	O
( *	O
func	O
)	O
(	O
)	O
,	O
int	O
argc	O
,	O
...	O
)	O
;	O
</s>
<s>
is	O
making	O
use	O
of	O
an	O
obsolescent	O
feature	O
of	O
the	O
ISO	O
C	B-Language
standard	O
.	O
</s>
<s>
Therefore	O
,	O
use	O
of	O
getcontext( )	O
,	O
makecontext( )	O
,	O
and	O
swapcontext( )	O
is	O
marked	O
obsolescent	O
.	O
</s>
<s>
There	O
is	O
no	O
way	O
in	O
the	O
ISO	O
C	B-Language
standard	O
to	O
specify	O
a	O
non-obsolescent	O
function	O
prototype	O
indicating	O
that	O
a	O
function	O
will	O
be	O
called	O
with	O
an	O
arbitrary	O
number	O
(	O
including	O
zero	O
)	O
of	O
arguments	O
of	O
arbitrary	O
types	O
(	O
including	O
integers	O
,	O
pointers	O
to	O
data	O
,	O
pointers	O
to	O
functions	O
,	O
and	O
composite	O
types	O
)	O
.	O
</s>
<s>
The	O
functions	O
and	O
associated	O
types	O
are	O
defined	O
in	O
the	O
ucontext.h	O
system	O
header	B-Language
file	I-Language
.	O
</s>
<s>
uc_link	O
points	O
to	O
the	O
context	B-Operating_System
which	O
will	O
be	O
resumed	O
when	O
the	O
current	O
context	B-Operating_System
exits	O
,	O
if	O
the	O
context	B-Operating_System
was	O
created	O
with	O
makecontext	B-Operating_System
(	O
a	O
secondary	O
context	B-Operating_System
)	O
.	O
</s>
<s>
uc_sigmask	O
is	O
used	O
to	O
store	O
the	O
set	O
of	O
signals	O
blocked	O
in	O
the	O
context	B-Operating_System
,	O
and	O
uc_stack	O
is	O
the	O
stack	B-General_Concept
used	O
by	O
the	O
context	B-Operating_System
.	O
</s>
<s>
uc_mcontext	O
stores	O
execution	B-General_Concept
state	B-Application
,	O
including	O
all	O
registers	B-General_Concept
and	O
CPU	B-General_Concept
flags	O
,	O
the	O
instruction	B-General_Concept
pointer	I-General_Concept
,	O
and	O
the	O
stack	B-General_Concept
pointer	O
;	O
mcontext_t	O
is	O
an	O
opaque	O
type	O
.	O
</s>
<s>
This	O
function	O
transfers	O
control	O
to	O
the	O
context	B-Operating_System
in	O
ucp	O
.	O
</s>
<s>
Execution	B-General_Concept
continues	O
from	O
the	O
point	O
at	O
which	O
the	O
context	B-Operating_System
was	O
stored	O
in	O
ucp	O
.	O
</s>
<s>
setcontext	B-Operating_System
does	O
not	O
return	O
.	O
</s>
<s>
Saves	O
current	O
context	B-Operating_System
into	O
ucp	O
.	O
</s>
<s>
This	O
function	O
returns	O
in	O
two	O
possible	O
cases	O
:	O
after	O
the	O
initial	O
call	O
,	O
or	O
when	O
a	O
thread	B-Operating_System
switches	O
to	O
the	O
context	B-Operating_System
in	O
ucp	O
via	O
setcontext	B-Operating_System
or	O
swapcontext	B-Operating_System
.	O
</s>
<s>
The	O
getcontext	B-Operating_System
function	O
does	O
not	O
provide	O
a	O
return	B-Language
value	I-Language
to	O
distinguish	O
the	O
cases	O
(	O
its	O
return	B-Language
value	I-Language
is	O
used	O
solely	O
to	O
signal	B-Operating_System
error	O
)	O
,	O
so	O
the	O
programmer	O
must	O
use	O
an	O
explicit	O
flag	O
variable	O
,	O
which	O
must	O
not	O
be	O
a	O
register	O
variable	O
and	O
must	O
be	O
declared	O
volatile	B-Operating_System
to	O
avoid	O
constant	O
propagation	O
or	O
other	O
compiler	B-Application
optimizations	I-Application
.	O
</s>
<s>
The	O
makecontext	B-Operating_System
function	O
sets	O
up	O
an	O
alternate	O
thread	B-Operating_System
of	O
control	O
in	O
ucp	O
,	O
which	O
has	O
previously	O
been	O
initialised	O
using	O
getcontext	B-Operating_System
.	O
</s>
<s>
The	O
ucp.uc_stack	O
member	O
should	O
be	O
pointed	O
to	O
an	O
appropriately	O
sized	O
stack	B-General_Concept
;	O
the	O
constant	O
SIGSTKSZ	O
is	O
commonly	O
used	O
.	O
</s>
<s>
When	O
ucp	O
is	O
jumped	O
to	O
using	O
setcontext	B-Operating_System
or	O
swapcontext	B-Operating_System
,	O
execution	B-General_Concept
will	O
begin	O
at	O
the	O
entry	B-Language
point	I-Language
to	O
the	O
function	O
pointed	O
to	O
by	O
func	O
,	O
with	O
argc	O
arguments	O
as	O
specified	O
.	O
</s>
<s>
Transfers	O
control	O
to	O
ucp	O
and	O
saves	O
the	O
current	O
execution	B-General_Concept
state	B-Application
into	O
oucp	O
.	O
</s>
<s>
The	O
example	O
below	O
demonstrates	O
an	O
iterator	O
using	O
setcontext	B-Operating_System
.	O
</s>
<s>
The	O
function	O
makecontext	B-Operating_System
requires	O
additional	O
parameters	O
to	O
be	O
type	O
int	O
,	O
but	O
the	O
example	O
passes	O
pointers	O
.	O
</s>
<s>
For	O
get	O
and	O
set	O
context	B-Operating_System
,	O
a	O
smaller	O
context	B-Operating_System
can	O
be	O
handy	O
:	O
</s>
<s>
This	O
makes	O
an	O
infinite	O
loop	O
because	O
context	B-Operating_System
holds	O
the	O
program	B-General_Concept
counter	I-General_Concept
.	O
</s>
