ticket_lock.v 6.9 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.
7
Set Default Proof Using "Type".
Zhen Zhang's avatar
Zhen Zhang committed
8 9 10
Import uPred.

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

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

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

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

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

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

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

42 43
  Definition lock_inv (γ : gname) (lo ln : loc) (R : iProp Σ) : iProp Σ :=
    ( o n : nat,
44 45 46
      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
47

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

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

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

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

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

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

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

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

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

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

170 171 172
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 |}.