Skip to content

core/thread: add thread_join() #21572

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: master
Choose a base branch
from
Open

Conversation

derMihai
Copy link
Contributor

Contribution description

Motivation

Currently there is no clean way of ensuring a thread ends. One can e.g.

void *joinee_fn(void *arg)
{
    ...
    mutex_unlock(arg);
    return NULL;
}

int main(void)
{
    mutex_t exit_signal = MUTEX_INIT_LOCKED;
    thread_create(stack, sizeof(stack),
                           THREAD_PRIORITY_MAIN - 1,
                           0,
                           joinee_fn,
                           &exit_signal,
                           "joinee");

    ...

    mutex_lock(&exit_signal);
    // we can only guarantee joinee finished at this point it its prio was higher!
}

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 resources
can be reused.

Implementation

⚠️ This implementation is rather experimental. Some features are not implemented. It also aims to remain 100% backwards compatible, but it needs not be so. See below.

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, and thread_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 to thread_join() will always be set to NULL. This is so because the thread function "returns" into sched_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.

@github-actions github-actions bot added Area: tests Area: tests and testing framework Area: core Area: RIOT kernel. Handle PRs marked with this with care! labels Jun 30, 2025
@crasbe crasbe added Type: new feature The issue requests / The PR implemements a new feature for RIOT CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jun 30, 2025
@riot-ci
Copy link

riot-ci commented Jun 30, 2025

Murdock results

✔️ PASSED

12c52cd core/thread: add thread_join()

Success Failures Total Runtime
10525 0 10526 26m:05s

Artifacts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants