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

Comparing EV/EV.xs (file contents):
Revision 1.32 by root, Thu Nov 1 11:11:39 2007 UTC vs.
Revision 1.47 by root, Sat Nov 3 21:58:51 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
8#define EV_STANDALONE 1
11#include "EV/EVAPI.h" 9#include "EV/EVAPI.h"
12 10
13#include "libev/ev.c" 11#include "libev/ev.c"
12#include "libev/event.h"
13#include "libev/event.c"
14#define DNS_USE_GETTIMEOFDAY_FOR_ID 1
15#define HAVE_STRUCT_IN6_ADDR 1
16#undef HAVE_STRTOK_R
17#undef strtok_r
18#define strtok_r fake_strtok_r
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{
123 PUSHMARK (SP); 144 PUSHMARK (SP);
124 EXTEND (SP, 2); 145 EXTEND (SP, 2);
125 PUSHs (sv_self); 146 PUSHs (sv_self);
126 PUSHs (sv_events); 147 PUSHs (sv_events);
127 148
128 if (revents & EV_CHILD)
129 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
130
131 PUTBACK; 149 PUTBACK;
132 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 150 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
133 SP = PL_stack_base + mark; PUTBACK; 151 SP = PL_stack_base + mark; PUTBACK;
134 152
135 SvREFCNT_dec (sv_self); 153 SvREFCNT_dec (sv_self);
147 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);
148 SP = PL_stack_base + mark; PUTBACK; 166 SP = PL_stack_base + mark; PUTBACK;
149 } 167 }
150} 168}
151 169
152#if 0
153///////////////////////////////////////////////////////////////////////////// 170/////////////////////////////////////////////////////////////////////////////
154// DNS 171// DNS
155 172
156static void 173static void
157dns_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)
199 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);
200 } 217 }
201 218
202 LEAVE; 219 LEAVE;
203} 220}
204#endif
205 221
206#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 222#define CHECK_REPEAT(repeat) if (repeat < 0.) \
207 croak (# repeat " value must be >= 0"); 223 croak (# repeat " value must be >= 0");
208 224
209#define CHECK_FD(fh,fd) if ((fd) < 0) \ 225#define CHECK_FD(fh,fd) if ((fd) < 0) \
224 static const struct { 240 static const struct {
225 const char *name; 241 const char *name;
226 IV iv; 242 IV iv;
227 } *civ, const_iv[] = { 243 } *civ, const_iv[] = {
228# 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
229 const_iv (EV_, UNDEF) 248 const_iv (EV_, UNDEF)
230 const_iv (EV_, NONE) 249 const_iv (EV_, NONE)
231 const_iv (EV_, TIMEOUT) 250 const_iv (EV_, TIMEOUT)
232 const_iv (EV_, READ) 251 const_iv (EV_, READ)
233 const_iv (EV_, WRITE) 252 const_iv (EV_, WRITE)
237 const_iv (EV_, ERROR) 256 const_iv (EV_, ERROR)
238 257
239 const_iv (EV, LOOP_ONESHOT) 258 const_iv (EV, LOOP_ONESHOT)
240 const_iv (EV, LOOP_NONBLOCK) 259 const_iv (EV, LOOP_NONBLOCK)
241 260
242 const_iv (EV, METHOD_NONE) 261 const_iv (EV, METHOD_AUTO)
243 const_iv (EV, METHOD_SELECT) 262 const_iv (EV, METHOD_SELECT)
263 const_iv (EV, METHOD_POLL)
244 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)
245 }; 269 };
246 270
247 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; )
248 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 272 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
249 273
264 /* the poor man's shared library emulator */ 288 /* the poor man's shared library emulator */
265 evapi.ver = EV_API_VERSION; 289 evapi.ver = EV_API_VERSION;
266 evapi.rev = EV_API_REVISION; 290 evapi.rev = EV_API_REVISION;
267 evapi.sv_fileno = sv_fileno; 291 evapi.sv_fileno = sv_fileno;
268 evapi.sv_signum = sv_signum; 292 evapi.sv_signum = sv_signum;
269 evapi.now = &ev_now; 293 evapi.now = ev_now;
270 evapi.method = &ev_method; 294 evapi.method = ev_method;
271 evapi.loop_done = &ev_loop_done; 295 evapi.unloop = ev_unloop;
272 evapi.time = ev_time; 296 evapi.time = ev_time;
273 evapi.loop = ev_loop; 297 evapi.loop = ev_loop;
274 evapi.once = ev_once; 298 evapi.once = ev_once;
275 evapi.io_start = ev_io_start; 299 evapi.io_start = ev_io_start;
276 evapi.io_stop = ev_io_stop; 300 evapi.io_stop = ev_io_stop;
291 evapi.child_stop = ev_child_stop; 315 evapi.child_stop = ev_child_stop;
292 316
293 sv_setiv (sv, (IV)&evapi); 317 sv_setiv (sv, (IV)&evapi);
294 SvREADONLY_on (sv); 318 SvREADONLY_on (sv);
295 } 319 }
320
321 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child);
296} 322}
297 323
298NV ev_now () 324NV ev_now ()
299 CODE:
300 RETVAL = ev_now;
301 OUTPUT:
302 RETVAL
303 325
304int ev_method () 326int ev_method ()
305 CODE:
306 RETVAL = ev_method;
307 OUTPUT:
308 RETVAL
309 327
310NV ev_time () 328NV ev_time ()
311 329
312void ev_init (int flags = 0) 330int ev_init (int methods = EVMETHOD_AUTO)
313 331
314void ev_loop (int flags = 0) 332void ev_loop (int flags = 0)
315 333
316void ev_loop_done (int value = 1) 334void ev_unloop (int how = 1)
317 CODE:
318 ev_loop_done = value;
319 335
320struct ev_io *io (SV *fh, int events, SV *cb) 336struct ev_io *io (SV *fh, int events, SV *cb)
321 ALIAS: 337 ALIAS:
322 io_ns = 1 338 io_ns = 1
323 CODE: 339 CODE:
399 415
400struct ev_child *child (int pid, SV *cb) 416struct ev_child *child (int pid, SV *cb)
401 ALIAS: 417 ALIAS:
402 check_ns = 1 418 check_ns = 1
403 CODE: 419 CODE:
404 RETVAL = e_new (sizeof (struct ev_check), cb); 420 RETVAL = e_new (sizeof (struct ev_child), cb);
405 ev_child_set (RETVAL, pid); 421 ev_child_set (RETVAL, pid);
406 if (!ix) ev_child_start (RETVAL); 422 if (!ix) ev_child_start (RETVAL);
407 OUTPUT: 423 OUTPUT:
408 RETVAL 424 RETVAL
409 425
427 443
428void trigger (struct ev_watcher *w, int revents = EV_NONE) 444void trigger (struct ev_watcher *w, int revents = EV_NONE)
429 CODE: 445 CODE:
430 w->cb (w, revents); 446 w->cb (w, revents);
431 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
432MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 481MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
433 482
434void ev_io_start (struct ev_io *w) 483void ev_io_start (struct ev_io *w)
435 484
436void ev_io_stop (struct ev_io *w) 485void ev_io_stop (struct ev_io *w)
437 486
438void DESTROY (struct ev_io *w) 487void DESTROY (struct ev_io *w)
439 CODE: 488 CODE:
440 ev_io_stop (w); 489 ev_io_stop (w);
490 e_destroy (w);
441 491
442void set (struct ev_io *w, SV *fh, int events) 492void set (struct ev_io *w, SV *fh, int events)
443 CODE: 493 CODE:
444{ 494{
445 int active = w->active; 495 int active = ev_is_active (w);
446 int fd = sv_fileno (fh); 496 int fd = sv_fileno (fh);
447 CHECK_FD (fh, fd); 497 CHECK_FD (fh, fd);
448 498
449 if (active) ev_io_stop (w); 499 if (active) ev_io_stop (w);
450 500
459{ 509{
460 RETVAL = newSVsv (w->fh); 510 RETVAL = newSVsv (w->fh);
461 511
462 if (items > 1) 512 if (items > 1)
463 { 513 {
464 int active = w->active; 514 int active = ev_is_active (w);
465 if (active) ev_io_stop (w); 515 if (active) ev_io_stop (w);
466 516
467 sv_setsv (w->fh, new_fh); 517 sv_setsv (w->fh, new_fh);
468 ev_io_set (w, sv_fileno (w->fh), w->events); 518 ev_io_set (w, sv_fileno (w->fh), w->events);
469 519
471 } 521 }
472} 522}
473 OUTPUT: 523 OUTPUT:
474 RETVAL 524 RETVAL
475 525
476short events (struct ev_io *w, short new_events = EV_UNDEF) 526int events (struct ev_io *w, int new_events = EV_UNDEF)
477 CODE: 527 CODE:
478{ 528{
479 RETVAL = w->events; 529 RETVAL = w->events;
480 530
481 if (items > 1) 531 if (items > 1)
482 { 532 {
483 int active = w->active; 533 int active = ev_is_active (w);
484 if (active) ev_io_stop (w); 534 if (active) ev_io_stop (w);
485 535
486 ev_io_set (w, w->fd, new_events); 536 ev_io_set (w, w->fd, new_events);
487 537
488 if (active) ev_io_start (w); 538 if (active) ev_io_start (w);
498void ev_signal_stop (struct ev_signal *w) 548void ev_signal_stop (struct ev_signal *w)
499 549
500void DESTROY (struct ev_signal *w) 550void DESTROY (struct ev_signal *w)
501 CODE: 551 CODE:
502 ev_signal_stop (w); 552 ev_signal_stop (w);
553 e_destroy (w);
503 554
504void set (struct ev_signal *w, SV *signal = 0) 555void set (struct ev_signal *w, SV *signal)
505 CODE: 556 CODE:
506{ 557{
507 Signal signum = sv_signum (signal); /* may croak here */ 558 Signal signum = sv_signum (signal); /* may croak here */
508 int active = w->active; 559 int active = ev_is_active (w);
509 560
510 if (active) ev_signal_stop (w); 561 if (active) ev_signal_stop (w);
562
511 ev_signal_set (w, signum); 563 ev_signal_set (w, signum);
564
512 if (active) ev_signal_start (w); 565 if (active) ev_signal_start (w);
513} 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
514 586
515MODULE = EV PACKAGE = EV::Time 587MODULE = EV PACKAGE = EV::Time
516 588
517MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 589MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
518 590
527 CHECK_REPEAT (w->repeat); 599 CHECK_REPEAT (w->repeat);
528 600
529void DESTROY (struct ev_timer *w) 601void DESTROY (struct ev_timer *w)
530 CODE: 602 CODE:
531 ev_timer_stop (w); 603 ev_timer_stop (w);
604 e_destroy (w);
532 605
533void set (struct ev_timer *w, NV after, NV repeat = 0.) 606void set (struct ev_timer *w, NV after, NV repeat = 0.)
534 INIT: 607 INIT:
535 CHECK_REPEAT (repeat); 608 CHECK_REPEAT (repeat);
536 CODE: 609 CODE:
537{ 610{
538 int active = w->active; 611 int active = ev_is_active (w);
539 if (active) ev_timer_stop (w); 612 if (active) ev_timer_stop (w);
540 ev_timer_set (w, after, repeat); 613 ev_timer_set (w, after, repeat);
541 if (active) ev_timer_start (w); 614 if (active) ev_timer_start (w);
542} 615}
543 616
550void ev_periodic_stop (struct ev_periodic *w) 623void ev_periodic_stop (struct ev_periodic *w)
551 624
552void DESTROY (struct ev_periodic *w) 625void DESTROY (struct ev_periodic *w)
553 CODE: 626 CODE:
554 ev_periodic_stop (w); 627 ev_periodic_stop (w);
628 e_destroy (w);
555 629
556void set (struct ev_periodic *w, NV at, NV interval = 0.) 630void set (struct ev_periodic *w, NV at, NV interval = 0.)
557 INIT: 631 INIT:
558 CHECK_REPEAT (interval); 632 CHECK_REPEAT (interval);
559 CODE: 633 CODE:
560{ 634{
561 int active = w->active; 635 int active = ev_is_active (w);
562 if (active) ev_periodic_stop (w); 636 if (active) ev_periodic_stop (w);
637
563 ev_periodic_set (w, at, interval); 638 ev_periodic_set (w, at, interval);
639
564 if (active) ev_periodic_start (w); 640 if (active) ev_periodic_start (w);
565} 641}
566 642
567MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 643MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
568 644
571void ev_idle_stop (struct ev_idle *w) 647void ev_idle_stop (struct ev_idle *w)
572 648
573void DESTROY (struct ev_idle *w) 649void DESTROY (struct ev_idle *w)
574 CODE: 650 CODE:
575 ev_idle_stop (w); 651 ev_idle_stop (w);
652 e_destroy (w);
576 653
577MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_ 654MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_
578 655
579void ev_prepare_start (struct ev_prepare *w) 656void ev_prepare_start (struct ev_prepare *w)
580 657
581void ev_prepare_stop (struct ev_prepare *w) 658void ev_prepare_stop (struct ev_prepare *w)
582 659
583void DESTROY (struct ev_prepare *w) 660void DESTROY (struct ev_prepare *w)
584 CODE: 661 CODE:
585 ev_prepare_stop (w); 662 ev_prepare_stop (w);
663 e_destroy (w);
586 664
587MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_ 665MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_
588 666
589void ev_check_start (struct ev_check *w) 667void ev_check_start (struct ev_check *w)
590 668
591void ev_check_stop (struct ev_check *w) 669void ev_check_stop (struct ev_check *w)
592 670
593void DESTROY (struct ev_check *w) 671void DESTROY (struct ev_check *w)
594 CODE: 672 CODE:
595 ev_check_stop (w); 673 ev_check_stop (w);
674 e_destroy (w);
596 675
597MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ 676MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_
598 677
599void ev_child_start (struct ev_child *w) 678void ev_child_start (struct ev_child *w)
600 679
601void ev_child_stop (struct ev_child *w) 680void ev_child_stop (struct ev_child *w)
602 681
603void DESTROY (struct ev_child *w) 682void DESTROY (struct ev_child *w)
604 CODE: 683 CODE:
605 ev_child_stop (w); 684 ev_child_stop (w);
685 e_destroy (w);
606 686
607void set (struct ev_child *w, int pid) 687void set (struct ev_child *w, int pid)
608 CODE: 688 CODE:
609{ 689{
610 int active = w->active; 690 int active = ev_is_active (w);
611 if (active) ev_child_stop (w); 691 if (active) ev_child_stop (w);
692
612 ev_child_set (w, pid); 693 ev_child_set (w, pid);
694
613 if (active) ev_child_start (w); 695 if (active) ev_child_start (w);
614} 696}
615 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
616int status (struct ev_child *w) 717int rstatus (struct ev_child *w)
718 ALIAS:
719 rpid = 1
617 CODE: 720 CODE:
618 RETVAL = w->status; 721 RETVAL = ix ? w->rpid : w->rstatus;
619 OUTPUT: 722 OUTPUT:
620 RETVAL 723 RETVAL
621
622#if 0
623 724
624MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 725MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
625 726
626BOOT: 727BOOT:
627{ 728{
710 811
711void evdns_search_add (char *domain) 812void evdns_search_add (char *domain)
712 813
713void evdns_search_ndots_set (int ndots) 814void evdns_search_ndots_set (int ndots)
714 815
816#if 0
715 817
716MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 818MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
717 819
718BOOT: 820BOOT:
719{ 821{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines