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.312 by root, Thu Nov 20 03:10:30 2008 UTC vs.
Revision 1.324 by root, Sat Nov 22 07:02:28 2008 UTC

98# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
99#endif 99#endif
100#ifndef CvISXSUB_on 100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv 101# define CvISXSUB_on(cv) (void)cv
102#endif 102#endif
103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
103 109
104/* 5.8.7 */ 110/* 5.8.7 */
105#ifndef SvRV_set 111#ifndef SvRV_set
106# define SvRV_set(s,v) SvRV(s) = (v) 112# define SvRV_set(s,v) SvRV(s) = (v)
107#endif 113#endif
167static AV *main_mainstack; /* used to differentiate between $main and others */ 173static AV *main_mainstack; /* used to differentiate between $main and others */
168static JMPENV *main_top_env; 174static JMPENV *main_top_env;
169static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
170static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
171 177
178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */
180
172static GV *irsgv; /* $/ */ 181static GV *irsgv; /* $/ */
173static GV *stdoutgv; /* *STDOUT */ 182static GV *stdoutgv; /* *STDOUT */
174static SV *rv_diehook; 183static SV *rv_diehook;
175static SV *rv_warnhook; 184static SV *rv_warnhook;
176static HV *hv_sig; /* %SIG */ 185static HV *hv_sig; /* %SIG */
177 186
178/* async_pool helper stuff */ 187/* async_pool helper stuff */
179static SV *sv_pool_rss; 188static SV *sv_pool_rss;
180static SV *sv_pool_size; 189static SV *sv_pool_size;
181static SV *sv_async_pool_idle; 190static SV *sv_async_pool_idle; /* description string */
182static AV *av_async_pool; 191static AV *av_async_pool; /* idle pool */
183static SV *sv_Coro; 192static SV *sv_Coro; /* class string */
184static CV *cv_pool_handler; 193static CV *cv_pool_handler;
185static CV *cv_coro_new; 194static CV *cv_coro_state_new;
186 195
187/* Coro::AnyEvent */ 196/* Coro::AnyEvent */
188static SV *sv_activity; 197static SV *sv_activity;
189 198
190static struct coro_cctx *cctx_first; 199static struct coro_cctx *cctx_first;
219 int valgrind_id; 228 int valgrind_id;
220#endif 229#endif
221 unsigned char flags; 230 unsigned char flags;
222} coro_cctx; 231} coro_cctx;
223 232
233coro_cctx *cctx_current; /* the currently running cctx */
234
235/*****************************************************************************/
236
224enum { 237enum {
225 CF_RUNNING = 0x0001, /* coroutine is running */ 238 CF_RUNNING = 0x0001, /* coroutine is running */
226 CF_READY = 0x0002, /* coroutine is ready */ 239 CF_READY = 0x0002, /* coroutine is ready */
227 CF_NEW = 0x0004, /* has never been switched to */ 240 CF_NEW = 0x0004, /* has never been switched to */
228 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 241 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
341 GV *gvp; 354 GV *gvp;
342 return sv_2cv (sv, &st, &gvp, 0); 355 return sv_2cv (sv, &st, &gvp, 0);
343} 356}
344 357
345static AV * 358static AV *
346coro_clone_padlist (pTHX_ CV *cv) 359coro_derive_padlist (pTHX_ CV *cv)
347{ 360{
348 AV *padlist = CvPADLIST (cv); 361 AV *padlist = CvPADLIST (cv);
349 AV *newpadlist, *newpad; 362 AV *newpadlist, *newpad;
350 363
351 newpadlist = newAV (); 364 newpadlist = newAV ();
471 CV *cp = Perl_cv_clone (aTHX_ cv); 484 CV *cp = Perl_cv_clone (aTHX_ cv);
472 CvPADLIST (cv) = CvPADLIST (cp); 485 CvPADLIST (cv) = CvPADLIST (cp);
473 CvPADLIST (cp) = 0; 486 CvPADLIST (cp) = 0;
474 SvREFCNT_dec (cp); 487 SvREFCNT_dec (cp);
475#else 488#else
476 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 489 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
477#endif 490#endif
478 } 491 }
479} 492}
480 493
481static void 494static void
831slf_check_nop (pTHX_ struct CoroSLF *frame) 844slf_check_nop (pTHX_ struct CoroSLF *frame)
832{ 845{
833 return 0; 846 return 0;
834} 847}
835 848
849static int
850slf_check_repeat (pTHX_ struct CoroSLF *frame)
851{
852 return 1;
853}
854
836static UNOP coro_setup_op; 855static UNOP coro_setup_op;
837 856
838static void NOINLINE /* noinline to keep it out of the transfer fast path */ 857static void NOINLINE /* noinline to keep it out of the transfer fast path */
839coro_setup (pTHX_ struct coro *coro) 858coro_setup (pTHX_ struct coro *coro)
840{ 859{
889 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 */
890 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 */
891 910
892 /* 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 */
893 coro_setup_op.op_next = PL_op; 912 coro_setup_op.op_next = PL_op;
894 coro_setup_op.op_type = OP_CUSTOM; 913 coro_setup_op.op_type = OP_ENTERSUB;
895 coro_setup_op.op_ppaddr = pp_slf; 914 coro_setup_op.op_ppaddr = pp_slf;
896 /* 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 */
897 916
898 PL_op = (OP *)&coro_setup_op; 917 PL_op = (OP *)&coro_setup_op;
899 918
952static int 971static int
953runops_trace (pTHX) 972runops_trace (pTHX)
954{ 973{
955 COP *oldcop = 0; 974 COP *oldcop = 0;
956 int oldcxix = -2; 975 int oldcxix = -2;
957 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
958 coro_cctx *cctx = coro->cctx;
959 976
960 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 977 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
961 { 978 {
962 PERL_ASYNC_CHECK (); 979 PERL_ASYNC_CHECK ();
963 980
964 if (cctx->flags & CC_TRACE_ALL) 981 if (cctx_current->flags & CC_TRACE_ALL)
965 { 982 {
966 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)
967 { 984 {
968 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 985 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
969 SV **bot, **top; 986 SV **bot, **top;
970 AV *av = newAV (); /* return values */ 987 AV *av = newAV (); /* return values */
971 SV **cb; 988 SV **cb;
1008 1025
1009 if (PL_curcop != &PL_compiling) 1026 if (PL_curcop != &PL_compiling)
1010 { 1027 {
1011 SV **cb; 1028 SV **cb;
1012 1029
1013 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1030 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1014 { 1031 {
1015 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1032 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1016 1033
1017 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1034 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1018 { 1035 {
1019 runops_proc_t old_runops = PL_runops;
1020 dSP; 1036 dSP;
1021 GV *gv = CvGV (cx->blk_sub.cv); 1037 GV *gv = CvGV (cx->blk_sub.cv);
1022 SV *fullname = sv_2mortal (newSV (0)); 1038 SV *fullname = sv_2mortal (newSV (0));
1023 1039
1024 if (isGV (gv)) 1040 if (isGV (gv))
1042 } 1058 }
1043 1059
1044 oldcxix = cxstack_ix; 1060 oldcxix = cxstack_ix;
1045 } 1061 }
1046 1062
1047 if (cctx->flags & CC_TRACE_LINE) 1063 if (cctx_current->flags & CC_TRACE_LINE)
1048 { 1064 {
1049 dSP; 1065 dSP;
1050 1066
1051 PL_runops = RUNOPS_DEFAULT; 1067 PL_runops = RUNOPS_DEFAULT;
1052 ENTER; 1068 ENTER;
1071 1087
1072 TAINT_NOT; 1088 TAINT_NOT;
1073 return 0; 1089 return 0;
1074} 1090}
1075 1091
1076static struct coro_cctx *cctx_ssl_cctx;
1077static struct CoroSLF cctx_ssl_frame; 1092static struct CoroSLF cctx_ssl_frame;
1078 1093
1079static void 1094static void
1080slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1095slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1081{ 1096{
1082 ta->prev = (struct coro *)cctx_ssl_cctx;
1083 ta->next = 0; 1097 ta->prev = 0;
1084} 1098}
1085 1099
1086static int 1100static int
1087slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1101slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1088{ 1102{
1091 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 */
1092} 1106}
1093 1107
1094/* 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 */
1095static void NOINLINE 1109static void NOINLINE
1096cctx_prepare (pTHX_ coro_cctx *cctx) 1110cctx_prepare (pTHX)
1097{ 1111{
1098 PL_top_env = &PL_start_env; 1112 PL_top_env = &PL_start_env;
1099 1113
1100 if (cctx->flags & CC_TRACE) 1114 if (cctx_current->flags & CC_TRACE)
1101 PL_runops = runops_trace; 1115 PL_runops = runops_trace;
1102 1116
1103 /* 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
1104 * that can lead to creation of a new cctx */ 1118 * that can lead to creation of a new cctx */
1105 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)",
1106 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1120 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1107 1121
1108 /* 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 */
1109 cctx_ssl_cctx = cctx;
1110 cctx_ssl_frame = slf_frame; 1123 cctx_ssl_frame = slf_frame;
1111 1124
1112 slf_frame.prepare = slf_prepare_set_stacklevel; 1125 slf_frame.prepare = slf_prepare_set_stacklevel;
1113 slf_frame.check = slf_check_set_stacklevel; 1126 slf_frame.check = slf_check_set_stacklevel;
1114} 1127}
1137 /* normally we would need to skip the entersub here */ 1150 /* normally we would need to skip the entersub here */
1138 /* 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 */
1139 /* PL_nop = PL_nop->op_next */ 1152 /* PL_nop = PL_nop->op_next */
1140 1153
1141 /* inject a fake subroutine call to cctx_init */ 1154 /* inject a fake subroutine call to cctx_init */
1142 cctx_prepare (aTHX_ (coro_cctx *)arg); 1155 cctx_prepare (aTHX);
1143 1156
1144 /* cctx_run is the alternative tail of transfer() */ 1157 /* cctx_run is the alternative tail of transfer() */
1145 transfer_tail (aTHX); 1158 transfer_tail (aTHX);
1146 1159
1147 /* 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 */
1242cctx_destroy (coro_cctx *cctx) 1255cctx_destroy (coro_cctx *cctx)
1243{ 1256{
1244 if (!cctx) 1257 if (!cctx)
1245 return; 1258 return;
1246 1259
1260 assert (cctx != cctx_current);//D temporary
1261
1247 --cctx_count; 1262 --cctx_count;
1248 coro_destroy (&cctx->cctx); 1263 coro_destroy (&cctx->cctx);
1249 1264
1250 /* coro_transfer creates new, empty cctx's */ 1265 /* coro_transfer creates new, empty cctx's */
1251 if (cctx->sptr) 1266 if (cctx->sptr)
1336transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1351transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1337{ 1352{
1338 dSTACKLEVEL; 1353 dSTACKLEVEL;
1339 1354
1340 /* sometimes transfer is only called to set idle_sp */ 1355 /* sometimes transfer is only called to set idle_sp */
1341 if (expect_false (!next)) 1356 if (expect_false (!prev))
1342 { 1357 {
1343 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1358 cctx_current->idle_sp = STACKLEVEL;
1344 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 */
1345 } 1360 }
1346 else if (expect_true (prev != next)) 1361 else if (expect_true (prev != next))
1347 { 1362 {
1348 coro_cctx *prev__cctx; 1363 coro_cctx *cctx_prev;
1349 1364
1350 if (expect_false (prev->flags & CF_NEW)) 1365 if (expect_false (prev->flags & CF_NEW))
1351 { 1366 {
1352 /* create a new empty/source context */ 1367 /* create a new empty/source context */
1353 prev->cctx = cctx_new_empty ();
1354 prev->flags &= ~CF_NEW; 1368 prev->flags &= ~CF_NEW;
1355 prev->flags |= CF_RUNNING; 1369 prev->flags |= CF_RUNNING;
1356 } 1370 }
1357 1371
1358 prev->flags &= ~CF_RUNNING; 1372 prev->flags &= ~CF_RUNNING;
1369 coro_setup (aTHX_ next); 1383 coro_setup (aTHX_ next);
1370 } 1384 }
1371 else 1385 else
1372 load_perl (aTHX_ next); 1386 load_perl (aTHX_ next);
1373 1387
1374 prev__cctx = prev->cctx; 1388 assert (!prev->cctx);//D temporary
1375 1389
1376 /* possibly untie and reuse the cctx */ 1390 /* possibly untie and reuse the cctx */
1377 if (expect_true ( 1391 if (expect_true (
1378 prev__cctx->idle_sp == STACKLEVEL 1392 cctx_current->idle_sp == STACKLEVEL
1379 && !(prev__cctx->flags & CC_TRACE) 1393 && !(cctx_current->flags & CC_TRACE)
1380 && !force_cctx 1394 && !force_cctx
1381 )) 1395 ))
1382 { 1396 {
1383 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1397 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1384 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1398 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1385 1399
1386 prev->cctx = 0;
1387
1388 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1400 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1389 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1401 /* without this the next cctx_get might destroy the running cctx while still in use */
1390 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1402 if (expect_false (CCTX_EXPIRED (cctx_current)))
1391 if (!next->cctx) 1403 if (expect_true (!next->cctx))
1392 next->cctx = cctx_get (aTHX); 1404 next->cctx = cctx_get (aTHX);
1393 1405
1394 cctx_put (prev__cctx); 1406 cctx_put (cctx_current);
1395 } 1407 }
1408 else
1409 prev->cctx = cctx_current;
1396 1410
1397 ++next->usecount; 1411 ++next->usecount;
1398 1412
1399 if (expect_true (!next->cctx)) 1413 cctx_prev = cctx_current;
1400 next->cctx = cctx_get (aTHX); 1414 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1401 1415
1402 if (expect_false (prev__cctx != next->cctx)) 1416 next->cctx = 0;
1417
1418 if (expect_false (cctx_prev != cctx_current))
1403 { 1419 {
1404 prev__cctx->top_env = PL_top_env; 1420 cctx_prev->top_env = PL_top_env;
1405 PL_top_env = next->cctx->top_env; 1421 PL_top_env = cctx_current->top_env;
1406 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1422 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1407 } 1423 }
1408 1424
1409 transfer_tail (aTHX); 1425 transfer_tail (aTHX);
1410 } 1426 }
1411} 1427}
1609api_is_ready (pTHX_ SV *coro_sv) 1625api_is_ready (pTHX_ SV *coro_sv)
1610{ 1626{
1611 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1627 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1612} 1628}
1613 1629
1630/* expects to own a reference to next->hv */
1614INLINE void 1631INLINE void
1632prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1633{
1634 SV *prev_sv = SvRV (coro_current);
1635
1636 ta->prev = SvSTATE_hv (prev_sv);
1637 ta->next = next;
1638
1639 TRANSFER_CHECK (*ta);
1640
1641 SvRV_set (coro_current, (SV *)next->hv);
1642
1643 free_coro_mortal (aTHX);
1644 coro_mortal = prev_sv;
1645}
1646
1647static void
1615prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1648prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1616{ 1649{
1617 SV *prev_sv, *next_sv;
1618
1619 for (;;) 1650 for (;;)
1620 { 1651 {
1621 next_sv = coro_deq (aTHX); 1652 SV *next_sv = coro_deq (aTHX);
1622 1653
1623 /* nothing to schedule: call the idle handler */
1624 if (expect_false (!next_sv)) 1654 if (expect_true (next_sv))
1625 { 1655 {
1656 struct coro *next = SvSTATE_hv (next_sv);
1657
1658 /* cannot transfer to destroyed coros, skip and look for next */
1659 if (expect_false (next->flags & CF_DESTROYED))
1660 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1661 else
1662 {
1663 next->flags &= ~CF_READY;
1664 --coro_nready;
1665
1666 prepare_schedule_to (aTHX_ ta, next);
1667 break;
1668 }
1669 }
1670 else
1671 {
1672 /* nothing to schedule: call the idle handler */
1626 dSP; 1673 dSP;
1627 1674
1628 ENTER; 1675 ENTER;
1629 SAVETMPS; 1676 SAVETMPS;
1630 1677
1632 PUTBACK; 1679 PUTBACK;
1633 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1680 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1634 1681
1635 FREETMPS; 1682 FREETMPS;
1636 LEAVE; 1683 LEAVE;
1637 continue;
1638 } 1684 }
1639
1640 ta->next = SvSTATE_hv (next_sv);
1641
1642 /* cannot transfer to destroyed coros, skip and look for next */
1643 if (expect_false (ta->next->flags & CF_DESTROYED))
1644 {
1645 SvREFCNT_dec (next_sv);
1646 /* coro_nready has already been taken care of by destroy */
1647 continue;
1648 }
1649
1650 --coro_nready;
1651 break;
1652 } 1685 }
1653
1654 /* free this only after the transfer */
1655 prev_sv = SvRV (coro_current);
1656 ta->prev = SvSTATE_hv (prev_sv);
1657 TRANSFER_CHECK (*ta);
1658 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1659 ta->next->flags &= ~CF_READY;
1660 SvRV_set (coro_current, next_sv);
1661
1662 free_coro_mortal (aTHX);
1663 coro_mortal = prev_sv;
1664} 1686}
1665 1687
1666INLINE void 1688INLINE void
1667prepare_cede (pTHX_ struct coro_transfer_args *ta) 1689prepare_cede (pTHX_ struct coro_transfer_args *ta)
1668{ 1690{
1689{ 1711{
1690 struct coro_transfer_args ta; 1712 struct coro_transfer_args ta;
1691 1713
1692 prepare_schedule (aTHX_ &ta); 1714 prepare_schedule (aTHX_ &ta);
1693 TRANSFER (ta, 1); 1715 TRANSFER (ta, 1);
1716}
1717
1718static void
1719api_schedule_to (pTHX_ SV *coro_sv)
1720{
1721 struct coro_transfer_args ta;
1722 struct coro *next = SvSTATE (coro_sv);
1723
1724 SvREFCNT_inc_NN (coro_sv);
1725 prepare_schedule_to (aTHX_ &ta, next);
1694} 1726}
1695 1727
1696static int 1728static int
1697api_cede (pTHX) 1729api_cede (pTHX)
1698{ 1730{
1727static void 1759static void
1728api_trace (pTHX_ SV *coro_sv, int flags) 1760api_trace (pTHX_ SV *coro_sv, int flags)
1729{ 1761{
1730 struct coro *coro = SvSTATE (coro_sv); 1762 struct coro *coro = SvSTATE (coro_sv);
1731 1763
1764 if (coro->flags & CF_RUNNING)
1765 croak ("cannot enable tracing on a running coroutine, caught");
1766
1732 if (flags & CC_TRACE) 1767 if (flags & CC_TRACE)
1733 { 1768 {
1734 if (!coro->cctx) 1769 if (!coro->cctx)
1735 coro->cctx = cctx_new_run (); 1770 coro->cctx = cctx_new_run ();
1736 else if (!(coro->cctx->flags & CC_TRACE)) 1771 else if (!(coro->cctx->flags & CC_TRACE))
1737 croak ("cannot enable tracing on coroutine with custom stack,"); 1772 croak ("cannot enable tracing on coroutine with custom stack, caught");
1738 1773
1739 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1774 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1740 } 1775 }
1741 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1776 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1742 { 1777 {
1747 else 1782 else
1748 coro->slot->runops = RUNOPS_DEFAULT; 1783 coro->slot->runops = RUNOPS_DEFAULT;
1749 } 1784 }
1750} 1785}
1751 1786
1787static void
1788coro_call_on_destroy (pTHX_ struct coro *coro)
1789{
1790 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1791 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1792
1793 if (on_destroyp)
1794 {
1795 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1796
1797 while (AvFILLp (on_destroy) >= 0)
1798 {
1799 dSP; /* don't disturb outer sp */
1800 SV *cb = av_pop (on_destroy);
1801
1802 PUSHMARK (SP);
1803
1804 if (statusp)
1805 {
1806 int i;
1807 AV *status = (AV *)SvRV (*statusp);
1808 EXTEND (SP, AvFILLp (status) + 1);
1809
1810 for (i = 0; i <= AvFILLp (status); ++i)
1811 PUSHs (AvARRAY (status)[i]);
1812 }
1813
1814 PUTBACK;
1815 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1816 }
1817 }
1818}
1819
1820static void
1821slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1822{
1823 int i;
1824 HV *hv = (HV *)SvRV (coro_current);
1825 AV *av = newAV ();
1826
1827 av_extend (av, items - 1);
1828 for (i = 0; i < items; ++i)
1829 av_push (av, SvREFCNT_inc_NN (arg [i]));
1830
1831 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1832
1833 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1834 api_ready (aTHX_ sv_manager);
1835
1836 frame->prepare = prepare_schedule;
1837 frame->check = slf_check_repeat;
1838}
1839
1752/*****************************************************************************/ 1840/*****************************************************************************/
1753/* async pool handler */ 1841/* async pool handler */
1754 1842
1755static int 1843static int
1756slf_check_pool_handler (pTHX_ struct CoroSLF *frame) 1844slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1760 1848
1761 if (!coro->invoke_cb) 1849 if (!coro->invoke_cb)
1762 return 1; /* loop till we have invoke */ 1850 return 1; /* loop till we have invoke */
1763 else 1851 else
1764 { 1852 {
1765 int i, len;
1766
1767 hv_store (hv, "desc", sizeof ("desc") - 1, 1853 hv_store (hv, "desc", sizeof ("desc") - 1,
1768 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1854 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1769 1855
1770 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); 1856 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1771
1772 len = av_len (coro->invoke_av);
1773 1857
1774 { 1858 {
1775 dSP; 1859 dSP;
1776 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; 1860 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1777 PUTBACK; 1861 PUTBACK;
1841 SV *data = (SV *)GENSUB_ARG; 1925 SV *data = (SV *)GENSUB_ARG;
1842 1926
1843 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1927 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1844 { 1928 {
1845 /* first call, set args */ 1929 /* first call, set args */
1846 int i;
1847 AV *av = newAV (); 1930 AV *av = newAV ();
1848 SV *coro = SvRV (data); 1931 SV *coro = SvRV (data);
1849 1932
1850 SvRV_set (data, (SV *)av); 1933 SvRV_set (data, (SV *)av);
1851 api_ready (aTHX_ coro); 1934 api_ready (aTHX_ coro);
1992static void 2075static void
1993slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2076slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1994{ 2077{
1995 frame->prepare = prepare_schedule; 2078 frame->prepare = prepare_schedule;
1996 frame->check = slf_check_nop; 2079 frame->check = slf_check_nop;
2080}
2081
2082static void
2083slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2084{
2085 struct coro *next = (struct coro *)slf_frame.data;
2086
2087 SvREFCNT_inc_NN (next->hv);
2088 prepare_schedule_to (aTHX_ ta, next);
2089}
2090
2091static void
2092slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2093{
2094 if (!items)
2095 croak ("Coro::schedule_to expects a coroutine argument, caught");
2096
2097 frame->data = (void *)SvSTATE (arg [0]);
2098 frame->prepare = slf_prepare_schedule_to;
2099 frame->check = slf_check_nop;
2100}
2101
2102static void
2103slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2104{
2105 api_ready (aTHX_ SvRV (coro_current));
2106
2107 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1997} 2108}
1998 2109
1999static void 2110static void
2000slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2111slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2001{ 2112{
2147 } 2258 }
2148 else 2259 else
2149 slf_argc = 0; 2260 slf_argc = 0;
2150 2261
2151 PL_op->op_ppaddr = pp_slf; 2262 PL_op->op_ppaddr = pp_slf;
2152 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2263 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2153 2264
2154 PL_op = (OP *)&slf_restore; 2265 PL_op = (OP *)&slf_restore;
2155} 2266}
2156 2267
2157/*****************************************************************************/ 2268/*****************************************************************************/
2621 XSRETURN_EMPTY; 2732 XSRETURN_EMPTY;
2622} 2733}
2623 2734
2624/*****************************************************************************/ 2735/*****************************************************************************/
2625 2736
2737#if CORO_CLONE
2738# include "clone.c"
2739#endif
2740
2626MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2741MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2627 2742
2628PROTOTYPES: DISABLE 2743PROTOTYPES: DISABLE
2629 2744
2630BOOT: 2745BOOT:
2634 coro_thx = PERL_GET_CONTEXT; 2749 coro_thx = PERL_GET_CONTEXT;
2635# endif 2750# endif
2636#endif 2751#endif
2637 BOOT_PAGESIZE; 2752 BOOT_PAGESIZE;
2638 2753
2754 cctx_current = cctx_new_empty ();
2755
2639 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2756 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2640 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2757 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2641 2758
2642 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2759 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2643 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2760 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2662 2779
2663 { 2780 {
2664 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2781 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2665 2782
2666 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2783 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2667 hv_store_ent (PL_custom_op_names, slf, 2784 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2668 newSVpv ("coro_slf", 0), 0);
2669 2785
2670 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2786 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2671 hv_store_ent (PL_custom_op_descs, slf, 2787 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2672 newSVpv ("coro schedule like function", 0), 0);
2673 } 2788 }
2674 2789
2675 coroapi.ver = CORO_API_VERSION; 2790 coroapi.ver = CORO_API_VERSION;
2676 coroapi.rev = CORO_API_REVISION; 2791 coroapi.rev = CORO_API_REVISION;
2677 2792
2770void 2885void
2771_exit (int code) 2886_exit (int code)
2772 PROTOTYPE: $ 2887 PROTOTYPE: $
2773 CODE: 2888 CODE:
2774 _exit (code); 2889 _exit (code);
2890
2891SV *
2892clone (Coro::State coro)
2893 CODE:
2894{
2895#if CORO_CLONE
2896 struct coro *ncoro = coro_clone (coro);
2897 MAGIC *mg;
2898 /* TODO: too much duplication */
2899 ncoro->hv = newHV ();
2900 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2901 mg->mg_flags |= MGf_DUP;
2902 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2903#else
2904 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2905#endif
2906}
2907 OUTPUT:
2908 RETVAL
2775 2909
2776int 2910int
2777cctx_stacksize (int new_stacksize = 0) 2911cctx_stacksize (int new_stacksize = 0)
2778 PROTOTYPE: ;$ 2912 PROTOTYPE: ;$
2779 CODE: 2913 CODE:
2900 3034
2901SV * 3035SV *
2902has_cctx (Coro::State coro) 3036has_cctx (Coro::State coro)
2903 PROTOTYPE: $ 3037 PROTOTYPE: $
2904 CODE: 3038 CODE:
2905 RETVAL = boolSV (!!coro->cctx); 3039 /* maybe manage the running flag differently */
3040 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2906 OUTPUT: 3041 OUTPUT:
2907 RETVAL 3042 RETVAL
2908 3043
2909int 3044int
2910is_traced (Coro::State coro) 3045is_traced (Coro::State coro)
2930 3065
2931void 3066void
2932force_cctx () 3067force_cctx ()
2933 PROTOTYPE: 3068 PROTOTYPE:
2934 CODE: 3069 CODE:
2935 SvSTATE_current->cctx->idle_sp = 0; 3070 cctx_current->idle_sp = 0;
2936 3071
2937void 3072void
2938swap_defsv (Coro::State self) 3073swap_defsv (Coro::State self)
2939 PROTOTYPE: $ 3074 PROTOTYPE: $
2940 ALIAS: 3075 ALIAS:
2955 3090
2956BOOT: 3091BOOT:
2957{ 3092{
2958 int i; 3093 int i;
2959 3094
2960 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2961 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3095 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2962 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3096 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2963 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3097 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2964 cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); 3098 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2965 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 3099 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2966 SvREADONLY_on (coro_current); 3100 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3101 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3102 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
2967 3103
2968 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3104 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
2969 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3105 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2970 cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); 3106 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
2971 cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); 3107 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2972 3108
2973 coro_stash = gv_stashpv ("Coro", TRUE); 3109 coro_stash = gv_stashpv ("Coro", TRUE);
2974 3110
2975 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3111 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2976 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3112 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2984 3120
2985 { 3121 {
2986 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3122 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2987 3123
2988 coroapi.schedule = api_schedule; 3124 coroapi.schedule = api_schedule;
3125 coroapi.schedule_to = api_schedule_to;
2989 coroapi.cede = api_cede; 3126 coroapi.cede = api_cede;
2990 coroapi.cede_notself = api_cede_notself; 3127 coroapi.cede_notself = api_cede_notself;
2991 coroapi.ready = api_ready; 3128 coroapi.ready = api_ready;
2992 coroapi.is_ready = api_is_ready; 3129 coroapi.is_ready = api_is_ready;
2993 coroapi.nready = coro_nready; 3130 coroapi.nready = coro_nready;
2998 SvREADONLY_on (sv); 3135 SvREADONLY_on (sv);
2999 } 3136 }
3000} 3137}
3001 3138
3002void 3139void
3140terminate (...)
3141 CODE:
3142 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3143
3144void
3003schedule (...) 3145schedule (...)
3004 CODE: 3146 CODE:
3005 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3147 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3148
3149void
3150schedule_to (...)
3151 CODE:
3152 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3153
3154void
3155cede_to (...)
3156 CODE:
3157 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
3006 3158
3007void 3159void
3008cede (...) 3160cede (...)
3009 CODE: 3161 CODE:
3010 CORO_EXECUTE_SLF_XS (slf_init_cede); 3162 CORO_EXECUTE_SLF_XS (slf_init_cede);
3011 3163
3012void 3164void
3013cede_notself (...) 3165cede_notself (...)
3014 CODE: 3166 CODE:
3015 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3167 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3168
3169void
3170_cancel (Coro::State self)
3171 CODE:
3172 coro_state_destroy (aTHX_ self);
3173 coro_call_on_destroy (aTHX_ self);
3016 3174
3017void 3175void
3018_set_current (SV *current) 3176_set_current (SV *current)
3019 PROTOTYPE: $ 3177 PROTOTYPE: $
3020 CODE: 3178 CODE:
3091 PUSHMARK (SP); 3249 PUSHMARK (SP);
3092 EXTEND (SP, 2); 3250 EXTEND (SP, 2);
3093 PUSHs (sv_Coro); 3251 PUSHs (sv_Coro);
3094 PUSHs ((SV *)cv_pool_handler); 3252 PUSHs ((SV *)cv_pool_handler);
3095 PUTBACK; 3253 PUTBACK;
3096 call_sv (cv_coro_new, G_SCALAR); 3254 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3097 SPAGAIN; 3255 SPAGAIN;
3098 3256
3099 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3257 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
3100 } 3258 }
3101 3259
3106 assert (!coro->invoke_av); 3264 assert (!coro->invoke_av);
3107 coro->invoke_cb = SvREFCNT_inc (cb); 3265 coro->invoke_cb = SvREFCNT_inc (cb);
3108 coro->invoke_av = av; 3266 coro->invoke_av = av;
3109 } 3267 }
3110 3268
3111 api_ready ((SV *)hv); 3269 api_ready (aTHX_ (SV *)hv);
3112 3270
3113 if (GIMME_V != G_VOID) 3271 if (GIMME_V != G_VOID)
3114 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 3272 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3115 else 3273 else
3116 SvREFCNT_dec (hv); 3274 SvREFCNT_dec (hv);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines