<s>
Coroutines	B-Architecture
are	O
computer	B-Application
program	I-Application
components	O
that	O
allow	O
execution	O
to	O
be	O
suspended	O
and	O
resumed	O
,	O
generalizing	O
subroutines	O
for	O
cooperative	B-Operating_System
multitasking	I-Operating_System
.	O
</s>
<s>
Coroutines	B-Architecture
are	O
well-suited	O
for	O
implementing	O
familiar	O
program	O
components	O
such	O
as	O
cooperative	O
tasks	O
,	O
exceptions	O
,	O
event	B-General_Concept
loops	I-General_Concept
,	O
iterators	O
,	O
infinite	O
lists	O
and	O
pipes	B-Operating_System
.	O
</s>
<s>
Melvin	O
Conway	O
coined	O
the	O
term	O
coroutine	B-Architecture
in	O
1958	O
when	O
he	O
applied	O
it	O
to	O
the	O
construction	O
of	O
an	O
assembly	B-Language
program	I-Language
.	O
</s>
<s>
The	O
first	O
published	O
explanation	O
of	O
the	O
coroutine	B-Architecture
appeared	O
later	O
,	O
in	O
1963	O
.	O
</s>
<s>
There	O
is	O
no	O
single	O
precise	O
definition	O
of	O
coroutine	B-Architecture
.	O
</s>
<s>
In	O
1980	O
Christopher	O
D	B-Application
.	O
Marlin	O
summarized	O
two	O
widely-acknowledged	O
fundamental	O
characteristics	O
of	O
a	O
coroutine	B-Architecture
:	O
</s>
<s>
the	O
values	O
of	O
data	O
local	O
to	O
a	O
coroutine	B-Architecture
persist	O
between	O
successive	O
calls	O
;	O
</s>
<s>
the	O
execution	O
of	O
a	O
coroutine	B-Architecture
is	O
suspended	O
as	O
control	O
leaves	O
it	O
,	O
only	O
to	O
carry	O
on	O
where	O
it	O
left	O
off	O
when	O
control	O
re-enters	O
the	O
coroutine	B-Architecture
at	O
some	O
later	O
stage	O
.	O
</s>
<s>
Besides	O
that	O
,	O
a	O
coroutine	B-Architecture
implementation	O
has	O
3	O
features	O
:	O
</s>
<s>
Asymmetric	O
coroutines	B-Architecture
usually	O
provide	O
keywords	O
like	O
yield	O
and	O
resume	O
.	O
</s>
<s>
The	O
runtime	O
only	O
yields	O
to	O
the	O
nearest	O
caller	O
of	O
the	O
current	O
coroutine	B-Architecture
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
in	O
symmetric	O
coroutine	B-Architecture
,	O
programmers	O
must	O
specify	O
a	O
yield	O
destination	O
.	O
</s>
<s>
whether	O
coroutines	B-Architecture
are	O
provided	O
in	O
the	O
language	O
as	O
first-class	O
objects	O
,	O
which	O
can	O
be	O
freely	O
manipulated	O
by	O
the	O
programmer	O
,	O
or	O
as	O
constrained	O
constructs	O
;	O
</s>
<s>
whether	O
a	O
coroutine	B-Architecture
is	O
able	O
to	O
suspend	O
its	O
execution	O
from	O
within	O
nested	O
function	O
calls	O
.	O
</s>
<s>
Such	O
a	O
coroutine	B-Architecture
is	O
stackful	O
.	O
</s>
<s>
One	O
to	O
the	O
contrary	O
is	O
called	O
stackless	B-Operating_System
coroutine	B-Architecture
,	O
where	O
unless	O
marked	O
as	O
coroutine	B-Architecture
,	O
a	O
regular	O
function	O
ca	O
n't	O
use	O
keyword	O
yield	O
.	O
</s>
<s>
Revisiting	O
Coroutines	B-Architecture
published	O
in	O
2009	O
proposed	O
term	O
Full	O
Coroutine	B-Architecture
to	O
denote	O
one	O
that	O
supports	O
first-class	O
coroutine	B-Architecture
and	O
is	O
stackful	O
.	O
</s>
<s>
Full	O
Coroutines	B-Architecture
deserve	O
their	O
own	O
name	O
in	O
that	O
they	O
have	O
the	O
same	O
expressive	B-Language
power	I-Language
as	O
one-shot	O
continuations	O
and	O
delimited	O
continuations	O
.	O
</s>
<s>
Full	O
coroutines	B-Architecture
are	O
either	O
symmetric	O
or	O
asymmetric	O
.	O
</s>
<s>
More	O
importantly	O
,	O
if	O
a	O
coroutine	B-Architecture
is	O
not	O
full	O
,	O
whether	O
symmetric	O
or	O
asymmetric	O
,	O
they	O
are	O
the	O
same	O
expressive	O
,	O
and	O
always	O
less	O
expressive	O
than	O
the	O
full	O
ones	O
.	O
</s>
<s>
Subroutines	O
are	O
special	O
cases	O
of	O
coroutines	B-Architecture
.	O
</s>
<s>
By	O
contrast	O
,	O
coroutines	B-Architecture
can	O
exit	O
by	O
calling	O
other	O
coroutines	B-Architecture
,	O
which	O
may	O
later	O
return	O
to	O
the	O
point	O
where	O
they	O
were	O
invoked	O
in	O
the	O
original	O
coroutine	B-Architecture
;	O
from	O
the	O
coroutine	B-Architecture
's	O
point	O
of	O
view	O
,	O
it	O
is	O
not	O
exiting	O
but	O
calling	O
another	O
coroutine	B-Architecture
.	O
</s>
<s>
Thus	O
,	O
a	O
coroutine	B-Architecture
instance	O
holds	O
state	O
,	O
and	O
varies	O
between	O
invocations	O
;	O
there	O
can	O
be	O
multiple	O
instances	O
of	O
a	O
given	O
coroutine	B-Architecture
at	O
once	O
.	O
</s>
<s>
The	O
difference	O
between	O
calling	O
another	O
coroutine	B-Architecture
by	O
means	O
of	O
"	B-Language
yielding	I-Language
"	I-Language
to	O
it	O
and	O
simply	O
calling	O
another	O
routine	O
(	O
which	O
then	O
,	O
also	O
,	O
would	O
return	O
to	O
the	O
original	O
point	O
)	O
,	O
is	O
that	O
the	O
relationship	O
between	O
two	O
coroutines	B-Architecture
which	O
yield	O
to	O
each	O
other	O
is	O
not	O
that	O
of	O
caller-callee	O
,	O
but	O
instead	O
symmetric	O
.	O
</s>
<s>
Any	O
subroutine	O
can	O
be	O
translated	O
to	O
a	O
coroutine	B-Architecture
which	O
does	O
not	O
call	O
yield	O
.	O
</s>
<s>
Here	O
is	O
a	O
simple	O
example	O
of	O
how	O
coroutines	B-Architecture
can	O
be	O
useful	O
.	O
</s>
<s>
The	O
queue	O
is	O
then	O
completely	O
filled	O
or	O
emptied	O
before	O
yielding	O
control	O
to	O
the	O
other	O
coroutine	B-Architecture
using	O
the	O
yield	O
command	O
.	O
</s>
<s>
The	O
further	O
coroutines	B-Architecture
calls	O
are	O
starting	O
right	O
after	O
the	O
yield	O
,	O
in	O
the	O
outer	O
coroutine	B-Architecture
loop	O
.	O
</s>
<s>
Although	O
this	O
example	O
is	O
often	O
used	O
as	O
an	O
introduction	O
to	O
multithreading	B-Operating_System
,	O
two	O
threads	B-Operating_System
are	O
not	O
needed	O
for	O
this	O
:	O
the	O
yield	B-Architecture
statement	I-Architecture
can	O
be	O
implemented	O
by	O
a	O
jump	O
directly	O
from	O
one	O
routine	O
into	O
the	O
other	O
.	O
</s>
<s>
Coroutines	B-Architecture
are	O
very	O
similar	O
to	O
threads	B-Operating_System
.	O
</s>
<s>
However	O
,	O
coroutines	B-Architecture
are	O
cooperatively	B-Operating_System
multitasked	I-Operating_System
,	O
whereas	O
threads	B-Operating_System
are	O
typically	O
preemptively	B-Operating_System
multitasked	I-Operating_System
.	O
</s>
<s>
Coroutines	B-Architecture
provide	O
concurrency	B-Operating_System
,	O
because	O
they	O
allow	O
tasks	O
to	O
be	O
performed	O
out	O
of	O
order	O
or	O
in	O
a	O
changeable	O
order	O
,	O
without	O
changing	O
the	O
overall	O
outcome	O
,	O
but	O
they	O
do	O
not	O
provide	O
parallelism	B-Operating_System
,	O
because	O
they	O
do	O
not	O
execute	O
multiple	O
tasks	O
simultaneously	O
.	O
</s>
<s>
The	O
advantages	O
of	O
coroutines	B-Architecture
over	O
threads	B-Operating_System
are	O
that	O
they	O
may	O
be	O
used	O
in	O
a	O
hard-realtime	O
context	O
(	O
switching	B-Operating_System
between	O
coroutines	B-Architecture
need	O
not	O
involve	O
any	O
system	B-Operating_System
calls	I-Operating_System
or	O
any	O
blocking	B-Operating_System
calls	O
whatsoever	O
)	O
,	O
there	O
is	O
no	O
need	O
for	O
synchronization	O
primitives	O
such	O
as	O
mutexes	B-Operating_System
,	O
semaphores	O
,	O
etc	O
.	O
</s>
<s>
in	O
order	O
to	O
guard	O
critical	B-Operating_System
sections	I-Operating_System
,	O
and	O
there	O
is	O
no	O
need	O
for	O
support	O
from	O
the	O
operating	O
system	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
implement	O
coroutines	B-Architecture
using	O
preemptively-scheduled	O
threads	B-Operating_System
,	O
in	O
a	O
way	O
that	O
will	O
be	O
transparent	O
to	O
the	O
calling	O
code	O
,	O
but	O
some	O
of	O
the	O
advantages	O
(	O
particularly	O
the	O
suitability	O
for	O
hard-realtime	O
operation	O
and	O
relative	O
cheapness	O
of	O
switching	B-Operating_System
between	O
them	O
)	O
will	O
be	O
lost	O
.	O
</s>
<s>
Generators	O
,	O
also	O
known	O
as	O
semicoroutines	O
,	O
are	O
a	O
subset	O
of	O
coroutines	B-Architecture
.	O
</s>
<s>
Specifically	O
,	O
while	O
both	O
can	O
yield	O
multiple	O
times	O
,	O
suspending	O
their	O
execution	O
and	O
allowing	O
re-entry	O
at	O
multiple	O
entry	O
points	O
,	O
they	O
differ	O
in	O
coroutines	B-Architecture
 '	O
