Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4566049rwd; Sun, 11 Jun 2023 10:00:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ55U0nXAYQmqZbnVdbGuX5MWliFyJcC/Jb7074hhTWCX6oF9FX6gwo/qjogc4nvRwVLDtgz X-Received: by 2002:a17:90a:65c3:b0:247:6ead:d0ed with SMTP id i3-20020a17090a65c300b002476eadd0edmr6709105pjs.28.1686502835338; Sun, 11 Jun 2023 10:00:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686502835; cv=none; d=google.com; s=arc-20160816; b=osgdT5+zzCHWRnUlGEqcEnT/ugcQM/A02DnCKmZwmHoV4tvdMZmJezlyrAvMzpvP9n CB5orriJB4eYnXditKXwe5q0+vfzdYFgAeZD0vz95OmNM/+FTlDvcfLkpO0IBgxSfh+W YtsmjLqIEkmyOqLUuT5JzXf6xp0kZR8nkXuMlfD59L/lwWBAAoMmbsOpr8XhanMRqFwR 6zqsVKS8ln6nts4kfoGSTlXlJ8cIqUumRHOPprhrMuo6Z4Mo6DBKi1nbm+lf8MFZkw+w AicBM3ODbdz1xIM4Bis3mAq7SqR88cZr7ooCc7rBdnH+6pSRHFMvJJbvYyv/l7xDc7YK 6SWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=YurAWGLNntc/9cNT1yr91zktO+CDzXDduvn70G9oHJk=; b=CtrPv66Wozv6ZXJ/u0auqfSEnEdxQaiK0y3J2Pp0d2lxAq8VpuaKLYkDHwhYkHWtG5 TFjb3FuqHujY1hhpYmf9dztBBAgl7rySbzLiYfcZbsMRmgzZ9RAh1GIjqc6qtWbNqCnV Ja7q4okOcrS+Xf0w6e8c6RZDKAeajTOmkJgJo3i0EZoWNiRMDMViMMOprLfRxV7ewcXn hK5/B2I7Zv+yOpKJjiTiqamfZxcEigvE6P7ASyT+GOUaSElg7alhnmaW5Ny2sZahuuVf LG3gt1ncFJT6Bj3JY6BYrWD4DIWadNFP4t30NMh+c4lnmolTV5INyi2IVCaI2WFJ3q9x XS3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=eD9vhpBQ; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ix10-20020a170902f80a00b001b02e8d8976si1645781plb.300.2023.06.11.10.00.21; Sun, 11 Jun 2023 10:00:34 -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=@proton.me header.s=protonmail header.b=eD9vhpBQ; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232975AbjFKQPU (ORCPT + 99 others); Sun, 11 Jun 2023 12:15:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229631AbjFKQPS (ORCPT ); Sun, 11 Jun 2023 12:15:18 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A076D122; Sun, 11 Jun 2023 09:15:14 -0700 (PDT) Date: Sun, 11 Jun 2023 16:15:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1686500112; x=1686759312; bh=YurAWGLNntc/9cNT1yr91zktO+CDzXDduvn70G9oHJk=; 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=eD9vhpBQFyrNxLvHBS+KjnkfsqlBaQtD6SYaN/FSQJeEF69cO7s1afaS0qwbKGcLU nZmYRyYQoXCafeJnjVnyl7vImvupOgtg5n1xdIN2ZwHlYLJ6cZX09jGACKRRxIw7Fz +E/vERafBFwkijur/6m5ueSUJww7wXAROsQW0MTQUeA9+ceTNamzPUc8gF/iDoBxBN 3vg6WaGmRjKk0YlAgHq8VAQ1xx8oqnJqWT3KQrPejPfhTxalqGM8FXyH5gfbdIgac4 em+C50eoEGDMcAe1gojAR6+9dMfL+BImcNeM128V8DmRuDSJg9RAVsBYZvs2b9ksYb OGE3YxLYvixUw== 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 v2 8/8] rust: workqueue: add examples Message-ID: In-Reply-To: <20230601134946.3887870-9-aliceryhl@google.com> References: <20230601134946.3887870-1-aliceryhl@google.com> <20230601134946.3887870-9-aliceryhl@google.com> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,PDS_OTHER_BAD_TLD,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On 01.06.23 15:49, Alice Ryhl wrote: > 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. >=20 > Signed-off-by: Alice Ryhl Reviewed-by: Benno Lossin I really like that you added these examples! Is there some particular reason you decided to not use 0 as the first index in the second example? (you can keep it this way) --=20 Cheers, Benno > --- > rust/kernel/workqueue.rs | 104 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 104 insertions(+) >=20 > 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 sched= uled on the workqueue. When > +//! the struct is executed, it will print the integer. Since there is on= ly 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 =3D 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 _ =3D 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 =3D Arc; > +//! > +//! fn run(this: Arc) { > +//! pr_info!("The value is: {}", this.value_1); > +//! } > +//! } > +//! > +//! impl WorkItem<2> for MyStruct { > +//! type Pointer =3D Arc; > +//! > +//! fn run(this: Arc) { > +//! pr_info!("The second value is: {}", this.value_2); > +//! } > +//! } > +//! > +//! fn print_1_later(val: Arc) { > +//! let _ =3D workqueue::system().enqueue::, 1>(val); > +//! } > +//! > +//! fn print_2_later(val: Arc) { > +//! let _ =3D workqueue::system().enqueue::, 2>(val); > +//! } > +//! ``` > +//! > //! C header: [`include/linux/workqueue.h`](../../../../include/linux/w= orkqueue.h) >=20 > use crate::{bindings, prelude::*, sync::Arc, types::Opaque}; > -- > 2.41.0.rc0.172.g3f132b7071-goog >=20