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

Comparing EV/EV.xs (file contents):
Revision 1.124 by root, Mon Jun 29 18:46:52 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;
412 evapi.loop_depth = ev_loop_depth;
413 evapi.set_userdata = ev_set_userdata;
414 evapi.userdata = ev_userdata;
470 evapi.now = ev_now; 415 evapi.now = ev_now;
471 evapi.now_update = ev_now_update; 416 evapi.now_update = ev_now_update;
472 evapi.suspend = ev_suspend; 417 evapi.suspend = ev_suspend;
473 evapi.resume = ev_resume; 418 evapi.resume = ev_resume;
474 evapi.backend = ev_backend; 419 evapi.backend = ev_backend;
475 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;
476 evapi.ref = ev_ref; 425 evapi.ref = ev_ref;
477 evapi.unref = ev_unref; 426 evapi.unref = ev_unref;
478 evapi.loop = ev_loop; 427 evapi.loop = ev_loop;
479 evapi.once = ev_once; 428 evapi.once = ev_once;
480 evapi.io_start = ev_io_start; 429 evapi.io_start = ev_io_start;
563 C_ARGS: evapi.default_loop 512 C_ARGS: evapi.default_loop
564 513
565unsigned int ev_backend () 514unsigned int ev_backend ()
566 C_ARGS: evapi.default_loop 515 C_ARGS: evapi.default_loop
567 516
517void ev_loop_verify ()
518 C_ARGS: evapi.default_loop
519
568unsigned int ev_loop_count () 520unsigned int ev_loop_count ()
569 C_ARGS: evapi.default_loop 521 C_ARGS: evapi.default_loop
570 522
523unsigned int ev_loop_depth ()
524 C_ARGS: evapi.default_loop
525
571void ev_set_io_collect_interval (NV interval) 526void ev_set_io_collect_interval (NV interval)
572 C_ARGS: evapi.default_loop, interval 527 C_ARGS: evapi.default_loop, interval
573 528
574void ev_set_timeout_collect_interval (NV interval) 529void ev_set_timeout_collect_interval (NV interval)
575 C_ARGS: evapi.default_loop, interval 530 C_ARGS: evapi.default_loop, interval
584 C_ARGS: evapi.default_loop, fd, revents 539 C_ARGS: evapi.default_loop, fd, revents
585 540
586void ev_feed_signal_event (SV *signal) 541void ev_feed_signal_event (SV *signal)
587 CODE: 542 CODE:
588{ 543{
589 Signal signum = sv_signum (signal); 544 Signal signum = s_signum (signal);
590 CHECK_SIG (signal, signum); 545 CHECK_SIG (signal, signum);
591 546
592 ev_feed_signal_event (evapi.default_loop, signum); 547 ev_feed_signal_event (evapi.default_loop, signum);
593} 548}
594 549
550unsigned int ev_pending_count ()
551 C_ARGS: evapi.default_loop
552
553void ev_invoke_pending ()
554 C_ARGS: evapi.default_loop
555
595ev_io *io (SV *fh, int events, SV *cb) 556ev_io *io (SV *fh, int events, SV *cb)
596 ALIAS: 557 ALIAS:
597 io_ns = 1 558 io_ns = 1
598 CODE: 559 CODE:
599{ 560{
600 int fd = sv_fileno (fh); 561 int fd = s_fileno (fh, events & EV_WRITE);
601 CHECK_FD (fh, fd); 562 CHECK_FD (fh, fd);
602 563
603 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); 564 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv);
604 RETVAL->fh = newSVsv (fh); 565 RETVAL->fh = newSVsv (fh);
605 ev_io_set (RETVAL, fd, events); 566 ev_io_set (RETVAL, fd, events);
640ev_signal *signal (SV *signal, SV *cb) 601ev_signal *signal (SV *signal, SV *cb)
641 ALIAS: 602 ALIAS:
642 signal_ns = 1 603 signal_ns = 1
643 CODE: 604 CODE:
644{ 605{
645 Signal signum = sv_signum (signal); 606 Signal signum = s_signum (signal);
646 CHECK_SIG (signal, signum); 607 CHECK_SIG (signal, signum);
647 608
648 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); 609 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv);
649 ev_signal_set (RETVAL, signum); 610 ev_signal_set (RETVAL, signum);
650 if (!ix) START (signal, RETVAL); 611 if (!ix) START (signal, RETVAL);
741 702
742void once (SV *fh, int events, SV *timeout, SV *cb) 703void once (SV *fh, int events, SV *timeout, SV *cb)
743 CODE: 704 CODE:
744 ev_once ( 705 ev_once (
745 evapi.default_loop, 706 evapi.default_loop,
746 sv_fileno (fh), events, 707 s_fileno (fh, events & EV_WRITE), events,
747 SvOK (timeout) ? SvNV (timeout) : -1., 708 SvOK (timeout) ? SvNV (timeout) : -1.,
748 e_once_cb, 709 e_once_cb,
749 newSVsv (cb) 710 newSVsv (cb)
750 ); 711 );
751 712
785SV *cb (ev_watcher *w, SV *new_cb = 0) 746SV *cb (ev_watcher *w, SV *new_cb = 0)
786 CODE: 747 CODE:
787{ 748{
788 if (items > 1) 749 if (items > 1)
789 { 750 {
790 new_cb = e_get_cv (new_cb); 751 new_cb = s_get_cv_croak (new_cb);
791 RETVAL = newRV_noinc (w->cb_sv); 752 RETVAL = newRV_noinc (w->cb_sv);
792 w->cb_sv = SvREFCNT_inc (new_cb); 753 w->cb_sv = SvREFCNT_inc (new_cb);
793 } 754 }
794 else 755 else
795 RETVAL = newRV_inc (w->cb_sv); 756 RETVAL = newRV_inc (w->cb_sv);
865 e_destroy (w); 826 e_destroy (w);
866 827
867void set (ev_io *w, SV *fh, int events) 828void set (ev_io *w, SV *fh, int events)
868 CODE: 829 CODE:
869{ 830{
870 int fd = sv_fileno (fh); 831 int fd = s_fileno (fh, events & EV_WRITE);
871 CHECK_FD (fh, fd); 832 CHECK_FD (fh, fd);
872 833
873 sv_setsv (w->fh, fh); 834 sv_setsv (w->fh, fh);
874 RESET (io, w, (w, fd, events)); 835 RESET (io, w, (w, fd, events));
875} 836}
877SV *fh (ev_io *w, SV *new_fh = 0) 838SV *fh (ev_io *w, SV *new_fh = 0)
878 CODE: 839 CODE:
879{ 840{
880 if (items > 1) 841 if (items > 1)
881 { 842 {
882 int fd = sv_fileno (new_fh); 843 int fd = s_fileno (new_fh, w->events & EV_WRITE);
883 CHECK_FD (new_fh, fd); 844 CHECK_FD (new_fh, fd);
884 845
885 RETVAL = w->fh; 846 RETVAL = w->fh;
886 w->fh = newSVsv (new_fh); 847 w->fh = newSVsv (new_fh);
887 848
920 e_destroy (w); 881 e_destroy (w);
921 882
922void set (ev_signal *w, SV *signal) 883void set (ev_signal *w, SV *signal)
923 CODE: 884 CODE:
924{ 885{
925 Signal signum = sv_signum (signal); 886 Signal signum = s_signum (signal);
926 CHECK_SIG (signal, signum); 887 CHECK_SIG (signal, signum);
927 888
928 RESET (signal, w, (w, signum)); 889 RESET (signal, w, (w, signum));
929} 890}
930 891
933{ 894{
934 RETVAL = w->signum; 895 RETVAL = w->signum;
935 896
936 if (items > 1) 897 if (items > 1)
937 { 898 {
938 Signal signum = sv_signum (new_signal); 899 Signal signum = s_signum (new_signal);
939 CHECK_SIG (new_signal, signum); 900 CHECK_SIG (new_signal, signum);
940 901
941 RESET (signal, w, (w, signum)); 902 RESET (signal, w, (w, signum));
942 } 903 }
943} 904}
1290 1251
1291unsigned int ev_backend (struct ev_loop *loop) 1252unsigned int ev_backend (struct ev_loop *loop)
1292 1253
1293unsigned int ev_loop_count (struct ev_loop *loop) 1254unsigned int ev_loop_count (struct ev_loop *loop)
1294 1255
1256unsigned int ev_loop_depth (struct ev_loop *loop)
1257
1295void ev_loop (struct ev_loop *loop, int flags = 0) 1258void ev_loop (struct ev_loop *loop, int flags = 0)
1296 1259
1297void ev_unloop (struct ev_loop *loop, int how = 1) 1260void ev_unloop (struct ev_loop *loop, int how = 1)
1298 1261
1299void 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)
1300 1263
1264unsigned int ev_pending_count (struct ev_loop *loop)
1265
1266void ev_invoke_pending (struct ev_loop *loop)
1267
1301#if 0 1268#if 0
1302 1269
1303void ev_feed_signal_event (struct ev_loop *loop, SV *signal) 1270void ev_feed_signal_event (struct ev_loop *loop, SV *signal)
1304 CODE: 1271 CODE:
1305{ 1272{
1306 Signal signum = sv_signum (signal); 1273 Signal signum = s_signum (signal);
1307 CHECK_SIG (signal, signum); 1274 CHECK_SIG (signal, signum);
1308 1275
1309 ev_feed_signal_event (loop, signum); 1276 ev_feed_signal_event (loop, signum);
1310} 1277}
1311 1278
1314ev_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)
1315 ALIAS: 1282 ALIAS:
1316 io_ns = 1 1283 io_ns = 1
1317 CODE: 1284 CODE:
1318{ 1285{
1319 int fd = sv_fileno (fh); 1286 int fd = s_fileno (fh, events & EV_WRITE);
1320 CHECK_FD (fh, fd); 1287 CHECK_FD (fh, fd);
1321 1288
1322 RETVAL = e_new (sizeof (ev_io), cb, ST (0)); 1289 RETVAL = e_new (sizeof (ev_io), cb, ST (0));
1323 RETVAL->fh = newSVsv (fh); 1290 RETVAL->fh = newSVsv (fh);
1324 ev_io_set (RETVAL, fd, events); 1291 ev_io_set (RETVAL, fd, events);
1361ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) 1328ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb)
1362 ALIAS: 1329 ALIAS:
1363 signal_ns = 1 1330 signal_ns = 1
1364 CODE: 1331 CODE:
1365{ 1332{
1366 Signal signum = sv_signum (signal); 1333 Signal signum = s_signum (signal);
1367 CHECK_SIG (signal, signum); 1334 CHECK_SIG (signal, signum);
1368 1335
1369 RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); 1336 RETVAL = e_new (sizeof (ev_signal), cb, ST (0));
1370 ev_signal_set (RETVAL, signum); 1337 ev_signal_set (RETVAL, signum);
1371 if (!ix) START (signal, RETVAL); 1338 if (!ix) START (signal, RETVAL);
1464 1431
1465void 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)
1466 CODE: 1433 CODE:
1467 ev_once ( 1434 ev_once (
1468 loop, 1435 loop,
1469 sv_fileno (fh), events, 1436 s_fileno (fh, events & EV_WRITE), events,
1470 SvOK (timeout) ? SvNV (timeout) : -1., 1437 SvOK (timeout) ? SvNV (timeout) : -1.,
1471 e_once_cb, 1438 e_once_cb,
1472 newSVsv (cb) 1439 newSVsv (cb)
1473 ); 1440 );
1474 1441

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines