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.152 by root, Thu Sep 20 12:02:25 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 *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 rss += sizeof (coro->curstackinfo);
553 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
554 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
555
556 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
557
558 rss += coro->tmps_max * sizeof (SV *);
559
560 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
561
562 rss += coro->scopestack_max * sizeof (I32);
563
564 rss += coro->savestack_max * sizeof (ANY);
565
566#if !PERL_VERSION_ATLEAST (5,9,0)
567 rss += coro->retstack_max * sizeof (OP *);
568#endif
569 }
570
571 return rss;
572}
573
541/** coroutine stack handling ************************************************/ 574/** coroutine stack handling ************************************************/
542 575
543static void 576static void
544setup_coro (pTHX_ struct coro *coro) 577setup_coro (pTHX_ struct coro *coro)
545{ 578{
566 Zero (&myop, 1, LOGOP); 599 Zero (&myop, 1, LOGOP);
567 myop.op_next = Nullop; 600 myop.op_next = Nullop;
568 myop.op_flags = OPf_WANT_VOID; 601 myop.op_flags = OPf_WANT_VOID;
569 602
570 PUSHMARK (SP); 603 PUSHMARK (SP);
571 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 604 XPUSHs (av_shift (GvAV (PL_defgv)));
572 PUTBACK; 605 PUTBACK;
573 PL_op = (OP *)&myop; 606 PL_op = (OP *)&myop;
574 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 607 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
575 SPAGAIN; 608 SPAGAIN;
576 } 609 }
587 coro_mortal = 0; 620 coro_mortal = 0;
588 } 621 }
589} 622}
590 623
591/* inject a fake call to Coro::State::_cctx_init into the execution */ 624/* inject a fake call to Coro::State::_cctx_init into the execution */
625/* _cctx_init should be careful, as it could be called at almost any time */
626/* during execution of a perl program */
592static void NOINLINE 627static void NOINLINE
593prepare_cctx (pTHX_ coro_cctx *cctx) 628prepare_cctx (pTHX_ coro_cctx *cctx)
594{ 629{
595 dSP; 630 dSP;
596 LOGOP myop; 631 LOGOP myop;
607 PL_op = (OP *)&myop; 642 PL_op = (OP *)&myop;
608 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 643 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
609 SPAGAIN; 644 SPAGAIN;
610} 645}
611 646
647/*
648 * this is a _very_ stripped down perl interpreter ;)
649 */
612static void 650static void
613coro_run (void *arg) 651coro_run (void *arg)
614{ 652{
615 dTHX; 653 dTHX;
616 654
617 /* coro_run is the alternative tail of transfer(), so unlock here. */ 655 /* coro_run is the alternative tail of transfer(), so unlock here. */
618 UNLOCK; 656 UNLOCK;
619 657
620 /*
621 * this is a _very_ stripped down perl interpreter ;)
622 */
623 PL_top_env = &PL_start_env; 658 PL_top_env = &PL_start_env;
624 659
625 /* inject call to cctx_init */ 660 /* inject a fake subroutine call to cctx_init */
626 prepare_cctx (aTHX_ (coro_cctx *)arg); 661 prepare_cctx (aTHX_ (coro_cctx *)arg);
627 662
628 /* somebody will hit me for both perl_run and PL_restartop */ 663 /* somebody or something will hit me for both perl_run and PL_restartop */
629 PL_restartop = PL_op; 664 PL_restartop = PL_op;
630 perl_run (PL_curinterp); 665 perl_run (PL_curinterp);
631 666
632 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 667 /*
633 abort (); 668 * If perl-run returns we assume exit() was being called or the coro
669 * fell off the end, which seems to be the only valid (non-bug)
670 * reason for perl_run to return. We try to exit by jumping to the
671 * bootstrap-time "top" top_env, as we cannot restore the "main"
672 * coroutine as Coro has no such concept
673 */
674 PL_top_env = main_top_env;
675 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
634} 676}
635 677
636static coro_cctx * 678static coro_cctx *
637cctx_new () 679cctx_new ()
638{ 680{
867 struct coro temp; 909 struct coro temp;
868 910
869 assert (!(coro->flags & CF_RUNNING)); 911 assert (!(coro->flags & CF_RUNNING));
870 912
871 Zero (&temp, 1, struct coro); 913 Zero (&temp, 1, struct coro);
872 temp.save = CORO_SAVE_DEF; 914 temp.save = CORO_SAVE_ALL;
873 915
874 if (coro->flags & CF_RUNNING) 916 if (coro->flags & CF_RUNNING)
875 croak ("FATAL: tried to destroy currently running coroutine"); 917 croak ("FATAL: tried to destroy currently running coroutine");
876 918
877 save_perl (aTHX_ &temp); 919 save_perl (aTHX_ &temp);
885 } 927 }
886 928
887 cctx_destroy (coro->cctx); 929 cctx_destroy (coro->cctx);
888 SvREFCNT_dec (coro->args); 930 SvREFCNT_dec (coro->args);
889 931
932 if (coro->next) coro->next->prev = coro->prev;
933 if (coro->prev) coro->prev->next = coro->next;
934 if (coro == first) first = coro->next;
935
890 return 1; 936 return 1;
891} 937}
892 938
893static int 939static int
894coro_state_free (pTHX_ SV *sv, MAGIC *mg) 940coro_state_free (pTHX_ SV *sv, MAGIC *mg)
895{ 941{
896 struct coro *coro = (struct coro *)mg->mg_ptr; 942 struct coro *coro = (struct coro *)mg->mg_ptr;
897 mg->mg_ptr = 0; 943 mg->mg_ptr = 0;
944
945 coro->hv = 0;
898 946
899 if (--coro->refcnt < 0) 947 if (--coro->refcnt < 0)
900 { 948 {
901 coro_state_destroy (aTHX_ coro); 949 coro_state_destroy (aTHX_ coro);
902 Safefree (coro); 950 Safefree (coro);
1175 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); 1223 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1176 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); 1224 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1177 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1225 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1178 1226
1179 main_mainstack = PL_mainstack; 1227 main_mainstack = PL_mainstack;
1228 main_top_env = PL_top_env;
1229
1230 while (main_top_env->je_prev)
1231 main_top_env = main_top_env->je_prev;
1180 1232
1181 coroapi.ver = CORO_API_VERSION; 1233 coroapi.ver = CORO_API_VERSION;
1182 coroapi.transfer = api_transfer; 1234 coroapi.transfer = api_transfer;
1183 1235
1184 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1236 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1195 Newz (0, coro, 1, struct coro); 1247 Newz (0, coro, 1, struct coro);
1196 coro->args = newAV (); 1248 coro->args = newAV ();
1197 coro->save = CORO_SAVE_DEF; 1249 coro->save = CORO_SAVE_DEF;
1198 coro->flags = CF_NEW; 1250 coro->flags = CF_NEW;
1199 1251
1252 if (first) first->prev = coro;
1253 coro->next = first;
1254 first = coro;
1255
1200 hv = newHV (); 1256 coro->hv = hv = newHV ();
1201 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1257 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)); 1258 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1203 1259
1204 for (i = 1; i < items; i++) 1260 for (i = 1; i < items; i++)
1205 av_push (coro->args, newSVsv (ST (i))); 1261 av_push (coro->args, newSVsv (ST (i)));
1307 CODE: 1363 CODE:
1308 RETVAL = cctx_idle; 1364 RETVAL = cctx_idle;
1309 OUTPUT: 1365 OUTPUT:
1310 RETVAL 1366 RETVAL
1311 1367
1368void
1369list ()
1370 PPCODE:
1371{
1372 struct coro *coro;
1373 for (coro = first; coro; coro = coro->next)
1374 if (coro->hv)
1375 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1376}
1377
1378void
1379_eval (Coro::State coro, SV *coderef)
1380 CODE:
1381{
1382 if (coro->mainstack)
1383 {
1384 struct coro temp;
1385 Zero (&temp, 1, struct coro);
1386 temp.save = CORO_SAVE_ALL;
1387
1388 if (!(coro->flags & CF_RUNNING))
1389 {
1390 save_perl (aTHX_ &temp);
1391 load_perl (aTHX_ coro);
1392 }
1393
1394 {
1395 dSP;
1396 ENTER;
1397 SAVETMPS;
1398 PUSHMARK (SP);
1399 PUTBACK;
1400 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1401 SPAGAIN;
1402 FREETMPS;
1403 LEAVE;
1404 PUTBACK;
1405 }
1406
1407 if (!(coro->flags & CF_RUNNING))
1408 {
1409 save_perl (aTHX_ coro);
1410 load_perl (aTHX_ &temp);
1411 }
1412 }
1413}
1414
1415SV *
1416is_ready (Coro::State coro)
1417 PROTOTYPE: $
1418 ALIAS:
1419 is_ready = CF_READY
1420 is_running = CF_RUNNING
1421 is_new = CF_NEW
1422 is_destroyed = CF_DESTROYED
1423 CODE:
1424 RETVAL = boolSV (coro->flags & ix);
1425 OUTPUT:
1426 RETVAL
1427
1428IV
1429rss (Coro::State coro)
1430 PROTOTYPE: $
1431 CODE:
1432 RETVAL = coro_rss (coro);
1433 OUTPUT:
1434 RETVAL
1435
1436
1312MODULE = Coro::State PACKAGE = Coro 1437MODULE = Coro::State PACKAGE = Coro
1313 1438
1314BOOT: 1439BOOT:
1315{ 1440{
1316 int i; 1441 int i;
1383 CODE: 1508 CODE:
1384 RETVAL = boolSV (api_ready (self)); 1509 RETVAL = boolSV (api_ready (self));
1385 OUTPUT: 1510 OUTPUT:
1386 RETVAL 1511 RETVAL
1387 1512
1388SV *
1389is_ready (SV *self)
1390 PROTOTYPE: $
1391 CODE:
1392 RETVAL = boolSV (api_is_ready (self));
1393 OUTPUT:
1394 RETVAL
1395
1396int 1513int
1397nready (...) 1514nready (...)
1398 PROTOTYPE: 1515 PROTOTYPE:
1399 CODE: 1516 CODE:
1400 RETVAL = coro_nready; 1517 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines