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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines