From 5703f5400913f2fc294510749543ddffdc27aa50 Mon Sep 17 00:00:00 2001 From: Raoul Strackx Date: Thu, 12 Oct 2023 17:23:38 +0200 Subject: [PATCH] Refactor `ReadPosition::is_past` --- intel-sgx/enclave-runner/src/usercalls/mod.rs | 8 +++- ipc-queue/src/interface_async.rs | 44 +++++++++---------- ipc-queue/src/position.rs | 11 ++--- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/intel-sgx/enclave-runner/src/usercalls/mod.rs b/intel-sgx/enclave-runner/src/usercalls/mod.rs index b855bfa9..ef8800c2 100644 --- a/intel-sgx/enclave-runner/src/usercalls/mod.rs +++ b/intel-sgx/enclave-runner/src/usercalls/mod.rs @@ -983,7 +983,13 @@ impl EnclaveState { } // cleanup old cancels let read_position = usercall_queue_monitor.read_position(); - cancels.retain(|_id, wp| !read_position.is_past(wp)); + cancels.retain(|_id, wp| + if let Some(past) = read_position.is_past(wp) { + !past + } else { + false + } + ); } }); diff --git a/ipc-queue/src/interface_async.rs b/ipc-queue/src/interface_async.rs index facee410..6b442c2b 100644 --- a/ipc-queue/src/interface_async.rs +++ b/ipc-queue/src/interface_async.rs @@ -193,28 +193,28 @@ mod tests { tx.send(Identified { id: id + 2, data: TestValue(3) }).await.unwrap(); let p3 = monitor.write_position(); id += 3; - assert!(monitor.read_position().is_past(&p0) == false); - assert!(monitor.read_position().is_past(&p1) == false); - assert!(monitor.read_position().is_past(&p2) == false); - assert!(monitor.read_position().is_past(&p3) == false); + assert!(monitor.read_position().is_past(&p0) == Some(false)); + assert!(monitor.read_position().is_past(&p1) == Some(false)); + assert!(monitor.read_position().is_past(&p2) == Some(false)); + assert!(monitor.read_position().is_past(&p3) == Some(false)); rx.recv().await.unwrap(); - assert!(monitor.read_position().is_past(&p0) == true); - assert!(monitor.read_position().is_past(&p1) == false); - assert!(monitor.read_position().is_past(&p2) == false); - assert!(monitor.read_position().is_past(&p3) == false); + assert!(monitor.read_position().is_past(&p0) == Some(true)); + assert!(monitor.read_position().is_past(&p1) == Some(false)); + assert!(monitor.read_position().is_past(&p2) == Some(false)); + assert!(monitor.read_position().is_past(&p3) == Some(false)); rx.recv().await.unwrap(); - assert!(monitor.read_position().is_past(&p0) == true); - assert!(monitor.read_position().is_past(&p1) == true); - assert!(monitor.read_position().is_past(&p2) == false); - assert!(monitor.read_position().is_past(&p3) == false); + assert!(monitor.read_position().is_past(&p0) == Some(true)); + assert!(monitor.read_position().is_past(&p1) == Some(true)); + assert!(monitor.read_position().is_past(&p2) == Some(false)); + assert!(monitor.read_position().is_past(&p3) == Some(false)); rx.recv().await.unwrap(); - assert!(monitor.read_position().is_past(&p0) == true); - assert!(monitor.read_position().is_past(&p1) == true); - assert!(monitor.read_position().is_past(&p2) == true); - assert!(monitor.read_position().is_past(&p3) == false); + assert!(monitor.read_position().is_past(&p0) == Some(true)); + assert!(monitor.read_position().is_past(&p1) == Some(true)); + assert!(monitor.read_position().is_past(&p2) == Some(true)); + assert!(monitor.read_position().is_past(&p3) == Some(false)); for i in 0..1000 { let n = 1 + (i % LEN); @@ -226,12 +226,12 @@ mod tests { let p5 = monitor.write_position(); for _ in 0..n { rx.recv().await.unwrap(); - assert!(monitor.read_position().is_past(&p0) == true); - assert!(monitor.read_position().is_past(&p1) == true); - assert!(monitor.read_position().is_past(&p2) == true); - assert!(monitor.read_position().is_past(&p3) == true); - assert!(monitor.read_position().is_past(&p4) == true); - assert!(monitor.read_position().is_past(&p5) == false); + assert!(monitor.read_position().is_past(&p0) == Some(true)); + assert!(monitor.read_position().is_past(&p1) == Some(true)); + assert!(monitor.read_position().is_past(&p2) == Some(true)); + assert!(monitor.read_position().is_past(&p3) == Some(true)); + assert!(monitor.read_position().is_past(&p4) == Some(true)); + assert!(monitor.read_position().is_past(&p5) == Some(false)); } } } diff --git a/ipc-queue/src/position.rs b/ipc-queue/src/position.rs index 07d7506d..fa37e622 100644 --- a/ipc-queue/src/position.rs +++ b/ipc-queue/src/position.rs @@ -78,17 +78,18 @@ impl ReadPosition { /// A `WritePosition` can be compared to a `ReadPosition` **correctly** if /// the ring buffer wrapped around at most 2³¹ (2 to the power of 31) times. /// - /// Returns true if the read position and the write position differ by non-significant bit, - /// or if the read position is higher than the write position. - pub fn is_past(&self, write: &WritePosition) -> bool { + /// Returns `None` if the read position and the write position cannot be compared, + /// `Some(true)` if the read position is strictly higher than the write position, + /// `Some(false)` otherwise + pub fn is_past(&self, write: &WritePosition) -> Option { let (read, write) = (self.0, write.0); let hr = read & (1 << 63); let hw = write & (1 << 63); if hr == hw { - read > write + Some(read > write) } else { - true + None } } }