Skip to content

Commit a2a3901

Browse files
roypatbonzini
authored andcommitted
add retry_eintr! utility macro
Add a macro that automatically retries a given I/O operation if EINTR is returned. This is a fairly common pattern in vm-memory. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
1 parent 974ff4a commit a2a3901

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/io.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ use std::io::Stdout;
1414
#[cfg(feature = "rawfd")]
1515
use std::os::fd::AsRawFd;
1616

17+
macro_rules! retry_eintr {
18+
($io_call: expr) => {
19+
loop {
20+
let r = $io_call;
21+
22+
if let Err(crate::VolatileMemoryError::IOError(ref err)) = r {
23+
if err.kind() == std::io::ErrorKind::Interrupted {
24+
continue;
25+
}
26+
}
27+
28+
break r;
29+
}
30+
};
31+
}
32+
33+
pub(crate) use retry_eintr;
34+
1735
/// A version of the standard library's [`Read`](std::io::Read) trait that operates on volatile
1836
/// memory instead of slices
1937
///
@@ -44,10 +62,7 @@ pub trait ReadVolatile {
4462
let mut partial_buf = buf.offset(0)?;
4563

4664
while !partial_buf.is_empty() {
47-
match self.read_volatile(&mut partial_buf) {
48-
Err(VolatileMemoryError::IOError(err)) if err.kind() == ErrorKind::Interrupted => {
49-
continue
50-
}
65+
match retry_eintr!(self.read_volatile(&mut partial_buf)) {
5166
Ok(0) => {
5267
return Err(VolatileMemoryError::IOError(std::io::Error::new(
5368
ErrorKind::UnexpectedEof,
@@ -93,10 +108,7 @@ pub trait WriteVolatile {
93108
let mut partial_buf = buf.offset(0)?;
94109

95110
while !partial_buf.is_empty() {
96-
match self.write_volatile(&partial_buf) {
97-
Err(VolatileMemoryError::IOError(err)) if err.kind() == ErrorKind::Interrupted => {
98-
continue
99-
}
111+
match retry_eintr!(self.write_volatile(&partial_buf)) {
100112
Ok(0) => {
101113
return Err(VolatileMemoryError::IOError(std::io::Error::new(
102114
ErrorKind::WriteZero,

0 commit comments

Comments
 (0)