barrier_client.v 2.5 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
12
13
  let: "b" := newbarrier #() in
  ("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

Robbert Krebbers's avatar
Robbert Krebbers committed
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
32
    iIntros "[#Hh Hrecv]". wp_lam. wp_let.
    wp_apply wait_spec; iFrame "Hrecv".
33
    iDestruct 1 as (f) "[Hy #Hf]".
Robbert Krebbers's avatar
Robbert Krebbers committed
34
    wp_seq. wp_load.
35
    iApply wp_wand_r; iSplitR; [iApply "Hf"|by iIntros (v) "_"].
Ralf Jung's avatar
Ralf Jung committed
36
37
  Qed.

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

59
60
Section ClosedProofs.

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

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

69
End ClosedProofs.
70
71

Print Assumptions client_adequate.