ticket_lock.v 6.86 KB
Newer Older
1 2
From iris.program_logic Require Export weakestpre.
From iris.heap_lang Require Export lang.
3
From iris.proofmode Require Import tactics.
Zhen Zhang's avatar
Zhen Zhang committed
4
From iris.heap_lang Require Import proofmode notation.
5
From iris.algebra Require Import auth gset.
6
From iris.heap_lang.lib Require Export lock.
Zhen Zhang's avatar
Zhen Zhang committed
7 8 9
Import uPred.

Definition wait_loop: val :=
10
  rec: "wait_loop" "x" "lk" :=
11
    let: "o" := !(Fst "lk") in
Zhen Zhang's avatar
Zhen Zhang committed
12 13
    if: "x" = "o"
      then #() (* my turn *)
14
      else "wait_loop" "x" "lk".
Zhen Zhang's avatar
Zhen Zhang committed
15

16
Definition newlock : val :=
17
  λ: <>, ((* owner *) ref #0, (* next *) ref #0).
Zhen Zhang's avatar
Zhen Zhang committed
18 19

Definition acquire : val :=
20
  rec: "acquire" "lk" :=
21 22 23
    let: "n" := !(Snd "lk") in
    if: CAS (Snd "lk") "n" ("n" + #1)
      then wait_loop "n" "lk"
24
      else "acquire" "lk".
Zhen Zhang's avatar
Zhen Zhang committed
25

26
Definition release : val :=
27
  λ: "lk", (Fst "lk") <- !(Fst "lk") + #1.
Zhen Zhang's avatar
Zhen Zhang committed
28

29
(** The CMRAs we need. *)
30 31
Class tlockG Σ :=
  tlock_G :> inG Σ (authR (prodUR (optionUR (exclR natC)) (gset_disjUR nat))).
32
Definition tlockΣ : gFunctors :=
33
  #[ GFunctor (constRF (authR (prodUR (optionUR (exclR natC)) (gset_disjUR nat)))) ].
34

35
Instance subG_tlockΣ {Σ} : subG tlockΣ Σ  tlockG Σ.
36
Proof. by intros ?%subG_inG. Qed.
Zhen Zhang's avatar
Zhen Zhang committed
37 38

Section proof.
Zhen Zhang's avatar
Zhen Zhang committed
39
  Context `{!heapG Σ, !tlockG Σ} (N : namespace).
Zhen Zhang's avatar
Zhen Zhang committed
40

41 42
  Definition lock_inv (γ : gname) (lo ln : loc) (R : iProp Σ) : iProp Σ :=
    ( o n : nat,
43 44 45
      lo  #o  ln  #n 
      own γ ( (Excl' o, GSet (seq_set 0 n))) 
      ((own γ ( (Excl' o, ))  R)  own γ ( (, GSet {[ o ]}))))%I.
Zhen Zhang's avatar
Zhen Zhang committed
46

47 48
  Definition is_lock (γ : gname) (lk : val) (R : iProp Σ) : iProp Σ :=
    ( lo ln : loc,
49
       lk = (#lo, #ln)%V  inv N (lock_inv γ lo ln R))%I.
Zhen Zhang's avatar
Zhen Zhang committed
50

51 52
  Definition issued (γ : gname) (lk : val) (x : nat) (R : iProp Σ) : iProp Σ :=
    ( lo ln: loc,
53
       lk = (#lo, #ln)%V  inv N (lock_inv γ lo ln R) 
54
       own γ ( (, GSet {[ x ]})))%I.
Zhen Zhang's avatar
Zhen Zhang committed
55

56
  Definition locked (γ : gname) : iProp Σ := ( o, own γ ( (Excl' o, )))%I.
Zhen Zhang's avatar
Zhen Zhang committed
57

58 59
  Global Instance lock_inv_ne n γ lo ln :
    Proper (dist n ==> dist n) (lock_inv γ lo ln).
Zhen Zhang's avatar
Zhen Zhang committed
60
  Proof. solve_proper. Qed.
61
  Global Instance is_lock_ne γ n lk : Proper (dist n ==> dist n) (is_lock γ lk).
Zhen Zhang's avatar
Zhen Zhang committed
62
  Proof. solve_proper. Qed.
63
  Global Instance is_lock_persistent γ lk R : PersistentP (is_lock γ lk R).
Zhen Zhang's avatar
Zhen Zhang committed
64
  Proof. apply _. Qed.
65
  Global Instance locked_timeless γ : TimelessP (locked γ).
Zhen Zhang's avatar
Zhen Zhang committed
66 67
  Proof. apply _. Qed.

68
  Lemma locked_exclusive (γ : gname) : locked γ - locked γ - False.
69
  Proof.
70 71
    iDestruct 1 as (o1) "H1". iDestruct 1 as (o2) "H2".
    iDestruct (own_valid_2 with "H1 H2") as %[[] _].
72
  Qed.
Zhen Zhang's avatar
Zhen Zhang committed
73

74
  Lemma newlock_spec (R : iProp Σ) :
75
    {{{ R }}} newlock #() {{{ lk γ, RET lk; is_lock γ lk R }}}.
Zhen Zhang's avatar
Zhen Zhang committed
76
  Proof.
77
    iIntros (Φ) "HR HΦ". rewrite -wp_fupd /newlock /=.
Zhen Zhang's avatar
Zhen Zhang committed
78
    wp_seq. wp_alloc lo as "Hlo". wp_alloc ln as "Hln".
79
    iMod (own_alloc ( (Excl' 0%nat, )   (Excl' 0%nat, ))) as (γ) "[Hγ Hγ']".
80
    { by rewrite -auth_both_op. }
81
    iMod (inv_alloc _ _ (lock_inv γ lo ln R) with "[-HΦ]").
82 83
    { iNext. rewrite /lock_inv.
      iExists 0%nat, 0%nat. iFrame. iLeft. by iFrame. }
84
    iModIntro. iApply ("HΦ" $! (#lo, #ln)%V γ). iExists lo, ln. eauto.
Zhen Zhang's avatar
Zhen Zhang committed
85 86
  Qed.

87
  Lemma wait_loop_spec γ lk x R :
88
    {{{ issued γ lk x R }}} wait_loop #x lk {{{ RET #(); locked γ  R }}}.
89
  Proof.
90
    iIntros (Φ) "Hl HΦ". iDestruct "Hl" as (lo ln) "(% & #? & Ht)".
91
    iLöb as "IH". wp_rec. subst. wp_let. wp_proj. wp_bind (! _)%E.
92
    iInv N as (o n) "(Hlo & Hln & Ha)" "Hclose".
93 94
    wp_load. destruct (decide (x = o)) as [->|Hneq].
    - iDestruct "Ha" as "[Hainv [[Ho HR] | Haown]]".
95
      + iMod ("Hclose" with "[Hlo Hln Hainv Ht]") as "_".
96
        { iNext. iExists o, n. iFrame. eauto. }
97
        iModIntro. wp_let. wp_op=>[_|[]] //.
98
        wp_if. 
99
        iApply ("HΦ" with "[-]"). rewrite /locked. iFrame. eauto.
100
      + iDestruct (own_valid_2 with "Ht Haown") as % [_ ?%gset_disj_valid_op].
101
        set_solver.
102
    - iMod ("Hclose" with "[Hlo Hln Ha]").
103
      { iNext. iExists o, n. by iFrame. }
104
      iModIntro. wp_let. wp_op=>[[/Nat2Z.inj //]|?].
105
      wp_if. iApply ("IH" with "Ht"). iNext. by iExact "HΦ".
106 107
  Qed.

108
  Lemma acquire_spec γ lk R :
109
    {{{ is_lock γ lk R }}} acquire lk {{{ RET #(); locked γ  R }}}.
110
  Proof.
111
    iIntros (ϕ) "Hl HΦ". iDestruct "Hl" as (lo ln) "[% #?]".
112 113
    iLöb as "IH". wp_rec. wp_bind (! _)%E. subst. wp_proj.
    iInv N as (o n) "[Hlo [Hln Ha]]" "Hclose".
114
    wp_load. iMod ("Hclose" with "[Hlo Hln Ha]") as "_".
115
    { iNext. iExists o, n. by iFrame. }
116
    iModIntro. wp_let. wp_proj. wp_op.
117
    wp_bind (CAS _ _ _).
118
    iInv N as (o' n') "(>Hlo' & >Hln' & >Hauth & Haown)" "Hclose".
119 120
    destruct (decide (#n' = #n))%V as [[= ->%Nat2Z.inj] | Hneq].
    - wp_cas_suc.
121
      iMod (own_update with "Hauth") as "[Hauth Hofull]".
122 123 124
      { eapply auth_update_alloc, prod_local_update_2.
        eapply (gset_disj_alloc_empty_local_update _ {[ n ]}).
        apply (seq_set_S_disjoint 0). }
125
      rewrite -(seq_set_S_union_L 0).
126
      iMod ("Hclose" with "[Hlo' Hln' Haown Hauth]") as "_".
127 128
      { iNext. iExists o', (S n).
        rewrite Nat2Z.inj_succ -Z.add_1_r. by iFrame. }
129
      iModIntro. wp_if.
Ralf Jung's avatar
Ralf Jung committed
130 131 132
      iApply (wait_loop_spec γ (#lo, #ln) with "[-HΦ]").
      + rewrite /issued; eauto 10.
      + by iNext. 
133
    - wp_cas_fail.
134
      iMod ("Hclose" with "[Hlo' Hln' Hauth Haown]") as "_".
135
      { iNext. iExists o', n'. by iFrame. }
Ralf Jung's avatar
Ralf Jung committed
136
      iModIntro. wp_if. by iApply "IH"; auto.
137 138
  Qed.

139
  Lemma release_spec γ lk R :
140
    {{{ is_lock γ lk R  locked γ  R }}} release lk {{{ RET #(); True }}}.
141
  Proof.
142
    iIntros (Φ) "(Hl & Hγ & HR) HΦ". iDestruct "Hl" as (lo ln) "[% #?]"; subst.
143
    iDestruct "Hγ" as (o) "Hγo".
144
    rewrite /release. wp_let. wp_proj. wp_proj. wp_bind (! _)%E.
145 146
    iInv N as (o' n) "(>Hlo & >Hln & >Hauth & Haown)" "Hclose".
    wp_load.
147
    iDestruct (own_valid_2 with "Hauth Hγo") as
148
      %[[<-%Excl_included%leibniz_equiv _]%prod_included _]%auth_valid_discrete_2.
149
    iMod ("Hclose" with "[Hlo Hln Hauth Haown]") as "_".
150
    { iNext. iExists o, n. by iFrame. }
151
    iModIntro. wp_op.
152 153
    iInv N as (o' n') "(>Hlo & >Hln & >Hauth & Haown)" "Hclose".
    wp_store.
154
    iDestruct (own_valid_2 with "Hauth Hγo") as
155
      %[[<-%Excl_included%leibniz_equiv _]%prod_included _]%auth_valid_discrete_2.
156
    iDestruct "Haown" as "[[Hγo' _]|?]".
157 158
    { iDestruct (own_valid_2 with "Hγo Hγo'") as %[[] ?]. }
    iMod (own_update_2 with "Hauth Hγo") as "[Hauth Hγo]".
159 160
    { apply auth_update, prod_local_update_1.
      by apply option_local_update, (exclusive_local_update _ (Excl (S o))). }
161
    iMod ("Hclose" with "[Hlo Hln Hauth Haown Hγo HR]") as "_"; last by iApply "HΦ".
162 163
    iNext. iExists (S o), n'.
    rewrite Nat2Z.inj_succ -Z.add_1_r. iFrame. iLeft. by iFrame.
164
  Qed.
Zhen Zhang's avatar
Zhen Zhang committed
165 166 167
End proof.

Typeclasses Opaque is_lock issued locked.
Zhen Zhang's avatar
Zhen Zhang committed
168

169 170 171
Definition ticket_lock `{!heapG Σ, !tlockG Σ} : lock Σ :=
  {| lock.locked_exclusive := locked_exclusive; lock.newlock_spec := newlock_spec;
     lock.acquire_spec := acquire_spec; lock.release_spec := release_spec |}.