ability	O
to	O
control	O
where	O
execution	O
continues	O
immediately	O
after	O
they	O
yield	O
,	O
while	O
generators	O
cannot	O
,	O
instead	O
transferring	O
control	O
back	O
to	O
the	O
generator	O
's	O
caller	O
.	O
</s>
<s>
That	O
is	O
,	O
since	O
generators	O
are	O
primarily	O
used	O
to	O
simplify	O
the	O
writing	O
of	O
iterators	O
,	O
the	O
yield	B-Architecture
statement	I-Architecture
in	O
a	O
generator	O
does	O
not	O
specify	O
a	O
coroutine	B-Architecture
to	O
jump	O
to	O
,	O
but	O
rather	O
passes	O
a	O
value	O
back	O
to	O
a	O
parent	O
routine	O
.	O
</s>
<s>
However	O
,	O
it	O
is	O
still	O
possible	O
to	O
implement	O
coroutines	B-Architecture
on	O
top	O
of	O
a	O
generator	O
facility	O
,	O
with	O
the	O
aid	O
of	O
a	O
top-level	O
dispatcher	O
routine	O
(	O
a	O
trampoline	B-General_Concept
,	O
essentially	O
)	O
that	O
passes	O
control	O
explicitly	O
to	O
child	O
generators	O
identified	O
by	O
tokens	O
passed	O
back	O
from	O
the	O
generators	O
:	O
</s>
<s>
A	O
number	O
of	O
implementations	O
of	O
coroutines	B-Architecture
for	O
languages	O
with	O
generator	O
support	O
but	O
no	O
native	O
coroutines	B-Architecture
(	O
e.g.	O
</s>
<s>
Python	B-Language
before	O
2.5	O
)	O
use	O
this	O
or	O
a	O
similar	O
model	O
.	O
</s>
<s>
Using	O
coroutines	B-Architecture
for	O
state	B-Architecture
machines	I-Architecture
or	O
concurrency	B-Operating_System
is	O
similar	O
to	O
using	O
mutual	B-Algorithm
recursion	I-Algorithm
with	O
tail	B-Language
calls	I-Language
,	O
as	O
in	O
both	O
cases	O
the	O
control	O
changes	O
to	O
a	O
different	O
one	O
of	O
a	O
set	O
of	O
routines	O
.	O
</s>
<s>
However	O
,	O
coroutines	B-Architecture
are	O
more	O
flexible	O
and	O
generally	O
more	O
efficient	O
.	O
</s>
<s>
Since	O
coroutines	B-Architecture
yield	O
rather	O
than	O
return	O
,	O
and	O
then	O
resume	O
execution	O
rather	O
than	O
restarting	O
from	O
the	O
beginning	O
,	O
they	O
are	O
able	O
to	O
hold	O
state	O
,	O
both	O
variables	O
(	O
as	O
in	O
a	O
closure	B-Language
)	O
and	O
execution	O
point	O
,	O
and	O
yields	O
are	O
not	O
limited	O
to	O
being	O
in	O
tail	O
position	O
;	O
mutually	B-Algorithm
recursive	I-Algorithm
subroutines	O
must	O
either	O
use	O
shared	O
variables	O
or	O
pass	O
state	O
as	O
parameters	O
.	O
</s>
<s>
Further	O
,	O
each	O
mutually	B-Algorithm
recursive	I-Algorithm
call	O
of	O
a	O
subroutine	O
requires	O
a	O
new	O
stack	O
frame	O
(	O
unless	O
tail	B-Language
call	I-Language
elimination	I-Language
is	O
implemented	O
)	O
,	O
while	O
passing	O
control	O
between	O
coroutines	B-Architecture
uses	O
the	O
existing	O
contexts	O
and	O
can	O
be	O
implemented	O
simply	O
by	O
a	O
jump	O
.	O
</s>
<s>
Coroutines	B-Architecture
are	O
useful	O
to	O
implement	O
the	O
following	O
:	O
</s>
<s>
State	B-Architecture
machines	I-Architecture
within	O
a	O
single	O
subroutine	O
,	O
where	O
the	O
state	O
is	O
determined	O
by	O
the	O
current	O
entry/exit	O
point	O
of	O
the	O
procedure	O
;	O
this	O
can	O
result	O
in	O
more	O
readable	O
code	O
compared	O
to	O
use	O
of	O
goto	B-Application
,	O
and	O
may	O
also	O
be	O
implemented	O
via	O
mutual	B-Algorithm
recursion	I-Algorithm
with	O
tail	B-Language
calls	I-Language
.	O
</s>
<s>
Actor	B-Application
model	I-Application
of	O
concurrency	B-Operating_System
,	O
for	O
instance	O
in	O
video	O
games	O
.	O
</s>
<s>
Each	O
actor	O
has	O
its	O
own	O
procedures	O
(	O
this	O
again	O
logically	O
separates	O
the	O
code	O
)	O
,	O
but	O
they	O
voluntarily	O
give	O
up	O
control	O
to	O
central	O
scheduler	O
,	O
which	O
executes	O
them	O
sequentially	O
(	O
this	O
is	O
a	O
form	O
of	O
cooperative	B-Operating_System
multitasking	I-Operating_System
)	O
.	O
</s>
<s>
Communicating	O
sequential	O
processes	O
where	O
each	O
sub-process	O
is	O
a	O
coroutine	B-Architecture
.	O
</s>
<s>
Channel	O
inputs/outputs	O
and	O
blocking	B-Operating_System
operations	O
yield	O
coroutines	B-Architecture
and	O
a	O
scheduler	O
unblocks	O
them	O
on	O
completion	O
events	O
.	O
</s>
<s>
Alternatively	O
,	O
each	O
sub-process	O
may	O
be	O
the	O
parent	O
of	O
the	O
one	O
following	O
it	O
in	O
the	O
data	O
pipeline	B-Operating_System
(	O
or	O
preceding	O
it	O
,	O
in	O
which	O
case	O
the	O
pattern	O
can	O
be	O
expressed	O
as	O
nested	O
generators	O
)	O
.	O
</s>
<s>
Coroutines	B-Architecture
originated	O
as	O
an	O
assembly	B-Language
language	I-Language
method	O
,	O
but	O
are	O
supported	O
in	O
some	O
high-level	B-Language
programming	I-Language
languages	I-Language
.	O
</s>
<s>
JavaScript	B-Language
(	O
since	O
1.7	O
,	O
standardized	O
in	O
ECMAScript	O
6	O
)	O
ECMAScript	O
2017	O
also	O
includes	O
await	O
support	O
.	O
</s>
<s>
Since	O
continuations	O
can	O
be	O
used	O
to	O
implement	O
coroutines	B-Architecture
,	O
programming	O
languages	O
that	O
support	O
them	O
can	O
also	O
quite	O
easily	O
support	O
coroutines	B-Architecture
.	O
</s>
<s>
,	O
many	O
of	O
the	O
most	O
popular	O
programming	O
languages	O
,	O
including	O
C	B-Language
and	O
its	O
derivatives	O
,	O
do	O
not	O
have	O
built-in	O
support	O
for	O
coroutines	B-Architecture
within	O
the	O
language	O
or	O
their	O
standard	O
libraries	O
.	O
</s>
<s>
This	O
is	O
,	O
in	O
large	O
part	O
,	O
due	O
to	O
the	O
limitations	O
of	O
stack-based	B-General_Concept
subroutine	O
implementation	O
.	O
</s>
<s>
An	O
exception	B-General_Concept
is	O
the	O
C++	B-Language
library	O
,	O
part	O
of	O
,	O
which	O
supports	O
context	O
swapping	O
on	O
ARM	O
,	O
MIPS	O
,	O
PowerPC	O
,	O
SPARC	O
and	O
x86	O
on	O
POSIX	O
,	O
Mac	O
OS	O
X	O
and	O
Windows	O
.	O
</s>
<s>
Coroutines	B-Architecture
can	O
be	O
built	O
upon	O
Boost.Context	O
.	O
</s>
<s>
In	O
situations	O
where	O
a	O
coroutine	B-Architecture
would	O
be	O
the	O
natural	O
implementation	O
of	O
a	O
mechanism	O
,	O
but	O
is	O
not	O
available	O
,	O
the	O
typical	O
response	O
is	O
to	O
use	O
a	O
closurea	O
subroutine	O
with	O
state	O
variables	O
(	O
static	B-General_Concept
variables	I-General_Concept
,	O
often	O
boolean	O
flags	O
)	O
to	O
maintain	O
an	O
internal	O
state	O
between	O
calls	O
,	O
and	O
to	O
transfer	O
control	O
to	O
the	O
correct	O
point	O
.	O
</s>
<s>
Another	O
typical	O
response	O
is	O
to	O
implement	O
an	O
explicit	O
state	B-Architecture
machine	I-Architecture
in	O
the	O
form	O
of	O
a	O
large	O
and	O
complex	O
switch	O
statement	O
or	O
via	O
a	O
goto	B-Application
statement	O
,	O
particularly	O
a	O
computed	O
goto	B-Application
.	O
</s>
<s>
Such	O
implementations	O
are	O
considered	O
difficult	O
to	O
understand	O
and	O
maintain	O
,	O
and	O
a	O
motivation	O
for	O
coroutine	B-Architecture
support	O
.	O
</s>
<s>
Threads	B-Operating_System
,	O
and	O
to	O
a	O
lesser	O
extent	O
fibers	B-Operating_System
,	O
are	O
an	O
alternative	O
to	O
coroutines	B-Architecture
in	O
mainstream	O
programming	O
environments	O
today	O
.	O
</s>
<s>
Threads	B-Operating_System
provide	O
facilities	O
for	O
managing	O
the	O
real-time	O
cooperative	O
interaction	O
of	O
simultaneously	O
executing	O
pieces	O
of	O
code	O
.	O
</s>
<s>
Threads	B-Operating_System
are	O
widely	O
available	O
in	O
environments	O
that	O
support	O
C	B-Language
(	O
and	O
are	O
supported	O
natively	O
in	O
many	O
other	O
modern	O
languages	O
)	O
,	O
are	O
familiar	O
to	O
many	O
programmers	O
,	O
and	O
are	O
usually	O
well-implemented	O
,	O
well-documented	O
and	O
well-supported	O
.	O
</s>
<s>
As	O
such	O
,	O
when	O
a	O
coroutine	B-Architecture
is	O
all	O
that	O
is	O
needed	O
,	O
using	O
a	O
thread	B-Operating_System
can	O
be	O
overkill	O
.	O
</s>
<s>
One	O
important	O
difference	O
between	O
threads	B-Operating_System
and	O
coroutines	B-Architecture
is	O
that	O
threads	B-Operating_System
are	O
typically	O
preemptively	O
scheduled	O
while	O
coroutines	B-Architecture
are	O
not	O
.	O
</s>
<s>
Because	O
threads	B-Operating_System
can	O
be	O
rescheduled	O
at	O
any	O
instant	O
and	O
can	O
execute	O
concurrently	O
,	O
programs	O
using	O
threads	B-Operating_System
must	O
be	O
careful	O
about	O
locking	B-Operating_System
.	O
</s>
<s>
In	O
contrast	O
,	O
because	O
coroutines	B-Architecture
can	O
only	O
be	O
rescheduled	O
at	O
specific	O
points	O
in	O
the	O
program	O
and	O
do	O
not	O
execute	O
concurrently	O
,	O
programs	O
using	O
coroutines	B-Architecture
can	O
often	O
avoid	O
locking	B-Operating_System
entirely	O
.	O
</s>
<s>
This	O
property	O
is	O
also	O
cited	O
as	O
a	O
benefit	O
of	O
event-driven	B-Application
or	O
asynchronous	O
programming	O
.	O
</s>
<s>
Since	O
fibers	B-Operating_System
are	O
cooperatively	O
scheduled	O
,	O
they	O
provide	O
an	O
ideal	O
base	O
for	O
implementing	O
coroutines	B-Architecture
above	O
.	O
</s>
<s>
However	O
,	O
system	O
support	O
for	O
fibers	B-Operating_System
is	O
often	O
lacking	O
compared	O
to	O
that	O
for	O
threads	B-Operating_System
.	O
</s>
<s>
In	O
order	O
to	O
implement	O
general-purpose	O
coroutines	B-Architecture
,	O
a	O
second	O
call	B-General_Concept
stack	I-General_Concept
must	O
be	O
obtained	O
,	O
which	O
is	O
a	O
feature	O
not	O
directly	O
supported	O
by	O
the	O
C	B-Language
language	I-Language
.	O
</s>
<s>
A	O
reliable	O
(	O
albeit	O
platform-specific	O
)	O
way	O
to	O
achieve	O
this	O
is	O
to	O
use	O
a	O
small	O
amount	O
of	O
inline	B-Language
assembly	I-Language
to	O
explicitly	O
manipulate	O
the	O
stack	O
pointer	O
during	O
initial	O
creation	O
of	O
the	O
coroutine	B-Architecture
.	O
</s>
<s>
This	O
is	O
the	O
approach	O
recommended	O
by	O
Tom	O
Duff	O
in	O
a	O
discussion	O
on	O
its	O
relative	O
merits	O
vs.	O
the	O
method	O
used	O
by	O
Protothreads	B-Operating_System
.	O
</s>
<s>
On	O
platforms	O
which	O
provide	O
the	O
POSIX	O
sigaltstack	O
system	B-Operating_System
call	I-Operating_System
,	O
a	O
second	O
call	B-General_Concept
stack	I-General_Concept
can	O
be	O
obtained	O
by	O
calling	O
a	O
springboard	O
function	O
from	O
within	O
a	O
signal	O
handler	O
to	O
achieve	O
the	O
same	O
goal	O
in	O
portable	O
C	B-Language
,	O
at	O
the	O
cost	O
of	O
some	O
extra	O
complexity	O
.	O
</s>
<s>
C	B-Language
libraries	I-Language
complying	O
to	O
POSIX	O
or	O
the	O
Single	O
Unix	O
Specification	O
(	O
SUSv3	O
)	O
provided	O
such	O
routines	O
as	O
getcontext	B-Operating_System
,	I-Operating_System
setcontext	I-Operating_System
,	I-Operating_System
makecontext	I-Operating_System
and	I-Operating_System
swapcontext	I-Operating_System
,	O
but	O
these	O
functions	O
were	O
declared	O
obsolete	O
in	O
POSIX	O
1.2008	O
.	O
</s>
<s>
Once	O
a	O
second	O
call	B-General_Concept
stack	I-General_Concept
has	O
been	O
obtained	O
with	O
one	O
of	O
the	O
methods	O
listed	O
above	O
,	O
the	O
setjmp	B-Language
and	I-Language
longjmp	I-Language
functions	O
in	O
the	O
standard	B-Language
C	I-Language
library	I-Language
can	O
then	O
be	O
used	O
to	O
implement	O
the	O
switches	O
between	O
coroutines	B-Architecture
.	O
</s>
<s>
These	O
functions	O
save	O
and	O
restore	O
,	O
respectively	O
,	O
the	O
stack	O
pointer	O
,	O
program	B-General_Concept
counter	I-General_Concept
,	O
callee-saved	O
registers	B-General_Concept
,	O
and	O
any	O
other	O
internal	O
state	O
as	O
required	O
by	O
the	O
ABI	B-Operating_System
,	O
such	O
that	O
returning	O
to	O
a	O
coroutine	B-Architecture
after	O
having	O
yielded	O
restores	O
all	O
the	O
state	O
that	O
would	O
be	O
restored	O
upon	O
returning	O
from	O
a	O
function	O
call	O
.	O
</s>
<s>
Minimalist	O
implementations	O
,	O
which	O
do	O
not	O
piggyback	O
off	O
the	O
setjmp	B-Language
and	I-Language
longjmp	I-Language
functions	O
,	O
may	O
achieve	O
the	O
same	O
result	O
via	O
a	O
small	O
block	O
of	O
inline	B-Language
assembly	I-Language
which	O
swaps	O
merely	O
the	O
stack	O
pointer	O
and	O
program	B-General_Concept
counter	I-General_Concept
,	O
and	O
clobbers	O
all	O
other	O
registers	B-General_Concept
.	O
</s>
<s>
This	O
can	O
be	O
significantly	O
faster	O
,	O
as	O
setjmp	B-Language
and	I-Language
longjmp	I-Language
must	O
conservatively	O
store	O
all	O
registers	B-General_Concept
which	O
may	O
be	O
in	O
use	O
according	O
to	O
the	O
ABI	B-Operating_System
,	O
whereas	O
the	O
clobber	O
method	O
allows	O
the	O
compiler	O
to	O
store	O
(	O
by	O
spilling	O
to	O
the	O
stack	O
)	O
only	O
what	O
it	O
knows	O
is	O
actually	O
in	O
use	O
.	O
</s>
<s>
Due	O
to	O
the	O
lack	O
of	O
direct	O
language	O
support	O
,	O
many	O
authors	O
have	O
written	O
their	O
own	O
libraries	O
for	O
coroutines	B-Architecture
which	O
hide	O
the	O
above	O
details	O
.	O
</s>
<s>
It	O
uses	O
the	O
context	O
functions	O
if	O
they	O
are	O
provided	O
by	O
the	O
native	O
C	B-Language
library	I-Language
;	O
otherwise	O
it	O
provides	O
its	O
own	O
implementations	O
for	O
ARM	O
,	O
PowerPC	O
,	O
Sparc	O
,	O
and	O
x86	O
.	O
</s>
<s>
In	O
addition	O
to	O
the	O
general	O
approach	O
above	O
,	O
several	O
attempts	O
have	O
been	O
made	O
to	O
approximate	O
coroutines	B-Architecture
in	O
C	B-Language
with	O
combinations	O
of	O
subroutines	O
and	O
macros	O
.	O
</s>
<s>
Simon	O
Tatham	O
's	O
contribution	O
,	O
based	O
on	O
Duff	O
's	O
device	O
,	O
is	O
a	O
notable	O
example	O
of	O
the	O
genre	O
,	O
and	O
is	O
the	O
basis	O
for	O
Protothreads	B-Operating_System
and	O
similar	O
implementations	O
.	O
</s>
<s>
In	O
addition	O
to	O
Duff	O
's	O
objections	O
,	O
Tatham	O
's	O
own	O
comments	O
provide	O
a	O
frank	O
evaluation	O
of	O
the	O
limitations	O
of	O
this	O
approach	O
:	O
"	O
As	O
far	O
as	O
I	O
know	O
,	O
this	O
is	O
the	O
worst	O
piece	O
of	O
C	B-Language
hackery	O
ever	O
seen	O
in	O
serious	O
production	O
code.	O
"	O
</s>
<s>
The	O
main	O
shortcomings	O
of	O
this	O
approximation	O
are	O
that	O
,	O
in	O
not	O
maintaining	O
a	O
separate	O
stack	O
frame	O
for	O
each	O
coroutine	B-Architecture
,	O
local	O
variables	O
are	O
not	O
preserved	O
across	O
yields	O
from	O
the	O
function	O
,	O
it	O
is	O
not	O
possible	O
to	O
have	O
multiple	O
entries	O
to	O
the	O
function	O
,	O
and	O
control	O
can	O
only	O
be	O
yielded	O
from	O
the	O
top-level	O
routine	O
.	O
</s>
<s>
C++20	B-Language
introduced	O
standardized	O
coroutines	B-Architecture
as	O
stackless	B-Operating_System
functions	O
that	O
can	O
be	O
suspended	O
in	O
the	O
middle	O
of	O
execution	O
and	O
resumed	O
at	O
a	O
later	O
point	O
.	O
</s>
<s>
The	O
suspended	O
state	O
of	O
a	O
coroutine	B-Architecture
is	O
stored	O
on	O
the	O
heap	O
.	O
</s>
<s>
Implementation	O
of	O
this	O
standard	O
is	O
ongoing	O
,	O
with	O
the	O
G++	O
and	O
MSVC	O
compilers	O
currently	O
fully	O
supporting	O
standard	O
coroutines	B-Architecture
in	O
recent	O
versions	O
.	O
</s>
<s>
-	O
created	O
by	O
Oliver	O
Kowalke	O
,	O
is	O
the	O
official	O
released	O
portable	O
coroutine	B-Architecture
library	O
of	O
since	O
version	O
1.53	O
.	O
</s>
<s>
-	O
also	O
created	O
by	O
Oliver	O
Kowalke	O
,	O
is	O
a	O
modernized	O
portable	O
coroutine	B-Architecture
library	O
since	O
boost	O
version	O
1.59	O
.	O
</s>
<s>
It	O
takes	O
advantage	O
of	O
C++11	O
features	O
,	O
but	O
removes	O
the	O
support	O
for	O
symmetric	O
coroutines	B-Architecture
.	O
</s>
<s>
-	O
In	O
2010	O
,	O
Mozy	O
open	O
sourced	O
a	O
C++	B-Language
library	O
implementing	O
coroutines	B-Architecture
,	O
with	O
an	O
emphasis	O
on	O
using	O
them	O
to	O
abstract	O
asynchronous	B-Architecture
I/O	I-Architecture
into	O
a	O
more	O
familiar	O
sequential	O
model	O
.	O
</s>
<s>
-	O
stackless	B-Operating_System
coroutine	B-Architecture
based	O
on	O
C++	B-Language
preprocessor	B-General_Concept
tricks	O
,	O
providing	O
await/yield	O
emulation	O
.	O
</s>
<s>
-	O
The	O
ScummVM	B-Device
project	O
implements	O
a	O
light-weight	O
version	O
of	O
stackless	B-Operating_System
coroutines	B-Architecture
based	O
on	O
.	O
</s>
<s>
Coroutines	B-Architecture
landed	O
in	O
Clang	B-Application
in	O
May	O
2017	O
,	O
with	O
libc++	O
implementation	O
ongoing	O
.	O
</s>
<s>
-	O
stackless	B-Operating_System
coroutines	B-Architecture
with	O
scheduling	O
designed	O
for	O
high-concurrency	O
level	O
I/O	O
operations	O
.	O
</s>
<s>
C#	B-Language
2.0	I-Language
added	O
semi-coroutine	O
(	O
generator	O
)	O
functionality	O
through	O
the	O
iterator	O
pattern	O
and	O
yield	O
keyword	O
.	O
</s>
<s>
C#	B-Application
5.0	I-Application
includes	O
await	O
syntax	O
support	O
.	O
</s>
<s>
The	O
MindTouch	O
Dream	O
REST	O
framework	O
provides	O
an	O
implementation	O
of	O
coroutines	B-Architecture
based	O
on	O
the	O
C#	B-Language
2.0	I-Language
iterator	O
pattern	O
.	O
</s>
<s>
The	O
Caliburn	O
(	O
)	O
screen	O
patterns	O
framework	O
for	O
WPF	O
uses	O
C#	B-Language
2.0	I-Language
iterators	O
to	O
ease	O
UI	O
programming	O
,	O
particularly	O
in	O
asynchronous	O
scenarios	O
.	O
</s>
<s>
The	O
Power	O
Threading	O
Library	O
(	O
)	O
by	O
Jeffrey	O
Richter	O
implements	O
an	O
AsyncEnumerator	O
that	O
provides	O
simplified	O
Asynchronous	O
Programming	O
Model	O
using	O
iterator-based	O
coroutines	B-Architecture
.	O
</s>
<s>
The	O
Unity	B-Application
game	I-Application
engine	I-Application
implements	O
coroutines	B-Architecture
.	O
</s>
<s>
The	O
implementation	O
is	O
based	O
on	O
Caliburn	O
's	O
Coroutines	B-Architecture
iterator	O
and	O
C#	B-Application
iterator	O
blocks	O
.	O
</s>
<s>
StreamThreads	O
is	O
an	O
open-source	O
,	O
light-weight	O
C#	B-Application
co-routine	B-Architecture
library	O
based	O
on	O
iterator	O
extension	O
methods	O
.	O
</s>
<s>
It	O
supports	O
error	B-General_Concept
handling	I-General_Concept
and	O
return	O
values	O
.	O
</s>
<s>
is	O
a	O
third-party	O
library	O
providing	O
support	O
for	O
stackless	B-Operating_System
coroutines	B-Architecture
in	O
Clojure	B-Language
.	O
</s>
<s>
It	O
's	O
implemented	O
as	O
a	O
macro	O
,	O
statically	O
splitting	O
an	O
arbitrary	O
code	O
block	O
on	O
arbitrary	O
var	O
calls	O
and	O
emitting	O
the	O
coroutine	B-Architecture
as	O
a	O
stateful	O
function	O
.	O
</s>
<s>
D	B-Application
implements	O
coroutines	B-Architecture
as	O
its	O
standard	O
library	O
class	O
A	O
generator	O
makes	O
it	O
trivial	O
to	O
expose	O
a	O
fiber	O
function	O
as	O
an	O
input	O
range	O
,	O
making	O
any	O
fiber	O
compatible	O
with	O
existing	O
range	O
algorithms	O
.	O
</s>
<s>
Go	B-Application
has	O
a	O
built-in	O
concept	O
of	O
"	O
goroutines	O
"	O
,	O
which	O
are	O
lightweight	O
,	O
independent	O
processes	O
managed	O
by	O
the	O
Go	B-Application
runtime	O
.	O
</s>
<s>
A	O
new	O
goroutine	O
can	O
be	O
started	O
using	O
the	O
"	O
go	B-Application
"	O
keyword	O
.	O
</s>
<s>
Goroutines	O
generally	O
communicate	O
using	O
Go	B-Application
's	O
built-in	O
channels	O
.	O
</s>
<s>
There	O
are	O
several	O
implementations	O
for	O
coroutines	B-Architecture
in	O
Java	B-Language
.	O
</s>
<s>
Despite	O
the	O
constraints	O
imposed	O
by	O
Java	B-Language
's	O
abstractions	O
,	O
the	O
JVM	O
does	O
not	O
preclude	O
the	O
possibility	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
build	O
a	O
patched	O
JVM	O
to	O
support	O
coroutines	B-Architecture
more	O
natively	O
.	O
</s>
<s>
Coroutine	B-Architecture
functionality	O
is	O
possible	O
by	O
rewriting	O
regular	O
Java	B-Language
bytecode	O
,	O
either	O
on	O
the	O
fly	O
or	O
at	O
compile	O
time	O
.	O
</s>
<s>
These	O
use	O
JNI	O
methods	O
implemented	O
in	O
the	O
OS	O
or	O
C	B-Language
libraries	I-Language
to	O
provide	O
the	O
functionality	O
to	O
the	O
JVM	O
.	O
</s>
<s>
Thread	B-Operating_System
abstractions	O
.	O
</s>
<s>
Coroutine	B-Architecture
libraries	O
which	O
are	O
implemented	O
using	O
threads	B-Operating_System
may	O
be	O
heavyweight	O
,	O
though	O
performance	O
will	O
vary	O
based	O
on	O
the	O
JVM	O
's	O
thread	B-Operating_System
implementation	O
.	O
</s>
<s>
-	O
fibjs	O
is	O
a	O
JavaScript	B-Language
runtime	O
built	O
on	O
Chrome	O
's	O
V8	O
JavaScript	B-Language
engine	O
.	O
</s>
<s>
fibjs	O
uses	O
fibers-switch	B-Operating_System
,	O
sync	O
style	O
,	O
and	O
non-blocking	B-Architecture
I/O	I-Architecture
model	O
to	O
build	O
scalable	O
systems	O
.	O
</s>
<s>
Since	O
,	O
stackless	B-Operating_System
coroutine	B-Architecture
functionality	O
through	O
"	O
generators	O
"	O
and	O
yield	O
expressions	O
is	O
provided	O
.	O
</s>
<s>
implements	O
coroutines	B-Architecture
as	O
part	O
of	O
a	O
.	O
</s>
<s>
Modula-2	B-Language
as	O
defined	O
by	O
Wirth	O
implements	O
coroutines	B-Architecture
as	O
part	O
of	O
the	O
standard	O
SYSTEM	O
library	O
.	O
</s>
<s>
The	O
procedure	O
NEWPROCESS( )	O
fills	O
in	O
a	O
context	O
given	O
a	O
code	O
block	O
and	O
space	O
for	O
a	O
stack	O
as	O
parameters	O
,	O
and	O
the	O
procedure	O
TRANSFER( )	O
transfers	O
control	O
to	O
a	O
coroutine	B-Architecture
given	O
the	O
coroutine	B-Architecture
's	O
context	O
as	O
its	O
parameter	O
.	O
</s>
<s>
The	O
Mono	B-Application
Common	O
Language	O
Runtime	O
has	O
support	O
for	O
continuations	O
,	O
from	O
which	O
coroutines	B-Architecture
can	O
be	O
built	O
.	O
</s>
<s>
During	O
the	O
development	O
of	O
the	O
.NET	B-Application
Framework	I-Application
2.0	O
,	O
Microsoft	O
extended	O
the	O
design	O
of	O
the	O
Common	O
Language	O
Runtime	O
(	O
CLR	O
)	O
hosting	O
APIs	O
to	O
handle	O
fiber-based	O
scheduling	O
with	O
an	O
eye	O
towards	O
its	O
use	O
in	O
fiber-mode	O
for	O
SQL	O
server	O
.	O
</s>
<s>
Before	O
release	O
,	O
support	O
for	O
the	O
task	O
switching	B-Operating_System
hook	O
ICLRTask::SwitchOut	O
was	O
removed	O
due	O
to	O
time	O
constraints	O
.	O
</s>
<s>
Consequently	O
,	O
the	O
use	O
of	O
the	O
fiber	O
API	O
to	O
switch	O
tasks	O
is	O
currently	O
not	O
a	O
viable	O
option	O
in	O
the	O
.NET	B-Application
Framework	I-Application
.	O
</s>
<s>
OCaml	O
supports	O
coroutines	B-Architecture
through	O
its	O
Thread	B-Operating_System
module	O
.	O
</s>
<s>
These	O
coroutines	B-Architecture
provide	O
concurrency	B-Operating_System
without	O
parallelism	B-Operating_System
,	O
and	O
are	O
scheduled	O
preemptively	O
on	O
a	O
single	O
operating	O
system	O
thread	B-Operating_System
.	O
</s>
<s>
Since	O
OCaml	O
5.0	O
,	O
green	B-Operating_System
threads	I-Operating_System
are	O
also	O
available	O
;	O
provided	O
by	O
different	O
modules	O
.	O
</s>
<s>
Coroutines	B-Architecture
are	O
natively	O
implemented	O
in	O
all	O
Raku	B-Application
backends	O
.	O
</s>
<s>
implemented	O
in	O
a	O
way	O
that	O
resembles	O
Python	B-Language
functions	O
,	O
and	O
some	O
Go	B-Application
,	O
many	O
showing	O
there	O
code	O
converted	O
with	O
same	O
number	O
of	O
lines	O
and	O
behavior	O
.	O
</s>
<s>
Python	B-Language
3.5	O
introduces	O
explicit	O
support	O
for	O
coroutines	B-Architecture
with	O
async/await	O
syntax	O
(	O
)	O
.	O
</s>
<s>
Since	O
Python	B-Language
3.7	O
,	O
async/await	O
have	O
become	O
reserved	O
keywords	O
.	O
</s>
<s>
Racket	B-Operating_System
provides	O
native	O
continuations	O
,	O
with	O
a	O
trivial	O
implementation	O
of	O
coroutines	B-Architecture
provided	O
in	O
the	O
official	O
package	O
catalog	O
.	O
</s>
<s>
Ruby	B-Language
1.9	O
supports	O
coroutines	B-Architecture
natively	O
which	O
are	O
implemented	O
as	O
,	O
which	O
are	O
semi-coroutines	O
.	O
</s>
<s>
Rust	B-Application
supports	O
coroutines	B-Architecture
since	O
version	O
1.39	O
.	O
</s>
<s>
Scala	B-Application
Coroutines	B-Architecture
is	O
a	O
coroutine	B-Architecture
implementation	O
for	O
Scala	B-Application
.	O
</s>
<s>
This	O
implementation	O
is	O
a	O
library-level	O
extension	O
that	O
relies	O
on	O
the	O
Scala	B-Application
macro	O
system	O
to	O
statically	O
transform	O
sections	O
of	O
the	O
program	O
into	O
coroutine	B-Architecture
objects	O
.	O
</s>
<s>
As	O
such	O
,	O
this	O
implementation	O
does	O
not	O
require	O
modifications	O
in	O
the	O
JVM	O
,	O
so	O
it	O
is	O
fully	O
portable	O
between	O
different	O
JVMs	O
and	O
works	O
with	O
alternative	O
Scala	B-Application
backends	O
,	O
such	O
as	O
Scala.js	B-Application
,	O
which	O
compiles	O
to	O
JavaScript	B-Language
.	O
</s>
<s>
Scala	B-Application
Coroutines	B-Architecture
rely	O
on	O
the	O
coroutine	B-Architecture
macro	O
that	O
transforms	O
a	O
normal	O
block	O
of	O
code	O
into	O
a	O
coroutine	B-Architecture
definition	O
.	O
</s>
<s>
Such	O
a	O
coroutine	B-Architecture
definition	O
can	O
be	O
invoked	O
with	O
the	O
call	O
operation	O
,	O
which	O
instantiates	O
a	O
coroutine	B-Architecture
frame	O
.	O
</s>
<s>
A	O
coroutine	B-Architecture
frame	O
can	O
be	O
resumed	O
with	O
the	O
resume	O
method	O
,	O
which	O
resumes	O
the	O
execution	O
of	O
the	O
coroutine	B-Architecture
's	O
body	O
,	O
until	O
reaching	O
a	O
yieldval	O
keyword	O
,	O
which	O
suspends	O
the	O
coroutine	B-Architecture
frame	O
.	O
</s>
<s>
Scala	B-Application
Coroutines	B-Architecture
also	O
expose	O
a	O
snapshot	O
method	O
,	O
which	O
effectively	O
duplicates	O
the	O
coroutine	B-Architecture
.	O
</s>
<s>
A	O
detailed	O
descriptions	O
of	O
Scala	B-Application
coroutines	B-Architecture
with	O
snapshots	O
appeared	O
at	O
,	O
along	O
with	O
their	O
.	O
</s>
<s>
Since	O
Scheme	B-Language
provides	O
full	O
support	O
for	O
continuations	O
,	O
implementing	O
coroutines	B-Architecture
is	O
nearly	O
trivial	O
,	O
requiring	O
only	O
that	O
a	O
queue	O
of	O
continuations	O
be	O
maintained	O
.	O
</s>
<s>
Since	O
,	O
in	O
most	O
Smalltalk	B-Application
environments	O
,	O
the	O
execution	O
stack	O
is	O
a	O
first-class	O
citizen	O
,	O
coroutines	B-Architecture
can	O
be	O
implemented	O
without	O
additional	O
library	O
or	O
VM	O
support	O
.	O
</s>
<s>
-	O
Swift	O
coroutines	B-Architecture
library	O
for	O
iOS	B-Application
,	O
macOS	O
and	O
Linux	O
.	O
</s>
<s>
Since	O
version	O
8.6	O
,	O
the	O
Tool	B-Operating_System
Command	I-Operating_System
Language	I-Operating_System
supports	O
coroutines	B-Architecture
in	O
the	O
core	O
language	O
.	O
</s>
<s>
Vala	B-Language
implements	O
native	O
support	O
for	O
coroutines	B-Architecture
.	O
</s>
<s>
They	O
are	O
designed	O
to	O
be	O
used	O
with	O
a	O
Gtk	O
Main	B-General_Concept
Loop	I-General_Concept
,	O
but	O
can	O
be	O
used	O
alone	O
if	O
care	O
is	O
taken	O
to	O
ensure	O
that	O
the	O
end	O
callback	O
will	O
never	O
have	O
to	O
be	O
called	O
before	O
doing	O
,	O
at	O
least	O
,	O
one	O
yield	O
.	O
</s>
<s>
Machine-dependent	O
assembly	B-Language
languages	I-Language
often	O
provide	O
direct	O
methods	O
for	O
coroutine	B-Architecture
execution	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
MACRO-11	B-Device
,	O
the	O
assembly	B-Language
language	I-Language
of	O
the	O
PDP-11	B-Device
family	O
of	O
minicomputers	O
,	O
the	O
“	O
classic	O
”	O
coroutine	B-Architecture
switch	O
is	O
effected	O
by	O
the	O
instruction	O
"	O
JSR	O
PC	O
,	O
@(SP )	O
+	O
"	O
,	O
which	O
jumps	O
to	O
the	O
address	O
popped	O
from	O
the	O
stack	O
and	O
pushes	O
the	O
current	O
(	O
i.e	O
that	O
of	O
the	O
next	O
)	O
instruction	O
address	O
onto	O
the	O
stack	O
.	O
</s>
<s>
On	O
VAXen	B-Device
(	O
in	O
VAX	B-Language
MACRO	I-Language
)	O
the	O
comparable	O
instruction	O
is	O
"	O
JSB	O
@(SP )	O
+	O
"	O
.	O
</s>
<s>
Even	O
on	O
a	O
Motorola	B-Device
6809	I-Device
there	O
is	O
the	O
instruction	O
"	O
JSR	O
[,S++]";	O
note	O
the	O
"	O
++	O
"	O
,	O
as	O
2	O
bytes	O
(	O
of	O
address	O
)	O
are	O
popped	O
from	O
the	O
stack	O
.	O
</s>
<s>
This	O
instruction	O
is	O
much	O
used	O
in	O
the	O
(	O
standard	O
)	O
'	O
monitor	O
 '	O
Assist	B-Application
09	O
.	O
</s>
