aboutsummaryrefslogtreecommitdiff
path: root/embassy-executor/src/arch
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-08-22 16:58:43 -0500
committerxoviat <[email protected]>2023-08-22 16:58:43 -0500
commit7d6edd7b15d2209ac0b96ff8814ecefce2964e36 (patch)
tree7988a9b46855ac187a92cbfc5f38cbbbff695e8d /embassy-executor/src/arch
parent9e3266b74554ea397bdd963ff12a26aa51e77b63 (diff)
parent7bff2ebab3b36cc922505e9db961840109c509ed (diff)
Merge branch 'main' of https://github.com/embassy-rs/embassy into rtc-lp
Diffstat (limited to 'embassy-executor/src/arch')
-rw-r--r--embassy-executor/src/arch/cortex_m.rs88
-rw-r--r--embassy-executor/src/arch/riscv32.rs18
-rw-r--r--embassy-executor/src/arch/std.rs17
-rw-r--r--embassy-executor/src/arch/wasm.rs33
-rw-r--r--embassy-executor/src/arch/xtensa.rs22
5 files changed, 84 insertions, 94 deletions
diff --git a/embassy-executor/src/arch/cortex_m.rs b/embassy-executor/src/arch/cortex_m.rs
index 94c8134d6..0806a22ab 100644
--- a/embassy-executor/src/arch/cortex_m.rs
+++ b/embassy-executor/src/arch/cortex_m.rs
@@ -1,3 +1,49 @@
1const THREAD_PENDER: usize = usize::MAX;
2
3#[export_name = "__pender"]
4#[cfg(any(feature = "executor-thread", feature = "executor-interrupt"))]
5fn __pender(context: *mut ()) {
6 unsafe {
7 // Safety: `context` is either `usize::MAX` created by `Executor::run`, or a valid interrupt
8 // request number given to `InterruptExecutor::start`.
9
10 let context = context as usize;
11
12 #[cfg(feature = "executor-thread")]
13 // Try to make Rust optimize the branching away if we only use thread mode.
14 if !cfg!(feature = "executor-interrupt") || context == THREAD_PENDER {
15 core::arch::asm!("sev");
16 return;
17 }
18
19 #[cfg(feature = "executor-interrupt")]
20 {
21 use cortex_m::interrupt::InterruptNumber;
22 use cortex_m::peripheral::NVIC;
23
24 #[derive(Clone, Copy)]
25 struct Irq(u16);
26 unsafe impl InterruptNumber for Irq {
27 fn number(self) -> u16 {
28 self.0
29 }
30 }
31
32 let irq = Irq(context as u16);
33
34 // STIR is faster, but is only available in v7 and higher.
35 #[cfg(not(armv6m))]
36 {
37 let mut nvic: NVIC = core::mem::transmute(());
38 nvic.request(irq);
39 }
40
41 #[cfg(armv6m)]
42 NVIC::pend(irq);
43 }
44 }
45}
46
1#[cfg(feature = "executor-thread")] 47#[cfg(feature = "executor-thread")]
2pub use thread::*; 48pub use thread::*;
3#[cfg(feature = "executor-thread")] 49#[cfg(feature = "executor-thread")]
@@ -8,18 +54,9 @@ mod thread {
8 #[cfg(feature = "nightly")] 54 #[cfg(feature = "nightly")]
9 pub use embassy_macros::main_cortex_m as main; 55 pub use embassy_macros::main_cortex_m as main;
10 56
11 use crate::raw::{Pender, PenderInner}; 57 use crate::arch::THREAD_PENDER;
12 use crate::{raw, Spawner}; 58 use crate::{raw, Spawner};
13 59
14 #[derive(Copy, Clone)]
15 pub(crate) struct ThreadPender;
16
17 impl ThreadPender {
18 pub(crate) fn pend(self) {
19 unsafe { core::arch::asm!("sev") }
20 }
21 }
22
23 /// Thread mode executor, using WFE/SEV. 60 /// Thread mode executor, using WFE/SEV.
24 /// 61 ///
25 /// This is the simplest and most common kind of executor. It runs on 62 /// This is the simplest and most common kind of executor. It runs on
@@ -39,7 +76,7 @@ mod thread {
39 /// Create a new Executor. 76 /// Create a new Executor.
40 pub fn new() -> Self { 77 pub fn new() -> Self {
41 Self { 78 Self {
42 inner: raw::Executor::new(Pender(PenderInner::Thread(ThreadPender))), 79 inner: raw::Executor::new(THREAD_PENDER as *mut ()),
43 not_send: PhantomData, 80 not_send: PhantomData,
44 } 81 }
45 } 82 }
@@ -86,30 +123,7 @@ mod interrupt {
86 use cortex_m::interrupt::InterruptNumber; 123 use cortex_m::interrupt::InterruptNumber;
87 use cortex_m::peripheral::NVIC; 124 use cortex_m::peripheral::NVIC;
88 125
89 use crate::raw::{self, Pender, PenderInner}; 126 use crate::raw;
90
91 #[derive(Clone, Copy)]
92 pub(crate) struct InterruptPender(u16);
93
94 impl InterruptPender {
95 pub(crate) fn pend(self) {
96 // STIR is faster, but is only available in v7 and higher.
97 #[cfg(not(armv6m))]
98 {
99 let mut nvic: cortex_m::peripheral::NVIC = unsafe { core::mem::transmute(()) };
100 nvic.request(self);
101 }
102
103 #[cfg(armv6m)]
104 cortex_m::peripheral::NVIC::pend(self);
105 }
106 }
107
108 unsafe impl cortex_m::interrupt::InterruptNumber for InterruptPender {
109 fn number(self) -> u16 {
110 self.0
111 }
112 }
113 127
114 /// Interrupt mode executor. 128 /// Interrupt mode executor.
115 /// 129 ///
@@ -194,9 +208,7 @@ mod interrupt {
194 unsafe { 208 unsafe {
195 (&mut *self.executor.get()) 209 (&mut *self.executor.get())
196 .as_mut_ptr() 210 .as_mut_ptr()
197 .write(raw::Executor::new(Pender(PenderInner::Interrupt(InterruptPender( 211 .write(raw::Executor::new(irq.number() as *mut ()))
198 irq.number(),
199 )))))
200 } 212 }
201 213
202 let executor = unsafe { (&*self.executor.get()).assume_init_ref() }; 214 let executor = unsafe { (&*self.executor.get()).assume_init_ref() };
diff --git a/embassy-executor/src/arch/riscv32.rs b/embassy-executor/src/arch/riscv32.rs
index ff7ec1575..40c6877e2 100644
--- a/embassy-executor/src/arch/riscv32.rs
+++ b/embassy-executor/src/arch/riscv32.rs
@@ -11,22 +11,16 @@ mod thread {
11 #[cfg(feature = "nightly")] 11 #[cfg(feature = "nightly")]
12 pub use embassy_macros::main_riscv as main; 12 pub use embassy_macros::main_riscv as main;
13 13
14 use crate::raw::{Pender, PenderInner};
15 use crate::{raw, Spawner}; 14 use crate::{raw, Spawner};
16 15
17 #[derive(Copy, Clone)]
18 pub(crate) struct ThreadPender;
19
20 impl ThreadPender {
21 #[allow(unused)]
22 pub(crate) fn pend(self) {
23 SIGNAL_WORK_THREAD_MODE.store(true, core::sync::atomic::Ordering::SeqCst);
24 }
25 }
26
27 /// global atomic used to keep track of whether there is work to do since sev() is not available on RISCV 16 /// global atomic used to keep track of whether there is work to do since sev() is not available on RISCV
28 static SIGNAL_WORK_THREAD_MODE: AtomicBool = AtomicBool::new(false); 17 static SIGNAL_WORK_THREAD_MODE: AtomicBool = AtomicBool::new(false);
29 18
19 #[export_name = "__pender"]
20 fn __pender(_context: *mut ()) {
21 SIGNAL_WORK_THREAD_MODE.store(true, Ordering::SeqCst);
22 }
23
30 /// RISCV32 Executor 24 /// RISCV32 Executor
31 pub struct Executor { 25 pub struct Executor {
32 inner: raw::Executor, 26 inner: raw::Executor,
@@ -37,7 +31,7 @@ mod thread {
37 /// Create a new Executor. 31 /// Create a new Executor.
38 pub fn new() -> Self { 32 pub fn new() -> Self {
39 Self { 33 Self {
40 inner: raw::Executor::new(Pender(PenderInner::Thread(ThreadPender))), 34 inner: raw::Executor::new(core::ptr::null_mut()),
41 not_send: PhantomData, 35 not_send: PhantomData,
42 } 36 }
43 } 37 }
diff --git a/embassy-executor/src/arch/std.rs b/embassy-executor/src/arch/std.rs
index 4e4a178f0..5b2f7e2e4 100644
--- a/embassy-executor/src/arch/std.rs
+++ b/embassy-executor/src/arch/std.rs
@@ -11,17 +11,12 @@ mod thread {
11 #[cfg(feature = "nightly")] 11 #[cfg(feature = "nightly")]
12 pub use embassy_macros::main_std as main; 12 pub use embassy_macros::main_std as main;
13 13
14 use crate::raw::{Pender, PenderInner};
15 use crate::{raw, Spawner}; 14 use crate::{raw, Spawner};
16 15
17 #[derive(Copy, Clone)] 16 #[export_name = "__pender"]
18 pub(crate) struct ThreadPender(&'static Signaler); 17 fn __pender(context: *mut ()) {
19 18 let signaler: &'static Signaler = unsafe { std::mem::transmute(context) };
20 impl ThreadPender { 19 signaler.signal()
21 #[allow(unused)]
22 pub(crate) fn pend(self) {
23 self.0.signal()
24 }
25 } 20 }
26 21
27 /// Single-threaded std-based executor. 22 /// Single-threaded std-based executor.
@@ -34,9 +29,9 @@ mod thread {
34 impl Executor { 29 impl Executor {
35 /// Create a new Executor. 30 /// Create a new Executor.
36 pub fn new() -> Self { 31 pub fn new() -> Self {
37 let signaler = &*Box::leak(Box::new(Signaler::new())); 32 let signaler = Box::leak(Box::new(Signaler::new()));
38 Self { 33 Self {
39 inner: raw::Executor::new(Pender(PenderInner::Thread(ThreadPender(signaler)))), 34 inner: raw::Executor::new(signaler as *mut Signaler as *mut ()),
40 not_send: PhantomData, 35 not_send: PhantomData,
41 signaler, 36 signaler,
42 } 37 }
diff --git a/embassy-executor/src/arch/wasm.rs b/embassy-executor/src/arch/wasm.rs
index 08ab16b99..934fd69e5 100644
--- a/embassy-executor/src/arch/wasm.rs
+++ b/embassy-executor/src/arch/wasm.rs
@@ -14,14 +14,12 @@ mod thread {
14 use wasm_bindgen::prelude::*; 14 use wasm_bindgen::prelude::*;
15 15
16 use crate::raw::util::UninitCell; 16 use crate::raw::util::UninitCell;
17 use crate::raw::{Pender, PenderInner};
18 use crate::{raw, Spawner}; 17 use crate::{raw, Spawner};
19 18
20 /// WASM executor, wasm_bindgen to schedule tasks on the JS event loop. 19 #[export_name = "__pender"]
21 pub struct Executor { 20 fn __pender(context: *mut ()) {
22 inner: raw::Executor, 21 let signaler: &'static WasmContext = unsafe { std::mem::transmute(context) };
23 ctx: &'static WasmContext, 22 let _ = signaler.promise.then(unsafe { signaler.closure.as_mut() });
24 not_send: PhantomData<*mut ()>,
25 } 23 }
26 24
27 pub(crate) struct WasmContext { 25 pub(crate) struct WasmContext {
@@ -29,16 +27,6 @@ mod thread {
29 closure: UninitCell<Closure<dyn FnMut(JsValue)>>, 27 closure: UninitCell<Closure<dyn FnMut(JsValue)>>,
30 } 28 }
31 29
32 #[derive(Copy, Clone)]
33 pub(crate) struct ThreadPender(&'static WasmContext);
34
35 impl ThreadPender {
36 #[allow(unused)]
37 pub(crate) fn pend(self) {
38 let _ = self.0.promise.then(unsafe { self.0.closure.as_mut() });
39 }
40 }
41
42 impl WasmContext { 30 impl WasmContext {
43 pub fn new() -> Self { 31 pub fn new() -> Self {
44 Self { 32 Self {
@@ -48,14 +36,21 @@ mod thread {
48 } 36 }
49 } 37 }
50 38
39 /// WASM executor, wasm_bindgen to schedule tasks on the JS event loop.
40 pub struct Executor {
41 inner: raw::Executor,
42 ctx: &'static WasmContext,
43 not_send: PhantomData<*mut ()>,
44 }
45
51 impl Executor { 46 impl Executor {
52 /// Create a new Executor. 47 /// Create a new Executor.
53 pub fn new() -> Self { 48 pub fn new() -> Self {
54 let ctx = &*Box::leak(Box::new(WasmContext::new())); 49 let ctx = Box::leak(Box::new(WasmContext::new()));
55 Self { 50 Self {
56 inner: raw::Executor::new(Pender(PenderInner::Thread(ThreadPender(ctx)))), 51 inner: raw::Executor::new(ctx as *mut WasmContext as *mut ()),
57 not_send: PhantomData,
58 ctx, 52 ctx,
53 not_send: PhantomData,
59 } 54 }
60 } 55 }
61 56
diff --git a/embassy-executor/src/arch/xtensa.rs b/embassy-executor/src/arch/xtensa.rs
index 017b2c52b..601d85002 100644
--- a/embassy-executor/src/arch/xtensa.rs
+++ b/embassy-executor/src/arch/xtensa.rs
@@ -8,22 +8,16 @@ mod thread {
8 use core::marker::PhantomData; 8 use core::marker::PhantomData;
9 use core::sync::atomic::{AtomicBool, Ordering}; 9 use core::sync::atomic::{AtomicBool, Ordering};
10 10
11 use crate::raw::{Pender, PenderInner};
12 use crate::{raw, Spawner}; 11 use crate::{raw, Spawner};
13 12
14 #[derive(Copy, Clone)]
15 pub(crate) struct ThreadPender;
16
17 impl ThreadPender {
18 #[allow(unused)]
19 pub(crate) fn pend(self) {
20 SIGNAL_WORK_THREAD_MODE.store(true, core::sync::atomic::Ordering::SeqCst);
21 }
22 }
23
24 /// global atomic used to keep track of whether there is work to do since sev() is not available on Xtensa 13 /// global atomic used to keep track of whether there is work to do since sev() is not available on Xtensa
25 static SIGNAL_WORK_THREAD_MODE: AtomicBool = AtomicBool::new(false); 14 static SIGNAL_WORK_THREAD_MODE: AtomicBool = AtomicBool::new(false);
26 15
16 #[export_name = "__pender"]
17 fn __pender(_context: *mut ()) {
18 SIGNAL_WORK_THREAD_MODE.store(true, Ordering::SeqCst);
19 }
20
27 /// Xtensa Executor 21 /// Xtensa Executor
28 pub struct Executor { 22 pub struct Executor {
29 inner: raw::Executor, 23 inner: raw::Executor,
@@ -34,7 +28,7 @@ mod thread {
34 /// Create a new Executor. 28 /// Create a new Executor.
35 pub fn new() -> Self { 29 pub fn new() -> Self {
36 Self { 30 Self {
37 inner: raw::Executor::new(Pender(PenderInner::Thread(ThreadPender))), 31 inner: raw::Executor::new(core::ptr::null_mut()),
38 not_send: PhantomData, 32 not_send: PhantomData,
39 } 33 }
40 } 34 }
@@ -77,8 +71,8 @@ mod thread {
77 SIGNAL_WORK_THREAD_MODE.store(false, Ordering::SeqCst); 71 SIGNAL_WORK_THREAD_MODE.store(false, Ordering::SeqCst);
78 72
79 core::arch::asm!( 73 core::arch::asm!(
80 "wsr.ps {0}", 74 "wsr.ps {0}",
81 "rsync", in(reg) token) 75 "rsync", in(reg) token)
82 } else { 76 } else {
83 // waiti sets the PS.INTLEVEL when slipping into sleep 77 // waiti sets the PS.INTLEVEL when slipping into sleep
84 // because critical sections in Xtensa are implemented via increasing 78 // because critical sections in Xtensa are implemented via increasing