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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines