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.129 by root, Tue Jul 14 20:34:58 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"
22/* due to bugs in OS X we have to use libev/ explicitly here */ 29/* due to bugs in OS X we have to use libev/ explicitly here */
23#include "libev/ev.c" 30#include "libev/ev.c"
24 31
25#ifndef _WIN32 32#ifndef _WIN32
26# include <pthread.h> 33# include <pthread.h>
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 34#endif
38 35
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
96 *stash_check, 93 *stash_check,
97 *stash_embed, 94 *stash_embed,
98 *stash_fork, 95 *stash_fork,
99 *stash_async; 96 *stash_async;
100 97
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///////////////////////////////////////////////////////////////////////////// 98/////////////////////////////////////////////////////////////////////////////
127// Event 99// Event
128 100
129static void e_cb (EV_P_ ev_watcher *w, int revents); 101static void e_cb (EV_P_ ev_watcher *w, int revents);
130 102
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 * 103static void *
162e_new (int size, SV *cb_sv, SV *loop) 104e_new (int size, SV *cb_sv, SV *loop)
163{ 105{
164 SV *cv = cb_sv ? e_get_cv (cb_sv) : 0; 106 SV *cv = cb_sv ? s_get_cv_croak (cb_sv) : 0;
165 ev_watcher *w; 107 ev_watcher *w;
166 SV *self = NEWSV (0, size); 108 SV *self = NEWSV (0, size);
167 SvPOK_only (self); 109 SvPOK_only (self);
168 SvCUR_set (self, size); 110 SvCUR_set (self, size);
169 111
455 397
456 /* the poor man's shared library emulator */ 398 /* the poor man's shared library emulator */
457 evapi.ver = EV_API_VERSION; 399 evapi.ver = EV_API_VERSION;
458 evapi.rev = EV_API_REVISION; 400 evapi.rev = EV_API_REVISION;
459 evapi.sv_fileno = sv_fileno; 401 evapi.sv_fileno = sv_fileno;
460 evapi.sv_signum = sv_signum; 402 evapi.sv_signum = s_signum;
461 evapi.supported_backends = ev_supported_backends (); 403 evapi.supported_backends = ev_supported_backends ();
462 evapi.recommended_backends = ev_recommended_backends (); 404 evapi.recommended_backends = ev_recommended_backends ();
463 evapi.embeddable_backends = ev_embeddable_backends (); 405 evapi.embeddable_backends = ev_embeddable_backends ();
464 evapi.time_ = ev_time; 406 evapi.time_ = ev_time;
465 evapi.sleep_ = ev_sleep; 407 evapi.sleep_ = ev_sleep;
466 evapi.loop_new = ev_loop_new; 408 evapi.loop_new = ev_loop_new;
467 evapi.loop_destroy = ev_loop_destroy; 409 evapi.loop_destroy = ev_loop_destroy;
468 evapi.loop_fork = ev_loop_fork; 410 evapi.loop_fork = ev_loop_fork;
469 evapi.loop_count = ev_loop_count; 411 evapi.loop_count = ev_loop_count;
470 evapi.loop_depth = ev_loop_depth; 412 evapi.loop_depth = ev_loop_depth;
413 evapi.set_userdata = ev_set_userdata;
414 evapi.userdata = ev_userdata;
471 evapi.now = ev_now; 415 evapi.now = ev_now;
472 evapi.now_update = ev_now_update; 416 evapi.now_update = ev_now_update;
473 evapi.suspend = ev_suspend; 417 evapi.suspend = ev_suspend;
474 evapi.resume = ev_resume; 418 evapi.resume = ev_resume;
475 evapi.backend = ev_backend; 419 evapi.backend = ev_backend;
476 evapi.unloop = ev_unloop; 420 evapi.unloop = ev_unloop;
421 evapi.invoke_pending = ev_invoke_pending;
422 evapi.pending_count = ev_pending_count;
423 evapi.set_loop_release_cb = ev_set_loop_release_cb;
424 evapi.set_invoke_pending_cb= ev_set_invoke_pending_cb;
477 evapi.ref = ev_ref; 425 evapi.ref = ev_ref;
478 evapi.unref = ev_unref; 426 evapi.unref = ev_unref;
479 evapi.loop = ev_loop; 427 evapi.loop = ev_loop;
480 evapi.once = ev_once; 428 evapi.once = ev_once;
481 evapi.io_start = ev_io_start; 429 evapi.io_start = ev_io_start;
591 C_ARGS: evapi.default_loop, fd, revents 539 C_ARGS: evapi.default_loop, fd, revents
592 540
593void ev_feed_signal_event (SV *signal) 541void ev_feed_signal_event (SV *signal)
594 CODE: 542 CODE:
595{ 543{
596 Signal signum = sv_signum (signal); 544 Signal signum = s_signum (signal);
597 CHECK_SIG (signal, signum); 545 CHECK_SIG (signal, signum);
598 546
599 ev_feed_signal_event (evapi.default_loop, signum); 547 ev_feed_signal_event (evapi.default_loop, signum);
600} 548}
601 549
550unsigned int ev_pending_count ()
551 C_ARGS: evapi.default_loop
552
553void ev_invoke_pending ()
554 C_ARGS: evapi.default_loop
555
602ev_io *io (SV *fh, int events, SV *cb) 556ev_io *io (SV *fh, int events, SV *cb)
603 ALIAS: 557 ALIAS:
604 io_ns = 1 558 io_ns = 1
605 CODE: 559 CODE:
606{ 560{
607 int fd = sv_fileno (fh); 561 int fd = s_fileno (fh, events & EV_WRITE);
608 CHECK_FD (fh, fd); 562 CHECK_FD (fh, fd);
609 563
610 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); 564 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv);
611 RETVAL->fh = newSVsv (fh); 565 RETVAL->fh = newSVsv (fh);
612 ev_io_set (RETVAL, fd, events); 566 ev_io_set (RETVAL, fd, events);
647ev_signal *signal (SV *signal, SV *cb) 601ev_signal *signal (SV *signal, SV *cb)
648 ALIAS: 602 ALIAS:
649 signal_ns = 1 603 signal_ns = 1
650 CODE: 604 CODE:
651{ 605{
652 Signal signum = sv_signum (signal); 606 Signal signum = s_signum (signal);
653 CHECK_SIG (signal, signum); 607 CHECK_SIG (signal, signum);
654 608
655 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); 609 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv);
656 ev_signal_set (RETVAL, signum); 610 ev_signal_set (RETVAL, signum);
657 if (!ix) START (signal, RETVAL); 611 if (!ix) START (signal, RETVAL);
748 702
749void once (SV *fh, int events, SV *timeout, SV *cb) 703void once (SV *fh, int events, SV *timeout, SV *cb)
750 CODE: 704 CODE:
751 ev_once ( 705 ev_once (
752 evapi.default_loop, 706 evapi.default_loop,
753 sv_fileno (fh), events, 707 s_fileno (fh, events & EV_WRITE), events,
754 SvOK (timeout) ? SvNV (timeout) : -1., 708 SvOK (timeout) ? SvNV (timeout) : -1.,
755 e_once_cb, 709 e_once_cb,
756 newSVsv (cb) 710 newSVsv (cb)
757 ); 711 );
758 712
792SV *cb (ev_watcher *w, SV *new_cb = 0) 746SV *cb (ev_watcher *w, SV *new_cb = 0)
793 CODE: 747 CODE:
794{ 748{
795 if (items > 1) 749 if (items > 1)
796 { 750 {
797 new_cb = e_get_cv (new_cb); 751 new_cb = s_get_cv_croak (new_cb);
798 RETVAL = newRV_noinc (w->cb_sv); 752 RETVAL = newRV_noinc (w->cb_sv);
799 w->cb_sv = SvREFCNT_inc (new_cb); 753 w->cb_sv = SvREFCNT_inc (new_cb);
800 } 754 }
801 else 755 else
802 RETVAL = newRV_inc (w->cb_sv); 756 RETVAL = newRV_inc (w->cb_sv);
872 e_destroy (w); 826 e_destroy (w);
873 827
874void set (ev_io *w, SV *fh, int events) 828void set (ev_io *w, SV *fh, int events)
875 CODE: 829 CODE:
876{ 830{
877 int fd = sv_fileno (fh); 831 int fd = s_fileno (fh, events & EV_WRITE);
878 CHECK_FD (fh, fd); 832 CHECK_FD (fh, fd);
879 833
880 sv_setsv (w->fh, fh); 834 sv_setsv (w->fh, fh);
881 RESET (io, w, (w, fd, events)); 835 RESET (io, w, (w, fd, events));
882} 836}
884SV *fh (ev_io *w, SV *new_fh = 0) 838SV *fh (ev_io *w, SV *new_fh = 0)
885 CODE: 839 CODE:
886{ 840{
887 if (items > 1) 841 if (items > 1)
888 { 842 {
889 int fd = sv_fileno (new_fh); 843 int fd = s_fileno (new_fh, w->events & EV_WRITE);
890 CHECK_FD (new_fh, fd); 844 CHECK_FD (new_fh, fd);
891 845
892 RETVAL = w->fh; 846 RETVAL = w->fh;
893 w->fh = newSVsv (new_fh); 847 w->fh = newSVsv (new_fh);
894 848
927 e_destroy (w); 881 e_destroy (w);
928 882
929void set (ev_signal *w, SV *signal) 883void set (ev_signal *w, SV *signal)
930 CODE: 884 CODE:
931{ 885{
932 Signal signum = sv_signum (signal); 886 Signal signum = s_signum (signal);
933 CHECK_SIG (signal, signum); 887 CHECK_SIG (signal, signum);
934 888
935 RESET (signal, w, (w, signum)); 889 RESET (signal, w, (w, signum));
936} 890}
937 891
940{ 894{
941 RETVAL = w->signum; 895 RETVAL = w->signum;
942 896
943 if (items > 1) 897 if (items > 1)
944 { 898 {
945 Signal signum = sv_signum (new_signal); 899 Signal signum = s_signum (new_signal);
946 CHECK_SIG (new_signal, signum); 900 CHECK_SIG (new_signal, signum);
947 901
948 RESET (signal, w, (w, signum)); 902 RESET (signal, w, (w, signum));
949 } 903 }
950} 904}
1305 1259
1306void ev_unloop (struct ev_loop *loop, int how = 1) 1260void ev_unloop (struct ev_loop *loop, int how = 1)
1307 1261
1308void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE) 1262void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE)
1309 1263
1264unsigned int ev_pending_count (struct ev_loop *loop)
1265
1266void ev_invoke_pending (struct ev_loop *loop)
1267
1310#if 0 1268#if 0
1311 1269
1312void ev_feed_signal_event (struct ev_loop *loop, SV *signal) 1270void ev_feed_signal_event (struct ev_loop *loop, SV *signal)
1313 CODE: 1271 CODE:
1314{ 1272{
1315 Signal signum = sv_signum (signal); 1273 Signal signum = s_signum (signal);
1316 CHECK_SIG (signal, signum); 1274 CHECK_SIG (signal, signum);
1317 1275
1318 ev_feed_signal_event (loop, signum); 1276 ev_feed_signal_event (loop, signum);
1319} 1277}
1320 1278
1323ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb) 1281ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb)
1324 ALIAS: 1282 ALIAS:
1325 io_ns = 1 1283 io_ns = 1
1326 CODE: 1284 CODE:
1327{ 1285{
1328 int fd = sv_fileno (fh); 1286 int fd = s_fileno (fh, events & EV_WRITE);
1329 CHECK_FD (fh, fd); 1287 CHECK_FD (fh, fd);
1330 1288
1331 RETVAL = e_new (sizeof (ev_io), cb, ST (0)); 1289 RETVAL = e_new (sizeof (ev_io), cb, ST (0));
1332 RETVAL->fh = newSVsv (fh); 1290 RETVAL->fh = newSVsv (fh);
1333 ev_io_set (RETVAL, fd, events); 1291 ev_io_set (RETVAL, fd, events);
1370ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) 1328ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb)
1371 ALIAS: 1329 ALIAS:
1372 signal_ns = 1 1330 signal_ns = 1
1373 CODE: 1331 CODE:
1374{ 1332{
1375 Signal signum = sv_signum (signal); 1333 Signal signum = s_signum (signal);
1376 CHECK_SIG (signal, signum); 1334 CHECK_SIG (signal, signum);
1377 1335
1378 RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); 1336 RETVAL = e_new (sizeof (ev_signal), cb, ST (0));
1379 ev_signal_set (RETVAL, signum); 1337 ev_signal_set (RETVAL, signum);
1380 if (!ix) START (signal, RETVAL); 1338 if (!ix) START (signal, RETVAL);
1473 1431
1474void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb) 1432void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb)
1475 CODE: 1433 CODE:
1476 ev_once ( 1434 ev_once (
1477 loop, 1435 loop,
1478 sv_fileno (fh), events, 1436 s_fileno (fh, events & EV_WRITE), events,
1479 SvOK (timeout) ? SvNV (timeout) : -1., 1437 SvOK (timeout) ? SvNV (timeout) : -1.,
1480 e_once_cb, 1438 e_once_cb,
1481 newSVsv (cb) 1439 newSVsv (cb)
1482 ); 1440 );
1483 1441

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines