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.146 by root, Sat Mar 17 19:51:57 2007 UTC vs.
Revision 1.151 by root, Wed Sep 19 21:39:15 2007 UTC

129}; 129};
130 130
131static size_t coro_stacksize = CORO_STACKSIZE; 131static size_t coro_stacksize = CORO_STACKSIZE;
132static struct CoroAPI coroapi; 132static struct CoroAPI coroapi;
133static AV *main_mainstack; /* used to differentiate between $main and others */ 133static AV *main_mainstack; /* used to differentiate between $main and others */
134static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 135static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 136static SV *coro_mortal; /* will be freed after next transfer */
136 137
137static struct coro_cctx *cctx_first; 138static struct coro_cctx *cctx_first;
138static int cctx_count, cctx_idle; 139static int cctx_count, cctx_idle;
177 178
178 /* optionally saved, might be zero */ 179 /* optionally saved, might be zero */
179 AV *defav; /* @_ */ 180 AV *defav; /* @_ */
180 SV *defsv; /* $_ */ 181 SV *defsv; /* $_ */
181 SV *errsv; /* $@ */ 182 SV *errsv; /* $@ */
183 GV *deffh; /* default filehandle */
182 SV *irssv; /* $/ */ 184 SV *irssv; /* $/ */
183 SV *irssv_sv; /* real $/ cache */ 185 SV *irssv_sv; /* real $/ cache */
184 186
185#define VAR(name,type) type name; 187#define VAR(name,type) type name;
186# include "state.h" 188# include "state.h"
187#undef VAR 189#undef VAR
188 190
189 /* coro process data */ 191 /* coro process data */
190 int prio; 192 int prio;
193
194 /* linked list */
195 struct coro *next, *prev;
196 HV *hv; /* the perl hash associated with this coro, if any */
191}; 197};
192 198
193typedef struct coro *Coro__State; 199typedef struct coro *Coro__State;
194typedef struct coro *Coro__State_or_hashref; 200typedef struct coro *Coro__State_or_hashref;
195 201
204 210
205/* for Coro.pm */ 211/* for Coro.pm */
206static SV *coro_current; 212static SV *coro_current;
207static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 213static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
208static int coro_nready; 214static int coro_nready;
215static struct coro *first;
209 216
210/** lowlevel stuff **********************************************************/ 217/** lowlevel stuff **********************************************************/
211 218
212static AV * 219static AV *
213coro_clone_padlist (pTHX_ CV *cv) 220coro_clone_padlist (pTHX_ CV *cv)
341#undef VAR 348#undef VAR
342 349
343 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 350 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
344 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 351 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
345 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 352 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
353 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh);
354
346 if (c->irssv) 355 if (c->irssv)
347 { 356 {
348 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) 357 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
358 {
349 SvREFCNT_dec (c->irssv); 359 SvREFCNT_dec (c->irssv);
360 c->irssv = 0;
361 }
350 else 362 else
351 { 363 {
352 REPLACE_SV (PL_rs, c->irssv); 364 REPLACE_SV (PL_rs, c->irssv);
353 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); 365 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
354 sv_setsv (c->irssv_sv, PL_rs); 366 sv_setsv (c->irssv_sv, PL_rs);
367 CvPADLIST (cv) = (AV *)POPs; 379 CvPADLIST (cv) = (AV *)POPs;
368 } 380 }
369 381
370 PUTBACK; 382 PUTBACK;
371 } 383 }
372 assert (!PL_comppad || AvARRAY (PL_comppad));//D
373} 384}
374 385
375static void 386static void
376save_perl (pTHX_ Coro__State c) 387save_perl (pTHX_ Coro__State c)
377{ 388{
378 assert (!PL_comppad || AvARRAY (PL_comppad));//D
379 { 389 {
380 dSP; 390 dSP;
381 I32 cxix = cxstack_ix; 391 I32 cxix = cxstack_ix;
382 PERL_CONTEXT *ccstk = cxstack; 392 PERL_CONTEXT *ccstk = cxstack;
383 PERL_SI *top_si = PL_curstackinfo; 393 PERL_SI *top_si = PL_curstackinfo;
394 { 404 {
395 while (cxix >= 0) 405 while (cxix >= 0)
396 { 406 {
397 PERL_CONTEXT *cx = &ccstk[cxix--]; 407 PERL_CONTEXT *cx = &ccstk[cxix--];
398 408
399 if (CxTYPE (cx) == CXt_SUB) 409 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
400 { 410 {
401 CV *cv = cx->blk_sub.cv; 411 CV *cv = cx->blk_sub.cv;
402 412
403 if (CvDEPTH (cv)) 413 if (CvDEPTH (cv))
404 { 414 {
425 } 435 }
426 436
427 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 437 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
428 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 438 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
429 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 439 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
440 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0;
430 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 441 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
431 442
432#define VAR(name,type)c->name = PL_ ## name; 443#define VAR(name,type)c->name = PL_ ## name;
433# include "state.h" 444# include "state.h"
434#undef VAR 445#undef VAR
559 Zero (&myop, 1, LOGOP); 570 Zero (&myop, 1, LOGOP);
560 myop.op_next = Nullop; 571 myop.op_next = Nullop;
561 myop.op_flags = OPf_WANT_VOID; 572 myop.op_flags = OPf_WANT_VOID;
562 573
563 PUSHMARK (SP); 574 PUSHMARK (SP);
564 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 575 XPUSHs (av_shift (GvAV (PL_defgv)));
565 PUTBACK; 576 PUTBACK;
566 PL_op = (OP *)&myop; 577 PL_op = (OP *)&myop;
567 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 578 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
568 SPAGAIN; 579 SPAGAIN;
569 } 580 }
580 coro_mortal = 0; 591 coro_mortal = 0;
581 } 592 }
582} 593}
583 594
584/* inject a fake call to Coro::State::_cctx_init into the execution */ 595/* inject a fake call to Coro::State::_cctx_init into the execution */
596/* _cctx_init should be careful, as it could be called at almost any time */
597/* during execution of a perl program */
585static void NOINLINE 598static void NOINLINE
586prepare_cctx (pTHX_ coro_cctx *cctx) 599prepare_cctx (pTHX_ coro_cctx *cctx)
587{ 600{
588 dSP; 601 dSP;
589 LOGOP myop; 602 LOGOP myop;
600 PL_op = (OP *)&myop; 613 PL_op = (OP *)&myop;
601 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 614 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
602 SPAGAIN; 615 SPAGAIN;
603} 616}
604 617
618/*
619 * this is a _very_ stripped down perl interpreter ;)
620 */
605static void 621static void
606coro_run (void *arg) 622coro_run (void *arg)
607{ 623{
608 dTHX; 624 dTHX;
609 625
610 /* coro_run is the alternative tail of transfer(), so unlock here. */ 626 /* coro_run is the alternative tail of transfer(), so unlock here. */
611 UNLOCK; 627 UNLOCK;
612 628
613 /*
614 * this is a _very_ stripped down perl interpreter ;)
615 */
616 PL_top_env = &PL_start_env; 629 PL_top_env = &PL_start_env;
617 630
618 /* inject call to cctx_init */ 631 /* inject a fake subroutine call to cctx_init */
619 prepare_cctx (aTHX_ (coro_cctx *)arg); 632 prepare_cctx (aTHX_ (coro_cctx *)arg);
620 633
621 /* somebody will hit me for both perl_run and PL_restartop */ 634 /* somebody or something will hit me for both perl_run and PL_restartop */
622 PL_restartop = PL_op; 635 PL_restartop = PL_op;
623 perl_run (PL_curinterp); 636 perl_run (PL_curinterp);
624 637
625 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 638 /*
626 abort (); 639 * If perl-run returns we assume exit() was being called or the coro
640 * fell off the end, which seems to be the only valid (non-bug)
641 * reason for perl_run to return. We try to exit by jumping to the
642 * bootstrap-time "top" top_env, as we cannot restore the "main"
643 * coroutine as Coro has no such concept
644 */
645 PL_top_env = main_top_env;
646 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
627} 647}
628 648
629static coro_cctx * 649static coro_cctx *
630cctx_new () 650cctx_new ()
631{ 651{
878 } 898 }
879 899
880 cctx_destroy (coro->cctx); 900 cctx_destroy (coro->cctx);
881 SvREFCNT_dec (coro->args); 901 SvREFCNT_dec (coro->args);
882 902
903 if (coro->next) coro->next->prev = coro->prev;
904 if (coro->prev) coro->prev->next = coro->next;
905 if (coro == first) first = coro->next;
906
883 return 1; 907 return 1;
884} 908}
885 909
886static int 910static int
887coro_state_free (pTHX_ SV *sv, MAGIC *mg) 911coro_state_free (pTHX_ SV *sv, MAGIC *mg)
888{ 912{
889 struct coro *coro = (struct coro *)mg->mg_ptr; 913 struct coro *coro = (struct coro *)mg->mg_ptr;
890 mg->mg_ptr = 0; 914 mg->mg_ptr = 0;
915
916 coro->hv = 0;
891 917
892 if (--coro->refcnt < 0) 918 if (--coro->refcnt < 0)
893 { 919 {
894 coro_state_destroy (aTHX_ coro); 920 coro_state_destroy (aTHX_ coro);
895 Safefree (coro); 921 Safefree (coro);
1163 1189
1164 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1190 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1165 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1191 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1166 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1192 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1167 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1193 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1194 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1195 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1168 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1196 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1169 1197
1170 main_mainstack = PL_mainstack; 1198 main_mainstack = PL_mainstack;
1199 main_top_env = PL_top_env;
1200
1201 while (main_top_env->je_prev)
1202 main_top_env = main_top_env->je_prev;
1171 1203
1172 coroapi.ver = CORO_API_VERSION; 1204 coroapi.ver = CORO_API_VERSION;
1173 coroapi.transfer = api_transfer; 1205 coroapi.transfer = api_transfer;
1174 1206
1175 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1207 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1183 HV *hv; 1215 HV *hv;
1184 int i; 1216 int i;
1185 1217
1186 Newz (0, coro, 1, struct coro); 1218 Newz (0, coro, 1, struct coro);
1187 coro->args = newAV (); 1219 coro->args = newAV ();
1188 coro->save = CORO_SAVE_ALL; 1220 coro->save = CORO_SAVE_DEF;
1189 coro->flags = CF_NEW; 1221 coro->flags = CF_NEW;
1190 1222
1223 if (first) first->prev = coro;
1224 coro->next = first;
1225 first = coro;
1226
1191 hv = newHV (); 1227 coro->hv = hv = newHV ();
1192 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1228 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1193 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1229 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1194 1230
1195 for (i = 1; i < items; i++) 1231 for (i = 1; i < items; i++)
1196 av_push (coro->args, newSVsv (ST (i))); 1232 av_push (coro->args, newSVsv (ST (i)));
1200 1236
1201int 1237int
1202save (SV *coro, int new_save = -1) 1238save (SV *coro, int new_save = -1)
1203 CODE: 1239 CODE:
1204 RETVAL = api_save (coro, new_save); 1240 RETVAL = api_save (coro, new_save);
1241 OUTPUT:
1242 RETVAL
1243
1244int
1245save_also (SV *coro_sv, int save_also)
1246 CODE:
1247{
1248 struct coro *coro = SvSTATE (coro_sv);
1249 RETVAL = coro->save;
1250 coro->save |= save_also;
1251}
1205 OUTPUT: 1252 OUTPUT:
1206 RETVAL 1253 RETVAL
1207 1254
1208void 1255void
1209_set_stacklevel (...) 1256_set_stacklevel (...)
1287 CODE: 1334 CODE:
1288 RETVAL = cctx_idle; 1335 RETVAL = cctx_idle;
1289 OUTPUT: 1336 OUTPUT:
1290 RETVAL 1337 RETVAL
1291 1338
1339void
1340list ()
1341 PPCODE:
1342{
1343 struct coro *coro;
1344 for (coro = first; coro; coro = coro->next)
1345 if (coro->hv)
1346 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1347}
1348
1349void
1350_eval (SV *coro_sv, SV *coderef)
1351 CODE:
1352{
1353 struct coro *coro = SvSTATE (coro_sv);
1354 if (coro->mainstack)
1355 {
1356 struct coro temp;
1357 Zero (&temp, 1, struct coro);
1358 temp.save = CORO_SAVE_ALL;
1359
1360 if (!(coro->flags & CF_RUNNING))
1361 {
1362 save_perl (aTHX_ &temp);
1363 load_perl (aTHX_ coro);
1364 }
1365
1366 {
1367 dSP;
1368 ENTER;
1369 SAVETMPS;
1370 PUSHMARK (SP);
1371 PUTBACK;
1372 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1373 SPAGAIN;
1374 FREETMPS;
1375 LEAVE;
1376 PUTBACK;
1377 }
1378
1379 if (!(coro->flags & CF_RUNNING))
1380 {
1381 save_perl (aTHX_ coro);
1382 load_perl (aTHX_ &temp);
1383 }
1384 }
1385}
1386
1387SV *
1388is_ready (SV *coro_sv)
1389 PROTOTYPE: $
1390 ALIAS:
1391 is_ready = CF_READY
1392 is_running = CF_RUNNING
1393 is_new = CF_NEW
1394 is_destroyed = CF_DESTROYED
1395 CODE:
1396 struct coro *coro = SvSTATE (coro_sv);
1397 RETVAL = boolSV (coro->flags & ix);
1398 OUTPUT:
1399 RETVAL
1400
1401
1292MODULE = Coro::State PACKAGE = Coro 1402MODULE = Coro::State PACKAGE = Coro
1293 1403
1294BOOT: 1404BOOT:
1295{ 1405{
1296 int i; 1406 int i;
1363 CODE: 1473 CODE:
1364 RETVAL = boolSV (api_ready (self)); 1474 RETVAL = boolSV (api_ready (self));
1365 OUTPUT: 1475 OUTPUT:
1366 RETVAL 1476 RETVAL
1367 1477
1368SV *
1369is_ready (SV *self)
1370 PROTOTYPE: $
1371 CODE:
1372 RETVAL = boolSV (api_is_ready (self));
1373 OUTPUT:
1374 RETVAL
1375
1376int 1478int
1377nready (...) 1479nready (...)
1378 PROTOTYPE: 1480 PROTOTYPE:
1379 CODE: 1481 CODE:
1380 RETVAL = coro_nready; 1482 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines