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.42 by root, Fri Nov 2 22:03:00 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#include "libev/evdns.c"
14 14
15typedef int Signal; 15typedef int Signal;
16 16
17static struct EVAPI evapi; 17static struct EVAPI evapi;
18 18
83 w->self = self; 83 w->self = self;
84 84
85 return (void *)w; 85 return (void *)w;
86} 86}
87 87
88static void *
89e_destroy (void *w_)
90{
91 struct ev_watcher *w = w_;
92
93 SvREFCNT_dec (w->fh ); w->fh = 0;
94 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0;
95}
96
88static SV * 97static SV *
89e_bless (struct ev_watcher *w, HV *stash) 98e_bless (struct ev_watcher *w, HV *stash)
90{ 99{
91 SV *rv; 100 SV *rv;
92 101
108 dSP; 117 dSP;
109 I32 mark = SP - PL_stack_base; 118 I32 mark = SP - PL_stack_base;
110 SV *sv_self, *sv_events, *sv_status = 0; 119 SV *sv_self, *sv_events, *sv_status = 0;
111 static SV *sv_events_cache; 120 static SV *sv_events_cache;
112 121
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 */ 122 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */
116 123
117 if (sv_events_cache) 124 if (sv_events_cache)
118 { 125 {
119 sv_events = sv_events_cache; sv_events_cache = 0; 126 sv_events = sv_events_cache; sv_events_cache = 0;
149 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 156 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
150 SP = PL_stack_base + mark; PUTBACK; 157 SP = PL_stack_base + mark; PUTBACK;
151 } 158 }
152} 159}
153 160
154#if 0
155///////////////////////////////////////////////////////////////////////////// 161/////////////////////////////////////////////////////////////////////////////
156// DNS 162// DNS
157 163
158static void 164static void
159dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) 165dns_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); 207 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
202 } 208 }
203 209
204 LEAVE; 210 LEAVE;
205} 211}
206#endif
207 212
208#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 213#define CHECK_REPEAT(repeat) if (repeat < 0.) \
209 croak (# repeat " value must be >= 0"); 214 croak (# repeat " value must be >= 0");
210 215
211#define CHECK_FD(fh,fd) if ((fd) < 0) \ 216#define CHECK_FD(fh,fd) if ((fd) < 0) \
226 static const struct { 231 static const struct {
227 const char *name; 232 const char *name;
228 IV iv; 233 IV iv;
229 } *civ, const_iv[] = { 234 } *civ, const_iv[] = {
230# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 235# define const_iv(pfx, name) { # name, (IV) pfx ## name },
236 const_iv (EV_, MINPRI)
237 const_iv (EV_, MAXPRI)
238
231 const_iv (EV_, UNDEF) 239 const_iv (EV_, UNDEF)
232 const_iv (EV_, NONE) 240 const_iv (EV_, NONE)
233 const_iv (EV_, TIMEOUT) 241 const_iv (EV_, TIMEOUT)
234 const_iv (EV_, READ) 242 const_iv (EV_, READ)
235 const_iv (EV_, WRITE) 243 const_iv (EV_, WRITE)
239 const_iv (EV_, ERROR) 247 const_iv (EV_, ERROR)
240 248
241 const_iv (EV, LOOP_ONESHOT) 249 const_iv (EV, LOOP_ONESHOT)
242 const_iv (EV, LOOP_NONBLOCK) 250 const_iv (EV, LOOP_NONBLOCK)
243 251
244 const_iv (EV, METHOD_NONE) 252 const_iv (EV, METHOD_AUTO)
245 const_iv (EV, METHOD_SELECT) 253 const_iv (EV, METHOD_SELECT)
254 const_iv (EV, METHOD_POLL)
246 const_iv (EV, METHOD_EPOLL) 255 const_iv (EV, METHOD_EPOLL)
256 const_iv (EV, METHOD_KQUEUE)
257 const_iv (EV, METHOD_DEVPOLL)
258 const_iv (EV, METHOD_PORT)
259 const_iv (EV, METHOD_ANY)
247 }; 260 };
248 261
249 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 262 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
250 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 263 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
251 264
293 evapi.child_stop = ev_child_stop; 306 evapi.child_stop = ev_child_stop;
294 307
295 sv_setiv (sv, (IV)&evapi); 308 sv_setiv (sv, (IV)&evapi);
296 SvREADONLY_on (sv); 309 SvREADONLY_on (sv);
297 } 310 }
311
312 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child);
298} 313}
299 314
300NV ev_now () 315NV ev_now ()
301 CODE: 316 CODE:
302 RETVAL = ev_now; 317 RETVAL = ev_now;
309 OUTPUT: 324 OUTPUT:
310 RETVAL 325 RETVAL
311 326
312NV ev_time () 327NV ev_time ()
313 328
314void ev_init (int flags = 0) 329void ev_init (int methods = EVMETHOD_AUTO)
315 330
316void ev_loop (int flags = 0) 331void ev_loop (int flags = 0)
317 332
318void ev_loop_done (int value = 1) 333void ev_loop_done (int value = 1)
319 CODE: 334 CODE:
429 444
430void trigger (struct ev_watcher *w, int revents = EV_NONE) 445void trigger (struct ev_watcher *w, int revents = EV_NONE)
431 CODE: 446 CODE:
432 w->cb (w, revents); 447 w->cb (w, revents);
433 448
449int priority (struct ev_watcher *w, int new_priority = 0)
450 CODE:
451{
452 RETVAL = w->priority;
453
454 if (items > 1)
455 {
456 int active = ev_is_active (w);
457
458 if (new_priority < EV_MINPRI || new_priority > EV_MAXPRI)
459 croak ("watcher priority out of range, value must be between %d and %d, inclusive", EV_MINPRI, EV_MAXPRI);
460
461 if (active)
462 {
463 /* grrr. */
464 PUSHMARK (SP);
465 XPUSHs (ST (0));
466 call_method ("stop", G_DISCARD | G_VOID);
467 }
468
469 ev_set_priority (w, new_priority);
470
471 if (active)
472 {
473 PUSHMARK (SP);
474 XPUSHs (ST (0));
475 call_method ("start", G_DISCARD | G_VOID);
476 }
477 }
478}
479 OUTPUT:
480 RETVAL
481
434MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 482MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
435 483
436void ev_io_start (struct ev_io *w) 484void ev_io_start (struct ev_io *w)
437 485
438void ev_io_stop (struct ev_io *w) 486void ev_io_stop (struct ev_io *w)
439 487
440void DESTROY (struct ev_io *w) 488void DESTROY (struct ev_io *w)
441 CODE: 489 CODE:
442 ev_io_stop (w); 490 ev_io_stop (w);
491 e_destroy (w);
443 492
444void set (struct ev_io *w, SV *fh, int events) 493void set (struct ev_io *w, SV *fh, int events)
445 CODE: 494 CODE:
446{ 495{
447 int active = w->active; 496 int active = ev_is_active (w);
448 int fd = sv_fileno (fh); 497 int fd = sv_fileno (fh);
449 CHECK_FD (fh, fd); 498 CHECK_FD (fh, fd);
450 499
451 if (active) ev_io_stop (w); 500 if (active) ev_io_stop (w);
452 501
461{ 510{
462 RETVAL = newSVsv (w->fh); 511 RETVAL = newSVsv (w->fh);
463 512
464 if (items > 1) 513 if (items > 1)
465 { 514 {
466 int active = w->active; 515 int active = ev_is_active (w);
467 if (active) ev_io_stop (w); 516 if (active) ev_io_stop (w);
468 517
469 sv_setsv (w->fh, new_fh); 518 sv_setsv (w->fh, new_fh);
470 ev_io_set (w, sv_fileno (w->fh), w->events); 519 ev_io_set (w, sv_fileno (w->fh), w->events);
471 520
473 } 522 }
474} 523}
475 OUTPUT: 524 OUTPUT:
476 RETVAL 525 RETVAL
477 526
478short events (struct ev_io *w, short new_events = EV_UNDEF) 527int events (struct ev_io *w, int new_events = EV_UNDEF)
479 CODE: 528 CODE:
480{ 529{
481 RETVAL = w->events; 530 RETVAL = w->events;
482 531
483 if (items > 1) 532 if (items > 1)
484 { 533 {
485 int active = w->active; 534 int active = ev_is_active (w);
486 if (active) ev_io_stop (w); 535 if (active) ev_io_stop (w);
487 536
488 ev_io_set (w, w->fd, new_events); 537 ev_io_set (w, w->fd, new_events);
489 538
490 if (active) ev_io_start (w); 539 if (active) ev_io_start (w);
500void ev_signal_stop (struct ev_signal *w) 549void ev_signal_stop (struct ev_signal *w)
501 550
502void DESTROY (struct ev_signal *w) 551void DESTROY (struct ev_signal *w)
503 CODE: 552 CODE:
504 ev_signal_stop (w); 553 ev_signal_stop (w);
554 e_destroy (w);
505 555
506void set (struct ev_signal *w, SV *signal = 0) 556void set (struct ev_signal *w, SV *signal)
507 CODE: 557 CODE:
508{ 558{
509 Signal signum = sv_signum (signal); /* may croak here */ 559 Signal signum = sv_signum (signal); /* may croak here */
510 int active = w->active; 560 int active = ev_is_active (w);
511 561
512 if (active) ev_signal_stop (w); 562 if (active) ev_signal_stop (w);
563
513 ev_signal_set (w, signum); 564 ev_signal_set (w, signum);
565
514 if (active) ev_signal_start (w); 566 if (active) ev_signal_start (w);
515} 567}
568
569int signal (struct ev_signal *w, SV *new_signal = 0)
570 CODE:
571{
572 RETVAL = w->signum;
573
574 if (items > 1)
575 {
576 Signal signum = sv_signum (new_signal); /* may croak here */
577 int active = ev_is_active (w);
578 if (active) ev_signal_stop (w);
579
580 ev_signal_set (w, signum);
581
582 if (active) ev_signal_start (w);
583 }
584}
585 OUTPUT:
586 RETVAL
516 587
517MODULE = EV PACKAGE = EV::Time 588MODULE = EV PACKAGE = EV::Time
518 589
519MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 590MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
520 591
529 CHECK_REPEAT (w->repeat); 600 CHECK_REPEAT (w->repeat);
530 601
531void DESTROY (struct ev_timer *w) 602void DESTROY (struct ev_timer *w)
532 CODE: 603 CODE:
533 ev_timer_stop (w); 604 ev_timer_stop (w);
605 e_destroy (w);
534 606
535void set (struct ev_timer *w, NV after, NV repeat = 0.) 607void set (struct ev_timer *w, NV after, NV repeat = 0.)
536 INIT: 608 INIT:
537 CHECK_REPEAT (repeat); 609 CHECK_REPEAT (repeat);
538 CODE: 610 CODE:
539{ 611{
540 int active = w->active; 612 int active = ev_is_active (w);
541 if (active) ev_timer_stop (w); 613 if (active) ev_timer_stop (w);
542 ev_timer_set (w, after, repeat); 614 ev_timer_set (w, after, repeat);
543 if (active) ev_timer_start (w); 615 if (active) ev_timer_start (w);
544} 616}
545 617
552void ev_periodic_stop (struct ev_periodic *w) 624void ev_periodic_stop (struct ev_periodic *w)
553 625
554void DESTROY (struct ev_periodic *w) 626void DESTROY (struct ev_periodic *w)
555 CODE: 627 CODE:
556 ev_periodic_stop (w); 628 ev_periodic_stop (w);
629 e_destroy (w);
557 630
558void set (struct ev_periodic *w, NV at, NV interval = 0.) 631void set (struct ev_periodic *w, NV at, NV interval = 0.)
559 INIT: 632 INIT:
560 CHECK_REPEAT (interval); 633 CHECK_REPEAT (interval);
561 CODE: 634 CODE:
562{ 635{
563 int active = w->active; 636 int active = ev_is_active (w);
564 if (active) ev_periodic_stop (w); 637 if (active) ev_periodic_stop (w);
638
565 ev_periodic_set (w, at, interval); 639 ev_periodic_set (w, at, interval);
640
566 if (active) ev_periodic_start (w); 641 if (active) ev_periodic_start (w);
567} 642}
568 643
569MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 644MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
570 645
573void ev_idle_stop (struct ev_idle *w) 648void ev_idle_stop (struct ev_idle *w)
574 649
575void DESTROY (struct ev_idle *w) 650void DESTROY (struct ev_idle *w)
576 CODE: 651 CODE:
577 ev_idle_stop (w); 652 ev_idle_stop (w);
653 e_destroy (w);
578 654
579MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_ 655MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_
580 656
581void ev_prepare_start (struct ev_prepare *w) 657void ev_prepare_start (struct ev_prepare *w)
582 658
583void ev_prepare_stop (struct ev_prepare *w) 659void ev_prepare_stop (struct ev_prepare *w)
584 660
585void DESTROY (struct ev_prepare *w) 661void DESTROY (struct ev_prepare *w)
586 CODE: 662 CODE:
587 ev_prepare_stop (w); 663 ev_prepare_stop (w);
664 e_destroy (w);
588 665
589MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_ 666MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_
590 667
591void ev_check_start (struct ev_check *w) 668void ev_check_start (struct ev_check *w)
592 669
593void ev_check_stop (struct ev_check *w) 670void ev_check_stop (struct ev_check *w)
594 671
595void DESTROY (struct ev_check *w) 672void DESTROY (struct ev_check *w)
596 CODE: 673 CODE:
597 ev_check_stop (w); 674 ev_check_stop (w);
675 e_destroy (w);
598 676
599MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ 677MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_
600 678
601void ev_child_start (struct ev_child *w) 679void ev_child_start (struct ev_child *w)
602 680
603void ev_child_stop (struct ev_child *w) 681void ev_child_stop (struct ev_child *w)
604 682
605void DESTROY (struct ev_child *w) 683void DESTROY (struct ev_child *w)
606 CODE: 684 CODE:
607 ev_child_stop (w); 685 ev_child_stop (w);
686 e_destroy (w);
608 687
609void set (struct ev_child *w, int pid) 688void set (struct ev_child *w, int pid)
610 CODE: 689 CODE:
611{ 690{
612 int active = w->active; 691 int active = ev_is_active (w);
613 if (active) ev_child_stop (w); 692 if (active) ev_child_stop (w);
693
614 ev_child_set (w, pid); 694 ev_child_set (w, pid);
695
615 if (active) ev_child_start (w); 696 if (active) ev_child_start (w);
616} 697}
617 698
699int pid (struct ev_child *w, int new_pid = 0)
700 CODE:
701{
702 RETVAL = w->pid;
703
704 if (items > 1)
705 {
706 int active = ev_is_active (w);
707 if (active) ev_child_stop (w);
708
709 ev_child_set (w, new_pid);
710
711 if (active) ev_child_start (w);
712 }
713}
714 OUTPUT:
715 RETVAL
716
717
618int status (struct ev_child *w) 718int status (struct ev_child *w)
619 CODE: 719 CODE:
620 RETVAL = w->status; 720 RETVAL = w->status;
621 OUTPUT: 721 OUTPUT:
622 RETVAL 722 RETVAL
623
624#if 0
625 723
626MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 724MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
627 725
628BOOT: 726BOOT:
629{ 727{
712 810
713void evdns_search_add (char *domain) 811void evdns_search_add (char *domain)
714 812
715void evdns_search_ndots_set (int ndots) 813void evdns_search_ndots_set (int ndots)
716 814
815#if 0
717 816
718MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 817MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
719 818
720BOOT: 819BOOT:
721{ 820{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines