-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof_treiber.v
236 lines (184 loc) · 8.47 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
From iris.algebra Require Import agree.
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 ebr.spec_rcu_simple ebr.spec_stack ebr.code_treiber.
Class treiberG Σ := TreiberG {
#[local] treiber_ghost_varG :: ghost_varG Σ (list val);
#[local] treiber_inG :: inG Σ (agreeR (prodO valO (optionO blkO)));
}.
Definition treiberΣ : gFunctors := #[ghost_varΣ (list val); GFunctor (agreeR (prodO valO (optionO blkO)))].
Global Instance subG_treiberΣ {Σ} :
subG treiberΣ Σ → treiberG Σ.
Proof. solve_inG. Qed.
Section treiber_stack.
Context `{!heapGS Σ, !treiberG Σ}.
Notation iProp := (iProp Σ).
Context (treiberN rcuN : namespace) (DISJN : treiberN ## rcuN).
(* iExists + iFrame *)
Ltac exfr := repeat (repeat iExists _; iFrame "∗#%").
Variable (rcu : rcu_simple_spec Σ rcuN).
Definition node_info γ_p (x : val) (n : option blk) :=
own γ_p (to_agree (x, n)).
Definition node (p : blk) lv γ_p : iProp :=
∃ x n, ⌜lv = [ x; #(oblk_to_lit n) ]⌝ ∗ node_info γ_p x n.
Fixpoint phys_list γe (lopt : option blk) (xs : list val) : iProp :=
match (lopt, xs) with
| (None , [] ) => True
| (None , _ :: _ ) => False
| (Some _, [] ) => False
| (Some l, x :: xs) => ∃ γ_l n,
rcu.(Managed) γe l γ_l nodeSize node ∗ node_info γ_l x n ∗
phys_list γe 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) (γe γs : gname) : iProp :=
∃ (h : option blk) (xs : list val),
phys_list γe h xs ∗ (st +ₗ head) ↦ #(oblk_to_lit h) ∗ ghost_var γs (1/2)%Qp xs.
(* Persistent assertions about the stack *)
Definition IsTStack (γe : gname) (γs : gname) (st : loc) : iProp :=
∃ (d : loc),
(st +ₗ domain) ↦□ #d ∗ rcu.(IsRCUDomain) γe d ∗
inv treiberN (TStackInternalInv st γe γs).
Global Instance IsTStack_Persistent γe γs st : Persistent (IsTStack γe γs st).
Proof. apply _. Qed.
(** * Automation hints for [eauto] ******************************************)
Local Hint Extern 0 (environments.envs_entails _
(node _ _ _)) => iExists _,_ : core.
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 rcuN tstack_new rcu TStack IsTStack.
Proof.
iIntros (γe d Φ) "!> #IED HΦ".
wp_lam. wp_alloc st as "st↦" "†st". wp_pures.
do 2 (wp_apply (wp_store_offset with "st↦") as "st↦"; [by simplify_list_eq|]; wp_pures).
rewrite /= array_cons array_singleton.
iDestruct "st↦" as "[st.h↦ st.d↦]".
iMod (pointsto_persist with "st.d↦") as "#st.d↦".
iMod (ghost_var_alloc []) as (γs) "[γs γs_I]".
iAssert (TStack γs []) with "[γs_I]" as "S"; first by iFrame.
iMod (inv_alloc treiberN _ (TStackInternalInv _ _ _) with "[-HΦ S]") as "#Inv".
{ iNext. iExists None, []. rewrite Loc.add_0. iFrame "∗#". }
iModIntro. iApply "HΦ". iFrame "∗#%".
Qed.
Lemma tstack_push_spec :
stack_push_spec' treiberN rcuN tstack_push rcu TStack IsTStack.
Proof using All.
iIntros (γe γs st g x).
iDestruct 1 as (?) "#(st.d↦ & IED & Inv)".
iIntros "G" (Φ) "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 (own_alloc (to_agree (x, h1))) as (γ_n) "#Info_new"; [done|].
iAssert (node new _ γ_n) with "[Info_new]" as "N_new"; [eauto|].
iMod (rcu.(rcu_domain_register) node with "IED [$new↦ $†new $N_new]") as "G_new"; [solve_ndisj|].
wp_cmpxchg_suc.
iAssert (phys_list γe (Some new) (x::xs2)) with "[Info_new G_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. iApply ("IH" with "G AU †new new↦").
Qed.
Lemma tstack_pop_spec :
stack_pop_spec' treiberN rcuN (tstack_pop rcu) rcu TStack IsTStack.
Proof using All.
iIntros (γe γs st g).
iDestruct 1 as (?) "#(st.d↦ & IED & Inv)".
iIntros "G" (Φ) "AU".
wp_lam. wp_pures.
wp_apply (rcu.(guard_activate_spec) with "IED G") as (?) "G"; [solve_ndisj|..].
wp_seq.
wp_bind ((tstack_pop_loop rcu) _). iLöb as "IH".
wp_rec. wp_pures. wp_bind (! _)%E.
(* If the value read is null, commit empty pop. Otherwise, register and 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.
wp_apply (rcu.(guard_deactivate_spec) with "IED G") as "G"; [solve_ndisj|].
wp_seq.
iApply "HΦ". by iFrame. }
(* prove for non-empty stack case and restore AU *)
iDestruct "Nodes" as (γ_h1 n1) "(G_h1 & #Info_h1 & Nodes)".
iMod (rcu.(guard_protect) with "IED G_h1 G") as "(G_h1 & G & #h1Info)"; [solve_ndisj|].
(* close inv *)
iModIntro. iSplitL "Nodes st.h↦ γs G_h1"; first by iExists (Some _); exfr.
wp_pures.
wp_apply (guard_read node with "[$h1Info $G]") as (??) "(G & #Info_h1' & %EQ)"; [solve_ndisj|lia|].
iDestruct "Info_h1'" as (x2 n2) "[-> Info_h1']".
iCombine "Info_h1 Info_h1'" gives %[= <- <-]%to_agree_op_inv_L.
iClear "Info_h1'". injection EQ as [= <-].
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 (γ_h2 n2) "(G_h2 & #Info_h2 & Nodes')".
wp_cmpxchg_suc.
iDestruct (rcu.(guard_managed_agree) with "h1Info G G_h2") as %<-.
iCombine "Info_h1 Info_h2" gives %[= <- <-]%to_agree_op_inv_L.
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 (guard_read node with "[$h1Info $G]") as (??) "(G & #Info_h1' & %EQ)"; [solve_ndisj|lia|].
iDestruct "Info_h1'" as (x2 n2) "[-> Info_h1']".
iCombine "Info_h1 Info_h1'" gives %[= <- <-]%to_agree_op_inv_L.
iClear "Info_h1'". injection EQ as [= <-].
wp_pures. wp_load. wp_pures.
wp_apply (rcu.(rcu_domain_retire_spec) with "IED G_h2") as "_"; [solve_ndisj|].
wp_pures.
wp_apply (rcu.(guard_deactivate_spec) with "IED G") as "G"; [solve_ndisj|]; wp_pures.
iApply "HΦ". by iFrame.
- (* 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 G").
Qed.
#[export] Typeclasses Opaque TStack IsTStack.
End treiber_stack.