ticket_lock.v 7.08 KB
Newer Older
1 2
From iris.proofmode Require Import tactics.
From iris.algebra Require Import excl auth gset.
3 4
From iris.program_logic Require Export weakestpre.
From iris.heap_lang Require Export lang.
Zhen Zhang's avatar
Zhen Zhang committed
5
From iris.heap_lang Require Import proofmode notation.
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".
Zhen Zhang's avatar
Zhen Zhang committed
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
    let: "n" := !(Snd "lk") in
23
    if: CAS (Snd "lk") "n" ("n" + #1)
24
      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
Class tlockG Σ :=
32
  tlock_G :> inG Σ (authR (prodUR (optionUR (exclR natO)) (gset_disjUR nat))).
33
Definition tlockΣ : gFunctors :=
34
  #[ GFunctor (authR (prodUR (optionUR (exclR natO)) (gset_disjUR nat))) ].
35

36
Instance subG_tlockΣ {Σ} : subG tlockΣ Σ  tlockG Σ.
37
Proof. solve_inG. Qed.
Zhen Zhang's avatar
Zhen Zhang committed
38 39

Section proof.
40 41
  Context `{!heapG Σ, !tlockG Σ}.
  Let N := nroot .@ "ticket_lock".
Zhen Zhang's avatar
Zhen Zhang committed
42

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

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

53
  Definition issued (γ : gname) (x : nat) : iProp Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
54
    own γ ( (ε, GSet {[ x ]}))%I.
Zhen Zhang's avatar
Zhen Zhang committed
55

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

58
  Global Instance lock_inv_ne γ lo ln : NonExpansive (lock_inv γ lo ln).
Zhen Zhang's avatar
Zhen Zhang committed
59
  Proof. solve_proper. Qed.
60 61
  Global Instance is_lock_contractive γ lk : Contractive (is_lock γ lk).
  Proof. solve_contractive. Qed.
62
  Global Instance is_lock_persistent γ lk R : Persistent (is_lock γ lk R).
Zhen Zhang's avatar
Zhen Zhang committed
63
  Proof. apply _. Qed.
64
  Global Instance locked_timeless γ : Timeless (locked γ).
Zhen Zhang's avatar
Zhen Zhang committed
65 66
  Proof. apply _. Qed.

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

73 74 75 76 77 78 79 80 81 82
  Lemma is_lock_iff γ lk R1 R2 :
    is_lock γ lk R1 -   (R1  R2) - is_lock γ lk R2.
  Proof.
    iDestruct 1 as (lo ln ->) "#Hinv"; iIntros "#HR".
    iExists lo, ln; iSplit; [done|]. iApply (inv_iff with "Hinv").
    iIntros "!> !#"; iSplit; iDestruct 1 as (o n) "(Ho & Hn & H● & H)";
      iExists o, n; iFrame "Ho Hn H●";
      (iDestruct "H" as "[[H◯ H]|H◯]"; [iLeft; iFrame "H◯"; by iApply "HR"|by iRight]).
  Qed.

83
  Lemma newlock_spec (R : iProp Σ) :
84
    {{{ R }}} newlock #() {{{ lk γ, RET lk; is_lock γ lk R }}}.
Zhen Zhang's avatar
Zhen Zhang committed
85
  Proof.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
86 87
    iIntros (Φ) "HR HΦ". rewrite -wp_fupd. wp_lam.
    wp_alloc ln as "Hln". wp_alloc lo as "Hlo".
Robbert Krebbers's avatar
Robbert Krebbers committed
88
    iMod (own_alloc ( (Excl' 0%nat, GSet )   (Excl' 0%nat, GSet ))) as (γ) "[Hγ Hγ']".
89
    { by apply auth_both_valid. }
90
    iMod (inv_alloc _ _ (lock_inv γ lo ln R) with "[-HΦ]").
91 92
    { iNext. rewrite /lock_inv.
      iExists 0%nat, 0%nat. iFrame. iLeft. by iFrame. }
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
93
    wp_pures. iModIntro. iApply ("HΦ" $! (#lo, #ln)%V γ). iExists lo, ln. eauto.
Zhen Zhang's avatar
Zhen Zhang committed
94 95
  Qed.

96
  Lemma wait_loop_spec γ lk x R :
97
    {{{ is_lock γ lk R  issued γ x }}} wait_loop #x lk {{{ RET #(); locked γ  R }}}.
98
  Proof.
99
    iIntros (Φ) "[Hl Ht] HΦ". iDestruct "Hl" as (lo ln ->) "#Hinv".
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
100
    iLöb as "IH". wp_rec. subst. wp_pures. wp_bind (! _)%E.
Ralf Jung's avatar
Ralf Jung committed
101
    iInv N as (o n) "(Hlo & Hln & Ha)".
102 103
    wp_load. destruct (decide (x = o)) as [->|Hneq].
    - iDestruct "Ha" as "[Hainv [[Ho HR] | Haown]]".
104
      + iModIntro. iSplitL "Hlo Hln Hainv Ht".
105
        { iNext. iExists o, n. iFrame. }
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
106
        wp_pures. case_bool_decide; [|done]. wp_if.
107
        iApply ("HΦ" with "[-]"). rewrite /locked. iFrame. eauto.
108
      + iDestruct (own_valid_2 with "Ht Haown") as % [_ ?%gset_disj_valid_op].
109
        set_solver.
110
    - iModIntro. iSplitL "Hlo Hln Ha".
111
      { iNext. iExists o, n. by iFrame. }
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
112
      wp_pures. case_bool_decide; [simplify_eq |].
113
      wp_if. iApply ("IH" with "Ht"). iNext. by iExact "HΦ".
114 115
  Qed.

116
  Lemma acquire_spec γ lk R :
117
    {{{ is_lock γ lk R }}} acquire lk {{{ RET #(); locked γ  R }}}.
118
  Proof.
119
    iIntros (ϕ) "Hl HΦ". iDestruct "Hl" as (lo ln ->) "#Hinv".
Robbert Krebbers's avatar
Robbert Krebbers committed
120
    iLöb as "IH". wp_rec. wp_bind (! _)%E. simplify_eq/=. wp_proj.
Ralf Jung's avatar
Ralf Jung committed
121
    iInv N as (o n) "[Hlo [Hln Ha]]".
122
    wp_load. iModIntro. iSplitL "Hlo Hln Ha".
123
    { iNext. iExists o, n. by iFrame. }
Ralf Jung's avatar
Ralf Jung committed
124
    wp_pures. wp_bind (CmpXchg _ _ _).
Ralf Jung's avatar
Ralf Jung committed
125
    iInv N as (o' n') "(>Hlo' & >Hln' & >Hauth & Haown)".
126
    destruct (decide (#n' = #n))%V as [[= ->%Nat2Z.inj] | Hneq].
Ralf Jung's avatar
Ralf Jung committed
127
    - iMod (own_update with "Hauth") as "[Hauth Hofull]".
128 129
      { eapply auth_update_alloc, prod_local_update_2.
        eapply (gset_disj_alloc_empty_local_update _ {[ n ]}).
Robbert Krebbers's avatar
Robbert Krebbers committed
130 131
        apply (set_seq_S_end_disjoint 0). }
      rewrite -(set_seq_S_end_union_L 0).
Ralf Jung's avatar
Ralf Jung committed
132
      wp_cmpxchg_suc. iModIntro. iSplitL "Hlo' Hln' Haown Hauth".
133 134
      { iNext. iExists o', (S n).
        rewrite Nat2Z.inj_succ -Z.add_1_r. by iFrame. }
135
      wp_pures.
Ralf Jung's avatar
Ralf Jung committed
136
      iApply (wait_loop_spec γ (#lo, #ln) with "[-HΦ]").
137
      + iFrame. rewrite /is_lock; eauto 10.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
138
      + by iNext.
Ralf Jung's avatar
Ralf Jung committed
139
    - wp_cmpxchg_fail. iModIntro.
Ralf Jung's avatar
Ralf Jung committed
140
      iSplitL "Hlo' Hln' Hauth Haown".
141
      { iNext. iExists o', n'. by iFrame. }
142
      wp_pures. by iApply "IH"; auto.
143 144
  Qed.

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

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

175
Canonical Structure ticket_lock `{!heapG Σ, !tlockG Σ} : lock Σ :=
176 177
  {| lock.locked_exclusive := locked_exclusive; lock.is_lock_iff := is_lock_iff;
     lock.newlock_spec := newlock_spec;
178
     lock.acquire_spec := acquire_spec; lock.release_spec := release_spec |}.