Merge pull request #3978 from avsaase/blocking-mutex-lock-mut
embassy-sync: add `lock_mut` to `blocking_mutex::Mutex`
This commit is contained in:
commit
154870b2c3
@ -50,6 +50,23 @@ impl<R: RawMutex, T> Mutex<R, T> {
|
|||||||
f(inner)
|
f(inner)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a critical section and grants temporary mutable access to the protected data.
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This method is marked unsafe because calling this method re-entrantly, i.e. within
|
||||||
|
/// another `lock_mut` or `lock` closure, violates Rust's aliasing rules. Calling this
|
||||||
|
/// method at the same time from different tasks is safe. For a safe alternative with
|
||||||
|
/// mutable access that never causes UB, use a `RefCell` in a `Mutex`.
|
||||||
|
pub unsafe fn lock_mut<U>(&self, f: impl FnOnce(&mut T) -> U) -> U {
|
||||||
|
self.raw.lock(|| {
|
||||||
|
let ptr = self.data.get() as *mut T;
|
||||||
|
// Safety: we have exclusive access to the data, as long as this mutex is not locked re-entrantly
|
||||||
|
let inner = unsafe { &mut *ptr };
|
||||||
|
f(inner)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R, T> Mutex<R, T> {
|
impl<R, T> Mutex<R, T> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user