Add task name to the preempt api (#4164)

This commit is contained in:
Dániel Buga 2025-09-22 17:34:28 +02:00 committed by GitHub
parent 94d24a99c3
commit 90382c92ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 86 additions and 59 deletions

View File

@ -98,13 +98,14 @@ impl SchedulerState {
pub(crate) fn create_task(
&mut self,
name: &str,
task: extern "C" fn(*mut c_void),
param: *mut c_void,
task_stack_size: usize,
priority: usize,
) -> TaskPtr {
let mut task = Box::new_in(
Task::new(task, param, task_stack_size, priority),
Task::new(name, task, param, task_stack_size, priority),
InternalMemory,
);
task.heap_allocated = true;
@ -115,7 +116,7 @@ impl SchedulerState {
task::yield_task();
}
debug!("Task created: {:?}", task_ptr);
debug!("Task '{}' created: {:?}", name, task_ptr);
task_ptr
}
@ -295,12 +296,13 @@ impl Scheduler {
pub(crate) fn create_task(
&self,
name: &str,
task: extern "C" fn(*mut c_void),
param: *mut c_void,
task_stack_size: usize,
priority: u32,
) -> TaskPtr {
self.with(|state| state.create_task(task, param, task_stack_size, priority as usize))
self.with(|state| state.create_task(name, task, param, task_stack_size, priority as usize))
}
pub(crate) fn sleep_until(&self, wake_at: Instant) -> bool {
@ -347,6 +349,7 @@ impl esp_radio_preempt_driver::Scheduler for Scheduler {
fn task_create(
&self,
name: &str,
task: extern "C" fn(*mut c_void),
param: *mut c_void,
priority: u32,
@ -354,6 +357,7 @@ impl esp_radio_preempt_driver::Scheduler for Scheduler {
task_stack_size: usize,
) -> *mut c_void {
self.create_task(
name,
task,
param,
task_stack_size,

View File

@ -231,14 +231,15 @@ const STACK_CANARY: u32 =
impl Task {
pub(crate) fn new(
name: &str,
task_fn: extern "C" fn(*mut c_void),
param: *mut c_void,
task_stack_size: usize,
priority: usize,
) -> Self {
trace!(
"task_create {:?}({:?}) stack_size = {} priority = {}",
task_fn, param, task_stack_size, priority
"task_create {} {:?}({:?}) stack_size = {} priority = {}",
name, task_fn, param, task_stack_size, priority
);
// Make sure the stack guard doesn't eat into the stack size.

View File

@ -57,7 +57,8 @@ impl TimerQueueInner {
}
} else {
// create the timer task
let task_ptr = SCHEDULER.create_task(timer_task, core::ptr::null_mut(), 8192, 2);
let task_ptr =
SCHEDULER.create_task("timer", timer_task, core::ptr::null_mut(), 8192, 2);
self.task = Some(task_ptr);
self.next_wakeup = due;
}

View File

@ -39,6 +39,7 @@ unsafe extern "Rust" {
fn esp_preempt_current_task() -> *mut c_void;
fn esp_preempt_max_task_priority() -> u32;
fn esp_preempt_task_create(
name: &str,
task: extern "C" fn(*mut c_void),
param: *mut c_void,
priority: u32,
@ -57,42 +58,43 @@ unsafe extern "Rust" {
/// See the [module documentation][crate] for an example.
#[macro_export]
macro_rules! scheduler_impl {
($vis:vis static $name:ident: $t: ty = $val:expr) => {
$vis static $name: $t = $val;
($vis:vis static $driver:ident: $t: ty = $val:expr) => {
$vis static $driver: $t = $val;
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_initialized() -> bool {
<$t as $crate::Scheduler>::initialized(&$name)
<$t as $crate::Scheduler>::initialized(&$driver)
}
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_yield_task() {
<$t as $crate::Scheduler>::yield_task(&$name)
<$t as $crate::Scheduler>::yield_task(&$driver)
}
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_yield_task_from_isr() {
<$t as $crate::Scheduler>::yield_task_from_isr(&$name)
<$t as $crate::Scheduler>::yield_task_from_isr(&$driver)
}
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_current_task() -> *mut c_void {
<$t as $crate::Scheduler>::current_task(&$name)
<$t as $crate::Scheduler>::current_task(&$driver)
}
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_max_task_priority() -> u32 {
<$t as $crate::Scheduler>::max_task_priority(&$name)
<$t as $crate::Scheduler>::max_task_priority(&$driver)
}
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_task_create(
name: &str,
task: extern "C" fn(*mut c_void),
param: *mut c_void,
priority: u32,
@ -100,7 +102,8 @@ macro_rules! scheduler_impl {
task_stack_size: usize,
) -> *mut c_void {
<$t as $crate::Scheduler>::task_create(
&$name,
&$driver,
name,
task,
param,
priority,
@ -112,25 +115,25 @@ macro_rules! scheduler_impl {
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_schedule_task_deletion(task_handle: *mut c_void) {
<$t as $crate::Scheduler>::schedule_task_deletion(&$name, task_handle)
<$t as $crate::Scheduler>::schedule_task_deletion(&$driver, task_handle)
}
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_current_task_thread_semaphore() -> SemaphorePtr {
<$t as $crate::Scheduler>::current_task_thread_semaphore(&$name)
<$t as $crate::Scheduler>::current_task_thread_semaphore(&$driver)
}
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_usleep(us: u32) {
<$t as $crate::Scheduler>::usleep(&$name, us)
<$t as $crate::Scheduler>::usleep(&$driver, us)
}
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_now() -> u64 {
<$t as $crate::Scheduler>::now(&$name)
<$t as $crate::Scheduler>::now(&$driver)
}
};
}
@ -166,6 +169,7 @@ macro_rules! scheduler_impl {
///
/// fn task_create(
/// &self,
/// name: &str,
/// task: extern "C" fn(*mut c_void),
/// param: *mut c_void,
/// priority: u32,
@ -220,6 +224,7 @@ pub trait Scheduler: Send + Sync + 'static {
/// It should allocate the stack.
fn task_create(
&self,
name: &str,
task: extern "C" fn(*mut c_void),
param: *mut c_void,
priority: u32,
@ -293,13 +298,14 @@ pub fn max_task_priority() -> u32 {
/// pointed to by `param` needs to be `Send` and the task takes ownership over it.
#[inline]
pub unsafe fn task_create(
name: &str,
task: extern "C" fn(*mut c_void),
param: *mut c_void,
priority: u32,
pin_to_core: Option<u32>,
task_stack_size: usize,
) -> *mut c_void {
unsafe { esp_preempt_task_create(task, param, priority, pin_to_core, task_stack_size) }
unsafe { esp_preempt_task_create(name, task, param, priority, pin_to_core, task_stack_size) }
}
/// Schedules the given task for deletion.

View File

@ -149,20 +149,18 @@ unsafe extern "C" fn mutex_unlock(_mutex: *const ()) -> i32 {
unsafe extern "C" fn task_create(
func: *mut crate::binary::c_types::c_void,
name: *const c_char,
name_ptr: *const c_char,
stack_depth: u32,
param: *mut crate::binary::c_types::c_void,
prio: u32,
handle: *mut crate::binary::c_types::c_void,
core_id: u32,
) -> i32 {
unsafe {
let n = str_from_c(name);
trace!(
"task_create {:?} {:?} {} {} {:?} {} {:?} {}",
func, name, n, stack_depth, param, prio, handle, core_id
);
}
let name = unsafe { str_from_c(name_ptr) };
trace!(
"task_create {:?} {:?} {} {} {:?} {} {:?} {}",
func, name_ptr, name, stack_depth, param, prio, handle, core_id
);
unsafe {
let task_func = core::mem::transmute::<
@ -171,6 +169,7 @@ unsafe extern "C" fn task_create(
>(func);
let task = crate::preempt::task_create(
name,
task_func,
param,
prio,

View File

@ -385,14 +385,11 @@ unsafe extern "C" fn task_create(
task_handle: *const c_void,
core_id: u32,
) -> i32 {
unsafe {
let name_str = str_from_c(name);
trace!(
"task_create {:?} {} {} {:?} {} {:?} {}",
task_func, name_str, stack_depth, param, prio, task_handle, core_id,
);
};
let name_str = unsafe { str_from_c(name) };
trace!(
"task_create {:?} {} {} {:?} {} {:?} {}",
task_func, name_str, stack_depth, param, prio, task_handle, core_id,
);
unsafe {
*(task_handle as *mut usize) = 0;
@ -402,6 +399,7 @@ unsafe extern "C" fn task_create(
let task_func = transmute::<*mut c_void, extern "C" fn(*mut c_void)>(task_func);
let task = crate::preempt::task_create(
name_str,
task_func,
param,
prio,

View File

@ -410,6 +410,41 @@ pub unsafe extern "C" fn event_group_wait_bits(
todo!("event_group_wait_bits")
}
fn common_task_create(
task_func: *mut c_void,
name: *const c_char,
stack_depth: u32,
param: *mut c_void,
prio: u32,
task_handle: *mut c_void,
core_id: Option<u32>,
) -> i32 {
let task_name = unsafe { str_from_c(name as _) };
trace!(
"task_create task_func {:?} name {} stack_depth {} param {:?} prio {}, task_handle {:?} core_id {:?}",
task_func, task_name, stack_depth, param, prio, task_handle, core_id
);
unsafe {
let task_func = core::mem::transmute::<
*mut c_void,
extern "C" fn(*mut esp_wifi_sys::c_types::c_void),
>(task_func);
let task = crate::preempt::task_create(
task_name,
task_func,
param,
prio,
core_id,
stack_depth as usize,
);
*(task_handle as *mut usize) = task as usize;
1
}
}
/// **************************************************************************
/// Name: esp_task_create_pinned_to_core
///
@ -440,34 +475,15 @@ pub unsafe extern "C" fn task_create_pinned_to_core(
task_handle: *mut c_void,
core_id: u32,
) -> i32 {
trace!(
"task_create_pinned_to_core task_func {:?} name {} stack_depth {} param {:?} prio {}, task_handle {:?} core_id {}",
common_task_create(
task_func,
unsafe { str_from_c(name as _) },
name,
stack_depth,
param,
prio,
task_handle,
core_id
);
unsafe {
let task_func = core::mem::transmute::<
*mut c_void,
extern "C" fn(*mut esp_wifi_sys::c_types::c_void),
>(task_func);
let task = crate::preempt::task_create(
task_func,
param,
prio,
if core_id < 2 { Some(core_id) } else { None },
stack_depth as usize,
);
*(task_handle as *mut usize) = task as usize;
1
}
if core_id < 2 { Some(core_id) } else { None },
)
}
/// **************************************************************************
@ -497,7 +513,7 @@ pub unsafe extern "C" fn task_create(
prio: u32,
task_handle: *mut c_void,
) -> i32 {
unsafe { task_create_pinned_to_core(task_func, name, stack_depth, param, prio, task_handle, 0) }
common_task_create(task_func, name, stack_depth, param, prio, task_handle, None)
}
/// **************************************************************************

View File

@ -226,6 +226,7 @@ mod tests {
unsafe {
info!("Low: spawning high priority task");
preempt::task_create(
"high_priority_task",
high_priority_task,
(&raw const test_context).cast::<c_void>().cast_mut(),
3,
@ -234,6 +235,7 @@ mod tests {
);
info!("Low: spawning medium priority task");
preempt::task_create(
"medium_priority_task",
medium_priority_task,
(&raw const test_context).cast::<c_void>().cast_mut(),
2,