… | |
… | |
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)) |
… | |
… | |
390 | evapi.sv_fileno = sv_fileno; |
437 | evapi.sv_fileno = sv_fileno; |
391 | evapi.sv_signum = sv_signum; |
438 | evapi.sv_signum = sv_signum; |
392 | evapi.supported_backends = ev_supported_backends (); |
439 | evapi.supported_backends = ev_supported_backends (); |
393 | evapi.recommended_backends = ev_recommended_backends (); |
440 | evapi.recommended_backends = ev_recommended_backends (); |
394 | evapi.embeddable_backends = ev_embeddable_backends (); |
441 | evapi.embeddable_backends = ev_embeddable_backends (); |
395 | evapi.time = ev_time; |
442 | evapi.time_ = ev_time; |
396 | evapi.sleep = ev_sleep; |
443 | evapi.sleep_ = ev_sleep; |
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; |
… | |
… | |
477 | NV ev_time () |
525 | NV ev_time () |
478 | |
526 | |
479 | NV ev_now () |
527 | NV ev_now () |
480 | C_ARGS: evapi.default_loop |
528 | C_ARGS: evapi.default_loop |
481 | |
529 | |
|
|
530 | void ev_now_update () |
|
|
531 | C_ARGS: evapi.default_loop |
|
|
532 | |
482 | unsigned int ev_backend () |
533 | unsigned int ev_backend () |
483 | C_ARGS: evapi.default_loop |
534 | C_ARGS: evapi.default_loop |
484 | |
535 | |
485 | unsigned int ev_loop_count () |
536 | unsigned int ev_loop_count () |
486 | C_ARGS: evapi.default_loop |
537 | C_ARGS: evapi.default_loop |
… | |
… | |
628 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
679 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
629 | if (!ix) START (stat, RETVAL); |
680 | if (!ix) START (stat, RETVAL); |
630 | OUTPUT: |
681 | OUTPUT: |
631 | RETVAL |
682 | RETVAL |
632 | |
683 | |
633 | ev_embed *embed (struct ev_loop *loop, SV *cb = &PL_sv_undef) |
684 | ev_embed *embed (struct ev_loop *loop, SV *cb = 0) |
634 | ALIAS: |
685 | ALIAS: |
635 | embed_ns = 1 |
686 | embed_ns = 1 |
636 | CODE: |
687 | CODE: |
637 | { |
688 | { |
638 | if (!(ev_backend (loop) & ev_embeddable_backends ())) |
689 | if (!(ev_backend (loop) & ev_embeddable_backends ())) |
639 | croak ("passed loop is not embeddable via EV::embed,"); |
690 | croak ("passed loop is not embeddable via EV::embed,"); |
640 | |
691 | |
641 | RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv); |
692 | RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv); |
642 | RETVAL->fh = newSVsv (ST (0)); |
693 | RETVAL->fh = newSVsv (ST (0)); |
643 | ev_embed_set (RETVAL, loop); |
694 | ev_embed_set (RETVAL, loop); |
644 | |
|
|
645 | if (!SvOK (cb)) ev_set_cb (RETVAL, 0); |
|
|
646 | |
|
|
647 | if (!ix) START (embed, RETVAL); |
695 | if (!ix) START (embed, RETVAL); |
648 | } |
696 | } |
649 | OUTPUT: |
697 | OUTPUT: |
650 | RETVAL |
698 | RETVAL |
651 | |
699 | |
… | |
… | |
703 | RETVAL |
751 | RETVAL |
704 | |
752 | |
705 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
753 | SV *cb (ev_watcher *w, SV *new_cb = 0) |
706 | CODE: |
754 | CODE: |
707 | { |
755 | { |
708 | RETVAL = newSVsv (w->cb_sv); |
|
|
709 | |
|
|
710 | if (items > 1) |
756 | if (items > 1) |
711 | sv_setsv (w->cb_sv, new_cb); |
757 | { |
|
|
758 | new_cb = e_get_cv (new_cb); |
|
|
759 | RETVAL = newRV_noinc (w->cb_sv); |
|
|
760 | w->cb_sv = SvREFCNT_inc (new_cb); |
|
|
761 | } |
|
|
762 | else |
|
|
763 | RETVAL = newRV_inc (w->cb_sv); |
712 | } |
764 | } |
713 | OUTPUT: |
765 | OUTPUT: |
714 | RETVAL |
766 | RETVAL |
715 | |
767 | |
716 | SV *data (ev_watcher *w, SV *new_data = 0) |
768 | SV *data (ev_watcher *w, SV *new_data = 0) |
… | |
… | |
889 | INIT: |
941 | INIT: |
890 | CHECK_REPEAT (repeat); |
942 | CHECK_REPEAT (repeat); |
891 | CODE: |
943 | CODE: |
892 | RESET (timer, w, (w, after, repeat)); |
944 | RESET (timer, w, (w, after, repeat)); |
893 | |
945 | |
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_ |
946 | MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_ |
901 | |
947 | |
902 | void ev_periodic_start (ev_periodic *w) |
948 | void ev_periodic_start (ev_periodic *w) |
903 | INIT: |
949 | INIT: |
904 | CHECK_REPEAT (w->interval); |
950 | CHECK_REPEAT (w->interval); |
… | |
… | |
931 | RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0)); |
977 | RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0)); |
932 | } |
978 | } |
933 | |
979 | |
934 | NV at (ev_periodic *w) |
980 | NV at (ev_periodic *w) |
935 | CODE: |
981 | CODE: |
936 | RETVAL = w->at; |
982 | RETVAL = ev_periodic_at (w); |
937 | OUTPUT: |
983 | OUTPUT: |
938 | RETVAL |
984 | RETVAL |
939 | |
985 | |
940 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
986 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
941 | |
987 | |
… | |
… | |
1168 | e_destroy (w); |
1214 | e_destroy (w); |
1169 | |
1215 | |
1170 | void ev_async_send (ev_async *w) |
1216 | void ev_async_send (ev_async *w) |
1171 | C_ARGS: e_loop (w), w |
1217 | C_ARGS: e_loop (w), w |
1172 | |
1218 | |
|
|
1219 | SV *ev_async_async_pending (ev_async *w) |
|
|
1220 | CODE: |
|
|
1221 | RETVAL = boolSV (ev_async_pending (w)); |
|
|
1222 | OUTPUT: |
|
|
1223 | RETVAL |
|
|
1224 | |
1173 | MODULE = EV PACKAGE = EV::Loop PREFIX = ev_ |
1225 | MODULE = EV PACKAGE = EV::Loop PREFIX = ev_ |
1174 | |
1226 | |
1175 | SV *new (SV *klass, unsigned int flags = 0) |
1227 | SV *new (SV *klass, unsigned int flags = 0) |
1176 | CODE: |
1228 | CODE: |
1177 | { |
1229 | { |
… | |
… | |
1190 | if (loop != evapi.default_loop) /* global destruction sucks */ |
1242 | if (loop != evapi.default_loop) /* global destruction sucks */ |
1191 | ev_loop_destroy (loop); |
1243 | ev_loop_destroy (loop); |
1192 | |
1244 | |
1193 | void ev_loop_fork (struct ev_loop *loop) |
1245 | void ev_loop_fork (struct ev_loop *loop) |
1194 | |
1246 | |
|
|
1247 | void ev_loop_verify (struct ev_loop *loop) |
|
|
1248 | |
1195 | NV ev_now (struct ev_loop *loop) |
1249 | NV ev_now (struct ev_loop *loop) |
|
|
1250 | |
|
|
1251 | void ev_now_update (struct ev_loop *loop) |
1196 | |
1252 | |
1197 | void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) |
1253 | void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) |
1198 | |
1254 | |
1199 | void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) |
1255 | void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) |
1200 | |
1256 | |
… | |
… | |
1344 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
1400 | ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); |
1345 | if (!ix) START (stat, RETVAL); |
1401 | if (!ix) START (stat, RETVAL); |
1346 | OUTPUT: |
1402 | OUTPUT: |
1347 | RETVAL |
1403 | RETVAL |
1348 | |
1404 | |
1349 | ev_embed *embed (struct ev_loop *loop, struct ev_loop *other, SV *cb = &PL_sv_undef) |
1405 | ev_embed *embed (struct ev_loop *loop, struct ev_loop *other, SV *cb = 0) |
1350 | ALIAS: |
1406 | ALIAS: |
1351 | embed_ns = 1 |
1407 | embed_ns = 1 |
1352 | CODE: |
1408 | CODE: |
1353 | { |
1409 | { |
1354 | if (!(ev_backend (other) & ev_embeddable_backends ())) |
1410 | if (!(ev_backend (other) & ev_embeddable_backends ())) |
1355 | croak ("passed loop is not embeddable via EV::embed,"); |
1411 | croak ("passed loop is not embeddable via EV::embed,"); |
1356 | |
1412 | |
1357 | RETVAL = e_new (sizeof (ev_embed), cb, ST (0)); |
1413 | RETVAL = e_new (sizeof (ev_embed), cb, ST (0)); |
1358 | RETVAL->fh = newSVsv (ST (1)); |
1414 | RETVAL->fh = newSVsv (ST (1)); |
1359 | ev_embed_set (RETVAL, other); |
1415 | ev_embed_set (RETVAL, other); |
1360 | |
|
|
1361 | if (!SvOK (cb)) ev_set_cb (RETVAL, 0); |
|
|
1362 | |
|
|
1363 | if (!ix) START (embed, RETVAL); |
1416 | if (!ix) START (embed, RETVAL); |
1364 | } |
1417 | } |
1365 | OUTPUT: |
1418 | OUTPUT: |
1366 | RETVAL |
1419 | RETVAL |
1367 | |
1420 | |