From nobody Sun Oct 5 01:50:00 2025 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 0AF3526529E; Mon, 11 Aug 2025 04:11:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754885514; cv=none; b=G3NeXNF7+JevUraS/LKnx5asO+W8EO88i+KwunR2zgHqaxxwOohigvK/YxCE/h7s/jdg3YoNrWHP/J+EaYgN54MKn0G80etnAQSeIf8kR0l1Q47RrlXycLUHIzzIga3KwSqrPjYBQ5IF53UFFa83VIE6H7gij/hm+QpMnJcMevA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754885514; c=relaxed/simple; bh=vj6R7yEBEScaXFjg+uDQW3Hn6iHtgNwZH55tzH+I1lI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l4wr0I0QP77vYpO283RvawA48FOtB93MsiaITseFNt+3iR6pMMrPCmRd7I46izn1DG2r0cN7l9wZlr/Tp+jm40W0SWENTiBOjPNW8pstXGxkMCCq0Pw9BWTdiiBtnZu/c5+Nb9bfG4YpQCgniCI4Svd8szRYAOoQw+ydRdxeNVI= 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=LnyZaf1D; arc=none smtp.client-ip=209.85.215.180 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="LnyZaf1D" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-b34a6d0c9a3so4545482a12.3; Sun, 10 Aug 2025 21:11:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754885512; x=1755490312; 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=03NiMWFy1lDygEZYdIVumyD+uLWaGBh68QLZgvH0C60=; b=LnyZaf1Dy7bZI+Ixocm20Hk1971O4sz/eJJhXWr21Du3c+NOF3Y8Xt19SWSyMRoBll PImhK3xugUdXd7eQtCzQbTzb07s45AV+7CZgD7W1i0qfFnz3IFYWMQx8NV/7mLbCHIEp 437K+2hNr7cGMc09IMhXJuE+2EsEC1TMk889CHYEnavHm96QBSbnGUM0rMtwafQx+UoL 9B1t1Qz9GKH7FhvS1f9JATaAEtQJXWAKuJuyeNK7tGeufcRGHzbpTRJt7XwTrEV2HJsT AmFsHtbhpKPhtq8255aCTbKFmOvuPemsyO1aggyGMzGR76ZV6pViwJYK+JeQD6eaTpoT YAdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754885512; x=1755490312; 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=03NiMWFy1lDygEZYdIVumyD+uLWaGBh68QLZgvH0C60=; b=xMC4C8DP7+Qi5SXfXLO+dqpDgdB1C1Og394DCLnO2hVl5snlgQffVTmhOAkWfnSNJQ EVWEVvAalxSAm+koH9hAgPjLWaIDeLbB7ryoGO2FNNw1SJV/qySyZ70+FKeSZxnLgJPG E6uBfOgjeOHPWelyxXOQV75AR5ffPsJmezzstYvbRdItu/7FtnO+d53Y+ybxI7pmYWaC TbmhxDVr5FErv42xwhwi+DeHVkyiXxSmtYn86u7hk5TwMpjRqBeLsFj3dTFp7dIkwLgx YjlJ9bEa+L/MykLuCLVRNxQV4RLKgIJRRsjY6XSS6PfOHKmYHfbypS7JnAxHjZzqy0Yt jMZA== X-Forwarded-Encrypted: i=1; AJvYcCWg2aA49UMTma+l1LdaqoUq11FcxvCij4OUGhzA1zqI+i6x2ndXtel5rkZ9SO71u35aFDZrQVj5UQ08PjA=@vger.kernel.org, AJvYcCXwB+TLlW0bN6NS1W8UTGRMF64RnPUbKrlMvIC0013x3kK6DtpWXYugnsO0edrtpjV9hr0s+5tmeeKWZ94FIWs=@vger.kernel.org X-Gm-Message-State: AOJu0YxPcup+pVZKiumTf/xxeM3hOdcv33Wt5IEL4/RWFNYXxy0bvMlc wg9k/BvrQHqMof6S8940/oo0++57BPVZsMMRA1zJZ5eKX99IZkBUoyhw X-Gm-Gg: ASbGncudahwvkWjUblv/EHEyZE4z+aay6qyeHEAWEjvlq4MjRA1CaYrM1U5Qv7MdYgO FSNmarCtxw70DYctxo/bY94YhPsfvJ7XPtOFMgRf8W/jEs2imr3Jv86KiEfQPLZCyt6OKQgM0qp +beq2t6ekZ0M2O3XRjfIjiTCuZt5PmQRH2sFB5pSD2tIL9+Wg7bSpdwKUPambyJgsYF0wHVILkx pXzbndwp5ClP6QfmjhtcUjOeI7WPuOFgUTq0BPOTdFjNEe9wHKNeCre4jdtUmrWDM0HOtGA1WYl 33iVTJSRbir6bBL+RHhwTKgI3FhRoe7bl2ktkOFIftBcG+Gd7pkDNNadxWf4H+Myh0DrrgITFOK 3KFLDPqcdbpJHqmPkd5IDjw61eg8zmSqFCVv4gj5Z7PAtOytJIUZY/FGzjm9i9qALHzUIpkU= X-Google-Smtp-Source: AGHT+IFZJyXqdISeq1LYGuDoRPB/KpqNGpnz/HFb5vd+PfVIBgIoznnfXea7mfEJECpLfocW6QKn0g== X-Received: by 2002:a17:903:4b08:b0:234:d7b2:2ac3 with SMTP id d9443c01a7336-242c21a7db6mr151772175ad.20.1754885512197; Sun, 10 Aug 2025 21:11:52 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-241e899aa1asm260029025ad.122.2025.08.10.21.11.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Aug 2025 21:11:51 -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 v1 1/2] rust: Add cpu_relax() helper Date: Mon, 11 Aug 2025 13:10:37 +0900 Message-ID: <20250811041039.3231548-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250811041039.3231548-1-fujita.tomonori@gmail.com> References: <20250811041039.3231548-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(). Signed-off-by: FUJITA Tomonori Reviewed-by: Alice Ryhl Reviewed-by: Andreas Hindborg --- rust/helpers/helpers.c | 1 + rust/helpers/processor.c | 8 ++++++++ rust/kernel/lib.rs | 1 + rust/kernel/processor.rs | 13 +++++++++++++ 4 files changed, 23 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..d1db85b76243 --- /dev/null +++ b/rust/kernel/processor.rs @@ -0,0 +1,13 @@ +// 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. +pub fn cpu_relax() { + // SAFETY: Always safe to call. + unsafe { bindings::cpu_relax() } +} --=20 2.43.0 From nobody Sun Oct 5 01:50:00 2025 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (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 9655324A06D; Mon, 11 Aug 2025 04:11:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754885519; cv=none; b=BEsXx1DUJcO0poz5HFYsdygcTyvpg3SIdyUkDcUqMIcBvVefTUwAg8bEUAXc5BD/E5o0e1NOaBYMRvZ/CeNRCIFd6cD+RkyROXbp8T3UrVjln8c2d4dMvASzMIR7cv0TuEGcA4dBlk4iHucNJRexlvcn0BYr5jLRru8ZV2G1/uE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754885519; c=relaxed/simple; bh=xwXLAtceT7f6iHArT7IsFD5C5AOOE91oY0DrjN2V0Lg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F5Ir8qcvve0tTdl9KCqoSaHMXHMoab/nwlGAhdRQRmcVDzmd4tGy1WKoi+Lqu2ziMX/yql0xIImafz84fhRCEye7JR7NKRFhxYPjfwXIVtIpShn1T6f7cAoAHym0URm01xr5es3KronHyqq+qJfJwbI1g2eHW148jrugO8i5r7E= 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=Rmz+RDLt; arc=none smtp.client-ip=209.85.215.172 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="Rmz+RDLt" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-b2c4e46a89fso2860881a12.2; Sun, 10 Aug 2025 21:11:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754885517; x=1755490317; 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=ohn/TkKZtr6iwWdjA5YyNUEtgLAL94J+pfrIn12Q9tw=; b=Rmz+RDLt0XnWjwDSHvQ9XLcwI24EbtsF5XSWdhgP9w3UpNdbTxD9QrS2YYfS4yoaLt pYYfQS+I2T17D8vwMVxB3KupVcEGQudYTgY3rkkeIAwlDrtd9IKlhbvDGEbt4nZgBlAH ZfC5BXXS5YRaoMvVz672UFpvjfeQTthqogyOspcTB7WzUDVUiJlck42lr+9SBfs5Th2v DMqPMfPG/HDrw8IXgNBPiYPCwdZO0XBj4nRUp0Q5qb9YthCt7yTVBDEmEfMEGduJiO7E 2EKdjY4pBWBMrdmwRnkJq47qrQJ1lmDg71SwrEbPlsRd0RrBYxsNu99eVap5fnP8wg29 vp/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754885517; x=1755490317; 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=ohn/TkKZtr6iwWdjA5YyNUEtgLAL94J+pfrIn12Q9tw=; b=K5WCU1DMuFAFuNwcVd5pNiOSFiYsGPHFRr33ZdMWUpDgp95hyHX/WimUBZ6y7bWbl9 SD7x35/pp1z1GAY76JTkHkH+p0foz97qC1xqmbRIeM0MxCnLPeFraHejxaXcsZbMOHak /qbklXhvC1uFYut12MGjYEnhijN1bthSZ4IXXY7cmRFbSHy2CKDjVK9yKtN1BWbmRmXH eawyZ6oOV7TbYaILoM+2rme77QxjfMynozKcDEEnryo1qseoE/1AM0fgZxdDOFfiF68l x9OEnTbWcAIe+2604ZXjcvkyIylGbbOYSwmE9ySncaJXBM77pfpCBLR74gHn9fnVSRF7 ei7w== X-Forwarded-Encrypted: i=1; AJvYcCVWSc/1wptFArg41Y71fmDjXtkcpdNiRKMnQZcE1keTVX4InYohT5OoDDqcMsZlKxSZBzG9YecFB6Q1IoH8wdg=@vger.kernel.org, AJvYcCVrP+OYkhpQ+Ri+V6bFdDTquLpfcG9M5lXBtUdHsAse86H+RZSWcopkCHfoguJ8k5nTTDae1KbpC9OgXuU=@vger.kernel.org X-Gm-Message-State: AOJu0YzDK1D/wb8808BSbjpH+iEF8HXIKDJLTffWKzI9LNxtlZRQtd4M vPPDCxCrkeXTthM45OI8BTtYn2jFb4mB+4FkGJ+c55YZTq+TPktzCh32 X-Gm-Gg: ASbGncsCAE6uD1ipvP2E21j3lsuWipeZnlIWD8YkZvQNU+mWDg4v40oNXwVH7nRE1Ma jFlRKPnIuwv9/XVQOyT3OjzpeG8Hy+kJPwwtYj2q3bz9/z7WcRyPZUNg/S8LlS+BeLjZk46LcP/ xRY88XFEUAIKhaa3uhs++7uhvFgtw2m5LVsdYeqMyN2tCcUwkOTP8WC+thmr4H36xROr1W7CLRy mC/FUJYbTIMmNLkXwyYCZo8VCH5JNaGajQhAAiig3SO40i+9TDMkbX2WDBhiACu/SfXiOv/r6ag ZOWkcUo/z5sqVBKh6sHSsJuVAPPudFH3ikYFRuBMEeYY8WFCeXqedfGLBF9L2kMOtqfwFmKgR6V vaQFo/I33DN77z/k3uFs7aruZWjHDuqeKnIbOPCthHD/Wn+EaJTbJVmlqw77nLpPx9xqT2og= X-Google-Smtp-Source: AGHT+IHn/aoNaKWBvFIZYUFU+x3t0lIBf7WFt/CWGDM6WsBHEraZEMm6pVXSn2WA99T3Ulh8nZQgPg== X-Received: by 2002:a17:903:1b4e:b0:242:9bc4:f1c9 with SMTP id d9443c01a7336-242c22a0e73mr157563865ad.56.1754885516829; Sun, 10 Aug 2025 21:11:56 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-241e899aa1asm260029025ad.122.2025.08.10.21.11.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Aug 2025 21:11:56 -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 v1 2/2] rust: Add read_poll_timeout functions Date: Mon, 11 Aug 2025 13:10:38 +0900 Message-ID: <20250811041039.3231548-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250811041039.3231548-1-fujita.tomonori@gmail.com> References: <20250811041039.3231548-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 functions 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. read_poll_timeout() can only be used in a nonatomic context. This requirement is not checked by these abstractions, but it is intended that klint [1] or a similar tool will be used to check it in the future. Link: https://rust-for-linux.com/klint [1] Reviewed-by: Fiona Behrens Tested-by: Daniel Almeida Signed-off-by: FUJITA Tomonori Reviewed-by: Andreas Hindborg --- rust/kernel/time.rs | 1 + rust/kernel/time/poll.rs | 104 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 rust/kernel/time/poll.rs diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 64c8dcf548d6..ec0ec33c838c 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -28,6 +28,7 @@ =20 pub mod delay; pub mod hrtimer; +pub mod poll; =20 /// The number of nanoseconds per microsecond. pub const NSEC_PER_USEC: i64 =3D bindings::NSEC_PER_USEC as i64; diff --git a/rust/kernel/time/poll.rs b/rust/kernel/time/poll.rs new file mode 100644 index 000000000000..9cf0acb1e165 --- /dev/null +++ b/rust/kernel/time/poll.rs @@ -0,0 +1,104 @@ +// 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; +/// use kernel::time::{poll::read_poll_timeout, Delta}; +/// +/// const HW_READY: u16 =3D 0x01; +/// +/// fn wait_for_hardware(io: &Io) -> Result<()> { +/// // The `op` closure reads the value of a specific status register. +/// let op =3D || -> Result { io.try_read16(0x1000) }; +/// +/// // The `cond` closure takes a reference to the value returned by `= op` +/// // and checks whether the hardware is ready. +/// let cond =3D |val: &u16| *val =3D=3D HW_READY; +/// +/// match read_poll_timeout(op, cond, Delta::from_millis(50), Some(Del= ta::from_secs(3))) { +/// Ok(_) =3D> { +/// // The hardware is ready. The returned value of the `op`` = closure isn't used. +/// Ok(()) +/// } +/// Err(e) =3D> Err(e), +/// } +/// } +/// ``` +/// +/// ```rust +/// use kernel::sync::{SpinLock, new_spinlock}; +/// use kernel::time::Delta; +/// use kernel::time::poll::read_poll_timeout; +/// +/// let lock =3D KBox::pin_init(new_spinlock!(()), kernel::alloc::flags::G= FP_KERNEL)?; +/// let g =3D lock.lock(); +/// read_poll_timeout(|| Ok(()), |()| true, Delta::from_micros(42), Some(D= elta::from_micros(42))); +/// drop(g); +/// +/// # Ok::<(), Error>(()) +/// ``` +#[track_caller] +pub fn read_poll_timeout( + mut op: Op, + mut cond: Cond, + sleep_delta: Delta, + timeout_delta: Option, +) -> Result +where + Op: FnMut() -> Result, + Cond: FnMut(&T) -> bool, +{ + let start: Instant =3D Instant::now(); + let sleep =3D !sleep_delta.is_zero(); + + // Unlike the C version, we always call `might_sleep()`. + 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 let Some(timeout_delta) =3D 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(); + } +} --=20 2.43.0