<s>
In	O
computing	O
,	O
a	O
computer	B-Application
program	I-Application
or	O
subroutine	O
is	O
called	O
reentrant	B-Operating_System
if	O
multiple	O
invocations	O
can	O
safely	O
run	O
concurrently	B-Architecture
on	O
multiple	B-Operating_System
processors	I-Operating_System
,	O
or	O
on	O
a	O
single-processor	B-Operating_System
system	I-Operating_System
,	O
where	O
a	O
reentrant	B-Operating_System
procedure	O
can	O
be	O
interrupted	O
in	O
the	O
middle	O
of	O
its	O
execution	B-General_Concept
and	O
then	O
safely	O
be	O
called	O
again	O
(	O
"	O
re-entered	O
"	O
)	O
before	O
its	O
previous	O
invocations	O
complete	O
execution	B-General_Concept
.	O
</s>
<s>
The	O
interruption	O
could	O
be	O
caused	O
by	O
an	O
internal	O
action	O
such	O
as	O
a	O
jump	B-General_Concept
or	O
call	O
,	O
or	O
by	O
an	O
external	O
action	O
such	O
as	O
an	O
interrupt	B-Application
or	O
signal	B-Operating_System
,	O
unlike	O
recursion	O
,	O
where	O
new	O
invocations	O
can	O
only	O
be	O
caused	O
by	O
internal	O
call	O
.	O
</s>
<s>
This	O
definition	O
originates	O
from	O
multiprogramming	O
environments	O
,	O
where	O
multiple	O
processes	O
may	O
be	O
active	O
concurrently	B-Architecture
and	O
where	O
the	O
flow	O
of	O
control	O
could	O
be	O
interrupted	O
by	O
an	O
interrupt	B-Application
and	O
transferred	O
to	O
an	O
interrupt	B-General_Concept
service	I-General_Concept
routine	I-General_Concept
(	O
ISR	O
)	O
or	O
"	O
handler	O
"	O
subroutine	O
.	O
</s>
<s>
Any	O
subroutine	O
used	O
by	O
the	O
handler	O
that	O
could	O
potentially	O
have	O
been	O
executing	O
when	O
the	O
interrupt	B-Application
was	O
triggered	O
should	O
be	O
reentrant	B-Operating_System
.	O
</s>
<s>
Similarly	O
,	O
code	O
shared	O
by	O
two	O
processors	O
accessing	O
shared	O
data	O
should	O
be	O
reentrant	B-Operating_System
.	O
</s>
<s>
Often	O
,	O
subroutines	O
accessible	O
via	O
the	O
operating	B-Operating_System
system	I-Operating_System
kernel	I-Operating_System
are	O
not	O
reentrant	B-Operating_System
.	O
</s>
<s>
Hence	O
,	O
interrupt	B-General_Concept
service	I-General_Concept
routines	I-General_Concept
are	O
limited	O
in	O
the	O
actions	O
they	O
can	O
perform	O
;	O
for	O
instance	O
,	O
they	O
are	O
usually	O
restricted	O
from	O
accessing	O
the	O
file	B-Application
system	I-Application
and	O
sometimes	O
even	O
from	O
allocating	B-General_Concept
memory	I-General_Concept
.	O
</s>
<s>
This	O
definition	O
of	O
reentrancy	B-Operating_System
differs	O
from	O
that	O
of	O
thread-safety	B-Operating_System
in	O
multi-threaded	O
environments	O
.	O
</s>
<s>
A	O
reentrant	B-Operating_System
subroutine	O
can	O
achieve	O
thread-safety	B-Operating_System
,	O
but	O
in	O
all	O
situations	O
.	O
</s>
<s>
Conversely	O
,	O
thread-safe	B-Operating_System
code	O
does	O
not	O
necessarily	O
have	O
to	O
be	O
reentrant	B-Operating_System
(	O
see	O
below	O
for	O
examples	O
)	O
.	O
</s>
<s>
Other	O
terms	O
used	O
for	O
reentrant	B-Operating_System
programs	I-Operating_System
include	O
"	O
sharable	B-Operating_System
code	I-Operating_System
"	O
.	O
</s>
<s>
Reentrant	B-Operating_System
subroutines	O
are	O
sometimes	O
marked	O
in	O
reference	O
material	O
as	O
being	O
"	O
signal	B-Operating_System
safe	O
"	O
.	O
</s>
<s>
Reentrant	B-Operating_System
programs	I-Operating_System
are	O
often	O
"	O
pure	B-Operating_System
procedures	I-Operating_System
"	O
.	O
</s>
<s>
Reentrancy	B-Operating_System
is	O
not	O
the	O
same	O
thing	O
as	O
idempotence	O
,	O
in	O
which	O
the	O
function	O
may	O
be	O
called	O
more	O
than	O
once	O
yet	O
generate	O
exactly	O
the	O
same	O
output	O
as	O
if	O
it	O
had	O
only	O
been	O
called	O
once	O
.	O
</s>
<s>
Global	O
data	O
is	O
defined	O
outside	O
functions	O
and	O
can	O
be	O
accessed	O
by	O
more	O
than	O
one	O
function	O
,	O
either	O
in	O
the	O
form	O
of	O
global	O
variables	O
(	O
data	O
shared	O
between	O
all	O
functions	O
)	O
,	O
or	O
as	O
static	B-General_Concept
variables	I-General_Concept
(	O
data	O
shared	O
by	O
all	O
invocations	O
of	O
the	O
same	O
function	O
)	O
.	O
</s>
<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
global	O
data	O
is	O
defined	O
in	O
the	O
scope	O
of	O
a	O
class	O
and	O
can	O
be	O
private	O
,	O
making	O
it	O
accessible	O
only	O
to	O
functions	O
of	O
that	O
class	O
.	O
</s>
<s>
There	O
is	O
also	O
the	O
concept	O
of	O
instance	B-Application
variables	I-Application
,	O
where	O
a	O
class	O
variable	O
is	O
bound	O
to	O
a	O
class	O
instance	O
.	O
</s>
<s>
For	O
these	O
reasons	O
,	O
in	O
object-oriented	B-Language
programming	I-Language
,	O
this	O
distinction	O
is	O
usually	O
reserved	O
for	O
the	O
data	O
accessible	O
outside	O
of	O
the	O
class	O
(	O
public	O
)	O
,	O
and	O
for	O
the	O
data	O
independent	O
of	O
class	O
instances	O
(	O
static	O
)	O
.	O
</s>
<s>
Reentrancy	B-Operating_System
is	O
distinct	O
from	O
,	O
but	O
closely	O
related	O
to	O
,	O
thread-safety	B-Operating_System
.	O
</s>
<s>
A	O
function	O
can	O
be	O
thread-safe	B-Operating_System
and	O
still	O
not	O
reentrant	B-Operating_System
.	O
</s>
<s>
For	O
example	O
,	O
a	O
function	O
could	O
be	O
wrapped	O
all	O
around	O
with	O
a	O
mutex	B-Operating_System
(	O
which	O
avoids	O
problems	O
in	O
multithreading	O
environments	O
)	O
,	O
but	O
,	O
if	O
that	O
function	O
were	O
used	O
in	O
an	O
interrupt	B-General_Concept
service	I-General_Concept
routine	I-General_Concept
,	O
it	O
could	O
starve	O
waiting	O
for	O
the	O
first	O
execution	B-General_Concept
to	O
release	O
the	O
mutex	B-Operating_System
.	O
</s>
<s>
The	O
key	O
for	O
avoiding	O
confusion	O
is	O
that	O
reentrant	B-Operating_System
refers	O
to	O
only	O
one	O
thread	O
executing	O
.	O
</s>
<s>
Reentrant	B-Operating_System
code	I-Operating_System
may	O
not	O
hold	O
any	O
static	O
or	O
global	O
non-constant	O
data	O
without	O
synchronization	O
.	O
</s>
<s>
Reentrant	B-Operating_System
functions	I-Operating_System
can	O
work	O
with	O
global	O
data	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
reentrant	B-Operating_System
interrupt	B-General_Concept
service	I-General_Concept
routine	I-General_Concept
could	O
grab	O
a	O
piece	O
of	O
hardware	O
status	O
to	O
work	O
with	O
(	O
e.g.	O
,	O
serial	O
port	O
read	O
buffer	O
)	O
which	O
is	O
not	O
only	O
global	O
,	O
but	O
volatile	O
.	O
</s>
<s>
Still	O
,	O
typical	O
use	O
of	O
static	B-General_Concept
variables	I-General_Concept
and	O
global	O
data	O
is	O
not	O
advised	O
,	O
in	O
the	O
sense	O
that	O
,	O
except	O
in	O
sections	O
of	O
code	O
that	O
are	O
synchronized	O
,	O
only	O
atomic	B-General_Concept
read-modify-write	B-Operating_System
instructions	O
should	O
be	O
used	O
in	O
these	O
variables	O
(	O
it	O
should	O
not	O
be	O
possible	O
for	O
an	O
interrupt	B-Application
or	O
signal	B-Operating_System
to	O
come	O
during	O
the	O
execution	B-General_Concept
of	O
such	O
an	O
instruction	O
)	O
.	O
</s>
<s>
Note	O
that	O
in	O
C	B-Language
,	O
even	O
a	O
read	O
or	O
write	O
is	O
not	O
guaranteed	O
to	O
be	O
atomic	B-General_Concept
;	O
it	O
may	O
be	O
split	O
into	O
several	O
reads	O
or	O
writes	O
.	O
</s>
<s>
The	O
C	B-Language
standard	O
and	O
SUSv3	O
provide	O
sig_atomic_t	O
for	O
this	O
purpose	O
,	O
although	O
with	O
guarantees	O
only	O
for	O
simple	O
reads	O
and	O
writes	O
,	O
not	O
for	O
incrementing	O
or	O
decrementing	O
.	O
</s>
<s>
More	O
complex	O
atomic	B-General_Concept
operations	I-General_Concept
are	O
available	O
in	O
C11	O
,	O
which	O
provides	O
stdatomic.h.	O
</s>
<s>
Reentrant	B-Operating_System
code	I-Operating_System
may	O
not	O
modify	B-Application
itself	I-Application
without	O
synchronization	O
.	O
</s>
<s>
There	O
are	O
various	O
reasons	O
for	O
this	O
(	O
e.g.	O
,	O
blitting	B-Algorithm
graphics	O
quickly	O
)	O
but	O
this	O
generally	O
requires	O
synchronization	O
to	O
avoid	O
problems	O
with	O
reentrancy.	O
<	O
p>It	O
may	O
,	O
however	O
,	O
modify	B-Application
itself	I-Application
if	O
it	O
resides	O
in	O
its	O
own	O
unique	O
memory	O
.	O
</s>
<s>
That	O
is	O
,	O
if	O
each	O
new	O
invocation	O
uses	O
a	O
different	O
physical	O
machine	O
code	O
location	O
where	O
a	O
copy	O
of	O
the	O
original	O
code	O
is	O
made	O
,	O
it	O
will	O
not	O
affect	O
other	O
invocations	O
even	O
if	O
it	O
modifies	O
itself	O
during	O
execution	B-General_Concept
of	O
that	O
particular	O
invocation	O
(	O
thread	O
)	O
.	O
</s>
<s>
Reentrant	B-Operating_System
code	I-Operating_System
may	O
not	O
call	O
non-reentrant	O
computer	B-Application
programs	I-Application
or	O
routines	O
.	O
</s>
<s>
Multiple	O
levels	O
of	O
user	O
,	O
object	O
,	O
or	O
process	O
priority	B-Application
or	O
multiprocessing	B-Operating_System
usually	O
complicate	O
the	O
control	O
of	O
reentrant	B-Operating_System
code	I-Operating_System
.	O
</s>
<s>
It	O
is	O
important	O
to	O
keep	O
track	O
of	O
any	O
access	O
or	O
side	O
effects	O
that	O
are	O
done	O
inside	O
a	O
routine	O
designed	O
to	O
be	O
reentrant	B-Operating_System
.	O
</s>
<s>
Reentrancy	B-Operating_System
of	O
a	O
subroutine	O
that	O
operates	O
on	O
operating-system	O
resources	O
or	O
non-local	O
data	O
depends	O
on	O
the	O
atomicity	B-General_Concept
of	O
the	O
respective	O
operations	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
the	O
subroutine	O
modifies	O
a	O
64-bit	O
global	O
variable	O
on	O
a	O
32-bit	O
machine	O
,	O
the	O
operation	O
may	O
be	O
split	O
into	O
two	O
32-bit	O
operations	O
,	O
and	O
thus	O
,	O
if	O
the	O
subroutine	O
is	O
interrupted	O
while	O
executing	O
,	O
and	O
called	O
again	O
from	O
the	O
interrupt	B-General_Concept
handler	I-General_Concept
,	O
the	O
global	O
variable	O
may	O
be	O
in	O
a	O
state	O
where	O
only	O
32	O
bits	O
have	O
been	O
updated	O
.	O
</s>
<s>
The	O
programming	O
language	O
might	O
provide	O
atomicity	B-General_Concept
guarantees	O
for	O
interruption	O
caused	O
by	O
an	O
internal	O
action	O
such	O
as	O
a	O
jump	B-General_Concept
or	O
call	O
.	O
</s>
<s>
(	O
The	O
latter	O
can	O
happen	O
in	O
C	B-Language
,	O
because	O
the	O
expression	O
has	O
no	O
sequence	B-Application
point	I-Application
.	O
)	O
</s>
<s>
The	O
operating	O
system	O
might	O
provide	O
atomicity	B-General_Concept
guarantees	O
for	O
signals	B-Operating_System
,	O
such	O
as	O
a	O
system	O
call	O
interrupted	O
by	O
a	O
signal	B-Operating_System
not	O
having	O
a	O
partial	O
effect	O
.	O
</s>
<s>
The	O
processor	O
hardware	O
might	O
provide	O
atomicity	B-General_Concept
guarantees	O
for	O
interrupts	B-Application
,	O
such	O
as	O
interrupted	O
processor	O
instructions	O
not	O
having	O
partial	O
effects	O
.	O
</s>
<s>
To	O
illustrate	O
reentrancy	B-Operating_System
,	O
this	O
article	O
uses	O
as	O
an	O
example	O
a	O
C	B-Language
utility	O
function	O
,	O
,	O
that	O
takes	O
two	O
pointers	O
and	O
transposes	O
their	O
values	O
,	O
and	O
an	O
interrupt-handling	O
routine	O
that	O
also	O
calls	O
the	O
swap	O
function	O
.	O
</s>
<s>
This	O
is	O
an	O
example	O
swap	O
function	O
that	O
fails	O
to	O
be	O
reentrant	B-Operating_System
or	O
thread-safe	B-Operating_System
.	O
</s>
<s>
As	O
such	O
,	O
it	O
should	O
not	O
have	O
been	O
used	O
in	O
the	O
interrupt	B-General_Concept
service	I-General_Concept
routine	I-General_Concept
isr( )	O
:	O
</s>
<s>
The	O
function	O
in	O
the	O
preceding	O
example	O
can	O
be	O
made	O
thread-safe	B-Operating_System
by	O
making	O
thread-local	O
.	O
</s>
<s>
It	O
still	O
fails	O
to	O
be	O
reentrant	B-Operating_System
,	O
and	O
this	O
will	O
continue	O
to	O
cause	O
problems	O
if	O
is	O
called	O
in	O
the	O
same	O
context	O
as	O
a	O
thread	O
already	O
executing	O
:	O
</s>
<s>
An	O
implementation	O
of	O
that	O
allocates	O
on	O
the	O
stack	B-General_Concept
instead	O
of	O
globally	O
and	O
that	O
is	O
called	O
only	O
with	O
unshared	O
variables	O
as	O
parameters	O
is	O
both	O
thread-safe	B-Operating_System
and	O
reentrant	B-Operating_System
.	O
</s>
<s>
Thread-safe	B-Operating_System
because	O
the	O
stack	B-General_Concept
is	O
local	O
to	O
a	O
thread	O
and	O
a	O
function	O
acting	O
just	O
on	O
local	O
data	O
will	O
always	O
produce	O
the	O
expected	O
result	O
.	O
</s>
<s>
A	O
reentrant	B-Operating_System
interrupt	B-General_Concept
handler	I-General_Concept
is	O
an	O
interrupt	B-General_Concept
handler	I-General_Concept
that	O
re-enables	O
interrupts	B-Application
early	O
in	O
the	O
interrupt	B-General_Concept
handler	I-General_Concept
.	O
</s>
<s>
This	O
may	O
reduce	O
interrupt	B-General_Concept
latency	I-General_Concept
.	O
</s>
<s>
In	O
general	O
,	O
while	O
programming	O
interrupt	B-General_Concept
service	I-General_Concept
routines	I-General_Concept
,	O
it	O
is	O
recommended	O
to	O
re-enable	O
interrupts	B-Application
as	O
soon	O
as	O
possible	O
in	O
the	O
interrupt	B-General_Concept
handler	I-General_Concept
.	O
</s>
<s>
This	O
practice	O
helps	O
to	O
avoid	O
losing	O
interrupts	B-Application
.	O
</s>
<s>
In	O
the	O
following	O
code	O
,	O
neither	O
f	O
nor	O
g	O
functions	O
is	O
reentrant	B-Operating_System
.	O
</s>
<s>
In	O
the	O
above	O
,	O
depends	O
on	O
a	O
non-constant	O
global	O
variable	O
;	O
thus	O
,	O
if	O
is	O
interrupted	O
during	O
execution	B-General_Concept
by	O
an	O
ISR	O
which	O
modifies	O
,	O
then	O
reentry	O
into	O
will	O
return	O
the	O
wrong	O
value	O
of	O
.	O
</s>
<s>
The	O
value	O
of	O
and	O
,	O
therefore	O
,	O
the	O
return	O
value	O
of	O
,	O
cannot	O
be	O
predicted	O
with	O
confidence	O
:	O
they	O
will	O
vary	O
depending	O
on	O
whether	O
an	O
interrupt	B-Application
modified	O
during	O
'	O
s	O
execution	B-General_Concept
.	O
</s>
<s>
Hence	O
,	O
is	O
not	O
reentrant	B-Operating_System
.	O
</s>
<s>
Neither	O
is	O
,	O
because	O
it	O
calls	O
,	O
which	O
is	O
not	O
reentrant	B-Operating_System
.	O
</s>
<s>
These	O
slightly	O
altered	O
versions	O
are	O
reentrant	B-Operating_System
:	O
</s>
<s>
In	O
the	O
following	O
,	O
the	O
function	O
is	O
thread-safe	B-Operating_System
,	O
but	O
not	O
(	O
necessarily	O
)	O
reentrant	B-Operating_System
:	O
</s>
<s>
But	O
,	O
if	O
the	O
function	O
is	O
used	O
in	O
a	O
reentrant	B-Operating_System
interrupt	B-General_Concept
handler	I-General_Concept
and	O
a	O
second	O
interrupt	B-Application
arises	O
inside	O
the	O
function	O
,	O
the	O
second	O
routine	O
will	O
hang	O
forever	O
.	O
</s>
<s>
As	O
interrupt	B-Application
servicing	O
can	O
disable	O
other	O
interrupts	B-Application
,	O
the	O
whole	O
system	O
could	O
suffer	O
.	O
</s>
