Skip to content

Add RwLockReadGuard::map for transforming guards to contain sub-borrows. #30834

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

Merged
merged 3 commits into from
Feb 3, 2016
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
54 changes: 53 additions & 1 deletion src/libstd/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,43 @@ impl<'mutex, T: ?Sized> MutexGuard<'mutex, T> {
}
})
}

/// 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.
///
/// ```rust
/// # 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")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue should be opened and these numbers updated before this lands.

Copy link
Member

Choose a reason for hiding this comment

The 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 {
let new_data = unsafe {
let data = cb(&mut *this.__data.get());
mem::transmute::<&'mutex mut U, &'mutex UnsafeCell<U>>(data)
};

let lock = unsafe { ptr::read(&this.__lock) };
let poison = unsafe { ptr::read(&this.__poison) };
mem::forget(this);

MutexGuard {
__lock: lock,
__data: new_data,
__poison: poison
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -421,7 +458,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;

Expand Down Expand Up @@ -665,4 +702,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(_) => {}
};
}
}
90 changes: 86 additions & 4 deletions src/libstd/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub const RW_LOCK_INIT: StaticRwLock = StaticRwLock::new();
#[stable(feature = "rust1", since = "1.0.0")]
pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
__lock: &'a StaticRwLock,
__data: &'a UnsafeCell<T>,
__data: &'a T,
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -417,10 +417,37 @@ impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
poison::map_result(lock.poison.borrow(), |_| {
RwLockReadGuard {
__lock: lock,
__data: data,
__data: unsafe { &*data.get() },
}
})
}

/// 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.
///
/// ```rust
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a # Examples heading above this as well?

/// # use std::sync::{RwLockReadGuard, RwLock};
/// let x = RwLock::new(vec![1, 2]);
///
/// let y = RwLockReadGuard::map(x.read().unwrap(), |v| &v[0]);
/// assert_eq!(*y, 1);
/// ```
#[unstable(feature = "guard_map",
reason = "recently added, needs RFC for stabilization",
issue = "0")]
pub fn map<U: ?Sized, F>(this: Self, cb: F) -> RwLockReadGuard<'rwlock, U>
where F: FnOnce(&'rwlock T) -> &'rwlock U {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistically where clauses like this in the standard library tend to be formatted like:

pub fn map<F>(...) -> T
    where F: ...
{
    // ...
}

let new = RwLockReadGuard {
__lock: this.__lock,
__data: cb(this.__data)
};

mem::forget(this);

new
}
}

impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
Expand All @@ -434,13 +461,52 @@ impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
}
})
}

/// 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.
///
/// ```rust
/// # use std::sync::{RwLockWriteGuard, RwLock};
/// let x = RwLock::new(vec![1, 2]);
///
/// {
/// let y = RwLockWriteGuard::map(x.write().unwrap(), |v| &mut v[0]);
/// assert_eq!(*y, 1);
///
/// *y = 10;
/// }
///
/// assert_eq!(&**x.read().unwrap(), &[10, 2]);
/// ```
#[unstable(feature = "guard_map",
reason = "recently added, needs RFC for stabilization",
issue = "0")]
pub fn map<U: ?Sized, F>(this: Self, cb: F) -> RwLockWriteGuard<'rwlock, U>
where F: FnOnce(&'rwlock mut T) -> &'rwlock mut U {
let new_data = unsafe {
let data: &'rwlock mut T = &mut *this.__data.get();
mem::transmute::<&'rwlock mut U, &'rwlock UnsafeCell<U>>(cb(data))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strategy above of changing the stored type to just T seemed to work out well, could that be done here to avoid the transmute?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change both RwLockWriteGuard and MutexGuard to store &mut Ts.

};

let poison = unsafe { ptr::read(&this.__poison) };
let lock = unsafe { ptr::read(&this.__lock) };
mem::forget(this);

RwLockWriteGuard {
__lock: lock,
__data: new_data,
__poison: poison
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'rwlock, T: ?Sized> Deref for RwLockReadGuard<'rwlock, 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")]
Expand Down Expand Up @@ -481,7 +547,7 @@ mod tests {
use rand::{self, Rng};
use sync::mpsc::channel;
use thread;
use sync::{Arc, RwLock, StaticRwLock, TryLockError};
use sync::{Arc, RwLock, StaticRwLock, TryLockError, RwLockWriteGuard};
use sync::atomic::{AtomicUsize, Ordering};

#[derive(Eq, PartialEq, Debug)]
Expand Down Expand Up @@ -729,4 +795,20 @@ mod tests {
Ok(x) => panic!("get_mut of poisoned RwLock is Ok: {:?}", x),
}
}

#[test]
fn test_rwlock_write_map_poison() {
let rwlock = Arc::new(RwLock::new(vec![1, 2]));
let rwlock2 = rwlock.clone();

thread::spawn(move || {
let _ = RwLockWriteGuard::map::<usize, _>(rwlock2.write().unwrap(), |_| panic!());
}).join().unwrap_err();

match rwlock.read() {
Ok(r) => panic!("Read lock on poisioned RwLock is Ok: {:?}", &*r),
Err(_) => {}
};
}
}