mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +02:00
sync: add #[must_use] to lock guards (#4886)
This commit is contained in:
@@ -14,7 +14,7 @@ fn read_uncontended(b: &mut Bencher) {
|
|||||||
rt.block_on(async move {
|
rt.block_on(async move {
|
||||||
for _ in 0..6 {
|
for _ in 0..6 {
|
||||||
let read = lock.read().await;
|
let read = lock.read().await;
|
||||||
black_box(read);
|
let _read = black_box(read);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@@ -28,7 +28,7 @@ fn read_concurrent_uncontended_multi(b: &mut Bencher) {
|
|||||||
|
|
||||||
async fn task(lock: Arc<RwLock<()>>) {
|
async fn task(lock: Arc<RwLock<()>>) {
|
||||||
let read = lock.read().await;
|
let read = lock.read().await;
|
||||||
black_box(read);
|
let _read = black_box(read);
|
||||||
}
|
}
|
||||||
|
|
||||||
let lock = Arc::new(RwLock::new(()));
|
let lock = Arc::new(RwLock::new(()));
|
||||||
@@ -55,7 +55,7 @@ fn read_concurrent_uncontended(b: &mut Bencher) {
|
|||||||
|
|
||||||
async fn task(lock: Arc<RwLock<()>>) {
|
async fn task(lock: Arc<RwLock<()>>) {
|
||||||
let read = lock.read().await;
|
let read = lock.read().await;
|
||||||
black_box(read);
|
let _read = black_box(read);
|
||||||
}
|
}
|
||||||
|
|
||||||
let lock = Arc::new(RwLock::new(()));
|
let lock = Arc::new(RwLock::new(()));
|
||||||
@@ -82,7 +82,7 @@ fn read_concurrent_contended_multi(b: &mut Bencher) {
|
|||||||
|
|
||||||
async fn task(lock: Arc<RwLock<()>>) {
|
async fn task(lock: Arc<RwLock<()>>) {
|
||||||
let read = lock.read().await;
|
let read = lock.read().await;
|
||||||
black_box(read);
|
let _read = black_box(read);
|
||||||
}
|
}
|
||||||
|
|
||||||
let lock = Arc::new(RwLock::new(()));
|
let lock = Arc::new(RwLock::new(()));
|
||||||
@@ -110,7 +110,7 @@ fn read_concurrent_contended(b: &mut Bencher) {
|
|||||||
|
|
||||||
async fn task(lock: Arc<RwLock<()>>) {
|
async fn task(lock: Arc<RwLock<()>>) {
|
||||||
let read = lock.read().await;
|
let read = lock.read().await;
|
||||||
black_box(read);
|
let _read = black_box(read);
|
||||||
}
|
}
|
||||||
|
|
||||||
let lock = Arc::new(RwLock::new(()));
|
let lock = Arc::new(RwLock::new(()));
|
||||||
|
|||||||
@@ -141,6 +141,7 @@ pub struct Mutex<T: ?Sized> {
|
|||||||
///
|
///
|
||||||
/// The lock is automatically released whenever the guard is dropped, at which
|
/// The lock is automatically released whenever the guard is dropped, at which
|
||||||
/// point `lock` will succeed yet again.
|
/// point `lock` will succeed yet again.
|
||||||
|
#[must_use = "if unused the Mutex will immediately unlock"]
|
||||||
pub struct MutexGuard<'a, T: ?Sized> {
|
pub struct MutexGuard<'a, T: ?Sized> {
|
||||||
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
||||||
resource_span: tracing::Span,
|
resource_span: tracing::Span,
|
||||||
@@ -766,7 +767,7 @@ impl<'a, T: ?Sized> MutexGuard<'a, T> {
|
|||||||
/// # async fn main() {
|
/// # async fn main() {
|
||||||
/// # let mutex = Mutex::new(0u32);
|
/// # let mutex = Mutex::new(0u32);
|
||||||
/// # let guard = mutex.lock().await;
|
/// # let guard = mutex.lock().await;
|
||||||
/// # unlock_and_relock(guard).await;
|
/// # let _guard = unlock_and_relock(guard).await;
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ use std::ops;
|
|||||||
///
|
///
|
||||||
/// [`read`]: method@crate::sync::RwLock::read
|
/// [`read`]: method@crate::sync::RwLock::read
|
||||||
/// [`RwLock`]: struct@crate::sync::RwLock
|
/// [`RwLock`]: struct@crate::sync::RwLock
|
||||||
|
#[must_use = "if unused the RwLock will immediately unlock"]
|
||||||
pub struct RwLockReadGuard<'a, T: ?Sized> {
|
pub struct RwLockReadGuard<'a, T: ?Sized> {
|
||||||
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
||||||
pub(super) resource_span: tracing::Span,
|
pub(super) resource_span: tracing::Span,
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ use std::ops;
|
|||||||
///
|
///
|
||||||
/// [`write`]: method@crate::sync::RwLock::write
|
/// [`write`]: method@crate::sync::RwLock::write
|
||||||
/// [`RwLock`]: struct@crate::sync::RwLock
|
/// [`RwLock`]: struct@crate::sync::RwLock
|
||||||
|
#[must_use = "if unused the RwLock will immediately unlock"]
|
||||||
pub struct RwLockWriteGuard<'a, T: ?Sized> {
|
pub struct RwLockWriteGuard<'a, T: ?Sized> {
|
||||||
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
||||||
pub(super) resource_span: tracing::Span,
|
pub(super) resource_span: tracing::Span,
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ fn readiness() {
|
|||||||
// But once g unlocks, we can acquire it
|
// But once g unlocks, we can acquire it
|
||||||
drop(g);
|
drop(g);
|
||||||
assert!(t2.is_woken());
|
assert!(t2.is_woken());
|
||||||
assert_ready!(t2.poll());
|
let _t2 = assert_ready!(t2.poll());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -103,7 +103,7 @@ async fn aborted_future_1() {
|
|||||||
timeout(Duration::from_millis(1u64), async move {
|
timeout(Duration::from_millis(1u64), async move {
|
||||||
let iv = interval(Duration::from_millis(1000));
|
let iv = interval(Duration::from_millis(1000));
|
||||||
tokio::pin!(iv);
|
tokio::pin!(iv);
|
||||||
m2.lock().await;
|
let _g = m2.lock().await;
|
||||||
iv.as_mut().tick().await;
|
iv.as_mut().tick().await;
|
||||||
iv.as_mut().tick().await;
|
iv.as_mut().tick().await;
|
||||||
})
|
})
|
||||||
@@ -112,7 +112,7 @@ async fn aborted_future_1() {
|
|||||||
}
|
}
|
||||||
// This should succeed as there is no lock left for the mutex.
|
// This should succeed as there is no lock left for the mutex.
|
||||||
timeout(Duration::from_millis(1u64), async move {
|
timeout(Duration::from_millis(1u64), async move {
|
||||||
m1.lock().await;
|
let _g = m1.lock().await;
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.expect("Mutex is locked");
|
.expect("Mutex is locked");
|
||||||
@@ -134,7 +134,7 @@ async fn aborted_future_2() {
|
|||||||
let m2 = m1.clone();
|
let m2 = m1.clone();
|
||||||
// Try to lock mutex in a future that is aborted prematurely
|
// Try to lock mutex in a future that is aborted prematurely
|
||||||
timeout(Duration::from_millis(1u64), async move {
|
timeout(Duration::from_millis(1u64), async move {
|
||||||
m2.lock().await;
|
let _g = m2.lock().await;
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
@@ -142,7 +142,7 @@ async fn aborted_future_2() {
|
|||||||
}
|
}
|
||||||
// This should succeed as there is no lock left for the mutex.
|
// This should succeed as there is no lock left for the mutex.
|
||||||
timeout(Duration::from_millis(1u64), async move {
|
timeout(Duration::from_millis(1u64), async move {
|
||||||
m1.lock().await;
|
let _g = m1.lock().await;
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.expect("Mutex is locked");
|
.expect("Mutex is locked");
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ fn mutex_blocking_lock_panic_caller() -> Result<(), Box<dyn Error>> {
|
|||||||
let rt = basic();
|
let rt = basic();
|
||||||
rt.block_on(async {
|
rt.block_on(async {
|
||||||
let mutex = Mutex::new(5_u32);
|
let mutex = Mutex::new(5_u32);
|
||||||
mutex.blocking_lock();
|
let _g = mutex.blocking_lock();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ fn read_shared() {
|
|||||||
let mut t1 = spawn(rwlock.read());
|
let mut t1 = spawn(rwlock.read());
|
||||||
let _g1 = assert_ready!(t1.poll());
|
let _g1 = assert_ready!(t1.poll());
|
||||||
let mut t2 = spawn(rwlock.read());
|
let mut t2 = spawn(rwlock.read());
|
||||||
assert_ready!(t2.poll());
|
let _g2 = assert_ready!(t2.poll());
|
||||||
}
|
}
|
||||||
|
|
||||||
// When there is an active shared owner, exclusive access should not be possible
|
// When there is an active shared owner, exclusive access should not be possible
|
||||||
@@ -75,7 +75,7 @@ fn exhaust_reading() {
|
|||||||
let g2 = reads.pop().unwrap();
|
let g2 = reads.pop().unwrap();
|
||||||
drop(g2);
|
drop(g2);
|
||||||
assert!(t1.is_woken());
|
assert!(t1.is_woken());
|
||||||
assert_ready!(t1.poll());
|
let _g1 = assert_ready!(t1.poll());
|
||||||
}
|
}
|
||||||
|
|
||||||
// When there is an active exclusive owner, subsequent exclusive access should not be possible
|
// When there is an active exclusive owner, subsequent exclusive access should not be possible
|
||||||
@@ -100,7 +100,7 @@ fn write_shared_drop() {
|
|||||||
assert_pending!(t2.poll());
|
assert_pending!(t2.poll());
|
||||||
drop(g1);
|
drop(g1);
|
||||||
assert!(t2.is_woken());
|
assert!(t2.is_woken());
|
||||||
assert_ready!(t2.poll());
|
let _g2 = assert_ready!(t2.poll());
|
||||||
}
|
}
|
||||||
|
|
||||||
// when there is an active shared owner, and exclusive access is triggered,
|
// when there is an active shared owner, and exclusive access is triggered,
|
||||||
@@ -112,7 +112,7 @@ fn write_read_shared_pending() {
|
|||||||
let _g1 = assert_ready!(t1.poll());
|
let _g1 = assert_ready!(t1.poll());
|
||||||
|
|
||||||
let mut t2 = spawn(rwlock.read());
|
let mut t2 = spawn(rwlock.read());
|
||||||
assert_ready!(t2.poll());
|
let _g2 = assert_ready!(t2.poll());
|
||||||
|
|
||||||
let mut t3 = spawn(rwlock.write());
|
let mut t3 = spawn(rwlock.write());
|
||||||
assert_pending!(t3.poll());
|
assert_pending!(t3.poll());
|
||||||
@@ -137,7 +137,7 @@ fn write_read_shared_drop_pending() {
|
|||||||
drop(t2);
|
drop(t2);
|
||||||
|
|
||||||
assert!(t3.is_woken());
|
assert!(t3.is_woken());
|
||||||
assert_ready!(t3.poll());
|
let _t3 = assert_ready!(t3.poll());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Acquire an RwLock nonexclusively by a single task
|
// Acquire an RwLock nonexclusively by a single task
|
||||||
|
|||||||
Reference in New Issue
Block a user