































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
































In
assembly: Functionality:
atomically
read
the
old
value and
store
a
new
value
at
the
location
pointed
to
by
the lock
pointer
returns
the
old
value
(by
storing
it
in
the
register
that
contained
the
new
value)
int
xchg(int
*lock,
int
val)
{
register
int
old;
old
*= lock;
*lock
=
val;
return
(old);
xchg }
(%esi),
%edi
lock
val
old
occur
atomically
Complication:
fundamentally,
this
involves
both
a
load
and
a
store to
a
memory
location
and
these
things
can’t
occur
simultaneously!
How
x
processors
handle
complex
instructions:
the
hardware
translates
x
instructions
into
simpler
μ
op instructions
e.g.,
add (%esi), %edi
actually
turns
into
μ
ops:
load (
%esi
)^
into
a
hardware
register
add
%edi
to
that
hardware
register
store result
into
(
%esi
)
Hence
at
the
μ
op level,
xchg (%esi), %edi
turns
into:
load (
%esi
into
a
hardware
register
-^
(through
hardware
register
renaming,
this
eventually
ends
up
in
%edi
)
store
%edi
into
%esi
Question:
how
do
you
think
cache
coherence handles
this
sequence?
Answer:
need
to
get
hold
the
block
exclusively
throughout
the
sequence
Uniprocessor
(review):
Who
has
the
lock?
Another
thread,
that
is
currently
blocked!
So
spinning
would
be
a
waste
of
time.
Multiprocessor:
Who
has
the
lock?
Another
thread,
that
is
hopefully
currently
running (on
a
different
processor)!
Also,
parallel
programmers
try
not
to
hold
locks
for
very
long
so
hopefully
it
will
become
available
soon,
and
we
want
to
grab
it
ASAP
Spin
waiting
looks
attractive!
unless
of
course
the
thread
holding
the
lock
gets
de
‐scheduled
for
some
reason
common
approach:
spin
for
a
while,
and
eventually
block
while (!xchg(&lock_available,
0)
continue;
while (!xchg(&lock_available,
0)
//
go
to
sleep
until
someone
does
an
unlock
//
(or
at
least
yield)
cond_wait(&unlock_event, …);
Spin
‐Waiting:
Blocking:
do {
while (lock_available == 0)
“Test”
loop
continue;
} while (!xchg(&lock_available, 0);
“Test
and
‐Set”
check
(In
the
synchronization
literature,
our
xchg
lock
is
called
a
“test
‐and
set”
lock.)
Note
that
the
“test”
loop
uses
a
normal
memory
load
(not
an
xchg
How
does
the
coherence
traffic change
with
this
modification?
while
the
lock
is
held,
the
other
processors
spin
locally
in
their
caches
using
normal
read
operations
in
the
“test”
loop,
which
hit
on
the
“Shared”
block
so
there
is
no
longer
a
flood
of
coherence
traffic while
the
lock
is
held
So
are
we
completely
happy
now?
Or
is
there
still
a
problem…
what
happens
when
the
lock
is
released?
a
sudden
burst
of
“test
‐and
set”
attempts,
with
all
but
one
of
them
failing
One
approach:
use
backoff
upon
failing
to
acquire
lock,
delay
for
a
while
before
retrying
either
constant
delay or
exponential backoff
The
good
news:
significantly
reduces
interconnect
traffic
The
bad
news:
exponential
backoff can
lead
to
starvation for
high
contention
locks
new
requestors
back
off
for
shorter
times
Exponential
backoff seems
to
work
well
in
practice.
Two
counters:
Lock: Unlock: •
What
is
the
coherence
traffic
like
upon
an
unlock?
an
invalidation,
and
then
a
read
miss
for
each
spinning
processor
Possible
solution:
use
delay
while
spinning
(but
by
how
much?)
next_ticket (#
of
requestors)
now_serving
of
releases
that
have
happened)
Both
initialized
to
0.
my_ticket =
atomic_fetch_and_increment(&next_ticket);
while (now_serving
!= my_ticket)
continue; now_serving++;
Regular
(non
‐atomic)
operations.
Can
be
implemented
with
Load
‐Linked/Store
‐Conditional
The
good
news:
guaranteed
order
so
starvation
is
not
possible
traffic
can
be
quite
low
But
could
it
be
better
still?
traffic
is
not
guaranteed
to
be
O(1) per
lock
acquire
Lock:
1
0
0
0
0
0
0
0
0 0
1
2
3
4
5
6
7
Availability
Flags:
Next
Position
in
Queue:
CPU
7
CPU
7
CPU
7
CPU
4
CPU
4
CPU
4
CPU
3
CPU
3
CPU
3
0123
my_index
= atomic_fetch_and_increment(&next_position)%NUM_PROCESSORS;
while (!lock_available[my_index])
continue;
Lock: Unlock: Tradeoffs:
Good:
FIFO order;
traffic (with
cache
coherence)
Bad:
requires
space
per
lock
proportional
to
P (x
cache
line
size)
1
0
0
0
0
0
0
0
0 0
1
2
3
4
5
6
7
Availability
Flags:
Next
Position
in
Queue:
CPU
7
CPU
7
CPU
7
CPU
4
CPU
4
CPU
4
CPU
3
CPU
3
CPU
3
0123
my_index
= atomic_fetch_and_increment(&next_position)%NUM_PROCESSORS;
while (!lock_available[my_index])
continue;
lock_available[my_index] = 0;next_index = (my_index+1)%NUM_PROCESSORS;lock_available[next_index] = 1;
0
1
It
depends
on
the
amount
of
lock
contention.
Low
contention locks:
test
and
test
‐and
‐set is
faster
less
work
to
acquire
the
lock
High
‐contention locks:
queue
based locks
may
be
faster
less
communication
traffic,
especially
on
large
‐scale
systems
Hybrid
approaches
have
been
proposed
switch
from
one
to
the
other,
depending
on
observed
contention
Functionality:
locks
have
their
own
addresses
separate
from
the
data
that
they
protect
mapping
between
locks
and
protected
data
is
not
explicit
usually
only
exists
in
programmer’s
head
locks
are
not
composable
concurrent
threads
often
need
to
know
locking
behavior
to
avoid
breaking
code
in
contrast
with
usual
modular
code
encapsulation
in
single
‐threaded
code
e.g.,
stack
modules
with
abstract
push/pop
operations:
moving
item
from
one
stack
to
another
requires
exposing
locking
mechanism
Performance:
locks
are
a
pessimistic
way
to
achieve
mutual
exclusion
need
to
get
permission
first,
before
starting
the
critical
section
code
if
a
collision
is
unlikely,
a
more
optimistic
approach
may
perform
better
Source Code: ...atomic
...access_shared_data();... } ...
TM System
Specifies threads/transactions in source code
...atomic
...access_shared_data();... } ...
...atomic
...access_shared_data();... } ...
Transactions:
Executes transactions optimistically in parallel
Programmer:
TM System:
Hardware
Changes
to
computer
system
and
extend
caches
to
buffer
writes
extended
coherence
protocol
to
track
conflicts
special
transaction
instructions
Support
for
limited
number
of
memory
locations
Software
Language
runtime
(or
library)
extensions
to
specify
transaction
Exploit
current
commodity
hardware
(multicores)
Get
experience
with
transactional
programming
model
Java:
(Marathe et
al.),
(Herlihy et
al.)
McRT
(Saha et
al.),
(Dice
et
al.),
Hybrid
TM (HyTM)