Fix module references in blocking read-write lock implementation

This commit is contained in:
Alix ANNERAUD 2025-02-28 16:41:41 +01:00
parent 33cf27adf6
commit 5568478225
2 changed files with 18 additions and 18 deletions

View File

@ -9,7 +9,7 @@ use self::raw::RawRwLock;
/// Blocking read-write lock (not async) /// Blocking read-write lock (not async)
/// ///
/// Provides a blocking read-write lock primitive backed by an implementation of [`raw_rwlock::RawRwLock`]. /// Provides a blocking read-write lock primitive backed by an implementation of [`raw::RawRwLock`].
/// ///
/// Which implementation you select depends on the context in which you're using the read-write lock, and you can choose which kind /// Which implementation you select depends on the context in which you're using the read-write lock, and you can choose which kind
/// of interior mutability fits your use case. /// of interior mutability fits your use case.
@ -94,16 +94,16 @@ impl<R, T> RwLock<R, T> {
/// # Safety /// # Safety
/// ///
/// This read-write lock is safe to share between different executors and interrupts. /// This read-write lock is safe to share between different executors and interrupts.
pub type CriticalSectionRwLock<T> = RwLock<raw_rwlock::CriticalSectionRawRwLock, T>; pub type CriticalSectionRwLock<T> = RwLock<raw::CriticalSectionRawRwLock, T>;
/// A read-write lock that allows borrowing data in the context of a single executor. /// A read-write lock that allows borrowing data in the context of a single executor.
/// ///
/// # Safety /// # Safety
/// ///
/// **This Read-Write Lock is only safe within a single executor.** /// **This Read-Write Lock is only safe within a single executor.**
pub type NoopRwLock<T> = RwLock<raw_rwlock::NoopRawRwLock, T>; pub type NoopRwLock<T> = RwLock<raw::NoopRawRwLock, T>;
impl<T> RwLock<raw_rwlock::CriticalSectionRawRwLock, T> { impl<T> RwLock<raw::CriticalSectionRawRwLock, T> {
/// Borrows the data for the duration of the critical section /// Borrows the data for the duration of the critical section
pub fn borrow<'cs>(&'cs self, _cs: critical_section::CriticalSection<'cs>) -> &'cs T { pub fn borrow<'cs>(&'cs self, _cs: critical_section::CriticalSection<'cs>) -> &'cs T {
let ptr = self.data.get() as *const T; let ptr = self.data.get() as *const T;
@ -111,7 +111,7 @@ impl<T> RwLock<raw_rwlock::CriticalSectionRawRwLock, T> {
} }
} }
impl<T> RwLock<raw_rwlock::NoopRawRwLock, T> { impl<T> RwLock<raw::NoopRawRwLock, T> {
/// Borrows the data /// Borrows the data
#[allow(clippy::should_implement_trait)] #[allow(clippy::should_implement_trait)]
pub fn borrow(&self) -> &T { pub fn borrow(&self) -> &T {
@ -184,7 +184,7 @@ mod thread_mode_rwlock {
/// This will panic if not currently running in thread mode. /// This will panic if not currently running in thread mode.
pub fn borrow(&self) -> &T { pub fn borrow(&self) -> &T {
assert!( assert!(
raw_rwlock::in_thread_mode(), raw::in_thread_mode(),
"ThreadModeRwLock can only be borrowed from thread mode." "ThreadModeRwLock can only be borrowed from thread mode."
); );
unsafe { &*self.inner.get() } unsafe { &*self.inner.get() }
@ -197,7 +197,7 @@ mod thread_mode_rwlock {
/// This will panic if not currently running in thread mode. /// This will panic if not currently running in thread mode.
pub fn borrow_mut(&self) -> &mut T { pub fn borrow_mut(&self) -> &mut T {
assert!( assert!(
raw_rwlock::in_thread_mode(), raw::in_thread_mode(),
"ThreadModeRwLock can only be borrowed from thread mode." "ThreadModeRwLock can only be borrowed from thread mode."
); );
unsafe { &mut *self.inner.get() } unsafe { &mut *self.inner.get() }
@ -211,7 +211,7 @@ mod thread_mode_rwlock {
// T isn't, so without this check a user could create a ThreadModeRwLock in thread mode, // T isn't, so without this check a user could create a ThreadModeRwLock in thread mode,
// send it to interrupt context and drop it there, which would "send" a T even if T is not Send. // send it to interrupt context and drop it there, which would "send" a T even if T is not Send.
assert!( assert!(
raw_rwlock::in_thread_mode(), raw::in_thread_mode(),
"ThreadModeRwLock can only be dropped from thread mode." "ThreadModeRwLock can only be dropped from thread mode."
); );

View File

@ -2,13 +2,13 @@
//! //!
//! This module provides a read-write lock that can be used to synchronize data between asynchronous tasks. //! This module provides a read-write lock that can be used to synchronize data between asynchronous tasks.
use core::cell::{RefCell, UnsafeCell}; use core::cell::{RefCell, UnsafeCell};
use core::fmt;
use core::future::{poll_fn, Future}; use core::future::{poll_fn, Future};
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use core::task::Poll; use core::task::Poll;
use core::{fmt, mem};
use crate::blocking_mutex::raw::RawRwLock; use crate::blocking_rwlock::raw::RawRwLock;
use crate::blocking_mutex::RwLock as BlockingRwLock; use crate::blocking_rwlock::RwLock as BlockingRwLock;
use crate::waitqueue::WakerRegistration; use crate::waitqueue::WakerRegistration;
/// Error returned by [`RwLock::try_read_lock`] and [`RwLock::try_write_lock`] /// Error returned by [`RwLock::try_read_lock`] and [`RwLock::try_write_lock`]
@ -77,7 +77,7 @@ where
/// This will wait for the lock to be available if it's already locked for writing. /// This will wait for the lock to be available if it's already locked for writing.
pub fn read_lock(&self) -> impl Future<Output = RwLockReadGuard<'_, R, T>> { pub fn read_lock(&self) -> impl Future<Output = RwLockReadGuard<'_, R, T>> {
poll_fn(|cx| { poll_fn(|cx| {
let ready = self.state.lock(|s| { let ready = self.state.write_lock(|s| {
let mut s = s.borrow_mut(); let mut s = s.borrow_mut();
if s.writer { if s.writer {
s.waker.register(cx.waker()); s.waker.register(cx.waker());
@ -101,7 +101,7 @@ where
/// This will wait for the lock to be available if it's already locked for reading or writing. /// This will wait for the lock to be available if it's already locked for reading or writing.
pub fn write_lock(&self) -> impl Future<Output = RwLockWriteGuard<'_, R, T>> { pub fn write_lock(&self) -> impl Future<Output = RwLockWriteGuard<'_, R, T>> {
poll_fn(|cx| { poll_fn(|cx| {
let ready = self.state.lock(|s| { let ready = self.state.write_lock(|s| {
let mut s = s.borrow_mut(); let mut s = s.borrow_mut();
if s.readers > 0 || s.writer { if s.readers > 0 || s.writer {
s.waker.register(cx.waker()); s.waker.register(cx.waker());
@ -124,7 +124,7 @@ where
/// ///
/// If the lock is already locked for writing, this will return an error instead of waiting. /// If the lock is already locked for writing, this will return an error instead of waiting.
pub fn try_read_lock(&self) -> Result<RwLockReadGuard<'_, R, T>, TryLockError> { pub fn try_read_lock(&self) -> Result<RwLockReadGuard<'_, R, T>, TryLockError> {
self.state.lock(|s| { self.state.read_lock(|s| {
let mut s = s.borrow_mut(); let mut s = s.borrow_mut();
if s.writer { if s.writer {
Err(TryLockError) Err(TryLockError)
@ -141,7 +141,7 @@ where
/// ///
/// If the lock is already locked for reading or writing, this will return an error instead of waiting. /// If the lock is already locked for reading or writing, this will return an error instead of waiting.
pub fn try_write_lock(&self) -> Result<RwLockWriteGuard<'_, R, T>, TryLockError> { pub fn try_write_lock(&self) -> Result<RwLockWriteGuard<'_, R, T>, TryLockError> {
self.state.lock(|s| { self.state.write_lock(|s| {
let mut s = s.borrow_mut(); let mut s = s.borrow_mut();
if s.readers > 0 || s.writer { if s.readers > 0 || s.writer {
Err(TryLockError) Err(TryLockError)
@ -229,7 +229,7 @@ where
T: ?Sized, T: ?Sized,
{ {
fn drop(&mut self) { fn drop(&mut self) {
self.rwlock.state.lock(|s| { self.rwlock.state.write_lock(|s| {
let mut s = unwrap!(s.try_borrow_mut()); let mut s = unwrap!(s.try_borrow_mut());
s.readers -= 1; s.readers -= 1;
if s.readers == 0 { if s.readers == 0 {
@ -294,7 +294,7 @@ where
T: ?Sized, T: ?Sized,
{ {
fn drop(&mut self) { fn drop(&mut self) {
self.rwlock.state.lock(|s| { self.rwlock.state.write_lock(|s| {
let mut s = unwrap!(s.try_borrow_mut()); let mut s = unwrap!(s.try_borrow_mut());
s.writer = false; s.writer = false;
s.waker.wake(); s.waker.wake();
@ -349,7 +349,7 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::blocking_mutex::raw_rwlock::NoopRawRwLock; use crate::blocking_rwlock::raw::NoopRawRwLock;
use crate::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
#[futures_test::test] #[futures_test::test]