slightly less unsafe take

This commit is contained in:
paul nehlich
2025-05-07 18:03:10 +02:00
parent a5713ac7b8
commit 6c937fa8b0
2 changed files with 11 additions and 7 deletions
+1 -1
View File
@@ -241,7 +241,7 @@ uint8_t freertos_simple_once(uint8_t *once_data) {
// TODO assert global_once_mutex != NULL
uint8_t result = 0;
// Todo: Replace Mutex with critical section
// This function is basically a flag stored in once_data, protected by the
// global_once_mutex
if (xSemaphoreTakeRecursive(global_once_mutex, portMAX_DELAY) != pdTRUE) {
@@ -26,12 +26,16 @@ impl<T: 'static> StaticReadOnceLock<T> {
impl<T: 'static + StaticInit> StaticReadOnceLock<T> {
pub fn take(&'static self) -> Option<&'static mut T> {
let inner = unsafe{&mut *self.inner.get()};
inner.static_init();
// Re-Borrow inner.
// TODO Is unsafe by leaking static references via static_init()
self.take_no_init()
if self.once.once() {
// SAFE: Prozected by once
let inner: &mut T = unsafe{&mut *self.inner.get()};
inner.static_init();
// Re-Borrow inner.
// TODO Is unsafe by leaking static references via static_init()
return Some(unsafe{&mut *self.inner.get()})
}
None
}
}
unsafe impl<T> Sync for StaticReadOnceLock<T> {}