… | |
… | |
25 | |
25 | |
26 | /* The next macro should declare a variable stacklevel that contains and approximation |
26 | /* The next macro should declare a variable stacklevel that contains and approximation |
27 | * to the current C stack pointer. Its property is that it changes with each call |
27 | * to the current C stack pointer. Its property is that it changes with each call |
28 | * and should be unique. */ |
28 | * and should be unique. */ |
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
|
|
30 | |
|
|
31 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
30 | |
32 | |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
33 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
32 | |
34 | |
33 | #include "CoroAPI.h" |
35 | #include "CoroAPI.h" |
34 | |
36 | |
… | |
… | |
487 | * still there is a memleak of 128 bytes... |
489 | * still there is a memleak of 128 bytes... |
488 | */ |
490 | */ |
489 | STATIC void |
491 | STATIC void |
490 | destroy_stacks(pTHX) |
492 | destroy_stacks(pTHX) |
491 | { |
493 | { |
|
|
494 | if (!IN_DESTRUCT) |
|
|
495 | { |
492 | /* is this ugly, I ask? */ |
496 | /* is this ugly, I ask? */ |
493 | while (PL_scopestack_ix) |
497 | while (PL_scopestack_ix) |
494 | LEAVE; |
498 | LEAVE; |
495 | |
499 | |
496 | /* sure it is, but more important: is it correct?? :/ */ |
500 | /* sure it is, but more important: is it correct?? :/ */ |
497 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
501 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
498 | FREETMPS; |
502 | FREETMPS; |
|
|
503 | } |
499 | |
504 | |
500 | while (PL_curstackinfo->si_next) |
505 | while (PL_curstackinfo->si_next) |
501 | PL_curstackinfo = PL_curstackinfo->si_next; |
506 | PL_curstackinfo = PL_curstackinfo->si_next; |
502 | |
507 | |
503 | while (PL_curstackinfo) |
508 | while (PL_curstackinfo) |
… | |
… | |
508 | dSP; |
513 | dSP; |
509 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
514 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
510 | PUTBACK; /* possibly superfluous */ |
515 | PUTBACK; /* possibly superfluous */ |
511 | } |
516 | } |
512 | |
517 | |
513 | if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ |
518 | if (!IN_DESTRUCT) |
|
|
519 | { |
514 | dounwind(-1); |
520 | dounwind(-1); |
515 | |
|
|
516 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
521 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
522 | } |
|
|
523 | |
517 | Safefree(PL_curstackinfo->si_cxstack); |
524 | Safefree(PL_curstackinfo->si_cxstack); |
518 | Safefree(PL_curstackinfo); |
525 | Safefree(PL_curstackinfo); |
519 | PL_curstackinfo = p; |
526 | PL_curstackinfo = p; |
520 | } |
527 | } |
521 | |
528 | |
… | |
… | |
643 | { |
650 | { |
644 | /* |
651 | /* |
645 | * this is a _very_ stripped down perl interpreter ;) |
652 | * this is a _very_ stripped down perl interpreter ;) |
646 | */ |
653 | */ |
647 | Coro__State ctx = (Coro__State)arg; |
654 | Coro__State ctx = (Coro__State)arg; |
|
|
655 | JMPENV coro_start_env; |
648 | |
656 | |
649 | /*FIXME*//* must set up top_env here */ |
657 | /* same as JMPENV_BOOTSTRAP */ |
|
|
658 | Zero(&coro_start_env, 1, JMPENV); |
|
|
659 | coro_start_env.je_ret = -1; |
|
|
660 | coro_start_env.je_mustcatch = TRUE; |
|
|
661 | PL_top_env = &coro_start_env; |
|
|
662 | |
650 | ctx->cursp = 0; |
663 | ctx->cursp = 0; |
651 | PL_op = PL_op->op_next; |
664 | PL_op = PL_op->op_next; |
652 | CALLRUNOPS(aTHX); |
665 | CALLRUNOPS(aTHX); |
653 | |
666 | |
654 | abort (); |
667 | abort (); |
… | |
… | |
731 | } |
744 | } |
732 | } |
745 | } |
733 | else |
746 | else |
734 | setup_coro (next); |
747 | setup_coro (next); |
735 | } |
748 | } |
736 | } |
|
|
737 | |
749 | |
738 | /* |
750 | /* |
739 | * xnext is now either prev or next, depending on wether |
751 | * xnext is now either prev or next, depending on wether |
740 | * we switched the c stack or not. that's why i use a global |
752 | * we switched the c stack or not. that's why i use a global |
741 | * variable, that should become thread-specific at one point. |
753 | * variable, that should become thread-specific at one point. |
742 | */ |
754 | */ |
743 | xnext->cursp = stacklevel; |
755 | xnext->cursp = stacklevel; |
|
|
756 | } |
744 | } |
757 | } |
745 | |
758 | |
746 | static struct coro * |
759 | static struct coro * |
747 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
760 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
748 | { |
761 | { |
… | |
… | |
755 | |
768 | |
756 | arg = HeVAL(he); |
769 | arg = HeVAL(he); |
757 | } |
770 | } |
758 | |
771 | |
759 | /* must also be changed inside Coro::Cont::yield */ |
772 | /* must also be changed inside Coro::Cont::yield */ |
|
|
773 | if (SvROK(arg) && SvOBJECT(SvRV(arg)) |
760 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
774 | && SvSTASH(SvRV(arg)) == coro_state_stash) |
761 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
775 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
762 | |
776 | |
763 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
777 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
764 | /*NORETURN*/ |
778 | /*NORETURN*/ |
765 | } |
779 | } |