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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 13 additions & 27 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ 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.

// A thread is considered reusable when it either returns from its
// entry point, calls pthread_exit, or acts upon a cancellation.
// Detached threads are responsible for calling this themselves,
// otherwise pthread_join is responsible for calling this.
if (!ENVIRONMENT_IS_PTHREAD) cleanupThread(thread);
else postMessage({ 'cmd': 'cleanupThread', 'thread': thread });
},
Expand Down Expand Up @@ -751,12 +756,12 @@ var LibraryPThread = {
#endif
},

pthread_kill__deps: ['$killThread', 'emscripten_main_browser_thread_id'],
pthread_kill__deps: ['emscripten_main_browser_thread_id'],
pthread_kill: function(thread, signal) {
if (signal < 0 || signal >= 65/*_NSIG*/) return {{{ cDefine('EINVAL') }}};
if (thread === _emscripten_main_browser_thread_id()) {
if (signal == 0) return 0; // signal == 0 is a no-op.
err('Main thread (id=' + thread + ') cannot be killed with pthread_kill!');
err('Main thread (id=0x' + thread.toString(16) + ') cannot be killed with pthread_kill!');
return {{{ cDefine('ESRCH') }}};
}
if (!thread) {
Expand All @@ -765,38 +770,19 @@ var LibraryPThread = {
}
var self = {{{ makeGetValue('thread', C_STRUCTS.pthread.self, 'i32') }}};
if (self !== thread) {
err('pthread_kill attempted on thread ' + thread + ', which does not point to a valid thread, or does not exist anymore!');
err('pthread_kill attempted on thread 0x' + thread.toString(16) + ', which does not point to a valid thread, or does not exist anymore!');
return {{{ cDefine('ESRCH') }}};
}
if (signal != 0) {
if (signal === {{{ cDefine('SIGCANCEL') }}}) { // Used by pthread_cancel in musl
if (!ENVIRONMENT_IS_PTHREAD) cancelThread(thread);
else postMessage({ 'cmd': 'cancelThread', 'thread': thread });
} else if (signal != 0) {
if (!ENVIRONMENT_IS_PTHREAD) killThread(thread);
else postMessage({ 'cmd': 'killThread', 'thread': thread});
else postMessage({ 'cmd': 'killThread', 'thread': thread });
}
return 0;
},

pthread_cancel__deps: ['$cancelThread', 'emscripten_main_browser_thread_id'],
pthread_cancel: function(thread) {
if (thread === _emscripten_main_browser_thread_id()) {
err('Main thread (id=' + thread + ') cannot be canceled!');
return {{{ cDefine('ESRCH') }}};
}
if (!thread) {
err('pthread_cancel attempted on a null thread pointer!');
return {{{ cDefine('ESRCH') }}};
}
var self = {{{ makeGetValue('thread', C_STRUCTS.pthread.self, 'i32') }}};
if (self !== thread) {
err('pthread_cancel attempted on thread ' + thread + ', which does not point to a valid thread, or does not exist anymore!');
return {{{ cDefine('ESRCH') }}};
}
// Signal the thread that it needs to cancel itself.
Atomics.store(HEAPU32, (thread + {{{ C_STRUCTS.pthread.cancel }}}) >> 2, 1);
if (!ENVIRONMENT_IS_PTHREAD) cancelThread(thread);
else postMessage({ 'cmd': 'cancelThread', 'thread': thread});
return 0;
},

// Returns 0 on success, or one of the values -ETIMEDOUT, -EWOULDBLOCK or -EINVAL on error.
_emscripten_futex_wait_non_blocking__deps: ['emscripten_main_thread_process_queued_calls'],
_emscripten_futex_wait_non_blocking: function(addr, val, timeout) {
Expand Down
5 changes: 4 additions & 1 deletion src/struct_info_internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"timeSpentInStatus",
"name"
]
}
},
"defines": [
"SIGCANCEL"
]
},
{
"file": "dynlink.h",
Expand Down
4 changes: 4 additions & 0 deletions system/lib/libc/musl/src/thread/pthread_cancel.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "pthread_impl.h"
#include "syscall.h"

#ifndef __EMSCRIPTEN__
hidden long __cancel(), __syscall_cp_asm(), __syscall_cp_c();

long __cancel()
Expand Down Expand Up @@ -83,14 +84,17 @@ static void init_cancellation()
memset(&sa.sa_mask, -1, _NSIG/8);
__libc_sigaction(SIGCANCEL, &sa, 0);
}
#endif

int pthread_cancel(pthread_t t)
{
#ifndef __EMSCRIPTEN__
static int init;
if (!init) {
init_cancellation();
init = 1;
}
#endif
a_store(&t->cancel, 1);
if (t == pthread_self()) {
if (t->canceldisable == PTHREAD_CANCEL_ENABLE && t->cancelasync)
Expand Down
1 change: 1 addition & 0 deletions tests/reference_struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
"SEEK_END": 2,
"SEEK_SET": 0,
"SIGALRM": 14,
"SIGCANCEL": 33,
"SOCK_CLOEXEC": 524288,
"SOCK_DGRAM": 2,
"SOCK_NONBLOCK": 2048,
Expand Down
1 change: 0 additions & 1 deletion tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,6 @@ def get_files(self):
# TODO: Support this. See #12216.
'pthread_setname_np.c',
# TODO: These could be moved away from JS in the upcoming musl upgrade.
'pthread_cancel.c',
'pthread_join.c', 'pthread_testcancel.c',
]
libc_files += files_in_path(
Expand Down