From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 348BAC7EE23 for ; Wed, 24 May 2023 14:53:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236267AbjEXOx5 (ORCPT ); Wed, 24 May 2023 10:53:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235990AbjEXOxo (ORCPT ); Wed, 24 May 2023 10:53:44 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30788E70 for ; Wed, 24 May 2023 07:53:13 -0700 (PDT) Date: Wed, 24 May 2023 14:52:50 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1684939988; x=1685199188; bh=eSrghzPVl/vMIb4NdzZs1ocOvP6Tdr8M0tKbZLF7eVs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=Rnu8bNMaBm93xHDRKTGpLoOO13wio9NMVOcEnC9UiYVEp4iwjoqaObiHpUVn/OW4e N58XCN7EgfSRJuPTpBKSZGQSCAaT5WSC20n4WYuWCF2bwyRNfORWDHo/hwX2NNsReP rvlgjkXYdXunSgTG7+C7IXo+cge1m5PGGe61fpF/sFG1+seG4RrQ8ctzJR6QMve2aJ ySzlsJTrMx5W9gWnsDTxkFB6Wl+hNdp7bx8wINQ4Z9P24YHzXGHZ+hEnBZJfwjYMa3 eA9l2QHnt1j4Y4qg0sZqrxTG1Rcenjmf4G7khiC7EBKPuFBWBREv6v60ecawqPvgXC s1C8Tg67nHkCg== To: Alice Ryhl From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Wedson Almeida Filho , Tejun Heo , Lai Jiangshan , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH v1 7/7] rust: workqueue: add `try_spawn` helper method Message-ID: In-Reply-To: <20230517203119.3160435-8-aliceryhl@google.com> References: <20230517203119.3160435-1-aliceryhl@google.com> <20230517203119.3160435-8-aliceryhl@google.com> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wednesday, May 17th, 2023 at 22:31, Alice Ryhl wr= ote: > This adds a convenience method that lets you spawn a closure for > execution on a workqueue. This will be the most convenient way to use > workqueues, but it is fallible because it needs to allocate memory. >=20 > Signed-off-by: Alice Ryhl > --- > rust/kernel/workqueue.rs | 40 ++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 40 insertions(+) >=20 > diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs > index 007005ddcaf0..303b72efd95f 100644 > --- a/rust/kernel/workqueue.rs > +++ b/rust/kernel/workqueue.rs > @@ -57,6 +57,42 @@ impl Queue { > }) > } > } > + > + /// Tries to spawn the given function or closure as a work item. > + /// > + /// This method can fail because it allocates memory to store the wo= rk item. > + pub fn try_spawn(&self, func: T) -> Result= { Why is this `Fn()` instead of `FnOnce()`? > + let init =3D pin_init!(ClosureWork { > + work <- Work::new(), > + func: Some(func), > + }); > + > + self.enqueue(Box::pin_init(init)?); > + Ok(()) > + } > +} > + > +/// A helper type used in `try_spawn`. > +#[pin_data] > +struct ClosureWork { > + #[pin] > + work: Work>>>, > + func: Option, > +} > + > +impl ClosureWork { > + fn project(self: Pin<&mut Self>) -> &mut Option { > + // SAFETY: The `func` field is not structurally pinned. > + unsafe { &mut self.get_unchecked_mut().func } > + } > +} > + > +impl BoxWorkItem for ClosureWork { > + fn run(mut self: Pin>) { > + if let Some(func) =3D self.as_mut().project().take() { > + (func)() > + } > + } > } >=20 > /// A work item. > @@ -280,6 +316,10 @@ macro_rules! impl_has_work { > )*}; > } >=20 > +impl_has_work! { > + impl HasWork>> for ClosureWork { self.work } > +} > + > /// Declares that [`Arc`] should implement [`WorkItem`]. > /// > /// # Examples > -- > 2.40.1.606.ga4b1b128d6-goog >=20 -- Cheers, Benno