From 63db3e340c9445fd90830e50fc659e504d92d0ca Mon Sep 17 00:00:00 2001 From: "Hui, Chunyang" Date: Mon, 21 Feb 2022 08:41:46 +0000 Subject: [PATCH] Fix futex wait timeout with absolute time --- src/libos/src/process/syscalls.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/libos/src/process/syscalls.rs b/src/libos/src/process/syscalls.rs index c00d0994..02dc70d8 100644 --- a/src/libos/src/process/syscalls.rs +++ b/src/libos/src/process/syscalls.rs @@ -286,6 +286,28 @@ pub fn do_futex( } else { ClockID::CLOCK_MONOTONIC }; + + // From futex man page: + // for FUTEX_WAIT, timeout is interpreted as a relative value. This differs from other futex operations, where + // timeout is interpreted as an absolute value. To obtain the equivalent of FUTEX_WAIT with an absolute timeout, + // employ FUTEX_WAIT_BITSET with val3 specified as FUTEX_BITSET_MATCH_ANY. + // + // However, in Occlum, we prefer to use relative value for timeout for performance consideration because getting current time + // requires an extra ocall. + let ts = match futex_op { + FutexOp::FUTEX_WAIT => ts, + _ => { + let relative_ts = { + let absolute_ts = ts.as_duration(); + let current_ts = crate::time::do_clock_gettime(clock_id)?.as_duration(); + debug_assert!(current_ts <= absolute_ts); + + timespec_t::from(absolute_ts - current_ts) + }; + relative_ts + } + }; + Ok(Some(FutexTimeout::new(clock_id, ts))) };