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.47 by root, Sat Nov 3 21:58:51 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
8#define EV_STANDALONE 1
11#include "EV/EVAPI.h" 9#include "EV/EVAPI.h"
12 10
13#include "libev/ev.c" 11#include "libev/ev.c"
12#include "libev/event.h"
13#include "libev/event.c"
14#define DNS_USE_GETTIMEOFDAY_FOR_ID 1
15#define HAVE_STRUCT_IN6_ADDR 1
16#undef HAVE_STRTOK_R
17#undef strtok_r
18#define strtok_r fake_strtok_r
19#include "libev/evdns.c"
14 20
15typedef int Signal; 21typedef int Signal;
16 22
17static struct EVAPI evapi; 23static struct EVAPI evapi;
18 24
25 *stash_idle, 31 *stash_idle,
26 *stash_prepare, 32 *stash_prepare,
27 *stash_check, 33 *stash_check,
28 *stash_child; 34 *stash_child;
29 35
36#ifndef SIG_SIZE
37/* kudos to Slaven Rezic for the idea */
38static char sig_size [] = { SIG_NUM };
39# define SIG_SIZE (sizeof (sig_size) + 1)
40#endif
41
30static int 42static int
31sv_signum (SV *sig) 43sv_signum (SV *sig)
32{ 44{
33 int signum; 45 int signum;
34 46
132 PUSHMARK (SP); 144 PUSHMARK (SP);
133 EXTEND (SP, 2); 145 EXTEND (SP, 2);
134 PUSHs (sv_self); 146 PUSHs (sv_self);
135 PUSHs (sv_events); 147 PUSHs (sv_events);
136 148
137 if (revents & EV_CHILD)
138 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
139
140 PUTBACK; 149 PUTBACK;
141 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 150 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
142 SP = PL_stack_base + mark; PUTBACK; 151 SP = PL_stack_base + mark; PUTBACK;
143 152
144 SvREFCNT_dec (sv_self); 153 SvREFCNT_dec (sv_self);
156 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 165 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
157 SP = PL_stack_base + mark; PUTBACK; 166 SP = PL_stack_base + mark; PUTBACK;
158 } 167 }
159} 168}
160 169
161#if 0
162///////////////////////////////////////////////////////////////////////////// 170/////////////////////////////////////////////////////////////////////////////
163// DNS 171// DNS
164 172
165static void 173static void
166dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) 174dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg)
208 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 216 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
209 } 217 }
210 218
211 LEAVE; 219 LEAVE;
212} 220}
213#endif
214 221
215#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 222#define CHECK_REPEAT(repeat) if (repeat < 0.) \
216 croak (# repeat " value must be >= 0"); 223 croak (# repeat " value must be >= 0");
217 224
218#define CHECK_FD(fh,fd) if ((fd) < 0) \ 225#define CHECK_FD(fh,fd) if ((fd) < 0) \
233 static const struct { 240 static const struct {
234 const char *name; 241 const char *name;
235 IV iv; 242 IV iv;
236 } *civ, const_iv[] = { 243 } *civ, const_iv[] = {
237# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 244# define const_iv(pfx, name) { # name, (IV) pfx ## name },
245 const_iv (EV_, MINPRI)
246 const_iv (EV_, MAXPRI)
247
238 const_iv (EV_, UNDEF) 248 const_iv (EV_, UNDEF)
239 const_iv (EV_, NONE) 249 const_iv (EV_, NONE)
240 const_iv (EV_, TIMEOUT) 250 const_iv (EV_, TIMEOUT)
241 const_iv (EV_, READ) 251 const_iv (EV_, READ)
242 const_iv (EV_, WRITE) 252 const_iv (EV_, WRITE)
246 const_iv (EV_, ERROR) 256 const_iv (EV_, ERROR)
247 257
248 const_iv (EV, LOOP_ONESHOT) 258 const_iv (EV, LOOP_ONESHOT)
249 const_iv (EV, LOOP_NONBLOCK) 259 const_iv (EV, LOOP_NONBLOCK)
250 260
251 const_iv (EV, METHOD_NONE) 261 const_iv (EV, METHOD_AUTO)
252 const_iv (EV, METHOD_SELECT) 262 const_iv (EV, METHOD_SELECT)
263 const_iv (EV, METHOD_POLL)
253 const_iv (EV, METHOD_EPOLL) 264 const_iv (EV, METHOD_EPOLL)
265 const_iv (EV, METHOD_KQUEUE)
266 const_iv (EV, METHOD_DEVPOLL)
267 const_iv (EV, METHOD_PORT)
268 const_iv (EV, METHOD_ANY)
254 }; 269 };
255 270
256 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 271 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
257 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 272 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
258 273
273 /* the poor man's shared library emulator */ 288 /* the poor man's shared library emulator */
274 evapi.ver = EV_API_VERSION; 289 evapi.ver = EV_API_VERSION;
275 evapi.rev = EV_API_REVISION; 290 evapi.rev = EV_API_REVISION;
276 evapi.sv_fileno = sv_fileno; 291 evapi.sv_fileno = sv_fileno;
277 evapi.sv_signum = sv_signum; 292 evapi.sv_signum = sv_signum;
278 evapi.now = &ev_now; 293 evapi.now = ev_now;
279 evapi.method = &ev_method; 294 evapi.method = ev_method;
280 evapi.loop_done = &ev_loop_done; 295 evapi.unloop = ev_unloop;
281 evapi.time = ev_time; 296 evapi.time = ev_time;
282 evapi.loop = ev_loop; 297 evapi.loop = ev_loop;
283 evapi.once = ev_once; 298 evapi.once = ev_once;
284 evapi.io_start = ev_io_start; 299 evapi.io_start = ev_io_start;
285 evapi.io_stop = ev_io_stop; 300 evapi.io_stop = ev_io_stop;
305 320
306 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 321 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child);
307} 322}
308 323
309NV ev_now () 324NV ev_now ()
310 CODE:
311 RETVAL = ev_now;
312 OUTPUT:
313 RETVAL
314 325
315int ev_method () 326int ev_method ()
316 CODE:
317 RETVAL = ev_method;
318 OUTPUT:
319 RETVAL
320 327
321NV ev_time () 328NV ev_time ()
322 329
323void ev_init (int flags = 0) 330int ev_init (int methods = EVMETHOD_AUTO)
324 331
325void ev_loop (int flags = 0) 332void ev_loop (int flags = 0)
326 333
327void ev_loop_done (int value = 1) 334void ev_unloop (int how = 1)
328 CODE:
329 ev_loop_done = value;
330 335
331struct ev_io *io (SV *fh, int events, SV *cb) 336struct ev_io *io (SV *fh, int events, SV *cb)
332 ALIAS: 337 ALIAS:
333 io_ns = 1 338 io_ns = 1
334 CODE: 339 CODE:
410 415
411struct ev_child *child (int pid, SV *cb) 416struct ev_child *child (int pid, SV *cb)
412 ALIAS: 417 ALIAS:
413 check_ns = 1 418 check_ns = 1
414 CODE: 419 CODE:
415 RETVAL = e_new (sizeof (struct ev_check), cb); 420 RETVAL = e_new (sizeof (struct ev_child), cb);
416 ev_child_set (RETVAL, pid); 421 ev_child_set (RETVAL, pid);
417 if (!ix) ev_child_start (RETVAL); 422 if (!ix) ev_child_start (RETVAL);
418 OUTPUT: 423 OUTPUT:
419 RETVAL 424 RETVAL
420 425
438 443
439void trigger (struct ev_watcher *w, int revents = EV_NONE) 444void trigger (struct ev_watcher *w, int revents = EV_NONE)
440 CODE: 445 CODE:
441 w->cb (w, revents); 446 w->cb (w, revents);
442 447
448int priority (struct ev_watcher *w, int new_priority = 0)
449 CODE:
450{
451 RETVAL = w->priority;
452
453 if (items > 1)
454 {
455 int active = ev_is_active (w);
456
457 if (new_priority < EV_MINPRI || new_priority > EV_MAXPRI)
458 croak ("watcher priority out of range, value must be between %d and %d, inclusive", EV_MINPRI, EV_MAXPRI);
459
460 if (active)
461 {
462 /* grrr. */
463 PUSHMARK (SP);
464 XPUSHs (ST (0));
465 call_method ("stop", G_DISCARD | G_VOID);
466 }
467
468 ev_set_priority (w, new_priority);
469
470 if (active)
471 {
472 PUSHMARK (SP);
473 XPUSHs (ST (0));
474 call_method ("start", G_DISCARD | G_VOID);
475 }
476 }
477}
478 OUTPUT:
479 RETVAL
480
443MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_ 481MODULE = EV PACKAGE = EV::Io PREFIX = ev_io_
444 482
445void ev_io_start (struct ev_io *w) 483void ev_io_start (struct ev_io *w)
446 484
447void ev_io_stop (struct ev_io *w) 485void ev_io_stop (struct ev_io *w)
452 e_destroy (w); 490 e_destroy (w);
453 491
454void set (struct ev_io *w, SV *fh, int events) 492void set (struct ev_io *w, SV *fh, int events)
455 CODE: 493 CODE:
456{ 494{
457 int active = w->active; 495 int active = ev_is_active (w);
458 int fd = sv_fileno (fh); 496 int fd = sv_fileno (fh);
459 CHECK_FD (fh, fd); 497 CHECK_FD (fh, fd);
460 498
461 if (active) ev_io_stop (w); 499 if (active) ev_io_stop (w);
462 500
471{ 509{
472 RETVAL = newSVsv (w->fh); 510 RETVAL = newSVsv (w->fh);
473 511
474 if (items > 1) 512 if (items > 1)
475 { 513 {
476 int active = w->active; 514 int active = ev_is_active (w);
477 if (active) ev_io_stop (w); 515 if (active) ev_io_stop (w);
478 516
479 sv_setsv (w->fh, new_fh); 517 sv_setsv (w->fh, new_fh);
480 ev_io_set (w, sv_fileno (w->fh), w->events); 518 ev_io_set (w, sv_fileno (w->fh), w->events);
481 519
483 } 521 }
484} 522}
485 OUTPUT: 523 OUTPUT:
486 RETVAL 524 RETVAL
487 525
488short events (struct ev_io *w, short new_events = EV_UNDEF) 526int events (struct ev_io *w, int new_events = EV_UNDEF)
489 CODE: 527 CODE:
490{ 528{
491 RETVAL = w->events; 529 RETVAL = w->events;
492 530
493 if (items > 1) 531 if (items > 1)
494 { 532 {
495 int active = w->active; 533 int active = ev_is_active (w);
496 if (active) ev_io_stop (w); 534 if (active) ev_io_stop (w);
497 535
498 ev_io_set (w, w->fd, new_events); 536 ev_io_set (w, w->fd, new_events);
499 537
500 if (active) ev_io_start (w); 538 if (active) ev_io_start (w);
512void DESTROY (struct ev_signal *w) 550void DESTROY (struct ev_signal *w)
513 CODE: 551 CODE:
514 ev_signal_stop (w); 552 ev_signal_stop (w);
515 e_destroy (w); 553 e_destroy (w);
516 554
517void set (struct ev_signal *w, SV *signal = 0) 555void set (struct ev_signal *w, SV *signal)
518 CODE: 556 CODE:
519{ 557{
520 Signal signum = sv_signum (signal); /* may croak here */ 558 Signal signum = sv_signum (signal); /* may croak here */
521 int active = w->active; 559 int active = ev_is_active (w);
522 560
523 if (active) ev_signal_stop (w); 561 if (active) ev_signal_stop (w);
562
524 ev_signal_set (w, signum); 563 ev_signal_set (w, signum);
564
525 if (active) ev_signal_start (w); 565 if (active) ev_signal_start (w);
526} 566}
567
568int signal (struct ev_signal *w, SV *new_signal = 0)
569 CODE:
570{
571 RETVAL = w->signum;
572
573 if (items > 1)
574 {
575 Signal signum = sv_signum (new_signal); /* may croak here */
576 int active = ev_is_active (w);
577 if (active) ev_signal_stop (w);
578
579 ev_signal_set (w, signum);
580
581 if (active) ev_signal_start (w);
582 }
583}
584 OUTPUT:
585 RETVAL
527 586
528MODULE = EV PACKAGE = EV::Time 587MODULE = EV PACKAGE = EV::Time
529 588
530MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 589MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
531 590
547void set (struct ev_timer *w, NV after, NV repeat = 0.) 606void set (struct ev_timer *w, NV after, NV repeat = 0.)
548 INIT: 607 INIT:
549 CHECK_REPEAT (repeat); 608 CHECK_REPEAT (repeat);
550 CODE: 609 CODE:
551{ 610{
552 int active = w->active; 611 int active = ev_is_active (w);
553 if (active) ev_timer_stop (w); 612 if (active) ev_timer_stop (w);
554 ev_timer_set (w, after, repeat); 613 ev_timer_set (w, after, repeat);
555 if (active) ev_timer_start (w); 614 if (active) ev_timer_start (w);
556} 615}
557 616
571void set (struct ev_periodic *w, NV at, NV interval = 0.) 630void set (struct ev_periodic *w, NV at, NV interval = 0.)
572 INIT: 631 INIT:
573 CHECK_REPEAT (interval); 632 CHECK_REPEAT (interval);
574 CODE: 633 CODE:
575{ 634{
576 int active = w->active; 635 int active = ev_is_active (w);
577 if (active) ev_periodic_stop (w); 636 if (active) ev_periodic_stop (w);
637
578 ev_periodic_set (w, at, interval); 638 ev_periodic_set (w, at, interval);
639
579 if (active) ev_periodic_start (w); 640 if (active) ev_periodic_start (w);
580} 641}
581 642
582MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 643MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
583 644
624 e_destroy (w); 685 e_destroy (w);
625 686
626void set (struct ev_child *w, int pid) 687void set (struct ev_child *w, int pid)
627 CODE: 688 CODE:
628{ 689{
629 int active = w->active; 690 int active = ev_is_active (w);
630 if (active) ev_child_stop (w); 691 if (active) ev_child_stop (w);
692
631 ev_child_set (w, pid); 693 ev_child_set (w, pid);
694
632 if (active) ev_child_start (w); 695 if (active) ev_child_start (w);
633} 696}
634 697
698int pid (struct ev_child *w, int new_pid = 0)
699 CODE:
700{
701 RETVAL = w->pid;
702
703 if (items > 1)
704 {
705 int active = ev_is_active (w);
706 if (active) ev_child_stop (w);
707
708 ev_child_set (w, new_pid);
709
710 if (active) ev_child_start (w);
711 }
712}
713 OUTPUT:
714 RETVAL
715
716
635int status (struct ev_child *w) 717int rstatus (struct ev_child *w)
718 ALIAS:
719 rpid = 1
636 CODE: 720 CODE:
637 RETVAL = w->status; 721 RETVAL = ix ? w->rpid : w->rstatus;
638 OUTPUT: 722 OUTPUT:
639 RETVAL 723 RETVAL
640
641#if 0
642 724
643MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 725MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
644 726
645BOOT: 727BOOT:
646{ 728{
729 811
730void evdns_search_add (char *domain) 812void evdns_search_add (char *domain)
731 813
732void evdns_search_ndots_set (int ndots) 814void evdns_search_ndots_set (int ndots)
733 815
816#if 0
734 817
735MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ 818MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
736 819
737BOOT: 820BOOT:
738{ 821{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines