Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Sep 3, 2024
1 parent 2972f3f commit 6827aee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
10 changes: 5 additions & 5 deletions system/lib/pthread/pthread_kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ void do_raise(void* arg) {
int sig = (intptr_t)arg;
if (sig == SIGCANCEL) {
// For `SIGCANCEL` there is no need to actually call raise to run the
// handler function. The calling then (the one calling `pthread_cancel`)
// handler function. The calling thread (the one calling `pthread_cancel`)
// will already have marked us as being cancelled. All we need to do is
// ensure that `pthread_testcancel` is eventually called and that will cause
// this thread to exit. We can't call `pthread_testcancel` here (since we
// are being called from the proxy queue process and we don't want to leave
// that in a bad state by unwinding). Instead, we rely on
// `pthread_testcancel` at the end of `_emscripten_check_mailbox`. Before
// we return though we do want to make sure we clear the keepalive state so
// that the thread will exit even if it has a reason to stay alive.
// TODO(sbc): Is this the correct behaviour, should `pthread_cancel`
// instead wait for threads to be done with outstanding work/event loops?
// we return, we do want to make sure we clear the keepalive state so that
// the thread will exit even if it has a reason to stay alive. TODO(sbc):
// Is this the correct behaviour, should `pthread_cancel` instead wait for
// threads to be done with outstanding work/event loops?
_emscripten_runtime_keepalive_clear();
return;
}
Expand Down
20 changes: 11 additions & 9 deletions test/pthread/test_pthread_kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
#include <errno.h>
#include <signal.h>

_Atomic int sharedVar = 0;
_Atomic int gotTermSignal = 0;
pthread_cond_t started_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t started_lock = PTHREAD_MUTEX_INITIALIZER;
_Atomic int got_term_signal = 0;

pthread_t thr;

void signal_handler(int sig, siginfo_t * info, void * arg) {
printf("signal: %d onthread=%d\n", sig, pthread_self() == thr);
if (sig == SIGTERM) {
gotTermSignal = 1;
got_term_signal = 1;
}
}

Expand All @@ -38,10 +39,12 @@ void sleepms(long msecs) {
}

void *thread_start(void *arg) {
pthread_mutex_lock(&started_lock);
pthread_cond_signal(&started_cond);
pthread_mutex_unlock(&started_lock);
// As long as this thread is running, keep the shared variable latched to nonzero value.
while (!gotTermSignal) {
while (!got_term_signal) {
sleepms(1);
++sharedVar;
}
printf("got term signal, shutting down thread\n");
pthread_exit(0);
Expand All @@ -50,14 +53,13 @@ void *thread_start(void *arg) {
int main() {
setup_handler();

sharedVar = 0;
int s = pthread_create(&thr, NULL, thread_start, 0);
assert(s == 0);

// Wait until thread kicks in and sets the shared variable.
while (sharedVar == 0) {
sleepms(10);
}
pthread_mutex_lock(&started_lock);
pthread_cond_wait(&started_cond, &started_lock);
pthread_mutex_unlock(&started_lock);
printf("thread has started, sending SIGTERM\n");

s = pthread_kill(thr, SIGTERM);
Expand Down

0 comments on commit 6827aee

Please sign in to comment.