… | |
… | |
27 | # ifndef IS_PADCONST |
27 | # ifndef IS_PADCONST |
28 | # define IS_PADCONST(v) 0 |
28 | # define IS_PADCONST(v) 0 |
29 | # endif |
29 | # endif |
30 | #endif |
30 | #endif |
31 | |
31 | |
|
|
32 | #include <stdio.h> |
32 | #include <errno.h> |
33 | #include <errno.h> |
33 | |
34 | |
34 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
35 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
35 | # undef STACKGUARD |
36 | # undef STACKGUARD |
36 | #endif |
37 | #endif |
… | |
… | |
57 | # else |
58 | # else |
58 | # define BOOT_PAGESIZE |
59 | # define BOOT_PAGESIZE |
59 | # endif |
60 | # endif |
60 | #endif |
61 | #endif |
61 | |
62 | |
62 | #define SUB_INIT "Coro::State::initialize" |
|
|
63 | |
|
|
64 | /* The next macro should declare a variable stacklevel that contains and approximation |
63 | /* The next macro should declare a variable stacklevel that contains and approximation |
65 | * to the current C stack pointer. Its property is that it changes with each call |
64 | * to the current C stack pointer. Its property is that it changes with each call |
66 | * and should be unique. */ |
65 | * and should be unique. */ |
|
|
66 | #define dSTACKLEVEL int stacklevel |
67 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
67 | #define STACKLEVEL ((void *)&stacklevel) |
68 | |
68 | |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
70 | |
70 | |
71 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
72 | |
|
|
73 | #include "CoroAPI.h" |
71 | #include "CoroAPI.h" |
|
|
72 | |
|
|
73 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
74 | |
74 | |
75 | #ifdef USE_ITHREADS |
75 | #ifdef USE_ITHREADS |
76 | static perl_mutex coro_mutex; |
76 | static perl_mutex coro_mutex; |
77 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
77 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
78 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
78 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
… | |
… | |
84 | static struct CoroAPI coroapi; |
84 | static struct CoroAPI coroapi; |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
86 | static HV *coro_state_stash, *coro_stash; |
86 | static HV *coro_state_stash, *coro_stash; |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
88 | |
88 | |
89 | /* this is actually not just the c stack but also c registers etc... */ |
89 | /* this is a structure representing a c-level coroutine */ |
90 | typedef struct coro_stack { |
90 | typedef struct coro_stack { |
91 | struct coro_stack *next; |
91 | struct coro_stack *next; |
92 | |
92 | |
93 | void *idle_sp; |
93 | /* the stack */ |
94 | void *sptr; |
94 | void *sptr; |
95 | long ssize; /* positive == mmap, otherwise malloc */ |
95 | long ssize; /* positive == mmap, otherwise malloc */ |
96 | |
96 | |
97 | /* cpu state */ |
97 | /* cpu state */ |
|
|
98 | void *idle_sp; /* original stacklevel when coroutine was created */ |
|
|
99 | JMPENV *top_env; |
98 | coro_context cctx; |
100 | coro_context cctx; |
99 | } coro_stack; |
101 | } coro_stack; |
100 | |
102 | |
101 | static coro_stack *main_stack; |
103 | /* this is a structure representing a perl-level coroutine */ |
102 | |
|
|
103 | struct coro { |
104 | struct coro { |
|
|
105 | /* the c coroutine allocated to this perl coroutine, if any */ |
|
|
106 | coro_stack *stack; |
|
|
107 | |
|
|
108 | /* data associated with this coroutine (initial args) */ |
|
|
109 | AV *args; |
|
|
110 | int refcnt; |
|
|
111 | |
104 | /* optionally saved, might be zero */ |
112 | /* optionally saved, might be zero */ |
105 | AV *defav; |
113 | AV *defav; |
106 | SV *defsv; |
114 | SV *defsv; |
107 | SV *errsv; |
115 | SV *errsv; |
108 | |
116 | |
109 | /* saved global state not related to stacks */ |
117 | /* saved global state not related to stacks */ |
110 | U8 dowarn; |
118 | U8 dowarn; |
111 | I32 in_eval; |
119 | I32 in_eval; |
112 | |
|
|
113 | /* the c stack, if any */ |
|
|
114 | coro_stack *stack; |
|
|
115 | |
120 | |
116 | /* the stacks and related info (callchain etc..) */ |
121 | /* the stacks and related info (callchain etc..) */ |
117 | PERL_SI *curstackinfo; |
122 | PERL_SI *curstackinfo; |
118 | AV *curstack; |
123 | AV *curstack; |
119 | AV *mainstack; |
124 | AV *mainstack; |
… | |
… | |
140 | OP **retstack; |
145 | OP **retstack; |
141 | I32 retstack_ix; |
146 | I32 retstack_ix; |
142 | I32 retstack_max; |
147 | I32 retstack_max; |
143 | PMOP *curpm; |
148 | PMOP *curpm; |
144 | COP *curcop; |
149 | COP *curcop; |
145 | JMPENV *top_env; |
|
|
146 | |
150 | |
147 | /* coro process data */ |
151 | /* coro process data */ |
148 | int prio; |
152 | int prio; |
149 | |
|
|
150 | /* data associated with this coroutine (initial args) */ |
|
|
151 | AV *args; |
|
|
152 | int refcnt; |
|
|
153 | }; |
153 | }; |
154 | |
154 | |
155 | typedef struct coro *Coro__State; |
155 | typedef struct coro *Coro__State; |
156 | typedef struct coro *Coro__State_or_hashref; |
156 | typedef struct coro *Coro__State_or_hashref; |
157 | |
157 | |
… | |
… | |
300 | PL_retstack_ix = c->retstack_ix; |
300 | PL_retstack_ix = c->retstack_ix; |
301 | PL_retstack_max = c->retstack_max; |
301 | PL_retstack_max = c->retstack_max; |
302 | #endif |
302 | #endif |
303 | PL_curpm = c->curpm; |
303 | PL_curpm = c->curpm; |
304 | PL_curcop = c->curcop; |
304 | PL_curcop = c->curcop; |
305 | PL_top_env = c->top_env; |
|
|
306 | |
305 | |
307 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
306 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
308 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
307 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
309 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
308 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
310 | |
309 | |
… | |
… | |
428 | c->retstack_ix = PL_retstack_ix; |
427 | c->retstack_ix = PL_retstack_ix; |
429 | c->retstack_max = PL_retstack_max; |
428 | c->retstack_max = PL_retstack_max; |
430 | #endif |
429 | #endif |
431 | c->curpm = PL_curpm; |
430 | c->curpm = PL_curpm; |
432 | c->curcop = PL_curcop; |
431 | c->curcop = PL_curcop; |
433 | c->top_env = PL_top_env; |
|
|
434 | } |
432 | } |
435 | |
433 | |
436 | /* |
434 | /* |
437 | * allocate various perl stacks. This is an exact copy |
435 | * allocate various perl stacks. This is an exact copy |
438 | * of perl.c:init_stacks, except that it uses less memory |
436 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
540 | * emulate part of the perl startup here. |
538 | * emulate part of the perl startup here. |
541 | */ |
539 | */ |
542 | dTHX; |
540 | dTHX; |
543 | dSP; |
541 | dSP; |
544 | UNOP myop; |
542 | UNOP myop; |
545 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
543 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
546 | |
544 | |
547 | coro_init_stacks (aTHX); |
545 | coro_init_stacks (aTHX); |
548 | /*PL_curcop = 0;*/ |
546 | /*PL_curcop = 0;*/ |
549 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
547 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
550 | SvREFCNT_dec (GvAV (PL_defgv)); |
548 | SvREFCNT_dec (GvAV (PL_defgv)); |
… | |
… | |
566 | |
564 | |
567 | ENTER; /* necessary e.g. for dounwind */ |
565 | ENTER; /* necessary e.g. for dounwind */ |
568 | } |
566 | } |
569 | |
567 | |
570 | static void |
568 | static void |
571 | transfer_tail () |
569 | free_coro_mortal () |
572 | { |
570 | { |
573 | if (coro_mortal) |
571 | if (coro_mortal) |
574 | { |
572 | { |
575 | SvREFCNT_dec (coro_mortal); |
573 | SvREFCNT_dec (coro_mortal); |
576 | coro_mortal = 0; |
574 | coro_mortal = 0; |
577 | } |
575 | } |
578 | |
|
|
579 | UNLOCK; |
|
|
580 | } |
576 | } |
581 | |
577 | |
582 | static void |
578 | static void |
583 | coro_run (void *arg) |
579 | coro_run (void *arg) |
584 | { |
580 | { |
585 | /* |
581 | /* |
586 | * this is a _very_ stripped down perl interpreter ;) |
582 | * this is a _very_ stripped down perl interpreter ;) |
587 | */ |
583 | */ |
588 | dTHX; |
584 | dTHX; |
|
|
585 | int ret; |
589 | |
586 | |
590 | transfer_tail (); |
587 | UNLOCK; |
591 | |
588 | |
592 | PL_top_env = &PL_start_env; |
589 | PL_top_env = &PL_start_env; |
593 | PL_restartop = PL_op->op_next; |
590 | |
|
|
591 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); |
|
|
592 | sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); |
|
|
593 | |
|
|
594 | /* continue at cctx_init, without entersub */ |
|
|
595 | PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
596 | |
594 | /* somebody will hit me for both perl_run and PL_restart_top */ |
597 | /* somebody will hit me for both perl_run and PL_restartop */ |
595 | perl_run (aTHX_ PERL_GET_CONTEXT); |
598 | ret = perl_run (aTHX_ PERL_GET_CONTEXT); |
|
|
599 | printf ("ret %d\n", ret);//D |
596 | |
600 | |
|
|
601 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
597 | abort (); |
602 | abort (); |
598 | } |
603 | } |
599 | |
604 | |
600 | static coro_stack * |
605 | static coro_stack * |
601 | stack_new () |
606 | stack_new () |
… | |
… | |
604 | |
609 | |
605 | New (0, stack, 1, coro_stack); |
610 | New (0, stack, 1, coro_stack); |
606 | |
611 | |
607 | #if HAVE_MMAP |
612 | #if HAVE_MMAP |
608 | |
613 | |
609 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
614 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
|
|
615 | /* mmap suppsedly does allocate-on-write for us */ |
610 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
616 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
611 | |
617 | |
612 | if (stack->sptr == (void *)-1) |
618 | if (stack->sptr == (void *)-1) |
613 | { |
619 | { |
614 | fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); |
620 | perror ("FATAL: unable to mmap stack for coroutine"); |
615 | _exit (EXIT_FAILURE); |
621 | _exit (EXIT_FAILURE); |
616 | } |
622 | } |
617 | |
623 | |
|
|
624 | # if STACKGUARD |
618 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
625 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
626 | # endif |
619 | |
627 | |
620 | #else |
628 | #else |
621 | |
629 | |
622 | stack->ssize = STACKSIZE * (long)sizeof (long); |
630 | stack->ssize = STACKSIZE * (long)sizeof (long); |
623 | New (0, stack->sptr, STACKSIZE, long); |
631 | New (0, stack->sptr, STACKSIZE, long); |
624 | |
632 | |
625 | if (!stack->sptr) |
633 | if (!stack->sptr) |
626 | { |
634 | { |
627 | fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); |
635 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
628 | _exit (EXIT_FAILURE); |
636 | _exit (EXIT_FAILURE); |
629 | } |
637 | } |
630 | |
638 | |
631 | #endif |
639 | #endif |
632 | |
640 | |
633 | coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); |
641 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
634 | |
642 | |
635 | return stack; |
643 | return stack; |
636 | } |
644 | } |
637 | |
645 | |
638 | static void |
646 | static void |
639 | stack_free (coro_stack *stack) |
647 | stack_free (coro_stack *stack) |
640 | { |
648 | { |
641 | if (!stack || stack == main_stack) |
649 | if (!stack) |
642 | return; |
650 | return; |
643 | |
651 | |
644 | #if HAVE_MMAP |
652 | #if HAVE_MMAP |
645 | munmap (stack->sptr, stack->ssize); |
653 | munmap (stack->sptr, stack->ssize); |
646 | #else |
654 | #else |
… | |
… | |
650 | Safefree (stack); |
658 | Safefree (stack); |
651 | } |
659 | } |
652 | |
660 | |
653 | static coro_stack *stack_first; |
661 | static coro_stack *stack_first; |
654 | |
662 | |
655 | static void |
663 | static coro_stack * |
656 | stack_get (struct coro *coro) |
664 | stack_get () |
657 | { |
665 | { |
|
|
666 | coro_stack *stack; |
|
|
667 | |
658 | if (stack_first) |
668 | if (stack_first) |
659 | { |
669 | { |
660 | coro->stack = stack_first; |
670 | stack = stack_first; |
661 | stack_first = stack_first->next; |
671 | stack_first = stack->next; |
662 | } |
672 | } |
663 | else |
673 | else |
|
|
674 | { |
664 | coro->stack = stack_new (); |
675 | stack = stack_new (); |
|
|
676 | PL_op = PL_op->op_next; |
|
|
677 | } |
|
|
678 | |
|
|
679 | return stack; |
665 | } |
680 | } |
666 | |
681 | |
667 | static void |
682 | static void |
668 | stack_put (coro_stack *stack) |
683 | stack_put (coro_stack *stack) |
669 | { |
684 | { |
… | |
… | |
676 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
691 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
677 | { |
692 | { |
678 | dSTACKLEVEL; |
693 | dSTACKLEVEL; |
679 | |
694 | |
680 | /* sometimes transfer is only called to set idle_sp */ |
695 | /* sometimes transfer is only called to set idle_sp */ |
681 | if (!prev->stack->idle_sp) |
696 | if (flags == TRANSFER_SET_STACKLEVEL) |
682 | prev->stack->idle_sp = stacklevel; |
697 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
683 | |
|
|
684 | LOCK; |
|
|
685 | |
|
|
686 | if (prev != next) |
698 | else if (prev != next) |
687 | { |
699 | { |
688 | coro_stack *prev_stack = prev->stack; |
700 | coro_stack *prev__stack; |
689 | |
701 | |
690 | /* possibly "free" the stack */ |
702 | LOCK; |
691 | if (prev_stack->idle_sp == stacklevel) |
703 | |
|
|
704 | if (next->mainstack) |
692 | { |
705 | { |
693 | stack_put (prev_stack); |
706 | /* coroutine already started */ |
694 | prev->stack = 0; |
707 | SAVE (prev, flags); |
|
|
708 | LOAD (next); |
695 | } |
709 | } |
696 | |
710 | else |
697 | if (!next->mainstack) |
|
|
698 | { |
711 | { |
|
|
712 | /* need to start coroutine */ |
699 | /* first get rid of the old state */ |
713 | /* first get rid of the old state */ |
700 | SAVE (prev, -1); |
714 | SAVE (prev, -1); |
701 | /* setup coroutine call */ |
715 | /* setup coroutine call */ |
702 | setup_coro (next); |
716 | setup_coro (next); |
|
|
717 | /* need a stack */ |
703 | next->stack = 0; |
718 | next->stack = 0; |
704 | } |
719 | } |
705 | else |
720 | |
|
|
721 | if (!prev->stack) |
|
|
722 | /* create a new empty context */ |
|
|
723 | Newz (0, prev->stack, 1, coro_stack); |
|
|
724 | |
|
|
725 | prev__stack = prev->stack; |
|
|
726 | |
|
|
727 | /* possibly "free" the stack */ |
|
|
728 | if (prev__stack->idle_sp == STACKLEVEL) |
706 | { |
729 | { |
707 | SAVE (prev, flags); |
730 | stack_put (prev__stack); |
708 | LOAD (next); |
731 | prev->stack = 0; |
709 | } |
732 | } |
710 | |
733 | |
711 | if (!next->stack) |
734 | if (!next->stack) |
712 | stack_get (next); |
735 | next->stack = stack_get (); |
713 | |
736 | |
714 | if (prev_stack != next->stack) |
737 | if (prev__stack != next->stack) |
|
|
738 | { |
|
|
739 | prev__stack->top_env = PL_top_env; |
|
|
740 | PL_top_env = next->stack->top_env; |
715 | coro_transfer (&prev_stack->cctx, &next->stack->cctx); |
741 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
|
|
742 | } |
|
|
743 | |
|
|
744 | free_coro_mortal (); |
|
|
745 | |
|
|
746 | UNLOCK; |
716 | } |
747 | } |
717 | |
|
|
718 | transfer_tail (); |
|
|
719 | } |
748 | } |
720 | |
749 | |
721 | /* use this function pointer to call the above function */ |
750 | /* use this function pointer to call the above function */ |
722 | /* this is done to increase chances of the compiler not inlining the call */ |
751 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
752 | /* not static to make it even harder for the compiler (and theoretically impossible in most cases */ |
723 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
753 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
754 | |
|
|
755 | struct transfer_args |
|
|
756 | { |
|
|
757 | struct coro *prev, *next; |
|
|
758 | int flags; |
|
|
759 | }; |
|
|
760 | |
|
|
761 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
724 | |
762 | |
725 | static void |
763 | static void |
726 | coro_state_destroy (struct coro *coro) |
764 | coro_state_destroy (struct coro *coro) |
727 | { |
765 | { |
728 | if (coro->refcnt--) |
766 | if (coro->refcnt--) |
… | |
… | |
791 | assert (mg->mg_type == PERL_MAGIC_ext); |
829 | assert (mg->mg_type == PERL_MAGIC_ext); |
792 | return (struct coro *)mg->mg_ptr; |
830 | return (struct coro *)mg->mg_ptr; |
793 | } |
831 | } |
794 | |
832 | |
795 | static void |
833 | static void |
|
|
834 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) |
|
|
835 | { |
|
|
836 | ta->prev = SvSTATE (prev); |
|
|
837 | ta->next = SvSTATE (next); |
|
|
838 | ta->flags = flags; |
|
|
839 | } |
|
|
840 | |
|
|
841 | static void |
796 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
842 | api_transfer (SV *prev, SV *next, int flags) |
797 | { |
843 | { |
798 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
844 | dTHX; |
|
|
845 | struct transfer_args ta; |
|
|
846 | |
|
|
847 | prepare_transfer (aTHX_ &ta, prev, next, flags); |
|
|
848 | TRANSFER (ta); |
799 | } |
849 | } |
800 | |
850 | |
801 | /** Coro ********************************************************************/ |
851 | /** Coro ********************************************************************/ |
802 | |
852 | |
803 | #define PRIO_MAX 3 |
853 | #define PRIO_MAX 3 |
… | |
… | |
857 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
907 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
858 | UNLOCK; |
908 | UNLOCK; |
859 | } |
909 | } |
860 | |
910 | |
861 | static void |
911 | static void |
862 | api_schedule (void) |
912 | prepare_schedule (aTHX_ struct transfer_args *ta) |
863 | { |
913 | { |
864 | dTHX; |
|
|
865 | |
|
|
866 | SV *prev, *next; |
914 | SV *current, *prev, *next; |
|
|
915 | |
|
|
916 | LOCK; |
|
|
917 | |
867 | SV *current = GvSV (coro_current); |
918 | current = GvSV (coro_current); |
868 | |
919 | |
869 | for (;;) |
920 | for (;;) |
870 | { |
921 | { |
871 | LOCK; |
922 | LOCK; |
872 | |
923 | |
… | |
… | |
894 | |
945 | |
895 | prev = SvRV (current); |
946 | prev = SvRV (current); |
896 | SvRV (current) = next; |
947 | SvRV (current) = next; |
897 | |
948 | |
898 | /* free this only after the transfer */ |
949 | /* free this only after the transfer */ |
|
|
950 | free_coro_mortal (); |
899 | coro_mortal = prev; |
951 | coro_mortal = prev; |
900 | |
952 | |
|
|
953 | ta->prev = SvSTATE (prev); |
|
|
954 | ta->next = SvSTATE (next); |
|
|
955 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
956 | |
901 | UNLOCK; |
957 | UNLOCK; |
902 | |
|
|
903 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); |
|
|
904 | } |
958 | } |
905 | |
959 | |
906 | static int coro_cede_self; |
960 | static void |
|
|
961 | prepare_cede (aTHX_ struct transfer_args *ta) |
|
|
962 | { |
|
|
963 | LOCK; |
|
|
964 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
965 | UNLOCK; |
|
|
966 | |
|
|
967 | prepare_schedule (ta); |
|
|
968 | } |
|
|
969 | |
|
|
970 | static void |
|
|
971 | api_schedule (void) |
|
|
972 | { |
|
|
973 | dTHX; |
|
|
974 | struct transfer_args ta; |
|
|
975 | |
|
|
976 | prepare_schedule (&ta); |
|
|
977 | TRANSFER (ta); |
|
|
978 | } |
907 | |
979 | |
908 | static void |
980 | static void |
909 | api_cede (void) |
981 | api_cede (void) |
910 | { |
982 | { |
911 | dTHX; |
983 | dTHX; |
912 | SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); |
984 | struct transfer_args ta; |
913 | |
985 | |
914 | LOCK; |
986 | prepare_cede (&ta); |
915 | |
987 | TRANSFER (ta); |
916 | if (coro_cede_self) |
|
|
917 | { |
|
|
918 | AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN]; |
|
|
919 | av_unshift (runqueue, 1); |
|
|
920 | av_store (runqueue, 0, current); |
|
|
921 | coro_nready++; |
|
|
922 | coro_cede_self = 0; |
|
|
923 | } |
|
|
924 | else |
|
|
925 | coro_enq (aTHX_ current); |
|
|
926 | |
|
|
927 | UNLOCK; |
|
|
928 | |
|
|
929 | api_schedule (); |
|
|
930 | } |
988 | } |
931 | |
989 | |
932 | MODULE = Coro::State PACKAGE = Coro::State |
990 | MODULE = Coro::State PACKAGE = Coro::State |
933 | |
991 | |
934 | PROTOTYPES: DISABLE |
992 | PROTOTYPES: DISABLE |
… | |
… | |
949 | main_mainstack = PL_mainstack; |
1007 | main_mainstack = PL_mainstack; |
950 | |
1008 | |
951 | coroapi.ver = CORO_API_VERSION; |
1009 | coroapi.ver = CORO_API_VERSION; |
952 | coroapi.transfer = api_transfer; |
1010 | coroapi.transfer = api_transfer; |
953 | |
1011 | |
954 | Newz (0, main_stack, 1, coro_stack); |
|
|
955 | main_stack->idle_sp = (void *)-1; |
|
|
956 | |
|
|
957 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1012 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
958 | } |
1013 | } |
959 | |
1014 | |
960 | SV * |
1015 | SV * |
961 | new (char *klass, ...) |
1016 | new (char *klass, ...) |
… | |
… | |
970 | |
1025 | |
971 | hv = newHV (); |
1026 | hv = newHV (); |
972 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1027 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
973 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1028 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
974 | |
1029 | |
975 | av_push (coro->args, newSVsv (RETVAL)); |
|
|
976 | for (i = 1; i < items; i++) |
1030 | for (i = 1; i < items; i++) |
977 | av_push (coro->args, newSVsv (ST (i))); |
1031 | av_push (coro->args, newSVsv (ST (i))); |
978 | |
1032 | |
979 | coro->stack = main_stack; |
|
|
980 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1033 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
981 | /*coro->stack = 0;*/ |
1034 | /*coro->stack = 0;*/ |
982 | } |
1035 | } |
983 | OUTPUT: |
1036 | OUTPUT: |
984 | RETVAL |
1037 | RETVAL |
985 | |
1038 | |
986 | void |
1039 | void |
987 | transfer (prev, next, flags) |
1040 | _set_stacklevel (...) |
988 | SV *prev |
1041 | ALIAS: |
989 | SV *next |
1042 | Coro::State::transfer = 1 |
990 | int flags |
1043 | Coro::schedule = 2 |
|
|
1044 | Coro::cede = 3 |
|
|
1045 | Coro::Cont::yield = 4 |
991 | CODE: |
1046 | CODE: |
992 | PUTBACK; |
|
|
993 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
|
|
994 | SPAGAIN; |
|
|
995 | |
|
|
996 | int |
|
|
997 | prio (Coro::State coro, int newprio = 0) |
|
|
998 | ALIAS: |
|
|
999 | nice = 1 |
|
|
1000 | CODE: |
|
|
1001 | { |
1047 | { |
1002 | RETVAL = coro->prio; |
1048 | struct transfer_args ta; |
1003 | |
1049 | |
1004 | if (items > 1) |
1050 | switch (ix) |
1005 | { |
1051 | { |
|
|
1052 | case 0: |
|
|
1053 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
|
|
1054 | ta.next = 0; |
|
|
1055 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1056 | break; |
|
|
1057 | |
|
|
1058 | case 1: |
1006 | if (ix) |
1059 | if (items != 3) |
1007 | newprio += coro->prio; |
1060 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1008 | |
1061 | |
1009 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1062 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1010 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1063 | break; |
1011 | |
1064 | |
1012 | coro->prio = newprio; |
1065 | case 2: |
|
|
1066 | prepare_schedule (&ta); |
|
|
1067 | break; |
|
|
1068 | |
|
|
1069 | case 3: |
|
|
1070 | prepare_cede (&ta); |
|
|
1071 | break; |
|
|
1072 | |
|
|
1073 | case 4: |
|
|
1074 | { |
|
|
1075 | SV *yieldstack; |
|
|
1076 | SV *sv; |
|
|
1077 | AV *defav = GvAV (PL_defgv); |
|
|
1078 | |
|
|
1079 | yieldstack = *hv_fetch ( |
|
|
1080 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1081 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1082 | 0 |
|
|
1083 | ); |
|
|
1084 | |
|
|
1085 | /* set up @_ -- ugly */ |
|
|
1086 | av_clear (defav); |
|
|
1087 | av_fill (defav, items - 1); |
|
|
1088 | while (items--) |
|
|
1089 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1090 | |
|
|
1091 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1092 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1093 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1094 | ta.flags = 0; |
|
|
1095 | SvREFCNT_dec (sv); |
|
|
1096 | } |
|
|
1097 | break; |
|
|
1098 | |
1013 | } |
1099 | } |
1014 | } |
|
|
1015 | |
1100 | |
1016 | void |
1101 | TRANSFER (ta); |
1017 | _clear_idle_sp (Coro::State self) |
1102 | } |
1018 | CODE: |
|
|
1019 | self->stack->idle_sp = 0; |
|
|
1020 | |
1103 | |
1021 | void |
1104 | void |
1022 | _clone_state_from (SV *dst, SV *src) |
1105 | _clone_state_from (SV *dst, SV *src) |
1023 | CODE: |
1106 | CODE: |
1024 | { |
1107 | { |
… | |
… | |
1027 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1110 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1028 | |
1111 | |
1029 | ++coro_src->refcnt; |
1112 | ++coro_src->refcnt; |
1030 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1113 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1031 | } |
1114 | } |
|
|
1115 | |
|
|
1116 | void |
|
|
1117 | _nonlocal_goto (IV nextop) |
|
|
1118 | CODE: |
|
|
1119 | /* uuh, somebody will kill me again for this */ |
|
|
1120 | PL_op->op_next = INT2PTR (OP *, nextop); |
1032 | |
1121 | |
1033 | void |
1122 | void |
1034 | _exit (code) |
1123 | _exit (code) |
1035 | int code |
1124 | int code |
1036 | PROTOTYPE: $ |
1125 | PROTOTYPE: $ |
1037 | CODE: |
1126 | CODE: |
1038 | _exit (code); |
1127 | _exit (code); |
1039 | |
1128 | |
1040 | MODULE = Coro::State PACKAGE = Coro::Cont |
|
|
1041 | |
|
|
1042 | void |
|
|
1043 | yield (...) |
|
|
1044 | PROTOTYPE: @ |
|
|
1045 | CODE: |
|
|
1046 | { |
|
|
1047 | SV *yieldstack; |
|
|
1048 | SV *sv; |
|
|
1049 | AV *defav = GvAV (PL_defgv); |
|
|
1050 | struct coro *prev, *next; |
|
|
1051 | |
|
|
1052 | yieldstack = *hv_fetch ( |
|
|
1053 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1054 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1055 | 0 |
|
|
1056 | ); |
|
|
1057 | |
|
|
1058 | /* set up @_ -- ugly */ |
|
|
1059 | av_clear (defav); |
|
|
1060 | av_fill (defav, items - 1); |
|
|
1061 | while (items--) |
|
|
1062 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1063 | |
|
|
1064 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1065 | prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1066 | next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1067 | SvREFCNT_dec (sv); |
|
|
1068 | |
|
|
1069 | coro_state_transfer (aTHX_ prev, next, 0); |
|
|
1070 | } |
|
|
1071 | |
|
|
1072 | MODULE = Coro::State PACKAGE = Coro |
1129 | MODULE = Coro::State PACKAGE = Coro |
1073 | |
1130 | |
1074 | BOOT: |
1131 | BOOT: |
1075 | { |
1132 | { |
1076 | int i; |
1133 | int i; |
… | |
… | |
1101 | |
1158 | |
1102 | GCoroAPI = &coroapi; |
1159 | GCoroAPI = &coroapi; |
1103 | sv_setiv (sv, (IV)&coroapi); |
1160 | sv_setiv (sv, (IV)&coroapi); |
1104 | SvREADONLY_on (sv); |
1161 | SvREADONLY_on (sv); |
1105 | } |
1162 | } |
|
|
1163 | } |
|
|
1164 | |
|
|
1165 | int |
|
|
1166 | prio (Coro::State coro, int newprio = 0) |
|
|
1167 | ALIAS: |
|
|
1168 | nice = 1 |
|
|
1169 | CODE: |
|
|
1170 | { |
|
|
1171 | RETVAL = coro->prio; |
|
|
1172 | |
|
|
1173 | if (items > 1) |
|
|
1174 | { |
|
|
1175 | if (ix) |
|
|
1176 | newprio += coro->prio; |
|
|
1177 | |
|
|
1178 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
1179 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
1180 | |
|
|
1181 | coro->prio = newprio; |
|
|
1182 | } |
1106 | } |
1183 | } |
1107 | |
1184 | |
1108 | void |
1185 | void |
1109 | ready (SV *self) |
1186 | ready (SV *self) |
1110 | PROTOTYPE: $ |
1187 | PROTOTYPE: $ |
… | |
… | |
1117 | CODE: |
1194 | CODE: |
1118 | RETVAL = coro_nready; |
1195 | RETVAL = coro_nready; |
1119 | OUTPUT: |
1196 | OUTPUT: |
1120 | RETVAL |
1197 | RETVAL |
1121 | |
1198 | |
1122 | void |
|
|
1123 | schedule (...) |
|
|
1124 | PROTOTYPE: |
|
|
1125 | CODE: |
|
|
1126 | api_schedule (); |
|
|
1127 | |
|
|
1128 | void |
|
|
1129 | _set_cede_self () |
|
|
1130 | CODE: |
|
|
1131 | coro_cede_self = 1; |
|
|
1132 | |
|
|
1133 | void |
|
|
1134 | cede (...) |
|
|
1135 | PROTOTYPE: |
|
|
1136 | CODE: |
|
|
1137 | api_cede (); |
|
|
1138 | |
|
|
1139 | MODULE = Coro::State PACKAGE = Coro::AIO |
1199 | MODULE = Coro::State PACKAGE = Coro::AIO |
1140 | |
1200 | |
1141 | SV * |
1201 | SV * |
1142 | _get_state () |
1202 | _get_state () |
1143 | CODE: |
1203 | CODE: |