<s>
In	O
software	B-General_Concept
engineering	I-General_Concept
,	O
a	O
spinlock	B-Operating_System
is	O
a	O
lock	B-Operating_System
that	O
causes	O
a	O
thread	B-Operating_System
trying	O
to	O
acquire	O
it	O
to	O
simply	O
wait	O
in	O
a	O
loop	O
(	O
"	O
spin	O
"	O
)	O
while	O
repeatedly	O
checking	O
whether	O
the	O
lock	B-Operating_System
is	O
available	O
.	O
</s>
<s>
Since	O
the	O
thread	B-Operating_System
remains	O
active	O
but	O
is	O
not	O
performing	O
a	O
useful	O
task	O
,	O
the	O
use	O
of	O
such	O
a	O
lock	B-Operating_System
is	O
a	O
kind	O
of	O
busy	B-Operating_System
waiting	I-Operating_System
.	O
</s>
<s>
Once	O
acquired	O
,	O
spinlocks	B-Operating_System
will	O
usually	O
be	O
held	O
until	O
they	O
are	O
explicitly	O
released	O
,	O
although	O
in	O
some	O
implementations	O
they	O
may	O
be	O
automatically	O
released	O
if	O
the	O
thread	B-Operating_System
being	O
waited	O
on	O
(	O
the	O
one	O
that	O
holds	O
the	O
lock	B-Operating_System
)	O
blocks	O
or	O
"	O
goes	O
to	O
sleep	O
"	O
.	O
</s>
<s>
Because	O
they	O
avoid	O
overhead	O
from	O
operating	B-General_Concept
system	I-General_Concept
process	O
rescheduling	O
or	O
context	B-Operating_System
switching	I-Operating_System
,	O
spinlocks	B-Operating_System
are	O
efficient	O
if	O
threads	B-Operating_System
are	O
likely	O
to	O
be	O
blocked	O
for	O
only	O
short	O
periods	O
.	O
</s>
<s>
For	O
this	O
reason	O
,	O
operating-system	B-Operating_System
kernels	I-Operating_System
often	O
use	O
spinlocks	B-Operating_System
.	O
</s>
<s>
However	O
,	O
spinlocks	B-Operating_System
become	O
wasteful	O
if	O
held	O
for	O
longer	O
durations	O
,	O
as	O
they	O
may	O
prevent	O
other	O
threads	B-Operating_System
from	O
running	O
and	O
require	O
rescheduling	O
.	O
</s>
<s>
The	O
longer	O
a	O
thread	B-Operating_System
holds	O
a	O
lock	B-Operating_System
,	O
the	O
greater	O
the	O
risk	O
that	O
the	O
thread	B-Operating_System
will	O
be	O
interrupted	O
by	O
the	O
OS	O
scheduler	O
while	O
holding	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
If	O
this	O
happens	O
,	O
other	O
threads	B-Operating_System
will	O
be	O
left	O
"	O
spinning	O
"	O
(	O
repeatedly	O
trying	O
to	O
acquire	O
the	O
lock	B-Operating_System
)	O
,	O
while	O
the	O
thread	B-Operating_System
holding	O
the	O
lock	B-Operating_System
is	O
not	O
making	O
progress	O
towards	O
releasing	O
it	O
.	O
</s>
<s>
The	O
result	O
is	O
an	O
indefinite	O
postponement	O
until	O
the	O
thread	B-Operating_System
holding	O
the	O
lock	B-Operating_System
can	O
finish	O
and	O
release	O
it	O
.	O
</s>
<s>
This	O
is	O
especially	O
true	O
on	O
a	O
single-processor	O
system	O
,	O
where	O
each	O
waiting	B-Operating_System
thread	B-Operating_System
of	O
the	O
same	O
priority	O
is	O
likely	O
to	O
waste	O
its	O
quantum	O
(	O
allocated	O
time	O
where	O
a	O
thread	B-Operating_System
can	O
run	O
)	O
spinning	O
until	O
the	O
thread	B-Operating_System
that	O
holds	O
the	O
lock	B-Operating_System
is	O
finally	O
finished	O
.	O
</s>
<s>
Implementing	O
spinlocks	B-Operating_System
correctly	O
is	O
challenging	O
because	O
programmers	O
must	O
take	O
into	O
account	O
the	O
possibility	O
of	O
simultaneous	O
access	O
to	O
the	O
lock	B-Operating_System
,	O
which	O
could	O
cause	O
race	B-Operating_System
conditions	I-Operating_System
.	O
</s>
<s>
Generally	O
,	O
such	O
an	O
implementation	O
is	O
possible	O
only	O
with	O
special	O
assembly	B-Language
language	I-Language
instructions	O
,	O
such	O
as	O
atomic	B-General_Concept
(	O
i.e.	O
</s>
<s>
un-interruptible	O
)	O
test-and-set	B-Operating_System
operations	O
and	O
cannot	O
be	O
easily	O
implemented	O
in	O
programming	O
languages	O
not	O
supporting	O
truly	O
atomic	B-General_Concept
operations	I-General_Concept
.	O
</s>
<s>
On	O
architectures	O
without	O
such	O
operations	O
,	O
or	O
if	O
high-level	O
language	O
implementation	O
is	O
required	O
,	O
a	O
non-atomic	O
locking	B-Operating_System
algorithm	O
may	O
be	O
used	O
,	O
e.g.	O
</s>
<s>
Peterson	B-Operating_System
's	I-Operating_System
algorithm	I-Operating_System
.	O
</s>
<s>
However	O
,	O
such	O
an	O
implementation	O
may	O
require	O
more	O
memory	B-General_Concept
than	O
a	O
spinlock	B-Operating_System
,	O
be	O
slower	O
to	O
allow	O
progress	O
after	O
unlocking	O
,	O
and	O
may	O
not	O
be	O
implementable	O
in	O
a	O
high-level	O
language	O
if	O
out-of-order	B-General_Concept
execution	I-General_Concept
is	O
allowed	O
.	O
</s>
<s>
The	O
following	O
example	O
uses	O
x86	O
assembly	B-Language
language	I-Language
to	O
implement	O
a	O
spinlock	B-Operating_System
.	O
</s>
<s>
It	O
will	O
work	O
on	O
any	O
Intel	B-General_Concept
80386	I-General_Concept
compatible	O
processor	O
.	O
</s>
<s>
This	O
is	O
due	O
to	O
subtle	O
memory	B-General_Concept
ordering	I-General_Concept
rules	O
which	O
support	O
this	O
,	O
even	O
though	O
MOV	O
is	O
not	O
a	O
full	O
memory	B-General_Concept
barrier	I-General_Concept
.	O
</s>
<s>
However	O
,	O
some	O
processors	O
(	O
some	O
Cyrix	O
processors	O
,	O
some	O
revisions	O
of	O
the	O
Intel	B-Device
Pentium	I-Device
Pro	I-Device
(	O
due	O
to	O
bugs	O
)	O
,	O
and	O
earlier	O
Pentium	B-Device
and	O
i486	B-General_Concept
SMP	B-Operating_System
systems	O
)	O
will	O
do	O
the	O
wrong	O
thing	O
and	O
data	O
protected	O
by	O
the	O
lock	B-Operating_System
could	O
be	O
corrupted	O
.	O
</s>
<s>
On	O
most	O
non-x86	O
architectures	O
,	O
explicit	O
memory	B-General_Concept
barrier	I-General_Concept
or	O
atomic	B-General_Concept
instructions	I-General_Concept
(	O
as	O
in	O
the	O
example	O
)	O
must	O
be	O
used	O
.	O
</s>
<s>
On	O
some	O
systems	O
,	O
such	O
as	O
IA-64	B-General_Concept
,	O
there	O
are	O
special	O
"	O
unlock	O
"	O
instructions	O
which	O
provide	O
the	O
needed	O
memory	B-General_Concept
ordering	I-General_Concept
.	O
</s>
<s>
To	O
reduce	O
inter-CPU	O
bus	B-General_Concept
traffic	I-General_Concept
,	O
code	O
trying	O
to	O
acquire	O
a	O
lock	B-Operating_System
should	O
loop	O
reading	O
without	O
trying	O
to	O
write	O
anything	O
until	O
it	O
reads	O
a	O
changed	O
value	O
.	O
</s>
<s>
Because	O
of	O
MESI	B-General_Concept
caching	O
protocols	O
,	O
this	O
causes	O
the	O
cache	O
line	O
for	O
the	O
lock	B-Operating_System
to	O
become	O
"	O
Shared	O
"	O
;	O
then	O
there	O
is	O
remarkably	O
no	O
bus	B-General_Concept
traffic	I-General_Concept
while	O
a	O
CPU	O
waits	O
for	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
This	O
optimization	O
is	O
effective	O
on	O
all	O
CPU	O
architectures	O
that	O
have	O
a	O
cache	O
per	O
CPU	O
,	O
because	O
MESI	B-General_Concept
is	O
so	O
widespread	O
.	O
</s>
<s>
On	O
Hyper-Threading	O
CPUs	O
,	O
pausing	O
with	O
rep	O
nop	O
gives	O
additional	O
performance	O
by	O
hinting	O
the	O
core	O
that	O
it	O
can	O
work	O
on	O
the	O
other	O
thread	B-Operating_System
while	O
the	O
lock	B-Operating_System
spins	O
waiting	B-Operating_System
.	O
</s>
<s>
Transactional	B-Operating_System
Synchronization	I-Operating_System
Extensions	I-Operating_System
and	O
other	O
hardware	B-Operating_System
transactional	I-Operating_System
memory	I-Operating_System
instruction	O
sets	O
serve	O
to	O
replace	O
locks	O
in	O
most	O
cases	O
.	O
</s>
<s>
Although	O
locks	O
are	O
still	O
required	O
as	O
a	O
fallback	O
,	O
they	O
have	O
the	O
potential	O
to	O
greatly	O
improve	O
performance	O
by	O
having	O
the	O
processor	O
handle	O
entire	O
blocks	O
of	O
atomic	B-General_Concept
operations	I-General_Concept
.	O
</s>
<s>
This	O
feature	O
is	O
built	O
into	O
some	O
mutex	B-Operating_System
implementations	O
,	O
for	O
example	O
in	O
glibc	B-Language
.	O
</s>
<s>
The	O
Hardware	O
Lock	B-Operating_System
Elision	O
(	O
HLE	O
)	O
in	O
x86	O
is	O
a	O
weakened	O
but	O
backwards-compatible	O
version	O
of	O
TSE	O
,	O
and	O
we	O
can	O
use	O
it	O
here	O
for	O
locking	B-Operating_System
without	O
losing	O
any	O
compatibility	O
.	O
</s>
<s>
In	O
this	O
particular	O
case	O
,	O
the	O
processor	O
can	O
choose	O
to	O
not	O
lock	B-Operating_System
until	O
two	O
threads	B-Operating_System
actually	O
conflict	O
with	O
each	O
other	O
.	O
</s>
<s>
On	O
any	O
multi-processor	O
system	O
that	O
uses	O
the	O
MESI	B-General_Concept
contention	O
protocol	O
,	O
</s>
<s>
such	O
a	O
test-and-test-and-set	O
lock	B-Operating_System
(	O
TTAS	O
)	O
performs	O
much	O
better	O
than	O
the	O
simple	O
test-and-set	B-Operating_System
lock	B-Operating_System
(	O
TAS	O
)	O
approach	O
.	O
</s>
<s>
adding	O
a	O
random	O
exponential	B-Algorithm
backoff	I-Algorithm
delay	O
before	O
re-checking	O
the	O
lock	B-Operating_System
performs	O
even	O
better	O
than	O
TTAS	O
.	O
</s>
<s>
A	O
few	O
multi-core	O
processors	O
have	O
a	O
"	O
power-conscious	O
spin-lock	O
"	O
instruction	O
that	O
puts	O
a	O
processor	O
to	O
sleep	O
,	O
then	O
wakes	O
it	O
up	O
on	O
the	O
next	O
cycle	O
after	O
the	O
lock	B-Operating_System
is	O
freed	O
.	O
</s>
<s>
A	O
spin-lock	O
using	O
such	O
instructions	O
is	O
more	O
efficient	O
and	O
uses	O
less	O
energy	O
than	O
spin	O
locks	O
with	O
or	O
without	O
a	O
back-off	O
loop	O
.	O
</s>
<s>
The	O
primary	O
disadvantage	O
of	O
a	O
spinlock	B-Operating_System
is	O
that	O
,	O
while	O
waiting	B-Operating_System
to	O
acquire	O
a	O
lock	B-Operating_System
,	O
it	O
wastes	O
time	O
that	O
might	O
be	O
productively	O
spent	O
elsewhere	O
.	O
</s>
<s>
Do	O
not	O
acquire	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
In	O
many	O
situations	O
it	O
is	O
possible	O
to	O
design	O
data	O
structures	O
that	O
do	B-Operating_System
not	I-Operating_System
require	I-Operating_System
locking	I-Operating_System
,	O
e.g.	O
</s>
<s>
by	O
using	O
per-thread	O
or	O
per-CPU	O
data	O
and	O
disabling	O
interrupts	B-Application
.	O
</s>
<s>
Switch	B-Operating_System
to	O
a	O
different	O
thread	B-Operating_System
while	O
waiting	B-Operating_System
.	O
</s>
<s>
This	O
typically	O
involves	O
attaching	O
the	O
current	O
thread	B-Operating_System
to	O
a	O
queue	O
of	O
threads	B-Operating_System
waiting	B-Operating_System
for	O
the	O
lock	B-Operating_System
,	O
followed	O
by	O
switching	O
to	O
another	O
thread	B-Operating_System
that	O
is	O
ready	O
to	O
do	O
some	O
useful	O
work	O
.	O
</s>
<s>
This	O
scheme	O
also	O
has	O
the	O
advantage	O
that	O
it	O
guarantees	O
that	O
resource	B-Operating_System
starvation	I-Operating_System
does	O
not	O
occur	O
as	O
long	O
as	O
all	O
threads	B-Operating_System
eventually	O
relinquish	O
locks	O
they	O
acquire	O
and	O
scheduling	O
decisions	O
can	O
be	O
made	O
about	O
which	O
thread	B-Operating_System
should	O
progress	O
first	O
.	O
</s>
<s>
Spinlocks	B-Operating_System
that	O
never	O
entail	O
switching	O
,	O
usable	O
by	O
real-time	B-Operating_System
operating	I-Operating_System
systems	I-Operating_System
,	O
are	O
sometimes	O
called	O
raw	O
spinlocks	B-Operating_System
.	O
</s>
<s>
Most	O
operating	B-General_Concept
systems	I-General_Concept
(	O
including	O
Solaris	B-Application
,	O
Mac	B-Operating_System
OS	I-Operating_System
X	I-Operating_System
and	O
FreeBSD	B-Operating_System
)	O
use	O
a	O
hybrid	O
approach	O
called	O
"	O
adaptive	O
mutex	B-Operating_System
"	O
.	O
</s>
<s>
The	O
idea	O
is	O
to	O
use	O
a	O
spinlock	B-Operating_System
when	O
trying	O
to	O
access	O
a	O
resource	O
locked	O
by	O
a	O
currently-running	O
thread	B-Operating_System
,	O
but	O
to	O
sleep	O
if	O
the	O
thread	B-Operating_System
is	O
not	O
currently	O
running	O
.	O
</s>
<s>
OpenBSD	B-Operating_System
attempted	O
to	O
replace	O
spinlocks	B-Operating_System
with	O
ticket	B-Operating_System
locks	I-Operating_System
which	O
enforced	O
first-in-first-out	B-Operating_System
behaviour	O
,	O
however	O
this	O
resulted	O
in	O
more	O
CPU	O
usage	O
in	O
the	O
kernel	B-Operating_System
and	O
larger	O
applications	O
,	O
such	O
as	O
Firefox	B-Application
,	O
becoming	O
much	O
slower	O
.	O
</s>
