Skip to content

Commit e41de3a

Browse files
committed
zephyr: Use constructor for static thread stack
Instead of fully expanding the thread stack initializer in the kobj_define macro, create a hidden constructor as a const fn. Signed-off-by: David Brown <[email protected]>
1 parent fb93a15 commit e41de3a

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

zephyr/src/object.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,8 @@ macro_rules! _kobj_stack {
289289
unsafe { ::core::mem::zeroed() };
290290

291291
// The proxy object used to ensure initialization is placed in initialized memory.
292-
$v static $name: $crate::_export::KStaticThreadStack = $crate::object::StaticKernelObject {
293-
value: ::core::cell::UnsafeCell::new($crate::sys::thread::StaticThreadStack {
294-
base: [< $name _REAL >].data.get() as *mut $crate::raw::z_thread_stack_element,
295-
size: $size,
296-
}),
297-
init: $crate::sync::atomic::AtomicUsize::new(0),
298-
};
292+
$v static $name: $crate::_export::KStaticThreadStack =
293+
$crate::_export::KStaticThreadStack::new_from(&[< $name _REAL >]);
299294
}
300295
};
301296

zephyr/src/sys/thread.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use zephyr_sys::{
5050
};
5151
use super::K_NO_WAIT;
5252

53-
use crate::{align::AlignAs, object::{StaticKernelObject, Wrapped}};
53+
use crate::{align::AlignAs, object::{StaticKernelObject, Wrapped}, sync::atomic::AtomicUsize};
5454

5555
/// Adjust the stack size for alignment. Note that, unlike the C code, we don't include the
5656
/// reservation in this, as it has its own fields in the struct.
@@ -123,6 +123,22 @@ impl Wrapped for StaticKernelObject<StaticThreadStack> {
123123
}
124124
}
125125

126+
impl StaticKernelObject<StaticThreadStack> {
127+
/// Construct a StaticThreadStack object.
128+
///
129+
/// This is not intended to be directly called, but is used by the [`kobj_define`] macro.
130+
#[doc(hidden)]
131+
pub const fn new_from<const SZ: usize>(real: &RealStaticThreadStack<SZ>) -> Self {
132+
Self {
133+
value: UnsafeCell::new(StaticThreadStack {
134+
base: real.data.get() as *mut z_thread_stack_element,
135+
size: SZ,
136+
}),
137+
init: AtomicUsize::new(0),
138+
}
139+
}
140+
}
141+
126142
/// A single Zephyr thread.
127143
///
128144
/// This wraps a `k_thread` type within Rust. This value is returned from

0 commit comments

Comments
 (0)