Add task priority to the preempt driver (#4064)

This commit is contained in:
Dániel Buga 2025-09-05 18:30:16 +02:00 committed by GitHub
parent b9dd86258a
commit 445f6fc1ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 65 additions and 7 deletions

View File

@ -321,10 +321,16 @@ impl esp_radio_preempt_driver::Scheduler for Scheduler {
timer::yield_task() timer::yield_task()
} }
fn max_task_priority(&self) -> u32 {
255
}
fn task_create( fn task_create(
&self, &self,
task: extern "C" fn(*mut c_void), task: extern "C" fn(*mut c_void),
param: *mut c_void, param: *mut c_void,
_priority: u32,
_pin_to_core: Option<u32>,
task_stack_size: usize, task_stack_size: usize,
) -> *mut c_void { ) -> *mut c_void {
let task = Box::new_in(Context::new(task, param, task_stack_size), InternalMemory); let task = Box::new_in(Context::new(task, param, task_stack_size), InternalMemory);

View File

@ -284,7 +284,7 @@ register_timer_implementation!(Timer);
/// Initializes the `timer` task for the Wi-Fi driver. /// Initializes the `timer` task for the Wi-Fi driver.
pub(crate) fn create_timer_task() { pub(crate) fn create_timer_task() {
// schedule the timer task // schedule the timer task
SCHEDULER.task_create(timer_task, core::ptr::null_mut(), 8192); SCHEDULER.task_create(timer_task, core::ptr::null_mut(), 1, None, 8192);
} }
/// Entry point for the timer task responsible for handling scheduled timer /// Entry point for the timer task responsible for handling scheduled timer

View File

@ -46,9 +46,12 @@ unsafe extern "Rust" {
fn esp_preempt_disable(); fn esp_preempt_disable();
fn esp_preempt_yield_task(); fn esp_preempt_yield_task();
fn esp_preempt_current_task() -> *mut c_void; fn esp_preempt_current_task() -> *mut c_void;
fn esp_preempt_max_task_priority() -> u32;
fn esp_preempt_task_create( fn esp_preempt_task_create(
task: extern "C" fn(*mut c_void), task: extern "C" fn(*mut c_void),
param: *mut c_void, param: *mut c_void,
priority: u32,
pin_to_core: Option<u32>,
task_stack_size: usize, task_stack_size: usize,
) -> *mut c_void; ) -> *mut c_void;
fn esp_preempt_schedule_task_deletion(task_handle: *mut c_void); fn esp_preempt_schedule_task_deletion(task_handle: *mut c_void);
@ -96,14 +99,29 @@ macro_rules! scheduler_impl {
<$t as $crate::Scheduler>::current_task(&$name) <$t as $crate::Scheduler>::current_task(&$name)
} }
#[unsafe(no_mangle)]
#[inline]
fn esp_preempt_max_task_priority() -> u32 {
<$t as $crate::Scheduler>::max_task_priority(&$name)
}
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
#[inline] #[inline]
fn esp_preempt_task_create( fn esp_preempt_task_create(
task: extern "C" fn(*mut c_void), task: extern "C" fn(*mut c_void),
param: *mut c_void, param: *mut c_void,
priority: u32,
core_id: Option<u32>,
task_stack_size: usize, task_stack_size: usize,
) -> *mut c_void { ) -> *mut c_void {
<$t as $crate::Scheduler>::task_create(&$name, task, param, task_stack_size) <$t as $crate::Scheduler>::task_create(
&$name,
task,
param,
priority,
core_id,
task_stack_size,
)
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
@ -153,12 +171,18 @@ pub trait Scheduler: Send + Sync + 'static {
/// This function is called by `esp_radio::init` to retrieve a pointer to the current task. /// This function is called by `esp_radio::init` to retrieve a pointer to the current task.
fn current_task(&self) -> *mut c_void; fn current_task(&self) -> *mut c_void;
/// This function returns the maximum task priority level.
/// Higher number is considered to be higher priority.
fn max_task_priority(&self) -> u32;
/// This function is used to create threads. /// This function is used to create threads.
/// It should allocate the stack. /// It should allocate the stack.
fn task_create( fn task_create(
&self, &self,
task: extern "C" fn(*mut c_void), task: extern "C" fn(*mut c_void),
param: *mut c_void, param: *mut c_void,
priority: u32,
core_id: Option<u32>,
task_stack_size: usize, task_stack_size: usize,
) -> *mut c_void; ) -> *mut c_void;
@ -218,6 +242,14 @@ pub fn current_task() -> *mut c_void {
unsafe { esp_preempt_current_task() } unsafe { esp_preempt_current_task() }
} }
/// Returns the maximum priority a task can have.
///
/// This function assumes that a bigger number means higher priority.
#[inline]
pub fn max_task_priority() -> u32 {
unsafe { esp_preempt_max_task_priority() }
}
/// Creates a new task with the given initial parameter and stack size. /// Creates a new task with the given initial parameter and stack size.
/// ///
/// ## Safety /// ## Safety
@ -228,9 +260,11 @@ pub fn current_task() -> *mut c_void {
pub unsafe fn task_create( pub unsafe fn task_create(
task: extern "C" fn(*mut c_void), task: extern "C" fn(*mut c_void),
param: *mut c_void, param: *mut c_void,
priority: u32,
pin_to_core: Option<u32>,
task_stack_size: usize, task_stack_size: usize,
) -> *mut c_void { ) -> *mut c_void {
unsafe { esp_preempt_task_create(task, param, task_stack_size) } unsafe { esp_preempt_task_create(task, param, priority, pin_to_core, task_stack_size) }
} }
/// Schedules the given task for deletion. /// Schedules the given task for deletion.

View File

@ -166,7 +166,13 @@ unsafe extern "C" fn task_create(
extern "C" fn(*mut esp_wifi_sys::c_types::c_void), extern "C" fn(*mut esp_wifi_sys::c_types::c_void),
>(func); >(func);
let task = crate::preempt::task_create(task_func, param, stack_depth as usize); let task = crate::preempt::task_create(
task_func,
param,
prio,
if core_id < 2 { Some(core_id) } else { None },
stack_depth as usize,
);
*(handle as *mut usize) = task as usize; *(handle as *mut usize) = task as usize;
} }

View File

@ -395,7 +395,13 @@ unsafe extern "C" fn task_create(
unsafe { unsafe {
let task_func = transmute::<*mut c_void, extern "C" fn(*mut c_void)>(task_func); let task_func = transmute::<*mut c_void, extern "C" fn(*mut c_void)>(task_func);
let task = crate::preempt::task_create(task_func, param, stack_depth as usize); 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; *(task_handle as *mut usize) = task as usize;
} }

View File

@ -456,7 +456,13 @@ pub unsafe extern "C" fn task_create_pinned_to_core(
extern "C" fn(*mut esp_wifi_sys::c_types::c_void), extern "C" fn(*mut esp_wifi_sys::c_types::c_void),
>(task_func); >(task_func);
let task = crate::preempt::task_create(task_func, param, stack_depth as usize); 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; *(task_handle as *mut usize) = task as usize;
1 1
@ -583,7 +589,7 @@ pub unsafe extern "C" fn task_get_current_task() -> *mut c_void {
/// ************************************************************************* /// *************************************************************************
pub unsafe extern "C" fn task_get_max_priority() -> i32 { pub unsafe extern "C" fn task_get_max_priority() -> i32 {
trace!("task_get_max_priority"); trace!("task_get_max_priority");
255 crate::preempt::max_task_priority() as i32
} }
/// ************************************************************************** /// **************************************************************************