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.63 by root, Sat Nov 10 03:19:21 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#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 */
12#include "libev/ev.c" 17#include "libev/ev.c"
13#include "libev/event.h"
14#include "libev/event.c" 18#include "event.c"
15 19
20#ifndef WIN32
16#define DNS_USE_GETTIMEOFDAY_FOR_ID 1 21#define DNS_USE_GETTIMEOFDAY_FOR_ID 1
22#if !defined (WIN32) && !defined(__CYGWIN__)
17#define HAVE_STRUCT_IN6_ADDR 1 23# define HAVE_STRUCT_IN6_ADDR 1
24#endif
18#define HAVE_STRTOK_R 1 25#undef HAVE_STRTOK_R
26#undef strtok_r
27#define strtok_r fake_strtok_r
28#include "evdns.h"
19#include "libev/evdns.c" 29#include "evdns.c"
30#endif
31
32#ifndef WIN32
33# include <pthread.h>
34#endif
20 35
21typedef int Signal; 36typedef int Signal;
22 37
23static struct EVAPI evapi; 38static struct EVAPI evapi;
24 39
31 *stash_idle, 46 *stash_idle,
32 *stash_prepare, 47 *stash_prepare,
33 *stash_check, 48 *stash_check,
34 *stash_child; 49 *stash_child;
35 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
36static int 57static int
37sv_signum (SV *sig) 58sv_signum (SV *sig)
38{ 59{
39 int signum; 60 int signum;
40 61
80 SvPOK_only (self); 101 SvPOK_only (self);
81 SvCUR_set (self, size); 102 SvCUR_set (self, size);
82 103
83 w = (struct ev_watcher *)SvPVX (self); 104 w = (struct ev_watcher *)SvPVX (self);
84 105
85 ev_watcher_init (w, e_cb); 106 ev_init (w, e_cb);
86 107
108 w->data = 0;
87 w->fh = 0; 109 w->fh = 0;
88 w->cb_sv = newSVsv (cb_sv); 110 w->cb_sv = newSVsv (cb_sv);
89 w->self = self; 111 w->self = self;
90 112
91 return (void *)w; 113 return (void *)w;
92} 114}
93 115
94static void * 116static void
95e_destroy (void *w_) 117e_destroy (void *w_)
96{ 118{
97 struct ev_watcher *w = w_; 119 struct ev_watcher *w = (struct ev_watcher *)w_;
98 120
99 SvREFCNT_dec (w->fh ); w->fh = 0; 121 SvREFCNT_dec (w->fh ); w->fh = 0;
100 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;
101} 124}
102 125
103static SV * 126static SV *
104e_bless (struct ev_watcher *w, HV *stash) 127e_bless (struct ev_watcher *w, HV *stash)
105{ 128{
138 PUSHMARK (SP); 161 PUSHMARK (SP);
139 EXTEND (SP, 2); 162 EXTEND (SP, 2);
140 PUSHs (sv_self); 163 PUSHs (sv_self);
141 PUSHs (sv_events); 164 PUSHs (sv_events);
142 165
143 if (revents & EV_CHILD)
144 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
145
146 PUTBACK; 166 PUTBACK;
147 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 167 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
148 SP = PL_stack_base + mark; PUTBACK; 168 SP = PL_stack_base + mark; PUTBACK;
149 169
150 SvREFCNT_dec (sv_self); 170 SvREFCNT_dec (sv_self);
162 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);
163 SP = PL_stack_base + mark; PUTBACK; 183 SP = PL_stack_base + mark; PUTBACK;
164 } 184 }
165} 185}
166 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
167///////////////////////////////////////////////////////////////////////////// 230/////////////////////////////////////////////////////////////////////////////
168// DNS 231// DNS
169 232
233#ifndef WIN32
170static void 234static void
171dns_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)
172{ 236{
173 dSP; 237 dSP;
174 SV *cb = (SV *)arg; 238 SV *cb = (SV *)arg;
213 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);
214 } 278 }
215 279
216 LEAVE; 280 LEAVE;
217} 281}
282#endif
218 283
219#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 284#define CHECK_REPEAT(repeat) if (repeat < 0.) \
220 croak (# repeat " value must be >= 0"); 285 croak (# repeat " value must be >= 0");
221 286
222#define CHECK_FD(fh,fd) if ((fd) < 0) \ 287#define CHECK_FD(fh,fd) if ((fd) < 0) \
229 294
230PROTOTYPES: ENABLE 295PROTOTYPES: ENABLE
231 296
232BOOT: 297BOOT:
233{ 298{
234 int i;
235 HV *stash = gv_stashpv ("EV", 1); 299 HV *stash = gv_stashpv ("EV", 1);
236 300
237 static const struct { 301 static const struct {
238 const char *name; 302 const char *name;
239 IV iv; 303 IV iv;
240 } *civ, const_iv[] = { 304 } *civ, const_iv[] = {
241# 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
242 const_iv (EV_, UNDEF) 309 const_iv (EV_, UNDEF)
243 const_iv (EV_, NONE) 310 const_iv (EV_, NONE)
244 const_iv (EV_, TIMEOUT) 311 const_iv (EV_, TIMEOUT)
245 const_iv (EV_, READ) 312 const_iv (EV_, READ)
246 const_iv (EV_, WRITE) 313 const_iv (EV_, WRITE)
250 const_iv (EV_, ERROR) 317 const_iv (EV_, ERROR)
251 318
252 const_iv (EV, LOOP_ONESHOT) 319 const_iv (EV, LOOP_ONESHOT)
253 const_iv (EV, LOOP_NONBLOCK) 320 const_iv (EV, LOOP_NONBLOCK)
254 321
255 const_iv (EV, METHOD_NONE) 322 const_iv (EV, METHOD_AUTO)
256 const_iv (EV, METHOD_SELECT) 323 const_iv (EV, METHOD_SELECT)
324 const_iv (EV, METHOD_POLL)
257 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)
258 }; 330 };
259 331
260 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; )
261 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 333 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
262 334
277 /* the poor man's shared library emulator */ 349 /* the poor man's shared library emulator */
278 evapi.ver = EV_API_VERSION; 350 evapi.ver = EV_API_VERSION;
279 evapi.rev = EV_API_REVISION; 351 evapi.rev = EV_API_REVISION;
280 evapi.sv_fileno = sv_fileno; 352 evapi.sv_fileno = sv_fileno;
281 evapi.sv_signum = sv_signum; 353 evapi.sv_signum = sv_signum;
282 evapi.now = &ev_now; 354 evapi.now = ev_now;
283 evapi.method = &ev_method; 355 evapi.method = ev_method;
284 evapi.loop_done = &ev_loop_done; 356 evapi.unloop = ev_unloop;
285 evapi.time = ev_time; 357 evapi.time = ev_time;
286 evapi.loop = ev_loop; 358 evapi.loop = ev_loop;
287 evapi.once = ev_once; 359 evapi.once = ev_once;
288 evapi.io_start = ev_io_start; 360 evapi.io_start = ev_io_start;
289 evapi.io_stop = ev_io_stop; 361 evapi.io_stop = ev_io_stop;
304 evapi.child_stop = ev_child_stop; 376 evapi.child_stop = ev_child_stop;
305 377
306 sv_setiv (sv, (IV)&evapi); 378 sv_setiv (sv, (IV)&evapi);
307 SvREADONLY_on (sv); 379 SvREADONLY_on (sv);
308 } 380 }
309 381#ifndef WIN32
310 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 382 pthread_atfork (0, 0, ev_default_fork);
383#endif
311} 384}
312 385
313NV ev_now () 386NV ev_now ()
314 CODE:
315 RETVAL = ev_now;
316 OUTPUT:
317 RETVAL
318 387
319int ev_method () 388int ev_method ()
320 CODE:
321 RETVAL = ev_method;
322 OUTPUT:
323 RETVAL
324 389
325NV ev_time () 390NV ev_time ()
326 391
327void ev_init (int flags = 0) 392int ev_default_loop (int methods = EVMETHOD_AUTO)
328 393
329void ev_loop (int flags = 0) 394void ev_loop (int flags = 0)
330 395
331void ev_loop_done (int value = 1) 396void ev_unloop (int how = 1)
332 CODE:
333 ev_loop_done = value;
334 397
335struct ev_io *io (SV *fh, int events, SV *cb) 398struct ev_io *io (SV *fh, int events, SV *cb)
336 ALIAS: 399 ALIAS:
337 io_ns = 1 400 io_ns = 1
338 CODE: 401 CODE:
358 ev_timer_set (RETVAL, after, repeat); 421 ev_timer_set (RETVAL, after, repeat);
359 if (!ix) ev_timer_start (RETVAL); 422 if (!ix) ev_timer_start (RETVAL);
360 OUTPUT: 423 OUTPUT:
361 RETVAL 424 RETVAL
362 425
363struct ev_periodic *periodic (NV at, NV interval, SV *cb) 426SV *periodic (NV at, NV interval, SV *reschedule_cb, SV *cb)
364 ALIAS: 427 ALIAS:
365 periodic_ns = 1 428 periodic_ns = 1
366 INIT: 429 INIT:
367 CHECK_REPEAT (interval); 430 CHECK_REPEAT (interval);
368 CODE: 431 CODE:
432{
433 struct ev_periodic *w;
369 RETVAL = e_new (sizeof (struct ev_periodic), cb); 434 w = e_new (sizeof (struct ev_periodic), cb);
370 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);
371 if (!ix) ev_periodic_start (RETVAL); 438 if (!ix) ev_periodic_start (w);
439}
372 OUTPUT: 440 OUTPUT:
373 RETVAL 441 RETVAL
374 442
375struct ev_signal *signal (Signal signum, SV *cb) 443struct ev_signal *signal (Signal signum, SV *cb)
376 ALIAS: 444 ALIAS:
414 482
415struct ev_child *child (int pid, SV *cb) 483struct ev_child *child (int pid, SV *cb)
416 ALIAS: 484 ALIAS:
417 check_ns = 1 485 check_ns = 1
418 CODE: 486 CODE:
419 RETVAL = e_new (sizeof (struct ev_check), cb); 487 RETVAL = e_new (sizeof (struct ev_child), cb);
420 ev_child_set (RETVAL, pid); 488 ev_child_set (RETVAL, pid);
421 if (!ix) ev_child_start (RETVAL); 489 if (!ix) ev_child_start (RETVAL);
422 OUTPUT: 490 OUTPUT:
423 RETVAL 491 RETVAL
424 492
438 sv_setsv (w->cb_sv, new_cb); 506 sv_setsv (w->cb_sv, new_cb);
439} 507}
440 OUTPUT: 508 OUTPUT:
441 RETVAL 509 RETVAL
442 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
443void trigger (struct ev_watcher *w, int revents = EV_NONE) 519void trigger (struct ev_watcher *w, int revents = EV_NONE)
444 CODE: 520 CODE:
445 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
446 555
447MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 556MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
448 557
449void ev_io_start (struct ev_io *w) 558void ev_io_start (struct ev_io *w)
450 559
456 e_destroy (w); 565 e_destroy (w);
457 566
458void set (struct ev_io *w, SV *fh, int events) 567void set (struct ev_io *w, SV *fh, int events)
459 CODE: 568 CODE:
460{ 569{
461 int active = w->active; 570 int active = ev_is_active (w);
462 int fd = sv_fileno (fh); 571 int fd = sv_fileno (fh);
463 CHECK_FD (fh, fd); 572 CHECK_FD (fh, fd);
464 573
465 if (active) ev_io_stop (w); 574 if (active) ev_io_stop (w);
466 575
475{ 584{
476 RETVAL = newSVsv (w->fh); 585 RETVAL = newSVsv (w->fh);
477 586
478 if (items > 1) 587 if (items > 1)
479 { 588 {
480 int active = w->active; 589 int active = ev_is_active (w);
481 if (active) ev_io_stop (w); 590 if (active) ev_io_stop (w);
482 591
483 sv_setsv (w->fh, new_fh); 592 sv_setsv (w->fh, new_fh);
484 ev_io_set (w, sv_fileno (w->fh), w->events); 593 ev_io_set (w, sv_fileno (w->fh), w->events);
485 594
487 } 596 }
488} 597}
489 OUTPUT: 598 OUTPUT:
490 RETVAL 599 RETVAL
491 600
492short events (struct ev_io *w, short new_events = EV_UNDEF) 601int events (struct ev_io *w, int new_events = EV_UNDEF)
493 CODE: 602 CODE:
494{ 603{
495 RETVAL = w->events; 604 RETVAL = w->events;
496 605
497 if (items > 1) 606 if (items > 1)
498 { 607 {
499 int active = w->active; 608 int active = ev_is_active (w);
500 if (active) ev_io_stop (w); 609 if (active) ev_io_stop (w);
501 610
502 ev_io_set (w, w->fd, new_events); 611 ev_io_set (w, w->fd, new_events);
503 612
504 if (active) ev_io_start (w); 613 if (active) ev_io_start (w);
516void DESTROY (struct ev_signal *w) 625void DESTROY (struct ev_signal *w)
517 CODE: 626 CODE:
518 ev_signal_stop (w); 627 ev_signal_stop (w);
519 e_destroy (w); 628 e_destroy (w);
520 629
521void set (struct ev_signal *w, SV *signal = 0) 630void set (struct ev_signal *w, SV *signal)
522 CODE: 631 CODE:
523{ 632{
524 Signal signum = sv_signum (signal); /* may croak here */ 633 Signal signum = sv_signum (signal); /* may croak here */
525 int active = w->active; 634 int active = ev_is_active (w);
526 635
527 if (active) ev_signal_stop (w); 636 if (active) ev_signal_stop (w);
637
528 ev_signal_set (w, signum); 638 ev_signal_set (w, signum);
639
529 if (active) ev_signal_start (w); 640 if (active) ev_signal_start (w);
530} 641}
531 642
532MODULE = 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
533 661
534MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 662MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
535 663
536void ev_timer_start (struct ev_timer *w) 664void ev_timer_start (struct ev_timer *w)
537 INIT: 665 INIT:
551void set (struct ev_timer *w, NV after, NV repeat = 0.) 679void set (struct ev_timer *w, NV after, NV repeat = 0.)
552 INIT: 680 INIT:
553 CHECK_REPEAT (repeat); 681 CHECK_REPEAT (repeat);
554 CODE: 682 CODE:
555{ 683{
556 int active = w->active; 684 int active = ev_is_active (w);
557 if (active) ev_timer_stop (w); 685 if (active) ev_timer_stop (w);
558 ev_timer_set (w, after, repeat); 686 ev_timer_set (w, after, repeat);
559 if (active) ev_timer_start (w); 687 if (active) ev_timer_start (w);
560} 688}
561 689
565 INIT: 693 INIT:
566 CHECK_REPEAT (w->interval); 694 CHECK_REPEAT (w->interval);
567 695
568void ev_periodic_stop (struct ev_periodic *w) 696void ev_periodic_stop (struct ev_periodic *w)
569 697
698void ev_periodic_again (struct ev_periodic *w)
699
570void DESTROY (struct ev_periodic *w) 700void DESTROY (struct ev_periodic *w)
571 CODE: 701 CODE:
572 ev_periodic_stop (w); 702 ev_periodic_stop (w);
573 e_destroy (w); 703 e_destroy (w);
574 704
575void 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)
576 INIT: 706 INIT:
577 CHECK_REPEAT (interval); 707 CHECK_REPEAT (interval);
578 CODE: 708 CODE:
579{ 709{
580 int active = w->active; 710 int active = ev_is_active (w);
581 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;
582 ev_periodic_set (w, at, interval); 715 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
716
583 if (active) ev_periodic_start (w); 717 if (active) ev_periodic_start (w);
584} 718}
585 719
586MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 720MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
587 721
628 e_destroy (w); 762 e_destroy (w);
629 763
630void set (struct ev_child *w, int pid) 764void set (struct ev_child *w, int pid)
631 CODE: 765 CODE:
632{ 766{
633 int active = w->active; 767 int active = ev_is_active (w);
634 if (active) ev_child_stop (w); 768 if (active) ev_child_stop (w);
769
635 ev_child_set (w, pid); 770 ev_child_set (w, pid);
771
636 if (active) ev_child_start (w); 772 if (active) ev_child_start (w);
637} 773}
638 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
639int status (struct ev_child *w) 794int rstatus (struct ev_child *w)
795 ALIAS:
796 rpid = 1
640 CODE: 797 CODE:
641 RETVAL = w->status; 798 RETVAL = ix ? w->rpid : w->rstatus;
642 OUTPUT: 799 OUTPUT:
643 RETVAL 800 RETVAL
801
802#ifndef WIN32
644 803
645MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 804MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
646 805
647BOOT: 806BOOT:
648{ 807{
773 932
774#void DESTROY (struct evhttp_request *req); 933#void DESTROY (struct evhttp_request *req);
775 934
776#endif 935#endif
777 936
937#endif
778 938
779 939
780 940
781 941
782 942

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines