core/thread: add thread_join() #21572
Open
+236
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Contribution description
Motivation
Currently there is no clean way of ensuring a thread ends. One can e.g.
But this only works if the priority of the joinee (the thread we're joining) was higher,
otherwise there's no guarantee of progress past
mutex_unlock(arg)
.This PR adds
thread_join()
, which guarantees a thread is finished and its resourcescan be reused.
Implementation
Backwards compatibility
The implementation adds a new thread creation flag. If not explicitly set, the threads life cycle remains unchanged, and it cannot be
thread_join()
ed. But if set, the thread goes into zombie state instead of stopping on exit, andthread_join()
will eventually stop it. I added this distinction for compatibility with any application relying on threads to automatically stop on exit. However, one can also argue that any app doing so is buggy (see above), and as such there is a point in lifting this distinction and making all threads go into zombie mode on exit.Return values
Return values are not implemented yet: the
exit_value
pointer passed tothread_join()
will always be set to NULL. This is so because the thread function "returns" intosched_task_exit()
, so without any CPU-specific preamble there is no way to catch the return value.Testing procedure
Ran the test app on native and a cortex-m board.