<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
test-and-set	B-Operating_System
instruction	B-General_Concept
is	O
an	O
instruction	B-General_Concept
used	O
to	O
write	O
(	O
set	O
)	O
1	O
to	O
a	O
memory	B-General_Concept
location	I-General_Concept
and	O
return	O
its	O
old	O
value	O
as	O
a	O
single	O
atomic	B-General_Concept
(	O
i.e.	O
,	O
non-interruptible	O
)	O
operation	O
.	O
</s>
<s>
If	O
multiple	O
processes	O
may	O
access	O
the	O
same	O
memory	B-General_Concept
location	I-General_Concept
,	O
and	O
if	O
a	O
process	O
is	O
currently	O
performing	O
a	O
test-and-set	B-Operating_System
,	O
no	O
other	O
process	O
may	O
begin	O
another	O
test-and-set	B-Operating_System
until	O
the	O
first	O
process	O
's	O
test-and-set	B-Operating_System
is	O
finished	O
.	O
</s>
<s>
A	O
central	B-General_Concept
processing	I-General_Concept
unit	I-General_Concept
(	O
CPU	O
)	O
may	O
use	O
a	O
test-and-set	B-Operating_System
instruction	B-General_Concept
offered	O
by	O
another	O
electronic	O
component	O
,	O
such	O
as	O
dual-port	B-General_Concept
RAM	I-General_Concept
;	O
a	O
CPU	O
itself	O
may	O
also	O
offer	O
a	O
test-and-set	B-Operating_System
instruction	B-General_Concept
.	O
</s>
<s>
A	O
lock	B-Operating_System
can	O
be	O
built	O
using	O
an	O
atomic	B-General_Concept
test-and-set	B-Operating_System
instruction	B-General_Concept
as	O
follows	O
:	O
</s>
<s>
This	O
code	O
assumes	O
that	O
the	O
memory	B-General_Concept
location	I-General_Concept
was	O
initialized	O
to	O
0	O
at	O
some	O
point	O
prior	O
to	O
the	O
first	O
test-and-set	B-Operating_System
.	O
</s>
<s>
The	O
calling	O
process	O
obtains	O
the	O
lock	B-Operating_System
if	O
the	O
old	O
value	O
was	O
0	O
,	O
otherwise	O
the	O
while-loop	O
spins	O
waiting	O
to	O
acquire	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
This	O
is	O
called	O
a	O
spinlock	B-Operating_System
.	O
</s>
<s>
At	O
any	O
point	O
,	O
the	O
holder	O
of	O
the	O
lock	B-Operating_System
can	O
simply	O
set	O
the	O
memory	B-General_Concept
location	I-General_Concept
back	O
to	O
0	O
to	O
release	O
the	O
lock	B-Operating_System
for	O
acquisition	O
by	O
another--this	O
does	O
not	O
require	O
any	O
special	O
handling	O
as	O
the	O
holder	O
"	O
owns	O
"	O
this	O
memory	B-General_Concept
location	I-General_Concept
.	O
</s>
<s>
"	O
Test	B-Operating_System
and	I-Operating_System
test-and-set	I-Operating_System
"	O
is	O
another	O
example	O
.	O
</s>
<s>
Maurice	O
Herlihy	O
(	O
1991	O
)	O
proved	O
that	O
test-and-set	B-Operating_System
(	O
1-bit	O
comparand	O
)	O
has	O
a	O
finite	O
consensus	B-Operating_System
number	I-Operating_System
and	O
can	O
solve	O
the	O
wait-free	O
consensus	B-Operating_System
problem	I-Operating_System
for	O
at-most	O
two	O
concurrent	O
processes	O
.	O
</s>
<s>
In	O
contrast	O
,	O
compare-and-swap	B-Operating_System
(	O
32-bit	O
comparand	O
)	O
offers	O
a	O
more	O
general	O
solution	O
to	O
this	O
problem	O
,	O
and	O
in	O
some	O
implementations	O
compare-double-and-swap	O
(	O
64-bit	O
comparand	O
)	O
is	O
also	O
available	O
for	O
extended	O
utility	O
.	O
</s>
<s>
DPRAM	B-General_Concept
test-and-set	B-Operating_System
instructions	O
can	O
work	O
in	O
many	O
ways	O
.	O
</s>
<s>
Here	O
are	O
two	O
variations	O
,	O
both	O
of	O
which	O
describe	O
a	O
DPRAM	B-General_Concept
which	O
provides	O
exactly	O
2	O
ports	O
,	O
allowing	O
2	O
separate	O
electronic	O
components	O
(	O
such	O
as	O
2	O
CPUs	O
)	O
access	O
to	O
every	O
memory	B-General_Concept
location	I-General_Concept
on	O
the	O
DPRAM	B-General_Concept
.	O
</s>
<s>
When	O
CPU	O
1	O
issues	O
a	O
test-and-set	B-Operating_System
instruction	B-General_Concept
,	O
the	O
DPRAM	B-General_Concept
first	O
makes	O
an	O
"	O
internal	O
note	O
"	O
of	O
this	O
by	O
storing	O
the	O
address	O
of	O
the	O
memory	B-General_Concept
location	I-General_Concept
in	O
a	O
special	O
place	O
.	O
</s>
<s>
If	O
at	O
this	O
point	O
,	O
CPU	O
2	O
happens	O
to	O
issue	O
a	O
test-and-set	B-Operating_System
instruction	B-General_Concept
for	O
the	O
same	O
memory	B-General_Concept
location	I-General_Concept
,	O
the	O
DPRAM	B-General_Concept
first	O
checks	O
its	O
"	O
internal	O
note	O
"	O
,	O
recognizes	O
the	O
situation	O
,	O
and	O
issues	O
a	O
BUSY	O
interrupt	B-Application
,	O
which	O
tells	O
CPU	O
2	O
that	O
it	O
must	O
wait	O
and	O
retry	O
.	O
</s>
<s>
This	O
is	O
an	O
implementation	O
of	O
a	O
busy	B-Operating_System
waiting	I-Operating_System
or	O
spinlock	B-Operating_System
using	O
the	O
interrupt	B-Application
mechanism	O
.	O
</s>
<s>
Since	O
all	O
this	O
happens	O
at	O
hardware	O
speeds	O
,	O
CPU	O
2	O
's	O
wait	O
to	O
get	O
out	O
of	O
the	O
spin-lock	O
is	O
very	O
short	O
.	O
</s>
<s>
Whether	O
or	O
not	O
CPU	O
2	O
was	O
trying	O
to	O
access	O
the	O
memory	B-General_Concept
location	I-General_Concept
,	O
the	O
DPRAM	B-General_Concept
performs	O
the	O
test	O
given	O
by	O
CPU	O
1	O
.	O
</s>
<s>
If	O
the	O
test	O
succeeds	O
,	O
the	O
DPRAM	B-General_Concept
sets	O
the	O
memory	B-General_Concept
location	I-General_Concept
to	O
the	O
value	O
given	O
by	O
CPU	O
1	O
.	O
</s>
<s>
Then	O
the	O
DPRAM	B-General_Concept
wipes	O
out	O
its	O
"	O
internal	O
note	O
"	O
that	O
CPU	O
1	O
was	O
writing	O
there	O
.	O
</s>
<s>
At	O
this	O
point	O
,	O
CPU	O
2	O
could	O
issue	O
a	O
test-and-set	B-Operating_System
,	O
which	O
would	O
succeed	O
.	O
</s>
<s>
CPU	O
1	O
issues	O
a	O
test-and-set	B-Operating_System
instruction	B-General_Concept
to	O
write	O
to	O
"	O
memory	B-General_Concept
location	I-General_Concept
A	O
"	O
.	O
</s>
<s>
The	O
DPRAM	B-General_Concept
does	O
not	O
immediately	O
store	O
the	O
value	O
in	O
memory	B-General_Concept
location	I-General_Concept
A	O
,	O
but	O
instead	O
simultaneously	O
moves	O
the	O
current	O
value	O
to	O
a	O
special	O
register	O
,	O
while	O
setting	O
the	O
contents	O
of	O
memory	B-General_Concept
location	I-General_Concept
A	O
to	O
a	O
special	O
"	O
flag	O
value	O
"	O
.	O
</s>
<s>
If	O
at	O
this	O
point	O
,	O
CPU	O
2	O
issues	O
a	O
test-and-set	B-Operating_System
to	O
memory	B-General_Concept
location	I-General_Concept
A	O
,	O
the	O
DPRAM	B-General_Concept
detects	O
the	O
special	O
flag	O
value	O
,	O
and	O
as	O
in	O
Variation	O
1	O
,	O
issues	O
a	O
BUSY	O
interrupt	B-Application
.	O
</s>
<s>
Whether	O
or	O
not	O
CPU	O
2	O
was	O
trying	O
to	O
access	O
the	O
memory	B-General_Concept
location	I-General_Concept
,	O
the	O
DPRAM	B-General_Concept
now	O
performs	O
CPU	O
1	O
's	O
test	O
.	O
</s>
<s>
If	O
the	O
test	O
succeeds	O
,	O
the	O
DPRAM	B-General_Concept
sets	O
memory	B-General_Concept
location	I-General_Concept
A	O
to	O
the	O
value	O
specified	O
by	O
CPU	O
1	O
.	O
</s>
<s>
If	O
the	O
test	O
fails	O
,	O
the	O
DPRAM	B-General_Concept
copies	O
the	O
value	O
back	O
from	O
the	O
special	O
register	O
to	O
memory	B-General_Concept
location	I-General_Concept
A	O
.	O
</s>
<s>
If	O
CPU	O
2	O
now	O
issues	O
a	O
test-and-set	B-Operating_System
,	O
it	O
will	O
succeed	O
.	O
</s>
<s>
Some	O
instruction	B-General_Concept
sets	I-General_Concept
have	O
an	O
atomic	B-General_Concept
test-and-set	B-Operating_System
machine	O
language	O
instruction	B-General_Concept
.	O
</s>
<s>
Examples	O
include	O
x86	B-Operating_System
and	O
IBM	B-Application
System/360	I-Application
and	O
its	O
successors	O
(	O
including	O
z/Architecture	B-Device
)	O
.	O
</s>
<s>
Those	O
that	O
do	O
not	O
can	O
still	O
implement	O
an	O
atomic	B-General_Concept
test-and-set	B-Operating_System
using	O
a	O
read-modify-write	B-Operating_System
or	O
compare-and-swap	B-Operating_System
instruction	B-General_Concept
.	O
</s>
<s>
The	O
test	B-Operating_System
and	I-Operating_System
set	I-Operating_System
instruction	B-General_Concept
,	O
when	O
used	O
with	O
boolean	O
values	O
,	O
uses	O
logic	O
like	O
that	O
shown	O
in	O
the	O
following	O
function	O
,	O
except	O
that	O
the	O
function	O
must	O
execute	O
atomically	B-General_Concept
.	O
</s>
<s>
That	O
is	O
,	O
no	O
other	O
process	O
must	O
be	O
able	O
to	O
interrupt	B-Application
the	O
function	O
mid-execution	O
,	O
thereby	O
seeing	O
a	O
state	O
that	O
only	O
exists	O
while	O
the	O
function	O
executes	O
.	O
</s>
<s>
Nevertheless	O
,	O
the	O
code	O
shown	O
helps	O
to	O
explain	O
the	O
behaviour	O
of	O
test-and-set	B-Operating_System
.	O
</s>
<s>
NOTE	O
:	O
In	O
this	O
example	O
,	O
'	O
lock	B-Operating_System
 '	O
