Skip to main content

oneliner_runtime/
arena.rs

1#[cfg(feature = "alloc")]
2use alloc::boxed::Box;
3
4use core::cell::UnsafeCell;
5use core::mem::MaybeUninit;
6
7/// Instance-owned model arena storage selected by the Oneliner `alloc` feature.
8pub struct OwnedArena<T> {
9    #[cfg(not(feature = "alloc"))]
10    inner: T,
11    #[cfg(feature = "alloc")]
12    inner: Box<T>,
13}
14
15impl<T> OwnedArena<T> {
16    pub fn new(value: T) -> Self {
17        Self {
18            #[cfg(not(feature = "alloc"))]
19            inner: value,
20            #[cfg(feature = "alloc")]
21            inner: Box::new(value),
22        }
23    }
24
25    pub fn get_mut(&mut self) -> &mut T {
26        &mut self.inner
27    }
28}
29
30impl<T: Default> Default for OwnedArena<T> {
31    fn default() -> Self {
32        Self::new(T::default())
33    }
34}
35
36/// Only stores the large arena value.
37///
38/// If `value` is all-zero, this object can be placed in `.bss`.
39pub struct ArenaStorage<T> {
40    val: UnsafeCell<T>,
41}
42
43impl<T> ArenaStorage<T> {
44    pub const fn new(value: T) -> Self {
45        Self {
46            val: UnsafeCell::new(value),
47        }
48    }
49}
50
51// Access is synchronized by the corresponding SharedArena.
52unsafe impl<T: Send> Sync for ArenaStorage<T> {}
53
54/// Synchronizes access to an arena shared by every instance of one model type.
55#[cfg(feature = "ariel-os")]
56pub struct SharedArena<T: 'static> {
57    storage: &'static ArenaStorage<T>,
58    lock: ariel_os::thread::sync::Lock,
59}
60
61#[cfg(feature = "ariel-os")]
62impl<T: 'static> SharedArena<T> {
63    pub const fn new(value: &'static ArenaStorage<T>) -> Self {
64        Self {
65            storage: value,
66            lock: ariel_os::thread::sync::Lock::new(),
67        }
68    }
69
70    pub fn with<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
71        self.lock.acquire();
72
73        let mut arena = unsafe { &mut *self.storage.val.get() };
74        let res = f(&mut arena);
75        self.lock.release();
76        res
77    }
78}
79
80#[cfg(not(feature = "ariel-os"))]
81pub struct SharedArena<T: 'static> {
82    storage: &'static ArenaStorage<T>,
83    _dummy_mutex: critical_section::Mutex<core::cell::RefCell<u8>>,
84}
85
86#[cfg(not(feature = "ariel-os"))]
87impl<T: 'static> SharedArena<T> {
88    pub const fn new(value: &'static ArenaStorage<T>) -> Self {
89        Self {
90            storage: value,
91            _dummy_mutex: critical_section::Mutex::new(core::cell::RefCell::new(42)),
92        }
93    }
94
95    pub fn with<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
96        critical_section::with(|cs| {
97            // Keep this guard alive for the entire closure.
98            // It detects recursive access to the same arena.
99            let _borrow = self._dummy_mutex.borrow_ref_mut(cs);
100
101            let arena: &mut T = unsafe { &mut *self.storage.val.get() };
102
103            f(arena)
104        })
105    }
106}
107unsafe impl<T: Send> Send for SharedArena<T> {}
108unsafe impl<T: Send> Sync for SharedArena<T> {}