Skip to content

Commit

Permalink
Merge pull request #247 from mystor/data_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed Jul 6, 2020
2 parents 8c154be + 0919aff commit bb111e2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lock_api/src/mutex.rs
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
///
/// You must ensure that there are no data races when dereferencing the
/// returned pointer, for example if the current thread logically owns
/// a `MutexGuard` but that guard has been discarded using `mem::forget`.
#[inline]
pub fn data_ptr(&self) -> *mut T {
self.data.get()
}
}

impl<R: RawMutexFair, T: ?Sized> Mutex<R, T> {
Expand Down
17 changes: 17 additions & 0 deletions lock_api/src/remutex.rs
Expand Up @@ -362,6 +362,23 @@ 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
///
/// You must ensure that there are no data races when dereferencing the
/// returned pointer, for example 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
17 changes: 17 additions & 0 deletions lock_api/src/rwlock.rs
Expand Up @@ -540,6 +540,23 @@ 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
///
/// You must ensure that there are no data races when dereferencing the
/// returned pointer, for example 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

0 comments on commit bb111e2

Please sign in to comment.