… | |
… | |
63 | #endif |
63 | #endif |
64 | |
64 | |
65 | /* the maximum number of idle cctx that will be pooled */ |
65 | /* the maximum number of idle cctx that will be pooled */ |
66 | static int cctx_max_idle = 4; |
66 | static int cctx_max_idle = 4; |
67 | |
67 | |
|
|
68 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
|
|
69 | # define HAS_SCOPESTACK_NAME 1 |
|
|
70 | #endif |
|
|
71 | |
68 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
72 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
69 | # undef CORO_STACKGUARD |
73 | # undef CORO_STACKGUARD |
70 | #endif |
74 | #endif |
71 | |
75 | |
72 | #ifndef CORO_STACKGUARD |
76 | #ifndef CORO_STACKGUARD |
… | |
… | |
177 | void *sptr; |
181 | void *sptr; |
178 | size_t ssize; |
182 | size_t ssize; |
179 | |
183 | |
180 | /* cpu state */ |
184 | /* cpu state */ |
181 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
185 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
186 | #ifndef NDEBUG |
182 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
187 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
|
|
188 | #endif |
183 | JMPENV *top_env; |
189 | JMPENV *top_env; |
184 | coro_context cctx; |
190 | coro_context cctx; |
185 | |
191 | |
186 | U32 gen; |
192 | U32 gen; |
187 | #if CORO_USE_VALGRIND |
193 | #if CORO_USE_VALGRIND |
… | |
… | |
335 | |
341 | |
336 | /** time stuff **************************************************************/ |
342 | /** time stuff **************************************************************/ |
337 | |
343 | |
338 | #ifdef HAS_GETTIMEOFDAY |
344 | #ifdef HAS_GETTIMEOFDAY |
339 | |
345 | |
340 | ECB_INLINE void |
346 | ecb_inline void |
341 | coro_u2time (pTHX_ UV ret[2]) |
347 | coro_u2time (pTHX_ UV ret[2]) |
342 | { |
348 | { |
343 | struct timeval tv; |
349 | struct timeval tv; |
344 | gettimeofday (&tv, 0); |
350 | gettimeofday (&tv, 0); |
345 | |
351 | |
346 | ret [0] = tv.tv_sec; |
352 | ret [0] = tv.tv_sec; |
347 | ret [1] = tv.tv_usec; |
353 | ret [1] = tv.tv_usec; |
348 | } |
354 | } |
349 | |
355 | |
350 | ECB_INLINE double |
356 | ecb_inline double |
351 | coro_nvtime (void) |
357 | coro_nvtime (void) |
352 | { |
358 | { |
353 | struct timeval tv; |
359 | struct timeval tv; |
354 | gettimeofday (&tv, 0); |
360 | gettimeofday (&tv, 0); |
355 | |
361 | |
356 | return tv.tv_sec + tv.tv_usec * 1e-6; |
362 | return tv.tv_sec + tv.tv_usec * 1e-6; |
357 | } |
363 | } |
358 | |
364 | |
359 | ECB_INLINE void |
365 | ecb_inline void |
360 | time_init (pTHX) |
366 | time_init (pTHX) |
361 | { |
367 | { |
362 | nvtime = coro_nvtime; |
368 | nvtime = coro_nvtime; |
363 | u2time = coro_u2time; |
369 | u2time = coro_u2time; |
364 | } |
370 | } |
365 | |
371 | |
366 | #else |
372 | #else |
367 | |
373 | |
368 | ECB_INLINE void |
374 | ecb_inline void |
369 | time_init (pTHX) |
375 | time_init (pTHX) |
370 | { |
376 | { |
371 | SV **svp; |
377 | SV **svp; |
372 | |
378 | |
373 | require_pv ("Time/HiRes.pm"); |
379 | require_pv ("Time/HiRes.pm"); |
… | |
… | |
415 | get_hv (name, create); |
421 | get_hv (name, create); |
416 | #endif |
422 | #endif |
417 | return get_hv (name, create); |
423 | return get_hv (name, create); |
418 | } |
424 | } |
419 | |
425 | |
420 | ECB_INLINE void |
426 | ecb_inline void |
421 | coro_times_update (void) |
427 | coro_times_update (void) |
422 | { |
428 | { |
423 | #ifdef coro_clock_gettime |
429 | #ifdef coro_clock_gettime |
424 | struct timespec ts; |
430 | struct timespec ts; |
425 | |
431 | |
… | |
… | |
438 | time_real [0] = tv [0]; |
444 | time_real [0] = tv [0]; |
439 | time_real [1] = tv [1] * 1000; |
445 | time_real [1] = tv [1] * 1000; |
440 | #endif |
446 | #endif |
441 | } |
447 | } |
442 | |
448 | |
443 | ECB_INLINE void |
449 | ecb_inline void |
444 | coro_times_add (struct coro *c) |
450 | coro_times_add (struct coro *c) |
445 | { |
451 | { |
446 | c->t_real [1] += time_real [1]; |
452 | c->t_real [1] += time_real [1]; |
447 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
453 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
448 | c->t_real [0] += time_real [0]; |
454 | c->t_real [0] += time_real [0]; |
… | |
… | |
450 | c->t_cpu [1] += time_cpu [1]; |
456 | c->t_cpu [1] += time_cpu [1]; |
451 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
457 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
452 | c->t_cpu [0] += time_cpu [0]; |
458 | c->t_cpu [0] += time_cpu [0]; |
453 | } |
459 | } |
454 | |
460 | |
455 | ECB_INLINE void |
461 | ecb_inline void |
456 | coro_times_sub (struct coro *c) |
462 | coro_times_sub (struct coro *c) |
457 | { |
463 | { |
458 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
464 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
459 | c->t_real [1] -= time_real [1]; |
465 | c->t_real [1] -= time_real [1]; |
460 | c->t_real [0] -= time_real [0]; |
466 | c->t_real [0] -= time_real [0]; |
… | |
… | |
481 | : 0) |
487 | : 0) |
482 | |
488 | |
483 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
489 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
484 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
490 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
485 | |
491 | |
486 | ECB_INLINE MAGIC * |
492 | ecb_inline MAGIC * |
487 | SvSTATEhv_p (pTHX_ SV *coro) |
493 | SvSTATEhv_p (pTHX_ SV *coro) |
488 | { |
494 | { |
489 | MAGIC *mg; |
495 | MAGIC *mg; |
490 | |
496 | |
491 | if (ecb_expect_true ( |
497 | if (ecb_expect_true ( |
… | |
… | |
496 | return mg; |
502 | return mg; |
497 | |
503 | |
498 | return 0; |
504 | return 0; |
499 | } |
505 | } |
500 | |
506 | |
501 | ECB_INLINE struct coro * |
507 | ecb_inline struct coro * |
502 | SvSTATE_ (pTHX_ SV *coro) |
508 | SvSTATE_ (pTHX_ SV *coro) |
503 | { |
509 | { |
504 | MAGIC *mg; |
510 | MAGIC *mg; |
505 | |
511 | |
506 | if (SvROK (coro)) |
512 | if (SvROK (coro)) |
… | |
… | |
520 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
526 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
521 | |
527 | |
522 | /*****************************************************************************/ |
528 | /*****************************************************************************/ |
523 | /* padlist management and caching */ |
529 | /* padlist management and caching */ |
524 | |
530 | |
525 | ECB_INLINE AV * |
531 | ecb_inline AV * |
526 | coro_derive_padlist (pTHX_ CV *cv) |
532 | coro_derive_padlist (pTHX_ CV *cv) |
527 | { |
533 | { |
528 | AV *padlist = CvPADLIST (cv); |
534 | AV *padlist = CvPADLIST (cv); |
529 | AV *newpadlist, *newpad; |
535 | AV *newpadlist, *newpad; |
530 | |
536 | |
… | |
… | |
542 | av_store (newpadlist, 1, (SV *)newpad); |
548 | av_store (newpadlist, 1, (SV *)newpad); |
543 | |
549 | |
544 | return newpadlist; |
550 | return newpadlist; |
545 | } |
551 | } |
546 | |
552 | |
547 | ECB_INLINE void |
553 | ecb_inline void |
548 | free_padlist (pTHX_ AV *padlist) |
554 | free_padlist (pTHX_ AV *padlist) |
549 | { |
555 | { |
550 | /* may be during global destruction */ |
556 | /* may be during global destruction */ |
551 | if (!IN_DESTRUCT) |
557 | if (!IN_DESTRUCT) |
552 | { |
558 | { |
… | |
… | |
595 | 0, 0, 0, 0, |
601 | 0, 0, 0, 0, |
596 | coro_cv_free |
602 | coro_cv_free |
597 | }; |
603 | }; |
598 | |
604 | |
599 | /* the next two functions merely cache the padlists */ |
605 | /* the next two functions merely cache the padlists */ |
600 | ECB_INLINE void |
606 | ecb_inline void |
601 | get_padlist (pTHX_ CV *cv) |
607 | get_padlist (pTHX_ CV *cv) |
602 | { |
608 | { |
603 | MAGIC *mg = CORO_MAGIC_cv (cv); |
609 | MAGIC *mg = CORO_MAGIC_cv (cv); |
604 | AV *av; |
610 | AV *av; |
605 | |
611 | |
… | |
… | |
618 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
624 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
619 | #endif |
625 | #endif |
620 | } |
626 | } |
621 | } |
627 | } |
622 | |
628 | |
623 | ECB_INLINE void |
629 | ecb_inline void |
624 | put_padlist (pTHX_ CV *cv) |
630 | put_padlist (pTHX_ CV *cv) |
625 | { |
631 | { |
626 | MAGIC *mg = CORO_MAGIC_cv (cv); |
632 | MAGIC *mg = CORO_MAGIC_cv (cv); |
627 | AV *av; |
633 | AV *av; |
628 | |
634 | |
… | |
… | |
637 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
643 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
638 | } |
644 | } |
639 | |
645 | |
640 | /** load & save, init *******************************************************/ |
646 | /** load & save, init *******************************************************/ |
641 | |
647 | |
|
|
648 | ecb_inline void |
|
|
649 | swap_sv (SV *a, SV *b) |
|
|
650 | { |
|
|
651 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
652 | SV tmp; |
|
|
653 | |
|
|
654 | /* swap sv_any */ |
|
|
655 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
656 | |
|
|
657 | /* swap sv_flags */ |
|
|
658 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
659 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
660 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
661 | |
|
|
662 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
663 | /* perl 5.10 and later complicates this _quite_ a bit, but it also |
|
|
664 | * is much faster, so no quarrels here. alternatively, we could |
|
|
665 | * sv_upgrade to avoid this. |
|
|
666 | */ |
|
|
667 | { |
|
|
668 | /* swap sv_u */ |
|
|
669 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
670 | |
|
|
671 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
672 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
673 | */ |
|
|
674 | #define svany_in_head(type) \ |
|
|
675 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
676 | |
|
|
677 | if (svany_in_head (SvTYPE (a))) |
|
|
678 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
679 | |
|
|
680 | if (svany_in_head (SvTYPE (b))) |
|
|
681 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
682 | } |
|
|
683 | #endif |
|
|
684 | } |
|
|
685 | |
642 | /* swap sv heads, at least logically */ |
686 | /* swap sv heads, at least logically */ |
643 | static void |
687 | static void |
644 | swap_svs (pTHX_ Coro__State c) |
688 | swap_svs (pTHX_ Coro__State c) |
645 | { |
689 | { |
646 | int i; |
690 | int i; |
647 | |
691 | |
648 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
692 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
649 | { |
693 | swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); |
650 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
651 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
652 | |
|
|
653 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
654 | SV tmp; |
|
|
655 | |
|
|
656 | /* swap sv_any */ |
|
|
657 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
658 | |
|
|
659 | /* swap sv_flags */ |
|
|
660 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
661 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
662 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
663 | |
|
|
664 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
665 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
666 | * much faster, so no quarrels here. alternatively, we could |
|
|
667 | * sv_upgrade to avoid this. |
|
|
668 | */ |
|
|
669 | { |
|
|
670 | /* swap sv_u */ |
|
|
671 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
672 | |
|
|
673 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
674 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
675 | */ |
|
|
676 | #define svany_in_head(type) \ |
|
|
677 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
678 | |
|
|
679 | if (svany_in_head (SvTYPE (a))) |
|
|
680 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
681 | |
|
|
682 | if (svany_in_head (SvTYPE (b))) |
|
|
683 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
684 | } |
|
|
685 | #endif |
|
|
686 | } |
|
|
687 | } |
694 | } |
688 | |
695 | |
689 | #define SWAP_SVS(coro) \ |
696 | #define SWAP_SVS(coro) \ |
690 | if (ecb_expect_false ((coro)->swap_sv)) \ |
697 | if (ecb_expect_false ((coro)->swap_sv)) \ |
691 | swap_svs (aTHX_ (coro)) |
698 | swap_svs (aTHX_ (coro)) |
… | |
… | |
817 | |
824 | |
818 | PUTBACK; |
825 | PUTBACK; |
819 | } |
826 | } |
820 | |
827 | |
821 | /* allocate some space on the context stack for our purposes */ |
828 | /* allocate some space on the context stack for our purposes */ |
822 | if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
829 | if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max)) |
823 | { |
830 | { |
824 | unsigned int i; |
831 | unsigned int i; |
825 | |
832 | |
826 | for (i = 0; i < SLOT_COUNT; ++i) |
833 | for (i = 0; i < SLOT_COUNT; ++i) |
827 | CXINC; |
834 | CXINC; |
… | |
… | |
879 | #endif |
886 | #endif |
880 | |
887 | |
881 | New(54,PL_scopestack,8,I32); |
888 | New(54,PL_scopestack,8,I32); |
882 | PL_scopestack_ix = 0; |
889 | PL_scopestack_ix = 0; |
883 | PL_scopestack_max = 8; |
890 | PL_scopestack_max = 8; |
|
|
891 | #if HAS_SCOPESTACK_NAME |
|
|
892 | New(54,PL_scopestack_name,8,const char*); |
|
|
893 | #endif |
884 | |
894 | |
885 | New(54,PL_savestack,24,ANY); |
895 | New(54,PL_savestack,24,ANY); |
886 | PL_savestack_ix = 0; |
896 | PL_savestack_ix = 0; |
887 | PL_savestack_max = 24; |
897 | PL_savestack_max = 24; |
888 | |
898 | |
… | |
… | |
916 | } |
926 | } |
917 | |
927 | |
918 | Safefree (PL_tmps_stack); |
928 | Safefree (PL_tmps_stack); |
919 | Safefree (PL_markstack); |
929 | Safefree (PL_markstack); |
920 | Safefree (PL_scopestack); |
930 | Safefree (PL_scopestack); |
|
|
931 | #if HAS_SCOPESTACK_NAME |
|
|
932 | Safefree (PL_scopestack_name); |
|
|
933 | #endif |
921 | Safefree (PL_savestack); |
934 | Safefree (PL_savestack); |
922 | #if !PERL_VERSION_ATLEAST (5,10,0) |
935 | #if !PERL_VERSION_ATLEAST (5,10,0) |
923 | Safefree (PL_retstack); |
936 | Safefree (PL_retstack); |
924 | #endif |
937 | #endif |
925 | } |
938 | } |
… | |
… | |
988 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
1001 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
989 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
1002 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
990 | |
1003 | |
991 | if (svp) |
1004 | if (svp) |
992 | { |
1005 | { |
993 | sv_setsv (sv, *svp ? *svp : &PL_sv_undef); |
1006 | SV *ssv; |
|
|
1007 | |
|
|
1008 | if (!*svp) |
|
|
1009 | ssv = &PL_sv_undef; |
|
|
1010 | else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ |
|
|
1011 | ssv = sv_2mortal (newRV_inc (*svp)); |
|
|
1012 | else |
|
|
1013 | ssv = *svp; |
|
|
1014 | |
|
|
1015 | sv_setsv (sv, ssv); |
994 | return 0; |
1016 | return 0; |
995 | } |
1017 | } |
996 | } |
1018 | } |
997 | |
1019 | |
998 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
1020 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
… | |
… | |
1220 | SvREFCNT_dec (coro->invoke_cb); |
1242 | SvREFCNT_dec (coro->invoke_cb); |
1221 | SvREFCNT_dec (coro->invoke_av); |
1243 | SvREFCNT_dec (coro->invoke_av); |
1222 | } |
1244 | } |
1223 | } |
1245 | } |
1224 | |
1246 | |
1225 | ECB_INLINE void |
1247 | ecb_inline void |
1226 | free_coro_mortal (pTHX) |
1248 | free_coro_mortal (pTHX) |
1227 | { |
1249 | { |
1228 | if (ecb_expect_true (coro_mortal)) |
1250 | if (ecb_expect_true (coro_mortal)) |
1229 | { |
1251 | { |
1230 | SvREFCNT_dec ((SV *)coro_mortal); |
1252 | SvREFCNT_dec ((SV *)coro_mortal); |
… | |
… | |
1388 | |
1410 | |
1389 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1411 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1390 | slf_frame.check = slf_check_set_stacklevel; |
1412 | slf_frame.check = slf_check_set_stacklevel; |
1391 | } |
1413 | } |
1392 | |
1414 | |
|
|
1415 | /* try to exit the same way perl's main function would do */ |
|
|
1416 | /* we do not bother resetting the environment or other things *7 |
|
|
1417 | /* that are not, uhm, essential */ |
|
|
1418 | /* this obviously also doesn't work when perl is embedded */ |
|
|
1419 | static void ecb_noinline ecb_cold |
|
|
1420 | perlish_exit (void) |
|
|
1421 | { |
|
|
1422 | int exitstatus = perl_destruct (PL_curinterp); |
|
|
1423 | perl_free (PL_curinterp); |
|
|
1424 | exit (exitstatus); |
|
|
1425 | } |
|
|
1426 | |
1393 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1427 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1394 | ECB_INLINE void |
1428 | ecb_inline void |
1395 | transfer_tail (pTHX) |
1429 | transfer_tail (pTHX) |
1396 | { |
1430 | { |
1397 | free_coro_mortal (aTHX); |
1431 | free_coro_mortal (aTHX); |
1398 | } |
1432 | } |
1399 | |
1433 | |
… | |
… | |
1431 | */ |
1465 | */ |
1432 | |
1466 | |
1433 | /* |
1467 | /* |
1434 | * If perl-run returns we assume exit() was being called or the coro |
1468 | * If perl-run returns we assume exit() was being called or the coro |
1435 | * fell off the end, which seems to be the only valid (non-bug) |
1469 | * fell off the end, which seems to be the only valid (non-bug) |
1436 | * reason for perl_run to return. We try to exit by jumping to the |
1470 | * reason for perl_run to return. We try to mimic whatever perl is normally |
1437 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1471 | * doing in that case. YMMV. |
1438 | * coroutine as Coro has no such concept. |
|
|
1439 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1440 | * that backend are too broken to do it in a standards-compliant way. |
|
|
1441 | */ |
1472 | */ |
1442 | PL_top_env = main_top_env; |
1473 | perlish_exit (); |
1443 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1444 | } |
1474 | } |
1445 | } |
1475 | } |
1446 | |
1476 | |
1447 | static coro_cctx * |
1477 | static coro_cctx * |
1448 | cctx_new (void) |
1478 | cctx_new (void) |
… | |
… | |
1792 | TRANSFER (ta, 1); |
1822 | TRANSFER (ta, 1); |
1793 | } |
1823 | } |
1794 | |
1824 | |
1795 | /** Coro ********************************************************************/ |
1825 | /** Coro ********************************************************************/ |
1796 | |
1826 | |
1797 | ECB_INLINE void |
1827 | ecb_inline void |
1798 | coro_enq (pTHX_ struct coro *coro) |
1828 | coro_enq (pTHX_ struct coro *coro) |
1799 | { |
1829 | { |
1800 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1830 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1801 | |
1831 | |
1802 | SvREFCNT_inc_NN (coro->hv); |
1832 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1804 | coro->next_ready = 0; |
1834 | coro->next_ready = 0; |
1805 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1835 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1806 | ready [1] = coro; |
1836 | ready [1] = coro; |
1807 | } |
1837 | } |
1808 | |
1838 | |
1809 | ECB_INLINE struct coro * |
1839 | ecb_inline struct coro * |
1810 | coro_deq (pTHX) |
1840 | coro_deq (pTHX) |
1811 | { |
1841 | { |
1812 | int prio; |
1842 | int prio; |
1813 | |
1843 | |
1814 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1844 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1867 | { |
1897 | { |
1868 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1898 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1869 | } |
1899 | } |
1870 | |
1900 | |
1871 | /* expects to own a reference to next->hv */ |
1901 | /* expects to own a reference to next->hv */ |
1872 | ECB_INLINE void |
1902 | ecb_inline void |
1873 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1903 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1874 | { |
1904 | { |
1875 | SV *prev_sv = SvRV (coro_current); |
1905 | SV *prev_sv = SvRV (coro_current); |
1876 | |
1906 | |
1877 | ta->prev = SvSTATE_hv (prev_sv); |
1907 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1936 | } |
1966 | } |
1937 | } |
1967 | } |
1938 | } |
1968 | } |
1939 | } |
1969 | } |
1940 | |
1970 | |
1941 | ECB_INLINE void |
1971 | ecb_inline void |
1942 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1972 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1943 | { |
1973 | { |
1944 | api_ready (aTHX_ coro_current); |
1974 | api_ready (aTHX_ coro_current); |
1945 | prepare_schedule (aTHX_ ta); |
1975 | prepare_schedule (aTHX_ ta); |
1946 | } |
1976 | } |
1947 | |
1977 | |
1948 | ECB_INLINE void |
1978 | ecb_inline void |
1949 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1979 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1950 | { |
1980 | { |
1951 | SV *prev = SvRV (coro_current); |
1981 | SV *prev = SvRV (coro_current); |
1952 | |
1982 | |
1953 | if (coro_nready) |
1983 | if (coro_nready) |
… | |
… | |
3343 | #endif |
3373 | #endif |
3344 | |
3374 | |
3345 | #if CORO_JIT |
3375 | #if CORO_JIT |
3346 | |
3376 | |
3347 | static void ecb_noinline ecb_cold |
3377 | static void ecb_noinline ecb_cold |
3348 | pushav_3uv (pTHX_ UV a, UV b, UV c) |
3378 | pushav_4uv (pTHX_ UV a, UV b, UV c, UV d) |
3349 | { |
3379 | { |
3350 | dSP; |
3380 | dSP; |
3351 | AV *av = newAV (); |
3381 | AV *av = newAV (); |
3352 | |
3382 | |
|
|
3383 | av_store (av, 3, newSVuv (d)); |
3353 | av_store (av, 2, newSVuv (c)); |
3384 | av_store (av, 2, newSVuv (c)); |
3354 | av_store (av, 1, newSVuv (b)); |
3385 | av_store (av, 1, newSVuv (b)); |
3355 | av_store (av, 0, newSVuv (a)); |
3386 | av_store (av, 0, newSVuv (a)); |
3356 | |
3387 | |
3357 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
3388 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
… | |
… | |
3370 | int count; |
3401 | int count; |
3371 | |
3402 | |
3372 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
3403 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
3373 | |
3404 | |
3374 | PUSHMARK (SP); |
3405 | PUSHMARK (SP); |
3375 | #define VARx(name,expr,type) pushav_3uv (aTHX_ (UV)&(expr), offsetof (perl_slots, name), sizeof (type)); |
3406 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
3376 | # include "state.h" |
3407 | # include "state.h" |
3377 | #undef VARx |
3408 | #undef VARx |
3378 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3409 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3379 | SPAGAIN; |
3410 | SPAGAIN; |
3380 | |
3411 | |
… | |
… | |
4138 | } |
4169 | } |
4139 | |
4170 | |
4140 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4171 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4141 | |
4172 | |
4142 | void |
4173 | void |
4143 | _may_delete (SV *sem, int count, int extra_refs) |
4174 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4144 | PPCODE: |
4175 | PPCODE: |
4145 | { |
4176 | { |
4146 | AV *av = (AV *)SvRV (sem); |
4177 | AV *av = (AV *)SvRV (sem); |
4147 | |
4178 | |
4148 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4179 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |