From nobody Sat Oct 4 11:11:50 2025 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCB8D2066DE; Sun, 17 Aug 2025 04:48:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755406115; cv=none; b=EEVBYBxGVU3db8wlKvzYXCq5OTe9g8cSlZpj7kb8TCcdXIIl19r1Vdy3T2IllljeSQLuCjhAWIuYv4I5LXc4q/HxODAxSEqv5pM0Q9aPFtfP6/o6N41Wj+3trEEWf3msdsAL/5i1pMX7lih0vuORoogap7E0xufkmjj7n048t88= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755406115; c=relaxed/simple; bh=zR5XKh7w5gkBdupYDlaIX7+lVBe6u+YpHl1FbIn7Jlw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a1fC3//9L8HQjLmXBQ40HOynkltKU4ddPLpdYvujjIVf0JI9y+gK7k4c0DgjxZwJliUEsn4T02/j7E3EIoN6OEaDef8sEvPcRkhhILgZgWptHzoFFkev9X0eziqmlODYCGKn63hW4KPsJjXr1bKYetUH8IJqUArQAlF1ArF8XGc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QY3I7nz5; arc=none smtp.client-ip=209.85.215.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QY3I7nz5" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-b47174beccfso2708996a12.2; Sat, 16 Aug 2025 21:48:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755406113; x=1756010913; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IZeul7DWVgd1xgnUZden0askSft4WTjZ/RPAZ88zu+Q=; b=QY3I7nz5GG73VOxHAu4t6IHTk8csDfmxqlZ461hKsAlca9SHLGD67qUc/BmLzeGCyF fXxK4Bf4ktMBlSAQA3lObqUPE9En3StmHWeqIQveX8EgAWaepqlB/e6+QeOvMzqz1m3K pFd8xPRq7FDjsiTTOa6bDtbtAZtzn+7a3Tnlo/1pielIN2xw2v99ghk4GFV/TJiwATPJ kV4dy/GqMUHQ2f4LiPubHENqb5NSmjOb+toQMPV/es7whQwS/3b4g2mh78tU2WFqbw1P 395QUW0Gu5jSNoFHo+aOVDHuiCV4P7kvJAo7z9ZUhIbGHnKtAD1X2oYG2WIyA4s9aZZp /G9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755406113; x=1756010913; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IZeul7DWVgd1xgnUZden0askSft4WTjZ/RPAZ88zu+Q=; b=h7rRLUb7IBM/1939j3lu6LvBbBRsbr5mJh1RucFrrdDR2l89pFPr5/LW+nLcz79EZz 6axCx9V0TRFMc54U3m7xHf9apH6SuieX+bOx9fdhNH77tALIyg5vpSsu4cczoP7ZmWE5 Qyk7WvoOkQ3ryFzmsnsr8plw3Ar1Y/BcFXvqUEBPK0cI+XIHnSpmLIDcqLBeu2qcrrVR QfF10V5G+q9dMa7PtbtP9G11B42TT167MBTa3iVkMmQDunEfPJ9ovZJaL6LjoncAlPXA zVQX872E+Af7ulwmOnGcHDBYNqYJET1+Uf6ovz+7EZBWtSvEtd8IK2sRGz9FqcxaFYWw kung== X-Forwarded-Encrypted: i=1; AJvYcCVa3ChMW/1rD2O6W4n4xJFbQgT4Xa86ZPlJHBtQTtCUBrGtNp6eRXg6lbHif+Pj/ZoSEbdfiq447R275q1Qqjc=@vger.kernel.org, AJvYcCWmcEKeuCPnncgCnQAaL+xejYM3FeizGpN4R4BirTXS5LvfySJj/Rd0pndYD7IcXrx5mYh8EEiBJgHiL6c=@vger.kernel.org X-Gm-Message-State: AOJu0YxtWdF7Aoq0Nq1MutZ2Jbqif/F0pAxXUFYfKNJruRCLFvUDaYTb ZCETwzmXJ9thaSZ2bW9Yp2aYY4jHNlai8QPBV0tBxlSIQMC5tOJapPpd X-Gm-Gg: ASbGncvEXfaES0Jo2/gS3UopGCUwEkRsJD7DI9oMi56ByaUc35xUgD9fdMujm8LvHXc 8bZOW9QCeb5m4+FD3kmsExLS/RZUC20zub2dY/6OkI14h1s8HFlWY3+lk863M+6SukD2ap3uffc PTbzrO9m7M4MJvQAVbe2ecVSb01rSYG/hdDqydcCnlDJVGhV6IqhKGMVyx0hHwquiZLUO0NFvyh KXqcIB32OkMV71vd22NURnFG6lObi8xqzRPu01CkH9JplP0oshPbH+0KAuHUc/Kq7ZBsr7t8w5s VG3aZfOKAWF8vkYxLdDSYHfOe7lBvOAThNNvlABMdc/IwzPpUG6U0HLDVMMzGEU2RK9TNC4tC3i N841TTjzdJonoG9nOe18zJH5wXIV1uscCWx50vUKCN/yCrDuO+Sk078zRdo+dWic7LUJ2aKk= X-Google-Smtp-Source: AGHT+IFX9oAZ7mTeM4M0+jCJMK6pzIebt99KVD0HFS1uAkWC989yIHy5IR7AHDIkL/tVgrbmvsNWMg== X-Received: by 2002:a17:903:3bc5:b0:242:1b:7f05 with SMTP id d9443c01a7336-2446d725139mr116745755ad.24.1755406113031; Sat, 16 Aug 2025 21:48:33 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3234368aeadsm4740645a91.0.2025.08.16.21.48.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Aug 2025 21:48:32 -0700 (PDT) From: FUJITA Tomonori To: a.hindborg@kernel.org, alex.gaynor@gmail.com, ojeda@kernel.org Cc: aliceryhl@google.com, anna-maria@linutronix.de, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, dakr@kernel.org, frederic@kernel.org, gary@garyguo.net, jstultz@google.com, linux-kernel@vger.kernel.org, lossin@kernel.org, lyude@redhat.com, rust-for-linux@vger.kernel.org, sboyd@kernel.org, tglx@linutronix.de, tmgross@umich.edu, acourbot@nvidia.com, daniel.almeida@collabora.com Subject: [PATCH v2 1/2] rust: Add cpu_relax() helper Date: Sun, 17 Aug 2025 13:47:22 +0900 Message-ID: <20250817044724.3528968-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250817044724.3528968-1-fujita.tomonori@gmail.com> References: <20250817044724.3528968-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add cpu_relax() helper in preparation for supporting read_poll_timeout(). Reviewed-by: Alice Ryhl Reviewed-by: Andreas Hindborg Signed-off-by: FUJITA Tomonori Reviewed-by: Daniel Almeida --- rust/helpers/helpers.c | 1 + rust/helpers/processor.c | 8 ++++++++ rust/kernel/lib.rs | 1 + rust/kernel/processor.rs | 14 ++++++++++++++ 4 files changed, 24 insertions(+) create mode 100644 rust/helpers/processor.c create mode 100644 rust/kernel/processor.rs diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 7cf7fe95e41d..04598665e7c8 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -34,6 +34,7 @@ #include "pid_namespace.c" #include "platform.c" #include "poll.c" +#include "processor.c" #include "property.c" #include "rbtree.c" #include "rcu.c" diff --git a/rust/helpers/processor.c b/rust/helpers/processor.c new file mode 100644 index 000000000000..d41355e14d6e --- /dev/null +++ b/rust/helpers/processor.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void rust_helper_cpu_relax(void) +{ + cpu_relax(); +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index ed53169e795c..c098c47c1817 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -110,6 +110,7 @@ pub mod platform; pub mod prelude; pub mod print; +pub mod processor; pub mod rbtree; pub mod regulator; pub mod revocable; diff --git a/rust/kernel/processor.rs b/rust/kernel/processor.rs new file mode 100644 index 000000000000..85b49b3614dd --- /dev/null +++ b/rust/kernel/processor.rs @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Processor related primitives. +//! +//! C header: [`include/linux/processor.h`](srctree/include/linux/processo= r.h) + +/// Lower CPU power consumption or yield to a hyperthreaded twin processor. +/// +/// It also happens to serve as a compiler barrier. +#[inline] +pub fn cpu_relax() { + // SAFETY: Always safe to call. + unsafe { bindings::cpu_relax() } +} --=20 2.43.0 From nobody Sat Oct 4 11:11:50 2025 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 538622153D2; Sun, 17 Aug 2025 04:48:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755406119; cv=none; b=ZdfM1YEb3VIUqkCgp5YJVmu8pHA6wMAoweUwFwiiHGzlP7/DBsaBO/1iAalE/NcJG48MfRzvbT6CjwiNlMBmPkQBUDrx43awkNo1cjOb0QmRlxAzTurnEQTGCkr4Dt/bMhQkPhvWE6JJBHjVsXIQQyi1C2quYKGe+hp/HSIt2R8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755406119; c=relaxed/simple; bh=OfUxQNP8egEuZF0aozRSs7eNFemq+iAsPOP/S6eHncs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kF2FO88/wlbXA8ATC0zgfNJIfWmphuf+v7LQHgg1h0h0HdIx2lpw00JkaRcS2b6ZFjcqxpEo7KeUX7oE6lLQSlGah9CnWSF3kXJ5bKD40vp2VYWut/jIW6sL+xoCgMW1CKDE5w9EeJ/7yrRTZsaF1lgETV09Q24LrTeHaQx7b7g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mi8T00iU; arc=none smtp.client-ip=209.85.215.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mi8T00iU" Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-b472da8ff0eso1219069a12.3; Sat, 16 Aug 2025 21:48:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755406118; x=1756010918; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aEV5IZfQmSvRoL3MwRp7ZUYnDWVnp35CPvZMegD2V0g=; b=mi8T00iU1Gm3JCHudNtLNzValjzbBxUEwXknSZ5B/jwWI88t/LZsN1rajl3Liek8P4 j8GLEYSDta6T7mqtKtDIGtMj1qY8yoWFbsvtXN/tuQUCae0uD5ExzAKG90z1NtcMqt8a yMPxRfl+IaS3Ebf1cGII4mFt94+ER6GnjY7z1lEIa6DnwFv4t5eVVi7zMPEmIQ3724h0 K/KDxIvEQt8+hkFniAEvF2JkSWKHtGdKH3kV7XFAgQs+cYBVmvZg3m9GWBeENvG0DadY JEUQtZZoQGaZ5K0Dyg9hUBQafPdl46gFwA+xsY1h/31bD6bKsoNDk0ayxk8WWa+bMGlc CyMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755406118; x=1756010918; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aEV5IZfQmSvRoL3MwRp7ZUYnDWVnp35CPvZMegD2V0g=; b=fWWGgwlORj5y3FD12vADiLmtPlCRvIbCjQctz5qu1W0cXeUMNWX5zB2VPwPKKZzFNo yLD0NBDbVxRW3GyhdirFavRVVcJgRIe14NS+dxlEEQaaSeF4Ahl/OoMal6d5Hg86dumc y7jxhHXgdmLrf2CJ8TvwrTHwXVEDbCt9zkF2uM7k8rpP1tU9xPlvkh5LlrkMC+03ZRsO 50RcIwlGB+6u+PqHbr/3rYfSIP0b6WujqtsdAjaC85+BBtKHgiHwASqc4hwn+qk9OQl5 z/ROT/QZ4k6p9vCRw/4R/JvYcZK9+Bz9MhdCJvyWY03U6ROg4gS9FIaBY8CpZkuqM4D0 2pyQ== X-Forwarded-Encrypted: i=1; AJvYcCUO4aARKrODTJwTy1vxLiioY8T6Zl9iAcseUajSP9ueevbZEF+3UkwTxizjpF8WNFDRrQuFhOi2FiowzfTsFpg=@vger.kernel.org, AJvYcCXHqBcMeimLfJp7pqy30TyT8h/ADCdQS7CG8X9tv1q+5nbocWYZzibIMNkZHe5HOwBy44o1gE2qUNaanB8=@vger.kernel.org X-Gm-Message-State: AOJu0YxPfLFXMqwTFg9+oAJvm93pn/40gl/SI9YjtJPCI0D2JdbXgs2K q/b9GSz2KtdfXn1bXyr6M5LGg03X4O4Mbk5ithTJ8ncdr2wDNDIulhzV X-Gm-Gg: ASbGncsCOdgSiPsa9TeFjJiFVhIIG9Nr918WxuZF7COOHreiI0xMLMTjMAu2EFrXFqM JMyTzFN91XKVO/g8G1LSDESa/dyq4KJCJFW7mfvzxp0wZEOyCcWzHfTWgyBYSpNl9uJ5sDwpbLo psn272a7GEgXOmd5fAtG3UmltyIPk4S0WQHzz3szPenMHVhLX+GTiDZG8OWuuQSNZ768iPPGmuF D1AtJD8/XP0q/u3KzRZ3weayuics+QG7gUHVkylDTRSiDB/e8UwwKnuzaseMKLZfl27Hh6h1Qz8 rJC2QAEH0i0y0DVimoSgctC801DQYZs7yyWIOfjAtGiODqz9LokkVsagAErrtJ8U8Bqj9gBpChU pmKl45AGbn0S62lZznkV29NPdIwYLMBtGi0Sg+TRWvZQYm9kR+80S0FuUE6Ve29aE9G9JCAY= X-Google-Smtp-Source: AGHT+IF3mKlARKZlgu4rIfuZaOYHZe+PHuOmFUESxWQXKaN9Yfogir+setAFdgEpphUdZDi2/mDncA== X-Received: by 2002:a17:90b:4a10:b0:31e:8203:4b9d with SMTP id 98e67ed59e1d1-3234dc9ec09mr5767729a91.29.1755406117580; Sat, 16 Aug 2025 21:48:37 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3234368aeadsm4740645a91.0.2025.08.16.21.48.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Aug 2025 21:48:37 -0700 (PDT) From: FUJITA Tomonori To: a.hindborg@kernel.org, alex.gaynor@gmail.com, ojeda@kernel.org Cc: aliceryhl@google.com, anna-maria@linutronix.de, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, dakr@kernel.org, frederic@kernel.org, gary@garyguo.net, jstultz@google.com, linux-kernel@vger.kernel.org, lossin@kernel.org, lyude@redhat.com, rust-for-linux@vger.kernel.org, sboyd@kernel.org, tglx@linutronix.de, tmgross@umich.edu, acourbot@nvidia.com, daniel.almeida@collabora.com, Fiona Behrens Subject: [PATCH v2 2/2] rust: Add read_poll_timeout functions Date: Sun, 17 Aug 2025 13:47:23 +0900 Message-ID: <20250817044724.3528968-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250817044724.3528968-1-fujita.tomonori@gmail.com> References: <20250817044724.3528968-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add read_poll_timeout function which poll periodically until a condition is met or a timeout is reached. The C's read_poll_timeout (include/linux/iopoll.h) is a complicated macro and a simple wrapper for Rust doesn't work. So this implements the same functionality in Rust. The C version uses usleep_range() while the Rust version uses fsleep(), which uses the best sleep method so it works with spans that usleep_range() doesn't work nicely with. The sleep_before_read argument isn't supported since there is no user for now. It's rarely used in the C version. Reviewed-by: Andreas Hindborg Reviewed-by: Fiona Behrens Tested-by: Daniel Almeida Signed-off-by: FUJITA Tomonori Reviewed-by: Alexandre Courbot Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Tested-by: Alexandre Courbot --- rust/kernel/io.rs | 1 + rust/kernel/io/poll.rs | 96 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 rust/kernel/io/poll.rs diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs index 03b467722b86..ee182b0b5452 100644 --- a/rust/kernel/io.rs +++ b/rust/kernel/io.rs @@ -8,6 +8,7 @@ use crate::{bindings, build_assert, ffi::c_void}; =20 pub mod mem; +pub mod poll; pub mod resource; =20 pub use resource::Resource; diff --git a/rust/kernel/io/poll.rs b/rust/kernel/io/poll.rs new file mode 100644 index 000000000000..e6325725d5a3 --- /dev/null +++ b/rust/kernel/io/poll.rs @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! IO polling. +//! +//! C header: [`include/linux/iopoll.h`](srctree/include/linux/iopoll.h). + +use crate::{ + error::{code::*, Result}, + processor::cpu_relax, + task::might_sleep, + time::{delay::fsleep, Delta, Instant, Monotonic}, +}; + +/// Polls periodically until a condition is met or a timeout is reached. +/// +/// The function repeatedly executes the given operation `op` closure and +/// checks its result using the condition closure `cond`. +/// +/// If `cond` returns `true`, the function returns successfully with the r= esult of `op`. +/// Otherwise, it waits for a duration specified by `sleep_delta` +/// before executing `op` again. +/// +/// This process continues until either `cond` returns `true` or the timeo= ut, +/// specified by `timeout_delta`, is reached. If `timeout_delta` is `None`, +/// polling continues indefinitely until `cond` evaluates to `true` or an = error occurs. +/// +/// This function can only be used in a nonatomic context. +/// +/// # Examples +/// +/// ```no_run +/// use kernel::io::{Io, poll::read_poll_timeout}; +/// use kernel::time::Delta; +/// +/// const HW_READY: u16 =3D 0x01; +/// +/// fn wait_for_hardware(io: &Io) -> Result<()> { +/// match read_poll_timeout( +/// // The `op` closure reads the value of a specific status regis= ter. +/// || io.try_read16(0x1000), +/// // The `cond` closure takes a reference to the value returned = by `op` +/// // and checks whether the hardware is ready. +/// |val: &u16| *val =3D=3D HW_READY, +/// Delta::from_millis(50), +/// Delta::from_secs(3), +/// ) { +/// Ok(_) =3D> { +/// // The hardware is ready. The returned value of the `op` c= losure +/// // isn't used. +/// Ok(()) +/// } +/// Err(e) =3D> Err(e), +/// } +/// } +/// ``` +#[track_caller] +pub fn read_poll_timeout( + mut op: Op, + mut cond: Cond, + sleep_delta: Delta, + timeout_delta: Delta, +) -> Result +where + Op: FnMut() -> Result, + Cond: FnMut(&T) -> bool, +{ + let start: Instant =3D Instant::now(); + + // Unlike the C version, we always call `might_sleep()` unconditionall= y, + // as conditional calls are error-prone. We clearly separate + // `read_poll_timeout()` and `read_poll_timeout_atomic()` to aid + // tools like klint. + might_sleep(); + + loop { + let val =3D op()?; + if cond(&val) { + // Unlike the C version, we immediately return. + // We know the condition is met so we don't need to check agai= n. + return Ok(val); + } + + 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 agai= n. + return Err(ETIMEDOUT); + } + + if !sleep_delta.is_zero() { + fsleep(sleep_delta); + } + + // fsleep() could be busy-wait loop so we always call cpu_relax(). + cpu_relax(); + } +} --=20 2.43.0