ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.100 by root, Mon Nov 27 00:07:49 2006 UTC vs.
Revision 1.106 by root, Mon Nov 27 02:08:55 2006 UTC

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)
61static long pagesize; 65static 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. */
97static AV *main_mainstack; /* used to differentiate between $main and others */ 104static AV *main_mainstack; /* used to differentiate between $main and others */
98static HV *coro_state_stash, *coro_stash; 105static HV *coro_state_stash, *coro_stash;
99static SV *coro_mortal; /* will be freed after next transfer */ 106static SV *coro_mortal; /* will be freed after next transfer */
100 107
101/* this is a structure representing a c-level coroutine */ 108/* this is a structure representing a c-level coroutine */
102typedef struct coro_stack { 109typedef struct coro_cctx {
103 struct coro_stack *next; 110 struct coro_cctx *next;
104 111
105 /* the stack */ 112 /* the stack */
106 void *sptr; 113 void *sptr;
107 long ssize; /* positive == mmap, otherwise malloc */ 114 long ssize; /* positive == mmap, otherwise malloc */
108 115
109 /* cpu state */ 116 /* cpu state */
110 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 117 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
111 JMPENV *top_env; 118 JMPENV *top_env;
112 coro_context cctx; 119 coro_context cctx;
113} coro_stack; 120
121#if USE_VALGRIND
122 int valgrind_id;
123#endif
124} coro_cctx;
114 125
115/* this is a structure representing a perl-level coroutine */ 126/* this is a structure representing a perl-level coroutine */
116struct coro { 127struct coro {
117 /* the c coroutine allocated to this perl coroutine, if any */ 128 /* the c coroutine allocated to this perl coroutine, if any */
118 coro_stack *stack; 129 coro_cctx *cctx;
119 130
120 /* data associated with this coroutine (initial args) */ 131 /* data associated with this coroutine (initial args) */
121 AV *args; 132 AV *args;
122 int refcnt; 133 int refcnt;
123 134
491 502
492/* 503/*
493 * destroy the stacks, the callchain etc... 504 * destroy the stacks, the callchain etc...
494 */ 505 */
495static void 506static void
496destroy_stacks() 507coro_destroy_stacks ()
497{ 508{
498 if (!IN_DESTRUCT) 509 if (!IN_DESTRUCT)
499 { 510 {
500 /* is this ugly, I ask? */ 511 /* is this ugly, I ask? */
501 LEAVE_SCOPE (0); 512 LEAVE_SCOPE (0);
543setup_coro (struct coro *coro) 554setup_coro (struct coro *coro)
544{ 555{
545 /* 556 /*
546 * emulate part of the perl startup here. 557 * emulate part of the perl startup here.
547 */ 558 */
548 dTHX;
549 dSP;
550 UNOP myop;
551 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
552 559
553 coro_init_stacks (); 560 coro_init_stacks ();
561
554 /*PL_curcop = 0;*/ 562 PL_curcop = 0;
555 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 563 PL_in_eval = 0;
564 PL_curpm = 0;
565
566 {
567 dSP;
568 LOGOP myop;
569
570 /* I have no idea why this is needed, but it is */
571 PUSHMARK (SP);
572
556 SvREFCNT_dec (GvAV (PL_defgv)); 573 SvREFCNT_dec (GvAV (PL_defgv));
557 GvAV (PL_defgv) = coro->args; coro->args = 0; 574 GvAV (PL_defgv) = coro->args; coro->args = 0;
558 575
559 SPAGAIN;
560
561 Zero (&myop, 1, UNOP); 576 Zero (&myop, 1, LOGOP);
562 myop.op_next = Nullop; 577 myop.op_next = Nullop;
563 myop.op_flags = OPf_WANT_VOID; 578 myop.op_flags = OPf_WANT_VOID;
564 579
565 PL_op = (OP *)&myop; 580 PL_op = (OP *)&myop;
566 581
567 PUSHMARK(SP); 582 PUSHMARK (SP);
568 XPUSHs (sub_init); 583 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE));
569 PUTBACK; 584 PUTBACK;
570 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 585 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
571 SPAGAIN; 586 SPAGAIN;
572 587
573 ENTER; /* necessary e.g. for dounwind */ 588 ENTER; /* necessary e.g. for dounwind */
589 }
574} 590}
575 591
576static void 592static void
577free_coro_mortal () 593free_coro_mortal ()
578{ 594{
582 coro_mortal = 0; 598 coro_mortal = 0;
583 } 599 }
584} 600}
585 601
586static void NOINLINE 602static void NOINLINE
587prepare_cctx (coro_stack *cctx) 603prepare_cctx (coro_cctx *cctx)
588{ 604{
589 dSP; 605 dSP;
590 UNOP myop; 606 LOGOP myop;
591 607
592 Zero (&myop, 1, UNOP); 608 Zero (&myop, 1, LOGOP);
593 myop.op_next = PL_op; 609 myop.op_next = PL_op;
594 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 610 myop.op_flags = OPf_WANT_VOID;
595 611
612 sv_setiv (get_sv ("Coro::State::cctx", FALSE), PTR2IV (cctx));
613
596 PUSHMARK(SP); 614 PUSHMARK (SP);
597 EXTEND (SP, 2);
598 PUSHs (newSViv (PTR2IV (cctx)));
599 PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 615 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
600 PUTBACK; 616 PUTBACK;
601 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 617 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
602 SPAGAIN; 618 SPAGAIN;
603} 619}
604 620
605static void 621static void
606coro_run (void *arg) 622coro_run (void *arg)
607{ 623{
624 /* coro_run is the alternative epilogue of transfer() */
625 UNLOCK;
626
608 /* 627 /*
609 * this is a _very_ stripped down perl interpreter ;) 628 * this is a _very_ stripped down perl interpreter ;)
610 */ 629 */
611 UNLOCK;
612
613 PL_top_env = &PL_start_env; 630 PL_top_env = &PL_start_env;
631 /* inject call to cctx_init */
614 prepare_cctx ((coro_stack *)arg); 632 prepare_cctx ((coro_cctx *)arg);
615 633
616 /* somebody will hit me for both perl_run and PL_restartop */ 634 /* somebody will hit me for both perl_run and PL_restartop */
617 perl_run (PERL_GET_CONTEXT); 635 perl_run (PL_curinterp);
618 636
619 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 637 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
620 abort (); 638 abort ();
621} 639}
622 640
623static coro_stack * 641static coro_cctx *
624stack_new () 642cctx_new ()
625{ 643{
626 coro_stack *stack; 644 coro_cctx *cctx;
627 645
628 New (0, stack, 1, coro_stack); 646 New (0, cctx, 1, coro_cctx);
629 647
630#if HAVE_MMAP 648#if HAVE_MMAP
631 649
632 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 650 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
633 /* mmap suppsedly does allocate-on-write for us */ 651 /* mmap suppsedly does allocate-on-write for us */
634 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 652 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
635 653
636 if (stack->sptr == (void *)-1) 654 if (cctx->sptr == (void *)-1)
637 { 655 {
638 perror ("FATAL: unable to mmap stack for coroutine"); 656 perror ("FATAL: unable to mmap stack for coroutine");
639 _exit (EXIT_FAILURE); 657 _exit (EXIT_FAILURE);
640 } 658 }
641 659
642# if STACKGUARD 660# if STACKGUARD
643 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 661 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
644# endif 662# endif
645 663
646#else 664#else
647 665
648 stack->ssize = STACKSIZE * (long)sizeof (long); 666 cctx->ssize = STACKSIZE * (long)sizeof (long);
649 New (0, stack->sptr, STACKSIZE, long); 667 New (0, cctx->sptr, STACKSIZE, long);
650 668
651 if (!stack->sptr) 669 if (!cctx->sptr)
652 { 670 {
653 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 671 perror ("FATAL: unable to malloc stack for coroutine");
654 _exit (EXIT_FAILURE); 672 _exit (EXIT_FAILURE);
655 } 673 }
656 674
657#endif 675#endif
658 676
677#if USE_VALGRIND
678 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
679 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
680 cctx->ssize + (char *)cctx->sptr
681 );
682#endif
683
659 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 684 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
660 685
661 return stack; 686 return cctx;
662} 687}
663 688
664static void 689static void
665stack_free (coro_stack *stack) 690cctx_free (coro_cctx *cctx)
666{ 691{
667 if (!stack) 692 if (!cctx)
668 return; 693 return;
669 694
695#if USE_VALGRIND
696 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
697#endif
698
670#if HAVE_MMAP 699#if HAVE_MMAP
671 munmap (stack->sptr, stack->ssize); 700 munmap (cctx->sptr, cctx->ssize);
672#else 701#else
673 Safefree (stack->sptr); 702 Safefree (cctx->sptr);
674#endif 703#endif
675 704
676 Safefree (stack); 705 Safefree (cctx);
677} 706}
678 707
679static coro_stack *stack_first; 708static coro_cctx *cctx_first;
709static int cctx_count, cctx_idle;
680 710
681static coro_stack * 711static coro_cctx *
682stack_get () 712cctx_get ()
683{ 713{
684 coro_stack *stack; 714 coro_cctx *cctx;
685 715
686 if (stack_first) 716 if (cctx_first)
687 { 717 {
688 stack = stack_first; 718 --cctx_idle;
719 cctx = cctx_first;
689 stack_first = stack->next; 720 cctx_first = cctx->next;
690 } 721 }
691 else 722 else
692 { 723 {
693 stack = stack_new (); 724 ++cctx_count;
725 cctx = cctx_new ();
694 PL_op = PL_op->op_next; 726 PL_op = PL_op->op_next;
695 } 727 }
696 728
697 return stack; 729 return cctx;
698} 730}
699 731
700static void 732static void
701stack_put (coro_stack *stack) 733cctx_put (coro_cctx *cctx)
702{ 734{
703 stack->next = stack_first; 735 ++cctx_idle;
704 stack_first = stack; 736 cctx->next = cctx_first;
737 cctx_first = cctx;
705} 738}
706 739
707/* never call directly, always through the coro_state_transfer global variable */ 740/* never call directly, always through the coro_state_transfer global variable */
708static void NOINLINE 741static void NOINLINE
709transfer (struct coro *prev, struct coro *next, int flags) 742transfer (struct coro *prev, struct coro *next, int flags)
710{ 743{
711 dSTACKLEVEL; 744 dSTACKLEVEL;
712 745
713 /* sometimes transfer is only called to set idle_sp */ 746 /* sometimes transfer is only called to set idle_sp */
714 if (flags == TRANSFER_SET_STACKLEVEL) 747 if (flags == TRANSFER_SET_STACKLEVEL)
715 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 748 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
716 else if (prev != next) 749 else if (prev != next)
717 { 750 {
718 coro_stack *prev__stack; 751 coro_cctx *prev__cctx;
719 752
720 LOCK; 753 LOCK;
721 754
722 if (next->mainstack) 755 if (next->mainstack)
723 { 756 {
731 /* first get rid of the old state */ 764 /* first get rid of the old state */
732 SAVE (prev, -1); 765 SAVE (prev, -1);
733 /* setup coroutine call */ 766 /* setup coroutine call */
734 setup_coro (next); 767 setup_coro (next);
735 /* need a stack */ 768 /* need a stack */
736 next->stack = 0; 769 next->cctx = 0;
737 } 770 }
738 771
739 if (!prev->stack) 772 if (!prev->cctx)
740 /* create a new empty context */ 773 /* create a new empty context */
741 Newz (0, prev->stack, 1, coro_stack); 774 Newz (0, prev->cctx, 1, coro_cctx);
742 775
743 prev__stack = prev->stack; 776 prev__cctx = prev->cctx;
744 777
745 /* possibly "free" the stack */ 778 /* possibly "free" the cctx */
746 if (prev__stack->idle_sp == STACKLEVEL) 779 if (prev__cctx->idle_sp == STACKLEVEL)
747 { 780 {
748 stack_put (prev__stack); 781 cctx_put (prev__cctx);
749 prev->stack = 0; 782 prev->cctx = 0;
750 } 783 }
751 784
752 if (!next->stack) 785 if (!next->cctx)
753 next->stack = stack_get (); 786 next->cctx = cctx_get ();
754 787
755 if (prev__stack != next->stack) 788 if (prev__cctx != next->cctx)
756 { 789 {
757 prev__stack->top_env = PL_top_env; 790 prev__cctx->top_env = PL_top_env;
758 PL_top_env = next->stack->top_env; 791 PL_top_env = next->cctx->top_env;
759 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 792 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
760 } 793 }
761 794
762 free_coro_mortal (); 795 free_coro_mortal ();
763 796
764 UNLOCK; 797 UNLOCK;
784 struct coro temp; 817 struct coro temp;
785 818
786 SAVE ((&temp), TRANSFER_SAVE_ALL); 819 SAVE ((&temp), TRANSFER_SAVE_ALL);
787 LOAD (coro); 820 LOAD (coro);
788 821
789 destroy_stacks (); 822 coro_destroy_stacks ();
790 823
791 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 824 LOAD ((&temp)); /* this will get rid of defsv etc.. */
792 825
793 coro->mainstack = 0; 826 coro->mainstack = 0;
794 } 827 }
795 828
796 stack_free (coro->stack); 829 cctx_free (coro->cctx);
797 SvREFCNT_dec (coro->args); 830 SvREFCNT_dec (coro->args);
798 Safefree (coro); 831 Safefree (coro);
799} 832}
800 833
801static int 834static int
823 0, 0, 0, 0, 856 0, 0, 0, 0,
824 coro_state_clear, 857 coro_state_clear,
825 0, 858 0,
826#ifdef MGf_DUP 859#ifdef MGf_DUP
827 coro_state_dup, 860 coro_state_dup,
861#else
862# define MGf_DUP 0
828#endif 863#endif
829}; 864};
830 865
831static struct coro * 866static struct coro *
832SvSTATE (SV *coro) 867SvSTATE (SV *coro)
1043 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1078 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1044 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1079 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1045 1080
1046 for (i = 1; i < items; i++) 1081 for (i = 1; i < items; i++)
1047 av_push (coro->args, newSVsv (ST (i))); 1082 av_push (coro->args, newSVsv (ST (i)));
1048
1049 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1050 /*coro->stack = 0;*/
1051} 1083}
1052 OUTPUT: 1084 OUTPUT:
1053 RETVAL 1085 RETVAL
1054 1086
1055void 1087void
1064 struct transfer_args ta; 1096 struct transfer_args ta;
1065 1097
1066 switch (ix) 1098 switch (ix)
1067 { 1099 {
1068 case 0: 1100 case 0:
1069 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1101 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1070 ta.next = 0; 1102 ta.next = 0;
1071 ta.flags = TRANSFER_SET_STACKLEVEL; 1103 ta.flags = TRANSFER_SET_STACKLEVEL;
1072 break; 1104 break;
1073 1105
1074 case 1: 1106 case 1:
1134 int code 1166 int code
1135 PROTOTYPE: $ 1167 PROTOTYPE: $
1136 CODE: 1168 CODE:
1137 _exit (code); 1169 _exit (code);
1138 1170
1171int
1172cctx_count ()
1173 CODE:
1174 RETVAL = cctx_count;
1175 OUTPUT:
1176 RETVAL
1177
1178int
1179cctx_idle ()
1180 CODE:
1181 RETVAL = cctx_idle;
1182 OUTPUT:
1183 RETVAL
1184
1139MODULE = Coro::State PACKAGE = Coro 1185MODULE = Coro::State PACKAGE = Coro
1140 1186
1141BOOT: 1187BOOT:
1142{ 1188{
1143 int i; 1189 int i;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines