thread_local.v 3.42 KB
Newer Older
1
From iris.base_logic.lib Require Export invariants.
2
From iris.algebra Require Export gmap gset coPset.
3
From iris.proofmode Require Import tactics.
4 5
Import uPred.

6
Definition thread_id := gname.
7

8
Class thread_localG Σ :=
9
  tl_inG :> inG Σ (prodR coPset_disjR (gset_disjR positive)).
10 11

Section defs.
12
  Context `{invG Σ, thread_localG Σ}.
13

14
  Definition tl_own (tid : thread_id) (E : coPset) : iProp Σ :=
15
    own tid (CoPset E, ).
16 17

  Definition tl_inv (tid : thread_id) (N : namespace) (P : iProp Σ) : iProp Σ :=
18
    ( i, i  N 
19
          inv N (P  own tid (, GSet {[i]})  tl_own tid {[i]}))%I.
20 21
End defs.

22
Instance: Params (@tl_inv) 3.
23
Typeclasses Opaque tl_own tl_inv.
24 25

Section proofs.
26
  Context `{invG Σ, thread_localG Σ}.
27

28 29 30 31 32 33 34 35 36 37 38
  Global Instance tl_own_timeless tid E : TimelessP (tl_own tid E).
  Proof. rewrite /tl_own; apply _. Qed.

  Global Instance tl_inv_ne tid N n : Proper (dist n ==> dist n) (tl_inv tid N).
  Proof. rewrite /tl_inv. solve_proper. Qed.
  Global Instance tl_inv_proper tid N : Proper (() ==> ()) (tl_inv tid N).
  Proof. apply (ne_proper _). Qed.

  Global Instance tl_inv_persistent tid N P : PersistentP (tl_inv tid N P).
  Proof. rewrite /tl_inv; apply _. Qed.

39
  Lemma tl_alloc : (|==>  tid, tl_own tid )%I.
40
  Proof. by apply own_alloc. Qed.
41

Robbert Krebbers's avatar
Robbert Krebbers committed
42
  Lemma tl_own_disjoint tid E1 E2 : tl_own tid E1 - tl_own tid E2 - E1  E2.
43
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
44
    apply wand_intro_r.
45
    rewrite /tl_own -own_op own_valid -coPset_disj_valid_op. by iIntros ([? _]).
46 47
  Qed.

48
  Lemma tl_own_union tid E1 E2 :
49
    E1  E2  tl_own tid (E1  E2)  tl_own tid E1  tl_own tid E2.
50
  Proof.
51
    intros ?. by rewrite /tl_own -own_op pair_op left_id coPset_disj_union.
52 53
  Qed.

54
  Lemma tl_inv_alloc tid E N P :  P ={E}= tl_inv tid N P.
55 56
  Proof.
    iIntros "HP".
57
    iMod (own_empty (prodUR coPset_disjUR (gset_disjUR positive)) tid) as "Hempty".
58
    iMod (own_updateP with "Hempty") as ([m1 m2]) "[Hm Hown]".
59
    { apply prod_updateP'. apply cmra_updateP_id, (reflexivity (R:=eq)).
60 61
      apply (gset_disj_alloc_empty_updateP_strong' (λ i, i  N)).
      intros Ef. exists (coPpick ( N  coPset.of_gset Ef)).
62 63 64 65
      rewrite -coPset.elem_of_of_gset comm -elem_of_difference.
      apply coPpick_elem_of=> Hfin.
      eapply nclose_infinite, (difference_finite_inv _ _), Hfin.
      apply of_gset_finite. }
66
    simpl. iDestruct "Hm" as %(<- & i & -> & ?).
67
    rewrite /tl_inv.
68
    iMod (inv_alloc N with "[-]"); last (iModIntro; iExists i; eauto).
69 70 71
    iNext. iLeft. by iFrame.
  Qed.

72 73 74 75
  Lemma tl_inv_open tid E N P :
    N  E 
    tl_inv tid N P - tl_own tid E ={E}=  P  tl_own tid (E∖↑N) 
                       ( P  tl_own tid (E∖↑N) ={E}= tl_own tid E).
76
  Proof.
77
    rewrite /tl_inv. iIntros (?) "#Htlinv Htoks".
78
    iDestruct "Htlinv" as (i) "[% Hinv]".
79 80
    rewrite [E as X in tl_own tid X](union_difference_L (N) E) //.
    rewrite [X in (X  _)](union_difference_L {[i]} (N)) ?tl_own_union; [|set_solver..].
Robbert Krebbers's avatar
Robbert Krebbers committed
81
    iDestruct "Htoks" as "[[Htoki $] $]".
82
    iInv N as "[[$ >Hdis]|>Htoki2]" "Hclose".
83 84
    - iMod ("Hclose" with "[Htoki]") as "_"; first auto.
      iIntros "!> [HP $]".
85
      iInv N as "[[_ >Hdis2]|>Hitok]" "Hclose".
86
      + iDestruct (own_valid_2 with "Hdis Hdis2") as %[_ Hval%gset_disj_valid_op].
Robbert Krebbers's avatar
Tweak.  
Robbert Krebbers committed
87
        set_solver.
88
      + iFrame. iApply "Hclose". iNext. iLeft. by iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
89
    - iDestruct (tl_own_disjoint with "Htoki Htoki2") as %?. set_solver.
90
  Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
91
End proofs.