Re: [PATCH v9 7/8] rust: Add read_poll_timeout functions
From: FUJITA Tomonori
Date: Mon Jan 27 2025 - 01:32:10 EST
On Mon, 27 Jan 2025 11:46:46 +0800
Gary Guo <gary@xxxxxxxxxxx> wrote:
>> +#[track_caller]
>> +pub fn read_poll_timeout<Op, Cond, T: Copy>(
>> + mut op: Op,
>> + mut cond: Cond,
>> + sleep_delta: Delta,
>> + timeout_delta: Delta,
>> +) -> Result<T>
>> +where
>> + Op: FnMut() -> Result<T>,
>> + Cond: FnMut(&T) -> bool,
>> +{
>> + let start = Instant::now();
>> + let sleep = !sleep_delta.is_zero();
>> + let timeout = !timeout_delta.is_zero();
>> +
>> + if sleep {
>> + might_sleep(Location::caller());
>> + }
>> +
>> + loop {
>> + let val = op()?;
>> + if cond(&val) {
>> + // Unlike the C version, we immediately return.
>> + // We know the condition is met so we don't need to check again.
>> + return Ok(val);
>> + }
>> + if timeout && start.elapsed() > timeout_delta {
>
> Re-reading this again I wonder if this is the desired behaviour? Maybe
> a timeout of 0 should mean check-once instead of no timeout. The
> special-casing of 0 makes sense in C but in Rust we should use `None`
> to mean it instead?
It's the behavior of the C version; the comment of this function says:
* @timeout_us: Timeout in us, 0 means never timeout
You meant that waiting for a condition without a timeout is generally
a bad idea? If so, can we simply return EINVAL for zero Delta?