Skip to content

Move pthread_tryjoin_np to native code. NFC#15458

Merged
sbc100 merged 1 commit into
mainfrom
tryjoin_np_native
Nov 11, 2021
Merged

Move pthread_tryjoin_np to native code. NFC#15458
sbc100 merged 1 commit into
mainfrom
tryjoin_np_native

Conversation

@sbc100
Copy link
Copy Markdown
Collaborator

@sbc100 sbc100 commented Nov 9, 2021

This matches more closely the musl version.

@sbc100 sbc100 force-pushed the tryjoin_np_native branch 2 times, most recently from 8cafa2e to a352e35 Compare November 9, 2021 23:30
@sbc100 sbc100 requested review from kleisauke and kripken November 9, 2021 23:32
@sbc100 sbc100 changed the title Move pthread_tryjoin_np to native code Move pthread_tryjoin_np to native code. NFC Nov 9, 2021
Comment thread system/lib/pthread/pthread_create.c Outdated
Comment thread system/lib/pthread/pthread_join.c Outdated
extern int __pthread_join_js(pthread_t thread, void **retval);

int __pthread_join(pthread_t thread, void **retval) {
if (thread->self != thread) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The JS version checked if thread was NULL before doing these, do we not still want that?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

None of the musl entry points to this.. so it seems unnecessary.. calling these functions with NULL in undefined behaviour, I suppose we could at a check in debug builds, but we might also need to then add a debug configuration of libc. I'm tempted to go with musl on this one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fair enough. ASan should catch bugs there anyhow.

Comment thread system/lib/pthread/pthread_join.c Outdated
// not exist anymore.
return ESRCH;
}
if (thread->detach_state == DT_DETACHED) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we not need to access this atomically? JS seemed to (on detached, which I can't seem to find.) It seems to be defined as "volatile", not atomic, in pthread_impl.h

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, musl is using volatile here for these locations rather than atomics. This is what they do for all their atomics api in src/internal/atomic.h.

I think this pre-check is equivalent to the existing one. We still need to handle the case where the thread becomes detached while are joining it.. and I don't thing the JS code currently does that.. but that is a separate issue I think.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I changed this to use emscripten_atomic_load_u32 to match the existing JS impl. And I added TODO.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

having made that change I'm not sure its really necessary.

musl code internally seems perfectly happy to read this variable without any special atomic functions:

https://github.com/emscripten-core/musl/blob/85e0e3519655220688e757b9d5bfd314923548bd/src/thread/pthread_join.c#L16

and:

https://github.com/emscripten-core/musl/blob/85e0e3519655220688e757b9d5bfd314923548bd/src/thread/pthread_join.c#L35

In these cases I think as long as we don't get tearing then its fine to read out-of-date values (and race with other threads).

see

Comment thread system/lib/pthread/pthread_join.c Outdated
extern int __pthread_join_js(pthread_t thread, void **retval);

int __pthread_join(pthread_t thread, void **retval) {
if (thread->self != thread) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fair enough. ASan should catch bugs there anyhow.

Copy link
Copy Markdown
Collaborator

@kleisauke kleisauke left a comment

Choose a reason for hiding this comment

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

LGTM! Though, I wonder about that posixtest.test_pthread_detach_1_2 test failure (IIRC, the same issue is also present in #13007 when I moved the whole pthread_join to native code, it was the only test I had to temporarily disable).

@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented Nov 10, 2021

LGTM! Though, I wonder about that posixtest.test_pthread_detach_1_2 test failure (IIRC, the same issue is also present in #13007 when I moved the whole pthread_join to native code, it was the only test I had to temporarily disable).

I found and fixed that issue.

@sbc100 sbc100 enabled auto-merge (squash) November 10, 2021 18:01
@sbc100 sbc100 force-pushed the tryjoin_np_native branch 3 times, most recently from dd29a37 to 25fc1bd Compare November 11, 2021 00:36
This matches more closely the musl version.
@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented Nov 11, 2021

I made a few final changes to this CL (mostly adding the extra tryjoin argument back), so maybe take another look post-landing..

@sbc100 sbc100 merged commit 0522fe6 into main Nov 11, 2021
@sbc100 sbc100 deleted the tryjoin_np_native branch November 11, 2021 02:12
@kripken
Copy link
Copy Markdown
Member

kripken commented Nov 11, 2021

Do you have a diff of those final changes? Adding more commits to PRs instead of constantly rebasing into a single commit would have made that easier to figure out. I do think that is a good model to follow in general as I've said.

@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented Nov 11, 2021

Sorry github doesn't keep track of that information very well it seems. I wish there was an easy way in the UI to show the differences.

I didn't realized you were suggesting that I avoid squashing in general went we chatting about this on the musl update PR. I can try to move in that directions but it will require some changes to my workflow. I'm very much about keeping each PR in a single commit and using git show to see the current state and git commit --amend to update it. I will create an alias to replace git show.. something like git diff @{upstream}... should do it I think.

@kripken
Copy link
Copy Markdown
Member

kripken commented Nov 11, 2021

Ah, I see. Well, I don't want to disrupt your general workflow, in specific PRs where this comes up we can figure something out I guess.

For this particular PR, do you recall what those last changes were?

@kripken
Copy link
Copy Markdown
Member

kripken commented Nov 11, 2021

Skimming the code now it looks reasonable, but I didn't read it as thoroughly as before where I compared each line to the previous JS for it etc.

@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented Nov 11, 2021

Ah, I see. Well, I don't want to disrupt your general workflow, in specific PRs where this comes up we can figure something out I guess.

For this particular PR, do you recall what those last changes were?

For this PR, the final change was to re-introduce the third argument to pthread_join_js so the JS could know if was being called from tryjoin or regular join.. the only difference is that tryjoin will not warn about blocking.

@kripken
Copy link
Copy Markdown
Member

kripken commented Nov 12, 2021

I see, thanks. That change lgtm.

mmarczell-graphisoft pushed a commit to GRAPHISOFT/emscripten that referenced this pull request Jan 5, 2022
This matches more closely the musl version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants