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

Comparing EV/EV.xs (file contents):
Revision 1.78 by root, Sat Nov 24 08:28:10 2007 UTC vs.
Revision 1.83 by root, Thu Nov 29 17:28:13 2007 UTC

59 do { \ 59 do { \
60 REF (w); \ 60 REF (w); \
61 ev_ ## type ## _stop (w); \ 61 ev_ ## type ## _stop (w); \
62 } while (0) 62 } while (0)
63 63
64#define RESET(type,w,seta) \
65 do { \
66 int active = ev_is_active (w); \
67 if (active) STOP (type, w); \
68 ev_ ## type ## _set seta; \
69 if (active) START (type, w); \
70 } while (0)
71
64typedef int Signal; 72typedef int Signal;
65 73
66static struct EVAPI evapi; 74static struct EVAPI evapi;
67 75
68static HV 76static HV
69 *stash_watcher, 77 *stash_watcher,
70 *stash_io, 78 *stash_io,
71 *stash_timer, 79 *stash_timer,
72 *stash_periodic, 80 *stash_periodic,
73 *stash_signal, 81 *stash_signal,
82 *stash_child,
83 *stash_stat,
74 *stash_idle, 84 *stash_idle,
75 *stash_prepare, 85 *stash_prepare,
76 *stash_check, 86 *stash_check,
77 *stash_child; 87 *stash_embed,
88 *stash_fork;
78 89
79#ifndef SIG_SIZE 90#ifndef SIG_SIZE
80/* kudos to Slaven Rezic for the idea */ 91/* kudos to Slaven Rezic for the idea */
81static char sig_size [] = { SIG_NUM }; 92static char sig_size [] = { SIG_NUM };
82# define SIG_SIZE (sizeof (sig_size) + 1) 93# define SIG_SIZE (sizeof (sig_size) + 1)
83#endif 94#endif
84 95
85static int 96static Signal
86sv_signum (SV *sig) 97sv_signum (SV *sig)
87{ 98{
88 int signum; 99 Signal signum;
89 100
90 SvGETMAGIC (sig); 101 SvGETMAGIC (sig);
91 102
92 for (signum = 1; signum < SIG_SIZE; ++signum) 103 for (signum = 1; signum < SIG_SIZE; ++signum)
93 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) 104 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum]))
94 return signum; 105 return signum;
95 106
96 if (SvIV (sig) > 0) 107 signum = SvIV (sig);
108
109 if (signum > 0 && signum < SIG_SIZE)
97 return SvIV (sig); 110 return signum;
98 111
99 return -1; 112 return -1;
100} 113}
101 114
102///////////////////////////////////////////////////////////////////////////// 115/////////////////////////////////////////////////////////////////////////////
354 croak (# repeat " value must be >= 0"); 367 croak (# repeat " value must be >= 0");
355 368
356#define CHECK_FD(fh,fd) if ((fd) < 0) \ 369#define CHECK_FD(fh,fd) if ((fd) < 0) \
357 croak ("illegal file descriptor or filehandle (either no attached file descriptor or illegal value): %s", SvPV_nolen (fh)); 370 croak ("illegal file descriptor or filehandle (either no attached file descriptor or illegal value): %s", SvPV_nolen (fh));
358 371
372#define CHECK_SIG(sv,num) if ((num) < 0) \
373 croak ("illegal signal number or name: %s", SvPV_nolen (sv));
374
359///////////////////////////////////////////////////////////////////////////// 375/////////////////////////////////////////////////////////////////////////////
360// XS interface functions 376// XS interface functions
361 377
362MODULE = EV PACKAGE = EV PREFIX = ev_ 378MODULE = EV PACKAGE = EV PREFIX = ev_
363 379
396 const_iv (EV, BACKEND_KQUEUE) 412 const_iv (EV, BACKEND_KQUEUE)
397 const_iv (EV, BACKEND_DEVPOLL) 413 const_iv (EV, BACKEND_DEVPOLL)
398 const_iv (EV, BACKEND_PORT) 414 const_iv (EV, BACKEND_PORT)
399 const_iv (EV, FLAG_AUTO) 415 const_iv (EV, FLAG_AUTO)
400 const_iv (EV, FLAG_NOENV) 416 const_iv (EV, FLAG_NOENV)
417 const_iv (EV, FLAG_FORKCHECK)
401 }; 418 };
402 419
403 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 420 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
404 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 421 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
405 422
410 stash_signal = gv_stashpv ("EV::Signal" , 1); 427 stash_signal = gv_stashpv ("EV::Signal" , 1);
411 stash_idle = gv_stashpv ("EV::Idle" , 1); 428 stash_idle = gv_stashpv ("EV::Idle" , 1);
412 stash_prepare = gv_stashpv ("EV::Prepare" , 1); 429 stash_prepare = gv_stashpv ("EV::Prepare" , 1);
413 stash_check = gv_stashpv ("EV::Check" , 1); 430 stash_check = gv_stashpv ("EV::Check" , 1);
414 stash_child = gv_stashpv ("EV::Child" , 1); 431 stash_child = gv_stashpv ("EV::Child" , 1);
432 stash_embed = gv_stashpv ("EV::Embed" , 1);
433 stash_stat = gv_stashpv ("EV::Stat" , 1);
415 434
416 { 435 {
417 SV *sv = perl_get_sv ("EV::API", TRUE); 436 SV *sv = perl_get_sv ("EV::API", TRUE);
418 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ 437 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */
419 438
423 evapi.sv_fileno = sv_fileno; 442 evapi.sv_fileno = sv_fileno;
424 evapi.sv_signum = sv_signum; 443 evapi.sv_signum = sv_signum;
425 evapi.now = ev_now; 444 evapi.now = ev_now;
426 evapi.backend = ev_backend; 445 evapi.backend = ev_backend;
427 evapi.unloop = ev_unloop; 446 evapi.unloop = ev_unloop;
447 evapi.ref = ev_ref;
448 evapi.unref = ev_unref;
428 evapi.time = ev_time; 449 evapi.time = ev_time;
429 evapi.loop = ev_loop; 450 evapi.loop = ev_loop;
430 evapi.once = ev_once; 451 evapi.once = ev_once;
431 evapi.io_start = ev_io_start; 452 evapi.io_start = ev_io_start;
432 evapi.io_stop = ev_io_stop; 453 evapi.io_stop = ev_io_stop;
443 evapi.prepare_stop = ev_prepare_stop; 464 evapi.prepare_stop = ev_prepare_stop;
444 evapi.check_start = ev_check_start; 465 evapi.check_start = ev_check_start;
445 evapi.check_stop = ev_check_stop; 466 evapi.check_stop = ev_check_stop;
446 evapi.child_start = ev_child_start; 467 evapi.child_start = ev_child_start;
447 evapi.child_stop = ev_child_stop; 468 evapi.child_stop = ev_child_stop;
448 evapi.ref = ev_ref; 469 evapi.stat_start = ev_stat_start;
449 evapi.unref = ev_unref; 470 evapi.stat_stop = ev_stat_stop;
471 evapi.stat_stat = ev_stat_stat;
450 472
451 sv_setiv (sv, (IV)&evapi); 473 sv_setiv (sv, (IV)&evapi);
452 SvREADONLY_on (sv); 474 SvREADONLY_on (sv);
453 } 475 }
454#ifndef _WIN32 476#ifndef _WIN32
511 if (!ix) START (periodic, w); 533 if (!ix) START (periodic, w);
512} 534}
513 OUTPUT: 535 OUTPUT:
514 RETVAL 536 RETVAL
515 537
516ev_signal *signal (Signal signum, SV *cb) 538ev_signal *signal (SV *signal, SV *cb)
517 ALIAS: 539 ALIAS:
518 signal_ns = 1 540 signal_ns = 1
519 CODE: 541 CODE:
542{
543 Signal signum = sv_signum (signal);
544 CHECK_SIG (signal, signum);
545
520 RETVAL = e_new (sizeof (ev_signal), cb); 546 RETVAL = e_new (sizeof (ev_signal), cb);
521 ev_signal_set (RETVAL, signum); 547 ev_signal_set (RETVAL, signum);
522 if (!ix) START (signal, RETVAL); 548 if (!ix) START (signal, RETVAL);
549}
523 OUTPUT: 550 OUTPUT:
524 RETVAL 551 RETVAL
525 552
526ev_idle *idle (SV *cb) 553ev_idle *idle (SV *cb)
527 ALIAS: 554 ALIAS:
558 child_ns = 1 585 child_ns = 1
559 CODE: 586 CODE:
560 RETVAL = e_new (sizeof (ev_child), cb); 587 RETVAL = e_new (sizeof (ev_child), cb);
561 ev_child_set (RETVAL, pid); 588 ev_child_set (RETVAL, pid);
562 if (!ix) START (child, RETVAL); 589 if (!ix) START (child, RETVAL);
590 OUTPUT:
591 RETVAL
592
593ev_stat *stat (SV *path, NV interval, SV *cb)
594 ALIAS:
595 stat_ns = 1
596 CODE:
597 RETVAL = e_new (sizeof (ev_stat), cb);
598 RETVAL->fh = newSVsv (path);
599 ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval);
600 if (!ix) START (stat, RETVAL);
563 OUTPUT: 601 OUTPUT:
564 RETVAL 602 RETVAL
565 603
566void once (SV *fh, int events, SV *timeout, SV *cb) 604void once (SV *fh, int events, SV *timeout, SV *cb)
567 CODE: 605 CODE:
609 647
610SV *data (ev_watcher *w, SV *new_data = 0) 648SV *data (ev_watcher *w, SV *new_data = 0)
611 CODE: 649 CODE:
612{ 650{
613 RETVAL = w->data ? newSVsv (w->data) : &PL_sv_undef; 651 RETVAL = w->data ? newSVsv (w->data) : &PL_sv_undef;
652
653 if (items > 1)
654 {
655 SvREFCNT_dec (w->data);
656 w->data = newSVsv (new_data);
657 }
614} 658}
615 OUTPUT: 659 OUTPUT:
616 RETVAL 660 RETVAL
617 661
618void trigger (ev_watcher *w, int revents = EV_NONE) 662void trigger (ev_watcher *w, int revents = EV_NONE)
668 e_destroy (w); 712 e_destroy (w);
669 713
670void set (ev_io *w, SV *fh, int events) 714void set (ev_io *w, SV *fh, int events)
671 CODE: 715 CODE:
672{ 716{
673 int active = ev_is_active (w);
674 int fd = sv_fileno (fh); 717 int fd = sv_fileno (fh);
675 CHECK_FD (fh, fd); 718 CHECK_FD (fh, fd);
676 719
677 if (active) STOP (io, w);
678
679 sv_setsv (w->fh, fh); 720 sv_setsv (w->fh, fh);
680 ev_io_set (w, fd, events); 721 RESET (io, w, (w, fd, events));
681
682 if (active) START (io, w);
683} 722}
684 723
685SV *fh (ev_io *w, SV *new_fh = 0) 724SV *fh (ev_io *w, SV *new_fh = 0)
686 CODE: 725 CODE:
687{ 726{
688 RETVAL = newSVsv (w->fh);
689
690 if (items > 1) 727 if (items > 1)
691 { 728 {
692 int active = ev_is_active (w); 729 int fd = sv_fileno (new_fh);
693 if (active) STOP (io, w); 730 CHECK_FD (new_fh, fd);
694 731
695 sv_setsv (w->fh, new_fh); 732 RETVAL = w->fh;
696 ev_io_set (w, sv_fileno (w->fh), w->events); 733 w->fh = newSVsv (new_fh);
697 734
698 if (active) START (io, w); 735 RESET (io, w, (w, fd, w->events));
699 } 736 }
737 else
738 RETVAL = newSVsv (w->fh);
700} 739}
701 OUTPUT: 740 OUTPUT:
702 RETVAL 741 RETVAL
703 742
704int events (ev_io *w, int new_events = EV_UNDEF) 743int events (ev_io *w, int new_events = EV_UNDEF)
705 CODE: 744 CODE:
706{ 745{
707 RETVAL = w->events; 746 RETVAL = w->events;
747
748 if (items > 1)
749 RESET (io, w, (w, w->fd, new_events));
750}
751 OUTPUT:
752 RETVAL
753
754MODULE = EV PACKAGE = EV::Signal PREFIX = ev_signal_
755
756void ev_signal_start (ev_signal *w)
757 CODE:
758 START (signal, w);
759
760void ev_signal_stop (ev_signal *w)
761 CODE:
762 STOP (signal, w);
763
764void DESTROY (ev_signal *w)
765 CODE:
766 STOP (signal, w);
767 e_destroy (w);
768
769void set (ev_signal *w, SV *signal)
770 CODE:
771{
772 Signal signum = sv_signum (signal);
773 CHECK_SIG (signal, signum);
774
775 RESET (signal, w, (w, signum));
776}
777
778int signal (ev_signal *w, SV *new_signal = 0)
779 CODE:
780{
781 RETVAL = w->signum;
708 782
709 if (items > 1) 783 if (items > 1)
710 { 784 {
711 int active = ev_is_active (w);
712 if (active) STOP (io, w);
713
714 ev_io_set (w, w->fd, new_events);
715
716 if (active) START (io, w);
717 }
718}
719 OUTPUT:
720 RETVAL
721
722MODULE = EV PACKAGE = EV::Signal PREFIX = ev_signal_
723
724void ev_signal_start (ev_signal *w)
725 CODE:
726 START (signal, w);
727
728void ev_signal_stop (ev_signal *w)
729 CODE:
730 STOP (signal, w);
731
732void DESTROY (ev_signal *w)
733 CODE:
734 STOP (signal, w);
735 e_destroy (w);
736
737void set (ev_signal *w, SV *signal)
738 CODE:
739{
740 Signal signum = sv_signum (signal); /* may croak here */
741 int active = ev_is_active (w);
742
743 if (active) STOP (signal, w);
744
745 ev_signal_set (w, signum);
746
747 if (active) START (signal, w);
748}
749
750int signal (ev_signal *w, SV *new_signal = 0)
751 CODE:
752{
753 RETVAL = w->signum;
754
755 if (items > 1)
756 {
757 Signal signum = sv_signum (new_signal); /* may croak here */ 785 Signal signum = sv_signum (new_signal);
758 int active = ev_is_active (w); 786 CHECK_SIG (new_signal, signum);
759 if (active) STOP (signal, w);
760 787
761 ev_signal_set (w, signum); 788 RESET (signal, w, (w, signum));
762
763 if (active) START (signal, w);
764 } 789 }
765} 790}
766 OUTPUT: 791 OUTPUT:
767 RETVAL 792 RETVAL
768 793
793 818
794void set (ev_timer *w, NV after, NV repeat = 0.) 819void set (ev_timer *w, NV after, NV repeat = 0.)
795 INIT: 820 INIT:
796 CHECK_REPEAT (repeat); 821 CHECK_REPEAT (repeat);
797 CODE: 822 CODE:
798{
799 int active = ev_is_active (w);
800 if (active) STOP (timer, w);
801 ev_timer_set (w, after, repeat); 823 RESET (timer, w, (w, after, repeat));
802 if (active) START (timer, w);
803}
804 824
805MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_ 825MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_
806 826
807void ev_periodic_start (ev_periodic *w) 827void ev_periodic_start (ev_periodic *w)
808 INIT: 828 INIT:
828void set (ev_periodic *w, NV at, NV interval = 0., SV *reschedule_cb = &PL_sv_undef) 848void set (ev_periodic *w, NV at, NV interval = 0., SV *reschedule_cb = &PL_sv_undef)
829 INIT: 849 INIT:
830 CHECK_REPEAT (interval); 850 CHECK_REPEAT (interval);
831 CODE: 851 CODE:
832{ 852{
833 int active = ev_is_active (w);
834 if (active) STOP (periodic, w);
835
836 SvREFCNT_dec (w->fh); 853 SvREFCNT_dec (w->fh);
837 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0; 854 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
855
838 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0); 856 RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0));
839
840 if (active) START (periodic, w);
841} 857}
842 858
843MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 859MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
844 860
845void ev_idle_start (ev_idle *w) 861void ev_idle_start (ev_idle *w)
900 STOP (child, w); 916 STOP (child, w);
901 e_destroy (w); 917 e_destroy (w);
902 918
903void set (ev_child *w, int pid) 919void set (ev_child *w, int pid)
904 CODE: 920 CODE:
905{ 921 RESET (child, w, (w, pid));
906 int active = ev_is_active (w);
907 if (active) STOP (child, w);
908
909 ev_child_set (w, pid);
910
911 if (active) START (child, w);
912}
913 922
914int pid (ev_child *w, int new_pid = 0) 923int pid (ev_child *w, int new_pid = 0)
915 CODE: 924 CODE:
916{ 925{
917 RETVAL = w->pid; 926 RETVAL = w->pid;
918 927
919 if (items > 1) 928 if (items > 1)
920 { 929 RESET (child, w, (w, new_pid));
921 int active = ev_is_active (w);
922 if (active) STOP (child, w);
923
924 ev_child_set (w, new_pid);
925
926 if (active) START (child, w);
927 }
928} 930}
929 OUTPUT: 931 OUTPUT:
930 RETVAL 932 RETVAL
931 933
932 934
935 rpid = 1 937 rpid = 1
936 CODE: 938 CODE:
937 RETVAL = ix ? w->rpid : w->rstatus; 939 RETVAL = ix ? w->rpid : w->rstatus;
938 OUTPUT: 940 OUTPUT:
939 RETVAL 941 RETVAL
942
943MODULE = EV PACKAGE = EV::Stat PREFIX = ev_stat_
944
945void ev_stat_start (ev_stat *w)
946 CODE:
947 START (stat, w);
948
949void ev_stat_stop (ev_stat *w)
950 CODE:
951 STOP (stat, w);
952
953void DESTROY (ev_stat *w)
954 CODE:
955 STOP (stat, w);
956 e_destroy (w);
957
958void set (ev_stat *w, SV *path, NV interval)
959 CODE:
960{
961 sv_setsv (w->fh, path);
962 RESET (stat, w, (w, SvPVbyte_nolen (w->fh), interval));
963}
964
965SV *path (ev_stat *w, SV *new_path = 0)
966 CODE:
967{
968 RETVAL = SvREFCNT_inc (w->fh);
969
970 if (items > 1)
971 {
972 SvREFCNT_dec (w->fh);
973 w->fh = newSVsv (new_path);
974 RESET (stat, w, (w, SvPVbyte_nolen (w->fh), w->interval));
975 }
976}
977 OUTPUT:
978 RETVAL
979
980NV interval (ev_stat *w, NV new_interval = 0.)
981 CODE:
982{
983 RETVAL = w->interval;
984
985 if (items > 1)
986 RESET (stat, w, (w, SvPVbyte_nolen (w->fh), new_interval));
987}
988 OUTPUT:
989 RETVAL
990
991void prev (ev_stat *w)
992 ALIAS:
993 stat = 1
994 attr = 2
995 PPCODE:
996{
997 ev_statdata *s = ix ? &w->attr : &w->prev;
998
999 if (ix == 1)
1000 ev_stat_stat (w);
1001 else if (!s->st_nlink)
1002 errno = ENOENT;
1003
1004 PL_statcache.st_dev = s->st_nlink;
1005 PL_statcache.st_ino = s->st_ino;
1006 PL_statcache.st_mode = s->st_mode;
1007 PL_statcache.st_nlink = s->st_nlink;
1008 PL_statcache.st_uid = s->st_uid;
1009 PL_statcache.st_gid = s->st_gid;
1010 PL_statcache.st_rdev = s->st_rdev;
1011 PL_statcache.st_size = s->st_size;
1012 PL_statcache.st_atime = s->st_atime;
1013 PL_statcache.st_mtime = s->st_mtime;
1014 PL_statcache.st_ctime = s->st_ctime;
1015
1016 if (GIMME_V == G_SCALAR)
1017 XPUSHs (boolSV (s->st_nlink));
1018 else if (GIMME_V == G_ARRAY && s->st_nlink)
1019 {
1020 EXTEND (SP, 13);
1021 PUSHs (sv_2mortal (newSViv (s->st_dev)));
1022 PUSHs (sv_2mortal (newSViv (s->st_ino)));
1023 PUSHs (sv_2mortal (newSVuv (s->st_mode)));
1024 PUSHs (sv_2mortal (newSVuv (s->st_nlink)));
1025 PUSHs (sv_2mortal (newSViv (s->st_uid)));
1026 PUSHs (sv_2mortal (newSViv (s->st_gid)));
1027 PUSHs (sv_2mortal (newSViv (s->st_rdev)));
1028 PUSHs (sv_2mortal (newSVnv ((NV)s->st_size)));
1029 PUSHs (sv_2mortal (newSVnv (s->st_atime)));
1030 PUSHs (sv_2mortal (newSVnv (s->st_mtime)));
1031 PUSHs (sv_2mortal (newSVnv (s->st_ctime)));
1032 PUSHs (sv_2mortal (newSVuv (4096)));
1033 PUSHs (sv_2mortal (newSVnv ((NV)((s->st_size + 4095) / 4096))));
1034 }
1035}
940 1036
941#ifndef _WIN32 1037#ifndef _WIN32
942 1038
943MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 1039MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
944 1040

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines