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 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 16 additions & 8 deletions zephyr/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use zephyr_sys::{

use crate::{
align::AlignAs,
error::to_result_void,
sys::{K_FOREVER, K_NO_WAIT},
time::{Forever, Timeout},
};

/// Adjust a given requested stack size up for the alignment. This is just the stack, and the
Expand Down Expand Up @@ -249,20 +251,26 @@ pub struct RunningThread {
}

impl RunningThread {
/// Wait, with timeout, for this thread to finish executing.
///
/// Will block until either the thread terminates, or the timeout occurrs.
pub fn join_timeout<T>(&self, timeout: T) -> crate::Result<()>
where
T: Into<Timeout>,
{
let timeout: Timeout = timeout.into();
let ret = unsafe { k_thread_join(self.id, timeout.0) };
to_result_void(ret)
}

/// Wait for this thread to finish executing.
///
/// 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 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think handling the timeout is a reasonable request. The Result will be Rust-y. But, thinly wrapped.

/// TODO: Should we try to return a value?
pub fn join(&self) {
unsafe {
// TODO: Can we do something meaningful with the result?
k_thread_join(self.id, K_FOREVER);

// TODO: Ideally, we could put the thread state back to avoid the need for another join
// check when re-allocating the thread.
}
pub fn join(&self) -> crate::Result<()> {
self.join_timeout(Forever)
}
}

Expand Down