Skip to content
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
8 changes: 4 additions & 4 deletions system/lib/libc/raise.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ int raise(int sig) {
handler(sig);
}
} else if (handler != SIG_IGN) {
// Avoid a direct call to the handler, and instead call via JS so we can
// avoid strict signature checking.
// https://github.com/emscripten-core/posixtestsuite/issues/6
__call_sighandler(handler, sig);
// Avoid a direct call to the handler, and instead call via JS so we can
// avoid strict signature checking.
// https://github.com/emscripten-core/posixtestsuite/issues/6
__call_sighandler(handler, sig);
}
}
return 0;
Expand Down
8 changes: 8 additions & 0 deletions system/lib/pthread/library_pthread_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,14 @@ int pthread_equal(pthread_t t1, pthread_t t2) {
return t1 == t2;
}

int pthread_kill(pthread_t thread, int sig) {
if (thread != pthread_self()) {
return EINVAL;
}
raise(sig);
return 0;
}

int pthread_mutexattr_init(pthread_mutexattr_t *attr) {
return 0;
}
Expand Down
20 changes: 11 additions & 9 deletions system/lib/pthread/pthread_kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
#include <emscripten_internal.h>

#include "pthread_impl.h"
#include "lock.h"

void do_raise(void* arg) {
int sig = (intptr_t)arg;
static void do_raise(int sig) {
if (sig == SIGCANCEL) {
// For `SIGCANCEL` there is no need to actually call raise to run the
// handler function. The calling thread (the one calling `pthread_cancel`)
Expand All @@ -31,24 +29,28 @@ void do_raise(void* arg) {
_emscripten_runtime_keepalive_clear();
return;
}
raise((intptr_t)sig);
raise(sig);
}

static void proxied_do_raise(void* arg) {
do_raise((intptr_t)arg);
}

int pthread_kill(pthread_t t, int sig) {
if (sig < 0 || sig >= _NSIG) {
return EINVAL;
}
if (t == emscripten_main_runtime_thread_id()) {
if (sig == 0) return 0; // signal == 0 is a no-op.
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.

Was this a perf optimization perhaps?

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.

Just simplifying the code a little.

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.

Oh.. also I'm removing this limitation. For some reason we were disallowing using pthread_kill to send signals to the main runtime thread.. I'm not sure why.

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.

Worth a changelog mention perhaps, if this is a breaking change? Though maybe not if there are hardly any users of this.

Though I would suspect this limitation was there for a reason... maybe git history helps?

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.

This is very niche.. and its changing from non-standard + restrictive behavior to standard + less restrictive behaviour. I tend to think that moving things towards standard behaviour and removing restrictions normally mean less reason to add ChangeLog entry?

Copy link
Copy Markdown
Collaborator Author

@sbc100 sbc100 Apr 9, 2026

Choose a reason for hiding this comment

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

Looks like this was inherited from the JS version in #17041.

The JS version had "err('Main thread (id=' + ptrToString(thread) + ') cannot be killed with pthread_kill!');"

The "cannot be killed" message dates all the way back to 4f29d30 in 2015.

I imagine it was logical to thing that the main browser thread cannot be killed, but kill and also be used to send all kinds of signal. Its doesn't actaully kill anything necessarily.. confusing name. In any case raise (which is what kill really does) works fine on the main browser thread.

return ESRCH;
}
if (!t || !_emscripten_thread_is_valid(t)) {
return ESRCH;
}
if (sig == 0) return 0; // signal == 0 is a no-op.

// The job of pthread_kill is basically to run the (process-wide) signal
// handler on the target thread.
emscripten_proxy_async(emscripten_proxy_get_system_queue(), t, do_raise, (void*)(intptr_t)sig);
if (pthread_equal(pthread_self(), t)) {
do_raise(sig);
} else {
emscripten_proxy_async(emscripten_proxy_get_system_queue(), t, proxied_do_raise, (void*)(intptr_t)sig);
}
return 0;
}
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_ctors1.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19194,
"a.out.js.gz": 7969,
"a.out.nodebug.wasm": 132638,
"a.out.nodebug.wasm.gz": 49927,
"total": 151832,
"total_gz": 57896,
"a.out.nodebug.wasm": 132635,
"a.out.nodebug.wasm.gz": 49922,
"total": 151829,
"total_gz": 57891,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_ctors2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19171,
"a.out.js.gz": 7957,
"a.out.nodebug.wasm": 132064,
"a.out.nodebug.wasm.gz": 49586,
"total": 151235,
"total_gz": 57543,
"a.out.nodebug.wasm": 132061,
"a.out.nodebug.wasm.gz": 49579,
"total": 151232,
"total_gz": 57536,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 23174,
"a.out.js.gz": 8960,
"a.out.nodebug.wasm": 172516,
"a.out.nodebug.wasm.gz": 57438,
"total": 195690,
"total_gz": 66398,
"a.out.nodebug.wasm": 172526,
"a.out.nodebug.wasm.gz": 57447,
"total": 195700,
"total_gz": 66407,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19026,
"a.out.js.gz": 7904,
"a.out.nodebug.wasm": 147922,
"a.out.nodebug.wasm.gz": 55312,
"total": 166948,
"total_gz": 63216,
"a.out.nodebug.wasm": 147926,
"a.out.nodebug.wasm.gz": 55308,
"total": 166952,
"total_gz": 63212,
"sent": [
"_abort_js",
"_tzset_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except_wasm_legacy.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19100,
"a.out.js.gz": 7929,
"a.out.nodebug.wasm": 145729,
"a.out.nodebug.wasm.gz": 54945,
"total": 164829,
"total_gz": 62874,
"a.out.nodebug.wasm": 145732,
"a.out.nodebug.wasm.gz": 54936,
"total": 164832,
"total_gz": 62865,
"sent": [
"_abort_js",
"_tzset_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_lto.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 18563,
"a.out.js.gz": 7666,
"a.out.nodebug.wasm": 102162,
"a.out.nodebug.wasm.gz": 39560,
"total": 120725,
"total_gz": 47226,
"a.out.nodebug.wasm": 102164,
"a.out.nodebug.wasm.gz": 39553,
"total": 120727,
"total_gz": 47219,
"sent": [
"a (emscripten_resize_heap)",
"b (_setitimer_js)",
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_cxx_mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 23224,
"a.out.js.gz": 8983,
"a.out.nodebug.wasm": 238957,
"a.out.nodebug.wasm.gz": 79842,
"a.out.nodebug.wasm.gz": 79820,
"total": 262181,
"total_gz": 88825,
"total_gz": 88803,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_noexcept.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19194,
"a.out.js.gz": 7969,
"a.out.nodebug.wasm": 134661,
"a.out.nodebug.wasm.gz": 50777,
"total": 153855,
"total_gz": 58746,
"a.out.nodebug.wasm": 134657,
"a.out.nodebug.wasm.gz": 50774,
"total": 153851,
"total_gz": 58743,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 7023,
"a.out.js.gz": 3310,
"a.out.nodebug.wasm": 172714,
"a.out.nodebug.wasm.gz": 63316,
"total": 179737,
"total_gz": 66626,
"a.out.nodebug.wasm": 172710,
"a.out.nodebug.wasm.gz": 63317,
"total": 179733,
"total_gz": 66627,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_file_preload.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 22141,
"a.out.js.gz": 9184,
"a.out.nodebug.wasm": 1648,
"a.out.nodebug.wasm.gz": 939,
"a.out.nodebug.wasm.gz": 938,
"total": 23789,
"total_gz": 10123,
"total_gz": 10122,
"sent": [
"a (fd_write)"
],
Expand Down
6 changes: 3 additions & 3 deletions test/codesize/test_codesize_hello_O0.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 24221,
"a.out.js.gz": 8713,
"a.out.js.gz": 8717,
"a.out.nodebug.wasm": 14850,
"a.out.nodebug.wasm.gz": 7311,
"a.out.nodebug.wasm.gz": 7314,
"total": 39071,
"total_gz": 16024,
"total_gz": 16031,
"sent": [
"fd_write"
],
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_O1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 6345,
"a.out.js.gz": 2456,
"a.out.nodebug.wasm": 2530,
"a.out.nodebug.wasm.gz": 1423,
"a.out.nodebug.wasm.gz": 1421,
"total": 8875,
"total_gz": 3879,
"total_gz": 3877,
"sent": [
"fd_write"
],
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_O2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 4323,
"a.out.js.gz": 2130,
"a.out.nodebug.wasm": 1898,
"a.out.nodebug.wasm.gz": 1122,
"a.out.nodebug.wasm.gz": 1120,
"total": 6221,
"total_gz": 3252,
"total_gz": 3250,
"sent": [
"fd_write"
],
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_O3.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 4265,
"a.out.js.gz": 2089,
"a.out.nodebug.wasm": 1648,
"a.out.nodebug.wasm.gz": 939,
"a.out.nodebug.wasm.gz": 938,
"total": 5913,
"total_gz": 3028,
"total_gz": 3027,
"sent": [
"a (fd_write)"
],
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_Os.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 4265,
"a.out.js.gz": 2089,
"a.out.nodebug.wasm": 1638,
"a.out.nodebug.wasm.gz": 942,
"a.out.nodebug.wasm.gz": 941,
"total": 5903,
"total_gz": 3031,
"total_gz": 3030,
"sent": [
"a (fd_write)"
],
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_Oz.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 3900,
"a.out.js.gz": 1896,
"a.out.nodebug.wasm": 1172,
"a.out.nodebug.wasm.gz": 724,
"a.out.nodebug.wasm.gz": 723,
"total": 5072,
"total_gz": 2620,
"total_gz": 2619,
"sent": [
"a (fd_write)"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_dylink.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 26185,
"a.out.js.gz": 11171,
"a.out.nodebug.wasm": 17668,
"a.out.nodebug.wasm.gz": 8921,
"total": 43853,
"total_gz": 20092,
"a.out.nodebug.wasm": 17671,
"a.out.nodebug.wasm.gz": 8923,
"total": 43856,
"total_gz": 20094,
"sent": [
"__syscall_stat64",
"emscripten_resize_heap",
Expand Down
6 changes: 4 additions & 2 deletions test/codesize/test_codesize_hello_dylink_all.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"a.out.js": 244343,
"a.out.nodebug.wasm": 577432,
"total": 821775,
"a.out.nodebug.wasm": 577473,
"total": 821816,
"sent": [
"IMG_Init",
"IMG_Load",
Expand Down Expand Up @@ -2879,6 +2879,7 @@
"pthread_join",
"pthread_key_create",
"pthread_key_delete",
"pthread_kill",
"pthread_mutex_consistent",
"pthread_mutex_destroy",
"pthread_mutex_init",
Expand Down Expand Up @@ -4621,6 +4622,7 @@
"$pthread_getattr_np",
"$pthread_getcpuclockid",
"$pthread_getspecific",
"$pthread_kill",
"$pthread_mutexattr_getprotocol",
"$pthread_mutexattr_getpshared",
"$pthread_mutexattr_getrobust",
Expand Down
2 changes: 1 addition & 1 deletion test/codesize/test_codesize_hello_single_file.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.out.js": 5223,
"a.out.js.gz": 2886,
"a.out.js.gz": 2887,
"sent": [
"a (fd_write)"
]
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_wasmfs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 4265,
"a.out.js.gz": 2089,
"a.out.nodebug.wasm": 1648,
"a.out.nodebug.wasm.gz": 939,
"a.out.nodebug.wasm.gz": 938,
"total": 5913,
"total_gz": 3028,
"total_gz": 3027,
"sent": [
"a (fd_write)"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"a.js": 7262,
"a.js.gz": 3324,
"a.wasm": 7099,
"a.wasm.gz": 3257,
"a.wasm.gz": 3246,
"total": 14909,
"total_gz": 6952
"total_gz": 6941
}
8 changes: 4 additions & 4 deletions test/codesize/test_unoptimized_code_size.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"hello_world.js": 56998,
"hello_world.js.gz": 17743,
"hello_world.js.gz": 17746,
"hello_world.wasm": 14850,
"hello_world.wasm.gz": 7311,
"hello_world.wasm.gz": 7314,
"no_asserts.js": 26622,
"no_asserts.js.gz": 8888,
"no_asserts.wasm": 12010,
"no_asserts.wasm.gz": 5880,
"strict.js": 54816,
"strict.js.gz": 17052,
"strict.wasm": 14850,
"strict.wasm.gz": 7311,
"strict.wasm.gz": 7310,
"total": 180146,
"total_gz": 64185
"total_gz": 64190
}
Loading
Loading