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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines