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; |
|
|
405 | evapi.loop_depth = ev_loop_depth; |
|
|
406 | evapi.set_userdata = ev_set_userdata; |
|
|
407 | evapi.userdata = ev_userdata; |
470 | evapi.now = ev_now; |
408 | evapi.now = ev_now; |
471 | evapi.now_update = ev_now_update; |
409 | evapi.now_update = ev_now_update; |
472 | evapi.suspend = ev_suspend; |
410 | evapi.suspend = ev_suspend; |
473 | evapi.resume = ev_resume; |
411 | evapi.resume = ev_resume; |
474 | evapi.backend = ev_backend; |
412 | evapi.backend = ev_backend; |
475 | 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; |
476 | evapi.ref = ev_ref; |
417 | evapi.ref = ev_ref; |
477 | evapi.unref = ev_unref; |
418 | evapi.unref = ev_unref; |
478 | evapi.loop = ev_loop; |
419 | evapi.loop = ev_loop; |
479 | evapi.once = ev_once; |
420 | evapi.once = ev_once; |
480 | evapi.io_start = ev_io_start; |
421 | evapi.io_start = ev_io_start; |
… | |
… | |
563 | C_ARGS: evapi.default_loop |
504 | C_ARGS: evapi.default_loop |
564 | |
505 | |
565 | unsigned int ev_backend () |
506 | unsigned int ev_backend () |
566 | C_ARGS: evapi.default_loop |
507 | C_ARGS: evapi.default_loop |
567 | |
508 | |
|
|
509 | void ev_loop_verify () |
|
|
510 | C_ARGS: evapi.default_loop |
|
|
511 | |
568 | unsigned int ev_loop_count () |
512 | unsigned int ev_loop_count () |
569 | C_ARGS: evapi.default_loop |
513 | C_ARGS: evapi.default_loop |
570 | |
514 | |
|
|
515 | unsigned int ev_loop_depth () |
|
|
516 | C_ARGS: evapi.default_loop |
|
|
517 | |
571 | void ev_set_io_collect_interval (NV interval) |
518 | void ev_set_io_collect_interval (NV interval) |
572 | C_ARGS: evapi.default_loop, interval |
519 | C_ARGS: evapi.default_loop, interval |
573 | |
520 | |
574 | void ev_set_timeout_collect_interval (NV interval) |
521 | void ev_set_timeout_collect_interval (NV interval) |
575 | C_ARGS: evapi.default_loop, interval |
522 | C_ARGS: evapi.default_loop, interval |
… | |
… | |
584 | C_ARGS: evapi.default_loop, fd, revents |
531 | C_ARGS: evapi.default_loop, fd, revents |
585 | |
532 | |
586 | void ev_feed_signal_event (SV *signal) |
533 | void ev_feed_signal_event (SV *signal) |
587 | CODE: |
534 | CODE: |
588 | { |
535 | { |
589 | Signal signum = sv_signum (signal); |
536 | Signal signum = s_signum (signal); |
590 | CHECK_SIG (signal, signum); |
537 | CHECK_SIG (signal, signum); |
591 | |
538 | |
592 | ev_feed_signal_event (evapi.default_loop, signum); |
539 | ev_feed_signal_event (evapi.default_loop, signum); |
593 | } |
540 | } |
594 | |
541 | |
595 | ev_io *io (SV *fh, int events, SV *cb) |
542 | ev_io *io (SV *fh, int events, SV *cb) |
596 | ALIAS: |
543 | ALIAS: |
597 | io_ns = 1 |
544 | io_ns = 1 |
598 | CODE: |
545 | CODE: |
599 | { |
546 | { |
600 | int fd = sv_fileno (fh); |
547 | int fd = s_fileno (fh, events & EV_WRITE); |
601 | CHECK_FD (fh, fd); |
548 | CHECK_FD (fh, fd); |
602 | |
549 | |
603 | RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); |
550 | RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); |
604 | RETVAL->fh = newSVsv (fh); |
551 | RETVAL->fh = newSVsv (fh); |
605 | ev_io_set (RETVAL, fd, events); |
552 | ev_io_set (RETVAL, fd, events); |
… | |
… | |
640 | ev_signal *signal (SV *signal, SV *cb) |
587 | ev_signal *signal (SV *signal, SV *cb) |
641 | ALIAS: |
588 | ALIAS: |
642 | signal_ns = 1 |
589 | signal_ns = 1 |
643 | CODE: |
590 | CODE: |
644 | { |
591 | { |
645 | Signal signum = sv_signum (signal); |
592 | Signal signum = s_signum (signal); |
646 | CHECK_SIG (signal, signum); |
593 | CHECK_SIG (signal, signum); |
647 | |
594 | |
648 | RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); |
595 | RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); |
649 | ev_signal_set (RETVAL, signum); |
596 | ev_signal_set (RETVAL, signum); |
650 | if (!ix) START (signal, RETVAL); |
597 | if (!ix) START (signal, RETVAL); |
… | |
… | |
741 | |
688 | |
742 | void once (SV *fh, int events, SV *timeout, SV *cb) |
689 | void once (SV *fh, int events, SV *timeout, SV *cb) |
743 | CODE: |
690 | CODE: |
744 | ev_once ( |
691 | ev_once ( |
745 | evapi.default_loop, |
692 | evapi.default_loop, |
746 | sv_fileno (fh), events, |
693 | s_fileno (fh, events & EV_WRITE), events, |
747 | SvOK (timeout) ? SvNV (timeout) : -1., |
694 | SvOK (timeout) ? SvNV (timeout) : -1., |
748 | e_once_cb, |
695 | e_once_cb, |
749 | newSVsv (cb) |
696 | newSVsv (cb) |
750 | ); |
697 | ); |
751 | |
698 | |
… | |
… | |
785 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
732 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
786 | CODE: |
733 | CODE: |
787 | { |
734 | { |
788 | if (items > 1) |
735 | if (items > 1) |
789 | { |
736 | { |
790 | new_cb = e_get_cv (new_cb); |
737 | new_cb = s_get_cv_croak (new_cb); |
791 | RETVAL = newRV_noinc (w->cb_sv); |
738 | RETVAL = newRV_noinc (w->cb_sv); |
792 | w->cb_sv = SvREFCNT_inc (new_cb); |
739 | w->cb_sv = SvREFCNT_inc (new_cb); |
793 | } |
740 | } |
794 | else |
741 | else |
795 | RETVAL = newRV_inc (w->cb_sv); |
742 | RETVAL = newRV_inc (w->cb_sv); |
… | |
… | |
865 | e_destroy (w); |
812 | e_destroy (w); |
866 | |
813 | |
867 | void set (ev_io *w, SV *fh, int events) |
814 | void set (ev_io *w, SV *fh, int events) |
868 | CODE: |
815 | CODE: |
869 | { |
816 | { |
870 | int fd = sv_fileno (fh); |
817 | int fd = s_fileno (fh, events & EV_WRITE); |
871 | CHECK_FD (fh, fd); |
818 | CHECK_FD (fh, fd); |
872 | |
819 | |
873 | sv_setsv (w->fh, fh); |
820 | sv_setsv (w->fh, fh); |
874 | RESET (io, w, (w, fd, events)); |
821 | RESET (io, w, (w, fd, events)); |
875 | } |
822 | } |
… | |
… | |
877 | SV *fh (ev_io *w, SV *new_fh = 0) |
824 | SV *fh (ev_io *w, SV *new_fh = 0) |
878 | CODE: |
825 | CODE: |
879 | { |
826 | { |
880 | if (items > 1) |
827 | if (items > 1) |
881 | { |
828 | { |
882 | int fd = sv_fileno (new_fh); |
829 | int fd = s_fileno (new_fh, w->events & EV_WRITE); |
883 | CHECK_FD (new_fh, fd); |
830 | CHECK_FD (new_fh, fd); |
884 | |
831 | |
885 | RETVAL = w->fh; |
832 | RETVAL = w->fh; |
886 | w->fh = newSVsv (new_fh); |
833 | w->fh = newSVsv (new_fh); |
887 | |
834 | |
… | |
… | |
920 | e_destroy (w); |
867 | e_destroy (w); |
921 | |
868 | |
922 | void set (ev_signal *w, SV *signal) |
869 | void set (ev_signal *w, SV *signal) |
923 | CODE: |
870 | CODE: |
924 | { |
871 | { |
925 | Signal signum = sv_signum (signal); |
872 | Signal signum = s_signum (signal); |
926 | CHECK_SIG (signal, signum); |
873 | CHECK_SIG (signal, signum); |
927 | |
874 | |
928 | RESET (signal, w, (w, signum)); |
875 | RESET (signal, w, (w, signum)); |
929 | } |
876 | } |
930 | |
877 | |
… | |
… | |
933 | { |
880 | { |
934 | RETVAL = w->signum; |
881 | RETVAL = w->signum; |
935 | |
882 | |
936 | if (items > 1) |
883 | if (items > 1) |
937 | { |
884 | { |
938 | Signal signum = sv_signum (new_signal); |
885 | Signal signum = s_signum (new_signal); |
939 | CHECK_SIG (new_signal, signum); |
886 | CHECK_SIG (new_signal, signum); |
940 | |
887 | |
941 | RESET (signal, w, (w, signum)); |
888 | RESET (signal, w, (w, signum)); |
942 | } |
889 | } |
943 | } |
890 | } |
… | |
… | |
1290 | |
1237 | |
1291 | unsigned int ev_backend (struct ev_loop *loop) |
1238 | unsigned int ev_backend (struct ev_loop *loop) |
1292 | |
1239 | |
1293 | unsigned int ev_loop_count (struct ev_loop *loop) |
1240 | unsigned int ev_loop_count (struct ev_loop *loop) |
1294 | |
1241 | |
|
|
1242 | unsigned int ev_loop_depth (struct ev_loop *loop) |
|
|
1243 | |
1295 | void ev_loop (struct ev_loop *loop, int flags = 0) |
1244 | void ev_loop (struct ev_loop *loop, int flags = 0) |
1296 | |
1245 | |
1297 | void ev_unloop (struct ev_loop *loop, int how = 1) |
1246 | void ev_unloop (struct ev_loop *loop, int how = 1) |
1298 | |
1247 | |
1299 | void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE) |
1248 | void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE) |
… | |
… | |
1301 | #if 0 |
1250 | #if 0 |
1302 | |
1251 | |
1303 | void ev_feed_signal_event (struct ev_loop *loop, SV *signal) |
1252 | void ev_feed_signal_event (struct ev_loop *loop, SV *signal) |
1304 | CODE: |
1253 | CODE: |
1305 | { |
1254 | { |
1306 | Signal signum = sv_signum (signal); |
1255 | Signal signum = s_signum (signal); |
1307 | CHECK_SIG (signal, signum); |
1256 | CHECK_SIG (signal, signum); |
1308 | |
1257 | |
1309 | ev_feed_signal_event (loop, signum); |
1258 | ev_feed_signal_event (loop, signum); |
1310 | } |
1259 | } |
1311 | |
1260 | |
… | |
… | |
1314 | 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) |
1315 | ALIAS: |
1264 | ALIAS: |
1316 | io_ns = 1 |
1265 | io_ns = 1 |
1317 | CODE: |
1266 | CODE: |
1318 | { |
1267 | { |
1319 | int fd = sv_fileno (fh); |
1268 | int fd = s_fileno (fh, events & EV_WRITE); |
1320 | CHECK_FD (fh, fd); |
1269 | CHECK_FD (fh, fd); |
1321 | |
1270 | |
1322 | RETVAL = e_new (sizeof (ev_io), cb, ST (0)); |
1271 | RETVAL = e_new (sizeof (ev_io), cb, ST (0)); |
1323 | RETVAL->fh = newSVsv (fh); |
1272 | RETVAL->fh = newSVsv (fh); |
1324 | ev_io_set (RETVAL, fd, events); |
1273 | ev_io_set (RETVAL, fd, events); |
… | |
… | |
1361 | ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) |
1310 | ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) |
1362 | ALIAS: |
1311 | ALIAS: |
1363 | signal_ns = 1 |
1312 | signal_ns = 1 |
1364 | CODE: |
1313 | CODE: |
1365 | { |
1314 | { |
1366 | Signal signum = sv_signum (signal); |
1315 | Signal signum = s_signum (signal); |
1367 | CHECK_SIG (signal, signum); |
1316 | CHECK_SIG (signal, signum); |
1368 | |
1317 | |
1369 | RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); |
1318 | RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); |
1370 | ev_signal_set (RETVAL, signum); |
1319 | ev_signal_set (RETVAL, signum); |
1371 | if (!ix) START (signal, RETVAL); |
1320 | if (!ix) START (signal, RETVAL); |
… | |
… | |
1464 | |
1413 | |
1465 | 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) |
1466 | CODE: |
1415 | CODE: |
1467 | ev_once ( |
1416 | ev_once ( |
1468 | loop, |
1417 | loop, |
1469 | sv_fileno (fh), events, |
1418 | s_fileno (fh, events & EV_WRITE), events, |
1470 | SvOK (timeout) ? SvNV (timeout) : -1., |
1419 | SvOK (timeout) ? SvNV (timeout) : -1., |
1471 | e_once_cb, |
1420 | e_once_cb, |
1472 | newSVsv (cb) |
1421 | newSVsv (cb) |
1473 | ); |
1422 | ); |
1474 | |
1423 | |