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 <netinet/in.h>*/ |
|
|
6 | |
|
|
7 | /* fix perl api breakage */ |
5 | /* fix perl api breakage */ |
8 | #undef signal |
6 | #undef signal |
9 | #undef sigaction |
7 | #undef sigaction |
|
|
8 | |
|
|
9 | #include "schmorp.h" |
10 | |
10 | |
11 | #define EV_PROTOTYPES 1 |
11 | #define EV_PROTOTYPES 1 |
12 | #define EV_USE_NANOSLEEP EV_USE_MONOTONIC |
12 | #define EV_USE_NANOSLEEP EV_USE_MONOTONIC |
13 | #define EV_H <ev.h> |
13 | #define EV_H <ev.h> |
14 | #include "EV/EVAPI.h" |
14 | #include "EV/EVAPI.h" |
… | |
… | |
22 | /* due to bugs in OS X we have to use libev/ explicitly here */ |
22 | /* due to bugs in OS X we have to use libev/ explicitly here */ |
23 | #include "libev/ev.c" |
23 | #include "libev/ev.c" |
24 | |
24 | |
25 | #ifndef _WIN32 |
25 | #ifndef _WIN32 |
26 | # include <pthread.h> |
26 | # include <pthread.h> |
27 | #endif |
|
|
28 | |
|
|
29 | /* 5.10.0 */ |
|
|
30 | #ifndef SvREFCNT_inc_NN |
|
|
31 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
32 | #endif |
|
|
33 | |
|
|
34 | /* 5.6.x */ |
|
|
35 | #ifndef SvRV_set |
|
|
36 | # define SvRV_set(a,b) SvRV ((a)) = (b) |
|
|
37 | #endif |
27 | #endif |
38 | |
28 | |
39 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) |
29 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) |
40 | |
30 | |
41 | #define WFLAG_KEEPALIVE 1 |
31 | #define WFLAG_KEEPALIVE 1 |
… | |
… | |
96 | *stash_check, |
86 | *stash_check, |
97 | *stash_embed, |
87 | *stash_embed, |
98 | *stash_fork, |
88 | *stash_fork, |
99 | *stash_async; |
89 | *stash_async; |
100 | |
90 | |
101 | #ifndef SIG_SIZE |
|
|
102 | /* kudos to Slaven Rezic for the idea */ |
|
|
103 | static char sig_size [] = { SIG_NUM }; |
|
|
104 | # define SIG_SIZE (sizeof (sig_size) + 1) |
|
|
105 | #endif |
|
|
106 | |
|
|
107 | static Signal |
|
|
108 | sv_signum (SV *sig) |
|
|
109 | { |
|
|
110 | Signal signum; |
|
|
111 | |
|
|
112 | SvGETMAGIC (sig); |
|
|
113 | |
|
|
114 | for (signum = 1; signum < SIG_SIZE; ++signum) |
|
|
115 | if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
|
|
116 | return signum; |
|
|
117 | |
|
|
118 | signum = SvIV (sig); |
|
|
119 | |
|
|
120 | if (signum > 0 && signum < SIG_SIZE) |
|
|
121 | return signum; |
|
|
122 | |
|
|
123 | return -1; |
|
|
124 | } |
|
|
125 | |
|
|
126 | ///////////////////////////////////////////////////////////////////////////// |
91 | ///////////////////////////////////////////////////////////////////////////// |
127 | // Event |
92 | // Event |
128 | |
93 | |
129 | static void e_cb (EV_P_ ev_watcher *w, int revents); |
94 | static void e_cb (EV_P_ ev_watcher *w, int revents); |
130 | |
95 | |
131 | static int |
|
|
132 | sv_fileno (SV *fh) |
|
|
133 | { |
|
|
134 | SvGETMAGIC (fh); |
|
|
135 | |
|
|
136 | if (SvROK (fh)) |
|
|
137 | fh = SvRV (fh); |
|
|
138 | |
|
|
139 | if (SvTYPE (fh) == SVt_PVGV) |
|
|
140 | return PerlIO_fileno (IoIFP (sv_2io (fh))); |
|
|
141 | |
|
|
142 | if (SvOK (fh) && (SvIV (fh) >= 0) && (SvIV (fh) < 0x7fffffffL)) |
|
|
143 | return SvIV (fh); |
|
|
144 | |
|
|
145 | return -1; |
|
|
146 | } |
|
|
147 | |
|
|
148 | static SV * |
|
|
149 | e_get_cv (SV *cb_sv) |
|
|
150 | { |
|
|
151 | HV *st; |
|
|
152 | GV *gvp; |
|
|
153 | CV *cv = sv_2cv (cb_sv, &st, &gvp, 0); |
|
|
154 | |
|
|
155 | if (!cv) |
|
|
156 | croak ("EV watcher callback must be a CODE reference"); |
|
|
157 | |
|
|
158 | return (SV *)cv; |
|
|
159 | } |
|
|
160 | |
|
|
161 | static void * |
96 | static void * |
162 | e_new (int size, SV *cb_sv, SV *loop) |
97 | e_new (int size, SV *cb_sv, SV *loop) |
163 | { |
98 | { |
164 | SV *cv = cb_sv ? e_get_cv (cb_sv) : 0; |
99 | SV *cv = cb_sv ? s_get_cv_croak (cb_sv) : 0; |
165 | ev_watcher *w; |
100 | ev_watcher *w; |
166 | SV *self = NEWSV (0, size); |
101 | SV *self = NEWSV (0, size); |
167 | SvPOK_only (self); |
102 | SvPOK_only (self); |
168 | SvCUR_set (self, size); |
103 | SvCUR_set (self, size); |
169 | |
104 | |
… | |
… | |
454 | perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ |
389 | perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ |
455 | |
390 | |
456 | /* the poor man's shared library emulator */ |
391 | /* the poor man's shared library emulator */ |
457 | evapi.ver = EV_API_VERSION; |
392 | evapi.ver = EV_API_VERSION; |
458 | evapi.rev = EV_API_REVISION; |
393 | evapi.rev = EV_API_REVISION; |
459 | evapi.sv_fileno = sv_fileno; |
394 | evapi.sv_fileno = s_fileno; |
460 | evapi.sv_signum = sv_signum; |
395 | evapi.sv_signum = s_signum; |
461 | evapi.supported_backends = ev_supported_backends (); |
396 | evapi.supported_backends = ev_supported_backends (); |
462 | evapi.recommended_backends = ev_recommended_backends (); |
397 | evapi.recommended_backends = ev_recommended_backends (); |
463 | evapi.embeddable_backends = ev_embeddable_backends (); |
398 | evapi.embeddable_backends = ev_embeddable_backends (); |
464 | evapi.time_ = ev_time; |
399 | evapi.time_ = ev_time; |
465 | evapi.sleep_ = ev_sleep; |
400 | evapi.sleep_ = ev_sleep; |
466 | evapi.loop_new = ev_loop_new; |
401 | evapi.loop_new = ev_loop_new; |
467 | evapi.loop_destroy = ev_loop_destroy; |
402 | evapi.loop_destroy = ev_loop_destroy; |
468 | evapi.loop_fork = ev_loop_fork; |
403 | evapi.loop_fork = ev_loop_fork; |
469 | evapi.loop_count = ev_loop_count; |
404 | evapi.loop_count = ev_loop_count; |
470 | evapi.loop_depth = ev_loop_depth; |
405 | evapi.loop_depth = ev_loop_depth; |
|
|
406 | evapi.set_userdata = ev_set_userdata; |
|
|
407 | evapi.userdata = ev_userdata; |
471 | evapi.now = ev_now; |
408 | evapi.now = ev_now; |
472 | evapi.now_update = ev_now_update; |
409 | evapi.now_update = ev_now_update; |
473 | evapi.suspend = ev_suspend; |
410 | evapi.suspend = ev_suspend; |
474 | evapi.resume = ev_resume; |
411 | evapi.resume = ev_resume; |
475 | evapi.backend = ev_backend; |
412 | evapi.backend = ev_backend; |
476 | evapi.unloop = ev_unloop; |
413 | evapi.unloop = ev_unloop; |
|
|
414 | evapi.invoke_pending = ev_invoke_pending; |
|
|
415 | evapi.set_loop_release_cb = ev_set_loop_release_cb; |
|
|
416 | evapi.set_invoke_pending_cb= ev_set_invoke_pending_cb; |
477 | evapi.ref = ev_ref; |
417 | evapi.ref = ev_ref; |
478 | evapi.unref = ev_unref; |
418 | evapi.unref = ev_unref; |
479 | evapi.loop = ev_loop; |
419 | evapi.loop = ev_loop; |
480 | evapi.once = ev_once; |
420 | evapi.once = ev_once; |
481 | evapi.io_start = ev_io_start; |
421 | evapi.io_start = ev_io_start; |
… | |
… | |
591 | C_ARGS: evapi.default_loop, fd, revents |
531 | C_ARGS: evapi.default_loop, fd, revents |
592 | |
532 | |
593 | void ev_feed_signal_event (SV *signal) |
533 | void ev_feed_signal_event (SV *signal) |
594 | CODE: |
534 | CODE: |
595 | { |
535 | { |
596 | Signal signum = sv_signum (signal); |
536 | Signal signum = s_signum (signal); |
597 | CHECK_SIG (signal, signum); |
537 | CHECK_SIG (signal, signum); |
598 | |
538 | |
599 | ev_feed_signal_event (evapi.default_loop, signum); |
539 | ev_feed_signal_event (evapi.default_loop, signum); |
600 | } |
540 | } |
601 | |
541 | |
602 | ev_io *io (SV *fh, int events, SV *cb) |
542 | ev_io *io (SV *fh, int events, SV *cb) |
603 | ALIAS: |
543 | ALIAS: |
604 | io_ns = 1 |
544 | io_ns = 1 |
605 | CODE: |
545 | CODE: |
606 | { |
546 | { |
607 | int fd = sv_fileno (fh); |
547 | int fd = s_fileno (fh, events & EV_WRITE); |
608 | CHECK_FD (fh, fd); |
548 | CHECK_FD (fh, fd); |
609 | |
549 | |
610 | RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); |
550 | RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); |
611 | RETVAL->fh = newSVsv (fh); |
551 | RETVAL->fh = newSVsv (fh); |
612 | ev_io_set (RETVAL, fd, events); |
552 | ev_io_set (RETVAL, fd, events); |
… | |
… | |
647 | ev_signal *signal (SV *signal, SV *cb) |
587 | ev_signal *signal (SV *signal, SV *cb) |
648 | ALIAS: |
588 | ALIAS: |
649 | signal_ns = 1 |
589 | signal_ns = 1 |
650 | CODE: |
590 | CODE: |
651 | { |
591 | { |
652 | Signal signum = sv_signum (signal); |
592 | Signal signum = s_signum (signal); |
653 | CHECK_SIG (signal, signum); |
593 | CHECK_SIG (signal, signum); |
654 | |
594 | |
655 | RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); |
595 | RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); |
656 | ev_signal_set (RETVAL, signum); |
596 | ev_signal_set (RETVAL, signum); |
657 | if (!ix) START (signal, RETVAL); |
597 | if (!ix) START (signal, RETVAL); |
… | |
… | |
748 | |
688 | |
749 | void once (SV *fh, int events, SV *timeout, SV *cb) |
689 | void once (SV *fh, int events, SV *timeout, SV *cb) |
750 | CODE: |
690 | CODE: |
751 | ev_once ( |
691 | ev_once ( |
752 | evapi.default_loop, |
692 | evapi.default_loop, |
753 | sv_fileno (fh), events, |
693 | s_fileno (fh, events & EV_WRITE), events, |
754 | SvOK (timeout) ? SvNV (timeout) : -1., |
694 | SvOK (timeout) ? SvNV (timeout) : -1., |
755 | e_once_cb, |
695 | e_once_cb, |
756 | newSVsv (cb) |
696 | newSVsv (cb) |
757 | ); |
697 | ); |
758 | |
698 | |
… | |
… | |
792 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
732 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
793 | CODE: |
733 | CODE: |
794 | { |
734 | { |
795 | if (items > 1) |
735 | if (items > 1) |
796 | { |
736 | { |
797 | new_cb = e_get_cv (new_cb); |
737 | new_cb = s_get_cv_croak (new_cb); |
798 | RETVAL = newRV_noinc (w->cb_sv); |
738 | RETVAL = newRV_noinc (w->cb_sv); |
799 | w->cb_sv = SvREFCNT_inc (new_cb); |
739 | w->cb_sv = SvREFCNT_inc (new_cb); |
800 | } |
740 | } |
801 | else |
741 | else |
802 | RETVAL = newRV_inc (w->cb_sv); |
742 | RETVAL = newRV_inc (w->cb_sv); |
… | |
… | |
872 | e_destroy (w); |
812 | e_destroy (w); |
873 | |
813 | |
874 | void set (ev_io *w, SV *fh, int events) |
814 | void set (ev_io *w, SV *fh, int events) |
875 | CODE: |
815 | CODE: |
876 | { |
816 | { |
877 | int fd = sv_fileno (fh); |
817 | int fd = s_fileno (fh, events & EV_WRITE); |
878 | CHECK_FD (fh, fd); |
818 | CHECK_FD (fh, fd); |
879 | |
819 | |
880 | sv_setsv (w->fh, fh); |
820 | sv_setsv (w->fh, fh); |
881 | RESET (io, w, (w, fd, events)); |
821 | RESET (io, w, (w, fd, events)); |
882 | } |
822 | } |
… | |
… | |
884 | SV *fh (ev_io *w, SV *new_fh = 0) |
824 | SV *fh (ev_io *w, SV *new_fh = 0) |
885 | CODE: |
825 | CODE: |
886 | { |
826 | { |
887 | if (items > 1) |
827 | if (items > 1) |
888 | { |
828 | { |
889 | int fd = sv_fileno (new_fh); |
829 | int fd = s_fileno (new_fh, w->events & EV_WRITE); |
890 | CHECK_FD (new_fh, fd); |
830 | CHECK_FD (new_fh, fd); |
891 | |
831 | |
892 | RETVAL = w->fh; |
832 | RETVAL = w->fh; |
893 | w->fh = newSVsv (new_fh); |
833 | w->fh = newSVsv (new_fh); |
894 | |
834 | |
… | |
… | |
927 | e_destroy (w); |
867 | e_destroy (w); |
928 | |
868 | |
929 | void set (ev_signal *w, SV *signal) |
869 | void set (ev_signal *w, SV *signal) |
930 | CODE: |
870 | CODE: |
931 | { |
871 | { |
932 | Signal signum = sv_signum (signal); |
872 | Signal signum = s_signum (signal); |
933 | CHECK_SIG (signal, signum); |
873 | CHECK_SIG (signal, signum); |
934 | |
874 | |
935 | RESET (signal, w, (w, signum)); |
875 | RESET (signal, w, (w, signum)); |
936 | } |
876 | } |
937 | |
877 | |
… | |
… | |
940 | { |
880 | { |
941 | RETVAL = w->signum; |
881 | RETVAL = w->signum; |
942 | |
882 | |
943 | if (items > 1) |
883 | if (items > 1) |
944 | { |
884 | { |
945 | Signal signum = sv_signum (new_signal); |
885 | Signal signum = s_signum (new_signal); |
946 | CHECK_SIG (new_signal, signum); |
886 | CHECK_SIG (new_signal, signum); |
947 | |
887 | |
948 | RESET (signal, w, (w, signum)); |
888 | RESET (signal, w, (w, signum)); |
949 | } |
889 | } |
950 | } |
890 | } |
… | |
… | |
1310 | #if 0 |
1250 | #if 0 |
1311 | |
1251 | |
1312 | void ev_feed_signal_event (struct ev_loop *loop, SV *signal) |
1252 | void ev_feed_signal_event (struct ev_loop *loop, SV *signal) |
1313 | CODE: |
1253 | CODE: |
1314 | { |
1254 | { |
1315 | Signal signum = sv_signum (signal); |
1255 | Signal signum = s_signum (signal); |
1316 | CHECK_SIG (signal, signum); |
1256 | CHECK_SIG (signal, signum); |
1317 | |
1257 | |
1318 | ev_feed_signal_event (loop, signum); |
1258 | ev_feed_signal_event (loop, signum); |
1319 | } |
1259 | } |
1320 | |
1260 | |
… | |
… | |
1323 | ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb) |
1263 | ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb) |
1324 | ALIAS: |
1264 | ALIAS: |
1325 | io_ns = 1 |
1265 | io_ns = 1 |
1326 | CODE: |
1266 | CODE: |
1327 | { |
1267 | { |
1328 | int fd = sv_fileno (fh); |
1268 | int fd = s_fileno (fh, events & EV_WRITE); |
1329 | CHECK_FD (fh, fd); |
1269 | CHECK_FD (fh, fd); |
1330 | |
1270 | |
1331 | RETVAL = e_new (sizeof (ev_io), cb, ST (0)); |
1271 | RETVAL = e_new (sizeof (ev_io), cb, ST (0)); |
1332 | RETVAL->fh = newSVsv (fh); |
1272 | RETVAL->fh = newSVsv (fh); |
1333 | ev_io_set (RETVAL, fd, events); |
1273 | ev_io_set (RETVAL, fd, events); |
… | |
… | |
1370 | ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) |
1310 | ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) |
1371 | ALIAS: |
1311 | ALIAS: |
1372 | signal_ns = 1 |
1312 | signal_ns = 1 |
1373 | CODE: |
1313 | CODE: |
1374 | { |
1314 | { |
1375 | Signal signum = sv_signum (signal); |
1315 | Signal signum = s_signum (signal); |
1376 | CHECK_SIG (signal, signum); |
1316 | CHECK_SIG (signal, signum); |
1377 | |
1317 | |
1378 | RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); |
1318 | RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); |
1379 | ev_signal_set (RETVAL, signum); |
1319 | ev_signal_set (RETVAL, signum); |
1380 | if (!ix) START (signal, RETVAL); |
1320 | if (!ix) START (signal, RETVAL); |
… | |
… | |
1473 | |
1413 | |
1474 | void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb) |
1414 | void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb) |
1475 | CODE: |
1415 | CODE: |
1476 | ev_once ( |
1416 | ev_once ( |
1477 | loop, |
1417 | loop, |
1478 | sv_fileno (fh), events, |
1418 | s_fileno (fh, events & EV_WRITE), events, |
1479 | SvOK (timeout) ? SvNV (timeout) : -1., |
1419 | SvOK (timeout) ? SvNV (timeout) : -1., |
1480 | e_once_cb, |
1420 | e_once_cb, |
1481 | newSVsv (cb) |
1421 | newSVsv (cb) |
1482 | ); |
1422 | ); |
1483 | |
1423 | |