Skip to content

Commit

Permalink
Add a rwlock() method to owned RwLock guards
Browse files Browse the repository at this point in the history
Adds a `rwlock()` method returning a reference to the original `Arc<RwLock>` to the `Owned*` guards.
  • Loading branch information
r3v2d0g committed Mar 20, 2024
1 parent d51f168 commit 6bc0332
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
30 changes: 27 additions & 3 deletions tokio/src/sync/rwlock/owned_read_guard.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::sync::rwlock::RwLock;
use std::marker::PhantomData;
use std::sync::Arc;
use std::{fmt, mem, ops, ptr};
use std::{fmt, marker::PhantomData, mem, ops, ptr, sync::Arc};

/// Owned RAII structure used to release the shared read access of a lock when
/// dropped.
Expand Down Expand Up @@ -138,6 +136,32 @@ impl<T: ?Sized, U: ?Sized> OwnedRwLockReadGuard<T, U> {
resource_span: this.resource_span,
})
}

/// Returns a reference to the original `Arc<RwLock>`.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use tokio::sync::{RwLock, OwnedRwLockReadGuard};
///
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// struct Foo(u32);
///
/// # #[tokio::main]
/// # async fn main() {
/// let lock = Arc::new(RwLock::new(Foo(1)));
///
/// let guard = lock.clone().read_owned().await;
/// assert!(Arc::ptr_eq(&lock, guard.rwlock()));
///
/// let guard = OwnedRwLockReadGuard::map(guard, |f| &f.0);
/// assert!(Arc::ptr_eq(&lock, guard.rwlock()));
/// # }
/// ```
pub fn rwlock(&self) -> &Arc<RwLock<T>> {
&self.lock
}
}

impl<T: ?Sized, U: ?Sized> ops::Deref for OwnedRwLockReadGuard<T, U> {
Expand Down
31 changes: 25 additions & 6 deletions tokio/src/sync/rwlock/owned_write_guard.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::sync::rwlock::owned_read_guard::OwnedRwLockReadGuard;
use crate::sync::rwlock::owned_write_guard_mapped::OwnedRwLockMappedWriteGuard;
use crate::sync::rwlock::RwLock;
use std::marker::PhantomData;
use std::sync::Arc;
use std::{fmt, mem, ops, ptr};
use crate::sync::rwlock::{
owned_read_guard::OwnedRwLockReadGuard, owned_write_guard_mapped::OwnedRwLockMappedWriteGuard,
RwLock,
};
use std::{fmt, marker::PhantomData, mem, ops, ptr, sync::Arc};

/// Owned RAII structure used to release the exclusive write access of a lock when
/// dropped.
Expand Down Expand Up @@ -390,6 +389,26 @@ impl<T: ?Sized> OwnedRwLockWriteGuard<T> {

guard
}

/// Returns a reference to the original `Arc<RwLock>`.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use tokio::sync::RwLock;
///
/// # #[tokio::main]
/// # async fn main() {
/// let lock = Arc::new(RwLock::new(1));
///
/// let guard = lock.clone().write_owned().await;
/// assert!(Arc::ptr_eq(&lock, guard.rwlock()));
/// # }
/// ```
pub fn rwlock(&self) -> &Arc<RwLock<T>> {
&self.lock
}
}

impl<T: ?Sized> ops::Deref for OwnedRwLockWriteGuard<T> {
Expand Down
25 changes: 22 additions & 3 deletions tokio/src/sync/rwlock/owned_write_guard_mapped.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::sync::rwlock::RwLock;
use std::marker::PhantomData;
use std::sync::Arc;
use std::{fmt, mem, ops, ptr};
use std::{fmt, marker::PhantomData, mem, ops, ptr, sync::Arc};

/// Owned RAII structure used to release the exclusive write access of a lock when
/// dropped.
Expand Down Expand Up @@ -155,6 +153,27 @@ impl<T: ?Sized, U: ?Sized> OwnedRwLockMappedWriteGuard<T, U> {
resource_span: this.resource_span,
})
}

/// Returns a reference to the original `Arc<RwLock>`.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use tokio::sync::{RwLock, OwnedRwLockMappedWriteGuard};
///
/// # #[tokio::main]
/// # async fn main() {
/// let lock = Arc::new(RwLock::new(1));
///
/// let guard = lock.clone().write_owned().await;
/// let guard = OwnedRwLockMappedWriteGuard::map(guard, |x| x);
/// assert!(Arc::ptr_eq(&lock, guard.rwlock()));
/// # }
/// ```
pub fn rwlock(&self) -> &Arc<RwLock<T>> {
&self.lock
}
}

impl<T: ?Sized, U: ?Sized> ops::Deref for OwnedRwLockMappedWriteGuard<T, U> {
Expand Down
3 changes: 1 addition & 2 deletions tokio/src/sync/rwlock/read_guard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::sync::batch_semaphore::Semaphore;
use std::marker::PhantomData;
use std::{fmt, mem, ops};
use std::{fmt, marker::PhantomData, mem, ops};

/// RAII structure used to release the shared read access of a lock when
/// dropped.
Expand Down

0 comments on commit 6bc0332

Please sign in to comment.