|
1 | 1 | //! OS-aware embassy executors. |
2 | 2 |
|
3 | | -use core::{cell::UnsafeCell, mem::MaybeUninit, sync::atomic::Ordering}; |
| 3 | +use core::{cell::UnsafeCell, mem::MaybeUninit, ptr::NonNull, sync::atomic::Ordering}; |
4 | 4 |
|
5 | 5 | use embassy_executor::{SendSpawner, Spawner, raw}; |
6 | | -use esp_hal::interrupt::{InterruptHandler, Priority, software::SoftwareInterrupt}; |
7 | | -#[cfg(multi_core)] |
8 | | -use esp_hal::system::Cpu; |
| 6 | +use esp_hal::{ |
| 7 | + interrupt::{InterruptHandler, Priority, software::SoftwareInterrupt}, |
| 8 | + system::Cpu, |
| 9 | + time::{Duration, Instant}, |
| 10 | +}; |
| 11 | +use esp_sync::NonReentrantMutex; |
9 | 12 | use macros::ram; |
10 | 13 | use portable_atomic::AtomicPtr; |
11 | 14 |
|
12 | | -use crate::task::flags::ThreadFlag; |
| 15 | +use crate::{ |
| 16 | + SCHEDULER, |
| 17 | + task::{TaskExt, TaskPtr}, |
| 18 | +}; |
| 19 | + |
| 20 | +pub(crate) struct FlagsInner { |
| 21 | + owner: TaskPtr, |
| 22 | + waiting: Option<TaskPtr>, |
| 23 | + set: bool, |
| 24 | +} |
| 25 | +impl FlagsInner { |
| 26 | + fn take(&mut self) -> bool { |
| 27 | + if self.set { |
| 28 | + // The flag was set while we weren't looking. |
| 29 | + self.set = false; |
| 30 | + true |
| 31 | + } else { |
| 32 | + // `waiting` signals that the owner should be resumed when the flag is set. Copying |
| 33 | + // the task pointer is an optimization that allows clearing the |
| 34 | + // waiting state without computing the address of a separate field. |
| 35 | + self.waiting = Some(self.owner); |
| 36 | + |
| 37 | + false |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// A single event bit, optimized for the thread-mode embassy executor. |
| 43 | +/// |
| 44 | +/// This takes shortcuts, which make it unsuitable for general purpose use (such as no wait |
| 45 | +/// queue, no timeout, assumes a single thread waits for the flag, there is only a single bit of |
| 46 | +/// flag information). |
| 47 | +struct ThreadFlag { |
| 48 | + inner: NonReentrantMutex<FlagsInner>, |
| 49 | +} |
| 50 | + |
| 51 | +impl ThreadFlag { |
| 52 | + fn new() -> Self { |
| 53 | + let owner = SCHEDULER.with(|scheduler| { |
| 54 | + let current_cpu = Cpu::current() as usize; |
| 55 | + if let Some(current_task) = scheduler.per_cpu[current_cpu].current_task { |
| 56 | + current_task |
| 57 | + } else { |
| 58 | + // We're cheating, the task hasn't been initialized yet. |
| 59 | + NonNull::from(&scheduler.per_cpu[current_cpu].main_task) |
| 60 | + } |
| 61 | + }); |
| 62 | + Self { |
| 63 | + inner: NonReentrantMutex::new(FlagsInner { |
| 64 | + owner, |
| 65 | + waiting: None, |
| 66 | + set: false, |
| 67 | + }), |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + fn with<R>(&self, f: impl FnOnce(&mut FlagsInner) -> R) -> R { |
| 72 | + self.inner.with(|inner| f(inner)) |
| 73 | + } |
| 74 | + |
| 75 | + fn set(&self) { |
| 76 | + self.with(|inner| { |
| 77 | + if let Some(waiting) = inner.waiting.take() { |
| 78 | + // The task is waiting, there is no need to set the flag - resuming the thread |
| 79 | + // is all the signal we need. |
| 80 | + waiting.resume(); |
| 81 | + } else { |
| 82 | + // The task isn't waiting, set the flag. |
| 83 | + inner.set = true; |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + fn get(&self) -> bool { |
| 89 | + self.with(|inner| inner.set) |
| 90 | + } |
| 91 | + |
| 92 | + fn wait(&self) { |
| 93 | + self.with(|inner| { |
| 94 | + if !inner.take() { |
| 95 | + // SCHEDULER.sleep_until, but we know the current task's ID, and we know there |
| 96 | + // is no timeout. |
| 97 | + SCHEDULER.with(|scheduler| { |
| 98 | + scheduler.sleep_task_until(inner.owner, Instant::EPOCH + Duration::MAX); |
| 99 | + crate::task::yield_task(); |
| 100 | + }); |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | +} |
13 | 105 |
|
14 | 106 | #[unsafe(export_name = "__pender")] |
15 | 107 | #[ram] |
|
0 commit comments