Skip to content

Commit 085b378

Browse files
authored
Move most of pthread_kill to native code. NFC (#17041)
1 parent af7a8f7 commit 085b378

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

src/library_pthread.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -795,27 +795,12 @@ var LibraryPThread = {
795795
#endif
796796
},
797797

798-
pthread_kill__deps: ['emscripten_main_browser_thread_id'],
799-
pthread_kill: function(thread, signal) {
800-
if (signal < 0 || signal >= 65/*_NSIG*/) return {{{ cDefine('EINVAL') }}};
801-
if (thread === _emscripten_main_browser_thread_id()) {
802-
if (signal == 0) return 0; // signal == 0 is a no-op.
803-
err('Main thread (id=' + ptrToString(thread) + ') cannot be killed with pthread_kill!');
804-
return {{{ cDefine('ESRCH') }}};
805-
}
806-
if (!thread) {
807-
err('pthread_kill attempted on a null thread pointer!');
808-
return {{{ cDefine('ESRCH') }}};
809-
}
810-
var self = {{{ makeGetValue('thread', C_STRUCTS.pthread.self, 'i32') }}};
811-
if (self !== thread) {
812-
err('pthread_kill attempted on thread ' + ptrToString(thread) + ', which does not point to a valid thread, or does not exist anymore!');
813-
return {{{ cDefine('ESRCH') }}};
814-
}
798+
__pthread_kill_js__deps: ['emscripten_main_browser_thread_id'],
799+
__pthread_kill_js: function(thread, signal) {
815800
if (signal === {{{ cDefine('SIGCANCEL') }}}) { // Used by pthread_cancel in musl
816801
if (!ENVIRONMENT_IS_PTHREAD) cancelThread(thread);
817802
else postMessage({ 'cmd': 'cancelThread', 'thread': thread });
818-
} else if (signal != 0) {
803+
} else {
819804
if (!ENVIRONMENT_IS_PTHREAD) killThread(thread);
820805
else postMessage({ 'cmd': 'killThread', 'thread': thread });
821806
}

system/lib/pthread/pthread_kill.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2022 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <emscripten/threading.h>
9+
10+
#include "pthread_impl.h"
11+
#include "lock.h"
12+
13+
int __pthread_kill_js(pthread_t t, int sig);
14+
15+
int pthread_kill(pthread_t t, int sig) {
16+
if (sig < 0 || sig >= _NSIG) {
17+
return EINVAL;
18+
}
19+
if (t == emscripten_main_browser_thread_id()) {
20+
if (sig == 0) return 0; // signal == 0 is a no-op.
21+
return ESRCH;
22+
}
23+
if (!t || !_emscripten_thread_is_valid(t)) {
24+
return ESRCH;
25+
}
26+
if (sig == 0) return 0; // signal == 0 is a no-op.
27+
return __pthread_kill_js(t, sig);
28+
}

tools/system_libs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ def get_files(self):
861861
'library_pthread.c',
862862
'proxying.c',
863863
'pthread_create.c',
864+
'pthread_kill.c',
864865
'emscripten_proxy_main.c',
865866
'emscripten_thread_init.c',
866867
'emscripten_thread_state.S',

0 commit comments

Comments
 (0)