barrier_client.v 2.53 KB
Newer Older
1
2
From iris.program_logic Require Export weakestpre.
From iris.heap_lang Require Export lang.
Ralf Jung's avatar
Ralf Jung committed
3
From iris.heap_lang.lib.barrier Require Import proof.
4
From iris.heap_lang Require Import par.
5
From iris.heap_lang Require Import adequacy proofmode.
Ralf Jung's avatar
Ralf Jung committed
6

7
Definition worker (n : Z) : val :=
8
9
  λ: "b" "y", wait "b" ;; !"y" #n.
Definition client : expr :=
10
  let: "y" := ref #0 in
11
  let: "b" := newbarrier #() in
12
13
  ("y" <- (λ: "z", "z" + #42) ;; signal "b") |||
    (worker 12 "b" "y" ||| worker 17 "b" "y").
14
Global Opaque worker client.
Ralf Jung's avatar
Ralf Jung committed
15
16

Section client.
17
  Context `{!heapG Σ, !barrierG Σ, !spawnG Σ} (N : namespace).
Ralf Jung's avatar
Ralf Jung committed
18

19
  Definition y_inv (q : Qp) (l : loc) : iProp Σ :=
20
    ( f : val, l {q} f    n : Z, WP f #n {{ v, v = #(n + 42) }})%I.
21

22
  Lemma y_inv_split q l : y_inv q l  (y_inv (q/2) l  y_inv (q/2) l).
Ralf Jung's avatar
Ralf Jung committed
23
  Proof.
24
    iDestruct 1 as (f) "[[Hl1 Hl2] #Hf]".
Robbert Krebbers's avatar
Robbert Krebbers committed
25
    iSplitL "Hl1"; iExists f; by iSplitL; try iAlways.
Ralf Jung's avatar
Ralf Jung committed
26
27
28
  Qed.

  Lemma worker_safe q (n : Z) (b y : loc) :
29
    heap_ctx  recv N b (y_inv q y)  WP worker n #b #y {{ _, True }}.
Ralf Jung's avatar
Ralf Jung committed
30
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
31
    iIntros "[#Hh Hrecv]". wp_lam. wp_let.
32
    wp_apply (wait_spec with "[- $Hrecv]"). iDestruct 1 as (f) "[Hy #Hf]".
Robbert Krebbers's avatar
Robbert Krebbers committed
33
    wp_seq. wp_load.
34
    iApply wp_wand_r. iSplitR; [iApply "Hf"|by iIntros (v) "_"].
Ralf Jung's avatar
Ralf Jung committed
35
36
  Qed.

37
  Lemma client_safe : heapN  N  heap_ctx  WP client {{ _, True }}.
Ralf Jung's avatar
Ralf Jung committed
38
  Proof.
39
    iIntros (?) "#Hh"; rewrite /client. wp_alloc y as "Hy". wp_let.
Ralf Jung's avatar
Ralf Jung committed
40
    wp_apply (newbarrier_spec N (y_inv 1 y) with "Hh"); first done.
41
42
    iIntros (l) "[Hr Hs]". wp_let.
    iApply (wp_par (λ _, True%I) (λ _, True%I) with "[-$Hh]"). iSplitL "Hy Hs".
43
    - (* The original thread, the sender. *)
Ralf Jung's avatar
Ralf Jung committed
44
45
      wp_store. iApply (signal_spec with "[-]"); last by iNext; auto.
      iSplitR "Hy"; first by eauto.
46
      iExists _; iSplitL; [done|]. iAlways; iIntros (n). wp_let. by wp_op.
47
    - (* The two spawned threads, the waiters. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
48
      iSplitL; [|by iIntros (_ _) "_ !>"].
49
50
      iDestruct (recv_weaken with "[] Hr") as "Hr".
      { iIntros "Hy". by iApply (y_inv_split with "Hy"). }
51
      iMod (recv_split with "Hr") as "[H1 H2]"; first done.
52
      iApply (wp_par (λ _, True%I) (λ _, True%I) with "[- $Hh]").
Robbert Krebbers's avatar
Robbert Krebbers committed
53
      iSplitL "H1"; [|iSplitL "H2"; [|by iIntros (_ _) "_ !>"]];
Robbert Krebbers's avatar
Robbert Krebbers committed
54
        iApply worker_safe; by iSplit.
55
Qed.
Ralf Jung's avatar
Ralf Jung committed
56
End client.
57

58
59
Section ClosedProofs.

60
Let Σ : gFunctors := #[ heapΣ ; barrierΣ ; spawnΣ ].
61

62
63
Lemma client_adequate σ : adequate client σ (λ _, True).
Proof.
64
  apply (heap_adequacy Σ)=> ?.
65
66
  apply (client_safe (nroot .@ "barrier")); auto with ndisj.
Qed.
67

68
End ClosedProofs.
69
70

Print Assumptions client_adequate.