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

Comparing EV/EV.xs (file contents):
Revision 1.35 by root, Thu Nov 1 13:33:12 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
9#define EV_PROTOTYPES 1 7#define EV_PROTOTYPES 1
10#include "EV/EVAPI.h" 8#include "EV/EVAPI.h"
11 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 */
12#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
13 32
14typedef int Signal; 33typedef int Signal;
15 34
16static struct EVAPI evapi; 35static struct EVAPI evapi;
17 36
24 *stash_idle, 43 *stash_idle,
25 *stash_prepare, 44 *stash_prepare,
26 *stash_check, 45 *stash_check,
27 *stash_child; 46 *stash_child;
28 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
29static int 54static int
30sv_signum (SV *sig) 55sv_signum (SV *sig)
31{ 56{
32 int signum; 57 int signum;
33 58
82 w->self = self; 107 w->self = self;
83 108
84 return (void *)w; 109 return (void *)w;
85} 110}
86 111
87static void * 112static void
88e_destroy (void *w_) 113e_destroy (void *w_)
89{ 114{
90 struct ev_watcher *w = w_; 115 struct ev_watcher *w = w_;
91 116
92 SvREFCNT_dec (w->fh ); w->fh = 0; 117 SvREFCNT_dec (w->fh ); w->fh = 0;
131 PUSHMARK (SP); 156 PUSHMARK (SP);
132 EXTEND (SP, 2); 157 EXTEND (SP, 2);
133 PUSHs (sv_self); 158 PUSHs (sv_self);
134 PUSHs (sv_events); 159 PUSHs (sv_events);
135 160
136 if (revents & EV_CHILD)
137 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
138
139 PUTBACK; 161 PUTBACK;
140 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 162 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
141 SP = PL_stack_base + mark; PUTBACK; 163 SP = PL_stack_base + mark; PUTBACK;
142 164
143 SvREFCNT_dec (sv_self); 165 SvREFCNT_dec (sv_self);
155 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);
156 SP = PL_stack_base + mark; PUTBACK; 178 SP = PL_stack_base + mark; PUTBACK;
157 } 179 }
158} 180}
159 181
160#if 0
161///////////////////////////////////////////////////////////////////////////// 182/////////////////////////////////////////////////////////////////////////////
162// DNS 183// DNS
163 184
185#ifndef WIN32
164static void 186static void
165dns_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)
166{ 188{
167 dSP; 189 dSP;
168 SV *cb = (SV *)arg; 190 SV *cb = (SV *)arg;
224 246
225PROTOTYPES: ENABLE 247PROTOTYPES: ENABLE
226 248
227BOOT: 249BOOT:
228{ 250{
229 int i;
230 HV *stash = gv_stashpv ("EV", 1); 251 HV *stash = gv_stashpv ("EV", 1);
231 252
232 static const struct { 253 static const struct {
233 const char *name; 254 const char *name;
234 IV iv; 255 IV iv;
235 } *civ, const_iv[] = { 256 } *civ, const_iv[] = {
236# 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
237 const_iv (EV_, UNDEF) 261 const_iv (EV_, UNDEF)
238 const_iv (EV_, NONE) 262 const_iv (EV_, NONE)
239 const_iv (EV_, TIMEOUT) 263 const_iv (EV_, TIMEOUT)
240 const_iv (EV_, READ) 264 const_iv (EV_, READ)
241 const_iv (EV_, WRITE) 265 const_iv (EV_, WRITE)
245 const_iv (EV_, ERROR) 269 const_iv (EV_, ERROR)
246 270
247 const_iv (EV, LOOP_ONESHOT) 271 const_iv (EV, LOOP_ONESHOT)
248 const_iv (EV, LOOP_NONBLOCK) 272 const_iv (EV, LOOP_NONBLOCK)
249 273
250 const_iv (EV, METHOD_NONE) 274 const_iv (EV, METHOD_AUTO)
251 const_iv (EV, METHOD_SELECT) 275 const_iv (EV, METHOD_SELECT)
276 const_iv (EV, METHOD_POLL)
252 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)
253 }; 282 };
254 283
255 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; )
256 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 285 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
257 286
272 /* the poor man's shared library emulator */ 301 /* the poor man's shared library emulator */
273 evapi.ver = EV_API_VERSION; 302 evapi.ver = EV_API_VERSION;
274 evapi.rev = EV_API_REVISION; 303 evapi.rev = EV_API_REVISION;
275 evapi.sv_fileno = sv_fileno; 304 evapi.sv_fileno = sv_fileno;
276 evapi.sv_signum = sv_signum; 305 evapi.sv_signum = sv_signum;
277 evapi.now = &ev_now; 306 evapi.now = ev_now;
278 evapi.method = &ev_method; 307 evapi.method = ev_method;
279 evapi.loop_done = &ev_loop_done; 308 evapi.unloop = ev_unloop;
280 evapi.time = ev_time; 309 evapi.time = ev_time;
281 evapi.loop = ev_loop; 310 evapi.loop = ev_loop;
282 evapi.once = ev_once; 311 evapi.once = ev_once;
283 evapi.io_start = ev_io_start; 312 evapi.io_start = ev_io_start;
284 evapi.io_stop = ev_io_stop; 313 evapi.io_stop = ev_io_stop;
299 evapi.child_stop = ev_child_stop; 328 evapi.child_stop = ev_child_stop;
300 329
301 sv_setiv (sv, (IV)&evapi); 330 sv_setiv (sv, (IV)&evapi);
302 SvREADONLY_on (sv); 331 SvREADONLY_on (sv);
303 } 332 }
304 333#ifndef WIN32
305 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 334 pthread_atfork (0, 0, ev_default_fork);
335#endif
306} 336}
307 337
308NV ev_now () 338NV ev_now ()
309 CODE:
310 RETVAL = ev_now;
311 OUTPUT:
312 RETVAL
313 339
314int ev_method () 340int ev_method ()
315 CODE:
316 RETVAL = ev_method;
317 OUTPUT:
318 RETVAL
319 341
320NV ev_time () 342NV ev_time ()
321 343
322void ev_init (int flags = 0) 344int ev_default_loop (int methods = EVMETHOD_AUTO)
323 345
324void ev_loop (int flags = 0) 346void ev_loop (int flags = 0)
325 347
326void ev_loop_done (int value = 1) 348void ev_unloop (int how = 1)
327 CODE:
328 ev_loop_done = value;
329 349
330struct ev_io *io (SV *fh, int events, SV *cb) 350struct ev_io *io (SV *fh, int events, SV *cb)
331 ALIAS: 351 ALIAS:
332 io_ns = 1 352 io_ns = 1
333 CODE: 353 CODE:
409 429
410struct ev_child *child (int pid, SV *cb) 430struct ev_child *child (int pid, SV *cb)
411 ALIAS: 431 ALIAS:
412 check_ns = 1 432 check_ns = 1
413 CODE: 433 CODE:
414 RETVAL = e_new (sizeof (struct ev_check), cb); 434 RETVAL = e_new (sizeof (struct ev_child), cb);
415 ev_child_set (RETVAL, pid); 435 ev_child_set (RETVAL, pid);
416 if (!ix) ev_child_start (RETVAL); 436 if (!ix) ev_child_start (RETVAL);
417 OUTPUT: 437 OUTPUT:
418 RETVAL 438 RETVAL
419 439
437 457
438void trigger (struct ev_watcher *w, int revents = EV_NONE) 458void trigger (struct ev_watcher *w, int revents = EV_NONE)
439 CODE: 459 CODE:
440 w->cb (w, revents); 460 w->cb (w, revents);
441 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
442MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 495MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
443 496
444void ev_io_start (struct ev_io *w) 497void ev_io_start (struct ev_io *w)
445 498
446void ev_io_stop (struct ev_io *w) 499void ev_io_stop (struct ev_io *w)
451 e_destroy (w); 504 e_destroy (w);
452 505
453void set (struct ev_io *w, SV *fh, int events) 506void set (struct ev_io *w, SV *fh, int events)
454 CODE: 507 CODE:
455{ 508{
456 int active = w->active; 509 int active = ev_is_active (w);
457 int fd = sv_fileno (fh); 510 int fd = sv_fileno (fh);
458 CHECK_FD (fh, fd); 511 CHECK_FD (fh, fd);
459 512
460 if (active) ev_io_stop (w); 513 if (active) ev_io_stop (w);
461 514
470{ 523{
471 RETVAL = newSVsv (w->fh); 524 RETVAL = newSVsv (w->fh);
472 525
473 if (items > 1) 526 if (items > 1)
474 { 527 {
475 int active = w->active; 528 int active = ev_is_active (w);
476 if (active) ev_io_stop (w); 529 if (active) ev_io_stop (w);
477 530
478 sv_setsv (w->fh, new_fh); 531 sv_setsv (w->fh, new_fh);
479 ev_io_set (w, sv_fileno (w->fh), w->events); 532 ev_io_set (w, sv_fileno (w->fh), w->events);
480 533
482 } 535 }
483} 536}
484 OUTPUT: 537 OUTPUT:
485 RETVAL 538 RETVAL
486 539
487short events (struct ev_io *w, short new_events = EV_UNDEF) 540int events (struct ev_io *w, int new_events = EV_UNDEF)
488 CODE: 541 CODE:
489{ 542{
490 RETVAL = w->events; 543 RETVAL = w->events;
491 544
492 if (items > 1) 545 if (items > 1)
493 { 546 {
494 int active = w->active; 547 int active = ev_is_active (w);
495 if (active) ev_io_stop (w); 548 if (active) ev_io_stop (w);
496 549
497 ev_io_set (w, w->fd, new_events); 550 ev_io_set (w, w->fd, new_events);
498 551
499 if (active) ev_io_start (w); 552 if (active) ev_io_start (w);
511void DESTROY (struct ev_signal *w) 564void DESTROY (struct ev_signal *w)
512 CODE: 565 CODE:
513 ev_signal_stop (w); 566 ev_signal_stop (w);
514 e_destroy (w); 567 e_destroy (w);
515 568
516void set (struct ev_signal *w, SV *signal = 0) 569void set (struct ev_signal *w, SV *signal)
517 CODE: 570 CODE:
518{ 571{
519 Signal signum = sv_signum (signal); /* may croak here */ 572 Signal signum = sv_signum (signal); /* may croak here */
520 int active = w->active; 573 int active = ev_is_active (w);
521 574
522 if (active) ev_signal_stop (w); 575 if (active) ev_signal_stop (w);
576
523 ev_signal_set (w, signum); 577 ev_signal_set (w, signum);
578
524 if (active) ev_signal_start (w); 579 if (active) ev_signal_start (w);
525} 580}
526 581
527MODULE = 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
528 600
529MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 601MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
530 602
531void ev_timer_start (struct ev_timer *w) 603void ev_timer_start (struct ev_timer *w)
532 INIT: 604 INIT:
546void set (struct ev_timer *w, NV after, NV repeat = 0.) 618void set (struct ev_timer *w, NV after, NV repeat = 0.)
547 INIT: 619 INIT:
548 CHECK_REPEAT (repeat); 620 CHECK_REPEAT (repeat);
549 CODE: 621 CODE:
550{ 622{
551 int active = w->active; 623 int active = ev_is_active (w);
552 if (active) ev_timer_stop (w); 624 if (active) ev_timer_stop (w);
553 ev_timer_set (w, after, repeat); 625 ev_timer_set (w, after, repeat);
554 if (active) ev_timer_start (w); 626 if (active) ev_timer_start (w);
555} 627}
556 628
570void set (struct ev_periodic *w, NV at, NV interval = 0.) 642void set (struct ev_periodic *w, NV at, NV interval = 0.)
571 INIT: 643 INIT:
572 CHECK_REPEAT (interval); 644 CHECK_REPEAT (interval);
573 CODE: 645 CODE:
574{ 646{
575 int active = w->active; 647 int active = ev_is_active (w);
576 if (active) ev_periodic_stop (w); 648 if (active) ev_periodic_stop (w);
649
577 ev_periodic_set (w, at, interval); 650 ev_periodic_set (w, at, interval);
651
578 if (active) ev_periodic_start (w); 652 if (active) ev_periodic_start (w);
579} 653}
580 654
581MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 655MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
582 656
623 e_destroy (w); 697 e_destroy (w);
624 698
625void set (struct ev_child *w, int pid) 699void set (struct ev_child *w, int pid)
626 CODE: 700 CODE:
627{ 701{
628 int active = w->active; 702 int active = ev_is_active (w);
629 if (active) ev_child_stop (w); 703 if (active) ev_child_stop (w);
704
630 ev_child_set (w, pid); 705 ev_child_set (w, pid);
706
631 if (active) ev_child_start (w); 707 if (active) ev_child_start (w);
632} 708}
633 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
634int status (struct ev_child *w) 729int rstatus (struct ev_child *w)
730 ALIAS:
731 rpid = 1
635 CODE: 732 CODE:
636 RETVAL = w->status; 733 RETVAL = ix ? w->rpid : w->rstatus;
637 OUTPUT: 734 OUTPUT:
638 RETVAL 735 RETVAL
639 736
640#if 0 737#ifndef WIN32
641 738
642MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 739MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
643 740
644BOOT: 741BOOT:
645{ 742{
728 825
729void evdns_search_add (char *domain) 826void evdns_search_add (char *domain)
730 827
731void evdns_search_ndots_set (int ndots) 828void evdns_search_ndots_set (int ndots)
732 829
830#if 0
733 831
734MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 832MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
735 833
736BOOT: 834BOOT:
737{ 835{
769 867
770#void DESTROY (struct evhttp_request *req); 868#void DESTROY (struct evhttp_request *req);
771 869
772#endif 870#endif
773 871
872#endif
774 873
775 874
776 875
777 876
778 877

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines