atomic_sync.v 4.43 KB
Newer Older
Zhen Zhang's avatar
Zhen Zhang committed
1
2
3
4
5
From iris.program_logic Require Export weakestpre hoare.
From iris.heap_lang Require Export lang.
From iris.heap_lang Require Import proofmode notation.
From iris.heap_lang.lib Require Import spin_lock.
From iris.algebra Require Import dec_agree frac.
Zhen Zhang's avatar
Zhen Zhang committed
6
From iris_atomic Require Import atomic sync misc.
Zhen Zhang's avatar
Zhen Zhang committed
7
8
9
10
11
12
13
14
15

Definition syncR := prodR fracR (dec_agreeR val).
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.
16
  Context `{EqDecision A, !heapG Σ, !lockG Σ, !inG Σ (prodR fracR (dec_agreeR A))} (N : namespace).
Zhen Zhang's avatar
Zhen Zhang committed
17
18
19
20
21
22
23
24

  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 Σ :=
25
26
27
28
    ( 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.
29

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

  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 
40
41
                               ( (v: val) (g': A),
                                  ϕ l g' - β x g g' v ={E}= Φ v)
Zhen Zhang's avatar
Zhen Zhang committed
42
                                WP f' x @ E {{ Φ }} )}}.
Zhen Zhang's avatar
Zhen Zhang committed
43
  (* XXX (zhen): The linear VS in the above post-condition is for the final step
44
45
     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
46
47
     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.  *)
48

49
  Lemma atomic_spec (mk_syncer f_seq l: val) (ϕ: val  A  iProp Σ) α β Ei:
Zhen Zhang's avatar
Zhen Zhang committed
50
       (g0: A),
Zhen Zhang's avatar
Zhen Zhang committed
51
        heapN  N  seq_spec f_seq ϕ α β  
Zhen Zhang's avatar
Zhen Zhang committed
52
        mk_syncer_spec N mk_syncer 
Zhen Zhang's avatar
Zhen Zhang committed
53
        heap_ctx  ϕ l g0
54
         WP (sync mk_syncer) f_seq l {{ f,  γ, gHalf γ g0   x,  atomic_triple' α β Ei  f x γ  }}.
Zhen Zhang's avatar
Zhen Zhang committed
55
  Proof.
Zhen Zhang's avatar
Zhen Zhang committed
56
57
58
59
    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 _).
60
    iApply (Hsync ( g: A, ϕ l g  gHalf γ g)%I)=>//. iFrame "Hh".
Zhen Zhang's avatar
Zhen Zhang committed
61
    iSplitL "Hg1 Hϕ".
Zhen Zhang's avatar
Zhen Zhang committed
62
    { iExists g0. by iFrame. }
63
64
65
66
67
68
69
    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
70
71
72
73
74
    iExists γ. iFrame.
    iIntros (x). iAlways.
    rewrite /atomic_triple'.
    iIntros (P Q) "#Hvss".
    rewrite /synced.
75
    iSpecialize ("Hsynced" $! P Q x).
76
77
78
    iIntros "!# HP". iApply wp_wand_r. iSplitL "HP".
    - iApply ("Hsynced" with "[]")=>//.
      iAlways. iIntros "[HR HP]". iDestruct "HR" as (g) "[Hϕ Hg1]".
79
      (* we should view shift at this point *)
80
81
82
83
84
85
86
87
88
89
90
91
      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
92
        apply cmra_update_exclusive. by rewrite pair_op dec_agree_idemp. }
93
94
95
96
97
98
      iVs ("Hvs2" with "[Hg1 Hβ]").
      { iExists g'. iFrame. }
      iVsIntro. iSplitL "Hg2 Hϕ'".
      * iExists g'. by iFrame.
      * done.
    - iIntros (?) "?". by iExists g0.
Zhen Zhang's avatar
Zhen Zhang committed
99
  Qed.
100

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