Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2317840rwd; Fri, 2 Jun 2023 07:53:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4ZaLfVMDbkTKKjtoiQt2KD0c7u0mEeQ/7LPge4zleshWLVgeCh1abFfcmGah/Y1Sp2LAuJ X-Received: by 2002:a05:6a00:3911:b0:652:dd02:f70e with SMTP id fh17-20020a056a00391100b00652dd02f70emr1370373pfb.6.1685717622834; Fri, 02 Jun 2023 07:53:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685717622; cv=none; d=google.com; s=arc-20160816; b=lvTOInmmzJVP+Bwk8y3IdIr+YaBpTg4digptkx8ewoX8kmfdKVR6KsdWvvFmQxp0WH y6Ddz12XTa8L2WCbZIfDfABL1Oq0UMIha5j2qCbL4inbNloRR36QfrFkISZiA4mYodlU N+vJeOFv1wMOFZMmnh89c2CVx2JqKew0GB/YkUw3CgK7U4mbDMaD16ejtqzkaK91/sFY uQayh9aCDOzwYoQPCBYk3AIMz8Fd9/2IwoiyDSK0/KUby9yr7Zl9YJN5oX7qxMA3Om7K Z3vn9V/ekO6DIRp3FwNTRdpTdPMZuoTZ2e7eo93Mnq/onfoXjsJt8RqB1wEmvqAkilBZ ZEjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:in-reply-to:date:subject :cc:to:from:user-agent:references:dkim-signature; bh=hJ9vneQJ4b683G/HZn1+z6+HzvNcmHcX4U7xxt8CZQM=; b=U1/z6IfYpwLZpIe4rFVDQLPr7468pZl41J4g4UXFn92tFLb+c/ebeTSiuTAVB+Wqz6 46Mnot4/BJLzXjQGHk8EgVa1FaVzmaTWFNMeJvmviUFHF0sY5How2JZSWb9CCKehC91M uQnhmIGhdrq78Si4h4hYeU4Y++HDQr95be9FHd4Vg/aNjjh0X4+/k8ael40AWjLzQhdg Ec+y94WzzyK9Xok1ZgL3gjZjYB0TNT+Abo9XW8Nklr7FFq1dkpq5I7jxV5uiuireADKr HUN6pSeatzWxVQ3mIqBJ5kLMtBPQbKVdboqMO/JA+1nSbMN0L3yrP0bMCWiQU6poQ/Zg 8jeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=yw8e+zIr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m64-20020a625843000000b00624eb57b45dsi886583pfb.74.2023.06.02.07.53.28; Fri, 02 Jun 2023 07:53:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=yw8e+zIr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236189AbjFBOob (ORCPT + 99 others); Fri, 2 Jun 2023 10:44:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236119AbjFBOo3 (ORCPT ); Fri, 2 Jun 2023 10:44:29 -0400 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36481E46 for ; Fri, 2 Jun 2023 07:44:28 -0700 (PDT) Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-970056276acso309567466b.2 for ; Fri, 02 Jun 2023 07:44:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20221208.gappssmtp.com; s=20221208; t=1685717066; x=1688309066; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:from:to:cc:subject:date:message-id:reply-to; bh=hJ9vneQJ4b683G/HZn1+z6+HzvNcmHcX4U7xxt8CZQM=; b=yw8e+zIr16AuMoV6eYfdQjAc1RLuoYXai0oH2Q1r+/dxRvIq4Qrd1wrLBkxDSk97Ef P5e8dzBAYbbqCjaR5HVf4KxjrpeqQ8inLNp6dWZrBx+Rk6JKWZWMISM4P3YTUkiszM/e eZVHzajN+5/a5SXS6FeVY96/iTQ75Bm1MH4rucFpNqZfrz/pqFEaLYTGBu9etkLl7Y9L SMpTblwnbC5Q8sr1AJh572wH1Om1uImoIM0E+ndW8+nCpSbOdKUMymkphz3dkCcTbL5u 9js/yLn7Bv24j7E88qzS4sOYIJDNHRbjbckxu3/s5UOxoWISZhO+A/1NjXMezdyLzU4p Vf4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685717066; x=1688309066; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=hJ9vneQJ4b683G/HZn1+z6+HzvNcmHcX4U7xxt8CZQM=; b=GHhAvxu5zPb2K5WWKHeHvmZdin9wzcN+Vtq+pWkpZCHqJPHrVi7nzd/PY/9C3dV9Oq /XrnhvUmkx+HbYF5b+OOTMHEj+7ZSgM/Ozypfctiy361ntV/JUIF72Kx1s/QJZsmjXxA uzXBHPAwhpJrPXUGd6MznxbYUJxfc6MbSU2GOMBi+491kl6DUsGcscOs+fA3w9hYkBlq A4avIIG7WpmH96xdQmcMMpcI0dvStaKSwMS+4EeuV4HbdQjTw5WbnUb2C6NH5mcLinJv wLh2q3q3+bQL4lqGFRbQ557+n7V4DKJj80AsBRFq8Rlbhupy3oWxd4AV7avxECr9az5/ zgyA== X-Gm-Message-State: AC+VfDyyi6lRKDZebMiGIPkpO0u/2ehg/Uet9hTfixxrQWYTW8Ihc+r3 7joLHdZixl50Y/+HWbLUHInrNA== X-Received: by 2002:a17:907:74d:b0:974:c1c:820b with SMTP id xc13-20020a170907074d00b009740c1c820bmr9332316ejb.51.1685717066661; Fri, 02 Jun 2023 07:44:26 -0700 (PDT) Received: from localhost ([194.62.217.2]) by smtp.gmail.com with ESMTPSA id a1-20020a1709066d4100b00974564fa7easm842690ejt.5.2023.06.02.07.44.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 07:44:26 -0700 (PDT) References: <20230601134946.3887870-1-aliceryhl@google.com> <20230601134946.3887870-9-aliceryhl@google.com> User-agent: mu4e 1.10.3; emacs 28.2.50 From: "Andreas Hindborg (Samsung)" To: Alice Ryhl 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 , Benno Lossin , linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH v2 8/8] rust: workqueue: add examples Date: Fri, 02 Jun 2023 16:44:15 +0200 In-reply-to: <20230601134946.3887870-9-aliceryhl@google.com> Message-ID: <877csl2aba.fsf@metaspace.dk> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,PDS_OTHER_BAD_TLD,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Alice Ryhl writes: > This adds two examples of how to use the workqueue. The first example > shows how to use it when you only have one `work_struct` field, and the > second example shows how to use it when you have multiple `work_struct` > fields. > > Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg (Samsung) > --- > rust/kernel/workqueue.rs | 104 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 104 insertions(+) > > diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs > index c302e8b8624b..cefcf43ff40e 100644 > --- a/rust/kernel/workqueue.rs > +++ b/rust/kernel/workqueue.rs > @@ -26,6 +26,110 @@ > //! * The `WorkItemPointer` trait is implemented for the pointer type that points at a something > //! that implements `WorkItem`. > //! > +//! ## Example > +//! > +//! This example defines a struct that holds an integer and can be scheduled on the workqueue. When > +//! the struct is executed, it will print the integer. Since there is only one `work_struct` field, > +//! we do not need to specify ids for the fields. > +//! > +//! ``` > +//! use kernel::prelude::*; > +//! use kernel::sync::Arc; > +//! use kernel::workqueue::{self, Work, WorkItem}; > +//! > +//! #[pin_data] > +//! struct MyStruct { > +//! value: i32, > +//! #[pin] > +//! work: Work, > +//! } > +//! > +//! impl_has_work! { > +//! impl HasWork for MyStruct { self.work } > +//! } > +//! > +//! impl MyStruct { > +//! fn new(value: i32) -> Result> { > +//! Arc::pin_init(pin_init!(MyStruct { > +//! value, > +//! work <- Work::new(), > +//! })) > +//! } > +//! } > +//! > +//! impl WorkItem for MyStruct { > +//! type Pointer = Arc; > +//! > +//! fn run(this: Arc) { > +//! pr_info!("The value is: {}", this.value); > +//! } > +//! } > +//! > +//! /// This method will enqueue the struct for execution on the system workqueue, where its value > +//! /// will be printed. > +//! fn print_later(val: Arc) { > +//! let _ = workqueue::system().enqueue(val); > +//! } > +//! ``` > +//! > +//! The following example shows how multiple `work_struct` fields can be used: > +//! > +//! ``` > +//! use kernel::prelude::*; > +//! use kernel::sync::Arc; > +//! use kernel::workqueue::{self, Work, WorkItem}; > +//! > +//! #[pin_data] > +//! struct MyStruct { > +//! value_1: i32, > +//! value_2: i32, > +//! #[pin] > +//! work_1: Work, > +//! #[pin] > +//! work_2: Work, > +//! } > +//! > +//! impl_has_work! { > +//! impl HasWork for MyStruct { self.work_1 } > +//! impl HasWork for MyStruct { self.work_2 } > +//! } > +//! > +//! impl MyStruct { > +//! fn new(value_1: i32, value_2: i32) -> Result> { > +//! Arc::pin_init(pin_init!(MyStruct { > +//! value_1, > +//! value_2, > +//! work_1 <- Work::new(), > +//! work_2 <- Work::new(), > +//! })) > +//! } > +//! } > +//! > +//! impl WorkItem<1> for MyStruct { > +//! type Pointer = Arc; > +//! > +//! fn run(this: Arc) { > +//! pr_info!("The value is: {}", this.value_1); > +//! } > +//! } > +//! > +//! impl WorkItem<2> for MyStruct { > +//! type Pointer = Arc; > +//! > +//! fn run(this: Arc) { > +//! pr_info!("The second value is: {}", this.value_2); > +//! } > +//! } > +//! > +//! fn print_1_later(val: Arc) { > +//! let _ = workqueue::system().enqueue::, 1>(val); > +//! } > +//! > +//! fn print_2_later(val: Arc) { > +//! let _ = workqueue::system().enqueue::, 2>(val); > +//! } > +//! ``` > +//! > //! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h) > > use crate::{bindings, prelude::*, sync::Arc, types::Opaque};