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.317 by root, Thu Nov 20 06:28:52 2008 UTC vs.
Revision 1.325 by root, Sun Nov 23 06:48:45 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
170static AV *main_mainstack; /* used to differentiate between $main and others */ 173static AV *main_mainstack; /* used to differentiate between $main and others */
171static JMPENV *main_top_env; 174static JMPENV *main_top_env;
172static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
174 177
178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */
180
175static GV *irsgv; /* $/ */ 181static GV *irsgv; /* $/ */
176static GV *stdoutgv; /* *STDOUT */ 182static GV *stdoutgv; /* *STDOUT */
177static SV *rv_diehook; 183static SV *rv_diehook;
178static SV *rv_warnhook; 184static SV *rv_warnhook;
179static HV *hv_sig; /* %SIG */ 185static HV *hv_sig; /* %SIG */
180 186
181/* async_pool helper stuff */ 187/* async_pool helper stuff */
182static SV *sv_pool_rss; 188static SV *sv_pool_rss;
183static SV *sv_pool_size; 189static SV *sv_pool_size;
184static SV *sv_async_pool_idle; 190static SV *sv_async_pool_idle; /* description string */
185static AV *av_async_pool; 191static AV *av_async_pool; /* idle pool */
186static SV *sv_Coro; 192static SV *sv_Coro; /* class string */
187static CV *cv_pool_handler; 193static CV *cv_pool_handler;
188static CV *cv_coro_state_new; 194static CV *cv_coro_state_new;
189 195
190/* Coro::AnyEvent */ 196/* Coro::AnyEvent */
191static SV *sv_activity; 197static SV *sv_activity;
222 int valgrind_id; 228 int valgrind_id;
223#endif 229#endif
224 unsigned char flags; 230 unsigned char flags;
225} coro_cctx; 231} coro_cctx;
226 232
233coro_cctx *cctx_current; /* the currently running cctx */
234
235/*****************************************************************************/
236
227enum { 237enum {
228 CF_RUNNING = 0x0001, /* coroutine is running */ 238 CF_RUNNING = 0x0001, /* coroutine is running */
229 CF_READY = 0x0002, /* coroutine is ready */ 239 CF_READY = 0x0002, /* coroutine is ready */
230 CF_NEW = 0x0004, /* has never been switched to */ 240 CF_NEW = 0x0004, /* has never been switched to */
231 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 241 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
344 GV *gvp; 354 GV *gvp;
345 return sv_2cv (sv, &st, &gvp, 0); 355 return sv_2cv (sv, &st, &gvp, 0);
346} 356}
347 357
348static AV * 358static AV *
349coro_clone_padlist (pTHX_ CV *cv) 359coro_derive_padlist (pTHX_ CV *cv)
350{ 360{
351 AV *padlist = CvPADLIST (cv); 361 AV *padlist = CvPADLIST (cv);
352 AV *newpadlist, *newpad; 362 AV *newpadlist, *newpad;
353 363
354 newpadlist = newAV (); 364 newpadlist = newAV ();
474 CV *cp = Perl_cv_clone (aTHX_ cv); 484 CV *cp = Perl_cv_clone (aTHX_ cv);
475 CvPADLIST (cv) = CvPADLIST (cp); 485 CvPADLIST (cv) = CvPADLIST (cp);
476 CvPADLIST (cp) = 0; 486 CvPADLIST (cp) = 0;
477 SvREFCNT_dec (cp); 487 SvREFCNT_dec (cp);
478#else 488#else
479 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 489 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
480#endif 490#endif
481 } 491 }
482} 492}
483 493
484static void 494static void
834slf_check_nop (pTHX_ struct CoroSLF *frame) 844slf_check_nop (pTHX_ struct CoroSLF *frame)
835{ 845{
836 return 0; 846 return 0;
837} 847}
838 848
849static int
850slf_check_repeat (pTHX_ struct CoroSLF *frame)
851{
852 return 1;
853}
854
839static UNOP coro_setup_op; 855static UNOP coro_setup_op;
840 856
841static 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 */
842coro_setup (pTHX_ struct coro *coro) 858coro_setup (pTHX_ struct coro *coro)
843{ 859{
892 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 */
893 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 */
894 910
895 /* 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 */
896 coro_setup_op.op_next = PL_op; 912 coro_setup_op.op_next = PL_op;
897 coro_setup_op.op_type = OP_CUSTOM; 913 coro_setup_op.op_type = OP_ENTERSUB;
898 coro_setup_op.op_ppaddr = pp_slf; 914 coro_setup_op.op_ppaddr = pp_slf;
899 /* 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 */
900 916
901 PL_op = (OP *)&coro_setup_op; 917 PL_op = (OP *)&coro_setup_op;
902 918
955static int 971static int
956runops_trace (pTHX) 972runops_trace (pTHX)
957{ 973{
958 COP *oldcop = 0; 974 COP *oldcop = 0;
959 int oldcxix = -2; 975 int oldcxix = -2;
960 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
961 coro_cctx *cctx = coro->cctx;
962 976
963 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 977 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
964 { 978 {
965 PERL_ASYNC_CHECK (); 979 PERL_ASYNC_CHECK ();
966 980
967 if (cctx->flags & CC_TRACE_ALL) 981 if (cctx_current->flags & CC_TRACE_ALL)
968 { 982 {
969 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)
970 { 984 {
971 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 985 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
972 SV **bot, **top; 986 SV **bot, **top;
973 AV *av = newAV (); /* return values */ 987 AV *av = newAV (); /* return values */
974 SV **cb; 988 SV **cb;
1011 1025
1012 if (PL_curcop != &PL_compiling) 1026 if (PL_curcop != &PL_compiling)
1013 { 1027 {
1014 SV **cb; 1028 SV **cb;
1015 1029
1016 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1030 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1017 { 1031 {
1018 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1032 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1019 1033
1020 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1034 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1021 { 1035 {
1022 runops_proc_t old_runops = PL_runops;
1023 dSP; 1036 dSP;
1024 GV *gv = CvGV (cx->blk_sub.cv); 1037 GV *gv = CvGV (cx->blk_sub.cv);
1025 SV *fullname = sv_2mortal (newSV (0)); 1038 SV *fullname = sv_2mortal (newSV (0));
1026 1039
1027 if (isGV (gv)) 1040 if (isGV (gv))
1045 } 1058 }
1046 1059
1047 oldcxix = cxstack_ix; 1060 oldcxix = cxstack_ix;
1048 } 1061 }
1049 1062
1050 if (cctx->flags & CC_TRACE_LINE) 1063 if (cctx_current->flags & CC_TRACE_LINE)
1051 { 1064 {
1052 dSP; 1065 dSP;
1053 1066
1054 PL_runops = RUNOPS_DEFAULT; 1067 PL_runops = RUNOPS_DEFAULT;
1055 ENTER; 1068 ENTER;
1074 1087
1075 TAINT_NOT; 1088 TAINT_NOT;
1076 return 0; 1089 return 0;
1077} 1090}
1078 1091
1079static struct coro_cctx *cctx_ssl_cctx;
1080static struct CoroSLF cctx_ssl_frame; 1092static struct CoroSLF cctx_ssl_frame;
1081 1093
1082static void 1094static void
1083slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1095slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1084{ 1096{
1085 ta->prev = (struct coro *)cctx_ssl_cctx;
1086 ta->next = 0; 1097 ta->prev = 0;
1087} 1098}
1088 1099
1089static int 1100static int
1090slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1101slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1091{ 1102{
1094 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 */
1095} 1106}
1096 1107
1097/* 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 */
1098static void NOINLINE 1109static void NOINLINE
1099cctx_prepare (pTHX_ coro_cctx *cctx) 1110cctx_prepare (pTHX)
1100{ 1111{
1101 PL_top_env = &PL_start_env; 1112 PL_top_env = &PL_start_env;
1102 1113
1103 if (cctx->flags & CC_TRACE) 1114 if (cctx_current->flags & CC_TRACE)
1104 PL_runops = runops_trace; 1115 PL_runops = runops_trace;
1105 1116
1106 /* 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
1107 * that can lead to creation of a new cctx */ 1118 * that can lead to creation of a new cctx */
1108 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)",
1109 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1120 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1110 1121
1111 /* 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 */
1112 cctx_ssl_cctx = cctx;
1113 cctx_ssl_frame = slf_frame; 1123 cctx_ssl_frame = slf_frame;
1114 1124
1115 slf_frame.prepare = slf_prepare_set_stacklevel; 1125 slf_frame.prepare = slf_prepare_set_stacklevel;
1116 slf_frame.check = slf_check_set_stacklevel; 1126 slf_frame.check = slf_check_set_stacklevel;
1117} 1127}
1140 /* normally we would need to skip the entersub here */ 1150 /* normally we would need to skip the entersub here */
1141 /* 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 */
1142 /* PL_nop = PL_nop->op_next */ 1152 /* PL_nop = PL_nop->op_next */
1143 1153
1144 /* inject a fake subroutine call to cctx_init */ 1154 /* inject a fake subroutine call to cctx_init */
1145 cctx_prepare (aTHX_ (coro_cctx *)arg); 1155 cctx_prepare (aTHX);
1146 1156
1147 /* cctx_run is the alternative tail of transfer() */ 1157 /* cctx_run is the alternative tail of transfer() */
1148 transfer_tail (aTHX); 1158 transfer_tail (aTHX);
1149 1159
1150 /* 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 */
1245cctx_destroy (coro_cctx *cctx) 1255cctx_destroy (coro_cctx *cctx)
1246{ 1256{
1247 if (!cctx) 1257 if (!cctx)
1248 return; 1258 return;
1249 1259
1260 assert (cctx != cctx_current);//D temporary
1261
1250 --cctx_count; 1262 --cctx_count;
1251 coro_destroy (&cctx->cctx); 1263 coro_destroy (&cctx->cctx);
1252 1264
1253 /* coro_transfer creates new, empty cctx's */ 1265 /* coro_transfer creates new, empty cctx's */
1254 if (cctx->sptr) 1266 if (cctx->sptr)
1317 /* TODO: throwing up here is considered harmful */ 1329 /* TODO: throwing up here is considered harmful */
1318 1330
1319 if (expect_true (prev != next)) 1331 if (expect_true (prev != next))
1320 { 1332 {
1321 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1333 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1322 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1334 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1323 1335
1324 if (expect_false (next->flags & CF_RUNNING)) 1336 if (expect_false (next->flags & CF_RUNNING))
1325 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); 1337 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1326 1338
1327 if (expect_false (next->flags & CF_DESTROYED)) 1339 if (expect_false (next->flags & CF_DESTROYED))
1339transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1351transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1340{ 1352{
1341 dSTACKLEVEL; 1353 dSTACKLEVEL;
1342 1354
1343 /* sometimes transfer is only called to set idle_sp */ 1355 /* sometimes transfer is only called to set idle_sp */
1344 if (expect_false (!next)) 1356 if (expect_false (!prev))
1345 { 1357 {
1346 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1358 cctx_current->idle_sp = STACKLEVEL;
1347 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 */
1348 } 1360 }
1349 else if (expect_true (prev != next)) 1361 else if (expect_true (prev != next))
1350 { 1362 {
1351 coro_cctx *prev__cctx; 1363 coro_cctx *cctx_prev;
1352 1364
1353 if (expect_false (prev->flags & CF_NEW)) 1365 if (expect_false (prev->flags & CF_NEW))
1354 { 1366 {
1355 /* create a new empty/source context */ 1367 /* create a new empty/source context */
1356 prev->cctx = cctx_new_empty ();
1357 prev->flags &= ~CF_NEW; 1368 prev->flags &= ~CF_NEW;
1358 prev->flags |= CF_RUNNING; 1369 prev->flags |= CF_RUNNING;
1359 } 1370 }
1360 1371
1361 prev->flags &= ~CF_RUNNING; 1372 prev->flags &= ~CF_RUNNING;
1372 coro_setup (aTHX_ next); 1383 coro_setup (aTHX_ next);
1373 } 1384 }
1374 else 1385 else
1375 load_perl (aTHX_ next); 1386 load_perl (aTHX_ next);
1376 1387
1377 prev__cctx = prev->cctx; 1388 assert (!prev->cctx);//D temporary
1378 1389
1379 /* possibly untie and reuse the cctx */ 1390 /* possibly untie and reuse the cctx */
1380 if (expect_true ( 1391 if (expect_true (
1381 prev__cctx->idle_sp == STACKLEVEL 1392 cctx_current->idle_sp == STACKLEVEL
1382 && !(prev__cctx->flags & CC_TRACE) 1393 && !(cctx_current->flags & CC_TRACE)
1383 && !force_cctx 1394 && !force_cctx
1384 )) 1395 ))
1385 { 1396 {
1386 /* 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 */
1387 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));
1388 1399
1389 prev->cctx = 0;
1390
1391 /* 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. */
1392 /* 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 */
1393 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1402 if (expect_false (CCTX_EXPIRED (cctx_current)))
1394 if (!next->cctx) 1403 if (expect_true (!next->cctx))
1395 next->cctx = cctx_get (aTHX); 1404 next->cctx = cctx_get (aTHX);
1396 1405
1397 cctx_put (prev__cctx); 1406 cctx_put (cctx_current);
1398 } 1407 }
1408 else
1409 prev->cctx = cctx_current;
1399 1410
1400 ++next->usecount; 1411 ++next->usecount;
1401 1412
1402 if (expect_true (!next->cctx)) 1413 cctx_prev = cctx_current;
1403 next->cctx = cctx_get (aTHX); 1414 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1404 1415
1405 if (expect_false (prev__cctx != next->cctx)) 1416 next->cctx = 0;
1417
1418 if (expect_false (cctx_prev != cctx_current))
1406 { 1419 {
1407 prev__cctx->top_env = PL_top_env; 1420 cctx_prev->top_env = PL_top_env;
1408 PL_top_env = next->cctx->top_env; 1421 PL_top_env = cctx_current->top_env;
1409 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1422 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1410 } 1423 }
1411 1424
1412 transfer_tail (aTHX); 1425 transfer_tail (aTHX);
1413 } 1426 }
1414} 1427}
1648 else 1661 else
1649 { 1662 {
1650 next->flags &= ~CF_READY; 1663 next->flags &= ~CF_READY;
1651 --coro_nready; 1664 --coro_nready;
1652 1665
1653 return prepare_schedule_to (aTHX_ ta, next); 1666 prepare_schedule_to (aTHX_ ta, next);
1667 break;
1654 } 1668 }
1655 } 1669 }
1656 else 1670 else
1657 { 1671 {
1658 /* nothing to schedule: call the idle handler */ 1672 /* nothing to schedule: call the idle handler */
1745static void 1759static void
1746api_trace (pTHX_ SV *coro_sv, int flags) 1760api_trace (pTHX_ SV *coro_sv, int flags)
1747{ 1761{
1748 struct coro *coro = SvSTATE (coro_sv); 1762 struct coro *coro = SvSTATE (coro_sv);
1749 1763
1764 if (coro->flags & CF_RUNNING)
1765 croak ("cannot enable tracing on a running coroutine, caught");
1766
1750 if (flags & CC_TRACE) 1767 if (flags & CC_TRACE)
1751 { 1768 {
1752 if (!coro->cctx) 1769 if (!coro->cctx)
1753 coro->cctx = cctx_new_run (); 1770 coro->cctx = cctx_new_run ();
1754 else if (!(coro->cctx->flags & CC_TRACE)) 1771 else if (!(coro->cctx->flags & CC_TRACE))
1755 croak ("cannot enable tracing on coroutine with custom stack,"); 1772 croak ("cannot enable tracing on coroutine with custom stack, caught");
1756 1773
1757 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1774 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1758 } 1775 }
1759 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1776 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1760 { 1777 {
1763 if (coro->flags & CF_RUNNING) 1780 if (coro->flags & CF_RUNNING)
1764 PL_runops = RUNOPS_DEFAULT; 1781 PL_runops = RUNOPS_DEFAULT;
1765 else 1782 else
1766 coro->slot->runops = RUNOPS_DEFAULT; 1783 coro->slot->runops = RUNOPS_DEFAULT;
1767 } 1784 }
1785}
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;
1768} 1838}
1769 1839
1770/*****************************************************************************/ 1840/*****************************************************************************/
1771/* async pool handler */ 1841/* async pool handler */
1772 1842
2188 } 2258 }
2189 else 2259 else
2190 slf_argc = 0; 2260 slf_argc = 0;
2191 2261
2192 PL_op->op_ppaddr = pp_slf; 2262 PL_op->op_ppaddr = pp_slf;
2193 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 */
2194 2264
2195 PL_op = (OP *)&slf_restore; 2265 PL_op = (OP *)&slf_restore;
2196} 2266}
2197 2267
2198/*****************************************************************************/ 2268/*****************************************************************************/
2662 XSRETURN_EMPTY; 2732 XSRETURN_EMPTY;
2663} 2733}
2664 2734
2665/*****************************************************************************/ 2735/*****************************************************************************/
2666 2736
2737#if CORO_CLONE
2738# include "clone.c"
2739#endif
2740
2667MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2741MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2668 2742
2669PROTOTYPES: DISABLE 2743PROTOTYPES: DISABLE
2670 2744
2671BOOT: 2745BOOT:
2675 coro_thx = PERL_GET_CONTEXT; 2749 coro_thx = PERL_GET_CONTEXT;
2676# endif 2750# endif
2677#endif 2751#endif
2678 BOOT_PAGESIZE; 2752 BOOT_PAGESIZE;
2679 2753
2754 cctx_current = cctx_new_empty ();
2755
2680 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2756 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2681 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2757 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2682 2758
2683 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;
2684 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;
2703 2779
2704 { 2780 {
2705 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2781 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2706 2782
2707 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2783 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2708 hv_store_ent (PL_custom_op_names, slf, 2784 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2709 newSVpv ("coro_slf", 0), 0);
2710 2785
2711 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2786 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2712 hv_store_ent (PL_custom_op_descs, slf, 2787 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2713 newSVpv ("coro schedule like function", 0), 0);
2714 } 2788 }
2715 2789
2716 coroapi.ver = CORO_API_VERSION; 2790 coroapi.ver = CORO_API_VERSION;
2717 coroapi.rev = CORO_API_REVISION; 2791 coroapi.rev = CORO_API_REVISION;
2718 2792
2811void 2885void
2812_exit (int code) 2886_exit (int code)
2813 PROTOTYPE: $ 2887 PROTOTYPE: $
2814 CODE: 2888 CODE:
2815 _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
2816 2909
2817int 2910int
2818cctx_stacksize (int new_stacksize = 0) 2911cctx_stacksize (int new_stacksize = 0)
2819 PROTOTYPE: ;$ 2912 PROTOTYPE: ;$
2820 CODE: 2913 CODE:
2941 3034
2942SV * 3035SV *
2943has_cctx (Coro::State coro) 3036has_cctx (Coro::State coro)
2944 PROTOTYPE: $ 3037 PROTOTYPE: $
2945 CODE: 3038 CODE:
2946 RETVAL = boolSV (!!coro->cctx); 3039 /* maybe manage the running flag differently */
3040 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2947 OUTPUT: 3041 OUTPUT:
2948 RETVAL 3042 RETVAL
2949 3043
2950int 3044int
2951is_traced (Coro::State coro) 3045is_traced (Coro::State coro)
2971 3065
2972void 3066void
2973force_cctx () 3067force_cctx ()
2974 PROTOTYPE: 3068 PROTOTYPE:
2975 CODE: 3069 CODE:
2976 SvSTATE_current->cctx->idle_sp = 0; 3070 cctx_current->idle_sp = 0;
2977 3071
2978void 3072void
2979swap_defsv (Coro::State self) 3073swap_defsv (Coro::State self)
2980 PROTOTYPE: $ 3074 PROTOTYPE: $
2981 ALIAS: 3075 ALIAS:
2996 3090
2997BOOT: 3091BOOT:
2998{ 3092{
2999 int i; 3093 int i;
3000 3094
3001 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3002 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3095 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3003 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3096 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3004 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3097 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
3005 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3098 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3006 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3099 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 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);
3007 3103
3008 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);
3009 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3105 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3010 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3106 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3011 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3107 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3039 SvREADONLY_on (sv); 3135 SvREADONLY_on (sv);
3040 } 3136 }
3041} 3137}
3042 3138
3043void 3139void
3140terminate (...)
3141 CODE:
3142 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3143
3144void
3044schedule (...) 3145schedule (...)
3045 CODE: 3146 CODE:
3046 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3147 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3047 3148
3048void 3149void
3062 3163
3063void 3164void
3064cede_notself (...) 3165cede_notself (...)
3065 CODE: 3166 CODE:
3066 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);
3067 3174
3068void 3175void
3069_set_current (SV *current) 3176_set_current (SV *current)
3070 PROTOTYPE: $ 3177 PROTOTYPE: $
3071 CODE: 3178 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines