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.63 by root, Sat Nov 10 03:19:21 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#define EV_SELECT_USE_WIN32_HANDLES 0
15#define EV_SELECT_USE_FD_SET 0
16/* due to bugs in OS X we have to use libev/ explicitly here */
10#include "libev/ev.c" 17#include "libev/ev.c"
11#include "libev/event.h"
12#include "libev/event.c" 18#include "event.c"
19
20#ifndef WIN32
21#define DNS_USE_GETTIMEOFDAY_FOR_ID 1
22#if !defined (WIN32) && !defined(__CYGWIN__)
23# define HAVE_STRUCT_IN6_ADDR 1
24#endif
25#undef HAVE_STRTOK_R
26#undef strtok_r
27#define strtok_r fake_strtok_r
28#include "evdns.h"
13#include "libev/evdns.c" 29#include "evdns.c"
30#endif
31
32#ifndef WIN32
33# include <pthread.h>
34#endif
14 35
15typedef int Signal; 36typedef int Signal;
16 37
17static struct EVAPI evapi; 38static struct EVAPI evapi;
18 39
25 *stash_idle, 46 *stash_idle,
26 *stash_prepare, 47 *stash_prepare,
27 *stash_check, 48 *stash_check,
28 *stash_child; 49 *stash_child;
29 50
51#ifndef SIG_SIZE
52/* kudos to Slaven Rezic for the idea */
53static char sig_size [] = { SIG_NUM };
54# define SIG_SIZE (sizeof (sig_size) + 1)
55#endif
56
30static int 57static int
31sv_signum (SV *sig) 58sv_signum (SV *sig)
32{ 59{
33 int signum; 60 int signum;
34 61
74 SvPOK_only (self); 101 SvPOK_only (self);
75 SvCUR_set (self, size); 102 SvCUR_set (self, size);
76 103
77 w = (struct ev_watcher *)SvPVX (self); 104 w = (struct ev_watcher *)SvPVX (self);
78 105
79 ev_watcher_init (w, e_cb); 106 ev_init (w, e_cb);
80 107
108 w->data = 0;
81 w->fh = 0; 109 w->fh = 0;
82 w->cb_sv = newSVsv (cb_sv); 110 w->cb_sv = newSVsv (cb_sv);
83 w->self = self; 111 w->self = self;
84 112
85 return (void *)w; 113 return (void *)w;
86} 114}
87 115
88static void * 116static void
89e_destroy (void *w_) 117e_destroy (void *w_)
90{ 118{
91 struct ev_watcher *w = w_; 119 struct ev_watcher *w = (struct ev_watcher *)w_;
92 120
93 SvREFCNT_dec (w->fh ); w->fh = 0; 121 SvREFCNT_dec (w->fh ); w->fh = 0;
94 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0; 122 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0;
123 SvREFCNT_dec (w->data ); w->data = 0;
95} 124}
96 125
97static SV * 126static SV *
98e_bless (struct ev_watcher *w, HV *stash) 127e_bless (struct ev_watcher *w, HV *stash)
99{ 128{
132 PUSHMARK (SP); 161 PUSHMARK (SP);
133 EXTEND (SP, 2); 162 EXTEND (SP, 2);
134 PUSHs (sv_self); 163 PUSHs (sv_self);
135 PUSHs (sv_events); 164 PUSHs (sv_events);
136 165
137 if (revents & EV_CHILD)
138 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
139
140 PUTBACK; 166 PUTBACK;
141 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 167 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
142 SP = PL_stack_base + mark; PUTBACK; 168 SP = PL_stack_base + mark; PUTBACK;
143 169
144 SvREFCNT_dec (sv_self); 170 SvREFCNT_dec (sv_self);
156 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 182 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
157 SP = PL_stack_base + mark; PUTBACK; 183 SP = PL_stack_base + mark; PUTBACK;
158 } 184 }
159} 185}
160 186
187static ev_tstamp
188e_periodic_cb (struct ev_periodic *w, ev_tstamp now)
189{
190 ev_tstamp retval;
191 int count;
192 dSP;
193
194 ENTER;
195 SAVETMPS;
196
197 PUSHMARK (SP);
198 EXTEND (SP, 2);
199 PUSHs (newRV_inc (w->self)); /* w->self MUST be blessed by now */
200 PUSHs (newSVnv (now));
201
202 PUTBACK;
203 count = call_sv (w->fh, G_SCALAR | G_EVAL);
204 SPAGAIN;
205
206 if (SvTRUE (ERRSV))
207 {
208 PUSHMARK (SP);
209 PUTBACK;
210 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
211 SPAGAIN;
212 }
213
214 if (count > 0)
215 {
216 retval = SvNV (TOPs);
217
218 if (retval < now)
219 retval = now;
220 }
221 else
222 retval = now;
223
224 FREETMPS;
225 LEAVE;
226
227 return retval;
228}
229
161///////////////////////////////////////////////////////////////////////////// 230/////////////////////////////////////////////////////////////////////////////
162// DNS 231// DNS
163 232
233#ifndef WIN32
164static void 234static void
165dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) 235dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg)
166{ 236{
167 dSP; 237 dSP;
168 SV *cb = (SV *)arg; 238 SV *cb = (SV *)arg;
207 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 277 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
208 } 278 }
209 279
210 LEAVE; 280 LEAVE;
211} 281}
282#endif
212 283
213#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 284#define CHECK_REPEAT(repeat) if (repeat < 0.) \
214 croak (# repeat " value must be >= 0"); 285 croak (# repeat " value must be >= 0");
215 286
216#define CHECK_FD(fh,fd) if ((fd) < 0) \ 287#define CHECK_FD(fh,fd) if ((fd) < 0) \
223 294
224PROTOTYPES: ENABLE 295PROTOTYPES: ENABLE
225 296
226BOOT: 297BOOT:
227{ 298{
228 int i;
229 HV *stash = gv_stashpv ("EV", 1); 299 HV *stash = gv_stashpv ("EV", 1);
230 300
231 static const struct { 301 static const struct {
232 const char *name; 302 const char *name;
233 IV iv; 303 IV iv;
234 } *civ, const_iv[] = { 304 } *civ, const_iv[] = {
235# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 305# define const_iv(pfx, name) { # name, (IV) pfx ## name },
306 const_iv (EV_, MINPRI)
307 const_iv (EV_, MAXPRI)
308
236 const_iv (EV_, UNDEF) 309 const_iv (EV_, UNDEF)
237 const_iv (EV_, NONE) 310 const_iv (EV_, NONE)
238 const_iv (EV_, TIMEOUT) 311 const_iv (EV_, TIMEOUT)
239 const_iv (EV_, READ) 312 const_iv (EV_, READ)
240 const_iv (EV_, WRITE) 313 const_iv (EV_, WRITE)
244 const_iv (EV_, ERROR) 317 const_iv (EV_, ERROR)
245 318
246 const_iv (EV, LOOP_ONESHOT) 319 const_iv (EV, LOOP_ONESHOT)
247 const_iv (EV, LOOP_NONBLOCK) 320 const_iv (EV, LOOP_NONBLOCK)
248 321
249 const_iv (EV, METHOD_NONE) 322 const_iv (EV, METHOD_AUTO)
250 const_iv (EV, METHOD_SELECT) 323 const_iv (EV, METHOD_SELECT)
324 const_iv (EV, METHOD_POLL)
251 const_iv (EV, METHOD_EPOLL) 325 const_iv (EV, METHOD_EPOLL)
326 const_iv (EV, METHOD_KQUEUE)
327 const_iv (EV, METHOD_DEVPOLL)
328 const_iv (EV, METHOD_PORT)
329 const_iv (EV, METHOD_ANY)
252 }; 330 };
253 331
254 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 332 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
255 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 333 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
256 334
271 /* the poor man's shared library emulator */ 349 /* the poor man's shared library emulator */
272 evapi.ver = EV_API_VERSION; 350 evapi.ver = EV_API_VERSION;
273 evapi.rev = EV_API_REVISION; 351 evapi.rev = EV_API_REVISION;
274 evapi.sv_fileno = sv_fileno; 352 evapi.sv_fileno = sv_fileno;
275 evapi.sv_signum = sv_signum; 353 evapi.sv_signum = sv_signum;
276 evapi.now = &ev_now; 354 evapi.now = ev_now;
277 evapi.method = &ev_method; 355 evapi.method = ev_method;
278 evapi.loop_done = &ev_loop_done; 356 evapi.unloop = ev_unloop;
279 evapi.time = ev_time; 357 evapi.time = ev_time;
280 evapi.loop = ev_loop; 358 evapi.loop = ev_loop;
281 evapi.once = ev_once; 359 evapi.once = ev_once;
282 evapi.io_start = ev_io_start; 360 evapi.io_start = ev_io_start;
283 evapi.io_stop = ev_io_stop; 361 evapi.io_stop = ev_io_stop;
298 evapi.child_stop = ev_child_stop; 376 evapi.child_stop = ev_child_stop;
299 377
300 sv_setiv (sv, (IV)&evapi); 378 sv_setiv (sv, (IV)&evapi);
301 SvREADONLY_on (sv); 379 SvREADONLY_on (sv);
302 } 380 }
303 381#ifndef WIN32
304 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 382 pthread_atfork (0, 0, ev_default_fork);
383#endif
305} 384}
306 385
307NV ev_now () 386NV ev_now ()
308 CODE:
309 RETVAL = ev_now;
310 OUTPUT:
311 RETVAL
312 387
313int ev_method () 388int ev_method ()
314 CODE:
315 RETVAL = ev_method;
316 OUTPUT:
317 RETVAL
318 389
319NV ev_time () 390NV ev_time ()
320 391
321void ev_init (int flags = 0) 392int ev_default_loop (int methods = EVMETHOD_AUTO)
322 393
323void ev_loop (int flags = 0) 394void ev_loop (int flags = 0)
324 395
325void ev_loop_done (int value = 1) 396void ev_unloop (int how = 1)
326 CODE:
327 ev_loop_done = value;
328 397
329struct ev_io *io (SV *fh, int events, SV *cb) 398struct ev_io *io (SV *fh, int events, SV *cb)
330 ALIAS: 399 ALIAS:
331 io_ns = 1 400 io_ns = 1
332 CODE: 401 CODE:
352 ev_timer_set (RETVAL, after, repeat); 421 ev_timer_set (RETVAL, after, repeat);
353 if (!ix) ev_timer_start (RETVAL); 422 if (!ix) ev_timer_start (RETVAL);
354 OUTPUT: 423 OUTPUT:
355 RETVAL 424 RETVAL
356 425
357struct ev_periodic *periodic (NV at, NV interval, SV *cb) 426SV *periodic (NV at, NV interval, SV *reschedule_cb, SV *cb)
358 ALIAS: 427 ALIAS:
359 periodic_ns = 1 428 periodic_ns = 1
360 INIT: 429 INIT:
361 CHECK_REPEAT (interval); 430 CHECK_REPEAT (interval);
362 CODE: 431 CODE:
432{
433 struct ev_periodic *w;
363 RETVAL = e_new (sizeof (struct ev_periodic), cb); 434 w = e_new (sizeof (struct ev_periodic), cb);
364 ev_periodic_set (RETVAL, at, interval); 435 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
436 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
437 RETVAL = e_bless ((struct ev_watcher *)w, stash_periodic);
365 if (!ix) ev_periodic_start (RETVAL); 438 if (!ix) ev_periodic_start (w);
439}
366 OUTPUT: 440 OUTPUT:
367 RETVAL 441 RETVAL
368 442
369struct ev_signal *signal (Signal signum, SV *cb) 443struct ev_signal *signal (Signal signum, SV *cb)
370 ALIAS: 444 ALIAS:
408 482
409struct ev_child *child (int pid, SV *cb) 483struct ev_child *child (int pid, SV *cb)
410 ALIAS: 484 ALIAS:
411 check_ns = 1 485 check_ns = 1
412 CODE: 486 CODE:
413 RETVAL = e_new (sizeof (struct ev_check), cb); 487 RETVAL = e_new (sizeof (struct ev_child), cb);
414 ev_child_set (RETVAL, pid); 488 ev_child_set (RETVAL, pid);
415 if (!ix) ev_child_start (RETVAL); 489 if (!ix) ev_child_start (RETVAL);
416 OUTPUT: 490 OUTPUT:
417 RETVAL 491 RETVAL
418 492
432 sv_setsv (w->cb_sv, new_cb); 506 sv_setsv (w->cb_sv, new_cb);
433} 507}
434 OUTPUT: 508 OUTPUT:
435 RETVAL 509 RETVAL
436 510
511SV *data (struct ev_watcher *w, SV *new_data = 0)
512 CODE:
513{
514 RETVAL = w->data ? newSVsv (w->data) : &PL_sv_undef;
515}
516 OUTPUT:
517 RETVAL
518
437void trigger (struct ev_watcher *w, int revents = EV_NONE) 519void trigger (struct ev_watcher *w, int revents = EV_NONE)
438 CODE: 520 CODE:
439 w->cb (w, revents); 521 w->cb (w, revents);
522
523int priority (struct ev_watcher *w, int new_priority = 0)
524 CODE:
525{
526 RETVAL = w->priority;
527
528 if (items > 1)
529 {
530 int active = ev_is_active (w);
531
532 if (new_priority < EV_MINPRI || new_priority > EV_MAXPRI)
533 croak ("watcher priority out of range, value must be between %d and %d, inclusive", EV_MINPRI, EV_MAXPRI);
534
535 if (active)
536 {
537 /* grrr. */
538 PUSHMARK (SP);
539 XPUSHs (ST (0));
540 call_method ("stop", G_DISCARD | G_VOID);
541 }
542
543 ev_set_priority (w, new_priority);
544
545 if (active)
546 {
547 PUSHMARK (SP);
548 XPUSHs (ST (0));
549 call_method ("start", G_DISCARD | G_VOID);
550 }
551 }
552}
553 OUTPUT:
554 RETVAL
440 555
441MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 556MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
442 557
443void ev_io_start (struct ev_io *w) 558void ev_io_start (struct ev_io *w)
444 559
450 e_destroy (w); 565 e_destroy (w);
451 566
452void set (struct ev_io *w, SV *fh, int events) 567void set (struct ev_io *w, SV *fh, int events)
453 CODE: 568 CODE:
454{ 569{
455 int active = w->active; 570 int active = ev_is_active (w);
456 int fd = sv_fileno (fh); 571 int fd = sv_fileno (fh);
457 CHECK_FD (fh, fd); 572 CHECK_FD (fh, fd);
458 573
459 if (active) ev_io_stop (w); 574 if (active) ev_io_stop (w);
460 575
469{ 584{
470 RETVAL = newSVsv (w->fh); 585 RETVAL = newSVsv (w->fh);
471 586
472 if (items > 1) 587 if (items > 1)
473 { 588 {
474 int active = w->active; 589 int active = ev_is_active (w);
475 if (active) ev_io_stop (w); 590 if (active) ev_io_stop (w);
476 591
477 sv_setsv (w->fh, new_fh); 592 sv_setsv (w->fh, new_fh);
478 ev_io_set (w, sv_fileno (w->fh), w->events); 593 ev_io_set (w, sv_fileno (w->fh), w->events);
479 594
488{ 603{
489 RETVAL = w->events; 604 RETVAL = w->events;
490 605
491 if (items > 1) 606 if (items > 1)
492 { 607 {
493 int active = w->active; 608 int active = ev_is_active (w);
494 if (active) ev_io_stop (w); 609 if (active) ev_io_stop (w);
495 610
496 ev_io_set (w, w->fd, new_events); 611 ev_io_set (w, w->fd, new_events);
497 612
498 if (active) ev_io_start (w); 613 if (active) ev_io_start (w);
510void DESTROY (struct ev_signal *w) 625void DESTROY (struct ev_signal *w)
511 CODE: 626 CODE:
512 ev_signal_stop (w); 627 ev_signal_stop (w);
513 e_destroy (w); 628 e_destroy (w);
514 629
515void set (struct ev_signal *w, SV *signal = 0) 630void set (struct ev_signal *w, SV *signal)
516 CODE: 631 CODE:
517{ 632{
518 Signal signum = sv_signum (signal); /* may croak here */ 633 Signal signum = sv_signum (signal); /* may croak here */
519 int active = w->active; 634 int active = ev_is_active (w);
520 635
521 if (active) ev_signal_stop (w); 636 if (active) ev_signal_stop (w);
637
522 ev_signal_set (w, signum); 638 ev_signal_set (w, signum);
639
523 if (active) ev_signal_start (w); 640 if (active) ev_signal_start (w);
524} 641}
525 642
526MODULE = EV PACKAGE = EV::Time 643int signal (struct ev_signal *w, SV *new_signal = 0)
644 CODE:
645{
646 RETVAL = w->signum;
647
648 if (items > 1)
649 {
650 Signal signum = sv_signum (new_signal); /* may croak here */
651 int active = ev_is_active (w);
652 if (active) ev_signal_stop (w);
653
654 ev_signal_set (w, signum);
655
656 if (active) ev_signal_start (w);
657 }
658}
659 OUTPUT:
660 RETVAL
527 661
528MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 662MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
529 663
530void ev_timer_start (struct ev_timer *w) 664void ev_timer_start (struct ev_timer *w)
531 INIT: 665 INIT:
545void set (struct ev_timer *w, NV after, NV repeat = 0.) 679void set (struct ev_timer *w, NV after, NV repeat = 0.)
546 INIT: 680 INIT:
547 CHECK_REPEAT (repeat); 681 CHECK_REPEAT (repeat);
548 CODE: 682 CODE:
549{ 683{
550 int active = w->active; 684 int active = ev_is_active (w);
551 if (active) ev_timer_stop (w); 685 if (active) ev_timer_stop (w);
552 ev_timer_set (w, after, repeat); 686 ev_timer_set (w, after, repeat);
553 if (active) ev_timer_start (w); 687 if (active) ev_timer_start (w);
554} 688}
555 689
559 INIT: 693 INIT:
560 CHECK_REPEAT (w->interval); 694 CHECK_REPEAT (w->interval);
561 695
562void ev_periodic_stop (struct ev_periodic *w) 696void ev_periodic_stop (struct ev_periodic *w)
563 697
698void ev_periodic_again (struct ev_periodic *w)
699
564void DESTROY (struct ev_periodic *w) 700void DESTROY (struct ev_periodic *w)
565 CODE: 701 CODE:
566 ev_periodic_stop (w); 702 ev_periodic_stop (w);
567 e_destroy (w); 703 e_destroy (w);
568 704
569void set (struct ev_periodic *w, NV at, NV interval = 0.) 705void set (struct ev_periodic *w, NV at, NV interval = 0., SV *reschedule_cb = &PL_sv_undef)
570 INIT: 706 INIT:
571 CHECK_REPEAT (interval); 707 CHECK_REPEAT (interval);
572 CODE: 708 CODE:
573{ 709{
574 int active = w->active; 710 int active = ev_is_active (w);
575 if (active) ev_periodic_stop (w); 711 if (active) ev_periodic_stop (w);
712
713 SvREFCNT_dec (w->fh);
714 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
576 ev_periodic_set (w, at, interval); 715 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
716
577 if (active) ev_periodic_start (w); 717 if (active) ev_periodic_start (w);
578} 718}
579 719
580MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 720MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
581 721
622 e_destroy (w); 762 e_destroy (w);
623 763
624void set (struct ev_child *w, int pid) 764void set (struct ev_child *w, int pid)
625 CODE: 765 CODE:
626{ 766{
627 int active = w->active; 767 int active = ev_is_active (w);
628 if (active) ev_child_stop (w); 768 if (active) ev_child_stop (w);
769
629 ev_child_set (w, pid); 770 ev_child_set (w, pid);
771
630 if (active) ev_child_start (w); 772 if (active) ev_child_start (w);
631} 773}
632 774
775int pid (struct ev_child *w, int new_pid = 0)
776 CODE:
777{
778 RETVAL = w->pid;
779
780 if (items > 1)
781 {
782 int active = ev_is_active (w);
783 if (active) ev_child_stop (w);
784
785 ev_child_set (w, new_pid);
786
787 if (active) ev_child_start (w);
788 }
789}
790 OUTPUT:
791 RETVAL
792
793
633int status (struct ev_child *w) 794int rstatus (struct ev_child *w)
795 ALIAS:
796 rpid = 1
634 CODE: 797 CODE:
635 RETVAL = w->status; 798 RETVAL = ix ? w->rpid : w->rstatus;
636 OUTPUT: 799 OUTPUT:
637 RETVAL 800 RETVAL
801
802#ifndef WIN32
638 803
639MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 804MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
640 805
641BOOT: 806BOOT:
642{ 807{
767 932
768#void DESTROY (struct evhttp_request *req); 933#void DESTROY (struct evhttp_request *req);
769 934
770#endif 935#endif
771 936
937#endif
772 938
773 939
774 940
775 941
776 942

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines