Skip to content

Commit 9779332

Browse files
Merge pull request #37 from SkyLabsAI/paolo/add-lockedG
recursive_mutex: add and use lockedG typeclass to prepare for ghost state
2 parents 202d164 + 199da53 commit 9779332

2 files changed

Lines changed: 47 additions & 36 deletions

File tree

rocq-brick-libstdcpp/proof/mutex/demo_cpp_proof.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Definition P `{Σ : cpp_logic, σ : genv} (this : ptr) : TT -t> mpred :=
3434

3535
sl.lock
3636
Definition CR
37-
`{Σ : cpp_logic, σ : genv, HasOwn (iPropI _) recursive_mutex.cmraR}
37+
`{Σ : cpp_logic, σ : genv, HasOwn (iPropI _) recursive_mutex.cmraR, !recursive_mutex.lockedG Σ}
3838
(γ : recursive_mutex.rmutex_gname) (q : cQp.t) : Rep :=
3939
structR "C" q **
4040
_field "C::mut" |-> recursive_mutex.R γ.(recursive_mutex.lock_gname) q **
@@ -49,6 +49,7 @@ Section recursive_mutex.
4949
Context `{Σ : cpp_logic, σ : genv}.
5050
Context {HAS_THREADS : HasStdThreads Σ}.
5151
Context {has_rmutex : HasOwn (iPropI _) recursive_mutex.cmraR}.
52+
Context `{!recursive_mutex.lockedG Σ}.
5253

5354
Lemma acquireable_update_equiv {TT : tele} γ th f t1 t2 P :
5455
acquire t1 t2 ->
@@ -66,6 +67,7 @@ Section with_cpp.
6667
Context `{Σ : cpp_logic, σ : genv}.
6768
Context {HAS_THREADS : HasStdThreads Σ}.
6869
Context {has_rmutex : HasOwn (iPropI _) recursive_mutex.cmraR}.
70+
Context `{!recursive_mutex.lockedG Σ}.
6971

7072
#[global] Instance: LearnEq2 CR'.
7173
Proof. solve_learnable. Qed.

rocq-brick-libstdcpp/proof/mutex/spec.v

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,12 @@ Section with_cpp.
1414
(** Fractional ownership of a <<std::mutex>> guarding the predicate <<P>>. *)
1515
Parameter R : forall {HAS_THREADS : HasStdThreads Σ} {σ : genv}, gname -> cQp.t -> mpred -> Rep.
1616
#[only(cfractional,cfracvalid,ascfractional,timeless)] derive R.
17-
(*
18-
#[global] Declare Instance mutex_rep_typed : Typed3 "std::mutex" mutex_rep.
19-
#[global] Declare Instance mutex_rep_cfrac : forall γ, CFractional1 (mutex_rep γ).
20-
#[global] Declare Instance mutex_rep_ascfrac : forall γ, AsCFractional2 (mutex_rep γ).
21-
#[global] Declare Instance mutex_rep_cfracvalid : forall γ, CFracValid2 (mutex_rep γ).
22-
#[global] Declare Instance mutex_rep_timeless : Timeless3 mutex_rep.
23-
*)
2417
#[global] Declare Instance mutex_rep_typed : forall {HAS_THREADS : HasStdThreads Σ} {σ : genv}, Typed3 "std::mutex" R.
2518

2619
(* TODO: index this by the specific mutex! Either via a mutex_gname or by making this a Rep *)
2720
(* TODO: why is this separate from [mutex_rep] *)
2821
Parameter mutex_token : forall {HAS_THREADS : HasStdThreads Σ} {σ : genv}, gname -> cQp.t -> mpred.
2922
#[only(cfractional,cfracvalid,ascfractional,timeless)] derive mutex_token.
30-
(*
31-
#[global] Declare Instance mutex_token_cfrac : CFractional1 mutex_token.
32-
#[global] Declare Instance mutex_token_ascfrac : AsCFractional1 mutex_token.
33-
#[global] Declare Instance mutex_token_cfracvalid : CFracValid1 mutex_token.
34-
#[global] Declare Instance mutex_token_timeless : Timeless2 mutex_token.
35-
*)
3623
#[global] Declare Instance mutex_rep_learnable : forall {HAS_THREADS : HasStdThreads Σ} {σ : genv},
3724
Cbn (Learn (learn_eq ==> any ==> learn_eq ==> learn_hints.fin) R).
3825

@@ -57,10 +44,6 @@ Section with_cpp.
5744
*)
5845
Parameter mutex_locked : forall {HAS_THREADS : HasStdThreads Σ} {σ : genv}, gname -> thread_idT -> mpred.
5946
#[only(timeless,exclusive)] derive mutex_locked.
60-
(*
61-
Declare Instance mutex_locked_timeless : Timeless2 mutex_locked.
62-
Declare Instance mutex_locked_excl g : Exclusive1 (mutex_locked g).
63-
*)
6447

6548
Context `{MOD : inc_hpp.source ⊧ σ}.
6649
Context {HAS_THREADS : HasStdThreads Σ}.
@@ -103,10 +86,42 @@ End mutex.
10386
Module recursive_mutex.
10487

