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 | |
|
|
11 | /* old API compatibility */ |
|
|
12 | static int |
|
|
13 | sv_fileno (SV *fh) |
|
|
14 | { |
|
|
15 | return s_fileno (fh, 0); |
|
|
16 | } |
10 | |
17 | |
11 | #define EV_PROTOTYPES 1 |
18 | #define EV_PROTOTYPES 1 |
12 | #define EV_USE_NANOSLEEP EV_USE_MONOTONIC |
19 | #define EV_USE_NANOSLEEP EV_USE_MONOTONIC |
13 | #define EV_H <ev.h> |
20 | #define EV_H <ev.h> |
14 | #include "EV/EVAPI.h" |
21 | #include "EV/EVAPI.h" |
… | |
… | |
22 | /* due to bugs in OS X we have to use libev/ explicitly here */ |
29 | /* due to bugs in OS X we have to use libev/ explicitly here */ |
23 | #include "libev/ev.c" |
30 | #include "libev/ev.c" |
24 | |
31 | |
25 | #ifndef _WIN32 |
32 | #ifndef _WIN32 |
26 | # include <pthread.h> |
33 | # 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 |
34 | #endif |
38 | |
35 | |
39 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) |
36 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) |
40 | |
37 | |
41 | #define WFLAG_KEEPALIVE 1 |
38 | #define WFLAG_KEEPALIVE 1 |
… | |
… | |
96 | *stash_check, |
93 | *stash_check, |
97 | *stash_embed, |
94 | *stash_embed, |
98 | *stash_fork, |
95 | *stash_fork, |
99 | *stash_async; |
96 | *stash_async; |
100 | |
97 | |
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 | ///////////////////////////////////////////////////////////////////////////// |
98 | ///////////////////////////////////////////////////////////////////////////// |
127 | // Event |
99 | // Event |
128 | |
100 | |
129 | static void e_cb (EV_P_ ev_watcher *w, int revents); |
101 | static void e_cb (EV_P_ ev_watcher *w, int revents); |
130 | |
102 | |
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 * |
103 | static void * |
162 | e_new (int size, SV *cb_sv, SV *loop) |
104 | e_new (int size, SV *cb_sv, SV *loop) |
163 | { |
105 | { |
164 | SV *cv = cb_sv ? e_get_cv (cb_sv) : 0; |
106 | SV *cv = cb_sv ? s_get_cv_croak (cb_sv) : 0; |
165 | ev_watcher *w; |
107 | ev_watcher *w; |
166 | SV *self = NEWSV (0, size); |
108 | SV *self = NEWSV (0, size); |
167 | SvPOK_only (self); |
109 | SvPOK_only (self); |
168 | SvCUR_set (self, size); |
110 | SvCUR_set (self, size); |
169 | |
111 | |
… | |
… | |
455 | |
397 | |
456 | /* the poor man's shared library emulator */ |
398 | /* the poor man's shared library emulator */ |
457 | evapi.ver = EV_API_VERSION; |
399 | evapi.ver = EV_API_VERSION; |
458 | evapi.rev = EV_API_REVISION; |
400 | evapi.rev = EV_API_REVISION; |
459 | evapi.sv_fileno = sv_fileno; |
401 | evapi.sv_fileno = sv_fileno; |
460 | evapi.sv_signum = sv_signum; |
402 | evapi.sv_signum = s_signum; |
461 | evapi.supported_backends = ev_supported_backends (); |
403 | evapi.supported_backends = ev_supported_backends (); |
462 | evapi.recommended_backends = ev_recommended_backends (); |
404 | evapi.recommended_backends = ev_recommended_backends (); |
463 | evapi.embeddable_backends = ev_embeddable_backends (); |
405 | evapi.embeddable_backends = ev_embeddable_backends (); |
464 | evapi.time_ = ev_time; |
406 | evapi.time_ = ev_time; |
465 | evapi.sleep_ = ev_sleep; |
407 | evapi.sleep_ = ev_sleep; |
466 | evapi.loop_new = ev_loop_new; |
408 | evapi.loop_new = ev_loop_new; |
467 | evapi.loop_destroy = ev_loop_destroy; |
409 | evapi.loop_destroy = ev_loop_destroy; |
468 | evapi.loop_fork = ev_loop_fork; |
410 | evapi.loop_fork = ev_loop_fork; |
469 | evapi.loop_count = ev_loop_count; |
411 | evapi.loop_count = ev_loop_count; |
470 | evapi.loop_depth = ev_loop_depth; |
412 | evapi.loop_depth = ev_loop_depth; |
|
|
413 | evapi.set_userdata = ev_set_userdata; |
|
|
414 | evapi.userdata = ev_userdata; |
471 | evapi.now = ev_now; |
415 | evapi.now = ev_now; |
472 | evapi.now_update = ev_now_update; |
416 | evapi.now_update = ev_now_update; |
473 | evapi.suspend = ev_suspend; |
417 | evapi.suspend = ev_suspend; |
474 | evapi.resume = ev_resume; |
418 | evapi.resume = ev_resume; |
475 | evapi.backend = ev_backend; |
419 | evapi.backend = ev_backend; |
476 | evapi.unloop = ev_unloop; |
420 | evapi.unloop = ev_unloop; |
|
|
421 | evapi.invoke_pending = ev_invoke_pending; |
|
|
422 | evapi.pending_count = ev_pending_count; |
|
|
423 | evapi.set_loop_release_cb = ev_set_loop_release_cb; |
|
|
424 | evapi.set_invoke_pending_cb= ev_set_invoke_pending_cb; |
477 | evapi.ref = ev_ref; |
425 | evapi.ref = ev_ref; |
478 | evapi.unref = ev_unref; |
426 | evapi.unref = ev_unref; |
479 | evapi.loop = ev_loop; |
427 | evapi.loop = ev_loop; |
480 | evapi.once = ev_once; |
428 | evapi.once = ev_once; |
481 | evapi.io_start = ev_io_start; |
429 | evapi.io_start = ev_io_start; |
… | |
… | |
591 | C_ARGS: evapi.default_loop, fd, revents |
539 | C_ARGS: evapi.default_loop, fd, revents |
592 | |
540 | |
593 | void ev_feed_signal_event (SV *signal) |
541 | void ev_feed_signal_event (SV *signal) |
594 | CODE: |
542 | CODE: |
595 | { |
543 | { |
596 | Signal signum = sv_signum (signal); |
544 | Signal signum = s_signum (signal); |
597 | CHECK_SIG (signal, signum); |
545 | CHECK_SIG (signal, signum); |
598 | |
546 | |
599 | ev_feed_signal_event (evapi.default_loop, signum); |
547 | ev_feed_signal_event (evapi.default_loop, signum); |
600 | } |
548 | } |
601 | |
549 | |
602 | ev_io *io (SV *fh, int events, SV *cb) |
550 | ev_io *io (SV *fh, int events, SV *cb) |
603 | ALIAS: |
551 | ALIAS: |
604 | io_ns = 1 |
552 | io_ns = 1 |
605 | CODE: |
553 | CODE: |
606 | { |
554 | { |
607 | int fd = sv_fileno (fh); |
555 | int fd = s_fileno (fh, events & EV_WRITE); |
608 | CHECK_FD (fh, fd); |
556 | CHECK_FD (fh, fd); |
609 | |
557 | |
610 | RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); |
558 | RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); |
611 | RETVAL->fh = newSVsv (fh); |
559 | RETVAL->fh = newSVsv (fh); |
612 | ev_io_set (RETVAL, fd, events); |
560 | ev_io_set (RETVAL, fd, events); |
… | |
… | |
647 | ev_signal *signal (SV *signal, SV *cb) |
595 | ev_signal *signal (SV *signal, SV *cb) |
648 | ALIAS: |
596 | ALIAS: |
649 | signal_ns = 1 |
597 | signal_ns = 1 |
650 | CODE: |
598 | CODE: |
651 | { |
599 | { |
652 | Signal signum = sv_signum (signal); |
600 | Signal signum = s_signum (signal); |
653 | CHECK_SIG (signal, signum); |
601 | CHECK_SIG (signal, signum); |
654 | |
602 | |
655 | RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); |
603 | RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); |
656 | ev_signal_set (RETVAL, signum); |
604 | ev_signal_set (RETVAL, signum); |
657 | if (!ix) START (signal, RETVAL); |
605 | if (!ix) START (signal, RETVAL); |
… | |
… | |
748 | |
696 | |
749 | void once (SV *fh, int events, SV *timeout, SV *cb) |
697 | void once (SV *fh, int events, SV *timeout, SV *cb) |
750 | CODE: |
698 | CODE: |
751 | ev_once ( |
699 | ev_once ( |
752 | evapi.default_loop, |
700 | evapi.default_loop, |
753 | sv_fileno (fh), events, |
701 | s_fileno (fh, events & EV_WRITE), events, |
754 | SvOK (timeout) ? SvNV (timeout) : -1., |
702 | SvOK (timeout) ? SvNV (timeout) : -1., |
755 | e_once_cb, |
703 | e_once_cb, |
756 | newSVsv (cb) |
704 | newSVsv (cb) |
757 | ); |
705 | ); |
758 | |
706 | |
… | |
… | |
792 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
740 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
793 | CODE: |
741 | CODE: |
794 | { |
742 | { |
795 | if (items > 1) |
743 | if (items > 1) |
796 | { |
744 | { |
797 | new_cb = e_get_cv (new_cb); |
745 | new_cb = s_get_cv_croak (new_cb); |
798 | RETVAL = newRV_noinc (w->cb_sv); |
746 | RETVAL = newRV_noinc (w->cb_sv); |
799 | w->cb_sv = SvREFCNT_inc (new_cb); |
747 | w->cb_sv = SvREFCNT_inc (new_cb); |
800 | } |
748 | } |
801 | else |
749 | else |
802 | RETVAL = newRV_inc (w->cb_sv); |
750 | RETVAL = newRV_inc (w->cb_sv); |
… | |
… | |
872 | e_destroy (w); |
820 | e_destroy (w); |
873 | |
821 | |
874 | void set (ev_io *w, SV *fh, int events) |
822 | void set (ev_io *w, SV *fh, int events) |
875 | CODE: |
823 | CODE: |
876 | { |
824 | { |
877 | int fd = sv_fileno (fh); |
825 | int fd = s_fileno (fh, events & EV_WRITE); |
878 | CHECK_FD (fh, fd); |
826 | CHECK_FD (fh, fd); |
879 | |
827 | |
880 | sv_setsv (w->fh, fh); |
828 | sv_setsv (w->fh, fh); |
881 | RESET (io, w, (w, fd, events)); |
829 | RESET (io, w, (w, fd, events)); |
882 | } |
830 | } |
… | |
… | |
884 | SV *fh (ev_io *w, SV *new_fh = 0) |
832 | SV *fh (ev_io *w, SV *new_fh = 0) |
885 | CODE: |
833 | CODE: |
886 | { |
834 | { |
887 | if (items > 1) |
835 | if (items > 1) |
888 | { |
836 | { |
889 | int fd = sv_fileno (new_fh); |
837 | int fd = s_fileno (new_fh, w->events & EV_WRITE); |
890 | CHECK_FD (new_fh, fd); |
838 | CHECK_FD (new_fh, fd); |
891 | |
839 | |
892 | RETVAL = w->fh; |
840 | RETVAL = w->fh; |
893 | w->fh = newSVsv (new_fh); |
841 | w->fh = newSVsv (new_fh); |
894 | |
842 | |
… | |
… | |
927 | e_destroy (w); |
875 | e_destroy (w); |
928 | |
876 | |
929 | void set (ev_signal *w, SV *signal) |
877 | void set (ev_signal *w, SV *signal) |
930 | CODE: |
878 | CODE: |
931 | { |
879 | { |
932 | Signal signum = sv_signum (signal); |
880 | Signal signum = s_signum (signal); |
933 | CHECK_SIG (signal, signum); |
881 | CHECK_SIG (signal, signum); |
934 | |
882 | |
935 | RESET (signal, w, (w, signum)); |
883 | RESET (signal, w, (w, signum)); |
936 | } |
884 | } |
937 | |
885 | |
… | |
… | |
940 | { |
888 | { |
941 | RETVAL = w->signum; |
889 | RETVAL = w->signum; |
942 | |
890 | |
943 | if (items > 1) |
891 | if (items > 1) |
944 | { |
892 | { |
945 | Signal signum = sv_signum (new_signal); |
893 | Signal signum = s_signum (new_signal); |
946 | CHECK_SIG (new_signal, signum); |
894 | CHECK_SIG (new_signal, signum); |
947 | |
895 | |
948 | RESET (signal, w, (w, signum)); |
896 | RESET (signal, w, (w, signum)); |
949 | } |
897 | } |
950 | } |
898 | } |
… | |
… | |
1310 | #if 0 |
1258 | #if 0 |
1311 | |
1259 | |
1312 | void ev_feed_signal_event (struct ev_loop *loop, SV *signal) |
1260 | void ev_feed_signal_event (struct ev_loop *loop, SV *signal) |
1313 | CODE: |
1261 | CODE: |
1314 | { |
1262 | { |
1315 | Signal signum = sv_signum (signal); |
1263 | Signal signum = s_signum (signal); |
1316 | CHECK_SIG (signal, signum); |
1264 | CHECK_SIG (signal, signum); |
1317 | |
1265 | |
1318 | ev_feed_signal_event (loop, signum); |
1266 | ev_feed_signal_event (loop, signum); |
1319 | } |
1267 | } |
1320 | |
1268 | |
… | |
… | |
1323 | ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb) |
1271 | ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb) |
1324 | ALIAS: |
1272 | ALIAS: |
1325 | io_ns = 1 |
1273 | io_ns = 1 |
1326 | CODE: |
1274 | CODE: |
1327 | { |
1275 | { |
1328 | int fd = sv_fileno (fh); |
1276 | int fd = s_fileno (fh, events & EV_WRITE); |
1329 | CHECK_FD (fh, fd); |
1277 | CHECK_FD (fh, fd); |
1330 | |
1278 | |
1331 | RETVAL = e_new (sizeof (ev_io), cb, ST (0)); |
1279 | RETVAL = e_new (sizeof (ev_io), cb, ST (0)); |
1332 | RETVAL->fh = newSVsv (fh); |
1280 | RETVAL->fh = newSVsv (fh); |
1333 | ev_io_set (RETVAL, fd, events); |
1281 | ev_io_set (RETVAL, fd, events); |
… | |
… | |
1370 | ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) |
1318 | ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) |
1371 | ALIAS: |
1319 | ALIAS: |
1372 | signal_ns = 1 |
1320 | signal_ns = 1 |
1373 | CODE: |
1321 | CODE: |
1374 | { |
1322 | { |
1375 | Signal signum = sv_signum (signal); |
1323 | Signal signum = s_signum (signal); |
1376 | CHECK_SIG (signal, signum); |
1324 | CHECK_SIG (signal, signum); |
1377 | |
1325 | |
1378 | RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); |
1326 | RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); |
1379 | ev_signal_set (RETVAL, signum); |
1327 | ev_signal_set (RETVAL, signum); |
1380 | if (!ix) START (signal, RETVAL); |
1328 | if (!ix) START (signal, RETVAL); |
… | |
… | |
1473 | |
1421 | |
1474 | void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb) |
1422 | void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb) |
1475 | CODE: |
1423 | CODE: |
1476 | ev_once ( |
1424 | ev_once ( |
1477 | loop, |
1425 | loop, |
1478 | sv_fileno (fh), events, |
1426 | s_fileno (fh, events & EV_WRITE), events, |
1479 | SvOK (timeout) ? SvNV (timeout) : -1., |
1427 | SvOK (timeout) ? SvNV (timeout) : -1., |
1480 | e_once_cb, |
1428 | e_once_cb, |
1481 | newSVsv (cb) |
1429 | newSVsv (cb) |
1482 | ); |
1430 | ); |
1483 | |
1431 | |