Commit 3f3ead35 authored by Zhen Zhang's avatar Zhen Zhang Committed by Zhen Zhang

Add lock interface

parent aa9b972e
......@@ -94,6 +94,7 @@ heap_lang/lib/spawn.v
heap_lang/lib/par.v
heap_lang/lib/assert.v
heap_lang/lib/lock.v
heap_lang/lib/spin_lock.v
heap_lang/lib/ticket_lock.v
heap_lang/lib/counter.v
heap_lang/lib/barrier/barrier.v
......
From iris.program_logic Require Export weakestpre.
From iris.heap_lang Require Export lang.
From iris.proofmode Require Import invariants tactics.
From iris.heap_lang Require Import proofmode notation.
From iris.algebra Require Import excl.
Definition newlock : val := λ: <>, ref #false.
Definition acquire : val :=
rec: "acquire" "l" :=
if: CAS "l" #false #true then #() else "acquire" "l".
Definition release : val := λ: "l", "l" <- #false.
Global Opaque newlock acquire release.
(** The CMRA we need. *)
(* Not bundling heapG, as it may be shared with other users. *)
Class lockG Σ := LockG { lock_tokG :> inG Σ (exclR unitC) }.
Definition lockΣ : gFunctors := #[GFunctor (constRF (exclR unitC))].
Instance subG_lockΣ {Σ} : subG lockΣ Σ lockG Σ.
Proof. intros [?%subG_inG _]%subG_inv. split; apply _. Qed.
Section proof.
Context `{!heapG Σ, !lockG Σ} (N : namespace).
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 Σ :=
( γ, heapN N heap_ctx inv N (lock_inv γ l R))%I.
Definition locked (l : loc) (R : iProp Σ) : iProp Σ :=
( γ, heapN N heap_ctx
inv N (lock_inv γ l R) own γ (Excl ()))%I.
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.
Lemma locked_is_lock l R : locked l R is_lock l R.
Proof. rewrite /is_lock. iDestruct 1 as (γ) "(?&?&?&_)"; eauto. Qed.
Lemma newlock_spec (R : iProp Σ) Φ :
heapN N
heap_ctx R ( l, is_lock l R - Φ #l) WP newlock #() {{ Φ }}.
Proof.
iIntros (?) "(#Hh & HR & HΦ)". rewrite /newlock.
wp_seq. wp_alloc l as "Hl".
iVs (own_alloc (Excl ())) as (γ) "Hγ"; first done.
iVs (inv_alloc N _ (lock_inv γ l R) with "[-HΦ]") as "#?".
{ iIntros "!>". iExists false. by iFrame. }
iVsIntro. iApply "HΦ". iExists γ; eauto.
Qed.
Lemma acquire_spec l R (Φ : val iProp Σ) :
is_lock l R (locked l R - R - Φ #()) WP acquire #l {{ Φ }}.
Proof.
iIntros "[Hl HΦ]". iDestruct "Hl" as (γ) "(%&#?&#?)".
iLöb as "IH". wp_rec. wp_bind (CAS _ _ _)%E.
iInv N as ([]) "[Hl HR]" "Hclose".
- wp_cas_fail. iVs ("Hclose" with "[Hl]"); first (iNext; iExists true; eauto).
iVsIntro. wp_if. by iApply "IH".
- wp_cas_suc. iDestruct "HR" as "[Hγ HR]".
iVs ("Hclose" with "[Hl]"); first (iNext; iExists true; eauto).
iVsIntro. wp_if. iApply ("HΦ" with "[-HR] HR"). iExists γ; eauto.
Qed.
Lemma release_spec R l (Φ : val iProp Σ) :
locked l R R Φ #() WP release #l {{ Φ }}.
Proof.
iIntros "(Hl&HR&HΦ)"; iDestruct "Hl" as (γ) "(% & #? & #? & Hγ)".
rewrite /release. wp_let. iInv N as (b) "[Hl _]" "Hclose".
wp_store. iFrame "HΦ". iApply "Hclose". iNext. iExists false. by iFrame.
Qed.
End proof.
(** Abstract Lock Interface **)
From iris.heap_lang Require Import heap notation.
Structure lock Σ `{!heapG Σ} :=
Lock {
(* operations *)
newlock : val;
acquire : val;
release : val;
(* predicates *)
is_lock (N: namespace) (lock: val) (R: iProp Σ) : iProp Σ;
locked (N: namespace) (lock: val) (R: iProp Σ) : iProp Σ;
(* general properties *)
is_lock_ne N lk n : Proper (dist n ==> dist n) (is_lock N lk);
locked_ne N lk n : Proper (dist n ==> dist n) (locked N lk);
is_lock_persistent N lk R : PersistentP (is_lock N lk R);
locked_is_lock N lk R : locked N lk R is_lock N lk R;
(* operation specs *)
newlock_spec N (R : iProp Σ) Φ :
heapN N
heap_ctx R ( l, is_lock N l R - Φ l) WP newlock #() {{ Φ }};
acquire_spec N lk R (Φ : val iProp Σ) :
heapN N
is_lock N lk R (locked N lk R - R - Φ #()) WP acquire lk {{ Φ }};
release_spec N R lk (Φ : val iProp Σ) :
heapN N
locked N lk R R Φ #() WP release lk {{ Φ }}
}.
Arguments newlock {_ _} _.
Arguments acquire {_ _} _.
Arguments release {_ _} _.
Arguments is_lock {_ _} _ _ _ _.
Arguments locked {_ _} _ _ _ _.
Existing Instances is_lock_ne locked_ne is_lock_persistent.
Instance is_lock_proper Σ `{!heapG Σ} (L: lock Σ) N lk :
Proper (() ==> ()) (is_lock L N lk) := ne_proper _.
Instance locked_proper `{!heapG Σ} (L : lock Σ) N lk:
Proper (() ==> ()) (locked L N lk) := ne_proper _.
From iris.program_logic Require Export weakestpre.
From iris.heap_lang Require Export lang.
From iris.proofmode Require Import invariants tactics.
From iris.heap_lang Require Import proofmode notation.
From iris.algebra Require Import excl.
From iris.heap_lang.lib Require Import lock.
Definition newlock : val := λ: <>, ref #false.
Definition acquire : val :=
rec: "acquire" "l" :=
if: CAS "l" #false #true then #() else "acquire" "l".
Definition release : val := λ: "l", "l" <- #false.
Global Opaque newlock acquire release.
(** The CMRA we need. *)
(* Not bundling heapG, as it may be shared with other users. *)
Class lockG Σ := LockG { lock_tokG :> inG Σ (exclR unitC) }.
Definition lockΣ : gFunctors := #[GFunctor (constRF (exclR unitC))].
Instance subG_lockΣ {Σ} : subG lockΣ Σ lockG Σ.
Proof. intros [?%subG_inG _]%subG_inv. split; apply _. Qed.
Section proof.
Context `{!heapG Σ, !lockG Σ} (N : namespace).
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 (lock : val) (R : iProp Σ) : iProp Σ :=
( γ (l: loc), heapN N heap_ctx lock = #l inv N (lock_inv γ l R))%I.
Definition locked (lock : val) (R : iProp Σ) : iProp Σ :=
( γ (l: loc), heapN N heap_ctx lock = #l
inv N (lock_inv γ l R) own γ (Excl ()))%I.
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.
Lemma locked_is_lock lk R : locked lk R is_lock lk R.
Proof. rewrite /is_lock. iDestruct 1 as (γ l) "(?&?&?&?&_)". iExists γ, l. auto. Qed.
Lemma newlock_spec (R : iProp Σ) Φ :
heapN N
heap_ctx R ( l, is_lock l R - Φ l) WP newlock #() {{ Φ }}.
Proof.
iIntros (HN) "(#Hh & HR & HΦ)". rewrite /newlock.
wp_seq. wp_alloc l as "Hl".
iVs (own_alloc (Excl ())) as (γ) "Hγ"; first done.
iVs (inv_alloc N _ (lock_inv γ l R) with "[-HΦ]") as "#?".
{ iIntros "!>". iExists false. by iFrame. }
iVsIntro. iApply "HΦ". iExists γ; eauto.
Qed.
Lemma acquire_spec lk R (Φ : val iProp Σ) :
heapN N
is_lock lk R (locked lk R - R - Φ #()) WP acquire lk {{ Φ }}.
Proof.
iIntros (HN) "[Hl HΦ]". iDestruct "Hl" as (γ l) "(% & #? & % & #?)". subst.
iLöb as "IH". wp_rec. wp_bind (CAS _ _ _)%E.
iInv N as ([]) "[Hlk HR]" "Hclose".
- wp_cas_fail. iVs ("Hclose" with "[Hlk]"); first (iNext; iExists true; eauto).
iVsIntro. wp_if. by iApply "IH".
- wp_cas_suc. iDestruct "HR" as "[Hγ HR]".
iVs ("Hclose" with "[Hlk]"); first (iNext; iExists true; eauto).
iVsIntro. wp_if. iApply ("HΦ" with "[-HR] HR"). iExists γ; iExists l. auto.
Qed.
Lemma release_spec R lk (Φ : val iProp Σ) :
heapN N
locked lk R R Φ #() WP release lk {{ Φ }}.
Proof.
iIntros (HN) "(Hl & HR & HΦ)"; iDestruct "Hl" as (γ l) "(% & #? & % & #? & Hγ)". subst.
rewrite /release. wp_let. iInv N as (b) "[Hlk _]" "Hclose".
wp_store. iFrame "HΦ". iApply "Hclose". iNext. iExists false. by iFrame.
Qed.
End proof.
(* impl *)
Definition spin_lock `{!heapG Σ} `{lockG Σ} :=
Lock _ _ newlock acquire release is_lock locked _ _ _ locked_is_lock newlock_spec acquire_spec release_spec.
......@@ -3,6 +3,7 @@ From iris.heap_lang Require Export lang.
From iris.program_logic Require Import auth.
From iris.proofmode Require Import invariants.
From iris.heap_lang Require Import proofmode notation.
From iris.heap_lang.lib Require Import lock.
From iris.algebra Require Import gset.
Import uPred.
......@@ -43,27 +44,34 @@ Instance subG_tlockΣ {Σ} : subG tlockΣ Σ → tlockG Σ.
Proof. intros [? [?%subG_inG _]%subG_inv]%subG_inv. split; apply _. Qed.
Section proof.
Context `{!heapG Σ, !tlockG Σ} (N : namespace) (HN: heapN N).
Context `{!heapG Σ, !tlockG Σ} (N : namespace).
Definition tickets_inv (n: nat) (gs: gset_disjUR nat) : iProp Σ :=
(gs = GSet (seq_set 0 n))%I.
Definition lock_inv (γ1 γ2: gname) (lo ln: loc) (R : iProp Σ) : iProp Σ :=
( (o n: nat),
lo #o ln #n
auth_inv γ1 (tickets_inv n)
((own γ2 (Excl ()) R) auth_own γ1 (GSet {[ o ]})))%I.
heapN N
lo #o ln #n
auth_inv γ1 (tickets_inv n)
((own γ2 (Excl ()) R) auth_own γ1 (GSet {[ o ]})))%I.
Definition is_lock (l: val) (R: iProp Σ) : iProp Σ :=
( γ1 γ2 (lo ln: loc), heap_ctx l = (#lo, #ln)%V inv N (lock_inv γ1 γ2 lo ln R))%I.
( γ1 γ2 (lo ln: loc),
heapN N heap_ctx
l = (#lo, #ln)%V inv N (lock_inv γ1 γ2 lo ln R))%I.
Definition issued (l : val) (x: nat) (R : iProp Σ) : iProp Σ :=
( γ1 γ2 (lo ln: loc), heap_ctx l = (#lo, #ln)%V inv N (lock_inv γ1 γ2 lo ln R)
auth_own γ1 (GSet {[ x ]}))%I.
( γ1 γ2 (lo ln: loc),
heapN N heap_ctx
l = (#lo, #ln)%V inv N (lock_inv γ1 γ2 lo ln R)
auth_own γ1 (GSet {[ x ]}))%I.
Definition locked (l : val) (R : iProp Σ) : iProp Σ :=
( γ1 γ2 (lo ln: loc), heap_ctx l = (#lo, #ln)%V inv N (lock_inv γ1 γ2 lo ln R)
own γ2 (Excl ()))%I.
( γ1 γ2 (lo ln: loc),
heapN N heap_ctx
l = (#lo, #ln)%V inv N (lock_inv γ1 γ2 lo ln R)
own γ2 (Excl ()))%I.
Global Instance lock_inv_ne n γ1 γ2 lo ln: Proper (dist n ==> dist n) (lock_inv γ1 γ2 lo ln).
Proof. solve_proper. Qed.
......@@ -75,16 +83,21 @@ Proof. solve_proper. Qed.
Global Instance is_lock_persistent l R : PersistentP (is_lock l R).
Proof. apply _. Qed.
Lemma locked_is_lock lk R : locked lk R is_lock lk R.
Proof. rewrite /is_lock. iDestruct 1 as (γ l lo ln) "(? & ? & ? & ? & ?)". iExists γ, l, lo, ln. auto. Qed.
Lemma newlock_spec (R : iProp Σ) Φ :
heapN N
heap_ctx R ( l, is_lock l R - Φ l) WP newlock #() {{ Φ }}.
Proof.
iIntros "(#Hh & HR & HΦ)". rewrite /newlock.
iIntros (HN) "(#Hh & HR & HΦ)". rewrite /newlock.
wp_seq. wp_alloc lo as "Hlo". wp_alloc ln as "Hln".
iVs (own_alloc (Excl ())) as (γ2) "Hγ2"; first done.
iVs (own_alloc_strong (Auth (Excl' ) ) {[ γ2 ]}) as (γ1) "[% Hγ1]"; first done.
iVs (inv_alloc N _ (lock_inv γ1 γ2 lo ln R) with "[-HΦ]").
{ iNext. rewrite /lock_inv.
iExists 0%nat, 0%nat.
iSplit; first by auto.
iFrame.
iSplitL "Hγ1".
{ rewrite /auth_inv.
......@@ -99,11 +112,12 @@ Proof.
Qed.
Lemma wait_loop_spec l x R (Φ : val iProp Σ) :
issued l x R ( l, locked l R - R - Φ #()) WP wait_loop #x l {{ Φ }}.
heapN N
issued l x R (locked l R - R - Φ #()) WP wait_loop #x l {{ Φ }}.
Proof.
iIntros "[Hl HΦ]". iDestruct "Hl" as (γ1 γ2 lo ln) "(#? & % & #? & Ht)".
iIntros (HN) "[Hl HΦ]". iDestruct "Hl" as (γ1 γ2 lo ln) "(% & #? & % & #? & Ht)".
iLöb as "IH". wp_rec. subst. wp_let. wp_proj. wp_bind (! _)%E.
iInv N as (o n) "[Hlo [Hln Ha]]" "Hclose".
iInv N as (o n) "[#? [Hlo [Hln Ha]]]" "Hclose".
wp_load. destruct (decide (x = o)) as [->|Hneq].
- iDestruct "Ha" as "[Hainv [[Ho HR] | Haown]]".
+ iVs ("Hclose" with "[Hlo Hln Hainv Ht]").
......@@ -121,16 +135,17 @@ Proof.
Qed.
Lemma acquire_spec l R (Φ : val iProp Σ) :
is_lock l R ( l, locked l R - R - Φ #()) WP acquire l {{ Φ }}.
heapN N
is_lock l R (locked l R - R - Φ #()) WP acquire l {{ Φ }}.
Proof.
iIntros "[Hl HΦ]". iDestruct "Hl" as (γ1 γ2 lo ln) "(#? & % & #?)".
iIntros (HN) "[Hl HΦ]". iDestruct "Hl" as (γ1 γ2 lo ln) "(% & #? & % & #?)".
iLöb as "IH". wp_rec. wp_bind (! _)%E. subst. wp_proj.
iInv N as (o n) "[Hlo [Hln Ha]]" "Hclose".
iInv N as (o n) "[#? [Hlo [Hln Ha]]]" "Hclose".
wp_load. iVs ("Hclose" with "[Hlo Hln Ha]").
{ iNext. iExists o, n. by iFrame. }
iVsIntro. wp_let. wp_proj. wp_op.
wp_bind (CAS _ _ _).
iInv N as (o' n') "[Hlo' [Hln' [Hainv Haown]]]" "Hclose".
iInv N as (o' n') "[#? [Hlo' [Hln' [Hainv Haown]]]]" "Hclose".
destruct (decide (#n' = #n))%V
as [[= ->%Nat2Z.inj] | Hneq].
- wp_cas_suc.
......@@ -144,9 +159,10 @@ Proof.
rewrite -(seq_set_S_union_L 0).
iNext. iExists o', (S n)%nat.
rewrite Nat2Z.inj_succ -Z.add_1_r.
iSplit; first by auto.
iFrame. iExists (GSet (seq_set 0 (S n))). by iFrame. }
iVsIntro. wp_if.
iApply (wait_loop_spec (#lo, #ln)).
iApply (wait_loop_spec (#lo, #ln)); first by auto.
iSplitR "HΦ"; last by done.
rewrite /issued /auth_own; eauto 10.
- wp_cas_fail.
......@@ -156,16 +172,17 @@ Proof.
Qed.
Lemma release_spec R l (Φ : val iProp Σ):
heapN N
locked l R R Φ #() WP release l {{ Φ }}.
Proof.
iIntros "(Hl & HR & HΦ)"; iDestruct "Hl" as (γ1 γ2 lo ln) "(#? & % & #? & Hγ)".
iIntros (HN) "(Hl & HR & HΦ)"; iDestruct "Hl" as (γ1 γ2 lo ln) "(% & #? & % & #? & Hγ)".
iLöb as "IH". wp_rec. subst. wp_proj. wp_bind (! _)%E.
iInv N as (o n) "[Hlo [Hln Hr]]" "Hclose".
wp_load. iVs ("Hclose" with "[Hlo Hln Hr]").
{ iNext. iExists o, n. by iFrame. }
iVsIntro. wp_let. wp_bind (CAS _ _ _ ).
wp_proj. wp_op.
iInv N as (o' n') "[Hlo' [Hln' Hr]]" "Hclose".
iInv N as (o' n') "[#? [Hlo' [Hln' Hr]]]" "Hclose".
destruct (decide (#o' = #o))%V
as [[= ->%Nat2Z.inj ] | Hneq].
- wp_cas_suc.
......@@ -174,7 +191,7 @@ Proof.
iDestruct (own_valid with "#Ho") as %[].
+ iVs ("Hclose" with "[Hlo' Hln' HR Hγ Hainv]").
{ iNext. iExists (o + 1)%nat, n'%nat.
iFrame. rewrite Nat2Z.inj_add.
iFrame. rewrite Nat2Z.inj_add. iSplit; first by auto.
iFrame. iLeft; by iFrame. }
iVsIntro. by wp_if.
- wp_cas_fail. iVs ("Hclose" with "[Hlo' Hln' Hr]").
......@@ -184,3 +201,7 @@ Qed.
End proof.
Typeclasses Opaque is_lock issued locked.
(* impl *)
Definition ticket_lock `{!heapG Σ, !tlockG Σ} :=
Lock _ _ newlock acquire release is_lock locked _ _ _ locked_is_lock newlock_spec acquire_spec release_spec.
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment