Skip to content

Commit

Permalink
feat(threads): introduce a declarative macro for autostarting threads (
Browse files Browse the repository at this point in the history
  • Loading branch information
ROMemories authored May 24, 2024
2 parents 61c839e + 6112dcf commit 8240907
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ document-features = "0.2.8"
heapless = { version = "0.8.0", default-features = false }
konst = { version = "0.3.8", default-features = false }
ld-memory = { version = "0.2.9" }
paste = { version = "1.0" }
static_cell = { version = "2.0.0", features = ["nightly"] }

[profile.dev]
Expand Down
10 changes: 2 additions & 8 deletions src/riot-rs-macros/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn thread(args: TokenStream, item: TokenStream) -> TokenStream {
#[allow(clippy::wildcard_imports)]
use thread::*;

use quote::{format_ident, quote};
use quote::quote;

let mut attrs = Attributes::default();
let thread_parser = syn::meta::parser(|meta| attrs.parse(&meta));
Expand All @@ -57,7 +57,6 @@ pub fn thread(args: TokenStream, item: TokenStream) -> TokenStream {
};

let fn_name = thread_function.sig.ident.clone();
let slice_fn_name_ident = format_ident!("__start_thread_{fn_name}");
let Parameters {
stack_size,
priority,
Expand All @@ -69,12 +68,7 @@ pub fn thread(args: TokenStream, item: TokenStream) -> TokenStream {
#no_mangle_attr
#thread_function

#[#riot_rs_crate::linkme::distributed_slice(#riot_rs_crate::thread::THREAD_FNS)]
#[linkme(crate = #riot_rs_crate::linkme)]
fn #slice_fn_name_ident() {
let stack = #riot_rs_crate::static_cell::make_static!([0u8; #stack_size as usize]);
#riot_rs_crate::thread::thread_create_noarg(#fn_name, stack, #priority);
}
#riot_rs_crate::thread::autostart_thread!(#fn_name, stacksize = #stack_size, priority = #priority);
};

TokenStream::from(expanded)
Expand Down
2 changes: 2 additions & 0 deletions src/riot-rs-threads/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ workspace = true
cfg-if.workspace = true
critical-section.workspace = true
linkme = { workspace = true }
paste.workspace = true
riot-rs-runqueue.workspace = true
static_cell.workspace = true

[target.'cfg(context = "esp32c3")'.dependencies]
esp-hal = { workspace = true, features = ["esp32c3"] }
Expand Down
16 changes: 16 additions & 0 deletions src/riot-rs-threads/src/autostart_thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// Starts the `fn_name` function in a dedicated thread at startup.
///
/// The thread is given a `stacksize`-byte stack, and has priority `priority`.
#[macro_export]
macro_rules! autostart_thread {
($fn_name:ident, stacksize = $stacksize:literal, priority = $priority:literal) => {
$crate::macro_reexports::paste::paste! {
#[$crate::macro_reexports::linkme::distributed_slice($crate::THREAD_FNS)]
#[linkme(crate = $crate::macro_reexports::linkme)]
fn [<__start_thread_ $fn_name>] () {
let stack = $crate::macro_reexports::static_cell::make_static!([0u8; $stacksize as usize]);
$crate::thread_create_noarg($fn_name, stack, $priority);
}
}
};
}
9 changes: 9 additions & 0 deletions src/riot-rs-threads/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![allow(clippy::indexing_slicing)]

mod arch;
mod autostart_thread;
mod ensure_once;
mod thread;
mod threadlist;
Expand All @@ -14,6 +15,14 @@ pub mod channel;
pub mod lock;
pub mod thread_flags;

#[doc(hidden)]
pub mod macro_reexports {
// Used by `autostart_thread`
pub use linkme;
pub use paste;
pub use static_cell;
}

pub use riot_rs_runqueue::{RunqueueId, ThreadId};
pub use thread_flags as flags;

Expand Down

0 comments on commit 8240907

Please sign in to comment.