Path: blob/main/system/lib/pthread/pthread_kill.c
6171 views
/*1* Copyright 2022 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#include <threading_internal.h>8#include <emscripten/proxying.h>9#include <emscripten/console.h>10#include <emscripten_internal.h>1112#include "pthread_impl.h"13#include "lock.h"1415void do_raise(void* arg) {16int sig = (intptr_t)arg;17if (sig == SIGCANCEL) {18// For `SIGCANCEL` there is no need to actually call raise to run the19// handler function. The calling thread (the one calling `pthread_cancel`)20// will already have marked us as being cancelled. All we need to do is21// ensure that `pthread_testcancel` is eventually called and that will cause22// this thread to exit. We can't call `pthread_testcancel` here (since we23// are being called from the proxy queue process and we don't want to leave24// that in a bad state by unwinding). Instead, we rely on25// `pthread_testcancel` at the end of `_emscripten_check_mailbox`. Before26// we return, we do want to make sure we clear the keepalive state so that27// the thread will exit even if it has a reason to stay alive. TODO(sbc):28// Is this the correct behaviour, should `pthread_cancel` instead wait for29// threads to be done with outstanding work/event loops?30_emscripten_runtime_keepalive_clear();31return;32}33raise((intptr_t)sig);34}3536int pthread_kill(pthread_t t, int sig) {37if (sig < 0 || sig >= _NSIG) {38return EINVAL;39}40if (t == emscripten_main_runtime_thread_id()) {41if (sig == 0) return 0; // signal == 0 is a no-op.42return ESRCH;43}44if (!t || !_emscripten_thread_is_valid(t)) {45return ESRCH;46}47if (sig == 0) return 0; // signal == 0 is a no-op.4849// The job of pthread_kill is basically to run the (process-wide) signal50// handler on the target thread.51emscripten_proxy_async(emscripten_proxy_get_system_queue(), t, do_raise, (void*)(intptr_t)sig);52return 0;53}545556