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.142 by root, Tue Feb 13 19:21:29 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
342 CvPADLIST (cv) = (AV *)POPs; 365 CvPADLIST (cv) = (AV *)POPs;
343 } 366 }
344 367
345 PUTBACK; 368 PUTBACK;
346 } 369 }
370 assert (!PL_comppad || AvARRAY (PL_comppad));//D
347} 371}
348 372
349static void 373static void
350save_perl (Coro__State c) 374save_perl (Coro__State c)
351{ 375{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D
352 { 377 {
353 dSP; 378 dSP;
354 I32 cxix = cxstack_ix; 379 I32 cxix = cxstack_ix;
355 PERL_CONTEXT *ccstk = cxstack; 380 PERL_CONTEXT *ccstk = cxstack;
356 PERL_SI *top_si = PL_curstackinfo; 381 PERL_SI *top_si = PL_curstackinfo;
471 496
472 /* free all temporaries */ 497 /* free all temporaries */
473 FREETMPS; 498 FREETMPS;
474 assert (PL_tmps_ix == -1); 499 assert (PL_tmps_ix == -1);
475 500
501 /* unwind all extra stacks */
476 POPSTACK_TO (PL_mainstack); 502 POPSTACK_TO (PL_mainstack);
503
504 /* unwind main stack */
505 dounwind (-1);
477 } 506 }
478 507
479 while (PL_curstackinfo->si_next) 508 while (PL_curstackinfo->si_next)
480 PL_curstackinfo = PL_curstackinfo->si_next; 509 PL_curstackinfo = PL_curstackinfo->si_next;
481 510
498#if !PERL_VERSION_ATLEAST (5,9,0) 527#if !PERL_VERSION_ATLEAST (5,9,0)
499 Safefree (PL_retstack); 528 Safefree (PL_retstack);
500#endif 529#endif
501} 530}
502 531
532/** coroutine stack handling ************************************************/
533
503static void 534static void
504setup_coro (struct coro *coro) 535setup_coro (struct coro *coro)
505{ 536{
506 /* 537 /*
507 * emulate part of the perl startup here. 538 * emulate part of the perl startup here.
509 540
510 coro_init_stacks (); 541 coro_init_stacks ();
511 542
512 PL_curcop = &PL_compiling; 543 PL_curcop = &PL_compiling;
513 PL_in_eval = EVAL_NULL; 544 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0;
514 PL_curpm = 0; 546 PL_curpm = 0;
515 PL_localizing = 0; 547 PL_localizing = 0;
516 PL_dirty = 0; 548 PL_dirty = 0;
517 PL_restartop = 0; 549 PL_restartop = 0;
518 550
604 636
605 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 637 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
606 /* mmap supposedly does allocate-on-write for us */ 638 /* 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); 639 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
608 640
609 if (cctx->sptr == (void *)-1) 641 if (cctx->sptr != (void *)-1)
610 {
611 perror ("FATAL: unable to mmap stack for coroutine");
612 _exit (EXIT_FAILURE);
613 } 642 {
614
615# if STACKGUARD 643# if STACKGUARD
616 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 644 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
617# endif 645# endif
618 646 REGISTER_STACK (
619#else 647 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, 648 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
635 cctx->ssize + (char *)cctx->sptr 649 cctx->ssize + (char *)cctx->sptr
636 ); 650 );
637#endif
638 651
639 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); 652 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
653 }
654 else
655#endif
656 {
657 cctx->ssize = -STACKSIZE * (long)sizeof (long);
658 New (0, cctx->sptr, STACKSIZE, long);
659
660 if (!cctx->sptr)
661 {
662 perror ("FATAL: unable to allocate stack for coroutine");
663 _exit (EXIT_FAILURE);
664 }
665
666 REGISTER_STACK (
667 cctx,
668 (char *)cctx->sptr,
669 (char *)cctx->sptr - cctx->ssize
670 );
671
672 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize);
673 }
640 674
641 return cctx; 675 return cctx;
642} 676}
643 677
644static void 678static void
652#if USE_VALGRIND 686#if USE_VALGRIND
653 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 687 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
654#endif 688#endif
655 689
656#if HAVE_MMAP 690#if HAVE_MMAP
691 if (cctx->ssize > 0)
657 munmap (cctx->sptr, cctx->ssize); 692 munmap (cctx->sptr, cctx->ssize);
658#else 693 else
694#endif
659 Safefree (cctx->sptr); 695 Safefree (cctx->sptr);
660#endif
661 696
662 Safefree (cctx); 697 Safefree (cctx);
663} 698}
664 699
665static coro_cctx * 700static coro_cctx *
698 733
699 ++cctx_idle; 734 ++cctx_idle;
700 cctx->next = cctx_first; 735 cctx->next = cctx_first;
701 cctx_first = cctx; 736 cctx_first = cctx;
702} 737}
738
739/** coroutine switching *****************************************************/
703 740
704/* never call directly, always through the coro_state_transfer global variable */ 741/* never call directly, always through the coro_state_transfer global variable */
705static void NOINLINE 742static void NOINLINE
706transfer (struct coro *prev, struct coro *next) 743transfer (struct coro *prev, struct coro *next)
707{ 744{
730 if (!prev->flags & CF_RUNNING) 767 if (!prev->flags & CF_RUNNING)
731 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 768 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
732 769
733 if (next->flags & CF_RUNNING) 770 if (next->flags & CF_RUNNING)
734 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 771 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
772
773 if (next->flags & CF_DESTROYED)
774 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
735 775
736 prev->flags &= ~CF_RUNNING; 776 prev->flags &= ~CF_RUNNING;
737 next->flags |= CF_RUNNING; 777 next->flags |= CF_RUNNING;
738 778
739 LOCK; 779 LOCK;
783 PL_top_env = next->cctx->top_env; 823 PL_top_env = next->cctx->top_env;
784 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 824 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
785 } 825 }
786 826
787 free_coro_mortal (); 827 free_coro_mortal ();
788
789 UNLOCK; 828 UNLOCK;
790 } 829 }
791} 830}
792 831
793struct transfer_args 832struct transfer_args
795 struct coro *prev, *next; 834 struct coro *prev, *next;
796}; 835};
797 836
798#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 837#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
799 838
839/** high level stuff ********************************************************/
840
800static void 841static int
801coro_state_destroy (struct coro *coro) 842coro_state_destroy (struct coro *coro)
802{ 843{
803 if (coro->refcnt--) 844 if (coro->flags & CF_DESTROYED)
804 return; 845 return 0;
846
847 coro->flags |= CF_DESTROYED;
848
849 if (coro->flags & CF_READY)
850 {
851 /* reduce nready, as destroying a ready coro effectively unreadies it */
852 /* alternative: look through all ready queues and remove the coro */
853 LOCK;
854 --coro_nready;
855 UNLOCK;
856 }
857 else
858 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
805 859
806 if (coro->mainstack && coro->mainstack != main_mainstack) 860 if (coro->mainstack && coro->mainstack != main_mainstack)
807 { 861 {
808 struct coro temp; 862 struct coro temp;
863
864 assert (!(coro->flags & CF_RUNNING));
865
809 Zero (&temp, 1, struct coro); 866 Zero (&temp, 1, struct coro);
810 temp.save = CORO_SAVE_ALL; 867 temp.save = CORO_SAVE_ALL;
811 868
812 if (coro->flags & CF_RUNNING) 869 if (coro->flags & CF_RUNNING)
813 croak ("FATAL: tried to destroy currently running coroutine"); 870 croak ("FATAL: tried to destroy currently running coroutine");
822 coro->mainstack = 0; 879 coro->mainstack = 0;
823 } 880 }
824 881
825 cctx_destroy (coro->cctx); 882 cctx_destroy (coro->cctx);
826 SvREFCNT_dec (coro->args); 883 SvREFCNT_dec (coro->args);
827 Safefree (coro); 884
885 return 1;
828} 886}
829 887
830static int 888static int
831coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 889coro_state_free (pTHX_ SV *sv, MAGIC *mg)
832{ 890{
833 struct coro *coro = (struct coro *)mg->mg_ptr; 891 struct coro *coro = (struct coro *)mg->mg_ptr;
834 mg->mg_ptr = 0; 892 mg->mg_ptr = 0;
835 893
894 if (--coro->refcnt < 0)
895 {
836 coro_state_destroy (coro); 896 coro_state_destroy (coro);
897 Safefree (coro);
898 }
837 899
838 return 0; 900 return 0;
839} 901}
840 902
841static int 903static int
848 return 0; 910 return 0;
849} 911}
850 912
851static MGVTBL coro_state_vtbl = { 913static MGVTBL coro_state_vtbl = {
852 0, 0, 0, 0, 914 0, 0, 0, 0,
853 coro_state_clear, 915 coro_state_free,
854 0, 916 0,
855#ifdef MGf_DUP 917#ifdef MGf_DUP
856 coro_state_dup, 918 coro_state_dup,
857#else 919#else
858# define MGf_DUP 0 920# define MGf_DUP 0
909 return old_save; 971 return old_save;
910} 972}
911 973
912/** Coro ********************************************************************/ 974/** Coro ********************************************************************/
913 975
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 976static void
927coro_enq (SV *coro_sv) 977coro_enq (SV *coro_sv)
928{ 978{
929 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 979 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
930 coro_nready++;
931} 980}
932 981
933static SV * 982static SV *
934coro_deq (int min_prio) 983coro_deq (int min_prio)
935{ 984{
939 if (min_prio < 0) 988 if (min_prio < 0)
940 min_prio = 0; 989 min_prio = 0;
941 990
942 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 991 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
943 if (AvFILLp (coro_ready [prio]) >= 0) 992 if (AvFILLp (coro_ready [prio]) >= 0)
944 {
945 coro_nready--;
946 return av_shift (coro_ready [prio]); 993 return av_shift (coro_ready [prio]);
947 }
948 994
949 return 0; 995 return 0;
950} 996}
951 997
952static int 998static int
964 1010
965 coro->flags |= CF_READY; 1011 coro->flags |= CF_READY;
966 1012
967 LOCK; 1013 LOCK;
968 coro_enq (SvREFCNT_inc (coro_sv)); 1014 coro_enq (SvREFCNT_inc (coro_sv));
1015 ++coro_nready;
969 UNLOCK; 1016 UNLOCK;
970 1017
971 return 1; 1018 return 1;
972} 1019}
973 1020
978} 1025}
979 1026
980static void 1027static void
981prepare_schedule (struct transfer_args *ta) 1028prepare_schedule (struct transfer_args *ta)
982{ 1029{
983 SV *prev, *next; 1030 SV *prev_sv, *next_sv;
984 1031
985 for (;;) 1032 for (;;)
986 { 1033 {
987 LOCK; 1034 LOCK;
988 next = coro_deq (PRIO_MIN); 1035 next_sv = coro_deq (PRIO_MIN);
1036
1037 /* nothing to schedule: call the idle handler */
1038 if (!next_sv)
1039 {
1040 dSP;
1041 UNLOCK;
1042
1043 ENTER;
1044 SAVETMPS;
1045
1046 PUSHMARK (SP);
1047 PUTBACK;
1048 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1049
1050 FREETMPS;
1051 LEAVE;
1052 continue;
1053 }
1054
1055 ta->next = SvSTATE (next_sv);
1056
1057 /* cannot transfer to destroyed coros, skip and look for next */
1058 if (ta->next->flags & CF_DESTROYED)
1059 {
1060 UNLOCK;
1061 SvREFCNT_dec (next_sv);
1062 /* coro_nready is already taken care of by destroy */
1063 continue;
1064 }
1065
1066 --coro_nready;
989 UNLOCK; 1067 UNLOCK;
990
991 if (next)
992 break; 1068 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 } 1069 }
1007 }
1008
1009 prev = SvRV (coro_current);
1010 SvRV_set (coro_current, next);
1011 1070
1012 /* free this only after the transfer */ 1071 /* free this only after the transfer */
1072 prev_sv = SvRV (coro_current);
1073 SvRV_set (coro_current, next_sv);
1074 ta->prev = SvSTATE (prev_sv);
1075
1076 assert (ta->next->flags & CF_READY);
1077 ta->next->flags &= ~CF_READY;
1078
1013 LOCK; 1079 LOCK;
1014 free_coro_mortal (); 1080 free_coro_mortal ();
1081 coro_mortal = prev_sv;
1015 UNLOCK; 1082 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} 1083}
1027 1084
1028static void 1085static void
1029prepare_cede (struct transfer_args *ta) 1086prepare_cede (struct transfer_args *ta)
1030{ 1087{
1182 break; 1239 break;
1183 } 1240 }
1184 1241
1185 BARRIER; 1242 BARRIER;
1186 TRANSFER (ta); 1243 TRANSFER (ta);
1187}
1188 1244
1189void 1245 if (GIMME_V != G_VOID && ta.next != ta.prev)
1190_clone_state_from (SV *dst, SV *src) 1246 XSRETURN_YES;
1247}
1248
1249bool
1250_destroy (SV *coro_sv)
1191 CODE: 1251 CODE:
1192{ 1252 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1193 struct coro *coro_src = SvSTATE (src); 1253 OUTPUT:
1194 1254 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 1255
1201void 1256void
1202_exit (code) 1257_exit (code)
1203 int code 1258 int code
1204 PROTOTYPE: $ 1259 PROTOTYPE: $

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines