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

Comparing EV/EV.xs (file contents):
Revision 1.122 by root, Wed Apr 15 19:35:53 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"
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 34#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
38
39#if __GNUC__ >= 3
40# define expect(expr,value) __builtin_expect ((expr),(value))
41#else
42# define expect(expr,value) (expr)
43#endif
44
45#define expect_false(expr) expect ((expr) != 0, 0)
46#define expect_true(expr) expect ((expr) != 0, 1)
47 35
48#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) 36#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop))
49 37
50#define WFLAG_KEEPALIVE 1 38#define WFLAG_KEEPALIVE 1
51#define WFLAG_UNREFED 2 /* has been unref'ed */ 39#define WFLAG_UNREFED 2 /* has been unref'ed */
105 *stash_check, 93 *stash_check,
106 *stash_embed, 94 *stash_embed,
107 *stash_fork, 95 *stash_fork,
108 *stash_async; 96 *stash_async;
109 97
110#ifndef SIG_SIZE
111/* kudos to Slaven Rezic for the idea */
112static char sig_size [] = { SIG_NUM };
113# define SIG_SIZE (sizeof (sig_size) + 1)
114#endif
115
116static Signal
117sv_signum (SV *sig)
118{
119 Signal signum;
120
121 SvGETMAGIC (sig);
122
123 for (signum = 1; signum < SIG_SIZE; ++signum)
124 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum]))
125 return signum;
126
127 signum = SvIV (sig);
128
129 if (signum > 0 && signum < SIG_SIZE)
130 return signum;
131
132 return -1;
133}
134
135///////////////////////////////////////////////////////////////////////////// 98/////////////////////////////////////////////////////////////////////////////
136// Event 99// Event
137 100
138static void e_cb (EV_P_ ev_watcher *w, int revents); 101static void e_cb (EV_P_ ev_watcher *w, int revents);
139 102
140static int
141sv_fileno (SV *fh)
142{
143 SvGETMAGIC (fh);
144
145 if (SvROK (fh))
146 fh = SvRV (fh);
147
148 if (SvTYPE (fh) == SVt_PVGV)
149 return PerlIO_fileno (IoIFP (sv_2io (fh)));
150
151 if (SvOK (fh) && (SvIV (fh) >= 0) && (SvIV (fh) < 0x7fffffffL))
152 return SvIV (fh);
153
154 return -1;
155}
156
157static SV *
158e_get_cv (SV *cb_sv)
159{
160 HV *st;
161 GV *gvp;
162 CV *cv = sv_2cv (cb_sv, &st, &gvp, 0);
163
164 if (!cv)
165 croak ("EV watcher callback must be a CODE reference");
166
167 return (SV *)cv;
168}
169
170static void * 103static void *
171e_new (int size, SV *cb_sv, SV *loop) 104e_new (int size, SV *cb_sv, SV *loop)
172{ 105{
173 SV *cv = cb_sv ? e_get_cv (cb_sv) : 0; 106 SV *cv = cb_sv ? s_get_cv_croak (cb_sv) : 0;
174 ev_watcher *w; 107 ev_watcher *w;
175 SV *self = NEWSV (0, size); 108 SV *self = NEWSV (0, size);
176 SvPOK_only (self); 109 SvPOK_only (self);
177 SvCUR_set (self, size); 110 SvCUR_set (self, size);
178 111
399 const_iv (EV_, MINPRI) 332 const_iv (EV_, MINPRI)
400 const_iv (EV_, MAXPRI) 333 const_iv (EV_, MAXPRI)
401 334
402 const_iv (EV_, UNDEF) 335 const_iv (EV_, UNDEF)
403 const_iv (EV_, NONE) 336 const_iv (EV_, NONE)
404 const_iv (EV_, TIMEOUT)
405 const_iv (EV_, READ) 337 const_iv (EV_, READ)
406 const_iv (EV_, WRITE) 338 const_iv (EV_, WRITE)
339 const_iv (EV_, IO)
340 const_iv (EV_, TIMEOUT)
341 const_iv (EV_, TIMER)
342 const_iv (EV_, PERIODIC)
407 const_iv (EV_, SIGNAL) 343 const_iv (EV_, SIGNAL)
344 const_iv (EV_, CHILD)
345 const_iv (EV_, STAT)
408 const_iv (EV_, IDLE) 346 const_iv (EV_, IDLE)
347 const_iv (EV_, PREPARE)
409 const_iv (EV_, CHECK) 348 const_iv (EV_, CHECK)
349 const_iv (EV_, EMBED)
350 const_iv (EV_, FORK)
351 const_iv (EV_, ASYNC)
352 const_iv (EV_, CUSTOM)
410 const_iv (EV_, ERROR) 353 const_iv (EV_, ERROR)
411 354
355 const_iv (EV, LOOP_NONBLOCK)
412 const_iv (EV, LOOP_ONESHOT) 356 const_iv (EV, LOOP_ONESHOT)
357
413 const_iv (EV, LOOP_NONBLOCK) 358 const_iv (EV, UNLOOP_CANCEL)
414 const_iv (EV, UNLOOP_ONE) 359 const_iv (EV, UNLOOP_ONE)
415 const_iv (EV, UNLOOP_ALL) 360 const_iv (EV, UNLOOP_ALL)
416 361
417 const_iv (EV, BACKEND_SELECT) 362 const_iv (EV, BACKEND_SELECT)
418 const_iv (EV, BACKEND_POLL) 363 const_iv (EV, BACKEND_POLL)
421 const_iv (EV, BACKEND_DEVPOLL) 366 const_iv (EV, BACKEND_DEVPOLL)
422 const_iv (EV, BACKEND_PORT) 367 const_iv (EV, BACKEND_PORT)
423 const_iv (EV, FLAG_AUTO) 368 const_iv (EV, FLAG_AUTO)
424 const_iv (EV, FLAG_NOENV) 369 const_iv (EV, FLAG_NOENV)
425 const_iv (EV, FLAG_FORKCHECK) 370 const_iv (EV, FLAG_FORKCHECK)
371
372 const_iv (EV_, VERSION_MAJOR)
373 const_iv (EV_, VERSION_MINOR)
426 }; 374 };
427 375
428 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 376 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
429 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 377 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
430 378
449 397
450 /* the poor man's shared library emulator */ 398 /* the poor man's shared library emulator */
451 evapi.ver = EV_API_VERSION; 399 evapi.ver = EV_API_VERSION;
452 evapi.rev = EV_API_REVISION; 400 evapi.rev = EV_API_REVISION;
453 evapi.sv_fileno = sv_fileno; 401 evapi.sv_fileno = sv_fileno;
454 evapi.sv_signum = sv_signum; 402 evapi.sv_signum = s_signum;
455 evapi.supported_backends = ev_supported_backends (); 403 evapi.supported_backends = ev_supported_backends ();
456 evapi.recommended_backends = ev_recommended_backends (); 404 evapi.recommended_backends = ev_recommended_backends ();
457 evapi.embeddable_backends = ev_embeddable_backends (); 405 evapi.embeddable_backends = ev_embeddable_backends ();
458 evapi.time_ = ev_time; 406 evapi.time_ = ev_time;
459 evapi.sleep_ = ev_sleep; 407 evapi.sleep_ = ev_sleep;
460 evapi.loop_new = ev_loop_new; 408 evapi.loop_new = ev_loop_new;
461 evapi.loop_destroy = ev_loop_destroy; 409 evapi.loop_destroy = ev_loop_destroy;
462 evapi.loop_fork = ev_loop_fork; 410 evapi.loop_fork = ev_loop_fork;
463 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;
464 evapi.now = ev_now; 415 evapi.now = ev_now;
465 evapi.now_update = ev_now_update; 416 evapi.now_update = ev_now_update;
466 evapi.suspend = ev_suspend; 417 evapi.suspend = ev_suspend;
467 evapi.resume = ev_resume; 418 evapi.resume = ev_resume;
468 evapi.backend = ev_backend; 419 evapi.backend = ev_backend;
469 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;
470 evapi.ref = ev_ref; 425 evapi.ref = ev_ref;
471 evapi.unref = ev_unref; 426 evapi.unref = ev_unref;
472 evapi.loop = ev_loop; 427 evapi.loop = ev_loop;
473 evapi.once = ev_once; 428 evapi.once = ev_once;
474 evapi.io_start = ev_io_start; 429 evapi.io_start = ev_io_start;
557 C_ARGS: evapi.default_loop 512 C_ARGS: evapi.default_loop
558 513
559unsigned int ev_backend () 514unsigned int ev_backend ()
560 C_ARGS: evapi.default_loop 515 C_ARGS: evapi.default_loop
561 516
517void ev_loop_verify ()
518 C_ARGS: evapi.default_loop
519
562unsigned int ev_loop_count () 520unsigned int ev_loop_count ()
563 C_ARGS: evapi.default_loop 521 C_ARGS: evapi.default_loop
564 522
523unsigned int ev_loop_depth ()
524 C_ARGS: evapi.default_loop
525
565void ev_set_io_collect_interval (NV interval) 526void ev_set_io_collect_interval (NV interval)
566 C_ARGS: evapi.default_loop, interval 527 C_ARGS: evapi.default_loop, interval
567 528
568void ev_set_timeout_collect_interval (NV interval) 529void ev_set_timeout_collect_interval (NV interval)
569 C_ARGS: evapi.default_loop, interval 530 C_ARGS: evapi.default_loop, interval
578 C_ARGS: evapi.default_loop, fd, revents 539 C_ARGS: evapi.default_loop, fd, revents
579 540
580void ev_feed_signal_event (SV *signal) 541void ev_feed_signal_event (SV *signal)
581 CODE: 542 CODE:
582{ 543{
583 Signal signum = sv_signum (signal); 544 Signal signum = s_signum (signal);
584 CHECK_SIG (signal, signum); 545 CHECK_SIG (signal, signum);
585 546
586 ev_feed_signal_event (evapi.default_loop, signum); 547 ev_feed_signal_event (evapi.default_loop, signum);
587} 548}
588 549
550unsigned int ev_pending_count ()
551 C_ARGS: evapi.default_loop
552
553void ev_invoke_pending ()
554 C_ARGS: evapi.default_loop
555
589ev_io *io (SV *fh, int events, SV *cb) 556ev_io *io (SV *fh, int events, SV *cb)
590 ALIAS: 557 ALIAS:
591 io_ns = 1 558 io_ns = 1
592 CODE: 559 CODE:
593{ 560{
594 int fd = sv_fileno (fh); 561 int fd = s_fileno (fh, events & EV_WRITE);
595 CHECK_FD (fh, fd); 562 CHECK_FD (fh, fd);
596 563
597 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); 564 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv);
598 RETVAL->fh = newSVsv (fh); 565 RETVAL->fh = newSVsv (fh);
599 ev_io_set (RETVAL, fd, events); 566 ev_io_set (RETVAL, fd, events);
634ev_signal *signal (SV *signal, SV *cb) 601ev_signal *signal (SV *signal, SV *cb)
635 ALIAS: 602 ALIAS:
636 signal_ns = 1 603 signal_ns = 1
637 CODE: 604 CODE:
638{ 605{
639 Signal signum = sv_signum (signal); 606 Signal signum = s_signum (signal);
640 CHECK_SIG (signal, signum); 607 CHECK_SIG (signal, signum);
641 608
642 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); 609 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv);
643 ev_signal_set (RETVAL, signum); 610 ev_signal_set (RETVAL, signum);
644 if (!ix) START (signal, RETVAL); 611 if (!ix) START (signal, RETVAL);
735 702
736void once (SV *fh, int events, SV *timeout, SV *cb) 703void once (SV *fh, int events, SV *timeout, SV *cb)
737 CODE: 704 CODE:
738 ev_once ( 705 ev_once (
739 evapi.default_loop, 706 evapi.default_loop,
740 sv_fileno (fh), events, 707 s_fileno (fh, events & EV_WRITE), events,
741 SvOK (timeout) ? SvNV (timeout) : -1., 708 SvOK (timeout) ? SvNV (timeout) : -1.,
742 e_once_cb, 709 e_once_cb,
743 newSVsv (cb) 710 newSVsv (cb)
744 ); 711 );
745 712
779SV *cb (ev_watcher *w, SV *new_cb = 0) 746SV *cb (ev_watcher *w, SV *new_cb = 0)
780 CODE: 747 CODE:
781{ 748{
782 if (items > 1) 749 if (items > 1)
783 { 750 {
784 new_cb = e_get_cv (new_cb); 751 new_cb = s_get_cv_croak (new_cb);
785 RETVAL = newRV_noinc (w->cb_sv); 752 RETVAL = newRV_noinc (w->cb_sv);
786 w->cb_sv = SvREFCNT_inc (new_cb); 753 w->cb_sv = SvREFCNT_inc (new_cb);
787 } 754 }
788 else 755 else
789 RETVAL = newRV_inc (w->cb_sv); 756 RETVAL = newRV_inc (w->cb_sv);
859 e_destroy (w); 826 e_destroy (w);
860 827
861void set (ev_io *w, SV *fh, int events) 828void set (ev_io *w, SV *fh, int events)
862 CODE: 829 CODE:
863{ 830{
864 int fd = sv_fileno (fh); 831 int fd = s_fileno (fh, events & EV_WRITE);
865 CHECK_FD (fh, fd); 832 CHECK_FD (fh, fd);
866 833
867 sv_setsv (w->fh, fh); 834 sv_setsv (w->fh, fh);
868 RESET (io, w, (w, fd, events)); 835 RESET (io, w, (w, fd, events));
869} 836}
871SV *fh (ev_io *w, SV *new_fh = 0) 838SV *fh (ev_io *w, SV *new_fh = 0)
872 CODE: 839 CODE:
873{ 840{
874 if (items > 1) 841 if (items > 1)
875 { 842 {
876 int fd = sv_fileno (new_fh); 843 int fd = s_fileno (new_fh, w->events & EV_WRITE);
877 CHECK_FD (new_fh, fd); 844 CHECK_FD (new_fh, fd);
878 845
879 RETVAL = w->fh; 846 RETVAL = w->fh;
880 w->fh = newSVsv (new_fh); 847 w->fh = newSVsv (new_fh);
881 848
914 e_destroy (w); 881 e_destroy (w);
915 882
916void set (ev_signal *w, SV *signal) 883void set (ev_signal *w, SV *signal)
917 CODE: 884 CODE:
918{ 885{
919 Signal signum = sv_signum (signal); 886 Signal signum = s_signum (signal);
920 CHECK_SIG (signal, signum); 887 CHECK_SIG (signal, signum);
921 888
922 RESET (signal, w, (w, signum)); 889 RESET (signal, w, (w, signum));
923} 890}
924 891
927{ 894{
928 RETVAL = w->signum; 895 RETVAL = w->signum;
929 896
930 if (items > 1) 897 if (items > 1)
931 { 898 {
932 Signal signum = sv_signum (new_signal); 899 Signal signum = s_signum (new_signal);
933 CHECK_SIG (new_signal, signum); 900 CHECK_SIG (new_signal, signum);
934 901
935 RESET (signal, w, (w, signum)); 902 RESET (signal, w, (w, signum));
936 } 903 }
937} 904}
1284 1251
1285unsigned int ev_backend (struct ev_loop *loop) 1252unsigned int ev_backend (struct ev_loop *loop)
1286 1253
1287unsigned int ev_loop_count (struct ev_loop *loop) 1254unsigned int ev_loop_count (struct ev_loop *loop)
1288 1255
1256unsigned int ev_loop_depth (struct ev_loop *loop)
1257
1289void ev_loop (struct ev_loop *loop, int flags = 0) 1258void ev_loop (struct ev_loop *loop, int flags = 0)
1290 1259
1291void ev_unloop (struct ev_loop *loop, int how = 1) 1260void ev_unloop (struct ev_loop *loop, int how = 1)
1292 1261
1293void 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)
1294 1263
1264unsigned int ev_pending_count (struct ev_loop *loop)
1265
1266void ev_invoke_pending (struct ev_loop *loop)
1267
1295#if 0 1268#if 0
1296 1269
1297void ev_feed_signal_event (struct ev_loop *loop, SV *signal) 1270void ev_feed_signal_event (struct ev_loop *loop, SV *signal)
1298 CODE: 1271 CODE:
1299{ 1272{
1300 Signal signum = sv_signum (signal); 1273 Signal signum = s_signum (signal);
1301 CHECK_SIG (signal, signum); 1274 CHECK_SIG (signal, signum);
1302 1275
1303 ev_feed_signal_event (loop, signum); 1276 ev_feed_signal_event (loop, signum);
1304} 1277}
1305 1278
1308ev_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)
1309 ALIAS: 1282 ALIAS:
1310 io_ns = 1 1283 io_ns = 1
1311 CODE: 1284 CODE:
1312{ 1285{
1313 int fd = sv_fileno (fh); 1286 int fd = s_fileno (fh, events & EV_WRITE);
1314 CHECK_FD (fh, fd); 1287 CHECK_FD (fh, fd);
1315 1288
1316 RETVAL = e_new (sizeof (ev_io), cb, ST (0)); 1289 RETVAL = e_new (sizeof (ev_io), cb, ST (0));
1317 RETVAL->fh = newSVsv (fh); 1290 RETVAL->fh = newSVsv (fh);
1318 ev_io_set (RETVAL, fd, events); 1291 ev_io_set (RETVAL, fd, events);
1355ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) 1328ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb)
1356 ALIAS: 1329 ALIAS:
1357 signal_ns = 1 1330 signal_ns = 1
1358 CODE: 1331 CODE:
1359{ 1332{
1360 Signal signum = sv_signum (signal); 1333 Signal signum = s_signum (signal);
1361 CHECK_SIG (signal, signum); 1334 CHECK_SIG (signal, signum);
1362 1335
1363 RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); 1336 RETVAL = e_new (sizeof (ev_signal), cb, ST (0));
1364 ev_signal_set (RETVAL, signum); 1337 ev_signal_set (RETVAL, signum);
1365 if (!ix) START (signal, RETVAL); 1338 if (!ix) START (signal, RETVAL);
1458 1431
1459void 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)
1460 CODE: 1433 CODE:
1461 ev_once ( 1434 ev_once (
1462 loop, 1435 loop,
1463 sv_fileno (fh), events, 1436 s_fileno (fh, events & EV_WRITE), events,
1464 SvOK (timeout) ? SvNV (timeout) : -1., 1437 SvOK (timeout) ? SvNV (timeout) : -1.,
1465 e_once_cb, 1438 e_once_cb,
1466 newSVsv (cb) 1439 newSVsv (cb)
1467 ); 1440 );
1468 1441

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines