<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
compare-and-swap	B-Operating_System
(	O
CAS	O
)	O
is	O
an	O
atomic	B-General_Concept
instruction	I-General_Concept
used	O
in	O
multithreading	O
to	O
achieve	O
synchronization	O
.	O
</s>
<s>
It	O
compares	O
the	O
contents	O
of	O
a	O
memory	B-General_Concept
location	I-General_Concept
with	O
a	O
given	O
value	O
and	O
,	O
only	O
if	O
they	O
are	O
the	O
same	O
,	O
modifies	O
the	O
contents	O
of	O
that	O
memory	B-General_Concept
location	I-General_Concept
to	O
a	O
new	O
given	O
value	O
.	O
</s>
<s>
This	O
is	O
done	O
as	O
a	O
single	O
atomic	B-General_Concept
operation	I-General_Concept
.	O
</s>
<s>
The	O
atomicity	B-General_Concept
guarantees	O
that	O
the	O
new	O
value	O
is	O
calculated	O
based	O
on	O
up-to-date	O
information	O
;	O
if	O
the	O
value	O
had	O
been	O
updated	O
by	O
another	O
thread	O
in	O
the	O
meantime	O
,	O
the	O
write	O
would	O
fail	O
.	O
</s>
<s>
The	O
result	O
of	O
the	O
operation	O
must	O
indicate	O
whether	O
it	O
performed	O
the	O
substitution	O
;	O
this	O
can	O
be	O
done	O
either	O
with	O
a	O
simple	O
boolean	O
response	O
(	O
this	O
variant	O
is	O
often	O
called	O
compare-and-set	B-Operating_System
)	O
,	O
or	O
by	O
returning	O
the	O
value	O
read	O
from	O
the	O
memory	B-General_Concept
location	I-General_Concept
(	O
not	O
the	O
value	O
written	O
to	O
it	O
)	O
.	O
</s>
<s>
A	O
compare-and-swap	B-Operating_System
operation	O
is	O
an	O
atomic	B-General_Concept
version	O
of	O
the	O
following	O
pseudocode	B-Language
,	O
where	O
denotes	O
access	O
through	O
a	O
pointer	O
:	O
</s>
<s>
This	O
operation	O
is	O
used	O
to	O
implement	O
synchronization	O
primitives	O
like	O
semaphores	B-Operating_System
and	O
mutexes	B-Operating_System
,	O
as	O
well	O
as	O
more	O
sophisticated	O
lock-free	B-Operating_System
and	I-Operating_System
wait-free	I-Operating_System
algorithms	I-Operating_System
.	O
</s>
<s>
Maurice	O
Herlihy	O
(	O
1991	O
)	O
proved	O
that	O
CAS	O
can	O
implement	O
more	O
of	O
these	O
algorithms	O
than	O
atomic	B-General_Concept
read	O
,	O
write	O
,	O
or	O
fetch-and-add	B-Operating_System
,	O
and	O
assuming	O
a	O
fairly	O
large	O
amount	O
of	O
memory	O
,	O
that	O
it	O
can	O
implement	O
all	O
of	O
them	O
.	O
</s>
<s>
CAS	O
is	O
equivalent	O
to	O
load-link/store	B-Operating_System
-conditional	I-Operating_System
,	O
in	O
the	O
sense	O
that	O
a	O
constant	O
number	O
of	O
invocations	O
of	O
either	O
primitive	O
can	O
be	O
used	O
to	O
implement	O
the	O
other	O
one	O
in	O
a	O
wait-free	B-Operating_System
manner	O
.	O
</s>
<s>
Algorithms	O
built	O
around	O
CAS	O
typically	O
read	O
some	O
key	O
memory	B-General_Concept
location	I-General_Concept
and	O
remember	O
the	O
old	O
value	O
.	O
</s>
<s>
Instead	O
of	O
immediately	O
retrying	O
after	O
a	O
CAS	O
operation	O
fails	O
,	O
researchers	O
have	O
found	O
that	O
total	O
system	O
performance	O
can	O
be	O
improved	O
in	O
multiprocessor	B-Operating_System
systems	O
—	O
where	O
many	O
threads	O
constantly	O
update	O
some	O
particular	O
shared	O
variable	O
—	O
if	O
threads	O
that	O
see	O
their	O
CAS	O
fail	O
use	O
exponential	B-Algorithm
backoff	I-Algorithm
—	O
in	O
other	O
words	O
,	O
wait	O
a	O
little	O
before	O
retrying	O
the	O
CAS	O
.	O
</s>
<s>
As	O
an	O
example	O
use	O
case	O
of	O
compare-and-swap	B-Operating_System
,	O
here	O
is	O
an	O
algorithm	O
for	O
atomically	B-Operating_System
incrementing	I-Operating_System
or	I-Operating_System
decrementing	I-Operating_System
an	I-Operating_System
integer	I-Operating_System
.	O
</s>
<s>
Unlike	O
in	O
the	O
pseudocode	B-Language
above	O
,	O
there	O
is	O
no	O
requirement	O
that	O
any	O
sequence	O
of	O
operations	O
is	O
atomic	B-General_Concept
except	O
for	O
.	O
</s>
<s>
value	O
←	O
*	O
p	O
//	O
Even	O
this	O
operation	O
does	O
n't	O
need	O
to	O
be	O
atomic	B-General_Concept
.	O
</s>
<s>
Some	O
CAS-based	O
algorithms	O
are	O
affected	O
by	O
and	O
must	O
handle	O
the	O
problem	O
of	O
a	O
false	O
positive	O
match	O
,	O
or	O
the	O
ABA	B-Operating_System
problem	I-Operating_System
.	O
</s>
<s>
It	O
is	O
possible	O
that	O
between	O
the	O
time	O
the	O
old	O
value	O
is	O
read	O
and	O
the	O
time	O
CAS	O
is	O
attempted	O
,	O
some	O
other	O
processors	O
or	O
threads	O
change	O
the	O
memory	B-General_Concept
location	I-General_Concept
two	O
or	O
more	O
times	O
such	O
that	O
it	O
acquires	O
a	O
bit	O
pattern	O
which	O
matches	O
the	O
old	O
value	O
.	O
</s>
<s>
This	O
is	O
in	O
effect	O
lock-free	B-Operating_System
garbage	O
collection	O
.	O
</s>
<s>
The	O
advantage	O
of	O
using	O
SMR	O
is	O
the	O
assurance	O
a	O
given	O
pointer	O
will	O
exist	O
only	O
once	O
at	O
any	O
one	O
time	O
in	O
the	O
data	O
structure	O
,	O
thus	O
the	O
ABA	B-Operating_System
problem	I-Operating_System
is	O
completely	O
solved	O
.	O
</s>
<s>
CAS	O
,	O
and	O
other	O
atomic	B-General_Concept
instructions	I-General_Concept
,	O
are	O
sometimes	O
thought	O
to	O
be	O
unnecessary	O
in	O
uniprocessor	O
systems	O
,	O
because	O
the	O
atomicity	B-General_Concept
of	O
any	O
sequence	O
of	O
instructions	O
can	O
be	O
achieved	O
by	O
disabling	O
interrupts	O
while	O
executing	O
it	O
.	O
</s>
<s>
Thus	O
,	O
even	O
programs	O
only	O
intended	O
to	O
run	O
on	O
uniprocessor	O
machines	O
will	O
benefit	O
from	O
atomic	B-General_Concept
instructions	I-General_Concept
,	O
as	O
in	O
the	O
case	O
of	O
Linux	O
's	O
futexes	B-Application
.	O
</s>
<s>
In	O
multiprocessor	B-Operating_System
systems	O
,	O
it	O
is	O
usually	O
impossible	O
to	O
disable	O
interrupts	O
on	O
all	O
processors	O
at	O
the	O
same	O
time	O
.	O
</s>
<s>
Even	O
if	O
it	O
were	O
possible	O
,	O
two	O
or	O
more	O
processors	O
could	O
be	O
attempting	O
to	O
access	O
the	O
same	O
semaphore	B-Operating_System
's	O
memory	O
at	O
the	O
same	O
time	O
,	O
and	O
thus	O
atomicity	B-General_Concept
would	O
not	O
be	O
achieved	O
.	O
</s>
<s>
The	O
compare-and-swap	B-Operating_System
instruction	O
allows	O
any	O
processor	O
to	O
atomically	O
test	O
and	O
modify	O
a	O
memory	B-General_Concept
location	I-General_Concept
,	O
preventing	O
such	O
multiple-processor	O
collisions	O
.	O
</s>
<s>
On	O
server-grade	O
multi-processor	B-Operating_System
architectures	O
of	O
the	O
2010s	O
,	O
compare-and-swap	B-Operating_System
is	O
cheap	O
relative	O
to	O
a	O
simple	O
load	O
that	O
is	O
not	O
served	O
from	O
cache	O
.	O
</s>
<s>
A	O
2013	O
paper	O
points	O
out	O
that	O
a	O
CAS	O
is	O
only	O
1.15	O
times	O
more	O
expensive	O
than	O
a	O
non-cached	O
load	O
on	O
Intel	O
Xeon	O
(	O
Westmere-EX	O
)	O
and	O
1.35	O
times	O
on	O
AMD	B-General_Concept
Opteron	I-General_Concept
(	O
Magny-Cours	O
)	O
.	O
</s>
<s>
Compare-and-swap	B-Operating_System
(	O
and	O
compare-and-swap-double	O
)	O
has	O
been	O
an	O
integral	O
part	O
of	O
the	O
IBM	B-Device
370	I-Device
(	O
and	O
all	O
successor	O
)	O
architectures	O
since	O
1970	O
.	O
</s>
<s>
The	O
operating	O
systems	O
that	O
run	O
on	O
these	O
architectures	O
make	O
extensive	O
use	O
of	O
this	O
instruction	O
to	O
facilitate	O
process	O
(	O
i.e.	O
,	O
system	O
and	O
user	O
tasks	O
)	O
and	O
processor	O
(	O
i.e.	O
,	O
central	O
processors	O
)	O
parallelism	B-Operating_System
while	O
eliminating	O
,	O
to	O
the	O
greatest	O
degree	O
possible	O
,	O
the	O
"	O
disabled	O
spinlocks	B-Operating_System
"	O
which	O
had	O
been	O
employed	O
in	O
earlier	O
IBM	O
operating	O
systems	O
.	O
</s>
<s>
Similarly	O
,	O
the	O
use	O
of	O
test-and-set	B-Operating_System
was	O
also	O
eliminated	O
.	O
</s>
<s>
In	O
these	O
operating	O
systems	O
,	O
new	O
units	O
of	O
work	O
may	O
be	O
instantiated	O
"	O
globally	O
"	O
,	O
into	O
the	O
global	O
service	O
priority	O
list	O
,	O
or	O
"	O
locally	O
"	O
,	O
into	O
the	O
local	O
service	O
priority	O
list	O
,	O
by	O
the	O
execution	O
of	O
a	O
single	O
compare-and-swap	B-Operating_System
instruction	O
.	O
</s>
<s>
In	O
the	O
x86	B-Operating_System
(	O
since	O
80486	B-General_Concept
)	O
and	O
Itanium	B-General_Concept
architectures	O
this	O
is	O
implemented	O
as	O
the	O
compare	B-Operating_System
and	I-Operating_System
exchange	I-Operating_System
(	O
CMPXCHG	O
)	O
instruction	O
(	O
on	O
a	O
multiprocessor	B-Operating_System
the	O
prefix	O
must	O
be	O
used	O
)	O
.	O
</s>
<s>
As	O
of	O
2013	O
,	O
most	O
multiprocessor	B-Operating_System
architectures	O
support	O
CAS	O
in	O
hardware	O
,	O
and	O
the	O
compare-and-swap	B-Operating_System
operation	O
is	O
the	O
most	O
popular	O
synchronization	O
primitive	O
for	O
implementing	O
both	O
lock-based	O
and	O
non-blocking	O
concurrent	B-Operating_System
data	I-Operating_System
structures	I-Operating_System
.	O
</s>
<s>
The	O
atomic	B-General_Concept
counter	O
and	O
atomic	B-General_Concept
bitmask	O
operations	O
in	O
the	O
Linux	O
kernel	O
typically	O
use	O
a	O
compare-and-swap	B-Operating_System
instruction	O
in	O
their	O
implementation	O
.	O
</s>
<s>
The	O
SPARC-V8	B-Architecture
and	O
PA-RISC	B-Device
architectures	O
are	O
two	O
of	O
the	O
very	O
few	O
recent	O
architectures	O
that	O
do	O
not	O
support	O
CAS	O
in	O
hardware	O
;	O
the	O
Linux	O
port	O
to	O
these	O
architectures	O
uses	O
a	O
spinlock	B-Operating_System
.	O
</s>
<s>
Many	O
C	O
compilers	O
support	O
using	O
compare-and-swap	B-Operating_System
either	O
with	O
the	O
C11	O
<stdatomic.h>	O
functions	O
,	O
or	O
some	O
non-standard	O
C	O
extension	O
of	O
that	O
particular	O
C	O
compiler	O
,	O
or	O
by	O
calling	O
a	O
function	O
written	O
directly	O
in	O
assembly	O
language	O
using	O
the	O
compare-and-swap	B-Operating_System
instruction	O
.	O
</s>
<s>
The	O
following	O
C	O
function	O
shows	O
the	O
basic	O
behavior	O
of	O
a	O
compare-and-swap	B-Operating_System
variant	O
that	O
returns	O
the	O
old	O
value	O
of	O
the	O
specified	O
memory	B-General_Concept
location	I-General_Concept
;	O
however	O
,	O
this	O
version	O
does	O
not	O
provide	O
the	O
crucial	O
guarantees	O
of	O
atomicity	B-General_Concept
that	O
a	O
real	O
compare-and-swap	B-Operating_System
operation	O
would	O
:	O
</s>
<s>
Since	O
CAS	O
operates	O
on	O
a	O
single	O
pointer-sized	O
memory	B-General_Concept
location	I-General_Concept
,	O
while	O
most	O
lock-free	B-Operating_System
and	I-Operating_System
wait-free	I-Operating_System
algorithms	I-Operating_System
need	O
to	O
modify	O
multiple	O
locations	O
,	O
several	O
extensions	O
have	O
been	O
implemented	O
.	O
</s>
<s>
Double	B-Operating_System
compare-and-swap	I-Operating_System
(	O
DCAS	O
)	O
Compares	O
two	O
unrelated	O
memory	B-General_Concept
locations	I-General_Concept
with	O
two	O
expected	O
values	O
,	O
and	O
if	O
they	O
're	O
equal	O
,	O
sets	O
both	O
locations	O
to	O
new	O
values	O
.	O
</s>
<s>
DCAS	O
and	O
MCAS	O
are	O
of	O
practical	O
interest	O
in	O
the	O
convenient	O
(	O
concurrent	B-Operating_System
)	O
implementation	O
of	O
some	O
data	O
structures	O
like	O
deques	B-Application
or	O
binary	B-Language
search	I-Language
trees	I-Language
.	O
</s>
<s>
DCAS	O
and	O
MCAS	O
may	O
be	O
implemented	O
however	O
using	O
the	O
more	O
expressive	O
hardware	B-Operating_System
transactional	I-Operating_System
memory	I-Operating_System
present	O
in	O
some	O
recent	O
processors	O
such	O
as	O
IBM	O
POWER8	B-Device
or	O
in	O
Intel	O
processors	O
supporting	O
Transactional	B-Operating_System
Synchronization	I-Operating_System
Extensions	I-Operating_System
(	O
TSX	O
)	O
.	O
</s>
<s>
Double-wide	O
compare-and-swap	B-Operating_System
Operates	O
on	O
two	O
adjacent	O
pointer-sized	O
locations	O
(	O
or	O
,	O
equivalently	O
,	O
one	O
location	O
twice	O
as	O
big	O
as	O
a	O
pointer	O
)	O
.	O
</s>
<s>
On	O
later	O
x86	B-Operating_System
processors	O
,	O
the	O
CMPXCHG8B	O
and	O
CMPXCHG16B	O
instructions	O
serve	O
this	O
role	O
,	O
although	O
early	O
64-bit	O
AMD	O
CPUs	O
did	O
not	O
support	O
CMPXCHG16B	O
(	O
modern	O
AMD	O
CPUs	O
do	O
)	O
.	O
</s>
<s>
Some	O
Intel	O
motherboards	O
from	O
the	O
Core	B-Device
2	I-Device
era	O
also	O
hamper	O
its	O
use	O
,	O
even	O
though	O
the	O
processors	O
support	O
it	O
.	O
</s>
<s>
These	O
issues	O
came	O
into	O
the	O
spotlight	O
at	O
the	O
launch	O
of	O
Windows	B-Device
8.1	I-Device
because	O
it	O
required	O
hardware	O
support	O
for	O
CMPXCHG16B	O
.	O
</s>
<s>
The	O
Itanium	B-General_Concept
's	O
cmp8xchg16	O
instruction	O
implements	O
this	O
,	O
where	O
the	O
two	O
written	O
pointers	O
are	O
adjacent	O
.	O
</s>
<s>
Multi-word	O
compare-and-swap	B-Operating_System
Is	O
a	O
generalisation	O
of	O
normal	O
compare-and-swap	B-Operating_System
.	O
</s>
<s>
It	O
can	O
be	O
used	O
to	O
atomically	O
swap	O
an	O
arbitrary	O
number	O
of	O
arbitrarily	O
located	O
memory	B-General_Concept
locations	I-General_Concept
.	O
</s>
<s>
Usually	O
,	O
multi-word	O
compare-and-swap	B-Operating_System
is	O
implemented	O
in	O
software	O
using	O
normal	O
double-wide	O
compare-and-swap	B-Operating_System
operations	O
.	O
</s>
