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.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#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#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 */
13#include "libev/ev.c" 17#include "libev/ev.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"
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
161#if 0 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
162///////////////////////////////////////////////////////////////////////////// 230/////////////////////////////////////////////////////////////////////////////
163// DNS 231// DNS
164 232
233#ifndef WIN32
165static void 234static void
166dns_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)
167{ 236{
168 dSP; 237 dSP;
169 SV *cb = (SV *)arg; 238 SV *cb = (SV *)arg;
225 294
226PROTOTYPES: ENABLE 295PROTOTYPES: ENABLE
227 296
228BOOT: 297BOOT:
229{ 298{
230 int i;
231 HV *stash = gv_stashpv ("EV", 1); 299 HV *stash = gv_stashpv ("EV", 1);
232 300
233 static const struct { 301 static const struct {
234 const char *name; 302 const char *name;
235 IV iv; 303 IV iv;
236 } *civ, const_iv[] = { 304 } *civ, const_iv[] = {
237# 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
238 const_iv (EV_, UNDEF) 309 const_iv (EV_, UNDEF)
239 const_iv (EV_, NONE) 310 const_iv (EV_, NONE)
240 const_iv (EV_, TIMEOUT) 311 const_iv (EV_, TIMEOUT)
241 const_iv (EV_, READ) 312 const_iv (EV_, READ)
242 const_iv (EV_, WRITE) 313 const_iv (EV_, WRITE)
246 const_iv (EV_, ERROR) 317 const_iv (EV_, ERROR)
247 318
248 const_iv (EV, LOOP_ONESHOT) 319 const_iv (EV, LOOP_ONESHOT)
249 const_iv (EV, LOOP_NONBLOCK) 320 const_iv (EV, LOOP_NONBLOCK)
250 321
251 const_iv (EV, METHOD_NONE) 322 const_iv (EV, METHOD_AUTO)
252 const_iv (EV, METHOD_SELECT) 323 const_iv (EV, METHOD_SELECT)
324 const_iv (EV, METHOD_POLL)
253 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)
254 }; 330 };
255 331
256 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; )
257 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 333 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
258 334
273 /* the poor man's shared library emulator */ 349 /* the poor man's shared library emulator */
274 evapi.ver = EV_API_VERSION; 350 evapi.ver = EV_API_VERSION;
275 evapi.rev = EV_API_REVISION; 351 evapi.rev = EV_API_REVISION;
276 evapi.sv_fileno = sv_fileno; 352 evapi.sv_fileno = sv_fileno;
277 evapi.sv_signum = sv_signum; 353 evapi.sv_signum = sv_signum;
278 evapi.now = &ev_now; 354 evapi.now = ev_now;
279 evapi.method = &ev_method; 355 evapi.method = ev_method;
280 evapi.loop_done = &ev_loop_done; 356 evapi.unloop = ev_unloop;
281 evapi.time = ev_time; 357 evapi.time = ev_time;
282 evapi.loop = ev_loop; 358 evapi.loop = ev_loop;
283 evapi.once = ev_once; 359 evapi.once = ev_once;
284 evapi.io_start = ev_io_start; 360 evapi.io_start = ev_io_start;
285 evapi.io_stop = ev_io_stop; 361 evapi.io_stop = ev_io_stop;
300 evapi.child_stop = ev_child_stop; 376 evapi.child_stop = ev_child_stop;
301 377
302 sv_setiv (sv, (IV)&evapi); 378 sv_setiv (sv, (IV)&evapi);
303 SvREADONLY_on (sv); 379 SvREADONLY_on (sv);
304 } 380 }
305 381#ifndef WIN32
306 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 382 pthread_atfork (0, 0, ev_default_fork);
383#endif
307} 384}
308 385
309NV ev_now () 386NV ev_now ()
310 CODE:
311 RETVAL = ev_now;
312 OUTPUT:
313 RETVAL
314 387
315int ev_method () 388int ev_method ()
316 CODE:
317 RETVAL = ev_method;
318 OUTPUT:
319 RETVAL
320 389
321NV ev_time () 390NV ev_time ()
322 391
323void ev_init (int flags = 0) 392int ev_default_loop (int methods = EVMETHOD_AUTO)
324 393
325void ev_loop (int flags = 0) 394void ev_loop (int flags = 0)
326 395
327void ev_loop_done (int value = 1) 396void ev_unloop (int how = 1)
328 CODE:
329 ev_loop_done = value;
330 397
331struct ev_io *io (SV *fh, int events, SV *cb) 398struct ev_io *io (SV *fh, int events, SV *cb)
332 ALIAS: 399 ALIAS:
333 io_ns = 1 400 io_ns = 1
334 CODE: 401 CODE:
354 ev_timer_set (RETVAL, after, repeat); 421 ev_timer_set (RETVAL, after, repeat);
355 if (!ix) ev_timer_start (RETVAL); 422 if (!ix) ev_timer_start (RETVAL);
356 OUTPUT: 423 OUTPUT:
357 RETVAL 424 RETVAL
358 425
359struct ev_periodic *periodic (NV at, NV interval, SV *cb) 426SV *periodic (NV at, NV interval, SV *reschedule_cb, SV *cb)
360 ALIAS: 427 ALIAS:
361 periodic_ns = 1 428 periodic_ns = 1
362 INIT: 429 INIT:
363 CHECK_REPEAT (interval); 430 CHECK_REPEAT (interval);
364 CODE: 431 CODE:
432{
433 struct ev_periodic *w;
365 RETVAL = e_new (sizeof (struct ev_periodic), cb); 434 w = e_new (sizeof (struct ev_periodic), cb);
366 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);
367 if (!ix) ev_periodic_start (RETVAL); 438 if (!ix) ev_periodic_start (w);
439}
368 OUTPUT: 440 OUTPUT:
369 RETVAL 441 RETVAL
370 442
371struct ev_signal *signal (Signal signum, SV *cb) 443struct ev_signal *signal (Signal signum, SV *cb)
372 ALIAS: 444 ALIAS:
410 482
411struct ev_child *child (int pid, SV *cb) 483struct ev_child *child (int pid, SV *cb)
412 ALIAS: 484 ALIAS:
413 check_ns = 1 485 check_ns = 1
414 CODE: 486 CODE:
415 RETVAL = e_new (sizeof (struct ev_check), cb); 487 RETVAL = e_new (sizeof (struct ev_child), cb);
416 ev_child_set (RETVAL, pid); 488 ev_child_set (RETVAL, pid);
417 if (!ix) ev_child_start (RETVAL); 489 if (!ix) ev_child_start (RETVAL);
418 OUTPUT: 490 OUTPUT:
419 RETVAL 491 RETVAL
420 492
434 sv_setsv (w->cb_sv, new_cb); 506 sv_setsv (w->cb_sv, new_cb);
435} 507}
436 OUTPUT: 508 OUTPUT:
437 RETVAL 509 RETVAL
438 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
439void trigger (struct ev_watcher *w, int revents = EV_NONE) 519void trigger (struct ev_watcher *w, int revents = EV_NONE)
440 CODE: 520 CODE:
441 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
442 555
443MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 556MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
444 557
445void ev_io_start (struct ev_io *w) 558void ev_io_start (struct ev_io *w)
446 559
452 e_destroy (w); 565 e_destroy (w);
453 566
454void set (struct ev_io *w, SV *fh, int events) 567void set (struct ev_io *w, SV *fh, int events)
455 CODE: 568 CODE:
456{ 569{
457 int active = w->active; 570 int active = ev_is_active (w);
458 int fd = sv_fileno (fh); 571 int fd = sv_fileno (fh);
459 CHECK_FD (fh, fd); 572 CHECK_FD (fh, fd);
460 573
461 if (active) ev_io_stop (w); 574 if (active) ev_io_stop (w);
462 575
471{ 584{
472 RETVAL = newSVsv (w->fh); 585 RETVAL = newSVsv (w->fh);
473 586
474 if (items > 1) 587 if (items > 1)
475 { 588 {
476 int active = w->active; 589 int active = ev_is_active (w);
477 if (active) ev_io_stop (w); 590 if (active) ev_io_stop (w);
478 591
479 sv_setsv (w->fh, new_fh); 592 sv_setsv (w->fh, new_fh);
480 ev_io_set (w, sv_fileno (w->fh), w->events); 593 ev_io_set (w, sv_fileno (w->fh), w->events);
481 594
483 } 596 }
484} 597}
485 OUTPUT: 598 OUTPUT:
486 RETVAL 599 RETVAL
487 600
488short events (struct ev_io *w, short new_events = EV_UNDEF) 601int events (struct ev_io *w, int new_events = EV_UNDEF)
489 CODE: 602 CODE:
490{ 603{
491 RETVAL = w->events; 604 RETVAL = w->events;
492 605
493 if (items > 1) 606 if (items > 1)
494 { 607 {
495 int active = w->active; 608 int active = ev_is_active (w);
496 if (active) ev_io_stop (w); 609 if (active) ev_io_stop (w);
497 610
498 ev_io_set (w, w->fd, new_events); 611 ev_io_set (w, w->fd, new_events);
499 612
500 if (active) ev_io_start (w); 613 if (active) ev_io_start (w);
512void DESTROY (struct ev_signal *w) 625void DESTROY (struct ev_signal *w)
513 CODE: 626 CODE:
514 ev_signal_stop (w); 627 ev_signal_stop (w);
515 e_destroy (w); 628 e_destroy (w);
516 629
517void set (struct ev_signal *w, SV *signal = 0) 630void set (struct ev_signal *w, SV *signal)
518 CODE: 631 CODE:
519{ 632{
520 Signal signum = sv_signum (signal); /* may croak here */ 633 Signal signum = sv_signum (signal); /* may croak here */
521 int active = w->active; 634 int active = ev_is_active (w);
522 635
523 if (active) ev_signal_stop (w); 636 if (active) ev_signal_stop (w);
637
524 ev_signal_set (w, signum); 638 ev_signal_set (w, signum);
639
525 if (active) ev_signal_start (w); 640 if (active) ev_signal_start (w);
526} 641}
527 642
528MODULE = 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
529 661
530MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 662MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
531 663
532void ev_timer_start (struct ev_timer *w) 664void ev_timer_start (struct ev_timer *w)
533 INIT: 665 INIT:
547void set (struct ev_timer *w, NV after, NV repeat = 0.) 679void set (struct ev_timer *w, NV after, NV repeat = 0.)
548 INIT: 680 INIT:
549 CHECK_REPEAT (repeat); 681 CHECK_REPEAT (repeat);
550 CODE: 682 CODE:
551{ 683{
552 int active = w->active; 684 int active = ev_is_active (w);
553 if (active) ev_timer_stop (w); 685 if (active) ev_timer_stop (w);
554 ev_timer_set (w, after, repeat); 686 ev_timer_set (w, after, repeat);
555 if (active) ev_timer_start (w); 687 if (active) ev_timer_start (w);
556} 688}
557 689
561 INIT: 693 INIT:
562 CHECK_REPEAT (w->interval); 694 CHECK_REPEAT (w->interval);
563 695
564void ev_periodic_stop (struct ev_periodic *w) 696void ev_periodic_stop (struct ev_periodic *w)
565 697
698void ev_periodic_again (struct ev_periodic *w)
699
566void DESTROY (struct ev_periodic *w) 700void DESTROY (struct ev_periodic *w)
567 CODE: 701 CODE:
568 ev_periodic_stop (w); 702 ev_periodic_stop (w);
569 e_destroy (w); 703 e_destroy (w);
570 704
571void 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)
572 INIT: 706 INIT:
573 CHECK_REPEAT (interval); 707 CHECK_REPEAT (interval);
574 CODE: 708 CODE:
575{ 709{
576 int active = w->active; 710 int active = ev_is_active (w);
577 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;
578 ev_periodic_set (w, at, interval); 715 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
716
579 if (active) ev_periodic_start (w); 717 if (active) ev_periodic_start (w);
580} 718}
581 719
582MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 720MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
583 721
624 e_destroy (w); 762 e_destroy (w);
625 763
626void set (struct ev_child *w, int pid) 764void set (struct ev_child *w, int pid)
627 CODE: 765 CODE:
628{ 766{
629 int active = w->active; 767 int active = ev_is_active (w);
630 if (active) ev_child_stop (w); 768 if (active) ev_child_stop (w);
769
631 ev_child_set (w, pid); 770 ev_child_set (w, pid);
771
632 if (active) ev_child_start (w); 772 if (active) ev_child_start (w);
633} 773}
634 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
635int status (struct ev_child *w) 794int rstatus (struct ev_child *w)
795 ALIAS:
796 rpid = 1
636 CODE: 797 CODE:
637 RETVAL = w->status; 798 RETVAL = ix ? w->rpid : w->rstatus;
638 OUTPUT: 799 OUTPUT:
639 RETVAL 800 RETVAL
640 801
641#if 0 802#ifndef WIN32
642 803
643MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 804MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
644 805
645BOOT: 806BOOT:
646{ 807{
729 890
730void evdns_search_add (char *domain) 891void evdns_search_add (char *domain)
731 892
732void evdns_search_ndots_set (int ndots) 893void evdns_search_ndots_set (int ndots)
733 894
895#if 0
734 896
735MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 897MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
736 898
737BOOT: 899BOOT:
738{ 900{
770 932
771#void DESTROY (struct evhttp_request *req); 933#void DESTROY (struct evhttp_request *req);
772 934
773#endif 935#endif
774 936
937#endif
775 938
776 939
777 940
778 941
779 942

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines