From 259cf6192b920dff4cce0486488d929bf4e05b96 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 24 Nov 2023 19:05:31 +0100 Subject: executor: Remove non-functional rtos-trace-interrupt. --- embassy-executor/src/lib.rs | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/lib.rs b/embassy-executor/src/lib.rs index f2c86d8e6..3b61b4ba5 100644 --- a/embassy-executor/src/lib.rs +++ b/embassy-executor/src/lib.rs @@ -44,25 +44,4 @@ pub use spawner::*; /// Implementation details for embassy macros. /// Do not use. Used for macros and HALs only. Not covered by semver guarantees. #[doc(hidden)] -pub mod _export { - #[cfg(feature = "rtos-trace")] - pub use rtos_trace::trace; - - /// Expands the given block of code when `embassy-executor` is compiled with - /// the `rtos-trace-interrupt` feature. - #[doc(hidden)] - #[macro_export] - #[cfg(feature = "rtos-trace-interrupt")] - macro_rules! rtos_trace_interrupt { - ($($tt:tt)*) => { $($tt)* }; - } - - /// Does not expand the given block of code when `embassy-executor` is - /// compiled without the `rtos-trace-interrupt` feature. - #[doc(hidden)] - #[macro_export] - #[cfg(not(feature = "rtos-trace-interrupt"))] - macro_rules! rtos_trace_interrupt { - ($($tt:tt)*) => {}; - } -} +pub mod _export {} -- cgit From 171cdb94c7906670723b0965ca66d72a2352ac73 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 24 Nov 2023 21:37:27 +0100 Subject: executor: add support for main/task macros in stable (allocates tasks in an arena) --- embassy-executor/src/arch/cortex_m.rs | 1 - embassy-executor/src/arch/riscv32.rs | 1 - embassy-executor/src/arch/std.rs | 1 - embassy-executor/src/arch/wasm.rs | 1 - embassy-executor/src/arch/xtensa.rs | 1 - embassy-executor/src/lib.rs | 95 +++++++++++++++++++++++++++++++++-- 6 files changed, 92 insertions(+), 8 deletions(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/arch/cortex_m.rs b/embassy-executor/src/arch/cortex_m.rs index 55299c94f..4a6d58575 100644 --- a/embassy-executor/src/arch/cortex_m.rs +++ b/embassy-executor/src/arch/cortex_m.rs @@ -51,7 +51,6 @@ mod thread { use core::arch::asm; use core::marker::PhantomData; - #[cfg(feature = "nightly")] pub use embassy_macros::main_cortex_m as main; use crate::{raw, Spawner}; diff --git a/embassy-executor/src/arch/riscv32.rs b/embassy-executor/src/arch/riscv32.rs index 6814e7844..ca12c3403 100644 --- a/embassy-executor/src/arch/riscv32.rs +++ b/embassy-executor/src/arch/riscv32.rs @@ -7,7 +7,6 @@ pub use thread::*; mod thread { use core::marker::PhantomData; - #[cfg(feature = "nightly")] pub use embassy_macros::main_riscv as main; use portable_atomic::{AtomicBool, Ordering}; diff --git a/embassy-executor/src/arch/std.rs b/embassy-executor/src/arch/std.rs index 5b2f7e2e4..598bb0509 100644 --- a/embassy-executor/src/arch/std.rs +++ b/embassy-executor/src/arch/std.rs @@ -8,7 +8,6 @@ mod thread { use std::marker::PhantomData; use std::sync::{Condvar, Mutex}; - #[cfg(feature = "nightly")] pub use embassy_macros::main_std as main; use crate::{raw, Spawner}; diff --git a/embassy-executor/src/arch/wasm.rs b/embassy-executor/src/arch/wasm.rs index 15aed867a..3faa92575 100644 --- a/embassy-executor/src/arch/wasm.rs +++ b/embassy-executor/src/arch/wasm.rs @@ -8,7 +8,6 @@ mod thread { use core::marker::PhantomData; - #[cfg(feature = "nightly")] pub use embassy_macros::main_wasm as main; use js_sys::Promise; use wasm_bindgen::prelude::*; diff --git a/embassy-executor/src/arch/xtensa.rs b/embassy-executor/src/arch/xtensa.rs index d335594e2..6ed9f9e72 100644 --- a/embassy-executor/src/arch/xtensa.rs +++ b/embassy-executor/src/arch/xtensa.rs @@ -8,7 +8,6 @@ mod thread { use core::marker::PhantomData; use core::sync::atomic::{AtomicBool, Ordering}; - #[cfg(feature = "nightly")] pub use embassy_macros::main_riscv as main; use crate::{raw, Spawner}; diff --git a/embassy-executor/src/lib.rs b/embassy-executor/src/lib.rs index 3b61b4ba5..ac7dbb035 100644 --- a/embassy-executor/src/lib.rs +++ b/embassy-executor/src/lib.rs @@ -1,5 +1,5 @@ #![cfg_attr(not(any(feature = "arch-std", feature = "arch-wasm")), no_std)] -#![cfg_attr(all(feature = "nightly", feature = "arch-xtensa"), feature(asm_experimental_arch))] +#![cfg_attr(feature = "arch-xtensa", feature(asm_experimental_arch))] #![allow(clippy::new_without_default)] #![doc = include_str!("../README.md")] #![warn(missing_docs)] @@ -7,7 +7,6 @@ // This mod MUST go first, so that the others see its macros. pub(crate) mod fmt; -#[cfg(feature = "nightly")] pub use embassy_macros::task; macro_rules! check_at_most_one { @@ -44,4 +43,94 @@ pub use spawner::*; /// Implementation details for embassy macros. /// Do not use. Used for macros and HALs only. Not covered by semver guarantees. #[doc(hidden)] -pub mod _export {} +#[cfg(not(feature = "nightly"))] +pub mod _export { + use core::alloc::Layout; + use core::cell::{Cell, UnsafeCell}; + use core::future::Future; + use core::mem::MaybeUninit; + use core::ptr::null_mut; + + use critical_section::{CriticalSection, Mutex}; + + use crate::raw::TaskPool; + + struct Arena { + buf: UnsafeCell>, + ptr: Mutex>, + } + + unsafe impl Sync for Arena {} + unsafe impl Send for Arena {} + + impl Arena { + const fn new() -> Self { + Self { + buf: UnsafeCell::new(MaybeUninit::uninit()), + ptr: Mutex::new(Cell::new(null_mut())), + } + } + + fn alloc(&'static self, cs: CriticalSection) -> &'static mut MaybeUninit { + let layout = Layout::new::(); + + let start = self.buf.get().cast::(); + let end = unsafe { start.add(N) }; + + let mut ptr = self.ptr.borrow(cs).get(); + if ptr.is_null() { + ptr = self.buf.get().cast::(); + } + + let bytes_left = (end as usize) - (ptr as usize); + let align_offset = (ptr as usize).next_multiple_of(layout.align()) - (ptr as usize); + + if align_offset + layout.size() > bytes_left { + panic!("arena full"); + } + + let res = unsafe { ptr.add(align_offset) }; + let ptr = unsafe { ptr.add(align_offset + layout.size()) }; + + self.ptr.borrow(cs).set(ptr); + + unsafe { &mut *(res as *mut MaybeUninit) } + } + } + + const ARENA_SIZE: usize = 16 * 1024; + static ARENA: Arena = Arena::new(); + + pub struct TaskPoolRef { + // type-erased `&'static mut TaskPool` + // Needed because statics can't have generics. + ptr: Mutex>, + } + unsafe impl Sync for TaskPoolRef {} + unsafe impl Send for TaskPoolRef {} + + impl TaskPoolRef { + pub const fn new() -> Self { + Self { + ptr: Mutex::new(Cell::new(null_mut())), + } + } + + /// Get the pool for this ref, allocating it from the arena the first time. + /// + /// safety: for a given TaskPoolRef instance, must always call with the exact + /// same generic params. + pub unsafe fn get(&'static self) -> &'static TaskPool { + critical_section::with(|cs| { + let ptr = self.ptr.borrow(cs); + if ptr.get().is_null() { + let pool = ARENA.alloc::>(cs); + pool.write(TaskPool::new()); + ptr.set(pool as *mut _ as _); + } + + unsafe { &*(ptr.get() as *const _) } + }) + } + } +} -- cgit From 996c3c1f7e389b1e7d26ca6f02524fff3d63212e Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 24 Nov 2023 22:39:08 +0100 Subject: executor: make task arena size configurable. --- embassy-executor/src/lib.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/lib.rs b/embassy-executor/src/lib.rs index ac7dbb035..d8ac4893b 100644 --- a/embassy-executor/src/lib.rs +++ b/embassy-executor/src/lib.rs @@ -40,6 +40,11 @@ pub mod raw; mod spawner; pub use spawner::*; +mod config { + #![allow(unused)] + include!(concat!(env!("OUT_DIR"), "/config.rs")); +} + /// Implementation details for embassy macros. /// Do not use. Used for macros and HALs only. Not covered by semver guarantees. #[doc(hidden)] @@ -86,7 +91,7 @@ pub mod _export { let align_offset = (ptr as usize).next_multiple_of(layout.align()) - (ptr as usize); if align_offset + layout.size() > bytes_left { - panic!("arena full"); + panic!("embassy-executor: task arena is full. You must increase the arena size, see the documentation for details: https://docs.embassy.dev/embassy-executor/"); } let res = unsafe { ptr.add(align_offset) }; @@ -98,8 +103,7 @@ pub mod _export { } } - const ARENA_SIZE: usize = 16 * 1024; - static ARENA: Arena = Arena::new(); + static ARENA: Arena<{ crate::config::TASK_ARENA_SIZE }> = Arena::new(); pub struct TaskPoolRef { // type-erased `&'static mut TaskPool` -- cgit