aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlix ANNERAUD <[email protected]>2025-02-28 16:41:41 +0100
committerAlix ANNERAUD <[email protected]>2025-02-28 16:41:41 +0100
commit55684782258b0241ede93ac6e43a07a3075ad028 (patch)
treec619083a7c55a78f175bae4045ae53329e486d2d
parent33cf27adf646bacd758b7289ebbf460b24c26fa5 (diff)
Fix module references in blocking read-write lock implementation
-rw-r--r--embassy-sync/src/blocking_rwlock/mod.rs16
-rw-r--r--embassy-sync/src/rwlock.rs20
2 files changed, 18 insertions, 18 deletions
diff --git a/embassy-sync/src/blocking_rwlock/mod.rs b/embassy-sync/src/blocking_rwlock/mod.rs
index a8fb7d6bc..88cd2164b 100644
--- a/embassy-sync/src/blocking_rwlock/mod.rs
+++ b/embassy-sync/src/blocking_rwlock/mod.rs
@@ -9,7 +9,7 @@ use self::raw::RawRwLock;
9 9
10/// Blocking read-write lock (not async) 10/// Blocking read-write lock (not async)
11/// 11///
12/// Provides a blocking read-write lock primitive backed by an implementation of [`raw_rwlock::RawRwLock`]. 12/// Provides a blocking read-write lock primitive backed by an implementation of [`raw::RawRwLock`].
13/// 13///
14/// Which implementation you select depends on the context in which you're using the read-write lock, and you can choose which kind 14/// Which implementation you select depends on the context in which you're using the read-write lock, and you can choose which kind
15/// of interior mutability fits your use case. 15/// of interior mutability fits your use case.
@@ -94,16 +94,16 @@ impl<R, T> RwLock<R, T> {
94/// # Safety 94/// # Safety
95/// 95///
96/// This read-write lock is safe to share between different executors and interrupts. 96/// This read-write lock is safe to share between different executors and interrupts.
97pub type CriticalSectionRwLock<T> = RwLock<raw_rwlock::CriticalSectionRawRwLock, T>; 97pub type CriticalSectionRwLock<T> = RwLock<raw::CriticalSectionRawRwLock, T>;
98 98
99/// A read-write lock that allows borrowing data in the context of a single executor. 99/// A read-write lock that allows borrowing data in the context of a single executor.
100/// 100///
101/// # Safety 101/// # Safety
102/// 102///
103/// **This Read-Write Lock is only safe within a single executor.** 103/// **This Read-Write Lock is only safe within a single executor.**
104pub type NoopRwLock<T> = RwLock<raw_rwlock::NoopRawRwLock, T>; 104pub type NoopRwLock<T> = RwLock<raw::NoopRawRwLock, T>;
105 105
106impl<T> RwLock<raw_rwlock::CriticalSectionRawRwLock, T> { 106impl<T> RwLock<raw::CriticalSectionRawRwLock, T> {
107 /// Borrows the data for the duration of the critical section 107 /// Borrows the data for the duration of the critical section
108 pub fn borrow<'cs>(&'cs self, _cs: critical_section::CriticalSection<'cs>) -> &'cs T { 108 pub fn borrow<'cs>(&'cs self, _cs: critical_section::CriticalSection<'cs>) -> &'cs T {
109 let ptr = self.data.get() as *const T; 109 let ptr = self.data.get() as *const T;
@@ -111,7 +111,7 @@ impl<T> RwLock<raw_rwlock::CriticalSectionRawRwLock, T> {
111 } 111 }
112} 112}
113 113
114impl<T> RwLock<raw_rwlock::NoopRawRwLock, T> { 114impl<T> RwLock<raw::NoopRawRwLock, T> {
115 /// Borrows the data 115 /// Borrows the data
116 #[allow(clippy::should_implement_trait)] 116 #[allow(clippy::should_implement_trait)]
117 pub fn borrow(&self) -> &T { 117 pub fn borrow(&self) -> &T {
@@ -184,7 +184,7 @@ mod thread_mode_rwlock {
184 /// This will panic if not currently running in thread mode. 184 /// This will panic if not currently running in thread mode.
185 pub fn borrow(&self) -> &T { 185 pub fn borrow(&self) -> &T {
186 assert!( 186 assert!(
187 raw_rwlock::in_thread_mode(), 187 raw::in_thread_mode(),
188 "ThreadModeRwLock can only be borrowed from thread mode." 188 "ThreadModeRwLock can only be borrowed from thread mode."
189 ); 189 );
190 unsafe { &*self.inner.get() } 190 unsafe { &*self.inner.get() }
@@ -197,7 +197,7 @@ mod thread_mode_rwlock {
197 /// This will panic if not currently running in thread mode. 197 /// This will panic if not currently running in thread mode.
198 pub fn borrow_mut(&self) -> &mut T { 198 pub fn borrow_mut(&self) -> &mut T {
199 assert!( 199 assert!(
200 raw_rwlock::in_thread_mode(), 200 raw::in_thread_mode(),
201 "ThreadModeRwLock can only be borrowed from thread mode." 201 "ThreadModeRwLock can only be borrowed from thread mode."
202 ); 202 );
203 unsafe { &mut *self.inner.get() } 203 unsafe { &mut *self.inner.get() }
@@ -211,7 +211,7 @@ mod thread_mode_rwlock {
211 // T isn't, so without this check a user could create a ThreadModeRwLock in thread mode, 211 // T isn't, so without this check a user could create a ThreadModeRwLock in thread mode,
212 // send it to interrupt context and drop it there, which would "send" a T even if T is not Send. 212 // send it to interrupt context and drop it there, which would "send" a T even if T is not Send.
213 assert!( 213 assert!(
214 raw_rwlock::in_thread_mode(), 214 raw::in_thread_mode(),
215 "ThreadModeRwLock can only be dropped from thread mode." 215 "ThreadModeRwLock can only be dropped from thread mode."
216 ); 216 );
217 217
diff --git a/embassy-sync/src/rwlock.rs b/embassy-sync/src/rwlock.rs
index 365f6fda5..9fa61ee56 100644
--- a/embassy-sync/src/rwlock.rs
+++ b/embassy-sync/src/rwlock.rs
@@ -2,13 +2,13 @@
2//! 2//!
3//! This module provides a read-write lock that can be used to synchronize data between asynchronous tasks. 3//! This module provides a read-write lock that can be used to synchronize data between asynchronous tasks.
4use core::cell::{RefCell, UnsafeCell}; 4use core::cell::{RefCell, UnsafeCell};
5use core::fmt;
5use core::future::{poll_fn, Future}; 6use core::future::{poll_fn, Future};
6use core::ops::{Deref, DerefMut}; 7use core::ops::{Deref, DerefMut};
7use core::task::Poll; 8use core::task::Poll;
8use core::{fmt, mem};
9 9
10use crate::blocking_mutex::raw::RawRwLock; 10use crate::blocking_rwlock::raw::RawRwLock;
11use crate::blocking_mutex::RwLock as BlockingRwLock; 11use crate::blocking_rwlock::RwLock as BlockingRwLock;
12use crate::waitqueue::WakerRegistration; 12use crate::waitqueue::WakerRegistration;
13 13
14/// Error returned by [`RwLock::try_read_lock`] and [`RwLock::try_write_lock`] 14/// Error returned by [`RwLock::try_read_lock`] and [`RwLock::try_write_lock`]
@@ -77,7 +77,7 @@ where
77 /// This will wait for the lock to be available if it's already locked for writing. 77 /// This will wait for the lock to be available if it's already locked for writing.
78 pub fn read_lock(&self) -> impl Future<Output = RwLockReadGuard<'_, R, T>> { 78 pub fn read_lock(&self) -> impl Future<Output = RwLockReadGuard<'_, R, T>> {
79 poll_fn(|cx| { 79 poll_fn(|cx| {
80 let ready = self.state.lock(|s| { 80 let ready = self.state.write_lock(|s| {
81 let mut s = s.borrow_mut(); 81 let mut s = s.borrow_mut();
82 if s.writer { 82 if s.writer {
83 s.waker.register(cx.waker()); 83 s.waker.register(cx.waker());
@@ -101,7 +101,7 @@ where
101 /// This will wait for the lock to be available if it's already locked for reading or writing. 101 /// This will wait for the lock to be available if it's already locked for reading or writing.
102 pub fn write_lock(&self) -> impl Future<Output = RwLockWriteGuard<'_, R, T>> { 102 pub fn write_lock(&self) -> impl Future<Output = RwLockWriteGuard<'_, R, T>> {
103 poll_fn(|cx| { 103 poll_fn(|cx| {
104 let ready = self.state.lock(|s| { 104 let ready = self.state.write_lock(|s| {
105 let mut s = s.borrow_mut(); 105 let mut s = s.borrow_mut();
106 if s.readers > 0 || s.writer { 106 if s.readers > 0 || s.writer {
107 s.waker.register(cx.waker()); 107 s.waker.register(cx.waker());
@@ -124,7 +124,7 @@ where
124 /// 124 ///
125 /// If the lock is already locked for writing, this will return an error instead of waiting. 125 /// If the lock is already locked for writing, this will return an error instead of waiting.
126 pub fn try_read_lock(&self) -> Result<RwLockReadGuard<'_, R, T>, TryLockError> { 126 pub fn try_read_lock(&self) -> Result<RwLockReadGuard<'_, R, T>, TryLockError> {
127 self.state.lock(|s| { 127 self.state.read_lock(|s| {
128 let mut s = s.borrow_mut(); 128 let mut s = s.borrow_mut();
129 if s.writer { 129 if s.writer {
130 Err(TryLockError) 130 Err(TryLockError)
@@ -141,7 +141,7 @@ where
141 /// 141 ///
142 /// If the lock is already locked for reading or writing, this will return an error instead of waiting. 142 /// If the lock is already locked for reading or writing, this will return an error instead of waiting.
143 pub fn try_write_lock(&self) -> Result<RwLockWriteGuard<'_, R, T>, TryLockError> { 143 pub fn try_write_lock(&self) -> Result<RwLockWriteGuard<'_, R, T>, TryLockError> {
144 self.state.lock(|s| { 144 self.state.write_lock(|s| {
145 let mut s = s.borrow_mut(); 145 let mut s = s.borrow_mut();
146 if s.readers > 0 || s.writer { 146 if s.readers > 0 || s.writer {
147 Err(TryLockError) 147 Err(TryLockError)
@@ -229,7 +229,7 @@ where
229 T: ?Sized, 229 T: ?Sized,
230{ 230{
231 fn drop(&mut self) { 231 fn drop(&mut self) {
232 self.rwlock.state.lock(|s| { 232 self.rwlock.state.write_lock(|s| {
233 let mut s = unwrap!(s.try_borrow_mut()); 233 let mut s = unwrap!(s.try_borrow_mut());
234 s.readers -= 1; 234 s.readers -= 1;
235 if s.readers == 0 { 235 if s.readers == 0 {
@@ -294,7 +294,7 @@ where
294 T: ?Sized, 294 T: ?Sized,
295{ 295{
296 fn drop(&mut self) { 296 fn drop(&mut self) {
297 self.rwlock.state.lock(|s| { 297 self.rwlock.state.write_lock(|s| {
298 let mut s = unwrap!(s.try_borrow_mut()); 298 let mut s = unwrap!(s.try_borrow_mut());
299 s.writer = false; 299 s.writer = false;
300 s.waker.wake(); 300 s.waker.wake();
@@ -349,7 +349,7 @@ where
349 349
350#[cfg(test)] 350#[cfg(test)]
351mod tests { 351mod tests {
352 use crate::blocking_mutex::raw_rwlock::NoopRawRwLock; 352 use crate::blocking_rwlock::raw::NoopRawRwLock;
353 use crate::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}; 353 use crate::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
354 354
355 #[futures_test::test] 355 #[futures_test::test]