Skip to content

rust: make Lock trait unsafe. #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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