-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof_treiber.v
195 lines (152 loc) · 6.24 KB
/
proof_treiber.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
From iris.base_logic.lib Require Import invariants ghost_var.
From smr.program_logic Require Import atomic.
From smr.lang Require Import proofmode notation.
From iris.prelude Require Import options.
From smr Require Import helpers no_recl.spec_stack no_recl.code_treiber.
Class treiberG Σ := TreiberG {
#[local] treiber_ghost_varG :: ghost_varG Σ (list val);
}.
Definition treiberΣ : gFunctors := #[ghost_varΣ (list val)].
Global Instance subG_treiberΣ {Σ} :
subG treiberΣ Σ → treiberG Σ.
Proof. solve_inG. Qed.
Section treiber_stack.
Context `{!heapGS Σ, !treiberG Σ}.
Notation iProp := (iProp Σ).
Context (treiberN : namespace).
Ltac exfr := repeat (repeat iExists _; iFrame "∗#%").
Fixpoint phys_list (lopt : option loc) (xs : list val) : iProp :=
match (lopt, xs) with
| (None , [] ) => True
| (None , _ :: _ ) => False
| (Some _, [] ) => False
| (Some l, x :: xs) => ∃ n,
l ↦∗□ [ x; #(oloc_to_lit n) ] ∗
phys_list n xs
end.
(* Ownership of the stack *)
Definition TStack (γ : gname) (xs : list val) : iProp :=
ghost_var γ (1/2)%Qp xs.
Global Instance TStack_Timeless γ xs: Timeless (TStack γ xs).
Proof. apply _. Qed.
Definition TStackInternalInv (st : loc) (γs : gname) : iProp :=
∃ (h : option loc) (xs : list val),
phys_list h xs ∗ (st +ₗ head) ↦ #(oloc_to_lit h) ∗ ghost_var γs (1/2)%Qp xs.
(* Persistent assertions about the stack *)
Definition IsTStack (γ : gname) (st : loc) : iProp :=
inv treiberN (TStackInternalInv st γ).
Global Instance IsTStack_Persistent γ l : Persistent (IsTStack γ l).
Proof. apply _. Qed.
(** * Automation hints for [eauto] ******************************************)
Local Hint Extern 0 (environments.envs_entails _
(TStack _ _ [])) => iExists None : core.
Local Hint Extern 0 (environments.envs_entails _
(TStack _ _ (_ :: _))) => iExists (Some _) : core.
Local Hint Extern 10 (environments.envs_entails _
(TStack _ _ _)) => unfold TStack : core.
Local Hint Extern 0 (environments.envs_entails _
(phys_list _ None [])) => simpl : core.
Local Hint Extern 0 (environments.envs_entails _
(phys_list _ (Some _) (_ :: _))) => simpl : core.
Local Hint Extern 0 (environments.envs_entails _
(IsTStack _ _)) => iExists _ : core.
Lemma tstack_new_spec :
stack_new_spec' treiberN tstack_new TStack IsTStack.
Proof.
iIntros (Φ) "!> _ HΦ".
wp_lam. wp_alloc st as "st↦" "†st". wp_pures.
rewrite array_singleton Loc.add_0. wp_store.
iMod (ghost_var_alloc []) as (γs) "[γs γs_I]".
iAssert (TStack γs []) with "γs_I" as "S".
iMod (inv_alloc treiberN _ (TStackInternalInv st γs) with "[-HΦ S]") as "#Inv".
{ iNext. iExists None, []. rewrite Loc.add_0. iFrame "∗#". }
iApply "HΦ". by iFrame "∗#%".
Qed.
Lemma tstack_push_spec :
stack_push_spec' treiberN tstack_push TStack IsTStack.
Proof.
iIntros (γ st x).
iDestruct 1 as "#Inv".
iIntros (Φ) "AU".
wp_lam. wp_alloc new as "new↦" "†new". wp_pures.
wp_apply (wp_store_offset with "new↦") as "new↦"; [by simplify_list_eq|]. wp_pures.
move: #0 => next.
iLöb as "IH" forall (next).
wp_rec. wp_pures. wp_bind (! _)%E.
(* Open inv to load head from st. *)
iInv "Inv" as (h1 xs1) "[Nodes >(st.h↦ & γs)]".
wp_load.
(* close inv *)
iModIntro. iSplitL "Nodes st.h↦ γs"; first by exfr.
wp_pures.
wp_apply (wp_store_offset with "new↦") as "new↦"; [by simplify_list_eq|]. wp_pures.
wp_bind (CmpXchg _ _ _).
iInv "Inv" as (h2 xs2) "[Nodes >(st.h↦ & γs)]".
case (decide (h2 = h1)) as [->|NE].
- (* successful CAS; commit push *) iClear "IH".
iMod (array_persist with "new↦") as "new↦".
wp_cmpxchg_suc.
iAssert (phys_list (Some (Loc.blk_to_loc new)) (x::xs2)) with "[new↦ Nodes]" as "Nodes'"; first by exfr.
iMod "AU" as (?) "[γs' [_ Commit]]".
iDestruct (ghost_var_agree with "γs γs'") as %<-.
iMod (ghost_var_update_halves (x :: xs2) with "γs γs'") as "[γs γs']".
iMod ("Commit" with "γs'") as "HΦ".
(* close inv *)
iModIntro. iSplitL "Nodes' st.h↦ γs"; first by exfr.
wp_pures. by iApply "HΦ".
- (* failed CAS; restore AU *)
wp_cmpxchg_fail.
(* close inv *)
iModIntro. iSplitL "Nodes st.h↦ γs"; first by exfr.
wp_pure. wp_if. wp_apply ("IH" with "AU †new new↦").
Qed.
Lemma tstack_pop_spec :
stack_pop_spec' treiberN tstack_pop TStack IsTStack.
Proof using All.
iIntros (γ st) "#Inv".
iIntros (Φ) "AU".
iLöb as "IH".
wp_rec. wp_pures. wp_bind (! _)%E.
(* If the validation read is null, commit empty pop. Otherwise, restore AU. *)
iInv "Inv" as (h1 xs1) "[Nodes >(st.h↦ & γs)]".
wp_load.
destruct h1 as [h1|], xs1 as [|x1 xs1']; simpl;
try done; last first.
{ iClear "IH".
iMod "AU" as (xs1') "[γs' [_ Commit]]".
iDestruct (ghost_var_agree with "γs γs'") as %<-.
iMod ("Commit" with "γs'") as "HΦ".
(* close inv *)
iModIntro. iSplitL "Nodes st.h↦ γs"; first by (iExists None,[]; exfr).
wp_pures.
iApply "HΦ".
}
(* prove for non-empty stack case and restore AU *)
iDestruct "Nodes" as (n1) "(#h↦ & Nodes)".
(* close inv *) iModIntro.
iSplitL "Nodes st.h↦ γs".
{ iExists (Some h1), _. iFrame. repeat iExists _. iFrame "∗#%". }
wp_pures.
wp_apply (wp_load_offset with "h↦") as "_"; [by simplify_list_eq|].
wp_pures. wp_bind (CmpXchg _ _ _).
iInv "Inv" as (h2 xs2) "[Nodes >(st.h↦ & γs)]".
case (decide (h2 = Some h1)) as [->|NE].
- (* successful CAS; commit pop *) iClear "IH".
destruct xs2 as [|x2 xs2']; [iMod "Nodes"; done|]. simpl.
iDestruct "Nodes" as (n2) "(#h'↦ & Nodes')".
wp_cmpxchg_suc.
iDestruct (array_agree with "h↦ h'↦") as %[= <- <-]; [done|].
iMod "AU" as (xs2) "[γs' [_ Commit]]".
iDestruct (ghost_var_agree with "γs γs'") as %<-.
iMod (ghost_var_update_halves (xs2') with "γs γs'") as "[γs γs']".
iMod ("Commit" with "γs'") as "HΦ".
iModIntro. iSplitL "Nodes' st.h↦ γs"; first by exfr. wp_pures.
wp_apply (wp_load_offset with "h↦") as "_"; [by simplify_list_eq|].
wp_pures. iApply "HΦ".
- (* failed CAS; restore AU *)
wp_cmpxchg_fail.
iModIntro. iSplitL "Nodes st.h↦ γs"; first by exfr.
wp_pure. wp_if. wp_apply ("IH" with "AU").
Qed.
#[export] Typeclasses Opaque TStack IsTStack.
End treiber_stack.