is	O
assumed	O
to	O
be	O
passed	O
by	O
reference	O
(	O
or	O
by	O
name	O
)	O
but	O
the	O
assignment	O
to	O
'	O
initial	O
 '	O
creates	O
a	O
new	O
value	O
(	O
not	O
just	O
copying	O
a	O
reference	O
)	O
.	O
</s>
<s>
boolean	O
initial	O
=	O
lock	B-Operating_System
;	O
</s>
<s>
lock	B-Operating_System
=	O
true	O
;	O
</s>
<s>
Not	O
only	O
is	O
the	O
code	O
shown	O
not	O
atomic	B-General_Concept
,	O
in	O
the	O
sense	O
of	O
the	O
test-and-set	B-Operating_System
instruction	B-General_Concept
,	O
it	O
also	O
differs	O
from	O
the	O
descriptions	O
of	O
DPRAM	B-General_Concept
hardware	O
test-and-set	B-Operating_System
above	O
.	O
</s>
<s>
Here	O
,	O
the	O
value	O
being	O
set	O
and	O
the	O
test	O
are	O
fixed	O
and	O
invariant	O
,	O
and	O
the	O
value	O
is	O
updated	O
regardless	O
of	O
the	O
outcome	O
of	O
the	O
test	O
,	O
whereas	O
for	O
the	O
DPRAM	B-General_Concept
test-and-set	B-Operating_System
,	O
the	O
memory	O
is	O
set	O
only	O
when	O
the	O
test	O
succeeds	O
,	O
and	O
the	O
value	O
to	O
set	O
and	O
the	O
test	O
condition	O
are	O
specified	O
by	O
the	O
CPU	O
.	O
</s>
<s>
Here	O
,	O
the	O
value	O
to	O
set	O
can	O
only	O
be	O
1	O
,	O
but	O
if	O
0	O
and	O
1	O
are	O
considered	O
the	O
only	O
valid	O
values	O
for	O
the	O
memory	B-General_Concept
location	I-General_Concept
,	O
and	O
"	O
value	O
is	O
nonzero	O
"	O
is	O
the	O
only	O
allowed	O
test	O
,	O
then	O
this	O
equates	O
to	O
the	O
case	O
described	O
for	O
DPRAM	B-General_Concept
hardware	O
(	O
or	O
,	O
more	O
specifically	O
,	O
the	O
DPRAM	B-General_Concept
case	O
reduces	O
to	O
this	O
under	O
these	O
constraints	O
)	O
.	O
</s>
<s>
From	O
that	O
viewpoint	O
,	O
this	O
can	O
,	O
correctly	O
,	O
be	O
called	O
"	O
test-and-set	B-Operating_System
"	O
in	O
the	O
full	O
,	O
conventional	O
sense	O
of	O
that	O
term	O
.	O
</s>
<s>
The	O
essential	O
point	O
to	O
note	O
is	O
the	O
general	O
intent	O
and	O
principle	O
of	O
test-and-set	B-Operating_System
:	O
a	O
value	O
is	O
both	O
tested	O
and	O
set	O
in	O
one	O
atomic	B-General_Concept
operation	I-General_Concept
such	O
that	O
no	O
other	O
program	O
thread	O
or	O
process	O
can	O
change	O
the	O
target	O
memory	B-General_Concept
location	I-General_Concept
after	O
it	O
is	O
tested	O
but	O
before	O
it	O
is	O
set	O
.	O
</s>
<s>
In	O
the	O
C	B-Language
programming	I-Language
language	I-Language
,	O
the	O
implementation	O
would	O
be	O
like	O
:	O
</s>
<s>
The	O
code	O
also	O
shows	O
that	O
there	O
are	O
really	O
two	O
operations	O
:	O
an	O
atomic	B-General_Concept
read-modify-write	B-Operating_System
and	O
a	O
test	O
.	O
</s>
<s>
Only	O
the	O
read-modify-write	B-Operating_System
needs	O
to	O
be	O
atomic	B-General_Concept
.	O
</s>
<s>
One	O
way	O
to	O
implement	O
mutual	B-Operating_System
exclusion	I-Operating_System
is	O
by	O
using	O
a	O
test-and-set	B-Operating_System
based	O
lock	B-Operating_System
as	O
follows	O
:	O
</s>
<s>
The	O
lock	B-Operating_System
variable	I-Operating_System
is	O
a	O
shared	O
variable	O
i.e.	O
</s>
<s>
Note	O
the	O
volatile	B-Operating_System
keyword	O
.	O
</s>
<s>
In	O
absence	O
of	O
volatile	B-Operating_System
,	O
the	O
compiler	O
and/or	O
the	O
CPU(s )	O
may	O
optimize	O
access	O
to	O
lock	B-Operating_System
and/or	O
use	O
cached	O
values	O
,	O
thus	O
rendering	O
the	O
above	O
code	O
erroneous	O
.	O
</s>
<s>
Conversely	O
,	O
and	O
unfortunately	O
,	O
the	O
presence	O
of	O
volatile	B-Operating_System
does	O
not	O
guarantee	O
that	O
reads	O
and	O
writes	O
are	O
committed	O
to	O
memory	O
.	O
</s>
<s>
Some	O
compilers	O
issue	O
memory	B-General_Concept
barriers	I-General_Concept
to	O
ensure	O
that	O
operations	O
are	O
committed	O
to	O
memory	O
,	O
but	O
since	O
the	O
semantics	O
of	O
volatile	B-Operating_System
in	O
C/C	O
++	O
is	O
quite	O
vague	O
,	O
not	O
all	O
compilers	O
will	O
do	O
that	O
.	O
</s>
<s>
This	O
spin	O
lock	B-Operating_System
function	O
can	O
be	O
called	O
by	O
multiple	O
processes	O
,	O
but	O
it	O
is	O
guaranteed	O
that	O
only	O
one	O
process	O
will	O
be	O
in	O
the	O
critical	B-Operating_System
section	I-Operating_System
at	O
a	O
time	O
.	O
</s>
<s>
The	O
rest	O
of	O
the	O
processes	O
will	O
keep	O
spinning	O
until	O
they	O
get	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
It	O
is	O
possible	O
that	O
a	O
process	O
is	O
never	O
granted	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
This	O
is	O
a	O
drawback	O
of	O
a	O
spin	O
lock	B-Operating_System
implementation	O
as	O
it	O
does	O
n't	O
ensure	O
fairness	O
.	O
</s>
<s>
Here	O
tsl	O
is	O
an	O
atomic	B-General_Concept
instruction	I-General_Concept
and	O
flag	O
is	O
the	O
lock	B-Operating_System
variable	I-Operating_System
.	O
</s>
<s>
The	O
process	O
does	O
n't	O
return	O
unless	O
it	O
acquires	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
The	O
four	O
major	O
evaluation	O
metrics	O
for	O
locks	O
in	O
general	O
are	O
uncontended	O
lock-acquisition	O
latency	O
,	O
bus	O
traffic	O
,	O
fairness	O
,	O
and	O
storage	O
.	O
</s>
<s>
Test-and-set	B-Operating_System
scores	O
low	O
on	O
two	O
of	O
them	O
,	O
namely	O
,	O
high	O
bus	O
traffic	O
and	O
unfairness	O
.	O
</s>
<s>
When	O
processor	O
P1	O
has	O
obtained	O
a	O
lock	B-Operating_System
and	O
processor	O
P2	O
is	O
also	O
waiting	O
for	O
the	O
lock	B-Operating_System
,	O
P2	O
will	O
keep	O
incurring	O
bus	O
transactions	O
in	O
attempts	O
to	O
acquire	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
When	O
a	O
processor	O
has	O
obtained	O
a	O
lock	B-Operating_System
,	O
all	O
other	O
processors	O
which	O
also	O
wish	O
to	O
obtain	O
the	O
same	O
lock	B-Operating_System
keep	O
trying	O
to	O
obtain	O
the	O
lock	B-Operating_System
by	O
initiating	O
bus	O
transactions	O
repeatedly	O
until	O
they	O
get	O
hold	O
of	O
the	O
lock	B-Operating_System
.	O
</s>
<s>
This	O
increases	O
the	O
bus	O
traffic	O
requirement	O
of	O
test-and-set	B-Operating_System
significantly	O
.	O
</s>
<s>
This	O
slows	O
down	O
all	O
other	O
traffic	O
from	O
cache	B-General_Concept
and	O
coherence	B-General_Concept
misses	O
.	O
</s>
<s>
It	O
slows	O
down	O
the	O
overall	O
section	O
,	O
since	O
the	O
traffic	O
is	O
saturated	O
by	O
failed	O
lock	B-Operating_System
acquisition	O
attempts	O
.	O
</s>
<s>
Test-and-test-and-set	B-Operating_System
is	O
an	O
improvement	O
over	O
TSL	O
since	O
it	O
does	O
not	O
initiate	O
lock	B-Operating_System
acquisition	O
requests	O
continuously	O
.	O
</s>
<s>
When	O
we	O
consider	O
fairness	O
,	O
we	O
consider	O
if	O
a	O
processor	O
gets	O
a	O
fair	O
chance	O
of	O
acquiring	O
the	O
lock	B-Operating_System
when	O
it	O
is	O
set	O
free	O
.	O
</s>
<s>
it	O
might	O
not	O
be	O
able	O
to	O
acquire	O
the	O
lock	B-Operating_System
for	O
an	O
extended	O
period	O
of	O
time	O
even	O
though	O
it	O
has	O
become	O
free	O
during	O
that	O
time	O
.	O
</s>
<s>
Storage	O
overhead	O
for	O
TSL	O
is	O
next	O
to	O
nothing	O
since	O
only	O
one	O
lock	B-Operating_System
is	O
required	O
.	O
</s>
<s>
Uncontended	O
latency	O
is	O
also	O
low	O
since	O
only	O
one	O
atomic	B-General_Concept
instruction	I-General_Concept
and	O
branch	O
are	O
needed	O
.	O
</s>