10588
(** <<locked γ th n>> <<th>> owns the mutex <<γ>> <<n>> times. *)
106-
Parameter locked : ∀ `{Σ : cpp_logic}, gname -> thread_idT -> nat -> mpred.
107-
#[global] Declare Instance
108-
locked_WeaklyObjective `{Σ : cpp_logic} γ thr n :
109-
WeaklyObjective (PROP := iPropI _) (locked γ thr n).
89+
#[projections(primitive=no)]
90+
Class lockedG `{Σ : cpp_logic} := {
91+
}.
92+
#[global] Arguments lockedG {_ _} Σ : assert.
93+
94+
Parameter locked :
95+
∀ `{Σ : cpp_logic, !lockedG Σ}
96+
(γ : gname) (th : thread_idT) (n : nat), mpred.
97+
98+
Parameter used_threads :
99+
∀ `{Σ : cpp_logic, !lockedG Σ, !HasStdThreads Σ}
100+
(γ : gname) (s : gset thread_idT), mpred.
101+
102+
#[only(timeless)] derive locked.
103+
#[only(timeless)] derive used_threads.
104+
105+
Section locked_with_cpp.
106+
Context `{Σ : cpp_logic}.
107+
Context `{!lockedG Σ}.
108+
Context `{!HasStdThreads Σ}.
109+
110+
Axiom use_thread : ∀ th g s,
111+
th ∉ s ->
112+
current_thread th ** used_threads g s |--
113+
|==> used_threads g (s ∪ {[ th ]}) ** locked g th 0.
114+
115+
#[global] Declare Instance
116+
locked_WeaklyObjective γ thr n :
117+
WeaklyObjective (PROP := iPropI _) (locked γ thr n).
118+
119+
Axiom locked_excl_same_thread : forall g th n m,
120+
locked g th n ** locked g th m |-- False.
121+
Axiom locked_excl_different_thread : forall g th th' n m,
122+
locked g th n ** locked g th' m |-- [| n = 0 \/ m = 0 |] ** True.
123+
124+
End locked_with_cpp.
110125

111126
(* the mask of recursive_mutex *)
112127
Definition mask := nroot .@@ "std" .@@ "recursive_mutex".
@@ -119,7 +134,9 @@ Module recursive_mutex.
119134
Canonical Structure cmraR := (excl_authR (prodO natO thread_idTO)).
120135

121136
sl.lock
122-
Definition inv_rmutex `{Σ : cpp_logic} `{!HasOwn (iPropI _) cmraR} (g : rmutex_gname) (P : mpred) : mpred :=
137+
Definition inv_rmutex
138+
`{Σ : cpp_logic} `{!lockedG Σ} `{!HasOwn (iPropI _) cmraR}
139+
(g : rmutex_gname) (P : mpred) : mpred :=
123140
inv rmutex_namespace
124141
(Exists n th, own g.(level_gname) (●E (n, th)) **
125142
match n with
@@ -135,8 +152,6 @@ Module recursive_mutex.
135152
| Held (n : nat) (xs : TT) (* acquired [n + 1] times with quantifiers [xs] *).
136153
#[global] Arguments acquire_state _ : clear implicits.
137154

138-
Parameter used_threads : ∀ `{Σ : cpp_logic}, gname -> gset thread_idT -> mpred.
139-
140155
sl.lock
141156
Definition acquire {TT} (a a' : acquire_state TT) : Prop :=
142157
match a with
@@ -167,7 +182,9 @@ Module recursive_mutex.
167182
end.
168183

169184
sl.lock
170-
Definition acquireable `{Σ : cpp_logic, !HasStdThreads Σ, !HasOwn (iPropI _) cmraR} (g : rmutex_gname) (th : thread_idT) {TT: tele} (t : acquire_state TT) (P : TT -t> mpred) : mpred :=
185+
Definition acquireable
186+
`{Σ : cpp_logic, !lockedG Σ, !HasStdThreads Σ, !HasOwn (iPropI _) cmraR}
187+
(g : rmutex_gname) (th : thread_idT) {TT: tele} (t : acquire_state TT) (P : TT -t> mpred) : mpred :=
171188
current_thread th **
172189
match t with
173190
| NotHeld => locked g.(lock_gname) th 0
@@ -177,13 +194,8 @@ Module recursive_mutex.
177194
Section with_cpp.
178195
Context `{Σ : cpp_logic}.
179196

180-
#[global] Declare Instance locked_timeless : Timeless3 locked.
181-
Axiom locked_excl_same_thread : forall g th n m,
182-
locked g th n ** locked g th m |-- False.
183-
Axiom locked_excl_different_thread : forall g th th' n m,
184-
locked g th n ** locked g th' m |-- [| n = 0 \/ m = 0 |] ** True.
185-
186197
Context `{!HasOwn (iPropI _) cmraR, !HasStdThreads Σ}.
198+
Context `{!lockedG Σ}.
187199

188200
#[global] Instance acquireable_learn γ th TT : LearnEq2 (acquireable γ th (TT := TT)).
189201
Proof. solve_learnable. Qed.
@@ -192,10 +204,6 @@ Module recursive_mutex.
192204
`{Observe (current_thread th) (acquireable g th (TT := TT) t P)}.
193205
Proof. rewrite acquireable.unlock; apply _. Qed.
194206

195-
Axiom use_thread : forall th g m,
196-
th ∉ m ->
197-
current_thread th ** used_threads g m |-- |==> used_threads g (m ∪ {[ th ]}) ** locked g th 0.
198-
199207
Lemma use_thread_acquirable {TT} th g m P :
200208
th ∉ m ->
201209
current_thread th ** used_threads g.(lock_gname) m |-- |==>
@@ -212,6 +220,7 @@ Module recursive_mutex.
212220
Section with_cpp.
213221
Context `{Σ : cpp_logic} `{MOD : source ⊧ σ}.
214222
Context {HAS_THREADS : HasStdThreads Σ}.
223+
Context `{!lockedG Σ}.
215224

216225
(* NOTE: Invariant used to protect resource [r]
217226
inv (r \\// exists th n, locked th (S n)) *)

0 commit comments

Comments
 (0)