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.109 by root, Mon Nov 27 18:45:29 2006 UTC vs.
Revision 1.114 by root, Thu Nov 30 21:22:45 2006 UTC

119 /* cpu state */ 119 /* cpu state */
120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
121 JMPENV *top_env; 121 JMPENV *top_env;
122 coro_context cctx; 122 coro_context cctx;
123 123
124 int inuse;
125
124#if USE_VALGRIND 126#if USE_VALGRIND
125 int valgrind_id; 127 int valgrind_id;
126#endif 128#endif
127} coro_cctx; 129} coro_cctx;
130
131enum {
132 CF_RUNNING, /* coroutine is running */
133 CF_READY, /* coroutine is ready */
134};
128 135
129/* this is a structure representing a perl-level coroutine */ 136/* this is a structure representing a perl-level coroutine */
130struct coro { 137struct coro {
131 /* the c coroutine allocated to this perl coroutine, if any */ 138 /* the c coroutine allocated to this perl coroutine, if any */
132 coro_cctx *cctx; 139 coro_cctx *cctx;
133 140
134 /* data associated with this coroutine (initial args) */ 141 /* data associated with this coroutine (initial args) */
135 AV *args; 142 AV *args;
136 int refcnt; 143 int refcnt;
144 int flags;
137 145
138 /* optionally saved, might be zero */ 146 /* optionally saved, might be zero */
139 AV *defav; 147 AV *defav;
140 SV *defsv; 148 SV *defsv;
141 SV *errsv; 149 SV *errsv;
665 if (flags == TRANSFER_SET_STACKLEVEL) 673 if (flags == TRANSFER_SET_STACKLEVEL)
666 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
667 else if (prev != next) 675 else if (prev != next)
668 { 676 {
669 coro_cctx *prev__cctx; 677 coro_cctx *prev__cctx;
678
679 if (!prev->cctx)
680 {
681 /* create a new empty context */
682 Newz (0, prev->cctx, 1, coro_cctx);
683 prev->cctx->inuse = 1;
684 prev->flags |= CF_RUNNING;
685 }
686
687 if (!prev->flags & CF_RUNNING)
688 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
689
690 if (next->flags & CF_RUNNING)
691 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
692
693 prev->flags &= ~CF_RUNNING;
694 next->flags |= CF_RUNNING;
670 695
671 LOCK; 696 LOCK;
672 697
673 if (next->mainstack) 698 if (next->mainstack)
674 { 699 {
681 /* need to start coroutine */ 706 /* need to start coroutine */
682 /* first get rid of the old state */ 707 /* first get rid of the old state */
683 SAVE (prev, -1); 708 SAVE (prev, -1);
684 /* setup coroutine call */ 709 /* setup coroutine call */
685 setup_coro (next); 710 setup_coro (next);
686 /* need a stack */ 711 /* need a new stack */
687 next->cctx = 0; 712 assert (!next->stack);
688 } 713 }
689
690 if (!prev->cctx)
691 /* create a new empty context */
692 Newz (0, prev->cctx, 1, coro_cctx);
693 714
694 prev__cctx = prev->cctx; 715 prev__cctx = prev->cctx;
695 716
696 /* possibly "free" the cctx */ 717 /* possibly "free" the cctx */
697 if (prev__cctx->idle_sp == STACKLEVEL) 718 if (prev__cctx->idle_sp == STACKLEVEL)
698 { 719 {
720 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
721 assert (PL_top_env == prev__cctx->top_env);
722
699 cctx_put (prev__cctx); 723 cctx_put (prev__cctx);
700 prev->cctx = 0; 724 prev->cctx = 0;
701 } 725 }
702 726
703 if (!next->cctx) 727 if (!next->cctx)
704 next->cctx = cctx_get (); 728 next->cctx = cctx_get ();
705 729
706 if (prev__cctx != next->cctx) 730 if (prev__cctx != next->cctx)
707 { 731 {
732 assert ( prev__cctx->inuse);
733 assert (!next->cctx->inuse);
734
735 prev__cctx->inuse = 0;
736 next->cctx->inuse = 1;
737
708 prev__cctx->top_env = PL_top_env; 738 prev__cctx->top_env = PL_top_env;
709 PL_top_env = next->cctx->top_env; 739 PL_top_env = next->cctx->top_env;
710 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 740 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
711 } 741 }
712 742
727static void 757static void
728coro_state_destroy (struct coro *coro) 758coro_state_destroy (struct coro *coro)
729{ 759{
730 if (coro->refcnt--) 760 if (coro->refcnt--)
731 return; 761 return;
762
763 if (coro->flags & CF_RUNNING)
764 croak ("FATAL: tried to destroy currently running coroutine");
732 765
733 if (coro->mainstack && coro->mainstack != main_mainstack) 766 if (coro->mainstack && coro->mainstack != main_mainstack)
734 { 767 {
735 struct coro temp; 768 struct coro temp;
736 769
834static GV *coro_current, *coro_idle; 867static GV *coro_current, *coro_idle;
835static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 868static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
836static int coro_nready; 869static int coro_nready;
837 870
838static void 871static void
839coro_enq (SV *sv) 872coro_enq (SV *coro_sv)
840{ 873{
841 int prio;
842
843 if (SvTYPE (sv) != SVt_PVHV)
844 croak ("Coro::ready tried to enqueue something that is not a coroutine");
845
846 prio = SvSTATE (sv)->prio;
847
848 av_push (coro_ready [prio - PRIO_MIN], sv); 874 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
849 coro_nready++; 875 coro_nready++;
850} 876}
851 877
852static SV * 878static SV *
853coro_deq (int min_prio) 879coro_deq (int min_prio)
866 } 892 }
867 893
868 return 0; 894 return 0;
869} 895}
870 896
871static void 897static int
872api_ready (SV *coro) 898api_ready (SV *coro_sv)
873{ 899{
874 dTHX; 900 struct coro *coro;
875 901
876 if (SvROK (coro)) 902 if (SvROK (coro_sv))
877 coro = SvRV (coro); 903 coro_sv = SvRV (coro_sv);
904
905 coro = SvSTATE (coro_sv);
906
907 if (coro->flags & CF_READY)
908 return 0;
909
910 if (coro->flags & CF_RUNNING)
911 croak ("Coro::ready called on currently running coroutine");
912
913 coro->flags |= CF_READY;
878 914
879 LOCK; 915 LOCK;
880 coro_enq (SvREFCNT_inc (coro)); 916 coro_enq (SvREFCNT_inc (coro_sv));
881 UNLOCK; 917 UNLOCK;
918
919 return 1;
920}
921
922static int
923api_is_ready (SV *coro_sv)
924{
925 return !!SvSTATE (coro_sv)->flags & CF_READY;
882} 926}
883 927
884static void 928static void
885prepare_schedule (struct transfer_args *ta) 929prepare_schedule (struct transfer_args *ta)
886{ 930{
922 coro_mortal = prev; 966 coro_mortal = prev;
923 967
924 ta->prev = SvSTATE (prev); 968 ta->prev = SvSTATE (prev);
925 ta->next = SvSTATE (next); 969 ta->next = SvSTATE (next);
926 ta->flags = TRANSFER_SAVE_ALL; 970 ta->flags = TRANSFER_SAVE_ALL;
971
972 ta->next->flags &= ~CF_READY;
927} 973}
928 974
929static void 975static void
930prepare_cede (struct transfer_args *ta) 976prepare_cede (struct transfer_args *ta)
931{ 977{
932 LOCK; 978 api_ready (GvSV (coro_current));
933 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
934 UNLOCK;
935 979
936 prepare_schedule (ta); 980 prepare_schedule (ta);
937} 981}
938 982
939static void 983static void
1125 SV *sv = perl_get_sv("Coro::API", 1); 1169 SV *sv = perl_get_sv("Coro::API", 1);
1126 1170
1127 coroapi.schedule = api_schedule; 1171 coroapi.schedule = api_schedule;
1128 coroapi.cede = api_cede; 1172 coroapi.cede = api_cede;
1129 coroapi.ready = api_ready; 1173 coroapi.ready = api_ready;
1174 coroapi.is_ready = api_is_ready;
1130 coroapi.nready = &coro_nready; 1175 coroapi.nready = &coro_nready;
1131 coroapi.current = coro_current; 1176 coroapi.current = coro_current;
1132 1177
1133 GCoroAPI = &coroapi; 1178 GCoroAPI = &coroapi;
1134 sv_setiv (sv, (IV)&coroapi); 1179 sv_setiv (sv, (IV)&coroapi);
1154 1199
1155 coro->prio = newprio; 1200 coro->prio = newprio;
1156 } 1201 }
1157} 1202}
1158 1203
1159void 1204SV *
1160ready (SV *self) 1205ready (SV *self)
1161 PROTOTYPE: $ 1206 PROTOTYPE: $
1162 CODE: 1207 CODE:
1163 api_ready (self); 1208 RETVAL = boolSV (api_ready (self));
1209 OUTPUT:
1210 RETVAL
1211
1212SV *
1213is_ready (SV *self)
1214 PROTOTYPE: $
1215 CODE:
1216 RETVAL = boolSV (api_is_ready (self));
1217 OUTPUT:
1218 RETVAL
1164 1219
1165int 1220int
1166nready (...) 1221nready (...)
1167 PROTOTYPE: 1222 PROTOTYPE:
1168 CODE: 1223 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines