Skip to content

Commit 8a11dbf

Browse files
committed
Add parking_lot README
1 parent 88f351c commit 8a11dbf

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

wrappers/parking_lot/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Shuttle support for `parking_lot`
2+
3+
This folder contains the implementation and wrapper that enables testing of [parking_lot](https://crates.io/crates/parking_lot) applications with Shuttle.
4+
5+
## How to use
6+
7+
To use it, add the following in your Cargo.toml:
8+
9+
```
10+
[features]
11+
shuttle = [
12+
"parking_lot/shuttle",
13+
]
14+
15+
[dependencies]
16+
parking_lot = { package = "shuttle-parking_lot", version = "VERSION_NUMBER" }
17+
```
18+
19+
The code will then behave as before when the `shuttle` feature flag is not provided, and will run with Shuttle-compatible primitives when the `shuttle` feature flag is provided.
20+
21+
## Limitations
22+
23+
Shuttle's parking_lot functionality is currently limited to a subset of the `Mutex` and `RwLock` primitives. If your project needs functionality which is not currently supported, please file an issue or, better yet, open a PR to contribute the functionality.

wrappers/parking_lot/parking_lot_impl/src/mutex.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! An asynchronous reader-writer lock.
1+
//! An asynchronous mutual exclusion primitive.
22
33
// This implementation is adapted from the one in shuttle-tokio
44

@@ -10,7 +10,7 @@ use std::ops::{Deref, DerefMut};
1010
use std::thread;
1111
use tracing::trace;
1212

13-
/// An asynchronous semaphore
13+
/// An asynchronous mutex
1414
pub struct Mutex<T: ?Sized> {
1515
semaphore: BatchSemaphore,
1616
inner: UnsafeCell<T>,
@@ -73,7 +73,7 @@ impl<T: ?Sized> Mutex<T> {
7373
unreachable!()
7474
}
7575
});
76-
trace!("acquired parking_Lot lock {:p}", self);
76+
trace!("acquired parking_lot lock {:p}", self);
7777
}
7878

7979
/// Acquires a mutex, blocking the current thread until it is able to do so.

wrappers/parking_lot/parking_lot_impl/src/rwlock.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ impl<T> RwLock<T> {
3232
/// Creates a new instance of an `RwLock<T>` which is unlocked
3333
/// and allows a maximum of `max_readers` concurrent readers.
3434
const fn with_max_readers(value: T, max_readers: usize) -> Self {
35-
let sem = BatchSemaphore::const_new(max_readers, Fairness::StrictlyFair);
36-
let rwlock = RwLock {
35+
RwLock {
3736
max_readers,
38-
sem,
37+
sem: BatchSemaphore::const_new(max_readers, Fairness::StrictlyFair),
3938
inner: UnsafeCell::new(value),
40-
};
41-
rwlock
39+
}
4240
}
4341
}
4442

4543
impl<T: ?Sized> RwLock<T> {
44+
/// Locks this `RwLock` with shared read access, blocking the current thread
4645
/// until it can be acquired.
4746
///
4847
/// The calling thread will be blocked until there are no more writers which
@@ -56,7 +55,7 @@ impl<T: ?Sized> RwLock<T> {
5655
/// once it is dropped.
5756
#[inline]
5857
pub fn read(&self) -> RwLockReadGuard<'_, T> {
59-
trace!("parking_lot rwlock {:p} acquiring read lock", self);
58+
trace!("parking_lot rwlock {:p} read acquiring RwLockReadGuard", self);
6059
self.sem.acquire_blocking(1).unwrap_or_else(|_| {
6160
// The semaphore was closed. but, we never explicitly close it, and we have a
6261
// handle to it through the Arc, which means that this can never happen.
@@ -65,7 +64,7 @@ impl<T: ?Sized> RwLock<T> {
6564
}
6665
});
6766

68-
trace!("parking_lot rwlock {:p} acquired", self);
67+
trace!("parking_lot rwlock {:p} read acquired RwLockReadGuard", self);
6968

7069
RwLockReadGuard {
7170
sem: &self.sem,
@@ -80,6 +79,8 @@ impl<T: ?Sized> RwLock<T> {
8079
/// Otherwise, an RAII guard is returned which will release read access
8180
/// when dropped.
8281
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> {
82+
trace!("parking_lot rwlock {:p} try_read acquiring RwlockReadGuard", self);
83+
8384
match self.sem.try_acquire(1) {
8485
Ok(permit) => permit,
8586
Err(TryAcquireError::NoPermits) => return None,
@@ -91,7 +92,7 @@ impl<T: ?Sized> RwLock<T> {
9192
}
9293
}
9394

94-
trace!("parking_lot rwlock {:p} try_read acquired ReadGuard", self);
95+
trace!("parking_lot rwlock {:p} try_read acquired RwLockReadGuard", self);
9596

9697
Some(RwLockReadGuard {
9798
sem: &self.sem,
@@ -103,7 +104,7 @@ impl<T: ?Sized> RwLock<T> {
103104
/// Locks this `RwLock` with exclusive write access, blocking the current
104105
/// thread until it can be acquired.
105106
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
106-
trace!("parking_lot rwlock {:p} acquiring write lock", self);
107+
trace!("parking_lot rwlock {:p} write acquiring RwLockWriteGuard", self);
107108
self.sem.acquire_blocking(1).unwrap_or_else(|_| {
108109
// The semaphore was closed. but, we never explicitly close it, and we have a
109110
// handle to it through the Arc, which means that this can never happen.
@@ -112,7 +113,7 @@ impl<T: ?Sized> RwLock<T> {
112113
}
113114
});
114115

115-
trace!("parking_lot rwlock {:p} acquired WriteGuard", self);
116+
trace!("parking_lot rwlock {:p} write acquired RwLockWriteGuard", self);
116117
RwLockWriteGuard {
117118
permits_acquired: self.max_readers,
118119
data: self.inner.get(),
@@ -127,7 +128,7 @@ impl<T: ?Sized> RwLock<T> {
127128
/// Otherwise, an RAII guard is returned which will release write access
128129
/// when dropped.
129130
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> {
130-
tracing::trace!("parking_lot rwlock {:p} try_write acquired WriteGuard", self,);
131+
tracing::trace!("parking_lot rwlock {:p} try_write acquiring RwLockWriteGuard", self);
131132

132133
match self.sem.try_acquire(1) {
133134
Ok(permit) => permit,
@@ -140,7 +141,7 @@ impl<T: ?Sized> RwLock<T> {
140141
}
141142
}
142143

143-
tracing::trace!("parking_lot rwlock {:p} try_write acquired WriteGuard", self,);
144+
tracing::trace!("parking_lot rwlock {:p} try_write acquired RwLockWriteGuard", self);
144145

145146
Some(RwLockWriteGuard {
146147
permits_acquired: self.max_readers,
@@ -261,7 +262,10 @@ impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> {
261262
let RwLockWriteGuard { sem, data, .. } = self;
262263
let to_release = self.permits_acquired - 1;
263264

264-
tracing::trace!("rwlock {:p} downgrading to ReadGuard", &self);
265+
tracing::trace!(
266+
"parking_lot rwlock {:p} downgrade RwLockWriteGuard to RwLockReadGuard",
267+
&self
268+
);
265269

266270
// NB: Forget to avoid drop impl from being called.
267271
std::mem::forget(self);

wrappers/tokio/impls/tokio/inner/src/sync/mutex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::Arc;
99
use std::thread;
1010
use tracing::trace;
1111

12-
/// An asynchronous semaphore
12+
/// An asynchronous mutex
1313
pub struct Mutex<T: ?Sized> {
1414
semaphore: BatchSemaphore,
1515
inner: UnsafeCell<T>,

0 commit comments

Comments
 (0)