ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.xs
(Generate patch)

Comparing EV/EV.xs (file contents):
Revision 1.125 by root, Wed Jul 8 02:46:05 2009 UTC vs.
Revision 1.134 by root, Fri Sep 18 21:28:33 2009 UTC

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
11/* old API compatibility */
12static int
13sv_fileno (SV *fh)
14{
15 return s_fileno (fh, 0);
16}
10 17
11#define EV_PROTOTYPES 1 18#define EV_PROTOTYPES 1
12#define EV_USE_NANOSLEEP EV_USE_MONOTONIC 19#define EV_USE_NANOSLEEP EV_USE_MONOTONIC
13#define EV_H <ev.h> 20#define EV_H <ev.h>
14#include "EV/EVAPI.h" 21#include "EV/EVAPI.h"
24 31
25#ifndef _WIN32 32#ifndef _WIN32
26# include <pthread.h> 33# include <pthread.h>
27#endif 34#endif
28 35
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#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) 36#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop))
40 37
41#define WFLAG_KEEPALIVE 1 38#define WFLAG_KEEPALIVE 1
42#define WFLAG_UNREFED 2 /* has been unref'ed */ 39#define WFLAG_UNREFED 2 /* has been unref'ed */
43 40
67 REF (w); \ 64 REF (w); \
68 ev_ ## type ## _stop (e_loop (w), w); \ 65 ev_ ## type ## _stop (e_loop (w), w); \
69 } while (0) 66 } while (0)
70 67
71#define RESET(type,w,seta) \ 68#define RESET(type,w,seta) \
72 do { \ 69 do { \
73 int active = ev_is_active (w); \ 70 int active = ev_is_active (w); \
74 if (active) STOP (type, w); \ 71 if (active) STOP (type, w); \
75 ev_ ## type ## _set seta; \ 72 ev_ ## type ## _set seta; \
76 if (active) START (type, w); \ 73 if (active) START (type, w); \
77 } while (0) 74 } while (0)
78 75
79typedef int Signal; 76typedef int Signal;
77
78/* horrible... */
79#define CHECK_SIGNAL_CAN_START(w) \
80 do { \
81 /* dive into the internals of libev to avoid aborting in libev */ \
82 if (signals [(w)->signum - 1].loop \
83 && signals [(w)->signum - 1].loop != e_loop (w)) \
84 croak ("unable to start signal watcher, signal %d already registered in another loop", w->signum); \
85 } while (0)
86
87#define START_SIGNAL(w) \
88 do { \
89 CHECK_SIGNAL_CAN_START (w); \
90 START (signal, w); \
91 } while (0) \
92
93#define RESET_SIGNAL(w,seta) \
94 do { \
95 int active = ev_is_active (w); \
96 if (active) STOP (signal, w); \
97 ev_ ## signal ## _set seta; \
98 if (active) START_SIGNAL (w); \
99 } while (0)
80 100
81static SV *default_loop_sv; 101static SV *default_loop_sv;
82 102
83static struct EVAPI evapi; 103static struct EVAPI evapi;
84 104
96 *stash_check, 116 *stash_check,
97 *stash_embed, 117 *stash_embed,
98 *stash_fork, 118 *stash_fork,
99 *stash_async; 119 *stash_async;
100 120
101#ifndef SIG_SIZE
102/* kudos to Slaven Rezic for the idea */
103static char sig_size [] = { SIG_NUM };
104# define SIG_SIZE (sizeof (sig_size) + 1)
105#endif
106
107static Signal
108sv_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///////////////////////////////////////////////////////////////////////////// 121/////////////////////////////////////////////////////////////////////////////
127// Event 122// Event
128 123
129static void e_cb (EV_P_ ev_watcher *w, int revents); 124static void e_cb (EV_P_ ev_watcher *w, int revents);
130 125
131static int
132sv_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
148static SV *
149e_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
161static void * 126static void *
162e_new (int size, SV *cb_sv, SV *loop) 127e_new (int size, SV *cb_sv, SV *loop)
163{ 128{
164 SV *cv = cb_sv ? e_get_cv (cb_sv) : 0; 129 SV *cv = cb_sv ? s_get_cv_croak (cb_sv) : 0;
165 ev_watcher *w; 130 ev_watcher *w;
166 SV *self = NEWSV (0, size); 131 SV *self = NEWSV (0, size);
167 SvPOK_only (self); 132 SvPOK_only (self);
168 SvCUR_set (self, size); 133 SvCUR_set (self, size);
169 134
421 const_iv (EV, BACKEND_POLL) 386 const_iv (EV, BACKEND_POLL)
422 const_iv (EV, BACKEND_EPOLL) 387 const_iv (EV, BACKEND_EPOLL)
423 const_iv (EV, BACKEND_KQUEUE) 388 const_iv (EV, BACKEND_KQUEUE)
424 const_iv (EV, BACKEND_DEVPOLL) 389 const_iv (EV, BACKEND_DEVPOLL)
425 const_iv (EV, BACKEND_PORT) 390 const_iv (EV, BACKEND_PORT)
391 const_iv (EV, BACKEND_ALL)
426 const_iv (EV, FLAG_AUTO) 392 const_iv (EV, FLAG_AUTO)
393 const_iv (EV, FLAG_FORKCHECK)
427 const_iv (EV, FLAG_NOENV) 394 const_iv (EV, FLAG_NOENV)
428 const_iv (EV, FLAG_FORKCHECK) 395 const_iv (EV, FLAG_NOSIGFD)
396 const_iv (EV, FLAG_NOINOTIFY)
429 397
430 const_iv (EV_, VERSION_MAJOR) 398 const_iv (EV_, VERSION_MAJOR)
431 const_iv (EV_, VERSION_MINOR) 399 const_iv (EV_, VERSION_MINOR)
432 }; 400 };
433 401
455 423
456 /* the poor man's shared library emulator */ 424 /* the poor man's shared library emulator */
457 evapi.ver = EV_API_VERSION; 425 evapi.ver = EV_API_VERSION;
458 evapi.rev = EV_API_REVISION; 426 evapi.rev = EV_API_REVISION;
459 evapi.sv_fileno = sv_fileno; 427 evapi.sv_fileno = sv_fileno;
460 evapi.sv_signum = sv_signum; 428 evapi.sv_signum = s_signum;
461 evapi.supported_backends = ev_supported_backends (); 429 evapi.supported_backends = ev_supported_backends ();
462 evapi.recommended_backends = ev_recommended_backends (); 430 evapi.recommended_backends = ev_recommended_backends ();
463 evapi.embeddable_backends = ev_embeddable_backends (); 431 evapi.embeddable_backends = ev_embeddable_backends ();
464 evapi.time_ = ev_time; 432 evapi.time_ = ev_time;
465 evapi.sleep_ = ev_sleep; 433 evapi.sleep_ = ev_sleep;
466 evapi.loop_new = ev_loop_new; 434 evapi.loop_new = ev_loop_new;
467 evapi.loop_destroy = ev_loop_destroy; 435 evapi.loop_destroy = ev_loop_destroy;
468 evapi.loop_fork = ev_loop_fork; 436 evapi.loop_fork = ev_loop_fork;
469 evapi.loop_count = ev_loop_count; 437 evapi.loop_count = ev_loop_count;
470 evapi.loop_depth = ev_loop_depth; 438 evapi.loop_depth = ev_loop_depth;
439 evapi.set_userdata = ev_set_userdata;
440 evapi.userdata = ev_userdata;
471 evapi.now = ev_now; 441 evapi.now = ev_now;
472 evapi.now_update = ev_now_update; 442 evapi.now_update = ev_now_update;
473 evapi.suspend = ev_suspend; 443 evapi.suspend = ev_suspend;
474 evapi.resume = ev_resume; 444 evapi.resume = ev_resume;
475 evapi.backend = ev_backend; 445 evapi.backend = ev_backend;
476 evapi.unloop = ev_unloop; 446 evapi.unloop = ev_unloop;
447 evapi.invoke_pending = ev_invoke_pending;
448 evapi.pending_count = ev_pending_count;
449 evapi.set_loop_release_cb = ev_set_loop_release_cb;
450 evapi.set_invoke_pending_cb= ev_set_invoke_pending_cb;
477 evapi.ref = ev_ref; 451 evapi.ref = ev_ref;
478 evapi.unref = ev_unref; 452 evapi.unref = ev_unref;
479 evapi.loop = ev_loop; 453 evapi.loop = ev_loop;
480 evapi.once = ev_once; 454 evapi.once = ev_once;
481 evapi.io_start = ev_io_start; 455 evapi.io_start = ev_io_start;
482 evapi.io_stop = ev_io_stop; 456 evapi.io_stop = ev_io_stop;
483 evapi.timer_start = ev_timer_start; 457 evapi.timer_start = ev_timer_start;
484 evapi.timer_stop = ev_timer_stop; 458 evapi.timer_stop = ev_timer_stop;
485 evapi.timer_again = ev_timer_again; 459 evapi.timer_again = ev_timer_again;
460 evapi.timer_remaining = ev_timer_remaining;
486 evapi.periodic_start = ev_periodic_start; 461 evapi.periodic_start = ev_periodic_start;
487 evapi.periodic_stop = ev_periodic_stop; 462 evapi.periodic_stop = ev_periodic_stop;
488 evapi.signal_start = ev_signal_start; 463 evapi.signal_start = ev_signal_start;
489 evapi.signal_stop = ev_signal_stop; 464 evapi.signal_stop = ev_signal_stop;
490 evapi.idle_start = ev_idle_start; 465 evapi.idle_start = ev_idle_start;
591 C_ARGS: evapi.default_loop, fd, revents 566 C_ARGS: evapi.default_loop, fd, revents
592 567
593void ev_feed_signal_event (SV *signal) 568void ev_feed_signal_event (SV *signal)
594 CODE: 569 CODE:
595{ 570{
596 Signal signum = sv_signum (signal); 571 Signal signum = s_signum (signal);
597 CHECK_SIG (signal, signum); 572 CHECK_SIG (signal, signum);
598 573
599 ev_feed_signal_event (evapi.default_loop, signum); 574 ev_feed_signal_event (evapi.default_loop, signum);
600} 575}
601 576
577unsigned int ev_pending_count ()
578 C_ARGS: evapi.default_loop
579
580void ev_invoke_pending ()
581 C_ARGS: evapi.default_loop
582
602ev_io *io (SV *fh, int events, SV *cb) 583ev_io *io (SV *fh, int events, SV *cb)
603 ALIAS: 584 ALIAS:
604 io_ns = 1 585 io_ns = 1
586 _ae_io = 2
605 CODE: 587 CODE:
606{ 588{
607 int fd = sv_fileno (fh); 589 int fd = s_fileno (fh, events & EV_WRITE);
608 CHECK_FD (fh, fd); 590 CHECK_FD (fh, fd);
591
592 if (ix == 2)
593 {
594 ix = 0;
595 events = events ? EV_WRITE : EV_READ;
596 }
609 597
610 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); 598 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv);
611 RETVAL->fh = newSVsv (fh); 599 RETVAL->fh = newSVsv (fh);
612 ev_io_set (RETVAL, fd, events); 600 ev_io_set (RETVAL, fd, events);
613 if (!ix) START (io, RETVAL); 601 if (!ix) START (io, RETVAL);
647ev_signal *signal (SV *signal, SV *cb) 635ev_signal *signal (SV *signal, SV *cb)
648 ALIAS: 636 ALIAS:
649 signal_ns = 1 637 signal_ns = 1
650 CODE: 638 CODE:
651{ 639{
652 Signal signum = sv_signum (signal); 640 Signal signum = s_signum (signal);
653 CHECK_SIG (signal, signum); 641 CHECK_SIG (signal, signum);
654 642
655 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); 643 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv);
656 ev_signal_set (RETVAL, signum); 644 ev_signal_set (RETVAL, signum);
657 if (!ix) START (signal, RETVAL); 645 if (!ix) START_SIGNAL (RETVAL);
658} 646}
659 OUTPUT: 647 OUTPUT:
660 RETVAL 648 RETVAL
661 649
662ev_idle *idle (SV *cb) 650ev_idle *idle (SV *cb)
748 736
749void once (SV *fh, int events, SV *timeout, SV *cb) 737void once (SV *fh, int events, SV *timeout, SV *cb)
750 CODE: 738 CODE:
751 ev_once ( 739 ev_once (
752 evapi.default_loop, 740 evapi.default_loop,
753 sv_fileno (fh), events, 741 s_fileno (fh, events & EV_WRITE), events,
754 SvOK (timeout) ? SvNV (timeout) : -1., 742 SvOK (timeout) ? SvNV (timeout) : -1.,
755 e_once_cb, 743 e_once_cb,
756 newSVsv (cb) 744 newSVsv (cb)
757 ); 745 );
758 746
792SV *cb (ev_watcher *w, SV *new_cb = 0) 780SV *cb (ev_watcher *w, SV *new_cb = 0)
793 CODE: 781 CODE:
794{ 782{
795 if (items > 1) 783 if (items > 1)
796 { 784 {
797 new_cb = e_get_cv (new_cb); 785 new_cb = s_get_cv_croak (new_cb);
798 RETVAL = newRV_noinc (w->cb_sv); 786 RETVAL = newRV_noinc (w->cb_sv);
799 w->cb_sv = SvREFCNT_inc (new_cb); 787 w->cb_sv = SvREFCNT_inc (new_cb);
800 } 788 }
801 else 789 else
802 RETVAL = newRV_inc (w->cb_sv); 790 RETVAL = newRV_inc (w->cb_sv);
872 e_destroy (w); 860 e_destroy (w);
873 861
874void set (ev_io *w, SV *fh, int events) 862void set (ev_io *w, SV *fh, int events)
875 CODE: 863 CODE:
876{ 864{
877 int fd = sv_fileno (fh); 865 int fd = s_fileno (fh, events & EV_WRITE);
878 CHECK_FD (fh, fd); 866 CHECK_FD (fh, fd);
879 867
880 sv_setsv (w->fh, fh); 868 sv_setsv (w->fh, fh);
881 RESET (io, w, (w, fd, events)); 869 RESET (io, w, (w, fd, events));
882} 870}
884SV *fh (ev_io *w, SV *new_fh = 0) 872SV *fh (ev_io *w, SV *new_fh = 0)
885 CODE: 873 CODE:
886{ 874{
887 if (items > 1) 875 if (items > 1)
888 { 876 {
889 int fd = sv_fileno (new_fh); 877 int fd = s_fileno (new_fh, w->events & EV_WRITE);
890 CHECK_FD (new_fh, fd); 878 CHECK_FD (new_fh, fd);
891 879
892 RETVAL = w->fh; 880 RETVAL = w->fh;
893 w->fh = newSVsv (new_fh); 881 w->fh = newSVsv (new_fh);
894 882
913 901
914MODULE = EV PACKAGE = EV::Signal PREFIX = ev_signal_ 902MODULE = EV PACKAGE = EV::Signal PREFIX = ev_signal_
915 903
916void ev_signal_start (ev_signal *w) 904void ev_signal_start (ev_signal *w)
917 CODE: 905 CODE:
918 START (signal, w); 906 START_SIGNAL (w);
919 907
920void ev_signal_stop (ev_signal *w) 908void ev_signal_stop (ev_signal *w)
921 CODE: 909 CODE:
922 STOP (signal, w); 910 STOP (signal, w);
923 911
927 e_destroy (w); 915 e_destroy (w);
928 916
929void set (ev_signal *w, SV *signal) 917void set (ev_signal *w, SV *signal)
930 CODE: 918 CODE:
931{ 919{
932 Signal signum = sv_signum (signal); 920 Signal signum = s_signum (signal);
933 CHECK_SIG (signal, signum); 921 CHECK_SIG (signal, signum);
934 922
935 RESET (signal, w, (w, signum)); 923 RESET_SIGNAL (w, (w, signum));
936} 924}
937 925
938int signal (ev_signal *w, SV *new_signal = 0) 926int signal (ev_signal *w, SV *new_signal = 0)
939 CODE: 927 CODE:
940{ 928{
941 RETVAL = w->signum; 929 RETVAL = w->signum;
942 930
943 if (items > 1) 931 if (items > 1)
944 { 932 {
945 Signal signum = sv_signum (new_signal); 933 Signal signum = s_signum (new_signal);
946 CHECK_SIG (new_signal, signum); 934 CHECK_SIG (new_signal, signum);
947 935
948 RESET (signal, w, (w, signum)); 936 RESET_SIGNAL (w, (w, signum));
949 } 937 }
950} 938}
951 OUTPUT: 939 OUTPUT:
952 RETVAL 940 RETVAL
953 941
967 INIT: 955 INIT:
968 CHECK_REPEAT (w->repeat); 956 CHECK_REPEAT (w->repeat);
969 CODE: 957 CODE:
970 ev_timer_again (e_loop (w), w); 958 ev_timer_again (e_loop (w), w);
971 UNREF (w); 959 UNREF (w);
960
961NV ev_timer_remaining (ev_timer *w)
962 C_ARGS: e_loop (w), w
972 963
973void DESTROY (ev_timer *w) 964void DESTROY (ev_timer *w)
974 CODE: 965 CODE:
975 STOP (timer, w); 966 STOP (timer, w);
976 e_destroy (w); 967 e_destroy (w);
1305 1296
1306void ev_unloop (struct ev_loop *loop, int how = 1) 1297void ev_unloop (struct ev_loop *loop, int how = 1)
1307 1298
1308void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE) 1299void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE)
1309 1300
1301unsigned int ev_pending_count (struct ev_loop *loop)
1302
1303void ev_invoke_pending (struct ev_loop *loop)
1304
1310#if 0 1305#if 0
1311 1306
1312void ev_feed_signal_event (struct ev_loop *loop, SV *signal) 1307void ev_feed_signal_event (struct ev_loop *loop, SV *signal)
1313 CODE: 1308 CODE:
1314{ 1309{
1315 Signal signum = sv_signum (signal); 1310 Signal signum = s_signum (signal);
1316 CHECK_SIG (signal, signum); 1311 CHECK_SIG (signal, signum);
1317 1312
1318 ev_feed_signal_event (loop, signum); 1313 ev_feed_signal_event (loop, signum);
1319} 1314}
1320 1315
1323ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb) 1318ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb)
1324 ALIAS: 1319 ALIAS:
1325 io_ns = 1 1320 io_ns = 1
1326 CODE: 1321 CODE:
1327{ 1322{
1328 int fd = sv_fileno (fh); 1323 int fd = s_fileno (fh, events & EV_WRITE);
1329 CHECK_FD (fh, fd); 1324 CHECK_FD (fh, fd);
1330 1325
1331 RETVAL = e_new (sizeof (ev_io), cb, ST (0)); 1326 RETVAL = e_new (sizeof (ev_io), cb, ST (0));
1332 RETVAL->fh = newSVsv (fh); 1327 RETVAL->fh = newSVsv (fh);
1333 ev_io_set (RETVAL, fd, events); 1328 ev_io_set (RETVAL, fd, events);
1363 if (!ix) START (periodic, w); 1358 if (!ix) START (periodic, w);
1364} 1359}
1365 OUTPUT: 1360 OUTPUT:
1366 RETVAL 1361 RETVAL
1367 1362
1368#if 0
1369
1370ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) 1363ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb)
1371 ALIAS: 1364 ALIAS:
1372 signal_ns = 1 1365 signal_ns = 1
1373 CODE: 1366 CODE:
1374{ 1367{
1375 Signal signum = sv_signum (signal); 1368 Signal signum = s_signum (signal);
1376 CHECK_SIG (signal, signum); 1369 CHECK_SIG (signal, signum);
1377 1370
1378 RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); 1371 RETVAL = e_new (sizeof (ev_signal), cb, ST (0));
1379 ev_signal_set (RETVAL, signum); 1372 ev_signal_set (RETVAL, signum);
1380 if (!ix) START (signal, RETVAL); 1373 if (!ix) START_SIGNAL (RETVAL);
1381} 1374}
1382 OUTPUT: 1375 OUTPUT:
1383 RETVAL 1376 RETVAL
1384
1385#endif
1386 1377
1387ev_idle *idle (struct ev_loop *loop, SV *cb) 1378ev_idle *idle (struct ev_loop *loop, SV *cb)
1388 ALIAS: 1379 ALIAS:
1389 idle_ns = 1 1380 idle_ns = 1
1390 CODE: 1381 CODE:
1473 1464
1474void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb) 1465void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb)
1475 CODE: 1466 CODE:
1476 ev_once ( 1467 ev_once (
1477 loop, 1468 loop,
1478 sv_fileno (fh), events, 1469 s_fileno (fh, events & EV_WRITE), events,
1479 SvOK (timeout) ? SvNV (timeout) : -1., 1470 SvOK (timeout) ? SvNV (timeout) : -1.,
1480 e_once_cb, 1471 e_once_cb,
1481 newSVsv (cb) 1472 newSVsv (cb)
1482 ); 1473 );
1483 1474

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines