diff --git a/drivers/android/node.rs b/drivers/android/node.rs index 015b83c9ade73d..c57f8fe10ee236 100644 --- a/drivers/android/node.rs +++ b/drivers/android/node.rs @@ -5,7 +5,7 @@ use kernel::{ io_buffer::IoBufferWriter, linked_list::{GetLinks, Links, List}, prelude::*, - sync::{Guard, LockedBy, Mutex, Ref, SpinLock}, + sync::{GuardMut, LockedBy, Mutex, Ref, SpinLock}, user_ptr::UserSlicePtrWriter, }; @@ -244,7 +244,7 @@ impl Node { pub(crate) fn next_death( &self, - guard: &mut Guard<'_, Mutex>, + guard: &mut GuardMut<'_, Mutex>, ) -> Option> { self.inner.access_mut(guard).death_list.pop_front() } @@ -252,7 +252,7 @@ impl Node { pub(crate) fn add_death( &self, death: Ref, - guard: &mut Guard<'_, Mutex>, + guard: &mut GuardMut<'_, Mutex>, ) { self.inner.access_mut(guard).death_list.push_back(death); } @@ -306,7 +306,7 @@ impl Node { pub(crate) fn populate_counts( &self, out: &mut BinderNodeInfoForRef, - guard: &Guard<'_, Mutex>, + guard: &GuardMut<'_, Mutex>, ) { let inner = self.inner.access(guard); out.strong_count = inner.strong.count as _; @@ -316,7 +316,7 @@ impl Node { pub(crate) fn populate_debug_info( &self, out: &mut BinderNodeDebugInfo, - guard: &Guard<'_, Mutex>, + guard: &GuardMut<'_, Mutex>, ) { out.ptr = self.ptr as _; out.cookie = self.cookie as _; @@ -329,7 +329,7 @@ impl Node { } } - pub(crate) fn force_has_count(&self, guard: &mut Guard<'_, Mutex>) { + pub(crate) fn force_has_count(&self, guard: &mut GuardMut<'_, Mutex>) { let inner = self.inner.access_mut(guard); inner.strong.has_count = true; inner.weak.has_count = true; diff --git a/drivers/android/process.rs b/drivers/android/process.rs index fffe179ca77b49..cb1845fceda44a 100644 --- a/drivers/android/process.rs +++ b/drivers/android/process.rs @@ -10,7 +10,7 @@ use kernel::{ pages::Pages, prelude::*, rbtree::RBTree, - sync::{Guard, Mutex, Ref, UniqueRef}, + sync::{GuardMut, Mutex, Ref, UniqueRef}, task::Task, user_ptr::{UserSlicePtr, UserSlicePtrReader}, }; @@ -925,7 +925,7 @@ impl<'a> Registration<'a> { fn new( process: &'a Process, thread: &'a Ref, - guard: &mut Guard<'_, Mutex>, + guard: &mut GuardMut<'_, Mutex>, ) -> Self { guard.ready_threads.push_back(thread.clone()); Self { process, thread } diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index 8add1b5be39ce9..3e993df3ed977b 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -5,7 +5,7 @@ //! This module allows Rust code to use the kernel's [`struct wait_queue_head`] as a condition //! variable. -use super::{Guard, Lock, NeedsLockClass}; +use super::{GuardMut, Lock, NeedsLockClass}; use crate::{bindings, str::CStr, task::Task}; use core::{cell::UnsafeCell, marker::PhantomPinned, mem::MaybeUninit, pin::Pin}; @@ -60,7 +60,7 @@ impl CondVar { /// /// Returns whether there is a signal pending. #[must_use = "wait returns if a signal is pending, so the caller must check the return value"] - pub fn wait(&self, guard: &mut Guard<'_, L>) -> bool { + pub fn wait(&self, guard: &mut GuardMut<'_, L>) -> bool { let lock = guard.lock; let mut wait = MaybeUninit::::uninit(); diff --git a/rust/kernel/sync/guard.rs b/rust/kernel/sync/guard.rs index 3af735d363571d..67ca27b181b8b8 100644 --- a/rust/kernel/sync/guard.rs +++ b/rust/kernel/sync/guard.rs @@ -10,23 +10,23 @@ /// when a guard goes out of scope. It also provides a safe and convenient way to access the data /// protected by the lock. #[must_use = "the lock unlocks immediately when the guard is unused"] -pub struct Guard<'a, L: Lock + ?Sized> { +pub struct GuardMut<'a, L: Lock + ?Sized> { pub(crate) lock: &'a L, pub(crate) context: L::GuardContext, } -// SAFETY: `Guard` is sync when the data protected by the lock is also sync. This is more +// SAFETY: `GuardMut` is sync when the data protected by the lock is also sync. This is more // conservative than the default compiler implementation; more details can be found on // https://github.com/rust-lang/rust/issues/41622 -- it refers to `MutexGuard` from the standard // library. -unsafe impl Sync for Guard<'_, L> +unsafe impl Sync for GuardMut<'_, L> where L: Lock + ?Sized, L::Inner: Sync, { } -impl core::ops::Deref for Guard<'_, L> { +impl core::ops::Deref for GuardMut<'_, L> { type Target = L::Inner; fn deref(&self) -> &Self::Target { @@ -35,21 +35,21 @@ impl core::ops::Deref for Guard<'_, L> { } } -impl core::ops::DerefMut for Guard<'_, L> { +impl core::ops::DerefMut for GuardMut<'_, L> { fn deref_mut(&mut self) -> &mut L::Inner { // SAFETY: The caller owns the lock, so it is safe to deref the protected data. unsafe { &mut *self.lock.locked_data().get() } } } -impl Drop for Guard<'_, L> { +impl Drop for GuardMut<'_, L> { fn drop(&mut self) { // SAFETY: The caller owns the lock, so it is safe to unlock it. unsafe { self.lock.unlock(&mut self.context) }; } } -impl<'a, L: Lock + ?Sized> Guard<'a, L> { +impl<'a, L: Lock + ?Sized> GuardMut<'a, L> { /// Constructs a new lock guard. /// /// # Safety @@ -62,8 +62,8 @@ impl<'a, L: Lock + ?Sized> Guard<'a, L> { /// A generic mutual exclusion primitive. /// -/// [`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. +/// [`GuardMut`] is written such that any mutual exclusion primitive that can implement this trait +/// can also benefit from having an automatic way to unlock itself. /// /// # Safety /// diff --git a/rust/kernel/sync/locked_by.rs b/rust/kernel/sync/locked_by.rs index d3e0b0d5e9b4ec..e7546e12b8dfff 100644 --- a/rust/kernel/sync/locked_by.rs +++ b/rust/kernel/sync/locked_by.rs @@ -2,7 +2,7 @@ //! A wrapper for data protected by a lock that does not wrap it. -use super::{Guard, Lock}; +use super::{GuardMut, Lock}; use core::{cell::UnsafeCell, ops::Deref, ptr}; /// Allows access to some data to be serialised by a lock that does not wrap it. @@ -77,8 +77,8 @@ impl LockedBy { impl LockedBy { /// Returns a reference to the protected data when the caller provides evidence (via a - /// [`Guard`]) that the owner is locked. - pub fn access<'a>(&'a self, guard: &'a Guard<'_, L>) -> &'a T { + /// [`GuardMut`]) that the owner is locked. + pub fn access<'a>(&'a self, guard: &'a GuardMut<'_, L>) -> &'a T { if !ptr::eq(guard.deref(), self.owner) { panic!("guard does not match owner"); } @@ -88,8 +88,8 @@ impl LockedBy { } /// Returns a mutable reference to the protected data when the caller provides evidence (via a - /// mutable [`Guard`]) that the owner is locked mutably. - pub fn access_mut<'a>(&'a self, guard: &'a mut Guard<'_, L>) -> &'a mut T { + /// mutable [`GuardMut`]) that the owner is locked mutably. + pub fn access_mut<'a>(&'a self, guard: &'a mut GuardMut<'_, L>) -> &'a mut T { if !ptr::eq(guard.deref().deref(), self.owner) { panic!("guard does not match owner"); } diff --git a/rust/kernel/sync/mod.rs b/rust/kernel/sync/mod.rs index fa691fb9380623..96a0f947cef953 100644 --- a/rust/kernel/sync/mod.rs +++ b/rust/kernel/sync/mod.rs @@ -33,7 +33,7 @@ mod spinlock; pub use arc::{Ref, RefBorrow, UniqueRef}; pub use condvar::CondVar; -pub use guard::{Guard, Lock}; +pub use guard::{GuardMut, Lock}; pub use locked_by::LockedBy; pub use mutex::Mutex; pub use spinlock::SpinLock; diff --git a/rust/kernel/sync/mutex.rs b/rust/kernel/sync/mutex.rs index 7053a5136abb94..f2e4fc38df05db 100644 --- a/rust/kernel/sync/mutex.rs +++ b/rust/kernel/sync/mutex.rs @@ -4,7 +4,7 @@ //! //! This module allows Rust code to use the kernel's [`struct mutex`]. -use super::{Guard, Lock, NeedsLockClass}; +use super::{GuardMut, Lock, NeedsLockClass}; use crate::bindings; use crate::str::CStr; use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin}; @@ -64,10 +64,10 @@ impl Mutex { impl Mutex { /// Locks the mutex and gives the caller access to the data protected by it. Only one thread at /// a time is allowed to access the protected data. - pub fn lock(&self) -> Guard<'_, Self> { + pub fn lock(&self) -> GuardMut<'_, Self> { self.lock_noguard(); // SAFETY: The mutex was just acquired. - unsafe { Guard::new(self, ()) } + unsafe { GuardMut::new(self, ()) } } } diff --git a/rust/kernel/sync/spinlock.rs b/rust/kernel/sync/spinlock.rs index 6fa00a514aac95..bdacbc57523a64 100644 --- a/rust/kernel/sync/spinlock.rs +++ b/rust/kernel/sync/spinlock.rs @@ -6,7 +6,7 @@ //! //! See . -use super::{Guard, Lock, NeedsLockClass}; +use super::{GuardMut, Lock, NeedsLockClass}; use crate::bindings; use crate::str::CStr; use core::{cell::UnsafeCell, marker::PhantomPinned, pin::Pin}; @@ -67,10 +67,10 @@ impl SpinLock { impl SpinLock { /// Locks the spinlock and gives the caller access to the data protected by it. Only one thread /// at a time is allowed to access the protected data. - pub fn lock(&self) -> Guard<'_, Self> { + pub fn lock(&self) -> GuardMut<'_, Self> { self.lock_noguard(); // SAFETY: The spinlock was just acquired. - unsafe { Guard::new(self, ()) } + unsafe { GuardMut::new(self, ()) } } }