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

Comparing EV/EV.xs (file contents):
Revision 1.35 by root, Thu Nov 1 13:33:12 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"
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
13 35
14typedef int Signal; 36typedef int Signal;
15 37
16static struct EVAPI evapi; 38static struct EVAPI evapi;
17 39
24 *stash_idle, 46 *stash_idle,
25 *stash_prepare, 47 *stash_prepare,
26 *stash_check, 48 *stash_check,
27 *stash_child; 49 *stash_child;
28 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
29static int 57static int
30sv_signum (SV *sig) 58sv_signum (SV *sig)
31{ 59{
32 int signum; 60 int signum;
33 61
73 SvPOK_only (self); 101 SvPOK_only (self);
74 SvCUR_set (self, size); 102 SvCUR_set (self, size);
75 103
76 w = (struct ev_watcher *)SvPVX (self); 104 w = (struct ev_watcher *)SvPVX (self);
77 105
78 ev_watcher_init (w, e_cb); 106 ev_init (w, e_cb);
79 107
108 w->data = 0;
80 w->fh = 0; 109 w->fh = 0;
81 w->cb_sv = newSVsv (cb_sv); 110 w->cb_sv = newSVsv (cb_sv);
82 w->self = self; 111 w->self = self;
83 112
84 return (void *)w; 113 return (void *)w;
85} 114}
86 115
87static void * 116static void
88e_destroy (void *w_) 117e_destroy (void *w_)
89{ 118{
90 struct ev_watcher *w = w_; 119 struct ev_watcher *w = (struct ev_watcher *)w_;
91 120
92 SvREFCNT_dec (w->fh ); w->fh = 0; 121 SvREFCNT_dec (w->fh ); w->fh = 0;
93 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;
94} 124}
95 125
96static SV * 126static SV *
97e_bless (struct ev_watcher *w, HV *stash) 127e_bless (struct ev_watcher *w, HV *stash)
98{ 128{
131 PUSHMARK (SP); 161 PUSHMARK (SP);
132 EXTEND (SP, 2); 162 EXTEND (SP, 2);
133 PUSHs (sv_self); 163 PUSHs (sv_self);
134 PUSHs (sv_events); 164 PUSHs (sv_events);
135 165
136 if (revents & EV_CHILD)
137 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
138
139 PUTBACK; 166 PUTBACK;
140 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 167 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
141 SP = PL_stack_base + mark; PUTBACK; 168 SP = PL_stack_base + mark; PUTBACK;
142 169
143 SvREFCNT_dec (sv_self); 170 SvREFCNT_dec (sv_self);
155 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);
156 SP = PL_stack_base + mark; PUTBACK; 183 SP = PL_stack_base + mark; PUTBACK;
157 } 184 }
158} 185}
159 186
160#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
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;
224 294
225PROTOTYPES: ENABLE 295PROTOTYPES: ENABLE
226 296
227BOOT: 297BOOT:
228{ 298{
229 int i;
230 HV *stash = gv_stashpv ("EV", 1); 299 HV *stash = gv_stashpv ("EV", 1);
231 300
232 static const struct { 301 static const struct {
233 const char *name; 302 const char *name;
234 IV iv; 303 IV iv;
235 } *civ, const_iv[] = { 304 } *civ, const_iv[] = {
236# 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
237 const_iv (EV_, UNDEF) 309 const_iv (EV_, UNDEF)
238 const_iv (EV_, NONE) 310 const_iv (EV_, NONE)
239 const_iv (EV_, TIMEOUT) 311 const_iv (EV_, TIMEOUT)
240 const_iv (EV_, READ) 312 const_iv (EV_, READ)
241 const_iv (EV_, WRITE) 313 const_iv (EV_, WRITE)
245 const_iv (EV_, ERROR) 317 const_iv (EV_, ERROR)
246 318
247 const_iv (EV, LOOP_ONESHOT) 319 const_iv (EV, LOOP_ONESHOT)
248 const_iv (EV, LOOP_NONBLOCK) 320 const_iv (EV, LOOP_NONBLOCK)
249 321
250 const_iv (EV, METHOD_NONE) 322 const_iv (EV, METHOD_AUTO)
251 const_iv (EV, METHOD_SELECT) 323 const_iv (EV, METHOD_SELECT)
324 const_iv (EV, METHOD_POLL)
252 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)
253 }; 330 };
254 331
255 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; )
256 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 333 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
257 334
272 /* the poor man's shared library emulator */ 349 /* the poor man's shared library emulator */
273 evapi.ver = EV_API_VERSION; 350 evapi.ver = EV_API_VERSION;
274 evapi.rev = EV_API_REVISION; 351 evapi.rev = EV_API_REVISION;
275 evapi.sv_fileno = sv_fileno; 352 evapi.sv_fileno = sv_fileno;
276 evapi.sv_signum = sv_signum; 353 evapi.sv_signum = sv_signum;
277 evapi.now = &ev_now; 354 evapi.now = ev_now;
278 evapi.method = &ev_method; 355 evapi.method = ev_method;
279 evapi.loop_done = &ev_loop_done; 356 evapi.unloop = ev_unloop;
280 evapi.time = ev_time; 357 evapi.time = ev_time;
281 evapi.loop = ev_loop; 358 evapi.loop = ev_loop;
282 evapi.once = ev_once; 359 evapi.once = ev_once;
283 evapi.io_start = ev_io_start; 360 evapi.io_start = ev_io_start;
284 evapi.io_stop = ev_io_stop; 361 evapi.io_stop = ev_io_stop;
299 evapi.child_stop = ev_child_stop; 376 evapi.child_stop = ev_child_stop;
300 377
301 sv_setiv (sv, (IV)&evapi); 378 sv_setiv (sv, (IV)&evapi);
302 SvREADONLY_on (sv); 379 SvREADONLY_on (sv);
303 } 380 }
304 381#ifndef WIN32
305 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 382 pthread_atfork (0, 0, ev_default_fork);
383#endif
306} 384}
307 385
308NV ev_now () 386NV ev_now ()
309 CODE:
310 RETVAL = ev_now;
311 OUTPUT:
312 RETVAL
313 387
314int ev_method () 388int ev_method ()
315 CODE:
316 RETVAL = ev_method;
317 OUTPUT:
318 RETVAL
319 389
320NV ev_time () 390NV ev_time ()
321 391
322void ev_init (int flags = 0) 392int ev_default_loop (int methods = EVMETHOD_AUTO)
323 393
324void ev_loop (int flags = 0) 394void ev_loop (int flags = 0)
325 395
326void ev_loop_done (int value = 1) 396void ev_unloop (int how = 1)
327 CODE:
328 ev_loop_done = value;
329 397
330struct ev_io *io (SV *fh, int events, SV *cb) 398struct ev_io *io (SV *fh, int events, SV *cb)
331 ALIAS: 399 ALIAS:
332 io_ns = 1 400 io_ns = 1
333 CODE: 401 CODE:
353 ev_timer_set (RETVAL, after, repeat); 421 ev_timer_set (RETVAL, after, repeat);
354 if (!ix) ev_timer_start (RETVAL); 422 if (!ix) ev_timer_start (RETVAL);
355 OUTPUT: 423 OUTPUT:
356 RETVAL 424 RETVAL
357 425
358struct ev_periodic *periodic (NV at, NV interval, SV *cb) 426SV *periodic (NV at, NV interval, SV *reschedule_cb, SV *cb)
359 ALIAS: 427 ALIAS:
360 periodic_ns = 1 428 periodic_ns = 1
361 INIT: 429 INIT:
362 CHECK_REPEAT (interval); 430 CHECK_REPEAT (interval);
363 CODE: 431 CODE:
432{
433 struct ev_periodic *w;
364 RETVAL = e_new (sizeof (struct ev_periodic), cb); 434 w = e_new (sizeof (struct ev_periodic), cb);
365 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);
366 if (!ix) ev_periodic_start (RETVAL); 438 if (!ix) ev_periodic_start (w);
439}
367 OUTPUT: 440 OUTPUT:
368 RETVAL 441 RETVAL
369 442
370struct ev_signal *signal (Signal signum, SV *cb) 443struct ev_signal *signal (Signal signum, SV *cb)
371 ALIAS: 444 ALIAS:
409 482
410struct ev_child *child (int pid, SV *cb) 483struct ev_child *child (int pid, SV *cb)
411 ALIAS: 484 ALIAS:
412 check_ns = 1 485 check_ns = 1
413 CODE: 486 CODE:
414 RETVAL = e_new (sizeof (struct ev_check), cb); 487 RETVAL = e_new (sizeof (struct ev_child), cb);
415 ev_child_set (RETVAL, pid); 488 ev_child_set (RETVAL, pid);
416 if (!ix) ev_child_start (RETVAL); 489 if (!ix) ev_child_start (RETVAL);
417 OUTPUT: 490 OUTPUT:
418 RETVAL 491 RETVAL
419 492
433 sv_setsv (w->cb_sv, new_cb); 506 sv_setsv (w->cb_sv, new_cb);
434} 507}
435 OUTPUT: 508 OUTPUT:
436 RETVAL 509 RETVAL
437 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
438void trigger (struct ev_watcher *w, int revents = EV_NONE) 519void trigger (struct ev_watcher *w, int revents = EV_NONE)
439 CODE: 520 CODE:
440 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
441 555
442MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 556MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
443 557
444void ev_io_start (struct ev_io *w) 558void ev_io_start (struct ev_io *w)
445 559
451 e_destroy (w); 565 e_destroy (w);
452 566
453void set (struct ev_io *w, SV *fh, int events) 567void set (struct ev_io *w, SV *fh, int events)
454 CODE: 568 CODE:
455{ 569{
456 int active = w->active; 570 int active = ev_is_active (w);
457 int fd = sv_fileno (fh); 571 int fd = sv_fileno (fh);
458 CHECK_FD (fh, fd); 572 CHECK_FD (fh, fd);
459 573
460 if (active) ev_io_stop (w); 574 if (active) ev_io_stop (w);
461 575
470{ 584{
471 RETVAL = newSVsv (w->fh); 585 RETVAL = newSVsv (w->fh);
472 586
473 if (items > 1) 587 if (items > 1)
474 { 588 {
475 int active = w->active; 589 int active = ev_is_active (w);
476 if (active) ev_io_stop (w); 590 if (active) ev_io_stop (w);
477 591
478 sv_setsv (w->fh, new_fh); 592 sv_setsv (w->fh, new_fh);
479 ev_io_set (w, sv_fileno (w->fh), w->events); 593 ev_io_set (w, sv_fileno (w->fh), w->events);
480 594
482 } 596 }
483} 597}
484 OUTPUT: 598 OUTPUT:
485 RETVAL 599 RETVAL
486 600
487short events (struct ev_io *w, short new_events = EV_UNDEF) 601int events (struct ev_io *w, int new_events = EV_UNDEF)
488 CODE: 602 CODE:
489{ 603{
490 RETVAL = w->events; 604 RETVAL = w->events;
491 605
492 if (items > 1) 606 if (items > 1)
493 { 607 {
494 int active = w->active; 608 int active = ev_is_active (w);
495 if (active) ev_io_stop (w); 609 if (active) ev_io_stop (w);
496 610
497 ev_io_set (w, w->fd, new_events); 611 ev_io_set (w, w->fd, new_events);
498 612
499 if (active) ev_io_start (w); 613 if (active) ev_io_start (w);
511void DESTROY (struct ev_signal *w) 625void DESTROY (struct ev_signal *w)
512 CODE: 626 CODE:
513 ev_signal_stop (w); 627 ev_signal_stop (w);
514 e_destroy (w); 628 e_destroy (w);
515 629
516void set (struct ev_signal *w, SV *signal = 0) 630void set (struct ev_signal *w, SV *signal)
517 CODE: 631 CODE:
518{ 632{
519 Signal signum = sv_signum (signal); /* may croak here */ 633 Signal signum = sv_signum (signal); /* may croak here */
520 int active = w->active; 634 int active = ev_is_active (w);
521 635
522 if (active) ev_signal_stop (w); 636 if (active) ev_signal_stop (w);
637
523 ev_signal_set (w, signum); 638 ev_signal_set (w, signum);
639
524 if (active) ev_signal_start (w); 640 if (active) ev_signal_start (w);
525} 641}
526 642
527MODULE = 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
528 661
529MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 662MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
530 663
531void ev_timer_start (struct ev_timer *w) 664void ev_timer_start (struct ev_timer *w)
532 INIT: 665 INIT:
546void set (struct ev_timer *w, NV after, NV repeat = 0.) 679void set (struct ev_timer *w, NV after, NV repeat = 0.)
547 INIT: 680 INIT:
548 CHECK_REPEAT (repeat); 681 CHECK_REPEAT (repeat);
549 CODE: 682 CODE:
550{ 683{
551 int active = w->active; 684 int active = ev_is_active (w);
552 if (active) ev_timer_stop (w); 685 if (active) ev_timer_stop (w);
553 ev_timer_set (w, after, repeat); 686 ev_timer_set (w, after, repeat);
554 if (active) ev_timer_start (w); 687 if (active) ev_timer_start (w);
555} 688}
556 689
560 INIT: 693 INIT:
561 CHECK_REPEAT (w->interval); 694 CHECK_REPEAT (w->interval);
562 695
563void ev_periodic_stop (struct ev_periodic *w) 696void ev_periodic_stop (struct ev_periodic *w)
564 697
698void ev_periodic_again (struct ev_periodic *w)
699
565void DESTROY (struct ev_periodic *w) 700void DESTROY (struct ev_periodic *w)
566 CODE: 701 CODE:
567 ev_periodic_stop (w); 702 ev_periodic_stop (w);
568 e_destroy (w); 703 e_destroy (w);
569 704
570void 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)
571 INIT: 706 INIT:
572 CHECK_REPEAT (interval); 707 CHECK_REPEAT (interval);
573 CODE: 708 CODE:
574{ 709{
575 int active = w->active; 710 int active = ev_is_active (w);
576 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;
577 ev_periodic_set (w, at, interval); 715 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
716
578 if (active) ev_periodic_start (w); 717 if (active) ev_periodic_start (w);
579} 718}
580 719
581MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 720MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
582 721
623 e_destroy (w); 762 e_destroy (w);
624 763
625void set (struct ev_child *w, int pid) 764void set (struct ev_child *w, int pid)
626 CODE: 765 CODE:
627{ 766{
628 int active = w->active; 767 int active = ev_is_active (w);
629 if (active) ev_child_stop (w); 768 if (active) ev_child_stop (w);
769
630 ev_child_set (w, pid); 770 ev_child_set (w, pid);
771
631 if (active) ev_child_start (w); 772 if (active) ev_child_start (w);
632} 773}
633 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
634int status (struct ev_child *w) 794int rstatus (struct ev_child *w)
795 ALIAS:
796 rpid = 1
635 CODE: 797 CODE:
636 RETVAL = w->status; 798 RETVAL = ix ? w->rpid : w->rstatus;
637 OUTPUT: 799 OUTPUT:
638 RETVAL 800 RETVAL
639 801
640#if 0 802#ifndef WIN32
641 803
642MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 804MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
643 805
644BOOT: 806BOOT:
645{ 807{
728 890
729void evdns_search_add (char *domain) 891void evdns_search_add (char *domain)
730 892
731void evdns_search_ndots_set (int ndots) 893void evdns_search_ndots_set (int ndots)
732 894
895#if 0
733 896
734MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 897MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
735 898
736BOOT: 899BOOT:
737{ 900{
769 932
770#void DESTROY (struct evhttp_request *req); 933#void DESTROY (struct evhttp_request *req);
771 934
772#endif 935#endif
773 936
937#endif
774 938
775 939
776 940
777 941
778 942

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines