-
Notifications
You must be signed in to change notification settings - Fork 1
/
arc.v
2765 lines (2593 loc) · 143 KB
/
arc.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From iris.bi Require Import fractional.
From iris.proofmode Require Import tactics.
From iris.algebra Require Import excl csum frac auth agree.
From iris.base_logic.lib Require Import invariants.
From gpfsl.gps Require Import middleware protocols.
From gpfsl.logic Require Import view_invariants.
From lrust.lang Require Import notation new_delete arc_cmra.
From iris.prelude Require Import options.
(* Rust Arc has a maximum number of clones to prevent overflow. We currently
don't support this. *)
Definition strong_count : val :=
λ: ["l"], !ᵃᶜ"l".
Definition weak_count : val :=
λ: ["l"], !ᵃᶜ("l" +ₗ #1).
Definition clone_arc : val :=
rec: "clone" ["l"] :=
let: "strong" := !ʳˡˣ"l" in
(* fully relaxed CAS *)
if: CAS "l" "strong" ("strong" + #1) Relaxed Relaxed Relaxed then #☠ else "clone" ["l"].
Definition clone_weak : val :=
rec: "clone" ["l"] :=
let: "weak" := !ʳˡˣ("l" +ₗ #1) in
if: CAS ("l" +ₗ #1) "weak" ("weak" + #1) Relaxed Relaxed Relaxed then #☠ else "clone" ["l"].
(* // the value usize::MAX acts as a sentinel for temporarily "locking" the
// ability to upgrade weak pointers or downgrade strong ones; this is used
// to avoid races in `make_mut` and `get_mut`.
Her we use -1 for usize::MAX.
*)
Definition downgrade : val :=
rec: "downgrade" ["l"] :=
let: "weak" := !ʳˡˣ("l" +ₗ #1) in
if: "weak" = #(-1) then
(* -1 in weak indicates that somebody locked the counts; let's spin. *)
"downgrade" ["l"]
else
(*
// Unlike with Clone(), we need this to be an Acquire read to
// synchronize with the write coming from `is_unique`, so that the
// events prior to that write happen before this read.
*)
(* acquire CAS, i.e. relaxed fail read, acquire success read, relaxed write *)
if: CAS ("l" +ₗ #1) "weak" ("weak" + #1) Relaxed AcqRel Relaxed then #☠
else "downgrade" ["l"].
Definition upgrade : val :=
rec: "upgrade" ["l"] :=
let: "strong" := !ʳˡˣ"l" in
if: "strong" = #0 then #false
else
if: CAS "l" "strong" ("strong" + #1) Relaxed Relaxed Relaxed then #true
else "upgrade" ["l"].
Definition drop_weak : val :=
rec: "drop" ["l"] :=
(* This can ignore the lock because the lock is only held when there
are no other weak refs in existence, so this code will never be called
with the lock held. *)
let: "weak" := !ʳˡˣ("l" +ₗ #1) in
(* release CAS, i.e. relaxed fail read, relaxed success read, release write *)
if: CAS ("l" +ₗ #1) "weak" ("weak" - #1) Relaxed Relaxed AcqRel
then if: "weak" = #1 then FenceAcq ;; #true else #false
else "drop" ["l"].
Definition drop_arc : val :=
rec: "drop" ["l"] :=
let: "strong" := !ʳˡˣ"l" in
if: CAS "l" "strong" ("strong" - #1) Relaxed Relaxed AcqRel
then if: "strong" = #1 then FenceAcq ;; #true else #false
else "drop" ["l"].
Definition try_unwrap : val :=
λ: ["l"],
(* release CAS *)
if: CAS "l" #1 #0 Relaxed Relaxed AcqRel
then FenceAcq ;; #true
else #false.
(* A bug was discovered where the read of strong was relaxed.
It can be fixed by using an acquire read.
See https://github.com/rust-lang/rust/issues/51780 *)
Definition is_unique : val :=
λ: ["l"],
(* Try to acquire the lock for the last ref by CASing weak count from 1 to -1. *)
(* acquire CAS *)
if: CAS ("l" +ₗ #1) #1 #(-1) Relaxed AcqRel Relaxed then
let: "strong" := !ᵃᶜ"l" in
let: "unique" := "strong" = #1 in
"l" +ₗ #1 <-ʳᵉˡ #1;;
"unique"
else
#false.
(* to be used in make_mut *)
(*// Note that we hold both a strong reference and a weak reference.
// Thus, releasing our strong reference only will not, by itself, cause
// the memory to be deallocated.
//
// Use Acquire to ensure that we see any writes to `weak` that happen
// before release writes (i.e., decrements) to `strong`. Since we hold a
// weak count, there's no chance the ArcInner itself could be
// deallocated. *)
(* - Returns #2 to signal that there is another strong pointer,
and trigger a deep copy and make a new Arc pointing to the new copy.
- Returns #1 to signal that we have the last strong pointer and
the main content, but there are more than 1 weak pointers left.
So we will move the main content to a new Arc, and leave the job
of dropping to old Arc to its remaining weak pointers.
- Returns #0 to signal that we have collected both the last strong and weak pointer,
so it's time to clean up everything. We reset "strong" back to 1 to unlock.
The write "l" <- #1 seems to be good enough, but C/Rust
uses a release write, may be because the strict distinction between atomics
and non-atomics. (TODO: is a relaxed write enough?) *)
(* we use acquire read instead of relaxed read on weak: we do not know how to
prove or disprove the relaxed read yet *)
Definition try_unwrap_full : val :=
λ: ["l"],
(* acquire CAS *)
if: CAS "l" #1 #0 Relaxed AcqRel Relaxed then
if: !ᵃᶜ("l" +ₗ #1) = #1 then "l" <- #1;; #0
else #1
else #2.
Section ArcInv.
(* P1 is the thing that is shared by strong reference owners (in practice,
this is the lifetime token), and P2 is the thing that is owned by the
protocol when only weak references are left (in practice, P2 is the
ownership of the underlying memory incl. deallocation). *)
Context `{!noprolG Σ, !arcG Σ, !view_invG Σ}
(P1 : Qp → vProp Σ) (P2 : vProp Σ) (N : namespace).
Local Notation vProp := (vProp Σ).
Definition SeenActs γ l (Mt: gset time) : vProp :=
(∃ t v, GPS_Reader l t () v γ ∗ ⌜∀ t', t' ∈ Mt → t' ⊑ t⌝)%I.
Definition WkResources lw ls t v γi γw γs γsw q Ut : vProp :=
(* writer permission and remaining reader permission *)
(view_tok γi (q/2) ∗ GPS_SWSharedReader lw t () v q γw ∗
(* I have seen all upgrades by strong that I'm involved in --- γs *)
WkUps γsw q Ut ∗ SeenActs γs ls Ut)%I.
Definition weak_interp ls γi γs γsw : interpO Σ unitProtocol :=
(λ pb lw γw t _ v, ∃ st, ⌜v = #(Z_of_arcweak_st st)⌝ ∗
if pb
(* Full interp *)
then (* the weak state st, the move authority Mt, the upgrade authority Ut *)
WeakAuth γsw st ∗
match st return _ with
| (None, None) => (* nothing *) True
| (Some qs, None) => ⌜qs = 1%Qp⌝ ∗ (* some strongs, no weak *)
(* weak resources are returned *)
GPS_SWSharedWriter lw t () v γw ∗
(∃ Ut, WkResources lw ls t v γi γw γs γsw 1%Qp Ut)
| (None, Some (Cinl (q, n))) => (* no strong, n weaks *)
(* remaining weak resources *)
GPS_SWSharedWriter lw t () v γw ∗
(∃ q' Ut, ⌜(q + q')%Qp = 1%Qp⌝ ∗
WkResources lw ls t v γi γw γs γsw q' Ut) ∗
P2 ∗
(* strong resources *)
view_tok γi (1/2)%Qp ∗
(∃ ts, GPS_SWWriter ls ts () #0 γs) ∗
(∃ Mts, StrMoves γsw 1%Qp Mts) ∗
(∃ Dts, StrDowns γsw 1%Qp Dts)
| (Some qs, Some (Cinl (q, n))) => ⌜qs = 1%Qp⌝ ∗
(* some strongs, n weaks *)
GPS_SWSharedWriter lw t () v γw ∗
(∃ q' Ut, ⌜(q + q')%Qp = 1%Qp⌝ ∗
WkResources lw ls t v γi γw γs γsw q' Ut)
| (Some qs, Some (Cinr (_, agq))) => ⌜qs = 1%Qp⌝ ∗
(* some strong arcs, no weak arcs, locked,
agown γsw (awk_n ((Some q, ε))) is the payload *)
∃ q, ⌜agq ≡ to_agree q⌝ ∗ StrWkTok γsw q
| _ => False (* anything else is impossible *)
end
(* Read interp *)
else ⌜0 < Z_of_arcweak_st st⌝ ∗ (* CASable values > 0 *)
match st with
| (Some _, None) | (None, Some (Cinl (_, 1%positive))) =>
(* move from 1 *)
∃ t' : time, ⌜(t < t')%positive⌝ ∗ StrDownCert γsw t'
| _ => True
end
)%I.
Definition weak_noCAS γsw : interpCasO Σ unitProtocol :=
(λ _ _ t _ v, ⌜v = #(-1)⌝ ∗ ∃ t' : time, ⌜(t < t')%positive⌝ ∗ StrDownCert γsw t')%I.
Definition StrResources lw ls t v γi γs γw γsw q Mt Dt :=
(* writer permission and remaining reader permission *)
(view_tok γi (q/2) ∗ GPS_SWSharedReader ls t () v q γs ∗
(* I have seen all my moves --- γ *)
StrMoves γsw q Mt ∗ SeenActs γs ls Mt ∗
(* I have seen all downgrads by weak that I'm involved in --- γs *)
StrDowns γsw q Dt ∗ SeenActs γw lw Dt ∗
(* weak resources I need to keep to use weak *)
StrWkTok γsw q)%I.
Definition strong_interp lw γi γw γsw: interpO Σ unitProtocol :=
(λ pb ls γs t _ v, ∃ st, ⌜v = #(Z_of_arcstrong_st st)⌝ ∗
if pb
(* Full interp *)
then (* the strong state st *)
StrongAuth γsw st ∗
match st return _ with
| None => (* nothing *) True
| Some (q, n) => (* n strongs *)
GPS_SWSharedWriter ls t () v γs ∗ ∃ q', ⌜(q + q')%Qp = 1%Qp⌝ ∗
P1 q' ∗
(∃ Mt Dt, StrResources lw ls t v γi γs γw γsw q' Mt Dt)
end
(* Read interp *)
else match st with
| None => False
| Some (_, 1%positive) =>
(* move from 1 *)
∃ t' : time, ⌜(t < t')%positive⌝ ∗ (StrMoveCert γsw t' ∨ WkUpCert γsw t')
| _ => True
end
)%I.
Definition strong_noCAS : interpCasO Σ unitProtocol :=
(λ _ _ _ _ v, ⌜v = #0⌝)%I.
Definition strong_arc t q l γi γs γw γsw : vProp :=
(∃ v Mt Dt, StrongTok γsw q ∗ StrResources (l >> 1) l t v γi γs γw γsw q Mt Dt)%I.
Definition weak_arc t q l γi γs γw γsw : vProp :=
(∃ v Ut, WeakTok γsw q ∗ WkResources (l >> 1) l t v γi γw γs γsw q Ut)%I.
Definition fake_arc l γi γs γw γsw :=
(view_tok γi (1/2)%Qp ∗ (∃ ts, GPS_SWWriter l ts () #0 γs) ∗
(∃ Mts, StrMoves γsw 1%Qp Mts ∗ SeenActs γs l Mts) ∗
(∃ Dts, StrDowns γsw 1%Qp Dts ∗ SeenActs γw (l >> 1) Dts) ∗
StrWkTok γsw 1%Qp)%I.
Global Instance strong_arc_timeless t q l γi γs γw γsw : Timeless (strong_arc t q l γi γs γw γsw) := _.
Global Instance weak_arc_timeless t q l γi γs γw γsw : Timeless (weak_arc t q l γi γs γw γsw) := _.
Global Instance fake_arc_timeless l γi γs γw γsw : Timeless (fake_arc l γi γs γw γsw) := _.
Definition strong_arc_acc l t γi γs γw γsw P E : vProp :=
(□ (P ={E,↑histN}=∗ ∃ Vb q,
(⊒Vb → ▷ strong_arc t q l γi γs γw γsw) ∗
((⊒Vb → ▷ strong_arc t q l γi γs γw γsw) ={↑histN,E}=∗ P)))%I.
Definition weak_arc_acc l t γi γs γw γsw P E : vProp :=
(□ (P ={E,↑histN}=∗ ∃ Vb q,
(⊒Vb → ▷ weak_arc t q l γi γs γw γsw) ∗
((⊒Vb → ▷ weak_arc t q l γi γs γw γsw) ={↑histN,E}=∗ P)))%I.
Definition arc_inv γi γs γw γsw (l : loc) : vProp :=
(((∃ Mt, StrongMoveAuth γsw Mt) ∗
(∃ Dt, StrongDownAuth γsw Dt) ∗ (∃ Ut, WeakUpAuth γsw Ut)) ∗
GPS_INV (strong_interp (l >> 1) γi γw γsw) l strong_noCAS γs ∗
GPS_INV (weak_interp l γi γs γsw) (l >> 1) (weak_noCAS γsw) γw)%I.
Definition is_arc γi γs γw γsw l : vProp :=
view_inv γi N (arc_inv γi γs γw γsw l).
Global Instance is_arc_persistent γi γs γw γsw l : Persistent (is_arc γi γs γw γsw l) := _.
End ArcInv.
Global Typeclasses Opaque StrongTok WeakTok StrMoves StrDowns WkUps.
Section arc.
(* P1 is the thing that is shared by strong reference owners (in practice,
this is the lifetime token), and P2 is the thing that is owned by the
protocol when only weak references are left (in practice, P2 is the
ownership of the underlying memory incl. deallocation). *)
Context `{!noprolG Σ, !arcG Σ, !view_invG Σ}
(P1 : Qp → vProp Σ)
(P2 : vProp Σ) (N : namespace).
Local Notation vProp := (vProp Σ).
Local Notation HP1 := (∀ q1 q2, P1 (q1 + q2)%Qp -∗ P1 q1 ∗ <obj> P1 q2).
Local Notation HP2 := (∀ q1 q2, P1 q1 ∗ P1 q2 -∗ P1 (q1 + q2)%Qp).
Global Instance arc_inv_ne n :
Proper (pointwise_relation _ (dist n) ==> dist n ==>
eq ==> eq ==> eq ==> eq ==> eq ==> dist n) arc_inv.
Proof.
repeat intro. subst.
apply bi.sep_ne; [done|]. apply bi.sep_ne; apply GPS_INV_ne; [|done| |done].
- move => b ?????. apply bi.exist_ne => st.
apply bi.sep_ne; [done|]. destruct b; [|done]. apply bi.sep_ne; [done|].
destruct st as [[]|]; [|done]. solve_proper.
- move => b ?????. apply bi.exist_ne => st.
apply bi.sep_ne; [done|]. destruct b; [|done]. apply bi.sep_ne; [done|].
destruct st as [[|] [[[]| |]|]]; simpl; try done. solve_proper.
Qed.
Global Instance arc_inv_proper :
Proper (pointwise_relation _ (≡) ==> (≡) ==>
eq ==> eq ==> eq ==> eq ==> eq ==> (≡))
arc_inv.
Proof.
repeat intro. subst.
apply bi.sep_proper; [done|].
apply bi.sep_proper; apply GPS_INV_proper; auto;
move => b ?????; apply bi.exist_proper => ?; solve_proper.
Qed.
Global Instance is_arc_contractive n :
Proper (pointwise_relation _ (dist_later n) ==> dist_later n ==>
eq ==> eq ==> eq ==> eq ==> eq ==> eq ==> dist n)
is_arc.
Proof.
repeat intro. subst. constructor => ?.
rewrite /is_arc. apply view_inv_contractive.
dist_later_intro; by apply arc_inv_ne.
Qed.
Global Instance is_arc_proper :
Proper (pointwise_relation _ (≡) ==> (≡) ==> eq
==> eq ==> eq ==> eq ==> eq ==> eq ==> (≡)) is_arc.
Proof.
repeat intro. subst.
by apply view_inv_proper, arc_inv_proper.
Qed.
Local Instance strong_interp_read_persistent γi γw γsw l γs t s v :
Persistent (strong_interp P1 (l >> 1) γi γw γsw false l γs t s v).
Proof. apply bi.exist_persistent. intros [[? []]|]; simpl; by apply _. Qed.
Local Instance weak_interp_read_persistent i γs γsw l γw t s v :
Persistent (weak_interp P2 l i γs γsw false (l >> 1) γw t s v).
Proof.
intros. apply bi.exist_persistent.
intros [[?|] [[[?[]]|?|]|]]; simpl; apply _.
Qed.
Lemma SeenActs_join γ l Mt Mt' :
⊢ (SeenActs γ l Mt ∗ SeenActs γ l Mt') ∗-∗ SeenActs γ l (Mt ∪ Mt').
Proof.
iSplit.
- iIntros "[S1 S2]". iDestruct "S1" as (t1 v1) "[R1 MAX1]".
iDestruct "S2" as (t2 v2) "[R2 MAX2]".
iDestruct "MAX1" as %MAX1. iDestruct "MAX2" as %MAX2.
case (decide (t1 ≤ t2)%positive) => [Le|/Pos.lt_nle /Pos.lt_le_incl Le].
+ iExists t2, v2. iFrame "R2". iPureIntro.
move => ? /elem_of_union [/MAX1 -> //|/MAX2//].
+ iExists t1, v1. iFrame "R1". iPureIntro.
move => ? /elem_of_union [/MAX1//|/MAX2 ->//].
- iDestruct 1 as (t v) "[#R MAX]". iDestruct "MAX" as %MAX.
iSplitL""; iExists t, v; iFrame "R"; iPureIntro; move => ? In;
apply MAX, elem_of_union; [by left|by right].
Qed.
Lemma arc_inv_split Vb γi γs γw γsw l E:
(⊒Vb → ▷ arc_inv P1 P2 γi γs γw γsw l)
={E}=∗ ((∃ Mt, StrongMoveAuth γsw Mt) ∗
(∃ Dt, StrongDownAuth γsw Dt) ∗
(∃ Ut, WeakUpAuth γsw Ut)) ∗
(⊒Vb →
▷ GPS_INV (strong_interp P1 (l >> 1) γi γw γsw) l strong_noCAS γs) ∗
(⊒Vb →
▷ GPS_INV (weak_interp P2 l γi γs γsw) (l >> 1) (weak_noCAS γsw) γw).
Proof.
rewrite -!view_join_unfold !view_join_later.
iDestruct 1 as "((SMA & SDA & WUA) & $ & $)".
iDestruct "SMA" as (Mt) ">SMA". iDestruct "SDA" as (Dt) ">SDA".
iDestruct "WUA" as (Ut) ">WUA".
iIntros "!>".
iSplitL "SMA"; last iSplitL "SDA"; iExists _;
rewrite view_join_embed; iFrame.
Qed.
Lemma arc_inv_join Vb γi γs γw γsw l :
(∃ Mt, StrongMoveAuth γsw Mt) -∗ (∃ Dt, StrongDownAuth γsw Dt) -∗
(∃ Ut, WeakUpAuth γsw Ut) -∗
(⊒Vb →
▷ GPS_INV (strong_interp P1 (l >> 1) γi γw γsw) l strong_noCAS γs) -∗
(⊒Vb →
▷ GPS_INV (weak_interp P2 l γi γs γsw) (l >> 1) (weak_noCAS γsw) γw) -∗
(⊒Vb → ▷ arc_inv P1 P2 γi γs γw γsw l).
Proof.
iIntros "SMA SDA WUA IS IW #In". iFrame "SMA SDA WUA".
iDestruct ("IS" with "In") as "$". by iDestruct ("IW" with "In") as "$".
Qed.
Lemma strong_arc_acc_open l t γi γs γw γsw P E :
P -∗ strong_arc_acc l t γi γs γw γsw P E ={E,↑histN}=∗
∃ Vb q Mt Dt, (StrongTok γsw q ∗
(⊒Vb → view_tok γi (q / 2)) ∗
(∃ v, ⊒Vb → GPS_SWSharedReader l t ((): unitProtocol) v q γs) ∗
StrMoves γsw q Mt ∗ StrDowns γsw q Dt ∗ StrWkTok γsw q) ∗
(∀ Mt' Dt',
StrongTok γsw q -∗
(⊒Vb → view_tok γi (q / 2)) -∗
(∃ v, ⊒Vb → GPS_SWSharedReader l t ((): unitProtocol) v q γs) -∗
StrMoves γsw q (Mt ∪ Mt') -∗
(if decide (Mt' = ∅) then True else SeenActs γs l Mt') -∗
StrDowns γsw q (Dt ∪ Dt') -∗
(if decide (Dt' = ∅) then True else SeenActs γw (l >> 1) Dt') -∗
StrWkTok γsw q ={↑histN,E}=∗ P).
Proof.
iIntros "HP Hacc". iMod ("Hacc" with "HP") as (Vb q) "[Hown Hclose2]".
iExists Vb, q. iMod (monPred_in_later_timeless with "Hown") as "Hown".
rewrite monPred_in_exist'. iDestruct "Hown" as (v) "Hown".
rewrite monPred_in_exist'. iDestruct "Hown" as (Mt) "Hown".
rewrite monPred_in_exist'. iDestruct "Hown" as (Dt) "Hown".
rewrite 7!monPred_in_sep'.
iDestruct "Hown" as "(St & tok & SR & SM & SeenM & SD & SeenD & oW)".
iExists Mt, Dt. iFrame "tok". iSplitR "Hclose2 SeenM SeenD".
- iSplitL "St". { by rewrite monPred_in_embed' /StrongTok. }
iSplitL "SR". { by iExists _. } iSplitL "SM".
{ by rewrite monPred_in_embed' /StrMoves. } iSplitL "SD".
{ by rewrite monPred_in_embed' /StrDowns. }
by rewrite monPred_in_embed'.
- iIntros "!>" (Mt' Dt') "St tok SR SM SeenM' SD SeenD' oW".
iMod ("Hclose2" with "[-]") as "$"; [|done]. iIntros "#mb !>".
iSpecialize ("tok" with "mb").
iDestruct "SR" as (v') "SR". iSpecialize ("SR" with "mb").
iExists _,_,_. iFrame "St tok SR SM SD oW".
iSpecialize ("SeenM" with "mb"). iSpecialize ("SeenD" with "mb").
iSplitL "SeenM SeenM'"; (case decide => [->|?];
[by rewrite right_id_L| iApply SeenActs_join; by iFrame]).
Qed.
Lemma strong_arc_acc_open_no_change l t γi γs γw γsw P E :
P -∗ strong_arc_acc l t γi γs γw γsw P E ={E,↑histN}=∗
∃ Vb q Mt Dt, (StrongTok γsw q ∗
(⊒Vb → view_tok γi (q / 2)) ∗
(∃ v, ⊒Vb → GPS_SWSharedReader l t ((): unitProtocol) v q γs) ∗
StrMoves γsw q Mt ∗ StrDowns γsw q Dt ∗ StrWkTok γsw q) ∗
(StrongTok γsw q -∗
(⊒Vb → view_tok γi (q / 2)) -∗
(∃ v, ⊒Vb → GPS_SWSharedReader l t ((): unitProtocol) v q γs) -∗
StrMoves γsw q Mt -∗ StrDowns γsw q Dt -∗
StrWkTok γsw q ={↑histN,E}=∗ P).
Proof.
iIntros "HP Hacc". iMod (strong_arc_acc_open with "HP Hacc")
as (Vs q Mt Dt) "[(St & tok & SR & SM & SD & oW) Hclose2]".
iExists Vs, q, Mt, Dt. iFrame "St tok SR SM SD oW".
iIntros "!> St tok SR SM SD oW".
iApply ("Hclose2" $! ∅ ∅ with "St tok SR [SM] [//] [SD] [//] oW");
by rewrite right_id_L.
Qed.
Lemma strong_arc_acc_open_2 l t γi γs γw γsw P E :
P -∗ strong_arc_acc l t γi γs γw γsw P E ={E,↑histN}=∗
∃ Vb q Mt Dt, (StrongTok γsw q ∗
(⊒Vb → view_tok γi (q / 2)) ∗
(∃ v, ⊒Vb → GPS_SWSharedReader l t ((): unitProtocol) v q γs) ∗
StrMoves γsw q Mt ∗ StrDowns γsw q Dt ∗ StrWkTok γsw q) ∗
(∀ Mt' Dt',
StrongTok γsw q -∗
(⊒Vb → view_tok γi (q / 2)) -∗
(∃ v, GPS_SWSharedReader l t ((): unitProtocol) v q γs) -∗
StrMoves γsw q (Mt ∪ Mt') -∗
(if decide (Mt' = ∅) then True else SeenActs γs l Mt') -∗
StrDowns γsw q (Dt ∪ Dt') -∗
(if decide (Dt' = ∅) then True else SeenActs γw (l >> 1) Dt') -∗
StrWkTok γsw q ={↑histN,E}=∗ P).
Proof.
iIntros "HP Hacc". iMod (strong_arc_acc_open with "HP Hacc")
as (Vs q Mt Dt) "[(St & tok & SR & SM & SD & oW) Hclose2]".
iExists Vs, q, Mt, Dt. iFrame "St tok SR SM SD oW".
iIntros "!>" (Mt' Dt') "St tok SR SM SeenM SD SeenD oW".
iApply ("Hclose2" $! Mt' Dt' with "St tok [SR] SM SeenM SD SeenD oW").
iDestruct "SR" as (?) "SR". iExists _. by iIntros "_".
Qed.
Lemma strong_arc_acc_open_no_change_2 l t γi γs γw γsw P E :
P -∗ strong_arc_acc l t γi γs γw γsw P E ={E,↑histN}=∗
∃ Vb q Mt Dt, (StrongTok γsw q ∗
(⊒Vb → view_tok γi (q / 2)) ∗
(∃ v, ⊒Vb → GPS_SWSharedReader l t ((): unitProtocol) v q γs) ∗
StrMoves γsw q Mt ∗ StrDowns γsw q Dt ∗ StrWkTok γsw q) ∗
(StrongTok γsw q -∗
(⊒Vb → view_tok γi (q / 2)) -∗
(∃ v, GPS_SWSharedReader l t ((): unitProtocol) v q γs) -∗
StrMoves γsw q Mt -∗ StrDowns γsw q Dt -∗
StrWkTok γsw q ={↑histN,E}=∗ P).
Proof.
iIntros "HP Hacc". iMod (strong_arc_acc_open_no_change with "HP Hacc")
as (Vs q Mt Dt) "[(St & tok & SR & SM & SD & oW) Hclose2]".
iExists Vs, q, Mt, Dt. iFrame "St tok SR SM SD oW".
iIntros "!> St tok SR SM SD oW". iApply ("Hclose2" with "St tok [SR] SM SD oW").
iDestruct "SR" as (?) "SR". iExists _. by iIntros "_".
Qed.
Lemma weak_arc_acc_open l t γi γs γw γsw P E :
P -∗ weak_arc_acc l t γi γs γw γsw P E ={E,↑histN}=∗
∃ Vb q Ut, (WeakTok γsw q ∗
(⊒Vb → view_tok γi (q / 2)) ∗
(∃ v, ⊒Vb →
GPS_SWSharedReader (l >> 1) t ((): unitProtocol) v q γw) ∗
WkUps γsw q Ut) ∗
(∀ Ut', WeakTok γsw q -∗
(⊒Vb → view_tok γi (q / 2)) -∗
(∃ v, ⊒Vb →
GPS_SWSharedReader (l >> 1) t ((): unitProtocol) v q γw) -∗
WkUps γsw q (Ut ∪ Ut') -∗
(if decide (Ut' = ∅) then True else SeenActs γs l Ut') ={↑histN,E}=∗ P).
Proof.
iIntros "HP Hacc". iMod ("Hacc" with "HP") as (Vb q) "[Hown Hclose2]".
iExists Vb, q. iMod (monPred_in_later_timeless with "Hown") as "Hown".
rewrite monPred_in_exist'. iDestruct "Hown" as (v) "Hown".
rewrite monPred_in_exist'. iDestruct "Hown" as (Ut) "Hown". iExists Ut.
rewrite 4!monPred_in_sep'. iDestruct "Hown" as "(Wt & $ & WR & WU & SeenU)".
iSplitR "Hclose2 SeenU".
- iSplitL "Wt". { by rewrite monPred_in_embed' /WeakTok. }
iSplitL "WR". { by iExists _. } by rewrite monPred_in_embed' /WkUps.
- iIntros "!>" (Ut') "Wt tok WR WU SeenU'".
iMod ("Hclose2" with "[-]") as "$"; [|done]. iIntros "#mb !>".
iDestruct "WR" as (v') "WR". iSpecialize ("WR" with "mb").
iSpecialize ("tok" with "mb").
iExists _,_. iFrame "Wt tok WR WU".
iSpecialize ("SeenU" with "mb"). case decide => [->|?];
[by rewrite right_id_L| iApply SeenActs_join; by iFrame].
Qed.
Lemma weak_arc_acc_open_no_change l t γi γs γw γsw P E :
P -∗ weak_arc_acc l t γi γs γw γsw P E ={E,↑histN}=∗
∃ Vb q Ut, (WeakTok γsw q ∗
(⊒Vb → view_tok γi (q / 2)) ∗
(∃ v, ⊒Vb →
GPS_SWSharedReader (l >> 1) t ((): unitProtocol) v q γw) ∗
WkUps γsw q Ut) ∗
(WeakTok γsw q -∗
(⊒Vb → view_tok γi (q / 2)) -∗
(∃ v, ⊒Vb →
GPS_SWSharedReader (l >> 1) t ((): unitProtocol) v q γw) -∗
WkUps γsw q Ut ={↑histN,E}=∗ P).
Proof.
iIntros "HP Hacc". iMod (weak_arc_acc_open with "HP Hacc")
as (Vs q Ut) "[(Wt & tok & WR & WU) Hclose2]".
iExists Vs, q, Ut. iFrame "Wt tok WR WU".
iIntros "!> Wt tok WR WU".
iApply ("Hclose2" $! ∅ with "Wt tok WR [WU] [//]"); by rewrite right_id_L.
Qed.
Lemma weak_arc_acc_open_2 l t γi γs γw γsw P E :
P -∗ weak_arc_acc l t γi γs γw γsw P E ={E,↑histN}=∗
∃ Vb q Ut, (WeakTok γsw q ∗
(⊒Vb → view_tok γi (q / 2)) ∗
(∃ v, ⊒Vb →
GPS_SWSharedReader (l >> 1) t ((): unitProtocol) v q γw) ∗
WkUps γsw q Ut) ∗
(∀ Ut', WeakTok γsw q -∗
(⊒Vb → view_tok γi (q / 2)) -∗
(∃ v, GPS_SWSharedReader (l >> 1) t ((): unitProtocol) v q γw) -∗
WkUps γsw q (Ut ∪ Ut') -∗
(if decide (Ut' = ∅) then True else SeenActs γs l Ut') ={↑histN,E}=∗ P).
Proof.
iIntros "HP Hacc". iMod (weak_arc_acc_open with "HP Hacc")
as (Vs q Ut) "[(Wt & tok & WR & WU) Hclose2]".
iExists Vs, q, Ut. iFrame "Wt tok WR WU".
iIntros "!>" (Ut') "Wt tok WR WU SeenU".
iApply ("Hclose2" $! Ut' with "Wt tok [WR] WU SeenU").
iDestruct "WR" as (?) "WR". iExists _. by iIntros "_".
Qed.
Lemma weak_arc_acc_open_no_change_2 l t γi γs γw γsw P E :
P -∗ weak_arc_acc l t γi γs γw γsw P E ={E,↑histN}=∗
∃ Vb q Ut, (WeakTok γsw q ∗
(⊒Vb → view_tok γi (q / 2)) ∗
(∃ v, ⊒Vb →
GPS_SWSharedReader (l >> 1) t ((): unitProtocol) v q γw) ∗
WkUps γsw q Ut) ∗
(WeakTok γsw q -∗
(⊒Vb → view_tok γi (q / 2)) -∗
(∃ v, GPS_SWSharedReader (l >> 1) t ((): unitProtocol) v q γw) -∗
WkUps γsw q Ut ={↑histN,E}=∗ P).
Proof.
iIntros "HP Hacc". iMod (weak_arc_acc_open_no_change with "HP Hacc")
as (Vs q Ut) "[(Wt & tok & WR & WU) Hclose2]".
iExists Vs, q, Ut. iFrame "Wt tok WR WU".
iIntros "!> Wt tok WR WU". iApply ("Hclose2" with "Wt tok [WR] WU").
iDestruct "WR" as (?) "WR". iExists _. by iIntros "_".
Qed.
(* MAIN SPECS START HERE *)
Lemma create_strong {HPn: HP1} E l :
l ↦ #1 -∗ (l >> 1) ↦ #1 -∗ P1 1%Qp ={E}=∗
∃ γi γs γw γsw t q, is_arc P1 P2 N γi γs γw γsw l ∗ P1 q ∗
strong_arc t q l γi γs γw γsw .
Proof.
iIntros "Hl1 Hl2 HP1".
iDestruct (HPn (1/2)%Qp (1/2)%Qp with "[HP1]") as "[HP1 HP1']";
[by rewrite Qp.div_2|rewrite monPred_objectively_elim].
set stS : strong_stUR := Some ((1/2)%Qp, xH).
set stW : weak_stUR := (Some 1%Qp, None).
iAssert (|==> ∃ γ, StrongAuth γ stS ∗ StrongTok γ (1/2)%Qp ∗
WeakAuth γ stW ∗
StrongMoveAuth γ ∅ ∗ StrMoves γ 1%Qp ∅ ∗
StrongDownAuth γ ∅ ∗ StrDowns γ 1%Qp ∅ ∗
WeakUpAuth γ ∅ ∗ WkUps γ 1%Qp ∅ ∗
⎡ own γ ((ε, ◯ ((Some 1%Qp, ε)), ε) : arcUR) ⎤)%I as ">Own".
{ do 9 setoid_rewrite <- embed_sep. do 9 setoid_rewrite <- own_op.
iMod (own_alloc (A:=arcUR)) as (γ) "Own"; last by iExists γ.
split; split; simpl.
- rewrite !right_id. by apply auth_both_valid_discrete.
- rewrite !left_id. by apply auth_both_valid_discrete.
- split; split; simpl; rewrite !left_id !right_id.
+ by apply auth_both_valid_discrete.
+ by apply auth_auth_valid.
+ by apply auth_both_valid_discrete.
+ by apply auth_auth_valid.
- split; simpl; rewrite !left_id !right_id.
+ by apply auth_both_valid_discrete.
+ by apply auth_auth_valid. }
iDestruct "Own" as (γsw)
"(SA & St & WA & SMA & [SM1 SM2] & SDA & [SD1 SD2] & WUA & WU & [S1 S2])".
iMod (view_inv_alloc_frac N _ (1/2 + 1/2/2) (1/2/2)) as (γi) "[[tok2 tok1] VI]";
first by rewrite -Qp.add_assoc 2!Qp.div_2.
set Qs : time → time → gname → gname → vProp :=
(λ ts tw γs γw,
GPS_SWSharedReader l ts (() : unitProtocol) #1 (1/2)%Qp γs ∗
GPS_Reader l ts (() : unitProtocol) #1 γs ∗
GPS_Reader (l >> 1) tw (() : unitProtocol) #1 γw)%I.
iMod (GPS_SWRaw_Init_vs_strong_2 _ _
(λ γw, strong_interp P1 (l >> 1) γi γw γsw)
(λ γs, weak_interp P2 l γi γs γsw) true _ _ () () Qs
with "Hl1 Hl2 [-HP1' S2 SM2 SD2 SMA SDA WUA St VI]")
as (γs γw ts tw) "INV".
{ iIntros (ts tw γs γw) "Ws Ww". simpl.
iDestruct (GPS_SWWriter_shared with "Ws") as "[Ws [Rs1 Rs2]]".
iDestruct (GPS_SWWriter_shared with "Ww") as "[Ww [Rw1 Rw2]]".
iDestruct (GPS_SWSharedReader_Reader with "Rs1") as "#R1".
iDestruct (GPS_SWSharedReader_Reader with "Rw2") as "#R2".
iModIntro. iSplitL "SA SM1 SD1 Ws Rs1 HP1 S1 tok1"; last iSplitR "Rs2".
- iNext. iExists stS. iSplitL ""; [done|]. iFrame "SA Ws".
iExists (1/2)%Qp. rewrite Qp.div_2. iSplitL ""; [done|]. iFrame "HP1".
iExists ∅,∅. iFrame "tok1 Rs1 SM1 SD1 S1".
iSplitL ""; iExists _, _; [by iFrame "R1"|by iFrame "R2"].
- iNext. iExists stW. iSplitL ""; [done|]. iFrame "WA". iSplitL ""; [done|].
iFrame "Ww". iExists ∅. iFrame "tok2 Rw2 WU Rw1". iExists _,_. by iFrame "R1".
- by iFrame "Rs2 R1 R2". }
iExists γi, γs, γw, γsw, _, (1/2)%Qp. iFrame "HP1'".
iDestruct ("INV" $! strong_noCAS (weak_noCAS γsw)) as "(Invs & Invw & Rs & #R1 & #R2)".
iMod ("VI" $! (arc_inv P1 P2 γi γs γw γsw l) with "[SMA SDA WUA $Invs $Invw]")
as "[$ tok]".
{ iNext. iSplitL "SMA"; [by iExists _|]. iSplitL "SDA"; by iExists _. }
iModIntro. iExists _,∅,∅. iFrame "St tok Rs SM2 SD2 S2".
iSplitL""; iExists _,_; [by iFrame "R1"|by iFrame "R2"].
Qed.
Lemma create_weak E l :
l ↦ #0 -∗ (l >> 1) ↦ #1 -∗ P2 ={E}=∗ ∃ γi γs γw γsw t q,
is_arc P1 P2 N γi γs γw γsw l ∗ weak_arc t q l γi γs γw γsw.
Proof.
iIntros "Hl1 Hl2 HP2".
set stS : strong_stUR := None.
set stW : weak_stUR := (None, Some (Cinl ((1/2)%Qp, xH))).
iAssert (|==> ∃ γ, StrongAuth γ stS ∗ WeakAuth γ stW ∗ WeakTok γ (1/2)%Qp ∗
StrongMoveAuth γ ∅ ∗ StrMoves γ 1%Qp ∅ ∗
StrongDownAuth γ ∅ ∗ StrDowns γ 1%Qp ∅ ∗
WeakUpAuth γ ∅ ∗ WkUps γ 1%Qp ∅)%I as ">Own".
{ do 8 setoid_rewrite <- embed_sep. do 8 setoid_rewrite <- own_op.
iMod (own_alloc (A:=arcUR)) as (γ) "?"; last by iExists γ.
split; split; simpl.
- rewrite !right_id. by apply auth_auth_valid.
- rewrite !left_id !right_id. by apply auth_both_valid_discrete.
- split; split; simpl; rewrite !left_id !right_id.
+ by apply auth_both_valid_discrete.
+ by apply auth_auth_valid.
+ by apply auth_both_valid_discrete.
+ by apply auth_auth_valid.
- split; simpl; rewrite !left_id ?right_id.
+ by apply auth_both_valid_discrete.
+ by apply auth_auth_valid. }
iDestruct "Own" as (γsw) "(SA & WA & Wt & SMA & SM & SDA & SD & WUA & [WU1 WU2])".
iMod (view_inv_alloc_frac N _ (1/2 + 1/2/2) (1/2/2)) as (γi) "[[tok2 tok1] VI]";
first by rewrite -Qp.add_assoc 2!Qp.div_2.
set Qs : time → time → gname → gname → vProp :=
(λ ts tw γs γw,
GPS_SWSharedReader (l >> 1) tw (() : unitProtocol) #1 (1 / 2)%Qp γw ∗
GPS_Reader l ts (() : unitProtocol) #0 γs ∗
GPS_Reader (l >> 1) tw (() : unitProtocol) #1 γw)%I.
iMod (GPS_SWRaw_Init_vs_strong_2 _ _
(λ γw, strong_interp P1 (l >> 1) γi γw γsw)
(λ γs, weak_interp P2 l γi γs γsw) true _ _ () () Qs
with "Hl1 Hl2 [- SMA SDA WUA WU2 Wt VI]") as (γs γw ts tw) "INV".
{ iIntros (ts tw γs γw) "Ws Ww". simpl.
iDestruct (GPS_SWWriter_shared with "Ww") as "[Ww [Rw1 Rw2]]".
iDestruct (GPS_SWWriter_Reader with "Ws") as "#R1".
iDestruct (GPS_SWSharedReader_Reader with "Rw2") as "#R2".
iModIntro. iSplitL "SA"; last iSplitR "Rw2".
- iNext. iExists stS. iSplitL ""; [done|]. by iFrame "SA".
- iNext. iExists stW. iSplitL ""; [done|].
iFrame "WA HP2 Ww". iSplitL "tok1 Rw1 WU1".
+ iExists (1/2)%Qp,∅. rewrite Qp.div_2. iSplitL ""; [done|].
iFrame "tok1 Rw1 WU1". iExists _, _; by iFrame "R1".
+ iFrame "tok2". iSplitL "Ws"; [by iExists _|]. iSplitL "SM"; iExists _; by iFrame.
- by iFrame "Rw2 R1 R2". }
iExists γi, γs, γw, γsw, _, (1/2)%Qp.
iDestruct ("INV" $! strong_noCAS (weak_noCAS γsw)) as "(Invs & Invw & Rw & #R1 & #R2)".
iMod ("VI" $! (arc_inv P1 P2 γi γs γw γsw l) with "[SMA SDA WUA $Invs $Invw]")
as "[$ tok]".
{ iNext. iSplitL "SMA"; [by iExists _|]. iSplitL "SDA"; by iExists _. }
iModIntro. iExists _,∅. iFrame "Wt tok Rw WU2". iExists _,_; by iFrame "R1".
Qed.
Lemma strong_count_spec γi γs γw γsw l t v (P : vProp) tid :
is_arc P1 P2 N γi γs γw γsw l -∗ strong_arc_acc l t γi γs γw γsw P (⊤ ∖ ↑N) -∗
{{{ GPS_Reader l t () v γs ∗ P }}}
strong_count [ #l] @ tid; ⊤
{{{ (c : Z), RET #c; P ∗ ⌜0 < c⌝ }}}.
Proof.
iIntros "#INV #Hacc !# * [#RR HP] HΦ". iLöb as "IH". wp_rec.
iMod (view_inv_acc_base_1 γi N with "INV") as "VI"; [done|].
iMod (strong_arc_acc_open_no_change_2 with "HP Hacc")
as (Vs q Mt Dt) "[(St & tok & SR & SM & SD & oW) Hclose2]".
iSpecialize ("VI" $! Vs with "tok").
iMod (fupd_mask_mono with "VI") as "[tok VI]"; [set_solver|].
iDestruct "VI" as (Vb) "[VI Hclose1]".
iMod (arc_inv_split with "VI") as "((SMA & MDU) & IS & IW)".
iDestruct "SMA" as (Mt') "SMA".
iDestruct (StrongMoveAuth_agree with "[$SMA $SM]") as %?. subst Mt'.
iDestruct "SR" as (v0) "SR".
iDestruct (GPS_SWSharedReader_Reader_join_subjectively _ _ _ _ _ _ q
with "RR [SR]") as "[SR1 SR2]".
{ rewrite monPred_subjectively_unfold.
iDestruct (monPred_in_view_elim with "SR") as (V') "SR". by iExists V'. }
set R : time → unitProtocol → val → vProp :=
(λ t' _ v', (∃ z : Z, ⌜v' = #z ∧ 0 < z⌝) ∗
StrongTok γsw q ∗ StrongMoveAuth γsw Mt )%I.
iApply (GPS_SWRaw_SharedReader_Read (strong_interp P1 (l >> 1) γi γw γsw) _
strong_noCAS R with "[$SR1 $IS St SMA]"); [done|by right|..].
{ iIntros "!>" (t' [] v' [_ Ext']). rewrite /StrongTok. iIntros "!>". iSplit.
- iIntros "#SI !>". iFrame "SI SMA". rewrite /StrongTok. iFrame "St".
iDestruct "SI" as (st) "[% Eq]". subst v'. iExists (Z_of_arcstrong_st st).
destruct st as [[]|]; simpl; [|done]. iPureIntro. split; [done|split].
- iDestruct 1 as (st) "(Eq1 & SA & SI)". iDestruct "Eq1" as %?. subst v'.
destruct st as [[q' n]|]; simpl.
+ iModIntro. iFrame "SMA". rewrite /StrongTok. iFrame "St".
iSplitR""; [iExists (Some (q',n))|iExists (Z.pos n)].
* iSplitL""; [done|]. by iFrame.
* iPureIntro. split; [done|split].
+ iDestruct (StrongTok_Auth_agree with "[$SA St]") as %(?&?&EQ&?); [|done].
rewrite /StrongTok. iFrame "St". }
iIntros "!>" (t' [] v'). iDestruct 1 as "(_ & RR' & IS & Rs & St & SMA)".
iDestruct "Rs" as (z) "[% %]". subst v'.
iDestruct (GPS_SWSharedReaders_join with "SR2 RR'") as "SR". rewrite Qp.div_2.
iSpecialize ("Hclose1" with "tok [SMA MDU IW IS]").
{ iDestruct "MDU" as "[SDA WUA]".
iApply (arc_inv_join with "[SMA] SDA WUA IS IW"). by iExists _. }
iMod (fupd_mask_mono with "Hclose1") as "[tok Hclose1]"; [set_solver|].
iMod ("Hclose2" with "St tok [SR] SM SD oW") as "HP"; [by iExists _|].
iModIntro. iMod "Hclose1". iModIntro. iApply ("HΦ"). by iFrame "HP".
Qed.
(* FIXME : in the case the weak count is locked, we can possibly
return -1. This problem already exists in Rust. *)
Lemma weak_count_spec γi γs γw γsw l t v ts (P : vProp) tid :
is_arc P1 P2 N γi γs γw γsw l -∗
strong_arc_acc l ts γi γs γw γsw P (⊤ ∖ ↑N) -∗
GPS_Reader (l >> 1) t () v γw -∗
{{{ P }}}
weak_count [ #l] @ tid; ⊤
{{{ (c : Z), RET #c; P ∗ ⌜-1 ≤ c⌝ }}}.
Proof.
iIntros "#INV #Hacc #RR !# * HP HΦ". iLöb as "IH". wp_rec. wp_op.
iMod (view_inv_acc_base_1 γi N with "INV") as "VI"; [done|].
iMod (strong_arc_acc_open_no_change with "HP Hacc")
as (Vs q Mt Dt) "[(St & tok & SR & SM & SD & oW) Hclose2]".
iSpecialize ("VI" $! Vs with "tok").
iMod (fupd_mask_mono with "VI") as "[tok VI]"; [set_solver|].
iDestruct "VI" as (Vb) "[VI Hclose1]".
iMod (arc_inv_split with "VI") as "(MA & IS & IW)".
set R : time → unitProtocol → val → vProp :=
(λ _ _ v', ∃ z : Z, ⌜v' = #z ∧ -1 ≤ z⌝)%I.
iApply (GPS_SWRaw_Read (weak_interp P2 l γi γs γsw) _
(weak_noCAS γsw) R with "[$RR $IW]"); [done|by right|..].
{ iIntros "!>" (t' [] v' [_ Ext']) "!>". iSplit; last iSplit.
- iIntros "#WI !>". iFrame "WI". iDestruct "WI" as (st) "[Eq1 Eq2]".
iDestruct "Eq1" as %?. subst v'. iExists (Z_of_arcweak_st st).
iPureIntro. split; [done|]. by apply weak_stUR_value.
- iDestruct 1 as (st) "[Eq1 WI]". iDestruct "Eq1" as %?. subst v'.
iModIntro. iSplitR "". { iExists _. by iFrame "WI". }
iExists _. iPureIntro. split; [done|]. by apply weak_stUR_value.
- iDestruct 1 as "[% $]". subst v'. iIntros "!> !%". split; [done|].
by eexists. }
iIntros "!>" (t' [] v'). iDestruct 1 as "(_ & _ & IW & Rs)".
iDestruct "Rs" as (z) "Eq". iDestruct "Eq" as %[? Le]. subst v'.
iSpecialize ("Hclose1" with "tok [MA IW IS]").
{ iDestruct "MA" as "[SMA [SDA WUA]]".
by iApply (arc_inv_join with "SMA SDA WUA IS IW"). }
iMod (fupd_mask_mono with "Hclose1") as "[tok Hclose1]"; [set_solver|].
iMod ("Hclose2" with "St tok SR SM SD oW") as "HP".
iModIntro. iMod "Hclose1". iModIntro. iApply ("HΦ"). by iFrame "HP".
Qed.
Lemma arc_strong_interp_comparable l γi γs γw γsw t s v (n: Z):
strong_interp P1 (l >> 1) γi γw γsw true l γs t s v
∨ strong_interp P1 (l >> 1) γi γw γsw false l γs t s v
-∗ ⌜∃ vl : lit, v = #vl ∧ lit_comparable n vl⌝.
Proof.
iIntros "[F|F]"; iDestruct "F" as (st ?) "_"; subst v;
iExists _; iPureIntro; (split; [done|]); by constructor.
Qed.
Lemma arc_strong_interp_comparable_2 l γi γs γw γsw t s v (n: Z):
strong_interp P1 (l >> 1) γi γw γsw true l γs t s v
∨ strong_interp P1 (l >> 1) γi γw γsw false l γs t s v
∨ strong_noCAS l γs t s v
-∗ ⌜∃ vl : lit, v = #vl ∧ lit_comparable n vl⌝.
Proof.
iIntros "[F|[F|%]]";
last (subst; iPureIntro; eexists; split; [done|by constructor]);
iDestruct "F" as (st ?) "_"; subst;
iExists _; iPureIntro; (split; [done|]); by constructor.
Qed.
Lemma clone_arc_spec {HPn: HP1} γi γs γw γsw l t v (P : vProp) tid :
is_arc P1 P2 N γi γs γw γsw l -∗ strong_arc_acc l t γi γs γw γsw P (⊤ ∖ ↑N) -∗
(∃ t' v', GPS_Reader (l >> 1) t' () v' γw) -∗
GPS_Reader l t () v γs -∗
{{{ P }}} clone_arc [ #l] @ tid; ⊤
{{{ t' q', RET #☠; P ∗ strong_arc t' q' l γi γs γw γsw ∗ P1 q' }}}.
Proof.
iIntros "#INV #Hacc #WR #RR !# * HP HΦ". iLöb as "IH". wp_rec.
(* read the value *)
wp_bind (!ʳˡˣ_)%E.
(* opening invariant *)
iMod (view_inv_acc_base_1 γi N with "INV") as "VI"; [done|].
iMod (strong_arc_acc_open_no_change_2 with "HP Hacc")
as (Vs q Mt Dt) "[(St & tok & SR & SM & SD & oW) Hclose2]".
iSpecialize ("VI" $! Vs with "tok").
iMod (fupd_mask_mono with "VI") as "[tok VI]"; [set_solver|].
iDestruct "VI" as (Vb) "[VI Hclose1]".
iMod (arc_inv_split with "VI") as "(MA & IS & IW)".
iDestruct "SR" as (v0) "SR".
iDestruct (GPS_SWSharedReader_Reader_join_subjectively _ _ _ _ _ _ q
with "RR [SR]") as "[SR1 SR2]".
{ rewrite monPred_subjectively_unfold.
iDestruct (monPred_in_view_elim with "SR") as (V') "SR". by iExists V'. }
set R : time → unitProtocol → val → vProp := (λ t' _ v', ∃ z : Z, ⌜v' = #z⌝)%I.
(* actual read *)
iApply (GPS_SWRaw_SharedReader_Read (strong_interp P1 (l >> 1) γi γw γsw) _
strong_noCAS R with "[$SR1 $IS]"); [done|by left|..].
{ iIntros "!>" (t' [] v' [_ Ext']) "!>". iSplit.
- iIntros "#SI !>". iFrame "SI". iDestruct "SI" as (st) "[Eq1 _]".
iDestruct "Eq1" as %?. subst v'. by iExists _.
- iDestruct 1 as (st) "[Eq1 SI]". iDestruct "Eq1" as %?. subst v'.
iModIntro. iSplitR ""; [iExists _; by iFrame "SI"|by iExists _]. }
iIntros "!>" (t' [] v'). iDestruct 1 as "(_ & SR1 & IS & Rs)".
iDestruct "Rs" as (z) "%". subst v'.
iDestruct (GPS_SWSharedReaders_join with "SR2 SR1") as "SR". rewrite Qp.div_2.
(* closing invariant *)
iSpecialize ("Hclose1" with "tok [MA IW IS]").
{ iDestruct "MA" as "[SMA [SDA WUA]]".
by iApply (arc_inv_join with "SMA SDA WUA IS IW"). }
iMod (fupd_mask_mono with "Hclose1") as "[tok Hclose1]"; [set_solver|].
iMod ("Hclose2" with "St tok [SR] SM SD oW") as "HP"; [by iExists _|].
iModIntro. iMod "Hclose1" as "_".
iModIntro. wp_let. wp_op. clear Vs R v0 Mt Dt t' Vb q.
(* now we can CAS *)
wp_bind (CAS _ _ _ _ _ _).
(* not really, just preparing *)
(* opening invariant *)
iMod (view_inv_acc_base_1 γi N with "INV") as "VI"; [done|].
iMod (strong_arc_acc_open_2 with "HP Hacc")
as (Vs q Mt Dt) "[(St & tok & SR & SM & SD & oW) Hclose2]".
iSpecialize ("VI" $! Vs with "tok").
iMod (fupd_mask_mono with "VI") as "[tok VI]"; [set_solver|].
iDestruct "VI" as (Vb) "[VI Hclose1]".
iMod (arc_inv_split with "VI") as "((SMA & MDU) & IS & IW)".
iDestruct "SR" as (v0) "SR".
iDestruct (GPS_SWSharedReader_Reader_join_subjectively _ _ _ _ _ _ q
with "RR [SR]") as "[SR1 SR2]".
{ rewrite monPred_subjectively_unfold.
iDestruct (monPred_in_view_elim with "SR") as (V') "SR". by iExists V'. }
iDestruct "SMA" as (Mt') "SMA".
iDestruct (StrongMoveAuth_agree with "[$SMA $SM]") as %?. subst Mt'.
set Q: time → unitProtocol → vProp :=
(λ t'' s'',
let Mt' := if decide (Z.to_pos z = 1%positive) then (Mt ∪ {[t'']}) else Mt in
StrongTok γsw q ∗ StrongMoveAuth γsw Mt' ∗ StrMoves γsw q Mt' ∗
∃ q'', StrongTok γsw q'' ∗ (<obj> P1 q'') ∗
(<obj> view_tok γi (q''/2)) ∗
GPS_SWSharedReader l t'' ((): unitProtocol) #(z+1) q'' γs ∗
StrMoves γsw q'' Mt' ∗ ⌜∀ t' : time, t' ∈ Mt' → t' ⊑ t''⌝ ∗
StrDowns γsw q'' ∅ ∗ agown γsw (awk_n ((Some q'', ε))))%I.
set Q1: time → unitProtocol → vProp := (λ _ _, True)%I.
set Q2: time → unitProtocol → vProp :=
(λ tr sr, strong_interp P1 (l >> 1) γi γw γsw true l γs tr sr #z).
set R: time → unitProtocol → lit → vProp :=
(λ _ _ _, StrongTok γsw q ∗ StrongMoveAuth γsw Mt ∗ StrMoves γsw q Mt)%I.
iMod (rel_True_intro tid) as "#rTrue".
(* here comes the CAS *)
iApply (GPS_SWRaw_SharedWriter_CAS_int_strong (strong_interp P1 (l >> 1) γi γw γsw)
_ strong_noCAS false Relaxed Relaxed Relaxed z #(z+1) _ _ _ (q/2) _
True%I Q Q1 Q2 R _ Vb _ (↑histN) with "[$SR1 $IS St SMA SM $rTrue]");
[done|done|by left|by left|by left|..].
{ iSplitL "". { iIntros "!>!>" (??? _). by iApply arc_strong_interp_comparable. }
rewrite /= -(bi.True_sep' (∀ _ (_ : unitProtocol), _)%I).
iApply (rel_sep_objectively with "[$rTrue St SMA SM]").
rewrite /StrongTok /StrMoves. iIntros "!>" (t' [] [_ Ext']). iSplit; last first.
{ iIntros (??) "!> !>". iSplit; iIntros "$ !>"; iFrame "SMA";
rewrite /StrongTok /StrMoves; by iFrame. }
iSplitL "". { by iIntros "!> $". } iIntros "_".
iDestruct 1 as (st) "[>Eq [>SA SI]]".
iDestruct (StrongTok_Auth_agree with "[$SA St]") as %(q'&n&EQ&Eq');
[by rewrite /StrongTok|]. subst st. iDestruct "Eq" as %Eq.
inversion Eq. subst z. clear Eq. iDestruct "SI" as "[$ SI]".
iDestruct "SI" as (q'') "(>Eq'' & HP & SI)". iDestruct "Eq''" as %Eq''.
iAssert (▷ (P1 (q''/2) ∗ <obj> P1 (q''/2)))%I with "[HP]" as "[HP1 HP2]".
{ iNext. iApply HPn. by rewrite Qp.div_2. }
iDestruct "SI" as (Mt' Dt') "(>tok & R & SM' & RestS)". rewrite /StrMoves.
iDestruct "SM'" as ">SM'".
iDestruct (StrongMoveAuth_agree with "[$SMA SM']") as %?;
[by rewrite /StrMoves|]. subst Mt'.
iDestruct (view_tok_split_unit γi (q''/2/2) (q''/2/2) with "[tok]")
as "[tok1 tok2]"; first by rewrite Qp.div_2.
iModIntro. iExists (). iSplitL""; [done|]. iIntros (t'' Lt'') "W' _".
set Mt' : gset time := if decide (n = 1%positive) then (Mt ∪ {[t'']}) else Mt.
iAssert (|==> StrongMoveAuth γsw Mt' ∗ StrMoves γsw q Mt' ∗
StrMoves γsw q'' Mt' ∗
<obj> if decide (n = 1%positive)
then StrMoveCert γsw t'' else True)%I
with "[SMA SM SM']" as ">(SMA & SM & [SM1 SM2] & Cert)".
{ case decide => Eq; last first.
{ rewrite /Mt' decide_False; [|done]. rewrite /StrMoves.
iFrame; by iIntros "!> !>". }
rewrite /Mt' (decide_True _ _ Eq). rewrite (decide_True _ _ Eq) in Eq'.
iDestruct (StrMoves_fractional _ _ q with "[SM SM']") as "SM";
[rewrite /StrMoves; by iFrame|]. subst q'. rewrite Eq''.
iMod (StrMoves_update _ _ {[t'']} with "[$SMA $SM]") as "($ & SM & Cert)".
iIntros. rewrite bi.sep_assoc -StrMoves_fractional Eq''. iFrame "SM".
iIntros "!> !>". by iFrame "Cert". }
iModIntro. iSplitL "Cert".
{ iIntros "!> _ !> !>". iExists (Some (q', n)). iSplit; [done|].
destruct n; simpl; [done..|]. iExists t''. iFrame (Lt'').
iDestruct (monPred_objectively_elim with "Cert") as "$". }
iIntros "!> !>".
iDestruct (GPS_SWSharedWriter_Reader_update with "W' R") as "[W' [R1 R2]]".
iDestruct "RestS" as "(#SeenM & SD & #SeenD & [OS1 OS2])".
iAssert (⌜∀ t' : time, t' ∈ Mt' → t' ⊑ t''⌝)%I with "[W']" as "#MAX".
{ iDestruct "SeenM" as (t4 v4) "[R4 MAX]". iDestruct "MAX" as %MAX'.
iDestruct (GPS_SWSharedWriter_latest_time_1 with "W' R4") as %Le4.
iPureIntro. have MAX2: ∀ t2 : time, t2 ∈ Mt → t2 ⊑ t'' by move =>? /MAX'->.
rewrite /Mt'. case decide => ?; [|done].
move => t5 /elem_of_union [/MAX2//|/elem_of_singleton -> //]. }
iDestruct (GPS_SWSharedReader_Reader with "R1") as "#R''".
iAssert (SeenActs γs l Mt')%I as "SeenM'". { iExists _,_. iFrame "R'' MAX". }
iMod (StrongAuth_new_tok _ q' q'' _ Eq'' with "SA") as "[SA St']".
iAssert (StrDowns γsw (q'' / 2) Dt' ∗ StrDowns γsw (q'' / 2) ∅)%I
with "[SD]" as "[SD1 SD2]".
{ rewrite -(right_id_L ∅ union Dt') -{1}(Qp.div_2 q'').
by iApply (StrDowns_forget with "SD"). }
iModIntro. iSplitR "SA W' tok1 R1 SM1 OS1 SD1 HP1 SeenM'".
- rewrite /Q Pos2Z.id. iFrame "SMA SM". iSplitL "St"; [by rewrite /StrongTok|].
iExists _. by iFrame "St' tok2 R2 SM2 HP2 MAX SD2 OS2".
- iExists (Some ((q' + q'' / 2)%Qp, (n + 1)%positive)). iSplitL ""; [done|].
iFrame "SA W'". iExists (q''/2)%Qp.
iSplitL "". { iPureIntro. by rewrite -Qp.add_assoc Qp.div_2. }
iFrame "HP1". iExists Mt', Dt'. iFrame "tok1 R1 SM1 SeenM' SD1 SeenD OS1". }
(* finally done with the CAS *)
iIntros "!>" (b t' [] v') "(IS &%& [([%[%%]]&R'&Q) | ([%[_ %]] &R'&Rs&_)])".
- subst b v'.
iDestruct "Q" as "(St & SMA & SM & Q)". iDestruct "Q" as (q'') "Q".
iDestruct "Q" as "(St'' & HP1 & tok'' & R'' & SM'' & %MAX'' & oW'' & SD'')".
iDestruct (GPS_SWSharedReaders_join_subjectively _ _ _ _ _ _ _ _ q''
with "R' [R'']") as "[R' R'']". { by iApply acq_subjective. }
iDestruct (GPS_SWSharedReader_Reader with "R'") as "#RR'".
set Mt' := if decide (Z.to_pos z = 1%positive) then Mt ∪ {[t']} else Mt.
iAssert (SeenActs γs l Mt')%I with "[]" as "#SeenM'".
{ iExists _, _. iFrame (MAX'') "RR'". }
iDestruct (GPS_SWSharedReaders_join with "SR2 R'") as "SR".
(* tedious removal of acq_mod *)
rewrite Qp.div_2 2!acq_objectively_elim 2!monPred_objectively_elim.
rewrite 7!acq_embed_elim.
(* closing *)
iSpecialize ("Hclose1" with "tok [SMA MDU IW IS]").
{ iClear "#". iDestruct "MDU" as "[SDA WUA]".
iApply (arc_inv_join with "[SMA] SDA WUA IS IW"). by iExists _. }
iMod (fupd_mask_mono with "Hclose1") as "[tok Hclose1]"; [set_solver|].
iMod ("Hclose2" $! (if decide (Z.to_pos z = 1%positive) then {[t']} else ∅) ∅
with "[St] tok [SR] [SM] [] [SD] [] oW") as "HP";
[by iExact "St"|by iExists _|..|by rewrite right_id_L|done|].
{ rewrite /Mt'. case decide => _; [done|by rewrite right_id_L]. }
{ destruct (decide (Z.to_pos _ = _)); [|done].
iExists _,_. iFrame "RR'". iPureIntro. move => ? /elem_of_singleton -> //. }
iModIntro. iMod "Hclose1" as "_". iModIntro.
wp_case. iApply ("HΦ" with "[- $HP]"). iFrame "HP1".
iExists _,_,∅. rewrite /StrongTok. iFrame "St'' tok'' R''".
rewrite /StrMoves /StrDowns. iFrame "SM'' SeenM' oW'' SD''".
iDestruct "WR" as (??) "WR". iExists _, _. by iFrame "WR".
- subst b.
iDestruct "Rs" as "(St & SMA & SM)". rewrite 3!acq_embed_elim.
iDestruct (GPS_SWSharedReaders_join with "SR2 R'") as "SR". rewrite Qp.div_2.