Skip to content

Commit 2d8366c

Browse files
committed
Cherry-pick #15603
1 parent 97d4676 commit 2d8366c

File tree

5 files changed

+20
-29
lines changed

5 files changed

+20
-29
lines changed

src/library_pthread.js

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ var LibraryPThread = {
408408
},
409409

410410
__emscripten_thread_cleanup: function(thread) {
411+
// Called when a thread needs to be cleaned up so it can be reused.
411412
if (!ENVIRONMENT_IS_PTHREAD) cleanupThread(thread);
412413
else postMessage({ 'cmd': 'cleanupThread', 'thread': thread });
413414
},
@@ -751,12 +752,12 @@ var LibraryPThread = {
751752
#endif
752753
},
753754

754-
pthread_kill__deps: ['$killThread', 'emscripten_main_browser_thread_id'],
755+
pthread_kill__deps: ['emscripten_main_browser_thread_id'],
755756
pthread_kill: function(thread, signal) {
756757
if (signal < 0 || signal >= 65/*_NSIG*/) return {{{ cDefine('EINVAL') }}};
757758
if (thread === _emscripten_main_browser_thread_id()) {
758759
if (signal == 0) return 0; // signal == 0 is a no-op.
759-
err('Main thread (id=' + thread + ') cannot be killed with pthread_kill!');
760+
err('Main thread (id=0x' + thread.toString(16) + ') cannot be killed with pthread_kill!');
760761
return {{{ cDefine('ESRCH') }}};
761762
}
762763
if (!thread) {
@@ -765,38 +766,19 @@ var LibraryPThread = {
765766
}
766767
var self = {{{ makeGetValue('thread', C_STRUCTS.pthread.self, 'i32') }}};
767768
if (self !== thread) {
768-
err('pthread_kill attempted on thread ' + thread + ', which does not point to a valid thread, or does not exist anymore!');
769+
err('pthread_kill attempted on thread 0x' + thread.toString(16) + ', which does not point to a valid thread, or does not exist anymore!');
769770
return {{{ cDefine('ESRCH') }}};
770771
}
771-
if (signal != 0) {
772+
if (signal === {{{ cDefine('SIGCANCEL') }}}) { // Used by pthread_cancel in musl
773+
if (!ENVIRONMENT_IS_PTHREAD) cancelThread(thread);
774+
else postMessage({ 'cmd': 'cancelThread', 'thread': thread });
775+
} else if (signal != 0) {
772776
if (!ENVIRONMENT_IS_PTHREAD) killThread(thread);
773-
else postMessage({ 'cmd': 'killThread', 'thread': thread});
777+
else postMessage({ 'cmd': 'killThread', 'thread': thread });
774778
}
775779
return 0;
776780
},
777781

778-
pthread_cancel__deps: ['$cancelThread', 'emscripten_main_browser_thread_id'],
779-
pthread_cancel: function(thread) {
780-
if (thread === _emscripten_main_browser_thread_id()) {
781-
err('Main thread (id=' + thread + ') cannot be canceled!');
782-
return {{{ cDefine('ESRCH') }}};
783-
}
784-
if (!thread) {
785-
err('pthread_cancel attempted on a null thread pointer!');
786-
return {{{ cDefine('ESRCH') }}};
787-
}
788-
var self = {{{ makeGetValue('thread', C_STRUCTS.pthread.self, 'i32') }}};
789-
if (self !== thread) {
790-
err('pthread_cancel attempted on thread ' + thread + ', which does not point to a valid thread, or does not exist anymore!');
791-
return {{{ cDefine('ESRCH') }}};
792-
}
793-
// Signal the thread that it needs to cancel itself.
794-
Atomics.store(HEAPU32, (thread + {{{ C_STRUCTS.pthread.cancel }}}) >> 2, 1);
795-
if (!ENVIRONMENT_IS_PTHREAD) cancelThread(thread);
796-
else postMessage({ 'cmd': 'cancelThread', 'thread': thread});
797-
return 0;
798-
},
799-
800782
// Returns 0 on success, or one of the values -ETIMEDOUT, -EWOULDBLOCK or -EINVAL on error.
801783
_emscripten_futex_wait_non_blocking__deps: ['emscripten_main_thread_process_queued_calls'],
802784
_emscripten_futex_wait_non_blocking: function(addr, val, timeout) {

src/struct_info_internal.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
"timeSpentInStatus",
3030
"name"
3131
]
32-
}
32+
},
33+
"defines": [
34+
"SIGCANCEL"
35+
]
3336
},
3437
{
3538
"file": "dynlink.h",

system/lib/libc/musl/src/thread/pthread_cancel.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ long __cancel()
1414
return -ECANCELED;
1515
}
1616

17+
#ifndef __EMSCRIPTEN__
1718
long __syscall_cp_asm(volatile void *, syscall_arg_t,
1819
syscall_arg_t, syscall_arg_t, syscall_arg_t,
1920
syscall_arg_t, syscall_arg_t, syscall_arg_t);
@@ -66,6 +67,7 @@ static void cancel_handler(int sig, siginfo_t *si, void *ctx)
6667

6768
__syscall(SYS_tkill, self->tid, SIGCANCEL);
6869
}
70+
#endif
6971

7072
void __testcancel()
7173
{
@@ -74,6 +76,7 @@ void __testcancel()
7476
__cancel();
7577
}
7678

79+
#ifndef __EMSCRIPTEN__
7780
static void init_cancellation()
7881
{
7982
struct sigaction sa = {
@@ -83,14 +86,17 @@ static void init_cancellation()
8386
memset(&sa.sa_mask, -1, _NSIG/8);
8487
__libc_sigaction(SIGCANCEL, &sa, 0);
8588
}
89+
#endif
8690

8791
int pthread_cancel(pthread_t t)
8892
{
93+
#ifndef __EMSCRIPTEN__
8994
static int init;
9095
if (!init) {
9196
init_cancellation();
9297
init = 1;
9398
}
99+
#endif
94100
a_store(&t->cancel, 1);
95101
if (t == pthread_self()) {
96102
if (t->canceldisable == PTHREAD_CANCEL_ENABLE && t->cancelasync)

tests/reference_struct_info.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@
322322
"SEEK_END": 2,
323323
"SEEK_SET": 0,
324324
"SIGALRM": 14,
325+
"SIGCANCEL": 33,
325326
"SOCK_CLOEXEC": 524288,
326327
"SOCK_DGRAM": 2,
327328
"SOCK_NONBLOCK": 2048,

tools/system_libs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,6 @@ def get_files(self):
823823
# TODO: Support this. See #12216.
824824
'pthread_setname_np.c',
825825
# TODO: These could be moved away from JS in the upcoming musl upgrade.
826-
'pthread_cancel.c',
827826
'pthread_join.c', 'pthread_testcancel.c',
828827
]
829828
libc_files += files_in_path(

0 commit comments

Comments
 (0)