Skip to content

Commit a61983f

Browse files
committed
Remove MutexGuard::map, as it is not safe in combination with Condvar.
It could return in the future if it returned a different guard type, which could not be used with Condvar, otherwise it is unsafe as another thread can invalidate an "inner" reference during a Condvar::wait. cc rust-lang#27746
1 parent 2ad6dc2 commit a61983f

File tree

1 file changed

+1
-60
lines changed

1 file changed

+1
-60
lines changed

src/libstd/sync/mutex.rs

+1-60
Original file line numberDiff line numberDiff line change
@@ -387,50 +387,6 @@ impl<'mutex, T: ?Sized> MutexGuard<'mutex, T> {
387387
}
388388
})
389389
}
390-
391-
/// Transform this guard to hold a sub-borrow of the original data.
392-
///
393-
/// Applies the supplied closure to the data, returning a new lock
394-
/// guard referencing the borrow returned by the closure.
395-
///
396-
/// # Examples
397-
///
398-
/// ```rust
399-
/// # #![feature(guard_map)]
400-
/// # use std::sync::{Mutex, MutexGuard};
401-
/// let x = Mutex::new(vec![1, 2]);
402-
///
403-
/// {
404-
/// let mut y = MutexGuard::map(x.lock().unwrap(), |v| &mut v[0]);
405-
/// *y = 3;
406-
/// }
407-
///
408-
/// assert_eq!(&*x.lock().unwrap(), &[3, 2]);
409-
/// ```
410-
#[unstable(feature = "guard_map",
411-
reason = "recently added, needs RFC for stabilization",
412-
issue = "27746")]
413-
pub fn map<U: ?Sized, F>(this: Self, cb: F) -> MutexGuard<'mutex, U>
414-
where F: FnOnce(&'mutex mut T) -> &'mutex mut U
415-
{
416-
// Compute the new data while still owning the original lock
417-
// in order to correctly poison if the callback panics.
418-
let data = unsafe { ptr::read(&this.__data) };
419-
let new_data = cb(data);
420-
421-
// We don't want to unlock the lock by running the destructor of the
422-
// original lock, so just read the fields we need and forget it.
423-
let (poison, lock) = unsafe {
424-
(ptr::read(&this.__poison), ptr::read(&this.__lock))
425-
};
426-
mem::forget(this);
427-
428-
MutexGuard {
429-
__lock: lock,
430-
__data: new_data,
431-
__poison: poison
432-
}
433-
}
434390
}
435391

436392
#[stable(feature = "rust1", since = "1.0.0")]
@@ -469,7 +425,7 @@ mod tests {
469425
use prelude::v1::*;
470426

471427
use sync::mpsc::channel;
472-
use sync::{Arc, Mutex, StaticMutex, Condvar, MutexGuard};
428+
use sync::{Arc, Mutex, StaticMutex, Condvar};
473429
use sync::atomic::{AtomicUsize, Ordering};
474430
use thread;
475431

@@ -713,19 +669,4 @@ mod tests {
713669
let comp: &[i32] = &[4, 2, 5];
714670
assert_eq!(&*mutex.lock().unwrap(), comp);
715671
}
716-
717-
#[test]
718-
fn test_mutex_guard_map_panic() {
719-
let mutex = Arc::new(Mutex::new(vec![1, 2]));
720-
let mutex2 = mutex.clone();
721-
722-
thread::spawn(move || {
723-
let _ = MutexGuard::map::<usize, _>(mutex2.lock().unwrap(), |_| panic!());
724-
}).join().unwrap_err();
725-
726-
match mutex.lock() {
727-
Ok(r) => panic!("Lock on poisioned Mutex is Ok: {:?}", &*r),
728-
Err(_) => {}
729-
};
730-
}
731672
}

0 commit comments

Comments
 (0)