|
| 1 | +//! Checks that the task priority is reset whenever a task is activated. |
| 2 | +use constance::{ |
| 3 | + kernel::{cfg::CfgBuilder, Hunk, Task}, |
| 4 | + prelude::*, |
| 5 | +}; |
| 6 | + |
| 7 | +use super::Driver; |
| 8 | +use crate::utils::SeqTracker; |
| 9 | + |
| 10 | +pub struct App<System> { |
| 11 | + task2: Task<System>, |
| 12 | + seq: Hunk<System, SeqTracker>, |
| 13 | +} |
| 14 | + |
| 15 | +impl<System: Kernel> App<System> { |
| 16 | + pub const fn new<D: Driver<Self>>(b: &mut CfgBuilder<System>) -> Self { |
| 17 | + Task::build() |
| 18 | + .start(task1_body::<System, D>) |
| 19 | + .priority(1) |
| 20 | + .active(true) |
| 21 | + .finish(b); |
| 22 | + let task2 = Task::build() |
| 23 | + .start(task2_body::<System, D>) |
| 24 | + .priority(2) |
| 25 | + .active(true) |
| 26 | + .finish(b); |
| 27 | + |
| 28 | + let seq = Hunk::<_, SeqTracker>::build().finish(b); |
| 29 | + |
| 30 | + App { task2, seq } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +fn task1_body<System: Kernel, D: Driver<App<System>>>(_: usize) { |
| 35 | + // `task1` executes first because it has a higher priority. |
| 36 | + D::app().seq.expect_and_replace(0, 1); |
| 37 | + |
| 38 | + // Raise `task2`'s priority to higher than `task1`. `task2` will start |
| 39 | + // executing. |
| 40 | + D::app().task2.set_priority(0).unwrap(); |
| 41 | + |
| 42 | + // Back from `task2`... |
| 43 | + D::app().seq.expect_and_replace(2, 3); |
| 44 | + |
| 45 | + // Activate `task2` again. Its priority is back to the initial value |
| 46 | + // (lower than `task1`). This time we don't raise its priority, so the |
| 47 | + // system won't perform a context switch until `task1` exits. |
| 48 | + D::app().task2.activate().unwrap(); |
| 49 | + |
| 50 | + D::app().seq.expect_and_replace(3, 4); |
| 51 | + |
| 52 | + // Exit from `task1`, relinquishing the control to `task2`. |
| 53 | +} |
| 54 | + |
| 55 | +fn task2_body<System: Kernel, D: Driver<App<System>>>(_: usize) { |
| 56 | + match D::app().seq.get() { |
| 57 | + 1 => { |
| 58 | + D::app().seq.expect_and_replace(1, 2); |
| 59 | + } |
| 60 | + _ => { |
| 61 | + D::app().seq.expect_and_replace(4, 5); |
| 62 | + D::success(); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments