… | |
… | |
12 | #include "perl.h" |
12 | #include "perl.h" |
13 | #include "XSUB.h" |
13 | #include "XSUB.h" |
14 | #include "perliol.h" |
14 | #include "perliol.h" |
15 | |
15 | |
16 | #include "schmorp.h" |
16 | #include "schmorp.h" |
|
|
17 | |
|
|
18 | #define ECB_NO_THREADS 1 |
17 | #include "ecb.h" |
19 | #include "ecb.h" |
18 | |
20 | |
19 | #include <stddef.h> |
21 | #include <stddef.h> |
20 | #include <stdio.h> |
22 | #include <stdio.h> |
21 | #include <errno.h> |
23 | #include <errno.h> |
… | |
… | |
63 | #endif |
65 | #endif |
64 | |
66 | |
65 | /* the maximum number of idle cctx that will be pooled */ |
67 | /* the maximum number of idle cctx that will be pooled */ |
66 | static int cctx_max_idle = 4; |
68 | static int cctx_max_idle = 4; |
67 | |
69 | |
|
|
70 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
|
|
71 | # define HAS_SCOPESTACK_NAME 1 |
|
|
72 | #endif |
|
|
73 | |
68 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
74 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
69 | # undef CORO_STACKGUARD |
75 | # undef CORO_STACKGUARD |
70 | #endif |
76 | #endif |
71 | |
77 | |
72 | #ifndef CORO_STACKGUARD |
78 | #ifndef CORO_STACKGUARD |
… | |
… | |
97 | # if CORO_PTHREAD |
103 | # if CORO_PTHREAD |
98 | static void *coro_thx; |
104 | static void *coro_thx; |
99 | # endif |
105 | # endif |
100 | #endif |
106 | #endif |
101 | |
107 | |
102 | /* used in state.h */ |
|
|
103 | #define VAR(name,type) VARx(name, PL_ ## name, type) |
|
|
104 | |
|
|
105 | #ifdef __linux |
108 | #ifdef __linux |
106 | # include <time.h> /* for timespec */ |
109 | # include <time.h> /* for timespec */ |
107 | # include <syscall.h> /* for SYS_* */ |
110 | # include <syscall.h> /* for SYS_* */ |
108 | # ifdef SYS_clock_gettime |
111 | # ifdef SYS_clock_gettime |
109 | # define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
112 | # define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
… | |
… | |
177 | void *sptr; |
180 | void *sptr; |
178 | size_t ssize; |
181 | size_t ssize; |
179 | |
182 | |
180 | /* cpu state */ |
183 | /* cpu state */ |
181 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
184 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
185 | #ifndef NDEBUG |
182 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
186 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
|
|
187 | #endif |
183 | JMPENV *top_env; |
188 | JMPENV *top_env; |
184 | coro_context cctx; |
189 | coro_context cctx; |
185 | |
190 | |
186 | U32 gen; |
191 | U32 gen; |
187 | #if CORO_USE_VALGRIND |
192 | #if CORO_USE_VALGRIND |
… | |
… | |
207 | }; |
212 | }; |
208 | |
213 | |
209 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
214 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
210 | typedef struct |
215 | typedef struct |
211 | { |
216 | { |
212 | #define VARx(name,expr,type) type name; |
217 | #define VARx(name,expr,type) type name; |
213 | # include "state.h" |
218 | #include "state.h" |
214 | #undef VARx |
|
|
215 | } perl_slots; |
219 | } perl_slots; |
216 | |
220 | |
217 | /* how many context stack entries do we need for perl_slots */ |
221 | /* how many context stack entries do we need for perl_slots */ |
218 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
222 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
219 | |
223 | |
… | |
… | |
335 | |
339 | |
336 | /** time stuff **************************************************************/ |
340 | /** time stuff **************************************************************/ |
337 | |
341 | |
338 | #ifdef HAS_GETTIMEOFDAY |
342 | #ifdef HAS_GETTIMEOFDAY |
339 | |
343 | |
340 | ECB_INLINE void |
344 | ecb_inline void |
341 | coro_u2time (pTHX_ UV ret[2]) |
345 | coro_u2time (pTHX_ UV ret[2]) |
342 | { |
346 | { |
343 | struct timeval tv; |
347 | struct timeval tv; |
344 | gettimeofday (&tv, 0); |
348 | gettimeofday (&tv, 0); |
345 | |
349 | |
346 | ret [0] = tv.tv_sec; |
350 | ret [0] = tv.tv_sec; |
347 | ret [1] = tv.tv_usec; |
351 | ret [1] = tv.tv_usec; |
348 | } |
352 | } |
349 | |
353 | |
350 | ECB_INLINE double |
354 | ecb_inline double |
351 | coro_nvtime (void) |
355 | coro_nvtime (void) |
352 | { |
356 | { |
353 | struct timeval tv; |
357 | struct timeval tv; |
354 | gettimeofday (&tv, 0); |
358 | gettimeofday (&tv, 0); |
355 | |
359 | |
356 | return tv.tv_sec + tv.tv_usec * 1e-6; |
360 | return tv.tv_sec + tv.tv_usec * 1e-6; |
357 | } |
361 | } |
358 | |
362 | |
359 | ECB_INLINE void |
363 | ecb_inline void |
360 | time_init (pTHX) |
364 | time_init (pTHX) |
361 | { |
365 | { |
362 | nvtime = coro_nvtime; |
366 | nvtime = coro_nvtime; |
363 | u2time = coro_u2time; |
367 | u2time = coro_u2time; |
364 | } |
368 | } |
365 | |
369 | |
366 | #else |
370 | #else |
367 | |
371 | |
368 | ECB_INLINE void |
372 | ecb_inline void |
369 | time_init (pTHX) |
373 | time_init (pTHX) |
370 | { |
374 | { |
371 | SV **svp; |
375 | SV **svp; |
372 | |
376 | |
373 | require_pv ("Time/HiRes.pm"); |
377 | require_pv ("Time/HiRes.pm"); |
374 | |
378 | |
375 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
379 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
376 | |
380 | |
377 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
381 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
378 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
382 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
379 | |
383 | |
… | |
… | |
415 | get_hv (name, create); |
419 | get_hv (name, create); |
416 | #endif |
420 | #endif |
417 | return get_hv (name, create); |
421 | return get_hv (name, create); |
418 | } |
422 | } |
419 | |
423 | |
420 | ECB_INLINE void |
424 | ecb_inline void |
421 | coro_times_update (void) |
425 | coro_times_update (void) |
422 | { |
426 | { |
423 | #ifdef coro_clock_gettime |
427 | #ifdef coro_clock_gettime |
424 | struct timespec ts; |
428 | struct timespec ts; |
425 | |
429 | |
… | |
… | |
438 | time_real [0] = tv [0]; |
442 | time_real [0] = tv [0]; |
439 | time_real [1] = tv [1] * 1000; |
443 | time_real [1] = tv [1] * 1000; |
440 | #endif |
444 | #endif |
441 | } |
445 | } |
442 | |
446 | |
443 | ECB_INLINE void |
447 | ecb_inline void |
444 | coro_times_add (struct coro *c) |
448 | coro_times_add (struct coro *c) |
445 | { |
449 | { |
446 | c->t_real [1] += time_real [1]; |
450 | c->t_real [1] += time_real [1]; |
447 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
451 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
448 | c->t_real [0] += time_real [0]; |
452 | c->t_real [0] += time_real [0]; |
… | |
… | |
450 | c->t_cpu [1] += time_cpu [1]; |
454 | c->t_cpu [1] += time_cpu [1]; |
451 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
455 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
452 | c->t_cpu [0] += time_cpu [0]; |
456 | c->t_cpu [0] += time_cpu [0]; |
453 | } |
457 | } |
454 | |
458 | |
455 | ECB_INLINE void |
459 | ecb_inline void |
456 | coro_times_sub (struct coro *c) |
460 | coro_times_sub (struct coro *c) |
457 | { |
461 | { |
458 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
462 | 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]; |
463 | c->t_real [1] -= time_real [1]; |
460 | c->t_real [0] -= time_real [0]; |
464 | c->t_real [0] -= time_real [0]; |
… | |
… | |
481 | : 0) |
485 | : 0) |
482 | |
486 | |
483 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
487 | #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) |
488 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
485 | |
489 | |
486 | ECB_INLINE MAGIC * |
490 | ecb_inline MAGIC * |
487 | SvSTATEhv_p (pTHX_ SV *coro) |
491 | SvSTATEhv_p (pTHX_ SV *coro) |
488 | { |
492 | { |
489 | MAGIC *mg; |
493 | MAGIC *mg; |
490 | |
494 | |
491 | if (ecb_expect_true ( |
495 | if (ecb_expect_true ( |
… | |
… | |
496 | return mg; |
500 | return mg; |
497 | |
501 | |
498 | return 0; |
502 | return 0; |
499 | } |
503 | } |
500 | |
504 | |
501 | ECB_INLINE struct coro * |
505 | ecb_inline struct coro * |
502 | SvSTATE_ (pTHX_ SV *coro) |
506 | SvSTATE_ (pTHX_ SV *coro) |
503 | { |
507 | { |
504 | MAGIC *mg; |
508 | MAGIC *mg; |
505 | |
509 | |
506 | if (SvROK (coro)) |
510 | if (SvROK (coro)) |
… | |
… | |
520 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
524 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
521 | |
525 | |
522 | /*****************************************************************************/ |
526 | /*****************************************************************************/ |
523 | /* padlist management and caching */ |
527 | /* padlist management and caching */ |
524 | |
528 | |
525 | ECB_INLINE AV * |
529 | ecb_inline AV * |
526 | coro_derive_padlist (pTHX_ CV *cv) |
530 | coro_derive_padlist (pTHX_ CV *cv) |
527 | { |
531 | { |
528 | AV *padlist = CvPADLIST (cv); |
532 | AV *padlist = CvPADLIST (cv); |
529 | AV *newpadlist, *newpad; |
533 | AV *newpadlist, *newpad; |
530 | |
534 | |
… | |
… | |
542 | av_store (newpadlist, 1, (SV *)newpad); |
546 | av_store (newpadlist, 1, (SV *)newpad); |
543 | |
547 | |
544 | return newpadlist; |
548 | return newpadlist; |
545 | } |
549 | } |
546 | |
550 | |
547 | ECB_INLINE void |
551 | ecb_inline void |
548 | free_padlist (pTHX_ AV *padlist) |
552 | free_padlist (pTHX_ AV *padlist) |
549 | { |
553 | { |
550 | /* may be during global destruction */ |
554 | /* may be during global destruction */ |
551 | if (!IN_DESTRUCT) |
555 | if (!IN_DESTRUCT) |
552 | { |
556 | { |
… | |
… | |
595 | 0, 0, 0, 0, |
599 | 0, 0, 0, 0, |
596 | coro_cv_free |
600 | coro_cv_free |
597 | }; |
601 | }; |
598 | |
602 | |
599 | /* the next two functions merely cache the padlists */ |
603 | /* the next two functions merely cache the padlists */ |
600 | ECB_INLINE void |
604 | ecb_inline void |
601 | get_padlist (pTHX_ CV *cv) |
605 | get_padlist (pTHX_ CV *cv) |
602 | { |
606 | { |
603 | MAGIC *mg = CORO_MAGIC_cv (cv); |
607 | MAGIC *mg = CORO_MAGIC_cv (cv); |
604 | AV *av; |
608 | AV *av; |
605 | |
609 | |
… | |
… | |
618 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
622 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
619 | #endif |
623 | #endif |
620 | } |
624 | } |
621 | } |
625 | } |
622 | |
626 | |
623 | ECB_INLINE void |
627 | ecb_inline void |
624 | put_padlist (pTHX_ CV *cv) |
628 | put_padlist (pTHX_ CV *cv) |
625 | { |
629 | { |
626 | MAGIC *mg = CORO_MAGIC_cv (cv); |
630 | MAGIC *mg = CORO_MAGIC_cv (cv); |
627 | AV *av; |
631 | AV *av; |
628 | |
632 | |
… | |
… | |
637 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
641 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
638 | } |
642 | } |
639 | |
643 | |
640 | /** load & save, init *******************************************************/ |
644 | /** load & save, init *******************************************************/ |
641 | |
645 | |
|
|
646 | ecb_inline void |
|
|
647 | swap_sv (SV *a, SV *b) |
|
|
648 | { |
|
|
649 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
650 | SV tmp; |
|
|
651 | |
|
|
652 | /* swap sv_any */ |
|
|
653 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
654 | |
|
|
655 | /* swap sv_flags */ |
|
|
656 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
657 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
658 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
659 | |
|
|
660 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
661 | /* perl 5.10 and later complicates this _quite_ a bit, but it also |
|
|
662 | * is much faster, so no quarrels here. alternatively, we could |
|
|
663 | * sv_upgrade to avoid this. |
|
|
664 | */ |
|
|
665 | { |
|
|
666 | /* swap sv_u */ |
|
|
667 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
668 | |
|
|
669 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
670 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
671 | */ |
|
|
672 | #define svany_in_head(type) \ |
|
|
673 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
674 | |
|
|
675 | if (svany_in_head (SvTYPE (a))) |
|
|
676 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
677 | |
|
|
678 | if (svany_in_head (SvTYPE (b))) |
|
|
679 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
680 | } |
|
|
681 | #endif |
|
|
682 | } |
|
|
683 | |
642 | /* swap sv heads, at least logically */ |
684 | /* swap sv heads, at least logically */ |
643 | static void |
685 | static void |
644 | swap_svs (pTHX_ Coro__State c) |
686 | swap_svs (pTHX_ Coro__State c) |
645 | { |
687 | { |
646 | int i; |
688 | int i; |
647 | |
689 | |
648 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
690 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
649 | { |
691 | 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 | } |
692 | } |
688 | |
693 | |
689 | #define SWAP_SVS(coro) \ |
694 | #define SWAP_SVS(coro) \ |
690 | if (ecb_expect_false ((coro)->swap_sv)) \ |
695 | if (ecb_expect_false ((coro)->swap_sv)) \ |
691 | swap_svs (aTHX_ (coro)) |
696 | swap_svs (aTHX_ (coro)) |
… | |
… | |
703 | |
708 | |
704 | #if CORO_JIT |
709 | #if CORO_JIT |
705 | load_perl_slots (slot); |
710 | load_perl_slots (slot); |
706 | #else |
711 | #else |
707 | #define VARx(name,expr,type) expr = slot->name; |
712 | #define VARx(name,expr,type) expr = slot->name; |
708 | # include "state.h" |
713 | #include "state.h" |
709 | #undef VARx |
|
|
710 | #endif |
714 | #endif |
711 | |
715 | |
712 | { |
716 | { |
713 | dSP; |
717 | dSP; |
714 | |
718 | |
… | |
… | |
836 | |
840 | |
837 | #if CORO_JIT |
841 | #if CORO_JIT |
838 | save_perl_slots (slot); |
842 | save_perl_slots (slot); |
839 | #else |
843 | #else |
840 | #define VARx(name,expr,type) slot->name = expr; |
844 | #define VARx(name,expr,type) slot->name = expr; |
841 | # include "state.h" |
845 | #include "state.h" |
842 | #undef VARx |
|
|
843 | #endif |
846 | #endif |
844 | } |
847 | } |
845 | } |
848 | } |
846 | |
849 | |
847 | /* |
850 | /* |
… | |
… | |
879 | #endif |
882 | #endif |
880 | |
883 | |
881 | New(54,PL_scopestack,8,I32); |
884 | New(54,PL_scopestack,8,I32); |
882 | PL_scopestack_ix = 0; |
885 | PL_scopestack_ix = 0; |
883 | PL_scopestack_max = 8; |
886 | PL_scopestack_max = 8; |
|
|
887 | #if HAS_SCOPESTACK_NAME |
|
|
888 | New(54,PL_scopestack_name,8,const char*); |
|
|
889 | #endif |
884 | |
890 | |
885 | New(54,PL_savestack,24,ANY); |
891 | New(54,PL_savestack,24,ANY); |
886 | PL_savestack_ix = 0; |
892 | PL_savestack_ix = 0; |
887 | PL_savestack_max = 24; |
893 | PL_savestack_max = 24; |
888 | |
894 | |
… | |
… | |
916 | } |
922 | } |
917 | |
923 | |
918 | Safefree (PL_tmps_stack); |
924 | Safefree (PL_tmps_stack); |
919 | Safefree (PL_markstack); |
925 | Safefree (PL_markstack); |
920 | Safefree (PL_scopestack); |
926 | Safefree (PL_scopestack); |
|
|
927 | #if HAS_SCOPESTACK_NAME |
|
|
928 | Safefree (PL_scopestack_name); |
|
|
929 | #endif |
921 | Safefree (PL_savestack); |
930 | Safefree (PL_savestack); |
922 | #if !PERL_VERSION_ATLEAST (5,10,0) |
931 | #if !PERL_VERSION_ATLEAST (5,10,0) |
923 | Safefree (PL_retstack); |
932 | Safefree (PL_retstack); |
924 | #endif |
933 | #endif |
925 | } |
934 | } |
… | |
… | |
985 | { |
994 | { |
986 | SV **svp = 0; |
995 | SV **svp = 0; |
987 | |
996 | |
988 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
997 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
989 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
998 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
990 | |
999 | |
991 | if (svp) |
1000 | if (svp) |
992 | { |
1001 | { |
993 | sv_setsv (sv, *svp ? *svp : &PL_sv_undef); |
1002 | SV *ssv; |
|
|
1003 | |
|
|
1004 | if (!*svp) |
|
|
1005 | ssv = &PL_sv_undef; |
|
|
1006 | else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ |
|
|
1007 | ssv = sv_2mortal (newRV_inc (*svp)); |
|
|
1008 | else |
|
|
1009 | ssv = *svp; |
|
|
1010 | |
|
|
1011 | sv_setsv (sv, ssv); |
994 | return 0; |
1012 | return 0; |
995 | } |
1013 | } |
996 | } |
1014 | } |
997 | |
1015 | |
998 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
1016 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
… | |
… | |
1092 | PL_hints = 0; |
1110 | PL_hints = 0; |
1093 | |
1111 | |
1094 | /* recreate the die/warn hooks */ |
1112 | /* recreate the die/warn hooks */ |
1095 | PL_diehook = SvREFCNT_inc (rv_diehook); |
1113 | PL_diehook = SvREFCNT_inc (rv_diehook); |
1096 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
1114 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
1097 | |
1115 | |
1098 | GvSV (PL_defgv) = newSV (0); |
1116 | GvSV (PL_defgv) = newSV (0); |
1099 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1117 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1100 | GvSV (PL_errgv) = newSV (0); |
1118 | GvSV (PL_errgv) = newSV (0); |
1101 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1119 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1102 | GvHV (PL_hintgv) = 0; |
1120 | GvHV (PL_hintgv) = 0; |
… | |
… | |
1220 | SvREFCNT_dec (coro->invoke_cb); |
1238 | SvREFCNT_dec (coro->invoke_cb); |
1221 | SvREFCNT_dec (coro->invoke_av); |
1239 | SvREFCNT_dec (coro->invoke_av); |
1222 | } |
1240 | } |
1223 | } |
1241 | } |
1224 | |
1242 | |
1225 | ECB_INLINE void |
1243 | ecb_inline void |
1226 | free_coro_mortal (pTHX) |
1244 | free_coro_mortal (pTHX) |
1227 | { |
1245 | { |
1228 | if (ecb_expect_true (coro_mortal)) |
1246 | if (ecb_expect_true (coro_mortal)) |
1229 | { |
1247 | { |
1230 | SvREFCNT_dec ((SV *)coro_mortal); |
1248 | SvREFCNT_dec ((SV *)coro_mortal); |
… | |
… | |
1389 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1407 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1390 | slf_frame.check = slf_check_set_stacklevel; |
1408 | slf_frame.check = slf_check_set_stacklevel; |
1391 | } |
1409 | } |
1392 | |
1410 | |
1393 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1411 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1394 | ECB_INLINE void |
1412 | ecb_inline void |
1395 | transfer_tail (pTHX) |
1413 | transfer_tail (pTHX) |
1396 | { |
1414 | { |
1397 | free_coro_mortal (aTHX); |
1415 | free_coro_mortal (aTHX); |
|
|
1416 | } |
|
|
1417 | |
|
|
1418 | /* try to exit the same way perl's main function would do */ |
|
|
1419 | /* we do not bother resetting the environment or other things *7 |
|
|
1420 | /* that are not, uhm, essential */ |
|
|
1421 | /* this obviously also doesn't work when perl is embedded */ |
|
|
1422 | static void ecb_noinline ecb_cold |
|
|
1423 | perlish_exit (pTHX) |
|
|
1424 | { |
|
|
1425 | int exitstatus = perl_destruct (PL_curinterp); |
|
|
1426 | perl_free (PL_curinterp); |
|
|
1427 | exit (exitstatus); |
1398 | } |
1428 | } |
1399 | |
1429 | |
1400 | /* |
1430 | /* |
1401 | * this is a _very_ stripped down perl interpreter ;) |
1431 | * this is a _very_ stripped down perl interpreter ;) |
1402 | */ |
1432 | */ |
… | |
… | |
1431 | */ |
1461 | */ |
1432 | |
1462 | |
1433 | /* |
1463 | /* |
1434 | * If perl-run returns we assume exit() was being called or the coro |
1464 | * 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) |
1465 | * 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 |
1466 | * 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" |
1467 | * 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 | */ |
1468 | */ |
1442 | PL_top_env = main_top_env; |
1469 | perlish_exit (aTHX); |
1443 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1444 | } |
1470 | } |
1445 | } |
1471 | } |
1446 | |
1472 | |
1447 | static coro_cctx * |
1473 | static coro_cctx * |
1448 | cctx_new (void) |
1474 | cctx_new (void) |
… | |
… | |
1480 | size_t stack_size; |
1506 | size_t stack_size; |
1481 | |
1507 | |
1482 | #if HAVE_MMAP |
1508 | #if HAVE_MMAP |
1483 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1509 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1484 | /* mmap supposedly does allocate-on-write for us */ |
1510 | /* mmap supposedly does allocate-on-write for us */ |
1485 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0); |
1511 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0); |
1486 | |
1512 | |
1487 | if (cctx->sptr != (void *)-1) |
1513 | if (cctx->sptr != (void *)-1) |
1488 | { |
1514 | { |
1489 | #if CORO_STACKGUARD |
1515 | #if CORO_STACKGUARD |
1490 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1516 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
… | |
… | |
1705 | return; |
1731 | return; |
1706 | |
1732 | |
1707 | slf_destroy (aTHX_ coro); |
1733 | slf_destroy (aTHX_ coro); |
1708 | |
1734 | |
1709 | coro->flags |= CF_ZOMBIE; |
1735 | coro->flags |= CF_ZOMBIE; |
1710 | |
1736 | |
1711 | if (coro->flags & CF_READY) |
1737 | if (coro->flags & CF_READY) |
1712 | { |
1738 | { |
1713 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1739 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1714 | /* alternative: look through all ready queues and remove the coro */ |
1740 | /* alternative: look through all ready queues and remove the coro */ |
1715 | --coro_nready; |
1741 | --coro_nready; |
… | |
… | |
1792 | TRANSFER (ta, 1); |
1818 | TRANSFER (ta, 1); |
1793 | } |
1819 | } |
1794 | |
1820 | |
1795 | /** Coro ********************************************************************/ |
1821 | /** Coro ********************************************************************/ |
1796 | |
1822 | |
1797 | ECB_INLINE void |
1823 | ecb_inline void |
1798 | coro_enq (pTHX_ struct coro *coro) |
1824 | coro_enq (pTHX_ struct coro *coro) |
1799 | { |
1825 | { |
1800 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1826 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1801 | |
1827 | |
1802 | SvREFCNT_inc_NN (coro->hv); |
1828 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1804 | coro->next_ready = 0; |
1830 | coro->next_ready = 0; |
1805 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1831 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1806 | ready [1] = coro; |
1832 | ready [1] = coro; |
1807 | } |
1833 | } |
1808 | |
1834 | |
1809 | ECB_INLINE struct coro * |
1835 | ecb_inline struct coro * |
1810 | coro_deq (pTHX) |
1836 | coro_deq (pTHX) |
1811 | { |
1837 | { |
1812 | int prio; |
1838 | int prio; |
1813 | |
1839 | |
1814 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1840 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1867 | { |
1893 | { |
1868 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1894 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1869 | } |
1895 | } |
1870 | |
1896 | |
1871 | /* expects to own a reference to next->hv */ |
1897 | /* expects to own a reference to next->hv */ |
1872 | ECB_INLINE void |
1898 | ecb_inline void |
1873 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1899 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1874 | { |
1900 | { |
1875 | SV *prev_sv = SvRV (coro_current); |
1901 | SV *prev_sv = SvRV (coro_current); |
1876 | |
1902 | |
1877 | ta->prev = SvSTATE_hv (prev_sv); |
1903 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1936 | } |
1962 | } |
1937 | } |
1963 | } |
1938 | } |
1964 | } |
1939 | } |
1965 | } |
1940 | |
1966 | |
1941 | ECB_INLINE void |
1967 | ecb_inline void |
1942 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1968 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1943 | { |
1969 | { |
1944 | api_ready (aTHX_ coro_current); |
1970 | api_ready (aTHX_ coro_current); |
1945 | prepare_schedule (aTHX_ ta); |
1971 | prepare_schedule (aTHX_ ta); |
1946 | } |
1972 | } |
1947 | |
1973 | |
1948 | ECB_INLINE void |
1974 | ecb_inline void |
1949 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1975 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1950 | { |
1976 | { |
1951 | SV *prev = SvRV (coro_current); |
1977 | SV *prev = SvRV (coro_current); |
1952 | |
1978 | |
1953 | if (coro_nready) |
1979 | if (coro_nready) |
… | |
… | |
2148 | |
2174 | |
2149 | static void |
2175 | static void |
2150 | coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) |
2176 | coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) |
2151 | { |
2177 | { |
2152 | AV *av; |
2178 | AV *av; |
2153 | |
2179 | |
2154 | if (coro->status) |
2180 | if (coro->status) |
2155 | { |
2181 | { |
2156 | av = coro->status; |
2182 | av = coro->status; |
2157 | av_clear (av); |
2183 | av_clear (av); |
2158 | } |
2184 | } |
… | |
… | |
2205 | |
2231 | |
2206 | coro = SvSTATE (arg [0]); |
2232 | coro = SvSTATE (arg [0]); |
2207 | coro_hv = coro->hv; |
2233 | coro_hv = coro->hv; |
2208 | |
2234 | |
2209 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2235 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2210 | |
2236 | |
2211 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2237 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2212 | { |
2238 | { |
2213 | /* coro currently busy cancelling something, so just notify it */ |
2239 | /* coro currently busy cancelling something, so just notify it */ |
2214 | coro->slf_frame.data = (void *)coro; |
2240 | coro->slf_frame.data = (void *)coro; |
2215 | |
2241 | |
… | |
… | |
2394 | |
2420 | |
2395 | static int |
2421 | static int |
2396 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
2422 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
2397 | { |
2423 | { |
2398 | SV *data = (SV *)frame->data; |
2424 | SV *data = (SV *)frame->data; |
2399 | |
2425 | |
2400 | if (CORO_THROW) |
2426 | if (CORO_THROW) |
2401 | return 0; |
2427 | return 0; |
2402 | |
2428 | |
2403 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2429 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2404 | return 1; |
2430 | return 1; |
… | |
… | |
2440 | cb = sv_2mortal (coro->rouse_cb); |
2466 | cb = sv_2mortal (coro->rouse_cb); |
2441 | coro->rouse_cb = 0; |
2467 | coro->rouse_cb = 0; |
2442 | } |
2468 | } |
2443 | |
2469 | |
2444 | if (!SvROK (cb) |
2470 | if (!SvROK (cb) |
2445 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2471 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2446 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2472 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2447 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2473 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2448 | |
2474 | |
2449 | { |
2475 | { |
2450 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
2476 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
… | |
… | |
3371 | int count; |
3397 | int count; |
3372 | |
3398 | |
3373 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
3399 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
3374 | |
3400 | |
3375 | PUSHMARK (SP); |
3401 | PUSHMARK (SP); |
3376 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
3402 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
3377 | # include "state.h" |
3403 | #include "state.h" |
3378 | #undef VARx |
|
|
3379 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3404 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3380 | SPAGAIN; |
3405 | SPAGAIN; |
3381 | |
3406 | |
3382 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
3407 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
3383 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
3408 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
… | |
… | |
3640 | RETVAL = boolSV (coro->flags & ix); |
3665 | RETVAL = boolSV (coro->flags & ix); |
3641 | OUTPUT: |
3666 | OUTPUT: |
3642 | RETVAL |
3667 | RETVAL |
3643 | |
3668 | |
3644 | void |
3669 | void |
3645 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3670 | throw (SV *self, SV *exception = &PL_sv_undef) |
3646 | PROTOTYPE: $;$ |
3671 | PROTOTYPE: $;$ |
3647 | CODE: |
3672 | CODE: |
3648 | { |
3673 | { |
|
|
3674 | struct coro *coro = SvSTATE (self); |
3649 | struct coro *current = SvSTATE_current; |
3675 | struct coro *current = SvSTATE_current; |
3650 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3676 | SV **exceptionp = coro == current ? &CORO_THROW : &coro->except; |
3651 | SvREFCNT_dec (*exceptionp); |
3677 | SvREFCNT_dec (*exceptionp); |
3652 | SvGETMAGIC (exception); |
3678 | SvGETMAGIC (exception); |
3653 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3679 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
|
|
3680 | |
|
|
3681 | api_ready (aTHX_ self); |
3654 | } |
3682 | } |
3655 | |
3683 | |
3656 | void |
3684 | void |
3657 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3685 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3658 | PROTOTYPE: $;$ |
3686 | PROTOTYPE: $;$ |
… | |
… | |
3788 | |
3816 | |
3789 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3817 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3790 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3818 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3791 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3819 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3792 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3820 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3793 | |
3821 | |
3794 | coro_stash = gv_stashpv ("Coro", TRUE); |
3822 | coro_stash = gv_stashpv ("Coro", TRUE); |
3795 | |
3823 | |
3796 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3824 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3797 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3825 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3798 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3826 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
4139 | } |
4167 | } |
4140 | |
4168 | |
4141 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4169 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4142 | |
4170 | |
4143 | void |
4171 | void |
4144 | _may_delete (SV *sem, int count, int extra_refs) |
4172 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4145 | PPCODE: |
4173 | PPCODE: |
4146 | { |
4174 | { |
4147 | AV *av = (AV *)SvRV (sem); |
4175 | AV *av = (AV *)SvRV (sem); |
4148 | |
4176 | |
4149 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4177 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |