Skip to content

Commit ba40b4b

Browse files
bugadanibjoernQ
authored andcommitted
Simplify multitasking (esp-rs#334)
1 parent 2e38785 commit ba40b4b

File tree

8 files changed

+64
-106
lines changed

8 files changed

+64
-106
lines changed

esp-wifi/src/compat/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
include::OSI_FUNCS_TIME_BLOCKING,
1010
},
1111
memory_fence::memory_fence,
12-
preempt::preempt::current_task,
12+
preempt::current_task,
1313
timer::yield_task,
1414
};
1515

esp-wifi/src/preempt/mod.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
use atomic_polyfill::AtomicBool;
2-
use core::sync::atomic::Ordering;
3-
4-
pub static mut FIRST_SWITCH: AtomicBool = AtomicBool::new(true);
5-
6-
static mut TASK_TOP: usize = 0;
7-
8-
static mut CTX_NOW: usize = 0;
9-
101
macro_rules! sum {
112
($h:expr) => ($h);
123
($h:expr, $($t:expr),*) =>
@@ -24,6 +15,28 @@ macro_rules! task_stack {
2415
};
2516
}
2617

18+
static mut TASK_TOP: usize = 1;
19+
static mut CTX_NOW: usize = 0;
20+
21+
fn allocate_task() -> usize {
22+
unsafe {
23+
let i = TASK_TOP - 1;
24+
CTX_NOW = TASK_TOP;
25+
TASK_TOP += 1;
26+
i
27+
}
28+
}
29+
30+
fn next_task() {
31+
unsafe {
32+
CTX_NOW = (CTX_NOW + 1) % TASK_TOP;
33+
}
34+
}
35+
36+
pub fn current_task() -> usize {
37+
unsafe { CTX_NOW }
38+
}
39+
2740
#[cfg(coex)]
2841
task_stack!(8192, 8192, 8192);
2942

esp-wifi/src/preempt/preempt_riscv.rs

+21-52
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ static mut CTX_TASKS: [Context; MAX_TASK] = [Context {
4949
_running: false,
5050
}; MAX_TASK];
5151

52-
pub fn task_create(task: extern "C" fn()) -> usize {
52+
pub fn task_create(task: extern "C" fn()) {
5353
unsafe {
54-
let i = TASK_TOP;
55-
TASK_TOP += 1;
54+
let i = allocate_task();
55+
5656
CTX_TASKS[i].trap_frame.pc = task as usize;
5757

5858
let task_stack_size = TASK_STACK_SIZE[i];
@@ -64,19 +64,6 @@ pub fn task_create(task: extern "C" fn()) -> usize {
6464
- 4;
6565
let stack_ptr = task_stack_ptr - (task_stack_ptr % 0x10);
6666
CTX_TASKS[i].trap_frame.sp = stack_ptr;
67-
68-
CTX_NOW = i;
69-
i
70-
}
71-
}
72-
73-
fn task_create_from_mepc(mepc: usize) -> usize {
74-
unsafe {
75-
let i = TASK_TOP;
76-
TASK_TOP += 1;
77-
CTX_TASKS[i].trap_frame.pc = mepc;
78-
CTX_NOW = i;
79-
i
8067
}
8168
}
8269

@@ -156,42 +143,24 @@ pub fn save_task_context(id: usize, pc: usize, trap_frame: &TrapFrame) {
156143
}
157144
}
158145

159-
pub fn next_task() {
160-
unsafe {
161-
CTX_NOW = (CTX_NOW + 1) % TASK_TOP;
162-
}
163-
}
164-
165146
pub fn task_switch(trap_frame: &mut TrapFrame) {
166-
unsafe {
167-
let old_mepc = trap_frame.pc;
168-
169-
if FIRST_SWITCH.load(Ordering::Relaxed) {
170-
FIRST_SWITCH.store(false, Ordering::Relaxed);
171-
let main_task = task_create_from_mepc(old_mepc);
172-
CTX_NOW = main_task;
173-
}
174-
175-
save_task_context(CTX_NOW, old_mepc, trap_frame);
176-
177-
next_task();
178-
179-
let new_pc = restore_task_context(CTX_NOW, trap_frame);
180-
trap_frame.pc = new_pc;
181-
182-
// debug aid! remove when not needed anymore!!!!!
183-
// static mut CNT: u32 = 0;
184-
// if CTX_NOW == 0 {
185-
// if CNT < 5_000 {
186-
// CNT += 1;
187-
// } else {
188-
// CNT = 0;
189-
// info!("@@@ Task {} PC = {:x} {:?}", CTX_NOW, new_pc, trap_frame);
190-
// }
191-
// }
192-
}
193-
}
147+
let old_mepc = trap_frame.pc;
148+
149+
save_task_context(current_task(), old_mepc, trap_frame);
150+
151+
next_task();
152+
153+
let new_pc = restore_task_context(current_task(), trap_frame);
154+
trap_frame.pc = new_pc;
194155

195-
pub fn current_task() -> usize {
196-
unsafe { CTX_NOW }
156+
// debug aid! remove when not needed anymore!!!!!
157+
// static mut CNT: u32 = 0;
158+
// if CTX_NOW == 0 {
159+
// if CNT < 5_000 {
160+
// CNT += 1;
161+
// } else {
162+
// CNT = 0;
163+
// info!("@@@ Task {} PC = {:x} {:?}", CTX_NOW, new_pc, trap_frame);
164+
// }
165+
// }
197166
}

esp-wifi/src/preempt/preempt_xtensa.rs

+17-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
23
use crate::hal::trapframe::TrapFrame;
34

45
#[derive(Debug, Clone, Copy)]
@@ -65,10 +66,9 @@ static mut CTX_TASKS: [TaskContext; MAX_TASK] = [TaskContext {
6566
},
6667
}; MAX_TASK];
6768

