Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf ·...

51
Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Transcript of Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf ·...

Page 1: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Mutual Exclusion

Companion slides for

The Art of Multiprocessor Programming

by Maurice Herlihy & Nir Shavit

Page 2: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

What Is Mutual Exclusion?

Page 3: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

What Is Mutual Exclusion?

Page 4: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

What Is Mutual Exclusion?

Page 5: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

What Is Mutual Exclusion?

Page 6: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

A More Realistic Example

myValue = counter++

myValue ← read(counter) write(counter, myValue+1)

Is this good?

Page 7: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

A More Realistic Example

myValue = counter++

lock() myValue ← read(counter) write(counter, myValue+1) unlock()

Is this good?

Page 8: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

The Critical Section

Bla bla

Bla bla bla

lock()

unlock()

Critical

section

Page 9: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Requirements

• Mutual exclusion:

• Critical sections may not overlap time

process i is in the

critical section

process j is in the

critical section

Page 10: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Requirements

• Violation of mutual exclusion:

time

Page 11: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Liveness

• The trivial solution for mutual exclusion:

Page 12: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Deadlock-Free

• If some thread calls lock()

– And never returns

– Then other threads must complete lock()

and unlock() calls infinitely often

• System as a whole makes progress

– Even if individuals starve

Page 13: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Starvation-Free

• If some thread calls lock()

– It will eventually return

• Individual threads make progress

Page 14: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

2-PROCESS MUTUAL

EXCLUSION

Page 15: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Attempt 1

process 0 process 0

Lock() code for process i:

flag[i] = 1;

while(flag[1-i] == 1) ; // wait

flag[2]

Page 16: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Attempt 1

process 0 process 0

Lock() code for process i:

flag[i] = 1;

while(flag[1-i] == 1) ; // wait

flag[2]

Page 17: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Attempt 1

process 0 process 0

Lock() code for process i:

flag[i] = 1;

while(flag[1-i] == 1) ; // wait

flag[2]

Page 18: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Attempt 1

process 0 process 0

Lock() code for process i:

flag[i] = 1;

while(flag[1-i] == 1) ; // wait

Unlock() code for process i:

flag[i] = 0;

flag[2]

Page 19: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Attempt 1: Mutual Exclusion?

• Yes

• Suppose both are in the critical section…

Lock() code for process i:

flag[i] = 1;

while(flag[1-i] == 1) ; // wait

Unlock() code for process i:

flag[i] = 0;

Page 20: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Attempt 1: Deadlock Freedom?

• Nope

Lock() code for process i:

flag[i] = 1;

while(flag[1-i] == 1) ; // wait

Unlock() code for process i:

flag[i] = 0;

Page 21: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Deadlock:

process 0 process 0

Lock() code for process i:

flag[i] = 1;

while(flag[1-i] == 1) ; // wait

flag[2]

Page 22: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Deadlock:

process 0 process 0

Lock() code for process i:

flag[i] = 1;

while(flag[1-i] == 1) ; // wait

flag[2]

But sequential

executions

are OK!

Page 23: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Attempt 2

code for process i:

int victim;

lock()

{

victim = i;

while (victim == i) {} // wait

}

unlock() {}

Page 24: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Attempt 2

code for process i:

int victim;

lock()

{

victim = i;

while (victim == i) {} // wait

}

unlock() {}

Page 25: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

lock() {

victim = i;

while (victim == i) {};

}

Attempt 2

• Satisfies mutual exclusion

– If thread i in CS

– Then victim == j

– Cannot be both 0 and 1

• Not deadlock free

– Sequential execution deadlocks

– Concurrent execution does not

Page 26: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Peterson's Algorithm

lock()

{

flag[i] = true;

victim = i;

while (flag[j] && victim == i) {};

}

unlock()

flag[i] = false;

}

Page 27: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

N-PROCESS MUTEX:

LAMPORT’S BAKERY ALGORITHM

Page 28: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Lamport’s Bakery Algorithm

A queue in Israel…

Page 29: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Lamport’s Bakery Algorithm

A queue in California

Page 30: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Bakery Algorithm

• Lock():

1. “Take a number”

2. Wait until lower numbers are done

• Lexicographic order

– (a,i) > (b,j) • If a > b, or a = b and i > j

Page 31: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Bakery Algorithm

flag[N]

number[N]

Page 32: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Bakery Algorithm

flag[N]

number[N]

max_num = max { number[1],…,number[N] };

number[i] = max_num;

Page 33: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Bakery Algorithm

flag[N]

number[N]

wait while ∃k: flag[k] == 1

and

(number[k],k) < (number[i],i)

Page 34: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Deep Philosophical Question

• The Bakery Algorithm is

– Succinct,

– Elegant, and

– Fair.

• Q: So why isn't it practical?

• A: N distinct variables!

Page 35: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Theorem

At least N registers are needed to

solve deadlock-free mutual exclusion.

(No matter how big they are!)

Page 36: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Theorem (For 2 Threads)

Theorem: Deadlock-free mutual

exclusion for 2 threads requires at least

2 multi-reader multi-writer registers

Proof: assume one register suffices

and derive a contradiction

Page 37: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Proof (2 Threads)

B

A

R

CS

Claim: A must write to R Suppose not…

CS

same!

Page 38: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Proof (2 Threads)

B

A

R

CS

CS

CS

Page 39: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Theorem

Deadlock-free mutual exclusion for 3

threads requires at least 3 registers

Page 40: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Proof: Assume Cover of 2

Write(RB)

B

Write(RC)

C A

Only 2 registers

Registers look as if CS empty

Page 41: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Run A Solo

Write(RB)

B

Write(RC)

C A

Writes to one or both

registers, enters CS CS

Page 42: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Obliterate Traces of A

Write(RB)

B

Write(RC)

C A

Other threads obliterate evidence

that A entered CS CS

Page 43: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Mutual Exclusion Fails

Write(RB)

B

Write(RC)

C A

CS CS

CS looks empty, so

another thread gets

in

Page 44: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Proof Strategy

• Proved: a contradiction starting from a

covering state for 2 registers

• Claim: can reach a covering state

Page 45: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

• If we run B through CS 3 times, first write of B

must twice be to some register, say RB

Covering State for Two

Write(RB)

B

Write(RA)

A

Page 46: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Covering State for Two

• Start with B covering register RB for the 1st time • Run A until it is about to write to uncovered RA

• Are we done?

Write(RB)

B

Write(RA)

A

Page 47: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Covering State for Two

• NO! A could have written to RB

• So CS no longer looks empty to thread C

Write(RB)

B

Write(RA)

A

Page 48: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Covering State for Two

• Run B obliterating traces of A in RB

• Run B again until it is about to write to RB

• Now we are done

Write(RB)

B

Write(RA)

A

Page 49: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Inductively We Can Show

• There is a covering state

– Where k threads not in CS cover k distinct registers

– Proof follows when k = N-1

Write(RB)

B

Write(RC)

C

Write(RA)

A

Page 50: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

Summary of Lecture

• In the 1960's several incorrect solutions

to mutual exclusion using registers were

published…

• We saw how to solve N thread mutual

exclusion using 2N R/W Registers

• … but N R/W registers are needed

Page 51: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../sites/46/2018/01/mutex.pdf · Deadlock-Free • If some thread calls lock() –And never returns –Then other threads must

“Cheating” Using Test&Set…

• test&set(x):

if x == 1 return 0

if x == 0 set x = 1 and return 1