Skip to content

Commit 31fa6de

Browse files
authored
Merge pull request #796 from wedsonaf/workqueue
rust: add support for work queues
2 parents c810c92 + 6a4300b commit 31fa6de

File tree

7 files changed

+546
-8
lines changed

7 files changed

+546
-8
lines changed

include/linux/workqueue.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,24 +221,31 @@ static inline unsigned int work_static(struct work_struct *work) { return 0; }
221221
* to generate better code.
222222
*/
223223
#ifdef CONFIG_LOCKDEP
224-
#define __INIT_WORK(_work, _func, _onstack) \
224+
#define __INIT_WORK_WITH_KEY(_work, _func, _onstack, _key) \
225225
do { \
226-
static struct lock_class_key __key; \
227-
\
228226
__init_work((_work), _onstack); \
229227
(_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
230-
lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, &__key, 0); \
228+
lockdep_init_map(&(_work)->lockdep_map, "(work_completion)"#_work, _key, 0); \
231229
INIT_LIST_HEAD(&(_work)->entry); \
232230
(_work)->func = (_func); \
233231
} while (0)
234-
#else
232+
235233
#define __INIT_WORK(_work, _func, _onstack) \
234+
do { \
235+
static struct lock_class_key __key; \
236+
__INIT_WORK_WITH_KEY(_work, _func, _onstack, &__key); \
237+
} while (0)
238+
#else
239+
#define __INIT_WORK_WITH_KEY(_work, _func, _onstack, _key) \
236240
do { \
237241
__init_work((_work), _onstack); \
238242
(_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
239243
INIT_LIST_HEAD(&(_work)->entry); \
240244
(_work)->func = (_func); \
241245
} while (0)
246+
247+
#define __INIT_WORK(_work, _func, _onstack) \
248+
__INIT_WORK_WITH_KEY(_work, _func, _onstack, NULL)
242249
#endif
243250

244251
#define INIT_WORK(_work, _func) \

rust/helpers.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,13 @@ unsigned int rust_helper_NF_QUEUE_NR(unsigned int n)
620620
}
621621
EXPORT_SYMBOL_GPL(rust_helper_NF_QUEUE_NR);
622622

623+
void rust_helper___INIT_WORK_WITH_KEY(struct work_struct *work,
624+
work_func_t func, bool on_stack, struct lock_class_key *key)
625+
{
626+
__INIT_WORK_WITH_KEY(work, func, on_stack, key);
627+
}
628+
EXPORT_SYMBOL_GPL(rust_helper___INIT_WORK_WITH_KEY);
629+
623630
/*
624631
* We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
625632
* as the Rust `usize` type, so we can use it in contexts where Rust

rust/kernel/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub mod revocable;
6565
pub mod security;
6666
pub mod str;
6767
pub mod task;
68+
pub mod workqueue;
6869

6970
pub mod linked_list;
7071
mod raw_list;
@@ -104,7 +105,8 @@ pub use build_error::build_error;
104105

105106
pub use crate::error::{to_result, Error, Result};
106107
pub use crate::types::{
107-
bit, bits_iter, ARef, AlwaysRefCounted, Bool, False, Mode, Opaque, ScopeGuard, True,
108+
bit, bits_iter, ARef, AlwaysRefCounted, Bool, Either, Either::Left, Either::Right, False, Mode,
109+
Opaque, ScopeGuard, True,
108110
};
109111

110112
use core::marker::PhantomData;

rust/kernel/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl LockClassKey {
6262
Self(UnsafeCell::new(MaybeUninit::uninit()))
6363
}
6464

65-
fn get(&self) -> *mut bindings::lock_class_key {
65+
pub(crate) fn get(&self) -> *mut bindings::lock_class_key {
6666
self.0.get().cast()
6767
}
6868
}

rust/kernel/types.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ impl<T, F: FnOnce(T)> Drop for ScopeGuard<T, F> {
282282
/// Stores an opaque value.
283283
///
284284
/// This is meant to be used with FFI objects that are never interpreted by Rust code.
285+
#[repr(transparent)]
285286
pub struct Opaque<T>(MaybeUninit<UnsafeCell<T>>);
286287

287288
impl<T> Opaque<T> {
@@ -677,3 +678,12 @@ impl<T: AlwaysRefCounted> Drop for ARef<T> {
677678
unsafe { T::dec_ref(self.ptr) };
678679
}
679680
}
681+
682+
/// A sum type that always holds either a value of type `L` or `R`.
683+
pub enum Either<L, R> {
684+
/// Constructs an instance of [`Either`] containing a value of type `L`.
685+
Left(L),
686+
687+
/// Constructs an instance of [`Either`] containing a value of type `R`.
688+
Right(R),
689+
}

0 commit comments

Comments
 (0)