|
1 | 1 | use crate::sync::atomic::{AtomicUsize, Ordering};
|
2 | 2 | use crate::sync::mpsc::channel;
|
3 |
| -use crate::sync::{Arc, Condvar, Mutex}; |
| 3 | +use crate::sync::{Arc, Condvar, Mutex, MutexGuard, MappedMutexGuard}; |
4 | 4 | use crate::thread;
|
5 | 5 |
|
6 | 6 | struct Packet<T>(Arc<(Mutex<T>, Condvar)>);
|
@@ -188,6 +188,21 @@ fn test_mutex_arc_poison() {
|
188 | 188 | assert!(arc.is_poisoned());
|
189 | 189 | }
|
190 | 190 |
|
| 191 | +#[test] |
| 192 | +fn test_mutex_arc_poison_mapped() { |
| 193 | + let arc = Arc::new(Mutex::new(1)); |
| 194 | + assert!(!arc.is_poisoned()); |
| 195 | + let arc2 = arc.clone(); |
| 196 | + let _ = thread::spawn(move || { |
| 197 | + let lock = arc2.lock().unwrap(); |
| 198 | + let lock = MutexGuard::map(lock, |val| val); |
| 199 | + assert_eq!(*lock, 2); // deliberate assertion failure to poison the mutex |
| 200 | + }) |
| 201 | + .join(); |
| 202 | + assert!(arc.lock().is_err()); |
| 203 | + assert!(arc.is_poisoned()); |
| 204 | +} |
| 205 | + |
191 | 206 | #[test]
|
192 | 207 | fn test_mutex_arc_nested() {
|
193 | 208 | // Tests nested mutexes and access
|
@@ -236,3 +251,17 @@ fn test_mutex_unsized() {
|
236 | 251 | let comp: &[i32] = &[4, 2, 5];
|
237 | 252 | assert_eq!(&*mutex.lock().unwrap(), comp);
|
238 | 253 | }
|
| 254 | + |
| 255 | + |
| 256 | +#[test] |
| 257 | +fn test_mapping_mapped_guard() { |
| 258 | + let arr = [0; 4]; |
| 259 | + let mut lock = Mutex::new(arr); |
| 260 | + let guard = lock.lock().unwrap(); |
| 261 | + let guard = MutexGuard::map(guard, |arr| &mut arr[..2]); |
| 262 | + let mut guard = MappedMutexGuard::map(guard, |slice| &mut slice[1..]); |
| 263 | + assert_eq!(guard.len(), 1); |
| 264 | + guard[0] = 42; |
| 265 | + drop(guard); |
| 266 | + assert_eq!(*lock.get_mut().unwrap(), [0, 42, 0, 0]); |
| 267 | +} |
0 commit comments