Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lock_api: Allow unsafely accessing data without a guard #247

Merged
merged 2 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lock_api/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ impl<R: RawMutex, T: ?Sized> Mutex<R, T> {
pub unsafe fn raw(&self) -> &R {
&self.raw
}

/// Returns a raw pointer to the underlying data.
///
/// This is useful when combined with `mem::forget` to hold a lock without
/// the need to maintain a `MutexGuard` object alive, for example when
/// dealing with FFI.
///
/// # Safety
///
/// The returned pointer must only be dereferenced if the current thread
/// logically owns a `MutexGuard` but that guard has been discarded using
/// `mem::forget`.
Amanieu marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub fn data_ptr(&self) -> *mut T {
self.data.get()
}
}

impl<R: RawMutexFair, T: ?Sized> Mutex<R, T> {
Expand Down
16 changes: 16 additions & 0 deletions lock_api/src/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ impl<R: RawMutex, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T> {
pub unsafe fn raw(&self) -> &R {
&self.raw.mutex
}

/// Returns a raw pointer to the underlying data.
///
/// This is useful when combined with `mem::forget` to hold a lock without
/// the need to maintain a `ReentrantMutexGuard` object alive, for example
/// when dealing with FFI.
///
/// # Safety
///
/// The returned pointer must only be dereferenced if the current thread
/// logically owns a `ReentrantMutexGuard` but that guard has been discarded
/// using `mem::forget`.
#[inline]
pub fn data_ptr(&self) -> *mut T {
self.data.get()
}
}

impl<R: RawMutexFair, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T> {
Expand Down
16 changes: 16 additions & 0 deletions lock_api/src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,22 @@ impl<R: RawRwLock, T: ?Sized> RwLock<R, T> {
pub unsafe fn raw(&self) -> &R {
&self.raw
}

/// Returns a raw pointer to the underlying data.
///
/// This is useful when combined with `mem::forget` to hold a lock without
/// the need to maintain a `RwLockReadGuard` or `RwLockWriteGuard` object
/// alive, for example when dealing with FFI.
///
/// # Safety
///
/// The returned pointer must only be dereferenced if the current thread
/// logically owns a `RwLockReadGuard` or `RwLockWriteGuard` but that guard
/// has been discarded using `mem::forget`.
#[inline]
pub fn data_ptr(&self) -> *mut T {
self.data.get()
}
}

impl<R: RawRwLockFair, T: ?Sized> RwLock<R, T> {
Expand Down