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: Unsafely access data within a Mutex without MutexGuard #246

Closed
mystor opened this issue Jul 4, 2020 · 0 comments · Fixed by #247
Closed

lock_api: Unsafely access data within a Mutex without MutexGuard #246

mystor opened this issue Jul 4, 2020 · 0 comments · Fixed by #247

Comments

@mystor
Copy link
Contributor

mystor commented Jul 4, 2020

I have a use-case where I need to manually manage locking/unlocking a Mutex, and can't store the MutexGuard between locking and accessing the locked data. It's possible to perform locking and unlocking of the RawMutex manually from outside this crate using the Mutex::raw() method, as well as mem::forget(mutex.lock()) and mutex.force_unlock(), but there appears to be no API to then unsafely access the raw data guarded by a &Mutex.

It would be nice to expose a method to extract the raw pointer to the mutex-guarded data within the UnsafeCell, perhaps with something like the following:

/// Returns a raw pointer to the underlying data.
///
/// # Safety
///
/// The returned pointer may only be dereferenced if the current thread logically
/// owns a `MutexGuard`, but that guard has been discarded using `mem::forget`.
pub fn data_ptr(&self) -> *mut T {
  self.data.get()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant