From a6a674740918ca368cf4798026d1d4226948e2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 26 Sep 2025 10:57:08 +0200 Subject: [PATCH] Delete task when it exits (#4185) --- esp-preempt/src/task/mod.rs | 6 ++++++ esp-preempt/src/task/riscv.rs | 5 +++-- esp-preempt/src/task/xtensa.rs | 5 +++-- hil-test/src/bin/esp_radio_init.rs | 25 ------------------------- 4 files changed, 12 insertions(+), 29 deletions(-) diff --git a/esp-preempt/src/task/mod.rs b/esp-preempt/src/task/mod.rs index dc6426e82..441746bae 100644 --- a/esp-preempt/src/task/mod.rs +++ b/esp-preempt/src/task/mod.rs @@ -262,6 +262,12 @@ pub(crate) struct Task { const STACK_CANARY: u32 = const { esp_config::esp_config_int!(u32, "ESP_HAL_CONFIG_STACK_GUARD_VALUE") }; +#[cfg(feature = "esp-radio")] +extern "C" fn task_wrapper(task_fn: extern "C" fn(*mut c_void), param: *mut c_void) { + task_fn(param); + schedule_task_deletion(core::ptr::null_mut()); +} + impl Task { #[cfg(feature = "esp-radio")] pub(crate) fn new( diff --git a/esp-preempt/src/task/riscv.rs b/esp-preempt/src/task/riscv.rs index a5c772e4c..24f412e96 100644 --- a/esp-preempt/src/task/riscv.rs +++ b/esp-preempt/src/task/riscv.rs @@ -128,8 +128,9 @@ pub(crate) fn new_task_context( let stack_top = stack_top - (stack_top % 16); CpuContext { - pc: task as usize, - a0: param as usize, + pc: super::task_wrapper as usize, + a0: task as usize, + a1: param as usize, sp: stack_top, ..Default::default() } diff --git a/esp-preempt/src/task/xtensa.rs b/esp-preempt/src/task/xtensa.rs index 3aa48e575..50533ea91 100644 --- a/esp-preempt/src/task/xtensa.rs +++ b/esp-preempt/src/task/xtensa.rs @@ -47,10 +47,11 @@ pub(crate) fn new_task_context( } CpuContext { - PC: task_fn as usize as u32, + PC: super::task_wrapper as usize as u32, A0: 0, A1: stack_top, - A6: param as usize as u32, + A6: task_fn as usize as u32, + A7: param as usize as u32, // For windowed ABI set WOE and CALLINC (pretend task was 'call4'd) PS: 0x00040000 | ((1 & 3) << 16), diff --git a/hil-test/src/bin/esp_radio_init.rs b/hil-test/src/bin/esp_radio_init.rs index cef389937..a7e700883 100644 --- a/hil-test/src/bin/esp_radio_init.rs +++ b/hil-test/src/bin/esp_radio_init.rs @@ -247,11 +247,6 @@ mod tests { } context.time_slice_observed.give(); - - // TODO: support one-shot tasks in esp-preempt - unsafe { - preempt::schedule_task_deletion(core::ptr::null_mut()); - } } unsafe { @@ -337,11 +332,6 @@ mod tests { info!("High: released mutex"); context.mutex.give(); - - // TODO: support one-shot tasks in esp-preempt - unsafe { - preempt::schedule_task_deletion(core::ptr::null_mut()); - } } extern "C" fn medium_priority_task(context: *mut c_void) { let context = unsafe { &*(context as *const TestContext) }; @@ -351,11 +341,6 @@ mod tests { info!("Medium: marking test finished"); context.ready_semaphore.give(); - - // TODO: support one-shot tasks in esp-preempt - unsafe { - preempt::schedule_task_deletion(core::ptr::null_mut()); - } } unsafe { @@ -426,21 +411,11 @@ mod tests { let context = unsafe { &*(context as *const TestContext) }; count_impl(context, Cpu::AppCpu); - - // TODO: support one-shot tasks in esp-preempt - unsafe { - preempt::schedule_task_deletion(core::ptr::null_mut()); - } } extern "C" fn count_on_pro_core(context: *mut c_void) { let context = unsafe { &*(context as *const TestContext) }; count_impl(context, Cpu::ProCpu); - - // TODO: support one-shot tasks in esp-preempt - unsafe { - preempt::schedule_task_deletion(core::ptr::null_mut()); - } } esp_preempt::start(