… | |
… | |
69 | static struct CoroAPI coroapi; |
69 | static struct CoroAPI coroapi; |
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
71 | static HV *coro_state_stash; |
71 | static HV *coro_state_stash; |
72 | static SV *ucoro_state_sv; |
72 | static SV *ucoro_state_sv; |
73 | static U32 ucoro_state_hash; |
73 | static U32 ucoro_state_hash; |
74 | static __thread SV *coro_mortal; /* will be freed after next transfer */ |
74 | static SV *coro_mortal; /* will be freed after next transfer */ |
75 | |
75 | |
76 | /* this is actually not only the c stack but also c registers etc... */ |
76 | /* this is actually not only the c stack but also c registers etc... */ |
77 | typedef struct { |
77 | typedef struct { |
78 | int refcnt; /* pointer reference counter */ |
78 | int refcnt; /* pointer reference counter */ |
79 | int usecnt; /* shared by how many coroutines */ |
79 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
532 | /* is this ugly, I ask? */ |
532 | /* is this ugly, I ask? */ |
533 | LEAVE_SCOPE (0); |
533 | LEAVE_SCOPE (0); |
534 | |
534 | |
535 | /* sure it is, but more important: is it correct?? :/ */ |
535 | /* sure it is, but more important: is it correct?? :/ */ |
536 | FREETMPS; |
536 | FREETMPS; |
|
|
537 | |
|
|
538 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
537 | } |
539 | } |
538 | |
540 | |
539 | while (PL_curstackinfo->si_next) |
541 | while (PL_curstackinfo->si_next) |
540 | PL_curstackinfo = PL_curstackinfo->si_next; |
542 | PL_curstackinfo = PL_curstackinfo->si_next; |
541 | |
543 | |
542 | while (PL_curstackinfo) |
544 | while (PL_curstackinfo) |
543 | { |
545 | { |
544 | PERL_SI *p = PL_curstackinfo->si_prev; |
546 | PERL_SI *p = PL_curstackinfo->si_prev; |
545 | |
547 | |
546 | { |
548 | { /*D*//*remove*/ |
547 | dSP; |
549 | dSP; |
548 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
550 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
549 | PUTBACK; /* possibly superfluous */ |
551 | PUTBACK; /* possibly superfluous */ |
550 | } |
552 | } |
551 | |
553 | |
552 | if (!IN_DESTRUCT) |
554 | if (!IN_DESTRUCT) |
553 | { |
555 | { |
554 | dounwind(-1); |
556 | dounwind (-1);/*D*//*remove*/ |
555 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
557 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
556 | } |
558 | } |
557 | |
559 | |
558 | Safefree(PL_curstackinfo->si_cxstack); |
560 | Safefree (PL_curstackinfo->si_cxstack); |
559 | Safefree(PL_curstackinfo); |
561 | Safefree (PL_curstackinfo); |
560 | PL_curstackinfo = p; |
562 | PL_curstackinfo = p; |
561 | } |
563 | } |
562 | |
564 | |
563 | Safefree(PL_tmps_stack); |
565 | Safefree (PL_tmps_stack); |
564 | Safefree(PL_markstack); |
566 | Safefree (PL_markstack); |
565 | Safefree(PL_scopestack); |
567 | Safefree (PL_scopestack); |
566 | Safefree(PL_savestack); |
568 | Safefree (PL_savestack); |
567 | Safefree(PL_retstack); |
569 | Safefree (PL_retstack); |
568 | } |
570 | } |
569 | |
571 | |
570 | static void |
572 | static void |
571 | allocate_stack (Coro__State ctx, int alloc) |
573 | allocate_stack (Coro__State ctx, int alloc) |
572 | { |
574 | { |
… | |
… | |
977 | croak ("Coro::State::_newprocess expects an arrayref"); |
979 | croak ("Coro::State::_newprocess expects an arrayref"); |
978 | |
980 | |
979 | Newz (0, coro, 1, struct coro); |
981 | Newz (0, coro, 1, struct coro); |
980 | |
982 | |
981 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
983 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
982 | coro->mainstack = 0; /* actual work is done inside transfer */ |
984 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
983 | coro->stack = 0; |
985 | /*coro->stack = 0;*/ |
984 | |
986 | |
985 | /* same as JMPENV_BOOTSTRAP */ |
987 | /* same as JMPENV_BOOTSTRAP */ |
986 | /* we might be able to recycle start_env, but safe is safe */ |
988 | /* we might be able to recycle start_env, but safe is safe */ |
987 | //Zero(&coro->start_env, 1, JMPENV); |
989 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
988 | coro->start_env.je_ret = -1; |
990 | coro->start_env.je_ret = -1; |
989 | coro->start_env.je_mustcatch = TRUE; |
991 | coro->start_env.je_mustcatch = TRUE; |
990 | |
992 | |
991 | RETVAL = coro; |
993 | RETVAL = coro; |
992 | OUTPUT: |
994 | OUTPUT: |
… | |
… | |
1013 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1015 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1014 | { |
1016 | { |
1015 | struct coro temp; |
1017 | struct coro temp; |
1016 | |
1018 | |
1017 | PUTBACK; |
1019 | PUTBACK; |
1018 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1020 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1019 | LOAD(aTHX_ coro); |
1021 | LOAD (aTHX_ coro); |
1020 | SPAGAIN; |
1022 | SPAGAIN; |
1021 | |
1023 | |
1022 | destroy_stacks (aTHX); |
1024 | destroy_stacks (aTHX); |
1023 | |
1025 | |
1024 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
1026 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
1025 | SPAGAIN; |
1027 | SPAGAIN; |
1026 | |
1028 | |
1027 | coro->mainstack = 0; |
1029 | coro->mainstack = 0; |
1028 | } |
1030 | } |
1029 | |
1031 | |
… | |
… | |
1058 | av_clear (defav); |
1060 | av_clear (defav); |
1059 | av_fill (defav, items - 1); |
1061 | av_fill (defav, items - 1); |
1060 | while (items--) |
1062 | while (items--) |
1061 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1063 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1062 | |
1064 | |
1063 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
1065 | SvGETMAGIC (returnstk); /* isn't documentation wrong for mg_get? */ |
1064 | sv = av_pop ((AV *)SvRV (returnstk)); |
1066 | sv = av_pop ((AV *)SvRV (returnstk)); |
1065 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1067 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1066 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1068 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1067 | SvREFCNT_dec (sv); |
1069 | SvREFCNT_dec (sv); |
1068 | |
1070 | |
1069 | transfer(aTHX_ prev, next, 0); |
1071 | transfer (aTHX_ prev, next, 0); |
1070 | |
1072 | |
1071 | MODULE = Coro::State PACKAGE = Coro |
1073 | MODULE = Coro::State PACKAGE = Coro |
1072 | |
1074 | |
1073 | # this is slightly dirty (should expose a c-level api) |
1075 | # this is slightly dirty (should expose a c-level api) |
1074 | |
1076 | |