ticket_lock.v 6.6 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".
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 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 (authR (prodUR (optionUR (exclR natC)) (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.
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
      lo  #o  ln  #n 
      own γ ( (Excl' o, GSet (seq_set 0 n))) 
Robbert Krebbers's avatar
Robbert Krebbers committed
46
      ((own γ ( (Excl' o, GSet ))  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
  Definition issued (γ : gname) (x : nat) : iProp Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
53
    own γ ( (ε, GSet {[ x ]}))%I.
Zhen Zhang's avatar
Zhen Zhang committed
54

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

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

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

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

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

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

165
Canonical Structure ticket_lock `{!heapG Σ, !tlockG Σ} : lock Σ :=
166 167
  {| lock.locked_exclusive := locked_exclusive; lock.newlock_spec := newlock_spec;
     lock.acquire_spec := acquire_spec; lock.release_spec := release_spec |}.