diff options
Diffstat (limited to 'embassy-sync/src/rwlock.rs')
| -rw-r--r-- | embassy-sync/src/rwlock.rs | 157 |
1 files changed, 52 insertions, 105 deletions
diff --git a/embassy-sync/src/rwlock.rs b/embassy-sync/src/rwlock.rs index 0ad5d5864..0e4088c1e 100644 --- a/embassy-sync/src/rwlock.rs +++ b/embassy-sync/src/rwlock.rs | |||
| @@ -7,8 +7,8 @@ use core::future::{poll_fn, Future}; | |||
| 7 | use core::ops::{Deref, DerefMut}; | 7 | use core::ops::{Deref, DerefMut}; |
| 8 | use core::task::Poll; | 8 | use core::task::Poll; |
| 9 | 9 | ||
| 10 | use crate::blocking_rwlock::raw::RawRwLock; | 10 | use crate::blocking_mutex::raw::RawMutex; |
| 11 | use crate::blocking_rwlock::RwLock as BlockingRwLock; | 11 | use crate::blocking_mutex::Mutex as BlockingMutex; |
| 12 | use crate::waitqueue::WakerRegistration; | 12 | use 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`] |
| @@ -31,53 +31,48 @@ struct State { | |||
| 31 | /// | 31 | /// |
| 32 | /// Which implementation you select depends on the context in which you're using the read-write lock. | 32 | /// Which implementation you select depends on the context in which you're using the read-write lock. |
| 33 | /// | 33 | /// |
| 34 | /// Use [`CriticalSectionRawRwLock`](crate::blocking_mutex::raw_rwlock::CriticalSectionRawRwLock) when data can be shared between threads and interrupts. | 34 | /// Use [`CriticalSectionMutex`] when data can be shared between threads and interrupts. |
| 35 | /// | 35 | /// |
| 36 | /// Use [`NoopRawRwLock`](crate::blocking_mutex::raw_rwlock::NoopRawRwLock) when data is only shared between tasks running on the same executor. | 36 | /// Use [`NoopMutex`] when data is only shared between tasks running on the same executor. |
| 37 | /// | 37 | /// |
| 38 | /// Use [`ThreadModeRawRwLock`](crate::blocking_mutex::raw_rwlock::ThreadModeRawRwLock) when data is shared between tasks running on the same executor but you want a singleton. | 38 | /// Use [`ThreadModeMutex`] when data is shared between tasks running on the same executor but you want a global singleton. |
| 39 | /// | 39 | /// |
| 40 | pub struct RwLock<R, T> | 40 | |
| 41 | pub struct RwLock<M, T> | ||
| 41 | where | 42 | where |
| 42 | R: RawRwLock, | 43 | M: RawMutex, |
| 43 | T: ?Sized, | 44 | T: ?Sized, |
| 44 | { | 45 | { |
| 45 | state: BlockingRwLock<R, RefCell<State>>, | 46 | state: BlockingMutex<M, RefCell<State>>, |
| 46 | inner: UnsafeCell<T>, | 47 | inner: UnsafeCell<T>, |
| 47 | } | 48 | } |
| 48 | 49 | ||
| 49 | unsafe impl<R: RawRwLock + Send, T: ?Sized + Send> Send for RwLock<R, T> {} | 50 | unsafe impl<M: RawMutex + Send, T: ?Sized + Send> Send for RwLock<M, T> {} |
| 50 | unsafe impl<R: RawRwLock + Sync, T: ?Sized + Send> Sync for RwLock<R, T> {} | 51 | unsafe impl<M: RawMutex + Sync, T: ?Sized + Send> Sync for RwLock<M, T> {} |
| 51 | 52 | ||
| 52 | /// Async read-write lock. | 53 | /// Async read-write lock. |
| 53 | impl<R, T> RwLock<R, T> | 54 | impl<R, T> RwLock<R, T> |
| 54 | where | 55 | where |
| 55 | R: RawRwLock, | 56 | R: RawMutex, |
| 56 | { | 57 | { |
| 57 | /// Create a new read-write lock with the given value. | 58 | /// Create a new read-write lock with the given value. |
| 58 | pub const fn new(value: T) -> Self { | 59 | pub const fn new(value: T) -> Self { |
| 59 | Self { | 60 | Self { |
| 60 | inner: UnsafeCell::new(value), | 61 | inner: UnsafeCell::new(value), |
| 61 | state: BlockingRwLock::new(RefCell::new(State { | 62 | state: BlockingMutex::new(RefCell::new(State { |
| 62 | readers: 0, | 63 | readers: 0, |
| 63 | writer: false, | 64 | writer: false, |
| 64 | waker: WakerRegistration::new(), | 65 | waker: WakerRegistration::new(), |
| 65 | })), | 66 | })), |
| 66 | } | 67 | } |
| 67 | } | 68 | } |
| 68 | } | ||
| 69 | 69 | ||
| 70 | impl<R, T> RwLock<R, T> | ||
| 71 | where | ||
| 72 | R: RawRwLock, | ||
| 73 | T: ?Sized, | ||
| 74 | { | ||
| 75 | /// Lock the read-write lock for reading. | 70 | /// Lock the read-write lock for reading. |
| 76 | /// | 71 | /// |
| 77 | /// This will wait for the lock to be available if it's already locked for writing. | 72 | /// 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>> { | 73 | pub fn read(&self) -> impl Future<Output = RwLockReadGuard<'_, R, T>> { |
| 79 | poll_fn(|cx| { | 74 | poll_fn(|cx| { |
| 80 | let ready = self.state.write_lock(|s| { | 75 | let ready = self.state.lock(|s| { |
| 81 | let mut s = s.borrow_mut(); | 76 | let mut s = s.borrow_mut(); |
| 82 | if s.writer { | 77 | if s.writer { |
| 83 | s.waker.register(cx.waker()); | 78 | s.waker.register(cx.waker()); |
| @@ -99,11 +94,11 @@ where | |||
| 99 | /// Lock the read-write lock for writing. | 94 | /// Lock the read-write lock for writing. |
| 100 | /// | 95 | /// |
| 101 | /// This will wait for the lock to be available if it's already locked for reading or writing. | 96 | /// 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>> { | 97 | pub fn write(&self) -> impl Future<Output = RwLockWriteGuard<'_, R, T>> { |
| 103 | poll_fn(|cx| { | 98 | poll_fn(|cx| { |
| 104 | let ready = self.state.write_lock(|s| { | 99 | let ready = self.state.lock(|s| { |
| 105 | let mut s = s.borrow_mut(); | 100 | let mut s = s.borrow_mut(); |
| 106 | if s.readers > 0 || s.writer { | 101 | if s.writer || s.readers > 0 { |
| 107 | s.waker.register(cx.waker()); | 102 | s.waker.register(cx.waker()); |
| 108 | false | 103 | false |
| 109 | } else { | 104 | } else { |
| @@ -119,41 +114,13 @@ where | |||
| 119 | } | 114 | } |
| 120 | }) | 115 | }) |
| 121 | } | 116 | } |
| 117 | } | ||
| 122 | 118 | ||
| 123 | /// Attempt to immediately lock the read-write lock for reading. | 119 | impl<R, T> RwLock<R, T> |
| 124 | /// | 120 | where |
| 125 | /// If the lock is already locked for writing, this will return an error instead of waiting. | 121 | R: RawMutex, |
| 126 | pub fn try_read_lock(&self) -> Result<RwLockReadGuard<'_, R, T>, TryLockError> { | 122 | T: ?Sized, |
| 127 | self.state.read_lock(|s| { | 123 | { |
| 128 | let mut s = s.borrow_mut(); | ||
| 129 | if s.writer { | ||
| 130 | Err(TryLockError) | ||
| 131 | } else { | ||
| 132 | s.readers += 1; | ||
| 133 | Ok(()) | ||
| 134 | } | ||
| 135 | })?; | ||
| 136 | |||
| 137 | Ok(RwLockReadGuard { rwlock: self }) | ||
| 138 | } | ||
| 139 | |||
| 140 | /// Attempt to immediately lock the read-write lock for writing. | ||
| 141 | /// | ||
| 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> { | ||
| 144 | self.state.write_lock(|s| { | ||
| 145 | let mut s = s.borrow_mut(); | ||
| 146 | if s.readers > 0 || s.writer { | ||
| 147 | Err(TryLockError) | ||
| 148 | } else { | ||
| 149 | s.writer = true; | ||
| 150 | Ok(()) | ||
| 151 | } | ||
| 152 | })?; | ||
| 153 | |||
| 154 | Ok(RwLockWriteGuard { rwlock: self }) | ||
| 155 | } | ||
| 156 | |||
| 157 | /// Consumes this read-write lock, returning the underlying data. | 124 | /// Consumes this read-write lock, returning the underlying data. |
| 158 | pub fn into_inner(self) -> T | 125 | pub fn into_inner(self) -> T |
| 159 | where | 126 | where |
| @@ -171,7 +138,7 @@ where | |||
| 171 | } | 138 | } |
| 172 | } | 139 | } |
| 173 | 140 | ||
| 174 | impl<R: RawRwLock, T> From<T> for RwLock<R, T> { | 141 | impl<R: RawMutex, T> From<T> for RwLock<R, T> { |
| 175 | fn from(from: T) -> Self { | 142 | fn from(from: T) -> Self { |
| 176 | Self::new(from) | 143 | Self::new(from) |
| 177 | } | 144 | } |
| @@ -179,7 +146,7 @@ impl<R: RawRwLock, T> From<T> for RwLock<R, T> { | |||
| 179 | 146 | ||
| 180 | impl<R, T> Default for RwLock<R, T> | 147 | impl<R, T> Default for RwLock<R, T> |
| 181 | where | 148 | where |
| 182 | R: RawRwLock, | 149 | R: RawMutex, |
| 183 | T: Default, | 150 | T: Default, |
| 184 | { | 151 | { |
| 185 | fn default() -> Self { | 152 | fn default() -> Self { |
| @@ -187,26 +154,6 @@ where | |||
| 187 | } | 154 | } |
| 188 | } | 155 | } |
| 189 | 156 | ||
| 190 | impl<R, T> fmt::Debug for RwLock<R, T> | ||
| 191 | where | ||
| 192 | R: RawRwLock, | ||
| 193 | T: ?Sized + fmt::Debug, | ||
| 194 | { | ||
| 195 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| 196 | let mut d = f.debug_struct("RwLock"); | ||
| 197 | match self.try_write_lock() { | ||
| 198 | Ok(value) => { | ||
| 199 | d.field("inner", &&*value); | ||
| 200 | } | ||
| 201 | Err(TryLockError) => { | ||
| 202 | d.field("inner", &format_args!("<locked>")); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | d.finish_non_exhaustive() | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | /// Async read lock guard. | 157 | /// Async read lock guard. |
| 211 | /// | 158 | /// |
| 212 | /// Owning an instance of this type indicates having | 159 | /// Owning an instance of this type indicates having |
| @@ -217,19 +164,19 @@ where | |||
| 217 | #[must_use = "if unused the RwLock will immediately unlock"] | 164 | #[must_use = "if unused the RwLock will immediately unlock"] |
| 218 | pub struct RwLockReadGuard<'a, R, T> | 165 | pub struct RwLockReadGuard<'a, R, T> |
| 219 | where | 166 | where |
| 220 | R: RawRwLock, | 167 | R: RawMutex, |
| 221 | T: ?Sized, | 168 | T: ?Sized, |
| 222 | { | 169 | { |
| 223 | rwlock: &'a RwLock<R, T>, | 170 | rwlock: &'a RwLock<R, T>, |
| 224 | } | 171 | } |
| 225 | 172 | ||
| 226 | impl<'a, R, T> Drop for RwLockReadGuard<'a, R, T> | 173 | impl<'a, M, T> Drop for RwLockReadGuard<'a, M, T> |
| 227 | where | 174 | where |
| 228 | R: RawRwLock, | 175 | M: RawMutex, |
| 229 | T: ?Sized, | 176 | T: ?Sized, |
| 230 | { | 177 | { |
| 231 | fn drop(&mut self) { | 178 | fn drop(&mut self) { |
| 232 | self.rwlock.state.write_lock(|s| { | 179 | self.rwlock.state.lock(|s| { |
| 233 | let mut s = unwrap!(s.try_borrow_mut()); | 180 | let mut s = unwrap!(s.try_borrow_mut()); |
| 234 | s.readers -= 1; | 181 | s.readers -= 1; |
| 235 | if s.readers == 0 { | 182 | if s.readers == 0 { |
| @@ -239,9 +186,9 @@ where | |||
| 239 | } | 186 | } |
| 240 | } | 187 | } |
| 241 | 188 | ||
| 242 | impl<'a, R, T> Deref for RwLockReadGuard<'a, R, T> | 189 | impl<'a, M, T> Deref for RwLockReadGuard<'a, M, T> |
| 243 | where | 190 | where |
| 244 | R: RawRwLock, | 191 | M: RawMutex, |
| 245 | T: ?Sized, | 192 | T: ?Sized, |
| 246 | { | 193 | { |
| 247 | type Target = T; | 194 | type Target = T; |
| @@ -252,9 +199,9 @@ where | |||
| 252 | } | 199 | } |
| 253 | } | 200 | } |
| 254 | 201 | ||
| 255 | impl<'a, R, T> fmt::Debug for RwLockReadGuard<'a, R, T> | 202 | impl<'a, M, T> fmt::Debug for RwLockReadGuard<'a, M, T> |
| 256 | where | 203 | where |
| 257 | R: RawRwLock, | 204 | M: RawMutex, |
| 258 | T: ?Sized + fmt::Debug, | 205 | T: ?Sized + fmt::Debug, |
| 259 | { | 206 | { |
| 260 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 207 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| @@ -262,9 +209,9 @@ where | |||
| 262 | } | 209 | } |
| 263 | } | 210 | } |
| 264 | 211 | ||
| 265 | impl<'a, R, T> fmt::Display for RwLockReadGuard<'a, R, T> | 212 | impl<'a, M, T> fmt::Display for RwLockReadGuard<'a, M, T> |
| 266 | where | 213 | where |
| 267 | R: RawRwLock, | 214 | M: RawMutex, |
| 268 | T: ?Sized + fmt::Display, | 215 | T: ?Sized + fmt::Display, |
| 269 | { | 216 | { |
| 270 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 217 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| @@ -282,7 +229,7 @@ where | |||
| 282 | #[must_use = "if unused the RwLock will immediately unlock"] | 229 | #[must_use = "if unused the RwLock will immediately unlock"] |
| 283 | pub struct RwLockWriteGuard<'a, R, T> | 230 | pub struct RwLockWriteGuard<'a, R, T> |
| 284 | where | 231 | where |
| 285 | R: RawRwLock, | 232 | R: RawMutex, |
| 286 | T: ?Sized, | 233 | T: ?Sized, |
| 287 | { | 234 | { |
| 288 | rwlock: &'a RwLock<R, T>, | 235 | rwlock: &'a RwLock<R, T>, |
| @@ -290,11 +237,11 @@ where | |||
| 290 | 237 | ||
| 291 | impl<'a, R, T> Drop for RwLockWriteGuard<'a, R, T> | 238 | impl<'a, R, T> Drop for RwLockWriteGuard<'a, R, T> |
| 292 | where | 239 | where |
| 293 | R: RawRwLock, | 240 | R: RawMutex, |
| 294 | T: ?Sized, | 241 | T: ?Sized, |
| 295 | { | 242 | { |
| 296 | fn drop(&mut self) { | 243 | fn drop(&mut self) { |
| 297 | self.rwlock.state.write_lock(|s| { | 244 | self.rwlock.state.lock(|s| { |
| 298 | let mut s = unwrap!(s.try_borrow_mut()); | 245 | let mut s = unwrap!(s.try_borrow_mut()); |
| 299 | s.writer = false; | 246 | s.writer = false; |
| 300 | s.waker.wake(); | 247 | s.waker.wake(); |
| @@ -304,7 +251,7 @@ where | |||
| 304 | 251 | ||
| 305 | impl<'a, R, T> Deref for RwLockWriteGuard<'a, R, T> | 252 | impl<'a, R, T> Deref for RwLockWriteGuard<'a, R, T> |
| 306 | where | 253 | where |
| 307 | R: RawRwLock, | 254 | R: RawMutex, |
| 308 | T: ?Sized, | 255 | T: ?Sized, |
| 309 | { | 256 | { |
| 310 | type Target = T; | 257 | type Target = T; |
| @@ -317,7 +264,7 @@ where | |||
| 317 | 264 | ||
| 318 | impl<'a, R, T> DerefMut for RwLockWriteGuard<'a, R, T> | 265 | impl<'a, R, T> DerefMut for RwLockWriteGuard<'a, R, T> |
| 319 | where | 266 | where |
| 320 | R: RawRwLock, | 267 | R: RawMutex, |
| 321 | T: ?Sized, | 268 | T: ?Sized, |
| 322 | { | 269 | { |
| 323 | fn deref_mut(&mut self) -> &mut Self::Target { | 270 | fn deref_mut(&mut self) -> &mut Self::Target { |
| @@ -329,7 +276,7 @@ where | |||
| 329 | 276 | ||
| 330 | impl<'a, R, T> fmt::Debug for RwLockWriteGuard<'a, R, T> | 277 | impl<'a, R, T> fmt::Debug for RwLockWriteGuard<'a, R, T> |
| 331 | where | 278 | where |
| 332 | R: RawRwLock, | 279 | R: RawMutex, |
| 333 | T: ?Sized + fmt::Debug, | 280 | T: ?Sized + fmt::Debug, |
| 334 | { | 281 | { |
| 335 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 282 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| @@ -339,7 +286,7 @@ where | |||
| 339 | 286 | ||
| 340 | impl<'a, R, T> fmt::Display for RwLockWriteGuard<'a, R, T> | 287 | impl<'a, R, T> fmt::Display for RwLockWriteGuard<'a, R, T> |
| 341 | where | 288 | where |
| 342 | R: RawRwLock, | 289 | R: RawMutex, |
| 343 | T: ?Sized + fmt::Display, | 290 | T: ?Sized + fmt::Display, |
| 344 | { | 291 | { |
| 345 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 292 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| @@ -349,41 +296,41 @@ where | |||
| 349 | 296 | ||
| 350 | #[cfg(test)] | 297 | #[cfg(test)] |
| 351 | mod tests { | 298 | mod tests { |
| 352 | use crate::blocking_rwlock::raw::NoopRawRwLock; | 299 | use crate::blocking_mutex::raw::NoopRawMutex; |
| 353 | use crate::rwlock::RwLock; | 300 | use crate::rwlock::RwLock; |
| 354 | 301 | ||
| 355 | #[futures_test::test] | 302 | #[futures_test::test] |
| 356 | async fn read_guard_releases_lock_when_dropped() { | 303 | async fn read_guard_releases_lock_when_dropped() { |
| 357 | let rwlock: RwLock<NoopRawRwLock, [i32; 2]> = RwLock::new([0, 1]); | 304 | let rwlock: RwLock<NoopRawMutex, [i32; 2]> = RwLock::new([0, 1]); |
| 358 | 305 | ||
| 359 | { | 306 | { |
| 360 | let guard = rwlock.read_lock().await; | 307 | let guard = rwlock.read().await; |
| 361 | assert_eq!(*guard, [0, 1]); | 308 | assert_eq!(*guard, [0, 1]); |
| 362 | } | 309 | } |
| 363 | 310 | ||
| 364 | { | 311 | { |
| 365 | let guard = rwlock.read_lock().await; | 312 | let guard = rwlock.read().await; |
| 366 | assert_eq!(*guard, [0, 1]); | 313 | assert_eq!(*guard, [0, 1]); |
| 367 | } | 314 | } |
| 368 | 315 | ||
| 369 | assert_eq!(*rwlock.read_lock().await, [0, 1]); | 316 | assert_eq!(*rwlock.read().await, [0, 1]); |
| 370 | } | 317 | } |
| 371 | 318 | ||
| 372 | #[futures_test::test] | 319 | #[futures_test::test] |
| 373 | async fn write_guard_releases_lock_when_dropped() { | 320 | async fn write_guard_releases_lock_when_dropped() { |
| 374 | let rwlock: RwLock<NoopRawRwLock, [i32; 2]> = RwLock::new([0, 1]); | 321 | let rwlock: RwLock<NoopRawMutex, [i32; 2]> = RwLock::new([0, 1]); |
| 375 | 322 | ||
| 376 | { | 323 | { |
| 377 | let mut guard = rwlock.write_lock().await; | 324 | let mut guard = rwlock.write().await; |
| 378 | assert_eq!(*guard, [0, 1]); | 325 | assert_eq!(*guard, [0, 1]); |
| 379 | guard[1] = 2; | 326 | guard[1] = 2; |
| 380 | } | 327 | } |
| 381 | 328 | ||
| 382 | { | 329 | { |
| 383 | let guard = rwlock.read_lock().await; | 330 | let guard = rwlock.read().await; |
| 384 | assert_eq!(*guard, [0, 2]); | 331 | assert_eq!(*guard, [0, 2]); |
| 385 | } | 332 | } |
| 386 | 333 | ||
| 387 | assert_eq!(*rwlock.read_lock().await, [0, 2]); | 334 | assert_eq!(*rwlock.read().await, [0, 2]); |
| 388 | } | 335 | } |
| 389 | } | 336 | } |
