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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines