Skip to content

Commit

Permalink
Refactor ReadPosition::is_past
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulstrackx committed Oct 12, 2023
1 parent 9376cef commit 5703f54
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
8 changes: 7 additions & 1 deletion intel-sgx/enclave-runner/src/usercalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
);
}
});

Expand Down
44 changes: 22 additions & 22 deletions ipc-queue/src/interface_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions ipc-queue/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
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
}
}
}

0 comments on commit 5703f54

Please sign in to comment.