-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Implement non-standard pthread_tryjoin and pthread_timedjoin. #72654
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
Conversation
|
Hello @ClaCodes, and thank you very much for your first pull request to the Zephyr project! |
|
@ClaCodes - I would definitely add the I'll add a more detailed review in a few minutes. |
cfriedt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should convert to k_timeout at the point of entry, rename pthread_join() to static pthread_timedjoin_internal(), also accept a k_timeout_t timeout, and then the other join functions can be 1-line wrappers for the internal version.
Additionally, please add tests in a second commit and follow the commit guidelines.
Tests should be directed to individual functions, so add
ZTEST(pthread, test_pthread_timedjoin)
ZTEST(pthread, test_pthread_tryjoin)
following the existing conventions (see git log), where each test case tests various permutations of invalid input and then valid input.
include/zephyr/posix/pthread.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| int pthread_timedjoin_np(pthread_t thread, void **status, const struct timespec abstime); | |
| int pthread_timedjoin_np(pthread_t thread, void **status, const struct timespec *abstime); |
lib/posix/options/pthread.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One possibly cleaner way to go about this is to convert all absolute struct timespec to k_timeout at the point of entry, and have an internal pthread_timedjoin_internal(pthread_t th, void **status, k_timeout_t timeout) (where the timeout is already specified as a k_timeout_t instead of struct timespec).
That way, e.g.
pthread_join(th, st)->pthread_timedjoin_internal(th, st, K_FOREVER)pthread_tryjoin(th, st)->pthread_timedjoin_internal(th, st, K_NO_WAIT)pthread_timedjoin_np(th, st, to)->pthread_timedjoin_internal(th, st, K_MSEC(timespec_to_timeoutms(to)))
|
@ClaCodes - be sure to tests prior to pushing via twister -i -T tests/posixand check compliance via ./scripts/checkpatch.pl -g <commit-before-yours>.. |
58beb60 to
ab80996
Compare
|
@cfriedt I tried to address your suggestions and used |
451fa74 to
2dddd68
Compare
cfriedt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to check the return value of k_thread_join() now that the assertion is gone.
Otherwise, looks ok.
I'm just on mobile atm, so cannot look in detail.
Please double check that all meaningful input permutations are made in the testsuite as well.
2dddd68 to
db20d74
Compare
There is probably more that could be done. For now idea was:
|
5063dc3 to
2e47c43
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A null check would be good before dereferencing abstime
99e26d2 to
3ba76d2
Compare
|
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
cfriedt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably could use a rebase, and there are a couple of platforms where tests are not passing.
|
@ClaCodes - would be nice to get this in - can you make some changes and resubmit? |
0532093 to
69417b0
Compare
I rebased and tried to make test more robust by adding a bit more time. I am not sure, why some test are failing. I might have some time to look into it more, if adding more time was not good enough. |
|
@ClaCodes - this is pretty close - just needs a nudge past the finish line 🏁 |
eb7e5b2 to
c7382b2
Compare
|
Unfortunately, I am still having trouble with the tests. If someone wants to pick this up, you are welcome to. Otherwise I will be closing. Thanks for understanding and sorry. |
c7382b2 to
b9fa2eb
Compare
|
@ClaCodes - there is a subtle bug that needs to be fixed in the commit titled "posix: pthread: implement non-standard try-join and timed-join". diff --git a/lib/posix/options/pthread.c b/lib/posix/options/pthread.c
index 5dfd6599784..db5d37fc8cc 100644
--- a/lib/posix/options/pthread.c
+++ b/lib/posix/options/pthread.c
@@ -1112,6 +1112,11 @@ static int pthread_timedjoin_internal(pthread_t pthread, void **status, k_timeou
}
ret = k_thread_join(&t->thread, timeout);
+ if (ret != 0) {
+ SYS_SEM_LOCK(&pthread_pool_lock) {
+ t->attr.detachstate = PTHREAD_CREATE_JOINABLE;
+ }
+ }
if (ret == -EBUSY) {
return EBUSY;
} else if (ret == -EAGAIN) { |
b9fa2eb to
9addfd3
Compare
@cfriedt Thank you for sending this patch. |
@cfriedt This seems to have fixed the tests. Could you resolve all conversations you consider resolved and then I will attempt to fix the rests (naming, return codes, ... etc.). |
tests/posix/common/src/pthread.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments should match the updated sleep time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjusted. Nice catch.
tests/posix/common/src/pthread.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be nice to pass the sleep time as arg as well:
| zassert_ok(pthread_create(&th, NULL, timedjoin_thread, NULL)); | |
| useconds_t sleep_time; | |
| ... | |
| /* Creating a threads that exits after 200ms */ | |
| sleep_time = USEC_PER_MSEC * 200U; | |
| zassert_ok(pthread_create(&th, NULL, timedjoin_thread, (void *)sleep_time); |
but probably just personal preference, so non-blocking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is a useconds_t guaranteed to fit into a void *?
For now, I have adjusted the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a bad suggestion, but INT_TO_POINTER() and POINTER_TO_INT() should be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we should use "create a thread" instead of "create a threads".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added this. But with type int instead of useconds_t, so that I could use the INT_TO_POINTER/POINTER_TO_INT macros. Let me know what you think.
tests/posix/common/src/pthread.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could have use the sleep_time variable if my suggestion above is adopted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I generally like the sugestion. See my concern above.
tests/posix/common/src/pthread.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /* Attempting to join immediately should fail */ | |
| /* Attempting to join without blocking should fail */ |
I was wondering why the comment says 'join immediately' when we slept for 100ms above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. I adjusted to /* Attempting to join, when thread is still running, should fail */, what do you think?
tests/posix/common/src/pthread.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just thought that this might be clearer (at least for me)
| /* Attempting to join immediately should succeed now */ | |
| /* Attempting to join without blocking should succeed now */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applied. Thanks.
9addfd3 to
d289583
Compare
tests/posix/common/src/pthread.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /* Creating a threads that exits after 200ms*/ | |
| /* Creating a thread that exits after 200ms*/ |
tests/posix/common/src/pthread.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we should use "create a thread" instead of "create a threads".
d289583 to
2d65e23
Compare
These functions can be used to join pthreads in a non-standard way. The function pthread_tryjoin will not block and simply test whether the thread has exited already. The function pthread_timed_join will only block until the specified time. The functions are wrappers for calling the k_thread_join with timeout K_NO_WAIT and with a specific timeout as opposed to calling it with K_FOREVER. Signed-off-by: Cla Galliard <[email protected]>
Test for the non-standard but useful pthread_timed_join- and pthread_try_join-functions. Signed-off-by: Cla Galliard <[email protected]>
2d65e23 to
3bbce6e
Compare
|
Hi @ClaCodes! To celebrate this milestone and showcase your contribution, we'd love to award you the Zephyr Technical Contributor badge. If you're interested, please claim your badge by filling out this form: Claim Your Zephyr Badge. Thank you for your valuable input, and we look forward to seeing more of your contributions in the future! 🪁 |
Implement the non-standard facilities for joining pthreads.
Open Questions
_npfor non-standard as gnu does?time_tto representforeverin the timespec, which is the correct/portable makro?timespectotimeout-> handle overflow?This is my first PR, so let me know, if something formal is wrong. @cfriedt please let me know your opinion on this.