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.107 by root, Mon Nov 27 02:15:51 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;
142 150
143 /* saved global state not related to stacks */ 151#define VAR(name,type) type name;
144 U8 dowarn; 152# include "state.h"
145 I32 in_eval; 153#undef VAR
146
147 /* the stacks and related info (callchain etc..) */
148 PERL_SI *curstackinfo;
149 AV *curstack;
150 AV *mainstack;
151 SV **stack_sp;
152 OP *op;
153 SV **curpad;
154 AV *comppad;
155 CV *compcv;
156 SV **stack_base;
157 SV **stack_max;
158 SV **tmps_stack;
159 I32 tmps_floor;
160 I32 tmps_ix;
161 I32 tmps_max;
162 I32 *markstack;
163 I32 *markstack_ptr;
164 I32 *markstack_max;
165 I32 *scopestack;
166 I32 scopestack_ix;
167 I32 scopestack_max;
168 ANY *savestack;
169 I32 savestack_ix;
170 I32 savestack_max;
171 OP **retstack;
172 I32 retstack_ix;
173 I32 retstack_max;
174 PMOP *curpm;
175 COP *curcop;
176 154
177 /* coro process data */ 155 /* coro process data */
178 int prio; 156 int prio;
179}; 157};
180 158
244 222
245#define PERL_MAGIC_coro PERL_MAGIC_ext 223#define PERL_MAGIC_coro PERL_MAGIC_ext
246 224
247static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 225static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
248 226
227#define CORO_MAGIC(cv) \
228 SvMAGIC (cv) \
229 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
230 ? SvMAGIC (cv) \
231 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
232 : 0
233
249/* the next two functions merely cache the padlists */ 234/* the next two functions merely cache the padlists */
250static void 235static void
251get_padlist (CV *cv) 236get_padlist (CV *cv)
252{ 237{
253 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 238 MAGIC *mg = CORO_MAGIC (cv);
239 AV *av;
254 240
255 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 241 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
256 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 242 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
257 else 243 else
258 { 244 {
259#if 0 245#if 0
260 /* this is probably cleaner, but also slower? */ 246 /* this is probably cleaner, but also slower? */
261 CV *cp = Perl_cv_clone (cv); 247 CV *cp = Perl_cv_clone (cv);
269} 255}
270 256
271static void 257static void
272put_padlist (CV *cv) 258put_padlist (CV *cv)
273{ 259{
274 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 260 MAGIC *mg = CORO_MAGIC (cv);
261 AV *av;
275 262
276 if (!mg) 263 if (!mg)
277 { 264 {
278 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 265 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
279 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 266 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
280 mg->mg_virtual = &vtbl_coro; 267 mg->mg_virtual = &vtbl_coro;
281 mg->mg_obj = (SV *)newAV (); 268 mg->mg_obj = (SV *)newAV ();
282 } 269 }
283 270
284 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 271 av = (AV *)mg->mg_obj;
272
273 if (AvFILLp (av) >= AvMAX (av))
274 av_extend (av, AvMAX (av) + 1);
275
276 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
285} 277}
286 278
287#define SB do { 279#define SB do {
288#define SE } while (0) 280#define SE } while (0)
289 281
293#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 285#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
294 286
295static void 287static void
296load_state(Coro__State c) 288load_state(Coro__State c)
297{ 289{
298 PL_dowarn = c->dowarn; 290#define VAR(name,type) PL_ ## name = c->name;
299 PL_in_eval = c->in_eval; 291# include "state.h"
300 292#undef VAR
301 PL_curstackinfo = c->curstackinfo;
302 PL_curstack = c->curstack;
303 PL_mainstack = c->mainstack;
304 PL_stack_sp = c->stack_sp;
305 PL_op = c->op;
306 PL_curpad = c->curpad;
307 PL_comppad = c->comppad;
308 PL_compcv = c->compcv;
309 PL_stack_base = c->stack_base;
310 PL_stack_max = c->stack_max;
311 PL_tmps_stack = c->tmps_stack;
312 PL_tmps_floor = c->tmps_floor;
313 PL_tmps_ix = c->tmps_ix;
314 PL_tmps_max = c->tmps_max;
315 PL_markstack = c->markstack;
316 PL_markstack_ptr = c->markstack_ptr;
317 PL_markstack_max = c->markstack_max;
318 PL_scopestack = c->scopestack;
319 PL_scopestack_ix = c->scopestack_ix;
320 PL_scopestack_max = c->scopestack_max;
321 PL_savestack = c->savestack;
322 PL_savestack_ix = c->savestack_ix;
323 PL_savestack_max = c->savestack_max;
324#if !PERL_VERSION_ATLEAST (5,9,0)
325 PL_retstack = c->retstack;
326 PL_retstack_ix = c->retstack_ix;
327 PL_retstack_max = c->retstack_max;
328#endif
329 PL_curpm = c->curpm;
330 PL_curcop = c->curcop;
331 293
332 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 294 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
333 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 295 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
334 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 296 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
335 297
338 CV *cv; 300 CV *cv;
339 301
340 /* now do the ugly restore mess */ 302 /* now do the ugly restore mess */
341 while ((cv = (CV *)POPs)) 303 while ((cv = (CV *)POPs))
342 { 304 {
343 AV *padlist = (AV *)POPs;
344
345 if (padlist)
346 {
347 put_padlist (cv); /* mark this padlist as available */ 305 put_padlist (cv); /* mark this padlist as available */
348 CvPADLIST(cv) = padlist; 306 CvDEPTH (cv) = PTR2IV (POPs);
349 } 307 CvPADLIST (cv) = (AV *)POPs;
350
351 ++CvDEPTH(cv);
352 } 308 }
353 309
354 PUTBACK; 310 PUTBACK;
355 } 311 }
356} 312}
378 PERL_CONTEXT *cx = &ccstk[cxix--]; 334 PERL_CONTEXT *cx = &ccstk[cxix--];
379 335
380 if (CxTYPE(cx) == CXt_SUB) 336 if (CxTYPE(cx) == CXt_SUB)
381 { 337 {
382 CV *cv = cx->blk_sub.cv; 338 CV *cv = cx->blk_sub.cv;
339
383 if (CvDEPTH(cv)) 340 if (CvDEPTH (cv))
384 { 341 {
385 EXTEND (SP, CvDEPTH(cv)*2); 342 EXTEND (SP, 3);
386
387 while (--CvDEPTH(cv))
388 {
389 /* this tells the restore code to increment CvDEPTH */
390 PUSHs (Nullsv);
391 PUSHs ((SV *)cv);
392 }
393 343
394 PUSHs ((SV *)CvPADLIST(cv)); 344 PUSHs ((SV *)CvPADLIST(cv));
345 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
395 PUSHs ((SV *)cv); 346 PUSHs ((SV *)cv);
396 347
348 CvDEPTH (cv) = 0;
397 get_padlist (cv); 349 get_padlist (cv);
398 } 350 }
399 } 351 }
400#ifdef CXt_FORMAT 352#ifdef CXt_FORMAT
401 else if (CxTYPE(cx) == CXt_FORMAT) 353 else if (CxTYPE(cx) == CXt_FORMAT)
420 372
421 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 373 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
422 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 374 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
423 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 375 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
424 376
425 c->dowarn = PL_dowarn; 377#define VAR(name,type)c->name = PL_ ## name;
426 c->in_eval = PL_in_eval; 378# include "state.h"
427 379#undef VAR
428 c->curstackinfo = PL_curstackinfo;
429 c->curstack = PL_curstack;
430 c->mainstack = PL_mainstack;
431 c->stack_sp = PL_stack_sp;
432 c->op = PL_op;
433 c->curpad = PL_curpad;
434 c->comppad = PL_comppad;
435 c->compcv = PL_compcv;
436 c->stack_base = PL_stack_base;
437 c->stack_max = PL_stack_max;
438 c->tmps_stack = PL_tmps_stack;
439 c->tmps_floor = PL_tmps_floor;
440 c->tmps_ix = PL_tmps_ix;
441 c->tmps_max = PL_tmps_max;
442 c->markstack = PL_markstack;
443 c->markstack_ptr = PL_markstack_ptr;
444 c->markstack_max = PL_markstack_max;
445 c->scopestack = PL_scopestack;
446 c->scopestack_ix = PL_scopestack_ix;
447 c->scopestack_max = PL_scopestack_max;
448 c->savestack = PL_savestack;
449 c->savestack_ix = PL_savestack_ix;
450 c->savestack_max = PL_savestack_max;
451#if !PERL_VERSION_ATLEAST (5,9,0)
452 c->retstack = PL_retstack;
453 c->retstack_ix = PL_retstack_ix;
454 c->retstack_max = PL_retstack_max;
455#endif
456 c->curpm = PL_curpm;
457 c->curcop = PL_curcop;
458} 380}
459 381
460/* 382/*
461 * allocate various perl stacks. This is an exact copy 383 * allocate various perl stacks. This is an exact copy
462 * of perl.c:init_stacks, except that it uses less memory 384 * of perl.c:init_stacks, except that it uses less memory
751 if (flags == TRANSFER_SET_STACKLEVEL) 673 if (flags == TRANSFER_SET_STACKLEVEL)
752 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
753 else if (prev != next) 675 else if (prev != next)
754 { 676 {
755 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;
756 692
757 LOCK; 693 LOCK;
758 694
759 if (next->mainstack) 695 if (next->mainstack)
760 { 696 {
768 /* first get rid of the old state */ 704 /* first get rid of the old state */
769 SAVE (prev, -1); 705 SAVE (prev, -1);
770 /* setup coroutine call */ 706 /* setup coroutine call */
771 setup_coro (next); 707 setup_coro (next);
772 /* need a stack */ 708 /* need a stack */
709 assert (!next->stack);
773 next->cctx = 0; 710 next->cctx = 0;
774 } 711 }
775
776 if (!prev->cctx)
777 /* create a new empty context */
778 Newz (0, prev->cctx, 1, coro_cctx);
779 712
780 prev__cctx = prev->cctx; 713 prev__cctx = prev->cctx;
781 714
782 /* possibly "free" the cctx */ 715 /* possibly "free" the cctx */
783 if (prev__cctx->idle_sp == STACKLEVEL) 716 if (prev__cctx->idle_sp == STACKLEVEL)
784 { 717 {
718 assert (PL_top_env == prev__cctx->top_env);//D
785 cctx_put (prev__cctx); 719 cctx_put (prev__cctx);
786 prev->cctx = 0; 720 prev->cctx = 0;
787 } 721 }
788 722
789 if (!next->cctx) 723 if (!next->cctx)
790 next->cctx = cctx_get (); 724 next->cctx = cctx_get ();
791 725
792 if (prev__cctx != next->cctx) 726 if (prev__cctx != next->cctx)
793 { 727 {
728 assert ( prev__cctx->inuse);
729 assert (!next->cctx->inuse);
730
731 prev__cctx->inuse = 0;
732 next->cctx->inuse = 1;
733
794 prev__cctx->top_env = PL_top_env; 734 prev__cctx->top_env = PL_top_env;
795 PL_top_env = next->cctx->top_env; 735 PL_top_env = next->cctx->top_env;
796 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 736 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
797 } 737 }
798 738
952 } 892 }
953 893
954 return 0; 894 return 0;
955} 895}
956 896
957static void 897static int
958api_ready (SV *coro) 898api_ready (SV *coro_sv)
959{ 899{
960 dTHX; 900 struct coro *coro;
961 901
962 if (SvROK (coro)) 902 if (SvROK (coro_sv))
963 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;
964 910
965 LOCK; 911 LOCK;
966 coro_enq (SvREFCNT_inc (coro)); 912 coro_enq (SvREFCNT_inc (coro_sv));
967 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);
968} 924}
969 925
970static void 926static void
971prepare_schedule (struct transfer_args *ta) 927prepare_schedule (struct transfer_args *ta)
972{ 928{
1008 coro_mortal = prev; 964 coro_mortal = prev;
1009 965
1010 ta->prev = SvSTATE (prev); 966 ta->prev = SvSTATE (prev);
1011 ta->next = SvSTATE (next); 967 ta->next = SvSTATE (next);
1012 ta->flags = TRANSFER_SAVE_ALL; 968 ta->flags = TRANSFER_SAVE_ALL;
969
970 ta->next->flags &= ~CF_READY;
1013} 971}
1014 972
1015static void 973static void
1016prepare_cede (struct transfer_args *ta) 974prepare_cede (struct transfer_args *ta)
1017{ 975{
1018 LOCK; 976 api_ready (GvSV (coro_current));
1019 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
1020 UNLOCK;
1021 977
1022 prepare_schedule (ta); 978 prepare_schedule (ta);
1023} 979}
1024 980
1025static void 981static void
1211 SV *sv = perl_get_sv("Coro::API", 1); 1167 SV *sv = perl_get_sv("Coro::API", 1);
1212 1168
1213 coroapi.schedule = api_schedule; 1169 coroapi.schedule = api_schedule;
1214 coroapi.cede = api_cede; 1170 coroapi.cede = api_cede;
1215 coroapi.ready = api_ready; 1171 coroapi.ready = api_ready;
1172 coroapi.is_ready = api_is_ready;
1216 coroapi.nready = &coro_nready; 1173 coroapi.nready = &coro_nready;
1217 coroapi.current = coro_current; 1174 coroapi.current = coro_current;
1218 1175
1219 GCoroAPI = &coroapi; 1176 GCoroAPI = &coroapi;
1220 sv_setiv (sv, (IV)&coroapi); 1177 sv_setiv (sv, (IV)&coroapi);
1240 1197
1241 coro->prio = newprio; 1198 coro->prio = newprio;
1242 } 1199 }
1243} 1200}
1244 1201
1245void 1202SV *
1246ready (SV *self) 1203ready (SV *self)
1247 PROTOTYPE: $ 1204 PROTOTYPE: $
1248 CODE: 1205 CODE:
1249 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
1250 1217
1251int 1218int
1252nready (...) 1219nready (...)
1253 PROTOTYPE: 1220 PROTOTYPE:
1254 CODE: 1221 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines