Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/pthread/pthread_kill.c
6171 views
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 <threading_internal.h>
9
#include <emscripten/proxying.h>
10
#include <emscripten/console.h>
11
#include <emscripten_internal.h>
12
13
#include "pthread_impl.h"
14
#include "lock.h"
15
16
void do_raise(void* arg) {
17
int sig = (intptr_t)arg;
18
if (sig == SIGCANCEL) {
19
// For `SIGCANCEL` there is no need to actually call raise to run the
20
// handler function. The calling thread (the one calling `pthread_cancel`)
21
// will already have marked us as being cancelled. All we need to do is
22
// ensure that `pthread_testcancel` is eventually called and that will cause
23
// this thread to exit. We can't call `pthread_testcancel` here (since we
24
// are being called from the proxy queue process and we don't want to leave
25
// that in a bad state by unwinding). Instead, we rely on
26
// `pthread_testcancel` at the end of `_emscripten_check_mailbox`. Before
27
// we return, we do want to make sure we clear the keepalive state so that
28
// the thread will exit even if it has a reason to stay alive. TODO(sbc):
29
// Is this the correct behaviour, should `pthread_cancel` instead wait for
30
// threads to be done with outstanding work/event loops?
31
_emscripten_runtime_keepalive_clear();
32
return;
33
}
34
raise((intptr_t)sig);
35
}
36
37
int pthread_kill(pthread_t t, int sig) {
38
if (sig < 0 || sig >= _NSIG) {
39
return EINVAL;
40
}
41
if (t == emscripten_main_runtime_thread_id()) {
42
if (sig == 0) return 0; // signal == 0 is a no-op.
43
return ESRCH;
44
}
45
if (!t || !_emscripten_thread_is_valid(t)) {
46
return ESRCH;
47
}
48
if (sig == 0) return 0; // signal == 0 is a no-op.
49
50
// The job of pthread_kill is basically to run the (process-wide) signal
51
// handler on the target thread.
52
emscripten_proxy_async(emscripten_proxy_get_system_queue(), t, do_raise, (void*)(intptr_t)sig);
53
return 0;
54
}
55
56