Skip to content

Commit d09a610

Browse files
Darksonnojeda
authored andcommitted
rust: task: add Send marker to Task
When a type also implements `Sync`, the meaning of `Send` is just "this type may be accessed mutably from threads other than the one it is created on". That's ok for this type. Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent be7724c commit d09a610

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

rust/kernel/task.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ macro_rules! current {
6464
#[repr(transparent)]
6565
pub struct Task(pub(crate) Opaque<bindings::task_struct>);
6666

67-
// SAFETY: It's OK to access `Task` through references from other threads because we're either
68-
// accessing properties that don't change (e.g., `pid`, `group_leader`) or that are properly
67+
// SAFETY: By design, the only way to access a `Task` is via the `current` function or via an
68+
// `ARef<Task>` obtained through the `AlwaysRefCounted` impl. This means that the only situation in
69+
// which a `Task` can be accessed mutably is when the refcount drops to zero and the destructor
70+
// runs. It is safe for that to happen on any thread, so it is ok for this type to be `Send`.
71+
unsafe impl Send for Task {}
72+
73+
// SAFETY: It's OK to access `Task` through shared references from other threads because we're
74+
// either accessing properties that don't change (e.g., `pid`, `group_leader`) or that are properly
6975
// synchronised by C code (e.g., `signal_pending`).
7076
unsafe impl Sync for Task {}
7177

0 commit comments

Comments
 (0)