… | |
… | |
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 | |
… | |
… | |
135 | typedef struct coro_cctx { |
138 | typedef struct coro_cctx { |
136 | struct coro_cctx *next; |
139 | struct coro_cctx *next; |
137 | |
140 | |
138 | /* the stack */ |
141 | /* the stack */ |
139 | void *sptr; |
142 | void *sptr; |
140 | long ssize; /* positive == mmap, otherwise malloc */ |
143 | ssize_t ssize; /* positive == mmap, otherwise malloc */ |
141 | |
144 | |
142 | /* cpu state */ |
145 | /* cpu state */ |
143 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
146 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
144 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
147 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
145 | JMPENV *top_env; |
148 | JMPENV *top_env; |
… | |
… | |
185 | int prio; |
188 | int prio; |
186 | }; |
189 | }; |
187 | |
190 | |
188 | typedef struct coro *Coro__State; |
191 | typedef struct coro *Coro__State; |
189 | 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 **********************************************************/ |
190 | |
209 | |
191 | static AV * |
210 | static AV * |
192 | coro_clone_padlist (CV *cv) |
211 | coro_clone_padlist (CV *cv) |
193 | { |
212 | { |
194 | AV *padlist = CvPADLIST (cv); |
213 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
303 | av_extend (av, AvMAX (av) + 1); |
322 | av_extend (av, AvMAX (av) + 1); |
304 | |
323 | |
305 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
324 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
306 | } |
325 | } |
307 | |
326 | |
|
|
327 | /** load & save, init *******************************************************/ |
|
|
328 | |
308 | #define SB do { |
329 | #define SB do { |
309 | #define SE } while (0) |
330 | #define SE } while (0) |
310 | |
331 | |
311 | #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 |
312 | |
333 | |
… | |
… | |
344 | CvPADLIST (cv) = (AV *)POPs; |
365 | CvPADLIST (cv) = (AV *)POPs; |
345 | } |
366 | } |
346 | |
367 | |
347 | PUTBACK; |
368 | PUTBACK; |
348 | } |
369 | } |
|
|
370 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
349 | } |
371 | } |
350 | |
372 | |
351 | static void |
373 | static void |
352 | save_perl (Coro__State c) |
374 | save_perl (Coro__State c) |
353 | { |
375 | { |
|
|
376 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
354 | { |
377 | { |
355 | dSP; |
378 | dSP; |
356 | I32 cxix = cxstack_ix; |
379 | I32 cxix = cxstack_ix; |
357 | PERL_CONTEXT *ccstk = cxstack; |
380 | PERL_CONTEXT *ccstk = cxstack; |
358 | PERL_SI *top_si = PL_curstackinfo; |
381 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
504 | #if !PERL_VERSION_ATLEAST (5,9,0) |
527 | #if !PERL_VERSION_ATLEAST (5,9,0) |
505 | Safefree (PL_retstack); |
528 | Safefree (PL_retstack); |
506 | #endif |
529 | #endif |
507 | } |
530 | } |
508 | |
531 | |
|
|
532 | /** coroutine stack handling ************************************************/ |
|
|
533 | |
509 | static void |
534 | static void |
510 | setup_coro (struct coro *coro) |
535 | setup_coro (struct coro *coro) |
511 | { |
536 | { |
512 | /* |
537 | /* |
513 | * emulate part of the perl startup here. |
538 | * emulate part of the perl startup here. |
… | |
… | |
515 | |
540 | |
516 | coro_init_stacks (); |
541 | coro_init_stacks (); |
517 | |
542 | |
518 | PL_curcop = &PL_compiling; |
543 | PL_curcop = &PL_compiling; |
519 | PL_in_eval = EVAL_NULL; |
544 | PL_in_eval = EVAL_NULL; |
|
|
545 | PL_comppad = 0; |
520 | PL_curpm = 0; |
546 | PL_curpm = 0; |
521 | PL_localizing = 0; |
547 | PL_localizing = 0; |
522 | PL_dirty = 0; |
548 | PL_dirty = 0; |
523 | PL_restartop = 0; |
549 | PL_restartop = 0; |
524 | |
550 | |
… | |
… | |
610 | |
636 | |
611 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
637 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
612 | /* mmap supposedly does allocate-on-write for us */ |
638 | /* mmap supposedly does allocate-on-write for us */ |
613 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
639 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
614 | |
640 | |
615 | if (cctx->sptr == (void *)-1) |
641 | if (cctx->sptr != (void *)-1) |
616 | { |
|
|
617 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
618 | _exit (EXIT_FAILURE); |
|
|
619 | } |
642 | { |
620 | |
|
|
621 | # if STACKGUARD |
643 | # if STACKGUARD |
622 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
644 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
623 | # endif |
645 | # endif |
624 | |
646 | REGISTER_STACK ( |
625 | #else |
647 | cctx, |
626 | |
|
|
627 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
628 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
629 | |
|
|
630 | if (!cctx->sptr) |
|
|
631 | { |
|
|
632 | perror ("FATAL: unable to malloc stack for coroutine"); |
|
|
633 | _exit (EXIT_FAILURE); |
|
|
634 | } |
|
|
635 | |
|
|
636 | #endif |
|
|
637 | |
|
|
638 | #if USE_VALGRIND |
|
|
639 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
640 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
648 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
641 | cctx->ssize + (char *)cctx->sptr |
649 | cctx->ssize + (char *)cctx->sptr |
642 | ); |
650 | ); |
643 | #endif |
|
|
644 | |
651 | |
645 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
652 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
|
|
653 | } |
|
|
654 | else |
|
|
655 | #endif |
|
|
656 | { |
|
|
657 | cctx->ssize = -STACKSIZE * (long)sizeof (long); |
|
|
658 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
659 | |
|
|
660 | if (!cctx->sptr) |
|
|
661 | { |
|
|
662 | perror ("FATAL: unable to allocate stack for coroutine"); |
|
|
663 | _exit (EXIT_FAILURE); |
|
|
664 | } |
|
|
665 | |
|
|
666 | REGISTER_STACK ( |
|
|
667 | cctx, |
|
|
668 | (char *)cctx->sptr, |
|
|
669 | (char *)cctx->sptr - cctx->ssize |
|
|
670 | ); |
|
|
671 | |
|
|
672 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize); |
|
|
673 | } |
646 | |
674 | |
647 | return cctx; |
675 | return cctx; |
648 | } |
676 | } |
649 | |
677 | |
650 | static void |
678 | static void |
… | |
… | |
658 | #if USE_VALGRIND |
686 | #if USE_VALGRIND |
659 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
687 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
660 | #endif |
688 | #endif |
661 | |
689 | |
662 | #if HAVE_MMAP |
690 | #if HAVE_MMAP |
|
|
691 | if (cctx->ssize > 0) |
663 | munmap (cctx->sptr, cctx->ssize); |
692 | munmap (cctx->sptr, cctx->ssize); |
664 | #else |
693 | else |
|
|
694 | #endif |
665 | Safefree (cctx->sptr); |
695 | Safefree (cctx->sptr); |
666 | #endif |
|
|
667 | |
696 | |
668 | Safefree (cctx); |
697 | Safefree (cctx); |
669 | } |
698 | } |
670 | |
699 | |
671 | static coro_cctx * |
700 | static coro_cctx * |
… | |
… | |
704 | |
733 | |
705 | ++cctx_idle; |
734 | ++cctx_idle; |
706 | cctx->next = cctx_first; |
735 | cctx->next = cctx_first; |
707 | cctx_first = cctx; |
736 | cctx_first = cctx; |
708 | } |
737 | } |
|
|
738 | |
|
|
739 | /** coroutine switching *****************************************************/ |
709 | |
740 | |
710 | /* never call directly, always through the coro_state_transfer global variable */ |
741 | /* never call directly, always through the coro_state_transfer global variable */ |
711 | static void NOINLINE |
742 | static void NOINLINE |
712 | transfer (struct coro *prev, struct coro *next) |
743 | transfer (struct coro *prev, struct coro *next) |
713 | { |
744 | { |
… | |
… | |
803 | struct coro *prev, *next; |
834 | struct coro *prev, *next; |
804 | }; |
835 | }; |
805 | |
836 | |
806 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
837 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
807 | |
838 | |
|
|
839 | /** high level stuff ********************************************************/ |
|
|
840 | |
808 | static int |
841 | static int |
809 | coro_state_destroy (struct coro *coro) |
842 | coro_state_destroy (struct coro *coro) |
810 | { |
843 | { |
811 | if (coro->flags & CF_DESTROYED) |
844 | if (coro->flags & CF_DESTROYED) |
812 | return 0; |
845 | return 0; |
813 | |
846 | |
814 | coro->flags |= CF_DESTROYED; |
847 | coro->flags |= CF_DESTROYED; |
|
|
848 | |
|
|
849 | if (coro->flags & CF_READY) |
|
|
850 | { |
|
|
851 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
|
|
852 | /* alternative: look through all ready queues and remove the coro */ |
|
|
853 | LOCK; |
|
|
854 | --coro_nready; |
|
|
855 | UNLOCK; |
|
|
856 | } |
|
|
857 | else |
|
|
858 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
815 | |
859 | |
816 | if (coro->mainstack && coro->mainstack != main_mainstack) |
860 | if (coro->mainstack && coro->mainstack != main_mainstack) |
817 | { |
861 | { |
|
|
862 | struct coro temp; |
|
|
863 | |
818 | assert (!(coro->flags & CF_RUNNING)); |
864 | assert (!(coro->flags & CF_RUNNING)); |
819 | |
865 | |
820 | struct coro temp; |
|
|
821 | Zero (&temp, 1, struct coro); |
866 | Zero (&temp, 1, struct coro); |
822 | temp.save = CORO_SAVE_ALL; |
867 | temp.save = CORO_SAVE_ALL; |
823 | |
868 | |
824 | if (coro->flags & CF_RUNNING) |
869 | if (coro->flags & CF_RUNNING) |
825 | croak ("FATAL: tried to destroy currently running coroutine"); |
870 | croak ("FATAL: tried to destroy currently running coroutine"); |
… | |
… | |
926 | return old_save; |
971 | return old_save; |
927 | } |
972 | } |
928 | |
973 | |
929 | /** Coro ********************************************************************/ |
974 | /** Coro ********************************************************************/ |
930 | |
975 | |
931 | #define PRIO_MAX 3 |
|
|
932 | #define PRIO_HIGH 1 |
|
|
933 | #define PRIO_NORMAL 0 |
|
|
934 | #define PRIO_LOW -1 |
|
|
935 | #define PRIO_IDLE -3 |
|
|
936 | #define PRIO_MIN -4 |
|
|
937 | |
|
|
938 | /* for Coro.pm */ |
|
|
939 | static SV *coro_current; |
|
|
940 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
941 | static int coro_nready; |
|
|
942 | |
|
|
943 | static void |
976 | static void |
944 | coro_enq (SV *coro_sv) |
977 | coro_enq (SV *coro_sv) |
945 | { |
978 | { |
946 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
979 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
947 | coro_nready++; |
|
|
948 | } |
980 | } |
949 | |
981 | |
950 | static SV * |
982 | static SV * |
951 | coro_deq (int min_prio) |
983 | coro_deq (int min_prio) |
952 | { |
984 | { |
… | |
… | |
956 | if (min_prio < 0) |
988 | if (min_prio < 0) |
957 | min_prio = 0; |
989 | min_prio = 0; |
958 | |
990 | |
959 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
991 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
960 | if (AvFILLp (coro_ready [prio]) >= 0) |
992 | if (AvFILLp (coro_ready [prio]) >= 0) |
961 | { |
|
|
962 | coro_nready--; |
|
|
963 | return av_shift (coro_ready [prio]); |
993 | return av_shift (coro_ready [prio]); |
964 | } |
|
|
965 | |
994 | |
966 | return 0; |
995 | return 0; |
967 | } |
996 | } |
968 | |
997 | |
969 | static int |
998 | static int |
… | |
… | |
981 | |
1010 | |
982 | coro->flags |= CF_READY; |
1011 | coro->flags |= CF_READY; |
983 | |
1012 | |
984 | LOCK; |
1013 | LOCK; |
985 | coro_enq (SvREFCNT_inc (coro_sv)); |
1014 | coro_enq (SvREFCNT_inc (coro_sv)); |
|
|
1015 | ++coro_nready; |
986 | UNLOCK; |
1016 | UNLOCK; |
987 | |
1017 | |
988 | return 1; |
1018 | return 1; |
989 | } |
1019 | } |
990 | |
1020 | |
… | |
… | |
1001 | |
1031 | |
1002 | for (;;) |
1032 | for (;;) |
1003 | { |
1033 | { |
1004 | LOCK; |
1034 | LOCK; |
1005 | next_sv = coro_deq (PRIO_MIN); |
1035 | next_sv = coro_deq (PRIO_MIN); |
1006 | UNLOCK; |
|
|
1007 | |
1036 | |
1008 | /* nothing to schedule: call the idle handler */ |
1037 | /* nothing to schedule: call the idle handler */ |
1009 | if (!next_sv) |
1038 | if (!next_sv) |
1010 | { |
1039 | { |
1011 | dSP; |
1040 | dSP; |
|
|
1041 | UNLOCK; |
1012 | |
1042 | |
1013 | ENTER; |
1043 | ENTER; |
1014 | SAVETMPS; |
1044 | SAVETMPS; |
1015 | |
1045 | |
1016 | PUSHMARK (SP); |
1046 | PUSHMARK (SP); |
… | |
… | |
1025 | ta->next = SvSTATE (next_sv); |
1055 | ta->next = SvSTATE (next_sv); |
1026 | |
1056 | |
1027 | /* cannot transfer to destroyed coros, skip and look for next */ |
1057 | /* cannot transfer to destroyed coros, skip and look for next */ |
1028 | if (ta->next->flags & CF_DESTROYED) |
1058 | if (ta->next->flags & CF_DESTROYED) |
1029 | { |
1059 | { |
|
|
1060 | UNLOCK; |
1030 | SvREFCNT_dec (next_sv); |
1061 | SvREFCNT_dec (next_sv); |
|
|
1062 | /* coro_nready is already taken care of by destroy */ |
1031 | continue; |
1063 | continue; |
1032 | } |
1064 | } |
1033 | |
1065 | |
|
|
1066 | --coro_nready; |
|
|
1067 | UNLOCK; |
1034 | break; |
1068 | break; |
1035 | } |
1069 | } |
1036 | |
1070 | |
1037 | /* free this only after the transfer */ |
1071 | /* free this only after the transfer */ |
1038 | prev_sv = SvRV (coro_current); |
1072 | prev_sv = SvRV (coro_current); |