From: Malcolm Beattie Date: Fri, 3 Oct 1997 11:53:51 +0000 (+0000) Subject: Reliable thread signal handling. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f152979c2653a5d3da400a6170d2b978b5612e11;p=p5sagit%2Fp5-mst-13.2.git Reliable thread signal handling. p4raw-id: //depot/perlext/Thread@102 --- diff --git a/Thread.xs b/Thread.xs index 4344439..dd94fd3 100644 --- a/Thread.xs +++ b/Thread.xs @@ -158,6 +158,7 @@ char *class; Thread savethread; int i; SV *sv; + sigset_t fullmask, oldmask; savethread = thr; sv = newSVpv("", 0); @@ -203,16 +204,29 @@ char *class; MUTEX_LOCK(&nthreads_mutex); nthreads++; MUTEX_UNLOCK(&nthreads_mutex); + sigfillset(&fullmask); + if (sigprocmask(SIG_SETMASK, &fullmask, &oldmask) == -1) + croak("panic: sigprocmask"); if (pthread_create(&self, NULL, threadstart, (void*) thr)) return NULL; /* XXX should clean up first */ /* Go */ MUTEX_UNLOCK(threadstart_mutexp); + if (sigprocmask(SIG_SETMASK, &oldmask, 0)) + croak("panic: sigprocmask"); #endif sv = newSViv(++threadnum); sv_magic(sv, oursv, '~', 0, 0); return sv_bless(newRV(sv), gv_stashpv(class, TRUE)); } +static Signal_t +handle_thread_signal(sig) +int sig; +{ + char c = (char) sig; + write(sig_pipe[0], &c, 1); +} + MODULE = Thread PACKAGE = Thread void @@ -345,3 +359,36 @@ CODE: } COND_BROADCAST(MgCONDP(mg)); MUTEX_UNLOCK(MgMUTEXP(mg)); + +MODULE = Thread PACKAGE = Thread::Signal + +void +kill_sighandler_thread() + PPCODE: + write(sig_pipe[0], "\0", 1); + PUSHs(&sv_yes); + +void +init_thread_signals() + PPCODE: + sighandlerp = handle_thread_signal; + if (pipe(sig_pipe) == -1) + XSRETURN_UNDEF; + PUSHs(&sv_yes); + +SV * +await_signal() + PREINIT: + char c; + ssize_t ret; + CODE: + do { + ret = read(sig_pipe[1], &c, 1); + } while (ret == -1 && errno == EINTR); + if (ret == -1) + croak("panic: await_signal"); + if (ret == 0) + XSRETURN_UNDEF; + RETVAL = c ? psig_ptr[c] : &sv_no; + OUTPUT: + RETVAL