-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof_ebr_rcu_base.v
2574 lines (2347 loc) · 112 KB
/
proof_ebr_rcu_base.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.algebra Require Import auth gset gmap.
From iris.base_logic.lib Require Import invariants ghost_map ghost_var.
From smr.base_logic.lib Require Import coP_ghost_map ghost_vars coP_cancellable_invariants mono_nats.
From smr.program_logic Require Import atomic.
From smr.logic Require Import token2 epoch_history.
From smr.lang Require Import proofmode notation.
From smr Require Import ebr.spec_rcu_base spec_slot_bag_onat spec_retired_list.
From smr Require Import ebr.code_ebr code_slot_bag_onat code_retired_list.
From smr Require Import helpers logic.reclamation.
From iris.prelude Require Import options.
Set Printing Projections.
(* TODO: Fix names. e.g, info or Im? ptrs or ptrs? rL or Rs? *)
Class ebrG Σ := EBRG {
#[export] ebr_reclG :: reclamationG Σ;
#[local] ebr_epoch_historyG :: epoch_historyG Σ;
(* Epoch history is
(1) not fractional
(2) may be updated with the retire set not being updated.
hence we use a separate ghost for the retire set.
*)
#[local] ebr_epoch_rsetG :: ghost_varG Σ (gset positive);
#[local] ebr_epoch_gmapG :: ghost_mapG Σ blk positive;
#[local] ebr_mono_natsG :: mono_natsG Σ;
}.
Definition ebrΣ : gFunctors := #[ reclamationΣ; epoch_historyΣ; ghost_varΣ (gset positive); ghost_mapΣ blk positive; mono_natsΣ ].
Global Instance subG_ebrΣ Σ :
subG ebrΣ Σ → ebrG Σ.
Proof. solve_inG. Qed.
Section ebr.
Context `{!heapGS Σ, !ebrG Σ}.
Notation iProp := (iProp Σ).
Notation resource := (resource Σ).
Implicit Types (R : resource).
Context (mgmtN ptrsN : namespace) (DISJN : mgmtN ## ptrsN).
Set Default Proof Using "DISJN".
Context (sbs : slot_bag_spec Σ) (rls : retired_list_spec Σ).
Implicit Types
(γsb γtok γinfo γptrs γeh γrs γU γR γV γe_nums : gname)
(info : gmap positive (alloc * gname))
(ptrs : gmap blk positive)
(sbvmap : gmap loc (bool * option nat))
(slist : list loc)
(rL : list (blk * nat * nat))
(ehist : list (gset positive))
(Syn retired unlinked : gset positive)
.
(*
Important ghosts
* A guard at e asserts this flag, meaning that it is validated with epoch e.
```
(sid e,sid idx) ↦P2[γV]{ 1/2/2 } true
```
(Remaining 1/2 in GE_minus, 1/2/2 in SlotInfos)
(Here, sid doesn't mean anything. It's just injection from nat to positive.)
TODO: Maybe this can be 1D ghost instead: `(sid idx) ↦ Some e`
With this flag, the guard takes two kinds of ownership:
- permission to add retires at epoch e:
```
epoch_history_frag γeh e {[sid si]}
```
- preventing global epoch advancement from e+1 to e+2:
```
mono_nats_auth γe_nums {[sid idx]} (1/2/2) e
```
(Remaining 1/2 in GE_minus, 1/2/2 in SlotInfos. When not active, all 1 is in SlotInfos.)
This is indirectly asserted by `mono_nats_lb γe_nums ⊤ (ge - 1)` in SlotInfos.
* γR (reclaim flag) is actually 1D, for each ptr id (the slot component is always ⊤).
The only reason it's using 2D ghost is code sharing for RetiredBase.
* γU (unlink flag) is 1D, for each ptr id (unchanged).
This flag is actually necessary (unlink HP where it's redundant): see UnlinkFlags_lookup_false.
Invariants
0. epoch_history tracks thing retired at each epoch.
When global epoch is e:
* e-2 is finalized: no more pointers can be retired at that epoch
* pointers reclaimed at e-3 are reclaimable
1. for each ptr id `i`, unlink flag is true iff it's in the epoch_history
* 1-1. If there's a retired node of `i` with epoch `e`, then `i` is in epoch `e`.
* 1-2. If `i` is in unlink history at `e`, then unlink flag is true.
2. Active epoch of each slot is monotone. γe_nums ghost.
When inactive, this ghost is set to global epoch.
Proof summary
* Guard-Activate (validating the local epoch at e)
* take a snapshot of the finalized part (up to `e-2`) of epoch history
* take all tokens that are not in the snapshot
* set the γV flag and take epoch ownership (retiring at e; advancement from e+1 → e+2)
* Guard-Protect
1. BaseManaged of `i` has `{i} ↪[U]{1/2} false`
2. auth unlink history doesn't contain it (∵ invariant 1)
3. my history snapshot doesn't contain it (by monotonicity)
4. I have token for it
* may_advance: check the lower bound of possibly active epoch numbers for each slot.
* try_advance e → e+1
1. observe that the lower bound of all active epoch is at least e (from may_advance)
2. If we are the one advancing the epoch,
domain should have all ownerships needed to advance epoch
3. collect tokens for pointers retired at e-2.
4. collect the epoch ownership of e-1.
If try_advance has seen active epoch lower bound e, then the guard can't be active at epoch e-1
(enforced by keeping mono_nat_auth in GE_minus for active guard).
* do_reclamation just takes already collected tokens
*)
(* Ghosts for finalized epochs 0..=(ge-2) *)
Definition GE_minus_2_et_al γV γeh ge slist : iProp :=
(* NOTE: We will throw away [γV] for ({[1]}, ⊤) *)
(sids_to (ge-1),sids_from (length slist)) ↦P2[γV] false ∗
(sids_to (ge-1),sids_to (length slist)) ↦P2[γV]{ 1/2 } false ∗
[∗ list] e ∈ seq 0 (ge-1),
epoch_history_frag γeh e ⊤.
(* Ghosts for e = ge - 1 and e = ge. *)
Definition GE_minus γV γeh γe_nums e slist sbvmap : iProp :=
({[sid e]},sids_from (length slist)) ↦P2[γV] false ∗
epoch_history_frag γeh e (sids_from' (length slist)) ∗
[∗ list] si ↦ slot ∈ slist, ∃ (b : bool) v (V : bool),
⌜ sbvmap !! slot = Some (b, v) ⌝ ∗
(sid e,sid si) ↦p2[γV]{ 1/2 } V ∗
⌜ V → b = true ∧ v = Some e ⌝ ∗
match V with
| true => mono_nats_auth γe_nums {[sid si]} (1/2) e
| false => epoch_history_frag γeh e {[sid si]}
end.
(*
(* non-allocated slots *)
({[ge]}, sids_from (length slist)) ↦P2[γV] false ∗
({[ge+1]},sids_from (length slist)) ↦P2[γV] false ∗
epoch_history_frag γeh (ge-1) (sids_from' (length slist)) ∗
epoch_history_frag γeh ge (sids_from' (length slist)) ∗
(* active slots *)
[∗ list] si ↦ slot ∈ slist, ∃ (b : bool) v (V_1 V_0 : bool),
⌜ sbvmap !! slot = Some (b, v) ⌝ ∗
(sid (ge-1), sid si) ↦p2[γV]{ 1/2 } V_1 ∗
(sid ge, sid si) ↦p2[γV]{ 1/2 } V_0 ∗
match V_1, V_0 with
| false, false => (* not active *)
epoch_history_frag γeh (ge-1) {[sid si]} ∗
epoch_history_frag γeh ge {[sid si]}
| true, false => (* at epoch ge-1 *)
⌜ b = true ∧ v = Some (ge-1) ⌝ ∗
(* this guard blocks ge → ge+1 *)
mono_nats_auth γe_nums {[sid si]} (1/2) (ge-1) ∗
epoch_history_frag γeh ge {[sid si]}
| false, true => (* at epoch ge *)
⌜ b = true ∧ v = Some ge ⌝ ∗
(* This guard blocks ge+1 → ge+2 *)
epoch_history_frag γeh (ge-1) {[sid si]} ∗
mono_nats_auth γe_nums {[sid si]} (1/2) ge
| true, true => False
end
*)
Definition GhostEpochInformations γV γeh γe_nums ge slist sbvmap : iProp :=
GE_minus_2_et_al γV γeh ge slist ∗
GE_minus γV γeh γe_nums (ge - 1) slist sbvmap ∗
GE_minus γV γeh γe_nums ge slist sbvmap ∗
([∗ list] si ↦ slot ∈ slist, ∃ (b : bool) v,
⌜ sbvmap !! slot = Some (b, v) ⌝) ∗
(sids_from (ge + 1),sids_to (length slist)) ↦P2[γV]{ 1/2 } false ∗
(sids_from (ge + 1),sids_from (length slist)) ↦P2[γV] false.
Global Instance ghost_epoch_informations_timeless γV γeh γe_nums ge slist sbvmap :
Timeless (GhostEpochInformations γV γeh γe_nums ge slist sbvmap).
Proof. apply _. Qed.
(* NOTE: Suppose we want to advance [ge] → [ge+1]. If a guard was at epoch
[ge], then it's epoch will never decrease. So we can take [frag]s at once when
we write the new global epoch. *)
(* BaseManaged should assert that it's not unlinked. *)
(* NOTE: Should keep this even in the epoch ownership model. *)
Definition UnlinkFlags γU info ehist : iProp :=
(⊤ ∖ gset_to_coPset (dom info)) ↦P[γU] false ∗
[∗ map] i ↦ info_i ∈ info, ∃ (U : bool),
i ↦p[γU]{1/2} U ∗
let retired := fold_hist ehist in
⌜ U ↔ i ∈ retired ⌝
.
Definition SlotInfos γV γtok γeh γe_nums slist sbvmap ehist (ge := length ehist - 1) : iProp :=
mono_nats_lb γe_nums ⊤ (ge - 1) ∗
mono_nats_auth γe_nums (sids_from (length slist)) 1 ge ∗
let reclaimable := gset_to_coPset (fold_hist (take (ge - 2) ehist)) in
toks γtok (⊤ ∖ reclaimable) (sids_from (length slist)) ∗
[∗ list] si ↦slot ∈ slist,
match sbvmap !! slot with
| Some (false, _) =>
toks γtok (⊤ ∖ reclaimable) {[sid si]} ∗
(* extra 1/2/2 for non-existent guard *)
(⊤,{[sid si]}) ↦P2[γV]{ 1/2 } false ∗
mono_nats_auth γe_nums {[sid si]} 1 ge
| Some (true, None) =>
toks γtok (⊤ ∖ reclaimable) {[sid si]} ∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2/2 } false ∗
mono_nats_auth γe_nums {[sid si]} 1 ge
| Some (true, Some e) => ∃ (V : bool),
(⊤ ∖ {[sid e]},{[sid si]}) ↦P2[γV]{ 1/2/2 } false ∗
(sid e,sid si) ↦p2[γV]{ 1/2/2 } V ∗
if V then ∃ ehistF,
⌜length ehistF = e - 1⌝ ∗
epoch_history_finalized γeh ehistF ∗
let finalized := gset_to_coPset (fold_hist ehistF) in
toks γtok (finalized ∖ reclaimable) {[sid si]} ∗
mono_nats_auth γe_nums {[sid si]} (1/2/2) e
else
toks γtok (⊤ ∖ reclaimable) {[sid si]} ∗
mono_nats_auth γe_nums {[sid si]} 1 ge
| None => False
end.
Global Instance slot_infos_timeless γV γtok γeh γe_nums slist sbvmap ge:
Timeless (SlotInfos γV γtok γeh γe_nums slist sbvmap ge).
Proof. apply _. Qed.
Definition ReclaimInfo γR γtok ehist info (ge := length ehist - 1): iProp :=
∃ reclaimed,
(* 0..=(ge-3) *)
let reclaimable := gset_to_coPset (fold_hist (take (ge - 2) ehist)) in
(gset_to_coPset reclaimed,⊤) ↦P2[γR]{ 1/2 } true ∗
(gset_to_coPset (dom info ∖ reclaimed),⊤) ↦P2[γR]{ 1/2 } false ∗
(⊤ ∖ gset_to_coPset (dom info),⊤) ↦P2[γR] false ∗
toks γtok (reclaimable ∖ (gset_to_coPset reclaimed)) ⊤
(* ⌜ reclaimed ⊆ reclaimable ⌝ *)
.
Definition BaseManaged γe (p : blk) (i_p : gname) (size_i : nat) R : iProp :=
∃ γsb γtok γinfo γptrs γeh γrs γU γV γR γe_nums (d : loc),
⌜γe = encode (γsb, γtok, γinfo, γptrs, γeh, γrs, γU, γV, γR, γe_nums, d)⌝ ∗
ManagedBase mgmtN ptrsN γtok γinfo γptrs γU γR p i_p size_i R.
Definition BaseNodeInfo γe (p : blk) (i_p : positive) (size_i : nat) R : iProp :=
∃ γsb γtok γinfo γptrs γeh γrs γU γV γR γe_nums (d : loc),
⌜γe = encode (γsb, γtok, γinfo, γptrs, γeh, γrs, γU, γV, γR, γe_nums, d)⌝ ∗
NodeInfoBase mgmtN ptrsN γtok γinfo γptrs p i_p size_i R.
(* Note: A persistent instance with a lot of evars may cause search for other such instances to become very slow due to a mismatch.
For example, [BaseNodeInfo_persistent] instance will cause the search for [IsEBRDomain_persistent] to be very slow.
Solution is to seal it or increse the cost for instances with lots of evars.
*)
Global Instance BaseNodeInfo_persistent γe p i_p size_p R : Persistent (BaseNodeInfo γe p i_p size_p R) | 2.
Proof. apply _. Qed.
Global Instance BaseNodeInfo_contractive γe p i_p size_p :
Contractive (λ (R : blk -d> list val -d> gname -d> iPropO Σ), BaseNodeInfo γe p i_p size_p R).
Proof.
intros ??? Hxy. rewrite /BaseNodeInfo /NodeInfoBase.
repeat apply bi.exist_ne => ?. repeat apply bi.sep_ne; try done.
repeat apply bi.exist_ne => ?. repeat apply bi.sep_ne; try done.
apply coP_cinv_contractive, dist_later_fin_iff.
destruct n; [done|]. simpl in *.
repeat apply bi.exist_ne => ?. repeat apply bi.sep_ne; try done.
apply Hxy. lia.
Qed.
Definition BaseGuardedNodeInfo γe γg (p : blk) (i_p : positive) (size_i : nat) R : iProp :=
BaseNodeInfo γe (p : blk) (i_p : gname) (size_i : nat) R ∗ p ↪[γg]□ i_p.
Global Instance BaseGuardedNodeInfo_persistent γe γg p i_p size_p R : Persistent (BaseGuardedNodeInfo γe γg p i_p size_p R).
Proof. apply _. Qed.
(* not using [ProtectedBase], because [γV] is not necessary here. *)
Definition Protected γtok γinfo γptrs (s : nat) (i_p : positive) (p : blk) : iProp :=
∃ γc_i size_i,
i_p ↪[γinfo]□ ({| addr := p; len := size_i |}, γc_i) ∗
p ↪c[γptrs]{{[ssid s]}} i_p ∗
coP_cinv_own γc_i {[ssid s]} ∗
Exchanges mgmtN γtok γptrs p i_p γc_i.
Definition EBRAuth γe info_a retired : iProp :=
∃ info γsb γtok γinfo γptrs γeh γrs γU γV γR γe_nums (d : loc),
⌜γe = encode (γsb, γtok, γinfo, γptrs, γeh, γrs, γU, γV, γR, γe_nums, d)⌝ ∗
ghost_map_auth γinfo (1/2) info ∗
ghost_var γrs (1/2) retired ∗
⌜info_a = fst <$> info⌝.
Global Instance EBRAuth_timeless γe info_a retired : Timeless (EBRAuth γe info_a retired) | 2.
Proof. apply _. Qed.
Definition EBRDomain γsb γtok γinfo γptrs γeh γrs γU γV γR γe_nums (d lBag rList : loc) : iProp :=
∃ info ptrs sbvmap slist rL ehist,
let retired := fold_hist ehist in
ghost_map_auth γinfo (1/2) info ∗
coP_ghost_map_auth γptrs 1 ptrs ∗
epoch_history_auth γeh ehist ∗
ghost_var γrs (1/2) retired ∗
sbs.(SlotBag) γsb lBag sbvmap slist ∗
rls.(RetiredList) rList rL ∗
(* Monotonicity of global epoch follows from monotonicity of epoch_history *)
let ge := length ehist - 1 in
GhostEpochInformations γV γeh γe_nums ge slist sbvmap ∗
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist ∗
UnlinkFlags γU info ehist ∗
⌜retired ⊆ (dom info)⌝ ∗
⌜ge ≥ 3⌝ ∗
(d +ₗ domGEpoch) ↦ #ge ∗
([∗ map] p ↦ i ∈ ptrs, ∃ info_i,
⌜info !! i = Some info_i⌝ ∗
(* Permission for freeing the pointer with this length.
This is used for proving [rcu_domain_register]. *)
†(Loc.blk_to_loc p)…info_i.1.(len)) ∗
ReclaimInfo γR γtok ehist info ∗
([∗ list] '(r,len,epoch) ∈ rL, ∃ i R unlinked_e,
RetiredBase mgmtN ptrsN γtok γinfo γptrs γU γR r i len R ∗
epoch_history_snap γeh epoch unlinked_e ∗
⌜ i ∈ unlinked_e ⌝) ∗
⌜∀ p i, ptrs !! p = Some i → ∃ z, info !! i = Some z ∧ z.1.(addr) = p ⌝ ∗
(* if a created id is not finalized, then its ptr is live with that id *)
⌜∀ i p, i ∉ fold_hist (take (ge - 2) ehist) → (∃ z, info !! i = Some z ∧ z.1.(addr) = p) → ptrs !! p = Some i ⌝
.
Definition ebrInvN := (to_baseN mgmtN) .@ "inv".
Definition IsEBRDomain γe (d : loc) : iProp :=
∃ γsb γtok γinfo γptrs γeh γrs γU γV γR γe_nums (lBag rList : loc),
⌜γe = encode (γsb, γtok, γinfo, γptrs, γeh, γrs, γU, γV, γR, γe_nums, d)⌝ ∗
(d +ₗ domLBag) ↦□ #lBag ∗
(d +ₗ domRSet) ↦□ #rList ∗
inv ebrInvN (EBRDomain γsb γtok γinfo γptrs γeh γrs γU γV γR γe_nums d lBag rList).
Global Instance IsEBRDomain_Persistent γe d : Persistent (IsEBRDomain γe d) | 2.
Proof. apply _. Qed.
Definition BaseInactive γe (g : loc) : iProp :=
∃ γsb γtok γinfo γptrs γeh γrs γU γV γR γe_nums
(d slot : loc) (idx : nat) (v : option nat),
⌜γe = encode (γsb, γtok, γinfo, γptrs, γeh, γrs, γU, γV, γR, γe_nums, d)⌝ ∗
(g +ₗ guardSlot) ↦ #slot ∗
(g +ₗ guardDom) ↦ #d ∗
†g…guardSize ∗
sbs.(Slot) γsb slot idx v ∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2/2 } false.
Definition BaseGuard γe γg (g : loc) Syn G : iProp :=
∃ γsb γtok γinfo γptrs γeh γrs γU γV γR γe_nums
(d slot : loc) (idx : nat) (v : option nat),
⌜γe = encode (γsb, γtok, γinfo, γptrs, γeh, γrs, γU, γV, γR, γe_nums, d)⌝ ∗
(g +ₗ guardSlot) ↦ #slot ∗
(g +ₗ guardDom) ↦ #d ∗
†g…guardSize ∗
sbs.(Slot) γsb slot idx v ∗
∃ e ehistF,
⌜v = Some e⌝ ∗
mono_nats_auth γe_nums {[sid idx]} (1/2/2) e ∗
(sid e,sid idx) ↦p2[γV]{ 1/2/2 } true ∗
(⊤∖{[sid e]},{[sid idx]}) ↦P2[γV]{ 1/2/2 } false ∗
epoch_history_frag γeh e {[sid idx]} ∗
let finalized := fold_hist ehistF in
ghost_map_auth γg 1 G ∗
⌜Syn = finalized ∧ length ehistF = e - 1⌝ ∗
epoch_history_finalized γeh ehistF ∗
toks γtok ((⊤ ∖ (gset_to_coPset finalized)) ∖ (gset_to_coPset (range G))) {[sid idx]} ∗
⌜(range G) ## finalized⌝ ∗
[∗ map] p ↦ i_p ∈ G,
p ↪[γg]□ i_p ∗
Protected γtok γinfo γptrs idx i_p p
.
(** Helper Lemmas *)
Lemma do_unprotect E γtok γinfo γptrs γg idx G :
↑(to_baseN mgmtN) ⊆ E →
( [∗ map] p ↦ i_p ∈ G, p ↪[γg]□ i_p ∗ Protected γtok γinfo γptrs idx i_p p)
={E}=∗
toks γtok (gset_to_coPset (range G)) {[sid idx]}.
Proof.
iIntros (?) "Prot".
iInduction (G) as [|p i_p G FRESH_l IH] using map_ind.
{ rewrite range_empty gset_to_coPset_empty. iApply token2_get_empty_1. }
iDestruct (big_sepM_delete _ _ p with "Prot") as "[[#p P] Prot]"; [apply lookup_insert|simpl].
iDestruct "P" as (??) "(#i↪ & pc & cinv & #Ex)".
iMod (exchange_stok_get with "Ex pc cinv") as "T"; [solve_ndisj|].
rewrite delete_insert; auto.
iMod ("IH" with "Prot") as "T'". iClear "IH".
rewrite range_insert // gset_to_coPset_union gset_to_coPset_singleton.
iModIntro.
iDestruct (toks_valid_1 with "T T'") as %Di; first set_solver.
rewrite -toks_union_1 //. iFrame.
Qed.
(** UnlinkFlags Lemmas *)
Lemma UnlinkFlags_alloc :
⊢ |==> ∃ γU, UnlinkFlags γU ∅ [∅; ∅; ∅; ∅].
Proof.
iMod (ghost_vars_top_alloc false) as (γU) "U⊤".
iModIntro. iExists γU. unfold UnlinkFlags.
rewrite dom_empty_L gset_to_coPset_empty difference_empty_L. iFrame.
by iApply big_sepM_empty.
Qed.
Lemma UnlinkFlags_lookup i info γU q (U : bool) ehist :
i ∈ dom info →
i ↦p[γU]{q} U -∗ UnlinkFlags γU info ehist -∗
let retired := fold_hist ehist in
⌜ U ↔ i ∈ retired ⌝.
Proof.
intros [? ElemOf]%elem_of_dom. iIntros "γU_i [_ γU]".
rewrite big_sepM_delete; last done.
iDestruct "γU" as "[[% [γU %]] _]".
by iDestruct (ghost_vars_agree with "γU_i γU") as %<-; [set_solver|].
Qed.
Lemma UnlinkFlags_lookup_false i info γU q ehist :
i ∈ dom info →
i ↦p[γU]{q} false -∗ UnlinkFlags γU info ehist -∗
let retired := fold_hist ehist in
⌜ i ∉ retired ⌝.
Proof.
iIntros (ElemOf) "γU_i γU".
iDestruct (UnlinkFlags_lookup with "γU_i γU") as %Hi; [done|].
destruct Hi as [_ Hi]. auto.
Qed.
Lemma UnlinkFlags_lookup_true i info γU q ehist :
i ∈ dom info →
i ↦p[γU]{q} true -∗ UnlinkFlags γU info ehist -∗
let retired := fold_hist ehist in
⌜ i ∈ retired ⌝.
Proof.
iIntros (ElemOf) "γU_i γU".
iDestruct (UnlinkFlags_lookup with "γU_i γU") as %Hi; [done|].
destruct Hi as [Hi _]. auto.
Qed.
Lemma UnlinkFlags_update_in γU γc_i info ehist i p k :
is_Some (info !! i) →
UnlinkFlags γU info ehist -∗
UnlinkFlags γU (<[i:=({| addr := p; len := k |}, γc_i)]> info) ehist.
Proof.
iIntros ([x Hi]) "[U UF]".
unfold UnlinkFlags. rewrite dom_insert_L.
rewrite subseteq_union_1_L; last first.
{ apply singleton_subseteq_l. by rewrite elem_of_dom. }
iFrame.
iDestruct (big_sepM_delete with "UF") as "[U UF]"; eauto.
iApply (big_sepM_delete _ _ i); first by rewrite lookup_insert.
rewrite delete_insert_delete. iFrame.
Qed.
Lemma UnlinkFlags_update_notin γU γc_i info ehist i p k :
info !! i = None →
i ∉ fold_hist ehist →
UnlinkFlags γU info ehist -∗
UnlinkFlags γU (<[i:=({| addr := p; len := k |}, γc_i)]> info) ehist ∗
i ↦p[γU]{1/2} false.
Proof.
iIntros (Hi Hfe) "[U_rest U_info]".
unfold UnlinkFlags. rewrite dom_insert_L.
rewrite (union_difference_singleton_L i (⊤ ∖ gset_to_coPset (dom info))); last first.
{ apply elem_of_complement. by rewrite not_elem_of_gset_to_coPset elem_of_dom Hi. }
rewrite -ghost_vars_union; last set_solver.
iDestruct "U_rest" as "[[Ui $] U_rest]".
rewrite !difference_difference_l_L union_comm_L -!gset_to_coPset_singleton -gset_to_coPset_union.
iFrame.
iApply big_sepM_insert; [done|]. iFrame.
iExists false. rewrite gset_to_coPset_singleton. iFrame. auto.
Qed.
(** GhostEpochInformation Lemmas *)
Lemma ghost_slot_unlink_reclaim_alloc :
⊢ |==> ∃ γeh γtok γR γV γe_nums,
epoch_history_auth γeh [∅; ∅; ∅; ∅] ∗
GhostEpochInformations γV γeh γe_nums 3 [] ∅ ∗
SlotInfos γV γtok γeh γe_nums [] ∅ [∅; ∅; ∅; ∅] ∗
ReclaimInfo γR γtok [∅; ∅; ∅; ∅] ∅.
Proof.
iMod epoch_history_alloc as (γeh) "(ehist & ◯eh1 & ◯eh2 & ◯eh3 & ◯eh4)".
iMod (ghost_vars2_top_alloc false) as (γV) "●V".
iEval (rewrite -{1}(sids_to_sids_from_union 2)) in "●V".
rewrite -ghost_vars2_union_1; last apply sids_to_sids_from_disjoint.
iDestruct "●V" as "[●Vt2T ●Vf2T]".
rewrite sids_from_S; auto.
rewrite -ghost_vars2_union_1; last apply sids_from_sid_disjoint; auto.
iDestruct "●Vf2T" as "[●Vf3T ●V2T]".
rewrite sids_from_S; auto.
rewrite -ghost_vars2_union_1; last apply sids_from_sid_disjoint; auto.
iDestruct "●Vf3T" as "[●Vf4T ●V3T]".
iMod (ghost_vars2_top_alloc false) as (γR) "●R".
iMod (mono_nats_own_alloc 2) as (γe_nums) "[●L ◯L]".
iMod (mono_nats_auth_update 3 with "●L") as "[●L ◯L']"; auto.
iMod (token2_alloc ⊤ ⊤) as (γtok) "T".
(* iModIntro. *)
iExists γeh, γtok, γR, γV, γe_nums. iFrame. unfold GE_minus, ReclaimInfo.
rewrite !big_sepL_nil !sids_to_0 !sids_from_0 !sids_from'_0 !dom_empty_L gset_to_coPset_empty !difference_empty_L.
iFrame.
do 2 (iMod ghost_vars2_get_empty_2 as "$").
iExists ∅. rewrite gset_to_coPset_empty !difference_empty_L.
do 2 (iMod ghost_vars2_get_empty_1 as "$").
iApply token2_get_empty_1.
Qed.
Lemma ghost_epoch_informations_new_slot slot γV γeh γe_nums ge slist sbvmap
(idx := length slist) :
(3 ≤ ge) →
sbvmap !! slot = None →
GhostEpochInformations γV γeh γe_nums ge slist sbvmap -∗
GhostEpochInformations γV γeh γe_nums ge (slist ++ [slot]) (<[slot:=(true, None)]> sbvmap) ∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2 } false.
Proof.
iIntros (LE3 Fresh) "EInfo".
iDestruct "EInfo" as "(GE_2_et_al & GE_1 & GE_0 & #slist & γV_to & γV_from)".
unfold GhostEpochInformations.
rewrite !length_app !Nat.add_1_r.
iEval (rewrite (sids_from_S (length slist))) in "γV_from".
iEval (rewrite -(sids_to_sids_from_union (S ge))).
rewrite -ghost_vars2_union_2; [|apply sids_from_sid_disjoint; lia].
rewrite -ghost_vars2_union_1; [|apply sids_to_sids_from_disjoint].
iDestruct "γV_from" as "[$ [γV_from $]]".
iEval (rewrite sids_to_S).
rewrite -ghost_vars2_union_2; [|apply sids_to_sid_disjoint;lia].
iFrame.
iAssert (GE_minus_2_et_al γV γeh ge (slist ++ [slot])
∗ (sids_to (ge -1),{[sid idx]}) ↦P2[γV]{ 1/2 } false)%I
with "[GE_2_et_al]" as "[$ γV_2_et_al]".
{ iDestruct "GE_2_et_al" as "(γV_from & γV_to & $)".
rewrite !length_app !Nat.add_1_r !(sids_from_S (length slist)).
rewrite -ghost_vars2_union_2; [|apply sids_from_sid_disjoint; lia].
iDestruct "γV_from" as "[$ [γV $]]".
iCombine "γV_to γV" as "γV_to".
rewrite ghost_vars2_union_2; [|apply sids_to_sid_disjoint; lia].
rewrite sids_to_S. iFrame.
}
iAssert(∀ e,
GE_minus γV γeh γe_nums e slist sbvmap -∗
GE_minus γV γeh γe_nums e (slist ++ [slot]) (<[slot:=(true, None)]> sbvmap)
∗ (sid e,sid idx) ↦p2[γV]{ 1/2 } false)%I
as "GE_update".
{ iIntros (e) "GE_minus". unfold GE_minus.
iDestruct "GE_minus" as "(γV & ◯ehist & slist_info)".
rewrite length_app Nat.add_1_r (sids_from_S (length slist)).
rewrite -ghost_vars2_union_2; [|apply sids_from_sid_disjoint; lia].
iDestruct "γV" as "[$ [γV $]]".
rewrite (sids_from'_S (length slist)).
rewrite -epoch_history_frag_union; [|apply sids_from'_sid_disjoint; lia].
iDestruct "◯ehist" as "[$ ◯ehist]".
rewrite big_sepL_snoc. iSplitL "slist_info".
- iApply (big_sepL_mono with "slist_info").
iIntros (si' slot' Hsi') "slot'".
iDestruct "slot'" as (b v V) "(% & ? & ? & ?)".
iExists b,v,V. iFrame. iPureIntro.
rewrite lookup_insert_ne; [done|intros <-; congruence].
- iExists true,None,false. iFrame. iPureIntro.
split; [by simplify_map_eq|inversion 1].
}
iDestruct ("GE_update" with "GE_1") as "[$ γV_1]".
iDestruct ("GE_update" with "GE_0") as "[$ γV_0]".
iCombine "γV_2_et_al γV_1" as "γV".
iEval (rewrite ghost_vars2_union_1; [|apply sids_to_sid_disjoint; lia]) in "γV".
rewrite -sids_to_S. assert (S (ge -1) = ge) as -> by lia.
iCombine "γV γV_0" as "γV".
iEval (rewrite ghost_vars2_union_1; [|apply sids_to_sid_disjoint; lia]) in "γV".
rewrite -sids_to_S. iFrame.
rewrite big_sepL_snoc. iSplit.
- iApply (big_sepL_mono with "slist").
iIntros (si slot' Hsi) "%Hslot'".
destruct Hslot' as (b & v & Hslot'). iExists b,v. iPureIntro.
rewrite lookup_insert_ne; [done|intros <-; congruence].
- iExists true,None. iPureIntro. by simplify_map_eq.
Qed.
Lemma ghost_epoch_informations_set idx slot v b v' b' γeh γtok γV γe_nums ge slist sbvmap :
NoDup slist →
slist !! idx = Some slot →
sbvmap !! slot = Some (b',v') →
GhostEpochInformations γV γeh γe_nums ge slist sbvmap -∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2/2 } false -∗
GhostEpochInformations γV γeh γe_nums ge slist (<[slot:=(b,v)]> sbvmap) ∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2/2 } false.
Proof.
iIntros (ND Hidx Hslot) "GEI ●VTI".
iDestruct "GEI" as "(GE2 & GE1 & GE0 & #sbvmap & γV_to & γV_from)".
iAssert (∀ e,
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2/2 } false -∗
GE_minus γV γeh γe_nums e slist sbvmap -∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2/2 } false ∗
GE_minus γV γeh γe_nums e slist (<[slot:=(b, v)]> sbvmap)
)%I as "GEupd".
{ iIntros (e) "●VTI GE".
iDestruct "GE" as "(●V1E & ◯H1E & GE1)".
iDestruct (big_sepL_delete with "GE1") as "[Gi GE1]"; eauto.
iDestruct "Gi" as (bi vi Vi) "(%Hi' & ●V1I & %HV & Ho)".
rewrite Hslot in Hi'. injection Hi' as [= <- <-].
iDestruct (ghost_vars2_agree with "●VTI ●V1I") as %<-; [set_solver..|].
iFrame.
iApply big_sepL_delete; eauto.
iSplitL "●V1I Ho".
{ iExists b,v,false. iFrame. iPureIntro.
split; [by rewrite lookup_insert|inversion 1]. }
iApply (big_sepL_mono with "GE1"); last auto.
iIntros (i l Hi) "GEi".
case_decide as EQi; auto.
iDestruct "GEi" as (bi vi Vi) "(%Hl & ●Vi & %HVi & Ho)".
iFrame. repeat iExists _. iPureIntro. split; [|done].
rewrite lookup_insert_ne; auto. intros ->.
eapply EQi, NoDup_lookup; eauto. }
iDestruct ("GEupd" with "●VTI GE1") as "[●VTI GE1]".
iDestruct ("GEupd" with "●VTI GE0") as "[●VTI GE0]".
iFrame.
iApply (big_sepL_mono with "sbvmap"); last auto.
iIntros (si' slot') "%Hslist %Hsi'".
destruct (decide (slot' = slot)) as [->|NE].
- repeat iExists _. iPureIntro. by simplify_map_eq.
- destruct Hsi' as (? & ? & ?). iExists _,_. iPureIntro.
rewrite lookup_insert_ne; [by simplify_map_eq|done].
Qed.
Lemma ghost_epoch_informations_slot_infos_activate idx slot γeh γtok γV γe_nums slist sbvmap ehist
ehistF (finalized := gset_to_coPset (fold_hist ehistF)) (ge := length ehist - 1) :
3 ≤ ge →
NoDup slist →
slist !! idx = Some slot →
length ehistF = ge - 1 →
sbvmap !! slot = Some (true, Some ge) →
ehistF `prefix_of` (take (length ehist - 2) ehist) →
GhostEpochInformations γV γeh γe_nums ge slist sbvmap -∗
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist -∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2/2 } false -∗
epoch_history_finalized γeh ehistF
==∗
GhostEpochInformations γV γeh γe_nums ge slist sbvmap ∗
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist ∗
(⊤ ∖ {[sid ge]},{[sid idx]}) ↦P2[γV]{ 1/2/2 } false ∗
(sid ge,sid idx) ↦p2[γV]{ 1/2/2 } true ∗
epoch_history_frag γeh ge {[sid idx]} ∗
(* (1/2) for GEI. (1/2/2) for SlotInfos. This is for Guard *)
mono_nats_auth γe_nums {[sid idx]} (1/2/2) ge ∗
toks γtok (⊤ ∖ finalized) {[sid idx]}.
Proof.
iIntros (LE_ge ND Hidx HlenF Hslot PF) "GEI SI ●VTI Fin".
iDestruct "GEI" as "(GE2 & GE1 & GE0 & #sbvmap & γV_to & γV_from)".
iDestruct "GE0" as "(●V0E & ◯H0E & GE0)".
iDestruct "SI" as "(◯LT & ●LE & TTE & SI)".
iDestruct (big_sepL_delete with "GE0") as "[Gi GE0]"; eauto.
iDestruct "Gi" as (bi vi Vi) "(%Hsl' & ●V0I & _ & T)".
rewrite Hslot in Hsl'. injection Hsl' as [= <- <-].
iDestruct (big_sepL_delete with "SI") as "[Si SI]"; eauto.
rewrite Hslot.
iDestruct "Si" as (Vi') "(●VTxI & ●V0I' & T')".
set reclaimable := fold_hist (take (ge-2) ehist).
(* update ghost *)
iDestruct (ghost_vars2_agree with "●VTI ●V0I") as %<-; [set_solver..|].
iDestruct (ghost_vars2_agree with "●V0I ●V0I'") as %<-; [set_solver..|].
iEval (rewrite (top_union_difference {[sid ge]})) in "●VTI".
rewrite -ghost_vars2_union_1; last set_solver.
iDestruct "●VTI" as "[●VTxI' ●V0I'']".
iCombine "●V0I' ●V0I''" as "●V0I'".
iRename "T" into "H0I".
iDestruct "T'" as "[TTI ●LI]".
iMod (ghost_vars2_update_halves' true with "●V0I ●V0I'") as "[●V0I ●V0I']".
(* redistribute *)
iDestruct "●V0I'" as "[●V0I' ●V0I'']".
iDestruct "●LI" as "[●LI [●LI' ●LI'']]".
assert ((fold_hist ehistF ∖ reclaimable) ## reclaimable).
{ apply disjoint_difference_l1. set_solver. }
iEval (rewrite (gset_to_coPset_top_difference reclaimable (fold_hist ehistF ∖ reclaimable)); last done) in "TTI".
rewrite -toks_union_1; last first.
{ apply gset_to_coPset_top_disjoint. }
iDestruct "TTI" as "[TEI TFI]".
(* close *)
iModIntro. iFrame.
iSplitL "GE0 ●V0I ●LI".
{ iFrame "∗#%". iApply big_sepL_delete; eauto. iFrame.
repeat (iExists _). iSplit; auto with iFrame. }
iSplitR "TEI"; last first.
{ rewrite -union_comm_L -union_difference_L; first done.
apply prefix_cut in PF. rewrite drop_ge in PF; last first.
{ rewrite length_take. lia. }
rewrite app_nil_r in PF. rewrite -PF.
apply fold_hist_prefix, take_prefix_le. lia.
}
iApply big_sepL_delete; eauto. iFrame.
rewrite Hslot. iExists _. iFrame. unfold reclaimable, ge.
rewrite gset_to_coPset_difference. iFrame. eauto.
Qed.
Lemma ghost_epoch_informations_slot_infos_deactivate
idx slot γeh γtok γV γR γe_nums slist sbvmap ehist ehistF e
(finalized := gset_to_coPset (fold_hist ehistF))
(ge := length ehist - 1) :
e ≤ ge →
NoDup slist →
slist !! idx = Some slot →
length ehistF = e - 1 →
sbvmap !! slot = Some (true, Some e) →
ehistF `prefix_of` ehist →
GhostEpochInformations γV γeh γe_nums ge slist sbvmap -∗
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist -∗
(⊤ ∖ {[sid e]},{[sid idx]}) ↦P2[γV]{ 1/2/2 } false -∗
(sid e,sid idx) ↦p2[γV]{ 1/2/2 } true -∗
epoch_history_frag γeh e {[sid idx]} -∗
(* (1/2) for GEI. (1/2/2) for SlotInfos. This is for Guard *)
mono_nats_auth γe_nums {[sid idx]} (1/2/2) e -∗
toks γtok (⊤ ∖ finalized) {[sid idx]} -∗
epoch_history_finalized γeh ehistF
==∗
GhostEpochInformations γV γeh γe_nums ge slist sbvmap ∗
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist ∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2/2 } false
.
Proof.
iIntros (Hle ND Hidx HlenF Hslot Hpref)
"GEI SI ●VTxI ●V0I F ●MI TTxI #Fin".
iDestruct "GEI" as "(GE2 & GE1 & GE0 & #sbvmap & GEI_REST)".
iDestruct "SI" as "(#◯MT & ●ME & TTE & SI)".
iDestruct (mono_nats_auth_lb_valid with "●MI ◯MT") as %Hge; first set_solver.
iDestruct (big_sepL_delete with "SI") as "[Si SI]"; eauto.
rewrite Hslot.
iDestruct "Si" as (Vi') "(●VTxI' & ●V0I' & T')".
iCombine "●ME TTE ●VTxI' SI" as "SI_REST".
(* update ghost *)
(* we need γV {[sid e]} {[sid idx]} 1.
1/4 is from ●V0I
1/4 is from ●V0I'
1/2 is from GE1 or GE0, whichever matches e
*)
iDestruct (ghost_vars2_agree with "●V0I ●V0I'") as %<-; try set_solver.
iCombine "●V0I ●V0I'" as "●V0I".
remember ((ge-1)+ge-e) as e'.
assert ((e = ge-1 ∧ e' = ge) ∨ (e = ge ∧ e' = ge-1)) as He by lia.
iAssert (
GE_minus γV γeh γe_nums e slist sbvmap ∗
GE_minus γV γeh γe_nums e' slist sbvmap
)%I with "[GE1 GE0]" as "[GEe GEe']".
{ destruct He as [[He He']|[He He']]. all: rewrite He He'; iFrame. }
iDestruct "GEe" as "(●VEI & FeE & GEe)".
iDestruct (big_sepL_delete with "GEe") as "[Gi GEe]"; eauto.
iDestruct "Gi" as (bi vi Vi) "(%Hslot' & ●V0I' & %HVi & ●MI')".
rewrite Hslot in Hslot'. injection Hslot' as [= <- <-].
iDestruct (ghost_vars2_agree with "●V0I ●V0I'") as %<-; [set_solver..|].
iMod (ghost_vars2_update_halves' false with "●V0I ●V0I'") as "[●V0I ●V0I']".
(* update mono nats *)
iDestruct "T'" as (ehistF') "(%HlenF' & #Fin' & TEEI & ●MI'')".
iDestruct (epoch_finalized_agree with "Fin Fin'") as %<-.
{ by rewrite HlenF HlenF'. }
iCombine "●MI' ●MI ●MI''" as "●MI".
iMod (mono_nats_auth_update ge with "●MI") as "[●MI #◯MI+]"; first lia.
(* redistribute *)
iDestruct "●V0I'" as "[●V0I' ●V0I'']".
iCombine "●VTxI ●V0I''" as "●VTI".
rewrite ghost_vars2_union_1; last set_solver.
rewrite -top_union_difference.
iAssert (
GE_minus γV γeh γe_nums ge slist sbvmap ∗
GE_minus γV γeh γe_nums (ge-1) slist sbvmap
)%I with "[GEe' ●VEI FeE ●V0I GEe F]" as "[GEe GEe']".
{ destruct He as [[He He']|[He He']]. all: rewrite He He'; iFrame.
all: iApply big_sepL_delete; eauto. all: iFrame.
all: repeat iExists _; iFrame. all: iSplit; auto.
}
iDestruct "SI_REST" as "(●ME & TTE & ●VTxI' & SI)".
iCombine "TTxI TEEI" as "TEI".
rewrite toks_union_1; last set_solver.
(* close *)
iModIntro. iFrame. fold ge. repeat iSplit; auto.
iApply big_sepL_delete; eauto. iFrame.
rewrite Hslot. iExists _. iFrame. iFrame.
rewrite difference_difference_r; [|set_solver|done]. unfold finalized.
rewrite -!gset_to_coPset_difference difference_diag_single; [done|].
apply prefix_cut in Hpref. rewrite Hpref.
apply fold_hist_prefix, take_app_prefix. lia.
Qed.
(** SlotInfos Lemmas *)
Lemma slot_infos_new_slot slot γV γtok γeh γe_nums slist sbvmap ge (idx := length slist) :
sbvmap !! slot = None →
SlotInfos γV γtok γeh γe_nums slist sbvmap ge -∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2 } false -∗
SlotInfos γV γtok γeh γe_nums (slist ++ [slot]) (<[slot:=(true,None)]> sbvmap) ge ∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2/2 } false.
Proof.
iIntros (Fresh) "SInfo [$ γV]".
iDestruct "SInfo" as "($ & ●γe_nums & ●toks & SInfo)".
rewrite (sids_from_S (length slist)) length_app Nat.add_1_r.
rewrite mono_nats_auth_union; [|apply sids_from_sid_disjoint; lia].
rewrite -toks_union_2; [|apply sids_from_sid_disjoint; lia].
iDestruct "●γe_nums" as "[$ ●γe_num]".
iDestruct "●toks" as "[$ ●tok]".
rewrite big_sepL_snoc. iSplitL "SInfo".
- iApply (big_sepL_mono with "SInfo"). iIntros (si slot' Hsi) "slot'".
destruct (sbvmap !! slot') as [e|] eqn:Hslot'; [|done].
rewrite lookup_insert_ne; [|intros ->; congruence].
rewrite Hslot'. iFrame.
- rewrite lookup_insert. iFrame.
Qed.
Lemma slot_infos_reactivate_slot slot γV γtok γeh γe_nums slist sbvmap si ge v':
NoDup slist →
slist !! si = Some slot →
sbvmap !! slot = Some (false, v') →
SlotInfos γV γtok γeh γe_nums slist sbvmap ge -∗
SlotInfos γV γtok γeh γe_nums slist (<[slot:= (true,None)]> sbvmap) ge ∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2/2 } false.
Proof.
iIntros (NoDup Hsi Hslot) "SInfo".
iDestruct "SInfo" as "($ & $ & $ & SInfo)".
iInduction slist as [|slot' slist IH] using rev_ind; [done|].
rewrite !big_sepL_snoc. iDestruct "SInfo" as "[SInfo slot']".
destruct (decide (slot' = slot)) as [->|NE].
{ iClear "IH". rewrite -assoc. iSplitL "SInfo".
- iApply (big_sepL_mono with "SInfo"). iIntros (si' slot' Hsi') "slot'".
rewrite lookup_insert_ne; [iFrame|].
intros <-. rewrite -NoDup_snoc in NoDup.
destruct NoDup as [NoDup NotIn]. apply NotIn.
rewrite elem_of_list_lookup. eauto.
- simplify_map_eq. assert (si = length slist) as ->.
{ eapply NoDup_lookup; [done..|]. apply snoc_lookup. }
iDestruct "slot'" as "($ & [$ $] & $)".
}
rewrite -NoDup_snoc in NoDup.
destruct NoDup as [NoDup NotIn].
assert (si < length slist) as LE.
{ rewrite Nat.lt_nge. intro le. apply NE.
assert (si = length slist) as ->.
{ apply lookup_lt_Some in Hsi. rewrite length_app Nat.add_1_r in Hsi. lia. }
rewrite snoc_lookup in Hsi. by injection Hsi. }
rewrite lookup_app_l in Hsi; [|done].
iSpecialize ("IH" with "[] [] [SInfo]"); [done..|].
iDestruct "IH" as "[$ $]".
rewrite lookup_insert_ne; [|done].
iFrame.
Qed.
(* Change the slot [si] to "NotValidated" state. *)
Lemma slot_infos_set v γV γtok γeh γe_nums slist sbvmap si slot ehist (ge := length ehist - 1) v' :
NoDup slist →
slist !! si = Some slot →
sbvmap !! slot = Some (true, v') →
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist -∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2/2 } false -∗
SlotInfos γV γtok γeh γe_nums slist (<[slot:=(true, v)]> sbvmap) ehist ∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2/2 } false.
Proof.
iIntros (ND Hi Hsl) "SI ●VTI".
iDestruct "SI" as "(◯MT & ●ME & TTE & SI)".
iDestruct (big_sepL_delete with "SI") as "[Si SI]"; eauto. rewrite Hsl.
set reclaimable := gset_to_coPset (fold_hist (take (ge-2) ehist)).
iAssert (
toks γtok (⊤ ∖ reclaimable) {[sid si]} ∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2/2 } false ∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2/2 } false ∗
mono_nats_auth γe_nums {[sid si]} 1 ge
)%I with "[Si ●VTI]" as "(TTI & ●VTI & ●VTI' & ●MI)".
{ destruct v'; last iFrame.
iDestruct "Si" as (Vi) "(●VTxI & ●VNI & Ho)".
iDestruct (ghost_vars2_agree with "●VTI ●VNI") as %<-; try set_solver.
iCombine "●VTxI ●VNI" as "●VTI'".
rewrite ghost_vars2_union_1; last set_solver.
rewrite -top_union_difference. iFrame. }
destruct v; iFrame.
- iApply big_sepL_delete; eauto. rewrite lookup_insert.
iSplitL "●VTI TTI ●MI".
{ iExists false. iFrame.
rewrite ghost_vars2_union_1; last set_solver.
by rewrite -top_union_difference. }
iApply big_sepL_mono; last auto.
iIntros (i p Hp) "SI". case_decide as Eqn; auto.
rewrite lookup_insert_ne; auto.
intro; subst. eapply Eqn, NoDup_lookup; eauto.
- iApply big_sepL_delete; eauto. rewrite lookup_insert. iFrame.
iApply big_sepL_mono; last auto.
iIntros (i p Hp) "SI". case_decide as Eqn; auto.
rewrite lookup_insert_ne; auto.
intro; subst. eapply Eqn, NoDup_lookup; eauto.
Qed.
Lemma slot_infos_unset v γV γtok γeh γe_nums slist sbvmap si slot ehist (ge := length ehist - 1) v' :
NoDup slist →
slist !! si = Some slot →
sbvmap !! slot = Some (true, v') →
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist -∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2/2 } false -∗
SlotInfos γV γtok γeh γe_nums slist (<[slot:=(false, v)]> sbvmap) ehist.
Proof.
iIntros (ND Hi Hsl) "SI ●VTI".
iDestruct "SI" as "(◯MT & ●ME & TTE & SI)".
iDestruct (big_sepL_delete with "SI") as "[Si SI]"; eauto. rewrite Hsl.
set reclaimable := gset_to_coPset (fold_hist (take (ge-2) ehist)).
iAssert (
toks γtok (⊤ ∖ reclaimable) {[sid si]} ∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2/2 } false ∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2/2 } false ∗
mono_nats_auth γe_nums {[sid si]} 1 ge
)%I with "[Si ●VTI]" as "(TTI & ●VTI & ●VTI' & ●MI)".
{ destruct v'; last iFrame.
iDestruct "Si" as (Vi) "(●VTxI & ●VNI & Ho)".
iDestruct (ghost_vars2_agree with "●VTI ●VNI") as %<-; try set_solver.
iCombine "●VTxI ●VNI" as "●VTI'".
rewrite ghost_vars2_union_1; last set_solver.
rewrite -top_union_difference. iFrame. }
iCombine "●VTI ●VTI'" as "●VTI". iFrame.
iApply big_sepL_delete; eauto. rewrite lookup_insert. iFrame.
iApply big_sepL_mono; last auto.
iIntros (i p Hp) "SI". case_decide as Eqn; auto.
rewrite lookup_insert_ne; auto.
intro; subst. eapply Eqn, NoDup_lookup; eauto.
Qed.
Lemma slot_infos_retire γV γtok γeh γe_nums slist sbvmap ehist i (ge := length ehist - 1) e:
ge - 1 ≤ e →
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist -∗
SlotInfos γV γtok γeh γe_nums slist sbvmap (alter (union {[i]}) e ehist).
Proof.
iIntros (e_GE_ge) "SInfo".
iDestruct "SInfo" as "(●γe_nums & ◯γe_nusm & toks & SInfo)".
set reclaimable := gset_to_coPset (fold_hist (take (ge - 2) ehist)).
unfold SlotInfos.
rewrite length_alter.
set reclaimable' := gset_to_coPset (fold_hist (take (ge - 2) (alter (union {[i]}) e ehist))).
assert (reclaimable' = reclaimable) as ->; last iFrame.
unfold reclaimable, reclaimable'. f_equal.
rewrite take_alter; [done|lia].
Qed.
Lemma slot_infos_get_inactive_lb slot γV γtok γeh γe_nums slist sbvmap ehist (ge := length ehist - 1) si v:
slist !! si = Some slot →
sbvmap !! slot = Some (false, v) →
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist -∗
mono_nats_lb γe_nums {[sid si]} ge.
Proof.
iIntros (Hsi HSlot).
iDestruct 1 as "(_ & _ & _ & SInfo)".
rewrite (big_sepL_delete); last done.
iDestruct "SInfo" as "[slot _]".
rewrite HSlot. iDestruct "slot" as "(_ & _ & ●γe_nums)".
iDestruct (mono_nats_lb_get with "●γe_nums") as "$".
Qed.
Lemma slot_infos_get_not_validated_lb slot γV γtok γeh γe_nums slist sbvmap ehist (ge := length ehist - 1) si b:
slist !! si = Some slot →
sbvmap !! slot = Some (b, None) →
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist -∗
mono_nats_lb γe_nums {[sid si]} ge.
Proof.
iIntros (Hsi HSlot).
iDestruct 1 as "(_ & _ & _ & SInfo)".
rewrite (big_sepL_delete); last done.
iDestruct "SInfo" as "[slot _]".
rewrite HSlot. destruct b.
all: iDestruct "slot" as "(_ & _ & ●γe_nums)".
all: iDestruct (mono_nats_lb_get with "●γe_nums") as "$".
Qed.
Lemma slot_infos_get_possibly_validated_lb slot γV γtok γeh γe_nums slist sbvmap ehist (ge := length ehist - 1) si b e:
e ≤ ge →
slist !! si = Some slot →
sbvmap !! slot = Some (b, Some e) →
SlotInfos γV γtok γeh γe_nums slist sbvmap ehist -∗
mono_nats_lb γe_nums {[sid si]} e.
Proof.
iIntros (LE Hsi HSlot).
iDestruct 1 as "(_ & _ & _ & SInfo)".
rewrite (big_sepL_delete); last done.
iDestruct "SInfo" as "[slot _]".