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 | /* fix perl api breakage */ |
5 | /* fix perl api breakage */ |
|
|
6 | #ifndef WIN32 |
6 | #undef signal |
7 | # undef signal |
7 | #undef sigaction |
8 | # undef sigaction |
|
|
9 | #endif |
8 | |
10 | |
9 | #include "schmorp.h" |
11 | #include "schmorp.h" |
10 | |
12 | |
11 | /* old API compatibility */ |
13 | /* old API compatibility */ |
12 | static int |
14 | static int |
… | |
… | |
35 | # define fd_mask Perl_fd_mask |
37 | # define fd_mask Perl_fd_mask |
36 | #endif |
38 | #endif |
37 | /* due to bugs in OS X we have to use libev/ explicitly here */ |
39 | /* due to bugs in OS X we have to use libev/ explicitly here */ |
38 | #include "libev/ev.c" |
40 | #include "libev/ev.c" |
39 | |
41 | |
40 | #if !defined _WIN32 && !defined _MINIX |
42 | #if !defined _WIN32 && !defined _MINIX && !EV_NO_ATFORK |
41 | # include <pthread.h> |
43 | # include <pthread.h> |
42 | #endif |
44 | #endif |
43 | |
45 | |
44 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX (((ev_watcher *)(w))->loop)) |
46 | #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX (((ev_watcher *)(w))->loop)) |
45 | #define e_flags(w) ((ev_watcher *)(w))->e_flags |
47 | #define e_flags(w) ((ev_watcher *)(w))->e_flags |
… | |
… | |
47 | #define e_fh(w) ((ev_watcher *)(w))->fh |
49 | #define e_fh(w) ((ev_watcher *)(w))->fh |
48 | #define e_data(w) ((ev_watcher *)(w))->data |
50 | #define e_data(w) ((ev_watcher *)(w))->data |
49 | |
51 | |
50 | #define WFLAG_KEEPALIVE 1 |
52 | #define WFLAG_KEEPALIVE 1 |
51 | #define WFLAG_UNREFED 2 /* has been unref'ed */ |
53 | #define WFLAG_UNREFED 2 /* has been unref'ed */ |
52 | #define WFLAG_NOARGS 4 /* do not pass anything to the callback */ |
|
|
53 | |
54 | |
54 | #define UNREF(w) \ |
55 | #define UNREF(w) \ |
55 | if (!(e_flags (w) & (WFLAG_KEEPALIVE | WFLAG_UNREFED)) \ |
56 | if (!(e_flags (w) & (WFLAG_KEEPALIVE | WFLAG_UNREFED)) \ |
56 | && ev_is_active (w)) \ |
57 | && ev_is_active (w)) \ |
57 | { \ |
58 | { \ |
… | |
… | |
200 | /* libev might have stopped the watcher */ |
201 | /* libev might have stopped the watcher */ |
201 | if (expect_false (w->e_flags & WFLAG_UNREFED) |
202 | if (expect_false (w->e_flags & WFLAG_UNREFED) |
202 | && !ev_is_active (w)) |
203 | && !ev_is_active (w)) |
203 | REF (w); |
204 | REF (w); |
204 | |
205 | |
205 | PUSHMARK (SP); |
206 | if (expect_true (sv_self_cache)) |
206 | |
|
|
207 | if (!expect_true (e_flags (w) & WFLAG_NOARGS)) |
|
|
208 | { |
207 | { |
209 | if (expect_true (sv_self_cache)) |
|
|
210 | { |
|
|
211 | sv_self = sv_self_cache; sv_self_cache = 0; |
208 | sv_self = sv_self_cache; sv_self_cache = 0; |
212 | SvRV_set (sv_self, SvREFCNT_inc_NN (w->self)); |
209 | SvRV_set (sv_self, SvREFCNT_inc_NN (w->self)); |
213 | } |
|
|
214 | else |
|
|
215 | { |
|
|
216 | sv_self = newRV_inc (w->self); /* e_self (w) MUST be blessed by now */ |
|
|
217 | SvREADONLY_on (sv_self); |
|
|
218 | } |
|
|
219 | |
|
|
220 | if (expect_true (sv_events_cache)) |
|
|
221 | { |
|
|
222 | sv_events = sv_events_cache; sv_events_cache = 0; |
|
|
223 | SvIV_set (sv_events, revents); |
|
|
224 | SvIOK_only (sv_events); |
|
|
225 | } |
|
|
226 | else |
|
|
227 | { |
|
|
228 | sv_events = newSViv (revents); |
|
|
229 | SvREADONLY_on (sv_events); |
|
|
230 | } |
|
|
231 | |
|
|
232 | EXTEND (SP, 2); |
|
|
233 | PUSHs (sv_self); |
|
|
234 | PUSHs (sv_events); |
|
|
235 | |
|
|
236 | PUTBACK; |
|
|
237 | call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); |
|
|
238 | |
|
|
239 | if (expect_false (SvREFCNT (sv_self) != 1 || sv_self_cache)) |
|
|
240 | SvREFCNT_dec (sv_self); |
|
|
241 | else |
|
|
242 | { |
|
|
243 | SvREFCNT_dec (SvRV (sv_self)); |
|
|
244 | SvRV_set (sv_self, &PL_sv_undef); |
|
|
245 | sv_self_cache = sv_self; |
|
|
246 | } |
|
|
247 | |
|
|
248 | if (expect_false (SvREFCNT (sv_events) != 1 || sv_events_cache)) |
|
|
249 | SvREFCNT_dec (sv_events); |
|
|
250 | else |
|
|
251 | sv_events_cache = sv_events; |
|
|
252 | } |
210 | } |
253 | else |
211 | else |
254 | { |
212 | { |
255 | PUTBACK; |
213 | sv_self = newRV_inc (w->self); /* e_self (w) MUST be blessed by now */ |
256 | call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); |
214 | SvREADONLY_on (sv_self); |
257 | } |
215 | } |
|
|
216 | |
|
|
217 | if (expect_true (sv_events_cache)) |
|
|
218 | { |
|
|
219 | sv_events = sv_events_cache; sv_events_cache = 0; |
|
|
220 | SvIV_set (sv_events, revents); |
|
|
221 | SvIOK_only (sv_events); |
|
|
222 | } |
|
|
223 | else |
|
|
224 | { |
|
|
225 | sv_events = newSViv (revents); |
|
|
226 | SvREADONLY_on (sv_events); |
|
|
227 | } |
|
|
228 | |
|
|
229 | PUSHMARK (SP); |
|
|
230 | EXTEND (SP, 2); |
|
|
231 | PUSHs (sv_self); |
|
|
232 | PUSHs (sv_events); |
|
|
233 | |
|
|
234 | PUTBACK; |
|
|
235 | call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); |
|
|
236 | |
|
|
237 | if (expect_false (SvREFCNT (sv_self) != 1 || sv_self_cache)) |
|
|
238 | SvREFCNT_dec (sv_self); |
|
|
239 | else |
|
|
240 | { |
|
|
241 | SvREFCNT_dec (SvRV (sv_self)); |
|
|
242 | SvRV_set (sv_self, &PL_sv_undef); |
|
|
243 | sv_self_cache = sv_self; |
|
|
244 | } |
|
|
245 | |
|
|
246 | if (expect_false (SvREFCNT (sv_events) != 1 || sv_events_cache)) |
|
|
247 | SvREFCNT_dec (sv_events); |
|
|
248 | else |
|
|
249 | sv_events_cache = sv_events; |
258 | |
250 | |
259 | if (expect_false (SvTRUE (ERRSV))) |
251 | if (expect_false (SvTRUE (ERRSV))) |
260 | { |
252 | { |
261 | SPAGAIN; |
253 | SPAGAIN; |
262 | PUSHMARK (SP); |
254 | PUSHMARK (SP); |
… | |
… | |
541 | evapi.invoke = ev_invoke; |
533 | evapi.invoke = ev_invoke; |
542 | |
534 | |
543 | sv_setiv (sv, (IV)&evapi); |
535 | sv_setiv (sv, (IV)&evapi); |
544 | SvREADONLY_on (sv); |
536 | SvREADONLY_on (sv); |
545 | } |
537 | } |
546 | #if !defined _WIN32 && !defined _MINIX |
538 | #if !defined _WIN32 && !defined _MINIX && !EV_NO_ATFORK |
547 | #if __linux |
539 | #if __linux |
548 | int __register_atfork(void (*prepare) (void), void (*parent) (void), void (*child) (void), void * __dso_handle); |
540 | int __register_atfork(void (*prepare) (void), void (*parent) (void), void (*child) (void), void * __dso_handle); |
549 | __register_atfork (0, 0, default_fork, 0); |
541 | __register_atfork (0, 0, default_fork, 0); |
550 | #else |
542 | #else |
551 | pthread_atfork (0, 0, default_fork); |
543 | pthread_atfork (0, 0, default_fork); |
… | |
… | |
588 | NV ev_time () |
580 | NV ev_time () |
589 | |
581 | |
590 | void ev_feed_signal (SV *signal) |
582 | void ev_feed_signal (SV *signal) |
591 | CODE: |
583 | CODE: |
592 | { |
584 | { |
593 | Signal signum = s_signum (signal); |
585 | Signal signum = s_signum (signal); |
594 | CHECK_SIG (signal, signum); |
586 | CHECK_SIG (signal, signum); |
595 | |
587 | |
596 | ev_feed_signal (signum); |
588 | ev_feed_signal (signum); |
597 | } |
589 | } |
598 | |
590 | |
… | |
… | |
666 | _ae_io = 2 |
658 | _ae_io = 2 |
667 | CODE: |
659 | CODE: |
668 | { |
660 | { |
669 | int fd = s_fileno (fh, events & EV_WRITE); |
661 | int fd = s_fileno (fh, events & EV_WRITE); |
670 | CHECK_FD (fh, fd); |
662 | CHECK_FD (fh, fd); |
|
|
663 | |
|
|
664 | if (ix == 2) |
|
|
665 | { |
|
|
666 | ix = 0; |
|
|
667 | events = events ? EV_WRITE : EV_READ; |
|
|
668 | } |
|
|
669 | |
671 | RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); |
670 | RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); |
672 | e_fh (RETVAL) = newSVsv (fh); |
671 | e_fh (RETVAL) = newSVsv (fh); |
673 | |
|
|
674 | if (ix == 2) |
|
|
675 | { |
|
|
676 | events = events ? EV_WRITE : EV_READ; |
|
|
677 | e_flags (RETVAL) |= WFLAG_NOARGS; |
|
|
678 | } |
|
|
679 | |
|
|
680 | ev_io_set (RETVAL, fd, events); |
672 | ev_io_set (RETVAL, fd, events); |
681 | if (ix != 1) START (io, RETVAL); |
673 | if (!ix) START (io, RETVAL); |
682 | } |
674 | } |
683 | OUTPUT: |
675 | OUTPUT: |
684 | RETVAL |
676 | RETVAL |
685 | |
677 | |
686 | ev_timer *timer (NV after, NV repeat, SV *cb) |
678 | ev_timer *timer (NV after, NV repeat, SV *cb) |
… | |
… | |
700 | periodic_ns = 1 |
692 | periodic_ns = 1 |
701 | INIT: |
693 | INIT: |
702 | CHECK_REPEAT (interval); |
694 | CHECK_REPEAT (interval); |
703 | CODE: |
695 | CODE: |
704 | { |
696 | { |
705 | ev_periodic *w; |
697 | ev_periodic *w; |
706 | w = e_new (sizeof (ev_periodic), cb, default_loop_sv); |
698 | w = e_new (sizeof (ev_periodic), cb, default_loop_sv); |
707 | e_fh (w) = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0; |
699 | e_fh (w) = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0; |
708 | ev_periodic_set (w, at, interval, e_fh (w) ? e_periodic_cb : 0); |
700 | ev_periodic_set (w, at, interval, e_fh (w) ? e_periodic_cb : 0); |
709 | RETVAL = e_bless ((ev_watcher *)w, stash_periodic); |
701 | RETVAL = e_bless ((ev_watcher *)w, stash_periodic); |
710 | if (!ix) START (periodic, w); |
702 | if (!ix) START (periodic, w); |
… | |
… | |
715 | ev_signal *signal (SV *signal, SV *cb) |
707 | ev_signal *signal (SV *signal, SV *cb) |
716 | ALIAS: |
708 | ALIAS: |
717 | signal_ns = 1 |
709 | signal_ns = 1 |
718 | CODE: |
710 | CODE: |
719 | { |
711 | { |
720 | Signal signum = s_signum (signal); |
712 | Signal signum = s_signum (signal); |
721 | CHECK_SIG (signal, signum); |
713 | CHECK_SIG (signal, signum); |
722 | |
714 | |
723 | RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); |
715 | RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); |
724 | ev_signal_set (RETVAL, signum); |
716 | ev_signal_set (RETVAL, signum); |
725 | if (!ix) START_SIGNAL (RETVAL); |
717 | if (!ix) START_SIGNAL (RETVAL); |
… | |
… | |
1053 | |
1045 | |
1054 | void ev_timer_stop (ev_timer *w) |
1046 | void ev_timer_stop (ev_timer *w) |
1055 | CODE: |
1047 | CODE: |
1056 | STOP (timer, w); |
1048 | STOP (timer, w); |
1057 | |
1049 | |
1058 | void ev_timer_again (ev_timer *w) |
1050 | void ev_timer_again (ev_timer *w, NV repeat = NO_INIT) |
1059 | INIT: |
1051 | CODE: |
|
|
1052 | if (items > 1) |
|
|
1053 | w->repeat = repeat; |
1060 | CHECK_REPEAT (w->repeat); |
1054 | CHECK_REPEAT (w->repeat); |
1061 | CODE: |
|
|
1062 | ev_timer_again (e_loop (w), w); |
1055 | ev_timer_again (e_loop (w), w); |
1063 | UNREF (w); |
1056 | UNREF (w); |
1064 | |
1057 | |
1065 | NV ev_timer_remaining (ev_timer *w) |
1058 | NV ev_timer_remaining (ev_timer *w) |
1066 | C_ARGS: e_loop (w), w |
1059 | C_ARGS: e_loop (w), w |