Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2326722rwd; Fri, 2 Jun 2023 08:01:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7JZpyVxwiuqQ3JbEvdv7Nr3+2JaGlJ3baSq13bAkoGyNQTcKWvTW4irllznY/lePEpD4ZQ X-Received: by 2002:a05:6a20:2588:b0:10b:dca5:74e5 with SMTP id k8-20020a056a20258800b0010bdca574e5mr10932775pzd.0.1685718078987; Fri, 02 Jun 2023 08:01:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685718078; cv=none; d=google.com; s=arc-20160816; b=D3WeqpqtBId8xEYxSADLL4ilEjGX7ZE6gbviyVoyLqBmPkr6pjFV7x6HWJINHBGty/ EKdwYxco8mDnctKhumu8tgihHKKYxHXX7wzg0r6dlJ93N9mxicRTB0KlHNX/s0fZ75ZY Zqv6rLu9jrStaEn+k0E7RDuMnPB5ro/e12im6aWCN5d/0mZZPHs2//i6ct6tjmxsIjqC J+jiIJWy2MbOmApLWnZXlOjNOTPbrgaoCXJlCcgoepkiN/d6ebyNoebWVVBXs46POz1c 94MKsnsUy/UIprDY9QBD/Vi0N6WpLR6mlw6IBfpl1zL/pjxW+BLQdjraMjAEPbtJOJPe ACLw== 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=unICLu7JCpbt/1fvuHdRa8aIDAQ2Vcl6hrm+u6Q4M5TU0Mrg0RHqyJsJFRDZAtjR76 y/GGwNezglSlIt+uC04/QFpYoOWbi3ji9DvyEU8tvlMFBXuG4TOBmC7ZGOyOLWFVBCGk LhFPtauNPeYLdeP6pdb9mUnnY3W7LONsNwpzsuj4lJ6QvJUzZ52a/RZ1/UhJ5357uccp r5Xvk1isZ8RHCObqhsovnTW/tVEB5pu7/e5seIdQ8P1y7rDfw+JF88vUJlqdaqs26x6o 39BVyzwM3pwkAzl393ETQKv6KZU0DNgHR8kjq3FIK3iNWTQyrwmkml/hVE7CK5c1V9HJ YVnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=fod6LE95; 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 s187-20020a6377c4000000b005429411d104si1031908pgc.897.2023.06.02.08.01.06; Fri, 02 Jun 2023 08:01:18 -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=fod6LE95; 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 S236219AbjFBOsq (ORCPT + 99 others); Fri, 2 Jun 2023 10:48:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236215AbjFBOsp (ORCPT ); Fri, 2 Jun 2023 10:48:45 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DB95E4E for ; Fri, 2 Jun 2023 07:48:42 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-51491b87565so3134156a12.1 for ; Fri, 02 Jun 2023 07:48:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20221208.gappssmtp.com; s=20221208; t=1685717321; x=1688309321; 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=fod6LE95FV3APy5Pg6yBDbE1/Y+vFXNmAWLeCfz4L5JBk30TL+NMVnUCdzzyz1FmGA tnAwb/pDxY6oxocRE7I92RpB1oMc4Ejd0AGm4RBYZmXgPLLbMDpQUSUJzxch0HcivYmH eeKQiKDauWuzCFuGJoK41t9I8GBIIvnUtBY3PmVgVsQ0Gxq5tBLjiVkfrdXuV433zoPH dsQNE3O+Rnb2IYR1p7VvhIGMcWM3DGsVhdqFVTwZqf2QmZZ1LKPrxOimSrdLO+ZMPKe0 LAZ+wOUz/ib1HVFZLCL609EZyw89w5tYV+UvpDTsdWHXTt1lKL2oaLhVA3CxTm5YuRBo CtZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685717321; x=1688309321; 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=FfzLpQuBQ2aZKfVC0A3CkNqFtlhlhIA0vzBBbEQfxsBG6jFNubEBSkvmn9Jh5BH1k6 zP+wey0YGYN05wh3HlOT6JYDWY7iuIaOHhBamBj4VyW1McHc+jE+VquK1NlwF+vkd+Rd FIj2vUaOwzrzVmpnKawyR5qHNnFeSlJOqTeFKmHix1aCM3LBckzhXbbFabGMslkJVos1 f2rEyHuUFqJyoFANkN7WveLqj2wFA+q50Ys01VGKIt6f7+Ncc9I6ktFEAsJHRmdA12QZ dt5sQ1LLxzkupif/D+nfX6UX1qOVNkI3lGLCKAYUVk2l/2gVB8OlMsovZ+Ib1YW77A5w q1VA== X-Gm-Message-State: AC+VfDxPIyATs7M0gsZLruP73HTWuQtgpk/MMZz9ScCwVirjfvOiuxUK v0OaMqma1S28zNr8MKjMp46zQw== X-Received: by 2002:a17:907:160d:b0:974:1c91:a751 with SMTP id hb13-20020a170907160d00b009741c91a751mr12009627ejc.29.1685717321001; Fri, 02 Jun 2023 07:48:41 -0700 (PDT) Received: from localhost ([194.62.217.2]) by smtp.gmail.com with ESMTPSA id cb2-20020a170906a44200b0097457363fc0sm851382ejb.33.2023.06.02.07.48.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 07:48:40 -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:48:14 +0200 In-reply-to: <20230601134946.3887870-9-aliceryhl@google.com> Message-ID: <87y1l1zzqw.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=unavailable 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};