Skip to content

Commit e64cadb

Browse files
committed
zephyr: thread: Add 'join_timeout' to RunningThread
Allow an explicit timeout to be given to join, replacing the implementation of the blocking one to one that calls it with a Forever timeout. Signed-off-by: David Brown <[email protected]>
1 parent a683eb8 commit e64cadb

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

zephyr/src/thread.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use zephyr_sys::{
2424

2525
use crate::{
2626
align::AlignAs,
27+
error::to_result_void,
2728
sys::{K_FOREVER, K_NO_WAIT},
29+
time::{Forever, Timeout},
2830
};
2931

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

251253
impl RunningThread {
254+
/// Wait, with timeout, for this thread to finish executing.
255+
///
256+
/// Will block until either the thread terminates, or the timeout occurrs.
257+
pub fn join_timeout<T>(&self, timeout: T) -> crate::Result<()>
258+
where
259+
T: Into<Timeout>,
260+
{
261+
let timeout: Timeout = timeout.into();
262+
let ret = unsafe { k_thread_join(self.id, timeout.0) };
263+
to_result_void(ret)
264+
}
265+
252266
/// Wait for this thread to finish executing.
253267
///
254268
/// Will block until the thread has terminated.
255269
///
256270
/// TODO: Allow a timeout?
257271
/// TODO: Should we try to return a value?
258-
pub fn join(&self) {
259-
unsafe {
260-
// TODO: Can we do something meaningful with the result?
261-
k_thread_join(self.id, K_FOREVER);
262-
263-
// TODO: Ideally, we could put the thread state back to avoid the need for another join
264-
// check when re-allocating the thread.
265-
}
272+
pub fn join(&self) -> crate::Result<()> {
273+
self.join_timeout(Forever)
266274
}
267275
}
268276

0 commit comments

Comments
 (0)