-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add RwLockReadGuard::map for transforming guards to contain sub-borrows. #30834
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,7 +172,7 @@ pub struct MutexGuard<'a, T: ?Sized + 'a> { | |
// funny underscores due to how Deref/DerefMut currently work (they | ||
// disregard field privacy). | ||
__lock: &'a StaticMutex, | ||
__data: &'a UnsafeCell<T>, | ||
__data: &'a mut T, | ||
__poison: poison::Guard, | ||
} | ||
|
||
|
@@ -212,7 +212,7 @@ impl<T: ?Sized> Mutex<T> { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn lock(&self) -> LockResult<MutexGuard<T>> { | ||
unsafe { self.inner.lock.lock() } | ||
MutexGuard::new(&*self.inner, &self.data) | ||
unsafe { MutexGuard::new(&*self.inner, &self.data) } | ||
} | ||
|
||
/// Attempts to acquire this lock. | ||
|
@@ -231,7 +231,7 @@ impl<T: ?Sized> Mutex<T> { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn try_lock(&self) -> TryLockResult<MutexGuard<T>> { | ||
if unsafe { self.inner.lock.try_lock() } { | ||
Ok(try!(MutexGuard::new(&*self.inner, &self.data))) | ||
Ok(try!(unsafe { MutexGuard::new(&*self.inner, &self.data) })) | ||
} else { | ||
Err(TryLockError::WouldBlock) | ||
} | ||
|
@@ -339,14 +339,14 @@ impl StaticMutex { | |
#[inline] | ||
pub fn lock(&'static self) -> LockResult<MutexGuard<()>> { | ||
unsafe { self.lock.lock() } | ||
MutexGuard::new(self, &DUMMY.0) | ||
unsafe { MutexGuard::new(self, &DUMMY.0) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is another case that would likely benefit from merging the two unsafe blocks |
||
} | ||
|
||
/// Attempts to grab this lock, see `Mutex::try_lock` | ||
#[inline] | ||
pub fn try_lock(&'static self) -> TryLockResult<MutexGuard<()>> { | ||
if unsafe { self.lock.try_lock() } { | ||
Ok(try!(MutexGuard::new(self, &DUMMY.0))) | ||
Ok(try!(unsafe { MutexGuard::new(self, &DUMMY.0) })) | ||
} else { | ||
Err(TryLockError::WouldBlock) | ||
} | ||
|
@@ -369,32 +369,70 @@ impl StaticMutex { | |
|
||
impl<'mutex, T: ?Sized> MutexGuard<'mutex, T> { | ||
|
||
fn new(lock: &'mutex StaticMutex, data: &'mutex UnsafeCell<T>) | ||
unsafe fn new(lock: &'mutex StaticMutex, data: &'mutex UnsafeCell<T>) | ||
-> LockResult<MutexGuard<'mutex, T>> { | ||
poison::map_result(lock.poison.borrow(), |guard| { | ||
MutexGuard { | ||
__lock: lock, | ||
__data: data, | ||
__data: &mut *data.get(), | ||
__poison: guard, | ||
} | ||
}) | ||
} | ||
|
||
/// Transform this guard to hold a sub-borrow of the original data. | ||
/// | ||
/// Applies the supplied closure to the data, returning a new lock | ||
/// guard referencing the borrow returned by the closure. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// # #![feature(guard_map)] | ||
/// # use std::sync::{Mutex, MutexGuard}; | ||
/// let x = Mutex::new(vec![1, 2]); | ||
/// | ||
/// { | ||
/// let y = MutexGuard::map(x.lock().unwrap(), |v| &mut v[0]); | ||
/// *y = 3; | ||
/// } | ||
/// | ||
/// assert_eq!(&*x.lock(), &[3, 2]); | ||
/// ``` | ||
#[unstable(feature = "guard_map", | ||
reason = "recently added, needs RFC for stabilization", | ||
issue = "0")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An issue should be opened and these numbers updated before this lands. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're going to fold this into #27746 so that's the issue number to use here. |
||
pub fn map<U: ?Sized, F>(this: Self, cb: F) -> MutexGuard<'mutex, U> | ||
where F: FnOnce(&'mutex mut T) -> &'mutex mut U { | ||
// Compute the new data while still owning the original lock | ||
// in order to correctly poison if the callback panics. | ||
let data = unsafe { ptr::read(&this.__data) }; | ||
let new_data = cb(data); | ||
|
||
// We don't want to unlock the lock by running the destructor of the | ||
// original lock, so just read the fields we need and forget it. | ||
let poison = unsafe { ptr::read(&this.__poison) }; | ||
let lock = unsafe { ptr::read(&this.__lock) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these unsafe blocks all be merged as well? |
||
mem::forget(this); | ||
|
||
MutexGuard { | ||
__lock: lock, | ||
__data: new_data, | ||
__poison: poison | ||
} | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<'mutex, T: ?Sized> Deref for MutexGuard<'mutex, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &T { | ||
unsafe { &*self.__data.get() } | ||
} | ||
fn deref(&self) -> &T {self.__data } | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<'mutex, T: ?Sized> DerefMut for MutexGuard<'mutex, T> { | ||
fn deref_mut(&mut self) -> &mut T { | ||
unsafe { &mut *self.__data.get() } | ||
} | ||
fn deref_mut(&mut self) -> &mut T { self.__data } | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
|
@@ -421,7 +459,7 @@ mod tests { | |
use prelude::v1::*; | ||
|
||
use sync::mpsc::channel; | ||
use sync::{Arc, Mutex, StaticMutex, Condvar}; | ||
use sync::{Arc, Mutex, StaticMutex, Condvar, MutexGuard}; | ||
use sync::atomic::{AtomicUsize, Ordering}; | ||
use thread; | ||
|
||
|
@@ -665,4 +703,19 @@ mod tests { | |
let comp: &[i32] = &[4, 2, 5]; | ||
assert_eq!(&*mutex.lock().unwrap(), comp); | ||
} | ||
|
||
#[test] | ||
fn test_mutex_guard_map_panic() { | ||
let mutex = Arc::new(Mutex::new(vec![1, 2])); | ||
let mutex2 = mutex.clone(); | ||
|
||
thread::spawn(move || { | ||
let _ = MutexGuard::map::<usize, _>(mutex2.lock().unwrap(), |_| panic!()); | ||
}).join().unwrap_err(); | ||
|
||
match mutex.lock() { | ||
Ok(r) => panic!("Lock on poisioned Mutex is Ok: {:?}", &*r), | ||
Err(_) => {} | ||
}; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this new proliferation of
unsafe
blocks, can blocks like this just be merged with the one above? (e.g. have one surrounding block)