diff options
| author | Alexander van Saase <[email protected]> | 2025-03-21 23:02:04 +0100 |
|---|---|---|
| committer | Alexander van Saase <[email protected]> | 2025-03-21 23:22:21 +0100 |
| commit | 7a031eed66ef27e83b8582e7c1e7ca00d16ccf64 (patch) | |
| tree | 52e0580d0becc5a1eeb28d6cec0df1709cf6be45 /embassy-sync | |
| parent | f3b9be7beede167295e8dc431e417bea77bb2455 (diff) | |
Add note about RefCell alternative
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/src/blocking_mutex/mod.rs | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/embassy-sync/src/blocking_mutex/mod.rs b/embassy-sync/src/blocking_mutex/mod.rs index 5b0e4144a..a41bc3569 100644 --- a/embassy-sync/src/blocking_mutex/mod.rs +++ b/embassy-sync/src/blocking_mutex/mod.rs | |||
| @@ -54,8 +54,11 @@ impl<R: RawMutex, T> Mutex<R, T> { | |||
| 54 | /// Creates a critical section and grants temporary mutable access to the protected data. | 54 | /// Creates a critical section and grants temporary mutable access to the protected data. |
| 55 | /// | 55 | /// |
| 56 | /// # Safety | 56 | /// # Safety |
| 57 | /// This method is unsafe because calling this method when the mutex is already locked, | 57 | /// |
| 58 | /// either using this method or `lock`, violates Rust's aliasing rules. | 58 | /// This method is marked unsafe because calling this method re-entrantly, i.e. within |
| 59 | /// another `lock_mut` or `lock` closure, violates Rust's aliasing rules. Calling this | ||
| 60 | /// method at the same time from different tasks is safe. For a safe alternative with | ||
| 61 | /// mutable access that never causes UB, use a `RefCell` in a `Mutex`. | ||
| 59 | pub unsafe fn lock_mut<U>(&self, f: impl FnOnce(&mut T) -> U) -> U { | 62 | pub unsafe fn lock_mut<U>(&self, f: impl FnOnce(&mut T) -> U) -> U { |
| 60 | self.raw.lock(|| { | 63 | self.raw.lock(|| { |
| 61 | let ptr = self.data.get() as *mut T; | 64 | let ptr = self.data.get() as *mut T; |
