Peterson’ s lock: Establishing A derivation mutual exclusion Claim: If the following invariant holds when T i is in CS, so does mutual exclusion Combines ideas from Like-to and Selfless locks ( ¬ in j ( in j ∧ turn = i ) ) in i ∧ ∨ Two variables: wants to enter CS, in j : thread is executing in CS, or trying to do so in i T i wants to enter CS does not desire to enter CS in i in j but it is ’ s turn in i : id of thread allowed to enter CS under turn contention ( ) turn = i ⇔ victim = 1 − i � 28 � 29 Establishing Towards a solution mutual exclusion Claim: If the following invariant holds when T i The problem then reduces to establishing the is in CS, so does mutual exclusion following: ( ¬ in j ( in j ∧ turn = i ) ) in i ∧ ∨ in i ∧ ( ¬ in j ∨ ( in j ∧ turn = i )) = in i ∧ ( ¬ in j ∨ turn = i ) wants to enter CS, in j How can we do that? wants to enter CS does not desire to enter CS in i in j but it is ’ s turn in i Why? If we instantiate the invariant for both T 1 and T 2 … …and take the conjunction of the two invariants (i.e., assume both threads in CS)… …it boils down to ( turn = 0 ∧ turn = 1) = false � 30 � 31
Recommend
More recommend