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.50 by root, Sun Nov 4 16:52:52 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
13#include "libev/ev.c" 10#include "libev/ev.c"
11#include "libev/event.h"
12#include "libev/event.c"
13#define DNS_USE_GETTIMEOFDAY_FOR_ID 1
14#define HAVE_STRUCT_IN6_ADDR 1
15#undef HAVE_STRTOK_R
16#undef strtok_r
17#define strtok_r fake_strtok_r
18#include "libev/evdns.h"
19#include "libev/evdns.c"
14 20
15typedef int Signal; 21typedef int Signal;
16 22
17static struct EVAPI evapi; 23static struct EVAPI evapi;
18 24
25 *stash_idle, 31 *stash_idle,
26 *stash_prepare, 32 *stash_prepare,
27 *stash_check, 33 *stash_check,
28 *stash_child; 34 *stash_child;
29 35
36#ifndef SIG_SIZE
37/* kudos to Slaven Rezic for the idea */
38static char sig_size [] = { SIG_NUM };
39# define SIG_SIZE (sizeof (sig_size) + 1)
40#endif
41
30static int 42static int
31sv_signum (SV *sig) 43sv_signum (SV *sig)
32{ 44{
33 int signum; 45 int signum;
34 46
81 w->fh = 0; 93 w->fh = 0;
82 w->cb_sv = newSVsv (cb_sv); 94 w->cb_sv = newSVsv (cb_sv);
83 w->self = self; 95 w->self = self;
84 96
85 return (void *)w; 97 return (void *)w;
98}
99
100static void *
101e_destroy (void *w_)
102{
103 struct ev_watcher *w = w_;
104
105 SvREFCNT_dec (w->fh ); w->fh = 0;
106 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0;
86} 107}
87 108
88static SV * 109static SV *
89e_bless (struct ev_watcher *w, HV *stash) 110e_bless (struct ev_watcher *w, HV *stash)
90{ 111{
108 dSP; 129 dSP;
109 I32 mark = SP - PL_stack_base; 130 I32 mark = SP - PL_stack_base;
110 SV *sv_self, *sv_events, *sv_status = 0; 131 SV *sv_self, *sv_events, *sv_status = 0;
111 static SV *sv_events_cache; 132 static SV *sv_events_cache;
112 133
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 */ 134 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */
116 135
117 if (sv_events_cache) 136 if (sv_events_cache)
118 { 137 {
119 sv_events = sv_events_cache; sv_events_cache = 0; 138 sv_events = sv_events_cache; sv_events_cache = 0;
125 PUSHMARK (SP); 144 PUSHMARK (SP);
126 EXTEND (SP, 2); 145 EXTEND (SP, 2);
127 PUSHs (sv_self); 146 PUSHs (sv_self);
128 PUSHs (sv_events); 147 PUSHs (sv_events);
129 148
130 if (revents & EV_CHILD)
131 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
132
133 PUTBACK; 149 PUTBACK;
134 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 150 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
135 SP = PL_stack_base + mark; PUTBACK; 151 SP = PL_stack_base + mark; PUTBACK;
136 152
137 SvREFCNT_dec (sv_self); 153 SvREFCNT_dec (sv_self);
149 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 165 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
150 SP = PL_stack_base + mark; PUTBACK; 166 SP = PL_stack_base + mark; PUTBACK;
151 } 167 }
152} 168}
153 169
154#if 0
155///////////////////////////////////////////////////////////////////////////// 170/////////////////////////////////////////////////////////////////////////////
156// DNS 171// DNS
157 172
158static void 173static void
159dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) 174dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg)
201 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 216 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
202 } 217 }
203 218
204 LEAVE; 219 LEAVE;
205} 220}
206#endif
207 221
208#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 222#define CHECK_REPEAT(repeat) if (repeat < 0.) \
209 croak (# repeat " value must be >= 0"); 223 croak (# repeat " value must be >= 0");
210 224
211#define CHECK_FD(fh,fd) if ((fd) < 0) \ 225#define CHECK_FD(fh,fd) if ((fd) < 0) \
226 static const struct { 240 static const struct {
227 const char *name; 241 const char *name;
228 IV iv; 242 IV iv;
229 } *civ, const_iv[] = { 243 } *civ, const_iv[] = {
230# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 244# define const_iv(pfx, name) { # name, (IV) pfx ## name },
245 const_iv (EV_, MINPRI)
246 const_iv (EV_, MAXPRI)
247
231 const_iv (EV_, UNDEF) 248 const_iv (EV_, UNDEF)
232 const_iv (EV_, NONE) 249 const_iv (EV_, NONE)
233 const_iv (EV_, TIMEOUT) 250 const_iv (EV_, TIMEOUT)
234 const_iv (EV_, READ) 251 const_iv (EV_, READ)
235 const_iv (EV_, WRITE) 252 const_iv (EV_, WRITE)
239 const_iv (EV_, ERROR) 256 const_iv (EV_, ERROR)
240 257
241 const_iv (EV, LOOP_ONESHOT) 258 const_iv (EV, LOOP_ONESHOT)
242 const_iv (EV, LOOP_NONBLOCK) 259 const_iv (EV, LOOP_NONBLOCK)
243 260
244 const_iv (EV, METHOD_NONE) 261 const_iv (EV, METHOD_AUTO)
245 const_iv (EV, METHOD_SELECT) 262 const_iv (EV, METHOD_SELECT)
263 const_iv (EV, METHOD_POLL)
246 const_iv (EV, METHOD_EPOLL) 264 const_iv (EV, METHOD_EPOLL)
265 const_iv (EV, METHOD_KQUEUE)
266 const_iv (EV, METHOD_DEVPOLL)
267 const_iv (EV, METHOD_PORT)
268 const_iv (EV, METHOD_ANY)
247 }; 269 };
248 270
249 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 271 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
250 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 272 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
251 273
266 /* the poor man's shared library emulator */ 288 /* the poor man's shared library emulator */
267 evapi.ver = EV_API_VERSION; 289 evapi.ver = EV_API_VERSION;
268 evapi.rev = EV_API_REVISION; 290 evapi.rev = EV_API_REVISION;
269 evapi.sv_fileno = sv_fileno; 291 evapi.sv_fileno = sv_fileno;
270 evapi.sv_signum = sv_signum; 292 evapi.sv_signum = sv_signum;
271 evapi.now = &ev_now; 293 evapi.now = ev_now;
272 evapi.method = &ev_method; 294 evapi.method = ev_method;
273 evapi.loop_done = &ev_loop_done; 295 evapi.unloop = ev_unloop;
274 evapi.time = ev_time; 296 evapi.time = ev_time;
275 evapi.loop = ev_loop; 297 evapi.loop = ev_loop;
276 evapi.once = ev_once; 298 evapi.once = ev_once;
277 evapi.io_start = ev_io_start; 299 evapi.io_start = ev_io_start;
278 evapi.io_stop = ev_io_stop; 300 evapi.io_stop = ev_io_stop;
293 evapi.child_stop = ev_child_stop; 315 evapi.child_stop = ev_child_stop;
294 316
295 sv_setiv (sv, (IV)&evapi); 317 sv_setiv (sv, (IV)&evapi);
296 SvREADONLY_on (sv); 318 SvREADONLY_on (sv);
297 } 319 }
320
321 pthread_atfork (0, 0, ev_default_fork);
298} 322}
299 323
300NV ev_now () 324NV ev_now ()
301 CODE:
302 RETVAL = ev_now;
303 OUTPUT:
304 RETVAL
305 325
306int ev_method () 326int ev_method ()
307 CODE:
308 RETVAL = ev_method;
309 OUTPUT:
310 RETVAL
311 327
312NV ev_time () 328NV ev_time ()
313 329
314void ev_init (int flags = 0) 330int ev_default_loop (int methods = EVMETHOD_AUTO)
315 331
316void ev_loop (int flags = 0) 332void ev_loop (int flags = 0)
317 333
318void ev_loop_done (int value = 1) 334void ev_unloop (int how = 1)
319 CODE:
320 ev_loop_done = value;
321 335
322struct ev_io *io (SV *fh, int events, SV *cb) 336struct ev_io *io (SV *fh, int events, SV *cb)
323 ALIAS: 337 ALIAS:
324 io_ns = 1 338 io_ns = 1
325 CODE: 339 CODE:
401 415
402struct ev_child *child (int pid, SV *cb) 416struct ev_child *child (int pid, SV *cb)
403 ALIAS: 417 ALIAS:
404 check_ns = 1 418 check_ns = 1
405 CODE: 419 CODE:
406 RETVAL = e_new (sizeof (struct ev_check), cb); 420 RETVAL = e_new (sizeof (struct ev_child), cb);
407 ev_child_set (RETVAL, pid); 421 ev_child_set (RETVAL, pid);
408 if (!ix) ev_child_start (RETVAL); 422 if (!ix) ev_child_start (RETVAL);
409 OUTPUT: 423 OUTPUT:
410 RETVAL 424 RETVAL
411 425
429 443
430void trigger (struct ev_watcher *w, int revents = EV_NONE) 444void trigger (struct ev_watcher *w, int revents = EV_NONE)
431 CODE: 445 CODE:
432 w->cb (w, revents); 446 w->cb (w, revents);
433 447
448int priority (struct ev_watcher *w, int new_priority = 0)
449 CODE:
450{
451 RETVAL = w->priority;
452
453 if (items > 1)
454 {
455 int active = ev_is_active (w);
456
457 if (new_priority < EV_MINPRI || new_priority > EV_MAXPRI)
458 croak ("watcher priority out of range, value must be between %d and %d, inclusive", EV_MINPRI, EV_MAXPRI);
459
460 if (active)
461 {
462 /* grrr. */
463 PUSHMARK (SP);
464 XPUSHs (ST (0));
465 call_method ("stop", G_DISCARD | G_VOID);
466 }
467
468 ev_set_priority (w, new_priority);
469
470 if (active)
471 {
472 PUSHMARK (SP);
473 XPUSHs (ST (0));
474 call_method ("start", G_DISCARD | G_VOID);
475 }
476 }
477}
478 OUTPUT:
479 RETVAL
480
434MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 481MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
435 482
436void ev_io_start (struct ev_io *w) 483void ev_io_start (struct ev_io *w)
437 484
438void ev_io_stop (struct ev_io *w) 485void ev_io_stop (struct ev_io *w)
439 486
440void DESTROY (struct ev_io *w) 487void DESTROY (struct ev_io *w)
441 CODE: 488 CODE:
442 ev_io_stop (w); 489 ev_io_stop (w);
490 e_destroy (w);
443 491
444void set (struct ev_io *w, SV *fh, int events) 492void set (struct ev_io *w, SV *fh, int events)
445 CODE: 493 CODE:
446{ 494{
447 int active = w->active; 495 int active = ev_is_active (w);
448 int fd = sv_fileno (fh); 496 int fd = sv_fileno (fh);
449 CHECK_FD (fh, fd); 497 CHECK_FD (fh, fd);
450 498
451 if (active) ev_io_stop (w); 499 if (active) ev_io_stop (w);
452 500
461{ 509{
462 RETVAL = newSVsv (w->fh); 510 RETVAL = newSVsv (w->fh);
463 511
464 if (items > 1) 512 if (items > 1)
465 { 513 {
466 int active = w->active; 514 int active = ev_is_active (w);
467 if (active) ev_io_stop (w); 515 if (active) ev_io_stop (w);
468 516
469 sv_setsv (w->fh, new_fh); 517 sv_setsv (w->fh, new_fh);
470 ev_io_set (w, sv_fileno (w->fh), w->events); 518 ev_io_set (w, sv_fileno (w->fh), w->events);
471 519
473 } 521 }
474} 522}
475 OUTPUT: 523 OUTPUT:
476 RETVAL 524 RETVAL
477 525
478short events (struct ev_io *w, short new_events = EV_UNDEF) 526int events (struct ev_io *w, int new_events = EV_UNDEF)
479 CODE: 527 CODE:
480{ 528{
481 RETVAL = w->events; 529 RETVAL = w->events;
482 530
483 if (items > 1) 531 if (items > 1)
484 { 532 {
485 int active = w->active; 533 int active = ev_is_active (w);
486 if (active) ev_io_stop (w); 534 if (active) ev_io_stop (w);
487 535
488 ev_io_set (w, w->fd, new_events); 536 ev_io_set (w, w->fd, new_events);
489 537
490 if (active) ev_io_start (w); 538 if (active) ev_io_start (w);
500void ev_signal_stop (struct ev_signal *w) 548void ev_signal_stop (struct ev_signal *w)
501 549
502void DESTROY (struct ev_signal *w) 550void DESTROY (struct ev_signal *w)
503 CODE: 551 CODE:
504 ev_signal_stop (w); 552 ev_signal_stop (w);
553 e_destroy (w);
505 554
506void set (struct ev_signal *w, SV *signal = 0) 555void set (struct ev_signal *w, SV *signal)
507 CODE: 556 CODE:
508{ 557{
509 Signal signum = sv_signum (signal); /* may croak here */ 558 Signal signum = sv_signum (signal); /* may croak here */
510 int active = w->active; 559 int active = ev_is_active (w);
511 560
512 if (active) ev_signal_stop (w); 561 if (active) ev_signal_stop (w);
562
513 ev_signal_set (w, signum); 563 ev_signal_set (w, signum);
564
514 if (active) ev_signal_start (w); 565 if (active) ev_signal_start (w);
515} 566}
567
568int signal (struct ev_signal *w, SV *new_signal = 0)
569 CODE:
570{
571 RETVAL = w->signum;
572
573 if (items > 1)
574 {
575 Signal signum = sv_signum (new_signal); /* may croak here */
576 int active = ev_is_active (w);
577 if (active) ev_signal_stop (w);
578
579 ev_signal_set (w, signum);
580
581 if (active) ev_signal_start (w);
582 }
583}
584 OUTPUT:
585 RETVAL
516 586
517MODULE = EV PACKAGE = EV::Time 587MODULE = EV PACKAGE = EV::Time
518 588
519MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 589MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
520 590
529 CHECK_REPEAT (w->repeat); 599 CHECK_REPEAT (w->repeat);
530 600
531void DESTROY (struct ev_timer *w) 601void DESTROY (struct ev_timer *w)
532 CODE: 602 CODE:
533 ev_timer_stop (w); 603 ev_timer_stop (w);
604 e_destroy (w);
534 605
535void set (struct ev_timer *w, NV after, NV repeat = 0.) 606void set (struct ev_timer *w, NV after, NV repeat = 0.)
536 INIT: 607 INIT:
537 CHECK_REPEAT (repeat); 608 CHECK_REPEAT (repeat);
538 CODE: 609 CODE:
539{ 610{
540 int active = w->active; 611 int active = ev_is_active (w);
541 if (active) ev_timer_stop (w); 612 if (active) ev_timer_stop (w);
542 ev_timer_set (w, after, repeat); 613 ev_timer_set (w, after, repeat);
543 if (active) ev_timer_start (w); 614 if (active) ev_timer_start (w);
544} 615}
545 616
552void ev_periodic_stop (struct ev_periodic *w) 623void ev_periodic_stop (struct ev_periodic *w)
553 624
554void DESTROY (struct ev_periodic *w) 625void DESTROY (struct ev_periodic *w)
555 CODE: 626 CODE:
556 ev_periodic_stop (w); 627 ev_periodic_stop (w);
628 e_destroy (w);
557 629
558void set (struct ev_periodic *w, NV at, NV interval = 0.) 630void set (struct ev_periodic *w, NV at, NV interval = 0.)
559 INIT: 631 INIT:
560 CHECK_REPEAT (interval); 632 CHECK_REPEAT (interval);
561 CODE: 633 CODE:
562{ 634{
563 int active = w->active; 635 int active = ev_is_active (w);
564 if (active) ev_periodic_stop (w); 636 if (active) ev_periodic_stop (w);
637
565 ev_periodic_set (w, at, interval); 638 ev_periodic_set (w, at, interval);
639
566 if (active) ev_periodic_start (w); 640 if (active) ev_periodic_start (w);
567} 641}
568 642
569MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 643MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
570 644
573void ev_idle_stop (struct ev_idle *w) 647void ev_idle_stop (struct ev_idle *w)
574 648
575void DESTROY (struct ev_idle *w) 649void DESTROY (struct ev_idle *w)
576 CODE: 650 CODE:
577 ev_idle_stop (w); 651 ev_idle_stop (w);
652 e_destroy (w);
578 653
579MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_ 654MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_
580 655
581void ev_prepare_start (struct ev_prepare *w) 656void ev_prepare_start (struct ev_prepare *w)
582 657
583void ev_prepare_stop (struct ev_prepare *w) 658void ev_prepare_stop (struct ev_prepare *w)
584 659
585void DESTROY (struct ev_prepare *w) 660void DESTROY (struct ev_prepare *w)
586 CODE: 661 CODE:
587 ev_prepare_stop (w); 662 ev_prepare_stop (w);
663 e_destroy (w);
588 664
589MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_ 665MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_
590 666
591void ev_check_start (struct ev_check *w) 667void ev_check_start (struct ev_check *w)
592 668
593void ev_check_stop (struct ev_check *w) 669void ev_check_stop (struct ev_check *w)
594 670
595void DESTROY (struct ev_check *w) 671void DESTROY (struct ev_check *w)
596 CODE: 672 CODE:
597 ev_check_stop (w); 673 ev_check_stop (w);
674 e_destroy (w);
598 675
599MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ 676MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_
600 677
601void ev_child_start (struct ev_child *w) 678void ev_child_start (struct ev_child *w)
602 679
603void ev_child_stop (struct ev_child *w) 680void ev_child_stop (struct ev_child *w)
604 681
605void DESTROY (struct ev_child *w) 682void DESTROY (struct ev_child *w)
606 CODE: 683 CODE:
607 ev_child_stop (w); 684 ev_child_stop (w);
685 e_destroy (w);
608 686
609void set (struct ev_child *w, int pid) 687void set (struct ev_child *w, int pid)
610 CODE: 688 CODE:
611{ 689{
612 int active = w->active; 690 int active = ev_is_active (w);
613 if (active) ev_child_stop (w); 691 if (active) ev_child_stop (w);
692
614 ev_child_set (w, pid); 693 ev_child_set (w, pid);
694
615 if (active) ev_child_start (w); 695 if (active) ev_child_start (w);
616} 696}
617 697
698int pid (struct ev_child *w, int new_pid = 0)
699 CODE:
700{
701 RETVAL = w->pid;
702
703 if (items > 1)
704 {
705 int active = ev_is_active (w);
706 if (active) ev_child_stop (w);
707
708 ev_child_set (w, new_pid);
709
710 if (active) ev_child_start (w);
711 }
712}
713 OUTPUT:
714 RETVAL
715
716
618int status (struct ev_child *w) 717int rstatus (struct ev_child *w)
718 ALIAS:
719 rpid = 1
619 CODE: 720 CODE:
620 RETVAL = w->status; 721 RETVAL = ix ? w->rpid : w->rstatus;
621 OUTPUT: 722 OUTPUT:
622 RETVAL 723 RETVAL
623
624#if 0
625 724
626MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 725MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
627 726
628BOOT: 727BOOT:
629{ 728{
712 811
713void evdns_search_add (char *domain) 812void evdns_search_add (char *domain)
714 813
715void evdns_search_ndots_set (int ndots) 814void evdns_search_ndots_set (int ndots)
716 815
816#if 0
717 817
718MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 818MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
719 819
720BOOT: 820BOOT:
721{ 821{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines