… | |
… | |
10 | #include "patchlevel.h" |
10 | #include "patchlevel.h" |
11 | |
11 | |
12 | #include <stdio.h> |
12 | #include <stdio.h> |
13 | #include <errno.h> |
13 | #include <errno.h> |
14 | #include <assert.h> |
14 | #include <assert.h> |
|
|
15 | #include <inttypes.h> /* portable stdint.h */ |
15 | |
16 | |
16 | #ifdef HAVE_MMAP |
17 | #ifdef HAVE_MMAP |
17 | # include <unistd.h> |
18 | # include <unistd.h> |
18 | # include <sys/mman.h> |
19 | # include <sys/mman.h> |
19 | # ifndef MAP_ANONYMOUS |
20 | # ifndef MAP_ANONYMOUS |
… | |
… | |
130 | #else |
131 | #else |
131 | # define LOCK (void)0 |
132 | # define LOCK (void)0 |
132 | # define UNLOCK (void)0 |
133 | # define UNLOCK (void)0 |
133 | #endif |
134 | #endif |
134 | |
135 | |
|
|
136 | #define strpair(const) const, sizeof (const) - 1 |
|
|
137 | |
135 | /* helper storage struct for Coro::AIO */ |
138 | /* helper storage struct for Coro::AIO */ |
136 | struct io_state |
139 | struct io_state |
137 | { |
140 | { |
138 | int errorno; |
141 | int errorno; |
139 | I32 laststype; |
142 | I32 laststype; |
… | |
… | |
148 | static HV *coro_state_stash, *coro_stash; |
151 | static HV *coro_state_stash, *coro_stash; |
149 | static SV *coro_mortal; /* will be freed after next transfer */ |
152 | static SV *coro_mortal; /* will be freed after next transfer */ |
150 | |
153 | |
151 | static GV *irsgv; /* $/ */ |
154 | static GV *irsgv; /* $/ */ |
152 | static GV *stdoutgv; /* *STDOUT */ |
155 | static GV *stdoutgv; /* *STDOUT */ |
|
|
156 | |
|
|
157 | static HV *hv_sig; /* %SIG */ |
153 | static SV *sv_diehook; |
158 | static SV *sv_diehook; |
154 | static SV *sv_warnhook; |
159 | static SV *sv_warnhook; |
155 | |
160 | |
156 | /* async_pool helper stuff */ |
161 | /* async_pool helper stuff */ |
157 | static SV *sv_pool_rss; |
162 | static SV *sv_pool_rss; |
158 | static SV *sv_pool_size; |
163 | static SV *sv_pool_size; |
159 | static AV *av_async_pool; |
164 | static AV *av_async_pool; |
160 | |
165 | |
161 | static struct coro_cctx *cctx_first; |
166 | static struct coro_cctx *cctx_first[3]; /* index by GIMME_V type, void, scalar, array */ |
162 | static int cctx_count, cctx_idle; |
167 | static int cctx_count, cctx_idle[3]; |
163 | |
168 | |
164 | enum { |
169 | enum { |
165 | CC_MAPPED = 0x01, |
170 | CC_MAPPED = 0x01, |
166 | CC_NOREUSE = 0x02, /* throw this away after tracing */ |
171 | CC_NOREUSE = 0x02, /* throw this away after tracing */ |
167 | CC_TRACE = 0x04, |
172 | CC_TRACE = 0x04, |
… | |
… | |
212 | |
217 | |
213 | /* this is a structure representing a perl-level coroutine */ |
218 | /* this is a structure representing a perl-level coroutine */ |
214 | struct coro { |
219 | struct coro { |
215 | /* the c coroutine allocated to this perl coroutine, if any */ |
220 | /* the c coroutine allocated to this perl coroutine, if any */ |
216 | coro_cctx *cctx; |
221 | coro_cctx *cctx; |
|
|
222 | int gimme; |
217 | |
223 | |
218 | /* process data */ |
224 | /* process data */ |
219 | AV *mainstack; |
225 | AV *mainstack; |
220 | perl_slots *slot; /* basically the saved sp */ |
226 | perl_slots *slot; /* basically the saved sp */ |
221 | |
227 | |
222 | /* data associated with this coroutine (initial args) */ |
228 | AV *args; /* data associated with this coroutine (initial args) */ |
223 | AV *args; |
229 | int refcnt; /* coroutines are refcounted, yes */ |
224 | int refcnt; |
|
|
225 | int flags; /* CF_ flags */ |
230 | int flags; /* CF_ flags */ |
|
|
231 | HV *hv; /* the perl hash associated with this coro, if any */ |
226 | |
232 | |
227 | /* statistics */ |
233 | /* statistics */ |
228 | int usecount; /* number of transfers to this coro */ |
234 | int usecount; /* number of transfers to this coro */ |
229 | |
235 | |
230 | /* coro process data */ |
236 | /* coro process data */ |
231 | int prio; |
237 | int prio; |
232 | SV *throw; |
238 | SV *throw; /* exception to be thrown */ |
233 | |
239 | |
234 | /* async_pool */ |
240 | /* async_pool */ |
235 | SV *saved_deffh; |
241 | SV *saved_deffh; |
236 | |
242 | |
237 | /* linked list */ |
243 | /* linked list */ |
238 | struct coro *next, *prev; |
244 | struct coro *next, *prev; |
239 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
240 | }; |
245 | }; |
241 | |
246 | |
242 | typedef struct coro *Coro__State; |
247 | typedef struct coro *Coro__State; |
243 | typedef struct coro *Coro__State_or_hashref; |
248 | typedef struct coro *Coro__State_or_hashref; |
244 | |
249 | |
… | |
… | |
267 | get_sv (name, create); |
272 | get_sv (name, create); |
268 | #endif |
273 | #endif |
269 | return get_sv (name, create); |
274 | return get_sv (name, create); |
270 | } |
275 | } |
271 | |
276 | |
272 | static SV * |
277 | static AV * |
273 | coro_get_av (const char *name, int create) |
278 | coro_get_av (const char *name, int create) |
274 | { |
279 | { |
275 | #if PERL_VERSION_ATLEAST (5,9,0) |
280 | #if PERL_VERSION_ATLEAST (5,9,0) |
276 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
281 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
277 | get_av (name, create); |
282 | get_av (name, create); |
278 | #endif |
283 | #endif |
279 | return get_av (name, create); |
284 | return get_av (name, create); |
|
|
285 | } |
|
|
286 | |
|
|
287 | static HV * |
|
|
288 | coro_get_hv (const char *name, int create) |
|
|
289 | { |
|
|
290 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
291 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
292 | get_hv (name, create); |
|
|
293 | #endif |
|
|
294 | return get_hv (name, create); |
280 | } |
295 | } |
281 | |
296 | |
282 | static AV * |
297 | static AV * |
283 | coro_clone_padlist (pTHX_ CV *cv) |
298 | coro_clone_padlist (pTHX_ CV *cv) |
284 | { |
299 | { |
… | |
… | |
439 | |
454 | |
440 | #define VAR(name,type) PL_ ## name = slot->name; |
455 | #define VAR(name,type) PL_ ## name = slot->name; |
441 | # include "state.h" |
456 | # include "state.h" |
442 | #undef VAR |
457 | #undef VAR |
443 | |
458 | |
|
|
459 | /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/ |
|
|
460 | /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/ |
|
|
461 | |
444 | { |
462 | { |
445 | dSP; |
463 | dSP; |
446 | |
464 | |
447 | CV *cv; |
465 | CV *cv; |
448 | |
466 | |
… | |
… | |
507 | |
525 | |
508 | PUTBACK; |
526 | PUTBACK; |
509 | } |
527 | } |
510 | |
528 | |
511 | /* allocate some space on the context stack for our purposes */ |
529 | /* allocate some space on the context stack for our purposes */ |
|
|
530 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
531 | if (SLOT_COUNT >= 1) CXINC; |
|
|
532 | if (SLOT_COUNT >= 2) CXINC; |
|
|
533 | if (SLOT_COUNT >= 3) CXINC; |
512 | { |
534 | { |
513 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
514 | int i; |
535 | int i; |
515 | if (SLOT_COUNT >= 1) CXINC; |
|
|
516 | if (SLOT_COUNT >= 2) CXINC; |
|
|
517 | if (SLOT_COUNT >= 3) CXINC; |
|
|
518 | for (i = 3; i < SLOT_COUNT; ++i) |
536 | for (i = 3; i < SLOT_COUNT; ++i) |
519 | CXINC; |
537 | CXINC; |
520 | |
|
|
521 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
522 | } |
538 | } |
|
|
539 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
523 | |
540 | |
524 | c->mainstack = PL_mainstack; |
541 | c->mainstack = PL_mainstack; |
525 | |
542 | |
526 | { |
543 | { |
527 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
544 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
671 | PL_curpm = 0; |
688 | PL_curpm = 0; |
672 | PL_curpad = 0; |
689 | PL_curpad = 0; |
673 | PL_localizing = 0; |
690 | PL_localizing = 0; |
674 | PL_dirty = 0; |
691 | PL_dirty = 0; |
675 | PL_restartop = 0; |
692 | PL_restartop = 0; |
676 | PL_diehook = SvREFCNT_inc (sv_diehook); |
693 | PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
677 | PL_warnhook = SvREFCNT_inc (sv_warnhook); |
694 | PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
678 | |
695 | |
679 | GvSV (PL_defgv) = newSV (0); |
696 | GvSV (PL_defgv) = newSV (0); |
680 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
697 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
681 | GvSV (PL_errgv) = newSV (0); |
698 | GvSV (PL_errgv) = newSV (0); |
682 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
699 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
695 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
712 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
696 | PUTBACK; |
713 | PUTBACK; |
697 | PL_op = (OP *)&myop; |
714 | PL_op = (OP *)&myop; |
698 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
715 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
699 | SPAGAIN; |
716 | SPAGAIN; |
|
|
717 | |
|
|
718 | /* |
|
|
719 | * now its very tricky. the "tail" of the next transfer might end up |
|
|
720 | * either in a new cctx, or an existing one. |
|
|
721 | * in case of an existing one we have to take care of whatever |
|
|
722 | * entersub and transfer do to the perl stack. |
|
|
723 | */ |
|
|
724 | ENTER; |
|
|
725 | EXTEND (SP, 4); |
|
|
726 | PUSHs ((SV *)0); /* items */ |
|
|
727 | PUSHs ((SV *)0); /* ix, set_stacklevel */ |
|
|
728 | PUSHs ((SV *)(sp - PL_stack_base + 1)); /* ax */ |
|
|
729 | PUSHs ((SV *)0); /* again */ |
|
|
730 | PUTBACK; |
700 | } |
731 | } |
701 | |
732 | |
702 | ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ |
733 | /* this newly created coroutine might be run on an existing cctx which most |
|
|
734 | * likely was suspended in set_stacklevel, called from entersub. |
|
|
735 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
|
|
736 | * so we ENTER here for symmetry |
|
|
737 | */ |
|
|
738 | ENTER; |
703 | } |
739 | } |
704 | |
740 | |
705 | static void |
741 | static void |
706 | coro_destroy (pTHX_ struct coro *coro) |
742 | coro_destroy (pTHX_ struct coro *coro) |
707 | { |
743 | { |
… | |
… | |
791 | PUSHMARK (SP); |
827 | PUSHMARK (SP); |
792 | PUSHs (&PL_sv_no); |
828 | PUSHs (&PL_sv_no); |
793 | PUSHs (fullname); |
829 | PUSHs (fullname); |
794 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
830 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
795 | PUTBACK; |
831 | PUTBACK; |
796 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
832 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
797 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
833 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
798 | SPAGAIN; |
834 | SPAGAIN; |
799 | FREETMPS; |
835 | FREETMPS; |
800 | LEAVE; |
836 | LEAVE; |
801 | PL_runops = runops_trace; |
837 | PL_runops = runops_trace; |
… | |
… | |
830 | PUSHMARK (SP); |
866 | PUSHMARK (SP); |
831 | PUSHs (&PL_sv_yes); |
867 | PUSHs (&PL_sv_yes); |
832 | PUSHs (fullname); |
868 | PUSHs (fullname); |
833 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
869 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
834 | PUTBACK; |
870 | PUTBACK; |
835 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
871 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
836 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
872 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
837 | SPAGAIN; |
873 | SPAGAIN; |
838 | FREETMPS; |
874 | FREETMPS; |
839 | LEAVE; |
875 | LEAVE; |
840 | PL_runops = runops_trace; |
876 | PL_runops = runops_trace; |
… | |
… | |
854 | PL_runops = RUNOPS_DEFAULT; |
890 | PL_runops = RUNOPS_DEFAULT; |
855 | PUSHMARK (SP); |
891 | PUSHMARK (SP); |
856 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
892 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
857 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
893 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
858 | PUTBACK; |
894 | PUTBACK; |
859 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
895 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); |
860 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
896 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
861 | SPAGAIN; |
897 | SPAGAIN; |
862 | FREETMPS; |
898 | FREETMPS; |
863 | LEAVE; |
899 | LEAVE; |
864 | PL_runops = runops_trace; |
900 | PL_runops = runops_trace; |
… | |
… | |
874 | |
910 | |
875 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
911 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
876 | /* _cctx_init should be careful, as it could be called at almost any time */ |
912 | /* _cctx_init should be careful, as it could be called at almost any time */ |
877 | /* during execution of a perl program */ |
913 | /* during execution of a perl program */ |
878 | static void NOINLINE |
914 | static void NOINLINE |
879 | prepare_cctx (pTHX_ coro_cctx *cctx) |
915 | cctx_prepare (pTHX_ coro_cctx *cctx) |
880 | { |
916 | { |
881 | dSP; |
917 | dSP; |
882 | LOGOP myop; |
918 | LOGOP myop; |
883 | |
919 | |
884 | PL_top_env = &PL_start_env; |
920 | PL_top_env = &PL_start_env; |
… | |
… | |
902 | |
938 | |
903 | /* |
939 | /* |
904 | * this is a _very_ stripped down perl interpreter ;) |
940 | * this is a _very_ stripped down perl interpreter ;) |
905 | */ |
941 | */ |
906 | static void |
942 | static void |
907 | coro_run (void *arg) |
943 | cctx_run (void *arg) |
908 | { |
944 | { |
909 | dTHX; |
945 | dTHX; |
910 | |
946 | |
911 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
947 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
912 | UNLOCK; |
948 | UNLOCK; |
913 | |
949 | |
914 | /* we now skip the entersub that lead to transfer() */ |
950 | /* we now skip the entersub that lead to transfer() */ |
915 | PL_op = PL_op->op_next; |
951 | PL_op = PL_op->op_next; |
916 | |
952 | |
917 | /* inject a fake subroutine call to cctx_init */ |
953 | /* inject a fake subroutine call to cctx_init */ |
918 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
954 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
919 | |
955 | |
920 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
956 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
921 | PL_restartop = PL_op; |
957 | PL_restartop = PL_op; |
922 | perl_run (PL_curinterp); |
958 | perl_run (PL_curinterp); |
923 | |
959 | |
… | |
… | |
942 | ++cctx_count; |
978 | ++cctx_count; |
943 | |
979 | |
944 | Newz (0, cctx, 1, coro_cctx); |
980 | Newz (0, cctx, 1, coro_cctx); |
945 | |
981 | |
946 | #if HAVE_MMAP |
982 | #if HAVE_MMAP |
947 | |
|
|
948 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
983 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
949 | /* mmap supposedly does allocate-on-write for us */ |
984 | /* mmap supposedly does allocate-on-write for us */ |
950 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
985 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
951 | |
986 | |
952 | if (cctx->sptr != (void *)-1) |
987 | if (cctx->sptr != (void *)-1) |
… | |
… | |
973 | stack_start = cctx->sptr; |
1008 | stack_start = cctx->sptr; |
974 | stack_size = cctx->ssize; |
1009 | stack_size = cctx->ssize; |
975 | } |
1010 | } |
976 | |
1011 | |
977 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1012 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
978 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
1013 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
979 | |
1014 | |
980 | return cctx; |
1015 | return cctx; |
981 | } |
1016 | } |
982 | |
1017 | |
983 | static void |
1018 | static void |
… | |
… | |
1004 | |
1039 | |
1005 | /* wether this cctx should be destructed */ |
1040 | /* wether this cctx should be destructed */ |
1006 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1041 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1007 | |
1042 | |
1008 | static coro_cctx * |
1043 | static coro_cctx * |
1009 | cctx_get (pTHX) |
1044 | cctx_get (pTHX_ int gimme) |
1010 | { |
1045 | { |
1011 | while (expect_true (cctx_first)) |
1046 | while (expect_true (cctx_first[gimme])) |
1012 | { |
1047 | { |
1013 | coro_cctx *cctx = cctx_first; |
1048 | coro_cctx *cctx = cctx_first[gimme]; |
1014 | cctx_first = cctx->next; |
1049 | cctx_first[gimme] = cctx->next; |
1015 | --cctx_idle; |
1050 | --cctx_idle[gimme]; |
1016 | |
1051 | |
1017 | if (expect_true (!CCTX_EXPIRED (cctx))) |
1052 | if (expect_true (!CCTX_EXPIRED (cctx))) |
1018 | return cctx; |
1053 | return cctx; |
1019 | |
1054 | |
1020 | cctx_destroy (cctx); |
1055 | cctx_destroy (cctx); |
1021 | } |
1056 | } |
1022 | |
1057 | |
|
|
1058 | assert (!gimme); |
1023 | return cctx_new (); |
1059 | return cctx_new (); |
1024 | } |
1060 | } |
1025 | |
1061 | |
1026 | static void |
1062 | static void |
1027 | cctx_put (coro_cctx *cctx) |
1063 | cctx_put (coro_cctx *cctx, int gimme) |
1028 | { |
1064 | { |
1029 | /* free another cctx if overlimit */ |
1065 | /* free another cctx if overlimit */ |
1030 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1066 | if (expect_false (cctx_idle[gimme] >= MAX_IDLE_CCTX)) |
1031 | { |
1067 | { |
1032 | coro_cctx *first = cctx_first; |
1068 | coro_cctx *first = cctx_first[gimme]; |
1033 | cctx_first = first->next; |
1069 | cctx_first[gimme] = first->next; |
1034 | --cctx_idle; |
1070 | --cctx_idle[gimme]; |
1035 | |
1071 | |
1036 | cctx_destroy (first); |
1072 | cctx_destroy (first); |
1037 | } |
1073 | } |
1038 | |
1074 | |
1039 | ++cctx_idle; |
1075 | ++cctx_idle[gimme]; |
1040 | cctx->next = cctx_first; |
1076 | cctx->next = cctx_first[gimme]; |
1041 | cctx_first = cctx; |
1077 | cctx_first[gimme] = cctx; |
1042 | } |
1078 | } |
1043 | |
1079 | |
1044 | /** coroutine switching *****************************************************/ |
1080 | /** coroutine switching *****************************************************/ |
1045 | |
1081 | |
1046 | static void |
1082 | static void |
… | |
… | |
1124 | prev->cctx = 0; |
1160 | prev->cctx = 0; |
1125 | |
1161 | |
1126 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1162 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1127 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1163 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1128 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1164 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
|
|
1165 | if (!next->cctx) |
1129 | next->cctx = cctx_get (aTHX); |
1166 | next->cctx = cctx_get (aTHX_ next->gimme); |
1130 | |
1167 | |
1131 | cctx_put (prev__cctx); |
1168 | cctx_put (prev__cctx, prev->gimme); |
1132 | } |
1169 | } |
1133 | |
1170 | |
1134 | ++next->usecount; |
1171 | ++next->usecount; |
1135 | |
1172 | |
1136 | if (expect_true (!next->cctx)) |
1173 | if (expect_true (!next->cctx)) |
1137 | next->cctx = cctx_get (aTHX); |
1174 | next->cctx = cctx_get (aTHX_ next->gimme); |
1138 | |
1175 | |
1139 | if (expect_false (prev__cctx != next->cctx)) |
1176 | if (expect_false (prev__cctx != next->cctx)) |
1140 | { |
1177 | { |
1141 | prev__cctx->top_env = PL_top_env; |
1178 | prev__cctx->top_env = PL_top_env; |
1142 | PL_top_env = next->cctx->top_env; |
1179 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1487 | BOOT_PAGESIZE; |
1524 | BOOT_PAGESIZE; |
1488 | |
1525 | |
1489 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1526 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1490 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1527 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1491 | |
1528 | |
|
|
1529 | hv_sig = coro_get_hv ("SIG", TRUE); |
1492 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1530 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1493 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1531 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1494 | |
1532 | |
1495 | if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook); |
1533 | if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
1496 | if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook); |
1534 | if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
1497 | |
1535 | |
1498 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1536 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1499 | |
1537 | |
1500 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1538 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1501 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1539 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1507 | |
1545 | |
1508 | while (main_top_env->je_prev) |
1546 | while (main_top_env->je_prev) |
1509 | main_top_env = main_top_env->je_prev; |
1547 | main_top_env = main_top_env->je_prev; |
1510 | |
1548 | |
1511 | coroapi.ver = CORO_API_VERSION; |
1549 | coroapi.ver = CORO_API_VERSION; |
|
|
1550 | coroapi.rev = CORO_API_REVISION; |
1512 | coroapi.transfer = api_transfer; |
1551 | coroapi.transfer = api_transfer; |
1513 | |
1552 | |
1514 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1553 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1515 | } |
1554 | } |
1516 | |
1555 | |
… | |
… | |
1583 | |
1622 | |
1584 | break; |
1623 | break; |
1585 | } |
1624 | } |
1586 | |
1625 | |
1587 | BARRIER; |
1626 | BARRIER; |
|
|
1627 | PUTBACK; |
1588 | TRANSFER (ta); |
1628 | TRANSFER (ta); |
1589 | |
1629 | SPAGAIN; /* might be the sp of a different coroutine now */ |
1590 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1630 | /* be extra careful not to ever do anything after TRANSFER */ |
1591 | XSRETURN_YES; |
|
|
1592 | } |
1631 | } |
1593 | |
1632 | |
1594 | bool |
1633 | bool |
1595 | _destroy (SV *coro_sv) |
1634 | _destroy (SV *coro_sv) |
1596 | CODE: |
1635 | CODE: |
… | |
… | |
1622 | RETVAL |
1661 | RETVAL |
1623 | |
1662 | |
1624 | int |
1663 | int |
1625 | cctx_idle () |
1664 | cctx_idle () |
1626 | CODE: |
1665 | CODE: |
1627 | RETVAL = cctx_idle; |
1666 | RETVAL = cctx_idle[0] + cctx_idle[1] + cctx_idle[2]; |
1628 | OUTPUT: |
1667 | OUTPUT: |
1629 | RETVAL |
1668 | RETVAL |
1630 | |
1669 | |
1631 | void |
1670 | void |
1632 | list () |
1671 | list () |
… | |
… | |
1656 | |
1695 | |
1657 | { |
1696 | { |
1658 | dSP; |
1697 | dSP; |
1659 | ENTER; |
1698 | ENTER; |
1660 | SAVETMPS; |
1699 | SAVETMPS; |
|
|
1700 | PUTBACK; |
|
|
1701 | PUSHSTACK; |
1661 | PUSHMARK (SP); |
1702 | PUSHMARK (SP); |
1662 | PUTBACK; |
|
|
1663 | |
1703 | |
1664 | if (ix) |
1704 | if (ix) |
1665 | eval_sv (coderef, 0); |
1705 | eval_sv (coderef, 0); |
1666 | else |
1706 | else |
1667 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1707 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1668 | |
1708 | |
1669 | SPAGAIN; |
1709 | POPSTACK; |
1670 | FREETMPS; |
1710 | FREETMPS; |
1671 | LEAVE; |
1711 | LEAVE; |
1672 | PUTBACK; |
1712 | PUTBACK; |
1673 | } |
1713 | } |
1674 | |
1714 | |
… | |
… | |
1828 | CODE: |
1868 | CODE: |
1829 | { |
1869 | { |
1830 | struct coro *coro = SvSTATE (coro_current); |
1870 | struct coro *coro = SvSTATE (coro_current); |
1831 | HV *hv = (HV *)SvRV (coro_current); |
1871 | HV *hv = (HV *)SvRV (coro_current); |
1832 | AV *defav = GvAV (PL_defgv); |
1872 | AV *defav = GvAV (PL_defgv); |
1833 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1873 | SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); |
1834 | AV *invoke_av; |
1874 | AV *invoke_av; |
1835 | int i, len; |
1875 | int i, len; |
1836 | |
1876 | |
1837 | if (!invoke) |
1877 | if (!invoke) |
1838 | croak ("\3async_pool terminate\2\n"); |
1878 | croak ("\3async_pool terminate\2\n"); |
1839 | |
1879 | |
1840 | SvREFCNT_dec (coro->saved_deffh); |
1880 | SvREFCNT_dec (coro->saved_deffh); |
1841 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1881 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1842 | |
1882 | |
1843 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1883 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1844 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1884 | newSVpvn (strpair ("[async_pool]")), 0); |
1845 | |
1885 | |
1846 | invoke_av = (AV *)SvRV (invoke); |
1886 | invoke_av = (AV *)SvRV (invoke); |
1847 | len = av_len (invoke_av); |
1887 | len = av_len (invoke_av); |
1848 | |
1888 | |
1849 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
1889 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
… | |
… | |
1872 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1912 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1873 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1913 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1874 | croak ("\3async_pool terminate\2\n"); |
1914 | croak ("\3async_pool terminate\2\n"); |
1875 | |
1915 | |
1876 | av_clear (GvAV (PL_defgv)); |
1916 | av_clear (GvAV (PL_defgv)); |
1877 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1917 | hv_store ((HV *)SvRV (coro_current), strpair ("desc"), |
1878 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1918 | newSVpvn (strpair ("[async_pool idle]")), 0); |
1879 | |
1919 | |
1880 | coro->prio = 0; |
1920 | coro->prio = 0; |
1881 | |
1921 | |
1882 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1922 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1883 | api_trace (coro_current, 0); |
1923 | api_trace (coro_current, 0); |