Skip to content

Move pthread_cancel to native code. NFC. #15603

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

Merged
merged 3 commits into from
Jan 12, 2022

Conversation

kleisauke
Copy link
Collaborator

@kleisauke kleisauke commented Nov 23, 2021

Split from PR #13007.

@@ -244,7 +244,7 @@ void _emscripten_thread_exit(void* result) {
}
}

// Mark as `no_sanitize("address"` since emscripten_pthread_exit destroys
// Mark as `no_sanitize("address")` since emscripten_pthread_exit destroys
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like all the changes to this file are just cleanups.. can you split this out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK, reverted those with commit 72da048. I'll make a new PR with these changes soon (perhaps with [skip ci] in the commit message, since these are all minor changes?).

src/worker.js Outdated
@@ -280,7 +280,6 @@ self.onmessage = function(e) {
if (Module['_pthread_self']()) {
Module['__emscripten_thread_exit'](-1/*PTHREAD_CANCELED*/);
}
postMessage({ 'cmd': 'cancelDone' });
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if the removal on cancelDone can be split out into its own change? It seems like a bugfix its own right as __emscripten_thread_exit should always be enough to recycle the thread. That would be very small bugix PR that would simplify this one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reverted with commit 3008b16, moved to PR #15624.

void __pthread_testcancel() {
struct pthread* self = pthread_self();
if (self->canceldisable)
return;
if (_pthread_isduecanceled(self)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can the removal of _pthread_isduecanceled also be split out into its own PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reverted with commit 8db22b9, moved to PR #15625.

@kleisauke kleisauke changed the title Move pthread_cancel to native code Move pthread_cancel to native code. NFC. Nov 24, 2021
Comment on lines 190 to 193
#ifdef __EMSCRIPTEN__
pthread_testcancel();
#endif

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed this since it looked redundant. This was introduced in commit d2bb705 (which was based on musl 1.0.5), but when musl was updated to 1.1.15 (commit 31cf2bb), it ends up calling __pthread_testcancel twice, see:

if (e == ECANCELED) {
__pthread_testcancel();
__pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
}
#ifdef __EMSCRIPTEN__
pthread_testcancel();
#endif

If preferred, I can also create a separate PR for this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reverted with commit 1146ba5, moved to PR #15657.

kleisauke added a commit to kleisauke/emscripten that referenced this pull request Nov 29, 2021
@kleisauke kleisauke force-pushed the pthread_cancel-native branch from 3008b16 to 1146ba5 Compare November 30, 2021 09:17
kleisauke added a commit to kleisauke/emscripten that referenced this pull request Nov 30, 2021
@kleisauke kleisauke force-pushed the pthread_cancel-native branch from 1146ba5 to d30f858 Compare December 3, 2021 09:53
@kleisauke kleisauke force-pushed the pthread_cancel-native branch from d30f858 to 8c66923 Compare January 11, 2022 13:08
kleisauke added a commit to kleisauke/emscripten that referenced this pull request Jan 11, 2022
@kleisauke
Copy link
Collaborator Author

Rebased, PTAL. This would make reviewing #15604 easier.

@kleisauke kleisauke force-pushed the pthread_cancel-native branch from 8c66923 to 90ef01f Compare January 11, 2022 13:52
kleisauke added a commit to kleisauke/emscripten that referenced this pull request Jan 11, 2022
Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

Great! (lets just ifdef out a little more of the unused code.. but otherwise looks great!)

@@ -408,6 +408,7 @@ var LibraryPThread = {
},

__emscripten_thread_cleanup: function(thread) {
// Called when a thread needs to be cleaned up so it can be reused.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps mention that its called in exactly two places: "When a detached thread exits and when an attached thread is joined"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mentioned/elaborated with commit f4058e2.

@@ -66,6 +67,7 @@ static void cancel_handler(int sig, siginfo_t *si, void *ctx)

__syscall(SYS_tkill, self->tid, SIGCANCEL);
}
#endif

void __testcancel()
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we are including this function, then perhaps we should also be including system/lib/libc/musl/src/thread/pthread_testcancel.c rather than our custom system/lib/pthread/pthread_testcancel.c?

How about ifdef'ing this function out for now and the adding it in as followup PR which includes system/lib/libc/musl/src/thread/pthread_testcancel.c? (i.e. lets keep this PR small and focused.. but we can remove these unused functions).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah you're right, I forgot to adjust the ifdef's when I split PR #15604 (which includes pthread_testcancel from musl rather than using a custom one). Fixed with commit f4058e2.

@@ -14,6 +14,7 @@ long __cancel()
return -ECANCELED;
}

#ifndef __EMSCRIPTEN__
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think can move this up to line 5, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, fixed with commit f4058e2.

@sbc100 sbc100 enabled auto-merge (squash) January 11, 2022 16:45
@sbc100
Copy link
Collaborator

sbc100 commented Jan 11, 2022

Great!

@sbc100
Copy link
Collaborator

sbc100 commented Jan 11, 2022

Can you rebase/merge?

@kleisauke kleisauke force-pushed the pthread_cancel-native branch from f4058e2 to 0b0d88b Compare January 12, 2022 09:57
@kleisauke
Copy link
Collaborator Author

Rebased as requested.

@sbc100 sbc100 merged commit 3add470 into emscripten-core:main Jan 12, 2022
@kleisauke kleisauke deleted the pthread_cancel-native branch January 12, 2022 11:07
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.

2 participants