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

Comparing Coro/Coro/State.xs (file contents):
Revision 1.132 by root, Fri Dec 29 13:03:05 2006 UTC vs.
Revision 1.141 by root, Mon Jan 22 18:45:17 2007 UTC

33# define BOOT_PAGESIZE (void)0 33# define BOOT_PAGESIZE (void)0
34#endif 34#endif
35 35
36#if USE_VALGRIND 36#if USE_VALGRIND
37# include <valgrind/valgrind.h> 37# include <valgrind/valgrind.h>
38# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
39#else
40# define REGISTER_STACK(cctx,start,end)
38#endif 41#endif
39 42
40/* the maximum number of idle cctx that will be pooled */ 43/* the maximum number of idle cctx that will be pooled */
41#define MAX_IDLE_CCTX 8 44#define MAX_IDLE_CCTX 8
42 45
112#else 115#else
113# define LOCK (void)0 116# define LOCK (void)0
114# define UNLOCK (void)0 117# define UNLOCK (void)0
115#endif 118#endif
116 119
120/* helper storage struct for Coro::AIO */
117struct io_state 121struct io_state
118{ 122{
119 int errorno; 123 int errorno;
120 I32 laststype; 124 I32 laststype;
121 int laststatval; 125 int laststatval;
134typedef struct coro_cctx { 138typedef struct coro_cctx {
135 struct coro_cctx *next; 139 struct coro_cctx *next;
136 140
137 /* the stack */ 141 /* the stack */
138 void *sptr; 142 void *sptr;
139 long ssize; /* positive == mmap, otherwise malloc */ 143 ssize_t ssize; /* positive == mmap, otherwise malloc */
140 144
141 /* cpu state */ 145 /* cpu state */
142 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 146 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
143 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 147 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
144 JMPENV *top_env; 148 JMPENV *top_env;
150 int valgrind_id; 154 int valgrind_id;
151#endif 155#endif
152} coro_cctx; 156} coro_cctx;
153 157
154enum { 158enum {
155 CF_RUNNING = 0x0001, /* coroutine is running */ 159 CF_RUNNING = 0x0001, /* coroutine is running */
156 CF_READY = 0x0002, /* coroutine is ready */ 160 CF_READY = 0x0002, /* coroutine is ready */
157 CF_NEW = 0x0004, /* ahs never been switched to */ 161 CF_NEW = 0x0004, /* has never been switched to */
162 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
158}; 163};
159 164
160/* this is a structure representing a perl-level coroutine */ 165/* this is a structure representing a perl-level coroutine */
161struct coro { 166struct coro {
162 /* the c coroutine allocated to this perl coroutine, if any */ 167 /* the c coroutine allocated to this perl coroutine, if any */
183 int prio; 188 int prio;
184}; 189};
185 190
186typedef struct coro *Coro__State; 191typedef struct coro *Coro__State;
187typedef struct coro *Coro__State_or_hashref; 192typedef struct coro *Coro__State_or_hashref;
193
194/** Coro ********************************************************************/
195
196#define PRIO_MAX 3
197#define PRIO_HIGH 1
198#define PRIO_NORMAL 0
199#define PRIO_LOW -1
200#define PRIO_IDLE -3
201#define PRIO_MIN -4
202
203/* for Coro.pm */
204static SV *coro_current;
205static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
206static int coro_nready;
207
208/** lowlevel stuff **********************************************************/
188 209
189static AV * 210static AV *
190coro_clone_padlist (CV *cv) 211coro_clone_padlist (CV *cv)
191{ 212{
192 AV *padlist = CvPADLIST (cv); 213 AV *padlist = CvPADLIST (cv);
301 av_extend (av, AvMAX (av) + 1); 322 av_extend (av, AvMAX (av) + 1);
302 323
303 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 324 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
304} 325}
305 326
327/** load & save, init *******************************************************/
328
306#define SB do { 329#define SB do {
307#define SE } while (0) 330#define SE } while (0)
308 331
309#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
310 333
471 494
472 /* free all temporaries */ 495 /* free all temporaries */
473 FREETMPS; 496 FREETMPS;
474 assert (PL_tmps_ix == -1); 497 assert (PL_tmps_ix == -1);
475 498
499 /* unwind all extra stacks */
476 POPSTACK_TO (PL_mainstack); 500 POPSTACK_TO (PL_mainstack);
501
502 /* unwind main stack */
503 dounwind (-1);
477 } 504 }
478 505
479 while (PL_curstackinfo->si_next) 506 while (PL_curstackinfo->si_next)
480 PL_curstackinfo = PL_curstackinfo->si_next; 507 PL_curstackinfo = PL_curstackinfo->si_next;
481 508
497 Safefree (PL_savestack); 524 Safefree (PL_savestack);
498#if !PERL_VERSION_ATLEAST (5,9,0) 525#if !PERL_VERSION_ATLEAST (5,9,0)
499 Safefree (PL_retstack); 526 Safefree (PL_retstack);
500#endif 527#endif
501} 528}
529
530/** coroutine stack handling ************************************************/
502 531
503static void 532static void
504setup_coro (struct coro *coro) 533setup_coro (struct coro *coro)
505{ 534{
506 /* 535 /*
604 633
605 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 634 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
606 /* mmap supposedly does allocate-on-write for us */ 635 /* mmap supposedly does allocate-on-write for us */
607 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 636 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
608 637
609 if (cctx->sptr == (void *)-1) 638 if (cctx->sptr != (void *)-1)
610 {
611 perror ("FATAL: unable to mmap stack for coroutine");
612 _exit (EXIT_FAILURE);
613 } 639 {
614
615# if STACKGUARD 640# if STACKGUARD
616 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 641 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
617# endif 642# endif
618 643 REGISTER_STACK (
619#else 644 cctx,
620
621 cctx->ssize = STACKSIZE * (long)sizeof (long);
622 New (0, cctx->sptr, STACKSIZE, long);
623
624 if (!cctx->sptr)
625 {
626 perror ("FATAL: unable to malloc stack for coroutine");
627 _exit (EXIT_FAILURE);
628 }
629
630#endif
631
632#if USE_VALGRIND
633 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
634 STACKGUARD * PAGESIZE + (char *)cctx->sptr, 645 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
635 cctx->ssize + (char *)cctx->sptr 646 cctx->ssize + (char *)cctx->sptr
636 ); 647 );
637#endif
638 648
639 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); 649 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
650 }
651 else
652#endif
653 {
654 cctx->ssize = -STACKSIZE * (long)sizeof (long);
655 New (0, cctx->sptr, STACKSIZE, long);
656
657 if (!cctx->sptr)
658 {
659 perror ("FATAL: unable to allocate stack for coroutine");
660 _exit (EXIT_FAILURE);
661 }
662
663 REGISTER_STACK (
664 cctx,
665 (char *)cctx->sptr,
666 (char *)cctx->sptr - cctx->ssize
667 );
668
669 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize);
670 }
640 671
641 return cctx; 672 return cctx;
642} 673}
643 674
644static void 675static void
652#if USE_VALGRIND 683#if USE_VALGRIND
653 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 684 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
654#endif 685#endif
655 686
656#if HAVE_MMAP 687#if HAVE_MMAP
688 if (cctx->ssize > 0)
657 munmap (cctx->sptr, cctx->ssize); 689 munmap (cctx->sptr, cctx->ssize);
658#else 690 else
691#endif
659 Safefree (cctx->sptr); 692 Safefree (cctx->sptr);
660#endif
661 693
662 Safefree (cctx); 694 Safefree (cctx);
663} 695}
664 696
665static coro_cctx * 697static coro_cctx *
698 730
699 ++cctx_idle; 731 ++cctx_idle;
700 cctx->next = cctx_first; 732 cctx->next = cctx_first;
701 cctx_first = cctx; 733 cctx_first = cctx;
702} 734}
735
736/** coroutine switching *****************************************************/
703 737
704/* never call directly, always through the coro_state_transfer global variable */ 738/* never call directly, always through the coro_state_transfer global variable */
705static void NOINLINE 739static void NOINLINE
706transfer (struct coro *prev, struct coro *next) 740transfer (struct coro *prev, struct coro *next)
707{ 741{
730 if (!prev->flags & CF_RUNNING) 764 if (!prev->flags & CF_RUNNING)
731 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 765 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
732 766
733 if (next->flags & CF_RUNNING) 767 if (next->flags & CF_RUNNING)
734 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 768 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
769
770 if (next->flags & CF_DESTROYED)
771 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
735 772
736 prev->flags &= ~CF_RUNNING; 773 prev->flags &= ~CF_RUNNING;
737 next->flags |= CF_RUNNING; 774 next->flags |= CF_RUNNING;
738 775
739 LOCK; 776 LOCK;
783 PL_top_env = next->cctx->top_env; 820 PL_top_env = next->cctx->top_env;
784 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 821 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
785 } 822 }
786 823
787 free_coro_mortal (); 824 free_coro_mortal ();
788
789 UNLOCK; 825 UNLOCK;
790 } 826 }
791} 827}
792 828
793struct transfer_args 829struct transfer_args
795 struct coro *prev, *next; 831 struct coro *prev, *next;
796}; 832};
797 833
798#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 834#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
799 835
836/** high level stuff ********************************************************/
837
800static void 838static int
801coro_state_destroy (struct coro *coro) 839coro_state_destroy (struct coro *coro)
802{ 840{
803 if (coro->refcnt--) 841 if (coro->flags & CF_DESTROYED)
804 return; 842 return 0;
843
844 coro->flags |= CF_DESTROYED;
845
846 if (coro->flags & CF_READY)
847 {
848 /* reduce nready, as destroying a ready coro effectively unreadies it */
849 /* alternative: look through all ready queues and remove the coro */
850 LOCK;
851 --coro_nready;
852 UNLOCK;
853 }
854 else
855 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
805 856
806 if (coro->mainstack && coro->mainstack != main_mainstack) 857 if (coro->mainstack && coro->mainstack != main_mainstack)
807 { 858 {
808 struct coro temp; 859 struct coro temp;
860
861 assert (!(coro->flags & CF_RUNNING));
862
809 Zero (&temp, 1, struct coro); 863 Zero (&temp, 1, struct coro);
810 temp.save = CORO_SAVE_ALL; 864 temp.save = CORO_SAVE_ALL;
811 865
812 if (coro->flags & CF_RUNNING) 866 if (coro->flags & CF_RUNNING)
813 croak ("FATAL: tried to destroy currently running coroutine"); 867 croak ("FATAL: tried to destroy currently running coroutine");
822 coro->mainstack = 0; 876 coro->mainstack = 0;
823 } 877 }
824 878
825 cctx_destroy (coro->cctx); 879 cctx_destroy (coro->cctx);
826 SvREFCNT_dec (coro->args); 880 SvREFCNT_dec (coro->args);
827 Safefree (coro); 881
882 return 1;
828} 883}
829 884
830static int 885static int
831coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 886coro_state_free (pTHX_ SV *sv, MAGIC *mg)
832{ 887{
833 struct coro *coro = (struct coro *)mg->mg_ptr; 888 struct coro *coro = (struct coro *)mg->mg_ptr;
834 mg->mg_ptr = 0; 889 mg->mg_ptr = 0;
835 890
891 if (--coro->refcnt < 0)
892 {
836 coro_state_destroy (coro); 893 coro_state_destroy (coro);
894 Safefree (coro);
895 }
837 896
838 return 0; 897 return 0;
839} 898}
840 899
841static int 900static int
848 return 0; 907 return 0;
849} 908}
850 909
851static MGVTBL coro_state_vtbl = { 910static MGVTBL coro_state_vtbl = {
852 0, 0, 0, 0, 911 0, 0, 0, 0,
853 coro_state_clear, 912 coro_state_free,
854 0, 913 0,
855#ifdef MGf_DUP 914#ifdef MGf_DUP
856 coro_state_dup, 915 coro_state_dup,
857#else 916#else
858# define MGf_DUP 0 917# define MGf_DUP 0
909 return old_save; 968 return old_save;
910} 969}
911 970
912/** Coro ********************************************************************/ 971/** Coro ********************************************************************/
913 972
914#define PRIO_MAX 3
915#define PRIO_HIGH 1
916#define PRIO_NORMAL 0
917#define PRIO_LOW -1
918#define PRIO_IDLE -3
919#define PRIO_MIN -4
920
921/* for Coro.pm */
922static SV *coro_current;
923static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
924static int coro_nready;
925
926static void 973static void
927coro_enq (SV *coro_sv) 974coro_enq (SV *coro_sv)
928{ 975{
929 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 976 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
930 coro_nready++;
931} 977}
932 978
933static SV * 979static SV *
934coro_deq (int min_prio) 980coro_deq (int min_prio)
935{ 981{
939 if (min_prio < 0) 985 if (min_prio < 0)
940 min_prio = 0; 986 min_prio = 0;
941 987
942 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 988 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
943 if (AvFILLp (coro_ready [prio]) >= 0) 989 if (AvFILLp (coro_ready [prio]) >= 0)
944 {
945 coro_nready--;
946 return av_shift (coro_ready [prio]); 990 return av_shift (coro_ready [prio]);
947 }
948 991
949 return 0; 992 return 0;
950} 993}
951 994
952static int 995static int
964 1007
965 coro->flags |= CF_READY; 1008 coro->flags |= CF_READY;
966 1009
967 LOCK; 1010 LOCK;
968 coro_enq (SvREFCNT_inc (coro_sv)); 1011 coro_enq (SvREFCNT_inc (coro_sv));
1012 ++coro_nready;
969 UNLOCK; 1013 UNLOCK;
970 1014
971 return 1; 1015 return 1;
972} 1016}
973 1017
978} 1022}
979 1023
980static void 1024static void
981prepare_schedule (struct transfer_args *ta) 1025prepare_schedule (struct transfer_args *ta)
982{ 1026{
983 SV *prev, *next; 1027 SV *prev_sv, *next_sv;
984 1028
985 for (;;) 1029 for (;;)
986 { 1030 {
987 LOCK; 1031 LOCK;
988 next = coro_deq (PRIO_MIN); 1032 next_sv = coro_deq (PRIO_MIN);
1033
1034 /* nothing to schedule: call the idle handler */
1035 if (!next_sv)
1036 {
1037 dSP;
1038 UNLOCK;
1039
1040 ENTER;
1041 SAVETMPS;
1042
1043 PUSHMARK (SP);
1044 PUTBACK;
1045 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1046
1047 FREETMPS;
1048 LEAVE;
1049 continue;
1050 }
1051
1052 ta->next = SvSTATE (next_sv);
1053
1054 /* cannot transfer to destroyed coros, skip and look for next */
1055 if (ta->next->flags & CF_DESTROYED)
1056 {
1057 UNLOCK;
1058 SvREFCNT_dec (next_sv);
1059 /* coro_nready is already taken care of by destroy */
1060 continue;
1061 }
1062
1063 --coro_nready;
989 UNLOCK; 1064 UNLOCK;
990
991 if (next)
992 break; 1065 break;
993
994 {
995 dSP;
996
997 ENTER;
998 SAVETMPS;
999
1000 PUSHMARK (SP);
1001 PUTBACK;
1002 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1003
1004 FREETMPS;
1005 LEAVE;
1006 } 1066 }
1007 }
1008
1009 prev = SvRV (coro_current);
1010 SvRV_set (coro_current, next);
1011 1067
1012 /* free this only after the transfer */ 1068 /* free this only after the transfer */
1069 prev_sv = SvRV (coro_current);
1070 SvRV_set (coro_current, next_sv);
1071 ta->prev = SvSTATE (prev_sv);
1072
1073 assert (ta->next->flags & CF_READY);
1074 ta->next->flags &= ~CF_READY;
1075
1013 LOCK; 1076 LOCK;
1014 free_coro_mortal (); 1077 free_coro_mortal ();
1078 coro_mortal = prev_sv;
1015 UNLOCK; 1079 UNLOCK;
1016 coro_mortal = prev;
1017
1018 assert (!SvROK(prev));//D
1019 assert (!SvROK(next));//D
1020
1021 ta->prev = SvSTATE (prev);
1022 ta->next = SvSTATE (next);
1023
1024 assert (ta->next->flags & CF_READY);
1025 ta->next->flags &= ~CF_READY;
1026} 1080}
1027 1081
1028static void 1082static void
1029prepare_cede (struct transfer_args *ta) 1083prepare_cede (struct transfer_args *ta)
1030{ 1084{
1182 break; 1236 break;
1183 } 1237 }
1184 1238
1185 BARRIER; 1239 BARRIER;
1186 TRANSFER (ta); 1240 TRANSFER (ta);
1187}
1188 1241
1189void 1242 if (GIMME_V != G_VOID && ta.next != ta.prev)
1190_clone_state_from (SV *dst, SV *src) 1243 XSRETURN_YES;
1244}
1245
1246bool
1247_destroy (SV *coro_sv)
1191 CODE: 1248 CODE:
1192{ 1249 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1193 struct coro *coro_src = SvSTATE (src); 1250 OUTPUT:
1194 1251 RETVAL
1195 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1196
1197 ++coro_src->refcnt;
1198 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1199}
1200 1252
1201void 1253void
1202_exit (code) 1254_exit (code)
1203 int code 1255 int code
1204 PROTOTYPE: $ 1256 PROTOTYPE: $

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines