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.103 by root, Mon Nov 27 01:33:30 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
496coro_destroy_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);
587 coro_mortal = 0; 598 coro_mortal = 0;
588 } 599 }
589} 600}
590 601
591static void NOINLINE 602static void NOINLINE
592prepare_cctx (coro_stack *cctx) 603prepare_cctx (coro_cctx *cctx)
593{ 604{
594 dSP; 605 dSP;
595 LOGOP myop; 606 LOGOP myop;
596 607
597 Zero (&myop, 1, LOGOP); 608 Zero (&myop, 1, LOGOP);
598 myop.op_next = PL_op; 609 myop.op_next = PL_op;
599 myop.op_flags = OPf_WANT_VOID; 610 myop.op_flags = OPf_WANT_VOID;
600 611
601 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 612 sv_setiv (get_sv ("Coro::State::cctx", FALSE), PTR2IV (cctx));
602 613
603 PUSHMARK (SP); 614 PUSHMARK (SP);
604 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 615 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
605 PUTBACK; 616 PUTBACK;
606 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 617 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
616 /* 627 /*
617 * this is a _very_ stripped down perl interpreter ;) 628 * this is a _very_ stripped down perl interpreter ;)
618 */ 629 */
619 PL_top_env = &PL_start_env; 630 PL_top_env = &PL_start_env;
620 /* inject call to cctx_init */ 631 /* inject call to cctx_init */
621 prepare_cctx ((coro_stack *)arg); 632 prepare_cctx ((coro_cctx *)arg);
622 633
623 /* somebody will hit me for both perl_run and PL_restartop */ 634 /* somebody will hit me for both perl_run and PL_restartop */
624 perl_run (PL_curinterp); 635 perl_run (PL_curinterp);
625 636
626 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);
627 abort (); 638 abort ();
628} 639}
629 640
630static coro_stack * 641static coro_cctx *
631stack_new () 642cctx_new ()
632{ 643{
633 coro_stack *stack; 644 coro_cctx *cctx;
634 645
635 New (0, stack, 1, coro_stack); 646 New (0, cctx, 1, coro_cctx);
636 647
637#if HAVE_MMAP 648#if HAVE_MMAP
638 649
639 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 650 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
640 /* mmap suppsedly does allocate-on-write for us */ 651 /* mmap suppsedly does allocate-on-write for us */
641 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);
642 653
643 if (stack->sptr == (void *)-1) 654 if (cctx->sptr == (void *)-1)
644 { 655 {
645 perror ("FATAL: unable to mmap stack for coroutine"); 656 perror ("FATAL: unable to mmap stack for coroutine");
646 _exit (EXIT_FAILURE); 657 _exit (EXIT_FAILURE);
647 } 658 }
648 659
649# if STACKGUARD 660# if STACKGUARD
650 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 661 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
651# endif 662# endif
652 663
653#else 664#else
654 665
655 stack->ssize = STACKSIZE * (long)sizeof (long); 666 cctx->ssize = STACKSIZE * (long)sizeof (long);
656 New (0, stack->sptr, STACKSIZE, long); 667 New (0, cctx->sptr, STACKSIZE, long);
657 668
658 if (!stack->sptr) 669 if (!cctx->sptr)
659 { 670 {
660 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 671 perror ("FATAL: unable to malloc stack for coroutine");
661 _exit (EXIT_FAILURE); 672 _exit (EXIT_FAILURE);
662 } 673 }
663 674
664#endif 675#endif
665 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
666 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);
667 685
668 return stack; 686 return cctx;
669} 687}
670 688
671static void 689static void
672stack_free (coro_stack *stack) 690cctx_free (coro_cctx *cctx)
673{ 691{
674 if (!stack) 692 if (!cctx)
675 return; 693 return;
676 694
695#if USE_VALGRIND
696 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
697#endif
698
677#if HAVE_MMAP 699#if HAVE_MMAP
678 munmap (stack->sptr, stack->ssize); 700 munmap (cctx->sptr, cctx->ssize);
679#else 701#else
680 Safefree (stack->sptr); 702 Safefree (cctx->sptr);
681#endif 703#endif
682 704
683 Safefree (stack); 705 Safefree (cctx);
684} 706}
685 707
686static coro_stack *stack_first; 708static coro_cctx *cctx_first;
687static int cctx_count, cctx_idle; 709static int cctx_count, cctx_idle;
688 710
689static coro_stack * 711static coro_cctx *
690stack_get () 712cctx_get ()
691{ 713{
692 coro_stack *stack; 714 coro_cctx *cctx;
693 715
694 if (stack_first) 716 if (cctx_first)
695 { 717 {
696 --cctx_idle; 718 --cctx_idle;
697 stack = stack_first; 719 cctx = cctx_first;
698 stack_first = stack->next; 720 cctx_first = cctx->next;
699 } 721 }
700 else 722 else
701 { 723 {
702 ++cctx_count; 724 ++cctx_count;
703 stack = stack_new (); 725 cctx = cctx_new ();
704 PL_op = PL_op->op_next; 726 PL_op = PL_op->op_next;
705 } 727 }
706 728
707 return stack; 729 return cctx;
708} 730}
709 731
710static void 732static void
711stack_put (coro_stack *stack) 733cctx_put (coro_cctx *cctx)
712{ 734{
713 ++cctx_idle; 735 ++cctx_idle;
714 stack->next = stack_first; 736 cctx->next = cctx_first;
715 stack_first = stack; 737 cctx_first = cctx;
716} 738}
717 739
718/* never call directly, always through the coro_state_transfer global variable */ 740/* never call directly, always through the coro_state_transfer global variable */
719static void NOINLINE 741static void NOINLINE
720transfer (struct coro *prev, struct coro *next, int flags) 742transfer (struct coro *prev, struct coro *next, int flags)
721{ 743{
722 dSTACKLEVEL; 744 dSTACKLEVEL;
723 745
724 /* sometimes transfer is only called to set idle_sp */ 746 /* sometimes transfer is only called to set idle_sp */
725 if (flags == TRANSFER_SET_STACKLEVEL) 747 if (flags == TRANSFER_SET_STACKLEVEL)
726 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 748 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
727 else if (prev != next) 749 else if (prev != next)
728 { 750 {
729 coro_stack *prev__stack; 751 coro_cctx *prev__cctx;
730 752
731 LOCK; 753 LOCK;
732 754
733 if (next->mainstack) 755 if (next->mainstack)
734 { 756 {
742 /* first get rid of the old state */ 764 /* first get rid of the old state */
743 SAVE (prev, -1); 765 SAVE (prev, -1);
744 /* setup coroutine call */ 766 /* setup coroutine call */
745 setup_coro (next); 767 setup_coro (next);
746 /* need a stack */ 768 /* need a stack */
747 next->stack = 0; 769 next->cctx = 0;
748 } 770 }
749 771
750 if (!prev->stack) 772 if (!prev->cctx)
751 /* create a new empty context */ 773 /* create a new empty context */
752 Newz (0, prev->stack, 1, coro_stack); 774 Newz (0, prev->cctx, 1, coro_cctx);
753 775
754 prev__stack = prev->stack; 776 prev__cctx = prev->cctx;
755 777
756 /* possibly "free" the stack */ 778 /* possibly "free" the cctx */
757 if (prev__stack->idle_sp == STACKLEVEL) 779 if (prev__cctx->idle_sp == STACKLEVEL)
758 { 780 {
759 stack_put (prev__stack); 781 cctx_put (prev__cctx);
760 prev->stack = 0; 782 prev->cctx = 0;
761 } 783 }
762 784
763 if (!next->stack) 785 if (!next->cctx)
764 next->stack = stack_get (); 786 next->cctx = cctx_get ();
765 787
766 if (prev__stack != next->stack) 788 if (prev__cctx != next->cctx)
767 { 789 {
768 prev__stack->top_env = PL_top_env; 790 prev__cctx->top_env = PL_top_env;
769 PL_top_env = next->stack->top_env; 791 PL_top_env = next->cctx->top_env;
770 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 792 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
771 } 793 }
772 794
773 free_coro_mortal (); 795 free_coro_mortal ();
774 796
775 UNLOCK; 797 UNLOCK;
802 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 824 LOAD ((&temp)); /* this will get rid of defsv etc.. */
803 825
804 coro->mainstack = 0; 826 coro->mainstack = 0;
805 } 827 }
806 828
807 stack_free (coro->stack); 829 cctx_free (coro->cctx);
808 SvREFCNT_dec (coro->args); 830 SvREFCNT_dec (coro->args);
809 Safefree (coro); 831 Safefree (coro);
810} 832}
811 833
812static int 834static int
1056 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;
1057 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1079 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1058 1080
1059 for (i = 1; i < items; i++) 1081 for (i = 1; i < items; i++)
1060 av_push (coro->args, newSVsv (ST (i))); 1082 av_push (coro->args, newSVsv (ST (i)));
1061
1062 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1063 /*coro->stack = 0;*/
1064} 1083}
1065 OUTPUT: 1084 OUTPUT:
1066 RETVAL 1085 RETVAL
1067 1086
1068void 1087void
1077 struct transfer_args ta; 1096 struct transfer_args ta;
1078 1097
1079 switch (ix) 1098 switch (ix)
1080 { 1099 {
1081 case 0: 1100 case 0:
1082 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1101 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1083 ta.next = 0; 1102 ta.next = 0;
1084 ta.flags = TRANSFER_SET_STACKLEVEL; 1103 ta.flags = TRANSFER_SET_STACKLEVEL;
1085 break; 1104 break;
1086 1105
1087 case 1: 1106 case 1:
1147 int code 1166 int code
1148 PROTOTYPE: $ 1167 PROTOTYPE: $
1149 CODE: 1168 CODE:
1150 _exit (code); 1169 _exit (code);
1151 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
1152MODULE = Coro::State PACKAGE = Coro 1185MODULE = Coro::State PACKAGE = Coro
1153 1186
1154BOOT: 1187BOOT:
1155{ 1188{
1156 int i; 1189 int i;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines