From e8a49f409240a953a64b3f9454867a68a6e2b19b Mon Sep 17 00:00:00 2001 From: Oleksandr Babak Date: Thu, 16 Jan 2025 14:28:56 +0100 Subject: [PATCH 1/4] feat(executor): add standalone waker constructor --- rtic/src/export/executor.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rtic/src/export/executor.rs b/rtic/src/export/executor.rs index 8d42c4153fa8..b82dfe97f770 100644 --- a/rtic/src/export/executor.rs +++ b/rtic/src/export/executor.rs @@ -192,11 +192,16 @@ impl AsyncTaskExecutor { self.set_pending(); } + #[inline(always)] + pub const fn waker(&self, wake: fn()) -> Waker { + unsafe { Waker::from_raw(RawWaker::new(wake as *const (), &WAKER_VTABLE)) } + } + /// Poll the future in the executor. #[inline(always)] pub fn poll(&self, wake: fn()) { if self.is_running() && self.check_and_clear_pending() { - let waker = unsafe { Waker::from_raw(RawWaker::new(wake as *const (), &WAKER_VTABLE)) }; + let waker = self.waker(wake); let mut cx = Context::from_waker(&waker); let future = unsafe { Pin::new_unchecked(&mut *(self.task.get() as *mut F)) }; From 4d97b705c90f1da0f7d9323e59fc0fb36c36c0e5 Mon Sep 17 00:00:00 2001 From: Oleksandr Babak Date: Mon, 27 Jan 2025 11:00:10 +0100 Subject: [PATCH 2/4] feat(rtic-macros): expose task waker --- rtic-macros/src/codegen/module.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/rtic-macros/src/codegen/module.rs b/rtic-macros/src/codegen/module.rs index a8700c5bafb9..c10c123ce2da 100644 --- a/rtic-macros/src/codegen/module.rs +++ b/rtic-macros/src/codegen/module.rs @@ -158,6 +158,7 @@ pub fn codegen(ctxt: Context, app: &App, analysis: &Analysis) -> TokenStream2 { }; let internal_spawn_ident = util::internal_task_ident(name, "spawn"); + let internal_waker_ident = util::internal_task_ident(name, "waker"); let from_ptr_n_args = util::from_ptr_n_args_ident(spawnee.inputs.len()); let (input_args, input_tupled, input_untupled, input_ty) = util::regroup_inputs(&spawnee.inputs); @@ -184,11 +185,36 @@ pub fn codegen(ctxt: Context, app: &App, analysis: &Analysis) -> TokenStream2 { } )); + // Waker + items.push(quote!( + #(#cfgs)* + /// Gives waker to the task + #[allow(non_snake_case)] + #[doc(hidden)] + pub fn #internal_waker_ident() -> ::core::task::Waker { + // SAFETY: If `try_allocate` succeeds one must call `spawn`, which we do. + unsafe { + let exec = rtic::export::executor::AsyncTaskExecutor::#from_ptr_n_args(#name, &#exec_name); + exec.waker(|| { + let exec = rtic::export::executor::AsyncTaskExecutor::#from_ptr_n_args(#name, &#exec_name); + exec.set_pending(); + #pend_interrupt + }) + } + } + )); + module_items.push(quote!( #(#cfgs)* #[doc(inline)] pub use super::#internal_spawn_ident as spawn; )); + + module_items.push(quote!( + #(#cfgs)* + #[doc(inline)] + pub use super::#internal_waker_ident as waker; + )); } if items.is_empty() { From aa6e9dbd35499654d36792c838d4cdbe394584c7 Mon Sep 17 00:00:00 2001 From: Oleksandr Babak Date: Mon, 27 Jan 2025 11:04:06 +0100 Subject: [PATCH 3/4] chore: changelog --- rtic-macros/CHANGELOG.md | 2 ++ rtic/CHANGELOG.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/rtic-macros/CHANGELOG.md b/rtic-macros/CHANGELOG.md index 86b051b695fc..6b583b8cbd07 100644 --- a/rtic-macros/CHANGELOG.md +++ b/rtic-macros/CHANGELOG.md @@ -7,6 +7,8 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top! ## [Unreleased] +- Added `waker` getter to software tasks + ## [v2.1.1] - 2024-12-06 ### Changed diff --git a/rtic/CHANGELOG.md b/rtic/CHANGELOG.md index faefaeb8edae..5bad996ec795 100644 --- a/rtic/CHANGELOG.md +++ b/rtic/CHANGELOG.md @@ -20,6 +20,8 @@ Example: ## [Unreleased] +- Added public `waker` constructor to the executor. + ## [v2.1.2] - 2024-12-06 ### Changed From 768a8153d3c71bf083aeb555acc75f0575a74186 Mon Sep 17 00:00:00 2001 From: Oleksandr Babak Date: Mon, 27 Jan 2025 11:09:14 +0100 Subject: [PATCH 4/4] chore: better safety comment --- rtic-macros/src/codegen/module.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtic-macros/src/codegen/module.rs b/rtic-macros/src/codegen/module.rs index c10c123ce2da..1d2f90a6928b 100644 --- a/rtic-macros/src/codegen/module.rs +++ b/rtic-macros/src/codegen/module.rs @@ -192,7 +192,7 @@ pub fn codegen(ctxt: Context, app: &App, analysis: &Analysis) -> TokenStream2 { #[allow(non_snake_case)] #[doc(hidden)] pub fn #internal_waker_ident() -> ::core::task::Waker { - // SAFETY: If `try_allocate` succeeds one must call `spawn`, which we do. + // SAFETY: #exec_name is a valid pointer to an executor. unsafe { let exec = rtic::export::executor::AsyncTaskExecutor::#from_ptr_n_args(#name, &#exec_name); exec.waker(|| {