Эх сурвалжийг харах

Factor out helper method to wait for queue.

Andrew Walbran 1 жил өмнө
parent
commit
9a21715e0d

+ 3 - 7
src/device/blk.rs

@@ -478,7 +478,7 @@ mod tests {
     };
     use alloc::{sync::Arc, vec};
     use core::{mem::size_of, ptr::NonNull};
-    use std::{sync::Mutex, thread, time::Duration};
+    use std::{sync::Mutex, thread};
 
     #[test]
     fn config() {
@@ -551,9 +551,7 @@ mod tests {
         // Start a thread to simulate the device waiting for a read request.
         let handle = thread::spawn(move || {
             println!("Device waiting for a request.");
-            while !state.lock().unwrap().queues[usize::from(QUEUE)].notified {
-                thread::sleep(Duration::from_millis(10));
-            }
+            State::wait_until_queue_notified(&state, QUEUE);
             println!("Transmit queue was notified.");
 
             state
@@ -626,9 +624,7 @@ mod tests {
         // Start a thread to simulate the device waiting for a write request.
         let handle = thread::spawn(move || {
             println!("Device waiting for a request.");
-            while !state.lock().unwrap().queues[usize::from(QUEUE)].notified {
-                thread::sleep(Duration::from_millis(10));
-            }
+            State::wait_until_queue_notified(&state, QUEUE);
             println!("Transmit queue was notified.");
 
             state

+ 2 - 4
src/device/console.rs

@@ -246,7 +246,7 @@ mod tests {
     };
     use alloc::{sync::Arc, vec};
     use core::ptr::NonNull;
-    use std::{sync::Mutex, thread, time::Duration};
+    use std::{sync::Mutex, thread};
 
     #[test]
     fn receive() {
@@ -323,9 +323,7 @@ mod tests {
         // Start a thread to simulate the device waiting for characters.
         let handle = thread::spawn(move || {
             println!("Device waiting for a character.");
-            while !state.lock().unwrap().queues[usize::from(QUEUE_TRANSMITQ_PORT_0)].notified {
-                thread::sleep(Duration::from_millis(10));
-            }
+            State::wait_until_queue_notified(&state, QUEUE_TRANSMITQ_PORT_0);
             println!("Transmit queue was notified.");
 
             let data = state

+ 6 - 21
src/device/socket/vsock.rs

@@ -569,7 +569,7 @@ mod tests {
         volatile::ReadOnly,
     };
     use alloc::{sync::Arc, vec};
-    use core::{ptr::NonNull, time::Duration};
+    use core::ptr::NonNull;
     use std::{sync::Mutex, thread};
 
     #[test]
@@ -630,10 +630,7 @@ mod tests {
         // Start a thread to simulate the device.
         let handle = thread::spawn(move || {
             // Wait for connection request.
-            while !state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified {
-                thread::sleep(Duration::from_millis(10));
-            }
-            state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified = false;
+            State::wait_until_queue_notified(&state, TX_QUEUE_IDX);
             assert_eq!(
                 VirtioVsockHdr::read_from(
                     state
@@ -676,10 +673,7 @@ mod tests {
             );
 
             // Expect a credit update.
-            while !state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified {
-                thread::sleep(Duration::from_millis(10));
-            }
-            state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified = false;
+            State::wait_until_queue_notified(&state, TX_QUEUE_IDX);
             assert_eq!(
                 VirtioVsockHdr::read_from(
                     state
@@ -704,10 +698,7 @@ mod tests {
             );
 
             // Expect the guest to send some data.
-            while !state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified {
-                thread::sleep(Duration::from_millis(10));
-            }
-            state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified = false;
+            State::wait_until_queue_notified(&state, TX_QUEUE_IDX);
             let request = state
                 .lock()
                 .unwrap()
@@ -758,10 +749,7 @@ mod tests {
                 .write_to_queue::<QUEUE_SIZE>(RX_QUEUE_IDX, &response);
 
             // Expect a credit update.
-            while !state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified {
-                thread::sleep(Duration::from_millis(10));
-            }
-            state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified = false;
+            State::wait_until_queue_notified(&state, TX_QUEUE_IDX);
             assert_eq!(
                 VirtioVsockHdr::read_from(
                     state
@@ -786,10 +774,7 @@ mod tests {
             );
 
             // Expect a shutdown.
-            while !state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified {
-                thread::sleep(Duration::from_millis(10));
-            }
-            state.lock().unwrap().queues[usize::from(TX_QUEUE_IDX)].notified = false;
+            State::wait_until_queue_notified(&state, TX_QUEUE_IDX);
             assert_eq!(
                 VirtioVsockHdr::read_from(
                     state

+ 10 - 2
src/transport/fake.rs

@@ -4,8 +4,8 @@ use crate::{
     PhysAddr, Result,
 };
 use alloc::{sync::Arc, vec::Vec};
-use core::{any::TypeId, ptr::NonNull};
-use std::sync::Mutex;
+use core::{any::TypeId, ptr::NonNull, time::Duration};
+use std::{sync::Mutex, thread};
 
 /// A fake implementation of [`Transport`] for unit tests.
 #[derive(Debug)]
@@ -168,6 +168,14 @@ impl State {
             handler,
         )
     }
+
+    /// Waits until the given queue is notified.
+    pub fn wait_until_queue_notified(state: &Mutex<Self>, queue_index: u16) {
+        while !state.lock().unwrap().queues[usize::from(queue_index)].notified {
+            thread::sleep(Duration::from_millis(10));
+        }
+        state.lock().unwrap().queues[usize::from(queue_index)].notified = false;
+    }
 }
 
 #[derive(Clone, Debug, Default, Eq, PartialEq)]