1 |
root |
1.1 |
#include "EXTERN.h" |
2 |
|
|
#include "perl.h" |
3 |
|
|
#include "XSUB.h" |
4 |
|
|
|
5 |
root |
1.8 |
#include "schmorp.h" |
6 |
|
|
|
7 |
root |
1.1 |
typedef volatile sig_atomic_t atomic_t; |
8 |
|
|
|
9 |
|
|
static int *sig_pending, *psig_pend; /* make local copies because of missing THX */ |
10 |
|
|
static Sighandler_t old_sighandler; |
11 |
|
|
static atomic_t async_pending; |
12 |
|
|
|
13 |
root |
1.5 |
#define PERL_VERSION_ATLEAST(a,b,c) \ |
14 |
|
|
(PERL_REVISION > (a) \ |
15 |
|
|
|| (PERL_REVISION == (a) \ |
16 |
|
|
&& (PERL_VERSION > (b) \ |
17 |
|
|
|| (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
18 |
|
|
|
19 |
|
|
#if defined(HAS_SIGACTION) && defined(SA_SIGINFO) |
20 |
|
|
# define HAS_SA_SIGINFO 1 |
21 |
|
|
#endif |
22 |
|
|
|
23 |
|
|
#if !PERL_VERSION_ATLEAST(5,10,0) |
24 |
|
|
# undef HAS_SA_SIGINFO |
25 |
|
|
#endif |
26 |
|
|
|
27 |
root |
1.6 |
/*****************************************************************************/ |
28 |
root |
1.1 |
|
29 |
root |
1.6 |
typedef struct { |
30 |
root |
1.1 |
SV *cb; |
31 |
root |
1.2 |
void (*c_cb)(pTHX_ void *c_arg, int value); |
32 |
|
|
void *c_arg; |
33 |
|
|
SV *fh_r, *fh_w; |
34 |
root |
1.9 |
SV *value; |
35 |
root |
1.6 |
int signum; |
36 |
root |
1.7 |
volatile int blocked; |
37 |
root |
1.1 |
|
38 |
root |
1.9 |
s_epipe ep; |
39 |
root |
1.6 |
int fd_wlen; |
40 |
|
|
atomic_t fd_enable; |
41 |
root |
1.2 |
atomic_t pending; |
42 |
root |
1.9 |
volatile IV *valuep; |
43 |
root |
1.6 |
} async_t; |
44 |
|
|
|
45 |
|
|
static AV *asyncs; |
46 |
|
|
static async_t *sig_async [SIG_SIZE]; |
47 |
|
|
|
48 |
|
|
#define SvASYNC_nrv(sv) INT2PTR (async_t *, SvIVX (sv)) |
49 |
|
|
#define SvASYNC(rv) SvASYNC_nrv (SvRV (rv)) |
50 |
root |
1.1 |
|
51 |
|
|
/* the main workhorse to signal */ |
52 |
|
|
static void |
53 |
|
|
async_signal (void *signal_arg, int value) |
54 |
|
|
{ |
55 |
root |
1.6 |
static char pipedata [8]; |
56 |
|
|
|
57 |
|
|
async_t *async = (async_t *)signal_arg; |
58 |
root |
1.2 |
int pending = async->pending; |
59 |
root |
1.1 |
|
60 |
root |
1.9 |
*async->valuep = value ? value : 1; |
61 |
root |
1.2 |
async->pending = 1; |
62 |
|
|
async_pending = 1; |
63 |
|
|
psig_pend [9] = 1; |
64 |
|
|
*sig_pending = 1; |
65 |
root |
1.1 |
|
66 |
root |
1.10 |
if (!pending && async->fd_enable && async->ep.len) |
67 |
|
|
s_epipe_signal (&async->ep); |
68 |
root |
1.2 |
} |
69 |
|
|
|
70 |
|
|
static void |
71 |
root |
1.6 |
handle_async (async_t *async) |
72 |
root |
1.2 |
{ |
73 |
|
|
int old_errno = errno; |
74 |
root |
1.9 |
int value = *async->valuep; |
75 |
root |
1.2 |
|
76 |
root |
1.9 |
*async->valuep = 0; |
77 |
root |
1.2 |
async->pending = 0; |
78 |
|
|
|
79 |
|
|
/* drain pipe */ |
80 |
root |
1.10 |
if (async->fd_enable && async->ep.len) |
81 |
root |
1.9 |
s_epipe_drain (&async->ep); |
82 |
root |
1.2 |
|
83 |
|
|
if (async->c_cb) |
84 |
|
|
{ |
85 |
|
|
dTHX; |
86 |
|
|
async->c_cb (aTHX_ async->c_arg, value); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
if (async->cb) |
90 |
|
|
{ |
91 |
|
|
dSP; |
92 |
|
|
|
93 |
|
|
SV *saveerr = SvOK (ERRSV) ? sv_mortalcopy (ERRSV) : 0; |
94 |
|
|
SV *savedie = PL_diehook; |
95 |
|
|
|
96 |
|
|
PL_diehook = 0; |
97 |
|
|
|
98 |
|
|
PUSHSTACKi (PERLSI_SIGNAL); |
99 |
|
|
|
100 |
|
|
PUSHMARK (SP); |
101 |
|
|
XPUSHs (sv_2mortal (newSViv (value))); |
102 |
|
|
PUTBACK; |
103 |
|
|
call_sv (async->cb, G_VOID | G_DISCARD | G_EVAL); |
104 |
|
|
|
105 |
|
|
if (SvTRUE (ERRSV)) |
106 |
|
|
{ |
107 |
|
|
SPAGAIN; |
108 |
|
|
|
109 |
|
|
PUSHMARK (SP); |
110 |
|
|
PUTBACK; |
111 |
|
|
call_sv (get_sv ("Async::Interrupt::DIED", 1), G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
112 |
|
|
|
113 |
|
|
sv_setpvn (ERRSV, "", 0); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
if (saveerr) |
117 |
|
|
sv_setsv (ERRSV, saveerr); |
118 |
root |
1.1 |
|
119 |
root |
1.2 |
{ |
120 |
|
|
SV *oldhook = PL_diehook; |
121 |
|
|
PL_diehook = savedie; |
122 |
|
|
SvREFCNT_dec (oldhook); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
POPSTACK; |
126 |
root |
1.1 |
} |
127 |
|
|
|
128 |
root |
1.2 |
errno = old_errno; |
129 |
root |
1.1 |
} |
130 |
|
|
|
131 |
|
|
static void |
132 |
root |
1.2 |
handle_asyncs (void) |
133 |
root |
1.1 |
{ |
134 |
root |
1.2 |
int i; |
135 |
|
|
|
136 |
root |
1.1 |
async_pending = 0; |
137 |
root |
1.2 |
|
138 |
|
|
for (i = AvFILLp (asyncs); i >= 0; --i) |
139 |
|
|
{ |
140 |
root |
1.6 |
async_t *async = SvASYNC_nrv (AvARRAY (asyncs)[i]); |
141 |
root |
1.2 |
|
142 |
|
|
if (async->pending && !async->blocked) |
143 |
|
|
handle_async (async); |
144 |
|
|
} |
145 |
root |
1.1 |
} |
146 |
|
|
|
147 |
root |
1.5 |
#if HAS_SA_SIGINFO |
148 |
root |
1.1 |
static Signal_t async_sighandler (int signum, siginfo_t *si, void *sarg) |
149 |
|
|
{ |
150 |
|
|
if (signum == 9) |
151 |
root |
1.2 |
handle_asyncs (); |
152 |
root |
1.1 |
else |
153 |
|
|
old_sighandler (signum, si, sarg); |
154 |
|
|
} |
155 |
|
|
#else |
156 |
root |
1.3 |
static Signal_t async_sighandler (int signum) |
157 |
root |
1.1 |
{ |
158 |
|
|
if (signum == 9) |
159 |
root |
1.3 |
handle_asyncs (); |
160 |
root |
1.1 |
else |
161 |
|
|
old_sighandler (signum); |
162 |
|
|
} |
163 |
|
|
#endif |
164 |
|
|
|
165 |
root |
1.4 |
static void |
166 |
root |
1.6 |
async_sigsend (int signum) |
167 |
|
|
{ |
168 |
|
|
async_signal (sig_async [signum], 0); |
169 |
|
|
} |
170 |
|
|
|
171 |
root |
1.7 |
#define block(async) ++(async)->blocked |
172 |
|
|
|
173 |
root |
1.6 |
static void |
174 |
root |
1.7 |
unblock (async_t *async) |
175 |
root |
1.4 |
{ |
176 |
|
|
--async->blocked; |
177 |
|
|
if (async->pending && !async->blocked) |
178 |
|
|
handle_async (async); |
179 |
root |
1.7 |
} |
180 |
root |
1.4 |
|
181 |
root |
1.7 |
static void |
182 |
|
|
scope_block_cb (pTHX_ void *async_sv) |
183 |
|
|
{ |
184 |
|
|
async_t *async = SvASYNC_nrv ((SV *)async_sv); |
185 |
|
|
unblock (async); |
186 |
root |
1.4 |
SvREFCNT_dec (async_sv); |
187 |
|
|
} |
188 |
root |
1.1 |
|
189 |
|
|
MODULE = Async::Interrupt PACKAGE = Async::Interrupt |
190 |
|
|
|
191 |
|
|
BOOT: |
192 |
|
|
old_sighandler = PL_sighandlerp; |
193 |
|
|
PL_sighandlerp = async_sighandler; |
194 |
|
|
sig_pending = &PL_sig_pending; |
195 |
|
|
psig_pend = PL_psig_pend; |
196 |
|
|
asyncs = newAV (); |
197 |
root |
1.4 |
CvNODEBUG_on (get_cv ("Async::Interrupt::scope_block", 0)); /* otherwise calling scope can be the debugger */ |
198 |
root |
1.1 |
|
199 |
root |
1.3 |
PROTOTYPES: DISABLE |
200 |
|
|
|
201 |
root |
1.6 |
void |
202 |
root |
1.9 |
_alloc (SV *cb, void *c_cb, void *c_arg, SV *fh_r, SV *fh_w, SV *signl, SV *pvalue) |
203 |
root |
1.6 |
PPCODE: |
204 |
root |
1.1 |
{ |
205 |
root |
1.8 |
SV *cv = SvOK (cb) ? SvREFCNT_inc (s_get_cv_croak (cb)) : 0; |
206 |
root |
1.6 |
async_t *async; |
207 |
root |
1.1 |
|
208 |
root |
1.6 |
Newz (0, async, 1, async_t); |
209 |
root |
1.1 |
|
210 |
root |
1.6 |
XPUSHs (sv_2mortal (newSViv (PTR2IV (async)))); |
211 |
root |
1.9 |
/* TODO: need to bless right now to ensure deallocation */ |
212 |
root |
1.6 |
av_push (asyncs, TOPs); |
213 |
root |
1.2 |
|
214 |
root |
1.9 |
SvGETMAGIC (fh_r); SvGETMAGIC (fh_w); |
215 |
|
|
if (SvOK (fh_r) || SvOK (fh_w)) |
216 |
|
|
{ |
217 |
|
|
int fd_r = s_fileno_croak (fh_r, 0); |
218 |
|
|
int fd_w = s_fileno_croak (fh_w, 1); |
219 |
|
|
|
220 |
|
|
async->fh_r = newSVsv (fh_r); |
221 |
|
|
async->fh_w = newSVsv (fh_w); |
222 |
|
|
async->ep.fd [0] = fd_r; |
223 |
|
|
async->ep.fd [1] = fd_w; |
224 |
|
|
async->ep.len = 1; |
225 |
|
|
async->fd_enable = 1; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
async->value = SvROK (pvalue) |
229 |
|
|
? SvREFCNT_inc_NN (SvRV (pvalue)) |
230 |
|
|
: NEWSV (0, 0); |
231 |
|
|
|
232 |
|
|
sv_setiv (async->value, 0); |
233 |
|
|
SvIOK_only (async->value); /* just to be sure */ |
234 |
|
|
SvREADONLY_on (async->value); |
235 |
|
|
|
236 |
|
|
async->valuep = &(SvIVX (async->value)); |
237 |
|
|
|
238 |
root |
1.6 |
async->cb = cv; |
239 |
|
|
async->c_cb = c_cb; |
240 |
|
|
async->c_arg = c_arg; |
241 |
root |
1.8 |
async->signum = SvOK (signl) ? s_signum_croak (signl) : 0; |
242 |
root |
1.6 |
|
243 |
|
|
if (async->signum) |
244 |
|
|
{ |
245 |
|
|
if (async->signum < 0) |
246 |
|
|
croak ("Async::Interrupt::new got passed illegal signal name or number: %s", SvPV_nolen (signl)); |
247 |
|
|
|
248 |
|
|
sig_async [async->signum] = async; |
249 |
|
|
#if _WIN32 |
250 |
|
|
signal (async->signum, async_sigsend); |
251 |
|
|
#else |
252 |
|
|
{ |
253 |
|
|
struct sigaction sa = { }; |
254 |
|
|
sa.sa_handler = async_sigsend; |
255 |
|
|
sigfillset (&sa.sa_mask); |
256 |
|
|
sigaction (async->signum, &sa, 0); |
257 |
|
|
} |
258 |
|
|
#endif |
259 |
|
|
} |
260 |
root |
1.1 |
} |
261 |
|
|
|
262 |
|
|
void |
263 |
root |
1.6 |
signal_func (async_t *async) |
264 |
root |
1.1 |
PPCODE: |
265 |
|
|
EXTEND (SP, 2); |
266 |
|
|
PUSHs (sv_2mortal (newSViv (PTR2IV (async_signal)))); |
267 |
root |
1.6 |
PUSHs (sv_2mortal (newSViv (PTR2IV (async)))); |
268 |
root |
1.1 |
|
269 |
root |
1.9 |
IV |
270 |
|
|
c_var (async_t *async) |
271 |
|
|
CODE: |
272 |
|
|
RETVAL = PTR2IV (async->valuep); |
273 |
|
|
OUTPUT: |
274 |
|
|
RETVAL |
275 |
|
|
|
276 |
root |
1.1 |
void |
277 |
root |
1.9 |
signal (async_t *async, int value = 1) |
278 |
root |
1.1 |
CODE: |
279 |
root |
1.6 |
async_signal (async, value); |
280 |
root |
1.2 |
|
281 |
|
|
void |
282 |
root |
1.6 |
block (async_t *async) |
283 |
root |
1.2 |
CODE: |
284 |
root |
1.7 |
block (async); |
285 |
root |
1.2 |
|
286 |
|
|
void |
287 |
root |
1.6 |
unblock (async_t *async) |
288 |
root |
1.2 |
CODE: |
289 |
root |
1.7 |
unblock (async); |
290 |
root |
1.1 |
|
291 |
|
|
void |
292 |
root |
1.4 |
scope_block (SV *self) |
293 |
|
|
CODE: |
294 |
|
|
{ |
295 |
|
|
SV *async_sv = SvRV (self); |
296 |
root |
1.6 |
async_t *async = SvASYNC_nrv (async_sv); |
297 |
root |
1.7 |
block (async); |
298 |
root |
1.4 |
|
299 |
|
|
LEAVE; /* unfortunately, perl sandwiches XS calls into ENTER/LEAVE */ |
300 |
|
|
SAVEDESTRUCTOR_X (scope_block_cb, (void *)SvREFCNT_inc (async_sv)); |
301 |
|
|
ENTER; /* unfortunately, perl sandwiches XS calls into ENTER/LEAVE */ |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
void |
305 |
root |
1.6 |
pipe_enable (async_t *async) |
306 |
|
|
ALIAS: |
307 |
|
|
pipe_enable = 1 |
308 |
|
|
pipe_disable = 0 |
309 |
|
|
CODE: |
310 |
|
|
async->fd_enable = ix; |
311 |
|
|
|
312 |
root |
1.9 |
int |
313 |
|
|
pipe_fileno (async_t *async) |
314 |
|
|
CODE: |
315 |
|
|
if (!async->ep.len) |
316 |
|
|
{ |
317 |
|
|
int res; |
318 |
|
|
|
319 |
|
|
/*block (async);*//*TODO*/ |
320 |
|
|
res = s_epipe_new (&async->ep); |
321 |
|
|
async->fd_enable = 1; |
322 |
|
|
/*unblock (async);*//*TODO*/ |
323 |
|
|
|
324 |
|
|
if (res < 0) |
325 |
|
|
croak ("Async::Interrupt: unable to initialize event pipe"); |
326 |
|
|
} |
327 |
|
|
|
328 |
|
|
RETVAL = async->ep.fd [0]; |
329 |
|
|
OUTPUT: |
330 |
|
|
RETVAL |
331 |
|
|
|
332 |
|
|
|
333 |
|
|
void |
334 |
|
|
post_fork (async_t *async) |
335 |
|
|
CODE: |
336 |
|
|
if (async->ep.len) |
337 |
|
|
{ |
338 |
|
|
int res; |
339 |
|
|
|
340 |
|
|
/*block (async);*//*TODO*/ |
341 |
|
|
res = s_epipe_renew (&async->ep); |
342 |
|
|
/*unblock (async);*//*TODO*/ |
343 |
|
|
|
344 |
|
|
if (res < 0) |
345 |
|
|
croak ("Async::Interrupt: unable to initialize event pipe after fork"); |
346 |
|
|
} |
347 |
|
|
|
348 |
root |
1.6 |
void |
349 |
root |
1.1 |
DESTROY (SV *self) |
350 |
|
|
CODE: |
351 |
|
|
{ |
352 |
|
|
int i; |
353 |
|
|
SV *async_sv = SvRV (self); |
354 |
root |
1.6 |
async_t *async = SvASYNC_nrv (async_sv); |
355 |
root |
1.1 |
|
356 |
root |
1.2 |
for (i = AvFILLp (asyncs); i >= 0; --i) |
357 |
root |
1.1 |
if (AvARRAY (asyncs)[i] == async_sv) |
358 |
|
|
{ |
359 |
|
|
if (i < AvFILLp (asyncs)) |
360 |
root |
1.3 |
AvARRAY (asyncs)[i] = AvARRAY (asyncs)[AvFILLp (asyncs)]; |
361 |
root |
1.1 |
|
362 |
|
|
assert (av_pop (asyncs) == async_sv); |
363 |
|
|
goto found; |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
if (!PL_dirty) |
367 |
root |
1.2 |
warn ("Async::Interrupt::DESTROY could not find async object in list of asyncs, please report"); |
368 |
root |
1.1 |
|
369 |
|
|
found: |
370 |
root |
1.6 |
|
371 |
|
|
if (async->signum) |
372 |
|
|
{ |
373 |
|
|
#if _WIN32 |
374 |
|
|
signal (async->signum, SIG_DFL); |
375 |
|
|
#else |
376 |
|
|
{ |
377 |
|
|
struct sigaction sa = { }; |
378 |
|
|
sa.sa_handler = SIG_DFL; |
379 |
|
|
sigaction (async->signum, &sa, 0); |
380 |
|
|
} |
381 |
|
|
#endif |
382 |
|
|
} |
383 |
|
|
|
384 |
root |
1.9 |
if (!async->fh_r && async->ep.len) |
385 |
|
|
s_epipe_destroy (&async->ep); |
386 |
|
|
|
387 |
root |
1.2 |
SvREFCNT_dec (async->fh_r); |
388 |
|
|
SvREFCNT_dec (async->fh_w); |
389 |
root |
1.1 |
SvREFCNT_dec (async->cb); |
390 |
root |
1.9 |
SvREFCNT_dec (async->value); |
391 |
root |
1.1 |
|
392 |
|
|
Safefree (async); |
393 |
|
|
} |
394 |
|
|
|