|
| 1 | +use crate::Test; |
| 2 | +use io_uring::types::FutexWaitV; |
| 3 | +use io_uring::{cqueue, opcode, squeue, IoUring}; |
| 4 | +use std::sync::atomic::{AtomicU32, Ordering}; |
| 5 | +use std::sync::Arc; |
| 6 | +use std::time::Duration; |
| 7 | +use std::{io, ptr, thread}; |
| 8 | + |
| 9 | +// Not defined by libc. |
| 10 | +// |
| 11 | +// From: https://github.com/torvalds/linux/blob/v6.7/include/uapi/linux/futex.h#L63 |
| 12 | +const FUTEX2_SIZE_U32: u32 = 2; |
| 13 | + |
| 14 | +const INIT_VAL: u32 = 0xDEAD_BEEF; |
| 15 | + |
| 16 | +fn syscall_futex(futex: *const u32, op: libc::c_int, val: u32) -> io::Result<i64> { |
| 17 | + let ret = unsafe { |
| 18 | + libc::syscall( |
| 19 | + libc::SYS_futex, |
| 20 | + futex, |
| 21 | + op, |
| 22 | + val, |
| 23 | + ptr::null::<u8>(), |
| 24 | + ptr::null::<u8>(), |
| 25 | + 0u32, |
| 26 | + ) |
| 27 | + }; |
| 28 | + if ret >= 0 { |
| 29 | + Ok(ret) |
| 30 | + } else { |
| 31 | + Err(io::Error::from_raw_os_error(-ret as _)) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +pub fn test_futex_wait<S: squeue::EntryMarker, C: cqueue::EntryMarker>( |
| 36 | + ring: &mut IoUring<S, C>, |
| 37 | + test: &Test, |
| 38 | +) -> anyhow::Result<()> { |
| 39 | + require!( |
| 40 | + test; |
| 41 | + test.probe.is_supported(opcode::FutexWait::CODE); |
| 42 | + ); |
| 43 | + |
| 44 | + const USER_DATA: u64 = 0xDEAD_BEEF_DEAD_BEEF; |
| 45 | + |
| 46 | + println!("test futex_wait"); |
| 47 | + |
| 48 | + let mut futex = INIT_VAL; |
| 49 | + |
| 50 | + let futex_wait_e = opcode::FutexWait::new( |
| 51 | + &futex, |
| 52 | + INIT_VAL as u64, |
| 53 | + // NB. FUTEX_BITSET_MATCH_ANY is signed. We are operating on 32-bit futex, thus this mask |
| 54 | + // must be 32-bit. Converting directly from c_int to u64 will yield `u64::MAX`, which is |
| 55 | + // invalid. |
| 56 | + libc::FUTEX_BITSET_MATCH_ANY as u32 as u64, |
| 57 | + FUTEX2_SIZE_U32, |
| 58 | + ); |
| 59 | + |
| 60 | + unsafe { |
| 61 | + let mut queue = ring.submission(); |
| 62 | + queue |
| 63 | + .push(&futex_wait_e.build().user_data(USER_DATA).into()) |
| 64 | + .expect("queue is full"); |
| 65 | + } |
| 66 | + |
| 67 | + ring.submit()?; |
| 68 | + thread::sleep(Duration::from_millis(100)); |
| 69 | + assert_eq!(ring.completion().len(), 0); |
| 70 | + |
| 71 | + futex += 1; |
| 72 | + let ret = syscall_futex(&futex, libc::FUTEX_WAKE, 1)?; |
| 73 | + assert_eq!(ret, 1); |
| 74 | + |
| 75 | + ring.submit_and_wait(1)?; |
| 76 | + let cqes: Vec<cqueue::Entry> = ring.completion().map(Into::into).collect(); |
| 77 | + |
| 78 | + assert_eq!(cqes.len(), 1); |
| 79 | + assert_eq!(cqes[0].user_data(), USER_DATA); |
| 80 | + assert_eq!(cqes[0].result(), 0); |
| 81 | + |
| 82 | + Ok(()) |
| 83 | +} |
| 84 | + |
| 85 | +pub fn test_futex_wake<S: squeue::EntryMarker, C: cqueue::EntryMarker>( |
| 86 | + ring: &mut IoUring<S, C>, |
| 87 | + test: &Test, |
| 88 | +) -> anyhow::Result<()> { |
| 89 | + require!( |
| 90 | + test; |
| 91 | + test.probe.is_supported(opcode::FutexWake::CODE); |
| 92 | + ); |
| 93 | + |
| 94 | + const USER_DATA: u64 = 0xBEEF_DEAD_BEEF_DEAD; |
| 95 | + |
| 96 | + println!("test futex_wake"); |
| 97 | + |
| 98 | + let futex = Arc::new(AtomicU32::new(INIT_VAL)); |
| 99 | + |
| 100 | + let wait_thread = std::thread::spawn({ |
| 101 | + let futex = futex.clone(); |
| 102 | + move || { |
| 103 | + while futex.load(Ordering::Relaxed) == INIT_VAL { |
| 104 | + let ret = syscall_futex(futex.as_ptr(), libc::FUTEX_WAIT, INIT_VAL); |
| 105 | + assert_eq!(ret.unwrap(), 0); |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + thread::sleep(Duration::from_millis(100)); |
| 111 | + assert!(!wait_thread.is_finished()); |
| 112 | + |
| 113 | + futex.store(INIT_VAL + 1, Ordering::Relaxed); |
| 114 | + |
| 115 | + let futex_wake_e = opcode::FutexWake::new( |
| 116 | + futex.as_ptr(), |
| 117 | + 1, |
| 118 | + // NB. See comments above for why it cannot be a single `as u64`. |
| 119 | + libc::FUTEX_BITSET_MATCH_ANY as u32 as u64, |
| 120 | + FUTEX2_SIZE_U32, |
| 121 | + ); |
| 122 | + unsafe { |
| 123 | + let mut queue = ring.submission(); |
| 124 | + queue |
| 125 | + .push(&futex_wake_e.build().user_data(USER_DATA).into()) |
| 126 | + .expect("queue is full"); |
| 127 | + } |
| 128 | + ring.submit_and_wait(1)?; |
| 129 | + let cqes: Vec<cqueue::Entry> = ring.completion().map(Into::into).collect(); |
| 130 | + assert_eq!(cqes.len(), 1); |
| 131 | + assert_eq!(cqes[0].user_data(), USER_DATA); |
| 132 | + assert_eq!(cqes[0].result(), 1); |
| 133 | + |
| 134 | + wait_thread.join().unwrap(); |
| 135 | + |
| 136 | + Ok(()) |
| 137 | +} |
| 138 | + |
| 139 | +pub fn test_futex_waitv<S: squeue::EntryMarker, C: cqueue::EntryMarker>( |
| 140 | + ring: &mut IoUring<S, C>, |
| 141 | + test: &Test, |
| 142 | +) -> anyhow::Result<()> { |
| 143 | + require!( |
| 144 | + test; |
| 145 | + test.probe.is_supported(opcode::FutexWaitV::CODE); |
| 146 | + ); |
| 147 | + |
| 148 | + const USER_DATA: u64 = 0xDEAD_BEEF_BEEF_DEAD; |
| 149 | + |
| 150 | + println!("test futex_waitv"); |
| 151 | + |
| 152 | + const FUTEX_CNT: usize = 5; |
| 153 | + const TRIGGER_IDX: usize = 3; |
| 154 | + |
| 155 | + let mut futexes = [INIT_VAL; FUTEX_CNT]; |
| 156 | + let mut waitv = [FutexWaitV::default(); FUTEX_CNT]; |
| 157 | + for (futex, waitv) in futexes.iter().zip(&mut waitv) { |
| 158 | + *waitv = FutexWaitV::new() |
| 159 | + .val(INIT_VAL as u64) |
| 160 | + .uaddr(std::ptr::from_ref(futex) as _) |
| 161 | + .flags(FUTEX2_SIZE_U32); |
| 162 | + } |
| 163 | + |
| 164 | + let futex_waitv_e = opcode::FutexWaitV::new(waitv.as_ptr().cast(), waitv.len() as _); |
| 165 | + unsafe { |
| 166 | + let mut queue = ring.submission(); |
| 167 | + queue |
| 168 | + .push(&futex_waitv_e.build().user_data(USER_DATA).into()) |
| 169 | + .expect("queue is full"); |
| 170 | + } |
| 171 | + |
| 172 | + ring.submit()?; |
| 173 | + thread::sleep(Duration::from_millis(100)); |
| 174 | + assert_eq!(ring.completion().len(), 0); |
| 175 | + |
| 176 | + futexes[TRIGGER_IDX] += 1; |
| 177 | + let ret = syscall_futex(&futexes[TRIGGER_IDX], libc::FUTEX_WAKE, 1)?; |
| 178 | + assert_eq!(ret, 1); |
| 179 | + |
| 180 | + ring.submit_and_wait(1)?; |
| 181 | + let cqes: Vec<cqueue::Entry> = ring.completion().map(Into::into).collect(); |
| 182 | + assert_eq!(cqes.len(), 1); |
| 183 | + assert_eq!(cqes[0].user_data(), USER_DATA); |
| 184 | + assert_eq!(cqes[0].result(), TRIGGER_IDX as _); |
| 185 | + |
| 186 | + Ok(()) |
| 187 | +} |
0 commit comments