1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#[cfg(any(test, feature = "coop"))]
mod coop_impl {
use crate::coop::Operation;
use std::sync::Arc;
use tokio::sync::RwLock as TokioRwLock;
/// A wrapper around tokio's RwLock that integrates with Bach's coop system.
///
/// This RwLock implementation ensures proper interleaving simulation with Bach's
/// cooperative scheduling system.
pub struct RwLock<T: ?Sized> {
lock_op: Operation,
// Store the inner rwlock in an Arc to enable owned_* methods
// This is required because tokio's read/write_owned methods accept Arc<RwLock> not just RwLock
inner: Arc<TokioRwLock<T>>,
}
impl<T: ?Sized> RwLock<T> {
/// Creates a new RwLock with the given value.
pub fn new(value: T) -> Self
where
T: Sized,
{
Self {
inner: Arc::new(TokioRwLock::new(value)),
lock_op: Operation::register(),
}
}
/// Acquires a read lock on the RwLock.
///
/// This method will register the read lock operation with Bach's coop system,
/// ensuring proper interleaving exploration during simulation.
pub async fn read(&self) -> RwLockReadGuard<'_, T> {
use crate::tracing::Instrument;
let span = crate::tracing::debug_span!("rwlock::read");
async {
// First acquire the operation through the coop system
self.lock_op.acquire().await;
// Then acquire the actual read lock
let guard = self.inner.read().await;
RwLockReadGuard { guard }
}
.instrument(span)
.await
}
/// Tries to acquire a read lock on the RwLock without waiting.
pub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>, tokio::sync::TryLockError> {
// Try to acquire the actual read lock
match self.inner.try_read() {
Ok(guard) => Ok(RwLockReadGuard { guard }),
Err(err) => Err(err),
}
}
/// Acquires a write lock on the RwLock.
///
/// This method will register the write lock operation with Bach's coop system,
/// ensuring proper interleaving exploration during simulation.
pub async fn write(&self) -> RwLockWriteGuard<'_, T> {
use crate::tracing::Instrument;
let span = crate::tracing::debug_span!("rwlock::write");
async {
// First acquire the operation through the coop system
self.lock_op.acquire().await;
// Then acquire the actual write lock
let guard = self.inner.write().await;
RwLockWriteGuard { guard }
}
.instrument(span)
.await
}
/// Tries to acquire a write lock on the RwLock without waiting.
pub fn try_write(&self) -> Result<RwLockWriteGuard<'_, T>, tokio::sync::TryLockError> {
// Try to acquire the actual write lock
match self.inner.try_write() {
Ok(guard) => Ok(RwLockWriteGuard { guard }),
Err(err) => Err(err),
}
}
/// Acquires a read lock on an Arc-wrapped RwLock, returning an owned guard that can be
/// held across await points.
///
/// This method will register the read lock operation with Bach's coop system,
/// ensuring proper interleaving exploration during simulation.
pub async fn read_owned(self: Arc<Self>) -> OwnedRwLockReadGuard<T>
where
T: Sized,
{
use crate::tracing::Instrument;
let span = crate::tracing::debug_span!("rwlock::read_owned");
async {
// First acquire the operation through the coop system
self.lock_op.acquire().await;
// Then use tokio's read_owned method with our already Arc-wrapped inner rwlock
let guard = self.inner.clone().read_owned().await;
OwnedRwLockReadGuard { guard }
}
.instrument(span)
.await
}
/// Tries to acquire an owned read lock on the RwLock without waiting.
pub fn try_read_owned(
self: Arc<Self>,
) -> Result<OwnedRwLockReadGuard<T>, tokio::sync::TryLockError>
where
T: Sized,
{
match self.inner.clone().try_read_owned() {
Ok(guard) => Ok(OwnedRwLockReadGuard { guard }),
Err(err) => Err(err),
}
}
/// Acquires a write lock on an Arc-wrapped RwLock, returning an owned guard that can be
/// held across await points.
///
/// This method will register the write lock operation with Bach's coop system,
/// ensuring proper interleaving exploration during simulation.
pub async fn write_owned(self: Arc<Self>) -> OwnedRwLockWriteGuard<T>
where
T: Sized,
{
use crate::tracing::Instrument;
let span = crate::tracing::debug_span!("rwlock::write_owned");
async {
// First acquire the operation through the coop system
self.lock_op.acquire().await;
// Then use tokio's write_owned method with our already Arc-wrapped inner rwlock
let guard = self.inner.clone().write_owned().await;
OwnedRwLockWriteGuard { guard }
}
.instrument(span)
.await
}
/// Tries to acquire an owned write lock on the RwLock without waiting.
pub fn try_write_owned(
self: Arc<Self>,
) -> Result<OwnedRwLockWriteGuard<T>, tokio::sync::TryLockError>
where
T: Sized,
{
match self.inner.clone().try_write_owned() {
Ok(guard) => Ok(OwnedRwLockWriteGuard { guard }),
Err(err) => Err(err),
}
}
}
/// A read guard that releases the read lock when dropped.
pub struct RwLockReadGuard<'a, T: ?Sized> {
guard: tokio::sync::RwLockReadGuard<'a, T>,
}
impl<'a, T: ?Sized> std::ops::Deref for RwLockReadGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.guard
}
}
/// A write guard that releases the write lock when dropped.
pub struct RwLockWriteGuard<'a, T: ?Sized> {
guard: tokio::sync::RwLockWriteGuard<'a, T>,
}
impl<'a, T: ?Sized> std::ops::Deref for RwLockWriteGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.guard
}
}
impl<'a, T: ?Sized> std::ops::DerefMut for RwLockWriteGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.guard
}
}
/// An owned read guard that releases the read lock when dropped.
pub struct OwnedRwLockReadGuard<T: ?Sized> {
guard: tokio::sync::OwnedRwLockReadGuard<T>,
}
impl<T: ?Sized> std::ops::Deref for OwnedRwLockReadGuard<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.guard
}
}
/// An owned write guard that releases the write lock when dropped.
pub struct OwnedRwLockWriteGuard<T: ?Sized> {
guard: tokio::sync::OwnedRwLockWriteGuard<T>,
}
impl<T: ?Sized> std::ops::Deref for OwnedRwLockWriteGuard<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.guard
}
}
impl<T: ?Sized> std::ops::DerefMut for OwnedRwLockWriteGuard<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.guard
}
}
}
// When the coop feature is enabled, export our wrapped implementation
#[cfg(any(test, feature = "coop"))]
pub use coop_impl::*;
// Otherwise, re-export tokio's rwlock types directly
#[cfg(not(any(test, feature = "coop")))]
pub use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};