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.110 by root, Tue Nov 28 23:08:07 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)
704 if (!next->cctx) 723 if (!next->cctx)
705 next->cctx = cctx_get (); 724 next->cctx = cctx_get ();
706 725
707 if (prev__cctx != next->cctx) 726 if (prev__cctx != next->cctx)
708 { 727 {
728 assert ( prev__cctx->inuse);
729 assert (!next->cctx->inuse);
730
731 prev__cctx->inuse = 0;
732 next->cctx->inuse = 1;
733
709 prev__cctx->top_env = PL_top_env; 734 prev__cctx->top_env = PL_top_env;
710 PL_top_env = next->cctx->top_env; 735 PL_top_env = next->cctx->top_env;
711 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 736 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
712 } 737 }
713 738
867 } 892 }
868 893
869 return 0; 894 return 0;
870} 895}
871 896
872static void 897static int
873api_ready (SV *coro) 898api_ready (SV *coro_sv)
874{ 899{
875 dTHX; 900 struct coro *coro;
876 901
877 if (SvROK (coro)) 902 if (SvROK (coro_sv))
878 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;
879 910
880 LOCK; 911 LOCK;
881 coro_enq (SvREFCNT_inc (coro)); 912 coro_enq (SvREFCNT_inc (coro_sv));
882 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);
883} 924}
884 925
885static void 926static void
886prepare_schedule (struct transfer_args *ta) 927prepare_schedule (struct transfer_args *ta)
887{ 928{
923 coro_mortal = prev; 964 coro_mortal = prev;
924 965
925 ta->prev = SvSTATE (prev); 966 ta->prev = SvSTATE (prev);
926 ta->next = SvSTATE (next); 967 ta->next = SvSTATE (next);
927 ta->flags = TRANSFER_SAVE_ALL; 968 ta->flags = TRANSFER_SAVE_ALL;
969
970 ta->next->flags &= ~CF_READY;
928} 971}
929 972
930static void 973static void
931prepare_cede (struct transfer_args *ta) 974prepare_cede (struct transfer_args *ta)
932{ 975{
933 LOCK; 976 api_ready (GvSV (coro_current));
934 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
935 UNLOCK;
936 977
937 prepare_schedule (ta); 978 prepare_schedule (ta);
938} 979}
939 980
940static void 981static void
1126 SV *sv = perl_get_sv("Coro::API", 1); 1167 SV *sv = perl_get_sv("Coro::API", 1);
1127 1168
1128 coroapi.schedule = api_schedule; 1169 coroapi.schedule = api_schedule;
1129 coroapi.cede = api_cede; 1170 coroapi.cede = api_cede;
1130 coroapi.ready = api_ready; 1171 coroapi.ready = api_ready;
1172 coroapi.is_ready = api_is_ready;
1131 coroapi.nready = &coro_nready; 1173 coroapi.nready = &coro_nready;
1132 coroapi.current = coro_current; 1174 coroapi.current = coro_current;
1133 1175
1134 GCoroAPI = &coroapi; 1176 GCoroAPI = &coroapi;
1135 sv_setiv (sv, (IV)&coroapi); 1177 sv_setiv (sv, (IV)&coroapi);
1155 1197
1156 coro->prio = newprio; 1198 coro->prio = newprio;
1157 } 1199 }
1158} 1200}
1159 1201
1160void 1202SV *
1161ready (SV *self) 1203ready (SV *self)
1162 PROTOTYPE: $ 1204 PROTOTYPE: $
1163 CODE: 1205 CODE:
1164 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
1165 1217
1166int 1218int
1167nready (...) 1219nready (...)
1168 PROTOTYPE: 1220 PROTOTYPE:
1169 CODE: 1221 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines