1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include <math.h> |
|
|
6 | #include <netinet/in.h> |
5 | /*#include <netinet/in.h>*/ |
7 | |
6 | |
8 | #define TIMEOUT_NONE HUGE_VAL |
7 | #define TIMEOUT_NONE HUGE_VAL |
|
|
8 | #define HAVE_EPOLL 1 |
9 | |
9 | |
10 | #define EV_COMMON \ |
10 | #define EV_PROTOTYPES 1 |
11 | SV *self; /* contains this struct */ \ |
|
|
12 | SV *cb_sv, *fh; |
|
|
13 | |
|
|
14 | #include "EV/EVAPI.h" |
11 | #include "EV/EVAPI.h" |
|
|
12 | |
|
|
13 | #include "libev/ev.c" |
15 | |
14 | |
16 | typedef int Signal; |
15 | typedef int Signal; |
17 | |
16 | |
18 | static struct EVAPI evapi; |
17 | static struct EVAPI evapi; |
19 | |
18 | |
… | |
… | |
23 | *stash_time, |
22 | *stash_time, |
24 | *stash_timer, |
23 | *stash_timer, |
25 | *stash_periodic, |
24 | *stash_periodic, |
26 | *stash_signal, |
25 | *stash_signal, |
27 | *stash_idle, |
26 | *stash_idle, |
|
|
27 | *stash_prepare, |
28 | *stash_check; |
28 | *stash_check, |
|
|
29 | *stash_child; |
29 | |
30 | |
30 | static int |
31 | static int |
31 | sv_signum (SV *sig) |
32 | sv_signum (SV *sig) |
32 | { |
33 | { |
33 | int signum; |
34 | int signum; |
… | |
… | |
38 | for (signum = 1; signum < SIG_SIZE; ++signum) |
39 | for (signum = 1; signum < SIG_SIZE; ++signum) |
39 | if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
40 | if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
40 | return signum; |
41 | return signum; |
41 | |
42 | |
42 | return -1; |
43 | return -1; |
43 | } |
|
|
44 | |
|
|
45 | static void |
|
|
46 | api_once (int fd, short events, double timeout, void (*cb)(int, short, void *), void *arg) |
|
|
47 | { |
|
|
48 | #if 0 |
|
|
49 | if (timeout >= 0.) |
|
|
50 | { |
|
|
51 | struct timeval tv; |
|
|
52 | tv_set (&tv, timeout); |
|
|
53 | event_once (fd, events, cb, arg, &tv); |
|
|
54 | } |
|
|
55 | else |
|
|
56 | event_once (fd, events, cb, arg, 0); |
|
|
57 | #endif |
|
|
58 | } |
44 | } |
59 | |
45 | |
60 | ///////////////////////////////////////////////////////////////////////////// |
46 | ///////////////////////////////////////////////////////////////////////////// |
61 | // Event |
47 | // Event |
62 | |
48 | |
… | |
… | |
209 | |
195 | |
210 | LEAVE; |
196 | LEAVE; |
211 | } |
197 | } |
212 | #endif |
198 | #endif |
213 | |
199 | |
|
|
200 | #define CHECK_REPEAT(repeat) if (repeat < 0.) \ |
|
|
201 | croak (# repeat " value must be >= 0"); |
|
|
202 | |
214 | ///////////////////////////////////////////////////////////////////////////// |
203 | ///////////////////////////////////////////////////////////////////////////// |
215 | // XS interface functions |
204 | // XS interface functions |
216 | |
205 | |
217 | MODULE = EV PACKAGE = EV PREFIX = ev_ |
206 | MODULE = EV PACKAGE = EV PREFIX = ev_ |
|
|
207 | |
|
|
208 | PROTOTYPES: ENABLE |
218 | |
209 | |
219 | BOOT: |
210 | BOOT: |
220 | { |
211 | { |
221 | int i; |
212 | int i; |
222 | HV *stash = gv_stashpv ("EV", 1); |
213 | HV *stash = gv_stashpv ("EV", 1); |
… | |
… | |
224 | static const struct { |
215 | static const struct { |
225 | const char *name; |
216 | const char *name; |
226 | IV iv; |
217 | IV iv; |
227 | } *civ, const_iv[] = { |
218 | } *civ, const_iv[] = { |
228 | # define const_iv(pfx, name) { # name, (IV) pfx ## name }, |
219 | # define const_iv(pfx, name) { # name, (IV) pfx ## name }, |
|
|
220 | const_iv (EV_, UNDEF) |
229 | const_iv (EV_, NONE) |
221 | const_iv (EV_, NONE) |
230 | const_iv (EV_, TIMEOUT) |
222 | const_iv (EV_, TIMEOUT) |
231 | const_iv (EV_, READ) |
223 | const_iv (EV_, READ) |
232 | const_iv (EV_, WRITE) |
224 | const_iv (EV_, WRITE) |
233 | const_iv (EV_, SIGNAL) |
225 | const_iv (EV_, SIGNAL) |
234 | const_iv (EV_, IDLE) |
226 | const_iv (EV_, IDLE) |
235 | const_iv (EV_, CHECK) |
227 | const_iv (EV_, CHECK) |
|
|
228 | const_iv (EV_, ERROR) |
236 | |
229 | |
237 | const_iv (EV, LOOP_ONESHOT) |
230 | const_iv (EV, LOOP_ONESHOT) |
238 | const_iv (EV, LOOP_NONBLOCK) |
231 | const_iv (EV, LOOP_NONBLOCK) |
239 | |
232 | |
240 | const_iv (EV, METHOD_NONE) |
233 | const_iv (EV, METHOD_NONE) |
… | |
… | |
250 | stash_time = gv_stashpv ("EV::Time" , 1); |
243 | stash_time = gv_stashpv ("EV::Time" , 1); |
251 | stash_timer = gv_stashpv ("EV::Timer" , 1); |
244 | stash_timer = gv_stashpv ("EV::Timer" , 1); |
252 | stash_periodic = gv_stashpv ("EV::Periodic", 1); |
245 | stash_periodic = gv_stashpv ("EV::Periodic", 1); |
253 | stash_signal = gv_stashpv ("EV::Signal" , 1); |
246 | stash_signal = gv_stashpv ("EV::Signal" , 1); |
254 | stash_idle = gv_stashpv ("EV::Idle" , 1); |
247 | stash_idle = gv_stashpv ("EV::Idle" , 1); |
|
|
248 | stash_prepare = gv_stashpv ("EV::Prepare" , 1); |
255 | stash_check = gv_stashpv ("EV::Check" , 1); |
249 | stash_check = gv_stashpv ("EV::Check" , 1); |
|
|
250 | stash_child = gv_stashpv ("EV::Child" , 1); |
256 | |
251 | |
257 | { |
252 | { |
258 | SV *sv = perl_get_sv ("EV::API", TRUE); |
253 | SV *sv = perl_get_sv ("EV::API", TRUE); |
259 | perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ |
254 | perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ |
260 | |
255 | |
261 | /* the poor man's shared library emulator */ |
256 | /* the poor man's shared library emulator */ |
262 | evapi.ver = EV_API_VERSION; |
257 | evapi.ver = EV_API_VERSION; |
263 | evapi.rev = EV_API_REVISION; |
258 | evapi.rev = EV_API_REVISION; |
264 | evapi.once = api_once; |
|
|
265 | evapi.sv_fileno = sv_fileno; |
259 | evapi.sv_fileno = sv_fileno; |
266 | evapi.sv_signum = sv_signum; |
260 | evapi.sv_signum = sv_signum; |
267 | evapi.now = &ev_now; |
261 | evapi.now = &ev_now; |
268 | evapi.method = &ev_method; |
262 | evapi.method = &ev_method; |
269 | evapi.loop_done = &ev_loop_done; |
263 | evapi.loop_done = &ev_loop_done; |
270 | evapi.time = ev_time; |
264 | evapi.time = ev_time; |
271 | evapi.loop = ev_loop; |
265 | evapi.loop = ev_loop; |
|
|
266 | evapi.once = ev_once; |
272 | evapi.io_start = evio_start; |
267 | evapi.io_start = evio_start; |
273 | evapi.io_stop = evio_stop; |
268 | evapi.io_stop = evio_stop; |
274 | evapi.timer_start = evtimer_start; |
269 | evapi.timer_start = evtimer_start; |
275 | evapi.timer_stop = evtimer_stop; |
270 | evapi.timer_stop = evtimer_stop; |
|
|
271 | evapi.timer_again = evtimer_again; |
276 | evapi.periodic_start = evperiodic_start; |
272 | evapi.periodic_start = evperiodic_start; |
277 | evapi.periodic_stop = evperiodic_stop; |
273 | evapi.periodic_stop = evperiodic_stop; |
278 | evapi.signal_start = evsignal_start; |
274 | evapi.signal_start = evsignal_start; |
279 | evapi.signal_stop = evsignal_stop; |
275 | evapi.signal_stop = evsignal_stop; |
280 | evapi.idle_start = evidle_start; |
276 | evapi.idle_start = evidle_start; |
281 | evapi.idle_stop = evidle_stop; |
277 | evapi.idle_stop = evidle_stop; |
|
|
278 | evapi.prepare_start = evprepare_start; |
|
|
279 | evapi.prepare_stop = evprepare_stop; |
282 | evapi.check_start = evcheck_start; |
280 | evapi.check_start = evcheck_start; |
283 | evapi.check_stop = evcheck_stop; |
281 | evapi.check_stop = evcheck_stop; |
284 | |
282 | |
285 | sv_setiv (sv, (IV)&evapi); |
283 | sv_setiv (sv, (IV)&evapi); |
286 | SvREADONLY_on (sv); |
284 | SvREADONLY_on (sv); |
… | |
… | |
321 | RETVAL |
319 | RETVAL |
322 | |
320 | |
323 | struct ev_timer *timer (NV after, NV repeat, SV *cb) |
321 | struct ev_timer *timer (NV after, NV repeat, SV *cb) |
324 | ALIAS: |
322 | ALIAS: |
325 | timer_ns = 1 |
323 | timer_ns = 1 |
|
|
324 | INIT: |
|
|
325 | CHECK_REPEAT (repeat); |
326 | CODE: |
326 | CODE: |
327 | RETVAL = e_new (sizeof (struct ev_timer), cb); |
327 | RETVAL = e_new (sizeof (struct ev_timer), cb); |
328 | evtimer_set (RETVAL, after, repeat); |
328 | evtimer_set (RETVAL, after, repeat); |
329 | if (!ix) evtimer_start (RETVAL); |
329 | if (!ix) evtimer_start (RETVAL); |
330 | OUTPUT: |
330 | OUTPUT: |
331 | RETVAL |
331 | RETVAL |
332 | |
332 | |
333 | struct ev_periodic *periodic (NV at, NV interval, SV *cb) |
333 | struct ev_periodic *periodic (NV at, NV interval, SV *cb) |
334 | ALIAS: |
334 | ALIAS: |
335 | periodic_ns = 1 |
335 | periodic_ns = 1 |
|
|
336 | INIT: |
|
|
337 | CHECK_REPEAT (interval); |
336 | CODE: |
338 | CODE: |
337 | RETVAL = e_new (sizeof (struct ev_periodic), cb); |
339 | RETVAL = e_new (sizeof (struct ev_periodic), cb); |
338 | evperiodic_set (RETVAL, at, interval); |
340 | evperiodic_set (RETVAL, at, interval); |
339 | if (!ix) evperiodic_start (RETVAL); |
341 | if (!ix) evperiodic_start (RETVAL); |
340 | OUTPUT: |
342 | OUTPUT: |
… | |
… | |
358 | evidle_set (RETVAL); |
360 | evidle_set (RETVAL); |
359 | if (!ix) evidle_start (RETVAL); |
361 | if (!ix) evidle_start (RETVAL); |
360 | OUTPUT: |
362 | OUTPUT: |
361 | RETVAL |
363 | RETVAL |
362 | |
364 | |
|
|
365 | struct ev_prepare *prepare (SV *cb) |
|
|
366 | ALIAS: |
|
|
367 | prepare_ns = 1 |
|
|
368 | CODE: |
|
|
369 | RETVAL = e_new (sizeof (struct ev_prepare), cb); |
|
|
370 | evprepare_set (RETVAL); |
|
|
371 | if (!ix) evprepare_start (RETVAL); |
|
|
372 | OUTPUT: |
|
|
373 | RETVAL |
|
|
374 | |
363 | struct ev_check *check (SV *cb) |
375 | struct ev_check *check (SV *cb) |
364 | ALIAS: |
376 | ALIAS: |
365 | check_ns = 1 |
377 | check_ns = 1 |
366 | CODE: |
378 | CODE: |
367 | RETVAL = e_new (sizeof (struct ev_check), cb); |
379 | RETVAL = e_new (sizeof (struct ev_check), cb); |
368 | evcheck_set (RETVAL); |
380 | evcheck_set (RETVAL); |
369 | if (!ix) evcheck_start (RETVAL); |
381 | if (!ix) evcheck_start (RETVAL); |
|
|
382 | OUTPUT: |
|
|
383 | RETVAL |
|
|
384 | |
|
|
385 | struct ev_child *child (SV *cb, int pid) |
|
|
386 | ALIAS: |
|
|
387 | check_ns = 1 |
|
|
388 | CODE: |
|
|
389 | RETVAL = e_new (sizeof (struct ev_check), cb); |
|
|
390 | evchild (RETVAL, pid); |
|
|
391 | if (!ix) evchild_start (RETVAL); |
370 | OUTPUT: |
392 | OUTPUT: |
371 | RETVAL |
393 | RETVAL |
372 | |
394 | |
373 | |
395 | |
374 | PROTOTYPES: DISABLE |
396 | PROTOTYPES: DISABLE |
… | |
… | |
463 | MODULE = EV PACKAGE = EV::Time |
485 | MODULE = EV PACKAGE = EV::Time |
464 | |
486 | |
465 | MODULE = EV PACKAGE = EV::Timer PREFIX = evtimer_ |
487 | MODULE = EV PACKAGE = EV::Timer PREFIX = evtimer_ |
466 | |
488 | |
467 | void evtimer_start (struct ev_timer *w) |
489 | void evtimer_start (struct ev_timer *w) |
|
|
490 | INIT: |
|
|
491 | CHECK_REPEAT (w->repeat); |
468 | |
492 | |
469 | void evtimer_stop (struct ev_timer *w) |
493 | void evtimer_stop (struct ev_timer *w) |
470 | |
494 | |
|
|
495 | void evtimer_again (struct ev_timer *w) |
|
|
496 | INIT: |
|
|
497 | CHECK_REPEAT (w->repeat); |
|
|
498 | |
471 | void set (struct ev_timer *w, NV after, NV repeat = 0.) |
499 | void set (struct ev_timer *w, NV after, NV repeat = 0.) |
|
|
500 | INIT: |
|
|
501 | CHECK_REPEAT (repeat); |
472 | CODE: |
502 | CODE: |
473 | { |
503 | { |
474 | int active = w->active; |
504 | int active = w->active; |
475 | if (active) evtimer_stop (w); |
505 | if (active) evtimer_stop (w); |
476 | evtimer_set (w, after, repeat); |
506 | evtimer_set (w, after, repeat); |
… | |
… | |
478 | } |
508 | } |
479 | |
509 | |
480 | MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_ |
510 | MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_ |
481 | |
511 | |
482 | void evperiodic_start (struct ev_periodic *w) |
512 | void evperiodic_start (struct ev_periodic *w) |
|
|
513 | INIT: |
|
|
514 | CHECK_REPEAT (w->interval); |
483 | |
515 | |
484 | void evperiodic_stop (struct ev_periodic *w) |
516 | void evperiodic_stop (struct ev_periodic *w) |
485 | |
517 | |
486 | void set (struct ev_periodic *w, NV at, NV interval = 0.) |
518 | void set (struct ev_periodic *w, NV at, NV interval = 0.) |
|
|
519 | INIT: |
|
|
520 | CHECK_REPEAT (interval); |
487 | CODE: |
521 | CODE: |
488 | { |
522 | { |
489 | int active = w->active; |
523 | int active = w->active; |
490 | if (active) evperiodic_stop (w); |
524 | if (active) evperiodic_stop (w); |
491 | evperiodic_set (w, at, interval); |
525 | evperiodic_set (w, at, interval); |
… | |
… | |
496 | |
530 | |
497 | void evidle_start (struct ev_idle *w) |
531 | void evidle_start (struct ev_idle *w) |
498 | |
532 | |
499 | void evidle_stop (struct ev_idle *w) |
533 | void evidle_stop (struct ev_idle *w) |
500 | |
534 | |
|
|
535 | MODULE = EV PACKAGE = EV::Prepare PREFIX = evcheck_ |
|
|
536 | |
|
|
537 | void evprepare_start (struct ev_prepare *w) |
|
|
538 | |
|
|
539 | void evprepare_stop (struct ev_prepare *w) |
|
|
540 | |
501 | MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_ |
541 | MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_ |
502 | |
542 | |
503 | void evcheck_start (struct ev_check *w) |
543 | void evcheck_start (struct ev_check *w) |
504 | |
544 | |
505 | void evcheck_stop (struct ev_check *w) |
545 | void evcheck_stop (struct ev_check *w) |
|
|
546 | |
|
|
547 | MODULE = EV PACKAGE = EV::Child PREFIX = evchild_ |
|
|
548 | |
|
|
549 | void evchild_start (struct ev_child *w) |
|
|
550 | |
|
|
551 | void evchild_stop (struct ev_child *w) |
|
|
552 | |
|
|
553 | void set (struct ev_child *w, int pid) |
|
|
554 | CODE: |
|
|
555 | { |
|
|
556 | int active = w->active; |
|
|
557 | if (active) evchild_stop (w); |
|
|
558 | evchild_set (w, pid); |
|
|
559 | if (active) evchild_start (w); |
|
|
560 | } |
506 | |
561 | |
507 | #if 0 |
562 | #if 0 |
508 | |
563 | |
509 | MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ |
564 | MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ |
510 | |
565 | |