From c842b3ee2db600d404ce39191345f87547c7236d Mon Sep 17 00:00:00 2001 From: Frederick Mayle Date: Thu, 9 Mar 2023 18:06:57 -0800 Subject: [PATCH] cros_async: block_on: simplify errno handling Change-Id: I9971911239a0c2e1cc0bbebeabc687025d3c2212 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4328233 Reviewed-by: Daniel Verkamp Commit-Queue: Daniel Verkamp --- common/cros_asyncv2/src/blocking/block_on.rs | 21 ++++++++------------ cros_async/src/blocking/sys/unix/block_on.rs | 21 ++++++++------------ 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/common/cros_asyncv2/src/blocking/block_on.rs b/common/cros_asyncv2/src/blocking/block_on.rs index fff11d1e9a..b2af052a47 100644 --- a/common/cros_asyncv2/src/blocking/block_on.rs +++ b/common/cros_asyncv2/src/blocking/block_on.rs @@ -26,12 +26,6 @@ thread_local!(static PER_THREAD_WAKER: Arc = Arc::new(Waker(AtomicI32::ne #[repr(transparent)] struct Waker(AtomicI32); -extern "C" { - #[cfg_attr(target_os = "android", link_name = "__errno")] - #[cfg_attr(target_os = "linux", link_name = "__errno_location")] - fn errno_location() -> *mut libc::c_int; -} - impl ArcWake for Waker { fn wake_by_ref(arc_self: &Arc) { let state = arc_self.0.swap(WOKEN, Ordering::Release); @@ -50,9 +44,10 @@ impl ArcWake for Waker { ) }; if res < 0 { - panic!("unexpected error from FUTEX_WAKE_PRIVATE: {}", unsafe { - *errno_location() - }); + panic!( + "unexpected error from FUTEX_WAKE_PRIVATE: {}", + std::io::Error::last_os_error() + ); } } } @@ -91,10 +86,10 @@ pub fn block_on(f: F) -> F::Output { }; if res < 0 { - // Safe because libc guarantees that this is a valid pointer. - match unsafe { *errno_location() } { - libc::EAGAIN | libc::EINTR => {} - e => panic!("unexpected error from FUTEX_WAIT_PRIVATE: {}", e), + let e = std::io::Error::last_os_error(); + match e.raw_os_error() { + Some(libc::EAGAIN) | Some(libc::EINTR) => {} + _ => panic!("unexpected error from FUTEX_WAIT_PRIVATE: {}", e), } } diff --git a/cros_async/src/blocking/sys/unix/block_on.rs b/cros_async/src/blocking/sys/unix/block_on.rs index fff11d1e9a..b2af052a47 100644 --- a/cros_async/src/blocking/sys/unix/block_on.rs +++ b/cros_async/src/blocking/sys/unix/block_on.rs @@ -26,12 +26,6 @@ thread_local!(static PER_THREAD_WAKER: Arc = Arc::new(Waker(AtomicI32::ne #[repr(transparent)] struct Waker(AtomicI32); -extern "C" { - #[cfg_attr(target_os = "android", link_name = "__errno")] - #[cfg_attr(target_os = "linux", link_name = "__errno_location")] - fn errno_location() -> *mut libc::c_int; -} - impl ArcWake for Waker { fn wake_by_ref(arc_self: &Arc) { let state = arc_self.0.swap(WOKEN, Ordering::Release); @@ -50,9 +44,10 @@ impl ArcWake for Waker { ) }; if res < 0 { - panic!("unexpected error from FUTEX_WAKE_PRIVATE: {}", unsafe { - *errno_location() - }); + panic!( + "unexpected error from FUTEX_WAKE_PRIVATE: {}", + std::io::Error::last_os_error() + ); } } } @@ -91,10 +86,10 @@ pub fn block_on(f: F) -> F::Output { }; if res < 0 { - // Safe because libc guarantees that this is a valid pointer. - match unsafe { *errno_location() } { - libc::EAGAIN | libc::EINTR => {} - e => panic!("unexpected error from FUTEX_WAIT_PRIVATE: {}", e), + let e = std::io::Error::last_os_error(); + match e.raw_os_error() { + Some(libc::EAGAIN) | Some(libc::EINTR) => {} + _ => panic!("unexpected error from FUTEX_WAIT_PRIVATE: {}", e), } }