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.88 by root, Fri Nov 24 15:34:33 2006 UTC vs.
Revision 1.89 by root, Sat Nov 25 00:40:26 2006 UTC

84static struct CoroAPI coroapi; 84static struct CoroAPI coroapi;
85static AV *main_mainstack; /* used to differentiate between $main and others */ 85static AV *main_mainstack; /* used to differentiate between $main and others */
86static HV *coro_state_stash, *coro_stash; 86static HV *coro_state_stash, *coro_stash;
87static SV *coro_mortal; /* will be freed after next transfer */ 87static SV *coro_mortal; /* will be freed after next transfer */
88 88
89/* this is actually not only the c stack but also c registers etc... */ 89/* this is actually not just the c stack but also c registers etc... */
90typedef struct { 90typedef struct coro_stack {
91 int refcnt; /* pointer reference counter */ 91 struct coro_stack *next;
92 int usecnt; /* shared by how many coroutines */
93 int gencnt; /* generation counter */
94 92
95 coro_context cctx; 93 void *idle_sp;
96
97 void *sptr; 94 void *sptr;
98 long ssize; /* positive == mmap, otherwise malloc */ 95 long ssize; /* positive == mmap, otherwise malloc */
96
97 /* cpu state */
98 coro_context cctx;
99} coro_stack; 99} coro_stack;
100 100
101static coro_stack *main_stack;
102
101struct coro { 103struct coro {
102 /* the optional C context */
103 coro_stack *stack;
104 void *cursp;
105 int gencnt;
106
107 /* optionally saved, might be zero */ 104 /* optionally saved, might be zero */
108 AV *defav; 105 AV *defav;
109 SV *defsv; 106 SV *defsv;
110 SV *errsv; 107 SV *errsv;
111 108
112 /* saved global state not related to stacks */ 109 /* saved global state not related to stacks */
113 U8 dowarn; 110 U8 dowarn;
114 I32 in_eval; 111 I32 in_eval;
112
113 /* the c stack, if any */
114 coro_stack *stack;
115 115
116 /* the stacks and related info (callchain etc..) */ 116 /* the stacks and related info (callchain etc..) */
117 PERL_SI *curstackinfo; 117 PERL_SI *curstackinfo;
118 AV *curstack; 118 AV *curstack;
119 AV *mainstack; 119 AV *mainstack;
532 Safefree (PL_retstack); 532 Safefree (PL_retstack);
533#endif 533#endif
534} 534}
535 535
536static void 536static void
537allocate_stack (Coro__State ctx, int alloc) 537setup_coro (struct coro *coro)
538{
539 coro_stack *stack;
540
541 New (0, stack, 1, coro_stack);
542
543 stack->refcnt = 1;
544 stack->usecnt = 1;
545 stack->gencnt = ctx->gencnt = 0;
546
547 if (alloc)
548 {
549#if HAVE_MMAP
550 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
551 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
552 if (stack->sptr != (void *)-1)
553 {
554# if STACKGUARD
555 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
556# endif
557 }
558 else
559#endif
560 {
561 stack->ssize = - (STACKSIZE * (long)sizeof (long));
562 New (0, stack->sptr, STACKSIZE, long);
563 }
564 }
565 else
566 stack->sptr = 0;
567
568 ctx->stack = stack;
569}
570
571static void
572deallocate_stack (Coro__State ctx)
573{
574 coro_stack *stack = ctx->stack;
575
576 ctx->stack = 0;
577
578 if (stack)
579 {
580 if (!--stack->refcnt)
581 {
582#ifdef HAVE_MMAP
583 if (stack->ssize > 0 && stack->sptr)
584 munmap (stack->sptr, stack->ssize);
585 else
586#endif
587 Safefree (stack->sptr);
588
589 Safefree (stack);
590 }
591 else if (ctx->gencnt == stack->gencnt)
592 --stack->usecnt;
593 }
594}
595
596static void
597setup_coro (void *arg)
598{ 538{
599 /* 539 /*
600 * emulate part of the perl startup here. 540 * emulate part of the perl startup here.
601 */ 541 */
602 dTHX; 542 dTHX;
603 dSP; 543 dSP;
604 Coro__State ctx = (Coro__State)arg; 544 UNOP myop;
605 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 545 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
606 546
607 coro_init_stacks (aTHX); 547 coro_init_stacks (aTHX);
608 /*PL_curcop = 0;*/ 548 /*PL_curcop = 0;*/
609 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 549 /*PL_in_eval = PL_in_eval;*/ /* inherit */
610 SvREFCNT_dec (GvAV (PL_defgv)); 550 SvREFCNT_dec (GvAV (PL_defgv));
611 GvAV (PL_defgv) = ctx->args; ctx->args = 0; 551 GvAV (PL_defgv) = coro->args; coro->args = 0;
612 552
613 SPAGAIN; 553 SPAGAIN;
614 554
615 if (ctx->stack) 555 Zero (&myop, 1, UNOP);
616 { 556 myop.op_next = Nullop;
617 ctx->cursp = 0; 557 myop.op_flags = OPf_WANT_VOID;
618 558
559 PL_op = (OP *)&myop;
560
619 PUSHMARK(SP); 561 PUSHMARK(SP);
562 XPUSHs (sub_init);
620 PUTBACK; 563 PUTBACK;
621 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); 564 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
565 SPAGAIN;
622 566
623 if (SvTRUE (ERRSV)) 567 ENTER; /* necessary e.g. for dounwind */
624 croak (NULL); 568}
625 else 569
626 croak ("FATAL: CCTXT coroutine returned!"); 570static void
571transfer_tail ()
572{
573 if (coro_mortal)
574 {
575 SvREFCNT_dec (coro_mortal);
576 coro_mortal = 0;
627 } 577 }
628 else
629 {
630 UNOP myop;
631 578
632 Zero(&myop, 1, UNOP); 579 UNLOCK;
633 myop.op_next = Nullop;
634 myop.op_flags = OPf_WANT_VOID;
635
636 PL_op = (OP *)&myop;
637
638 PUSHMARK(SP);
639 XPUSHs (sub_init);
640 /*
641 * the next line is slightly wrong, as PL_op->op_next
642 * is actually being executed so we skip the first op.
643 * that doesn't matter, though, since it is only
644 * pp_nextstate and we never return...
645 * ah yes, and I don't care anyways ;)
646 */
647 PUTBACK;
648 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
649 SPAGAIN;
650
651 ENTER; /* necessary e.g. for dounwind */
652 }
653} 580}
654 581
655static void 582static void
656continue_coro (void *arg) 583coro_run (void *arg)
657{ 584{
658 /* 585 /*
659 * this is a _very_ stripped down perl interpreter ;) 586 * this is a _very_ stripped down perl interpreter ;)
660 */ 587 */
661 dTHX; 588 dTHX;
662 Coro__State ctx = (Coro__State)arg; 589
590 transfer_tail ();
663 591
664 PL_top_env = &PL_start_env; 592 PL_top_env = &PL_start_env;
665
666 ctx->cursp = 0;
667 PL_op = PL_op->op_next; 593 PL_restartop = PL_op->op_next;
668 CALLRUNOPS(aTHX); 594 /* somebody will hit me for both perl_run and PL_restart_top */
595 perl_run (aTHX_ PERL_GET_CONTEXT);
669 596
670 abort (); 597 abort ();
671} 598}
672 599
600static coro_stack *
601stack_new ()
602{
603 coro_stack *stack;
604
605 New (0, stack, 1, coro_stack);
606
607#if HAVE_MMAP
608
609 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
610 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
611
612 if (stack->sptr == (void *)-1)
613 {
614 fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n");
615 _exit (EXIT_FAILURE);
616 }
617
618 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
619
620#else
621
622 stack->ssize = STACKSIZE * (long)sizeof (long);
623 New (0, stack->sptr, STACKSIZE, long);
624
625 if (!stack->sptr)
626 {
627 fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n");
628 _exit (EXIT_FAILURE);
629 }
630
631#endif
632
633 coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize);
634
635 return stack;
636}
637
638static void
639stack_free (coro_stack *stack)
640{
641 if (!stack || stack == main_stack)
642 return;
643
644#if HAVE_MMAP
645 munmap (stack->sptr, stack->ssize);
646#else
647 Safefree (stack->sptr);
648#endif
649
650 Safefree (stack);
651}
652
653static coro_stack *stack_first;
654
655static void
656stack_get (struct coro *coro)
657{
658 if (stack_first)
659 {
660 coro->stack = stack_first;
661 stack_first = stack_first->next;
662 }
663 else
664 coro->stack = stack_new ();
665}
666
667static void
668stack_put (coro_stack *stack)
669{
670 stack->next = stack_first;
671 stack_first = stack;
672}
673
673/* never call directly, always through the coro_state_transfer global variable */ 674/* never call directly, always through the coro_state_transfer global variable */
674static void 675static void
675transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 676transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
676{ 677{
677 dSTACKLEVEL; 678 dSTACKLEVEL;
678 679
680 /* sometimes transfer is only called to set idle_sp */
681 if (!prev->stack->idle_sp)
682 prev->stack->idle_sp = stacklevel;
683
684 LOCK;
685
679 if (prev != next) 686 if (prev != next)
680 { 687 {
681 /* has this coro been created yet? */ 688 coro_stack *prev_stack = prev->stack;
682 if (next->mainstack) 689
690 /* possibly "free" the stack */
691 if (prev_stack->idle_sp == stacklevel)
683 { 692 {
684 LOCK; 693 stack_put (prev_stack);
694 prev->stack = 0;
695 }
696
697 if (!next->mainstack)
698 {
699 /* first get rid of the old state */
700 SAVE (prev, -1);
701 /* setup coroutine call */
702 setup_coro (next);
703 next->stack = 0;
704 }
705 else
706 {
685 SAVE (prev, flags); 707 SAVE (prev, flags);
686 LOAD (next); 708 LOAD (next);
687 UNLOCK;
688
689 /* mark this state as in-use */
690 next->mainstack = 0;
691 next->tmps_ix = -2;
692
693 /* stacklevel changed? if yes, grab the stack for us! */
694 if (flags & TRANSFER_SAVE_CCTXT)
695 {
696 if (!prev->stack)
697 allocate_stack (prev, 0);
698 else if (prev->cursp != stacklevel
699 && prev->stack->usecnt > 1)
700 {
701 prev->gencnt = ++prev->stack->gencnt;
702 prev->stack->usecnt = 1;
703 }
704
705 /* has our stack been invalidated? */
706 if (next->stack && next->stack->gencnt != next->gencnt)
707 {
708 deallocate_stack (next);
709 allocate_stack (next, 1);
710 coro_create (&(next->stack->cctx),
711 continue_coro, (void *)next,
712 next->stack->sptr, labs (next->stack->ssize));
713 }
714
715 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
716 prev->cursp = stacklevel;
717 /* don't add any code here */
718 }
719 else
720 next->cursp = stacklevel;
721 } 709 }
722 else if (next->tmps_ix == -2)
723 croak ("tried to transfer to running coroutine");
724 else
725 {
726 LOCK;
727 SAVE (prev, -1); /* first get rid of the old state */
728 UNLOCK;
729 710
730 /* create the coroutine for the first time */ 711 if (!next->stack)
731 if (flags & TRANSFER_SAVE_CCTXT) 712 stack_get (next);
732 {
733 if (!prev->stack)
734 allocate_stack (prev, 0);
735 713
736 /* the new coroutine starts with start_env again */ 714 if (prev_stack != next->stack)
737 PL_top_env = &PL_start_env;
738
739 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
740 {
741 setup_coro (next);
742 next->cursp = stacklevel;
743
744 prev->stack->refcnt++;
745 prev->stack->usecnt++;
746 next->stack = prev->stack;
747 next->gencnt = prev->gencnt;
748 }
749 else
750 {
751 assert (!next->stack);
752 allocate_stack (next, 1);
753 coro_create (&(next->stack->cctx),
754 setup_coro, (void *)next,
755 next->stack->sptr, labs (next->stack->ssize));
756 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 715 coro_transfer (&prev_stack->cctx, &next->stack->cctx);
757 prev->cursp = stacklevel;
758 /* don't add any code here */
759 }
760 }
761 else
762 {
763 setup_coro (next);
764 next->cursp = stacklevel;
765 }
766 }
767 } 716 }
768 717
769 LOCK; 718 transfer_tail ();
770 if (coro_mortal)
771 {
772 SvREFCNT_dec (coro_mortal);
773 coro_mortal = 0;
774 }
775 UNLOCK;
776} 719}
777 720
778/* use this function pointer to call the above function */ 721/* use this function pointer to call the above function */
779/* this is done to increase chances of the compiler not inlining the call */ 722/* this is done to increase chances of the compiler not inlining the call */
780void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; 723void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
797 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 740 LOAD ((&temp)); /* this will get rid of defsv etc.. */
798 741
799 coro->mainstack = 0; 742 coro->mainstack = 0;
800 } 743 }
801 744
802 deallocate_stack (coro); 745 stack_free (coro->stack);
803 SvREFCNT_dec (coro->args); 746 SvREFCNT_dec (coro->args);
804 Safefree (coro); 747 Safefree (coro);
805} 748}
806 749
807static int 750static int
955 /* free this only after the transfer */ 898 /* free this only after the transfer */
956 coro_mortal = prev; 899 coro_mortal = prev;
957 900
958 UNLOCK; 901 UNLOCK;
959 902
960 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 903 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL);
961 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
962} 904}
905
906static int coro_cede_self;
963 907
964static void 908static void
965api_cede (void) 909api_cede (void)
966{ 910{
967 dTHX; 911 dTHX;
912 SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current)));
968 913
969 LOCK; 914 LOCK;
970 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 915
916 if (coro_cede_self)
917 {
918 AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN];
919 av_unshift (runqueue, 1);
920 av_store (runqueue, 0, current);
921 coro_nready++;
922 coro_cede_self = 0;
923 }
924 else
925 coro_enq (aTHX_ current);
926
971 UNLOCK; 927 UNLOCK;
972 928
973 api_schedule (); 929 api_schedule ();
974} 930}
975 931
987 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 943 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
988 944
989 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 945 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
990 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 946 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
991 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 947 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
992 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
993 948
994 main_mainstack = PL_mainstack; 949 main_mainstack = PL_mainstack;
995 950
996 coroapi.ver = CORO_API_VERSION; 951 coroapi.ver = CORO_API_VERSION;
997 coroapi.transfer = api_transfer; 952 coroapi.transfer = api_transfer;
953
954 Newz (0, main_stack, 1, coro_stack);
955 main_stack->idle_sp = (void *)-1;
998 956
999 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 957 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1000} 958}
1001 959
1002SV * 960SV *
1008 int i; 966 int i;
1009 967
1010 Newz (0, coro, 1, struct coro); 968 Newz (0, coro, 1, struct coro);
1011 coro->args = newAV (); 969 coro->args = newAV ();
1012 970
1013 for (i = 1; i < items; i++)
1014 av_push (coro->args, newSVsv (ST (i)));
1015
1016 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1017 /*coro->stack = 0;*/
1018
1019 hv = newHV (); 971 hv = newHV ();
1020 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 972 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1021 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 973 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
974
975 av_push (coro->args, newSVsv (RETVAL));
976 for (i = 1; i < items; i++)
977 av_push (coro->args, newSVsv (ST (i)));
978
979 coro->stack = main_stack;
980 /*coro->mainstack = 0; *//*actual work is done inside transfer */
981 /*coro->stack = 0;*/
1022} 982}
1023 OUTPUT: 983 OUTPUT:
1024 RETVAL 984 RETVAL
1025 985
1026void 986void
1027transfer(prev, next, flags) 987transfer (prev, next, flags)
1028 SV *prev 988 SV *prev
1029 SV *next 989 SV *next
1030 int flags 990 int flags
1031 CODE: 991 CODE:
1032 PUTBACK; 992 PUTBACK;
1052 coro->prio = newprio; 1012 coro->prio = newprio;
1053 } 1013 }
1054} 1014}
1055 1015
1056void 1016void
1017_clear_idle_sp (Coro::State self)
1018 CODE:
1019 self->stack->idle_sp = 0;
1020
1021void
1057_clone_state_from (SV *dst, SV *src) 1022_clone_state_from (SV *dst, SV *src)
1058 CODE: 1023 CODE:
1059{ 1024{
1060 struct coro *coro_src = SvSTATE (src); 1025 struct coro *coro_src = SvSTATE (src);
1061 1026
1138 sv_setiv (sv, (IV)&coroapi); 1103 sv_setiv (sv, (IV)&coroapi);
1139 SvREADONLY_on (sv); 1104 SvREADONLY_on (sv);
1140 } 1105 }
1141} 1106}
1142 1107
1143#if !PERL_MICRO
1144
1145void 1108void
1146ready (self) 1109ready (SV *self)
1147 SV * self
1148 PROTOTYPE: $ 1110 PROTOTYPE: $
1149 CODE: 1111 CODE:
1150 api_ready (self); 1112 api_ready (self);
1151
1152#endif
1153 1113
1154int 1114int
1155nready (...) 1115nready (...)
1156 PROTOTYPE: 1116 PROTOTYPE:
1157 CODE: 1117 CODE:
1164 PROTOTYPE: 1124 PROTOTYPE:
1165 CODE: 1125 CODE:
1166 api_schedule (); 1126 api_schedule ();
1167 1127
1168void 1128void
1129_set_cede_self ()
1130 CODE:
1131 coro_cede_self = 1;
1132
1133void
1169cede (...) 1134cede (...)
1170 PROTOTYPE: 1135 PROTOTYPE:
1171 CODE: 1136 CODE:
1172 api_cede (); 1137 api_cede ();
1173 1138
1174# and these are hacks 1139MODULE = Coro::State PACKAGE = Coro::AIO
1140
1175SV * 1141SV *
1176_aio_get_state () 1142_get_state ()
1177 CODE: 1143 CODE:
1178{ 1144{
1179 struct { 1145 struct {
1180 int errorno; 1146 int errorno;
1181 int laststype; 1147 int laststype;
1192} 1158}
1193 OUTPUT: 1159 OUTPUT:
1194 RETVAL 1160 RETVAL
1195 1161
1196void 1162void
1197_aio_set_state (char *data_) 1163_set_state (char *data_)
1198 PROTOTYPE: $ 1164 PROTOTYPE: $
1199 CODE: 1165 CODE:
1200{ 1166{
1201 struct { 1167 struct {
1202 int errorno; 1168 int errorno;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines