… | |
… | |
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 |
… | |
… | |
71 | # ifndef IS_PADCONST |
72 | # ifndef IS_PADCONST |
72 | # define IS_PADCONST(v) 0 |
73 | # define IS_PADCONST(v) 0 |
73 | # endif |
74 | # endif |
74 | #endif |
75 | #endif |
75 | |
76 | |
|
|
77 | /* 5.8.8 */ |
|
|
78 | #ifndef GV_NOTQUAL |
|
|
79 | # define GV_NOTQUAL 0 |
|
|
80 | #endif |
|
|
81 | #ifndef newSV |
|
|
82 | # define newSV(l) NEWSV(0,l) |
|
|
83 | #endif |
|
|
84 | |
|
|
85 | /* 5.11 */ |
|
|
86 | #ifndef CxHASARGS |
|
|
87 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
88 | #endif |
|
|
89 | |
76 | /* 5.8.7 */ |
90 | /* 5.8.7 */ |
77 | #ifndef SvRV_set |
91 | #ifndef SvRV_set |
78 | # define SvRV_set(s,v) SvRV(s) = (v) |
92 | # define SvRV_set(s,v) SvRV(s) = (v) |
79 | #endif |
|
|
80 | |
|
|
81 | /* 5.8.8 */ |
|
|
82 | #ifndef GV_NOTQUAL |
|
|
83 | # define GV_NOTQUAL 0 |
|
|
84 | #endif |
|
|
85 | #ifndef newSV |
|
|
86 | # define newSV(l) NEWSV(0,l) |
|
|
87 | #endif |
93 | #endif |
88 | |
94 | |
89 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
95 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
90 | # undef CORO_STACKGUARD |
96 | # undef CORO_STACKGUARD |
91 | #endif |
97 | #endif |
… | |
… | |
144 | static size_t coro_stacksize = CORO_STACKSIZE; |
150 | static size_t coro_stacksize = CORO_STACKSIZE; |
145 | static struct CoroAPI coroapi; |
151 | static struct CoroAPI coroapi; |
146 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
152 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
147 | static JMPENV *main_top_env; |
153 | static JMPENV *main_top_env; |
148 | static HV *coro_state_stash, *coro_stash; |
154 | static HV *coro_state_stash, *coro_stash; |
149 | static SV *coro_mortal; /* will be freed after next transfer */ |
155 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
150 | |
156 | |
151 | static GV *irsgv; /* $/ */ |
157 | static GV *irsgv; /* $/ */ |
152 | static GV *stdoutgv; /* *STDOUT */ |
158 | static GV *stdoutgv; /* *STDOUT */ |
153 | static SV *sv_diehook; |
159 | static SV *rv_diehook; |
154 | static SV *sv_warnhook; |
160 | static SV *rv_warnhook; |
|
|
161 | static HV *hv_sig; /* %SIG */ |
155 | |
162 | |
156 | /* async_pool helper stuff */ |
163 | /* async_pool helper stuff */ |
157 | static SV *sv_pool_rss; |
164 | static SV *sv_pool_rss; |
158 | static SV *sv_pool_size; |
165 | static SV *sv_pool_size; |
159 | static AV *av_async_pool; |
166 | static AV *av_async_pool; |
… | |
… | |
217 | |
224 | |
218 | /* process data */ |
225 | /* process data */ |
219 | AV *mainstack; |
226 | AV *mainstack; |
220 | perl_slots *slot; /* basically the saved sp */ |
227 | perl_slots *slot; /* basically the saved sp */ |
221 | |
228 | |
222 | /* data associated with this coroutine (initial args) */ |
229 | AV *args; /* data associated with this coroutine (initial args) */ |
223 | AV *args; |
230 | int refcnt; /* coroutines are refcounted, yes */ |
224 | int refcnt; |
|
|
225 | int flags; /* CF_ flags */ |
231 | int flags; /* CF_ flags */ |
|
|
232 | HV *hv; /* the perl hash associated with this coro, if any */ |
226 | |
233 | |
227 | /* statistics */ |
234 | /* statistics */ |
228 | int usecount; /* number of transfers to this coro */ |
235 | int usecount; /* number of transfers to this coro */ |
229 | |
236 | |
230 | /* coro process data */ |
237 | /* coro process data */ |
231 | int prio; |
238 | int prio; |
232 | SV *throw; |
239 | SV *throw; /* exception to be thrown */ |
233 | |
240 | |
234 | /* async_pool */ |
241 | /* async_pool */ |
235 | SV *saved_deffh; |
242 | SV *saved_deffh; |
236 | |
243 | |
237 | /* linked list */ |
244 | /* linked list */ |
238 | struct coro *next, *prev; |
245 | struct coro *next, *prev; |
239 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
240 | }; |
246 | }; |
241 | |
247 | |
242 | typedef struct coro *Coro__State; |
248 | typedef struct coro *Coro__State; |
243 | typedef struct coro *Coro__State_or_hashref; |
249 | typedef struct coro *Coro__State_or_hashref; |
244 | |
250 | |
… | |
… | |
258 | static struct coro *coro_first; |
264 | static struct coro *coro_first; |
259 | |
265 | |
260 | /** lowlevel stuff **********************************************************/ |
266 | /** lowlevel stuff **********************************************************/ |
261 | |
267 | |
262 | static SV * |
268 | static SV * |
263 | coro_get_sv (const char *name, int create) |
269 | coro_get_sv (pTHX_ const char *name, int create) |
264 | { |
270 | { |
265 | #if PERL_VERSION_ATLEAST (5,9,0) |
271 | #if PERL_VERSION_ATLEAST (5,9,0) |
266 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
272 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
267 | get_sv (name, create); |
273 | get_sv (name, create); |
268 | #endif |
274 | #endif |
269 | return get_sv (name, create); |
275 | return get_sv (name, create); |
270 | } |
276 | } |
271 | |
277 | |
272 | static SV * |
278 | static AV * |
273 | coro_get_av (const char *name, int create) |
279 | coro_get_av (pTHX_ const char *name, int create) |
274 | { |
280 | { |
275 | #if PERL_VERSION_ATLEAST (5,9,0) |
281 | #if PERL_VERSION_ATLEAST (5,9,0) |
276 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
282 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
277 | get_av (name, create); |
283 | get_av (name, create); |
278 | #endif |
284 | #endif |
279 | return get_av (name, create); |
285 | return get_av (name, create); |
|
|
286 | } |
|
|
287 | |
|
|
288 | static HV * |
|
|
289 | coro_get_hv (pTHX_ const char *name, int create) |
|
|
290 | { |
|
|
291 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
292 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
293 | get_hv (name, create); |
|
|
294 | #endif |
|
|
295 | return get_hv (name, create); |
280 | } |
296 | } |
281 | |
297 | |
282 | static AV * |
298 | static AV * |
283 | coro_clone_padlist (pTHX_ CV *cv) |
299 | coro_clone_padlist (pTHX_ CV *cv) |
284 | { |
300 | { |
… | |
… | |
333 | |
349 | |
334 | /* casting is fun. */ |
350 | /* casting is fun. */ |
335 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
351 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
336 | free_padlist (aTHX_ padlist); |
352 | free_padlist (aTHX_ padlist); |
337 | |
353 | |
338 | SvREFCNT_dec (av); |
|
|
339 | |
|
|
340 | return 0; |
354 | return 0; |
341 | } |
355 | } |
342 | |
356 | |
343 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
357 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
|
|
358 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
344 | |
359 | |
345 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
360 | static MGVTBL coro_cv_vtbl = { |
|
|
361 | 0, 0, 0, 0, |
|
|
362 | coro_cv_free |
|
|
363 | }; |
346 | |
364 | |
347 | #define CORO_MAGIC(cv) \ |
365 | #define CORO_MAGIC(sv,type) \ |
348 | SvMAGIC (cv) \ |
366 | SvMAGIC (sv) \ |
349 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
367 | ? SvMAGIC (sv)->mg_type == type \ |
350 | ? SvMAGIC (cv) \ |
368 | ? SvMAGIC (sv) \ |
351 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
369 | : mg_find (sv, type) \ |
352 | : 0 |
370 | : 0 |
|
|
371 | |
|
|
372 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
373 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
353 | |
374 | |
354 | static struct coro * |
375 | static struct coro * |
355 | SvSTATE_ (pTHX_ SV *coro) |
376 | SvSTATE_ (pTHX_ SV *coro) |
356 | { |
377 | { |
357 | HV *stash; |
378 | HV *stash; |
… | |
… | |
369 | /* very slow, but rare, check */ |
390 | /* very slow, but rare, check */ |
370 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
391 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
371 | croak ("Coro::State object required"); |
392 | croak ("Coro::State object required"); |
372 | } |
393 | } |
373 | |
394 | |
374 | mg = CORO_MAGIC (coro); |
395 | mg = CORO_MAGIC_state (coro); |
375 | return (struct coro *)mg->mg_ptr; |
396 | return (struct coro *)mg->mg_ptr; |
376 | } |
397 | } |
377 | |
398 | |
378 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
399 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
379 | |
400 | |
380 | /* the next two functions merely cache the padlists */ |
401 | /* the next two functions merely cache the padlists */ |
381 | static void |
402 | static void |
382 | get_padlist (pTHX_ CV *cv) |
403 | get_padlist (pTHX_ CV *cv) |
383 | { |
404 | { |
384 | MAGIC *mg = CORO_MAGIC (cv); |
405 | MAGIC *mg = CORO_MAGIC_cv (cv); |
385 | AV *av; |
406 | AV *av; |
386 | |
407 | |
387 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
408 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
388 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
409 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
389 | else |
410 | else |
… | |
… | |
401 | } |
422 | } |
402 | |
423 | |
403 | static void |
424 | static void |
404 | put_padlist (pTHX_ CV *cv) |
425 | put_padlist (pTHX_ CV *cv) |
405 | { |
426 | { |
406 | MAGIC *mg = CORO_MAGIC (cv); |
427 | MAGIC *mg = CORO_MAGIC_cv (cv); |
407 | AV *av; |
428 | AV *av; |
408 | |
429 | |
409 | if (expect_false (!mg)) |
430 | if (expect_false (!mg)) |
410 | { |
431 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
411 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
412 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
413 | mg->mg_virtual = &vtbl_coro; |
|
|
414 | mg->mg_obj = (SV *)newAV (); |
|
|
415 | } |
|
|
416 | |
432 | |
417 | av = (AV *)mg->mg_obj; |
433 | av = (AV *)mg->mg_obj; |
418 | |
434 | |
419 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
435 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
420 | av_extend (av, AvMAX (av) + 1); |
436 | av_extend (av, AvMAX (av) + 1); |
… | |
… | |
507 | |
523 | |
508 | PUTBACK; |
524 | PUTBACK; |
509 | } |
525 | } |
510 | |
526 | |
511 | /* allocate some space on the context stack for our purposes */ |
527 | /* allocate some space on the context stack for our purposes */ |
|
|
528 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
529 | if (SLOT_COUNT >= 1) CXINC; |
|
|
530 | if (SLOT_COUNT >= 2) CXINC; |
|
|
531 | if (SLOT_COUNT >= 3) CXINC; |
512 | { |
532 | { |
513 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
514 | int i; |
533 | 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) |
534 | for (i = 3; i < SLOT_COUNT; ++i) |
519 | CXINC; |
535 | CXINC; |
520 | |
|
|
521 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
522 | } |
536 | } |
|
|
537 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
523 | |
538 | |
524 | c->mainstack = PL_mainstack; |
539 | c->mainstack = PL_mainstack; |
525 | |
540 | |
526 | { |
541 | { |
527 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
542 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
654 | return rss; |
669 | return rss; |
655 | } |
670 | } |
656 | |
671 | |
657 | /** coroutine stack handling ************************************************/ |
672 | /** coroutine stack handling ************************************************/ |
658 | |
673 | |
|
|
674 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
675 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
676 | |
|
|
677 | /* |
|
|
678 | * This overrides the default magic get method of %SIG elements. |
|
|
679 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
|
|
680 | * and instead of tryign to save and restore the hash elements, we just provide |
|
|
681 | * readback here. |
|
|
682 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
683 | * not expecting this to actually change the hook. This is a potential problem |
|
|
684 | * when a schedule happens then, but we ignore this. |
|
|
685 | */ |
|
|
686 | static int |
|
|
687 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
|
|
688 | { |
|
|
689 | const char *s = MgPV_nolen_const (mg); |
|
|
690 | |
|
|
691 | if (*s == '_') |
|
|
692 | { |
|
|
693 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
|
|
694 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
|
|
695 | } |
|
|
696 | |
|
|
697 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
|
|
698 | } |
|
|
699 | |
|
|
700 | static int |
|
|
701 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
|
|
702 | { |
|
|
703 | const char *s = MgPV_nolen_const (mg); |
|
|
704 | |
|
|
705 | if (*s == '_') |
|
|
706 | { |
|
|
707 | SV **svp = 0; |
|
|
708 | |
|
|
709 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
710 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
711 | |
|
|
712 | if (svp) |
|
|
713 | { |
|
|
714 | SV *old = *svp; |
|
|
715 | *svp = newSVsv (sv); |
|
|
716 | SvREFCNT_dec (old); |
|
|
717 | return; |
|
|
718 | } |
|
|
719 | } |
|
|
720 | |
|
|
721 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
|
|
722 | } |
|
|
723 | |
659 | static void |
724 | static void |
660 | coro_setup (pTHX_ struct coro *coro) |
725 | coro_setup (pTHX_ struct coro *coro) |
661 | { |
726 | { |
662 | /* |
727 | /* |
663 | * emulate part of the perl startup here. |
728 | * emulate part of the perl startup here. |
… | |
… | |
671 | PL_curpm = 0; |
736 | PL_curpm = 0; |
672 | PL_curpad = 0; |
737 | PL_curpad = 0; |
673 | PL_localizing = 0; |
738 | PL_localizing = 0; |
674 | PL_dirty = 0; |
739 | PL_dirty = 0; |
675 | PL_restartop = 0; |
740 | PL_restartop = 0; |
676 | PL_diehook = SvREFCNT_inc (sv_diehook); |
741 | |
677 | PL_warnhook = SvREFCNT_inc (sv_warnhook); |
742 | /* recreate the die/warn hooks */ |
|
|
743 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
|
|
744 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
678 | |
745 | |
679 | GvSV (PL_defgv) = newSV (0); |
746 | GvSV (PL_defgv) = newSV (0); |
680 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
747 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
681 | GvSV (PL_errgv) = newSV (0); |
748 | GvSV (PL_errgv) = newSV (0); |
682 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
749 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
697 | PL_op = (OP *)&myop; |
764 | PL_op = (OP *)&myop; |
698 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
765 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
699 | SPAGAIN; |
766 | SPAGAIN; |
700 | } |
767 | } |
701 | |
768 | |
702 | ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ |
769 | /* this newly created coroutine might be run on an existing cctx which most |
|
|
770 | * likely was suspended in set_stacklevel, called from entersub. |
|
|
771 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
|
|
772 | * so we ENTER here for symmetry |
|
|
773 | */ |
|
|
774 | ENTER; |
703 | } |
775 | } |
704 | |
776 | |
705 | static void |
777 | static void |
706 | coro_destroy (pTHX_ struct coro *coro) |
778 | coro_destroy (pTHX_ struct coro *coro) |
707 | { |
779 | { |
… | |
… | |
828 | SAVETMPS; |
900 | SAVETMPS; |
829 | EXTEND (SP, 3); |
901 | EXTEND (SP, 3); |
830 | PUSHMARK (SP); |
902 | PUSHMARK (SP); |
831 | PUSHs (&PL_sv_yes); |
903 | PUSHs (&PL_sv_yes); |
832 | PUSHs (fullname); |
904 | PUSHs (fullname); |
833 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
905 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
834 | PUTBACK; |
906 | PUTBACK; |
835 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
907 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
836 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
908 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
837 | SPAGAIN; |
909 | SPAGAIN; |
838 | FREETMPS; |
910 | FREETMPS; |
… | |
… | |
874 | |
946 | |
875 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
947 | /* 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 */ |
948 | /* _cctx_init should be careful, as it could be called at almost any time */ |
877 | /* during execution of a perl program */ |
949 | /* during execution of a perl program */ |
878 | static void NOINLINE |
950 | static void NOINLINE |
879 | prepare_cctx (pTHX_ coro_cctx *cctx) |
951 | cctx_prepare (pTHX_ coro_cctx *cctx) |
880 | { |
952 | { |
881 | dSP; |
953 | dSP; |
882 | LOGOP myop; |
954 | LOGOP myop; |
883 | |
955 | |
884 | PL_top_env = &PL_start_env; |
956 | PL_top_env = &PL_start_env; |
… | |
… | |
902 | |
974 | |
903 | /* |
975 | /* |
904 | * this is a _very_ stripped down perl interpreter ;) |
976 | * this is a _very_ stripped down perl interpreter ;) |
905 | */ |
977 | */ |
906 | static void |
978 | static void |
907 | coro_run (void *arg) |
979 | cctx_run (void *arg) |
908 | { |
980 | { |
909 | dTHX; |
981 | dTHX; |
910 | |
982 | |
911 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
983 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
912 | UNLOCK; |
984 | UNLOCK; |
913 | |
985 | |
914 | /* we now skip the entersub that lead to transfer() */ |
986 | /* we now skip the entersub that lead to transfer() */ |
915 | PL_op = PL_op->op_next; |
987 | PL_op = PL_op->op_next; |
916 | |
988 | |
917 | /* inject a fake subroutine call to cctx_init */ |
989 | /* inject a fake subroutine call to cctx_init */ |
918 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
990 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
919 | |
991 | |
920 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
992 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
921 | PL_restartop = PL_op; |
993 | PL_restartop = PL_op; |
922 | perl_run (PL_curinterp); |
994 | perl_run (PL_curinterp); |
923 | |
995 | |
… | |
… | |
942 | ++cctx_count; |
1014 | ++cctx_count; |
943 | |
1015 | |
944 | Newz (0, cctx, 1, coro_cctx); |
1016 | Newz (0, cctx, 1, coro_cctx); |
945 | |
1017 | |
946 | #if HAVE_MMAP |
1018 | #if HAVE_MMAP |
947 | |
|
|
948 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1019 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
949 | /* mmap supposedly does allocate-on-write for us */ |
1020 | /* 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); |
1021 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
951 | |
1022 | |
952 | if (cctx->sptr != (void *)-1) |
1023 | if (cctx->sptr != (void *)-1) |
… | |
… | |
973 | stack_start = cctx->sptr; |
1044 | stack_start = cctx->sptr; |
974 | stack_size = cctx->ssize; |
1045 | stack_size = cctx->ssize; |
975 | } |
1046 | } |
976 | |
1047 | |
977 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1048 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
978 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
1049 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
979 | |
1050 | |
980 | return cctx; |
1051 | return cctx; |
981 | } |
1052 | } |
982 | |
1053 | |
983 | static void |
1054 | static void |
… | |
… | |
1057 | if (expect_false (next->flags & CF_DESTROYED)) |
1128 | if (expect_false (next->flags & CF_DESTROYED)) |
1058 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1129 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1059 | |
1130 | |
1060 | if ( |
1131 | if ( |
1061 | #if PERL_VERSION_ATLEAST (5,9,0) |
1132 | #if PERL_VERSION_ATLEAST (5,9,0) |
1062 | expect_false (PL_parser) |
1133 | expect_false (PL_parser && PL_parser->lex_state != LEX_NOTPARSING) |
1063 | #else |
1134 | #else |
1064 | expect_false (PL_lex_state != LEX_NOTPARSING) |
1135 | expect_false (PL_lex_state != LEX_NOTPARSING) |
1065 | #endif |
1136 | #endif |
1066 | ) |
1137 | ) |
1067 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
1138 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
1068 | } |
1139 | } |
1069 | } |
1140 | } |
1070 | |
1141 | |
1071 | /* always use the TRANSFER macro */ |
1142 | /* always use the TRANSFER macro */ |
1072 | static void NOINLINE |
1143 | static void NOINLINE |
1073 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1144 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1074 | { |
1145 | { |
1075 | dSTACKLEVEL; |
1146 | dSTACKLEVEL; |
|
|
1147 | static volatile int has_throw; |
1076 | |
1148 | |
1077 | /* sometimes transfer is only called to set idle_sp */ |
1149 | /* sometimes transfer is only called to set idle_sp */ |
1078 | if (expect_false (!next)) |
1150 | if (expect_false (!next)) |
1079 | { |
1151 | { |
1080 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1152 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1095 | prev->flags &= ~CF_RUNNING; |
1167 | prev->flags &= ~CF_RUNNING; |
1096 | next->flags |= CF_RUNNING; |
1168 | next->flags |= CF_RUNNING; |
1097 | |
1169 | |
1098 | LOCK; |
1170 | LOCK; |
1099 | |
1171 | |
|
|
1172 | /* first get rid of the old state */ |
|
|
1173 | save_perl (aTHX_ prev); |
|
|
1174 | |
1100 | if (expect_false (next->flags & CF_NEW)) |
1175 | if (expect_false (next->flags & CF_NEW)) |
1101 | { |
1176 | { |
1102 | /* need to start coroutine */ |
1177 | /* need to start coroutine */ |
1103 | next->flags &= ~CF_NEW; |
1178 | next->flags &= ~CF_NEW; |
1104 | /* first get rid of the old state */ |
|
|
1105 | save_perl (aTHX_ prev); |
|
|
1106 | /* setup coroutine call */ |
1179 | /* setup coroutine call */ |
1107 | coro_setup (aTHX_ next); |
1180 | coro_setup (aTHX_ next); |
1108 | } |
1181 | } |
1109 | else |
1182 | else |
1110 | { |
|
|
1111 | /* coroutine already started */ |
|
|
1112 | save_perl (aTHX_ prev); |
|
|
1113 | load_perl (aTHX_ next); |
1183 | load_perl (aTHX_ next); |
1114 | } |
|
|
1115 | |
1184 | |
1116 | prev__cctx = prev->cctx; |
1185 | prev__cctx = prev->cctx; |
1117 | |
1186 | |
1118 | /* possibly "free" the cctx */ |
1187 | /* possibly "free" the cctx */ |
1119 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
1188 | if (expect_true ( |
|
|
1189 | prev__cctx->idle_sp == STACKLEVEL |
|
|
1190 | && !(prev__cctx->flags & CC_TRACE) |
|
|
1191 | && !force_cctx |
|
|
1192 | )) |
1120 | { |
1193 | { |
1121 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1194 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1122 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1195 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1123 | |
1196 | |
1124 | prev->cctx = 0; |
1197 | prev->cctx = 0; |
1125 | |
1198 | |
1126 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1199 | /* 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 */ |
1200 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1128 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1201 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
|
|
1202 | if (!next->cctx) |
1129 | next->cctx = cctx_get (aTHX); |
1203 | next->cctx = cctx_get (aTHX); |
1130 | |
1204 | |
1131 | cctx_put (prev__cctx); |
1205 | cctx_put (prev__cctx); |
1132 | } |
1206 | } |
1133 | |
1207 | |
1134 | ++next->usecount; |
1208 | ++next->usecount; |
1135 | |
1209 | |
1136 | if (expect_true (!next->cctx)) |
1210 | if (expect_true (!next->cctx)) |
1137 | next->cctx = cctx_get (aTHX); |
1211 | next->cctx = cctx_get (aTHX); |
|
|
1212 | |
|
|
1213 | has_throw = !!next->throw; |
1138 | |
1214 | |
1139 | if (expect_false (prev__cctx != next->cctx)) |
1215 | if (expect_false (prev__cctx != next->cctx)) |
1140 | { |
1216 | { |
1141 | prev__cctx->top_env = PL_top_env; |
1217 | prev__cctx->top_env = PL_top_env; |
1142 | PL_top_env = next->cctx->top_env; |
1218 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1144 | } |
1220 | } |
1145 | |
1221 | |
1146 | free_coro_mortal (aTHX); |
1222 | free_coro_mortal (aTHX); |
1147 | UNLOCK; |
1223 | UNLOCK; |
1148 | |
1224 | |
1149 | if (expect_false (prev->throw || next->throw)) |
1225 | if (expect_false (has_throw)) |
1150 | { |
1226 | { |
1151 | struct coro *coro = SvSTATE (coro_current); |
1227 | struct coro *coro = SvSTATE (coro_current); |
1152 | |
1228 | |
1153 | if (coro->throw) |
1229 | if (coro->throw) |
1154 | { |
1230 | { |
… | |
… | |
1164 | struct transfer_args |
1240 | struct transfer_args |
1165 | { |
1241 | { |
1166 | struct coro *prev, *next; |
1242 | struct coro *prev, *next; |
1167 | }; |
1243 | }; |
1168 | |
1244 | |
1169 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1245 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1170 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1246 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1171 | |
1247 | |
1172 | /** high level stuff ********************************************************/ |
1248 | /** high level stuff ********************************************************/ |
1173 | |
1249 | |
1174 | static int |
1250 | static int |
… | |
… | |
1268 | { |
1344 | { |
1269 | dTHX; |
1345 | dTHX; |
1270 | struct transfer_args ta; |
1346 | struct transfer_args ta; |
1271 | |
1347 | |
1272 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1348 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1273 | TRANSFER (ta); |
1349 | TRANSFER (ta, 1); |
1274 | } |
1350 | } |
1275 | |
1351 | |
1276 | /** Coro ********************************************************************/ |
1352 | /** Coro ********************************************************************/ |
1277 | |
1353 | |
1278 | static void |
1354 | static void |
… | |
… | |
1280 | { |
1356 | { |
1281 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1357 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1282 | } |
1358 | } |
1283 | |
1359 | |
1284 | static SV * |
1360 | static SV * |
1285 | coro_deq (pTHX_ int min_prio) |
1361 | coro_deq (pTHX) |
1286 | { |
1362 | { |
1287 | int prio = PRIO_MAX - PRIO_MIN; |
1363 | int prio; |
1288 | |
1364 | |
1289 | min_prio -= PRIO_MIN; |
|
|
1290 | if (min_prio < 0) |
|
|
1291 | min_prio = 0; |
|
|
1292 | |
|
|
1293 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
1365 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1294 | if (AvFILLp (coro_ready [prio]) >= 0) |
1366 | if (AvFILLp (coro_ready [prio]) >= 0) |
1295 | return av_shift (coro_ready [prio]); |
1367 | return av_shift (coro_ready [prio]); |
1296 | |
1368 | |
1297 | return 0; |
1369 | return 0; |
1298 | } |
1370 | } |
… | |
… | |
1334 | SV *prev_sv, *next_sv; |
1406 | SV *prev_sv, *next_sv; |
1335 | |
1407 | |
1336 | for (;;) |
1408 | for (;;) |
1337 | { |
1409 | { |
1338 | LOCK; |
1410 | LOCK; |
1339 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1411 | next_sv = coro_deq (aTHX); |
1340 | |
1412 | |
1341 | /* nothing to schedule: call the idle handler */ |
1413 | /* nothing to schedule: call the idle handler */ |
1342 | if (expect_false (!next_sv)) |
1414 | if (expect_false (!next_sv)) |
1343 | { |
1415 | { |
1344 | dSP; |
1416 | dSP; |
… | |
… | |
1348 | SAVETMPS; |
1420 | SAVETMPS; |
1349 | |
1421 | |
1350 | PUSHMARK (SP); |
1422 | PUSHMARK (SP); |
1351 | PUTBACK; |
1423 | PUTBACK; |
1352 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1424 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1425 | SPAGAIN; |
1353 | |
1426 | |
1354 | FREETMPS; |
1427 | FREETMPS; |
1355 | LEAVE; |
1428 | LEAVE; |
1356 | continue; |
1429 | continue; |
1357 | } |
1430 | } |
… | |
… | |
1412 | { |
1485 | { |
1413 | dTHX; |
1486 | dTHX; |
1414 | struct transfer_args ta; |
1487 | struct transfer_args ta; |
1415 | |
1488 | |
1416 | prepare_schedule (aTHX_ &ta); |
1489 | prepare_schedule (aTHX_ &ta); |
1417 | TRANSFER (ta); |
1490 | TRANSFER (ta, 1); |
1418 | } |
1491 | } |
1419 | |
1492 | |
1420 | static int |
1493 | static int |
1421 | api_cede (void) |
1494 | api_cede (void) |
1422 | { |
1495 | { |
… | |
… | |
1425 | |
1498 | |
1426 | prepare_cede (aTHX_ &ta); |
1499 | prepare_cede (aTHX_ &ta); |
1427 | |
1500 | |
1428 | if (expect_true (ta.prev != ta.next)) |
1501 | if (expect_true (ta.prev != ta.next)) |
1429 | { |
1502 | { |
1430 | TRANSFER (ta); |
1503 | TRANSFER (ta, 1); |
1431 | return 1; |
1504 | return 1; |
1432 | } |
1505 | } |
1433 | else |
1506 | else |
1434 | return 0; |
1507 | return 0; |
1435 | } |
1508 | } |
… | |
… | |
1440 | dTHX; |
1513 | dTHX; |
1441 | struct transfer_args ta; |
1514 | struct transfer_args ta; |
1442 | |
1515 | |
1443 | if (prepare_cede_notself (aTHX_ &ta)) |
1516 | if (prepare_cede_notself (aTHX_ &ta)) |
1444 | { |
1517 | { |
1445 | TRANSFER (ta); |
1518 | TRANSFER (ta, 1); |
1446 | return 1; |
1519 | return 1; |
1447 | } |
1520 | } |
1448 | else |
1521 | else |
1449 | return 0; |
1522 | return 0; |
1450 | } |
1523 | } |
… | |
… | |
1487 | BOOT_PAGESIZE; |
1560 | BOOT_PAGESIZE; |
1488 | |
1561 | |
1489 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1562 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1490 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1563 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1491 | |
1564 | |
1492 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1565 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
1493 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1566 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1567 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; |
|
|
1568 | PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
1494 | |
1569 | |
1495 | if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook); |
1570 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1496 | if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook); |
1571 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
|
|
1572 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1497 | |
1573 | |
1498 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1574 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1499 | |
1575 | |
1500 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1576 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1501 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1577 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1507 | |
1583 | |
1508 | while (main_top_env->je_prev) |
1584 | while (main_top_env->je_prev) |
1509 | main_top_env = main_top_env->je_prev; |
1585 | main_top_env = main_top_env->je_prev; |
1510 | |
1586 | |
1511 | coroapi.ver = CORO_API_VERSION; |
1587 | coroapi.ver = CORO_API_VERSION; |
|
|
1588 | coroapi.rev = CORO_API_REVISION; |
1512 | coroapi.transfer = api_transfer; |
1589 | coroapi.transfer = api_transfer; |
1513 | |
1590 | |
1514 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1591 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1515 | } |
1592 | } |
1516 | |
1593 | |
1517 | SV * |
1594 | SV * |
1518 | new (char *klass, ...) |
1595 | new (char *klass, ...) |
1519 | CODE: |
1596 | CODE: |
1520 | { |
1597 | { |
1521 | struct coro *coro; |
1598 | struct coro *coro; |
|
|
1599 | MAGIC *mg; |
1522 | HV *hv; |
1600 | HV *hv; |
1523 | int i; |
1601 | int i; |
1524 | |
1602 | |
1525 | Newz (0, coro, 1, struct coro); |
1603 | Newz (0, coro, 1, struct coro); |
1526 | coro->args = newAV (); |
1604 | coro->args = newAV (); |
… | |
… | |
1529 | if (coro_first) coro_first->prev = coro; |
1607 | if (coro_first) coro_first->prev = coro; |
1530 | coro->next = coro_first; |
1608 | coro->next = coro_first; |
1531 | coro_first = coro; |
1609 | coro_first = coro; |
1532 | |
1610 | |
1533 | coro->hv = hv = newHV (); |
1611 | coro->hv = hv = newHV (); |
1534 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1612 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
1613 | mg->mg_flags |= MGf_DUP; |
1535 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1614 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1536 | |
1615 | |
1537 | av_extend (coro->args, items - 1); |
1616 | av_extend (coro->args, items - 1); |
1538 | for (i = 1; i < items; i++) |
1617 | for (i = 1; i < items; i++) |
1539 | av_push (coro->args, newSVsv (ST (i))); |
1618 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1553 | Coro::cede_notself = 4 |
1632 | Coro::cede_notself = 4 |
1554 | CODE: |
1633 | CODE: |
1555 | { |
1634 | { |
1556 | struct transfer_args ta; |
1635 | struct transfer_args ta; |
1557 | |
1636 | |
|
|
1637 | PUTBACK; |
1558 | switch (ix) |
1638 | switch (ix) |
1559 | { |
1639 | { |
1560 | case 0: |
1640 | case 0: |
1561 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1641 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1562 | ta.next = 0; |
1642 | ta.next = 0; |
… | |
… | |
1581 | if (!prepare_cede_notself (aTHX_ &ta)) |
1661 | if (!prepare_cede_notself (aTHX_ &ta)) |
1582 | XSRETURN_EMPTY; |
1662 | XSRETURN_EMPTY; |
1583 | |
1663 | |
1584 | break; |
1664 | break; |
1585 | } |
1665 | } |
|
|
1666 | SPAGAIN; |
1586 | |
1667 | |
1587 | BARRIER; |
1668 | BARRIER; |
|
|
1669 | PUTBACK; |
1588 | TRANSFER (ta); |
1670 | TRANSFER (ta, 0); |
1589 | |
1671 | SPAGAIN; /* might be the sp of a different coroutine now */ |
1590 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1672 | /* be extra careful not to ever do anything after TRANSFER */ |
1591 | XSRETURN_YES; |
|
|
1592 | } |
1673 | } |
1593 | |
1674 | |
1594 | bool |
1675 | bool |
1595 | _destroy (SV *coro_sv) |
1676 | _destroy (SV *coro_sv) |
1596 | CODE: |
1677 | CODE: |
… | |
… | |
1648 | { |
1729 | { |
1649 | struct coro temp; |
1730 | struct coro temp; |
1650 | |
1731 | |
1651 | if (!(coro->flags & CF_RUNNING)) |
1732 | if (!(coro->flags & CF_RUNNING)) |
1652 | { |
1733 | { |
|
|
1734 | PUTBACK; |
1653 | save_perl (aTHX_ &temp); |
1735 | save_perl (aTHX_ &temp); |
1654 | load_perl (aTHX_ coro); |
1736 | load_perl (aTHX_ coro); |
1655 | } |
1737 | } |
1656 | |
1738 | |
1657 | { |
1739 | { |
1658 | dSP; |
1740 | dSP; |
1659 | ENTER; |
1741 | ENTER; |
1660 | SAVETMPS; |
1742 | SAVETMPS; |
|
|
1743 | PUTBACK; |
|
|
1744 | PUSHSTACK; |
1661 | PUSHMARK (SP); |
1745 | PUSHMARK (SP); |
1662 | PUTBACK; |
|
|
1663 | |
1746 | |
1664 | if (ix) |
1747 | if (ix) |
1665 | eval_sv (coderef, 0); |
1748 | eval_sv (coderef, 0); |
1666 | else |
1749 | else |
1667 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1750 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1668 | |
1751 | |
|
|
1752 | POPSTACK; |
1669 | SPAGAIN; |
1753 | SPAGAIN; |
1670 | FREETMPS; |
1754 | FREETMPS; |
1671 | LEAVE; |
1755 | LEAVE; |
1672 | PUTBACK; |
1756 | PUTBACK; |
1673 | } |
1757 | } |
1674 | |
1758 | |
1675 | if (!(coro->flags & CF_RUNNING)) |
1759 | if (!(coro->flags & CF_RUNNING)) |
1676 | { |
1760 | { |
1677 | save_perl (aTHX_ coro); |
1761 | save_perl (aTHX_ coro); |
1678 | load_perl (aTHX_ &temp); |
1762 | load_perl (aTHX_ &temp); |
|
|
1763 | SPAGAIN; |
1679 | } |
1764 | } |
1680 | } |
1765 | } |
1681 | } |
1766 | } |
1682 | |
1767 | |
1683 | SV * |
1768 | SV * |
… | |
… | |
1724 | case 1: RETVAL = coro->usecount; break; |
1809 | case 1: RETVAL = coro->usecount; break; |
1725 | } |
1810 | } |
1726 | OUTPUT: |
1811 | OUTPUT: |
1727 | RETVAL |
1812 | RETVAL |
1728 | |
1813 | |
|
|
1814 | void |
|
|
1815 | force_cctx () |
|
|
1816 | CODE: |
|
|
1817 | struct coro *coro = SvSTATE (coro_current); |
|
|
1818 | coro->cctx->idle_sp = 0; |
1729 | |
1819 | |
1730 | MODULE = Coro::State PACKAGE = Coro |
1820 | MODULE = Coro::State PACKAGE = Coro |
1731 | |
1821 | |
1732 | BOOT: |
1822 | BOOT: |
1733 | { |
1823 | { |
1734 | int i; |
1824 | int i; |
1735 | |
1825 | |
1736 | sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); |
1826 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
1737 | sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); |
1827 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
1738 | av_async_pool = coro_get_av ("Coro::async_pool", TRUE); |
1828 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
1739 | |
1829 | |
1740 | coro_current = coro_get_sv ("Coro::current", FALSE); |
1830 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
1741 | SvREADONLY_on (coro_current); |
1831 | SvREADONLY_on (coro_current); |
1742 | |
1832 | |
1743 | coro_stash = gv_stashpv ("Coro", TRUE); |
1833 | coro_stash = gv_stashpv ("Coro", TRUE); |
1744 | |
1834 | |
1745 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1835 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
1820 | PROTOTYPE: $;$ |
1910 | PROTOTYPE: $;$ |
1821 | CODE: |
1911 | CODE: |
1822 | SvREFCNT_dec (self->throw); |
1912 | SvREFCNT_dec (self->throw); |
1823 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
1913 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
1824 | |
1914 | |
|
|
1915 | void |
|
|
1916 | swap_defsv (Coro::State self) |
|
|
1917 | PROTOTYPE: $ |
|
|
1918 | ALIAS: |
|
|
1919 | swap_defav = 1 |
|
|
1920 | CODE: |
|
|
1921 | if (!self->slot) |
|
|
1922 | croak ("cannot swap state with coroutine that has no saved state"); |
|
|
1923 | else |
|
|
1924 | { |
|
|
1925 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
|
|
1926 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
|
|
1927 | |
|
|
1928 | SV *tmp = *src; *src = *dst; *dst = tmp; |
|
|
1929 | } |
|
|
1930 | |
1825 | # for async_pool speedup |
1931 | # for async_pool speedup |
1826 | void |
1932 | void |
1827 | _pool_1 (SV *cb) |
1933 | _pool_1 (SV *cb) |
1828 | CODE: |
1934 | CODE: |
1829 | { |
1935 | { |
… | |
… | |
1833 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1939 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1834 | AV *invoke_av; |
1940 | AV *invoke_av; |
1835 | int i, len; |
1941 | int i, len; |
1836 | |
1942 | |
1837 | if (!invoke) |
1943 | if (!invoke) |
|
|
1944 | { |
|
|
1945 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
1838 | croak ("\3async_pool terminate\2\n"); |
1946 | croak ("\3async_pool terminate\2\n"); |
|
|
1947 | } |
1839 | |
1948 | |
1840 | SvREFCNT_dec (coro->saved_deffh); |
1949 | SvREFCNT_dec (coro->saved_deffh); |
1841 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1950 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1842 | |
1951 | |
1843 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1952 | hv_store (hv, "desc", sizeof ("desc") - 1, |
… | |
… | |
1869 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1978 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1870 | coro->saved_deffh = 0; |
1979 | coro->saved_deffh = 0; |
1871 | |
1980 | |
1872 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1981 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1873 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1982 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1983 | { |
|
|
1984 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
1874 | croak ("\3async_pool terminate\2\n"); |
1985 | croak ("\3async_pool terminate\2\n"); |
|
|
1986 | } |
1875 | |
1987 | |
1876 | av_clear (GvAV (PL_defgv)); |
1988 | av_clear (GvAV (PL_defgv)); |
1877 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1989 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1878 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1990 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1879 | |
1991 | |