[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250129.135314.187500982981537852.fujita.tomonori@gmail.com>
Date: Wed, 29 Jan 2025 13:53:14 +0900 (JST)
From: FUJITA Tomonori <fujita.tomonori@...il.com>
To: me@...enk.dev
Cc: fujita.tomonori@...il.com, gary@...yguo.net,
linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org,
netdev@...r.kernel.org, andrew@...n.ch, hkallweit1@...il.com,
tmgross@...ch.edu, ojeda@...nel.org, alex.gaynor@...il.com,
bjorn3_gh@...tonmail.com, benno.lossin@...ton.me, a.hindborg@...sung.com,
aliceryhl@...gle.com, anna-maria@...utronix.de, frederic@...nel.org,
tglx@...utronix.de, arnd@...db.de, jstultz@...gle.com, sboyd@...nel.org,
mingo@...hat.com, peterz@...radead.org, juri.lelli@...hat.com,
vincent.guittot@...aro.org, dietmar.eggemann@....com, rostedt@...dmis.org,
bsegall@...gle.com, mgorman@...e.de, vschneid@...hat.com,
tgunders@...hat.com
Subject: Re: [PATCH v9 7/8] rust: Add read_poll_timeout functions
On Tue, 28 Jan 2025 11:49:48 +0100
Fiona Behrens <me@...enk.dev> wrote:
>> +#[track_caller]
>> +pub fn read_poll_timeout<Op, Cond, T: Copy>(
>> + mut op: Op,
>> + mut cond: Cond,
>> + sleep_delta: Delta,
>> + timeout_delta: Option<Delta>,
>> +) -> Result<T>
>> +where
>> + Op: FnMut() -> Result<T>,
>> + Cond: FnMut(&T) -> bool,
>> +{
>> + let start = Instant::now();
>> + let sleep = !sleep_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 let Some(timeout_delta) = timeout_delta {
>> + if start.elapsed() > timeout_delta {
>> + // Unlike the C version, we immediately return.
>> + // We have just called `op()` so we don't need to call it again.
>> + return Err(ETIMEDOUT);
>> + }
>> + }
>> + if sleep {
>> + fsleep(sleep_delta);
>> + }
>> + // fsleep() could be busy-wait loop so we always call cpu_relax().
>> + cpu_relax();
>> + }
>> +}
>
> I wonder if it makes sense to then switch `Delta` to wrap a `NonZeroI64` and forbid deltas with 0 nanoseconds with that and use the niche optimization. Not sure if we make other apis horrible by that, but this would prevent deltas that encode no time passing.
I think that there are valid use casese for a zero Delta type.
About this function, using zero Delta for sleep_delta means busy
polling. Sevaral drivers use the C version of this function in that
manner.
Using zero Delta for timeout_delta means checking a condition only
once.
Powered by blists - more mailing lists