… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | /*#include <netinet/in.h>*/ |
5 | /*#include <netinet/in.h>*/ |
6 | |
6 | |
7 | #define TIMEOUT_NONE HUGE_VAL |
7 | #define TIMEOUT_NONE HUGE_VAL |
8 | #define HAVE_EPOLL 1 |
|
|
9 | |
8 | |
10 | #define EV_PROTOTYPES 1 |
9 | #define EV_PROTOTYPES 1 |
11 | #include "EV/EVAPI.h" |
10 | #include "EV/EVAPI.h" |
12 | |
11 | |
13 | #include "libev/ev.c" |
12 | #include "libev/ev.c" |
… | |
… | |
17 | static struct EVAPI evapi; |
16 | static struct EVAPI evapi; |
18 | |
17 | |
19 | static HV |
18 | static HV |
20 | *stash_watcher, |
19 | *stash_watcher, |
21 | *stash_io, |
20 | *stash_io, |
22 | *stash_time, |
|
|
23 | *stash_timer, |
21 | *stash_timer, |
24 | *stash_periodic, |
22 | *stash_periodic, |
25 | *stash_signal, |
23 | *stash_signal, |
26 | *stash_idle, |
24 | *stash_idle, |
27 | *stash_prepare, |
25 | *stash_prepare, |
… | |
… | |
31 | static int |
29 | static int |
32 | sv_signum (SV *sig) |
30 | sv_signum (SV *sig) |
33 | { |
31 | { |
34 | int signum; |
32 | int signum; |
35 | |
33 | |
36 | if (SvIV (sig) > 0) |
34 | SvGETMAGIC (sig); |
37 | return SvIV (sig); |
|
|
38 | |
35 | |
39 | for (signum = 1; signum < SIG_SIZE; ++signum) |
36 | for (signum = 1; signum < SIG_SIZE; ++signum) |
40 | if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
37 | if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
41 | return signum; |
38 | return signum; |
42 | |
39 | |
|
|
40 | if (SvIV (sig) > 0) |
|
|
41 | return SvIV (sig); |
|
|
42 | |
43 | return -1; |
43 | return -1; |
44 | } |
44 | } |
45 | |
45 | |
46 | ///////////////////////////////////////////////////////////////////////////// |
46 | ///////////////////////////////////////////////////////////////////////////// |
47 | // Event |
47 | // Event |
… | |
… | |
57 | fh = SvRV (fh); |
57 | fh = SvRV (fh); |
58 | |
58 | |
59 | if (SvTYPE (fh) == SVt_PVGV) |
59 | if (SvTYPE (fh) == SVt_PVGV) |
60 | return PerlIO_fileno (IoIFP (sv_2io (fh))); |
60 | return PerlIO_fileno (IoIFP (sv_2io (fh))); |
61 | |
61 | |
62 | if (SvIOK (fh)) |
62 | if ((SvIV (fh) >= 0) && (SvIV (fh) < 0x7ffffff)) |
63 | return SvIV (fh); |
63 | return SvIV (fh); |
64 | |
64 | |
65 | return -1; |
65 | return -1; |
66 | } |
66 | } |
67 | |
67 | |
… | |
… | |
80 | w->fh = 0; |
80 | w->fh = 0; |
81 | w->cb_sv = newSVsv (cb_sv); |
81 | w->cb_sv = newSVsv (cb_sv); |
82 | w->self = self; |
82 | w->self = self; |
83 | |
83 | |
84 | return (void *)w; |
84 | return (void *)w; |
|
|
85 | } |
|
|
86 | |
|
|
87 | static void * |
|
|
88 | e_destroy (void *w_) |
|
|
89 | { |
|
|
90 | struct ev_watcher *w = w_; |
|
|
91 | |
|
|
92 | SvREFCNT_dec (w->fh ); w->fh = 0; |
|
|
93 | SvREFCNT_dec (w->cb_sv); w->cb_sv = 0; |
85 | } |
94 | } |
86 | |
95 | |
87 | static SV * |
96 | static SV * |
88 | e_bless (struct ev_watcher *w, HV *stash) |
97 | e_bless (struct ev_watcher *w, HV *stash) |
89 | { |
98 | { |
… | |
… | |
203 | #endif |
212 | #endif |
204 | |
213 | |
205 | #define CHECK_REPEAT(repeat) if (repeat < 0.) \ |
214 | #define CHECK_REPEAT(repeat) if (repeat < 0.) \ |
206 | croak (# repeat " value must be >= 0"); |
215 | croak (# repeat " value must be >= 0"); |
207 | |
216 | |
|
|
217 | #define CHECK_FD(fh,fd) if ((fd) < 0) \ |
|
|
218 | croak ("illegal file descriptor or filehandle (either no attached file descriptor or illegal value): %s", SvPV_nolen (fh)); |
|
|
219 | |
208 | ///////////////////////////////////////////////////////////////////////////// |
220 | ///////////////////////////////////////////////////////////////////////////// |
209 | // XS interface functions |
221 | // XS interface functions |
210 | |
222 | |
211 | MODULE = EV PACKAGE = EV PREFIX = ev_ |
223 | MODULE = EV PACKAGE = EV PREFIX = ev_ |
212 | |
224 | |
… | |
… | |
243 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
255 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
244 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
256 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
245 | |
257 | |
246 | stash_watcher = gv_stashpv ("EV::Watcher" , 1); |
258 | stash_watcher = gv_stashpv ("EV::Watcher" , 1); |
247 | stash_io = gv_stashpv ("EV::Io" , 1); |
259 | stash_io = gv_stashpv ("EV::Io" , 1); |
248 | stash_time = gv_stashpv ("EV::Time" , 1); |
|
|
249 | stash_timer = gv_stashpv ("EV::Timer" , 1); |
260 | stash_timer = gv_stashpv ("EV::Timer" , 1); |
250 | stash_periodic = gv_stashpv ("EV::Periodic", 1); |
261 | stash_periodic = gv_stashpv ("EV::Periodic", 1); |
251 | stash_signal = gv_stashpv ("EV::Signal" , 1); |
262 | stash_signal = gv_stashpv ("EV::Signal" , 1); |
252 | stash_idle = gv_stashpv ("EV::Idle" , 1); |
263 | stash_idle = gv_stashpv ("EV::Idle" , 1); |
253 | stash_prepare = gv_stashpv ("EV::Prepare" , 1); |
264 | stash_prepare = gv_stashpv ("EV::Prepare" , 1); |
… | |
… | |
288 | evapi.child_stop = ev_child_stop; |
299 | evapi.child_stop = ev_child_stop; |
289 | |
300 | |
290 | sv_setiv (sv, (IV)&evapi); |
301 | sv_setiv (sv, (IV)&evapi); |
291 | SvREADONLY_on (sv); |
302 | SvREADONLY_on (sv); |
292 | } |
303 | } |
|
|
304 | |
|
|
305 | pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); |
293 | } |
306 | } |
294 | |
307 | |
295 | NV ev_now () |
308 | NV ev_now () |
296 | CODE: |
309 | CODE: |
297 | RETVAL = ev_now; |
310 | RETVAL = ev_now; |
… | |
… | |
316 | |
329 | |
317 | struct ev_io *io (SV *fh, int events, SV *cb) |
330 | struct ev_io *io (SV *fh, int events, SV *cb) |
318 | ALIAS: |
331 | ALIAS: |
319 | io_ns = 1 |
332 | io_ns = 1 |
320 | CODE: |
333 | CODE: |
|
|
334 | { |
|
|
335 | int fd = sv_fileno (fh); |
|
|
336 | CHECK_FD (fh, fd); |
|
|
337 | |
321 | RETVAL = e_new (sizeof (struct ev_io), cb); |
338 | RETVAL = e_new (sizeof (struct ev_io), cb); |
322 | RETVAL->fh = newSVsv (fh); |
339 | RETVAL->fh = newSVsv (fh); |
323 | ev_io_set (RETVAL, sv_fileno (RETVAL->fh), events); |
340 | ev_io_set (RETVAL, fd, events); |
324 | if (!ix) ev_io_start (RETVAL); |
341 | if (!ix) ev_io_start (RETVAL); |
|
|
342 | } |
325 | OUTPUT: |
343 | OUTPUT: |
326 | RETVAL |
344 | RETVAL |
327 | |
345 | |
328 | struct ev_timer *timer (NV after, NV repeat, SV *cb) |
346 | struct ev_timer *timer (NV after, NV repeat, SV *cb) |
329 | ALIAS: |
347 | ALIAS: |
… | |
… | |
428 | void ev_io_stop (struct ev_io *w) |
446 | void ev_io_stop (struct ev_io *w) |
429 | |
447 | |
430 | void DESTROY (struct ev_io *w) |
448 | void DESTROY (struct ev_io *w) |
431 | CODE: |
449 | CODE: |
432 | ev_io_stop (w); |
450 | ev_io_stop (w); |
|
|
451 | e_destroy (w); |
433 | |
452 | |
434 | void set (struct ev_io *w, SV *fh, int events) |
453 | void set (struct ev_io *w, SV *fh, int events) |
435 | CODE: |
454 | CODE: |
436 | { |
455 | { |
437 | int active = w->active; |
456 | int active = w->active; |
|
|
457 | int fd = sv_fileno (fh); |
|
|
458 | CHECK_FD (fh, fd); |
|
|
459 | |
438 | if (active) ev_io_stop (w); |
460 | if (active) ev_io_stop (w); |
439 | |
461 | |
440 | sv_setsv (w->fh, fh); |
462 | sv_setsv (w->fh, fh); |
441 | ev_io_set (w, sv_fileno (w->fh), events); |
463 | ev_io_set (w, fd, events); |
442 | |
464 | |
443 | if (active) ev_io_start (w); |
465 | if (active) ev_io_start (w); |
444 | } |
466 | } |
445 | |
467 | |
446 | SV *fh (struct ev_io *w, SV *new_fh = 0) |
468 | SV *fh (struct ev_io *w, SV *new_fh = 0) |
… | |
… | |
487 | void ev_signal_stop (struct ev_signal *w) |
509 | void ev_signal_stop (struct ev_signal *w) |
488 | |
510 | |
489 | void DESTROY (struct ev_signal *w) |
511 | void DESTROY (struct ev_signal *w) |
490 | CODE: |
512 | CODE: |
491 | ev_signal_stop (w); |
513 | ev_signal_stop (w); |
|
|
514 | e_destroy (w); |
492 | |
515 | |
493 | void set (struct ev_signal *w, SV *signal = 0) |
516 | void set (struct ev_signal *w, SV *signal = 0) |
494 | CODE: |
517 | CODE: |
495 | { |
518 | { |
496 | Signal signum = sv_signum (signal); /* may croak here */ |
519 | Signal signum = sv_signum (signal); /* may croak here */ |
… | |
… | |
516 | CHECK_REPEAT (w->repeat); |
539 | CHECK_REPEAT (w->repeat); |
517 | |
540 | |
518 | void DESTROY (struct ev_timer *w) |
541 | void DESTROY (struct ev_timer *w) |
519 | CODE: |
542 | CODE: |
520 | ev_timer_stop (w); |
543 | ev_timer_stop (w); |
|
|
544 | e_destroy (w); |
521 | |
545 | |
522 | void set (struct ev_timer *w, NV after, NV repeat = 0.) |
546 | void set (struct ev_timer *w, NV after, NV repeat = 0.) |
523 | INIT: |
547 | INIT: |
524 | CHECK_REPEAT (repeat); |
548 | CHECK_REPEAT (repeat); |
525 | CODE: |
549 | CODE: |
… | |
… | |
539 | void ev_periodic_stop (struct ev_periodic *w) |
563 | void ev_periodic_stop (struct ev_periodic *w) |
540 | |
564 | |
541 | void DESTROY (struct ev_periodic *w) |
565 | void DESTROY (struct ev_periodic *w) |
542 | CODE: |
566 | CODE: |
543 | ev_periodic_stop (w); |
567 | ev_periodic_stop (w); |
|
|
568 | e_destroy (w); |
544 | |
569 | |
545 | void set (struct ev_periodic *w, NV at, NV interval = 0.) |
570 | void set (struct ev_periodic *w, NV at, NV interval = 0.) |
546 | INIT: |
571 | INIT: |
547 | CHECK_REPEAT (interval); |
572 | CHECK_REPEAT (interval); |
548 | CODE: |
573 | CODE: |
… | |
… | |
560 | void ev_idle_stop (struct ev_idle *w) |
585 | void ev_idle_stop (struct ev_idle *w) |
561 | |
586 | |
562 | void DESTROY (struct ev_idle *w) |
587 | void DESTROY (struct ev_idle *w) |
563 | CODE: |
588 | CODE: |
564 | ev_idle_stop (w); |
589 | ev_idle_stop (w); |
|
|
590 | e_destroy (w); |
565 | |
591 | |
566 | MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_ |
592 | MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_ |
567 | |
593 | |
568 | void ev_prepare_start (struct ev_prepare *w) |
594 | void ev_prepare_start (struct ev_prepare *w) |
569 | |
595 | |
570 | void ev_prepare_stop (struct ev_prepare *w) |
596 | void ev_prepare_stop (struct ev_prepare *w) |
571 | |
597 | |
572 | void DESTROY (struct ev_prepare *w) |
598 | void DESTROY (struct ev_prepare *w) |
573 | CODE: |
599 | CODE: |
574 | ev_prepare_stop (w); |
600 | ev_prepare_stop (w); |
|
|
601 | e_destroy (w); |
575 | |
602 | |
576 | MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_ |
603 | MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_ |
577 | |
604 | |
578 | void ev_check_start (struct ev_check *w) |
605 | void ev_check_start (struct ev_check *w) |
579 | |
606 | |
580 | void ev_check_stop (struct ev_check *w) |
607 | void ev_check_stop (struct ev_check *w) |
581 | |
608 | |
582 | void DESTROY (struct ev_check *w) |
609 | void DESTROY (struct ev_check *w) |
583 | CODE: |
610 | CODE: |
584 | ev_check_stop (w); |
611 | ev_check_stop (w); |
|
|
612 | e_destroy (w); |
585 | |
613 | |
586 | MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ |
614 | MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ |
587 | |
615 | |
588 | void ev_child_start (struct ev_child *w) |
616 | void ev_child_start (struct ev_child *w) |
589 | |
617 | |
590 | void ev_child_stop (struct ev_child *w) |
618 | void ev_child_stop (struct ev_child *w) |
591 | |
619 | |
592 | void DESTROY (struct ev_child *w) |
620 | void DESTROY (struct ev_child *w) |
593 | CODE: |
621 | CODE: |
594 | ev_child_stop (w); |
622 | ev_child_stop (w); |
|
|
623 | e_destroy (w); |
595 | |
624 | |
596 | void set (struct ev_child *w, int pid) |
625 | void set (struct ev_child *w, int pid) |
597 | CODE: |
626 | CODE: |
598 | { |
627 | { |
599 | int active = w->active; |
628 | int active = w->active; |