Skip to contentSkip to conversation

rust: sync: Add Guard related type-aliases

PATCH Applied Lyude Paul 4 participants 8 messages Nov 20, 2024 – Dec 24, 2024

Subsystems (3)
lore
Share & tools
lore

Lyude Paul

author reviewer
#0 lore raw

cover rust: sync: Add Guard related type-aliases

This is something I originally meant to do when adding Lock::from_raw and exposing Guard::new - introduce type aliases for the various types of Guard that we can run into.

Lyude Paul (2):
rust: sync: Add MutexGuard type alias
rust: sync: Add SpinLockGuard type alias

 rust/kernel/sync.rs               | 4 ++--
 rust/kernel/sync/lock/mutex.rs    | 8 ++++++++
 rust/kernel/sync/lock/spinlock.rs | 8 ++++++++
 3 files changed, 18 insertions(+), 2 deletions(-)

base-commit: b2603f8ac8217bc59f5c7f248ac248423b9b99cb
prerequisite-patch-id: 8c65a39abe47832d0c98c9c266b4b9348fb3526a
prerequisite-patch-id: 211faf8533feec77907b0a1b9b2f788e72c5ac58

Signature · 1 line
2.47.0
author reviewer
#1 lore raw

PATCH 1/2 rust: sync: Add MutexGuard type alias

A simple helper alias for code that needs to deal with Guard types returned from Mutexes.

---
 rust/kernel/sync.rs            | 2 +-
 rust/kernel/sync/lock/mutex.rs | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
rust/kernel/sync.rs +1 -1
index 2bdd1cffcdab3..0f630714da2aa 100644
@@ -15,7 +15,7 @@
 pub use arc::{Arc, ArcBorrow, UniqueArc};
 pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
 pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
-pub use lock::mutex::{new_mutex, Mutex};
+pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
 pub use lock::spinlock::{new_spinlock, SpinLock};
 pub use locked_by::LockedBy;
 
rust/kernel/sync/lock/mutex.rs +8 -0
index 0e946ebefce12..10a70c07268dc 100644
@@ -86,6 +86,14 @@ macro_rules! new_mutex {
 /// [`struct mutex`]: srctree/include/linux/mutex.h
 pub type Mutex<T> = super::Lock<T, MutexBackend>;
 
+/// A [`Guard`] acquired from locking a [`Mutex`].
+///
+/// This is simply a type alias for a [`Guard`] returned from locking a [`Mutex`]. It will unlock
+/// the [`Mutex`] upon being dropped.
+///
+/// [`Guard`]: super::Guard
+pub type MutexGuard<'a, T> = super::Guard<'a, T, MutexBackend>;
+
 /// A kernel `struct mutex` lock backend.
 pub struct MutexBackend;
 
Signature · 1 line
2.47.0
merge bot Applied
#3 lore raw

[tip: locking/core] rust: sync: Add MutexGuard type alias

In reply to Lyude Paul
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     37624dde4768ec25d2f9798aa75bf32e18c0eae2
Gitweb:        https://git.kernel.org/tip/37624dde4768ec25d2f9798aa75bf32e18c0eae2
Author:        Lyude Paul <[email protected]>
AuthorDate:    Wed, 20 Nov 2024 17:26:28 -05:00
Committer:     Boqun Feng <[email protected]>
CommitterDate: Thu, 19 Dec 2024 14:04:42 -08:00

rust: sync: Add MutexGuard type alias

A simple helper alias for code that needs to deal with Guard types returned from Mutexes.

---
 rust/kernel/sync.rs            | 2 +-
 rust/kernel/sync/lock/mutex.rs | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
rust/kernel/sync.rs +1 -1
index 1eab7eb..2721b5c 100644
@@ -16,7 +16,7 @@ pub mod poll;
 pub use arc::{Arc, ArcBorrow, UniqueArc};
 pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
 pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
-pub use lock::mutex::{new_mutex, Mutex};
+pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
 pub use lock::spinlock::{new_spinlock, SpinLock};
 pub use locked_by::LockedBy;
 
rust/kernel/sync/lock/mutex.rs +8 -0
index 0e946eb..10a70c0 100644
@@ -86,6 +86,14 @@ pub use new_mutex;
 /// [`struct mutex`]: srctree/include/linux/mutex.h
 pub type Mutex<T> = super::Lock<T, MutexBackend>;
 
+/// A [`Guard`] acquired from locking a [`Mutex`].
+///
+/// This is simply a type alias for a [`Guard`] returned from locking a [`Mutex`]. It will unlock
+/// the [`Mutex`] upon being dropped.
+///
+/// [`Guard`]: super::Guard
+pub type MutexGuard<'a, T> = super::Guard<'a, T, MutexBackend>;
+
 /// A kernel `struct mutex` lock backend.
 pub struct MutexBackend;
 
author reviewer
#4 lore raw

PATCH 2/2 rust: sync: Add SpinLockGuard type alias

In reply to Lyude Paul

A simple helper alias for code that needs to deal with Guard types returned from SpinLocks.

---
 rust/kernel/sync.rs               | 2 +-
 rust/kernel/sync/lock/spinlock.rs | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
rust/kernel/sync.rs +1 -1
index 0f630714da2aa..b469d796747a4 100644
@@ -16,7 +16,7 @@
 pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
 pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
 pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
-pub use lock::spinlock::{new_spinlock, SpinLock};
+pub use lock::spinlock::{new_spinlock, SpinLock, SpinLockGuard};
 pub use locked_by::LockedBy;
 
 /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
rust/kernel/sync/lock/spinlock.rs +8 -0
index 9f4d128bed983..081c0220013b3 100644
@@ -87,6 +87,14 @@ macro_rules! new_spinlock {
 /// A kernel `spinlock_t` lock backend.
 pub struct SpinLockBackend;
 
+/// A [`Guard`] acquired from locking a [`SpinLock`].
+///
+/// This is simply a type alias for a [`Guard`] returned from locking a [`SpinLock`]. It will unlock
+/// the [`SpinLock`] upon being dropped.
+///
+/// [`Guard`]: super::Guard
+pub type SpinLockGuard<'a, T> = super::Guard<'a, T, SpinLockBackend>;
+
 // SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion. `relock` uses the
 // default implementation that always calls the same locking method.
 unsafe impl super::Backend for SpinLockBackend {
Signature · 1 line
2.47.0
merge bot Applied
#6 lore raw

[tip: locking/core] rust: sync: Add SpinLockGuard type alias

In reply to Lyude Paul
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     eb5ccb038284dc0e69822d71aafcbf7b57394aad
Gitweb:        https://git.kernel.org/tip/eb5ccb038284dc0e69822d71aafcbf7b57394aad
Author:        Lyude Paul <[email protected]>
AuthorDate:    Wed, 20 Nov 2024 17:26:29 -05:00
Committer:     Boqun Feng <[email protected]>
CommitterDate: Thu, 19 Dec 2024 14:04:42 -08:00

rust: sync: Add SpinLockGuard type alias

A simple helper alias for code that needs to deal with Guard types returned from SpinLocks.

---
 rust/kernel/sync.rs               | 2 +-
 rust/kernel/sync/lock/spinlock.rs | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
rust/kernel/sync.rs +1 -1
index 2721b5c..dffdaad 100644
@@ -17,7 +17,7 @@ pub use arc::{Arc, ArcBorrow, UniqueArc};
 pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
 pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
 pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
-pub use lock::spinlock::{new_spinlock, SpinLock};
+pub use lock::spinlock::{new_spinlock, SpinLock, SpinLockGuard};
 pub use locked_by::LockedBy;
 
 /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
rust/kernel/sync/lock/spinlock.rs +8 -0
index 9f4d128..081c022 100644
@@ -87,6 +87,14 @@ pub type SpinLock<T> = super::Lock<T, SpinLockBackend>;
 /// A kernel `spinlock_t` lock backend.
 pub struct SpinLockBackend;
 
+/// A [`Guard`] acquired from locking a [`SpinLock`].
+///
+/// This is simply a type alias for a [`Guard`] returned from locking a [`SpinLock`]. It will unlock
+/// the [`SpinLock`] upon being dropped.
+///
+/// [`Guard`]: super::Guard
+pub type SpinLockGuard<'a, T> = super::Guard<'a, T, SpinLockBackend>;
+
 // SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion. `relock` uses the
 // default implementation that always calls the same locking method.
 unsafe impl super::Backend for SpinLockBackend {
#7 lore raw
In reply to Lyude Paul
Quoted 8 lines · Lyude Paul

This is something I originally meant to do when adding Lock::from_raw and exposing Guard::new - introduce type aliases for the various types of Guard that we can run into.

Lyude Paul (2):
rust: sync: Add MutexGuard type alias
rust: sync: Add SpinLockGuard type alias

Queued for future tests and reviews, thanks!

Regards,
Boqun

Quoted 12 lines
 rust/kernel/sync.rs               | 4 ++--
 rust/kernel/sync/lock/mutex.rs    | 8 ++++++++
 rust/kernel/sync/lock/spinlock.rs | 8 ++++++++
 3 files changed, 18 insertions(+), 2 deletions(-)

base-commit: b2603f8ac8217bc59f5c7f248ac248423b9b99cb
prerequisite-patch-id: 8c65a39abe47832d0c98c9c266b4b9348fb3526a
prerequisite-patch-id: 211faf8533feec77907b0a1b9b2f788e72c5ac58

Signature · 1 line
2.47.0