… | |
… | |
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 | #if __GNUC__ >= 3 |
|
|
35 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
36 | #else |
|
|
37 | # define expect(expr,value) (expr) |
|
|
38 | #endif |
|
|
39 | |
|
|
40 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
41 | #define expect_true(expr) expect ((expr) != 0, 1) |
28 | |
42 | |
29 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) |
43 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) |
30 | |
44 | |
31 | #define WFLAG_KEEPALIVE 1 |
45 | #define WFLAG_KEEPALIVE 1 |
32 | |
46 | |
… | |
… | |
127 | return SvIV (fh); |
141 | return SvIV (fh); |
128 | |
142 | |
129 | return -1; |
143 | return -1; |
130 | } |
144 | } |
131 | |
145 | |
|
|
146 | static SV * |
|
|
147 | e_get_cv (SV *cb_sv) |
|
|
148 | { |
|
|
149 | HV *st; |
|
|
150 | GV *gvp; |
|
|
151 | CV *cv = sv_2cv (cb_sv, &st, &gvp, 0); |
|
|
152 | |
|
|
153 | if (!cv) |
|
|
154 | croak ("EV watcher callback must be a CODE reference"); |
|
|
155 | |
|
|
156 | return (SV *)cv; |
|
|
157 | } |
|
|
158 | |
132 | static void * |
159 | static void * |
133 | e_new (int size, SV *cb_sv, SV *loop) |
160 | e_new (int size, SV *cb_sv, SV *loop) |
134 | { |
161 | { |
|
|
162 | SV *cv = cb_sv ? e_get_cv (cb_sv) : 0; |
135 | ev_watcher *w; |
163 | ev_watcher *w; |
136 | SV *self = NEWSV (0, size); |
164 | SV *self = NEWSV (0, size); |
137 | SvPOK_only (self); |
165 | SvPOK_only (self); |
138 | SvCUR_set (self, size); |
166 | SvCUR_set (self, size); |
139 | |
167 | |
140 | w = (ev_watcher *)SvPVX (self); |
168 | w = (ev_watcher *)SvPVX (self); |
141 | |
169 | |
142 | ev_init (w, e_cb); |
170 | ev_init (w, cv ? e_cb : 0); |
143 | |
171 | |
144 | w->loop = SvREFCNT_inc (SvRV (loop)); |
172 | w->loop = SvREFCNT_inc (SvRV (loop)); |
145 | w->e_flags = WFLAG_KEEPALIVE; |
173 | w->e_flags = WFLAG_KEEPALIVE; |
146 | w->data = 0; |
174 | w->data = 0; |
147 | w->fh = 0; |
175 | w->fh = 0; |
148 | w->cb_sv = SvTEMP (cb_sv) && SvREFCNT (cb_sv) == 1 ? SvREFCNT_inc (cb_sv) : newSVsv (cb_sv); |
176 | w->cb_sv = SvREFCNT_inc (cv); |
149 | w->self = self; |
177 | w->self = self; |
150 | |
178 | |
151 | return (void *)w; |
179 | return (void *)w; |
152 | } |
180 | } |
153 | |
181 | |
… | |
… | |
177 | } |
205 | } |
178 | |
206 | |
179 | return rv; |
207 | return rv; |
180 | } |
208 | } |
181 | |
209 | |
182 | static SV *sv_events_cache; |
210 | static SV *sv_self_cache, *sv_events_cache; |
183 | |
211 | |
184 | static void |
212 | static void |
185 | e_cb (EV_P_ ev_watcher *w, int revents) |
213 | e_cb (EV_P_ ev_watcher *w, int revents) |
186 | { |
214 | { |
187 | dSP; |
215 | dSP; |
188 | I32 mark = SP - PL_stack_base; |
216 | I32 mark = SP - PL_stack_base; |
189 | SV *sv_self, *sv_events; |
217 | SV *sv_self, *sv_events; |
190 | |
218 | |
|
|
219 | if (expect_true (sv_self_cache)) |
|
|
220 | { |
|
|
221 | sv_self = sv_self_cache; sv_self_cache = 0; |
|
|
222 | SvRV_set (sv_self, SvREFCNT_inc_NN (w->self)); |
|
|
223 | } |
|
|
224 | else |
|
|
225 | { |
191 | sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ |
226 | sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ |
|
|
227 | SvREADONLY_on (sv_self); |
|
|
228 | } |
192 | |
229 | |
193 | if (sv_events_cache) |
230 | if (expect_true (sv_events_cache)) |
194 | { |
231 | { |
195 | sv_events = sv_events_cache; sv_events_cache = 0; |
232 | sv_events = sv_events_cache; sv_events_cache = 0; |
196 | SvIV_set (sv_events, revents); |
233 | SvIV_set (sv_events, revents); |
197 | } |
234 | } |
198 | else |
235 | else |
|
|
236 | { |
199 | sv_events = newSViv (revents); |
237 | sv_events = newSViv (revents); |
|
|
238 | SvREADONLY_on (sv_events); |
|
|
239 | } |
200 | |
240 | |
201 | PUSHMARK (SP); |
241 | PUSHMARK (SP); |
202 | EXTEND (SP, 2); |
242 | EXTEND (SP, 2); |
203 | PUSHs (sv_self); |
243 | PUSHs (sv_self); |
204 | PUSHs (sv_events); |
244 | PUSHs (sv_events); |
205 | |
245 | |
206 | PUTBACK; |
246 | PUTBACK; |
207 | call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); |
247 | call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); |
208 | |
248 | |
|
|
249 | if (expect_false (sv_self_cache)) |
209 | SvREFCNT_dec (sv_self); |
250 | SvREFCNT_dec (sv_self); |
|
|
251 | else |
|
|
252 | { |
|
|
253 | SvREFCNT_dec (SvRV (sv_self)); |
|
|
254 | SvRV_set (sv_self, &PL_sv_undef); |
|
|
255 | sv_self_cache = sv_self; |
|
|
256 | } |
210 | |
257 | |
211 | if (sv_events_cache) |
258 | if (expect_false (sv_events_cache)) |
212 | SvREFCNT_dec (sv_events); |
259 | SvREFCNT_dec (sv_events); |
213 | else |
260 | else |
214 | sv_events_cache = sv_events; |
261 | sv_events_cache = sv_events; |
215 | |
262 | |
216 | if (SvTRUE (ERRSV)) |
263 | if (SvTRUE (ERRSV)) |
… | |
… | |
397 | evapi.loop_new = ev_loop_new; |
444 | evapi.loop_new = ev_loop_new; |
398 | evapi.loop_destroy = ev_loop_destroy; |
445 | evapi.loop_destroy = ev_loop_destroy; |
399 | evapi.loop_fork = ev_loop_fork; |
446 | evapi.loop_fork = ev_loop_fork; |
400 | evapi.loop_count = ev_loop_count; |
447 | evapi.loop_count = ev_loop_count; |
401 | evapi.now = ev_now; |
448 | evapi.now = ev_now; |
|
|
449 | evapi.now_update = ev_now_update; |
402 | evapi.backend = ev_backend; |
450 | evapi.backend = ev_backend; |
403 | evapi.unloop = ev_unloop; |
451 | evapi.unloop = ev_unloop; |
404 | evapi.ref = ev_ref; |
452 | evapi.ref = ev_ref; |
405 | evapi.unref = ev_unref; |
453 | evapi.unref = ev_unref; |
406 | evapi.loop = ev_loop; |
454 | evapi.loop = ev_loop; |
… | |
… | |
472 | |
520 | |
473 | unsigned int ev_recommended_backends () |
521 | unsigned int ev_recommended_backends () |
474 | |
522 | |
475 | unsigned int ev_embeddable_backends () |
523 | unsigned int ev_embeddable_backends () |
476 | |
524 | |
|
|
525 | void ev_sleep (NV interval) |
|
|
526 | |
477 | NV ev_time () |
527 | NV ev_time () |
478 | |
528 | |
479 | NV ev_now () |
529 | NV ev_now () |
|
|
530 | C_ARGS: evapi.default_loop |
|
|
531 | |
|
|
532 | void ev_now_update () |
480 | C_ARGS: evapi.default_loop |
533 | C_ARGS: evapi.default_loop |
481 | |
534 | |
482 | unsigned int ev_backend () |
535 | unsigned int ev_backend () |
483 | C_ARGS: evapi.default_loop |
536 | C_ARGS: evapi.default_loop |
484 | |
537 | |
… | |
… | |
628 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
681 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
629 | if (!ix) START (stat, RETVAL); |
682 | if (!ix) START (stat, RETVAL); |
630 | OUTPUT: |
683 | OUTPUT: |
631 | RETVAL |
684 | RETVAL |
632 | |
685 | |
633 | ev_embed *embed (struct ev_loop *loop, SV *cb = &PL_sv_undef) |
686 | ev_embed *embed (struct ev_loop *loop, SV *cb = 0) |
634 | ALIAS: |
687 | ALIAS: |
635 | embed_ns = 1 |
688 | embed_ns = 1 |
636 | CODE: |
689 | CODE: |
637 | { |
690 | { |
638 | if (!(ev_backend (loop) & ev_embeddable_backends ())) |
691 | if (!(ev_backend (loop) & ev_embeddable_backends ())) |
639 | croak ("passed loop is not embeddable via EV::embed,"); |
692 | croak ("passed loop is not embeddable via EV::embed,"); |
640 | |
693 | |
641 | RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv); |
694 | RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv); |
642 | RETVAL->fh = newSVsv (ST (0)); |
695 | RETVAL->fh = newSVsv (ST (0)); |
643 | ev_embed_set (RETVAL, loop); |
696 | ev_embed_set (RETVAL, loop); |
644 | |
|
|
645 | if (!SvOK (cb)) ev_set_cb (RETVAL, 0); |
|
|
646 | |
|
|
647 | if (!ix) START (embed, RETVAL); |
697 | if (!ix) START (embed, RETVAL); |
648 | } |
698 | } |
649 | OUTPUT: |
699 | OUTPUT: |
650 | RETVAL |
700 | RETVAL |
651 | |
701 | |
… | |
… | |
703 | RETVAL |
753 | RETVAL |
704 | |
754 | |
705 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
755 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
706 | CODE: |
756 | CODE: |
707 | { |
757 | { |
708 | RETVAL = newSVsv (w->cb_sv); |
|
|
709 | |
|
|
710 | if (items > 1) |
758 | if (items > 1) |
711 | sv_setsv (w->cb_sv, new_cb); |
759 | { |
|
|
760 | new_cb = e_get_cv (new_cb); |
|
|
761 | RETVAL = newRV_noinc (w->cb_sv); |
|
|
762 | w->cb_sv = SvREFCNT_inc (new_cb); |
|
|
763 | } |
|
|
764 | else |
|
|
765 | RETVAL = newRV_inc (w->cb_sv); |
712 | } |
766 | } |
713 | OUTPUT: |
767 | OUTPUT: |
714 | RETVAL |
768 | RETVAL |
715 | |
769 | |
716 | SV *data (ev_watcher *w, SV *new_data = 0) |
770 | SV *data (ev_watcher *w, SV *new_data = 0) |
… | |
… | |
889 | INIT: |
943 | INIT: |
890 | CHECK_REPEAT (repeat); |
944 | CHECK_REPEAT (repeat); |
891 | CODE: |
945 | CODE: |
892 | RESET (timer, w, (w, after, repeat)); |
946 | RESET (timer, w, (w, after, repeat)); |
893 | |
947 | |
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_ |
948 | MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_ |
901 | |
949 | |
902 | void ev_periodic_start (ev_periodic *w) |
950 | void ev_periodic_start (ev_periodic *w) |
903 | INIT: |
951 | INIT: |
904 | CHECK_REPEAT (w->interval); |
952 | CHECK_REPEAT (w->interval); |
… | |
… | |
931 | RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0)); |
979 | RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0)); |
932 | } |
980 | } |
933 | |
981 | |
934 | NV at (ev_periodic *w) |
982 | NV at (ev_periodic *w) |
935 | CODE: |
983 | CODE: |
936 | RETVAL = w->at; |
984 | RETVAL = ev_periodic_at (w); |
937 | OUTPUT: |
985 | OUTPUT: |
938 | RETVAL |
986 | RETVAL |
939 | |
987 | |
940 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
988 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
941 | |
989 | |
… | |
… | |
1168 | e_destroy (w); |
1216 | e_destroy (w); |
1169 | |
1217 | |
1170 | void ev_async_send (ev_async *w) |
1218 | void ev_async_send (ev_async *w) |
1171 | C_ARGS: e_loop (w), w |
1219 | C_ARGS: e_loop (w), w |
1172 | |
1220 | |
|
|
1221 | SV *ev_async_async_pending (ev_async *w) |
|
|
1222 | CODE: |
|
|
1223 | RETVAL = boolSV (ev_async_pending (w)); |
|
|
1224 | OUTPUT: |
|
|
1225 | RETVAL |
|
|
1226 | |
1173 | MODULE = EV PACKAGE = EV::Loop PREFIX = ev_ |
1227 | MODULE = EV PACKAGE = EV::Loop PREFIX = ev_ |
1174 | |
1228 | |
1175 | SV *new (SV *klass, unsigned int flags = 0) |
1229 | SV *new (SV *klass, unsigned int flags = 0) |
1176 | CODE: |
1230 | CODE: |
1177 | { |
1231 | { |
… | |
… | |
1190 | if (loop != evapi.default_loop) /* global destruction sucks */ |
1244 | if (loop != evapi.default_loop) /* global destruction sucks */ |
1191 | ev_loop_destroy (loop); |
1245 | ev_loop_destroy (loop); |
1192 | |
1246 | |
1193 | void ev_loop_fork (struct ev_loop *loop) |
1247 | void ev_loop_fork (struct ev_loop *loop) |
1194 | |
1248 | |
|
|
1249 | void ev_loop_verify (struct ev_loop *loop) |
|
|
1250 | |
1195 | NV ev_now (struct ev_loop *loop) |
1251 | NV ev_now (struct ev_loop *loop) |
|
|
1252 | |
|
|
1253 | void ev_now_update (struct ev_loop *loop) |
1196 | |
1254 | |
1197 | void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) |
1255 | void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) |
1198 | |
1256 | |
1199 | void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) |
1257 | void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) |
1200 | |
1258 | |
… | |
… | |
1344 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
1402 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
1345 | if (!ix) START (stat, RETVAL); |
1403 | if (!ix) START (stat, RETVAL); |
1346 | OUTPUT: |
1404 | OUTPUT: |
1347 | RETVAL |
1405 | RETVAL |
1348 | |
1406 | |
1349 | ev_embed *embed (struct ev_loop *loop, struct ev_loop *other, SV *cb = &PL_sv_undef) |
1407 | ev_embed *embed (struct ev_loop *loop, struct ev_loop *other, SV *cb = 0) |
1350 | ALIAS: |
1408 | ALIAS: |
1351 | embed_ns = 1 |
1409 | embed_ns = 1 |
1352 | CODE: |
1410 | CODE: |
1353 | { |
1411 | { |
1354 | if (!(ev_backend (other) & ev_embeddable_backends ())) |
1412 | if (!(ev_backend (other) & ev_embeddable_backends ())) |
1355 | croak ("passed loop is not embeddable via EV::embed,"); |
1413 | croak ("passed loop is not embeddable via EV::embed,"); |
1356 | |
1414 | |
1357 | RETVAL = e_new (sizeof (ev_embed), cb, ST (0)); |
1415 | RETVAL = e_new (sizeof (ev_embed), cb, ST (0)); |
1358 | RETVAL->fh = newSVsv (ST (1)); |
1416 | RETVAL->fh = newSVsv (ST (1)); |
1359 | ev_embed_set (RETVAL, other); |
1417 | ev_embed_set (RETVAL, other); |
1360 | |
|
|
1361 | if (!SvOK (cb)) ev_set_cb (RETVAL, 0); |
|
|
1362 | |
|
|
1363 | if (!ix) START (embed, RETVAL); |
1418 | if (!ix) START (embed, RETVAL); |
1364 | } |
1419 | } |
1365 | OUTPUT: |
1420 | OUTPUT: |
1366 | RETVAL |
1421 | RETVAL |
1367 | |
1422 | |