<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
reentrant	B-Operating_System
mutex	I-Operating_System
(	O
recursive	B-Operating_System
mutex	I-Operating_System
,	O
recursive	B-Operating_System
lock	I-Operating_System
)	O
is	O
a	O
particular	O
type	O
of	O
mutual	B-Operating_System
exclusion	I-Operating_System
(	O
mutex	B-Operating_System
)	O
device	O
that	O
may	O
be	O
locked	O
multiple	O
times	O
by	O
the	O
same	O
process/thread	B-Operating_System
,	O
without	O
causing	O
a	O
deadlock	B-Operating_System
.	O
</s>
<s>
While	O
any	O
attempt	O
to	O
perform	O
the	O
"	O
lock	O
"	O
operation	O
on	O
an	O
ordinary	O
mutex	B-Operating_System
(	O
lock	O
)	O
would	O
either	O
fail	O
or	O
block	O
when	O
the	O
mutex	B-Operating_System
is	O
already	O
locked	O
,	O
on	O
a	O
recursive	B-Operating_System
mutex	I-Operating_System
this	O
operation	O
will	O
succeed	O
if	O
and	O
only	O
if	O
the	O
locking	O
thread	B-Operating_System
is	O
the	O
one	O
that	O
already	O
holds	O
the	O
lock	O
.	O
</s>
<s>
Typically	O
,	O
a	O
recursive	B-Operating_System
mutex	I-Operating_System
tracks	O
the	O
number	O
of	O
times	O
it	O
has	O
been	O
locked	O
,	O
and	O
requires	O
equally	O
many	O
unlock	O
operations	O
to	O
be	O
performed	O
before	O
other	O
threads	B-Operating_System
may	O
lock	O
it	O
.	O
</s>
<s>
Recursive	B-Operating_System
mutexes	I-Operating_System
solve	O
the	O
problem	O
of	O
non-reentrancy	B-Operating_System
with	O
regular	O
mutexes	B-Operating_System
:	O
if	O
a	O
function	O
that	O
takes	O
a	O
lock	O
and	O
executes	O
a	O
callback	O
is	O
itself	O
called	O
by	O
the	O
callback	O
,	O
deadlock	B-Operating_System
ensues	O
.	O
</s>
<s>
In	O
pseudocode	B-Language
,	O
that	O
is	O
the	O
following	O
situation	O
:	O
</s>
<s>
var	O
m	O
:	O
Mutex	B-Operating_System
//	O
A	O
non-recursive	O
mutex	B-Operating_System
,	O
initially	O
unlocked	O
.	O
</s>
<s>
—	O
deadlock	B-Operating_System
,	O
because	O
is	O
already	O
locked	O
,	O
so	O
the	O
executing	O
thread	B-Operating_System
will	O
block	O
,	O
waiting	O
for	O
itself	O
.	O
</s>
<s>
Replacing	O
the	O
mutex	B-Operating_System
with	O
a	O
recursive	O
one	O
solves	O
the	O
problem	O
,	O
because	O
the	O
final	O
will	O
succeed	O
without	O
blocking	O
.	O
</s>
<s>
W	O
.	O
Richard	O
Stevens	O
notes	O
that	O
recursive	B-Operating_System
locks	I-Operating_System
are	O
"	O
tricky	O
"	O
to	O
use	O
correctly	O
,	O
and	O
recommends	O
their	O
use	O
for	O
adapting	O
single-threaded	B-Operating_System
code	O
without	O
changing	O
APIs	B-Application
,	O
but	O
"	O
only	O
when	O
no	O
other	O
solution	O
is	O
possible	O
"	O
.	O
</s>
<s>
The	O
Java	B-Language
language	I-Language
's	O
native	O
synchronization	O
mechanism	O
,	O
monitor	O
,	O
uses	O
recursive	B-Operating_System
locks	I-Operating_System
.	O
</s>
<s>
Syntactically	O
,	O
a	O
lock	O
is	O
a	O
block	O
of	O
code	O
with	O
the	O
'	O
synchronized	O
 '	O
keyword	O
preceding	O
it	O
and	O
any	O
Object	O
reference	O
in	O
parentheses	O
that	O
will	O
be	O
used	O
as	O
the	O
mutex	B-Operating_System
.	O
</s>
<s>
Inside	O
the	O
synchronized	O
block	O
,	O
the	O
given	O
object	O
can	O
be	O
used	O
as	O
a	O
condition	O
variable	O
by	O
doing	O
a	O
wait( )	O
,	O
notify( )	O
,	O
or	O
notifyAll( )	O
on	O
it	O
.	O
</s>
<s>
Thus	O
all	O
Objects	O
are	O
both	O
recursive	B-Operating_System
mutexes	I-Operating_System
and	O
condition	O
variables	O
.	O
</s>
<s>
Thread	B-Operating_System
A	O
's	O
F	O
calls	O
itself	O
recursively	O
.	O
</s>
<s>
It	O
already	O
owns	O
the	O
lock	O
,	O
so	O
it	O
will	O
not	O
block	O
itself	O
(	O
no	O
deadlock	B-Operating_System
)	O
.	O
</s>
<s>
This	O
is	O
the	O
central	O
idea	O
of	O
a	O
reentrant	B-Operating_System
mutex	I-Operating_System
,	O
and	O
is	O
what	O
makes	O
it	O
different	O
from	O
a	O
regular	O
lock	O
.	O
</s>
<s>
If	O
the	O
owner	O
is	O
set	O
and	O
not	O
the	O
current	O
thread	B-Operating_System
,	O
wait	O
for	O
the	O
control	O
condition	O
to	O
be	O
notified	O
(	O
this	O
also	O
releases	O
the	O
condition	O
)	O
.	O
</s>
<s>
Set	O
the	O
owner	O
to	O
the	O
current	O
thread	B-Operating_System
.	O
</s>
