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

Comparing EV/EV.xs (file contents):
Revision 1.121 by root, Wed Apr 15 17:49:26 2009 UTC vs.
Revision 1.126 by root, Tue Jul 14 00:09:59 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 10
11#define EV_PROTOTYPES 1 11#define EV_PROTOTYPES 1
12#define EV_USE_NANOSLEEP EV_USE_MONOTONIC 12#define EV_USE_NANOSLEEP EV_USE_MONOTONIC
13#define EV_H <ev.h> 13#define EV_H <ev.h>
14#include "EV/EVAPI.h" 14#include "EV/EVAPI.h"
23#include "libev/ev.c" 23#include "libev/ev.c"
24 24
25#ifndef _WIN32 25#ifndef _WIN32
26# include <pthread.h> 26# include <pthread.h>
27#endif 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
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 28
48#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) 29#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop))
49 30
50#define WFLAG_KEEPALIVE 1 31#define WFLAG_KEEPALIVE 1
51#define WFLAG_UNREFED 2 /* has been unref'ed */ 32#define WFLAG_UNREFED 2 /* has been unref'ed */
105 *stash_check, 86 *stash_check,
106 *stash_embed, 87 *stash_embed,
107 *stash_fork, 88 *stash_fork,
108 *stash_async; 89 *stash_async;
109 90
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///////////////////////////////////////////////////////////////////////////// 91/////////////////////////////////////////////////////////////////////////////
136// Event 92// Event
137 93
138static void e_cb (EV_P_ ev_watcher *w, int revents); 94static void e_cb (EV_P_ ev_watcher *w, int revents);
139 95
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 * 96static void *
171e_new (int size, SV *cb_sv, SV *loop) 97e_new (int size, SV *cb_sv, SV *loop)
172{ 98{
173 SV *cv = cb_sv ? e_get_cv (cb_sv) : 0; 99 SV *cv = cb_sv ? s_get_cv_croak (cb_sv) : 0;
174 ev_watcher *w; 100 ev_watcher *w;
175 SV *self = NEWSV (0, size); 101 SV *self = NEWSV (0, size);
176 SvPOK_only (self); 102 SvPOK_only (self);
177 SvCUR_set (self, size); 103 SvCUR_set (self, size);
178 104
399 const_iv (EV_, MINPRI) 325 const_iv (EV_, MINPRI)
400 const_iv (EV_, MAXPRI) 326 const_iv (EV_, MAXPRI)
401 327
402 const_iv (EV_, UNDEF) 328 const_iv (EV_, UNDEF)
403 const_iv (EV_, NONE) 329 const_iv (EV_, NONE)
404 const_iv (EV_, TIMEOUT)
405 const_iv (EV_, READ) 330 const_iv (EV_, READ)
406 const_iv (EV_, WRITE) 331 const_iv (EV_, WRITE)
332 const_iv (EV_, IO)
333 const_iv (EV_, TIMEOUT)
334 const_iv (EV_, TIMER)
335 const_iv (EV_, PERIODIC)
407 const_iv (EV_, SIGNAL) 336 const_iv (EV_, SIGNAL)
337 const_iv (EV_, CHILD)
338 const_iv (EV_, STAT)
408 const_iv (EV_, IDLE) 339 const_iv (EV_, IDLE)
340 const_iv (EV_, PREPARE)
409 const_iv (EV_, CHECK) 341 const_iv (EV_, CHECK)
342 const_iv (EV_, EMBED)
343 const_iv (EV_, FORK)
344 const_iv (EV_, ASYNC)
345 const_iv (EV_, CUSTOM)
410 const_iv (EV_, ERROR) 346 const_iv (EV_, ERROR)
411 347
348 const_iv (EV, LOOP_NONBLOCK)
412 const_iv (EV, LOOP_ONESHOT) 349 const_iv (EV, LOOP_ONESHOT)
350
413 const_iv (EV, LOOP_NONBLOCK) 351 const_iv (EV, UNLOOP_CANCEL)
414 const_iv (EV, UNLOOP_ONE) 352 const_iv (EV, UNLOOP_ONE)
415 const_iv (EV, UNLOOP_ALL) 353 const_iv (EV, UNLOOP_ALL)
416 354
417 const_iv (EV, BACKEND_SELECT) 355 const_iv (EV, BACKEND_SELECT)
418 const_iv (EV, BACKEND_POLL) 356 const_iv (EV, BACKEND_POLL)
421 const_iv (EV, BACKEND_DEVPOLL) 359 const_iv (EV, BACKEND_DEVPOLL)
422 const_iv (EV, BACKEND_PORT) 360 const_iv (EV, BACKEND_PORT)
423 const_iv (EV, FLAG_AUTO) 361 const_iv (EV, FLAG_AUTO)
424 const_iv (EV, FLAG_NOENV) 362 const_iv (EV, FLAG_NOENV)
425 const_iv (EV, FLAG_FORKCHECK) 363 const_iv (EV, FLAG_FORKCHECK)
364
365 const_iv (EV_, VERSION_MAJOR)
366 const_iv (EV_, VERSION_MINOR)
426 }; 367 };
427 368
428 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 369 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
429 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 370 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
430 371
448 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ 389 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */
449 390
450 /* the poor man's shared library emulator */ 391 /* the poor man's shared library emulator */
451 evapi.ver = EV_API_VERSION; 392 evapi.ver = EV_API_VERSION;
452 evapi.rev = EV_API_REVISION; 393 evapi.rev = EV_API_REVISION;
453 evapi.sv_fileno = sv_fileno; 394 evapi.sv_fileno = s_fileno;
454 evapi.sv_signum = sv_signum; 395 evapi.sv_signum = s_signum;
455 evapi.supported_backends = ev_supported_backends (); 396 evapi.supported_backends = ev_supported_backends ();
456 evapi.recommended_backends = ev_recommended_backends (); 397 evapi.recommended_backends = ev_recommended_backends ();
457 evapi.embeddable_backends = ev_embeddable_backends (); 398 evapi.embeddable_backends = ev_embeddable_backends ();
458 evapi.time_ = ev_time; 399 evapi.time_ = ev_time;
459 evapi.sleep_ = ev_sleep; 400 evapi.sleep_ = ev_sleep;
460 evapi.loop_new = ev_loop_new; 401 evapi.loop_new = ev_loop_new;
461 evapi.loop_destroy = ev_loop_destroy; 402 evapi.loop_destroy = ev_loop_destroy;
462 evapi.loop_fork = ev_loop_fork; 403 evapi.loop_fork = ev_loop_fork;
463 evapi.loop_count = ev_loop_count; 404 evapi.loop_count = ev_loop_count;
405 evapi.loop_depth = ev_loop_depth;
406 evapi.set_userdata = ev_set_userdata;
407 evapi.userdata = ev_userdata;
464 evapi.now = ev_now; 408 evapi.now = ev_now;
465 evapi.now_update = ev_now_update; 409 evapi.now_update = ev_now_update;
410 evapi.suspend = ev_suspend;
411 evapi.resume = ev_resume;
466 evapi.backend = ev_backend; 412 evapi.backend = ev_backend;
467 evapi.unloop = ev_unloop; 413 evapi.unloop = ev_unloop;
414 evapi.invoke_pending = ev_invoke_pending;
415 evapi.set_loop_release_cb = ev_set_loop_release_cb;
416 evapi.set_invoke_pending_cb= ev_set_invoke_pending_cb;
468 evapi.ref = ev_ref; 417 evapi.ref = ev_ref;
469 evapi.unref = ev_unref; 418 evapi.unref = ev_unref;
470 evapi.loop = ev_loop; 419 evapi.loop = ev_loop;
471 evapi.once = ev_once; 420 evapi.once = ev_once;
472 evapi.io_start = ev_io_start; 421 evapi.io_start = ev_io_start;
546 C_ARGS: evapi.default_loop 495 C_ARGS: evapi.default_loop
547 496
548void ev_now_update () 497void ev_now_update ()
549 C_ARGS: evapi.default_loop 498 C_ARGS: evapi.default_loop
550 499
500void ev_suspend ()
501 C_ARGS: evapi.default_loop
502
503void ev_resume ()
504 C_ARGS: evapi.default_loop
505
551unsigned int ev_backend () 506unsigned int ev_backend ()
552 C_ARGS: evapi.default_loop 507 C_ARGS: evapi.default_loop
553 508
509void ev_loop_verify ()
510 C_ARGS: evapi.default_loop
511
554unsigned int ev_loop_count () 512unsigned int ev_loop_count ()
555 C_ARGS: evapi.default_loop 513 C_ARGS: evapi.default_loop
556 514
515unsigned int ev_loop_depth ()
516 C_ARGS: evapi.default_loop
517
557void ev_set_io_collect_interval (NV interval) 518void ev_set_io_collect_interval (NV interval)
558 C_ARGS: evapi.default_loop, interval 519 C_ARGS: evapi.default_loop, interval
559 520
560void ev_set_timeout_collect_interval (NV interval) 521void ev_set_timeout_collect_interval (NV interval)
561 C_ARGS: evapi.default_loop, interval 522 C_ARGS: evapi.default_loop, interval
570 C_ARGS: evapi.default_loop, fd, revents 531 C_ARGS: evapi.default_loop, fd, revents
571 532
572void ev_feed_signal_event (SV *signal) 533void ev_feed_signal_event (SV *signal)
573 CODE: 534 CODE:
574{ 535{
575 Signal signum = sv_signum (signal); 536 Signal signum = s_signum (signal);
576 CHECK_SIG (signal, signum); 537 CHECK_SIG (signal, signum);
577 538
578 ev_feed_signal_event (evapi.default_loop, signum); 539 ev_feed_signal_event (evapi.default_loop, signum);
579} 540}
580 541
581ev_io *io (SV *fh, int events, SV *cb) 542ev_io *io (SV *fh, int events, SV *cb)
582 ALIAS: 543 ALIAS:
583 io_ns = 1 544 io_ns = 1
584 CODE: 545 CODE:
585{ 546{
586 int fd = sv_fileno (fh); 547 int fd = s_fileno (fh, events & EV_WRITE);
587 CHECK_FD (fh, fd); 548 CHECK_FD (fh, fd);
588 549
589 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv); 550 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv);
590 RETVAL->fh = newSVsv (fh); 551 RETVAL->fh = newSVsv (fh);
591 ev_io_set (RETVAL, fd, events); 552 ev_io_set (RETVAL, fd, events);
626ev_signal *signal (SV *signal, SV *cb) 587ev_signal *signal (SV *signal, SV *cb)
627 ALIAS: 588 ALIAS:
628 signal_ns = 1 589 signal_ns = 1
629 CODE: 590 CODE:
630{ 591{
631 Signal signum = sv_signum (signal); 592 Signal signum = s_signum (signal);
632 CHECK_SIG (signal, signum); 593 CHECK_SIG (signal, signum);
633 594
634 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv); 595 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv);
635 ev_signal_set (RETVAL, signum); 596 ev_signal_set (RETVAL, signum);
636 if (!ix) START (signal, RETVAL); 597 if (!ix) START (signal, RETVAL);
727 688
728void once (SV *fh, int events, SV *timeout, SV *cb) 689void once (SV *fh, int events, SV *timeout, SV *cb)
729 CODE: 690 CODE:
730 ev_once ( 691 ev_once (
731 evapi.default_loop, 692 evapi.default_loop,
732 sv_fileno (fh), events, 693 s_fileno (fh, events & EV_WRITE), events,
733 SvOK (timeout) ? SvNV (timeout) : -1., 694 SvOK (timeout) ? SvNV (timeout) : -1.,
734 e_once_cb, 695 e_once_cb,
735 newSVsv (cb) 696 newSVsv (cb)
736 ); 697 );
737 698
771SV *cb (ev_watcher *w, SV *new_cb = 0) 732SV *cb (ev_watcher *w, SV *new_cb = 0)
772 CODE: 733 CODE:
773{ 734{
774 if (items > 1) 735 if (items > 1)
775 { 736 {
776 new_cb = e_get_cv (new_cb); 737 new_cb = s_get_cv_croak (new_cb);
777 RETVAL = newRV_noinc (w->cb_sv); 738 RETVAL = newRV_noinc (w->cb_sv);
778 w->cb_sv = SvREFCNT_inc (new_cb); 739 w->cb_sv = SvREFCNT_inc (new_cb);
779 } 740 }
780 else 741 else
781 RETVAL = newRV_inc (w->cb_sv); 742 RETVAL = newRV_inc (w->cb_sv);
851 e_destroy (w); 812 e_destroy (w);
852 813
853void set (ev_io *w, SV *fh, int events) 814void set (ev_io *w, SV *fh, int events)
854 CODE: 815 CODE:
855{ 816{
856 int fd = sv_fileno (fh); 817 int fd = s_fileno (fh, events & EV_WRITE);
857 CHECK_FD (fh, fd); 818 CHECK_FD (fh, fd);
858 819
859 sv_setsv (w->fh, fh); 820 sv_setsv (w->fh, fh);
860 RESET (io, w, (w, fd, events)); 821 RESET (io, w, (w, fd, events));
861} 822}
863SV *fh (ev_io *w, SV *new_fh = 0) 824SV *fh (ev_io *w, SV *new_fh = 0)
864 CODE: 825 CODE:
865{ 826{
866 if (items > 1) 827 if (items > 1)
867 { 828 {
868 int fd = sv_fileno (new_fh); 829 int fd = s_fileno (new_fh, w->events & EV_WRITE);
869 CHECK_FD (new_fh, fd); 830 CHECK_FD (new_fh, fd);
870 831
871 RETVAL = w->fh; 832 RETVAL = w->fh;
872 w->fh = newSVsv (new_fh); 833 w->fh = newSVsv (new_fh);
873 834
906 e_destroy (w); 867 e_destroy (w);
907 868
908void set (ev_signal *w, SV *signal) 869void set (ev_signal *w, SV *signal)
909 CODE: 870 CODE:
910{ 871{
911 Signal signum = sv_signum (signal); 872 Signal signum = s_signum (signal);
912 CHECK_SIG (signal, signum); 873 CHECK_SIG (signal, signum);
913 874
914 RESET (signal, w, (w, signum)); 875 RESET (signal, w, (w, signum));
915} 876}
916 877
919{ 880{
920 RETVAL = w->signum; 881 RETVAL = w->signum;
921 882
922 if (items > 1) 883 if (items > 1)
923 { 884 {
924 Signal signum = sv_signum (new_signal); 885 Signal signum = s_signum (new_signal);
925 CHECK_SIG (new_signal, signum); 886 CHECK_SIG (new_signal, signum);
926 887
927 RESET (signal, w, (w, signum)); 888 RESET (signal, w, (w, signum));
928 } 889 }
929} 890}
1264 1225
1265NV ev_now (struct ev_loop *loop) 1226NV ev_now (struct ev_loop *loop)
1266 1227
1267void ev_now_update (struct ev_loop *loop) 1228void ev_now_update (struct ev_loop *loop)
1268 1229
1230void ev_suspend (struct ev_loop *loop)
1231
1232void ev_resume (struct ev_loop *loop)
1233
1269void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) 1234void ev_set_io_collect_interval (struct ev_loop *loop, NV interval)
1270 1235
1271void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) 1236void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval)
1272 1237
1273unsigned int ev_backend (struct ev_loop *loop) 1238unsigned int ev_backend (struct ev_loop *loop)
1274 1239
1275unsigned int ev_loop_count (struct ev_loop *loop) 1240unsigned int ev_loop_count (struct ev_loop *loop)
1276 1241
1242unsigned int ev_loop_depth (struct ev_loop *loop)
1243
1277void ev_loop (struct ev_loop *loop, int flags = 0) 1244void ev_loop (struct ev_loop *loop, int flags = 0)
1278 1245
1279void ev_unloop (struct ev_loop *loop, int how = 1) 1246void ev_unloop (struct ev_loop *loop, int how = 1)
1280 1247
1281void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE) 1248void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE)
1283#if 0 1250#if 0
1284 1251
1285void ev_feed_signal_event (struct ev_loop *loop, SV *signal) 1252void ev_feed_signal_event (struct ev_loop *loop, SV *signal)
1286 CODE: 1253 CODE:
1287{ 1254{
1288 Signal signum = sv_signum (signal); 1255 Signal signum = s_signum (signal);
1289 CHECK_SIG (signal, signum); 1256 CHECK_SIG (signal, signum);
1290 1257
1291 ev_feed_signal_event (loop, signum); 1258 ev_feed_signal_event (loop, signum);
1292} 1259}
1293 1260
1296ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb) 1263ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb)
1297 ALIAS: 1264 ALIAS:
1298 io_ns = 1 1265 io_ns = 1
1299 CODE: 1266 CODE:
1300{ 1267{
1301 int fd = sv_fileno (fh); 1268 int fd = s_fileno (fh, events & EV_WRITE);
1302 CHECK_FD (fh, fd); 1269 CHECK_FD (fh, fd);
1303 1270
1304 RETVAL = e_new (sizeof (ev_io), cb, ST (0)); 1271 RETVAL = e_new (sizeof (ev_io), cb, ST (0));
1305 RETVAL->fh = newSVsv (fh); 1272 RETVAL->fh = newSVsv (fh);
1306 ev_io_set (RETVAL, fd, events); 1273 ev_io_set (RETVAL, fd, events);
1343ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb) 1310ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb)
1344 ALIAS: 1311 ALIAS:
1345 signal_ns = 1 1312 signal_ns = 1
1346 CODE: 1313 CODE:
1347{ 1314{
1348 Signal signum = sv_signum (signal); 1315 Signal signum = s_signum (signal);
1349 CHECK_SIG (signal, signum); 1316 CHECK_SIG (signal, signum);
1350 1317
1351 RETVAL = e_new (sizeof (ev_signal), cb, ST (0)); 1318 RETVAL = e_new (sizeof (ev_signal), cb, ST (0));
1352 ev_signal_set (RETVAL, signum); 1319 ev_signal_set (RETVAL, signum);
1353 if (!ix) START (signal, RETVAL); 1320 if (!ix) START (signal, RETVAL);
1446 1413
1447void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb) 1414void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb)
1448 CODE: 1415 CODE:
1449 ev_once ( 1416 ev_once (
1450 loop, 1417 loop,
1451 sv_fileno (fh), events, 1418 s_fileno (fh, events & EV_WRITE), events,
1452 SvOK (timeout) ? SvNV (timeout) : -1., 1419 SvOK (timeout) ? SvNV (timeout) : -1.,
1453 e_once_cb, 1420 e_once_cb,
1454 newSVsv (cb) 1421 newSVsv (cb)
1455 ); 1422 );
1456 1423

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines