Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit a6ab1c4

Browse files
DarksonnTreehugger Robot
authored and
Treehugger Robot
committed
UPSTREAM: rust: workqueue: add examples
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 <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: "Andreas Hindborg (Samsung)" <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Boqun Feng <[email protected]> Signed-off-by: Tejun Heo <[email protected]> [ Upstream commit 15b286d ] Change-Id: Ib50b5e4deace28b788269e5f01b74b6439c16caf Signed-off-by: Alice Ryhl <[email protected]>
1 parent 3525914 commit a6ab1c4

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

rust/kernel/workqueue.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,112 @@
2626
//! * The `WorkItemPointer` trait is implemented for the pointer type that points at a something
2727
//! that implements `WorkItem`.
2828
//!
29+
//! ## Example
30+
//!
31+
//! This example defines a struct that holds an integer and can be scheduled on the workqueue. When
32+
//! the struct is executed, it will print the integer. Since there is only one `work_struct` field,
33+
//! we do not need to specify ids for the fields.
34+
//!
35+
//! ```
36+
//! use kernel::prelude::*;
37+
//! use kernel::sync::Arc;
38+
//! use kernel::workqueue::{self, Work, WorkItem};
39+
//! use kernel::{impl_has_work, new_work};
40+
//!
41+
//! #[pin_data]
42+
//! struct MyStruct {
43+
//! value: i32,
44+
//! #[pin]
45+
//! work: Work<MyStruct>,
46+
//! }
47+
//!
48+
//! impl_has_work! {
49+
//! impl HasWork<Self> for MyStruct { self.work }
50+
//! }
51+
//!
52+
//! impl MyStruct {
53+
//! fn new(value: i32) -> Result<Arc<Self>> {
54+
//! Arc::pin_init(pin_init!(MyStruct {
55+
//! value,
56+
//! work <- new_work!("MyStruct::work"),
57+
//! }))
58+
//! }
59+
//! }
60+
//!
61+
//! impl WorkItem for MyStruct {
62+
//! type Pointer = Arc<MyStruct>;
63+
//!
64+
//! fn run(this: Arc<MyStruct>) {
65+
//! pr_info!("The value is: {}", this.value);
66+
//! }
67+
//! }
68+
//!
69+
//! /// This method will enqueue the struct for execution on the system workqueue, where its value
70+
//! /// will be printed.
71+
//! fn print_later(val: Arc<MyStruct>) {
72+
//! let _ = workqueue::system().enqueue(val);
73+
//! }
74+
//! ```
75+
//!
76+
//! The following example shows how multiple `work_struct` fields can be used:
77+
//!
78+
//! ```
79+
//! use kernel::prelude::*;
80+
//! use kernel::sync::Arc;
81+
//! use kernel::workqueue::{self, Work, WorkItem};
82+
//! use kernel::{impl_has_work, new_work};
83+
//!
84+
//! #[pin_data]
85+
//! struct MyStruct {
86+
//! value_1: i32,
87+
//! value_2: i32,
88+
//! #[pin]
89+
//! work_1: Work<MyStruct, 1>,
90+
//! #[pin]
91+
//! work_2: Work<MyStruct, 2>,
92+
//! }
93+
//!
94+
//! impl_has_work! {
95+
//! impl HasWork<Self, 1> for MyStruct { self.work_1 }
96+
//! impl HasWork<Self, 2> for MyStruct { self.work_2 }
97+
//! }
98+
//!
99+
//! impl MyStruct {
100+
//! fn new(value_1: i32, value_2: i32) -> Result<Arc<Self>> {
101+
//! Arc::pin_init(pin_init!(MyStruct {
102+
//! value_1,
103+
//! value_2,
104+
//! work_1 <- new_work!("MyStruct::work_1"),
105+
//! work_2 <- new_work!("MyStruct::work_2"),
106+
//! }))
107+
//! }
108+
//! }
109+
//!
110+
//! impl WorkItem<1> for MyStruct {
111+
//! type Pointer = Arc<MyStruct>;
112+
//!
113+
//! fn run(this: Arc<MyStruct>) {
114+
//! pr_info!("The value is: {}", this.value_1);
115+
//! }
116+
//! }
117+
//!
118+
//! impl WorkItem<2> for MyStruct {
119+
//! type Pointer = Arc<MyStruct>;
120+
//!
121+
//! fn run(this: Arc<MyStruct>) {
122+
//! pr_info!("The second value is: {}", this.value_2);
123+
//! }
124+
//! }
125+
//!
126+
//! fn print_1_later(val: Arc<MyStruct>) {
127+
//! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 1>(val);
128+
//! }
129+
//!
130+
//! fn print_2_later(val: Arc<MyStruct>) {
131+
//! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 2>(val);
132+
//! }
133+
//! ```
134+
//!
29135
//! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h)
30136
31137
use crate::{bindings, prelude::*, sync::Arc, sync::LockClassKey, types::Opaque};

0 commit comments

Comments
 (0)