… | |
… | |
3 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
4 | #include "perl.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
5 | #include "XSUB.h" |
6 | |
6 | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
|
|
8 | |
|
|
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
8 | |
12 | |
9 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
10 | (PERL_REVISION > (a) \ |
14 | (PERL_REVISION > (a) \ |
11 | || (PERL_REVISION == (a) \ |
15 | || (PERL_REVISION == (a) \ |
12 | && (PERL_VERSION > (b) \ |
16 | && (PERL_VERSION > (b) \ |
… | |
… | |
58 | # ifndef PAGESIZE |
62 | # ifndef PAGESIZE |
59 | # define PAGESIZE pagesize |
63 | # define PAGESIZE pagesize |
60 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
64 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
61 | static long pagesize; |
65 | static long pagesize; |
62 | # else |
66 | # else |
63 | # define BOOT_PAGESIZE |
67 | # define BOOT_PAGESIZE (void)0 |
64 | # endif |
68 | # endif |
|
|
69 | #else |
|
|
70 | # define PAGESIZE 0 |
|
|
71 | # define BOOT_PAGESIZE (void)0 |
65 | #endif |
72 | #endif |
66 | |
73 | |
67 | /* The next macro should declare a variable stacklevel that contains and approximation |
74 | /* The next macro should declare a variable stacklevel that contains and approximation |
68 | * to the current C stack pointer. Its property is that it changes with each call |
75 | * to the current C stack pointer. Its property is that it changes with each call |
69 | * and should be unique. */ |
76 | * and should be unique. */ |
… | |
… | |
96 | static struct CoroAPI coroapi; |
103 | static struct CoroAPI coroapi; |
97 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
98 | static HV *coro_state_stash, *coro_stash; |
105 | static HV *coro_state_stash, *coro_stash; |
99 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
100 | |
107 | |
|
|
108 | static struct coro_cctx *cctx_first; |
|
|
109 | static int cctx_count, cctx_idle; |
|
|
110 | |
101 | /* this is a structure representing a c-level coroutine */ |
111 | /* this is a structure representing a c-level coroutine */ |
102 | typedef struct coro_stack { |
112 | typedef struct coro_cctx { |
103 | struct coro_stack *next; |
113 | struct coro_cctx *next; |
104 | |
114 | |
105 | /* the stack */ |
115 | /* the stack */ |
106 | void *sptr; |
116 | void *sptr; |
107 | long ssize; /* positive == mmap, otherwise malloc */ |
117 | long ssize; /* positive == mmap, otherwise malloc */ |
108 | |
118 | |
109 | /* cpu state */ |
119 | /* cpu state */ |
110 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
111 | JMPENV *top_env; |
121 | JMPENV *top_env; |
112 | coro_context cctx; |
122 | coro_context cctx; |
113 | } coro_stack; |
123 | |
|
|
124 | #if USE_VALGRIND |
|
|
125 | int valgrind_id; |
|
|
126 | #endif |
|
|
127 | } coro_cctx; |
114 | |
128 | |
115 | /* this is a structure representing a perl-level coroutine */ |
129 | /* this is a structure representing a perl-level coroutine */ |
116 | struct coro { |
130 | struct coro { |
117 | /* the c coroutine allocated to this perl coroutine, if any */ |
131 | /* the c coroutine allocated to this perl coroutine, if any */ |
118 | coro_stack *stack; |
132 | coro_cctx *cctx; |
119 | |
133 | |
120 | /* data associated with this coroutine (initial args) */ |
134 | /* data associated with this coroutine (initial args) */ |
121 | AV *args; |
135 | AV *args; |
122 | int refcnt; |
136 | int refcnt; |
123 | |
137 | |
… | |
… | |
491 | |
505 | |
492 | /* |
506 | /* |
493 | * destroy the stacks, the callchain etc... |
507 | * destroy the stacks, the callchain etc... |
494 | */ |
508 | */ |
495 | static void |
509 | static void |
496 | coro_destroy_stacks() |
510 | coro_destroy_stacks () |
497 | { |
511 | { |
498 | if (!IN_DESTRUCT) |
512 | if (!IN_DESTRUCT) |
499 | { |
513 | { |
500 | /* is this ugly, I ask? */ |
514 | /* is this ugly, I ask? */ |
501 | LEAVE_SCOPE (0); |
515 | LEAVE_SCOPE (0); |
… | |
… | |
546 | * emulate part of the perl startup here. |
560 | * emulate part of the perl startup here. |
547 | */ |
561 | */ |
548 | |
562 | |
549 | coro_init_stacks (); |
563 | coro_init_stacks (); |
550 | |
564 | |
|
|
565 | PL_curcop = 0; |
|
|
566 | PL_in_eval = 0; |
|
|
567 | PL_curpm = 0; |
|
|
568 | |
551 | { |
569 | { |
552 | dSP; |
570 | dSP; |
553 | LOGOP myop; |
571 | LOGOP myop; |
554 | |
572 | |
555 | /*PL_curcop = 0;*/ |
573 | /* I have no idea why this is needed, but it is */ |
556 | PL_in_eval = 0; |
574 | PUSHMARK (SP); |
|
|
575 | |
557 | SvREFCNT_dec (GvAV (PL_defgv)); |
576 | SvREFCNT_dec (GvAV (PL_defgv)); |
558 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
577 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
559 | |
|
|
560 | SPAGAIN; |
|
|
561 | |
578 | |
562 | Zero (&myop, 1, LOGOP); |
579 | Zero (&myop, 1, LOGOP); |
563 | myop.op_next = Nullop; |
580 | myop.op_next = Nullop; |
564 | myop.op_flags = OPf_WANT_VOID; |
581 | myop.op_flags = OPf_WANT_VOID; |
565 | |
582 | |
566 | PL_op = (OP *)&myop; |
583 | PL_op = (OP *)&myop; |
567 | |
584 | |
568 | PUSHMARK (SP); |
585 | PUSHMARK (SP); |
569 | PUSHMARK (SP); |
|
|
570 | XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); |
586 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
571 | PUTBACK; |
587 | PUTBACK; |
572 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
588 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
573 | SPAGAIN; |
589 | SPAGAIN; |
574 | |
590 | |
575 | ENTER; /* necessary e.g. for dounwind */ |
591 | ENTER; /* necessary e.g. for dounwind */ |
… | |
… | |
585 | coro_mortal = 0; |
601 | coro_mortal = 0; |
586 | } |
602 | } |
587 | } |
603 | } |
588 | |
604 | |
589 | static void NOINLINE |
605 | static void NOINLINE |
590 | prepare_cctx (coro_stack *cctx) |
606 | prepare_cctx (coro_cctx *cctx) |
591 | { |
607 | { |
592 | dSP; |
608 | dSP; |
593 | LOGOP myop; |
609 | LOGOP myop; |
594 | |
610 | |
595 | Zero (&myop, 1, LOGOP); |
611 | Zero (&myop, 1, LOGOP); |
596 | myop.op_next = PL_op; |
612 | myop.op_next = PL_op; |
597 | myop.op_flags = OPf_WANT_VOID; |
613 | myop.op_flags = OPf_WANT_VOID; |
598 | |
614 | |
599 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); |
615 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
600 | |
616 | |
601 | PUSHMARK (SP); |
617 | PUSHMARK (SP); |
602 | XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
618 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
603 | PUTBACK; |
619 | PUTBACK; |
604 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
620 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
605 | SPAGAIN; |
621 | SPAGAIN; |
606 | } |
622 | } |
607 | |
623 | |
… | |
… | |
613 | |
629 | |
614 | /* |
630 | /* |
615 | * this is a _very_ stripped down perl interpreter ;) |
631 | * this is a _very_ stripped down perl interpreter ;) |
616 | */ |
632 | */ |
617 | PL_top_env = &PL_start_env; |
633 | PL_top_env = &PL_start_env; |
|
|
634 | |
618 | /* inject call to cctx_init */ |
635 | /* inject call to cctx_init */ |
619 | prepare_cctx ((coro_stack *)arg); |
636 | prepare_cctx ((coro_cctx *)arg); |
620 | |
637 | |
621 | /* somebody will hit me for both perl_run and PL_restartop */ |
638 | /* somebody will hit me for both perl_run and PL_restartop */ |
622 | perl_run (PL_curinterp); |
639 | perl_run (PL_curinterp); |
623 | |
640 | |
624 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
641 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
625 | abort (); |
642 | abort (); |
626 | } |
643 | } |
627 | |
644 | |
628 | static coro_stack * |
645 | static coro_cctx * |
629 | stack_new () |
646 | cctx_new () |
630 | { |
647 | { |
631 | coro_stack *stack; |
648 | coro_cctx *cctx; |
632 | |
649 | |
|
|
650 | ++cctx_count; |
|
|
651 | |
633 | New (0, stack, 1, coro_stack); |
652 | New (0, cctx, 1, coro_cctx); |
634 | |
653 | |
635 | #if HAVE_MMAP |
654 | #if HAVE_MMAP |
636 | |
655 | |
637 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
656 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
638 | /* mmap suppsedly does allocate-on-write for us */ |
657 | /* mmap suppsedly does allocate-on-write for us */ |
639 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
658 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
640 | |
659 | |
641 | if (stack->sptr == (void *)-1) |
660 | if (cctx->sptr == (void *)-1) |
642 | { |
661 | { |
643 | perror ("FATAL: unable to mmap stack for coroutine"); |
662 | perror ("FATAL: unable to mmap stack for coroutine"); |
644 | _exit (EXIT_FAILURE); |
663 | _exit (EXIT_FAILURE); |
645 | } |
664 | } |
646 | |
665 | |
647 | # if STACKGUARD |
666 | # if STACKGUARD |
648 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
667 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
649 | # endif |
668 | # endif |
650 | |
669 | |
651 | #else |
670 | #else |
652 | |
671 | |
653 | stack->ssize = STACKSIZE * (long)sizeof (long); |
672 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
654 | New (0, stack->sptr, STACKSIZE, long); |
673 | New (0, cctx->sptr, STACKSIZE, long); |
655 | |
674 | |
656 | if (!stack->sptr) |
675 | if (!cctx->sptr) |
657 | { |
676 | { |
658 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
677 | perror ("FATAL: unable to malloc stack for coroutine"); |
659 | _exit (EXIT_FAILURE); |
678 | _exit (EXIT_FAILURE); |
660 | } |
679 | } |
661 | |
680 | |
662 | #endif |
681 | #endif |
663 | |
682 | |
|
|
683 | #if USE_VALGRIND |
|
|
684 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
685 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
686 | cctx->ssize + (char *)cctx->sptr |
|
|
687 | ); |
|
|
688 | #endif |
|
|
689 | |
664 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
690 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
665 | |
691 | |
666 | return stack; |
692 | return cctx; |
667 | } |
693 | } |
668 | |
694 | |
669 | static void |
695 | static void |
670 | stack_free (coro_stack *stack) |
696 | cctx_free (coro_cctx *cctx) |
671 | { |
697 | { |
672 | if (!stack) |
698 | if (!cctx) |
673 | return; |
699 | return; |
674 | |
700 | |
|
|
701 | --cctx_count; |
|
|
702 | |
|
|
703 | #if USE_VALGRIND |
|
|
704 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
705 | #endif |
|
|
706 | |
675 | #if HAVE_MMAP |
707 | #if HAVE_MMAP |
676 | munmap (stack->sptr, stack->ssize); |
708 | munmap (cctx->sptr, cctx->ssize); |
677 | #else |
709 | #else |
678 | Safefree (stack->sptr); |
710 | Safefree (cctx->sptr); |
679 | #endif |
711 | #endif |
680 | |
712 | |
681 | Safefree (stack); |
713 | Safefree (cctx); |
682 | } |
714 | } |
683 | |
715 | |
684 | static coro_stack *stack_first; |
|
|
685 | static int cctx_count, cctx_idle; |
|
|
686 | |
|
|
687 | static coro_stack * |
716 | static coro_cctx * |
688 | stack_get () |
717 | cctx_get () |
689 | { |
718 | { |
690 | coro_stack *stack; |
719 | coro_cctx *cctx; |
691 | |
720 | |
692 | if (stack_first) |
721 | if (cctx_first) |
693 | { |
722 | { |
694 | --cctx_idle; |
723 | --cctx_idle; |
695 | stack = stack_first; |
724 | cctx = cctx_first; |
696 | stack_first = stack->next; |
725 | cctx_first = cctx->next; |
697 | } |
726 | } |
698 | else |
727 | else |
699 | { |
728 | { |
700 | ++cctx_count; |
729 | cctx = cctx_new (); |
701 | stack = stack_new (); |
|
|
702 | PL_op = PL_op->op_next; |
730 | PL_op = PL_op->op_next; |
703 | } |
731 | } |
704 | |
732 | |
705 | return stack; |
733 | return cctx; |
706 | } |
734 | } |
707 | |
735 | |
708 | static void |
736 | static void |
709 | stack_put (coro_stack *stack) |
737 | cctx_put (coro_cctx *cctx) |
710 | { |
738 | { |
711 | ++cctx_idle; |
739 | ++cctx_idle; |
712 | stack->next = stack_first; |
740 | cctx->next = cctx_first; |
713 | stack_first = stack; |
741 | cctx_first = cctx; |
714 | } |
742 | } |
715 | |
743 | |
716 | /* never call directly, always through the coro_state_transfer global variable */ |
744 | /* never call directly, always through the coro_state_transfer global variable */ |
717 | static void NOINLINE |
745 | static void NOINLINE |
718 | transfer (struct coro *prev, struct coro *next, int flags) |
746 | transfer (struct coro *prev, struct coro *next, int flags) |
719 | { |
747 | { |
720 | dSTACKLEVEL; |
748 | dSTACKLEVEL; |
721 | |
749 | |
722 | /* sometimes transfer is only called to set idle_sp */ |
750 | /* sometimes transfer is only called to set idle_sp */ |
723 | if (flags == TRANSFER_SET_STACKLEVEL) |
751 | if (flags == TRANSFER_SET_STACKLEVEL) |
724 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
752 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
725 | else if (prev != next) |
753 | else if (prev != next) |
726 | { |
754 | { |
727 | coro_stack *prev__stack; |
755 | coro_cctx *prev__cctx; |
728 | |
756 | |
729 | LOCK; |
757 | LOCK; |
730 | |
758 | |
731 | if (next->mainstack) |
759 | if (next->mainstack) |
732 | { |
760 | { |
… | |
… | |
740 | /* first get rid of the old state */ |
768 | /* first get rid of the old state */ |
741 | SAVE (prev, -1); |
769 | SAVE (prev, -1); |
742 | /* setup coroutine call */ |
770 | /* setup coroutine call */ |
743 | setup_coro (next); |
771 | setup_coro (next); |
744 | /* need a stack */ |
772 | /* need a stack */ |
745 | next->stack = 0; |
773 | next->cctx = 0; |
746 | } |
774 | } |
747 | |
775 | |
748 | if (!prev->stack) |
776 | if (!prev->cctx) |
749 | /* create a new empty context */ |
777 | /* create a new empty context */ |
750 | Newz (0, prev->stack, 1, coro_stack); |
778 | Newz (0, prev->cctx, 1, coro_cctx); |
751 | |
779 | |
752 | prev__stack = prev->stack; |
780 | prev__cctx = prev->cctx; |
753 | |
781 | |
754 | /* possibly "free" the stack */ |
782 | /* possibly "free" the cctx */ |
755 | if (prev__stack->idle_sp == STACKLEVEL) |
783 | if (prev__cctx->idle_sp == STACKLEVEL) |
756 | { |
784 | { |
757 | stack_put (prev__stack); |
785 | cctx_put (prev__cctx); |
758 | prev->stack = 0; |
786 | prev->cctx = 0; |
759 | } |
787 | } |
760 | |
788 | |
761 | if (!next->stack) |
789 | if (!next->cctx) |
762 | next->stack = stack_get (); |
790 | next->cctx = cctx_get (); |
763 | |
791 | |
764 | if (prev__stack != next->stack) |
792 | if (prev__cctx != next->cctx) |
765 | { |
793 | { |
766 | prev__stack->top_env = PL_top_env; |
794 | prev__cctx->top_env = PL_top_env; |
767 | PL_top_env = next->stack->top_env; |
795 | PL_top_env = next->cctx->top_env; |
768 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
796 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
769 | } |
797 | } |
770 | |
798 | |
771 | free_coro_mortal (); |
799 | free_coro_mortal (); |
772 | |
800 | |
773 | UNLOCK; |
801 | UNLOCK; |
… | |
… | |
800 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
828 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
801 | |
829 | |
802 | coro->mainstack = 0; |
830 | coro->mainstack = 0; |
803 | } |
831 | } |
804 | |
832 | |
805 | stack_free (coro->stack); |
833 | cctx_free (coro->cctx); |
806 | SvREFCNT_dec (coro->args); |
834 | SvREFCNT_dec (coro->args); |
807 | Safefree (coro); |
835 | Safefree (coro); |
808 | } |
836 | } |
809 | |
837 | |
810 | static int |
838 | static int |
… | |
… | |
1054 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1082 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1055 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1083 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1056 | |
1084 | |
1057 | for (i = 1; i < items; i++) |
1085 | for (i = 1; i < items; i++) |
1058 | av_push (coro->args, newSVsv (ST (i))); |
1086 | av_push (coro->args, newSVsv (ST (i))); |
1059 | |
|
|
1060 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1061 | /*coro->stack = 0;*/ |
|
|
1062 | } |
1087 | } |
1063 | OUTPUT: |
1088 | OUTPUT: |
1064 | RETVAL |
1089 | RETVAL |
1065 | |
1090 | |
1066 | void |
1091 | void |
… | |
… | |
1075 | struct transfer_args ta; |
1100 | struct transfer_args ta; |
1076 | |
1101 | |
1077 | switch (ix) |
1102 | switch (ix) |
1078 | { |
1103 | { |
1079 | case 0: |
1104 | case 0: |
1080 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1105 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1081 | ta.next = 0; |
1106 | ta.next = 0; |
1082 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1107 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1083 | break; |
1108 | break; |
1084 | |
1109 | |
1085 | case 1: |
1110 | case 1: |
… | |
… | |
1145 | int code |
1170 | int code |
1146 | PROTOTYPE: $ |
1171 | PROTOTYPE: $ |
1147 | CODE: |
1172 | CODE: |
1148 | _exit (code); |
1173 | _exit (code); |
1149 | |
1174 | |
|
|
1175 | int |
|
|
1176 | cctx_count () |
|
|
1177 | CODE: |
|
|
1178 | RETVAL = cctx_count; |
|
|
1179 | OUTPUT: |
|
|
1180 | RETVAL |
|
|
1181 | |
|
|
1182 | int |
|
|
1183 | cctx_idle () |
|
|
1184 | CODE: |
|
|
1185 | RETVAL = cctx_idle; |
|
|
1186 | OUTPUT: |
|
|
1187 | RETVAL |
|
|
1188 | |
1150 | MODULE = Coro::State PACKAGE = Coro |
1189 | MODULE = Coro::State PACKAGE = Coro |
1151 | |
1190 | |
1152 | BOOT: |
1191 | BOOT: |
1153 | { |
1192 | { |
1154 | int i; |
1193 | int i; |