Skip to content

Commit 445f6fc

Browse files
authored
Add task priority to the preempt driver (#4064)
1 parent b9dd862 commit 445f6fc

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed

esp-preempt/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,16 @@ impl esp_radio_preempt_driver::Scheduler for Scheduler {
321321
timer::yield_task()
322322
}
323323

324+
fn max_task_priority(&self) -> u32 {
325+
255
326+
}
327+
324328
fn task_create(
325329
&self,
326330
task: extern "C" fn(*mut c_void),
327331
param: *mut c_void,
332+
_priority: u32,
333+
_pin_to_core: Option<u32>,
328334
task_stack_size: usize,
329335
) -> *mut c_void {
330336
let task = Box::new_in(Context::new(task, param, task_stack_size), InternalMemory);

esp-preempt/src/timer_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ register_timer_implementation!(Timer);
284284
/// Initializes the `timer` task for the Wi-Fi driver.
285285
pub(crate) fn create_timer_task() {
286286
// schedule the timer task
287-
SCHEDULER.task_create(timer_task, core::ptr::null_mut(), 8192);
287+
SCHEDULER.task_create(timer_task, core::ptr::null_mut(), 1, None, 8192);
288288
}
289289

290290
/// Entry point for the timer task responsible for handling scheduled timer

esp-radio-preempt-driver/src/lib.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ unsafe extern "Rust" {
4646
fn esp_preempt_disable();
4747
fn esp_preempt_yield_task();
4848
fn esp_preempt_current_task() -> *mut c_void;
49+
fn esp_preempt_max_task_priority() -> u32;
4950
fn esp_preempt_task_create(
5051
task: extern "C" fn(*mut c_void),
5152
param: *mut c_void,
53+
priority: u32,
54+
pin_to_core: Option<u32>,
5255
task_stack_size: usize,
5356
) -> *mut c_void;
5457
fn esp_preempt_schedule_task_deletion(task_handle: *mut c_void);
@@ -96,14 +99,29 @@ macro_rules! scheduler_impl {
9699
<$t as $crate::Scheduler>::current_task(&$name)
97100
}
98101

102+
#[unsafe(no_mangle)]
103+
#[inline]
104+
fn esp_preempt_max_task_priority() -> u32 {
105+
<$t as $crate::Scheduler>::max_task_priority(&$name)
106+
}
107+
99108
#[unsafe(no_mangle)]
100109
#[inline]
101110
fn esp_preempt_task_create(
102111
task: extern "C" fn(*mut c_void),
103112
param: *mut c_void,
113+
priority: u32,
114+
core_id: Option<u32>,
104115
task_stack_size: usize,
105116
) -> *mut c_void {
106-
<$t as $crate::Scheduler>::task_create(&$name, task, param, task_stack_size)
117+
<$t as $crate::Scheduler>::task_create(
118+
&$name,
119+
task,
120+
param,
121+
priority,
122+
core_id,
123+
task_stack_size,
124+
)
107125
}
108126

109127
#[unsafe(no_mangle)]
@@ -153,12 +171,18 @@ pub trait Scheduler: Send + Sync + 'static {
153171
/// This function is called by `esp_radio::init` to retrieve a pointer to the current task.
154172
fn current_task(&self) -> *mut c_void;
155173

174+
/// This function returns the maximum task priority level.
175+
/// Higher number is considered to be higher priority.
176+
fn max_task_priority(&self) -> u32;
177+
156178
/// This function is used to create threads.
157179
/// It should allocate the stack.
158180
fn task_create(
159181
&self,
160182
task: extern "C" fn(*mut c_void),
161183
param: *mut c_void,
184+
priority: u32,
185+
core_id: Option<u32>,
162186
task_stack_size: usize,
163187
) -> *mut c_void;
164188

@@ -218,6 +242,14 @@ pub fn current_task() -> *mut c_void {
218242
unsafe { esp_preempt_current_task() }
219243
}
220244

245+
/// Returns the maximum priority a task can have.
246+
///
247+
/// This function assumes that a bigger number means higher priority.
248+
#[inline]
249+
pub fn max_task_priority() -> u32 {
250+
unsafe { esp_preempt_max_task_priority() }
251+
}
252+
221253
/// Creates a new task with the given initial parameter and stack size.
222254
///
223255
/// ## Safety
@@ -228,9 +260,11 @@ pub fn current_task() -> *mut c_void {
228260
pub unsafe fn task_create(
229261
task: extern "C" fn(*mut c_void),
230262
param: *mut c_void,
263+
priority: u32,
264+
pin_to_core: Option<u32>,
231265
task_stack_size: usize,
232266
) -> *mut c_void {
233-
unsafe { esp_preempt_task_create(task, param, task_stack_size) }
267+
unsafe { esp_preempt_task_create(task, param, priority, pin_to_core, task_stack_size) }
234268
}
235269

236270
/// Schedules the given task for deletion.

esp-radio/src/ble/btdm.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ unsafe extern "C" fn task_create(
166166
extern "C" fn(*mut esp_wifi_sys::c_types::c_void),
167167
>(func);
168168

169-
let task = crate::preempt::task_create(task_func, param, stack_depth as usize);
169+
let task = crate::preempt::task_create(
170+
task_func,
171+
param,
172+
prio,
173+
if core_id < 2 { Some(core_id) } else { None },
174+
stack_depth as usize,
175+
);
170176
*(handle as *mut usize) = task as usize;
171177
}
172178

esp-radio/src/ble/npl.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,13 @@ unsafe extern "C" fn task_create(
395395
unsafe {
396396
let task_func = transmute::<*mut c_void, extern "C" fn(*mut c_void)>(task_func);
397397

398-
let task = crate::preempt::task_create(task_func, param, stack_depth as usize);
398+
let task = crate::preempt::task_create(
399+
task_func,
400+
param,
401+
prio,
402+
if core_id < 2 { Some(core_id) } else { None },
403+
stack_depth as usize,
404+
);
399405
*(task_handle as *mut usize) = task as usize;
400406
}
401407

esp-radio/src/wifi/os_adapter/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,13 @@ pub unsafe extern "C" fn task_create_pinned_to_core(
456456
extern "C" fn(*mut esp_wifi_sys::c_types::c_void),
457457
>(task_func);
458458

459-
let task = crate::preempt::task_create(task_func, param, stack_depth as usize);
459+
let task = crate::preempt::task_create(
460+
task_func,
461+
param,
462+
prio,
463+
if core_id < 2 { Some(core_id) } else { None },
464+
stack_depth as usize,
465+
);
460466
*(task_handle as *mut usize) = task as usize;
461467

462468
1
@@ -583,7 +589,7 @@ pub unsafe extern "C" fn task_get_current_task() -> *mut c_void {
583589
/// *************************************************************************
584590
pub unsafe extern "C" fn task_get_max_priority() -> i32 {
585591
trace!("task_get_max_priority");
586-
255
592+
crate::preempt::max_task_priority() as i32
587593
}
588594

589595
/// **************************************************************************

0 commit comments

Comments
 (0)