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.147 by root, Mon Mar 19 15:50:48 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;
187# include "state.h" 188# include "state.h"
188#undef VAR 189#undef VAR
189 190
190 /* coro process data */ 191 /* coro process data */
191 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 */
192}; 197};
193 198
194typedef struct coro *Coro__State; 199typedef struct coro *Coro__State;
195typedef struct coro *Coro__State_or_hashref; 200typedef struct coro *Coro__State_or_hashref;
196 201
205 210
206/* for Coro.pm */ 211/* for Coro.pm */
207static SV *coro_current; 212static SV *coro_current;
208static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 213static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
209static int coro_nready; 214static int coro_nready;
215static struct coro *coro_first;
210 216
211/** lowlevel stuff **********************************************************/ 217/** lowlevel stuff **********************************************************/
212 218
213static AV * 219static AV *
214coro_clone_padlist (pTHX_ CV *cv) 220coro_clone_padlist (pTHX_ CV *cv)
373 CvPADLIST (cv) = (AV *)POPs; 379 CvPADLIST (cv) = (AV *)POPs;
374 } 380 }
375 381
376 PUTBACK; 382 PUTBACK;
377 } 383 }
378 assert (!PL_comppad || AvARRAY (PL_comppad));//D
379} 384}
380 385
381static void 386static void
382save_perl (pTHX_ Coro__State c) 387save_perl (pTHX_ Coro__State c)
383{ 388{
384 assert (!PL_comppad || AvARRAY (PL_comppad));//D
385 { 389 {
386 dSP; 390 dSP;
387 I32 cxix = cxstack_ix; 391 I32 cxix = cxstack_ix;
388 PERL_CONTEXT *ccstk = cxstack; 392 PERL_CONTEXT *ccstk = cxstack;
389 PERL_SI *top_si = PL_curstackinfo; 393 PERL_SI *top_si = PL_curstackinfo;
400 { 404 {
401 while (cxix >= 0) 405 while (cxix >= 0)
402 { 406 {
403 PERL_CONTEXT *cx = &ccstk[cxix--]; 407 PERL_CONTEXT *cx = &ccstk[cxix--];
404 408
405 if (CxTYPE (cx) == CXt_SUB) 409 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
406 { 410 {
407 CV *cv = cx->blk_sub.cv; 411 CV *cv = cx->blk_sub.cv;
408 412
409 if (CvDEPTH (cv)) 413 if (CvDEPTH (cv))
410 { 414 {
536#if !PERL_VERSION_ATLEAST (5,9,0) 540#if !PERL_VERSION_ATLEAST (5,9,0)
537 Safefree (PL_retstack); 541 Safefree (PL_retstack);
538#endif 542#endif
539} 543}
540 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
541/** coroutine stack handling ************************************************/ 576/** coroutine stack handling ************************************************/
542 577
543static void 578static void
544setup_coro (pTHX_ struct coro *coro) 579setup_coro (pTHX_ struct coro *coro)
545{ 580{
566 Zero (&myop, 1, LOGOP); 601 Zero (&myop, 1, LOGOP);
567 myop.op_next = Nullop; 602 myop.op_next = Nullop;
568 myop.op_flags = OPf_WANT_VOID; 603 myop.op_flags = OPf_WANT_VOID;
569 604
570 PUSHMARK (SP); 605 PUSHMARK (SP);
571 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 606 XPUSHs (av_shift (GvAV (PL_defgv)));
572 PUTBACK; 607 PUTBACK;
573 PL_op = (OP *)&myop; 608 PL_op = (OP *)&myop;
574 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 609 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
575 SPAGAIN; 610 SPAGAIN;
576 } 611 }
587 coro_mortal = 0; 622 coro_mortal = 0;
588 } 623 }
589} 624}
590 625
591/* 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 */
592static void NOINLINE 629static void NOINLINE
593prepare_cctx (pTHX_ coro_cctx *cctx) 630prepare_cctx (pTHX_ coro_cctx *cctx)
594{ 631{
595 dSP; 632 dSP;
596 LOGOP myop; 633 LOGOP myop;
607 PL_op = (OP *)&myop; 644 PL_op = (OP *)&myop;
608 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 645 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
609 SPAGAIN; 646 SPAGAIN;
610} 647}
611 648
649/*
650 * this is a _very_ stripped down perl interpreter ;)
651 */
612static void 652static void
613coro_run (void *arg) 653coro_run (void *arg)
614{ 654{
615 dTHX; 655 dTHX;
616 656
617 /* coro_run is the alternative tail of transfer(), so unlock here. */ 657 /* coro_run is the alternative tail of transfer(), so unlock here. */
618 UNLOCK; 658 UNLOCK;
619 659
620 /*
621 * this is a _very_ stripped down perl interpreter ;)
622 */
623 PL_top_env = &PL_start_env; 660 PL_top_env = &PL_start_env;
624 661
625 /* inject call to cctx_init */ 662 /* inject a fake subroutine call to cctx_init */
626 prepare_cctx (aTHX_ (coro_cctx *)arg); 663 prepare_cctx (aTHX_ (coro_cctx *)arg);
627 664
628 /* 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 */
629 PL_restartop = PL_op; 666 PL_restartop = PL_op;
630 perl_run (PL_curinterp); 667 perl_run (PL_curinterp);
631 668
632 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 669 /*
633 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 */
634} 678}
635 679
636static coro_cctx * 680static coro_cctx *
637cctx_new () 681cctx_new ()
638{ 682{
867 struct coro temp; 911 struct coro temp;
868 912
869 assert (!(coro->flags & CF_RUNNING)); 913 assert (!(coro->flags & CF_RUNNING));
870 914
871 Zero (&temp, 1, struct coro); 915 Zero (&temp, 1, struct coro);
872 temp.save = CORO_SAVE_DEF; 916 temp.save = CORO_SAVE_ALL;
873 917
874 if (coro->flags & CF_RUNNING) 918 if (coro->flags & CF_RUNNING)
875 croak ("FATAL: tried to destroy currently running coroutine"); 919 croak ("FATAL: tried to destroy currently running coroutine");
876 920
877 save_perl (aTHX_ &temp); 921 save_perl (aTHX_ &temp);
885 } 929 }
886 930
887 cctx_destroy (coro->cctx); 931 cctx_destroy (coro->cctx);
888 SvREFCNT_dec (coro->args); 932 SvREFCNT_dec (coro->args);
889 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
890 return 1; 938 return 1;
891} 939}
892 940
893static int 941static int
894coro_state_free (pTHX_ SV *sv, MAGIC *mg) 942coro_state_free (pTHX_ SV *sv, MAGIC *mg)
895{ 943{
896 struct coro *coro = (struct coro *)mg->mg_ptr; 944 struct coro *coro = (struct coro *)mg->mg_ptr;
897 mg->mg_ptr = 0; 945 mg->mg_ptr = 0;
946
947 coro->hv = 0;
898 948
899 if (--coro->refcnt < 0) 949 if (--coro->refcnt < 0)
900 { 950 {
901 coro_state_destroy (aTHX_ coro); 951 coro_state_destroy (aTHX_ coro);
902 Safefree (coro); 952 Safefree (coro);
1175 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); 1225 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1176 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); 1226 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1177 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1227 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1178 1228
1179 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;
1180 1234
1181 coroapi.ver = CORO_API_VERSION; 1235 coroapi.ver = CORO_API_VERSION;
1182 coroapi.transfer = api_transfer; 1236 coroapi.transfer = api_transfer;
1183 1237
1184 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1238 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1195 Newz (0, coro, 1, struct coro); 1249 Newz (0, coro, 1, struct coro);
1196 coro->args = newAV (); 1250 coro->args = newAV ();
1197 coro->save = CORO_SAVE_DEF; 1251 coro->save = CORO_SAVE_DEF;
1198 coro->flags = CF_NEW; 1252 coro->flags = CF_NEW;
1199 1253
1254 if (coro_first) coro_first->prev = coro;
1255 coro->next = coro_first;
1256 coro_first = coro;
1257
1200 hv = newHV (); 1258 coro->hv = hv = newHV ();
1201 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;
1202 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1260 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1203 1261
1204 for (i = 1; i < items; i++) 1262 for (i = 1; i < items; i++)
1205 av_push (coro->args, newSVsv (ST (i))); 1263 av_push (coro->args, newSVsv (ST (i)));
1307 CODE: 1365 CODE:
1308 RETVAL = cctx_idle; 1366 RETVAL = cctx_idle;
1309 OUTPUT: 1367 OUTPUT:
1310 RETVAL 1368 RETVAL
1311 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
1312MODULE = Coro::State PACKAGE = Coro 1439MODULE = Coro::State PACKAGE = Coro
1313 1440
1314BOOT: 1441BOOT:
1315{ 1442{
1316 int i; 1443 int i;
1383 CODE: 1510 CODE:
1384 RETVAL = boolSV (api_ready (self)); 1511 RETVAL = boolSV (api_ready (self));
1385 OUTPUT: 1512 OUTPUT:
1386 RETVAL 1513 RETVAL
1387 1514
1388SV *
1389is_ready (SV *self)
1390 PROTOTYPE: $
1391 CODE:
1392 RETVAL = boolSV (api_is_ready (self));
1393 OUTPUT:
1394 RETVAL
1395
1396int 1515int
1397nready (...) 1516nready (...)
1398 PROTOTYPE: 1517 PROTOTYPE:
1399 CODE: 1518 CODE:
1400 RETVAL = coro_nready; 1519 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines