… | |
… | |
33 | # define setjmp _setjmp /* deep magic */ |
33 | # define setjmp _setjmp /* deep magic */ |
34 | #else |
34 | #else |
35 | # include <inttypes.h> /* most portable stdint.h */ |
35 | # include <inttypes.h> /* most portable stdint.h */ |
36 | #endif |
36 | #endif |
37 | |
37 | |
38 | #ifdef HAVE_MMAP |
38 | #if HAVE_MMAP |
39 | # include <unistd.h> |
39 | # include <unistd.h> |
40 | # include <sys/mman.h> |
40 | # include <sys/mman.h> |
41 | # ifndef MAP_ANONYMOUS |
41 | # ifndef MAP_ANONYMOUS |
42 | # ifdef MAP_ANON |
42 | # ifdef MAP_ANON |
43 | # define MAP_ANONYMOUS MAP_ANON |
43 | # define MAP_ANONYMOUS MAP_ANON |
… | |
… | |
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 |
… | |
… | |
212 | #define VARx(name,expr,type) type name; |
218 | #define VARx(name,expr,type) type name; |
213 | # include "state.h" |
219 | # include "state.h" |
214 | #undef VARx |
220 | #undef VARx |
215 | } perl_slots; |
221 | } perl_slots; |
216 | |
222 | |
217 | // how many context stack entries do we need for perl_slots |
223 | /* 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)) |
224 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
219 | |
225 | |
220 | /* this is a structure representing a perl-level coroutine */ |
226 | /* this is a structure representing a perl-level coroutine */ |
221 | struct coro |
227 | struct coro |
222 | { |
228 | { |
… | |
… | |
291 | #define coro_nready coroapi.nready |
297 | #define coro_nready coroapi.nready |
292 | |
298 | |
293 | /** JIT *********************************************************************/ |
299 | /** JIT *********************************************************************/ |
294 | |
300 | |
295 | #if CORO_JIT |
301 | #if CORO_JIT |
|
|
302 | /* APPLE doesn't have HAVE_MMAP though */ |
|
|
303 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
296 | #ifndef CORO_JIT_TYPE |
304 | #ifndef CORO_JIT_TYPE |
297 | #if __linux && __amd64 |
305 | #if __x86_64 && CORO_JIT_UNIXY |
298 | #define CORO_JIT_TYPE "amd64-unix" |
306 | #define CORO_JIT_TYPE "amd64-unix" |
299 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
307 | #elif __i386 && CORO_JIT_UNIXY |
300 | #else |
308 | #define CORO_JIT_TYPE "x86-unix" |
301 | #undef CORO_JIT |
|
|
302 | #endif |
309 | #endif |
303 | #endif |
310 | #endif |
304 | #endif |
311 | #endif |
305 | |
312 | |
|
|
313 | #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP |
|
|
314 | #undef CORO_JIT |
|
|
315 | #endif |
|
|
316 | |
306 | #if CORO_JIT |
317 | #if CORO_JIT |
307 | |
318 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
308 | static load_save_perl_slots_type load_perl_slots, save_perl_slots; |
319 | static load_save_perl_slots_type load_perl_slots, save_perl_slots; |
309 | |
|
|
310 | #endif |
320 | #endif |
311 | |
321 | |
312 | /** Coro::Select ************************************************************/ |
322 | /** Coro::Select ************************************************************/ |
313 | |
323 | |
314 | static OP *(*coro_old_pp_sselect) (pTHX); |
324 | static OP *(*coro_old_pp_sselect) (pTHX); |
… | |
… | |
331 | |
341 | |
332 | /** time stuff **************************************************************/ |
342 | /** time stuff **************************************************************/ |
333 | |
343 | |
334 | #ifdef HAS_GETTIMEOFDAY |
344 | #ifdef HAS_GETTIMEOFDAY |
335 | |
345 | |
336 | ECB_INLINE void |
346 | ecb_inline void |
337 | coro_u2time (pTHX_ UV ret[2]) |
347 | coro_u2time (pTHX_ UV ret[2]) |
338 | { |
348 | { |
339 | struct timeval tv; |
349 | struct timeval tv; |
340 | gettimeofday (&tv, 0); |
350 | gettimeofday (&tv, 0); |
341 | |
351 | |
342 | ret [0] = tv.tv_sec; |
352 | ret [0] = tv.tv_sec; |
343 | ret [1] = tv.tv_usec; |
353 | ret [1] = tv.tv_usec; |
344 | } |
354 | } |
345 | |
355 | |
346 | ECB_INLINE double |
356 | ecb_inline double |
347 | coro_nvtime () |
357 | coro_nvtime (void) |
348 | { |
358 | { |
349 | struct timeval tv; |
359 | struct timeval tv; |
350 | gettimeofday (&tv, 0); |
360 | gettimeofday (&tv, 0); |
351 | |
361 | |
352 | return tv.tv_sec + tv.tv_usec * 1e-6; |
362 | return tv.tv_sec + tv.tv_usec * 1e-6; |
353 | } |
363 | } |
354 | |
364 | |
355 | ECB_INLINE void |
365 | ecb_inline void |
356 | time_init (pTHX) |
366 | time_init (pTHX) |
357 | { |
367 | { |
358 | nvtime = coro_nvtime; |
368 | nvtime = coro_nvtime; |
359 | u2time = coro_u2time; |
369 | u2time = coro_u2time; |
360 | } |
370 | } |
361 | |
371 | |
362 | #else |
372 | #else |
363 | |
373 | |
364 | ECB_INLINE void |
374 | ecb_inline void |
365 | time_init (pTHX) |
375 | time_init (pTHX) |
366 | { |
376 | { |
367 | SV **svp; |
377 | SV **svp; |
368 | |
378 | |
369 | require_pv ("Time/HiRes.pm"); |
379 | require_pv ("Time/HiRes.pm"); |
… | |
… | |
411 | get_hv (name, create); |
421 | get_hv (name, create); |
412 | #endif |
422 | #endif |
413 | return get_hv (name, create); |
423 | return get_hv (name, create); |
414 | } |
424 | } |
415 | |
425 | |
416 | ECB_INLINE void |
426 | ecb_inline void |
417 | coro_times_update () |
427 | coro_times_update (void) |
418 | { |
428 | { |
419 | #ifdef coro_clock_gettime |
429 | #ifdef coro_clock_gettime |
420 | struct timespec ts; |
430 | struct timespec ts; |
421 | |
431 | |
422 | ts.tv_sec = ts.tv_nsec = 0; |
432 | ts.tv_sec = ts.tv_nsec = 0; |
… | |
… | |
434 | time_real [0] = tv [0]; |
444 | time_real [0] = tv [0]; |
435 | time_real [1] = tv [1] * 1000; |
445 | time_real [1] = tv [1] * 1000; |
436 | #endif |
446 | #endif |
437 | } |
447 | } |
438 | |
448 | |
439 | ECB_INLINE void |
449 | ecb_inline void |
440 | coro_times_add (struct coro *c) |
450 | coro_times_add (struct coro *c) |
441 | { |
451 | { |
442 | c->t_real [1] += time_real [1]; |
452 | c->t_real [1] += time_real [1]; |
443 | 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]; } |
444 | c->t_real [0] += time_real [0]; |
454 | c->t_real [0] += time_real [0]; |
… | |
… | |
446 | c->t_cpu [1] += time_cpu [1]; |
456 | c->t_cpu [1] += time_cpu [1]; |
447 | 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]; } |
448 | c->t_cpu [0] += time_cpu [0]; |
458 | c->t_cpu [0] += time_cpu [0]; |
449 | } |
459 | } |
450 | |
460 | |
451 | ECB_INLINE void |
461 | ecb_inline void |
452 | coro_times_sub (struct coro *c) |
462 | coro_times_sub (struct coro *c) |
453 | { |
463 | { |
454 | 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]; } |
455 | c->t_real [1] -= time_real [1]; |
465 | c->t_real [1] -= time_real [1]; |
456 | c->t_real [0] -= time_real [0]; |
466 | c->t_real [0] -= time_real [0]; |
… | |
… | |
477 | : 0) |
487 | : 0) |
478 | |
488 | |
479 | #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) |
480 | #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) |
481 | |
491 | |
482 | ECB_INLINE MAGIC * |
492 | ecb_inline MAGIC * |
483 | SvSTATEhv_p (pTHX_ SV *coro) |
493 | SvSTATEhv_p (pTHX_ SV *coro) |
484 | { |
494 | { |
485 | MAGIC *mg; |
495 | MAGIC *mg; |
486 | |
496 | |
487 | if (ecb_expect_true ( |
497 | if (ecb_expect_true ( |
… | |
… | |
492 | return mg; |
502 | return mg; |
493 | |
503 | |
494 | return 0; |
504 | return 0; |
495 | } |
505 | } |
496 | |
506 | |
497 | ECB_INLINE struct coro * |
507 | ecb_inline struct coro * |
498 | SvSTATE_ (pTHX_ SV *coro) |
508 | SvSTATE_ (pTHX_ SV *coro) |
499 | { |
509 | { |
500 | MAGIC *mg; |
510 | MAGIC *mg; |
501 | |
511 | |
502 | if (SvROK (coro)) |
512 | if (SvROK (coro)) |
… | |
… | |
516 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
526 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
517 | |
527 | |
518 | /*****************************************************************************/ |
528 | /*****************************************************************************/ |
519 | /* padlist management and caching */ |
529 | /* padlist management and caching */ |
520 | |
530 | |
521 | ECB_INLINE AV * |
531 | ecb_inline AV * |
522 | coro_derive_padlist (pTHX_ CV *cv) |
532 | coro_derive_padlist (pTHX_ CV *cv) |
523 | { |
533 | { |
524 | AV *padlist = CvPADLIST (cv); |
534 | AV *padlist = CvPADLIST (cv); |
525 | AV *newpadlist, *newpad; |
535 | AV *newpadlist, *newpad; |
526 | |
536 | |
… | |
… | |
538 | av_store (newpadlist, 1, (SV *)newpad); |
548 | av_store (newpadlist, 1, (SV *)newpad); |
539 | |
549 | |
540 | return newpadlist; |
550 | return newpadlist; |
541 | } |
551 | } |
542 | |
552 | |
543 | ECB_INLINE void |
553 | ecb_inline void |
544 | free_padlist (pTHX_ AV *padlist) |
554 | free_padlist (pTHX_ AV *padlist) |
545 | { |
555 | { |
546 | /* may be during global destruction */ |
556 | /* may be during global destruction */ |
547 | if (!IN_DESTRUCT) |
557 | if (!IN_DESTRUCT) |
548 | { |
558 | { |
… | |
… | |
591 | 0, 0, 0, 0, |
601 | 0, 0, 0, 0, |
592 | coro_cv_free |
602 | coro_cv_free |
593 | }; |
603 | }; |
594 | |
604 | |
595 | /* the next two functions merely cache the padlists */ |
605 | /* the next two functions merely cache the padlists */ |
596 | ECB_INLINE void |
606 | ecb_inline void |
597 | get_padlist (pTHX_ CV *cv) |
607 | get_padlist (pTHX_ CV *cv) |
598 | { |
608 | { |
599 | MAGIC *mg = CORO_MAGIC_cv (cv); |
609 | MAGIC *mg = CORO_MAGIC_cv (cv); |
600 | AV *av; |
610 | AV *av; |
601 | |
611 | |
… | |
… | |
614 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
624 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
615 | #endif |
625 | #endif |
616 | } |
626 | } |
617 | } |
627 | } |
618 | |
628 | |
619 | ECB_INLINE void |
629 | ecb_inline void |
620 | put_padlist (pTHX_ CV *cv) |
630 | put_padlist (pTHX_ CV *cv) |
621 | { |
631 | { |
622 | MAGIC *mg = CORO_MAGIC_cv (cv); |
632 | MAGIC *mg = CORO_MAGIC_cv (cv); |
623 | AV *av; |
633 | AV *av; |
624 | |
634 | |
… | |
… | |
633 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
643 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
634 | } |
644 | } |
635 | |
645 | |
636 | /** load & save, init *******************************************************/ |
646 | /** load & save, init *******************************************************/ |
637 | |
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 | |
638 | /* swap sv heads, at least logically */ |
686 | /* swap sv heads, at least logically */ |
639 | static void |
687 | static void |
640 | swap_svs (pTHX_ Coro__State c) |
688 | swap_svs (pTHX_ Coro__State c) |
641 | { |
689 | { |
642 | int i; |
690 | int i; |
643 | |
691 | |
644 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
692 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
645 | { |
693 | swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); |
646 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
647 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
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 complicates this _quite_ a bit, but it also is |
|
|
662 | * 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 | } |
694 | } |
684 | |
695 | |
685 | #define SWAP_SVS(coro) \ |
696 | #define SWAP_SVS(coro) \ |
686 | if (ecb_expect_false ((coro)->swap_sv)) \ |
697 | if (ecb_expect_false ((coro)->swap_sv)) \ |
687 | swap_svs (aTHX_ (coro)) |
698 | swap_svs (aTHX_ (coro)) |
… | |
… | |
813 | |
824 | |
814 | PUTBACK; |
825 | PUTBACK; |
815 | } |
826 | } |
816 | |
827 | |
817 | /* allocate some space on the context stack for our purposes */ |
828 | /* allocate some space on the context stack for our purposes */ |
818 | if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
829 | if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max)) |
819 | { |
830 | { |
820 | unsigned int i; |
831 | unsigned int i; |
821 | |
832 | |
822 | for (i = 0; i < SLOT_COUNT; ++i) |
833 | for (i = 0; i < SLOT_COUNT; ++i) |
823 | CXINC; |
834 | CXINC; |
… | |
… | |
875 | #endif |
886 | #endif |
876 | |
887 | |
877 | New(54,PL_scopestack,8,I32); |
888 | New(54,PL_scopestack,8,I32); |
878 | PL_scopestack_ix = 0; |
889 | PL_scopestack_ix = 0; |
879 | 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 |
880 | |
894 | |
881 | New(54,PL_savestack,24,ANY); |
895 | New(54,PL_savestack,24,ANY); |
882 | PL_savestack_ix = 0; |
896 | PL_savestack_ix = 0; |
883 | PL_savestack_max = 24; |
897 | PL_savestack_max = 24; |
884 | |
898 | |
… | |
… | |
912 | } |
926 | } |
913 | |
927 | |
914 | Safefree (PL_tmps_stack); |
928 | Safefree (PL_tmps_stack); |
915 | Safefree (PL_markstack); |
929 | Safefree (PL_markstack); |
916 | Safefree (PL_scopestack); |
930 | Safefree (PL_scopestack); |
|
|
931 | #if HAS_SCOPESTACK_NAME |
|
|
932 | Safefree (PL_scopestack_name); |
|
|
933 | #endif |
917 | Safefree (PL_savestack); |
934 | Safefree (PL_savestack); |
918 | #if !PERL_VERSION_ATLEAST (5,10,0) |
935 | #if !PERL_VERSION_ATLEAST (5,10,0) |
919 | Safefree (PL_retstack); |
936 | Safefree (PL_retstack); |
920 | #endif |
937 | #endif |
921 | } |
938 | } |
… | |
… | |
984 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
1001 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
985 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
1002 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
986 | |
1003 | |
987 | if (svp) |
1004 | if (svp) |
988 | { |
1005 | { |
989 | 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); |
990 | return 0; |
1016 | return 0; |
991 | } |
1017 | } |
992 | } |
1018 | } |
993 | |
1019 | |
994 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
1020 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
… | |
… | |
1186 | /* restore swapped sv's */ |
1212 | /* restore swapped sv's */ |
1187 | SWAP_SVS (coro); |
1213 | SWAP_SVS (coro); |
1188 | |
1214 | |
1189 | coro_destruct_stacks (aTHX); |
1215 | coro_destruct_stacks (aTHX); |
1190 | |
1216 | |
1191 | // now save some sv's to be free'd later |
1217 | /* now save some sv's to be free'd later */ |
1192 | svf [0] = GvSV (PL_defgv); |
1218 | svf [0] = GvSV (PL_defgv); |
1193 | svf [1] = (SV *)GvAV (PL_defgv); |
1219 | svf [1] = (SV *)GvAV (PL_defgv); |
1194 | svf [2] = GvSV (PL_errgv); |
1220 | svf [2] = GvSV (PL_errgv); |
1195 | svf [3] = (SV *)PL_defoutgv; |
1221 | svf [3] = (SV *)PL_defoutgv; |
1196 | svf [4] = PL_rs; |
1222 | svf [4] = PL_rs; |
… | |
… | |
1216 | SvREFCNT_dec (coro->invoke_cb); |
1242 | SvREFCNT_dec (coro->invoke_cb); |
1217 | SvREFCNT_dec (coro->invoke_av); |
1243 | SvREFCNT_dec (coro->invoke_av); |
1218 | } |
1244 | } |
1219 | } |
1245 | } |
1220 | |
1246 | |
1221 | ECB_INLINE void |
1247 | ecb_inline void |
1222 | free_coro_mortal (pTHX) |
1248 | free_coro_mortal (pTHX) |
1223 | { |
1249 | { |
1224 | if (ecb_expect_true (coro_mortal)) |
1250 | if (ecb_expect_true (coro_mortal)) |
1225 | { |
1251 | { |
1226 | SvREFCNT_dec ((SV *)coro_mortal); |
1252 | SvREFCNT_dec ((SV *)coro_mortal); |
… | |
… | |
1384 | |
1410 | |
1385 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1411 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1386 | slf_frame.check = slf_check_set_stacklevel; |
1412 | slf_frame.check = slf_check_set_stacklevel; |
1387 | } |
1413 | } |
1388 | |
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 | |
1389 | /* 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 */ |
1390 | ECB_INLINE void |
1428 | ecb_inline void |
1391 | transfer_tail (pTHX) |
1429 | transfer_tail (pTHX) |
1392 | { |
1430 | { |
1393 | free_coro_mortal (aTHX); |
1431 | free_coro_mortal (aTHX); |
1394 | } |
1432 | } |
1395 | |
1433 | |
… | |
… | |
1427 | */ |
1465 | */ |
1428 | |
1466 | |
1429 | /* |
1467 | /* |
1430 | * 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 |
1431 | * 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) |
1432 | * 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 |
1433 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1471 | * doing in that case. YMMV. |
1434 | * coroutine as Coro has no such concept. |
|
|
1435 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1436 | * that backend are too broken to do it in a standards-compliant way. |
|
|
1437 | */ |
1472 | */ |
1438 | PL_top_env = main_top_env; |
1473 | perlish_exit (); |
1439 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1440 | } |
1474 | } |
1441 | } |
1475 | } |
1442 | |
1476 | |
1443 | static coro_cctx * |
1477 | static coro_cctx * |
1444 | cctx_new () |
1478 | cctx_new (void) |
1445 | { |
1479 | { |
1446 | coro_cctx *cctx; |
1480 | coro_cctx *cctx; |
1447 | |
1481 | |
1448 | ++cctx_count; |
1482 | ++cctx_count; |
1449 | New (0, cctx, 1, coro_cctx); |
1483 | New (0, cctx, 1, coro_cctx); |
… | |
… | |
1455 | return cctx; |
1489 | return cctx; |
1456 | } |
1490 | } |
1457 | |
1491 | |
1458 | /* create a new cctx only suitable as source */ |
1492 | /* create a new cctx only suitable as source */ |
1459 | static coro_cctx * |
1493 | static coro_cctx * |
1460 | cctx_new_empty () |
1494 | cctx_new_empty (void) |
1461 | { |
1495 | { |
1462 | coro_cctx *cctx = cctx_new (); |
1496 | coro_cctx *cctx = cctx_new (); |
1463 | |
1497 | |
1464 | cctx->sptr = 0; |
1498 | cctx->sptr = 0; |
1465 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1499 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
… | |
… | |
1467 | return cctx; |
1501 | return cctx; |
1468 | } |
1502 | } |
1469 | |
1503 | |
1470 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1504 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1471 | static coro_cctx * |
1505 | static coro_cctx * |
1472 | cctx_new_run () |
1506 | cctx_new_run (void) |
1473 | { |
1507 | { |
1474 | coro_cctx *cctx = cctx_new (); |
1508 | coro_cctx *cctx = cctx_new (); |
1475 | void *stack_start; |
1509 | void *stack_start; |
1476 | size_t stack_size; |
1510 | size_t stack_size; |
1477 | |
1511 | |
1478 | #if HAVE_MMAP |
1512 | #if HAVE_MMAP |
1479 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1513 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1480 | /* mmap supposedly does allocate-on-write for us */ |
1514 | /* mmap supposedly does allocate-on-write for us */ |
1481 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1515 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0); |
1482 | |
1516 | |
1483 | if (cctx->sptr != (void *)-1) |
1517 | if (cctx->sptr != (void *)-1) |
1484 | { |
1518 | { |
1485 | #if CORO_STACKGUARD |
1519 | #if CORO_STACKGUARD |
1486 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1520 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
… | |
… | |
1788 | TRANSFER (ta, 1); |
1822 | TRANSFER (ta, 1); |
1789 | } |
1823 | } |
1790 | |
1824 | |
1791 | /** Coro ********************************************************************/ |
1825 | /** Coro ********************************************************************/ |
1792 | |
1826 | |
1793 | ECB_INLINE void |
1827 | ecb_inline void |
1794 | coro_enq (pTHX_ struct coro *coro) |
1828 | coro_enq (pTHX_ struct coro *coro) |
1795 | { |
1829 | { |
1796 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1830 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1797 | |
1831 | |
1798 | SvREFCNT_inc_NN (coro->hv); |
1832 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1800 | coro->next_ready = 0; |
1834 | coro->next_ready = 0; |
1801 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1835 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1802 | ready [1] = coro; |
1836 | ready [1] = coro; |
1803 | } |
1837 | } |
1804 | |
1838 | |
1805 | ECB_INLINE struct coro * |
1839 | ecb_inline struct coro * |
1806 | coro_deq (pTHX) |
1840 | coro_deq (pTHX) |
1807 | { |
1841 | { |
1808 | int prio; |
1842 | int prio; |
1809 | |
1843 | |
1810 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1844 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1863 | { |
1897 | { |
1864 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1898 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1865 | } |
1899 | } |
1866 | |
1900 | |
1867 | /* expects to own a reference to next->hv */ |
1901 | /* expects to own a reference to next->hv */ |
1868 | ECB_INLINE void |
1902 | ecb_inline void |
1869 | 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) |
1870 | { |
1904 | { |
1871 | SV *prev_sv = SvRV (coro_current); |
1905 | SV *prev_sv = SvRV (coro_current); |
1872 | |
1906 | |
1873 | ta->prev = SvSTATE_hv (prev_sv); |
1907 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1932 | } |
1966 | } |
1933 | } |
1967 | } |
1934 | } |
1968 | } |
1935 | } |
1969 | } |
1936 | |
1970 | |
1937 | ECB_INLINE void |
1971 | ecb_inline void |
1938 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1972 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1939 | { |
1973 | { |
1940 | api_ready (aTHX_ coro_current); |
1974 | api_ready (aTHX_ coro_current); |
1941 | prepare_schedule (aTHX_ ta); |
1975 | prepare_schedule (aTHX_ ta); |
1942 | } |
1976 | } |
1943 | |
1977 | |
1944 | ECB_INLINE void |
1978 | ecb_inline void |
1945 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1979 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1946 | { |
1980 | { |
1947 | SV *prev = SvRV (coro_current); |
1981 | SV *prev = SvRV (coro_current); |
1948 | |
1982 | |
1949 | if (coro_nready) |
1983 | if (coro_nready) |
… | |
… | |
2949 | { |
2983 | { |
2950 | int i; |
2984 | int i; |
2951 | /* if we were woken up but can't down, we look through the whole */ |
2985 | /* if we were woken up but can't down, we look through the whole */ |
2952 | /* waiters list and only add us if we aren't in there already */ |
2986 | /* waiters list and only add us if we aren't in there already */ |
2953 | /* this avoids some degenerate memory usage cases */ |
2987 | /* this avoids some degenerate memory usage cases */ |
2954 | for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug |
2988 | for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */ |
2955 | if (AvARRAY (av)[i] == coro_hv) |
2989 | if (AvARRAY (av)[i] == coro_hv) |
2956 | return 1; |
2990 | return 1; |
2957 | |
2991 | |
2958 | av_push (av, SvREFCNT_inc (coro_hv)); |
2992 | av_push (av, SvREFCNT_inc (coro_hv)); |
2959 | return 1; |
2993 | return 1; |
… | |
… | |
3339 | #endif |
3373 | #endif |
3340 | |
3374 | |
3341 | #if CORO_JIT |
3375 | #if CORO_JIT |
3342 | |
3376 | |
3343 | static void ecb_noinline ecb_cold |
3377 | static void ecb_noinline ecb_cold |
3344 | pushav_3uv (pTHX_ UV a, UV b, UV c) |
3378 | pushav_4uv (pTHX_ UV a, UV b, UV c, UV d) |
3345 | { |
3379 | { |
3346 | dSP; |
3380 | dSP; |
3347 | AV *av = newAV (); |
3381 | AV *av = newAV (); |
3348 | |
3382 | |
|
|
3383 | av_store (av, 3, newSVuv (d)); |
3349 | av_store (av, 2, newSVuv (c)); |
3384 | av_store (av, 2, newSVuv (c)); |
3350 | av_store (av, 1, newSVuv (b)); |
3385 | av_store (av, 1, newSVuv (b)); |
3351 | av_store (av, 0, newSVuv (a)); |
3386 | av_store (av, 0, newSVuv (a)); |
3352 | |
3387 | |
3353 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
3388 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
… | |
… | |
3360 | { |
3395 | { |
3361 | dSP; |
3396 | dSP; |
3362 | SV *load, *save; |
3397 | SV *load, *save; |
3363 | char *map_base; |
3398 | char *map_base; |
3364 | char *load_ptr, *save_ptr; |
3399 | char *load_ptr, *save_ptr; |
3365 | STRLEN load_len, save_len; |
3400 | STRLEN load_len, save_len, map_len; |
3366 | int count; |
3401 | int count; |
3367 | |
3402 | |
|
|
3403 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
|
|
3404 | |
3368 | PUSHMARK (SP); |
3405 | PUSHMARK (SP); |
3369 | PUTBACK; |
|
|
3370 | #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)); |
3371 | # include "state.h" |
3407 | # include "state.h" |
3372 | #undef VARx |
3408 | #undef VARx |
3373 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3409 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3374 | SPAGAIN; |
3410 | SPAGAIN; |
3375 | |
3411 | |
3376 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
3412 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
3377 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
3413 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
3378 | |
3414 | |
|
|
3415 | map_len = load_len + save_len + 16; |
|
|
3416 | |
3379 | map_base = mmap (0, load_len + save_len + 16, PROT_EXEC | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3417 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3380 | |
3418 | |
3381 | assert (("Coro: unable mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
3419 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
3382 | |
3420 | |
3383 | load_perl_slots = (load_save_perl_slots_type)map_base; |
3421 | load_perl_slots = (load_save_perl_slots_type)map_base; |
3384 | memcpy (map_base, load_ptr, load_len); |
3422 | memcpy (map_base, load_ptr, load_len); |
3385 | |
3423 | |
3386 | map_base += (load_len + 15) & ~15; |
3424 | map_base += (load_len + 15) & ~15; |
3387 | |
3425 | |
3388 | save_perl_slots = (load_save_perl_slots_type)map_base; |
3426 | save_perl_slots = (load_save_perl_slots_type)map_base; |
3389 | memcpy (map_base, save_ptr, save_len); |
3427 | memcpy (map_base, save_ptr, save_len); |
|
|
3428 | |
|
|
3429 | /* we are good citizens and try to make the page read-only, so the evil evil */ |
|
|
3430 | /* hackers might have it a bit more difficult */ |
|
|
3431 | mprotect (map_base, map_len, PROT_READ | PROT_EXEC); |
|
|
3432 | |
|
|
3433 | PUTBACK; |
|
|
3434 | eval_pv ("undef &Coro::State::_jit", 1); |
3390 | } |
3435 | } |
3391 | |
3436 | |
3392 | #endif |
3437 | #endif |
3393 | |
3438 | |
3394 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3439 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
… | |
… | |
3462 | time_init (aTHX); |
3507 | time_init (aTHX); |
3463 | |
3508 | |
3464 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3509 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3465 | #if CORO_JIT |
3510 | #if CORO_JIT |
3466 | PUTBACK; |
3511 | PUTBACK; |
3467 | require_pv ("Coro/jit-" CORO_JIT_TYPE ".pl"); |
|
|
3468 | jit_init (aTHX); |
3512 | jit_init (aTHX); |
3469 | perl_eval_pv ("undef &Coro::State::_jit", 1); |
|
|
3470 | SPAGAIN; |
3513 | SPAGAIN; |
3471 | #endif |
3514 | #endif |
3472 | } |
3515 | } |
3473 | |
3516 | |
3474 | SV * |
3517 | SV * |
… | |
… | |
4126 | } |
4169 | } |
4127 | |
4170 | |
4128 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4171 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4129 | |
4172 | |
4130 | void |
4173 | void |
4131 | _may_delete (SV *sem, int count, int extra_refs) |
4174 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4132 | PPCODE: |
4175 | PPCODE: |
4133 | { |
4176 | { |
4134 | AV *av = (AV *)SvRV (sem); |
4177 | AV *av = (AV *)SvRV (sem); |
4135 | |
4178 | |
4136 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4179 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |