| 23 | #[cfg(feature = "std")] |
| 24 | #[test] |
| 25 | fn test_wait_wake() { |
| 26 | let lock = std::sync::Arc::new(AtomicU32::new(0)); |
| 27 | |
| 28 | match futex::wait(&lock, futex::Flags::empty(), 1, None) { |
| 29 | Ok(()) => panic!("Nobody should be waking us!"), |
| 30 | Err(Errno::AGAIN) => { |
| 31 | assert_eq!(lock.load(Ordering::SeqCst), 0, "the lock should still be 0") |
| 32 | } |
| 33 | Err(err) => panic!("{err}"), |
| 34 | } |
| 35 | |
| 36 | let other = std::thread::spawn({ |
| 37 | let lock = std::sync::Arc::clone(&lock); |
| 38 | move || { |
| 39 | std::thread::sleep(std::time::Duration::from_millis(1)); |
| 40 | lock.store(1, Ordering::SeqCst); |
| 41 | futex::wake(&lock, futex::Flags::empty(), 1).unwrap(); |
| 42 | |
| 43 | std::thread::sleep(std::time::Duration::from_millis(50)); |
| 44 | match futex::wait(&lock, futex::Flags::empty(), 1, None) { |
| 45 | Ok(()) => panic!("Nobody should be waking us now!"), |
| 46 | Err(Errno::AGAIN) => { |
| 47 | assert_eq!(lock.load(Ordering::SeqCst), 2, "the lock should now be 2") |
| 48 | } |
| 49 | Err(err) => panic!("{err}"), |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | |
| 54 | match futex::wait(&lock, futex::Flags::empty(), 0, None) { |
| 55 | Ok(()) => (), |
| 56 | Err(Errno::AGAIN) => assert_eq!(lock.load(Ordering::SeqCst), 1, "the lock should now be 1"), |
| 57 | Err(err) => panic!("{err}"), |
| 58 | } |
| 59 | |
| 60 | lock.store(2, Ordering::SeqCst); |
| 61 | futex::wake(&lock, futex::Flags::empty(), 1).unwrap(); |
| 62 | |
| 63 | other.join().unwrap(); |
| 64 | } |
| 65 | |
| 66 | // Same as `test_wait_wake` but using `waitv`. |
| 67 | #[cfg(feature = "std")] |