… | |
… | |
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 */ |
… | |
… | |
109 | AV *mainstack; |
109 | AV *mainstack; |
110 | SV **stack_sp; |
110 | SV **stack_sp; |
111 | OP *op; |
111 | OP *op; |
112 | SV **curpad; |
112 | SV **curpad; |
113 | AV *comppad; |
113 | AV *comppad; |
|
|
114 | CV *compcv; |
114 | SV **stack_base; |
115 | SV **stack_base; |
115 | SV **stack_max; |
116 | SV **stack_max; |
116 | SV **tmps_stack; |
117 | SV **tmps_stack; |
117 | I32 tmps_floor; |
118 | I32 tmps_floor; |
118 | I32 tmps_ix; |
119 | I32 tmps_ix; |
… | |
… | |
270 | AV *av = (AV *)mg->mg_obj; |
271 | AV *av = (AV *)mg->mg_obj; |
271 | |
272 | |
272 | /* casting is fun. */ |
273 | /* casting is fun. */ |
273 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
274 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
274 | free_padlist (aTHX_ padlist); |
275 | free_padlist (aTHX_ padlist); |
|
|
276 | |
|
|
277 | SvREFCNT_dec (av); |
275 | } |
278 | } |
276 | |
279 | |
277 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
280 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
278 | |
281 | |
279 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
282 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
… | |
… | |
325 | PL_mainstack = c->mainstack; |
328 | PL_mainstack = c->mainstack; |
326 | PL_stack_sp = c->stack_sp; |
329 | PL_stack_sp = c->stack_sp; |
327 | PL_op = c->op; |
330 | PL_op = c->op; |
328 | PL_curpad = c->curpad; |
331 | PL_curpad = c->curpad; |
329 | PL_comppad = c->comppad; |
332 | PL_comppad = c->comppad; |
|
|
333 | PL_compcv = c->compcv; |
330 | PL_stack_base = c->stack_base; |
334 | PL_stack_base = c->stack_base; |
331 | PL_stack_max = c->stack_max; |
335 | PL_stack_max = c->stack_max; |
332 | PL_tmps_stack = c->tmps_stack; |
336 | PL_tmps_stack = c->tmps_stack; |
333 | PL_tmps_floor = c->tmps_floor; |
337 | PL_tmps_floor = c->tmps_floor; |
334 | PL_tmps_ix = c->tmps_ix; |
338 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
449 | c->mainstack = PL_mainstack; |
453 | c->mainstack = PL_mainstack; |
450 | c->stack_sp = PL_stack_sp; |
454 | c->stack_sp = PL_stack_sp; |
451 | c->op = PL_op; |
455 | c->op = PL_op; |
452 | c->curpad = PL_curpad; |
456 | c->curpad = PL_curpad; |
453 | c->comppad = PL_comppad; |
457 | c->comppad = PL_comppad; |
|
|
458 | c->compcv = PL_compcv; |
454 | c->stack_base = PL_stack_base; |
459 | c->stack_base = PL_stack_base; |
455 | c->stack_max = PL_stack_max; |
460 | c->stack_max = PL_stack_max; |
456 | c->tmps_stack = PL_tmps_stack; |
461 | c->tmps_stack = PL_tmps_stack; |
457 | c->tmps_floor = PL_tmps_floor; |
462 | c->tmps_floor = PL_tmps_floor; |
458 | c->tmps_ix = PL_tmps_ix; |
463 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
532 | /* is this ugly, I ask? */ |
537 | /* is this ugly, I ask? */ |
533 | LEAVE_SCOPE (0); |
538 | LEAVE_SCOPE (0); |
534 | |
539 | |
535 | /* sure it is, but more important: is it correct?? :/ */ |
540 | /* sure it is, but more important: is it correct?? :/ */ |
536 | FREETMPS; |
541 | FREETMPS; |
|
|
542 | |
|
|
543 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
537 | } |
544 | } |
538 | |
545 | |
539 | while (PL_curstackinfo->si_next) |
546 | while (PL_curstackinfo->si_next) |
540 | PL_curstackinfo = PL_curstackinfo->si_next; |
547 | PL_curstackinfo = PL_curstackinfo->si_next; |
541 | |
548 | |
542 | while (PL_curstackinfo) |
549 | while (PL_curstackinfo) |
543 | { |
550 | { |
544 | PERL_SI *p = PL_curstackinfo->si_prev; |
551 | PERL_SI *p = PL_curstackinfo->si_prev; |
545 | |
552 | |
546 | { |
553 | { /*D*//*remove*/ |
547 | dSP; |
554 | dSP; |
548 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
555 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
549 | PUTBACK; /* possibly superfluous */ |
556 | PUTBACK; /* possibly superfluous */ |
550 | } |
557 | } |
551 | |
558 | |
552 | if (!IN_DESTRUCT) |
559 | if (!IN_DESTRUCT) |
553 | { |
560 | { |
554 | dounwind(-1); |
561 | dounwind (-1);/*D*//*remove*/ |
555 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
562 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
556 | } |
563 | } |
557 | |
564 | |
558 | Safefree(PL_curstackinfo->si_cxstack); |
565 | Safefree (PL_curstackinfo->si_cxstack); |
559 | Safefree(PL_curstackinfo); |
566 | Safefree (PL_curstackinfo); |
560 | PL_curstackinfo = p; |
567 | PL_curstackinfo = p; |
561 | } |
568 | } |
562 | |
569 | |
563 | Safefree(PL_tmps_stack); |
570 | Safefree (PL_tmps_stack); |
564 | Safefree(PL_markstack); |
571 | Safefree (PL_markstack); |
565 | Safefree(PL_scopestack); |
572 | Safefree (PL_scopestack); |
566 | Safefree(PL_savestack); |
573 | Safefree (PL_savestack); |
567 | Safefree(PL_retstack); |
574 | Safefree (PL_retstack); |
568 | } |
575 | } |
569 | |
576 | |
570 | static void |
577 | static void |
571 | allocate_stack (Coro__State ctx, int alloc) |
578 | allocate_stack (Coro__State ctx, int alloc) |
572 | { |
579 | { |
… | |
… | |
977 | croak ("Coro::State::_newprocess expects an arrayref"); |
984 | croak ("Coro::State::_newprocess expects an arrayref"); |
978 | |
985 | |
979 | Newz (0, coro, 1, struct coro); |
986 | Newz (0, coro, 1, struct coro); |
980 | |
987 | |
981 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
988 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
982 | coro->mainstack = 0; /* actual work is done inside transfer */ |
989 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
983 | coro->stack = 0; |
990 | /*coro->stack = 0;*/ |
984 | |
991 | |
985 | /* same as JMPENV_BOOTSTRAP */ |
992 | /* same as JMPENV_BOOTSTRAP */ |
986 | /* we might be able to recycle start_env, but safe is safe */ |
993 | /* we might be able to recycle start_env, but safe is safe */ |
987 | //Zero(&coro->start_env, 1, JMPENV); |
994 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
988 | coro->start_env.je_ret = -1; |
995 | coro->start_env.je_ret = -1; |
989 | coro->start_env.je_mustcatch = TRUE; |
996 | coro->start_env.je_mustcatch = TRUE; |
990 | |
997 | |
991 | RETVAL = coro; |
998 | RETVAL = coro; |
992 | OUTPUT: |
999 | OUTPUT: |
… | |
… | |
1013 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1020 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1014 | { |
1021 | { |
1015 | struct coro temp; |
1022 | struct coro temp; |
1016 | |
1023 | |
1017 | PUTBACK; |
1024 | PUTBACK; |
1018 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1025 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1019 | LOAD(aTHX_ coro); |
1026 | LOAD (aTHX_ coro); |
1020 | SPAGAIN; |
1027 | SPAGAIN; |
1021 | |
1028 | |
1022 | destroy_stacks (aTHX); |
1029 | destroy_stacks (aTHX); |
1023 | |
1030 | |
1024 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
1031 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
1025 | SPAGAIN; |
1032 | SPAGAIN; |
1026 | |
1033 | |
1027 | coro->mainstack = 0; |
1034 | coro->mainstack = 0; |
1028 | } |
1035 | } |
1029 | |
1036 | |
… | |
… | |
1058 | av_clear (defav); |
1065 | av_clear (defav); |
1059 | av_fill (defav, items - 1); |
1066 | av_fill (defav, items - 1); |
1060 | while (items--) |
1067 | while (items--) |
1061 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1068 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1062 | |
1069 | |
1063 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
1070 | SvGETMAGIC (returnstk); /* isn't documentation wrong for mg_get? */ |
1064 | sv = av_pop ((AV *)SvRV (returnstk)); |
1071 | sv = av_pop ((AV *)SvRV (returnstk)); |
1065 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1072 | 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))); |
1073 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1067 | SvREFCNT_dec (sv); |
1074 | SvREFCNT_dec (sv); |
1068 | |
1075 | |
1069 | transfer(aTHX_ prev, next, 0); |
1076 | transfer (aTHX_ prev, next, 0); |
1070 | |
1077 | |
1071 | MODULE = Coro::State PACKAGE = Coro |
1078 | MODULE = Coro::State PACKAGE = Coro |
1072 | |
1079 | |
1073 | # this is slightly dirty (should expose a c-level api) |
1080 | # this is slightly dirty (should expose a c-level api) |
1074 | |
1081 | |