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

Comparing EV/EV.xs (file contents):
Revision 1.38 by root, Thu Nov 1 17:45:30 2007 UTC vs.
Revision 1.56 by root, Tue Nov 6 16:09:37 2007 UTC

5/*#include <netinet/in.h>*/ 5/*#include <netinet/in.h>*/
6 6
7#define EV_PROTOTYPES 1 7#define EV_PROTOTYPES 1
8#include "EV/EVAPI.h" 8#include "EV/EVAPI.h"
9 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 */
10#include "libev/ev.c" 15#include "libev/ev.c"
11#include "libev/event.h"
12#include "libev/event.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
13#include "libev/evdns.c" 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);
159} 180}
160 181
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;
207 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);
208 } 230 }
209 231
210 LEAVE; 232 LEAVE;
211} 233}
234#endif
212 235
213#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 236#define CHECK_REPEAT(repeat) if (repeat < 0.) \
214 croak (# repeat " value must be >= 0"); 237 croak (# repeat " value must be >= 0");
215 238
216#define CHECK_FD(fh,fd) if ((fd) < 0) \ 239#define CHECK_FD(fh,fd) if ((fd) < 0) \
223 246
224PROTOTYPES: ENABLE 247PROTOTYPES: ENABLE
225 248
226BOOT: 249BOOT:
227{ 250{
228 int i;
229 HV *stash = gv_stashpv ("EV", 1); 251 HV *stash = gv_stashpv ("EV", 1);
230 252
231 static const struct { 253 static const struct {
232 const char *name; 254 const char *name;
233 IV iv; 255 IV iv;
234 } *civ, const_iv[] = { 256 } *civ, const_iv[] = {
235# 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
236 const_iv (EV_, UNDEF) 261 const_iv (EV_, UNDEF)
237 const_iv (EV_, NONE) 262 const_iv (EV_, NONE)
238 const_iv (EV_, TIMEOUT) 263 const_iv (EV_, TIMEOUT)
239 const_iv (EV_, READ) 264 const_iv (EV_, READ)
240 const_iv (EV_, WRITE) 265 const_iv (EV_, WRITE)
244 const_iv (EV_, ERROR) 269 const_iv (EV_, ERROR)
245 270
246 const_iv (EV, LOOP_ONESHOT) 271 const_iv (EV, LOOP_ONESHOT)
247 const_iv (EV, LOOP_NONBLOCK) 272 const_iv (EV, LOOP_NONBLOCK)
248 273
249 const_iv (EV, METHOD_NONE) 274 const_iv (EV, METHOD_AUTO)
250 const_iv (EV, METHOD_SELECT) 275 const_iv (EV, METHOD_SELECT)
276 const_iv (EV, METHOD_POLL)
251 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)
252 }; 282 };
253 283
254 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; )
255 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 285 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
256 286
271 /* the poor man's shared library emulator */ 301 /* the poor man's shared library emulator */
272 evapi.ver = EV_API_VERSION; 302 evapi.ver = EV_API_VERSION;
273 evapi.rev = EV_API_REVISION; 303 evapi.rev = EV_API_REVISION;
274 evapi.sv_fileno = sv_fileno; 304 evapi.sv_fileno = sv_fileno;
275 evapi.sv_signum = sv_signum; 305 evapi.sv_signum = sv_signum;
276 evapi.now = &ev_now; 306 evapi.now = ev_now;
277 evapi.method = &ev_method; 307 evapi.method = ev_method;
278 evapi.loop_done = &ev_loop_done; 308 evapi.unloop = ev_unloop;
279 evapi.time = ev_time; 309 evapi.time = ev_time;
280 evapi.loop = ev_loop; 310 evapi.loop = ev_loop;
281 evapi.once = ev_once; 311 evapi.once = ev_once;
282 evapi.io_start = ev_io_start; 312 evapi.io_start = ev_io_start;
283 evapi.io_stop = ev_io_stop; 313 evapi.io_stop = ev_io_stop;
298 evapi.child_stop = ev_child_stop; 328 evapi.child_stop = ev_child_stop;
299 329
300 sv_setiv (sv, (IV)&evapi); 330 sv_setiv (sv, (IV)&evapi);
301 SvREADONLY_on (sv); 331 SvREADONLY_on (sv);
302 } 332 }
303 333#ifndef WIN32
304 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 334 pthread_atfork (0, 0, ev_default_fork);
335#endif
305} 336}
306 337
307NV ev_now () 338NV ev_now ()
308 CODE:
309 RETVAL = ev_now;
310 OUTPUT:
311 RETVAL
312 339
313int ev_method () 340int ev_method ()
314 CODE:
315 RETVAL = ev_method;
316 OUTPUT:
317 RETVAL
318 341
319NV ev_time () 342NV ev_time ()
320 343
321void ev_init (int flags = 0) 344int ev_default_loop (int methods = EVMETHOD_AUTO)
322 345
323void ev_loop (int flags = 0) 346void ev_loop (int flags = 0)
324 347
325void ev_loop_done (int value = 1) 348void ev_unloop (int how = 1)
326 CODE:
327 ev_loop_done = value;
328 349
329struct ev_io *io (SV *fh, int events, SV *cb) 350struct ev_io *io (SV *fh, int events, SV *cb)
330 ALIAS: 351 ALIAS:
331 io_ns = 1 352 io_ns = 1
332 CODE: 353 CODE:
408 429
409struct ev_child *child (int pid, SV *cb) 430struct ev_child *child (int pid, SV *cb)
410 ALIAS: 431 ALIAS:
411 check_ns = 1 432 check_ns = 1
412 CODE: 433 CODE:
413 RETVAL = e_new (sizeof (struct ev_check), cb); 434 RETVAL = e_new (sizeof (struct ev_child), cb);
414 ev_child_set (RETVAL, pid); 435 ev_child_set (RETVAL, pid);
415 if (!ix) ev_child_start (RETVAL); 436 if (!ix) ev_child_start (RETVAL);
416 OUTPUT: 437 OUTPUT:
417 RETVAL 438 RETVAL
418 439
436 457
437void trigger (struct ev_watcher *w, int revents = EV_NONE) 458void trigger (struct ev_watcher *w, int revents = EV_NONE)
438 CODE: 459 CODE:
439 w->cb (w, revents); 460 w->cb (w, revents);
440 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
441MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 495MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
442 496
443void ev_io_start (struct ev_io *w) 497void ev_io_start (struct ev_io *w)
444 498
445void ev_io_stop (struct ev_io *w) 499void ev_io_stop (struct ev_io *w)
450 e_destroy (w); 504 e_destroy (w);
451 505
452void set (struct ev_io *w, SV *fh, int events) 506void set (struct ev_io *w, SV *fh, int events)
453 CODE: 507 CODE:
454{ 508{
455 int active = w->active; 509 int active = ev_is_active (w);
456 int fd = sv_fileno (fh); 510 int fd = sv_fileno (fh);
457 CHECK_FD (fh, fd); 511 CHECK_FD (fh, fd);
458 512
459 if (active) ev_io_stop (w); 513 if (active) ev_io_stop (w);
460 514
469{ 523{
470 RETVAL = newSVsv (w->fh); 524 RETVAL = newSVsv (w->fh);
471 525
472 if (items > 1) 526 if (items > 1)
473 { 527 {
474 int active = w->active; 528 int active = ev_is_active (w);
475 if (active) ev_io_stop (w); 529 if (active) ev_io_stop (w);
476 530
477 sv_setsv (w->fh, new_fh); 531 sv_setsv (w->fh, new_fh);
478 ev_io_set (w, sv_fileno (w->fh), w->events); 532 ev_io_set (w, sv_fileno (w->fh), w->events);
479 533
488{ 542{
489 RETVAL = w->events; 543 RETVAL = w->events;
490 544
491 if (items > 1) 545 if (items > 1)
492 { 546 {
493 int active = w->active; 547 int active = ev_is_active (w);
494 if (active) ev_io_stop (w); 548 if (active) ev_io_stop (w);
495 549
496 ev_io_set (w, w->fd, new_events); 550 ev_io_set (w, w->fd, new_events);
497 551
498 if (active) ev_io_start (w); 552 if (active) ev_io_start (w);
510void DESTROY (struct ev_signal *w) 564void DESTROY (struct ev_signal *w)
511 CODE: 565 CODE:
512 ev_signal_stop (w); 566 ev_signal_stop (w);
513 e_destroy (w); 567 e_destroy (w);
514 568
515void set (struct ev_signal *w, SV *signal = 0) 569void set (struct ev_signal *w, SV *signal)
516 CODE: 570 CODE:
517{ 571{
518 Signal signum = sv_signum (signal); /* may croak here */ 572 Signal signum = sv_signum (signal); /* may croak here */
519 int active = w->active; 573 int active = ev_is_active (w);
520 574
521 if (active) ev_signal_stop (w); 575 if (active) ev_signal_stop (w);
576
522 ev_signal_set (w, signum); 577 ev_signal_set (w, signum);
578
523 if (active) ev_signal_start (w); 579 if (active) ev_signal_start (w);
524} 580}
525 581
526MODULE = 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
527 600
528MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 601MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
529 602
530void ev_timer_start (struct ev_timer *w) 603void ev_timer_start (struct ev_timer *w)
531 INIT: 604 INIT:
545void set (struct ev_timer *w, NV after, NV repeat = 0.) 618void set (struct ev_timer *w, NV after, NV repeat = 0.)
546 INIT: 619 INIT:
547 CHECK_REPEAT (repeat); 620 CHECK_REPEAT (repeat);
548 CODE: 621 CODE:
549{ 622{
550 int active = w->active; 623 int active = ev_is_active (w);
551 if (active) ev_timer_stop (w); 624 if (active) ev_timer_stop (w);
552 ev_timer_set (w, after, repeat); 625 ev_timer_set (w, after, repeat);
553 if (active) ev_timer_start (w); 626 if (active) ev_timer_start (w);
554} 627}
555 628
569void set (struct ev_periodic *w, NV at, NV interval = 0.) 642void set (struct ev_periodic *w, NV at, NV interval = 0.)
570 INIT: 643 INIT:
571 CHECK_REPEAT (interval); 644 CHECK_REPEAT (interval);
572 CODE: 645 CODE:
573{ 646{
574 int active = w->active; 647 int active = ev_is_active (w);
575 if (active) ev_periodic_stop (w); 648 if (active) ev_periodic_stop (w);
649
576 ev_periodic_set (w, at, interval); 650 ev_periodic_set (w, at, interval);
651
577 if (active) ev_periodic_start (w); 652 if (active) ev_periodic_start (w);
578} 653}
579 654
580MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 655MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
581 656
622 e_destroy (w); 697 e_destroy (w);
623 698
624void set (struct ev_child *w, int pid) 699void set (struct ev_child *w, int pid)
625 CODE: 700 CODE:
626{ 701{
627 int active = w->active; 702 int active = ev_is_active (w);
628 if (active) ev_child_stop (w); 703 if (active) ev_child_stop (w);
704
629 ev_child_set (w, pid); 705 ev_child_set (w, pid);
706
630 if (active) ev_child_start (w); 707 if (active) ev_child_start (w);
631} 708}
632 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
633int status (struct ev_child *w) 729int rstatus (struct ev_child *w)
730 ALIAS:
731 rpid = 1
634 CODE: 732 CODE:
635 RETVAL = w->status; 733 RETVAL = ix ? w->rpid : w->rstatus;
636 OUTPUT: 734 OUTPUT:
637 RETVAL 735 RETVAL
736
737#ifndef WIN32
638 738
639MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 739MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
640 740
641BOOT: 741BOOT:
642{ 742{
767 867
768#void DESTROY (struct evhttp_request *req); 868#void DESTROY (struct evhttp_request *req);
769 869
770#endif 870#endif
771 871
872#endif
772 873
773 874
774 875
775 876
776 877

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines