… | |
… | |
16 | # undef HAVE_MMAP |
16 | # undef HAVE_MMAP |
17 | # endif |
17 | # endif |
18 | # endif |
18 | # endif |
19 | #endif |
19 | #endif |
20 | |
20 | |
21 | #define MAY_FLUSH /* increases codesize */ |
21 | #define MAY_FLUSH /* increases codesize and is rarely used */ |
22 | |
22 | |
23 | #define SUB_INIT "Coro::State::initialize" |
23 | #define SUB_INIT "Coro::State::initialize" |
24 | #define UCORO_STATE "_coro_state" |
24 | #define UCORO_STATE "_coro_state" |
25 | |
25 | |
26 | /* The next macro should delcare 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 | |
30 | |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
… | |
… | |
270 | #endif |
270 | #endif |
271 | |
271 | |
272 | #define SB do { |
272 | #define SB do { |
273 | #define SE } while (0) |
273 | #define SE } while (0) |
274 | |
274 | |
275 | #define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE |
275 | #define LOAD(state) load_state(aTHX_ (state)); |
276 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE |
276 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
277 | |
277 | |
278 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
278 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
279 | |
279 | |
280 | static void |
280 | static void |
281 | load_state(pTHX_ Coro__State c) |
281 | load_state(pTHX_ Coro__State c) |
… | |
… | |
355 | |
355 | |
356 | PUSHs (Nullsv); |
356 | PUSHs (Nullsv); |
357 | /* this loop was inspired by pp_caller */ |
357 | /* this loop was inspired by pp_caller */ |
358 | for (;;) |
358 | for (;;) |
359 | { |
359 | { |
360 | do |
360 | while (cxix >= 0) |
361 | { |
361 | { |
362 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
362 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
363 | |
363 | |
364 | if (CxTYPE(cx) == CXt_SUB) |
364 | if (CxTYPE(cx) == CXt_SUB) |
365 | { |
365 | { |
… | |
… | |
391 | /* I never used formats, so how should I know how these are implemented? */ |
391 | /* I never used formats, so how should I know how these are implemented? */ |
392 | /* my bold guess is as a simple, plain sub... */ |
392 | /* my bold guess is as a simple, plain sub... */ |
393 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
393 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
394 | } |
394 | } |
395 | } |
395 | } |
396 | while (cxix >= 0); |
|
|
397 | |
396 | |
398 | if (top_si->si_type == PERLSI_MAIN) |
397 | if (top_si->si_type == PERLSI_MAIN) |
399 | break; |
398 | break; |
400 | |
399 | |
401 | top_si = top_si->si_prev; |
400 | top_si = top_si->si_prev; |
… | |
… | |
407 | } |
406 | } |
408 | |
407 | |
409 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
408 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
410 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
409 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
411 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
410 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
412 | |
|
|
413 | /* I have not the slightest idea of why av_reify is necessary */ |
|
|
414 | /* but if it's missing the defav contents magically get replaced sometimes */ |
|
|
415 | if (c->defav) |
|
|
416 | av_reify (c->defav); |
|
|
417 | |
411 | |
418 | c->dowarn = PL_dowarn; |
412 | c->dowarn = PL_dowarn; |
419 | c->in_eval = PL_in_eval; |
413 | c->in_eval = PL_in_eval; |
420 | |
414 | |
421 | c->curstackinfo = PL_curstackinfo; |
415 | c->curstackinfo = PL_curstackinfo; |
… | |
… | |
661 | } |
655 | } |
662 | |
656 | |
663 | STATIC void |
657 | STATIC void |
664 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
658 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
665 | { |
659 | { |
666 | dSP; |
|
|
667 | dSTACKLEVEL; |
660 | dSTACKLEVEL; |
|
|
661 | static struct coro *xnext; |
668 | |
662 | |
669 | if (prev != next) |
663 | if (prev != next) |
670 | { |
664 | { |
|
|
665 | xnext = next; |
|
|
666 | |
671 | if (next->mainstack) |
667 | if (next->mainstack) |
672 | { |
668 | { |
673 | SAVE (prev, flags); |
669 | SAVE (prev, flags); |
674 | LOAD (next); |
670 | LOAD (next); |
675 | |
671 | |
… | |
… | |
698 | continue_coro, (void *)next, |
694 | continue_coro, (void *)next, |
699 | next->stack->sptr, labs (next->stack->ssize)); |
695 | next->stack->sptr, labs (next->stack->ssize)); |
700 | } |
696 | } |
701 | |
697 | |
702 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
698 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
699 | /* don't add any code here */ |
703 | } |
700 | } |
704 | |
701 | |
705 | } |
702 | } |
706 | else if (next->tmps_ix == -2) |
703 | else if (next->tmps_ix == -2) |
707 | croak ("tried to transfer to running coroutine"); |
704 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
728 | allocate_stack (next, 1); |
725 | allocate_stack (next, 1); |
729 | coro_create (&(next->stack->cctx), |
726 | coro_create (&(next->stack->cctx), |
730 | setup_coro, (void *)next, |
727 | setup_coro, (void *)next, |
731 | next->stack->sptr, labs (next->stack->ssize)); |
728 | next->stack->sptr, labs (next->stack->ssize)); |
732 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
729 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
730 | /* don't add any code here */ |
733 | } |
731 | } |
734 | } |
732 | } |
735 | else |
733 | else |
736 | setup_coro (next); |
734 | setup_coro (next); |
737 | } |
735 | } |
738 | } |
736 | } |
739 | |
737 | |
|
|
738 | /* |
|
|
739 | * 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 |
|
|
741 | * variable, that should become thread-specific at one point. |
|
|
742 | */ |
740 | next->cursp = stacklevel; |
743 | xnext->cursp = stacklevel; |
741 | } |
744 | } |
742 | |
745 | |
743 | static struct coro * |
746 | static struct coro * |
744 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
747 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
745 | { |
748 | { |
… | |
… | |
920 | |
923 | |
921 | if (coro->mainstack && coro->mainstack != main_mainstack) |
924 | if (coro->mainstack && coro->mainstack != main_mainstack) |
922 | { |
925 | { |
923 | struct coro temp; |
926 | struct coro temp; |
924 | |
927 | |
|
|
928 | PUTBACK; |
925 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
929 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
926 | LOAD(aTHX_ coro); |
930 | LOAD(aTHX_ coro); |
|
|
931 | SPAGAIN; |
927 | |
932 | |
928 | destroy_stacks (aTHX); |
933 | destroy_stacks (aTHX); |
929 | |
934 | |
930 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
935 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
936 | SPAGAIN; |
931 | |
937 | |
932 | coro->mainstack = 0; |
938 | coro->mainstack = 0; |
933 | } |
939 | } |
934 | |
940 | |
935 | deallocate_stack (coro); |
941 | deallocate_stack (coro); |