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.153 by root, Thu Sep 20 12:18:00 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 *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
529#if !PERL_VERSION_ATLEAST (5,9,0) 540#if !PERL_VERSION_ATLEAST (5,9,0)
530 Safefree (PL_retstack); 541 Safefree (PL_retstack);
531#endif 542#endif
532} 543}
533 544
545static size_t
546coro_rss (struct coro *coro)
547{
548 size_t rss = sizeof (coro);
549
550 if (coro->mainstack)
551 {
552 if (coro->flags & CF_RUNNING)
553 {
554 #define VAR(name,type)coro->name = PL_ ## name;
555 # include "state.h"
556 #undef VAR
557 }
558
559 rss += sizeof (coro->curstackinfo);
560 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
561 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
562 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
563 rss += coro->tmps_max * sizeof (SV *);
564 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
565 rss += coro->scopestack_max * sizeof (I32);
566 rss += coro->savestack_max * sizeof (ANY);
567
568#if !PERL_VERSION_ATLEAST (5,9,0)
569 rss += coro->retstack_max * sizeof (OP *);
570#endif
571 }
572
573 return rss;
574}
575
534/** coroutine stack handling ************************************************/ 576/** coroutine stack handling ************************************************/
535 577
536static void 578static void
537setup_coro (pTHX_ struct coro *coro) 579setup_coro (pTHX_ struct coro *coro)
538{ 580{
559 Zero (&myop, 1, LOGOP); 601 Zero (&myop, 1, LOGOP);
560 myop.op_next = Nullop; 602 myop.op_next = Nullop;
561 myop.op_flags = OPf_WANT_VOID; 603 myop.op_flags = OPf_WANT_VOID;
562 604
563 PUSHMARK (SP); 605 PUSHMARK (SP);
564 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 606 XPUSHs (av_shift (GvAV (PL_defgv)));
565 PUTBACK; 607 PUTBACK;
566 PL_op = (OP *)&myop; 608 PL_op = (OP *)&myop;
567 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 609 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
568 SPAGAIN; 610 SPAGAIN;
569 } 611 }
580 coro_mortal = 0; 622 coro_mortal = 0;
581 } 623 }
582} 624}
583 625
584/* inject a fake call to Coro::State::_cctx_init into the execution */ 626/* inject a fake call to Coro::State::_cctx_init into the execution */
627/* _cctx_init should be careful, as it could be called at almost any time */
628/* during execution of a perl program */
585static void NOINLINE 629static void NOINLINE
586prepare_cctx (pTHX_ coro_cctx *cctx) 630prepare_cctx (pTHX_ coro_cctx *cctx)
587{ 631{
588 dSP; 632 dSP;
589 LOGOP myop; 633 LOGOP myop;
600 PL_op = (OP *)&myop; 644 PL_op = (OP *)&myop;
601 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 645 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
602 SPAGAIN; 646 SPAGAIN;
603} 647}
604 648
649/*
650 * this is a _very_ stripped down perl interpreter ;)
651 */
605static void 652static void
606coro_run (void *arg) 653coro_run (void *arg)
607{ 654{
608 dTHX; 655 dTHX;
609 656
610 /* coro_run is the alternative tail of transfer(), so unlock here. */ 657 /* coro_run is the alternative tail of transfer(), so unlock here. */
611 UNLOCK; 658 UNLOCK;
612 659
613 /*
614 * this is a _very_ stripped down perl interpreter ;)
615 */
616 PL_top_env = &PL_start_env; 660 PL_top_env = &PL_start_env;
617 661
618 /* inject call to cctx_init */ 662 /* inject a fake subroutine call to cctx_init */
619 prepare_cctx (aTHX_ (coro_cctx *)arg); 663 prepare_cctx (aTHX_ (coro_cctx *)arg);
620 664
621 /* somebody will hit me for both perl_run and PL_restartop */ 665 /* somebody or something will hit me for both perl_run and PL_restartop */
622 PL_restartop = PL_op; 666 PL_restartop = PL_op;
623 perl_run (PL_curinterp); 667 perl_run (PL_curinterp);
624 668
625 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 669 /*
626 abort (); 670 * If perl-run returns we assume exit() was being called or the coro
671 * fell off the end, which seems to be the only valid (non-bug)
672 * reason for perl_run to return. We try to exit by jumping to the
673 * bootstrap-time "top" top_env, as we cannot restore the "main"
674 * coroutine as Coro has no such concept
675 */
676 PL_top_env = main_top_env;
677 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
627} 678}
628 679
629static coro_cctx * 680static coro_cctx *
630cctx_new () 681cctx_new ()
631{ 682{
878 } 929 }
879 930
880 cctx_destroy (coro->cctx); 931 cctx_destroy (coro->cctx);
881 SvREFCNT_dec (coro->args); 932 SvREFCNT_dec (coro->args);
882 933
934 if (coro->next) coro->next->prev = coro->prev;
935 if (coro->prev) coro->prev->next = coro->next;
936 if (coro == coro_first) coro_first = coro->next;
937
883 return 1; 938 return 1;
884} 939}
885 940
886static int 941static int
887coro_state_free (pTHX_ SV *sv, MAGIC *mg) 942coro_state_free (pTHX_ SV *sv, MAGIC *mg)
888{ 943{
889 struct coro *coro = (struct coro *)mg->mg_ptr; 944 struct coro *coro = (struct coro *)mg->mg_ptr;
890 mg->mg_ptr = 0; 945 mg->mg_ptr = 0;
946
947 coro->hv = 0;
891 948
892 if (--coro->refcnt < 0) 949 if (--coro->refcnt < 0)
893 { 950 {
894 coro_state_destroy (aTHX_ coro); 951 coro_state_destroy (aTHX_ coro);
895 Safefree (coro); 952 Safefree (coro);
1163 1220
1164 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1221 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1165 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1222 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1166 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1223 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1167 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1224 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1225 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1226 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1168 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1227 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1169 1228
1170 main_mainstack = PL_mainstack; 1229 main_mainstack = PL_mainstack;
1230 main_top_env = PL_top_env;
1231
1232 while (main_top_env->je_prev)
1233 main_top_env = main_top_env->je_prev;
1171 1234
1172 coroapi.ver = CORO_API_VERSION; 1235 coroapi.ver = CORO_API_VERSION;
1173 coroapi.transfer = api_transfer; 1236 coroapi.transfer = api_transfer;
1174 1237
1175 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1238 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1183 HV *hv; 1246 HV *hv;
1184 int i; 1247 int i;
1185 1248
1186 Newz (0, coro, 1, struct coro); 1249 Newz (0, coro, 1, struct coro);
1187 coro->args = newAV (); 1250 coro->args = newAV ();
1188 coro->save = CORO_SAVE_ALL; 1251 coro->save = CORO_SAVE_DEF;
1189 coro->flags = CF_NEW; 1252 coro->flags = CF_NEW;
1190 1253
1254 if (coro_first) coro_first->prev = coro;
1255 coro->next = coro_first;
1256 coro_first = coro;
1257
1191 hv = newHV (); 1258 coro->hv = hv = newHV ();
1192 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1259 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)); 1260 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1194 1261
1195 for (i = 1; i < items; i++) 1262 for (i = 1; i < items; i++)
1196 av_push (coro->args, newSVsv (ST (i))); 1263 av_push (coro->args, newSVsv (ST (i)));
1200 1267
1201int 1268int
1202save (SV *coro, int new_save = -1) 1269save (SV *coro, int new_save = -1)
1203 CODE: 1270 CODE:
1204 RETVAL = api_save (coro, new_save); 1271 RETVAL = api_save (coro, new_save);
1272 OUTPUT:
1273 RETVAL
1274
1275int
1276save_also (SV *coro_sv, int save_also)
1277 CODE:
1278{
1279 struct coro *coro = SvSTATE (coro_sv);
1280 RETVAL = coro->save;
1281 coro->save |= save_also;
1282}
1205 OUTPUT: 1283 OUTPUT:
1206 RETVAL 1284 RETVAL
1207 1285
1208void 1286void
1209_set_stacklevel (...) 1287_set_stacklevel (...)
1287 CODE: 1365 CODE:
1288 RETVAL = cctx_idle; 1366 RETVAL = cctx_idle;
1289 OUTPUT: 1367 OUTPUT:
1290 RETVAL 1368 RETVAL
1291 1369
1370void
1371list ()
1372 PPCODE:
1373{
1374 struct coro *coro;
1375 for (coro = coro_first; coro; coro = coro->next)
1376 if (coro->hv)
1377 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1378}
1379
1380void
1381_eval (Coro::State coro, SV *coderef)
1382 CODE:
1383{
1384 if (coro->mainstack)
1385 {
1386 struct coro temp;
1387 Zero (&temp, 1, struct coro);
1388 temp.save = CORO_SAVE_ALL;
1389
1390 if (!(coro->flags & CF_RUNNING))
1391 {
1392 save_perl (aTHX_ &temp);
1393 load_perl (aTHX_ coro);
1394 }
1395
1396 {
1397 dSP;
1398 ENTER;
1399 SAVETMPS;
1400 PUSHMARK (SP);
1401 PUTBACK;
1402 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1403 SPAGAIN;
1404 FREETMPS;
1405 LEAVE;
1406 PUTBACK;
1407 }
1408
1409 if (!(coro->flags & CF_RUNNING))
1410 {
1411 save_perl (aTHX_ coro);
1412 load_perl (aTHX_ &temp);
1413 }
1414 }
1415}
1416
1417SV *
1418is_ready (Coro::State coro)
1419 PROTOTYPE: $
1420 ALIAS:
1421 is_ready = CF_READY
1422 is_running = CF_RUNNING
1423 is_new = CF_NEW
1424 is_destroyed = CF_DESTROYED
1425 CODE:
1426 RETVAL = boolSV (coro->flags & ix);
1427 OUTPUT:
1428 RETVAL
1429
1430IV
1431rss (Coro::State coro)
1432 PROTOTYPE: $
1433 CODE:
1434 RETVAL = coro_rss (coro);
1435 OUTPUT:
1436 RETVAL
1437
1438
1292MODULE = Coro::State PACKAGE = Coro 1439MODULE = Coro::State PACKAGE = Coro
1293 1440
1294BOOT: 1441BOOT:
1295{ 1442{
1296 int i; 1443 int i;
1363 CODE: 1510 CODE:
1364 RETVAL = boolSV (api_ready (self)); 1511 RETVAL = boolSV (api_ready (self));
1365 OUTPUT: 1512 OUTPUT:
1366 RETVAL 1513 RETVAL
1367 1514
1368SV *
1369is_ready (SV *self)
1370 PROTOTYPE: $
1371 CODE:
1372 RETVAL = boolSV (api_is_ready (self));
1373 OUTPUT:
1374 RETVAL
1375
1376int 1515int
1377nready (...) 1516nready (...)
1378 PROTOTYPE: 1517 PROTOTYPE:
1379 CODE: 1518 CODE:
1380 RETVAL = coro_nready; 1519 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines