Skip to content

Commit

Permalink
Merge pull request torvalds#519 from wedsonaf/lock-unsafe
Browse files Browse the repository at this point in the history
rust: make `Lock` trait unsafe.
  • Loading branch information
wedsonaf authored Oct 15, 2021
2 parents 6d76783 + 2c1e84a commit a6d4c3f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion rust/kernel/sync/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ impl<'a, L: Lock + ?Sized> Guard<'a, L> {
///
/// [`Guard`] is written such that any mutual exclusion primitive that can implement this trait can
/// also benefit from having an automatic way to unlock itself.
pub trait Lock {
///
/// # Safety
///
/// Implementers of this trait must ensure that only one thread/CPU may access the protected data
/// once the lock is held, that is, between calls to `lock_noguard` and `unlock`.
pub unsafe trait Lock {
/// The type of the data protected by the lock.
type Inner: ?Sized;

Expand Down
3 changes: 2 additions & 1 deletion rust/kernel/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ impl<T: ?Sized> NeedsLockClass for Mutex<T> {
}
}

impl<T: ?Sized> Lock for Mutex<T> {
// SAFETY: The underlying kernel `struct mutex` object ensures mutual exclusion.
unsafe impl<T: ?Sized> Lock for Mutex<T> {
type Inner = T;
type GuardContext = ();

Expand Down
3 changes: 2 additions & 1 deletion rust/kernel/sync/spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl<T: ?Sized> NeedsLockClass for SpinLock<T> {
}
}

impl<T: ?Sized> Lock for SpinLock<T> {
// SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion.
unsafe impl<T: ?Sized> Lock for SpinLock<T> {
type Inner = T;
type GuardContext = ();

Expand Down

0 comments on commit a6d4c3f

Please sign in to comment.