|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Work queues. |
| 4 | +//! |
| 5 | +//! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h) |
| 6 | +
|
| 7 | +use crate::{bindings, types::Opaque}; |
| 8 | + |
| 9 | +/// A kernel work queue. |
| 10 | +/// |
| 11 | +/// Wraps the kernel's C `struct workqueue_struct`. |
| 12 | +/// |
| 13 | +/// It allows work items to be queued to run on thread pools managed by the kernel. Several are |
| 14 | +/// always available, for example, `system`, `system_highpri`, `system_long`, etc. |
| 15 | +#[repr(transparent)] |
| 16 | +pub struct Queue(Opaque<bindings::workqueue_struct>); |
| 17 | + |
| 18 | +// SAFETY: Kernel workqueues are usable from any thread. |
| 19 | +unsafe impl Send for Queue {} |
| 20 | +unsafe impl Sync for Queue {} |
| 21 | + |
| 22 | +impl Queue { |
| 23 | + /// Use the provided `struct workqueue_struct` with Rust. |
| 24 | + /// |
| 25 | + /// # Safety |
| 26 | + /// |
| 27 | + /// The caller must ensure that the provided raw pointer is not dangling, that it points at a |
| 28 | + /// valid workqueue, and that it remains valid until the end of 'a. |
| 29 | + pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue { |
| 30 | + // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The |
| 31 | + // caller promises that the pointer is not dangling. |
| 32 | + unsafe { &*(ptr as *const Queue) } |
| 33 | + } |
| 34 | + |
| 35 | + /// Enqueues a work item. |
| 36 | + /// |
| 37 | + /// This may fail if the work item is already enqueued in a workqueue. |
| 38 | + pub fn enqueue<T: WorkItem + Send + 'static>(&self, w: T) -> T::EnqueueOutput { |
| 39 | + let queue_ptr = self.0.get(); |
| 40 | + |
| 41 | + // SAFETY: There are two cases. |
| 42 | + // |
| 43 | + // 1. If `queue_work_on` returns false, then we failed to push the work item to the queue. |
| 44 | + // In this case, we don't touch the work item again. |
| 45 | + // |
| 46 | + // 2. If `queue_work_on` returns true, then we pushed the work item to the queue. The work |
| 47 | + // queue will call the function pointer in the `work_struct` at some point in the |
| 48 | + // future. We require `T` to be static, so the type has no lifetimes annotated on it. |
| 49 | + // We require `T` to be send, so there are no thread-safety issues to take care of. |
| 50 | + // |
| 51 | + // In either case we follow the safety requirements of `__enqueue`. |
| 52 | + unsafe { |
| 53 | + w.__enqueue(move |work_ptr| { |
| 54 | + bindings::queue_work_on(bindings::WORK_CPU_UNBOUND as _, queue_ptr, work_ptr) |
| 55 | + }) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// A work item. |
| 61 | +/// |
| 62 | +/// This is the low-level trait that is designed for being as general as possible. |
| 63 | +/// |
| 64 | +/// # Safety |
| 65 | +/// |
| 66 | +/// Implementers must ensure that `__enqueue` behaves as documented. |
| 67 | +pub unsafe trait WorkItem { |
| 68 | + /// The return type of [`Queue::enqueue`]. |
| 69 | + type EnqueueOutput; |
| 70 | + |
| 71 | + /// Enqueues this work item on a queue using the provided `queue_work_on` method. |
| 72 | + /// |
| 73 | + /// # Safety |
| 74 | + /// |
| 75 | + /// Calling this method guarantees that the provided closure will be called with a raw pointer |
| 76 | + /// to a `struct work_struct`. The closure should behave in the following way: |
| 77 | + /// |
| 78 | + /// 1. If the `struct work_struct` cannot be pushed to a workqueue because its already in one, |
| 79 | + /// then the closure should return `false`. It may not access the pointer after returning |
| 80 | + /// `false`. |
| 81 | + /// 2. If the `struct work_struct` is successfully added to a workqueue, then the closure |
| 82 | + /// should return `true`. When the workqueue executes the work item, it will do so by |
| 83 | + /// calling the function pointer stored in the `struct work_struct`. The work item ensures |
| 84 | + /// that the raw pointer remains valid until that happens. |
| 85 | + /// |
| 86 | + /// This method may not have any other failure cases than the closure returning `false`. The |
| 87 | + /// output type should reflect this, but it may also be an infallible type if the work item |
| 88 | + /// statically ensures that pushing the `struct work_struct` will succeed. |
| 89 | + /// |
| 90 | + /// If the work item type is annotated with any lifetimes, then the workqueue must call the |
| 91 | + /// function pointer before any such lifetime expires. (Or it may forget the work item and |
| 92 | + /// never call the function pointer at all.) |
| 93 | + /// |
| 94 | + /// If the work item type is not [`Send`], then the work item must be executed on the same |
| 95 | + /// thread as the call to `__enqueue`. |
| 96 | + unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput |
| 97 | + where |
| 98 | + F: FnOnce(*mut bindings::work_struct) -> bool; |
| 99 | +} |
0 commit comments