<s>
In	O
concurrent	B-Architecture
programming	I-Architecture
,	O
an	O
operation	O
(	O
or	O
set	O
of	O
operations	O
)	O
is	O
linearizable	B-General_Concept
if	O
it	O
consists	O
of	O
an	O
ordered	O
list	O
of	O
invocation	B-General_Concept
and	O
response	O
events	O
,	O
that	O
may	O
be	O
extended	O
by	O
adding	O
response	O
events	O
such	O
that	O
:	O
</s>
<s>
The	O
extended	O
list	O
can	O
be	O
re-expressed	O
as	O
a	O
sequential	O
history	O
(	O
is	O
serializable	B-Operating_System
)	O
.	O
</s>
<s>
Informally	O
,	O
this	O
means	O
that	O
the	O
unmodified	O
list	O
of	O
events	O
is	O
linearizable	B-General_Concept
if	O
and	O
only	O
if	O
its	O
invocations	O
were	O
serializable	B-Operating_System
,	O
but	O
some	O
of	O
the	O
responses	O
of	O
the	O
serial	O
schedule	O
have	O
yet	O
to	O
return	O
.	O
</s>
<s>
Because	O
multiple	O
processes	O
are	O
accessing	O
a	O
single	O
object	O
,	O
there	O
may	O
arise	O
a	O
situation	O
in	O
which	O
while	O
one	O
process	B-Operating_System
is	O
accessing	O
the	O
object	O
,	O
another	O
process	B-Operating_System
changes	O
its	O
contents	O
.	O
</s>
<s>
Making	O
a	O
system	O
linearizable	B-General_Concept
is	O
one	O
solution	O
to	O
this	O
problem	O
.	O
</s>
<s>
In	O
a	O
linearizable	B-General_Concept
system	O
,	O
although	O
operations	O
overlap	O
on	O
a	O
shared	O
object	O
,	O
each	O
operation	O
appears	O
to	O
take	O
place	O
instantaneously	O
.	O
</s>
<s>
Linearizability	B-General_Concept
is	O
a	O
strong	O
correctness	O
condition	O
,	O
which	O
constrains	O
what	O
outputs	O
are	O
possible	O
when	O
an	O
object	O
is	O
accessed	O
by	O
multiple	O
processes	O
concurrently	O
.	O
</s>
<s>
If	O
a	O
system	O
is	O
linearizable	B-General_Concept
it	O
allows	O
a	O
programmer	O
to	O
reason	O
about	O
the	O
system	O
.	O
</s>
<s>
Linearizability	B-General_Concept
was	O
first	O
introduced	O
as	O
a	O
consistency	B-General_Concept
model	I-General_Concept
by	O
Herlihy	O
and	O
Wing	O
in	O
1987	O
.	O
</s>
<s>
It	O
encompassed	O
more	O
restrictive	O
definitions	O
of	O
atomic	O
,	O
such	O
as	O
"	O
an	O
atomic	B-General_Concept
operation	I-General_Concept
is	O
one	O
which	O
cannot	O
be	O
(	O
or	O
is	O
not	O
)	O
interrupted	O
by	O
concurrent	O
operations	O
"	O
,	O
which	O
are	O
usually	O
vague	O
about	O
when	O
an	O
operation	O
is	O
considered	O
to	O
begin	O
and	O
end	O
.	O
</s>
<s>
Specifically	O
,	O
linearizability	B-General_Concept
guarantees	O
that	O
the	O
invariants	B-Application
of	O
a	O
system	O
are	O
observed	O
and	O
preserved	O
by	O
all	O
operations	O
:	O
if	O
all	O
operations	O
individually	O
preserve	O
an	O
invariant	B-Application
,	O
the	O
system	O
as	O
a	O
whole	O
will	O
.	O
</s>
<s>
Linearizability	B-General_Concept
is	O
important	O
in	O
these	O
concurrent	O
systems	O
where	O
objects	O
may	O
be	O
accessed	O
by	O
multiple	O
processes	O
at	O
the	O
same	O
time	O
and	O
a	O
programmer	O
needs	O
to	O
be	O
able	O
to	O
reason	O
about	O
the	O
expected	O
results	O
.	O
</s>
<s>
A	O
history	O
is	O
a	O
sequence	O
of	O
invocations	O
and	O
responses	O
made	O
of	O
an	O
object	O
by	O
a	O
set	O
of	O
threads	B-Operating_System
or	O
processes	O
.	O
</s>
<s>
An	O
invocation	B-General_Concept
can	O
be	O
thought	O
of	O
as	O
the	O
start	O
of	O
an	O
operation	O
,	O
and	O
the	O
response	O
being	O
the	O
signaled	O
end	O
of	O
that	O
operation	O
.	O
</s>
<s>
Each	O
invocation	B-General_Concept
of	O
a	O
function	O
will	O
have	O
a	O
subsequent	O
response	O
.	O
</s>
<s>
Suppose	O
,	O
for	O
example	O
,	O
that	O
two	O
threads	B-Operating_System
,	O
A	O
and	O
B	O
,	O
both	O
attempt	O
to	O
grab	O
a	O
lock	B-Operating_System
,	O
backing	O
off	O
if	O
it	O
's	O
already	O
taken	O
.	O
</s>
<s>
This	O
would	O
be	O
modeled	O
as	O
both	O
threads	B-Operating_System
invoking	O
the	O
lock	B-Operating_System
operation	O
,	O
then	O
both	O
threads	B-Operating_System
receiving	O
a	O
response	O
,	O
one	O
successful	O
,	O
one	O
not	O
.	O
</s>
<s>
A	O
sequential	O
history	O
is	O
one	O
in	O
which	O
all	O
invocations	O
have	O
immediate	O
responses	O
;	O
that	O
is	O
the	O
invocation	B-General_Concept
and	O
response	O
are	O
considered	O
to	O
take	O
place	O
instantaneously	O
.	O
</s>
<s>
A	O
sequential	O
history	O
should	O
be	O
trivial	O
to	O
reason	O
about	O
,	O
as	O
it	O
has	O
no	O
real	O
concurrency	B-Architecture
;	O
the	O
previous	O
example	O
was	O
not	O
sequential	O
,	O
and	O
thus	O
is	O
hard	O
to	O
reason	O
about	O
.	O
</s>
<s>
This	O
is	O
where	O
linearizability	B-General_Concept
comes	O
in	O
.	O
</s>
<s>
A	O
history	O
is	O
linearizable	B-General_Concept
if	O
there	O
is	O
a	O
linear	O
order	O
of	O
the	O
completed	O
operations	O
such	O
that	O
:	O
</s>
<s>
if	O
a	O
response	O
preceded	O
an	O
invocation	B-General_Concept
in	O
the	O
original	O
history	O
,	O
it	O
must	O
still	O
precede	O
it	O
in	O
the	O
sequential	O
reordering	O
.	O
</s>
<s>
(	O
Note	O
that	O
the	O
first	O
two	O
bullet	O
points	O
here	O
match	O
serializability	B-Operating_System
:	O
the	O
operations	O
appear	O
to	O
happen	O
in	O
some	O
order	O
.	O
</s>
<s>
It	O
is	O
the	O
last	O
point	O
which	O
is	O
unique	O
to	O
linearizability	B-General_Concept
,	O
and	O
is	O
thus	O
the	O
major	O
contribution	O
of	O
Herlihy	O
and	O
Wing	O
.	O
)	O
</s>
<s>
Let	O
us	O
look	O
at	O
two	O
ways	O
of	O
reordering	O
the	O
locking	B-Operating_System
example	O
above	O
.	O
</s>
<s>
Reordering	O
B	O
's	O
invocation	B-General_Concept
below	O
A	O
's	O
response	O
yields	O
a	O
sequential	O
history	O
.	O
</s>
<s>
Unfortunately	O
,	O
it	O
does	O
n't	O
match	O
the	O
sequential	O
definition	O
of	O
the	O
object	O
(	O
it	O
does	O
n't	O
match	O
the	O
semantics	O
of	O
the	O
program	O
)	O
:	O
A	O
should	O
have	O
successfully	O
obtained	O
the	O
lock	B-Operating_System
,	O
and	O
B	O
should	O
have	O
subsequently	O
aborted	O
.	O
</s>
<s>
Note	O
that	O
the	O
definition	O
of	O
linearizability	B-General_Concept
only	O
precludes	O
responses	O
that	O
precede	O
invocations	O
from	O
being	O
reordered	O
;	O
since	O
the	O
original	O
history	O
had	O
no	O
responses	O
before	O
invocations	O
,	O
we	O
can	O
reorder	O
it	O
as	O
we	O
wish	O
.	O
</s>
<s>
Hence	O
the	O
original	O
history	O
is	O
indeed	O
linearizable	B-General_Concept
.	O
</s>
<s>
An	O
object	O
(	O
as	O
opposed	O
to	O
a	O
history	O
)	O
is	O
linearizable	B-General_Concept
if	O
all	O
valid	O
histories	O
of	O
its	O
use	O
can	O
be	O
linearized	O
.	O
</s>
<s>
Consider	O
the	O
following	O
history	O
,	O
again	O
of	O
two	O
objects	O
interacting	O
with	O
a	O
lock	B-Operating_System
:	O
</s>
<s>
This	O
history	O
is	O
not	O
valid	O
because	O
there	O
is	O
a	O
point	O
at	O
which	O
both	O
A	O
and	O
B	O
hold	O
the	O
lock	B-Operating_System
;	O
moreover	O
,	O
it	O
cannot	O
be	O
reordered	O
to	O
a	O
valid	O
sequential	O
history	O
without	O
violating	O
the	O
ordering	O
rule	O
.	O
</s>
<s>
Therefore	O
,	O
it	O
is	O
not	O
linearizable	B-General_Concept
.	O
</s>
<s>
However	O
,	O
under	O
serializability	B-Operating_System
,	O
B	O
's	O
unlock	O
operation	O
may	O
be	O
moved	O
to	O
before	O
A	O
's	O
original	O
lock	B-Operating_System
,	O
which	O
is	O
a	O
valid	O
history	O
(	O
assuming	O
the	O
object	O
begins	O
the	O
history	O
in	O
a	O
locked	O
state	O
)	O
:	O
</s>
<s>
This	O
reordering	O
is	O
sensible	O
provided	O
there	O
is	O
no	O
alternative	O
means	O
of	O
communicating	O
between	O
A	O
and	O
B	O
.	O
Linearizability	B-General_Concept
is	O
better	O
when	O
considering	O
individual	O
objects	O
separately	O
,	O
as	O
the	O
reordering	O
restrictions	O
ensure	O
that	O
multiple	O
linearizable	B-General_Concept
objects	O
are	O
,	O
considered	O
as	O
a	O
whole	O
,	O
still	O
linearizable	B-General_Concept
.	O
</s>
<s>
This	O
definition	O
of	O
linearizability	B-General_Concept
is	O
equivalent	O
to	O
the	O
following	O
:	O
</s>
<s>
All	O
function	O
calls	O
have	O
a	O
linearization	O
point	O
at	O
some	O
instant	O
between	O
their	O
invocation	B-General_Concept
and	O
their	O
response	O
.	O
</s>
<s>
This	O
property	O
of	O
occurring	O
instantaneously	O
,	O
or	O
indivisibly	O
,	O
leads	O
to	O
the	O
use	O
of	O
the	O
term	O
atomic	O
as	O
an	O
alternative	O
to	O
the	O
longer	O
"	O
linearizable	B-General_Concept
"	O
.	O
</s>
<s>
In	O
the	O
examples	O
below	O
,	O
the	O
linearization	O
point	O
of	O
the	O
counter	O
built	O
on	O
compare-and-swap	B-Operating_System
is	O
the	O
linearization	O
point	O
of	O
the	O
first	O
(	O
and	O
only	O
)	O
successful	O
compare-and-swap	B-Operating_System
update	O
.	O
</s>
<s>
The	O
counter	O
built	O
using	O
locking	B-Operating_System
can	O
be	O
considered	O
to	O
linearize	O
at	O
any	O
moment	O
while	O
the	O
locks	B-Operating_System
are	O
held	O
,	O
since	O
any	O
potentially	O
conflicting	O
operations	O
are	O
excluded	O
from	O
running	O
during	O
that	O
period	O
.	O
</s>
<s>
Processors	B-General_Concept
have	O
instructions	O
that	O
can	O
be	O
used	O
to	O
implement	O
locking	B-Operating_System
and	O
lock-free	B-Operating_System
and	I-Operating_System
wait-free	I-Operating_System
algorithms	I-Operating_System
.	O
</s>
<s>
The	O
ability	O
to	O
temporarily	O
inhibit	O
interrupts	B-Application
,	O
ensuring	O
that	O
the	O
currently	O
running	O
process	B-Operating_System
cannot	O
be	O
context	O
switched	O
,	O
also	O
suffices	O
on	O
a	O
uniprocessor	B-Operating_System
.	O
</s>
<s>
atomic	O
swap	O
(	O
the	O
RDLK	O
instruction	O
in	O
some	O
Burroughs	O
mainframes	O
,	O
and	O
the	O
XCHG	O
x86	B-Device
instruction	I-Device
)	O
;	O
</s>
<s>
test-and-set	B-Operating_System
;	O
</s>
<s>
fetch-and-add	B-Operating_System
;	O
</s>
<s>
compare-and-swap	B-Operating_System
;	O
</s>
<s>
load-link/store	B-Operating_System
-conditional	I-Operating_System
.	O
</s>
<s>
Most	O
processors	B-General_Concept
include	O
store	O
operations	O
that	O
are	O
not	O
atomic	O
with	O
respect	O
to	O
memory	O
.	O
</s>
<s>
Should	O
a	O
high	O
priority	O
interrupt	B-Application
occur	O
when	O
a	O
portion	O
of	O
the	O
store	O
is	O
complete	O
,	O
the	O
operation	O
must	O
be	O
completed	O
when	O
the	O
interrupt	B-Application
level	O
is	O
returned	O
.	O
</s>
<s>
The	O
routine	O
that	O
processes	O
the	O
interrupt	B-Application
must	O
not	O
modify	O
the	O
memory	O
being	O
changed	O
.	O
</s>
<s>
It	O
is	O
important	O
to	O
take	O
this	O
into	O
account	O
when	O
writing	O
interrupt	B-Application
routines	O
.	O
</s>
<s>
When	O
there	O
are	O
multiple	O
instructions	O
which	O
must	O
be	O
completed	O
without	O
interruption	O
,	O
a	O
CPU	O
instruction	O
which	O
temporarily	O
disables	O
interrupts	B-Application
is	O
used	O
.	O
</s>
<s>
This	O
must	O
be	O
kept	O
to	O
only	O
a	O
few	O
instructions	O
and	O
the	O
interrupts	B-Application
must	O
be	O
re-enabled	O
to	O
avoid	O
unacceptable	O
response	O
time	O
to	O
interrupts	B-Application
or	O
even	O
losing	O
interrupts	B-Application
.	O
</s>
<s>
This	O
mechanism	O
is	O
not	O
sufficient	O
in	O
a	O
multi-processor	O
environment	O
since	O
each	O
CPU	O
can	O
interfere	O
with	O
the	O
process	B-Operating_System
regardless	O
of	O
whether	O
interrupts	B-Application
occur	O
or	O
not	O
.	O
</s>
<s>
Further	O
,	O
in	O
the	O
presence	O
of	O
an	O
instruction	B-General_Concept
pipeline	I-General_Concept
,	O
uninterruptible	O
operations	O
present	O
a	O
security	O
risk	O
,	O
as	O
they	O
can	O
potentially	O
be	O
chained	O
in	O
an	O
infinite	B-Algorithm
loop	I-Algorithm
to	O
create	O
a	O
denial	O
of	O
service	O
attack	O
,	O
as	O
in	O
the	O
Cyrix	B-Error_Name
coma	I-Error_Name
bug	I-Error_Name
.	O
</s>
<s>
The	O
C	B-Language
standard	I-Language
and	O
SUSv3	O
provide	O
sig_atomic_t	O
for	O
simple	O
atomic	O
reads	O
and	O
writes	O
;	O
incrementing	O
or	O
decrementing	O
is	O
not	O
guaranteed	O
to	O
be	O
atomic	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>
The	O
ARM	B-Architecture
instruction	I-Architecture
set	I-Architecture
provides	O
LDREX	O
and	O
STREX	O
instructions	O
which	O
can	O
be	O
used	O
to	O
implement	O
atomic	O
memory	O
access	O
by	O
using	O
exclusive	O
monitors	O
implemented	O
in	O
the	O
processor	O
to	O
track	O
memory	O
accesses	O
for	O
a	O
specific	O
address	O
.	O
</s>
<s>
However	O
,	O
if	O
a	O
context	B-Operating_System
switch	I-Operating_System
occurs	O
between	O
calls	O
to	O
LDREX	O
and	O
STREX	O
,	O
the	O
documentation	O
notes	O
that	O
STREX	O
will	O
fail	O
,	O
indicating	O
the	O
operation	O
should	O
be	O
retried	O
.	O
</s>
<s>
The	O
easiest	O
way	O
to	O
achieve	O
linearizability	B-General_Concept
is	O
running	O
groups	O
of	O
primitive	O
operations	O
in	O
a	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Strictly	O
,	O
independent	O
operations	O
can	O
then	O
be	O
carefully	O
permitted	O
to	O
overlap	O
their	O
critical	B-Operating_System
sections	I-Operating_System
,	O
provided	O
this	O
does	O
not	O
violate	O
linearizability	B-General_Concept
.	O
</s>
<s>
Such	O
an	O
approach	O
must	O
balance	O
the	O
cost	O
of	O
large	O
numbers	O
of	O
locks	B-Operating_System
against	O
the	O
benefits	O
of	O
increased	O
parallelism	O
.	O
</s>
<s>
Another	O
approach	O
,	O
favoured	O
by	O
researchers	O
(	O
but	O
not	O
yet	O
widely	O
used	O
in	O
the	O
software	O
industry	O
)	O
,	O
is	O
to	O
design	O
a	O
linearizable	B-General_Concept
object	O
using	O
the	O
native	O
atomic	B-General_Concept
primitives	I-General_Concept
provided	O
by	O
the	O
hardware	O
.	O
</s>
<s>
A	O
promising	O
hybrid	O
of	O
these	O
two	O
is	O
to	O
provide	O
a	O
transactional	B-Operating_System
memory	I-Operating_System
abstraction	O
.	O
</s>
<s>
As	O
with	O
critical	B-Operating_System
sections	I-Operating_System
,	O
the	O
user	O
marks	O
sequential	O
code	O
that	O
must	O
be	O
run	O
in	O
isolation	O
from	O
other	O
threads	B-Operating_System
.	O
</s>
<s>
This	O
style	O
of	O
abstraction	O
is	O
common	O
when	O
interacting	O
with	O
databases	O
;	O
for	O
instance	O
,	O
when	O
using	O
the	B-Application
Spring	I-Application
Framework	I-Application
,	O
annotating	O
a	O
method	O
with	O
@Transactional	O
will	O
ensure	O
all	O
enclosed	O
database	O
interactions	O
occur	O
in	O
a	O
single	O
database	B-General_Concept
transaction	I-General_Concept
.	O
</s>
<s>
Transactional	B-Operating_System
memory	I-Operating_System
goes	O
a	O
step	O
further	O
,	O
ensuring	O
that	O
all	O
memory	O
interactions	O
occur	O
atomically	O
.	O
</s>
<s>
As	O
with	O
database	B-General_Concept
transactions	I-General_Concept
,	O
issues	O
arise	O
regarding	O
composition	O
of	O
transactions	O
,	O
especially	O
database	O
and	O
in-memory	O
transactions	O
.	O
</s>
<s>
A	O
common	O
theme	O
when	O
designing	O
linearizable	B-General_Concept
objects	O
is	O
to	O
provide	O
an	O
all-or-nothing	O
interface	O
:	O
either	O
an	O
operation	O
succeeds	O
completely	O
,	O
or	O
it	O
fails	O
and	O
does	O
nothing	O
.	O
</s>
<s>
(	O
ACID	O
databases	O
refer	O
to	O
this	O
principle	O
as	O
atomicity	B-General_Concept
.	O
)	O
</s>
<s>
Compare-and-swap	B-Operating_System
writes	O
a	O
new	O
value	O
into	O
a	O
location	O
only	O
if	O
the	O
latter	O
's	O
contents	O
matches	O
a	O
supplied	O
old	O
value	O
.	O
</s>
<s>
This	O
is	O
commonly	O
used	O
in	O
a	O
read-modify-CAS	O
sequence	O
:	O
the	O
user	O
reads	O
the	O
location	O
,	O
computes	O
a	O
new	O
value	O
to	O
write	O
,	O
and	O
writes	O
it	O
with	O
a	O
CAS	O
(	O
compare-and-swap	B-Operating_System
)	O
;	O
if	O
the	O
value	O
changes	O
concurrently	O
,	O
the	O
CAS	O
will	O
fail	O
and	O
the	O
user	O
tries	O
again	O
.	O
</s>
<s>
Load-link/store	B-Operating_System
-conditional	I-Operating_System
encodes	O
this	O
pattern	O
more	O
directly	O
:	O
the	O
user	O
reads	O
the	O
location	O
with	O
load-link	O
,	O
computes	O
a	O
new	O
value	O
to	O
write	O
,	O
and	O
writes	O
it	O
with	O
store-conditional	O
;	O
if	O
the	O
value	O
has	O
changed	O
concurrently	O
,	O
the	O
SC	O
(	O
store-conditional	O
)	O
will	O
fail	O
and	O
the	O
user	O
tries	O
again	O
.	O
</s>
<s>
In	O
a	O
database	B-General_Concept
transaction	I-General_Concept
,	O
if	O
the	O
transaction	O
cannot	O
be	O
completed	O
due	O
to	O
a	O
concurrent	O
operation	O
(	O
e.g.	O
</s>
<s>
in	O
a	O
deadlock	B-Operating_System
)	O
,	O
the	O
transaction	O
will	O
be	O
aborted	O
and	O
the	O
user	O
must	O
try	O
again	O
.	O
</s>
<s>
To	O
demonstrate	O
the	O
power	O
and	O
necessity	O
of	O
linearizability	B-General_Concept
we	O
will	O
consider	O
a	O
simple	O
counter	O
which	O
different	O
processes	O
can	O
increment	O
.	O
</s>
<s>
Our	O
first	O
attempt	O
which	O
we	O
will	O
see	O
is	O
non-linearizable	O
has	O
the	O
following	O
implementation	O
using	O
one	O
shared	B-Operating_System
register	I-Operating_System
among	O
the	O
processes	O
.	O
</s>
<s>
This	O
simple	O
implementation	O
is	O
not	O
linearizable	B-General_Concept
,	O
as	O
is	O
demonstrated	O
by	O
the	O
following	O
example	O
.	O
</s>
<s>
The	O
first	O
process	B-Operating_System
reads	O
the	O
value	O
in	O
the	O
register	O
as	O
0	O
.	O
</s>
<s>
The	O
first	O
process	B-Operating_System
adds	O
one	O
to	O
the	O
value	O
,	O
the	O
counter	O
's	O
value	O
should	O
be	O
1	O
,	O
but	O
before	O
it	O
has	O
finished	O
writing	O
the	O
new	O
value	O
back	O
to	O
the	O
register	O
it	O
may	O
become	O
suspended	O
,	O
meanwhile	O
the	O
second	O
process	B-Operating_System
is	O
running	O
:	O
</s>
<s>
The	O
second	O
process	B-Operating_System
reads	O
the	O
value	O
in	O
the	O
register	O
,	O
which	O
is	O
still	O
equal	O
to	O
0	O
;	O
</s>
<s>
The	O
second	O
process	B-Operating_System
adds	O
one	O
to	O
the	O
value	O
;	O
</s>
<s>
the	O
second	O
process	B-Operating_System
writes	O
the	O
new	O
value	O
into	O
the	O
register	O
,	O
the	O
register	O
now	O
has	O
value	O
1	O
.	O
</s>
<s>
The	O
second	O
process	B-Operating_System
is	O
finished	O
running	O
and	O
the	O
first	O
process	B-Operating_System
continues	O
running	O
from	O
where	O
it	O
left	O
off	O
:	O
</s>
<s>
The	O
first	O
process	B-Operating_System
writes	O
1	O
into	O
the	O
register	O
,	O
unaware	O
that	O
the	O
other	O
process	B-Operating_System
has	O
already	O
updated	O
the	O
value	O
in	O
the	O
register	O
to	O
1	O
.	O
</s>
<s>
One	O
of	O
the	O
increment	O
operations	O
was	O
lost	O
as	O
a	O
result	O
of	O
the	O
system	O
not	O
being	O
linearizable	B-General_Concept
.	O
</s>
<s>
The	O
above	O
example	O
shows	O
the	O
need	O
for	O
carefully	O
thinking	O
through	O
implementations	O
of	O
data	O
structures	O
and	O
how	O
linearizability	B-General_Concept
can	O
have	O
an	O
effect	O
on	O
the	O
correctness	O
of	O
the	O
system	O
.	O
</s>
<s>
Each	O
process	B-Operating_System
increments	O
and	O
reads	O
according	O
to	O
the	O
following	O
algorithm	O
:	O
</s>
<s>
For	O
example	O
,	O
reading	O
a	O
64-bit	B-Device
value	O
from	O
memory	O
may	O
actually	O
be	O
implemented	O
as	O
two	O
sequential	O
reads	O
of	O
two	O
32-bit	O
memory	O
locations	O
.	O
</s>
<s>
If	O
a	O
process	B-Operating_System
has	O
only	O
read	O
the	O
first	O
32bits	O
,	O
and	O
before	O
it	O
reads	O
the	O
second	O
32bits	O
the	O
value	O
in	O
memory	O
gets	O
changed	O
,	O
it	O
will	O
have	O
neither	O
the	O
original	O
value	O
nor	O
the	O
new	O
value	O
but	O
a	O
mixed-up	O
value	O
.	O
</s>
<s>
Furthermore	O
,	O
the	O
specific	O
order	O
in	O
which	O
the	O
processes	O
run	O
can	O
change	O
the	O
results	O
,	O
making	O
such	O
an	O
error	O
difficult	O
to	O
detect	O
,	O
reproduce	O
and	O
debug	B-Device
.	O
</s>
<s>
Most	O
systems	O
provide	O
an	O
atomic	O
compare-and-swap	B-Operating_System
instruction	O
that	O
reads	O
from	O
a	O
memory	O
location	O
,	O
compares	O
the	O
value	O
with	O
an	O
"	O
expected	O
"	O
one	O
provided	O
by	O
the	O
user	O
,	O
and	O
writes	O
out	O
a	O
"	O
new	O
"	O
value	O
if	O
the	O
two	O
match	O
,	O
returning	O
whether	O
the	O
update	O
succeeded	O
.	O
</s>
<s>
use	O
compare-and-swap	B-Operating_System
to	O
write	O
the	O
incremented	O
value	O
back	O
;	O
</s>
<s>
retry	O
if	O
the	O
value	O
read	O
in	O
by	O
the	O
compare-and-swap	B-Operating_System
did	O
not	O
match	O
the	O
value	O
we	O
originally	O
read	O
.	O
</s>
<s>
Since	O
the	O
compare-and-swap	B-Operating_System
occurs	O
(	O
or	O
appears	O
to	O
occur	O
)	O
instantaneously	O
,	O
if	O
another	O
process	B-Operating_System
updates	O
the	O
location	O
while	O
we	O
are	O
in-progress	O
,	O
the	O
compare-and-swap	B-Operating_System
is	O
guaranteed	O
to	O
fail	O
.	O
</s>
<s>
Many	O
systems	O
provide	O
an	O
atomic	O
fetch-and-increment	B-Operating_System
instruction	O
that	O
reads	O
from	O
a	O
memory	O
location	O
,	O
unconditionally	O
writes	O
a	O
new	O
value	O
(	O
the	O
old	O
value	O
plus	O
one	O
)	O
,	O
and	O
returns	O
the	O
old	O
value	O
.	O
</s>
<s>
Use	O
fetch-and-increment	B-Operating_System
to	O
read	O
the	O
old	O
value	O
and	O
write	O
the	O
incremented	O
value	O
back	O
.	O
</s>
<s>
Using	O
fetch-and	O
increment	O
is	O
always	O
better	O
(	O
requires	O
fewer	O
memory	O
references	O
)	O
for	O
some	O
algorithms	O
—	O
such	O
as	O
the	O
one	O
shown	O
here	O
—	O
than	O
compare-and-swap	B-Operating_System
,	O
even	O
though	O
Herlihy	O
earlier	O
proved	O
that	O
compare-and-swap	B-Operating_System
is	O
better	O
for	O
certain	O
other	O
algorithms	O
that	O
ca	O
n't	O
be	O
implemented	O
at	O
all	O
using	O
only	O
fetch-and-increment	B-Operating_System
.	O
</s>
<s>
So	O
CPU	B-General_Concept
designs	I-General_Concept
with	O
both	O
fetch-and-increment	B-Operating_System
and	O
compare-and-swap	B-Operating_System
(	O
or	O
equivalent	O
instructions	O
)	O
may	O
be	O
a	O
better	O
choice	O
than	O
ones	O
with	O
only	O
one	O
or	O
the	O
other	O
.	O
</s>
<s>
Another	O
approach	O
is	O
to	O
turn	O
the	O
naive	O
algorithm	O
into	O
a	O
critical	B-Operating_System
section	I-Operating_System
,	O
preventing	O
other	O
threads	B-Operating_System
from	O
disrupting	O
it	O
,	O
using	O
a	O
lock	B-Operating_System
.	O
</s>
<s>
Acquire	O
a	O
lock	B-Operating_System
,	O
excluding	O
other	O
threads	B-Operating_System
from	O
running	O
the	O
critical	B-Operating_System
section	I-Operating_System
(	O
steps	O
2	O
–	O
4	O
)	O
at	O
the	O
same	O
time	O
;	O
</s>
<s>
release	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
This	O
strategy	O
works	O
as	O
expected	O
;	O
the	O
lock	B-Operating_System
prevents	O
other	O
threads	B-Operating_System
from	O
updating	O
the	O
value	O
until	O
it	O
is	O
released	O
.	O
</s>
<s>
However	O
,	O
when	O
compared	O
with	O
direct	O
use	O
of	O
atomic	B-General_Concept
operations	I-General_Concept
,	O
it	O
can	O
suffer	O
from	O
significant	O
overhead	O
due	O
to	O
lock	B-Operating_System
contention	O
.	O
</s>
<s>
To	O
improve	O
program	O
performance	O
,	O
it	O
may	O
therefore	O
be	O
a	O
good	O
idea	O
to	O
replace	O
simple	O
critical	B-Operating_System
sections	I-Operating_System
with	O
atomic	B-General_Concept
operations	I-General_Concept
for	O
non-blocking	B-Operating_System
synchronization	I-Operating_System
(	O
as	O
we	O
have	O
just	O
done	O
for	O
the	O
counter	O
with	O
compare-and-swap	B-Operating_System
and	O
fetch-and-increment	B-Operating_System
)	O
,	O
instead	O
of	O
the	O
other	O
way	O
around	O
,	O
but	O
unfortunately	O
a	O
significant	O
improvement	O
is	O
not	O
guaranteed	O
and	O
lock-free	B-Operating_System
algorithms	I-Operating_System
can	O
easily	O
become	O
too	O
complicated	O
to	O
be	O
worth	O
the	O
effort	O
.	O
</s>
