From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 AE3AE14F70 for ; Fri, 1 Aug 2025 09:05:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754039110; cv=none; b=P1DqmRL/ZCNJ41/ICrJpamJC6AT0pCzKfo3IGFlgvAqgcaPP/F521UNgCPeGX9LIwvbs+YSvEGkxJSji34kKHLEx7aMSmA+W3YWUDO9Ah7Vb83aWykllfPchcdcCbMSri2QFAzKqZvu5Bamhh9+0wLJyzYPlj01Urn588/TyMcU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754039110; c=relaxed/simple; bh=7kG/HUcTp9/Qvr5h6tdmy/wUlx3CrjfsxU2jp4kAN6A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=A/zdKh9b2gqbTAkOaZQz8Sfx9Nm51FEQ4VlRlm4SOzTLQ34VUlFnrqxRmAfpG4caKNdCNj9+ESSl46S7Hy7hvCA3SqD7X0SObErJr9h2+nltgB5Bwm+y9L0owa4XY+piDrGevshYiaXncJy7zaziJO1lk5h3QS+1qmiQ9iIl08I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yr4nkXYr; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yr4nkXYr" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-458a31421a3so6202675e9.0 for ; Fri, 01 Aug 2025 02:05:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754039107; x=1754643907; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CVIXz4OClJs/jKNYaFwgOLlddhdlcUfndXSfXzIRugU=; b=yr4nkXYrXZsMif5bb5BJdrE3vkcrehwajutdZ6ZLDpuAlETiNTpAlBl/AdAHgTg+kM BtAlS+fhZG7AIyq6SbJz0BmuXEuJJ77aY6j0TMGwJZ+8xPdWxEh7MlJhkQ4vpZOp66ML GsmbtCVDLz88CfT8MfToT+fPiUjqkPNHNaq6yeL7cw2M3vFrpLW5G56hk2a3mq2HrdRy rIKhEHil4iaNeYUm4Gaq8KHyz2NqXSIFq6KgiPd6XciAT489g7jiBAHCN+28me9uLzzt 86ocLd7nFWa96rmQjiNYCR7vbo2aGygvH1YHu/6NvlVDD/02T6wuUG0qWTPMBScWyYip qeqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754039107; x=1754643907; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CVIXz4OClJs/jKNYaFwgOLlddhdlcUfndXSfXzIRugU=; b=NZrp7Hl+y+pDuDNBHPo8P0ZWudzt/Tb2o+O5Fc1Nm1GIrHOvOpznaS+DrLPiEl4Xn/ VzKstcZTESXebcj4750MX112+/r97+OnrnrfojC4fmqqtGylGJRd1cycBkVQeBycWK2/ GGYer+9n/Koz65KzoLg/UPByLIzV6AewE/EYOgALU1mUtaoCi3OwAnp/jVC6Y2zlhH8G m+dbvfcKFTYrURjWCiIP/ZjEifqMfckJMPjjGSF1KoEJxPuUFkOMT8cQp9P1dqi23oQb 61mSBEisfo9Oml/NYiDGrjR8CXwPODiLZZ1C28sU67hclHs/aCfzH9j7HUFfBEFqVAHm nWbQ== X-Gm-Message-State: AOJu0YzUii0KBkQDP7B2tY7MZoBZwNn7UNPOKcIiW5oghCsetHHmkAYe y7qi8h/ke+lG7QLogGQvoU/6Av7oMzxG1qQWRmLItoEhcawOMGHSqwgzJDXXuKAtUMPQ13y2bbj u4YnCqTZXTIHIte1wIw== X-Google-Smtp-Source: AGHT+IGEHg2PiEkyL1lIyZM0NnEVAwjk72qlAnQYVQaHVIxWbAZGwCkMPKNfs8tba0IRFEV6/LDdvg0jCCa1GOk= X-Received: from wmbay35.prod.google.com ([2002:a05:600c:1e23:b0:442:f9fc:2a02]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4ed3:b0:456:1e5a:8879 with SMTP id 5b1f17b1804b1-458aa31fbe8mr18057495e9.9.1754039107176; Fri, 01 Aug 2025 02:05:07 -0700 (PDT) Date: Fri, 1 Aug 2025 09:05:06 +0000 In-Reply-To: <20250730163439.50753-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250730163439.50753-1-boqun.feng@gmail.com> Message-ID: Subject: Re: [RFC PATCH] rust: workqueue: Add an example for try_spawn() From: Alice Ryhl To: Boqun Feng Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Tejun Heo , Tamir Duberstein , Hamza Mahfooz , Alban Kurti , Joel Fernandes , "Paul E. McKenney" Content-Type: text/plain; charset="utf-8" On Wed, Jul 30, 2025 at 09:34:39AM -0700, Boqun Feng wrote: > `try_spawn()` could use an example to demonstrate the usage, and > arguably it's the most simple usage of workqueue in case someone needs a > deferred work, so add it. > > Cc: Joel Fernandes > Cc: "Paul E. McKenney" > Signed-off-by: Boqun Feng > --- > Miguel, Alice and Tejun, while I'm at it, should we also rename the > function to `spawn()` because of the motivation mentioned here [1]? > > [1]: https://rust-for-linux.zulipchat.com/#narrow/channel/291566-Library/topic/.60new.60.20or.20.60try_new.60.3F/near/529533317 Renaming sounds good to me. > Also I find the `{ || { } }` is really good if I only need to > clone the Arc for passing to a callback closure, but I'm not sure how > people feel about it, so criticism is welcome ;-) It's a neat trick, but I think it risks confusing people. I would probably not include it in an example like this one. > rust/kernel/workqueue.rs | 27 +++++++++++++++++++++++++++ > 1 file changed, 27 insertions(+) > > diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs > index b9343d5bc00f..59c1a5e14d12 100644 > --- a/rust/kernel/workqueue.rs > +++ b/rust/kernel/workqueue.rs > @@ -331,6 +331,33 @@ pub fn enqueue_delayed(&self, w: W, delay: Jiffies) -> W::Enqu > /// Tries to spawn the given function or closure as a work item. > /// > /// This method can fail because it allocates memory to store the work item. > + /// > + /// # Examples > + /// > + /// ``` > + /// use kernel::{alloc::flags, sync::{Arc, Completion, new_spinlock}, workqueue}; > + /// > + /// let work_done = Arc::pin_init(Completion::new(), flags::GFP_KERNEL)?; > + /// let data = Arc::pin_init(new_spinlock!(0), flags::GFP_KERNEL)?; > + /// > + /// workqueue::system().try_spawn( > + /// flags::GFP_KERNEL, > + /// { > + /// let work_done = work_done.clone(); > + /// let data = data.clone(); > + /// move || { > + /// *data.lock() = 42; > + /// work_done.complete_all(); > + /// } > + /// } > + /// )?; > + /// > + /// work_done.wait_for_completion(); > + /// > + /// // `work_done` being completed implies the observation of the write of `data` in the work. > + /// assert_eq!(*data.lock(), 42); > + /// # Ok::<(), Error>(()) > + /// ``` > pub fn try_spawn( > &self, > flags: Flags, > -- > 2.39.5 (Apple Git-154) >