|
26 | 26 | //! * The `WorkItemPointer` trait is implemented for the pointer type that points at a something
|
27 | 27 | //! that implements `WorkItem`.
|
28 | 28 | //!
|
| 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 | +//! |
29 | 135 | //! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h)
|
30 | 136 |
|
31 | 137 | use crate::{bindings, prelude::*, sync::Arc, sync::LockClassKey, types::Opaque};
|
|
0 commit comments