diff options
| author | Alexander van Saase <[email protected]> | 2025-03-19 19:32:05 +0100 |
|---|---|---|
| committer | Alexander van Saase <[email protected]> | 2025-03-19 20:24:52 +0100 |
| commit | f3b9be7beede167295e8dc431e417bea77bb2455 (patch) | |
| tree | 429cd3ad7ca38757630d0770c4b4ba5a3f5c3f4d /embassy-sync | |
| parent | fec98fa366b94ff4740d04a89344b88685a381f1 (diff) | |
embassy-sync: add lock_mut to blocking_mutex::Mutex
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> { |
