<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
ticket	B-Operating_System
lock	I-Operating_System
is	O
a	O
synchronization	O
mechanism	O
,	O
or	O
locking	B-Operating_System
algorithm	I-Operating_System
,	O
that	O
is	O
a	O
type	O
of	O
spinlock	B-Operating_System
that	O
uses	O
"	O
tickets	O
"	O
to	O
control	O
which	O
thread	B-Operating_System
of	I-Operating_System
execution	I-Operating_System
is	O
allowed	O
to	O
enter	O
a	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
The	O
basic	O
concept	O
of	O
a	O
ticket	B-Operating_System
lock	I-Operating_System
is	O
similar	O
to	O
the	O
ticket	O
queue	O
management	O
system	O
.	O
</s>
<s>
Like	O
this	O
system	O
,	O
a	O
ticket	B-Operating_System
lock	I-Operating_System
is	O
a	O
first	B-Operating_System
in	I-Operating_System
first	I-Operating_System
out	I-Operating_System
(	O
FIFO	B-Operating_System
)	O
queue-based	O
mechanism	O
.	O
</s>
<s>
It	O
adds	O
the	O
benefit	O
of	O
fairness	B-Application
of	O
lock	O
acquisition	O
and	O
works	O
as	O
follows	O
;	O
there	O
are	O
two	O
integer	O
values	O
which	O
begin	O
at	O
0	O
.	O
</s>
<s>
The	O
queue	O
ticket	O
is	O
the	O
thread	B-Operating_System
's	O
position	O
in	O
the	O
queue	O
,	O
and	O
the	O
dequeue	O
ticket	O
is	O
the	O
ticket	O
,	O
or	O
queue	O
position	O
,	O
that	O
now	O
has	O
the	O
lock	O
(	O
Now	O
Serving	O
)	O
.	O
</s>
<s>
When	O
a	O
thread	B-Operating_System
arrives	O
,	O
it	O
atomically	O
obtains	O
and	O
then	O
increments	O
the	O
queue	O
ticket	O
.	O
</s>
<s>
The	O
atomicity	B-General_Concept
of	O
this	O
operation	O
is	O
required	O
to	O
prevent	O
two	O
threads	B-Operating_System
from	O
simultaneously	O
being	O
able	O
to	O
obtain	O
the	O
same	O
ticket	O
number	O
.	O
</s>
<s>
If	O
they	O
are	O
the	O
same	O
,	O
the	O
thread	B-Operating_System
is	O
permitted	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
If	O
they	O
are	O
not	O
the	O
same	O
,	O
then	O
another	O
thread	B-Operating_System
must	O
already	O
be	O
in	O
the	O
critical	B-Operating_System
section	I-Operating_System
and	O
this	O
thread	B-Operating_System
must	O
busy-wait	B-Operating_System
or	O
yield	O
.	O
</s>
<s>
When	O
a	O
thread	B-Operating_System
leaves	O
the	O
critical	B-Operating_System
section	I-Operating_System
controlled	O
by	O
the	O
lock	O
,	O
it	O
atomically	O
increments	O
the	O
dequeue	O
ticket	O
.	O
</s>
<s>
This	O
permits	O
the	O
next	O
waiting	O
thread	B-Operating_System
,	O
the	O
one	O
with	O
the	O
next	O
sequential	O
ticket	O
number	O
,	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
The	O
notion	O
of	O
fairness	B-Application
in	O
lock	O
acquisition	O
applies	O
to	O
the	O
order	O
in	O
which	O
threads	B-Operating_System
acquire	O
a	O
lock	O
successfully	O
.	O
</s>
<s>
If	O
some	O
type	O
of	O
fairness	B-Application
is	O
implemented	O
,	O
it	O
prevents	O
a	O
thread	B-Operating_System
from	O
being	O
starved	B-Operating_System
out	O
of	O
execution	O
for	O
a	O
long	O
time	O
due	O
to	O
inability	O
to	O
acquire	O
a	O
lock	O
in	O
favor	O
of	O
other	O
threads	B-Operating_System
.	O
</s>
<s>
With	O
no	O
fairness	B-Application
guarantees	O
,	O
a	O
situation	O
can	O
arise	O
where	O
a	O
thread	B-Operating_System
(	O
or	O
multiple	O
threads	B-Operating_System
)	O
can	O
take	O
a	O
disproportionately	O
long	O
time	O
to	O
execute	O
as	O
compared	O
to	O
others	O
.	O
</s>
<s>
A	O
simple	O
example	O
will	O
now	O
be	O
presented	O
to	O
show	O
how	O
a	O
thread	B-Operating_System
could	O
be	O
excessively	O
delayed	O
due	O
to	O
a	O
lack	O
of	O
fairness	B-Application
in	O
lock	O
acquisition	O
.	O
</s>
<s>
Assume	O
a	O
case	O
where	O
three	O
threads	B-Operating_System
,	O
each	O
executing	O
on	O
one	O
of	O
three	O
processors	O
,	O
are	O
executing	O
the	O
following	O
pseudocode	O
that	O
uses	O
a	O
lock	O
with	O
no	O
consideration	O
for	O
fairness	B-Application
.	O
</s>
<s>
Now	O
further	O
assume	O
the	O
physical	O
arrangement	O
of	O
the	O
three	O
processors	O
,	O
P1	O
,	O
P2	O
,	O
and	O
P3	O
,	O
results	O
in	O
a	O
non-uniform	B-Operating_System
memory	I-Operating_System
access	I-Operating_System
time	O
to	O
the	O
location	O
of	O
the	O
shared	O
lock	B-Operating_System
variable	I-Operating_System
.	O
</s>
<s>
The	O
order	O
of	O
increasing	O
access	O
time	O
to	O
the	O
lock	B-Operating_System
variable	I-Operating_System
for	O
the	O
three	O
processors	O
is	O
P1	O
<	O
P2	O
<	O
P3	O
.	O
</s>
<s>
How	O
this	O
situation	O
leads	O
to	O
thread	B-Operating_System
starvation	I-Operating_System
in	O
the	O
absence	O
of	O
a	O
fairness	B-Application
guarantee	O
is	O
shown	O
in	O
the	O
following	O
illustration	O
of	O
the	O
execution	O
of	O
the	O
above	O
pseudocode	O
by	O
these	O
three	O
processors	O
.	O
</s>
<s>
+Starvation	O
of	O
P3TimeP1P2P31lock	O
attempt	O
(	O
success	O
)	O
lock	O
attempt	O
(	O
failed	O
)	O
lock	O
attempt	O
(	O
failed	O
)	O
2critical	O
sectionspinspin3release	O
locklock	O
attempt	O
(	O
success	O
)	O
lock	O
attempt	O
(	O
failed	O
)	O
4	O
...	O
critical	O
sectionspin5lock	O
attempt	O
(	O
failed	O
)	O
6lock	O
......	O
attempt	O
(	O
success	O
)	O
release	O
locklock	O
attempt	O
(	O
failed	O
)	O
7critical	O
sectionspinspin	O
............	O
</s>
<s>
Due	O
to	O
P1	O
having	O
the	O
fastest	O
access	O
time	O
to	O
the	O
lock	O
,	O
it	O
acquires	O
it	O
first	O
and	O
enters	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
P2	O
and	O
P3	O
now	O
spin	O
while	O
P1	O
is	O
in	O
the	O
critical	B-Operating_System
section	I-Operating_System
(	O
Time	O
2	O
)	O
.	O
</s>
<s>
Upon	O
exiting	O
the	O
critical	B-Operating_System
section	I-Operating_System
(	O
Time	O
3	O
)	O
,	O
P1	O
executes	O
an	O
unlock	O
,	O
releasing	O
the	O
lock	O
.	O
</s>
<s>
Since	O
P2	O
has	O
faster	O
access	O
to	O
the	O
lock	O
than	O
P3	O
,	O
it	O
acquires	O
the	O
lock	O
next	O
and	O
enters	O
the	O
critical	B-Operating_System
section	I-Operating_System
(	O
Time	O
4	O
)	O
.	O
</s>
<s>
While	O
P2	O
is	O
in	O
the	O
critical	B-Operating_System
section	I-Operating_System
,	O
P1	O
once	O
again	O
attempts	O
to	O
acquire	O
the	O
lock	O
but	O
ca	O
n’t	O
(	O
Time	O
5	O
)	O
,	O
forcing	O
it	O
to	O
spin	O
wait	O
along	O
with	O
P3	O
.	O
</s>
<s>
Once	O
P2	O
finishes	O
the	O
critical	B-Operating_System
section	I-Operating_System
and	O
issues	O
an	O
unlock	O
,	O
both	O
P1	O
and	O
P3	O
simultaneously	O
attempt	O
to	O
acquire	O
it	O
once	O
again	O
(	O
Time	O
6	O
)	O
.	O
</s>
<s>
But	O
P1	O
,	O
with	O
its	O
faster	O
access	O
time	O
wins	O
again	O
,	O
thus	O
entering	O
the	O
critical	B-Operating_System
section	I-Operating_System
(	O
Time	O
7	O
)	O
.	O
</s>
<s>
This	O
illustrates	O
the	O
need	O
to	O
ensure	O
some	O
level	O
of	O
fairness	B-Application
in	O
lock	O
acquisition	O
in	O
certain	O
circumstances	O
.	O
</s>
<s>
Not	O
all	O
locks	O
have	O
mechanisms	O
that	O
ensure	O
any	O
level	O
of	O
fairness	B-Application
,	O
leaving	O
the	O
potential	O
for	O
situations	O
similar	O
to	O
that	O
illustrated	O
above	O
.	O
</s>
<s>
See	O
the	O
Comparison	O
of	O
locks	O
section	O
below	O
for	O
examples	O
of	O
locks	O
that	O
do	O
n't	O
implement	O
any	O
fairness	B-Application
guarantees	O
.	O
</s>
<s>
In	O
a	O
Non-Uniform	B-Operating_System
Memory	I-Operating_System
Architecture	I-Operating_System
(	O
NUMA	O
)	O
system	O
it	O
is	O
important	O
to	O
have	O
a	O
lock	O
implementation	O
that	O
guarantees	O
some	O
level	O
of	O
fairness	B-Application
of	O
lock	O
acquisition	O
.	O
</s>
<s>
The	O
ticket	B-Operating_System
lock	I-Operating_System
is	O
an	O
implementation	O
of	O
spinlock	B-Operating_System
that	O
adds	O
this	O
desired	O
attribute	O
.	O
</s>
<s>
A	O
call	O
to	O
ticketLock_acquire	O
would	O
precede	O
the	O
critical	B-Operating_System
section	I-Operating_System
of	O
the	O
code	O
and	O
ticketLock_release	O
would	O
follow	O
it	O
.	O
</s>
<s>
Following	O
along	O
with	O
the	O
pseudocode	O
above	O
we	O
can	O
see	O
that	O
each	O
time	O
a	O
processor	O
tries	O
to	O
acquire	O
a	O
lock	O
with	O
ticketLock_acquire( )	O
,	O
fetch_and_inc	B-Operating_System
is	O
called	O
,	O
returning	O
the	O
current	O
value	O
of	O
next_ticket	O
into	O
the	O
thread	B-Operating_System
private	O
my_ticket	O
and	O
incrementing	O
the	O
shared	O
next_ticket	O
.	O
</s>
<s>
Once	O
my_ticket	O
has	O
been	O
received	O
,	O
each	O
thread	B-Operating_System
will	O
spin	O
in	O
the	O
while	O
loop	O
while	O
now_serving	O
is	O
n't	O
equal	O
to	O
its	O
my_ticket	O
.	O
</s>
<s>
Once	O
now_serving	O
becomes	O
equal	O
to	O
a	O
given	O
thread	B-Operating_System
's	O
my_ticket	O
they	O
are	O
allowed	O
to	O
return	O
from	O
ticketLock_acquire( )	O
and	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
of	O
code	O
.	O
</s>
<s>
After	O
the	O
critical	B-Operating_System
section	I-Operating_System
of	O
the	O
code	O
,	O
the	O
thread	B-Operating_System
performs	O
ticketLock_release( )	O
which	O
increments	O
now_serving	O
.	O
</s>
<s>
This	O
allows	O
the	O
thread	B-Operating_System
with	O
the	O
next	O
sequential	O
my_ticket	O
to	O
exit	O
from	O
ticketLock_acquire( )	O
and	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Since	O
the	O
my_ticket	O
values	O
are	O
acquired	O
in	O
the	O
order	O
of	O
thread	B-Operating_System
arrival	O
at	O
the	O
lock	O
,	O
subsequent	O
acquisition	O
of	O
the	O
lock	O
is	O
guaranteed	O
to	O
also	O
be	O
in	O
this	O
same	O
order	O
.	O
</s>
<s>
Thus	O
,	O
fairness	B-Application
of	O
lock	O
acquisition	O
is	O
ensured	O
,	O
enforcing	O
a	O
FIFO	B-Operating_System
ordering	O
.	O
</s>
<s>
The	O
following	O
table	O
shows	O
an	O
example	O
of	O
ticket	B-Operating_System
lock	I-Operating_System
in	O
action	O
in	O
a	O
system	O
with	O
four	O
processors	O
(	O
P1	O
,	O
P2	O
,	O
P3	O
,	O
P4	O
)	O
competing	O
for	O
access	O
to	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
All	O
subsequent	O
attempts	O
,	O
while	O
the	O
first	O
still	O
holds	O
the	O
lock	O
,	O
serves	O
to	O
form	O
the	O
queue	O
of	O
processors	O
waiting	O
their	O
turn	O
in	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
The	O
first	O
step	O
,	O
prior	O
to	O
use	O
of	O
the	O
lock	O
,	O
is	O
initialization	O
of	O
all	O
lock	B-Operating_System
variables	I-Operating_System
(	O
Row	O
1	O
)	O
.	O
</s>
<s>
Having	O
next_ticket	O
and	O
now_serving	O
initialized	O
to	O
0	O
ensures	O
that	O
the	O
first	O
thread	B-Operating_System
that	O
attempts	O
to	O
get	O
the	O
lock	O
will	O
get	O
ticket	O
0	O
,	O
thus	O
acquiring	O
the	O
lock	O
due	O
to	O
its	O
ticket	O
matching	O
now_serving	O
.	O
</s>
<s>
No	O
currently	O
waiting	O
threads	B-Operating_System
have	O
this	O
ticket	O
number	O
,	O
so	O
the	O
next	O
thread	B-Operating_System
to	O
arrive	O
will	O
get	O
4	O
for	O
its	O
ticket	O
and	O
immediately	O
acquire	O
the	O
lock	O
.	O
</s>
<s>
The	O
Linux	B-Operating_System
kernel	I-Operating_System
implementation	O
can	O
have	O
lower	O
latency	O
than	O
the	O
simpler	O
test-and-set	B-Operating_System
or	O
exchange	B-Operating_System
based	O
spinlock	B-Operating_System
algorithms	O
on	O
modern	O
machines	O
.	O
</s>
<s>
The	O
more	O
basic	O
locking	B-Operating_System
mechanisms	O
have	O
lower	O
uncontended	O
latency	O
than	O
the	O
advanced	O
locking	B-Operating_System
mechanisms	O
.	O
</s>
<s>
One	O
advantage	O
of	O
a	O
ticket	B-Operating_System
lock	I-Operating_System
over	O
other	O
spinlock	B-Operating_System
algorithms	O
is	O
that	O
it	O
is	O
fair	O
.	O
</s>
<s>
The	O
waiting	O
threads	B-Operating_System
are	O
processed	O
in	O
a	O
first-in	B-Operating_System
first-out	I-Operating_System
basis	O
as	O
the	O
dequeue	O
ticket	O
integer	O
increases	O
,	O
thus	O
preventing	O
starvation	B-Operating_System
.	O
</s>
<s>
A	O
ticket	B-Operating_System
lock	I-Operating_System
algorithm	O
also	O
prevents	O
the	O
thundering	B-Operating_System
herd	I-Operating_System
problem	I-Operating_System
occurring	O
since	O
only	O
one	O
thread	B-Operating_System
at	O
a	O
time	O
tries	O
to	O
enter	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
Storage	O
is	O
not	O
necessarily	O
a	O
problem	O
as	O
all	O
threads	B-Operating_System
spin	O
on	O
one	O
variable	O
,	O
unlike	O
array-based	B-Operating_System
queueing	I-Operating_System
locks	I-Operating_System
(	O
ABQL	B-Operating_System
)	O
who	O
have	O
threads	B-Operating_System
spin	O
on	O
individual	O
elements	O
of	O
an	O
array	O
.	O
</s>
<s>
One	O
disadvantage	O
is	O
that	O
there	O
is	O
a	O
higher	O
uncontended	O
latency	O
due	O
to	O
the	O
extra	O
instructions	O
required	O
to	O
read	O
and	O
test	O
the	O
value	O
that	O
all	O
threads	B-Operating_System
are	O
spinning	O
on	O
before	O
a	O
lock	O
is	O
released	O
.	O
</s>
<s>
Another	O
major	O
disadvantage	O
of	O
a	O
ticket	B-Operating_System
lock	I-Operating_System
is	O
that	O
it	O
is	O
non-scalable	O
.	O
</s>
<s>
Research	O
indicated	O
that	O
as	O
processors	O
are	O
added	O
to	O
a	O
Ticket	B-Operating_System
Lock	I-Operating_System
system	O
,	O
performance	O
appears	O
to	O
decay	O
exponentially	O
.	O
</s>
<s>
All	O
threads	B-Operating_System
are	O
spinning	O
on	O
one	O
variable	O
,	O
so	O
when	O
the	O
lock	O
is	O
released	O
there	O
are	O
Ө(p )	O
invalidations	O
(	O
as	O
well	O
as	O
Ө(p )	O
acquisitions	O
)	O
.	O
</s>
<s>
This	O
is	O
because	O
all	O
threads	B-Operating_System
must	O
reload	O
their	O
block	O
into	O
the	O
cache	O
and	O
perform	O
a	O
test	O
to	O
determine	O
their	O
admittance	O
to	O
the	O
critical	B-Operating_System
section	I-Operating_System
.	O
</s>
<s>
The	O
ticket	B-Operating_System
lock	I-Operating_System
was	O
introduced	O
by	O
Mellor-Crummey	O
and	O
Scott	O
in	O
1991	O
.	O
</s>
<s>
This	O
algorithm	O
was	O
introduced	O
into	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
in	O
2008	O
due	O
to	O
its	O
advantages	O
,	O
but	O
was	O
omitted	O
in	O
paravirtualized	O
environments	O
where	O
it	O
had	O
disadvantages	O
.	O
</s>
<s>
,	O
work	O
is	O
in	O
progress	O
to	O
enable	O
the	O
use	O
of	O
ticket	B-Operating_System
locks	I-Operating_System
in	O
paravirtualization	O
.	O
</s>
<s>
As	O
of	O
March	O
2015	O
this	O
type	O
of	O
locking	B-Operating_System
scheme	O
has	O
been	O
reemployed	O
by	O
Red	O
Hat	O
Enterprise	O
Linux	B-Operating_System
in	O
their	O
system	O
.	O
</s>
<s>
Lamport	B-Operating_System
's	I-Operating_System
bakery	I-Operating_System
algorithm	I-Operating_System
uses	O
a	O
similar	O
concept	O
of	O
a	O
"	O
ticket	O
"	O
or	O
"	O
counter	O
"	O
but	O
does	O
not	O
make	O
the	O
use	O
of	O
atomic	O
hardware	O
operations	O
.	O
</s>
<s>
Queue-based	O
spin	O
locks	O
guarantee	O
fairness	B-Application
by	O
maintaining	O
a	O
queue	O
of	O
waiters	O
and	O
by	O
granting	O
the	O
lock	O
to	O
the	O
first	O
waiter	O
during	O
an	O
unlock	O
operation	O
.	O
</s>
