-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.v
242 lines (215 loc) · 8.44 KB
/
client.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
237
238
239
240
241
242
From iris.base_logic.lib Require Import invariants.
From smr.program_logic Require Import atomic.
From smr.lang Require Import adequacy notation proofmode.
From smr.hazptr Require Import closed_proofs.
From iris.prelude Require Import options.
Section code.
Definition hazptr_code := hazard_pointer_code_impl.
Definition counter_code := counter_code_impl.
Definition hcounter_code := hcounter_code_impl.
Definition treiber_code := treiber_code_impl.
Definition elimstack_code := elimstack_code_impl.
Definition ms_code := ms_code_impl.
Definition dglm_code := dglm_code_impl.
Definition rdcss_code := rdcss_code_impl.
Definition cldeque_code := cldeque_code_impl.
Definition client1 : val := λ: <>,
let: "dom" := hazptr_code.(spec_hazptr.hazard_domain_new) #() in
let: "c" := counter_code.(spec_counter.counter_new) "dom" in
let: "h" := hcounter_code.(spec_hybrid_counter.hcounter_new) "dom" in
counter_code.(spec_counter.counter_inc) "c";;
hcounter_code.(spec_hybrid_counter.hcounter_inc) "h";;
let: "s" := treiber_code.(spec_stack.stack_new) "dom" in
let: "q" := ms_code.(spec_queue.queue_new) "dom" in
treiber_code.(spec_stack.stack_push) "s" #1%Z;;
ms_code.(spec_queue.queue_push) "q" #2%Z;;
let: "oa" := treiber_code.(spec_stack.stack_pop) "s" in
let: "ob" := ms_code.(spec_queue.queue_pop) "q" in
hazptr_code.(spec_hazptr.hazard_domain_do_reclamation) "dom";;
let: "a" :=
match: "oa" with
NONE => #()
| SOME "a" => "a"
end
in
let: "b" :=
match: "ob" with
NONE => #()
| SOME "b" => "b"
end
in
"a" + "b".
Definition reclaim_forever : val :=
rec: "loop" "dom" :=
hazptr_code.(spec_hazptr.hazard_domain_do_reclamation) "dom";;
"loop" "dom".
Definition client2 : val := λ: <>,
let: "dom" := hazptr_code.(spec_hazptr.hazard_domain_new) #() in
Fork (reclaim_forever "dom");;
let: "s" := treiber_code.(spec_stack.stack_new) "dom" in
Fork(
treiber_code.(spec_stack.stack_push) "s" #10%Z;;
treiber_code.(spec_stack.stack_pop) "s"
);;
treiber_code.(spec_stack.stack_push) "s" #10%Z;;
treiber_code.(spec_stack.stack_pop) "s";;
#().
Definition client3 : val := λ: <>,
let: "dom" := hazptr_code.(spec_hazptr.hazard_domain_new) #() in
let: "s1" := elimstack_code.(spec_stack.stack_new) "dom" in
let: "s2" := elimstack_code.(spec_stack.stack_new) "dom" in
elimstack_code.(spec_stack.stack_push) "s1" #10%Z;;
let: "oa" := elimstack_code.(spec_stack.stack_pop) "s1" in
let: "a" :=
match: "oa" with
NONE => #()
| SOME "a" => "a"
end
in
elimstack_code.(spec_stack.stack_push) "s2" "a";;
let: "ob" := elimstack_code.(spec_stack.stack_pop) "s2" in
match: "ob" with
NONE => #()
| SOME "b" => "b"
end
.
End code.
Section proof.
Context `{!heapGS Σ, !hazptrG Σ, !counterG Σ, !hcounterG Σ, !treiberG Σ, !elimstackG Σ, msG Σ}.
Definition hazptr := hazard_pointer_impl Σ.
Definition counter := counter_impl Σ.
Definition hcounter := hcounter_impl Σ.
Definition treiber := treiber_impl Σ.
Definition elimstack := elimstack_impl Σ.
Definition ms := ms_impl Σ.
Definition clientN := nroot .@ "client".
Lemma client1_spec :
{{{ True }}}
client1 #()
{{{ RET #3; True }}}.
Proof using All.
iIntros (Φ) "_ HΦ".
wp_lam. wp_apply (hazptr.(spec_hazptr.hazard_domain_new_spec) with "[//]") as (??) "#D".
wp_let.
(* counters *)
wp_apply (counter.(spec_counter.counter_new_spec) with "[//]").
(* FIXME: iIntros #IC takes 3 seconds, whereas iIntros #IH is instant *)
iIntros (??) "[#IC C]". wp_let.
wp_apply (hcounter.(spec_hybrid_counter.hcounter_new_spec) with "[//]") as (??) "[#IH H]".
wp_let.
awp_apply (counter.(spec_counter.counter_inc_spec) with "IC") without "HΦ".
iAaccIntro with "C"; first by eauto with iFrame.
iIntros "C !> HΦ". wp_seq.
awp_apply (hcounter.(spec_hybrid_counter.hcounter_inc_spec) with "IH") without "HΦ".
iAaccIntro with "H"; first by eauto with iFrame.
iIntros "H !> HΦ". wp_seq.
(* stack, queue *)
wp_apply (treiber.(spec_stack.stack_new_spec) with "[//]") as (??) "[#IS S]".
wp_let.
wp_apply (ms.(spec_queue.queue_new_spec) with "[//]") as (??) "[#IQ Q]".
wp_let.
(* NOTE: we need to explictly give the values for push
because Coq will otherwise not be able to do unification.
Suspected reason is simplification due to [of_val] *)
awp_apply (treiber.(spec_stack.stack_push_spec) $! _ _ #1 with "IS") without "HΦ".
iAaccIntro with "S"; first by eauto with iFrame.
iIntros "S !> HΦ". wp_seq.
awp_apply (ms.(spec_queue.queue_push_spec) $! _ _ #2 with "IQ") without "HΦ".
iAaccIntro with "Q"; first by eauto with iFrame.
iIntros "Q !> HΦ". wp_seq.
awp_apply (treiber.(spec_stack.stack_pop_spec) with "IS") without "HΦ".
iAaccIntro with "S"; first by eauto with iFrame.
iIntros "S !> HΦ". wp_pures.
awp_apply (ms.(spec_queue.queue_pop_spec) with "IQ") without "HΦ".
iAaccIntro with "Q"; first by eauto with iFrame.
iIntros "Q !> HΦ". wp_pures.
wp_apply (hazptr.(spec_hazptr.hazard_domain_do_reclamation_spec) with "D"); auto.
iIntros. wp_pures. by iApply "HΦ".
Qed.
Lemma reclaim_forever_spec γd d :
(hazard_pointer_impl Σ).(spec_hazptr.IsHazardDomain) γd d -∗
{{{ True }}} reclaim_forever #d {{{ RET #(); True }}}.
Proof.
iIntros "#D".
iIntros (Φ) "!> _ HΦ". iLöb as "IH".
wp_lam. wp_apply (hazptr.(spec_hazptr.hazard_domain_do_reclamation_spec)); auto.
iIntros. wp_seq. by iApply "IH".
Qed.
Lemma client2_spec :
{{{ True }}}
client2 #()
{{{ RET #(); True }}}.
Proof using All.
iIntros (Φ) "_ HΦ".
wp_lam. wp_apply (hazptr.(spec_hazptr.hazard_domain_new_spec) with "[//]") as (??) "#D".
wp_let.
wp_apply (wp_fork). { wp_apply reclaim_forever_spec; auto. }
iIntros. wp_seq.
wp_apply (treiber.(spec_stack.stack_new_spec) with "[//]") as (??) "[#IS S]".
wp_let.
iMod (inv_alloc clientN _ (∃ xs, TStack γ xs) with "[S]") as "#SInv". { eauto. }
wp_apply wp_fork.
{ awp_apply (treiber.(spec_stack.stack_push_spec) $! _ _ #10 with "IS").
iInv "SInv" as (xs) ">S". iAaccIntro with "S".
{ iIntros "S !>". iFrame. }
iIntros "S !>". iSplitL "S"; eauto.
wp_seq.
awp_apply (treiber.(spec_stack.stack_pop_spec) with "IS").
iInv "SInv" as (xs') ">S". iAaccIntro with "S".
{ iIntros "S". eauto with iFrame. }
iIntros "S !>". iSplitL "S"; eauto with iFrame.
}
iIntros. wp_seq.
awp_apply (treiber.(spec_stack.stack_push_spec) $! _ _ #10 with "IS").
iInv "SInv" as (xs) ">S". iAaccIntro with "S".
{ iIntros "S !>". iFrame. }
iIntros "S !>". iSplitL "S"; eauto.
wp_seq.
awp_apply (treiber.(spec_stack.stack_pop_spec) with "IS") without "HΦ".
iInv "SInv" as (xs') ">S". iAaccIntro with "S".
{ iIntros "S". eauto with iFrame. }
iIntros "S !>". iSplitL "S"; eauto with iFrame.
iIntros "HΦ". wp_pures. by iApply "HΦ".
Qed.
Lemma client3_spec :
{{{ True }}}
client3 #()
{{{ RET #10; True }}}.
Proof using All.
iIntros (Φ) "_ HΦ".
wp_lam. wp_apply (hazptr.(spec_hazptr.hazard_domain_new_spec) with "[//]") as (??) "#D".
wp_let.
wp_apply (elimstack.(spec_stack.stack_new_spec) with "[//]") as (γ1 ?) "[#IS1 S1]".
wp_let.
wp_apply (elimstack.(spec_stack.stack_new_spec) with "[//]") as (γ2 ?) "[#IS2 S2]".
wp_let.
awp_apply (elimstack.(spec_stack.stack_push_spec) $! _ _ #10 with "IS1") without "HΦ".
iAaccIntro with "S1"; first by eauto with iFrame.
iIntros "S1 !> HΦ". wp_seq.
awp_apply (elimstack.(spec_stack.stack_pop_spec) with "IS1") without "HΦ".
iAaccIntro with "S1"; first by eauto with iFrame.
iIntros "S1 !> HΦ". wp_pures.
awp_apply (elimstack.(spec_stack.stack_push_spec) $! _ _ #10 with "IS2") without "HΦ".
iAaccIntro with "S2"; first by eauto with iFrame.
iIntros "S2 !> HΦ". wp_seq.
awp_apply (elimstack.(spec_stack.stack_pop_spec) with "IS2") without "HΦ".
iAaccIntro with "S2"; first by eauto with iFrame.
iIntros "S2 !> HΦ". wp_pures. by iApply "HΦ".
Qed.
End proof.
Definition clientΣ : gFunctors := #[heapΣ; hazptrΣ; counterΣ; hcounterΣ; treiberΣ; elimstackΣ; msΣ].
Lemma client1_adequate σ : adequate NotStuck (client1 #()) σ (λ v _, v = #3).
Proof.
apply (heap_adequacy clientΣ)=> ?.
iIntros "_". iApply client1_spec; done.
Qed.
Lemma client2_adequate σ : adequate NotStuck (client2 #()) σ (λ v _, v = #()).
Proof.
apply (heap_adequacy clientΣ)=> ?.
iIntros "_". iApply client2_spec; done.
Qed.
Lemma client3_adequate σ : adequate NotStuck (client3 #()) σ (λ v _, v = #10).
Proof.
apply (heap_adequacy clientΣ)=> ?.
iIntros "_". iApply client3_spec; done.
Qed.