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

Comparing EV/EV.xs (file contents):
Revision 1.91 by root, Thu Dec 20 07:12:57 2007 UTC vs.
Revision 1.96 by root, Thu Dec 20 10:12:22 2007 UTC

3#include "XSUB.h" 3#include "XSUB.h"
4 4
5/*#include <netinet/in.h>*/ 5/*#include <netinet/in.h>*/
6 6
7#define EV_PROTOTYPES 1 7#define EV_PROTOTYPES 1
8#define EV_H <ev.h>
8#include "EV/EVAPI.h" 9#include "EV/EVAPI.h"
9 10
10/* fix perl api breakage */ 11/* fix perl api breakage */
11#undef signal 12#undef signal
12#undef sigaction 13#undef sigaction
17# define NFDBITS PERL_NFDBITS 18# define NFDBITS PERL_NFDBITS
18# define fd_mask Perl_fd_mask 19# define fd_mask Perl_fd_mask
19#endif 20#endif
20/* due to bugs in OS X we have to use libev/ explicitly here */ 21/* due to bugs in OS X we have to use libev/ explicitly here */
21#include "libev/ev.c" 22#include "libev/ev.c"
22#include "event.c"
23 23
24#ifndef _WIN32 24#ifndef _WIN32
25# include <pthread.h> 25# include <pthread.h>
26#endif 26#endif
27 27
28#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop))
29
28#define WFLAG_KEEPALIVE 1 30#define WFLAG_KEEPALIVE 1
29 31
30#define UNREF(w) \ 32#define UNREF(w) \
31 if (!((w)->flags & WFLAG_KEEPALIVE) \ 33 if (!((w)->flags & WFLAG_KEEPALIVE) \
32 && !ev_is_active (w)) \ 34 && !ev_is_active (w)) \
33 ev_unref (w->loop); 35 ev_unref (e_loop (w));
34 36
35#define REF(w) \ 37#define REF(w) \
36 if (!((w)->flags & WFLAG_KEEPALIVE) \ 38 if (!((w)->flags & WFLAG_KEEPALIVE) \
37 && ev_is_active (w)) \ 39 && ev_is_active (w)) \
38 ev_ref (w->loop); 40 ev_ref (e_loop (w));
39 41
40#define START(type,w) \ 42#define START(type,w) \
41 do { \ 43 do { \
42 UNREF (w); \ 44 UNREF (w); \
43 ev_ ## type ## _start (w->loop, w); \ 45 ev_ ## type ## _start (e_loop (w), w); \
44 } while (0) 46 } while (0)
45 47
46#define STOP(type,w) \ 48#define STOP(type,w) \
47 do { \ 49 do { \
48 REF (w); \ 50 REF (w); \
49 ev_ ## type ## _stop (w->loop, w); \ 51 ev_ ## type ## _stop (e_loop (w), w); \
50 } while (0) 52 } while (0)
51 53
52#define RESET(type,w,seta) \ 54#define RESET(type,w,seta) \
53 do { \ 55 do { \
54 int active = ev_is_active (w); \ 56 int active = ev_is_active (w); \
57 if (active) START (type, w); \ 59 if (active) START (type, w); \
58 } while (0) 60 } while (0)
59 61
60typedef int Signal; 62typedef int Signal;
61 63
64static SV *default_loop_sv;
65
62static struct EVAPI evapi; 66static struct EVAPI evapi;
63 67
64static HV 68static HV
69 *stash_loop,
65 *stash_watcher, 70 *stash_watcher,
66 *stash_io, 71 *stash_io,
67 *stash_timer, 72 *stash_timer,
68 *stash_periodic, 73 *stash_periodic,
69 *stash_signal, 74 *stash_signal,
121 126
122 return -1; 127 return -1;
123} 128}
124 129
125static void * 130static void *
126e_new (int size, SV *cb_sv) 131e_new (int size, SV *cb_sv, SV *loop)
127{ 132{
128 ev_watcher *w; 133 ev_watcher *w;
129 SV *self = NEWSV (0, size); 134 SV *self = NEWSV (0, size);
130 SvPOK_only (self); 135 SvPOK_only (self);
131 SvCUR_set (self, size); 136 SvCUR_set (self, size);
132 137
133 w = (ev_watcher *)SvPVX (self); 138 w = (ev_watcher *)SvPVX (self);
134 139
135 ev_init (w, e_cb); 140 ev_init (w, e_cb);
136 141
137 w->loop = EV_DEFAULT; 142 w->loop = SvREFCNT_inc (SvRV (loop));
138 w->flags = WFLAG_KEEPALIVE; 143 w->flags = WFLAG_KEEPALIVE;
139 w->data = 0; 144 w->data = 0;
140 w->fh = 0; 145 w->fh = 0;
141 w->cb_sv = newSVsv (cb_sv); 146 w->cb_sv = SvTEMP (cb_sv) && SvREFCNT (cb_sv) == 1 ? SvREFCNT_inc (cb_sv) : newSVsv (cb_sv);
142 w->self = self; 147 w->self = self;
143 148
144 return (void *)w; 149 return (void *)w;
145} 150}
146 151
147static void 152static void
148e_destroy (void *w_) 153e_destroy (void *w_)
149{ 154{
150 ev_watcher *w = (ev_watcher *)w_; 155 ev_watcher *w = (ev_watcher *)w_;
151 156
157 SvREFCNT_dec (w->loop ); w->loop = 0;
152 SvREFCNT_dec (w->fh ); w->fh = 0; 158 SvREFCNT_dec (w->fh ); w->fh = 0;
153 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0; 159 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0;
154 SvREFCNT_dec (w->data ); w->data = 0; 160 SvREFCNT_dec (w->data ); w->data = 0;
155} 161}
156 162
355 }; 361 };
356 362
357 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 363 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
358 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 364 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
359 365
366 stash_loop = gv_stashpv ("EV::Loop" , 1);
360 stash_watcher = gv_stashpv ("EV::Watcher" , 1); 367 stash_watcher = gv_stashpv ("EV::Watcher" , 1);
361 stash_io = gv_stashpv ("EV::IO" , 1); 368 stash_io = gv_stashpv ("EV::IO" , 1);
362 stash_timer = gv_stashpv ("EV::Timer" , 1); 369 stash_timer = gv_stashpv ("EV::Timer" , 1);
363 stash_periodic = gv_stashpv ("EV::Periodic", 1); 370 stash_periodic = gv_stashpv ("EV::Periodic", 1);
364 stash_signal = gv_stashpv ("EV::Signal" , 1); 371 stash_signal = gv_stashpv ("EV::Signal" , 1);
431} 438}
432 439
433SV *ev_default_loop (unsigned int flags = ev_supported_backends ()) 440SV *ev_default_loop (unsigned int flags = ev_supported_backends ())
434 CODE: 441 CODE:
435{ 442{
443 if (!default_loop_sv)
444 {
436 evapi.default_loop = ev_default_loop (flags); 445 evapi.default_loop = ev_default_loop (flags);
446
437 if (!evapi.default_loop) 447 if (!evapi.default_loop)
438 XSRETURN_UNDEF; 448 XSRETURN_UNDEF;
439 449
440 RETVAL = sv_bless (newRV_noinc (newSViv (PTR2IV (ev_default_loop (flags)))), 450 default_loop_sv = sv_bless (newRV_noinc (newSViv (PTR2IV (evapi.default_loop))), stash_loop);
441 gv_stashpv ("EV::Loop::Default", 1)); 451 }
452
453 RETVAL = newSVsv (default_loop_sv);
442} 454}
443 OUTPUT: 455 OUTPUT:
444 RETVAL 456 RETVAL
445 457
446NV ev_time () 458NV ev_time ()
478 CODE: 490 CODE:
479{ 491{
480 int fd = sv_fileno (fh); 492 int fd = sv_fileno (fh);
481 CHECK_FD (fh, fd); 493 CHECK_FD (fh, fd);
482 494
483 RETVAL = e_new (sizeof (ev_io), cb); 495 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv);
484 RETVAL->fh = newSVsv (fh); 496 RETVAL->fh = newSVsv (fh);
485 ev_io_set (RETVAL, fd, events); 497 ev_io_set (RETVAL, fd, events);
486 if (!ix) START (io, RETVAL); 498 if (!ix) START (io, RETVAL);
487} 499}
488 OUTPUT: 500 OUTPUT:
492 ALIAS: 504 ALIAS:
493 timer_ns = 1 505 timer_ns = 1
494 INIT: 506 INIT:
495 CHECK_REPEAT (repeat); 507 CHECK_REPEAT (repeat);
496 CODE: 508 CODE:
497 RETVAL = e_new (sizeof (ev_timer), cb); 509 RETVAL = e_new (sizeof (ev_timer), cb, default_loop_sv);
498 ev_timer_set (RETVAL, after, repeat); 510 ev_timer_set (RETVAL, after, repeat);
499 if (!ix) START (timer, RETVAL); 511 if (!ix) START (timer, RETVAL);
500 OUTPUT: 512 OUTPUT:
501 RETVAL 513 RETVAL
502 514
506 INIT: 518 INIT:
507 CHECK_REPEAT (interval); 519 CHECK_REPEAT (interval);
508 CODE: 520 CODE:
509{ 521{
510 ev_periodic *w; 522 ev_periodic *w;
511 w = e_new (sizeof (ev_periodic), cb); 523 w = e_new (sizeof (ev_periodic), cb, default_loop_sv);
512 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0; 524 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
513 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0); 525 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
514 RETVAL = e_bless ((ev_watcher *)w, stash_periodic); 526 RETVAL = e_bless ((ev_watcher *)w, stash_periodic);
515 if (!ix) START (periodic, w); 527 if (!ix) START (periodic, w);
516} 528}
523 CODE: 535 CODE:
524{ 536{
525 Signal signum = sv_signum (signal); 537 Signal signum = sv_signum (signal);
526 CHECK_SIG (signal, signum); 538 CHECK_SIG (signal, signum);
527 539
528 RETVAL = e_new (sizeof (ev_signal), cb); 540 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv);
529 ev_signal_set (RETVAL, signum); 541 ev_signal_set (RETVAL, signum);
530 if (!ix) START (signal, RETVAL); 542 if (!ix) START (signal, RETVAL);
531} 543}
532 OUTPUT: 544 OUTPUT:
533 RETVAL 545 RETVAL
534 546
535ev_idle *idle (SV *cb) 547ev_idle *idle (SV *cb)
536 ALIAS: 548 ALIAS:
537 idle_ns = 1 549 idle_ns = 1
538 CODE: 550 CODE:
539 RETVAL = e_new (sizeof (ev_idle), cb); 551 RETVAL = e_new (sizeof (ev_idle), cb, default_loop_sv);
540 ev_idle_set (RETVAL); 552 ev_idle_set (RETVAL);
541 if (!ix) START (idle, RETVAL); 553 if (!ix) START (idle, RETVAL);
542 OUTPUT: 554 OUTPUT:
543 RETVAL 555 RETVAL
544 556
545ev_prepare *prepare (SV *cb) 557ev_prepare *prepare (SV *cb)
546 ALIAS: 558 ALIAS:
547 prepare_ns = 1 559 prepare_ns = 1
548 CODE: 560 CODE:
549 RETVAL = e_new (sizeof (ev_prepare), cb); 561 RETVAL = e_new (sizeof (ev_prepare), cb, default_loop_sv);
550 ev_prepare_set (RETVAL); 562 ev_prepare_set (RETVAL);
551 if (!ix) START (prepare, RETVAL); 563 if (!ix) START (prepare, RETVAL);
552 OUTPUT: 564 OUTPUT:
553 RETVAL 565 RETVAL
554 566
555ev_check *check (SV *cb) 567ev_check *check (SV *cb)
556 ALIAS: 568 ALIAS:
557 check_ns = 1 569 check_ns = 1
558 CODE: 570 CODE:
559 RETVAL = e_new (sizeof (ev_check), cb); 571 RETVAL = e_new (sizeof (ev_check), cb, default_loop_sv);
560 ev_check_set (RETVAL); 572 ev_check_set (RETVAL);
561 if (!ix) START (check, RETVAL); 573 if (!ix) START (check, RETVAL);
562 OUTPUT: 574 OUTPUT:
563 RETVAL 575 RETVAL
564 576
565ev_fork *fork (SV *cb) 577ev_fork *fork (SV *cb)
566 ALIAS: 578 ALIAS:
567 fork_ns = 1 579 fork_ns = 1
568 CODE: 580 CODE:
569 RETVAL = e_new (sizeof (ev_fork), cb); 581 RETVAL = e_new (sizeof (ev_fork), cb, default_loop_sv);
570 ev_fork_set (RETVAL); 582 ev_fork_set (RETVAL);
571 if (!ix) START (fork, RETVAL); 583 if (!ix) START (fork, RETVAL);
572 OUTPUT: 584 OUTPUT:
573 RETVAL 585 RETVAL
574 586
575ev_child *child (int pid, SV *cb) 587ev_child *child (int pid, SV *cb)
576 ALIAS: 588 ALIAS:
577 child_ns = 1 589 child_ns = 1
578 CODE: 590 CODE:
579 RETVAL = e_new (sizeof (ev_child), cb); 591 RETVAL = e_new (sizeof (ev_child), cb, default_loop_sv);
580 ev_child_set (RETVAL, pid); 592 ev_child_set (RETVAL, pid);
581 if (!ix) START (child, RETVAL); 593 if (!ix) START (child, RETVAL);
582 OUTPUT: 594 OUTPUT:
583 RETVAL 595 RETVAL
584 596
585ev_stat *stat (SV *path, NV interval, SV *cb) 597ev_stat *stat (SV *path, NV interval, SV *cb)
586 ALIAS: 598 ALIAS:
587 stat_ns = 1 599 stat_ns = 1
588 CODE: 600 CODE:
589 RETVAL = e_new (sizeof (ev_stat), cb); 601 RETVAL = e_new (sizeof (ev_stat), cb, default_loop_sv);
590 RETVAL->fh = newSVsv (path); 602 RETVAL->fh = newSVsv (path);
591 ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); 603 ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval);
592 if (!ix) START (stat, RETVAL); 604 if (!ix) START (stat, RETVAL);
605 OUTPUT:
606 RETVAL
607
608ev_embed *embed (struct ev_loop *loop, SV *cb = &PL_sv_undef)
609 ALIAS:
610 embed_ns = 1
611 CODE:
612{
613 if (!(ev_backend (loop) & ev_embeddable_backends ()))
614 croak ("passed loop is not embeddable via EV::embed,");
615
616 RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv);
617 RETVAL->fh = newSVsv (ST (0));
618 ev_embed_set (RETVAL, loop);
619
620 if (!SvOK (cb)) ev_set_cb (RETVAL, 0);
621
622 if (!ix) START (embed, RETVAL);
623}
593 OUTPUT: 624 OUTPUT:
594 RETVAL 625 RETVAL
595 626
596void once (SV *fh, int events, SV *timeout, SV *cb) 627void once (SV *fh, int events, SV *timeout, SV *cb)
597 CODE: 628 CODE:
610int ev_is_active (ev_watcher *w) 641int ev_is_active (ev_watcher *w)
611 642
612int ev_is_pending (ev_watcher *w) 643int ev_is_pending (ev_watcher *w)
613 644
614void ev_invoke (ev_watcher *w, int revents = EV_NONE) 645void ev_invoke (ev_watcher *w, int revents = EV_NONE)
615 C_ARGS: w->loop, w, revents 646 C_ARGS: e_loop (w), w, revents
616 647
617int ev_clear_pending (ev_watcher *w) 648int ev_clear_pending (ev_watcher *w)
618 C_ARGS: w->loop, w 649 C_ARGS: e_loop (w), w
619 650
620void ev_feed_event (ev_watcher *w, int revents = EV_NONE) 651void ev_feed_event (ev_watcher *w, int revents = EV_NONE)
621 C_ARGS: w->loop, w, revents 652 C_ARGS: e_loop (w), w, revents
622 653
623int keepalive (ev_watcher *w, int new_value = 0) 654int keepalive (ev_watcher *w, int new_value = 0)
624 CODE: 655 CODE:
625{ 656{
626 RETVAL = w->flags & WFLAG_KEEPALIVE; 657 RETVAL = w->flags & WFLAG_KEEPALIVE;
803void ev_timer_again (ev_timer *w) 834void ev_timer_again (ev_timer *w)
804 INIT: 835 INIT:
805 CHECK_REPEAT (w->repeat); 836 CHECK_REPEAT (w->repeat);
806 CODE: 837 CODE:
807 REF (w); 838 REF (w);
808 ev_timer_again (w->loop, w); 839 ev_timer_again (e_loop (w), w);
809 UNREF (w); 840 UNREF (w);
810 841
811void DESTROY (ev_timer *w) 842void DESTROY (ev_timer *w)
812 CODE: 843 CODE:
813 STOP (timer, w); 844 STOP (timer, w);
838 STOP (periodic, w); 869 STOP (periodic, w);
839 870
840void ev_periodic_again (ev_periodic *w) 871void ev_periodic_again (ev_periodic *w)
841 CODE: 872 CODE:
842 REF (w); 873 REF (w);
843 ev_periodic_again (w->loop, w); 874 ev_periodic_again (e_loop (w), w);
844 UNREF (w); 875 UNREF (w);
845 876
846void DESTROY (ev_periodic *w) 877void DESTROY (ev_periodic *w)
847 CODE: 878 CODE:
848 STOP (periodic, w); 879 STOP (periodic, w);
1019 PPCODE: 1050 PPCODE:
1020{ 1051{
1021 ev_statdata *s = ix ? &w->attr : &w->prev; 1052 ev_statdata *s = ix ? &w->attr : &w->prev;
1022 1053
1023 if (ix == 1) 1054 if (ix == 1)
1024 ev_stat_stat (w->loop, w); 1055 ev_stat_stat (e_loop (w), w);
1025 else if (!s->st_nlink) 1056 else if (!s->st_nlink)
1026 errno = ENOENT; 1057 errno = ENOENT;
1027 1058
1028 PL_statcache.st_dev = s->st_nlink; 1059 PL_statcache.st_dev = s->st_nlink;
1029 PL_statcache.st_ino = s->st_ino; 1060 PL_statcache.st_ino = s->st_ino;
1056 PUSHs (sv_2mortal (newSVuv (4096))); 1087 PUSHs (sv_2mortal (newSVuv (4096)));
1057 PUSHs (sv_2mortal (newSVnv ((NV)((s->st_size + 4095) / 4096)))); 1088 PUSHs (sv_2mortal (newSVnv ((NV)((s->st_size + 4095) / 4096))));
1058 } 1089 }
1059} 1090}
1060 1091
1061#if 0 1092MODULE = EV PACKAGE = EV::Embed PREFIX = ev_embed_
1062 1093
1094void ev_embed_start (ev_embed *w)
1095 CODE:
1096 START (embed, w);
1097
1098void ev_embed_stop (ev_embed *w)
1099 CODE:
1100 STOP (embed, w);
1101
1102void DESTROY (ev_embed *w)
1103 CODE:
1104 STOP (embed, w);
1105 e_destroy (w);
1106
1107void set (ev_embed *w, struct ev_loop *loop)
1108 CODE:
1109{
1110 sv_setsv (w->fh, ST (1));
1111 RESET (embed, w, (w, loop));
1112}
1113
1114SV *other (ev_embed *w)
1115 CODE:
1116 RETVAL = newSVsv (w->fh);
1117 OUTPUT:
1118 RETVAL
1119
1063MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 1120MODULE = EV PACKAGE = EV::Loop PREFIX = ev_loop_
1064 1121
1065BOOT: 1122SV *new (SV *klass, unsigned int flags = ev_supported_backends ())
1123 CODE:
1066{ 1124{
1067 HV *stash = gv_stashpv ("EV::HTTP", 1); 1125 struct ev_loop *loop = ev_loop_new (flags);
1068 1126
1069 static const struct { 1127 if (!loop)
1070 const char *name; 1128 XSRETURN_UNDEF;
1071 IV iv;
1072 } *civ, const_iv[] = {
1073# define const_iv(pfx, name) { # name, (IV) pfx ## name },
1074 const_iv (HTTP_, OK)
1075 const_iv (HTTP_, NOCONTENT)
1076 const_iv (HTTP_, MOVEPERM)
1077 const_iv (HTTP_, MOVETEMP)
1078 const_iv (HTTP_, NOTMODIFIED)
1079 const_iv (HTTP_, BADREQUEST)
1080 const_iv (HTTP_, NOTFOUND)
1081 const_iv (HTTP_, SERVUNAVAIL)
1082 const_iv (EVHTTP_, REQ_OWN_CONNECTION)
1083 const_iv (EVHTTP_, PROXY_REQUEST)
1084 const_iv (EVHTTP_, REQ_GET)
1085 const_iv (EVHTTP_, REQ_POST)
1086 const_iv (EVHTTP_, REQ_HEAD)
1087 const_iv (EVHTTP_, REQUEST)
1088 const_iv (EVHTTP_, RESPONSE)
1089 };
1090 1129
1091 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1130 RETVAL = sv_bless (newRV_noinc (newSViv (PTR2IV (loop))), stash_loop);
1092 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
1093} 1131}
1132 OUTPUT:
1133 RETVAL
1094 1134
1095MODULE = EV PACKAGE = EV::HTTP::Request PREFIX = evhttp_request_ 1135void DESTROY (struct ev_loop *loop)
1136 CODE:
1137 if (loop != evapi.default_loop) /* global destruction sucks */
1138 ev_loop_destroy (loop);
1096 1139
1097#HttpRequest new (SV *klass, SV *cb) 1140ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb)
1141 ALIAS:
1142 io_ns = 1
1143 CODE:
1144{
1145 int fd = sv_fileno (fh);
1146 CHECK_FD (fh, fd);
1098 1147
1099#void DESTROY (struct evhttp_request *req); 1148 RETVAL = e_new (sizeof (ev_io), cb, ST (0));
1149 RETVAL->fh = newSVsv (fh);
1150 ev_io_set (RETVAL, fd, events);
1151 if (!ix) START (io, RETVAL);
1152}
1153 OUTPUT:
1154 RETVAL
1100 1155
1101#endif
1102
1103
1104
1105
1106
1107
1108
1109

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines