#include "EXTERN.h" #include "perl.h" #include "XSUB.h" typedef volatile sig_atomic_t atomic_t; static int *sig_pending, *psig_pend; /* make local copies because of missing THX */ static Sighandler_t old_sighandler; static atomic_t async_pending; static int extract_fd (SV *fh, int wr) { int fd = PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); if (fd < 0) croak ("illegal fh argument, either not an OS file or read/write mode mismatch"); return fd; } static SV * get_cb (SV *cb_sv) { HV *st; GV *gvp; CV *cv; if (!SvOK (cb_sv)) return 0; cv = sv_2cv (cb_sv, &st, &gvp, 0); if (!cv) croak ("Async::Interrupt callback must be undef or a CODE reference"); return (SV *)cv; } static AV *asyncs; struct async { SV *cb; void (*c_cb)(pTHX_ void *c_data, int value); void *c_data; SV *fh; int fd; atomic_t value; atomic_t signalled; }; /* the main workhorse to signal */ static void async_signal (void *signal_arg, int value) { struct async *async = (struct async *)signal_arg; async->value = value; if (!async->signalled) { async->signalled = 1; if (async->fd >= 0) write (async->fd, async, 1); } async_pending = 1; psig_pend [9] = 1; *sig_pending = 1; } static void async_handle (void) { async_pending = 0; } #if defined(HAS_SIGACTION) && defined(SA_SIGINFO) static Signal_t async_sighandler (int signum, siginfo_t *si, void *sarg) { if (signum == 9) async_handle (); else old_sighandler (signum, si, sarg); } #else static Signal_t async_sighandler (int) { if (signum == 9) async_handle (); else old_sighandler (signum); } #endif MODULE = Async::Interrupt PACKAGE = Async::Interrupt BOOT: old_sighandler = PL_sighandlerp; PL_sighandlerp = async_sighandler; sig_pending = &PL_sig_pending; psig_pend = PL_psig_pend; asyncs = newAV (); SV * _alloc (SV *cb, void *c_cb, void *c_data, SV *fh) CODE: { SV *cv = SvOK (cb) ? SvREFCNT_inc_NN (get_cb (cb)) : 0; int fd = SvOK (fh) ? extract_fd (fh, 1) : -1; struct async *async; Newz (0, async, 1, struct async); async->fh = fd >= 0 ? newSVsv (fh) : 0; async->fd = fd; async->cb = cb; async->c_cb = c_cb; async->c_data = c_data; RETVAL = newSViv (PTR2IV (async)); av_push (asyncs, RETVAL); RETVAL = newRV_noinc (RETVAL); } OUTPUT: RETVAL void signal_cb (SV *self) PPCODE: EXTEND (SP, 2); PUSHs (sv_2mortal (newSViv (PTR2IV (async_signal)))); PUSHs (sv_2mortal (newSViv (SvIV (SvRV (self))))); void signal (SV *self, int value = 0) CODE: async_signal (INT2PTR (void *, SvIV (SvRV (self))), value); void DESTROY (SV *self) CODE: { int i; SV *async_sv = SvRV (self); struct async *async = INT2PTR (struct async *, SvIV (async_sv)); for (i = AvFILLp (asyncs); i > 0; --i) if (AvARRAY (asyncs)[i] == async_sv) { if (i < AvFILLp (asyncs)) AvARRAY (asyncs)[i] == AvARRAY (asyncs)[AvFILLp (asyncs)]; assert (av_pop (asyncs) == async_sv); goto found; } if (!PL_dirty) warn ("Async::Interrupt::DESTROY could not find async object in list of asyncs, please report!"); found: SvREFCNT_dec (async->fh); SvREFCNT_dec (async->cb); Safefree (async); }