Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standalone waker constructor for executor and expose it #1015

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rtic-macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions rtic-macros/src/codegen/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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: #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(|| {
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() {
Expand Down
2 changes: 2 additions & 0 deletions rtic/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Example:

## [Unreleased]

- Added public `waker` constructor to the executor.

## [v2.1.2] - 2024-12-06

### Changed
Expand Down
7 changes: 6 additions & 1 deletion rtic/src/export/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,16 @@ impl<F: Future> AsyncTaskExecutor<F> {
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)) };

Expand Down
Loading