base: unix: timer: use mark_waited() in wait_for()

Remove some duplicated code that called read() on the timerfd in an
almost identical way in two functions by reusing mark_waited() within
wait_for().

BUG=None
TEST=cargo test -p base timer -- --include-ignored

Change-Id: I3bf4a6eb3c777aa9314c408281e4d60ff218fd2a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3652891
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Anton Romanov <romanton@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Daniel Verkamp 2022-05-17 15:30:08 -07:00 committed by Chromeos LUCI
parent 1e8aae8b8d
commit f3be99cd03

View file

@ -126,27 +126,11 @@ impl Timer {
return Ok(WaitResult::Timeout);
}
let mut count = 0u64;
// Safe because this will only modify |buf| and we check the return value.
let ret = unsafe {
libc::read(
self.as_raw_descriptor(),
&mut count as *mut _ as *mut libc::c_void,
mem::size_of_val(&count),
)
};
// EAGAIN is a valid error in the case where another thread has called timerfd_settime
// in between this thread calling ppoll and read. Since the ppoll returned originally
// without any revents it means the timer did expire, so we treat this as a
// WaitResult::Expired.
if ret < 0 {
let error = Error::last();
if error.errno() != EAGAIN {
return Err(error);
}
}
let _ = self.mark_waited()?;
Ok(WaitResult::Expired)
}