diff options
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/src/blocking_mutex/mod.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/embassy-sync/src/blocking_mutex/mod.rs b/embassy-sync/src/blocking_mutex/mod.rs index beafdb43d..5b0e4144a 100644 --- a/embassy-sync/src/blocking_mutex/mod.rs +++ b/embassy-sync/src/blocking_mutex/mod.rs | |||
| @@ -50,6 +50,20 @@ impl<R: RawMutex, T> Mutex<R, T> { | |||
| 50 | f(inner) | 50 | f(inner) |
| 51 | }) | 51 | }) |
| 52 | } | 52 | } |
| 53 | |||
| 54 | /// Creates a critical section and grants temporary mutable access to the protected data. | ||
| 55 | /// | ||
| 56 | /// # Safety | ||
| 57 | /// This method is unsafe because calling this method when the mutex is already locked, | ||
| 58 | /// either using this method or `lock`, violates Rust's aliasing rules. | ||
| 59 | pub unsafe fn lock_mut<U>(&self, f: impl FnOnce(&mut T) -> U) -> U { | ||
| 60 | self.raw.lock(|| { | ||
| 61 | let ptr = self.data.get() as *mut T; | ||
| 62 | // Safety: we have exclusive access to the data, as long as this mutex is not locked re-entrantly | ||
| 63 | let inner = unsafe { &mut *ptr }; | ||
| 64 | f(inner) | ||
| 65 | }) | ||
| 66 | } | ||
| 53 | } | 67 | } |
| 54 | 68 | ||
| 55 | impl<R, T> Mutex<R, T> { | 69 | impl<R, T> Mutex<R, T> { |
