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.130 by root, Mon Dec 25 13:25:55 2006 UTC vs.
Revision 1.136 by root, Fri Jan 12 01:05:55 2007 UTC

93 93
94#define IN_DESTRUCT (PL_main_cv == Nullcv) 94#define IN_DESTRUCT (PL_main_cv == Nullcv)
95 95
96#if __GNUC__ >= 3 96#if __GNUC__ >= 3
97# define attribute(x) __attribute__(x) 97# define attribute(x) __attribute__(x)
98# define BARRIER __asm__ __volatile__ ("" : : : "memory")
98#else 99#else
99# define attribute(x) 100# define attribute(x)
101# define BARRIER
100#endif 102#endif
101 103
102#define NOINLINE attribute ((noinline)) 104#define NOINLINE attribute ((noinline))
103 105
104#include "CoroAPI.h" 106#include "CoroAPI.h"
110#else 112#else
111# define LOCK (void)0 113# define LOCK (void)0
112# define UNLOCK (void)0 114# define UNLOCK (void)0
113#endif 115#endif
114 116
117/* helper storage struct for Coro::AIO */
115struct io_state 118struct io_state
116{ 119{
117 int errorno; 120 int errorno;
118 I32 laststype; 121 I32 laststype;
119 int laststatval; 122 int laststatval;
135 /* the stack */ 138 /* the stack */
136 void *sptr; 139 void *sptr;
137 long ssize; /* positive == mmap, otherwise malloc */ 140 long ssize; /* positive == mmap, otherwise malloc */
138 141
139 /* cpu state */ 142 /* cpu state */
140 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 143 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
144 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
141 JMPENV *top_env; 145 JMPENV *top_env;
142 coro_context cctx; 146 coro_context cctx;
143 147
144 int inuse; 148 int inuse;
145 149
147 int valgrind_id; 151 int valgrind_id;
148#endif 152#endif
149} coro_cctx; 153} coro_cctx;
150 154
151enum { 155enum {
152 CF_RUNNING = 0x0001, /* coroutine is running */ 156 CF_RUNNING = 0x0001, /* coroutine is running */
153 CF_READY = 0x0002, /* coroutine is ready */ 157 CF_READY = 0x0002, /* coroutine is ready */
154 CF_NEW = 0x0004, /* ahs never been switched to */ 158 CF_NEW = 0x0004, /* has never been switched to */
159 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
155}; 160};
156 161
157/* this is a structure representing a perl-level coroutine */ 162/* this is a structure representing a perl-level coroutine */
158struct coro { 163struct coro {
159 /* the c coroutine allocated to this perl coroutine, if any */ 164 /* the c coroutine allocated to this perl coroutine, if any */
468 473
469 /* free all temporaries */ 474 /* free all temporaries */
470 FREETMPS; 475 FREETMPS;
471 assert (PL_tmps_ix == -1); 476 assert (PL_tmps_ix == -1);
472 477
478 /* unwind all extra stacks */
473 POPSTACK_TO (PL_mainstack); 479 POPSTACK_TO (PL_mainstack);
480
481 /* unwind main stack */
482 dounwind (-1);
474 } 483 }
475 484
476 while (PL_curstackinfo->si_next) 485 while (PL_curstackinfo->si_next)
477 PL_curstackinfo = PL_curstackinfo->si_next; 486 PL_curstackinfo = PL_curstackinfo->si_next;
478 487
706 715
707 /* sometimes transfer is only called to set idle_sp */ 716 /* sometimes transfer is only called to set idle_sp */
708 if (!next) 717 if (!next)
709 { 718 {
710 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 719 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
711 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ 720 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
712 } 721 }
713 else if (prev != next) 722 else if (prev != next)
714 { 723 {
715 coro_cctx *prev__cctx; 724 coro_cctx *prev__cctx;
716 725
727 if (!prev->flags & CF_RUNNING) 736 if (!prev->flags & CF_RUNNING)
728 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 737 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
729 738
730 if (next->flags & CF_RUNNING) 739 if (next->flags & CF_RUNNING)
731 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 740 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
741
742 if (next->flags & CF_DESTROYED)
743 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
732 744
733 prev->flags &= ~CF_RUNNING; 745 prev->flags &= ~CF_RUNNING;
734 next->flags |= CF_RUNNING; 746 next->flags |= CF_RUNNING;
735 747
736 LOCK; 748 LOCK;
757 769
758 /* possibly "free" the cctx */ 770 /* possibly "free" the cctx */
759 if (prev__cctx->idle_sp == STACKLEVEL) 771 if (prev__cctx->idle_sp == STACKLEVEL)
760 { 772 {
761 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 773 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
762 assert (PL_top_env == prev__cctx->top_env); 774 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
763 775
764 prev->cctx = 0; 776 prev->cctx = 0;
765 777
766 cctx_put (prev__cctx); 778 cctx_put (prev__cctx);
767 prev__cctx->inuse = 0; 779 prev__cctx->inuse = 0;
780 PL_top_env = next->cctx->top_env; 792 PL_top_env = next->cctx->top_env;
781 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 793 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
782 } 794 }
783 795
784 free_coro_mortal (); 796 free_coro_mortal ();
785
786 UNLOCK; 797 UNLOCK;
787 } 798 }
788} 799}
789 800
790struct transfer_args 801struct transfer_args
792 struct coro *prev, *next; 803 struct coro *prev, *next;
793}; 804};
794 805
795#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 806#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
796 807
797static void 808static int
798coro_state_destroy (struct coro *coro) 809coro_state_destroy (struct coro *coro)
799{ 810{
800 if (coro->refcnt--) 811 if (coro->flags & CF_DESTROYED)
801 return; 812 return 0;
813
814 coro->flags |= CF_DESTROYED;
802 815
803 if (coro->mainstack && coro->mainstack != main_mainstack) 816 if (coro->mainstack && coro->mainstack != main_mainstack)
804 { 817 {
818 assert (!(coro->flags & CF_RUNNING));
819
805 struct coro temp; 820 struct coro temp;
806 Zero (&temp, 1, struct coro); 821 Zero (&temp, 1, struct coro);
807 temp.save = CORO_SAVE_ALL; 822 temp.save = CORO_SAVE_ALL;
808 823
809 if (coro->flags & CF_RUNNING) 824 if (coro->flags & CF_RUNNING)
819 coro->mainstack = 0; 834 coro->mainstack = 0;
820 } 835 }
821 836
822 cctx_destroy (coro->cctx); 837 cctx_destroy (coro->cctx);
823 SvREFCNT_dec (coro->args); 838 SvREFCNT_dec (coro->args);
824 Safefree (coro); 839
840 return 1;
825} 841}
826 842
827static int 843static int
828coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 844coro_state_free (pTHX_ SV *sv, MAGIC *mg)
829{ 845{
830 struct coro *coro = (struct coro *)mg->mg_ptr; 846 struct coro *coro = (struct coro *)mg->mg_ptr;
831 mg->mg_ptr = 0; 847 mg->mg_ptr = 0;
832 848
849 if (--coro->refcnt < 0)
850 {
833 coro_state_destroy (coro); 851 coro_state_destroy (coro);
852 Safefree (coro);
853 }
834 854
835 return 0; 855 return 0;
836} 856}
837 857
838static int 858static int
845 return 0; 865 return 0;
846} 866}
847 867
848static MGVTBL coro_state_vtbl = { 868static MGVTBL coro_state_vtbl = {
849 0, 0, 0, 0, 869 0, 0, 0, 0,
850 coro_state_clear, 870 coro_state_free,
851 0, 871 0,
852#ifdef MGf_DUP 872#ifdef MGf_DUP
853 coro_state_dup, 873 coro_state_dup,
854#else 874#else
855# define MGf_DUP 0 875# define MGf_DUP 0
975} 995}
976 996
977static void 997static void
978prepare_schedule (struct transfer_args *ta) 998prepare_schedule (struct transfer_args *ta)
979{ 999{
980 SV *prev, *next; 1000 SV *prev_sv, *next_sv;
981 1001
982 for (;;) 1002 for (;;)
983 { 1003 {
984 LOCK; 1004 LOCK;
985 next = coro_deq (PRIO_MIN); 1005 next_sv = coro_deq (PRIO_MIN);
986 UNLOCK; 1006 UNLOCK;
987 1007
1008 /* nothing to schedule: call the idle handler */
988 if (next) 1009 if (!next_sv)
989 break;
990
991 { 1010 {
992 dSP; 1011 dSP;
993 1012
994 ENTER; 1013 ENTER;
995 SAVETMPS; 1014 SAVETMPS;
996 1015
997 PUSHMARK (SP); 1016 PUSHMARK (SP);
998 PUTBACK; 1017 PUTBACK;
999 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1018 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1000 1019
1001 FREETMPS; 1020 FREETMPS;
1002 LEAVE; 1021 LEAVE;
1022 continue;
1003 } 1023 }
1004 }
1005 1024
1006 prev = SvRV (coro_current); 1025 ta->next = SvSTATE (next_sv);
1007 SvRV_set (coro_current, next); 1026
1027 /* cannot transfer to destroyed coros, skip and look for next */
1028 if (ta->next->flags & CF_DESTROYED)
1029 {
1030 SvREFCNT_dec (next_sv);
1031 continue;
1032 }
1033
1034 break;
1035 }
1008 1036
1009 /* free this only after the transfer */ 1037 /* free this only after the transfer */
1038 prev_sv = SvRV (coro_current);
1039 SvRV_set (coro_current, next_sv);
1040 ta->prev = SvSTATE (prev_sv);
1041
1042 assert (ta->next->flags & CF_READY);
1043 ta->next->flags &= ~CF_READY;
1044
1010 LOCK; 1045 LOCK;
1011 free_coro_mortal (); 1046 free_coro_mortal ();
1047 coro_mortal = prev_sv;
1012 UNLOCK; 1048 UNLOCK;
1013 coro_mortal = prev;
1014
1015 assert (!SvROK(prev));//D
1016 assert (!SvROK(next));//D
1017
1018 ta->prev = SvSTATE (prev);
1019 ta->next = SvSTATE (next);
1020
1021 assert (ta->next->flags & CF_READY);
1022 ta->next->flags &= ~CF_READY;
1023} 1049}
1024 1050
1025static void 1051static void
1026prepare_cede (struct transfer_args *ta) 1052prepare_cede (struct transfer_args *ta)
1027{ 1053{
1028 api_ready (coro_current); 1054 api_ready (coro_current);
1029
1030 prepare_schedule (ta); 1055 prepare_schedule (ta);
1056}
1057
1058static int
1059prepare_cede_notself (struct transfer_args *ta)
1060{
1061 if (coro_nready)
1062 {
1063 SV *prev = SvRV (coro_current);
1064 prepare_schedule (ta);
1065 api_ready (prev);
1066 return 1;
1067 }
1068 else
1069 return 0;
1031} 1070}
1032 1071
1033static void 1072static void
1034api_schedule (void) 1073api_schedule (void)
1035{ 1074{
1037 1076
1038 prepare_schedule (&ta); 1077 prepare_schedule (&ta);
1039 TRANSFER (ta); 1078 TRANSFER (ta);
1040} 1079}
1041 1080
1042static void 1081static int
1043api_cede (void) 1082api_cede (void)
1044{ 1083{
1045 struct transfer_args ta; 1084 struct transfer_args ta;
1046 1085
1047 prepare_cede (&ta); 1086 prepare_cede (&ta);
1087
1088 if (ta.prev != ta.next)
1089 {
1048 TRANSFER (ta); 1090 TRANSFER (ta);
1091 return 1;
1092 }
1093 else
1094 return 0;
1095}
1096
1097static int
1098api_cede_notself (void)
1099{
1100 struct transfer_args ta;
1101
1102 if (prepare_cede_notself (&ta))
1103 {
1104 TRANSFER (ta);
1105 return 1;
1106 }
1107 else
1108 return 0;
1049} 1109}
1050 1110
1051MODULE = Coro::State PACKAGE = Coro::State 1111MODULE = Coro::State PACKAGE = Coro::State
1052 1112
1053PROTOTYPES: DISABLE 1113PROTOTYPES: DISABLE
1109_set_stacklevel (...) 1169_set_stacklevel (...)
1110 ALIAS: 1170 ALIAS:
1111 Coro::State::transfer = 1 1171 Coro::State::transfer = 1
1112 Coro::schedule = 2 1172 Coro::schedule = 2
1113 Coro::cede = 3 1173 Coro::cede = 3
1174 Coro::cede_notself = 4
1114 CODE: 1175 CODE:
1115{ 1176{
1116 struct transfer_args ta; 1177 struct transfer_args ta;
1117 1178
1118 switch (ix) 1179 switch (ix)
1134 break; 1195 break;
1135 1196
1136 case 3: 1197 case 3:
1137 prepare_cede (&ta); 1198 prepare_cede (&ta);
1138 break; 1199 break;
1200
1201 case 4:
1202 if (!prepare_cede_notself (&ta))
1203 XSRETURN_EMPTY;
1204
1205 break;
1139 } 1206 }
1140 1207
1208 BARRIER;
1141 TRANSFER (ta); 1209 TRANSFER (ta);
1142}
1143 1210
1144void 1211 if (GIMME_V != G_VOID && ta.next != ta.prev)
1145_clone_state_from (SV *dst, SV *src) 1212 XSRETURN_YES;
1213}
1214
1215bool
1216_destroy (SV *coro_sv)
1146 CODE: 1217 CODE:
1147{ 1218 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1148 struct coro *coro_src = SvSTATE (src); 1219 OUTPUT:
1149 1220 RETVAL
1150 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1151
1152 ++coro_src->refcnt;
1153 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1154}
1155 1221
1156void 1222void
1157_exit (code) 1223_exit (code)
1158 int code 1224 int code
1159 PROTOTYPE: $ 1225 PROTOTYPE: $
1196 coro_ready[i] = newAV (); 1262 coro_ready[i] = newAV ();
1197 1263
1198 { 1264 {
1199 SV *sv = perl_get_sv("Coro::API", 1); 1265 SV *sv = perl_get_sv("Coro::API", 1);
1200 1266
1201 coroapi.schedule = api_schedule; 1267 coroapi.schedule = api_schedule;
1202 coroapi.save = api_save; 1268 coroapi.save = api_save;
1203 coroapi.cede = api_cede; 1269 coroapi.cede = api_cede;
1270 coroapi.cede_notself = api_cede_notself;
1204 coroapi.ready = api_ready; 1271 coroapi.ready = api_ready;
1205 coroapi.is_ready = api_is_ready; 1272 coroapi.is_ready = api_is_ready;
1206 coroapi.nready = &coro_nready; 1273 coroapi.nready = &coro_nready;
1207 coroapi.current = coro_current; 1274 coroapi.current = coro_current;
1208 1275
1209 GCoroAPI = &coroapi; 1276 GCoroAPI = &coroapi;
1210 sv_setiv (sv, (IV)&coroapi); 1277 sv_setiv (sv, (IV)&coroapi);
1211 SvREADONLY_on (sv); 1278 SvREADONLY_on (sv);
1212 } 1279 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines