… | |
… | |
130 | int valgrind_id; |
130 | int valgrind_id; |
131 | #endif |
131 | #endif |
132 | } coro_cctx; |
132 | } coro_cctx; |
133 | |
133 | |
134 | enum { |
134 | enum { |
135 | CF_RUNNING, /* coroutine is running */ |
135 | CF_RUNNING = 0x0001, /* coroutine is running */ |
136 | CF_READY, /* coroutine is ready */ |
136 | CF_READY = 0x0002, /* coroutine is ready */ |
|
|
137 | CF_NEW = 0x0004, /* ahs never been switched to */ |
137 | }; |
138 | }; |
138 | |
139 | |
139 | /* this is a structure representing a perl-level coroutine */ |
140 | /* this is a structure representing a perl-level coroutine */ |
140 | struct coro { |
141 | struct coro { |
141 | /* the c coroutine allocated to this perl coroutine, if any */ |
142 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
280 | } |
281 | } |
281 | |
282 | |
282 | #define SB do { |
283 | #define SB do { |
283 | #define SE } while (0) |
284 | #define SE } while (0) |
284 | |
285 | |
285 | #define LOAD(state) load_state((state)); |
286 | #define LOAD(state) load_state ((state)) |
286 | #define SAVE(state,flags) save_state((state),(flags)); |
287 | #define SAVE(state,flags) save_state ((state),(flags)) |
287 | |
288 | |
288 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
289 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
289 | |
290 | |
290 | static void |
291 | static void |
291 | load_state(Coro__State c) |
292 | load_state (Coro__State c) |
292 | { |
293 | { |
293 | #define VAR(name,type) PL_ ## name = c->name; |
294 | #define VAR(name,type) PL_ ## name = c->name; |
294 | # include "state.h" |
295 | # include "state.h" |
295 | #undef VAR |
296 | #undef VAR |
296 | |
297 | |
… | |
… | |
313 | PUTBACK; |
314 | PUTBACK; |
314 | } |
315 | } |
315 | } |
316 | } |
316 | |
317 | |
317 | static void |
318 | static void |
318 | save_state(Coro__State c, int flags) |
319 | save_state (Coro__State c, int flags) |
319 | { |
320 | { |
320 | { |
321 | { |
321 | dSP; |
322 | dSP; |
322 | I32 cxix = cxstack_ix; |
323 | I32 cxix = cxstack_ix; |
323 | PERL_CONTEXT *ccstk = cxstack; |
324 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
334 | { |
335 | { |
335 | while (cxix >= 0) |
336 | while (cxix >= 0) |
336 | { |
337 | { |
337 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
338 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
338 | |
339 | |
339 | if (CxTYPE(cx) == CXt_SUB) |
340 | if (CxTYPE (cx) == CXt_SUB) |
340 | { |
341 | { |
341 | CV *cv = cx->blk_sub.cv; |
342 | CV *cv = cx->blk_sub.cv; |
342 | |
343 | |
343 | if (CvDEPTH (cv)) |
344 | if (CvDEPTH (cv)) |
344 | { |
345 | { |
345 | EXTEND (SP, 3); |
346 | EXTEND (SP, 3); |
346 | |
347 | |
347 | PUSHs ((SV *)CvPADLIST(cv)); |
348 | PUSHs ((SV *)CvPADLIST (cv)); |
348 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
349 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
349 | PUSHs ((SV *)cv); |
350 | PUSHs ((SV *)cv); |
350 | |
351 | |
351 | CvDEPTH (cv) = 0; |
352 | CvDEPTH (cv) = 0; |
352 | get_padlist (cv); |
353 | get_padlist (cv); |
353 | } |
354 | } |
354 | } |
355 | } |
355 | #ifdef CXt_FORMAT |
356 | #ifdef CXt_FORMAT |
356 | else if (CxTYPE(cx) == CXt_FORMAT) |
357 | else if (CxTYPE (cx) == CXt_FORMAT) |
357 | { |
358 | { |
358 | /* I never used formats, so how should I know how these are implemented? */ |
359 | /* I never used formats, so how should I know how these are implemented? */ |
359 | /* my bold guess is as a simple, plain sub... */ |
360 | /* my bold guess is as a simple, plain sub... */ |
360 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
361 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
361 | } |
362 | } |
… | |
… | |
485 | * emulate part of the perl startup here. |
486 | * emulate part of the perl startup here. |
486 | */ |
487 | */ |
487 | |
488 | |
488 | coro_init_stacks (); |
489 | coro_init_stacks (); |
489 | |
490 | |
490 | PL_curcop = 0; |
491 | PL_curcop = &PL_compiling; |
491 | PL_in_eval = 0; |
492 | PL_in_eval = EVAL_NULL; |
492 | PL_curpm = 0; |
493 | PL_curpm = 0; |
|
|
494 | PL_localizing = 0; |
|
|
495 | PL_dirty = 0; |
|
|
496 | PL_restartop = 0; |
493 | |
497 | |
494 | { |
498 | { |
495 | dSP; |
499 | dSP; |
496 | LOGOP myop; |
500 | LOGOP myop; |
497 | |
501 | |
… | |
… | |
510 | PUSHMARK (SP); |
514 | PUSHMARK (SP); |
511 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
515 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
512 | PUTBACK; |
516 | PUTBACK; |
513 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
517 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
514 | SPAGAIN; |
518 | SPAGAIN; |
515 | |
|
|
516 | ENTER; /* necessary e.g. for dounwind */ |
|
|
517 | } |
519 | } |
|
|
520 | |
|
|
521 | ENTER; /* necessary e.g. for dounwind */ |
518 | } |
522 | } |
519 | |
523 | |
520 | static void |
524 | static void |
521 | free_coro_mortal () |
525 | free_coro_mortal () |
522 | { |
526 | { |
… | |
… | |
688 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
692 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
689 | else if (prev != next) |
693 | else if (prev != next) |
690 | { |
694 | { |
691 | coro_cctx *prev__cctx; |
695 | coro_cctx *prev__cctx; |
692 | |
696 | |
693 | if (!prev->cctx) |
697 | if (prev->flags & CF_NEW) |
694 | { |
698 | { |
695 | /* create a new empty context */ |
699 | /* create a new empty context */ |
696 | Newz (0, prev->cctx, 1, coro_cctx); |
700 | Newz (0, prev->cctx, 1, coro_cctx); |
697 | prev->cctx->inuse = 1; |
701 | prev->cctx->inuse = 1; |
|
|
702 | prev->flags &= ~CF_NEW; |
698 | prev->flags |= CF_RUNNING; |
703 | prev->flags |= CF_RUNNING; |
699 | } |
704 | } |
700 | |
705 | |
|
|
706 | /*TODO: must not croak here */ |
701 | if (!prev->flags & CF_RUNNING) |
707 | if (!prev->flags & CF_RUNNING) |
702 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
708 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
703 | |
709 | |
704 | if (next->flags & CF_RUNNING) |
710 | if (next->flags & CF_RUNNING) |
705 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
711 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
… | |
… | |
716 | LOAD (next); |
722 | LOAD (next); |
717 | } |
723 | } |
718 | else |
724 | else |
719 | { |
725 | { |
720 | /* need to start coroutine */ |
726 | /* need to start coroutine */ |
|
|
727 | assert (next->flags & CF_NEW); |
|
|
728 | next->flags &= ~CF_NEW; |
721 | /* first get rid of the old state */ |
729 | /* first get rid of the old state */ |
722 | SAVE (prev, -1); |
730 | SAVE (prev, -1); |
723 | /* setup coroutine call */ |
731 | /* setup coroutine call */ |
724 | setup_coro (next); |
732 | setup_coro (next); |
725 | /* need a new stack */ |
733 | /* need a new stack */ |
… | |
… | |
727 | } |
735 | } |
728 | |
736 | |
729 | prev__cctx = prev->cctx; |
737 | prev__cctx = prev->cctx; |
730 | |
738 | |
731 | /* possibly "free" the cctx */ |
739 | /* possibly "free" the cctx */ |
732 | if (prev__cctx->idle_sp == STACKLEVEL) |
740 | if (prev__cctx->idle_sp == STACKLEVEL && 0) |
733 | { |
741 | { |
734 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
742 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
735 | assert (PL_top_env == prev__cctx->top_env); |
743 | assert (PL_top_env == prev__cctx->top_env); |
736 | |
744 | |
|
|
745 | prev->cctx = 0; |
|
|
746 | |
737 | cctx_put (prev__cctx); |
747 | cctx_put (prev__cctx); |
738 | prev->cctx = 0; |
748 | prev__cctx->inuse = 0; |
739 | } |
749 | } |
740 | |
750 | |
741 | if (!next->cctx) |
751 | if (!next->cctx) |
|
|
752 | { |
742 | next->cctx = cctx_get (); |
753 | next->cctx = cctx_get (); |
|
|
754 | assert (!next->cctx->inuse); |
|
|
755 | next->cctx->inuse = 1; |
|
|
756 | } |
743 | |
757 | |
744 | if (prev__cctx != next->cctx) |
758 | if (prev__cctx != next->cctx) |
745 | { |
759 | { |
746 | assert ( prev__cctx->inuse); |
|
|
747 | assert (!next->cctx->inuse); |
|
|
748 | |
|
|
749 | prev__cctx->inuse = 0; |
|
|
750 | next->cctx->inuse = 1; |
|
|
751 | |
|
|
752 | prev__cctx->top_env = PL_top_env; |
760 | prev__cctx->top_env = PL_top_env; |
753 | PL_top_env = next->cctx->top_env; |
761 | PL_top_env = next->cctx->top_env; |
754 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
762 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
755 | } |
763 | } |
756 | |
764 | |
… | |
… | |
772 | coro_state_destroy (struct coro *coro) |
780 | coro_state_destroy (struct coro *coro) |
773 | { |
781 | { |
774 | if (coro->refcnt--) |
782 | if (coro->refcnt--) |
775 | return; |
783 | return; |
776 | |
784 | |
777 | if (coro->flags & CF_RUNNING) |
|
|
778 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
779 | |
|
|
780 | if (coro->mainstack && coro->mainstack != main_mainstack) |
785 | if (coro->mainstack && coro->mainstack != main_mainstack) |
781 | { |
786 | { |
782 | struct coro temp; |
787 | struct coro temp; |
|
|
788 | |
|
|
789 | if (coro->flags & CF_RUNNING) |
|
|
790 | croak ("FATAL: tried to destroy currently running coroutine"); |
783 | |
791 | |
784 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
792 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
785 | LOAD (coro); |
793 | LOAD (coro); |
786 | |
794 | |
787 | coro_destroy_stacks (); |
795 | coro_destroy_stacks (); |
… | |
… | |
859 | } |
867 | } |
860 | |
868 | |
861 | static void |
869 | static void |
862 | api_transfer (SV *prev, SV *next, int flags) |
870 | api_transfer (SV *prev, SV *next, int flags) |
863 | { |
871 | { |
864 | dTHX; |
|
|
865 | struct transfer_args ta; |
872 | struct transfer_args ta; |
866 | |
873 | |
867 | prepare_transfer (&ta, prev, next, flags); |
874 | prepare_transfer (&ta, prev, next, flags); |
868 | TRANSFER (ta); |
875 | TRANSFER (ta); |
869 | } |
876 | } |
… | |
… | |
876 | #define PRIO_LOW -1 |
883 | #define PRIO_LOW -1 |
877 | #define PRIO_IDLE -3 |
884 | #define PRIO_IDLE -3 |
878 | #define PRIO_MIN -4 |
885 | #define PRIO_MIN -4 |
879 | |
886 | |
880 | /* for Coro.pm */ |
887 | /* for Coro.pm */ |
881 | static GV *coro_current, *coro_idle; |
888 | static SV *coro_current; |
882 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
889 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
883 | static int coro_nready; |
890 | static int coro_nready; |
884 | |
891 | |
885 | static void |
892 | static void |
886 | coro_enq (SV *coro_sv) |
893 | coro_enq (SV *coro_sv) |
… | |
… | |
919 | coro = SvSTATE (coro_sv); |
926 | coro = SvSTATE (coro_sv); |
920 | |
927 | |
921 | if (coro->flags & CF_READY) |
928 | if (coro->flags & CF_READY) |
922 | return 0; |
929 | return 0; |
923 | |
930 | |
|
|
931 | #if 0 /* this is actually harmless */ |
924 | if (coro->flags & CF_RUNNING) |
932 | if (coro->flags & CF_RUNNING) |
925 | croak ("Coro::ready called on currently running coroutine"); |
933 | croak ("Coro::ready called on currently running coroutine"); |
|
|
934 | #endif |
926 | |
935 | |
927 | coro->flags |= CF_READY; |
936 | coro->flags |= CF_READY; |
928 | |
937 | |
929 | LOCK; |
938 | LOCK; |
930 | coro_enq (SvREFCNT_inc (coro_sv)); |
939 | coro_enq (SvREFCNT_inc (coro_sv)); |
… | |
… | |
940 | } |
949 | } |
941 | |
950 | |
942 | static void |
951 | static void |
943 | prepare_schedule (struct transfer_args *ta) |
952 | prepare_schedule (struct transfer_args *ta) |
944 | { |
953 | { |
945 | SV *current, *prev, *next; |
954 | SV *prev, *next; |
946 | |
|
|
947 | current = GvSV (coro_current); |
|
|
948 | |
955 | |
949 | for (;;) |
956 | for (;;) |
950 | { |
957 | { |
951 | LOCK; |
958 | LOCK; |
952 | next = coro_deq (PRIO_MIN); |
959 | next = coro_deq (PRIO_MIN); |
… | |
… | |
961 | ENTER; |
968 | ENTER; |
962 | SAVETMPS; |
969 | SAVETMPS; |
963 | |
970 | |
964 | PUSHMARK (SP); |
971 | PUSHMARK (SP); |
965 | PUTBACK; |
972 | PUTBACK; |
966 | call_sv (GvSV (coro_idle), G_DISCARD); |
973 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
967 | |
974 | |
968 | FREETMPS; |
975 | FREETMPS; |
969 | LEAVE; |
976 | LEAVE; |
970 | } |
977 | } |
971 | } |
978 | } |
972 | |
979 | |
973 | prev = SvRV (current); |
980 | prev = SvRV (coro_current); |
974 | SvRV (current) = next; |
981 | SvRV_set (coro_current, next); |
975 | |
982 | |
976 | /* free this only after the transfer */ |
983 | /* free this only after the transfer */ |
977 | LOCK; |
984 | LOCK; |
978 | free_coro_mortal (); |
985 | free_coro_mortal (); |
979 | UNLOCK; |
986 | UNLOCK; |
980 | coro_mortal = prev; |
987 | coro_mortal = prev; |
981 | |
988 | |
|
|
989 | assert (!SvROK(prev));//D |
|
|
990 | assert (!SvROK(next));//D |
|
|
991 | |
982 | ta->prev = SvSTATE (prev); |
992 | ta->prev = SvSTATE (prev); |
983 | ta->next = SvSTATE (next); |
993 | ta->next = SvSTATE (next); |
984 | ta->flags = TRANSFER_SAVE_ALL; |
994 | ta->flags = TRANSFER_SAVE_ALL; |
985 | |
995 | |
|
|
996 | assert (ta->flags & CF_READY); |
986 | ta->next->flags &= ~CF_READY; |
997 | ta->next->flags &= ~CF_READY; |
987 | } |
998 | } |
988 | |
999 | |
989 | static void |
1000 | static void |
990 | prepare_cede (struct transfer_args *ta) |
1001 | prepare_cede (struct transfer_args *ta) |
991 | { |
1002 | { |
992 | api_ready (GvSV (coro_current)); |
1003 | api_ready (coro_current); |
993 | |
1004 | |
994 | prepare_schedule (ta); |
1005 | prepare_schedule (ta); |
995 | } |
1006 | } |
996 | |
1007 | |
997 | static void |
1008 | static void |
998 | api_schedule (void) |
1009 | api_schedule (void) |
999 | { |
1010 | { |
1000 | dTHX; |
|
|
1001 | struct transfer_args ta; |
1011 | struct transfer_args ta; |
1002 | |
1012 | |
1003 | prepare_schedule (&ta); |
1013 | prepare_schedule (&ta); |
1004 | TRANSFER (ta); |
1014 | TRANSFER (ta); |
1005 | } |
1015 | } |
1006 | |
1016 | |
1007 | static void |
1017 | static void |
1008 | api_cede (void) |
1018 | api_cede (void) |
1009 | { |
1019 | { |
1010 | dTHX; |
|
|
1011 | struct transfer_args ta; |
1020 | struct transfer_args ta; |
1012 | |
1021 | |
1013 | prepare_cede (&ta); |
1022 | prepare_cede (&ta); |
1014 | TRANSFER (ta); |
1023 | TRANSFER (ta); |
1015 | } |
1024 | } |
… | |
… | |
1047 | HV *hv; |
1056 | HV *hv; |
1048 | int i; |
1057 | int i; |
1049 | |
1058 | |
1050 | Newz (0, coro, 1, struct coro); |
1059 | Newz (0, coro, 1, struct coro); |
1051 | coro->args = newAV (); |
1060 | coro->args = newAV (); |
|
|
1061 | coro->flags = CF_NEW; |
1052 | |
1062 | |
1053 | hv = newHV (); |
1063 | hv = newHV (); |
1054 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1064 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1055 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1065 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1056 | |
1066 | |
… | |
… | |
1079 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1089 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1080 | break; |
1090 | break; |
1081 | |
1091 | |
1082 | case 1: |
1092 | case 1: |
1083 | if (items != 3) |
1093 | if (items != 3) |
1084 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1094 | croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); |
1085 | |
1095 | |
1086 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1096 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1087 | break; |
1097 | break; |
1088 | |
1098 | |
1089 | case 2: |
1099 | case 2: |
… | |
… | |
1099 | SV *yieldstack; |
1109 | SV *yieldstack; |
1100 | SV *sv; |
1110 | SV *sv; |
1101 | AV *defav = GvAV (PL_defgv); |
1111 | AV *defav = GvAV (PL_defgv); |
1102 | |
1112 | |
1103 | yieldstack = *hv_fetch ( |
1113 | yieldstack = *hv_fetch ( |
1104 | (HV *)SvRV (GvSV (coro_current)), |
1114 | (HV *)SvRV (coro_current), |
1105 | "yieldstack", sizeof ("yieldstack") - 1, |
1115 | "yieldstack", sizeof ("yieldstack") - 1, |
1106 | 0 |
1116 | 0 |
1107 | ); |
1117 | ); |
1108 | |
1118 | |
1109 | /* set up @_ -- ugly */ |
1119 | /* set up @_ -- ugly */ |
… | |
… | |
1171 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1181 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1172 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1182 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1173 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1183 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1174 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1184 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1175 | |
1185 | |
1176 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1186 | coro_current = get_sv ("Coro::current", FALSE); |
1177 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1187 | SvREADONLY_on (coro_current); |
1178 | |
1188 | |
1179 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1189 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1180 | coro_ready[i] = newAV (); |
1190 | coro_ready[i] = newAV (); |
1181 | |
1191 | |
1182 | { |
1192 | { |
… | |
… | |
1237 | CODE: |
1247 | CODE: |
1238 | RETVAL = coro_nready; |
1248 | RETVAL = coro_nready; |
1239 | OUTPUT: |
1249 | OUTPUT: |
1240 | RETVAL |
1250 | RETVAL |
1241 | |
1251 | |
|
|
1252 | void |
|
|
1253 | _set_current (SV *current) |
|
|
1254 | PROTOTYPE: $ |
|
|
1255 | CODE: |
|
|
1256 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1257 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
|
|
1258 | |
1242 | MODULE = Coro::State PACKAGE = Coro::AIO |
1259 | MODULE = Coro::State PACKAGE = Coro::AIO |
1243 | |
1260 | |
1244 | SV * |
1261 | SV * |
1245 | _get_state () |
1262 | _get_state () |
1246 | CODE: |
1263 | CODE: |