… | |
… | |
2 | #include "perl.h" |
2 | #include "perl.h" |
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 |
|
|
8 | #define EV_USE_EPOLL 1 |
|
|
9 | |
|
|
10 | #define EV_PROTOTYPES 1 |
7 | #define EV_PROTOTYPES 1 |
11 | #include "EV/EVAPI.h" |
8 | #include "EV/EVAPI.h" |
12 | |
9 | |
13 | #include "libev/ev.c" |
10 | #include "libev/ev.c" |
|
|
11 | #include "libev/event.h" |
|
|
12 | #include "libev/event.c" |
|
|
13 | #include "libev/evdns.c" |
14 | |
14 | |
15 | typedef int Signal; |
15 | typedef int Signal; |
16 | |
16 | |
17 | static struct EVAPI evapi; |
17 | static struct EVAPI evapi; |
18 | |
18 | |
… | |
… | |
83 | w->self = self; |
83 | w->self = self; |
84 | |
84 | |
85 | return (void *)w; |
85 | return (void *)w; |
86 | } |
86 | } |
87 | |
87 | |
|
|
88 | static void * |
|
|
89 | e_destroy (void *w_) |
|
|
90 | { |
|
|
91 | struct ev_watcher *w = w_; |
|
|
92 | |
|
|
93 | SvREFCNT_dec (w->fh ); w->fh = 0; |
|
|
94 | SvREFCNT_dec (w->cb_sv); w->cb_sv = 0; |
|
|
95 | } |
|
|
96 | |
88 | static SV * |
97 | static SV * |
89 | e_bless (struct ev_watcher *w, HV *stash) |
98 | e_bless (struct ev_watcher *w, HV *stash) |
90 | { |
99 | { |
91 | SV *rv; |
100 | SV *rv; |
92 | |
101 | |
… | |
… | |
147 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
156 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
148 | SP = PL_stack_base + mark; PUTBACK; |
157 | SP = PL_stack_base + mark; PUTBACK; |
149 | } |
158 | } |
150 | } |
159 | } |
151 | |
160 | |
152 | #if 0 |
|
|
153 | ///////////////////////////////////////////////////////////////////////////// |
161 | ///////////////////////////////////////////////////////////////////////////// |
154 | // DNS |
162 | // DNS |
155 | |
163 | |
156 | static void |
164 | static void |
157 | dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) |
165 | dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) |
… | |
… | |
199 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
207 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
200 | } |
208 | } |
201 | |
209 | |
202 | LEAVE; |
210 | LEAVE; |
203 | } |
211 | } |
204 | #endif |
|
|
205 | |
212 | |
206 | #define CHECK_REPEAT(repeat) if (repeat < 0.) \ |
213 | #define CHECK_REPEAT(repeat) if (repeat < 0.) \ |
207 | croak (# repeat " value must be >= 0"); |
214 | croak (# repeat " value must be >= 0"); |
208 | |
215 | |
209 | #define CHECK_FD(fh,fd) if ((fd) < 0) \ |
216 | #define CHECK_FD(fh,fd) if ((fd) < 0) \ |
… | |
… | |
224 | static const struct { |
231 | static const struct { |
225 | const char *name; |
232 | const char *name; |
226 | IV iv; |
233 | IV iv; |
227 | } *civ, const_iv[] = { |
234 | } *civ, const_iv[] = { |
228 | # define const_iv(pfx, name) { # name, (IV) pfx ## name }, |
235 | # define const_iv(pfx, name) { # name, (IV) pfx ## name }, |
|
|
236 | const_iv (EV_, MINPRI) |
|
|
237 | const_iv (EV_, MAXPRI) |
|
|
238 | |
229 | const_iv (EV_, UNDEF) |
239 | const_iv (EV_, UNDEF) |
230 | const_iv (EV_, NONE) |
240 | const_iv (EV_, NONE) |
231 | const_iv (EV_, TIMEOUT) |
241 | const_iv (EV_, TIMEOUT) |
232 | const_iv (EV_, READ) |
242 | const_iv (EV_, READ) |
233 | const_iv (EV_, WRITE) |
243 | const_iv (EV_, WRITE) |
… | |
… | |
237 | const_iv (EV_, ERROR) |
247 | const_iv (EV_, ERROR) |
238 | |
248 | |
239 | const_iv (EV, LOOP_ONESHOT) |
249 | const_iv (EV, LOOP_ONESHOT) |
240 | const_iv (EV, LOOP_NONBLOCK) |
250 | const_iv (EV, LOOP_NONBLOCK) |
241 | |
251 | |
242 | const_iv (EV, METHOD_NONE) |
252 | const_iv (EV, METHOD_AUTO) |
243 | const_iv (EV, METHOD_SELECT) |
253 | const_iv (EV, METHOD_SELECT) |
|
|
254 | const_iv (EV, METHOD_POLL) |
244 | const_iv (EV, METHOD_EPOLL) |
255 | const_iv (EV, METHOD_EPOLL) |
|
|
256 | const_iv (EV, METHOD_KQUEUE) |
|
|
257 | const_iv (EV, METHOD_DEVPOLL) |
|
|
258 | const_iv (EV, METHOD_PORT) |
|
|
259 | const_iv (EV, METHOD_ANY) |
245 | }; |
260 | }; |
246 | |
261 | |
247 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
262 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
248 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
263 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
249 | |
264 | |
… | |
… | |
291 | evapi.child_stop = ev_child_stop; |
306 | evapi.child_stop = ev_child_stop; |
292 | |
307 | |
293 | sv_setiv (sv, (IV)&evapi); |
308 | sv_setiv (sv, (IV)&evapi); |
294 | SvREADONLY_on (sv); |
309 | SvREADONLY_on (sv); |
295 | } |
310 | } |
|
|
311 | |
|
|
312 | pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); |
296 | } |
313 | } |
297 | |
314 | |
298 | NV ev_now () |
315 | NV ev_now () |
299 | CODE: |
316 | CODE: |
300 | RETVAL = ev_now; |
317 | RETVAL = ev_now; |
… | |
… | |
307 | OUTPUT: |
324 | OUTPUT: |
308 | RETVAL |
325 | RETVAL |
309 | |
326 | |
310 | NV ev_time () |
327 | NV ev_time () |
311 | |
328 | |
312 | void ev_init (int flags = 0) |
329 | void ev_init (int methods = EVMETHOD_AUTO) |
313 | |
330 | |
314 | void ev_loop (int flags = 0) |
331 | void ev_loop (int flags = 0) |
315 | |
332 | |
316 | void ev_loop_done (int value = 1) |
333 | void ev_loop_done (int value = 1) |
317 | CODE: |
334 | CODE: |
… | |
… | |
427 | |
444 | |
428 | void trigger (struct ev_watcher *w, int revents = EV_NONE) |
445 | void trigger (struct ev_watcher *w, int revents = EV_NONE) |
429 | CODE: |
446 | CODE: |
430 | w->cb (w, revents); |
447 | w->cb (w, revents); |
431 | |
448 | |
|
|
449 | int priority (struct ev_watcher *w, int new_priority = 0) |
|
|
450 | CODE: |
|
|
451 | { |
|
|
452 | RETVAL = w->priority; |
|
|
453 | |
|
|
454 | if (items > 1) |
|
|
455 | { |
|
|
456 | int active = ev_is_active (w); |
|
|
457 | |
|
|
458 | if (new_priority < EV_MINPRI || new_priority > EV_MAXPRI) |
|
|
459 | croak ("watcher priority out of range, value must be between %d and %d, inclusive", EV_MINPRI, EV_MAXPRI); |
|
|
460 | |
|
|
461 | if (active) |
|
|
462 | { |
|
|
463 | /* grrr. */ |
|
|
464 | PUSHMARK (SP); |
|
|
465 | XPUSHs (ST (0)); |
|
|
466 | call_method ("stop", G_DISCARD | G_VOID); |
|
|
467 | } |
|
|
468 | |
|
|
469 | ev_set_priority (w, new_priority); |
|
|
470 | |
|
|
471 | if (active) |
|
|
472 | { |
|
|
473 | PUSHMARK (SP); |
|
|
474 | XPUSHs (ST (0)); |
|
|
475 | call_method ("start", G_DISCARD | G_VOID); |
|
|
476 | } |
|
|
477 | } |
|
|
478 | } |
|
|
479 | OUTPUT: |
|
|
480 | RETVAL |
|
|
481 | |
432 | MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ |
482 | MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ |
433 | |
483 | |
434 | void ev_io_start (struct ev_io *w) |
484 | void ev_io_start (struct ev_io *w) |
435 | |
485 | |
436 | void ev_io_stop (struct ev_io *w) |
486 | void ev_io_stop (struct ev_io *w) |
437 | |
487 | |
438 | void DESTROY (struct ev_io *w) |
488 | void DESTROY (struct ev_io *w) |
439 | CODE: |
489 | CODE: |
440 | ev_io_stop (w); |
490 | ev_io_stop (w); |
|
|
491 | e_destroy (w); |
441 | |
492 | |
442 | void set (struct ev_io *w, SV *fh, int events) |
493 | void set (struct ev_io *w, SV *fh, int events) |
443 | CODE: |
494 | CODE: |
444 | { |
495 | { |
445 | int active = w->active; |
496 | int active = ev_is_active (w); |
446 | int fd = sv_fileno (fh); |
497 | int fd = sv_fileno (fh); |
447 | CHECK_FD (fh, fd); |
498 | CHECK_FD (fh, fd); |
448 | |
499 | |
449 | if (active) ev_io_stop (w); |
500 | if (active) ev_io_stop (w); |
450 | |
501 | |
… | |
… | |
459 | { |
510 | { |
460 | RETVAL = newSVsv (w->fh); |
511 | RETVAL = newSVsv (w->fh); |
461 | |
512 | |
462 | if (items > 1) |
513 | if (items > 1) |
463 | { |
514 | { |
464 | int active = w->active; |
515 | int active = ev_is_active (w); |
465 | if (active) ev_io_stop (w); |
516 | if (active) ev_io_stop (w); |
466 | |
517 | |
467 | sv_setsv (w->fh, new_fh); |
518 | sv_setsv (w->fh, new_fh); |
468 | ev_io_set (w, sv_fileno (w->fh), w->events); |
519 | ev_io_set (w, sv_fileno (w->fh), w->events); |
469 | |
520 | |
… | |
… | |
471 | } |
522 | } |
472 | } |
523 | } |
473 | OUTPUT: |
524 | OUTPUT: |
474 | RETVAL |
525 | RETVAL |
475 | |
526 | |
476 | short events (struct ev_io *w, short new_events = EV_UNDEF) |
527 | int events (struct ev_io *w, int new_events = EV_UNDEF) |
477 | CODE: |
528 | CODE: |
478 | { |
529 | { |
479 | RETVAL = w->events; |
530 | RETVAL = w->events; |
480 | |
531 | |
481 | if (items > 1) |
532 | if (items > 1) |
482 | { |
533 | { |
483 | int active = w->active; |
534 | int active = ev_is_active (w); |
484 | if (active) ev_io_stop (w); |
535 | if (active) ev_io_stop (w); |
485 | |
536 | |
486 | ev_io_set (w, w->fd, new_events); |
537 | ev_io_set (w, w->fd, new_events); |
487 | |
538 | |
488 | if (active) ev_io_start (w); |
539 | if (active) ev_io_start (w); |
… | |
… | |
498 | void ev_signal_stop (struct ev_signal *w) |
549 | void ev_signal_stop (struct ev_signal *w) |
499 | |
550 | |
500 | void DESTROY (struct ev_signal *w) |
551 | void DESTROY (struct ev_signal *w) |
501 | CODE: |
552 | CODE: |
502 | ev_signal_stop (w); |
553 | ev_signal_stop (w); |
|
|
554 | e_destroy (w); |
503 | |
555 | |
504 | void set (struct ev_signal *w, SV *signal = 0) |
556 | void set (struct ev_signal *w, SV *signal) |
505 | CODE: |
557 | CODE: |
506 | { |
558 | { |
507 | Signal signum = sv_signum (signal); /* may croak here */ |
559 | Signal signum = sv_signum (signal); /* may croak here */ |
508 | int active = w->active; |
560 | int active = ev_is_active (w); |
509 | |
561 | |
510 | if (active) ev_signal_stop (w); |
562 | if (active) ev_signal_stop (w); |
|
|
563 | |
511 | ev_signal_set (w, signum); |
564 | ev_signal_set (w, signum); |
|
|
565 | |
512 | if (active) ev_signal_start (w); |
566 | if (active) ev_signal_start (w); |
513 | } |
567 | } |
|
|
568 | |
|
|
569 | int signal (struct ev_signal *w, SV *new_signal = 0) |
|
|
570 | CODE: |
|
|
571 | { |
|
|
572 | RETVAL = w->signum; |
|
|
573 | |
|
|
574 | if (items > 1) |
|
|
575 | { |
|
|
576 | Signal signum = sv_signum (new_signal); /* may croak here */ |
|
|
577 | int active = ev_is_active (w); |
|
|
578 | if (active) ev_signal_stop (w); |
|
|
579 | |
|
|
580 | ev_signal_set (w, signum); |
|
|
581 | |
|
|
582 | if (active) ev_signal_start (w); |
|
|
583 | } |
|
|
584 | } |
|
|
585 | OUTPUT: |
|
|
586 | RETVAL |
514 | |
587 | |
515 | MODULE = EV PACKAGE = EV::Time |
588 | MODULE = EV PACKAGE = EV::Time |
516 | |
589 | |
517 | MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ |
590 | MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ |
518 | |
591 | |
… | |
… | |
527 | CHECK_REPEAT (w->repeat); |
600 | CHECK_REPEAT (w->repeat); |
528 | |
601 | |
529 | void DESTROY (struct ev_timer *w) |
602 | void DESTROY (struct ev_timer *w) |
530 | CODE: |
603 | CODE: |
531 | ev_timer_stop (w); |
604 | ev_timer_stop (w); |
|
|
605 | e_destroy (w); |
532 | |
606 | |
533 | void set (struct ev_timer *w, NV after, NV repeat = 0.) |
607 | void set (struct ev_timer *w, NV after, NV repeat = 0.) |
534 | INIT: |
608 | INIT: |
535 | CHECK_REPEAT (repeat); |
609 | CHECK_REPEAT (repeat); |
536 | CODE: |
610 | CODE: |
537 | { |
611 | { |
538 | int active = w->active; |
612 | int active = ev_is_active (w); |
539 | if (active) ev_timer_stop (w); |
613 | if (active) ev_timer_stop (w); |
540 | ev_timer_set (w, after, repeat); |
614 | ev_timer_set (w, after, repeat); |
541 | if (active) ev_timer_start (w); |
615 | if (active) ev_timer_start (w); |
542 | } |
616 | } |
543 | |
617 | |
… | |
… | |
550 | void ev_periodic_stop (struct ev_periodic *w) |
624 | void ev_periodic_stop (struct ev_periodic *w) |
551 | |
625 | |
552 | void DESTROY (struct ev_periodic *w) |
626 | void DESTROY (struct ev_periodic *w) |
553 | CODE: |
627 | CODE: |
554 | ev_periodic_stop (w); |
628 | ev_periodic_stop (w); |
|
|
629 | e_destroy (w); |
555 | |
630 | |
556 | void set (struct ev_periodic *w, NV at, NV interval = 0.) |
631 | void set (struct ev_periodic *w, NV at, NV interval = 0.) |
557 | INIT: |
632 | INIT: |
558 | CHECK_REPEAT (interval); |
633 | CHECK_REPEAT (interval); |
559 | CODE: |
634 | CODE: |
560 | { |
635 | { |
561 | int active = w->active; |
636 | int active = ev_is_active (w); |
562 | if (active) ev_periodic_stop (w); |
637 | if (active) ev_periodic_stop (w); |
|
|
638 | |
563 | ev_periodic_set (w, at, interval); |
639 | ev_periodic_set (w, at, interval); |
|
|
640 | |
564 | if (active) ev_periodic_start (w); |
641 | if (active) ev_periodic_start (w); |
565 | } |
642 | } |
566 | |
643 | |
567 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
644 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
568 | |
645 | |
… | |
… | |
571 | void ev_idle_stop (struct ev_idle *w) |
648 | void ev_idle_stop (struct ev_idle *w) |
572 | |
649 | |
573 | void DESTROY (struct ev_idle *w) |
650 | void DESTROY (struct ev_idle *w) |
574 | CODE: |
651 | CODE: |
575 | ev_idle_stop (w); |
652 | ev_idle_stop (w); |
|
|
653 | e_destroy (w); |
576 | |
654 | |
577 | MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_ |
655 | MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_ |
578 | |
656 | |
579 | void ev_prepare_start (struct ev_prepare *w) |
657 | void ev_prepare_start (struct ev_prepare *w) |
580 | |
658 | |
581 | void ev_prepare_stop (struct ev_prepare *w) |
659 | void ev_prepare_stop (struct ev_prepare *w) |
582 | |
660 | |
583 | void DESTROY (struct ev_prepare *w) |
661 | void DESTROY (struct ev_prepare *w) |
584 | CODE: |
662 | CODE: |
585 | ev_prepare_stop (w); |
663 | ev_prepare_stop (w); |
|
|
664 | e_destroy (w); |
586 | |
665 | |
587 | MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_ |
666 | MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_ |
588 | |
667 | |
589 | void ev_check_start (struct ev_check *w) |
668 | void ev_check_start (struct ev_check *w) |
590 | |
669 | |
591 | void ev_check_stop (struct ev_check *w) |
670 | void ev_check_stop (struct ev_check *w) |
592 | |
671 | |
593 | void DESTROY (struct ev_check *w) |
672 | void DESTROY (struct ev_check *w) |
594 | CODE: |
673 | CODE: |
595 | ev_check_stop (w); |
674 | ev_check_stop (w); |
|
|
675 | e_destroy (w); |
596 | |
676 | |
597 | MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ |
677 | MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ |
598 | |
678 | |
599 | void ev_child_start (struct ev_child *w) |
679 | void ev_child_start (struct ev_child *w) |
600 | |
680 | |
601 | void ev_child_stop (struct ev_child *w) |
681 | void ev_child_stop (struct ev_child *w) |
602 | |
682 | |
603 | void DESTROY (struct ev_child *w) |
683 | void DESTROY (struct ev_child *w) |
604 | CODE: |
684 | CODE: |
605 | ev_child_stop (w); |
685 | ev_child_stop (w); |
|
|
686 | e_destroy (w); |
606 | |
687 | |
607 | void set (struct ev_child *w, int pid) |
688 | void set (struct ev_child *w, int pid) |
608 | CODE: |
689 | CODE: |
609 | { |
690 | { |
610 | int active = w->active; |
691 | int active = ev_is_active (w); |
611 | if (active) ev_child_stop (w); |
692 | if (active) ev_child_stop (w); |
|
|
693 | |
612 | ev_child_set (w, pid); |
694 | ev_child_set (w, pid); |
|
|
695 | |
613 | if (active) ev_child_start (w); |
696 | if (active) ev_child_start (w); |
614 | } |
697 | } |
615 | |
698 | |
|
|
699 | int pid (struct ev_child *w, int new_pid = 0) |
|
|
700 | CODE: |
|
|
701 | { |
|
|
702 | RETVAL = w->pid; |
|
|
703 | |
|
|
704 | if (items > 1) |
|
|
705 | { |
|
|
706 | int active = ev_is_active (w); |
|
|
707 | if (active) ev_child_stop (w); |
|
|
708 | |
|
|
709 | ev_child_set (w, new_pid); |
|
|
710 | |
|
|
711 | if (active) ev_child_start (w); |
|
|
712 | } |
|
|
713 | } |
|
|
714 | OUTPUT: |
|
|
715 | RETVAL |
|
|
716 | |
|
|
717 | |
616 | int status (struct ev_child *w) |
718 | int status (struct ev_child *w) |
617 | CODE: |
719 | CODE: |
618 | RETVAL = w->status; |
720 | RETVAL = w->status; |
619 | OUTPUT: |
721 | OUTPUT: |
620 | RETVAL |
722 | RETVAL |
621 | |
|
|
622 | #if 0 |
|
|
623 | |
723 | |
624 | MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ |
724 | MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ |
625 | |
725 | |
626 | BOOT: |
726 | BOOT: |
627 | { |
727 | { |
… | |
… | |
710 | |
810 | |
711 | void evdns_search_add (char *domain) |
811 | void evdns_search_add (char *domain) |
712 | |
812 | |
713 | void evdns_search_ndots_set (int ndots) |
813 | void evdns_search_ndots_set (int ndots) |
714 | |
814 | |
|
|
815 | #if 0 |
715 | |
816 | |
716 | MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ |
817 | MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ |
717 | |
818 | |
718 | BOOT: |
819 | BOOT: |
719 | { |
820 | { |