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>*/ |
5 | /*#include <netinet/in.h>*/ |
|
|
6 | |
|
|
7 | /* fix perl api breakage */ |
|
|
8 | #undef signal |
|
|
9 | #undef sigaction |
6 | |
10 | |
7 | #define EV_PROTOTYPES 1 |
11 | #define EV_PROTOTYPES 1 |
8 | #define EV_USE_NANOSLEEP EV_USE_MONOTONIC |
12 | #define EV_USE_NANOSLEEP EV_USE_MONOTONIC |
9 | #define EV_H <ev.h> |
13 | #define EV_H <ev.h> |
10 | #include "EV/EVAPI.h" |
14 | #include "EV/EVAPI.h" |
11 | |
|
|
12 | /* fix perl api breakage */ |
|
|
13 | #undef signal |
|
|
14 | #undef sigaction |
|
|
15 | |
15 | |
16 | #define EV_SELECT_IS_WINSOCKET 0 |
16 | #define EV_SELECT_IS_WINSOCKET 0 |
17 | #ifdef _WIN32 |
17 | #ifdef _WIN32 |
18 | # define EV_SELECT_USE_FD_SET 0 |
18 | # define EV_SELECT_USE_FD_SET 0 |
19 | # define NFDBITS PERL_NFDBITS |
19 | # define NFDBITS PERL_NFDBITS |
… | |
… | |
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 |
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 |
|
|
38 | |
|
|
39 | #if __GNUC__ >= 3 |
|
|
40 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
41 | #else |
|
|
42 | # define expect(expr,value) (expr) |
|
|
43 | #endif |
|
|
44 | |
|
|
45 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
46 | #define expect_true(expr) expect ((expr) != 0, 1) |
28 | |
47 | |
29 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) |
48 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) |
30 | |
49 | |
31 | #define WFLAG_KEEPALIVE 1 |
50 | #define WFLAG_KEEPALIVE 1 |
32 | |
51 | |
… | |
… | |
127 | return SvIV (fh); |
146 | return SvIV (fh); |
128 | |
147 | |
129 | return -1; |
148 | return -1; |
130 | } |
149 | } |
131 | |
150 | |
|
|
151 | static SV * |
|
|
152 | e_get_cv (SV *cb_sv) |
|
|
153 | { |
|
|
154 | HV *st; |
|
|
155 | GV *gvp; |
|
|
156 | CV *cv = sv_2cv (cb_sv, &st, &gvp, 0); |
|
|
157 | |
|
|
158 | if (!cv) |
|
|
159 | croak ("EV watcher callback must be a CODE reference"); |
|
|
160 | |
|
|
161 | return (SV *)cv; |
|
|
162 | } |
|
|
163 | |
132 | static void * |
164 | static void * |
133 | e_new (int size, SV *cb_sv, SV *loop) |
165 | e_new (int size, SV *cb_sv, SV *loop) |
134 | { |
166 | { |
|
|
167 | SV *cv = cb_sv ? e_get_cv (cb_sv) : 0; |
135 | ev_watcher *w; |
168 | ev_watcher *w; |
136 | SV *self = NEWSV (0, size); |
169 | SV *self = NEWSV (0, size); |
137 | SvPOK_only (self); |
170 | SvPOK_only (self); |
138 | SvCUR_set (self, size); |
171 | SvCUR_set (self, size); |
139 | |
172 | |
140 | w = (ev_watcher *)SvPVX (self); |
173 | w = (ev_watcher *)SvPVX (self); |
141 | |
174 | |
142 | ev_init (w, e_cb); |
175 | ev_init (w, cv ? e_cb : 0); |
143 | |
176 | |
144 | w->loop = SvREFCNT_inc (SvRV (loop)); |
177 | w->loop = SvREFCNT_inc (SvRV (loop)); |
145 | w->e_flags = WFLAG_KEEPALIVE; |
178 | w->e_flags = WFLAG_KEEPALIVE; |
146 | w->data = 0; |
179 | w->data = 0; |
147 | w->fh = 0; |
180 | w->fh = 0; |
148 | w->cb_sv = SvTEMP (cb_sv) && SvREFCNT (cb_sv) == 1 ? SvREFCNT_inc (cb_sv) : newSVsv (cb_sv); |
181 | w->cb_sv = SvREFCNT_inc (cv); |
149 | w->self = self; |
182 | w->self = self; |
150 | |
183 | |
151 | return (void *)w; |
184 | return (void *)w; |
152 | } |
185 | } |
153 | |
186 | |
… | |
… | |
177 | } |
210 | } |
178 | |
211 | |
179 | return rv; |
212 | return rv; |
180 | } |
213 | } |
181 | |
214 | |
182 | static SV *sv_events_cache; |
215 | static SV *sv_self_cache, *sv_events_cache; |
183 | |
216 | |
184 | static void |
217 | static void |
185 | e_cb (EV_P_ ev_watcher *w, int revents) |
218 | e_cb (EV_P_ ev_watcher *w, int revents) |
186 | { |
219 | { |
187 | dSP; |
220 | dSP; |
188 | I32 mark = SP - PL_stack_base; |
221 | I32 mark = SP - PL_stack_base; |
189 | SV *sv_self, *sv_events; |
222 | SV *sv_self, *sv_events; |
190 | |
223 | |
|
|
224 | if (expect_true (sv_self_cache)) |
|
|
225 | { |
|
|
226 | sv_self = sv_self_cache; sv_self_cache = 0; |
|
|
227 | SvRV_set (sv_self, SvREFCNT_inc_NN (w->self)); |
|
|
228 | } |
|
|
229 | else |
|
|
230 | { |
191 | sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ |
231 | sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ |
|
|
232 | SvREADONLY_on (sv_self); |
|
|
233 | } |
192 | |
234 | |
193 | if (sv_events_cache) |
235 | if (expect_true (sv_events_cache)) |
194 | { |
236 | { |
195 | sv_events = sv_events_cache; sv_events_cache = 0; |
237 | sv_events = sv_events_cache; sv_events_cache = 0; |
196 | SvIV_set (sv_events, revents); |
238 | SvIV_set (sv_events, revents); |
197 | } |
239 | } |
198 | else |
240 | else |
|
|
241 | { |
199 | sv_events = newSViv (revents); |
242 | sv_events = newSViv (revents); |
|
|
243 | SvREADONLY_on (sv_events); |
|
|
244 | } |
200 | |
245 | |
201 | PUSHMARK (SP); |
246 | PUSHMARK (SP); |
202 | EXTEND (SP, 2); |
247 | EXTEND (SP, 2); |
203 | PUSHs (sv_self); |
248 | PUSHs (sv_self); |
204 | PUSHs (sv_events); |
249 | PUSHs (sv_events); |
205 | |
250 | |
206 | PUTBACK; |
251 | PUTBACK; |
207 | call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); |
252 | call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); |
208 | |
253 | |
|
|
254 | if (expect_false (sv_self_cache)) |
209 | SvREFCNT_dec (sv_self); |
255 | SvREFCNT_dec (sv_self); |
|
|
256 | else |
|
|
257 | { |
|
|
258 | SvREFCNT_dec (SvRV (sv_self)); |
|
|
259 | SvRV_set (sv_self, &PL_sv_undef); |
|
|
260 | sv_self_cache = sv_self; |
|
|
261 | } |
210 | |
262 | |
211 | if (sv_events_cache) |
263 | if (expect_false (sv_events_cache)) |
212 | SvREFCNT_dec (sv_events); |
264 | SvREFCNT_dec (sv_events); |
213 | else |
265 | else |
214 | sv_events_cache = sv_events; |
266 | sv_events_cache = sv_events; |
215 | |
267 | |
216 | if (SvTRUE (ERRSV)) |
268 | if (SvTRUE (ERRSV)) |
… | |
… | |
390 | evapi.sv_fileno = sv_fileno; |
442 | evapi.sv_fileno = sv_fileno; |
391 | evapi.sv_signum = sv_signum; |
443 | evapi.sv_signum = sv_signum; |
392 | evapi.supported_backends = ev_supported_backends (); |
444 | evapi.supported_backends = ev_supported_backends (); |
393 | evapi.recommended_backends = ev_recommended_backends (); |
445 | evapi.recommended_backends = ev_recommended_backends (); |
394 | evapi.embeddable_backends = ev_embeddable_backends (); |
446 | evapi.embeddable_backends = ev_embeddable_backends (); |
395 | evapi.time = ev_time; |
447 | evapi.time_ = ev_time; |
396 | evapi.sleep = ev_sleep; |
448 | evapi.sleep_ = ev_sleep; |
397 | evapi.loop_new = ev_loop_new; |
449 | evapi.loop_new = ev_loop_new; |
398 | evapi.loop_destroy = ev_loop_destroy; |
450 | evapi.loop_destroy = ev_loop_destroy; |
399 | evapi.loop_fork = ev_loop_fork; |
451 | evapi.loop_fork = ev_loop_fork; |
400 | evapi.loop_count = ev_loop_count; |
452 | evapi.loop_count = ev_loop_count; |
401 | evapi.now = ev_now; |
453 | evapi.now = ev_now; |
|
|
454 | evapi.now_update = ev_now_update; |
402 | evapi.backend = ev_backend; |
455 | evapi.backend = ev_backend; |
403 | evapi.unloop = ev_unloop; |
456 | evapi.unloop = ev_unloop; |
404 | evapi.ref = ev_ref; |
457 | evapi.ref = ev_ref; |
405 | evapi.unref = ev_unref; |
458 | evapi.unref = ev_unref; |
406 | evapi.loop = ev_loop; |
459 | evapi.loop = ev_loop; |
… | |
… | |
472 | |
525 | |
473 | unsigned int ev_recommended_backends () |
526 | unsigned int ev_recommended_backends () |
474 | |
527 | |
475 | unsigned int ev_embeddable_backends () |
528 | unsigned int ev_embeddable_backends () |
476 | |
529 | |
|
|
530 | void ev_sleep (NV interval) |
|
|
531 | |
477 | NV ev_time () |
532 | NV ev_time () |
478 | |
533 | |
479 | NV ev_now () |
534 | NV ev_now () |
|
|
535 | C_ARGS: evapi.default_loop |
|
|
536 | |
|
|
537 | void ev_now_update () |
480 | C_ARGS: evapi.default_loop |
538 | C_ARGS: evapi.default_loop |
481 | |
539 | |
482 | unsigned int ev_backend () |
540 | unsigned int ev_backend () |
483 | C_ARGS: evapi.default_loop |
541 | C_ARGS: evapi.default_loop |
484 | |
542 | |
… | |
… | |
628 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
686 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
629 | if (!ix) START (stat, RETVAL); |
687 | if (!ix) START (stat, RETVAL); |
630 | OUTPUT: |
688 | OUTPUT: |
631 | RETVAL |
689 | RETVAL |
632 | |
690 | |
633 | ev_embed *embed (struct ev_loop *loop, SV *cb = &PL_sv_undef) |
691 | ev_embed *embed (struct ev_loop *loop, SV *cb = 0) |
634 | ALIAS: |
692 | ALIAS: |
635 | embed_ns = 1 |
693 | embed_ns = 1 |
636 | CODE: |
694 | CODE: |
637 | { |
695 | { |
638 | if (!(ev_backend (loop) & ev_embeddable_backends ())) |
696 | if (!(ev_backend (loop) & ev_embeddable_backends ())) |
639 | croak ("passed loop is not embeddable via EV::embed,"); |
697 | croak ("passed loop is not embeddable via EV::embed,"); |
640 | |
698 | |
641 | RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv); |
699 | RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv); |
642 | RETVAL->fh = newSVsv (ST (0)); |
700 | RETVAL->fh = newSVsv (ST (0)); |
643 | ev_embed_set (RETVAL, loop); |
701 | ev_embed_set (RETVAL, loop); |
644 | |
|
|
645 | if (!SvOK (cb)) ev_set_cb (RETVAL, 0); |
|
|
646 | |
|
|
647 | if (!ix) START (embed, RETVAL); |
702 | if (!ix) START (embed, RETVAL); |
648 | } |
703 | } |
649 | OUTPUT: |
704 | OUTPUT: |
650 | RETVAL |
705 | RETVAL |
651 | |
706 | |
… | |
… | |
703 | RETVAL |
758 | RETVAL |
704 | |
759 | |
705 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
760 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
706 | CODE: |
761 | CODE: |
707 | { |
762 | { |
708 | RETVAL = newSVsv (w->cb_sv); |
|
|
709 | |
|
|
710 | if (items > 1) |
763 | if (items > 1) |
711 | sv_setsv (w->cb_sv, new_cb); |
764 | { |
|
|
765 | new_cb = e_get_cv (new_cb); |
|
|
766 | RETVAL = newRV_noinc (w->cb_sv); |
|
|
767 | w->cb_sv = SvREFCNT_inc (new_cb); |
|
|
768 | } |
|
|
769 | else |
|
|
770 | RETVAL = newRV_inc (w->cb_sv); |
712 | } |
771 | } |
713 | OUTPUT: |
772 | OUTPUT: |
714 | RETVAL |
773 | RETVAL |
715 | |
774 | |
716 | SV *data (ev_watcher *w, SV *new_data = 0) |
775 | SV *data (ev_watcher *w, SV *new_data = 0) |
… | |
… | |
889 | INIT: |
948 | INIT: |
890 | CHECK_REPEAT (repeat); |
949 | CHECK_REPEAT (repeat); |
891 | CODE: |
950 | CODE: |
892 | RESET (timer, w, (w, after, repeat)); |
951 | RESET (timer, w, (w, after, repeat)); |
893 | |
952 | |
894 | NV at (ev_timer *w) |
|
|
895 | CODE: |
|
|
896 | RETVAL = w->at; |
|
|
897 | OUTPUT: |
|
|
898 | RETVAL |
|
|
899 | |
|
|
900 | MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_ |
953 | MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_ |
901 | |
954 | |
902 | void ev_periodic_start (ev_periodic *w) |
955 | void ev_periodic_start (ev_periodic *w) |
903 | INIT: |
956 | INIT: |
904 | CHECK_REPEAT (w->interval); |
957 | CHECK_REPEAT (w->interval); |
… | |
… | |
931 | RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0)); |
984 | RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0)); |
932 | } |
985 | } |
933 | |
986 | |
934 | NV at (ev_periodic *w) |
987 | NV at (ev_periodic *w) |
935 | CODE: |
988 | CODE: |
936 | RETVAL = w->at; |
989 | RETVAL = ev_periodic_at (w); |
937 | OUTPUT: |
990 | OUTPUT: |
938 | RETVAL |
991 | RETVAL |
939 | |
992 | |
940 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
993 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
941 | |
994 | |
… | |
… | |
1147 | CODE: |
1200 | CODE: |
1148 | RETVAL = newSVsv (w->fh); |
1201 | RETVAL = newSVsv (w->fh); |
1149 | OUTPUT: |
1202 | OUTPUT: |
1150 | RETVAL |
1203 | RETVAL |
1151 | |
1204 | |
|
|
1205 | void ev_embed_sweep (ev_embed *w) |
|
|
1206 | C_ARGS: e_loop (w), w |
|
|
1207 | |
1152 | MODULE = EV PACKAGE = EV::Async PREFIX = ev_async_ |
1208 | MODULE = EV PACKAGE = EV::Async PREFIX = ev_async_ |
1153 | |
1209 | |
1154 | void ev_async_start (ev_async *w) |
1210 | void ev_async_start (ev_async *w) |
1155 | CODE: |
1211 | CODE: |
1156 | START (async, w); |
1212 | START (async, w); |
… | |
… | |
1162 | void DESTROY (ev_async *w) |
1218 | void DESTROY (ev_async *w) |
1163 | CODE: |
1219 | CODE: |
1164 | STOP (async, w); |
1220 | STOP (async, w); |
1165 | e_destroy (w); |
1221 | e_destroy (w); |
1166 | |
1222 | |
|
|
1223 | void ev_async_send (ev_async *w) |
|
|
1224 | C_ARGS: e_loop (w), w |
|
|
1225 | |
|
|
1226 | SV *ev_async_async_pending (ev_async *w) |
|
|
1227 | CODE: |
|
|
1228 | RETVAL = boolSV (ev_async_pending (w)); |
|
|
1229 | OUTPUT: |
|
|
1230 | RETVAL |
|
|
1231 | |
1167 | MODULE = EV PACKAGE = EV::Loop PREFIX = ev_ |
1232 | MODULE = EV PACKAGE = EV::Loop PREFIX = ev_ |
1168 | |
1233 | |
1169 | SV *new (SV *klass, unsigned int flags = 0) |
1234 | SV *new (SV *klass, unsigned int flags = 0) |
1170 | CODE: |
1235 | CODE: |
1171 | { |
1236 | { |
… | |
… | |
1184 | if (loop != evapi.default_loop) /* global destruction sucks */ |
1249 | if (loop != evapi.default_loop) /* global destruction sucks */ |
1185 | ev_loop_destroy (loop); |
1250 | ev_loop_destroy (loop); |
1186 | |
1251 | |
1187 | void ev_loop_fork (struct ev_loop *loop) |
1252 | void ev_loop_fork (struct ev_loop *loop) |
1188 | |
1253 | |
|
|
1254 | void ev_loop_verify (struct ev_loop *loop) |
|
|
1255 | |
1189 | NV ev_now (struct ev_loop *loop) |
1256 | NV ev_now (struct ev_loop *loop) |
|
|
1257 | |
|
|
1258 | void ev_now_update (struct ev_loop *loop) |
1190 | |
1259 | |
1191 | void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) |
1260 | void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) |
1192 | |
1261 | |
1193 | void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) |
1262 | void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) |
1194 | |
1263 | |
… | |
… | |
1338 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
1407 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
1339 | if (!ix) START (stat, RETVAL); |
1408 | if (!ix) START (stat, RETVAL); |
1340 | OUTPUT: |
1409 | OUTPUT: |
1341 | RETVAL |
1410 | RETVAL |
1342 | |
1411 | |
1343 | ev_embed *embed (struct ev_loop *loop, struct ev_loop *other, SV *cb = &PL_sv_undef) |
1412 | ev_embed *embed (struct ev_loop *loop, struct ev_loop *other, SV *cb = 0) |
1344 | ALIAS: |
1413 | ALIAS: |
1345 | embed_ns = 1 |
1414 | embed_ns = 1 |
1346 | CODE: |
1415 | CODE: |
1347 | { |
1416 | { |
1348 | if (!(ev_backend (other) & ev_embeddable_backends ())) |
1417 | if (!(ev_backend (other) & ev_embeddable_backends ())) |
1349 | croak ("passed loop is not embeddable via EV::embed,"); |
1418 | croak ("passed loop is not embeddable via EV::embed,"); |
1350 | |
1419 | |
1351 | RETVAL = e_new (sizeof (ev_embed), cb, ST (0)); |
1420 | RETVAL = e_new (sizeof (ev_embed), cb, ST (0)); |
1352 | RETVAL->fh = newSVsv (ST (1)); |
1421 | RETVAL->fh = newSVsv (ST (1)); |
1353 | ev_embed_set (RETVAL, other); |
1422 | ev_embed_set (RETVAL, other); |
1354 | |
|
|
1355 | if (!SvOK (cb)) ev_set_cb (RETVAL, 0); |
|
|
1356 | |
|
|
1357 | if (!ix) START (embed, RETVAL); |
1423 | if (!ix) START (embed, RETVAL); |
1358 | } |
1424 | } |
1359 | OUTPUT: |
1425 | OUTPUT: |
1360 | RETVAL |
1426 | RETVAL |
1361 | |
1427 | |