lock.v 3.1 KB
Newer Older
1 2 3 4 5 6 7
From iris.program_logic Require Export global_functor.
From iris.proofmode Require Import invariants ghost_ownership.
From iris.heap_lang Require Import proofmode notation.
Import uPred.

Definition newlock : val := λ: <>, ref #false.
Definition acquire : val :=
Robbert Krebbers's avatar
Robbert Krebbers committed
8
  rec: "acquire" "l" :=
9 10
    if: CAS "l" #false #true then #() else "acquire" "l".
Definition release : val := λ: "l", "l" <- #false.
11
Global Opaque newlock acquire release.
12 13 14

(** The CMRA we need. *)
(* Not bundling heapG, as it may be shared with other users. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
15
Class lockG Σ := LockG { lock_tokG :> inG heap_lang Σ (exclR unitC) }.
16 17 18 19 20
Definition lockGF : gFunctorList := [GFunctor (constRF (exclR unitC))].
Instance inGF_lockG `{H : inGFs heap_lang Σ lockGF} : lockG Σ.
Proof. destruct H. split. apply: inGF_inG. Qed.

Section proof.
21
Context `{!heapG Σ, !lockG Σ} (N : namespace).
22 23 24 25 26 27
Local Notation iProp := (iPropG heap_lang Σ).

Definition lock_inv (γ : gname) (l : loc) (R : iProp) : iProp :=
  ( b : bool, l  #b  if b then True else own γ (Excl ())  R)%I.

Definition is_lock (l : loc) (R : iProp) : iProp :=
28
  ( γ, heapN  N  heap_ctx  inv N (lock_inv γ l R))%I.
29 30

Definition locked (l : loc) (R : iProp) : iProp :=
31 32
  ( γ, heapN  N  heap_ctx 
        inv N (lock_inv γ l R)  own γ (Excl ()))%I.
33 34 35 36 37 38 39 40 41 42 43 44

Global Instance lock_inv_ne n γ l : Proper (dist n ==> dist n) (lock_inv γ l).
Proof. solve_proper. Qed.
Global Instance is_lock_ne n l : Proper (dist n ==> dist n) (is_lock l).
Proof. solve_proper. Qed.
Global Instance locked_ne n l : Proper (dist n ==> dist n) (locked l).
Proof. solve_proper. Qed.

(** The main proofs. *)
Global Instance is_lock_persistent l R : PersistentP (is_lock l R).
Proof. apply _. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
45
Lemma locked_is_lock l R : locked l R  is_lock l R.
46
Proof. rewrite /is_lock. iDestruct 1 as (γ) "(?&?&?&_)"; eauto. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
47

48
Lemma newlock_spec (R : iProp) Φ :
49
  heapN  N 
50
  heap_ctx  R  ( l, is_lock l R - Φ #l)  WP newlock #() {{ Φ }}.
51
Proof.
52
  iIntros (?) "(#Hh & HR & HΦ)". rewrite /newlock.
53
  wp_seq. wp_alloc l as "Hl".
54
  iPvs (own_alloc (Excl ())) as (γ) "Hγ"; first done.
55
  iPvs (inv_alloc N _ (lock_inv γ l R) with "[-HΦ]") as "#?"; first done.
56
  { iIntros ">". iExists false. by iFrame. }
57
  iPvsIntro. iApply "HΦ". iExists γ; eauto.
58 59 60
Qed.

Lemma acquire_spec l R (Φ : val  iProp) :
61
  is_lock l R  (locked l R - R - Φ #())  WP acquire #l {{ Φ }}.
62
Proof.
63
  iIntros "[Hl HΦ]". iDestruct "Hl" as (γ) "(%&#?&#?)".
64
  iLöb as "IH". wp_rec. wp_focus (CAS _ _ _)%E.
65
  iInv N as ([]) "[Hl HR]".
66
  - wp_cas_fail. iPvsIntro; iSplitL "Hl".
67
    + iNext. iExists true; eauto.
68
    + wp_if. by iApply "IH".
69
  - wp_cas_suc. iPvsIntro. iDestruct "HR" as "[Hγ HR]". iSplitL "Hl".
70
    + iNext. iExists true; eauto.
71
    + wp_if. iApply ("HΦ" with "[-HR] HR"). iExists γ; eauto.
72 73 74
Qed.

Lemma release_spec R l (Φ : val  iProp) :
75
  locked l R  R  Φ #()  WP release #l {{ Φ }}.
76
Proof.
77
  iIntros "(Hl&HR&HΦ)"; iDestruct "Hl" as (γ) "(% & #? & #? & Hγ)".
78
  rewrite /release. wp_let. iInv N as (b) "[Hl _]".
79
  wp_store. iPvsIntro. iFrame "HΦ". iNext. iExists false. by iFrame.
80 81
Qed.
End proof.