… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | /*#include <netinet/in.h>*/ |
5 | /*#include <netinet/in.h>*/ |
6 | |
6 | |
7 | #define EV_PROTOTYPES 1 |
7 | #define EV_PROTOTYPES 1 |
|
|
8 | #define EV_H <ev.h> |
8 | #include "EV/EVAPI.h" |
9 | #include "EV/EVAPI.h" |
9 | |
10 | |
10 | /* fix perl api breakage */ |
11 | /* fix perl api breakage */ |
11 | #undef signal |
12 | #undef signal |
12 | #undef sigaction |
13 | #undef sigaction |
… | |
… | |
17 | # define NFDBITS PERL_NFDBITS |
18 | # define NFDBITS PERL_NFDBITS |
18 | # define fd_mask Perl_fd_mask |
19 | # define fd_mask Perl_fd_mask |
19 | #endif |
20 | #endif |
20 | /* due to bugs in OS X we have to use libev/ explicitly here */ |
21 | /* due to bugs in OS X we have to use libev/ explicitly here */ |
21 | #include "libev/ev.c" |
22 | #include "libev/ev.c" |
22 | #include "event.c" |
|
|
23 | |
23 | |
24 | #ifndef _WIN32 |
24 | #ifndef _WIN32 |
25 | # include <pthread.h> |
25 | # include <pthread.h> |
26 | #endif |
26 | #endif |
27 | |
27 | |
28 | #define WFLAG_KEEPALIVE 1 |
28 | #define WFLAG_KEEPALIVE 1 |
29 | |
29 | |
30 | #define UNREF(w) \ |
30 | #define UNREF(w) \ |
31 | if (!((w)->flags & WFLAG_KEEPALIVE) \ |
31 | if (!((w)->flags & WFLAG_KEEPALIVE) \ |
32 | && !ev_is_active (w)) \ |
32 | && !ev_is_active (w)) \ |
33 | ev_unref (); |
33 | ev_unref (w->loop); |
34 | |
34 | |
35 | #define REF(w) \ |
35 | #define REF(w) \ |
36 | if (!((w)->flags & WFLAG_KEEPALIVE) \ |
36 | if (!((w)->flags & WFLAG_KEEPALIVE) \ |
37 | && ev_is_active (w)) \ |
37 | && ev_is_active (w)) \ |
38 | ev_ref (); |
38 | ev_ref (w->loop); |
39 | |
39 | |
40 | #define START(type,w) \ |
40 | #define START(type,w) \ |
41 | do { \ |
41 | do { \ |
42 | UNREF (w); \ |
42 | UNREF (w); \ |
43 | ev_ ## type ## _start (w); \ |
43 | ev_ ## type ## _start (w->loop, w); \ |
44 | } while (0) |
44 | } while (0) |
45 | |
45 | |
46 | #define STOP(type,w) \ |
46 | #define STOP(type,w) \ |
47 | do { \ |
47 | do { \ |
48 | REF (w); \ |
48 | REF (w); \ |
49 | ev_ ## type ## _stop (w); \ |
49 | ev_ ## type ## _stop (w->loop, w); \ |
50 | } while (0) |
50 | } while (0) |
51 | |
51 | |
52 | #define RESET(type,w,seta) \ |
52 | #define RESET(type,w,seta) \ |
53 | do { \ |
53 | do { \ |
54 | int active = ev_is_active (w); \ |
54 | int active = ev_is_active (w); \ |
… | |
… | |
101 | } |
101 | } |
102 | |
102 | |
103 | ///////////////////////////////////////////////////////////////////////////// |
103 | ///////////////////////////////////////////////////////////////////////////// |
104 | // Event |
104 | // Event |
105 | |
105 | |
106 | static void e_cb (ev_watcher *w, int revents); |
106 | static void e_cb (EV_P_ ev_watcher *w, int revents); |
107 | |
107 | |
108 | static int |
108 | static int |
109 | sv_fileno (SV *fh) |
109 | sv_fileno (SV *fh) |
110 | { |
110 | { |
111 | SvGETMAGIC (fh); |
111 | SvGETMAGIC (fh); |
… | |
… | |
132 | |
132 | |
133 | w = (ev_watcher *)SvPVX (self); |
133 | w = (ev_watcher *)SvPVX (self); |
134 | |
134 | |
135 | ev_init (w, e_cb); |
135 | ev_init (w, e_cb); |
136 | |
136 | |
|
|
137 | w->loop = EV_DEFAULT; |
137 | w->flags = WFLAG_KEEPALIVE; |
138 | w->flags = WFLAG_KEEPALIVE; |
138 | w->data = 0; |
139 | w->data = 0; |
139 | w->fh = 0; |
140 | w->fh = 0; |
140 | w->cb_sv = newSVsv (cb_sv); |
141 | w->cb_sv = newSVsv (cb_sv); |
141 | w->self = self; |
142 | w->self = self; |
… | |
… | |
171 | } |
172 | } |
172 | |
173 | |
173 | static SV *sv_events_cache; |
174 | static SV *sv_events_cache; |
174 | |
175 | |
175 | static void |
176 | static void |
176 | e_cb (ev_watcher *w, int revents) |
177 | e_cb (EV_P_ ev_watcher *w, int revents) |
177 | { |
178 | { |
178 | dSP; |
179 | dSP; |
179 | I32 mark = SP - PL_stack_base; |
180 | I32 mark = SP - PL_stack_base; |
180 | SV *sv_self, *sv_events; |
181 | SV *sv_self, *sv_events; |
181 | |
182 | |
… | |
… | |
365 | stash_prepare = gv_stashpv ("EV::Prepare" , 1); |
366 | stash_prepare = gv_stashpv ("EV::Prepare" , 1); |
366 | stash_check = gv_stashpv ("EV::Check" , 1); |
367 | stash_check = gv_stashpv ("EV::Check" , 1); |
367 | stash_child = gv_stashpv ("EV::Child" , 1); |
368 | stash_child = gv_stashpv ("EV::Child" , 1); |
368 | stash_embed = gv_stashpv ("EV::Embed" , 1); |
369 | stash_embed = gv_stashpv ("EV::Embed" , 1); |
369 | stash_stat = gv_stashpv ("EV::Stat" , 1); |
370 | stash_stat = gv_stashpv ("EV::Stat" , 1); |
|
|
371 | stash_fork = gv_stashpv ("EV::Fork" , 1); |
370 | |
372 | |
371 | { |
373 | { |
372 | SV *sv = perl_get_sv ("EV::API", TRUE); |
374 | SV *sv = perl_get_sv ("EV::API", TRUE); |
373 | perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ |
375 | perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ |
374 | |
376 | |
375 | /* the poor man's shared library emulator */ |
377 | /* the poor man's shared library emulator */ |
376 | evapi.ver = EV_API_VERSION; |
378 | evapi.ver = EV_API_VERSION; |
377 | evapi.rev = EV_API_REVISION; |
379 | evapi.rev = EV_API_REVISION; |
378 | evapi.sv_fileno = sv_fileno; |
380 | evapi.sv_fileno = sv_fileno; |
379 | evapi.sv_signum = sv_signum; |
381 | evapi.sv_signum = sv_signum; |
|
|
382 | evapi.supported_backends = ev_supported_backends (); |
|
|
383 | evapi.recommended_backends = ev_recommended_backends (); |
|
|
384 | evapi.embeddable_backends = ev_embeddable_backends (); |
|
|
385 | evapi.time = ev_time; |
|
|
386 | evapi.loop_new = ev_loop_new; |
|
|
387 | evapi.loop_destroy = ev_loop_destroy; |
|
|
388 | evapi.loop_fork = ev_loop_fork; |
|
|
389 | evapi.loop_count = ev_loop_count; |
380 | evapi.now = ev_now; |
390 | evapi.now = ev_now; |
381 | evapi.backend = ev_backend; |
391 | evapi.backend = ev_backend; |
382 | evapi.unloop = ev_unloop; |
392 | evapi.unloop = ev_unloop; |
383 | evapi.ref = ev_ref; |
393 | evapi.ref = ev_ref; |
384 | evapi.unref = ev_unref; |
394 | evapi.unref = ev_unref; |
385 | evapi.time = ev_time; |
|
|
386 | evapi.loop = ev_loop; |
395 | evapi.loop = ev_loop; |
387 | evapi.once = ev_once; |
396 | evapi.once = ev_once; |
388 | evapi.io_start = ev_io_start; |
397 | evapi.io_start = ev_io_start; |
389 | evapi.io_stop = ev_io_stop; |
398 | evapi.io_stop = ev_io_stop; |
390 | evapi.timer_start = ev_timer_start; |
399 | evapi.timer_start = ev_timer_start; |
391 | evapi.timer_stop = ev_timer_stop; |
400 | evapi.timer_stop = ev_timer_stop; |
392 | evapi.timer_again = ev_timer_again; |
401 | evapi.timer_again = ev_timer_again; |
393 | evapi.periodic_start = ev_periodic_start; |
402 | evapi.periodic_start = ev_periodic_start; |
394 | evapi.periodic_stop = ev_periodic_stop; |
403 | evapi.periodic_stop = ev_periodic_stop; |
395 | evapi.signal_start = ev_signal_start; |
404 | evapi.signal_start = ev_signal_start; |
396 | evapi.signal_stop = ev_signal_stop; |
405 | evapi.signal_stop = ev_signal_stop; |
397 | evapi.idle_start = ev_idle_start; |
406 | evapi.idle_start = ev_idle_start; |
398 | evapi.idle_stop = ev_idle_stop; |
407 | evapi.idle_stop = ev_idle_stop; |
399 | evapi.prepare_start = ev_prepare_start; |
408 | evapi.prepare_start = ev_prepare_start; |
400 | evapi.prepare_stop = ev_prepare_stop; |
409 | evapi.prepare_stop = ev_prepare_stop; |
401 | evapi.check_start = ev_check_start; |
410 | evapi.check_start = ev_check_start; |
402 | evapi.check_stop = ev_check_stop; |
411 | evapi.check_stop = ev_check_stop; |
403 | evapi.child_start = ev_child_start; |
412 | evapi.child_start = ev_child_start; |
404 | evapi.child_stop = ev_child_stop; |
413 | evapi.child_stop = ev_child_stop; |
405 | evapi.stat_start = ev_stat_start; |
414 | evapi.stat_start = ev_stat_start; |
406 | evapi.stat_stop = ev_stat_stop; |
415 | evapi.stat_stop = ev_stat_stop; |
407 | evapi.stat_stat = ev_stat_stat; |
416 | evapi.stat_stat = ev_stat_stat; |
|
|
417 | evapi.embed_start = ev_embed_start; |
|
|
418 | evapi.embed_stop = ev_embed_stop; |
|
|
419 | evapi.embed_sweep = ev_embed_sweep; |
|
|
420 | evapi.fork_start = ev_fork_start; |
|
|
421 | evapi.fork_stop = ev_fork_stop; |
408 | evapi.clear_pending = ev_clear_pending; |
422 | evapi.clear_pending = ev_clear_pending; |
409 | evapi.invoke = ev_invoke; |
423 | evapi.invoke = ev_invoke; |
410 | |
424 | |
411 | sv_setiv (sv, (IV)&evapi); |
425 | sv_setiv (sv, (IV)&evapi); |
412 | SvREADONLY_on (sv); |
426 | SvREADONLY_on (sv); |
413 | } |
427 | } |
414 | #ifndef _WIN32 |
428 | #ifndef _WIN32 |
415 | pthread_atfork (0, 0, ev_default_fork); |
429 | pthread_atfork (0, 0, ev_default_fork); |
416 | #endif |
430 | #endif |
417 | } |
431 | } |
418 | |
432 | |
|
|
433 | SV *ev_default_loop (unsigned int flags = ev_supported_backends ()) |
|
|
434 | CODE: |
|
|
435 | { |
|
|
436 | evapi.default_loop = ev_default_loop (flags); |
|
|
437 | if (!evapi.default_loop) |
|
|
438 | XSRETURN_UNDEF; |
|
|
439 | |
|
|
440 | RETVAL = sv_bless (newRV_noinc (newSViv (PTR2IV (ev_default_loop (flags)))), |
|
|
441 | gv_stashpv ("EV::Loop::Default", 1)); |
|
|
442 | } |
|
|
443 | OUTPUT: |
|
|
444 | RETVAL |
|
|
445 | |
|
|
446 | NV ev_time () |
|
|
447 | |
419 | NV ev_now () |
448 | NV ev_now () |
|
|
449 | C_ARGS: evapi.default_loop |
420 | |
450 | |
421 | unsigned int ev_backend () |
451 | unsigned int ev_backend () |
422 | |
452 | C_ARGS: evapi.default_loop |
423 | NV ev_time () |
|
|
424 | |
|
|
425 | unsigned int ev_default_loop (unsigned int flags = ev_supported_backends ()) |
|
|
426 | |
453 | |
427 | unsigned int ev_loop_count () |
454 | unsigned int ev_loop_count () |
|
|
455 | C_ARGS: evapi.default_loop |
428 | |
456 | |
429 | void ev_loop (int flags = 0) |
457 | void ev_loop (int flags = 0) |
|
|
458 | C_ARGS: evapi.default_loop, flags |
430 | |
459 | |
431 | void ev_unloop (int how = 1) |
460 | void ev_unloop (int how = 1) |
|
|
461 | C_ARGS: evapi.default_loop, how |
432 | |
462 | |
433 | void ev_feed_fd_event (int fd, int revents = EV_NONE) |
463 | void ev_feed_fd_event (int fd, int revents = EV_NONE) |
|
|
464 | C_ARGS: evapi.default_loop, fd, revents |
434 | |
465 | |
435 | void ev_feed_signal_event (SV *signal) |
466 | void ev_feed_signal_event (SV *signal) |
436 | CODE: |
467 | CODE: |
437 | { |
468 | { |
438 | Signal signum = sv_signum (signal); |
469 | Signal signum = sv_signum (signal); |
439 | CHECK_SIG (signal, signum); |
470 | CHECK_SIG (signal, signum); |
440 | |
471 | |
441 | ev_feed_signal_event (EV_DEFAULT_ signum); |
472 | ev_feed_signal_event (evapi.default_loop, signum); |
442 | } |
473 | } |
443 | |
474 | |
444 | ev_io *io (SV *fh, int events, SV *cb) |
475 | ev_io *io (SV *fh, int events, SV *cb) |
445 | ALIAS: |
476 | ALIAS: |
446 | io_ns = 1 |
477 | io_ns = 1 |
… | |
… | |
529 | ev_check_set (RETVAL); |
560 | ev_check_set (RETVAL); |
530 | if (!ix) START (check, RETVAL); |
561 | if (!ix) START (check, RETVAL); |
531 | OUTPUT: |
562 | OUTPUT: |
532 | RETVAL |
563 | RETVAL |
533 | |
564 | |
|
|
565 | ev_fork *fork (SV *cb) |
|
|
566 | ALIAS: |
|
|
567 | fork_ns = 1 |
|
|
568 | CODE: |
|
|
569 | RETVAL = e_new (sizeof (ev_fork), cb); |
|
|
570 | ev_fork_set (RETVAL); |
|
|
571 | if (!ix) START (fork, RETVAL); |
|
|
572 | OUTPUT: |
|
|
573 | RETVAL |
|
|
574 | |
534 | ev_child *child (int pid, SV *cb) |
575 | ev_child *child (int pid, SV *cb) |
535 | ALIAS: |
576 | ALIAS: |
536 | child_ns = 1 |
577 | child_ns = 1 |
537 | CODE: |
578 | CODE: |
538 | RETVAL = e_new (sizeof (ev_child), cb); |
579 | RETVAL = e_new (sizeof (ev_child), cb); |
… | |
… | |
553 | RETVAL |
594 | RETVAL |
554 | |
595 | |
555 | void once (SV *fh, int events, SV *timeout, SV *cb) |
596 | void once (SV *fh, int events, SV *timeout, SV *cb) |
556 | CODE: |
597 | CODE: |
557 | ev_once ( |
598 | ev_once ( |
|
|
599 | evapi.default_loop, |
558 | sv_fileno (fh), events, |
600 | sv_fileno (fh), events, |
559 | SvOK (timeout) ? SvNV (timeout) : -1., |
601 | SvOK (timeout) ? SvNV (timeout) : -1., |
560 | e_once_cb, |
602 | e_once_cb, |
561 | newSVsv (cb) |
603 | newSVsv (cb) |
562 | ); |
604 | ); |
… | |
… | |
568 | int ev_is_active (ev_watcher *w) |
610 | int ev_is_active (ev_watcher *w) |
569 | |
611 | |
570 | int ev_is_pending (ev_watcher *w) |
612 | int ev_is_pending (ev_watcher *w) |
571 | |
613 | |
572 | void ev_invoke (ev_watcher *w, int revents = EV_NONE) |
614 | void ev_invoke (ev_watcher *w, int revents = EV_NONE) |
|
|
615 | C_ARGS: w->loop, w, revents |
573 | |
616 | |
574 | int ev_clear_pending (ev_watcher *w) |
617 | int ev_clear_pending (ev_watcher *w) |
|
|
618 | C_ARGS: w->loop, w |
575 | |
619 | |
576 | void ev_feed_event (ev_watcher *w, int revents = EV_NONE) |
620 | void ev_feed_event (ev_watcher *w, int revents = EV_NONE) |
|
|
621 | C_ARGS: w->loop, w, revents |
577 | |
622 | |
578 | int keepalive (ev_watcher *w, int new_value = 0) |
623 | int keepalive (ev_watcher *w, int new_value = 0) |
579 | CODE: |
624 | CODE: |
580 | { |
625 | { |
581 | RETVAL = w->flags & WFLAG_KEEPALIVE; |
626 | RETVAL = w->flags & WFLAG_KEEPALIVE; |
… | |
… | |
758 | void ev_timer_again (ev_timer *w) |
803 | void ev_timer_again (ev_timer *w) |
759 | INIT: |
804 | INIT: |
760 | CHECK_REPEAT (w->repeat); |
805 | CHECK_REPEAT (w->repeat); |
761 | CODE: |
806 | CODE: |
762 | REF (w); |
807 | REF (w); |
763 | ev_timer_again (w); |
808 | ev_timer_again (w->loop, w); |
764 | UNREF (w); |
809 | UNREF (w); |
765 | |
810 | |
766 | void DESTROY (ev_timer *w) |
811 | void DESTROY (ev_timer *w) |
767 | CODE: |
812 | CODE: |
768 | STOP (timer, w); |
813 | STOP (timer, w); |
… | |
… | |
793 | STOP (periodic, w); |
838 | STOP (periodic, w); |
794 | |
839 | |
795 | void ev_periodic_again (ev_periodic *w) |
840 | void ev_periodic_again (ev_periodic *w) |
796 | CODE: |
841 | CODE: |
797 | REF (w); |
842 | REF (w); |
798 | ev_periodic_again (w); |
843 | ev_periodic_again (w->loop, w); |
799 | UNREF (w); |
844 | UNREF (w); |
800 | |
845 | |
801 | void DESTROY (ev_periodic *w) |
846 | void DESTROY (ev_periodic *w) |
802 | CODE: |
847 | CODE: |
803 | STOP (periodic, w); |
848 | STOP (periodic, w); |
… | |
… | |
861 | STOP (check, w); |
906 | STOP (check, w); |
862 | |
907 | |
863 | void DESTROY (ev_check *w) |
908 | void DESTROY (ev_check *w) |
864 | CODE: |
909 | CODE: |
865 | STOP (check, w); |
910 | STOP (check, w); |
|
|
911 | e_destroy (w); |
|
|
912 | |
|
|
913 | MODULE = EV PACKAGE = EV::Fork PREFIX = ev_fork_ |
|
|
914 | |
|
|
915 | void ev_fork_start (ev_fork *w) |
|
|
916 | CODE: |
|
|
917 | START (fork, w); |
|
|
918 | |
|
|
919 | void ev_fork_stop (ev_fork *w) |
|
|
920 | CODE: |
|
|
921 | STOP (fork, w); |
|
|
922 | |
|
|
923 | void DESTROY (ev_fork *w) |
|
|
924 | CODE: |
|
|
925 | STOP (fork, w); |
866 | e_destroy (w); |
926 | e_destroy (w); |
867 | |
927 | |
868 | MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ |
928 | MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ |
869 | |
929 | |
870 | void ev_child_start (ev_child *w) |
930 | void ev_child_start (ev_child *w) |
… | |
… | |
959 | PPCODE: |
1019 | PPCODE: |
960 | { |
1020 | { |
961 | ev_statdata *s = ix ? &w->attr : &w->prev; |
1021 | ev_statdata *s = ix ? &w->attr : &w->prev; |
962 | |
1022 | |
963 | if (ix == 1) |
1023 | if (ix == 1) |
964 | ev_stat_stat (w); |
1024 | ev_stat_stat (w->loop, w); |
965 | else if (!s->st_nlink) |
1025 | else if (!s->st_nlink) |
966 | errno = ENOENT; |
1026 | errno = ENOENT; |
967 | |
1027 | |
968 | PL_statcache.st_dev = s->st_nlink; |
1028 | PL_statcache.st_dev = s->st_nlink; |
969 | PL_statcache.st_ino = s->st_ino; |
1029 | PL_statcache.st_ino = s->st_ino; |