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

Comparing EV/EV.xs (file contents):
Revision 1.79 by root, Sat Nov 24 16:57:30 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:
674 e_destroy (w); 712 e_destroy (w);
675 713
676void set (ev_io *w, SV *fh, int events) 714void set (ev_io *w, SV *fh, int events)
677 CODE: 715 CODE:
678{ 716{
679 int active = ev_is_active (w);
680 int fd = sv_fileno (fh); 717 int fd = sv_fileno (fh);
681 CHECK_FD (fh, fd); 718 CHECK_FD (fh, fd);
682 719
683 if (active) STOP (io, w);
684
685 sv_setsv (w->fh, fh); 720 sv_setsv (w->fh, fh);
686 ev_io_set (w, fd, events); 721 RESET (io, w, (w, fd, events));
687
688 if (active) START (io, w);
689} 722}
690 723
691SV *fh (ev_io *w, SV *new_fh = 0) 724SV *fh (ev_io *w, SV *new_fh = 0)
692 CODE: 725 CODE:
693{ 726{
694 RETVAL = newSVsv (w->fh);
695
696 if (items > 1) 727 if (items > 1)
697 { 728 {
698 int active = ev_is_active (w); 729 int fd = sv_fileno (new_fh);
699 if (active) STOP (io, w); 730 CHECK_FD (new_fh, fd);
700 731
701 sv_setsv (w->fh, new_fh); 732 RETVAL = w->fh;
702 ev_io_set (w, sv_fileno (w->fh), w->events); 733 w->fh = newSVsv (new_fh);
703 734
704 if (active) START (io, w); 735 RESET (io, w, (w, fd, w->events));
705 } 736 }
737 else
738 RETVAL = newSVsv (w->fh);
706} 739}
707 OUTPUT: 740 OUTPUT:
708 RETVAL 741 RETVAL
709 742
710int events (ev_io *w, int new_events = EV_UNDEF) 743int events (ev_io *w, int new_events = EV_UNDEF)
711 CODE: 744 CODE:
712{ 745{
713 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;
714 782
715 if (items > 1) 783 if (items > 1)
716 { 784 {
717 int active = ev_is_active (w);
718 if (active) STOP (io, w);
719
720 ev_io_set (w, w->fd, new_events);
721
722 if (active) START (io, w);
723 }
724}
725 OUTPUT:
726 RETVAL
727
728MODULE = EV PACKAGE = EV::Signal PREFIX = ev_signal_
729
730void ev_signal_start (ev_signal *w)
731 CODE:
732 START (signal, w);
733
734void ev_signal_stop (ev_signal *w)
735 CODE:
736 STOP (signal, w);
737
738void DESTROY (ev_signal *w)
739 CODE:
740 STOP (signal, w);
741 e_destroy (w);
742
743void set (ev_signal *w, SV *signal)
744 CODE:
745{
746 Signal signum = sv_signum (signal); /* may croak here */
747 int active = ev_is_active (w);
748
749 if (active) STOP (signal, w);
750
751 ev_signal_set (w, signum);
752
753 if (active) START (signal, w);
754}
755
756int signal (ev_signal *w, SV *new_signal = 0)
757 CODE:
758{
759 RETVAL = w->signum;
760
761 if (items > 1)
762 {
763 Signal signum = sv_signum (new_signal); /* may croak here */ 785 Signal signum = sv_signum (new_signal);
764 int active = ev_is_active (w); 786 CHECK_SIG (new_signal, signum);
765 if (active) STOP (signal, w);
766 787
767 ev_signal_set (w, signum); 788 RESET (signal, w, (w, signum));
768
769 if (active) START (signal, w);
770 } 789 }
771} 790}
772 OUTPUT: 791 OUTPUT:
773 RETVAL 792 RETVAL
774 793
799 818
800void set (ev_timer *w, NV after, NV repeat = 0.) 819void set (ev_timer *w, NV after, NV repeat = 0.)
801 INIT: 820 INIT:
802 CHECK_REPEAT (repeat); 821 CHECK_REPEAT (repeat);
803 CODE: 822 CODE:
804{
805 int active = ev_is_active (w);
806 if (active) STOP (timer, w);
807 ev_timer_set (w, after, repeat); 823 RESET (timer, w, (w, after, repeat));
808 if (active) START (timer, w);
809}
810 824
811MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_ 825MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_
812 826
813void ev_periodic_start (ev_periodic *w) 827void ev_periodic_start (ev_periodic *w)
814 INIT: 828 INIT:
834void 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)
835 INIT: 849 INIT:
836 CHECK_REPEAT (interval); 850 CHECK_REPEAT (interval);
837 CODE: 851 CODE:
838{ 852{
839 int active = ev_is_active (w);
840 if (active) STOP (periodic, w);
841
842 SvREFCNT_dec (w->fh); 853 SvREFCNT_dec (w->fh);
843 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0; 854 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
855
844 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));
845
846 if (active) START (periodic, w);
847} 857}
848 858
849MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 859MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
850 860
851void ev_idle_start (ev_idle *w) 861void ev_idle_start (ev_idle *w)
906 STOP (child, w); 916 STOP (child, w);
907 e_destroy (w); 917 e_destroy (w);
908 918
909void set (ev_child *w, int pid) 919void set (ev_child *w, int pid)
910 CODE: 920 CODE:
911{ 921 RESET (child, w, (w, pid));
912 int active = ev_is_active (w);
913 if (active) STOP (child, w);
914
915 ev_child_set (w, pid);
916
917 if (active) START (child, w);
918}
919 922
920int pid (ev_child *w, int new_pid = 0) 923int pid (ev_child *w, int new_pid = 0)
921 CODE: 924 CODE:
922{ 925{
923 RETVAL = w->pid; 926 RETVAL = w->pid;
924 927
925 if (items > 1) 928 if (items > 1)
926 { 929 RESET (child, w, (w, new_pid));
927 int active = ev_is_active (w);
928 if (active) STOP (child, w);
929
930 ev_child_set (w, new_pid);
931
932 if (active) START (child, w);
933 }
934} 930}
935 OUTPUT: 931 OUTPUT:
936 RETVAL 932 RETVAL
937 933
938 934
941 rpid = 1 937 rpid = 1
942 CODE: 938 CODE:
943 RETVAL = ix ? w->rpid : w->rstatus; 939 RETVAL = ix ? w->rpid : w->rstatus;
944 OUTPUT: 940 OUTPUT:
945 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}
946 1036
947#ifndef _WIN32 1037#ifndef _WIN32
948 1038
949MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 1039MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
950 1040

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines