|
| 1 | +// ignore-windows: Concurrency on Windows is not supported yet. |
| 2 | + |
| 3 | +//! Cause a panic in one thread while another thread is unwinding. This checks |
| 4 | +//! that separate threads have their own panicking state. |
| 5 | +
|
| 6 | +use std::sync::{Arc, Condvar, Mutex}; |
| 7 | +use std::thread::{spawn, JoinHandle}; |
| 8 | + |
| 9 | +struct BlockOnDrop(Option<JoinHandle<()>>); |
| 10 | + |
| 11 | +impl BlockOnDrop { |
| 12 | + fn new(handle: JoinHandle<()>) -> BlockOnDrop { |
| 13 | + BlockOnDrop(Some(handle)) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +impl Drop for BlockOnDrop { |
| 18 | + fn drop(&mut self) { |
| 19 | + eprintln!("Thread 2 blocking on thread 1"); |
| 20 | + let _ = self.0.take().unwrap().join(); |
| 21 | + eprintln!("Thread 1 has exited"); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +fn main() { |
| 26 | + let t1_started_pair = Arc::new((Mutex::new(false), Condvar::new())); |
| 27 | + let t2_started_pair = Arc::new((Mutex::new(false), Condvar::new())); |
| 28 | + |
| 29 | + let t1_continue_mutex = Arc::new(Mutex::new(())); |
| 30 | + let t1_continue_guard = t1_continue_mutex.lock(); |
| 31 | + |
| 32 | + let t1 = { |
| 33 | + let t1_started_pair = t1_started_pair.clone(); |
| 34 | + let t1_continue_mutex = t1_continue_mutex.clone(); |
| 35 | + spawn(move || { |
| 36 | + eprintln!("Thread 1 starting, will block on mutex"); |
| 37 | + let (mutex, condvar) = &*t1_started_pair; |
| 38 | + *mutex.lock().unwrap() = true; |
| 39 | + condvar.notify_one(); |
| 40 | + |
| 41 | + drop(t1_continue_mutex.lock()); |
| 42 | + panic!("panic in thread 1"); |
| 43 | + }) |
| 44 | + }; |
| 45 | + |
| 46 | + // Wait for thread 1 to signal it has started. |
| 47 | + let (t1_started_mutex, t1_started_condvar) = &*t1_started_pair; |
| 48 | + let mut t1_started_guard = t1_started_mutex.lock().unwrap(); |
| 49 | + while !*t1_started_guard { |
| 50 | + t1_started_guard = t1_started_condvar.wait(t1_started_guard).unwrap(); |
| 51 | + } |
| 52 | + eprintln!("Thread 1 reported it has started"); |
| 53 | + // Thread 1 should now be blocked waiting on t1_continue_mutex. |
| 54 | + |
| 55 | + let t2 = { |
| 56 | + let t2_started_pair = t2_started_pair.clone(); |
| 57 | + let block_on_drop = BlockOnDrop::new(t1); |
| 58 | + spawn(move || { |
| 59 | + let _ = block_on_drop; |
| 60 | + |
| 61 | + let (mutex, condvar) = &*t2_started_pair; |
| 62 | + *mutex.lock().unwrap() = true; |
| 63 | + condvar.notify_one(); |
| 64 | + |
| 65 | + panic!("panic in thread 2"); |
| 66 | + }) |
| 67 | + }; |
| 68 | + |
| 69 | + // Wait for thread 2 to signal it has started. |
| 70 | + let (t2_started_mutex, t2_started_condvar) = &*t2_started_pair; |
| 71 | + let mut t2_started_guard = t2_started_mutex.lock().unwrap(); |
| 72 | + while !*t2_started_guard { |
| 73 | + t2_started_guard = t2_started_condvar.wait(t2_started_guard).unwrap(); |
| 74 | + } |
| 75 | + eprintln!("Thread 2 reported it has started"); |
| 76 | + // Thread 2 should now have already panicked and be in the middle of |
| 77 | + // unwinding. It should now be blocked on joining thread 1. |
| 78 | + |
| 79 | + // Unlock t1_continue_mutex, and allow thread 1 to proceed. |
| 80 | + eprintln!("Unlocking mutex"); |
| 81 | + drop(t1_continue_guard); |
| 82 | + // Thread 1 will panic the next time it is scheduled. This will test the |
| 83 | + // behavior of interest to this test, whether Miri properly handles |
| 84 | + // concurrent panics in two different threads. |
| 85 | + |
| 86 | + // Block the main thread on waiting to join thread 2. Thread 2 should |
| 87 | + // already be blocked on joining thread 1, so thread 1 will be scheduled |
| 88 | + // to run next, as it is the only ready thread. |
| 89 | + assert!(t2.join().is_err()); |
| 90 | + eprintln!("Thread 2 has exited"); |
| 91 | +} |
0 commit comments