… | |
… | |
33 | # define BOOT_PAGESIZE (void)0 |
33 | # define BOOT_PAGESIZE (void)0 |
34 | #endif |
34 | #endif |
35 | |
35 | |
36 | #if USE_VALGRIND |
36 | #if USE_VALGRIND |
37 | # include <valgrind/valgrind.h> |
37 | # include <valgrind/valgrind.h> |
|
|
38 | # define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) |
|
|
39 | #else |
|
|
40 | # define REGISTER_STACK(cctx,start,end) |
38 | #endif |
41 | #endif |
39 | |
42 | |
40 | /* the maximum number of idle cctx that will be pooled */ |
43 | /* the maximum number of idle cctx that will be pooled */ |
41 | #define MAX_IDLE_CCTX 8 |
44 | #define MAX_IDLE_CCTX 8 |
42 | |
45 | |
… | |
… | |
93 | |
96 | |
94 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
97 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
95 | |
98 | |
96 | #if __GNUC__ >= 3 |
99 | #if __GNUC__ >= 3 |
97 | # define attribute(x) __attribute__(x) |
100 | # define attribute(x) __attribute__(x) |
|
|
101 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
98 | #else |
102 | #else |
99 | # define attribute(x) |
103 | # define attribute(x) |
|
|
104 | # define BARRIER |
100 | #endif |
105 | #endif |
101 | |
106 | |
102 | #define NOINLINE attribute ((noinline)) |
107 | #define NOINLINE attribute ((noinline)) |
103 | |
108 | |
104 | #include "CoroAPI.h" |
109 | #include "CoroAPI.h" |
… | |
… | |
110 | #else |
115 | #else |
111 | # define LOCK (void)0 |
116 | # define LOCK (void)0 |
112 | # define UNLOCK (void)0 |
117 | # define UNLOCK (void)0 |
113 | #endif |
118 | #endif |
114 | |
119 | |
|
|
120 | /* helper storage struct for Coro::AIO */ |
115 | struct io_state |
121 | struct io_state |
116 | { |
122 | { |
117 | int errorno; |
123 | int errorno; |
118 | I32 laststype; |
124 | I32 laststype; |
119 | int laststatval; |
125 | int laststatval; |
… | |
… | |
132 | typedef struct coro_cctx { |
138 | typedef struct coro_cctx { |
133 | struct coro_cctx *next; |
139 | struct coro_cctx *next; |
134 | |
140 | |
135 | /* the stack */ |
141 | /* the stack */ |
136 | void *sptr; |
142 | void *sptr; |
137 | long ssize; /* positive == mmap, otherwise malloc */ |
143 | ssize_t ssize; /* positive == mmap, otherwise malloc */ |
138 | |
144 | |
139 | /* cpu state */ |
145 | /* cpu state */ |
140 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
146 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
147 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
141 | JMPENV *top_env; |
148 | JMPENV *top_env; |
142 | coro_context cctx; |
149 | coro_context cctx; |
143 | |
150 | |
144 | int inuse; |
151 | int inuse; |
145 | |
152 | |
… | |
… | |
147 | int valgrind_id; |
154 | int valgrind_id; |
148 | #endif |
155 | #endif |
149 | } coro_cctx; |
156 | } coro_cctx; |
150 | |
157 | |
151 | enum { |
158 | enum { |
152 | CF_RUNNING = 0x0001, /* coroutine is running */ |
159 | CF_RUNNING = 0x0001, /* coroutine is running */ |
153 | CF_READY = 0x0002, /* coroutine is ready */ |
160 | CF_READY = 0x0002, /* coroutine is ready */ |
154 | CF_NEW = 0x0004, /* ahs never been switched to */ |
161 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
162 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
155 | }; |
163 | }; |
156 | |
164 | |
157 | /* this is a structure representing a perl-level coroutine */ |
165 | /* this is a structure representing a perl-level coroutine */ |
158 | struct coro { |
166 | struct coro { |
159 | /* the c coroutine allocated to this perl coroutine, if any */ |
167 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
180 | int prio; |
188 | int prio; |
181 | }; |
189 | }; |
182 | |
190 | |
183 | typedef struct coro *Coro__State; |
191 | typedef struct coro *Coro__State; |
184 | typedef struct coro *Coro__State_or_hashref; |
192 | typedef struct coro *Coro__State_or_hashref; |
|
|
193 | |
|
|
194 | /** Coro ********************************************************************/ |
|
|
195 | |
|
|
196 | #define PRIO_MAX 3 |
|
|
197 | #define PRIO_HIGH 1 |
|
|
198 | #define PRIO_NORMAL 0 |
|
|
199 | #define PRIO_LOW -1 |
|
|
200 | #define PRIO_IDLE -3 |
|
|
201 | #define PRIO_MIN -4 |
|
|
202 | |
|
|
203 | /* for Coro.pm */ |
|
|
204 | static SV *coro_current; |
|
|
205 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
206 | static int coro_nready; |
|
|
207 | |
|
|
208 | /** lowlevel stuff **********************************************************/ |
185 | |
209 | |
186 | static AV * |
210 | static AV * |
187 | coro_clone_padlist (CV *cv) |
211 | coro_clone_padlist (CV *cv) |
188 | { |
212 | { |
189 | AV *padlist = CvPADLIST (cv); |
213 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
298 | av_extend (av, AvMAX (av) + 1); |
322 | av_extend (av, AvMAX (av) + 1); |
299 | |
323 | |
300 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
324 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
301 | } |
325 | } |
302 | |
326 | |
|
|
327 | /** load & save, init *******************************************************/ |
|
|
328 | |
303 | #define SB do { |
329 | #define SB do { |
304 | #define SE } while (0) |
330 | #define SE } while (0) |
305 | |
331 | |
306 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
332 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
307 | |
333 | |
… | |
… | |
468 | |
494 | |
469 | /* free all temporaries */ |
495 | /* free all temporaries */ |
470 | FREETMPS; |
496 | FREETMPS; |
471 | assert (PL_tmps_ix == -1); |
497 | assert (PL_tmps_ix == -1); |
472 | |
498 | |
|
|
499 | /* unwind all extra stacks */ |
473 | POPSTACK_TO (PL_mainstack); |
500 | POPSTACK_TO (PL_mainstack); |
|
|
501 | |
|
|
502 | /* unwind main stack */ |
|
|
503 | dounwind (-1); |
474 | } |
504 | } |
475 | |
505 | |
476 | while (PL_curstackinfo->si_next) |
506 | while (PL_curstackinfo->si_next) |
477 | PL_curstackinfo = PL_curstackinfo->si_next; |
507 | PL_curstackinfo = PL_curstackinfo->si_next; |
478 | |
508 | |
… | |
… | |
494 | Safefree (PL_savestack); |
524 | Safefree (PL_savestack); |
495 | #if !PERL_VERSION_ATLEAST (5,9,0) |
525 | #if !PERL_VERSION_ATLEAST (5,9,0) |
496 | Safefree (PL_retstack); |
526 | Safefree (PL_retstack); |
497 | #endif |
527 | #endif |
498 | } |
528 | } |
|
|
529 | |
|
|
530 | /** coroutine stack handling ************************************************/ |
499 | |
531 | |
500 | static void |
532 | static void |
501 | setup_coro (struct coro *coro) |
533 | setup_coro (struct coro *coro) |
502 | { |
534 | { |
503 | /* |
535 | /* |
… | |
… | |
601 | |
633 | |
602 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
634 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
603 | /* mmap supposedly does allocate-on-write for us */ |
635 | /* mmap supposedly does allocate-on-write for us */ |
604 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
636 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
605 | |
637 | |
606 | if (cctx->sptr == (void *)-1) |
638 | if (cctx->sptr != (void *)-1) |
607 | { |
|
|
608 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
609 | _exit (EXIT_FAILURE); |
|
|
610 | } |
639 | { |
611 | |
|
|
612 | # if STACKGUARD |
640 | # if STACKGUARD |
613 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
641 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
614 | # endif |
642 | # endif |
615 | |
643 | REGISTER_STACK ( |
616 | #else |
644 | cctx, |
617 | |
|
|
618 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
619 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
620 | |
|
|
621 | if (!cctx->sptr) |
|
|
622 | { |
|
|
623 | perror ("FATAL: unable to malloc stack for coroutine"); |
|
|
624 | _exit (EXIT_FAILURE); |
|
|
625 | } |
|
|
626 | |
|
|
627 | #endif |
|
|
628 | |
|
|
629 | #if USE_VALGRIND |
|
|
630 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
631 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
645 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
632 | cctx->ssize + (char *)cctx->sptr |
646 | cctx->ssize + (char *)cctx->sptr |
633 | ); |
647 | ); |
634 | #endif |
|
|
635 | |
648 | |
636 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
649 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
|
|
650 | } |
|
|
651 | else |
|
|
652 | #endif |
|
|
653 | { |
|
|
654 | cctx->ssize = -STACKSIZE * (long)sizeof (long); |
|
|
655 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
656 | |
|
|
657 | if (!cctx->sptr) |
|
|
658 | { |
|
|
659 | perror ("FATAL: unable to allocate stack for coroutine"); |
|
|
660 | _exit (EXIT_FAILURE); |
|
|
661 | } |
|
|
662 | |
|
|
663 | REGISTER_STACK ( |
|
|
664 | cctx, |
|
|
665 | (char *)cctx->sptr, |
|
|
666 | (char *)cctx->sptr - cctx->ssize |
|
|
667 | ); |
|
|
668 | |
|
|
669 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize); |
|
|
670 | } |
637 | |
671 | |
638 | return cctx; |
672 | return cctx; |
639 | } |
673 | } |
640 | |
674 | |
641 | static void |
675 | static void |
… | |
… | |
649 | #if USE_VALGRIND |
683 | #if USE_VALGRIND |
650 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
684 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
651 | #endif |
685 | #endif |
652 | |
686 | |
653 | #if HAVE_MMAP |
687 | #if HAVE_MMAP |
|
|
688 | if (cctx->ssize > 0) |
654 | munmap (cctx->sptr, cctx->ssize); |
689 | munmap (cctx->sptr, cctx->ssize); |
655 | #else |
690 | else |
|
|
691 | #endif |
656 | Safefree (cctx->sptr); |
692 | Safefree (cctx->sptr); |
657 | #endif |
|
|
658 | |
693 | |
659 | Safefree (cctx); |
694 | Safefree (cctx); |
660 | } |
695 | } |
661 | |
696 | |
662 | static coro_cctx * |
697 | static coro_cctx * |
… | |
… | |
696 | ++cctx_idle; |
731 | ++cctx_idle; |
697 | cctx->next = cctx_first; |
732 | cctx->next = cctx_first; |
698 | cctx_first = cctx; |
733 | cctx_first = cctx; |
699 | } |
734 | } |
700 | |
735 | |
|
|
736 | /** coroutine switching *****************************************************/ |
|
|
737 | |
701 | /* never call directly, always through the coro_state_transfer global variable */ |
738 | /* never call directly, always through the coro_state_transfer global variable */ |
702 | static void NOINLINE |
739 | static void NOINLINE |
703 | transfer (struct coro *prev, struct coro *next) |
740 | transfer (struct coro *prev, struct coro *next) |
704 | { |
741 | { |
705 | dSTACKLEVEL; |
742 | dSTACKLEVEL; |
706 | |
743 | |
707 | /* sometimes transfer is only called to set idle_sp */ |
744 | /* sometimes transfer is only called to set idle_sp */ |
708 | if (!next) |
745 | if (!next) |
709 | { |
746 | { |
710 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
747 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
711 | assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ |
748 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
712 | } |
749 | } |
713 | else if (prev != next) |
750 | else if (prev != next) |
714 | { |
751 | { |
715 | coro_cctx *prev__cctx; |
752 | coro_cctx *prev__cctx; |
716 | |
753 | |
… | |
… | |
727 | if (!prev->flags & CF_RUNNING) |
764 | if (!prev->flags & CF_RUNNING) |
728 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
765 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
729 | |
766 | |
730 | if (next->flags & CF_RUNNING) |
767 | if (next->flags & CF_RUNNING) |
731 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
768 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
769 | |
|
|
770 | if (next->flags & CF_DESTROYED) |
|
|
771 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
732 | |
772 | |
733 | prev->flags &= ~CF_RUNNING; |
773 | prev->flags &= ~CF_RUNNING; |
734 | next->flags |= CF_RUNNING; |
774 | next->flags |= CF_RUNNING; |
735 | |
775 | |
736 | LOCK; |
776 | LOCK; |
… | |
… | |
757 | |
797 | |
758 | /* possibly "free" the cctx */ |
798 | /* possibly "free" the cctx */ |
759 | if (prev__cctx->idle_sp == STACKLEVEL) |
799 | if (prev__cctx->idle_sp == STACKLEVEL) |
760 | { |
800 | { |
761 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
801 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
762 | assert (PL_top_env == prev__cctx->top_env); |
802 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
763 | |
803 | |
764 | prev->cctx = 0; |
804 | prev->cctx = 0; |
765 | |
805 | |
766 | cctx_put (prev__cctx); |
806 | cctx_put (prev__cctx); |
767 | prev__cctx->inuse = 0; |
807 | prev__cctx->inuse = 0; |
… | |
… | |
780 | PL_top_env = next->cctx->top_env; |
820 | PL_top_env = next->cctx->top_env; |
781 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
821 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
782 | } |
822 | } |
783 | |
823 | |
784 | free_coro_mortal (); |
824 | free_coro_mortal (); |
785 | |
|
|
786 | UNLOCK; |
825 | UNLOCK; |
787 | } |
826 | } |
788 | } |
827 | } |
789 | |
828 | |
790 | struct transfer_args |
829 | struct transfer_args |
… | |
… | |
792 | struct coro *prev, *next; |
831 | struct coro *prev, *next; |
793 | }; |
832 | }; |
794 | |
833 | |
795 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
834 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
796 | |
835 | |
|
|
836 | /** high level stuff ********************************************************/ |
|
|
837 | |
797 | static void |
838 | static int |
798 | coro_state_destroy (struct coro *coro) |
839 | coro_state_destroy (struct coro *coro) |
799 | { |
840 | { |
800 | if (coro->refcnt--) |
841 | if (coro->flags & CF_DESTROYED) |
801 | return; |
842 | return 0; |
|
|
843 | |
|
|
844 | coro->flags |= CF_DESTROYED; |
|
|
845 | |
|
|
846 | if (coro->flags & CF_READY) |
|
|
847 | { |
|
|
848 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
|
|
849 | /* alternative: look through all ready queues and remove the coro */ |
|
|
850 | LOCK; |
|
|
851 | --coro_nready; |
|
|
852 | UNLOCK; |
|
|
853 | } |
|
|
854 | else |
|
|
855 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
802 | |
856 | |
803 | if (coro->mainstack && coro->mainstack != main_mainstack) |
857 | if (coro->mainstack && coro->mainstack != main_mainstack) |
804 | { |
858 | { |
805 | struct coro temp; |
859 | struct coro temp; |
|
|
860 | |
|
|
861 | assert (!(coro->flags & CF_RUNNING)); |
|
|
862 | |
806 | Zero (&temp, 1, struct coro); |
863 | Zero (&temp, 1, struct coro); |
807 | temp.save = CORO_SAVE_ALL; |
864 | temp.save = CORO_SAVE_ALL; |
808 | |
865 | |
809 | if (coro->flags & CF_RUNNING) |
866 | if (coro->flags & CF_RUNNING) |
810 | croak ("FATAL: tried to destroy currently running coroutine"); |
867 | croak ("FATAL: tried to destroy currently running coroutine"); |
… | |
… | |
819 | coro->mainstack = 0; |
876 | coro->mainstack = 0; |
820 | } |
877 | } |
821 | |
878 | |
822 | cctx_destroy (coro->cctx); |
879 | cctx_destroy (coro->cctx); |
823 | SvREFCNT_dec (coro->args); |
880 | SvREFCNT_dec (coro->args); |
824 | Safefree (coro); |
881 | |
|
|
882 | return 1; |
825 | } |
883 | } |
826 | |
884 | |
827 | static int |
885 | static int |
828 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
886 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
829 | { |
887 | { |
830 | struct coro *coro = (struct coro *)mg->mg_ptr; |
888 | struct coro *coro = (struct coro *)mg->mg_ptr; |
831 | mg->mg_ptr = 0; |
889 | mg->mg_ptr = 0; |
832 | |
890 | |
|
|
891 | if (--coro->refcnt < 0) |
|
|
892 | { |
833 | coro_state_destroy (coro); |
893 | coro_state_destroy (coro); |
|
|
894 | Safefree (coro); |
|
|
895 | } |
834 | |
896 | |
835 | return 0; |
897 | return 0; |
836 | } |
898 | } |
837 | |
899 | |
838 | static int |
900 | static int |
… | |
… | |
845 | return 0; |
907 | return 0; |
846 | } |
908 | } |
847 | |
909 | |
848 | static MGVTBL coro_state_vtbl = { |
910 | static MGVTBL coro_state_vtbl = { |
849 | 0, 0, 0, 0, |
911 | 0, 0, 0, 0, |
850 | coro_state_clear, |
912 | coro_state_free, |
851 | 0, |
913 | 0, |
852 | #ifdef MGf_DUP |
914 | #ifdef MGf_DUP |
853 | coro_state_dup, |
915 | coro_state_dup, |
854 | #else |
916 | #else |
855 | # define MGf_DUP 0 |
917 | # define MGf_DUP 0 |
… | |
… | |
906 | return old_save; |
968 | return old_save; |
907 | } |
969 | } |
908 | |
970 | |
909 | /** Coro ********************************************************************/ |
971 | /** Coro ********************************************************************/ |
910 | |
972 | |
911 | #define PRIO_MAX 3 |
|
|
912 | #define PRIO_HIGH 1 |
|
|
913 | #define PRIO_NORMAL 0 |
|
|
914 | #define PRIO_LOW -1 |
|
|
915 | #define PRIO_IDLE -3 |
|
|
916 | #define PRIO_MIN -4 |
|
|
917 | |
|
|
918 | /* for Coro.pm */ |
|
|
919 | static SV *coro_current; |
|
|
920 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
921 | static int coro_nready; |
|
|
922 | |
|
|
923 | static void |
973 | static void |
924 | coro_enq (SV *coro_sv) |
974 | coro_enq (SV *coro_sv) |
925 | { |
975 | { |
926 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
976 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
927 | coro_nready++; |
|
|
928 | } |
977 | } |
929 | |
978 | |
930 | static SV * |
979 | static SV * |
931 | coro_deq (int min_prio) |
980 | coro_deq (int min_prio) |
932 | { |
981 | { |
… | |
… | |
936 | if (min_prio < 0) |
985 | if (min_prio < 0) |
937 | min_prio = 0; |
986 | min_prio = 0; |
938 | |
987 | |
939 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
988 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
940 | if (AvFILLp (coro_ready [prio]) >= 0) |
989 | if (AvFILLp (coro_ready [prio]) >= 0) |
941 | { |
|
|
942 | coro_nready--; |
|
|
943 | return av_shift (coro_ready [prio]); |
990 | return av_shift (coro_ready [prio]); |
944 | } |
|
|
945 | |
991 | |
946 | return 0; |
992 | return 0; |
947 | } |
993 | } |
948 | |
994 | |
949 | static int |
995 | static int |
… | |
… | |
961 | |
1007 | |
962 | coro->flags |= CF_READY; |
1008 | coro->flags |= CF_READY; |
963 | |
1009 | |
964 | LOCK; |
1010 | LOCK; |
965 | coro_enq (SvREFCNT_inc (coro_sv)); |
1011 | coro_enq (SvREFCNT_inc (coro_sv)); |
|
|
1012 | ++coro_nready; |
966 | UNLOCK; |
1013 | UNLOCK; |
967 | |
1014 | |
968 | return 1; |
1015 | return 1; |
969 | } |
1016 | } |
970 | |
1017 | |
971 | static int |
1018 | static int |
972 | api_is_ready (SV *coro_sv) |
1019 | api_is_ready (SV *coro_sv) |
973 | { |
1020 | { |
974 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
1021 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
975 | } |
1022 | } |
976 | |
1023 | |
977 | static void |
1024 | static void |
978 | prepare_schedule (struct transfer_args *ta) |
1025 | prepare_schedule (struct transfer_args *ta) |
979 | { |
1026 | { |
980 | SV *prev, *next; |
1027 | SV *prev_sv, *next_sv; |
981 | |
1028 | |
982 | for (;;) |
1029 | for (;;) |
983 | { |
1030 | { |
984 | LOCK; |
1031 | LOCK; |
985 | next = coro_deq (PRIO_MIN); |
1032 | next_sv = coro_deq (PRIO_MIN); |
|
|
1033 | |
|
|
1034 | /* nothing to schedule: call the idle handler */ |
|
|
1035 | if (!next_sv) |
|
|
1036 | { |
|
|
1037 | dSP; |
|
|
1038 | UNLOCK; |
|
|
1039 | |
|
|
1040 | ENTER; |
|
|
1041 | SAVETMPS; |
|
|
1042 | |
|
|
1043 | PUSHMARK (SP); |
|
|
1044 | PUTBACK; |
|
|
1045 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1046 | |
|
|
1047 | FREETMPS; |
|
|
1048 | LEAVE; |
|
|
1049 | continue; |
|
|
1050 | } |
|
|
1051 | |
|
|
1052 | ta->next = SvSTATE (next_sv); |
|
|
1053 | |
|
|
1054 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1055 | if (ta->next->flags & CF_DESTROYED) |
|
|
1056 | { |
|
|
1057 | UNLOCK; |
|
|
1058 | SvREFCNT_dec (next_sv); |
|
|
1059 | /* coro_nready is already taken care of by destroy */ |
|
|
1060 | continue; |
|
|
1061 | } |
|
|
1062 | |
|
|
1063 | --coro_nready; |
986 | UNLOCK; |
1064 | UNLOCK; |
987 | |
|
|
988 | if (next) |
|
|
989 | break; |
1065 | break; |
990 | |
|
|
991 | { |
|
|
992 | dSP; |
|
|
993 | |
|
|
994 | ENTER; |
|
|
995 | SAVETMPS; |
|
|
996 | |
|
|
997 | PUSHMARK (SP); |
|
|
998 | PUTBACK; |
|
|
999 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1000 | |
|
|
1001 | FREETMPS; |
|
|
1002 | LEAVE; |
|
|
1003 | } |
1066 | } |
1004 | } |
|
|
1005 | |
|
|
1006 | prev = SvRV (coro_current); |
|
|
1007 | SvRV_set (coro_current, next); |
|
|
1008 | |
1067 | |
1009 | /* free this only after the transfer */ |
1068 | /* free this only after the transfer */ |
|
|
1069 | prev_sv = SvRV (coro_current); |
|
|
1070 | SvRV_set (coro_current, next_sv); |
|
|
1071 | ta->prev = SvSTATE (prev_sv); |
|
|
1072 | |
|
|
1073 | assert (ta->next->flags & CF_READY); |
|
|
1074 | ta->next->flags &= ~CF_READY; |
|
|
1075 | |
1010 | LOCK; |
1076 | LOCK; |
1011 | free_coro_mortal (); |
1077 | free_coro_mortal (); |
|
|
1078 | coro_mortal = prev_sv; |
1012 | UNLOCK; |
1079 | UNLOCK; |
1013 | coro_mortal = prev; |
|
|
1014 | |
|
|
1015 | assert (!SvROK(prev));//D |
|
|
1016 | assert (!SvROK(next));//D |
|
|
1017 | |
|
|
1018 | ta->prev = SvSTATE (prev); |
|
|
1019 | ta->next = SvSTATE (next); |
|
|
1020 | |
|
|
1021 | assert (ta->next->flags & CF_READY); |
|
|
1022 | ta->next->flags &= ~CF_READY; |
|
|
1023 | } |
1080 | } |
1024 | |
1081 | |
1025 | static void |
1082 | static void |
1026 | prepare_cede (struct transfer_args *ta) |
1083 | prepare_cede (struct transfer_args *ta) |
1027 | { |
1084 | { |
1028 | api_ready (coro_current); |
1085 | api_ready (coro_current); |
1029 | |
|
|
1030 | prepare_schedule (ta); |
1086 | prepare_schedule (ta); |
|
|
1087 | } |
|
|
1088 | |
|
|
1089 | static int |
|
|
1090 | prepare_cede_notself (struct transfer_args *ta) |
|
|
1091 | { |
|
|
1092 | if (coro_nready) |
|
|
1093 | { |
|
|
1094 | SV *prev = SvRV (coro_current); |
|
|
1095 | prepare_schedule (ta); |
|
|
1096 | api_ready (prev); |
|
|
1097 | return 1; |
|
|
1098 | } |
|
|
1099 | else |
|
|
1100 | return 0; |
1031 | } |
1101 | } |
1032 | |
1102 | |
1033 | static void |
1103 | static void |
1034 | api_schedule (void) |
1104 | api_schedule (void) |
1035 | { |
1105 | { |
… | |
… | |
1037 | |
1107 | |
1038 | prepare_schedule (&ta); |
1108 | prepare_schedule (&ta); |
1039 | TRANSFER (ta); |
1109 | TRANSFER (ta); |
1040 | } |
1110 | } |
1041 | |
1111 | |
1042 | static void |
1112 | static int |
1043 | api_cede (void) |
1113 | api_cede (void) |
1044 | { |
1114 | { |
1045 | struct transfer_args ta; |
1115 | struct transfer_args ta; |
1046 | |
1116 | |
1047 | prepare_cede (&ta); |
1117 | prepare_cede (&ta); |
|
|
1118 | |
|
|
1119 | if (ta.prev != ta.next) |
|
|
1120 | { |
1048 | TRANSFER (ta); |
1121 | TRANSFER (ta); |
|
|
1122 | return 1; |
|
|
1123 | } |
|
|
1124 | else |
|
|
1125 | return 0; |
|
|
1126 | } |
|
|
1127 | |
|
|
1128 | static int |
|
|
1129 | api_cede_notself (void) |
|
|
1130 | { |
|
|
1131 | struct transfer_args ta; |
|
|
1132 | |
|
|
1133 | if (prepare_cede_notself (&ta)) |
|
|
1134 | { |
|
|
1135 | TRANSFER (ta); |
|
|
1136 | return 1; |
|
|
1137 | } |
|
|
1138 | else |
|
|
1139 | return 0; |
1049 | } |
1140 | } |
1050 | |
1141 | |
1051 | MODULE = Coro::State PACKAGE = Coro::State |
1142 | MODULE = Coro::State PACKAGE = Coro::State |
1052 | |
1143 | |
1053 | PROTOTYPES: DISABLE |
1144 | PROTOTYPES: DISABLE |
… | |
… | |
1109 | _set_stacklevel (...) |
1200 | _set_stacklevel (...) |
1110 | ALIAS: |
1201 | ALIAS: |
1111 | Coro::State::transfer = 1 |
1202 | Coro::State::transfer = 1 |
1112 | Coro::schedule = 2 |
1203 | Coro::schedule = 2 |
1113 | Coro::cede = 3 |
1204 | Coro::cede = 3 |
|
|
1205 | Coro::cede_notself = 4 |
1114 | CODE: |
1206 | CODE: |
1115 | { |
1207 | { |
1116 | struct transfer_args ta; |
1208 | struct transfer_args ta; |
1117 | |
1209 | |
1118 | switch (ix) |
1210 | switch (ix) |
… | |
… | |
1134 | break; |
1226 | break; |
1135 | |
1227 | |
1136 | case 3: |
1228 | case 3: |
1137 | prepare_cede (&ta); |
1229 | prepare_cede (&ta); |
1138 | break; |
1230 | break; |
|
|
1231 | |
|
|
1232 | case 4: |
|
|
1233 | if (!prepare_cede_notself (&ta)) |
|
|
1234 | XSRETURN_EMPTY; |
|
|
1235 | |
|
|
1236 | break; |
1139 | } |
1237 | } |
1140 | |
1238 | |
|
|
1239 | BARRIER; |
1141 | TRANSFER (ta); |
1240 | TRANSFER (ta); |
1142 | } |
|
|
1143 | |
1241 | |
1144 | void |
1242 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1145 | _clone_state_from (SV *dst, SV *src) |
1243 | XSRETURN_YES; |
|
|
1244 | } |
|
|
1245 | |
|
|
1246 | bool |
|
|
1247 | _destroy (SV *coro_sv) |
1146 | CODE: |
1248 | CODE: |
1147 | { |
1249 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1148 | struct coro *coro_src = SvSTATE (src); |
1250 | OUTPUT: |
1149 | |
1251 | RETVAL |
1150 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1151 | |
|
|
1152 | ++coro_src->refcnt; |
|
|
1153 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1154 | } |
|
|
1155 | |
1252 | |
1156 | void |
1253 | void |
1157 | _exit (code) |
1254 | _exit (code) |
1158 | int code |
1255 | int code |
1159 | PROTOTYPE: $ |
1256 | PROTOTYPE: $ |
… | |
… | |
1196 | coro_ready[i] = newAV (); |
1293 | coro_ready[i] = newAV (); |
1197 | |
1294 | |
1198 | { |
1295 | { |
1199 | SV *sv = perl_get_sv("Coro::API", 1); |
1296 | SV *sv = perl_get_sv("Coro::API", 1); |
1200 | |
1297 | |
1201 | coroapi.schedule = api_schedule; |
1298 | coroapi.schedule = api_schedule; |
1202 | coroapi.save = api_save; |
1299 | coroapi.save = api_save; |
1203 | coroapi.cede = api_cede; |
1300 | coroapi.cede = api_cede; |
|
|
1301 | coroapi.cede_notself = api_cede_notself; |
1204 | coroapi.ready = api_ready; |
1302 | coroapi.ready = api_ready; |
1205 | coroapi.is_ready = api_is_ready; |
1303 | coroapi.is_ready = api_is_ready; |
1206 | coroapi.nready = &coro_nready; |
1304 | coroapi.nready = &coro_nready; |
1207 | coroapi.current = coro_current; |
1305 | coroapi.current = coro_current; |
1208 | |
1306 | |
1209 | GCoroAPI = &coroapi; |
1307 | GCoroAPI = &coroapi; |
1210 | sv_setiv (sv, (IV)&coroapi); |
1308 | sv_setiv (sv, (IV)&coroapi); |
1211 | SvREADONLY_on (sv); |
1309 | SvREADONLY_on (sv); |
1212 | } |
1310 | } |
… | |
… | |
1228 | RETVAL = coro->prio; |
1326 | RETVAL = coro->prio; |
1229 | |
1327 | |
1230 | if (items > 1) |
1328 | if (items > 1) |
1231 | { |
1329 | { |
1232 | if (ix) |
1330 | if (ix) |
1233 | newprio += coro->prio; |
1331 | newprio = coro->prio - newprio; |
1234 | |
1332 | |
1235 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1333 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1236 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1334 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1237 | |
1335 | |
1238 | coro->prio = newprio; |
1336 | coro->prio = newprio; |
1239 | } |
1337 | } |
1240 | } |
1338 | } |
|
|
1339 | OUTPUT: |
|
|
1340 | RETVAL |
1241 | |
1341 | |
1242 | SV * |
1342 | SV * |
1243 | ready (SV *self) |
1343 | ready (SV *self) |
1244 | PROTOTYPE: $ |
1344 | PROTOTYPE: $ |
1245 | CODE: |
1345 | CODE: |