68-
pub fn task_create(task: extern "C" fn()) -> usize {
69+
pub fn task_create(task: extern "C" fn()) {
6970
unsafe {
70-
let i = TASK_TOP;
71-
TASK_TOP += 1;
71+
let i = allocate_task();
7272

7373
CTX_TASKS[i].trap_frame.PC = task as u32;
7474

@@ -90,9 +90,6 @@ pub fn task_create(task: extern "C" fn()) -> usize {
9090
*((task_stack_ptr - 8) as *mut u32) = 0;
9191
*((task_stack_ptr - 12) as *mut u32) = stack_ptr;
9292
*((task_stack_ptr - 16) as *mut u32) = 0;
93-
94-
CTX_NOW = i;
95-
i
9693
}
9794
}
9895

@@ -108,37 +105,19 @@ fn save_task_context(id: usize, trap_frame: &TrapFrame) {
108105
}
109106
}
110107

111-
pub fn next_task() {
112-
unsafe {
113-
CTX_NOW = (CTX_NOW + 1) % TASK_TOP;
114-
}
115-
}
116-
117108
pub fn task_switch(trap_frame: &mut TrapFrame) {
118-
unsafe {
119-
if FIRST_SWITCH.load(Ordering::Relaxed) {
120-
FIRST_SWITCH.store(false, Ordering::Relaxed);
121-
TASK_TOP += 1;
122-
CTX_NOW = TASK_TOP - 1;
123-
}
124-
125-
save_task_context(CTX_NOW, trap_frame);
126-
next_task();
127-
restore_task_context(CTX_NOW, trap_frame);
128-
129-
// debug aid! remove when not needed anymore!!!!!
130-
// static mut CNT: u32 = 0;
131-
// if CTX_NOW == 0 {
132-
// if CNT < 2_000 {
133-
// CNT += 1;
134-
// } else {
135-
// CNT = 0;
136-
// info!("@@@ Task {} {:?} ", 1, CTX_TASKS[1].trap_frame.PC);
137-
// }
138-
// }
139-
};
140-
}
141-
142-
pub fn current_task() -> usize {
143-
unsafe { CTX_NOW }
109+
save_task_context(current_task(), trap_frame);
110+
next_task();
111+
restore_task_context(current_task(), trap_frame);
112+
113+
// debug aid! remove when not needed anymore!!!!!
114+
// static mut CNT: u32 = 0;
115+
// if CTX_NOW == 0 {
116+
// if CNT < 2_000 {
117+
// CNT += 1;
118+
// } else {
119+
// CNT = 0;
120+
// info!("@@@ Task {} {:?} ", 1, CTX_TASKS[1].trap_frame.PC);
121+
// }
122+
// }
144123
}

esp-wifi/src/timer/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn setup_timer_isr(timebase: TimeBase) {
1919
setup_timer(timebase);
2020

2121
setup_multitasking();
22+
23+
yield_task();
2224
}
2325

2426
#[allow(unused)]

esp-wifi/src/timer/riscv.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::cell::RefCell;
2-
use core::sync::atomic::Ordering;
32

43
use critical_section::Mutex;
54

@@ -56,8 +55,6 @@ pub fn setup_multitasking() {
5655
unsafe {
5756
riscv::interrupt::enable();
5857
}
59-
60-
while unsafe { crate::preempt::FIRST_SWITCH.load(Ordering::Relaxed) } {}
6158
}
6259

6360
#[interrupt]

esp-wifi/src/timer/xtensa.rs

-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ pub fn setup_multitasking() {
7373
| xtensa_lx_rt::interrupt::CpuInterruptLevel::Level6.mask() | enabled,
7474
);
7575
}
76-
77-
while unsafe { crate::preempt::FIRST_SWITCH.load(Ordering::Relaxed) } {}
7876
}
7977

8078
#[allow(non_snake_case)]

esp-wifi/src/wifi/os_adapter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ pub unsafe extern "C" fn task_ms_to_tick(ms: u32) -> i32 {
789789
*
790790
****************************************************************************/
791791
pub unsafe extern "C" fn task_get_current_task() -> *mut crate::binary::c_types::c_void {
792-
let res = crate::preempt::preempt::current_task() as *mut crate::binary::c_types::c_void;
792+
let res = crate::preempt::current_task() as *mut crate::binary::c_types::c_void;
793793
trace!("task get current task - return {:?}", res);
794794

795795
res

0 commit comments

Comments
 (0)