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.57 by root, Tue Nov 6 17:20:42 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 = (struct ev_watcher *)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{
123 PUSHMARK (SP); 156 PUSHMARK (SP);
124 EXTEND (SP, 2); 157 EXTEND (SP, 2);
125 PUSHs (sv_self); 158 PUSHs (sv_self);
126 PUSHs (sv_events); 159 PUSHs (sv_events);
127 160
128 if (revents & EV_CHILD)
129 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
130
131 PUTBACK; 161 PUTBACK;
132 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 162 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
133 SP = PL_stack_base + mark; PUTBACK; 163 SP = PL_stack_base + mark; PUTBACK;
134 164
135 SvREFCNT_dec (sv_self); 165 SvREFCNT_dec (sv_self);
147 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);
148 SP = PL_stack_base + mark; PUTBACK; 178 SP = PL_stack_base + mark; PUTBACK;
149 } 179 }
150} 180}
151 181
152#if 0
153///////////////////////////////////////////////////////////////////////////// 182/////////////////////////////////////////////////////////////////////////////
154// DNS 183// DNS
155 184
185#ifndef WIN32
156static void 186static void
157dns_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)
158{ 188{
159 dSP; 189 dSP;
160 SV *cb = (SV *)arg; 190 SV *cb = (SV *)arg;
216 246
217PROTOTYPES: ENABLE 247PROTOTYPES: ENABLE
218 248
219BOOT: 249BOOT:
220{ 250{
221 int i;
222 HV *stash = gv_stashpv ("EV", 1); 251 HV *stash = gv_stashpv ("EV", 1);
223 252
224 static const struct { 253 static const struct {
225 const char *name; 254 const char *name;
226 IV iv; 255 IV iv;
227 } *civ, const_iv[] = { 256 } *civ, const_iv[] = {
228# 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
229 const_iv (EV_, UNDEF) 261 const_iv (EV_, UNDEF)
230 const_iv (EV_, NONE) 262 const_iv (EV_, NONE)
231 const_iv (EV_, TIMEOUT) 263 const_iv (EV_, TIMEOUT)
232 const_iv (EV_, READ) 264 const_iv (EV_, READ)
233 const_iv (EV_, WRITE) 265 const_iv (EV_, WRITE)
237 const_iv (EV_, ERROR) 269 const_iv (EV_, ERROR)
238 270
239 const_iv (EV, LOOP_ONESHOT) 271 const_iv (EV, LOOP_ONESHOT)
240 const_iv (EV, LOOP_NONBLOCK) 272 const_iv (EV, LOOP_NONBLOCK)
241 273
242 const_iv (EV, METHOD_NONE) 274 const_iv (EV, METHOD_AUTO)
243 const_iv (EV, METHOD_SELECT) 275 const_iv (EV, METHOD_SELECT)
276 const_iv (EV, METHOD_POLL)
244 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)
245 }; 282 };
246 283
247 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; )
248 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 285 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
249 286
264 /* the poor man's shared library emulator */ 301 /* the poor man's shared library emulator */
265 evapi.ver = EV_API_VERSION; 302 evapi.ver = EV_API_VERSION;
266 evapi.rev = EV_API_REVISION; 303 evapi.rev = EV_API_REVISION;
267 evapi.sv_fileno = sv_fileno; 304 evapi.sv_fileno = sv_fileno;
268 evapi.sv_signum = sv_signum; 305 evapi.sv_signum = sv_signum;
269 evapi.now = &ev_now; 306 evapi.now = ev_now;
270 evapi.method = &ev_method; 307 evapi.method = ev_method;
271 evapi.loop_done = &ev_loop_done; 308 evapi.unloop = ev_unloop;
272 evapi.time = ev_time; 309 evapi.time = ev_time;
273 evapi.loop = ev_loop; 310 evapi.loop = ev_loop;
274 evapi.once = ev_once; 311 evapi.once = ev_once;
275 evapi.io_start = ev_io_start; 312 evapi.io_start = ev_io_start;
276 evapi.io_stop = ev_io_stop; 313 evapi.io_stop = ev_io_stop;
291 evapi.child_stop = ev_child_stop; 328 evapi.child_stop = ev_child_stop;
292 329
293 sv_setiv (sv, (IV)&evapi); 330 sv_setiv (sv, (IV)&evapi);
294 SvREADONLY_on (sv); 331 SvREADONLY_on (sv);
295 } 332 }
333#ifndef WIN32
334 pthread_atfork (0, 0, ev_default_fork);
335#endif
296} 336}
297 337
298NV ev_now () 338NV ev_now ()
299 CODE:
300 RETVAL = ev_now;
301 OUTPUT:
302 RETVAL
303 339
304int ev_method () 340int ev_method ()
305 CODE:
306 RETVAL = ev_method;
307 OUTPUT:
308 RETVAL
309 341
310NV ev_time () 342NV ev_time ()
311 343
312void ev_init (int flags = 0) 344int ev_default_loop (int methods = EVMETHOD_AUTO)
313 345
314void ev_loop (int flags = 0) 346void ev_loop (int flags = 0)
315 347
316void ev_loop_done (int value = 1) 348void ev_unloop (int how = 1)
317 CODE:
318 ev_loop_done = value;
319 349
320struct ev_io *io (SV *fh, int events, SV *cb) 350struct ev_io *io (SV *fh, int events, SV *cb)
321 ALIAS: 351 ALIAS:
322 io_ns = 1 352 io_ns = 1
323 CODE: 353 CODE:
399 429
400struct ev_child *child (int pid, SV *cb) 430struct ev_child *child (int pid, SV *cb)
401 ALIAS: 431 ALIAS:
402 check_ns = 1 432 check_ns = 1
403 CODE: 433 CODE:
404 RETVAL = e_new (sizeof (struct ev_check), cb); 434 RETVAL = e_new (sizeof (struct ev_child), cb);
405 ev_child_set (RETVAL, pid); 435 ev_child_set (RETVAL, pid);
406 if (!ix) ev_child_start (RETVAL); 436 if (!ix) ev_child_start (RETVAL);
407 OUTPUT: 437 OUTPUT:
408 RETVAL 438 RETVAL
409 439
427 457
428void trigger (struct ev_watcher *w, int revents = EV_NONE) 458void trigger (struct ev_watcher *w, int revents = EV_NONE)
429 CODE: 459 CODE:
430 w->cb (w, revents); 460 w->cb (w, revents);
431 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
432MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 495MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
433 496
434void ev_io_start (struct ev_io *w) 497void ev_io_start (struct ev_io *w)
435 498
436void ev_io_stop (struct ev_io *w) 499void ev_io_stop (struct ev_io *w)
437 500
438void DESTROY (struct ev_io *w) 501void DESTROY (struct ev_io *w)
439 CODE: 502 CODE:
440 ev_io_stop (w); 503 ev_io_stop (w);
504 e_destroy (w);
441 505
442void set (struct ev_io *w, SV *fh, int events) 506void set (struct ev_io *w, SV *fh, int events)
443 CODE: 507 CODE:
444{ 508{
445 int active = w->active; 509 int active = ev_is_active (w);
446 int fd = sv_fileno (fh); 510 int fd = sv_fileno (fh);
447 CHECK_FD (fh, fd); 511 CHECK_FD (fh, fd);
448 512
449 if (active) ev_io_stop (w); 513 if (active) ev_io_stop (w);
450 514
459{ 523{
460 RETVAL = newSVsv (w->fh); 524 RETVAL = newSVsv (w->fh);
461 525
462 if (items > 1) 526 if (items > 1)
463 { 527 {
464 int active = w->active; 528 int active = ev_is_active (w);
465 if (active) ev_io_stop (w); 529 if (active) ev_io_stop (w);
466 530
467 sv_setsv (w->fh, new_fh); 531 sv_setsv (w->fh, new_fh);
468 ev_io_set (w, sv_fileno (w->fh), w->events); 532 ev_io_set (w, sv_fileno (w->fh), w->events);
469 533
471 } 535 }
472} 536}
473 OUTPUT: 537 OUTPUT:
474 RETVAL 538 RETVAL
475 539
476short events (struct ev_io *w, short new_events = EV_UNDEF) 540int events (struct ev_io *w, int new_events = EV_UNDEF)
477 CODE: 541 CODE:
478{ 542{
479 RETVAL = w->events; 543 RETVAL = w->events;
480 544
481 if (items > 1) 545 if (items > 1)
482 { 546 {
483 int active = w->active; 547 int active = ev_is_active (w);
484 if (active) ev_io_stop (w); 548 if (active) ev_io_stop (w);
485 549
486 ev_io_set (w, w->fd, new_events); 550 ev_io_set (w, w->fd, new_events);
487 551
488 if (active) ev_io_start (w); 552 if (active) ev_io_start (w);
498void ev_signal_stop (struct ev_signal *w) 562void ev_signal_stop (struct ev_signal *w)
499 563
500void DESTROY (struct ev_signal *w) 564void DESTROY (struct ev_signal *w)
501 CODE: 565 CODE:
502 ev_signal_stop (w); 566 ev_signal_stop (w);
567 e_destroy (w);
503 568
504void set (struct ev_signal *w, SV *signal = 0) 569void set (struct ev_signal *w, SV *signal)
505 CODE: 570 CODE:
506{ 571{
507 Signal signum = sv_signum (signal); /* may croak here */ 572 Signal signum = sv_signum (signal); /* may croak here */
508 int active = w->active; 573 int active = ev_is_active (w);
509 574
510 if (active) ev_signal_stop (w); 575 if (active) ev_signal_stop (w);
576
511 ev_signal_set (w, signum); 577 ev_signal_set (w, signum);
578
512 if (active) ev_signal_start (w); 579 if (active) ev_signal_start (w);
513} 580}
514 581
515MODULE = 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
516 600
517MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 601MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
518 602
519void ev_timer_start (struct ev_timer *w) 603void ev_timer_start (struct ev_timer *w)
520 INIT: 604 INIT:
527 CHECK_REPEAT (w->repeat); 611 CHECK_REPEAT (w->repeat);
528 612
529void DESTROY (struct ev_timer *w) 613void DESTROY (struct ev_timer *w)
530 CODE: 614 CODE:
531 ev_timer_stop (w); 615 ev_timer_stop (w);
616 e_destroy (w);
532 617
533void set (struct ev_timer *w, NV after, NV repeat = 0.) 618void set (struct ev_timer *w, NV after, NV repeat = 0.)
534 INIT: 619 INIT:
535 CHECK_REPEAT (repeat); 620 CHECK_REPEAT (repeat);
536 CODE: 621 CODE:
537{ 622{
538 int active = w->active; 623 int active = ev_is_active (w);
539 if (active) ev_timer_stop (w); 624 if (active) ev_timer_stop (w);
540 ev_timer_set (w, after, repeat); 625 ev_timer_set (w, after, repeat);
541 if (active) ev_timer_start (w); 626 if (active) ev_timer_start (w);
542} 627}
543 628
550void ev_periodic_stop (struct ev_periodic *w) 635void ev_periodic_stop (struct ev_periodic *w)
551 636
552void DESTROY (struct ev_periodic *w) 637void DESTROY (struct ev_periodic *w)
553 CODE: 638 CODE:
554 ev_periodic_stop (w); 639 ev_periodic_stop (w);
640 e_destroy (w);
555 641
556void set (struct ev_periodic *w, NV at, NV interval = 0.) 642void set (struct ev_periodic *w, NV at, NV interval = 0.)
557 INIT: 643 INIT:
558 CHECK_REPEAT (interval); 644 CHECK_REPEAT (interval);
559 CODE: 645 CODE:
560{ 646{
561 int active = w->active; 647 int active = ev_is_active (w);
562 if (active) ev_periodic_stop (w); 648 if (active) ev_periodic_stop (w);
649
563 ev_periodic_set (w, at, interval); 650 ev_periodic_set (w, at, interval);
651
564 if (active) ev_periodic_start (w); 652 if (active) ev_periodic_start (w);
565} 653}
566 654
567MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 655MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
568 656
571void ev_idle_stop (struct ev_idle *w) 659void ev_idle_stop (struct ev_idle *w)
572 660
573void DESTROY (struct ev_idle *w) 661void DESTROY (struct ev_idle *w)
574 CODE: 662 CODE:
575 ev_idle_stop (w); 663 ev_idle_stop (w);
664 e_destroy (w);
576 665
577MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_ 666MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_
578 667
579void ev_prepare_start (struct ev_prepare *w) 668void ev_prepare_start (struct ev_prepare *w)
580 669
581void ev_prepare_stop (struct ev_prepare *w) 670void ev_prepare_stop (struct ev_prepare *w)
582 671
583void DESTROY (struct ev_prepare *w) 672void DESTROY (struct ev_prepare *w)
584 CODE: 673 CODE:
585 ev_prepare_stop (w); 674 ev_prepare_stop (w);
675 e_destroy (w);
586 676
587MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_ 677MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_
588 678
589void ev_check_start (struct ev_check *w) 679void ev_check_start (struct ev_check *w)
590 680
591void ev_check_stop (struct ev_check *w) 681void ev_check_stop (struct ev_check *w)
592 682
593void DESTROY (struct ev_check *w) 683void DESTROY (struct ev_check *w)
594 CODE: 684 CODE:
595 ev_check_stop (w); 685 ev_check_stop (w);
686 e_destroy (w);
596 687
597MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_ 688MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_
598 689
599void ev_child_start (struct ev_child *w) 690void ev_child_start (struct ev_child *w)
600 691
601void ev_child_stop (struct ev_child *w) 692void ev_child_stop (struct ev_child *w)
602 693
603void DESTROY (struct ev_child *w) 694void DESTROY (struct ev_child *w)
604 CODE: 695 CODE:
605 ev_child_stop (w); 696 ev_child_stop (w);
697 e_destroy (w);
606 698
607void set (struct ev_child *w, int pid) 699void set (struct ev_child *w, int pid)
608 CODE: 700 CODE:
609{ 701{
610 int active = w->active; 702 int active = ev_is_active (w);
611 if (active) ev_child_stop (w); 703 if (active) ev_child_stop (w);
704
612 ev_child_set (w, pid); 705 ev_child_set (w, pid);
706
613 if (active) ev_child_start (w); 707 if (active) ev_child_start (w);
614} 708}
615 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
616int status (struct ev_child *w) 729int rstatus (struct ev_child *w)
730 ALIAS:
731 rpid = 1
617 CODE: 732 CODE:
618 RETVAL = w->status; 733 RETVAL = ix ? w->rpid : w->rstatus;
619 OUTPUT: 734 OUTPUT:
620 RETVAL 735 RETVAL
621 736
622#if 0 737#ifndef WIN32
623 738
624MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 739MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
625 740
626BOOT: 741BOOT:
627{ 742{
710 825
711void evdns_search_add (char *domain) 826void evdns_search_add (char *domain)
712 827
713void evdns_search_ndots_set (int ndots) 828void evdns_search_ndots_set (int ndots)
714 829
830#if 0
715 831
716MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 832MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
717 833
718BOOT: 834BOOT:
719{ 835{
751 867
752#void DESTROY (struct evhttp_request *req); 868#void DESTROY (struct evhttp_request *req);
753 869
754#endif 870#endif
755 871
872#endif
756 873
757 874
758 875
759 876
760 877

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines