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.105 by root, Mon Nov 27 02:01:33 2006 UTC vs.
Revision 1.106 by root, Mon Nov 27 02:08:55 2006 UTC

62# ifndef PAGESIZE 62# ifndef PAGESIZE
63# define PAGESIZE pagesize 63# define PAGESIZE pagesize
64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
65static long pagesize; 65static long pagesize;
66# else 66# else
67# define BOOT_PAGESIZE 67# define BOOT_PAGESIZE (void)0
68# endif 68# endif
69#else
70# define PAGESIZE 0
71# define BOOT_PAGESIZE (void)0
69#endif 72#endif
70 73
71/* 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
72 * 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
73 * and should be unique. */ 76 * and should be unique. */
101static AV *main_mainstack; /* used to differentiate between $main and others */ 104static AV *main_mainstack; /* used to differentiate between $main and others */
102static HV *coro_state_stash, *coro_stash; 105static HV *coro_state_stash, *coro_stash;
103static SV *coro_mortal; /* will be freed after next transfer */ 106static SV *coro_mortal; /* will be freed after next transfer */
104 107
105/* this is a structure representing a c-level coroutine */ 108/* this is a structure representing a c-level coroutine */
106typedef struct coro_stack { 109typedef struct coro_cctx {
107 struct coro_stack *next; 110 struct coro_cctx *next;
108 111
109 /* the stack */ 112 /* the stack */
110 void *sptr; 113 void *sptr;
111 long ssize; /* positive == mmap, otherwise malloc */ 114 long ssize; /* positive == mmap, otherwise malloc */
112 115
116 coro_context cctx; 119 coro_context cctx;
117 120
118#if USE_VALGRIND 121#if USE_VALGRIND
119 int valgrind_id; 122 int valgrind_id;
120#endif 123#endif
121} coro_stack; 124} coro_cctx;
122 125
123/* this is a structure representing a perl-level coroutine */ 126/* this is a structure representing a perl-level coroutine */
124struct coro { 127struct coro {
125 /* the c coroutine allocated to this perl coroutine, if any */ 128 /* the c coroutine allocated to this perl coroutine, if any */
126 coro_stack *stack; 129 coro_cctx *cctx;
127 130
128 /* data associated with this coroutine (initial args) */ 131 /* data associated with this coroutine (initial args) */
129 AV *args; 132 AV *args;
130 int refcnt; 133 int refcnt;
131 134
499 502
500/* 503/*
501 * destroy the stacks, the callchain etc... 504 * destroy the stacks, the callchain etc...
502 */ 505 */
503static void 506static void
504coro_destroy_stacks() 507coro_destroy_stacks ()
505{ 508{
506 if (!IN_DESTRUCT) 509 if (!IN_DESTRUCT)
507 { 510 {
508 /* is this ugly, I ask? */ 511 /* is this ugly, I ask? */
509 LEAVE_SCOPE (0); 512 LEAVE_SCOPE (0);
595 coro_mortal = 0; 598 coro_mortal = 0;
596 } 599 }
597} 600}
598 601
599static void NOINLINE 602static void NOINLINE
600prepare_cctx (coro_stack *cctx) 603prepare_cctx (coro_cctx *cctx)
601{ 604{
602 dSP; 605 dSP;
603 LOGOP myop; 606 LOGOP myop;
604 607
605 Zero (&myop, 1, LOGOP); 608 Zero (&myop, 1, LOGOP);
606 myop.op_next = PL_op; 609 myop.op_next = PL_op;
607 myop.op_flags = OPf_WANT_VOID; 610 myop.op_flags = OPf_WANT_VOID;
608 611
609 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 612 sv_setiv (get_sv ("Coro::State::cctx", FALSE), PTR2IV (cctx));
610 613
611 PUSHMARK (SP); 614 PUSHMARK (SP);
612 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 615 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
613 PUTBACK; 616 PUTBACK;
614 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 617 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
624 /* 627 /*
625 * this is a _very_ stripped down perl interpreter ;) 628 * this is a _very_ stripped down perl interpreter ;)
626 */ 629 */
627 PL_top_env = &PL_start_env; 630 PL_top_env = &PL_start_env;
628 /* inject call to cctx_init */ 631 /* inject call to cctx_init */
629 prepare_cctx ((coro_stack *)arg); 632 prepare_cctx ((coro_cctx *)arg);
630 633
631 /* somebody will hit me for both perl_run and PL_restartop */ 634 /* somebody will hit me for both perl_run and PL_restartop */
632 perl_run (PL_curinterp); 635 perl_run (PL_curinterp);
633 636
634 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 637 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
635 abort (); 638 abort ();
636} 639}
637 640
638static coro_stack * 641static coro_cctx *
639stack_new () 642cctx_new ()
640{ 643{
641 coro_stack *stack; 644 coro_cctx *cctx;
642 645
643 New (0, stack, 1, coro_stack); 646 New (0, cctx, 1, coro_cctx);
644 647
645#if HAVE_MMAP 648#if HAVE_MMAP
646 649
647 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 650 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
648 /* mmap suppsedly does allocate-on-write for us */ 651 /* mmap suppsedly does allocate-on-write for us */
649 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);
650 653
651 if (stack->sptr == (void *)-1) 654 if (cctx->sptr == (void *)-1)
652 { 655 {
653 perror ("FATAL: unable to mmap stack for coroutine"); 656 perror ("FATAL: unable to mmap stack for coroutine");
654 _exit (EXIT_FAILURE); 657 _exit (EXIT_FAILURE);
655 } 658 }
656 659
657# if STACKGUARD 660# if STACKGUARD
658 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 661 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
659# endif 662# endif
660 663
661#else 664#else
662 665
663 stack->ssize = STACKSIZE * (long)sizeof (long); 666 cctx->ssize = STACKSIZE * (long)sizeof (long);
664 New (0, stack->sptr, STACKSIZE, long); 667 New (0, cctx->sptr, STACKSIZE, long);
665 668
666 if (!stack->sptr) 669 if (!cctx->sptr)
667 { 670 {
668 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 671 perror ("FATAL: unable to malloc stack for coroutine");
669 _exit (EXIT_FAILURE); 672 _exit (EXIT_FAILURE);
670 } 673 }
671 674
672#endif 675#endif
673 676
674#if USE_VALGRIND 677#if USE_VALGRIND
675 stack->valgrind_id = VALGRIND_STACK_REGISTER ( 678 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
676 STACKGUARD * PAGESIZE + (char *)stack->sptr, 679 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
677 stack->ssize + (char *)stack->sptr 680 cctx->ssize + (char *)cctx->sptr
678 ); 681 );
679#endif 682#endif
680 683
681 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);
682 685
683 return stack; 686 return cctx;
684} 687}
685 688
686static void 689static void
687stack_free (coro_stack *stack) 690cctx_free (coro_cctx *cctx)
688{ 691{
689 if (!stack) 692 if (!cctx)
690 return; 693 return;
691 694
692#if USE_VALGRIND 695#if USE_VALGRIND
693 VALGRIND_STACK_DEREGISTER (stack->valgrind_id); 696 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
694#endif 697#endif
695 698
696#if HAVE_MMAP 699#if HAVE_MMAP
697 munmap (stack->sptr, stack->ssize); 700 munmap (cctx->sptr, cctx->ssize);
698#else 701#else
699 Safefree (stack->sptr); 702 Safefree (cctx->sptr);
700#endif 703#endif
701 704
702 Safefree (stack); 705 Safefree (cctx);
703} 706}
704 707
705static coro_stack *stack_first; 708static coro_cctx *cctx_first;
706static int cctx_count, cctx_idle; 709static int cctx_count, cctx_idle;
707 710
708static coro_stack * 711static coro_cctx *
709stack_get () 712cctx_get ()
710{ 713{
711 coro_stack *stack; 714 coro_cctx *cctx;
712 715
713 if (stack_first) 716 if (cctx_first)
714 { 717 {
715 --cctx_idle; 718 --cctx_idle;
716 stack = stack_first; 719 cctx = cctx_first;
717 stack_first = stack->next; 720 cctx_first = cctx->next;
718 } 721 }
719 else 722 else
720 { 723 {
721 ++cctx_count; 724 ++cctx_count;
722 stack = stack_new (); 725 cctx = cctx_new ();
723 PL_op = PL_op->op_next; 726 PL_op = PL_op->op_next;
724 } 727 }
725 728
726 return stack; 729 return cctx;
727} 730}
728 731
729static void 732static void
730stack_put (coro_stack *stack) 733cctx_put (coro_cctx *cctx)
731{ 734{
732 ++cctx_idle; 735 ++cctx_idle;
733 stack->next = stack_first; 736 cctx->next = cctx_first;
734 stack_first = stack; 737 cctx_first = cctx;
735} 738}
736 739
737/* never call directly, always through the coro_state_transfer global variable */ 740/* never call directly, always through the coro_state_transfer global variable */
738static void NOINLINE 741static void NOINLINE
739transfer (struct coro *prev, struct coro *next, int flags) 742transfer (struct coro *prev, struct coro *next, int flags)
740{ 743{
741 dSTACKLEVEL; 744 dSTACKLEVEL;
742 745
743 /* sometimes transfer is only called to set idle_sp */ 746 /* sometimes transfer is only called to set idle_sp */
744 if (flags == TRANSFER_SET_STACKLEVEL) 747 if (flags == TRANSFER_SET_STACKLEVEL)
745 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 748 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
746 else if (prev != next) 749 else if (prev != next)
747 { 750 {
748 coro_stack *prev__stack; 751 coro_cctx *prev__cctx;
749 752
750 LOCK; 753 LOCK;
751 754
752 if (next->mainstack) 755 if (next->mainstack)
753 { 756 {
761 /* first get rid of the old state */ 764 /* first get rid of the old state */
762 SAVE (prev, -1); 765 SAVE (prev, -1);
763 /* setup coroutine call */ 766 /* setup coroutine call */
764 setup_coro (next); 767 setup_coro (next);
765 /* need a stack */ 768 /* need a stack */
766 next->stack = 0; 769 next->cctx = 0;
767 } 770 }
768 771
769 if (!prev->stack) 772 if (!prev->cctx)
770 /* create a new empty context */ 773 /* create a new empty context */
771 Newz (0, prev->stack, 1, coro_stack); 774 Newz (0, prev->cctx, 1, coro_cctx);
772 775
773 prev__stack = prev->stack; 776 prev__cctx = prev->cctx;
774 777
775 /* possibly "free" the stack */ 778 /* possibly "free" the cctx */
776 if (prev__stack->idle_sp == STACKLEVEL) 779 if (prev__cctx->idle_sp == STACKLEVEL)
777 { 780 {
778 stack_put (prev__stack); 781 cctx_put (prev__cctx);
779 prev->stack = 0; 782 prev->cctx = 0;
780 } 783 }
781 784
782 if (!next->stack) 785 if (!next->cctx)
783 next->stack = stack_get (); 786 next->cctx = cctx_get ();
784 787
785 if (prev__stack != next->stack) 788 if (prev__cctx != next->cctx)
786 { 789 {
787 prev__stack->top_env = PL_top_env; 790 prev__cctx->top_env = PL_top_env;
788 PL_top_env = next->stack->top_env; 791 PL_top_env = next->cctx->top_env;
789 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 792 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
790 } 793 }
791 794
792 free_coro_mortal (); 795 free_coro_mortal ();
793 796
794 UNLOCK; 797 UNLOCK;
821 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 824 LOAD ((&temp)); /* this will get rid of defsv etc.. */
822 825
823 coro->mainstack = 0; 826 coro->mainstack = 0;
824 } 827 }
825 828
826 stack_free (coro->stack); 829 cctx_free (coro->cctx);
827 SvREFCNT_dec (coro->args); 830 SvREFCNT_dec (coro->args);
828 Safefree (coro); 831 Safefree (coro);
829} 832}
830 833
831static int 834static int
1075 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;
1076 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1079 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1077 1080
1078 for (i = 1; i < items; i++) 1081 for (i = 1; i < items; i++)
1079 av_push (coro->args, newSVsv (ST (i))); 1082 av_push (coro->args, newSVsv (ST (i)));
1080
1081 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1082 /*coro->stack = 0;*/
1083} 1083}
1084 OUTPUT: 1084 OUTPUT:
1085 RETVAL 1085 RETVAL
1086 1086
1087void 1087void
1096 struct transfer_args ta; 1096 struct transfer_args ta;
1097 1097
1098 switch (ix) 1098 switch (ix)
1099 { 1099 {
1100 case 0: 1100 case 0:
1101 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1101 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1102 ta.next = 0; 1102 ta.next = 0;
1103 ta.flags = TRANSFER_SET_STACKLEVEL; 1103 ta.flags = TRANSFER_SET_STACKLEVEL;
1104 break; 1104 break;
1105 1105
1106 case 1: 1106 case 1:
1166 int code 1166 int code
1167 PROTOTYPE: $ 1167 PROTOTYPE: $
1168 CODE: 1168 CODE:
1169 _exit (code); 1169 _exit (code);
1170 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
1171MODULE = Coro::State PACKAGE = Coro 1185MODULE = Coro::State PACKAGE = Coro
1172 1186
1173BOOT: 1187BOOT:
1174{ 1188{
1175 int i; 1189 int i;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines