Skip to content

Commit be7724c

Browse files
Darksonnojeda
authored andcommitted
rust: specify when ARef is thread safe
An `ARef` behaves just like the `Arc` when it comes to thread safety, so we can reuse the thread safety comments from `Arc` here. This is necessary because without this change, the Rust compiler will assume that things are not thread safe even though they are. Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Reviewed-by: Boqun Feng <[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 d701e06 commit be7724c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

rust/kernel/types.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ pub struct ARef<T: AlwaysRefCounted> {
321321
_p: PhantomData<T>,
322322
}
323323

324+
// SAFETY: It is safe to send `ARef<T>` to another thread when the underlying `T` is `Sync` because
325+
// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
326+
// `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
327+
// mutable reference, for example, when the reference count reaches zero and `T` is dropped.
328+
unsafe impl<T: AlwaysRefCounted + Sync + Send> Send for ARef<T> {}
329+
330+
// SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync`
331+
// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
332+
// it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an
333+
// `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
334+
// example, when the reference count reaches zero and `T` is dropped.
335+
unsafe impl<T: AlwaysRefCounted + Sync + Send> Sync for ARef<T> {}
336+
324337
impl<T: AlwaysRefCounted> ARef<T> {
325338
/// Creates a new instance of [`ARef`].
326339
///

0 commit comments

Comments
 (0)