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.318 by root, Thu Nov 20 07:02:43 2008 UTC vs.
Revision 1.323 by root, Sat Nov 22 06:03:10 2008 UTC

101# define CvISXSUB_on(cv) (void)cv 101# define CvISXSUB_on(cv) (void)cv
102#endif 102#endif
103#ifndef CvISXSUB 103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) 104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif 105#endif
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
106 109
107/* 5.8.7 */ 110/* 5.8.7 */
108#ifndef SvRV_set 111#ifndef SvRV_set
109# define SvRV_set(s,v) SvRV(s) = (v) 112# define SvRV_set(s,v) SvRV(s) = (v)
110#endif 113#endif
225 int valgrind_id; 228 int valgrind_id;
226#endif 229#endif
227 unsigned char flags; 230 unsigned char flags;
228} coro_cctx; 231} coro_cctx;
229 232
233coro_cctx *cctx_current; /* the currently running cctx */
234
235/*****************************************************************************/
236
230enum { 237enum {
231 CF_RUNNING = 0x0001, /* coroutine is running */ 238 CF_RUNNING = 0x0001, /* coroutine is running */
232 CF_READY = 0x0002, /* coroutine is ready */ 239 CF_READY = 0x0002, /* coroutine is ready */
233 CF_NEW = 0x0004, /* has never been switched to */ 240 CF_NEW = 0x0004, /* has never been switched to */
234 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 241 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
347 GV *gvp; 354 GV *gvp;
348 return sv_2cv (sv, &st, &gvp, 0); 355 return sv_2cv (sv, &st, &gvp, 0);
349} 356}
350 357
351static AV * 358static AV *
352coro_clone_padlist (pTHX_ CV *cv) 359coro_derive_padlist (pTHX_ CV *cv)
353{ 360{
354 AV *padlist = CvPADLIST (cv); 361 AV *padlist = CvPADLIST (cv);
355 AV *newpadlist, *newpad; 362 AV *newpadlist, *newpad;
356 363
357 newpadlist = newAV (); 364 newpadlist = newAV ();
477 CV *cp = Perl_cv_clone (aTHX_ cv); 484 CV *cp = Perl_cv_clone (aTHX_ cv);
478 CvPADLIST (cv) = CvPADLIST (cp); 485 CvPADLIST (cv) = CvPADLIST (cp);
479 CvPADLIST (cp) = 0; 486 CvPADLIST (cp) = 0;
480 SvREFCNT_dec (cp); 487 SvREFCNT_dec (cp);
481#else 488#else
482 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 489 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
483#endif 490#endif
484 } 491 }
485} 492}
486 493
487static void 494static void
901 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 908 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
902 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 909 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
903 910
904 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 911 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
905 coro_setup_op.op_next = PL_op; 912 coro_setup_op.op_next = PL_op;
906 coro_setup_op.op_type = OP_CUSTOM; 913 coro_setup_op.op_type = OP_ENTERSUB;
907 coro_setup_op.op_ppaddr = pp_slf; 914 coro_setup_op.op_ppaddr = pp_slf;
908 /* no flags etc. required, as an init function won't be called */ 915 /* no flags etc. required, as an init function won't be called */
909 916
910 PL_op = (OP *)&coro_setup_op; 917 PL_op = (OP *)&coro_setup_op;
911 918
964static int 971static int
965runops_trace (pTHX) 972runops_trace (pTHX)
966{ 973{
967 COP *oldcop = 0; 974 COP *oldcop = 0;
968 int oldcxix = -2; 975 int oldcxix = -2;
969 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
970 coro_cctx *cctx = coro->cctx;
971 976
972 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 977 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
973 { 978 {
974 PERL_ASYNC_CHECK (); 979 PERL_ASYNC_CHECK ();
975 980
976 if (cctx->flags & CC_TRACE_ALL) 981 if (cctx_current->flags & CC_TRACE_ALL)
977 { 982 {
978 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 983 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
979 { 984 {
980 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 985 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
981 SV **bot, **top; 986 SV **bot, **top;
982 AV *av = newAV (); /* return values */ 987 AV *av = newAV (); /* return values */
983 SV **cb; 988 SV **cb;
1020 1025
1021 if (PL_curcop != &PL_compiling) 1026 if (PL_curcop != &PL_compiling)
1022 { 1027 {
1023 SV **cb; 1028 SV **cb;
1024 1029
1025 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1030 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1026 { 1031 {
1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1032 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1028 1033
1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1034 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1030 { 1035 {
1031 runops_proc_t old_runops = PL_runops;
1032 dSP; 1036 dSP;
1033 GV *gv = CvGV (cx->blk_sub.cv); 1037 GV *gv = CvGV (cx->blk_sub.cv);
1034 SV *fullname = sv_2mortal (newSV (0)); 1038 SV *fullname = sv_2mortal (newSV (0));
1035 1039
1036 if (isGV (gv)) 1040 if (isGV (gv))
1054 } 1058 }
1055 1059
1056 oldcxix = cxstack_ix; 1060 oldcxix = cxstack_ix;
1057 } 1061 }
1058 1062
1059 if (cctx->flags & CC_TRACE_LINE) 1063 if (cctx_current->flags & CC_TRACE_LINE)
1060 { 1064 {
1061 dSP; 1065 dSP;
1062 1066
1063 PL_runops = RUNOPS_DEFAULT; 1067 PL_runops = RUNOPS_DEFAULT;
1064 ENTER; 1068 ENTER;
1083 1087
1084 TAINT_NOT; 1088 TAINT_NOT;
1085 return 0; 1089 return 0;
1086} 1090}
1087 1091
1088static struct coro_cctx *cctx_ssl_cctx;
1089static struct CoroSLF cctx_ssl_frame; 1092static struct CoroSLF cctx_ssl_frame;
1090 1093
1091static void 1094static void
1092slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1095slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1093{ 1096{
1094 ta->prev = (struct coro *)cctx_ssl_cctx;
1095 ta->next = 0; 1097 ta->prev = 0;
1096} 1098}
1097 1099
1098static int 1100static int
1099slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1101slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1100{ 1102{
1103 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1105 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1104} 1106}
1105 1107
1106/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1108/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1107static void NOINLINE 1109static void NOINLINE
1108cctx_prepare (pTHX_ coro_cctx *cctx) 1110cctx_prepare (pTHX)
1109{ 1111{
1110 PL_top_env = &PL_start_env; 1112 PL_top_env = &PL_start_env;
1111 1113
1112 if (cctx->flags & CC_TRACE) 1114 if (cctx_current->flags & CC_TRACE)
1113 PL_runops = runops_trace; 1115 PL_runops = runops_trace;
1114 1116
1115 /* we already must be executing an SLF op, there is no other valid way 1117 /* we already must be executing an SLF op, there is no other valid way
1116 * that can lead to creation of a new cctx */ 1118 * that can lead to creation of a new cctx */
1117 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1119 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1118 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1120 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1119 1121
1120 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1122 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1121 cctx_ssl_cctx = cctx;
1122 cctx_ssl_frame = slf_frame; 1123 cctx_ssl_frame = slf_frame;
1123 1124
1124 slf_frame.prepare = slf_prepare_set_stacklevel; 1125 slf_frame.prepare = slf_prepare_set_stacklevel;
1125 slf_frame.check = slf_check_set_stacklevel; 1126 slf_frame.check = slf_check_set_stacklevel;
1126} 1127}
1149 /* normally we would need to skip the entersub here */ 1150 /* normally we would need to skip the entersub here */
1150 /* not doing so will re-execute it, which is exactly what we want */ 1151 /* not doing so will re-execute it, which is exactly what we want */
1151 /* PL_nop = PL_nop->op_next */ 1152 /* PL_nop = PL_nop->op_next */
1152 1153
1153 /* inject a fake subroutine call to cctx_init */ 1154 /* inject a fake subroutine call to cctx_init */
1154 cctx_prepare (aTHX_ (coro_cctx *)arg); 1155 cctx_prepare (aTHX);
1155 1156
1156 /* cctx_run is the alternative tail of transfer() */ 1157 /* cctx_run is the alternative tail of transfer() */
1157 transfer_tail (aTHX); 1158 transfer_tail (aTHX);
1158 1159
1159 /* somebody or something will hit me for both perl_run and PL_restartop */ 1160 /* somebody or something will hit me for both perl_run and PL_restartop */
1254cctx_destroy (coro_cctx *cctx) 1255cctx_destroy (coro_cctx *cctx)
1255{ 1256{
1256 if (!cctx) 1257 if (!cctx)
1257 return; 1258 return;
1258 1259
1260 assert (cctx != cctx_current);//D temporary
1261
1259 --cctx_count; 1262 --cctx_count;
1260 coro_destroy (&cctx->cctx); 1263 coro_destroy (&cctx->cctx);
1261 1264
1262 /* coro_transfer creates new, empty cctx's */ 1265 /* coro_transfer creates new, empty cctx's */
1263 if (cctx->sptr) 1266 if (cctx->sptr)
1348transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1351transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1349{ 1352{
1350 dSTACKLEVEL; 1353 dSTACKLEVEL;
1351 1354
1352 /* sometimes transfer is only called to set idle_sp */ 1355 /* sometimes transfer is only called to set idle_sp */
1353 if (expect_false (!next)) 1356 if (expect_false (!prev))
1354 { 1357 {
1355 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1358 cctx_current->idle_sp = STACKLEVEL;
1356 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1359 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1357 } 1360 }
1358 else if (expect_true (prev != next)) 1361 else if (expect_true (prev != next))
1359 { 1362 {
1360 coro_cctx *prev__cctx; 1363 coro_cctx *cctx_prev;
1361 1364
1362 if (expect_false (prev->flags & CF_NEW)) 1365 if (expect_false (prev->flags & CF_NEW))
1363 { 1366 {
1364 /* create a new empty/source context */ 1367 /* create a new empty/source context */
1365 prev->cctx = cctx_new_empty ();
1366 prev->flags &= ~CF_NEW; 1368 prev->flags &= ~CF_NEW;
1367 prev->flags |= CF_RUNNING; 1369 prev->flags |= CF_RUNNING;
1368 } 1370 }
1369 1371
1370 prev->flags &= ~CF_RUNNING; 1372 prev->flags &= ~CF_RUNNING;
1381 coro_setup (aTHX_ next); 1383 coro_setup (aTHX_ next);
1382 } 1384 }
1383 else 1385 else
1384 load_perl (aTHX_ next); 1386 load_perl (aTHX_ next);
1385 1387
1386 prev__cctx = prev->cctx;
1387
1388 /* possibly untie and reuse the cctx */ 1388 /* possibly untie and reuse the cctx */
1389 if (expect_true ( 1389 if (expect_true (
1390 prev__cctx->idle_sp == STACKLEVEL 1390 cctx_current->idle_sp == STACKLEVEL
1391 && !(prev__cctx->flags & CC_TRACE) 1391 && !(cctx_current->flags & CC_TRACE)
1392 && !force_cctx 1392 && !force_cctx
1393 )) 1393 ))
1394 { 1394 {
1395 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1395 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1396 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1396 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1397
1398 prev->cctx = 0;
1399 1397
1400 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1398 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1401 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1399 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1402 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1400 if (expect_false (CCTX_EXPIRED (cctx_current)))
1403 if (!next->cctx) 1401 if (!next->cctx)
1404 next->cctx = cctx_get (aTHX); 1402 next->cctx = cctx_get (aTHX);
1405 1403
1406 cctx_put (prev__cctx); 1404 cctx_put (cctx_current);
1405 assert (!prev->cctx);//D temporary
1407 } 1406 }
1407 else
1408 prev->cctx = cctx_current;
1408 1409
1409 ++next->usecount; 1410 ++next->usecount;
1410 1411
1411 if (expect_true (!next->cctx)) 1412 cctx_prev = cctx_current;
1412 next->cctx = cctx_get (aTHX); 1413 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1413 1414
1414 if (expect_false (prev__cctx != next->cctx)) 1415 next->cctx = 0;
1416
1417 if (expect_false (cctx_prev != cctx_current))
1415 { 1418 {
1416 prev__cctx->top_env = PL_top_env; 1419 cctx_prev->top_env = PL_top_env;
1417 PL_top_env = next->cctx->top_env; 1420 PL_top_env = cctx_current->top_env;
1418 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1421 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1419 } 1422 }
1420 1423
1421 transfer_tail (aTHX); 1424 transfer_tail (aTHX);
1422 } 1425 }
1423} 1426}
1657 else 1660 else
1658 { 1661 {
1659 next->flags &= ~CF_READY; 1662 next->flags &= ~CF_READY;
1660 --coro_nready; 1663 --coro_nready;
1661 1664
1662 return prepare_schedule_to (aTHX_ ta, next); 1665 prepare_schedule_to (aTHX_ ta, next);
1666 break;
1663 } 1667 }
1664 } 1668 }
1665 else 1669 else
1666 { 1670 {
1667 /* nothing to schedule: call the idle handler */ 1671 /* nothing to schedule: call the idle handler */
2250 } 2254 }
2251 else 2255 else
2252 slf_argc = 0; 2256 slf_argc = 0;
2253 2257
2254 PL_op->op_ppaddr = pp_slf; 2258 PL_op->op_ppaddr = pp_slf;
2255 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2259 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2256 2260
2257 PL_op = (OP *)&slf_restore; 2261 PL_op = (OP *)&slf_restore;
2258} 2262}
2259 2263
2260/*****************************************************************************/ 2264/*****************************************************************************/
2724 XSRETURN_EMPTY; 2728 XSRETURN_EMPTY;
2725} 2729}
2726 2730
2727/*****************************************************************************/ 2731/*****************************************************************************/
2728 2732
2733#if CORO_CLONE
2734# include "clone.c"
2735#endif
2736
2729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2737MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2730 2738
2731PROTOTYPES: DISABLE 2739PROTOTYPES: DISABLE
2732 2740
2733BOOT: 2741BOOT:
2737 coro_thx = PERL_GET_CONTEXT; 2745 coro_thx = PERL_GET_CONTEXT;
2738# endif 2746# endif
2739#endif 2747#endif
2740 BOOT_PAGESIZE; 2748 BOOT_PAGESIZE;
2741 2749
2750 cctx_current = cctx_new_empty ();
2751
2742 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2752 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2743 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2753 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2744 2754
2745 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2755 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2746 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2756 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2765 2775
2766 { 2776 {
2767 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2777 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2768 2778
2769 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2779 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2770 hv_store_ent (PL_custom_op_names, slf, 2780 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2771 newSVpv ("coro_slf", 0), 0);
2772 2781
2773 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2782 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2774 hv_store_ent (PL_custom_op_descs, slf, 2783 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2775 newSVpv ("coro schedule like function", 0), 0);
2776 } 2784 }
2777 2785
2778 coroapi.ver = CORO_API_VERSION; 2786 coroapi.ver = CORO_API_VERSION;
2779 coroapi.rev = CORO_API_REVISION; 2787 coroapi.rev = CORO_API_REVISION;
2780 2788
2873void 2881void
2874_exit (int code) 2882_exit (int code)
2875 PROTOTYPE: $ 2883 PROTOTYPE: $
2876 CODE: 2884 CODE:
2877 _exit (code); 2885 _exit (code);
2886
2887SV *
2888clone (Coro::State coro)
2889 CODE:
2890{
2891#if CORO_CLONE
2892 struct coro *ncoro = coro_clone (coro);
2893 MAGIC *mg;
2894 /* TODO: too much duplication */
2895 ncoro->hv = newHV ();
2896 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2897 mg->mg_flags |= MGf_DUP;
2898 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2899#else
2900 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2901#endif
2902}
2903 OUTPUT:
2904 RETVAL
2878 2905
2879int 2906int
2880cctx_stacksize (int new_stacksize = 0) 2907cctx_stacksize (int new_stacksize = 0)
2881 PROTOTYPE: ;$ 2908 PROTOTYPE: ;$
2882 CODE: 2909 CODE:
3003 3030
3004SV * 3031SV *
3005has_cctx (Coro::State coro) 3032has_cctx (Coro::State coro)
3006 PROTOTYPE: $ 3033 PROTOTYPE: $
3007 CODE: 3034 CODE:
3008 RETVAL = boolSV (!!coro->cctx); 3035 /* maybe manage the running flag differently */
3036 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3009 OUTPUT: 3037 OUTPUT:
3010 RETVAL 3038 RETVAL
3011 3039
3012int 3040int
3013is_traced (Coro::State coro) 3041is_traced (Coro::State coro)
3033 3061
3034void 3062void
3035force_cctx () 3063force_cctx ()
3036 PROTOTYPE: 3064 PROTOTYPE:
3037 CODE: 3065 CODE:
3038 SvSTATE_current->cctx->idle_sp = 0; 3066 cctx_current->idle_sp = 0;
3039 3067
3040void 3068void
3041swap_defsv (Coro::State self) 3069swap_defsv (Coro::State self)
3042 PROTOTYPE: $ 3070 PROTOTYPE: $
3043 ALIAS: 3071 ALIAS:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines