… | |
… | |
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) |
… | |
… | |
408 | |
408 | |
409 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
409 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
410 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
410 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
411 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
411 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
412 | |
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 | |
|
|
418 | c->dowarn = PL_dowarn; |
413 | c->dowarn = PL_dowarn; |
419 | c->in_eval = PL_in_eval; |
414 | c->in_eval = PL_in_eval; |
420 | |
415 | |
421 | c->curstackinfo = PL_curstackinfo; |
416 | c->curstackinfo = PL_curstackinfo; |
422 | c->curstack = PL_curstack; |
417 | c->curstack = PL_curstack; |
… | |
… | |
568 | |
563 | |
569 | ctx->stack = 0; |
564 | ctx->stack = 0; |
570 | |
565 | |
571 | if (stack) |
566 | if (stack) |
572 | { |
567 | { |
|
|
568 | printf ("deallocating stack %p %d\n", stack, stack->refcnt);/*D*/ |
573 | if (!--stack->refcnt) |
569 | if (!--stack->refcnt) |
574 | { |
570 | { |
575 | #ifdef HAVE_MMAP |
571 | #ifdef HAVE_MMAP |
576 | if (stack->ssize > 0 && stack->sptr) |
572 | if (stack->ssize > 0 && stack->sptr) |
577 | munmap (stack->sptr, stack->ssize); |
573 | munmap (stack->sptr, stack->ssize); |
… | |
… | |
661 | } |
657 | } |
662 | |
658 | |
663 | STATIC void |
659 | STATIC void |
664 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
660 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
665 | { |
661 | { |
666 | dSP; |
|
|
667 | dSTACKLEVEL; |
662 | dSTACKLEVEL; |
|
|
663 | static struct coro *xnext; |
668 | |
664 | |
|
|
665 | printf ("%p => %p\n", prev, next);/*D*/ |
669 | if (prev != next) |
666 | if (prev != next) |
670 | { |
667 | { |
|
|
668 | xnext = next; |
|
|
669 | |
671 | if (next->mainstack) |
670 | if (next->mainstack) |
672 | { |
671 | { |
673 | SAVE (prev, flags); |
672 | SAVE (prev, flags); |
674 | LOAD (next); |
673 | LOAD (next); |
675 | |
674 | |
… | |
… | |
698 | continue_coro, (void *)next, |
697 | continue_coro, (void *)next, |
699 | next->stack->sptr, labs (next->stack->ssize)); |
698 | next->stack->sptr, labs (next->stack->ssize)); |
700 | } |
699 | } |
701 | |
700 | |
702 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
701 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
702 | /* don't add any code here */ |
703 | } |
703 | } |
704 | |
704 | |
705 | } |
705 | } |
706 | else if (next->tmps_ix == -2) |
706 | else if (next->tmps_ix == -2) |
707 | croak ("tried to transfer to running coroutine"); |
707 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
728 | allocate_stack (next, 1); |
728 | allocate_stack (next, 1); |
729 | coro_create (&(next->stack->cctx), |
729 | coro_create (&(next->stack->cctx), |
730 | setup_coro, (void *)next, |
730 | setup_coro, (void *)next, |
731 | next->stack->sptr, labs (next->stack->ssize)); |
731 | next->stack->sptr, labs (next->stack->ssize)); |
732 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
732 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
733 | /* don't add any code here */ |
733 | } |
734 | } |
734 | } |
735 | } |
735 | else |
736 | else |
736 | setup_coro (next); |
737 | setup_coro (next); |
737 | } |
738 | } |
738 | } |
739 | } |
739 | |
740 | |
740 | next->cursp = stacklevel; |
741 | xnext->cursp = stacklevel; |
741 | } |
742 | } |
742 | |
743 | |
743 | static struct coro * |
744 | static struct coro * |
744 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
745 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
745 | { |
746 | { |
… | |
… | |
920 | |
921 | |
921 | if (coro->mainstack && coro->mainstack != main_mainstack) |
922 | if (coro->mainstack && coro->mainstack != main_mainstack) |
922 | { |
923 | { |
923 | struct coro temp; |
924 | struct coro temp; |
924 | |
925 | |
|
|
926 | PUTBACK; |
925 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
927 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
926 | LOAD(aTHX_ coro); |
928 | LOAD(aTHX_ coro); |
|
|
929 | SPAGAIN; |
927 | |
930 | |
928 | destroy_stacks (aTHX); |
931 | destroy_stacks (aTHX); |
929 | |
932 | |
930 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
933 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
934 | SPAGAIN; |
931 | |
935 | |
932 | coro->mainstack = 0; |
936 | coro->mainstack = 0; |
933 | } |
937 | } |
934 | |
938 | |
935 | deallocate_stack (coro); |
939 | deallocate_stack (coro); |