atomic_sync.v 4.38 KB
Newer Older
Zhen Zhang's avatar
Zhen Zhang committed
1
From iris.program_logic Require Export weakestpre hoare.
Zhen Zhang's avatar
Zhen Zhang committed
2
From iris.heap_lang Require Export lang proofmode notation.
Zhen Zhang's avatar
Zhen Zhang committed
3 4
From iris.heap_lang.lib Require Import spin_lock.
From iris.algebra Require Import dec_agree frac.
Zhen Zhang's avatar
Zhen Zhang committed
5
From iris_atomic Require Import atomic sync misc.
Zhen Zhang's avatar
Zhen Zhang committed
6

Zhen Zhang's avatar
Zhen Zhang committed
7
Definition syncR := prodR fracR (dec_agreeR val). (* track the local knowledge of ghost state *)
Zhen Zhang's avatar
Zhen Zhang committed
8 9 10 11 12 13 14
Class syncG Σ := sync_tokG :> inG Σ syncR.
Definition syncΣ : gFunctors := #[GFunctor (constRF syncR)].

Instance subG_syncΣ {Σ} : subG syncΣ Σ  syncG Σ.
Proof. by intros ?%subG_inG. Qed.

Section atomic_sync.
15
  Context `{EqDecision A, !heapG Σ, !lockG Σ, !inG Σ (prodR fracR (dec_agreeR A))} (N : namespace).
Zhen Zhang's avatar
Zhen Zhang committed
16 17 18 19 20 21 22 23

  Definition gHalf (γ: gname) g : iProp Σ := own γ ((1/2)%Qp, DecAgree g).

  Definition atomic_triple'
             (α: val  iProp Σ)
             (β: val  A  A  val  iProp Σ)
             (Ei Eo: coPset)
             (f x: val) γ : iProp Σ :=
24 25 26 27
    ( P Q, atomic_triple_base A (fun g => gHalf γ g   α x)
                                 (fun g v =>  g':A, gHalf γ g'  β x g g' v)
                                 Ei Eo
                                (f x) (P x) (fun _ => Q x))%I.
28

29
  Definition sync (mk_syncer: val) : val :=
30
    λ: "f_seq" "l",
31 32
       let: "s" := mk_syncer #() in
       "s" ("f_seq" "l").
Zhen Zhang's avatar
Zhen Zhang committed
33 34 35 36 37 38

  Definition seq_spec (f: val) (ϕ: val  A  iProp Σ) α β (E: coPset) :=
       (Φ: val  iProp Σ) (l: val),
         {{ True }} f l {{ f',  ( (x: val) (Φ: val  iProp Σ) (g: A),
                               heapN  N 
                               heap_ctx  ϕ l g   α x 
39 40
                               ( (v: val) (g': A),
                                  ϕ l g' - β x g g' v ={E}= Φ v)
Zhen Zhang's avatar
Zhen Zhang committed
41
                                WP f' x @ E {{ Φ }} )}}.
Zhen Zhang's avatar
Zhen Zhang committed
42
  (* The linear view shift in the above post-condition is for the final step
43 44
     of computation. The client side of such triple will have to prove that the
     specific post-condition he wants can be lvs'd from whatever threaded together
Zhen Zhang's avatar
Zhen Zhang committed
45 46
     by magic wands. The library side, when proving seq_spec, will always have
     a view shift at the end of evalutation, which is exactly what we need.  *)
47

48
  Lemma atomic_spec (mk_syncer f_seq l: val) (ϕ: val  A  iProp Σ) α β Ei:
Zhen Zhang's avatar
Zhen Zhang committed
49
       (g0: A),
Zhen Zhang's avatar
Zhen Zhang committed
50
        heapN  N  seq_spec f_seq ϕ α β  
Zhen Zhang's avatar
Zhen Zhang committed
51
        mk_syncer_spec N mk_syncer 
Zhen Zhang's avatar
Zhen Zhang committed
52
        heap_ctx  ϕ l g0
53
         WP (sync mk_syncer) f_seq l {{ f,  γ, gHalf γ g0   x,  atomic_triple' α β Ei  f x γ  }}.
Zhen Zhang's avatar
Zhen Zhang committed
54
  Proof.
Zhen Zhang's avatar
Zhen Zhang committed
55 56 57 58
    iIntros (g0 HN Hseq Hsync) "[#Hh Hϕ]".
    iVs (own_alloc (((1 / 2)%Qp, DecAgree g0)  ((1 / 2)%Qp, DecAgree g0))) as (γ) "[Hg1 Hg2]".
    { by rewrite pair_op dec_agree_idemp. }
    repeat wp_let. wp_bind (mk_syncer _).
59
    iApply (Hsync ( g: A, ϕ l g  gHalf γ g)%I)=>//. iFrame "Hh".
Zhen Zhang's avatar
Zhen Zhang committed
60
    iSplitL "Hg1 Hϕ".
Zhen Zhang's avatar
Zhen Zhang committed
61
    { iExists g0. by iFrame. }
62 63 64 65 66 67 68
    iIntros (s) "#Hsyncer".
    wp_let. wp_bind (f_seq _). iApply wp_wand_r.
    iSplitR; first by iApply (Hseq with "[]")=>//.
    iIntros (f) "%".
    iApply wp_wand_r.
    iSplitR; first iApply "Hsyncer".
    iIntros (f') "#Hsynced".
Zhen Zhang's avatar
Zhen Zhang committed
69 70 71
    iExists γ. iFrame.
    iIntros (x). iAlways.
    iIntros (P Q) "#Hvss".
72
    iSpecialize ("Hsynced" $! P Q x).
73 74 75
    iIntros "!# HP". iApply wp_wand_r. iSplitL "HP".
    - iApply ("Hsynced" with "[]")=>//.
      iAlways. iIntros "[HR HP]". iDestruct "HR" as (g) "[Hϕ Hg1]".
76
      (* we should view shift at this point *)
77 78 79 80 81 82 83 84 85 86 87 88
      iDestruct ("Hvss" with "HP") as "Hvss'". iApply pvs_wp.
      iVs "Hvss'". iDestruct "Hvss'" as (?) "[[Hg2 #Hα] [Hvs1 _]]".
      iVs ("Hvs1" with "[Hg2]") as "HP"; first by iFrame.
      iVsIntro. iApply H=>//.
      iFrame "Hh Hϕ". iSplitR; first done. iIntros (ret g') "Hϕ' Hβ".
      iVs ("Hvss" with "HP") as (g'') "[[Hg'' _] [_ Hvs2]]".
      iSpecialize ("Hvs2" $! ret).
      iDestruct (m_frag_agree' with "[Hg'' Hg1]") as "[Hg %]"; first iFrame. subst.
      rewrite Qp_div_2.
      iAssert (|=r=> own γ (((1 / 2)%Qp, DecAgree g')  ((1 / 2)%Qp, DecAgree g')))%I
              with "[Hg]" as "==>[Hg1 Hg2]".
      { iApply own_update; last by iAssumption.
Zhen Zhang's avatar
Zhen Zhang committed
89
        apply cmra_update_exclusive. by rewrite pair_op dec_agree_idemp. }
90 91
      iVs ("Hvs2" with "[Hg1 Hβ]").
      { iExists g'. iFrame. }
Zhen Zhang's avatar
Zhen Zhang committed
92 93
      iVsIntro. iSplitL "Hg2 Hϕ'"; last done.
      iExists g'. by iFrame.
94
    - iIntros (?) "?". by iExists g0.
Zhen Zhang's avatar
Zhen Zhang committed
95
  Qed.
96

Zhen Zhang's avatar
Zhen Zhang committed
97
End atomic_sync.