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.109 by root, Mon Nov 27 18:45:29 2006 UTC vs.
Revision 1.111 by root, Thu Nov 30 18:21:14 2006 UTC

119 /* cpu state */ 119 /* cpu state */
120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
121 JMPENV *top_env; 121 JMPENV *top_env;
122 coro_context cctx; 122 coro_context cctx;
123 123
124 int inuse;
125
124#if USE_VALGRIND 126#if USE_VALGRIND
125 int valgrind_id; 127 int valgrind_id;
126#endif 128#endif
127} coro_cctx; 129} coro_cctx;
130
131enum {
132 CF_RUNNING, /* coroutine is running */
133 CF_READY, /* coroutine is ready */
134};
128 135
129/* this is a structure representing a perl-level coroutine */ 136/* this is a structure representing a perl-level coroutine */
130struct coro { 137struct coro {
131 /* the c coroutine allocated to this perl coroutine, if any */ 138 /* the c coroutine allocated to this perl coroutine, if any */
132 coro_cctx *cctx; 139 coro_cctx *cctx;
133 140
134 /* data associated with this coroutine (initial args) */ 141 /* data associated with this coroutine (initial args) */
135 AV *args; 142 AV *args;
136 int refcnt; 143 int refcnt;
144 int flags;
137 145
138 /* optionally saved, might be zero */ 146 /* optionally saved, might be zero */
139 AV *defav; 147 AV *defav;
140 SV *defsv; 148 SV *defsv;
141 SV *errsv; 149 SV *errsv;
665 if (flags == TRANSFER_SET_STACKLEVEL) 673 if (flags == TRANSFER_SET_STACKLEVEL)
666 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
667 else if (prev != next) 675 else if (prev != next)
668 { 676 {
669 coro_cctx *prev__cctx; 677 coro_cctx *prev__cctx;
678
679 if (!prev->cctx)
680 {
681 /* create a new empty context */
682 Newz (0, prev->cctx, 1, coro_cctx);
683 prev->cctx->inuse = 1;
684 prev->flags |= CF_RUNNING;
685 }
686
687 assert ( prev->flags & CF_RUNNING);
688 assert (!next->flags & CF_RUNNING);
689
690 prev->flags &= ~CF_RUNNING;
691 next->flags |= CF_RUNNING;
670 692
671 LOCK; 693 LOCK;
672 694
673 if (next->mainstack) 695 if (next->mainstack)
674 { 696 {
682 /* first get rid of the old state */ 704 /* first get rid of the old state */
683 SAVE (prev, -1); 705 SAVE (prev, -1);
684 /* setup coroutine call */ 706 /* setup coroutine call */
685 setup_coro (next); 707 setup_coro (next);
686 /* need a stack */ 708 /* need a stack */
709 assert (!next->stack);
687 next->cctx = 0; 710 next->cctx = 0;
688 } 711 }
689
690 if (!prev->cctx)
691 /* create a new empty context */
692 Newz (0, prev->cctx, 1, coro_cctx);
693 712
694 prev__cctx = prev->cctx; 713 prev__cctx = prev->cctx;
695 714
696 /* possibly "free" the cctx */ 715 /* possibly "free" the cctx */
697 if (prev__cctx->idle_sp == STACKLEVEL) 716 if (prev__cctx->idle_sp == STACKLEVEL)
698 { 717 {
718 assert (PL_top_env == prev__cctx->top_env);//D
699 cctx_put (prev__cctx); 719 cctx_put (prev__cctx);
700 prev->cctx = 0; 720 prev->cctx = 0;
701 } 721 }
702 722
703 if (!next->cctx) 723 if (!next->cctx)
704 next->cctx = cctx_get (); 724 next->cctx = cctx_get ();
705 725
706 if (prev__cctx != next->cctx) 726 if (prev__cctx != next->cctx)
707 { 727 {
728 assert ( prev__cctx->inuse);
729 assert (!next->cctx->inuse);
730
731 prev__cctx->inuse = 0;
732 next->cctx->inuse = 1;
733
708 prev__cctx->top_env = PL_top_env; 734 prev__cctx->top_env = PL_top_env;
709 PL_top_env = next->cctx->top_env; 735 PL_top_env = next->cctx->top_env;
710 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 736 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
711 } 737 }
712 738
866 } 892 }
867 893
868 return 0; 894 return 0;
869} 895}
870 896
871static void 897static int
872api_ready (SV *coro) 898api_ready (SV *coro_sv)
873{ 899{
874 dTHX; 900 struct coro *coro;
875 901
876 if (SvROK (coro)) 902 if (SvROK (coro_sv))
877 coro = SvRV (coro); 903 coro_sv = SvRV (coro_sv);
904
905 coro = SvSTATE (coro_sv);
906 if (coro->flags & CF_READY)
907 return 0;
908
909 coro->flags |= CF_READY;
878 910
879 LOCK; 911 LOCK;
880 coro_enq (SvREFCNT_inc (coro)); 912 coro_enq (SvREFCNT_inc (coro_sv));
881 UNLOCK; 913 UNLOCK;
914
915 return 1;
916}
917
918static int
919api_is_ready (SV *coro_sv)
920{
921 struct coro *coro;
922
923 return !!(SvSTATE (coro_sv)->flags & CF_READY);
882} 924}
883 925
884static void 926static void
885prepare_schedule (struct transfer_args *ta) 927prepare_schedule (struct transfer_args *ta)
886{ 928{
922 coro_mortal = prev; 964 coro_mortal = prev;
923 965
924 ta->prev = SvSTATE (prev); 966 ta->prev = SvSTATE (prev);
925 ta->next = SvSTATE (next); 967 ta->next = SvSTATE (next);
926 ta->flags = TRANSFER_SAVE_ALL; 968 ta->flags = TRANSFER_SAVE_ALL;
969
970 ta->next->flags &= ~CF_READY;
927} 971}
928 972
929static void 973static void
930prepare_cede (struct transfer_args *ta) 974prepare_cede (struct transfer_args *ta)
931{ 975{
932 LOCK; 976 api_ready (GvSV (coro_current));
933 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
934 UNLOCK;
935 977
936 prepare_schedule (ta); 978 prepare_schedule (ta);
937} 979}
938 980
939static void 981static void
1125 SV *sv = perl_get_sv("Coro::API", 1); 1167 SV *sv = perl_get_sv("Coro::API", 1);
1126 1168
1127 coroapi.schedule = api_schedule; 1169 coroapi.schedule = api_schedule;
1128 coroapi.cede = api_cede; 1170 coroapi.cede = api_cede;
1129 coroapi.ready = api_ready; 1171 coroapi.ready = api_ready;
1172 coroapi.is_ready = api_is_ready;
1130 coroapi.nready = &coro_nready; 1173 coroapi.nready = &coro_nready;
1131 coroapi.current = coro_current; 1174 coroapi.current = coro_current;
1132 1175
1133 GCoroAPI = &coroapi; 1176 GCoroAPI = &coroapi;
1134 sv_setiv (sv, (IV)&coroapi); 1177 sv_setiv (sv, (IV)&coroapi);
1154 1197
1155 coro->prio = newprio; 1198 coro->prio = newprio;
1156 } 1199 }
1157} 1200}
1158 1201
1159void 1202SV *
1160ready (SV *self) 1203ready (SV *self)
1161 PROTOTYPE: $ 1204 PROTOTYPE: $
1162 CODE: 1205 CODE:
1163 api_ready (self); 1206 RETVAL = boolSV (api_ready (self));
1207 OUTPUT:
1208 RETVAL
1209
1210SV *
1211is_ready (SV *self)
1212 PROTOTYPE: $
1213 CODE:
1214 RETVAL = boolSV (api_is_ready (self));
1215 OUTPUT:
1216 RETVAL
1164 1217
1165int 1218int
1166nready (...) 1219nready (...)
1167 PROTOTYPE: 1220 PROTOTYPE:
1168 CODE: 1221 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines