Skip to content

Implement a proc-macros threads creation #83

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Implement a proc-macros threads creation #83

wants to merge 3 commits into from

Conversation

d3zd3z
Copy link
Collaborator

@d3zd3z d3zd3z commented Apr 9, 2025

Replace how Zephyr threads are declared in Rust. Provides a proc-macro to make this fairly straightforward:

#[zephyr::thread(stack_size = MY_STACK_SIZE)]
fn my_thread(arg1: u32, arg2: MyType) { ... }

This function can then be called, and will return a ReadThread. This has methods for setting priority, and a start method, which returns a RunningThread, which has a join method. The thread can be respawned after it has exited.

The thread macro also accepts a pool_size = nnn parameter, which will create an array of threads, and can be started that many times.

d3zd3z added 3 commits April 9, 2025 10:41
Implement a proc macro that allows a declaration like:

```rust
fn mythread(arg: usize, arg2: &'static Thing) { .. }
```

With this change, creation of threads, with arbitrary "Send" arguments
can be done without needing allocation.  The macros reserves a static
area alongside the `k_thread` structure to use for handing the threads
over.

This creates a function `mythread` with the given args that returns a
ReadyThread.  This has a `start()` method which will begin execution of
the thread.  This results in a RunningThread, which has a join method
that can be used to wait for termination.

Signed-off-by: David Brown <[email protected]>
Move away from the `kobj_define` task declaration to use the new
`#[zephyr::thread]` to define these.  This allows for a more natural
declaration where the thread just looks like an attribute added to a
regular function declaration.

This also eliminates the static Mutex, as the Mutex now has a
constructor that avoids allocation (it is still put in an Arc, though).

Signed-off-by: David Brown <[email protected]>
To help with debugging, try to give created Zephyr threads a readable
name.  Currently, this is based off of the name of the function used in
the declaration of the thread.

Signed-off-by: David Brown <[email protected]>
@d3zd3z d3zd3z requested review from teburd and cfriedt April 9, 2025 16:47
Copy link
Collaborator

@teburd teburd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it

///
/// Will block until the thread has terminated.
///
/// TODO: Allow a timeout?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be smart to allow a timeout and return the integer result.

Copy link
Member

@cfriedt cfriedt Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, would it make more sense to do something similar to what std::thread does where we return a Result that can be either Ok or an Err? I guess that's more "rustonic" than integer return values 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants