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

Comparing EV/EV.xs (file contents):
Revision 1.36 by root, Thu Nov 1 15:46:43 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
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"
13#include "libev/event.h"
14#include "libev/event.c" 16#include "event.c"
15 17
18#ifndef WIN32
16#define DNS_USE_GETTIMEOFDAY_FOR_ID 1 19#define DNS_USE_GETTIMEOFDAY_FOR_ID 1
20#if !defined (WIN32) && !defined(__CYGWIN__)
17#define HAVE_STRUCT_IN6_ADDR 1 21# define HAVE_STRUCT_IN6_ADDR 1
22#endif
18#define HAVE_STRTOK_R 1 23#undef HAVE_STRTOK_R
24#undef strtok_r
25#define strtok_r fake_strtok_r
19#include "libev/evdns.c" 26#include "evdns.c"
27#endif
28
29#ifndef WIN32
30# include <pthread.h>
31#endif
20 32
21typedef int Signal; 33typedef int Signal;
22 34
23static struct EVAPI evapi; 35static struct EVAPI evapi;
24 36
31 *stash_idle, 43 *stash_idle,
32 *stash_prepare, 44 *stash_prepare,
33 *stash_check, 45 *stash_check,
34 *stash_child; 46 *stash_child;
35 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
36static int 54static int
37sv_signum (SV *sig) 55sv_signum (SV *sig)
38{ 56{
39 int signum; 57 int signum;
40 58
89 w->self = self; 107 w->self = self;
90 108
91 return (void *)w; 109 return (void *)w;
92} 110}
93 111
94static void * 112static void
95e_destroy (void *w_) 113e_destroy (void *w_)
96{ 114{
97 struct ev_watcher *w = w_; 115 struct ev_watcher *w = (struct ev_watcher *)w_;
98 116
99 SvREFCNT_dec (w->fh ); w->fh = 0; 117 SvREFCNT_dec (w->fh ); w->fh = 0;
100 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0; 118 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0;
101} 119}
102 120
138 PUSHMARK (SP); 156 PUSHMARK (SP);
139 EXTEND (SP, 2); 157 EXTEND (SP, 2);
140 PUSHs (sv_self); 158 PUSHs (sv_self);
141 PUSHs (sv_events); 159 PUSHs (sv_events);
142 160
143 if (revents & EV_CHILD)
144 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
145
146 PUTBACK; 161 PUTBACK;
147 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 162 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
148 SP = PL_stack_base + mark; PUTBACK; 163 SP = PL_stack_base + mark; PUTBACK;
149 164
150 SvREFCNT_dec (sv_self); 165 SvREFCNT_dec (sv_self);
165} 180}
166 181
167///////////////////////////////////////////////////////////////////////////// 182/////////////////////////////////////////////////////////////////////////////
168// DNS 183// DNS
169 184
185#ifndef WIN32
170static void 186static void
171dns_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)
172{ 188{
173 dSP; 189 dSP;
174 SV *cb = (SV *)arg; 190 SV *cb = (SV *)arg;
213 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 229 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
214 } 230 }
215 231
216 LEAVE; 232 LEAVE;
217} 233}
234#endif
218 235
219#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 236#define CHECK_REPEAT(repeat) if (repeat < 0.) \
220 croak (# repeat " value must be >= 0"); 237 croak (# repeat " value must be >= 0");
221 238
222#define CHECK_FD(fh,fd) if ((fd) < 0) \ 239#define CHECK_FD(fh,fd) if ((fd) < 0) \
229 246
230PROTOTYPES: ENABLE 247PROTOTYPES: ENABLE
231 248
232BOOT: 249BOOT:
233{ 250{
234 int i;
235 HV *stash = gv_stashpv ("EV", 1); 251 HV *stash = gv_stashpv ("EV", 1);
236 252
237 static const struct { 253 static const struct {
238 const char *name; 254 const char *name;
239 IV iv; 255 IV iv;
240 } *civ, const_iv[] = { 256 } *civ, const_iv[] = {
241# 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
242 const_iv (EV_, UNDEF) 261 const_iv (EV_, UNDEF)
243 const_iv (EV_, NONE) 262 const_iv (EV_, NONE)
244 const_iv (EV_, TIMEOUT) 263 const_iv (EV_, TIMEOUT)
245 const_iv (EV_, READ) 264 const_iv (EV_, READ)
246 const_iv (EV_, WRITE) 265 const_iv (EV_, WRITE)
250 const_iv (EV_, ERROR) 269 const_iv (EV_, ERROR)
251 270
252 const_iv (EV, LOOP_ONESHOT) 271 const_iv (EV, LOOP_ONESHOT)
253 const_iv (EV, LOOP_NONBLOCK) 272 const_iv (EV, LOOP_NONBLOCK)
254 273
255 const_iv (EV, METHOD_NONE) 274 const_iv (EV, METHOD_AUTO)
256 const_iv (EV, METHOD_SELECT) 275 const_iv (EV, METHOD_SELECT)
276 const_iv (EV, METHOD_POLL)
257 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)
258 }; 282 };
259 283
260 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; )
261 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 285 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
262 286
277 /* the poor man's shared library emulator */ 301 /* the poor man's shared library emulator */
278 evapi.ver = EV_API_VERSION; 302 evapi.ver = EV_API_VERSION;
279 evapi.rev = EV_API_REVISION; 303 evapi.rev = EV_API_REVISION;
280 evapi.sv_fileno = sv_fileno; 304 evapi.sv_fileno = sv_fileno;
281 evapi.sv_signum = sv_signum; 305 evapi.sv_signum = sv_signum;
282 evapi.now = &ev_now; 306 evapi.now = ev_now;
283 evapi.method = &ev_method; 307 evapi.method = ev_method;
284 evapi.loop_done = &ev_loop_done; 308 evapi.unloop = ev_unloop;
285 evapi.time = ev_time; 309 evapi.time = ev_time;
286 evapi.loop = ev_loop; 310 evapi.loop = ev_loop;
287 evapi.once = ev_once; 311 evapi.once = ev_once;
288 evapi.io_start = ev_io_start; 312 evapi.io_start = ev_io_start;
289 evapi.io_stop = ev_io_stop; 313 evapi.io_stop = ev_io_stop;
304 evapi.child_stop = ev_child_stop; 328 evapi.child_stop = ev_child_stop;
305 329
306 sv_setiv (sv, (IV)&evapi); 330 sv_setiv (sv, (IV)&evapi);
307 SvREADONLY_on (sv); 331 SvREADONLY_on (sv);
308 } 332 }
309 333#ifndef WIN32
310 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 334 pthread_atfork (0, 0, ev_default_fork);
335#endif
311} 336}
312 337
313NV ev_now () 338NV ev_now ()
314 CODE:
315 RETVAL = ev_now;
316 OUTPUT:
317 RETVAL
318 339
319int ev_method () 340int ev_method ()
320 CODE:
321 RETVAL = ev_method;
322 OUTPUT:
323 RETVAL
324 341
325NV ev_time () 342NV ev_time ()
326 343
327void ev_init (int flags = 0) 344int ev_default_loop (int methods = EVMETHOD_AUTO)
328 345
329void ev_loop (int flags = 0) 346void ev_loop (int flags = 0)
330 347
331void ev_loop_done (int value = 1) 348void ev_unloop (int how = 1)
332 CODE:
333 ev_loop_done = value;
334 349
335struct ev_io *io (SV *fh, int events, SV *cb) 350struct ev_io *io (SV *fh, int events, SV *cb)
336 ALIAS: 351 ALIAS:
337 io_ns = 1 352 io_ns = 1
338 CODE: 353 CODE:
414 429
415struct ev_child *child (int pid, SV *cb) 430struct ev_child *child (int pid, SV *cb)
416 ALIAS: 431 ALIAS:
417 check_ns = 1 432 check_ns = 1
418 CODE: 433 CODE:
419 RETVAL = e_new (sizeof (struct ev_check), cb); 434 RETVAL = e_new (sizeof (struct ev_child), cb);
420 ev_child_set (RETVAL, pid); 435 ev_child_set (RETVAL, pid);
421 if (!ix) ev_child_start (RETVAL); 436 if (!ix) ev_child_start (RETVAL);
422 OUTPUT: 437 OUTPUT:
423 RETVAL 438 RETVAL
424 439
442 457
443void trigger (struct ev_watcher *w, int revents = EV_NONE) 458void trigger (struct ev_watcher *w, int revents = EV_NONE)
444 CODE: 459 CODE:
445 w->cb (w, revents); 460 w->cb (w, revents);
446 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
447MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 495MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
448 496
449void ev_io_start (struct ev_io *w) 497void ev_io_start (struct ev_io *w)
450 498
451void ev_io_stop (struct ev_io *w) 499void ev_io_stop (struct ev_io *w)
456 e_destroy (w); 504 e_destroy (w);
457 505
458void set (struct ev_io *w, SV *fh, int events) 506void set (struct ev_io *w, SV *fh, int events)
459 CODE: 507 CODE:
460{ 508{
461 int active = w->active; 509 int active = ev_is_active (w);
462 int fd = sv_fileno (fh); 510 int fd = sv_fileno (fh);
463 CHECK_FD (fh, fd); 511 CHECK_FD (fh, fd);
464 512
465 if (active) ev_io_stop (w); 513 if (active) ev_io_stop (w);
466 514
475{ 523{
476 RETVAL = newSVsv (w->fh); 524 RETVAL = newSVsv (w->fh);
477 525
478 if (items > 1) 526 if (items > 1)
479 { 527 {
480 int active = w->active; 528 int active = ev_is_active (w);
481 if (active) ev_io_stop (w); 529 if (active) ev_io_stop (w);
482 530
483 sv_setsv (w->fh, new_fh); 531 sv_setsv (w->fh, new_fh);
484 ev_io_set (w, sv_fileno (w->fh), w->events); 532 ev_io_set (w, sv_fileno (w->fh), w->events);
485 533
487 } 535 }
488} 536}
489 OUTPUT: 537 OUTPUT:
490 RETVAL 538 RETVAL
491 539
492short events (struct ev_io *w, short new_events = EV_UNDEF) 540int events (struct ev_io *w, int new_events = EV_UNDEF)
493 CODE: 541 CODE:
494{ 542{
495 RETVAL = w->events; 543 RETVAL = w->events;
496 544
497 if (items > 1) 545 if (items > 1)
498 { 546 {
499 int active = w->active; 547 int active = ev_is_active (w);
500 if (active) ev_io_stop (w); 548 if (active) ev_io_stop (w);
501 549
502 ev_io_set (w, w->fd, new_events); 550 ev_io_set (w, w->fd, new_events);
503 551
504 if (active) ev_io_start (w); 552 if (active) ev_io_start (w);
516void DESTROY (struct ev_signal *w) 564void DESTROY (struct ev_signal *w)
517 CODE: 565 CODE:
518 ev_signal_stop (w); 566 ev_signal_stop (w);
519 e_destroy (w); 567 e_destroy (w);
520 568
521void set (struct ev_signal *w, SV *signal = 0) 569void set (struct ev_signal *w, SV *signal)
522 CODE: 570 CODE:
523{ 571{
524 Signal signum = sv_signum (signal); /* may croak here */ 572 Signal signum = sv_signum (signal); /* may croak here */
525 int active = w->active; 573 int active = ev_is_active (w);
526 574
527 if (active) ev_signal_stop (w); 575 if (active) ev_signal_stop (w);
576
528 ev_signal_set (w, signum); 577 ev_signal_set (w, signum);
578
529 if (active) ev_signal_start (w); 579 if (active) ev_signal_start (w);
530} 580}
531 581
532MODULE = 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
533 600
534MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 601MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
535 602
536void ev_timer_start (struct ev_timer *w) 603void ev_timer_start (struct ev_timer *w)
537 INIT: 604 INIT:
551void set (struct ev_timer *w, NV after, NV repeat = 0.) 618void set (struct ev_timer *w, NV after, NV repeat = 0.)
552 INIT: 619 INIT:
553 CHECK_REPEAT (repeat); 620 CHECK_REPEAT (repeat);
554 CODE: 621 CODE:
555{ 622{
556 int active = w->active; 623 int active = ev_is_active (w);
557 if (active) ev_timer_stop (w); 624 if (active) ev_timer_stop (w);
558 ev_timer_set (w, after, repeat); 625 ev_timer_set (w, after, repeat);
559 if (active) ev_timer_start (w); 626 if (active) ev_timer_start (w);
560} 627}
561 628
575void set (struct ev_periodic *w, NV at, NV interval = 0.) 642void set (struct ev_periodic *w, NV at, NV interval = 0.)
576 INIT: 643 INIT:
577 CHECK_REPEAT (interval); 644 CHECK_REPEAT (interval);
578 CODE: 645 CODE:
579{ 646{
580 int active = w->active; 647 int active = ev_is_active (w);
581 if (active) ev_periodic_stop (w); 648 if (active) ev_periodic_stop (w);
649
582 ev_periodic_set (w, at, interval); 650 ev_periodic_set (w, at, interval);
651
583 if (active) ev_periodic_start (w); 652 if (active) ev_periodic_start (w);
584} 653}
585 654
586MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 655MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
587 656
628 e_destroy (w); 697 e_destroy (w);
629 698
630void set (struct ev_child *w, int pid) 699void set (struct ev_child *w, int pid)
631 CODE: 700 CODE:
632{ 701{
633 int active = w->active; 702 int active = ev_is_active (w);
634 if (active) ev_child_stop (w); 703 if (active) ev_child_stop (w);
704
635 ev_child_set (w, pid); 705 ev_child_set (w, pid);
706
636 if (active) ev_child_start (w); 707 if (active) ev_child_start (w);
637} 708}
638 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
639int status (struct ev_child *w) 729int rstatus (struct ev_child *w)
730 ALIAS:
731 rpid = 1
640 CODE: 732 CODE:
641 RETVAL = w->status; 733 RETVAL = ix ? w->rpid : w->rstatus;
642 OUTPUT: 734 OUTPUT:
643 RETVAL 735 RETVAL
736
737#ifndef WIN32
644 738
645MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 739MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
646 740
647BOOT: 741BOOT:
648{ 742{
773 867
774#void DESTROY (struct evhttp_request *req); 868#void DESTROY (struct evhttp_request *req);
775 869
776#endif 870#endif
777 871
872#endif
778 873
779 874
780 875
781 876
782 877

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines