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

Comparing EV/EV.xs (file contents):
Revision 1.31 by root, Thu Nov 1 11:11:21 2007 UTC vs.
Revision 1.56 by root, Tue Nov 6 16:09:37 2007 UTC

2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5/*#include <netinet/in.h>*/ 5/*#include <netinet/in.h>*/
6 6
7#define TIMEOUT_NONE HUGE_VAL
8#define EV_USE_EPOLL 1
9
10#define EV_PROTOTYPES 1 7#define EV_PROTOTYPES 1
11#include "EV/EVAPI.h" 8#include "EV/EVAPI.h"
12 9
10/* fix perl api breakage */
11#undef signal
12#undef sigaction
13
14/* due to bugs in OS X we have to use libev/ explicitly here */
13#include "libev/ev.c" 15#include "libev/ev.c"
16#include "event.c"
17
18#ifndef WIN32
19#define DNS_USE_GETTIMEOFDAY_FOR_ID 1
20#if !defined (WIN32) && !defined(__CYGWIN__)
21# define HAVE_STRUCT_IN6_ADDR 1
22#endif
23#undef HAVE_STRTOK_R
24#undef strtok_r
25#define strtok_r fake_strtok_r
26#include "evdns.c"
27#endif
28
29#ifndef WIN32
30# include <pthread.h>
31#endif
14 32
15typedef int Signal; 33typedef int Signal;
16 34
17static struct EVAPI evapi; 35static struct EVAPI evapi;
18 36
25 *stash_idle, 43 *stash_idle,
26 *stash_prepare, 44 *stash_prepare,
27 *stash_check, 45 *stash_check,
28 *stash_child; 46 *stash_child;
29 47
48#ifndef SIG_SIZE
49/* kudos to Slaven Rezic for the idea */
50static char sig_size [] = { SIG_NUM };
51# define SIG_SIZE (sizeof (sig_size) + 1)
52#endif
53
30static int 54static int
31sv_signum (SV *sig) 55sv_signum (SV *sig)
32{ 56{
33 int signum; 57 int signum;
34 58
81 w->fh = 0; 105 w->fh = 0;
82 w->cb_sv = newSVsv (cb_sv); 106 w->cb_sv = newSVsv (cb_sv);
83 w->self = self; 107 w->self = self;
84 108
85 return (void *)w; 109 return (void *)w;
110}
111
112static void
113e_destroy (void *w_)
114{
115 struct ev_watcher *w = w_;
116
117 SvREFCNT_dec (w->fh ); w->fh = 0;
118 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0;
86} 119}
87 120
88static SV * 121static SV *
89e_bless (struct ev_watcher *w, HV *stash) 122e_bless (struct ev_watcher *w, HV *stash)
90{ 123{
108 dSP; 141 dSP;
109 I32 mark = SP - PL_stack_base; 142 I32 mark = SP - PL_stack_base;
110 SV *sv_self, *sv_events, *sv_status = 0; 143 SV *sv_self, *sv_events, *sv_status = 0;
111 static SV *sv_events_cache; 144 static SV *sv_events_cache;
112 145
113 fprintf (stderr, "e_cb %p,%x\n", w, revents);//D
114
115 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ 146 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */
116 147
117 if (sv_events_cache) 148 if (sv_events_cache)
118 { 149 {
119 sv_events = sv_events_cache; sv_events_cache = 0; 150 sv_events = sv_events_cache; sv_events_cache = 0;
125 PUSHMARK (SP); 156 PUSHMARK (SP);
126 EXTEND (SP, 2); 157 EXTEND (SP, 2);
127 PUSHs (sv_self); 158 PUSHs (sv_self);
128 PUSHs (sv_events); 159 PUSHs (sv_events);
129 160
130 if (revents & EV_CHILD)
131 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
132
133 PUTBACK; 161 PUTBACK;
134 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 162 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
135 SP = PL_stack_base + mark; PUTBACK; 163 SP = PL_stack_base + mark; PUTBACK;
136 164
137 SvREFCNT_dec (sv_self); 165 SvREFCNT_dec (sv_self);
149 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 177 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
150 SP = PL_stack_base + mark; PUTBACK; 178 SP = PL_stack_base + mark; PUTBACK;
151 } 179 }
152} 180}
153 181
154#if 0
155///////////////////////////////////////////////////////////////////////////// 182/////////////////////////////////////////////////////////////////////////////
156// DNS 183// DNS
157 184
185#ifndef WIN32
158static void 186static void
159dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) 187dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg)
160{ 188{
161 dSP; 189 dSP;
162 SV *cb = (SV *)arg; 190 SV *cb = (SV *)arg;
218 246
219PROTOTYPES: ENABLE 247PROTOTYPES: ENABLE
220 248
221BOOT: 249BOOT:
222{ 250{
223 int i;
224 HV *stash = gv_stashpv ("EV", 1); 251 HV *stash = gv_stashpv ("EV", 1);
225 252
226 static const struct { 253 static const struct {
227 const char *name; 254 const char *name;
228 IV iv; 255 IV iv;
229 } *civ, const_iv[] = { 256 } *civ, const_iv[] = {
230# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 257# define const_iv(pfx, name) { # name, (IV) pfx ## name },
258 const_iv (EV_, MINPRI)
259 const_iv (EV_, MAXPRI)
260
231 const_iv (EV_, UNDEF) 261 const_iv (EV_, UNDEF)
232 const_iv (EV_, NONE) 262 const_iv (EV_, NONE)
233 const_iv (EV_, TIMEOUT) 263 const_iv (EV_, TIMEOUT)
234 const_iv (EV_, READ) 264 const_iv (EV_, READ)
235 const_iv (EV_, WRITE) 265 const_iv (EV_, WRITE)
239 const_iv (EV_, ERROR) 269 const_iv (EV_, ERROR)
240 270
241 const_iv (EV, LOOP_ONESHOT) 271 const_iv (EV, LOOP_ONESHOT)
242 const_iv (EV, LOOP_NONBLOCK) 272 const_iv (EV, LOOP_NONBLOCK)
243 273
244 const_iv (EV, METHOD_NONE) 274 const_iv (EV, METHOD_AUTO)
245 const_iv (EV, METHOD_SELECT) 275 const_iv (EV, METHOD_SELECT)
276 const_iv (EV, METHOD_POLL)
246 const_iv (EV, METHOD_EPOLL) 277 const_iv (EV, METHOD_EPOLL)
278 const_iv (EV, METHOD_KQUEUE)
279 const_iv (EV, METHOD_DEVPOLL)
280 const_iv (EV, METHOD_PORT)
281 const_iv (EV, METHOD_ANY)
247 }; 282 };
248 283
249 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 284 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
250 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 285 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
251 286
266 /* the poor man's shared library emulator */ 301 /* the poor man's shared library emulator */
267 evapi.ver = EV_API_VERSION; 302 evapi.ver = EV_API_VERSION;
268 evapi.rev = EV_API_REVISION; 303 evapi.rev = EV_API_REVISION;
269 evapi.sv_fileno = sv_fileno; 304 evapi.sv_fileno = sv_fileno;
270 evapi.sv_signum = sv_signum; 305 evapi.sv_signum = sv_signum;
271 evapi.now = &ev_now; 306 evapi.now = ev_now;
272 evapi.method = &ev_method; 307 evapi.method = ev_method;
273 evapi.loop_done = &ev_loop_done; 308 evapi.unloop = ev_unloop;
274 evapi.time = ev_time; 309 evapi.time = ev_time;
275 evapi.loop = ev_loop; 310 evapi.loop = ev_loop;
276 evapi.once = ev_once; 311 evapi.once = ev_once;
277 evapi.io_start = ev_io_start; 312 evapi.io_start = ev_io_start;
278 evapi.io_stop = ev_io_stop; 313 evapi.io_stop = ev_io_stop;
293 evapi.child_stop = ev_child_stop; 328 evapi.child_stop = ev_child_stop;
294 329
295 sv_setiv (sv, (IV)&evapi); 330 sv_setiv (sv, (IV)&evapi);
296 SvREADONLY_on (sv); 331 SvREADONLY_on (sv);
297 } 332 }
333#ifndef WIN32
334 pthread_atfork (0, 0, ev_default_fork);
335#endif
298} 336}
299 337
300NV ev_now () 338NV ev_now ()
301 CODE:
302 RETVAL = ev_now;
303 OUTPUT:
304 RETVAL
305 339
306int ev_method () 340int ev_method ()
307 CODE:
308 RETVAL = ev_method;
309 OUTPUT:
310 RETVAL
311 341
312NV ev_time () 342NV ev_time ()
313 343
314void ev_init (int flags = 0) 344int ev_default_loop (int methods = EVMETHOD_AUTO)
315 345
316void ev_loop (int flags = 0) 346void ev_loop (int flags = 0)
317 347
318void ev_loop_done (int value = 1) 348void ev_unloop (int how = 1)
319 CODE:
320 ev_loop_done = value;
321 349
322struct ev_io *io (SV *fh, int events, SV *cb) 350struct ev_io *io (SV *fh, int events, SV *cb)
323 ALIAS: 351 ALIAS:
324 io_ns = 1 352 io_ns = 1
325 CODE: 353 CODE:
401 429
402struct ev_child *child (int pid, SV *cb) 430struct ev_child *child (int pid, SV *cb)
403 ALIAS: 431 ALIAS:
404 check_ns = 1 432 check_ns = 1
405 CODE: 433 CODE:
406 RETVAL = e_new (sizeof (struct ev_check), cb); 434 RETVAL = e_new (sizeof (struct ev_child), cb);
407 ev_child_set (RETVAL, pid); 435 ev_child_set (RETVAL, pid);
408 if (!ix) ev_child_start (RETVAL); 436 if (!ix) ev_child_start (RETVAL);
409 OUTPUT: 437 OUTPUT:
410 RETVAL 438 RETVAL
411 439
429 457
430void trigger (struct ev_watcher *w, int revents = EV_NONE) 458void trigger (struct ev_watcher *w, int revents = EV_NONE)
431 CODE: 459 CODE:
432 w->cb (w, revents); 460 w->cb (w, revents);
433 461
462int priority (struct ev_watcher *w, int new_priority = 0)
463 CODE:
464{
465 RETVAL = w->priority;
466
467 if (items > 1)
468 {
469 int active = ev_is_active (w);
470
471 if (new_priority < EV_MINPRI || new_priority > EV_MAXPRI)
472 croak ("watcher priority out of range, value must be between %d and %d, inclusive", EV_MINPRI, EV_MAXPRI);
473
474 if (active)
475 {
476 /* grrr. */
477 PUSHMARK (SP);
478 XPUSHs (ST (0));
479 call_method ("stop", G_DISCARD | G_VOID);
480 }
481
482 ev_set_priority (w, new_priority);
483
484 if (active)
485 {
486 PUSHMARK (SP);
487 XPUSHs (ST (0));
488 call_method ("start", G_DISCARD | G_VOID);
489 }
490 }
491}
492 OUTPUT:
493 RETVAL
494
434MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 495MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
435 496
436void ev_io_start (struct ev_io *w) 497void ev_io_start (struct ev_io *w)
437 498
438void ev_io_stop (struct ev_io *w) 499void ev_io_stop (struct ev_io *w)
439 500
440void DESTROY (struct ev_io *w) 501void DESTROY (struct ev_io *w)
441 CODE: 502 CODE:
442 ev_io_stop (w); 503 ev_io_stop (w);
504 e_destroy (w);
443 505
444void set (struct ev_io *w, SV *fh, int events) 506void set (struct ev_io *w, SV *fh, int events)
445 CODE: 507 CODE:
446{ 508{
447 int active = w->active; 509 int active = ev_is_active (w);
448 int fd = sv_fileno (fh); 510 int fd = sv_fileno (fh);
449 CHECK_FD (fh, fd); 511 CHECK_FD (fh, fd);
450 512
451 if (active) ev_io_stop (w); 513 if (active) ev_io_stop (w);
452 514
461{ 523{
462 RETVAL = newSVsv (w->fh); 524 RETVAL = newSVsv (w->fh);
463 525
464 if (items > 1) 526 if (items > 1)
465 { 527 {
466 int active = w->active; 528 int active = ev_is_active (w);
467 if (active) ev_io_stop (w); 529 if (active) ev_io_stop (w);
468 530
469 sv_setsv (w->fh, new_fh); 531 sv_setsv (w->fh, new_fh);
470 ev_io_set (w, sv_fileno (w->fh), w->events); 532 ev_io_set (w, sv_fileno (w->fh), w->events);
471 533
473 } 535 }
474} 536}
475 OUTPUT: 537 OUTPUT:
476 RETVAL 538 RETVAL
477 539
478short events (struct ev_io *w, short new_events = EV_UNDEF) 540int events (struct ev_io *w, int new_events = EV_UNDEF)
479 CODE: 541 CODE:
480{ 542{
481 RETVAL = w->events; 543 RETVAL = w->events;
482 544
483 if (items > 1) 545 if (items > 1)
484 { 546 {
485 int active = w->active; 547 int active = ev_is_active (w);
486 if (active) ev_io_stop (w); 548 if (active) ev_io_stop (w);
487 549
488 ev_io_set (w, w->fd, new_events); 550 ev_io_set (w, w->fd, new_events);
489 551
490 if (active) ev_io_start (w); 552 if (active) ev_io_start (w);
500void ev_signal_stop (struct ev_signal *w) 562void ev_signal_stop (struct ev_signal *w)
501 563
502void DESTROY (struct ev_signal *w) 564void DESTROY (struct ev_signal *w)
503 CODE: 565 CODE:
504 ev_signal_stop (w); 566 ev_signal_stop (w);
567 e_destroy (w);
505 568
506void set (struct ev_signal *w, SV *signal = 0) 569void set (struct ev_signal *w, SV *signal)
507 CODE: 570 CODE:
508{ 571{
509 Signal signum = sv_signum (signal); /* may croak here */ 572 Signal signum = sv_signum (signal); /* may croak here */
510 int active = w->active; 573 int active = ev_is_active (w);
511 574
512 if (active) ev_signal_stop (w); 575 if (active) ev_signal_stop (w);
576
513 ev_signal_set (w, signum); 577 ev_signal_set (w, signum);
578
514 if (active) ev_signal_start (w); 579 if (active) ev_signal_start (w);
515} 580}
516 581
517MODULE = EV PACKAGE = EV::Time 582int signal (struct ev_signal *w, SV *new_signal = 0)
583 CODE:
584{
585 RETVAL = w->signum;
586
587 if (items > 1)
588 {
589 Signal signum = sv_signum (new_signal); /* may croak here */
590 int active = ev_is_active (w);
591 if (active) ev_signal_stop (w);
592
593 ev_signal_set (w, signum);
594
595 if (active) ev_signal_start (w);
596 }
597}
598 OUTPUT:
599 RETVAL
518 600
519MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 601MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
520 602
521void ev_timer_start (struct ev_timer *w) 603void ev_timer_start (struct ev_timer *w)
522 INIT: 604 INIT:
529 CHECK_REPEAT (w->repeat); 611 CHECK_REPEAT (w->repeat);
530 612
531void DESTROY (struct ev_timer *w) 613void DESTROY (struct ev_timer *w)
532 CODE: 614 CODE:
533 ev_timer_stop (w); 615 ev_timer_stop (w);
616 e_destroy (w);
534 617
535void set (struct ev_timer *w, NV after, NV repeat = 0.) 618void set (struct ev_timer *w, NV after, NV repeat = 0.)
536 INIT: 619 INIT:
537 CHECK_REPEAT (repeat); 620 CHECK_REPEAT (repeat);
538 CODE: 621 CODE:
539{ 622{
540 int active = w->active; 623 int active = ev_is_active (w);
541 if (active) ev_timer_stop (w); 624 if (active) ev_timer_stop (w);
542 ev_timer_set (w, after, repeat); 625 ev_timer_set (w, after, repeat);
543 if (active) ev_timer_start (w); 626 if (active) ev_timer_start (w);
544} 627}
545 628
552void ev_periodic_stop (struct ev_periodic *w) 635void ev_periodic_stop (struct ev_periodic *w)
553 636
554void DESTROY (struct ev_periodic *w) 637void DESTROY (struct ev_periodic *w)
555 CODE: 638 CODE:
556 ev_periodic_stop (w); 639 ev_periodic_stop (w);
640 e_destroy (w);
557 641
558void set (struct ev_periodic *w, NV at, NV interval = 0.) 642void set (struct ev_periodic *w, NV at, NV interval = 0.)
559 INIT: 643 INIT:
560 CHECK_REPEAT (interval); 644 CHECK_REPEAT (interval);
561 CODE: 645 CODE:
562{ 646{
563 int active = w->active; 647 int active = ev_is_active (w);
564 if (active) ev_periodic_stop (w); 648 if (active) ev_periodic_stop (w);
649
565 ev_periodic_set (w, at, interval); 650 ev_periodic_set (w, at, interval);
651
566 if (active) ev_periodic_start (w); 652 if (active) ev_periodic_start (w);
567} 653}
568 654
569MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 655MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
570 656
573void ev_idle_stop (struct ev_idle *w) 659void ev_idle_stop (struct ev_idle *w)
574 660
575void DESTROY (struct ev_idle *w) 661void DESTROY (struct ev_idle *w)
576 CODE: 662 CODE:
577 ev_idle_stop (w); 663 ev_idle_stop (w);
664 e_destroy (w);
578 665
579MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_ 666MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_
580 667
581void ev_prepare_start (struct ev_prepare *w) 668void ev_prepare_start (struct ev_prepare *w)
582 669
583void ev_prepare_stop (struct ev_prepare *w) 670void ev_prepare_stop (struct ev_prepare *w)
584 671
585void DESTROY (struct ev_prepare *w) 672void DESTROY (struct ev_prepare *w)
586 CODE: 673 CODE:
587 ev_prepare_stop (w); 674 ev_prepare_stop (w);
675 e_destroy (w);
588 676
589MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_ 677MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_
590 678
591void ev_check_start (struct ev_check *w) 679void ev_check_start (struct ev_check *w)
592 680
593void ev_check_stop (struct ev_check *w) 681void ev_check_stop (struct ev_check *w)
594 682
595void DESTROY (struct ev_check *w) 683void DESTROY (struct ev_check *w)
596 CODE: 684 CODE:
597 ev_check_stop (w); 685 ev_check_stop (w);
686 e_destroy (w);
598 687
599MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ 688MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_
600 689
601void ev_child_start (struct ev_child *w) 690void ev_child_start (struct ev_child *w)
602 691
603void ev_child_stop (struct ev_child *w) 692void ev_child_stop (struct ev_child *w)
604 693
605void DESTROY (struct ev_child *w) 694void DESTROY (struct ev_child *w)
606 CODE: 695 CODE:
607 ev_child_stop (w); 696 ev_child_stop (w);
697 e_destroy (w);
608 698
609void set (struct ev_child *w, int pid) 699void set (struct ev_child *w, int pid)
610 CODE: 700 CODE:
611{ 701{
612 int active = w->active; 702 int active = ev_is_active (w);
613 if (active) ev_child_stop (w); 703 if (active) ev_child_stop (w);
704
614 ev_child_set (w, pid); 705 ev_child_set (w, pid);
706
615 if (active) ev_child_start (w); 707 if (active) ev_child_start (w);
616} 708}
617 709
710int pid (struct ev_child *w, int new_pid = 0)
711 CODE:
712{
713 RETVAL = w->pid;
714
715 if (items > 1)
716 {
717 int active = ev_is_active (w);
718 if (active) ev_child_stop (w);
719
720 ev_child_set (w, new_pid);
721
722 if (active) ev_child_start (w);
723 }
724}
725 OUTPUT:
726 RETVAL
727
728
618int status (struct ev_child *w) 729int rstatus (struct ev_child *w)
730 ALIAS:
731 rpid = 1
619 CODE: 732 CODE:
620 RETVAL = w->status; 733 RETVAL = ix ? w->rpid : w->rstatus;
621 OUTPUT: 734 OUTPUT:
622 RETVAL 735 RETVAL
623 736
624#if 0 737#ifndef WIN32
625 738
626MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 739MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
627 740
628BOOT: 741BOOT:
629{ 742{
712 825
713void evdns_search_add (char *domain) 826void evdns_search_add (char *domain)
714 827
715void evdns_search_ndots_set (int ndots) 828void evdns_search_ndots_set (int ndots)
716 829
830#if 0
717 831
718MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 832MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
719 833
720BOOT: 834BOOT:
721{ 835{
753 867
754#void DESTROY (struct evhttp_request *req); 868#void DESTROY (struct evhttp_request *req);
755 869
756#endif 870#endif
757 871
872#endif
758 873
759 874
760 875
761 876
762 877

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines