Skip to content
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
12 changes: 6 additions & 6 deletions drivers/android/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -244,15 +244,15 @@ impl Node {

pub(crate) fn next_death(
&self,
guard: &mut Guard<'_, Mutex<ProcessInner>>,
guard: &mut GuardMut<'_, Mutex<ProcessInner>>,
) -> Option<Ref<NodeDeath>> {
self.inner.access_mut(guard).death_list.pop_front()
}

pub(crate) fn add_death(
&self,
death: Ref<NodeDeath>,
guard: &mut Guard<'_, Mutex<ProcessInner>>,
guard: &mut GuardMut<'_, Mutex<ProcessInner>>,
) {
self.inner.access_mut(guard).death_list.push_back(death);
}
Expand Down Expand Up @@ -306,7 +306,7 @@ impl Node {
pub(crate) fn populate_counts(
&self,
out: &mut BinderNodeInfoForRef,
guard: &Guard<'_, Mutex<ProcessInner>>,
guard: &GuardMut<'_, Mutex<ProcessInner>>,
) {
let inner = self.inner.access(guard);
out.strong_count = inner.strong.count as _;
Expand All @@ -316,7 +316,7 @@ impl Node {
pub(crate) fn populate_debug_info(
&self,
out: &mut BinderNodeDebugInfo,
guard: &Guard<'_, Mutex<ProcessInner>>,
guard: &GuardMut<'_, Mutex<ProcessInner>>,
) {
out.ptr = self.ptr as _;
out.cookie = self.cookie as _;
Expand All @@ -329,7 +329,7 @@ impl Node {
}
}

pub(crate) fn force_has_count(&self, guard: &mut Guard<'_, Mutex<ProcessInner>>) {
pub(crate) fn force_has_count(&self, guard: &mut GuardMut<'_, Mutex<ProcessInner>>) {
let inner = self.inner.access_mut(guard);
inner.strong.has_count = true;
inner.weak.has_count = true;
Expand Down
4 changes: 2 additions & 2 deletions drivers/android/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -925,7 +925,7 @@ impl<'a> Registration<'a> {
fn new(
process: &'a Process,
thread: &'a Ref<Thread>,
guard: &mut Guard<'_, Mutex<ProcessInner>>,
guard: &mut GuardMut<'_, Mutex<ProcessInner>>,
) -> Self {
guard.ready_threads.push_back(thread.clone());
Self { process, thread }
Expand Down
4 changes: 2 additions & 2 deletions rust/kernel/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<L: Lock>(&self, guard: &mut Guard<'_, L>) -> bool {
pub fn wait<L: Lock>(&self, guard: &mut GuardMut<'_, L>) -> bool {
let lock = guard.lock;
let mut wait = MaybeUninit::<bindings::wait_queue_entry>::uninit();

Expand Down
18 changes: 9 additions & 9 deletions rust/kernel/sync/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<L> Sync for Guard<'_, L>
unsafe impl<L> Sync for GuardMut<'_, L>
where
L: Lock + ?Sized,
L::Inner: Sync,
{
}

impl<L: Lock + ?Sized> core::ops::Deref for Guard<'_, L> {
impl<L: Lock + ?Sized> core::ops::Deref for GuardMut<'_, L> {
type Target = L::Inner;

fn deref(&self) -> &Self::Target {
Expand All @@ -35,21 +35,21 @@ impl<L: Lock + ?Sized> core::ops::Deref for Guard<'_, L> {
}
}

impl<L: Lock + ?Sized> core::ops::DerefMut for Guard<'_, L> {
impl<L: Lock + ?Sized> 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<L: Lock + ?Sized> Drop for Guard<'_, L> {
impl<L: Lock + ?Sized> 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
Expand All @@ -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
///
Expand Down
10 changes: 5 additions & 5 deletions rust/kernel/sync/locked_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -77,8 +77,8 @@ impl<T, L: Lock + ?Sized> LockedBy<T, L> {

impl<T: ?Sized, L: Lock + ?Sized> LockedBy<T, L> {
/// 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");
}
Expand All @@ -88,8 +88,8 @@ impl<T: ?Sized, L: Lock + ?Sized> LockedBy<T, L> {
}

/// 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");
}
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions rust/kernel/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -64,10 +64,10 @@ impl<T> Mutex<T> {
impl<T: ?Sized> Mutex<T> {
/// 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, ()) }
}
}

Expand Down
6 changes: 3 additions & 3 deletions rust/kernel/sync/spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! See <https://www.kernel.org/doc/Documentation/locking/spinlocks.txt>.

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};
Expand Down Expand Up @@ -67,10 +67,10 @@ impl<T> SpinLock<T> {
impl<T: ?Sized> SpinLock<T> {
/// 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, ()) }
}
}

Expand Down