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.262 by root, Mon Nov 10 20:38:19 2008 UTC vs.
Revision 1.271 by root, Fri Nov 14 07:35:32 2008 UTC

46# define BOOT_PAGESIZE (void)0 46# define BOOT_PAGESIZE (void)0
47#endif 47#endif
48 48
49#if CORO_USE_VALGRIND 49#if CORO_USE_VALGRIND
50# include <valgrind/valgrind.h> 50# include <valgrind/valgrind.h>
51# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
52#else
53# define REGISTER_STACK(cctx,start,end)
54#endif 51#endif
55 52
56/* the maximum number of idle cctx that will be pooled */ 53/* the maximum number of idle cctx that will be pooled */
57static int cctx_max_idle = 4; 54static int cctx_max_idle = 4;
58 55
119# define CORO_PREFER_PERL_FUNCTIONS 0 116# define CORO_PREFER_PERL_FUNCTIONS 0
120#endif 117#endif
121 118
122/* The next macros try to return the current stack pointer, in an as 119/* The next macros try to return the current stack pointer, in an as
123 * portable way as possible. */ 120 * portable way as possible. */
124#define dSTACKLEVEL volatile char stacklevel 121#if __GNUC__ >= 4
125#define STACKLEVEL ((void *)&stacklevel) 122# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0)
123#else
124# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel
125#endif
126 126
127#define IN_DESTRUCT (PL_main_cv == Nullcv) 127#define IN_DESTRUCT (PL_main_cv == Nullcv)
128 128
129#if __GNUC__ >= 3 129#if __GNUC__ >= 3
130# define attribute(x) __attribute__(x) 130# define attribute(x) __attribute__(x)
131# define BARRIER __asm__ __volatile__ ("" : : : "memory")
132# define expect(expr,value) __builtin_expect ((expr),(value)) 131# define expect(expr,value) __builtin_expect ((expr),(value))
132# define INLINE static inline
133#else 133#else
134# define attribute(x) 134# define attribute(x)
135# define BARRIER
136# define expect(expr,value) (expr) 135# define expect(expr,value) (expr)
136# define INLINE static
137#endif 137#endif
138 138
139#define expect_false(expr) expect ((expr) != 0, 0) 139#define expect_false(expr) expect ((expr) != 0, 0)
140#define expect_true(expr) expect ((expr) != 0, 1) 140#define expect_true(expr) expect ((expr) != 0, 1)
141 141
299 299
300/* for Coro.pm */ 300/* for Coro.pm */
301static SV *coro_current; 301static SV *coro_current;
302static SV *coro_readyhook; 302static SV *coro_readyhook;
303static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 303static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
304static int coro_nready;
305static struct coro *coro_first; 304static struct coro *coro_first;
305#define coro_nready coroapi.nready
306 306
307/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
308 308
309static SV * 309static SV *
310coro_get_sv (pTHX_ const char *name, int create) 310coro_get_sv (pTHX_ const char *name, int create)
413 : 0 413 : 0
414 414
415#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 415#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
416#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 416#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
417 417
418static struct coro * 418INLINE struct coro *
419SvSTATE_ (pTHX_ SV *coro) 419SvSTATE_ (pTHX_ SV *coro)
420{ 420{
421 HV *stash; 421 HV *stash;
422 MAGIC *mg; 422 MAGIC *mg;
423 423
714 } 714 }
715 715
716 return rss; 716 return rss;
717} 717}
718 718
719/** set stacklevel support **************************************************/
720
721/* we sometimes need to create the effect of pp_slf calling us */
722#define SLF_HEAD (void)0
723/* we sometimes need to create the effect of leaving via pp_slf */
724#define SLF_TAIL slf_tail (aTHX)
725
726INLINE void
727slf_tail (pTHX)
728{
729 dSP;
730 SV **bot = SP;
731
732 int gimme = GIMME_V;
733
734 /* make sure we put something on the stack in scalar context */
735 if (gimme == G_SCALAR)
736 {
737 if (sp == bot)
738 XPUSHs (&PL_sv_undef);
739
740 SP = bot + 1;
741 }
742
743 PUTBACK;
744}
745
719/** coroutine stack handling ************************************************/ 746/** coroutine stack handling ************************************************/
720 747
721static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 748static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
722static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 749static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
723static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); 750static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
855 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 882 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
856 SPAGAIN; 883 SPAGAIN;
857 } 884 }
858 885
859 /* this newly created coroutine might be run on an existing cctx which most 886 /* this newly created coroutine might be run on an existing cctx which most
860 * likely was suspended in set_stacklevel, called from entersub. 887 * likely was suspended in set_stacklevel, called from pp_set_stacklevel,
861 * set_stacklevel doesn't do anything on return, but entersub does LEAVE, 888 * so we have to emulate entering pp_set_stacklevel here.
862 * so we ENTER here for symmetry.
863 */ 889 */
864 ENTER; 890 SLF_HEAD;
865} 891}
866 892
867static void 893static void
868coro_destruct (pTHX_ struct coro *coro) 894coro_destruct (pTHX_ struct coro *coro)
869{ 895{
898 SvREFCNT_dec (coro->throw); 924 SvREFCNT_dec (coro->throw);
899 925
900 coro_destruct_stacks (aTHX); 926 coro_destruct_stacks (aTHX);
901} 927}
902 928
903static void 929INLINE void
904free_coro_mortal (pTHX) 930free_coro_mortal (pTHX)
905{ 931{
906 if (expect_true (coro_mortal)) 932 if (expect_true (coro_mortal))
907 { 933 {
908 SvREFCNT_dec (coro_mortal); 934 SvREFCNT_dec (coro_mortal);
1069 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1095 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1070 SPAGAIN; 1096 SPAGAIN;
1071} 1097}
1072 1098
1073/* the tail of transfer: execute stuff we can only do after a transfer */ 1099/* the tail of transfer: execute stuff we can only do after a transfer */
1074static void 1100INLINE void
1075transfer_tail (pTHX) 1101transfer_tail (pTHX)
1076{ 1102{
1077 struct coro *next = (struct coro *)transfer_next; 1103 struct coro *next = (struct coro *)transfer_next;
1078 transfer_next = 0; //D for temporary assertion in transfer 1104 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1079 assert (("FATAL ERROR: internal error 1067 in Coro module, please report", next));//D 1105 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1080 1106
1081 free_coro_mortal (aTHX); 1107 free_coro_mortal (aTHX);
1082 UNLOCK; 1108 UNLOCK;
1083 1109
1084 if (expect_false (next->throw)) 1110 if (expect_false (next->throw))
1103# endif 1129# endif
1104#endif 1130#endif
1105 { 1131 {
1106 dTHX; 1132 dTHX;
1107 1133
1108 /* entersub called ENTER, but we never 'returned', undo that here */ 1134 /* we are the alternative tail to pp_set_stacklevel */
1109 LEAVE; 1135 /* so do the same things here */
1136 SLF_TAIL;
1110 1137
1111 /* we now skip the entersub that did lead to transfer() */ 1138 /* we now skip the op that did lead to transfer() */
1112 PL_op = PL_op->op_next; 1139 PL_op = PL_op->op_next;
1113 1140
1114 /* inject a fake subroutine call to cctx_init */ 1141 /* inject a fake subroutine call to cctx_init */
1115 cctx_prepare (aTHX_ (coro_cctx *)arg); 1142 cctx_prepare (aTHX_ (coro_cctx *)arg);
1116 1143
1173 /* mmap supposedly does allocate-on-write for us */ 1200 /* mmap supposedly does allocate-on-write for us */
1174 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1201 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1175 1202
1176 if (cctx->sptr != (void *)-1) 1203 if (cctx->sptr != (void *)-1)
1177 { 1204 {
1178# if CORO_STACKGUARD 1205 #if CORO_STACKGUARD
1179 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1206 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1180# endif 1207 #endif
1181 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1208 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1182 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1209 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1183 cctx->flags |= CC_MAPPED; 1210 cctx->flags |= CC_MAPPED;
1184 } 1211 }
1185 else 1212 else
1186#endif 1213#endif
1187 { 1214 {
1188 cctx->ssize = cctx_stacksize * (long)sizeof (long); 1215 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1189 New (0, cctx->sptr, cctx_stacksize, long); 1216 New (0, cctx->sptr, cctx_stacksize, long);
1190 1217
1191 if (!cctx->sptr) 1218 if (!cctx->sptr)
1192 { 1219 {
1193 perror ("FATAL: unable to allocate stack for coroutine"); 1220 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1194 _exit (EXIT_FAILURE); 1221 _exit (EXIT_FAILURE);
1195 } 1222 }
1196 1223
1197 stack_start = cctx->sptr; 1224 stack_start = cctx->sptr;
1198 stack_size = cctx->ssize; 1225 stack_size = cctx->ssize;
1199 } 1226 }
1200 1227
1201 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1228 #if CORO_USE_VALGRIND
1229 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1230 #endif
1231
1202 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1232 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1203 1233
1204 return cctx; 1234 return cctx;
1205} 1235}
1206 1236
1214 coro_destroy (&cctx->cctx); 1244 coro_destroy (&cctx->cctx);
1215 1245
1216 /* coro_transfer creates new, empty cctx's */ 1246 /* coro_transfer creates new, empty cctx's */
1217 if (cctx->sptr) 1247 if (cctx->sptr)
1218 { 1248 {
1219#if CORO_USE_VALGRIND 1249 #if CORO_USE_VALGRIND
1220 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1250 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1221#endif 1251 #endif
1222 1252
1223#if HAVE_MMAP 1253#if HAVE_MMAP
1224 if (cctx->flags & CC_MAPPED) 1254 if (cctx->flags & CC_MAPPED)
1225 munmap (cctx->sptr, cctx->ssize); 1255 munmap (cctx->sptr, cctx->ssize);
1226 else 1256 else
1253} 1283}
1254 1284
1255static void 1285static void
1256cctx_put (coro_cctx *cctx) 1286cctx_put (coro_cctx *cctx)
1257{ 1287{
1258 assert (("cctx_put called on non-initialised cctx", cctx->sptr)); 1288 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1259 1289
1260 /* free another cctx if overlimit */ 1290 /* free another cctx if overlimit */
1261 if (expect_false (cctx_idle >= cctx_max_idle)) 1291 if (expect_false (cctx_idle >= cctx_max_idle))
1262 { 1292 {
1263 coro_cctx *first = cctx_first; 1293 coro_cctx *first = cctx_first;
1302 dSTACKLEVEL; 1332 dSTACKLEVEL;
1303 1333
1304 /* sometimes transfer is only called to set idle_sp */ 1334 /* sometimes transfer is only called to set idle_sp */
1305 if (expect_false (!next)) 1335 if (expect_false (!next))
1306 { 1336 {
1307 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1337 ((coro_cctx *)prev)->idle_sp = stacklevel;
1308 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1338 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1309 } 1339 }
1310 else if (expect_true (prev != next)) 1340 else if (expect_true (prev != next))
1311 { 1341 {
1312 coro_cctx *prev__cctx; 1342 coro_cctx *prev__cctx;
1339 1369
1340 prev__cctx = prev->cctx; 1370 prev__cctx = prev->cctx;
1341 1371
1342 /* possibly untie and reuse the cctx */ 1372 /* possibly untie and reuse the cctx */
1343 if (expect_true ( 1373 if (expect_true (
1344 prev__cctx->idle_sp == STACKLEVEL 1374 prev__cctx->idle_sp == stacklevel
1345 && !(prev__cctx->flags & CC_TRACE) 1375 && !(prev__cctx->flags & CC_TRACE)
1346 && !force_cctx 1376 && !force_cctx
1347 )) 1377 ))
1348 { 1378 {
1349 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1379 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1350 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1380 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1351 1381
1352 prev->cctx = 0; 1382 prev->cctx = 0;
1353 1383
1354 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1384 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1355 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1385 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1363 ++next->usecount; 1393 ++next->usecount;
1364 1394
1365 if (expect_true (!next->cctx)) 1395 if (expect_true (!next->cctx))
1366 next->cctx = cctx_get (aTHX); 1396 next->cctx = cctx_get (aTHX);
1367 1397
1368 assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D 1398 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1369 transfer_next = next; 1399 transfer_next = next;
1370 1400
1371 if (expect_false (prev__cctx != next->cctx)) 1401 if (expect_false (prev__cctx != next->cctx))
1372 { 1402 {
1373 prev__cctx->top_env = PL_top_env; 1403 prev__cctx->top_env = PL_top_env;
1475 ta->next = SvSTATE (next_sv); 1505 ta->next = SvSTATE (next_sv);
1476 TRANSFER_CHECK (*ta); 1506 TRANSFER_CHECK (*ta);
1477} 1507}
1478 1508
1479static void 1509static void
1480api_transfer (SV *prev_sv, SV *next_sv) 1510api_transfer (pTHX_ SV *prev_sv, SV *next_sv)
1481{ 1511{
1482 dTHX;
1483 struct transfer_args ta; 1512 struct transfer_args ta;
1484 1513
1485 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1514 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1486 TRANSFER (ta, 1); 1515 TRANSFER (ta, 1);
1487} 1516}
1505 1534
1506 return 0; 1535 return 0;
1507} 1536}
1508 1537
1509static int 1538static int
1510api_ready (SV *coro_sv) 1539api_ready (pTHX_ SV *coro_sv)
1511{ 1540{
1512 dTHX;
1513 struct coro *coro; 1541 struct coro *coro;
1514 SV *sv_hook; 1542 SV *sv_hook;
1515 void (*xs_hook)(void); 1543 void (*xs_hook)(void);
1516 1544
1517 if (SvROK (coro_sv)) 1545 if (SvROK (coro_sv))
1555 1583
1556 return 1; 1584 return 1;
1557} 1585}
1558 1586
1559static int 1587static int
1560api_is_ready (SV *coro_sv) 1588api_is_ready (pTHX_ SV *coro_sv)
1561{ 1589{
1562 dTHX;
1563 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1590 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1564} 1591}
1565 1592
1566static void 1593INLINE void
1567prepare_schedule (pTHX_ struct transfer_args *ta) 1594prepare_schedule (pTHX_ struct transfer_args *ta)
1568{ 1595{
1569 SV *prev_sv, *next_sv; 1596 SV *prev_sv, *next_sv;
1570 1597
1571 for (;;) 1598 for (;;)
1610 1637
1611 /* free this only after the transfer */ 1638 /* free this only after the transfer */
1612 prev_sv = SvRV (coro_current); 1639 prev_sv = SvRV (coro_current);
1613 ta->prev = SvSTATE (prev_sv); 1640 ta->prev = SvSTATE (prev_sv);
1614 TRANSFER_CHECK (*ta); 1641 TRANSFER_CHECK (*ta);
1615 assert (ta->next->flags & CF_READY); 1642 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1616 ta->next->flags &= ~CF_READY; 1643 ta->next->flags &= ~CF_READY;
1617 SvRV_set (coro_current, next_sv); 1644 SvRV_set (coro_current, next_sv);
1618 1645
1619 LOCK; 1646 LOCK;
1620 free_coro_mortal (aTHX); 1647 free_coro_mortal (aTHX);
1621 coro_mortal = prev_sv; 1648 coro_mortal = prev_sv;
1622 UNLOCK; 1649 UNLOCK;
1623} 1650}
1624 1651
1652INLINE void
1653prepare_cede (pTHX_ struct transfer_args *ta)
1654{
1655 api_ready (aTHX_ coro_current);
1656 prepare_schedule (aTHX_ ta);
1657}
1658
1625static void 1659static void
1626prepare_cede (pTHX_ struct transfer_args *ta) 1660prepare_cede_notself (pTHX_ struct transfer_args *ta)
1627{ 1661{
1628 api_ready (coro_current); 1662 SV *prev = SvRV (coro_current);
1663
1664 if (coro_nready)
1665 {
1666 prepare_schedule (aTHX_ ta);
1667 api_ready (aTHX_ prev);
1668 }
1669 else
1670 ta->prev = ta->next = SvSTATE (prev);
1671}
1672
1673static void
1674api_schedule (pTHX)
1675{
1676 struct transfer_args ta;
1677
1629 prepare_schedule (aTHX_ ta); 1678 prepare_schedule (aTHX_ &ta);
1679 TRANSFER (ta, 1);
1630} 1680}
1631 1681
1632static int 1682static int
1633prepare_cede_notself (pTHX_ struct transfer_args *ta) 1683api_cede (pTHX)
1634{ 1684{
1635 if (coro_nready) 1685 struct transfer_args ta;
1636 { 1686
1637 SV *prev = SvRV (coro_current);
1638 prepare_schedule (aTHX_ ta); 1687 prepare_cede (aTHX_ &ta);
1639 api_ready (prev); 1688
1689 if (expect_true (ta.prev != ta.next))
1690 {
1691 TRANSFER (ta, 1);
1640 return 1; 1692 return 1;
1641 } 1693 }
1642 else 1694 else
1643 return 0; 1695 return 0;
1644} 1696}
1645 1697
1646static void
1647api_schedule (void)
1648{
1649 dTHX;
1650 struct transfer_args ta;
1651
1652 prepare_schedule (aTHX_ &ta);
1653 TRANSFER (ta, 1);
1654}
1655
1656static int 1698static int
1657api_cede (void) 1699api_cede_notself (pTHX)
1658{ 1700{
1659 dTHX; 1701 if (coro_nready)
1702 {
1660 struct transfer_args ta; 1703 struct transfer_args ta;
1661 1704
1662 prepare_cede (aTHX_ &ta); 1705 prepare_cede_notself (aTHX_ &ta);
1663
1664 if (expect_true (ta.prev != ta.next))
1665 {
1666 TRANSFER (ta, 1); 1706 TRANSFER (ta, 1);
1667 return 1; 1707 return 1;
1668 } 1708 }
1669 else 1709 else
1670 return 0; 1710 return 0;
1671} 1711}
1672 1712
1673static int
1674api_cede_notself (void)
1675{
1676 dTHX;
1677 struct transfer_args ta;
1678
1679 if (prepare_cede_notself (aTHX_ &ta))
1680 {
1681 TRANSFER (ta, 1);
1682 return 1;
1683 }
1684 else
1685 return 0;
1686}
1687
1688static void 1713static void
1689api_trace (SV *coro_sv, int flags) 1714api_trace (pTHX_ SV *coro_sv, int flags)
1690{ 1715{
1691 dTHX;
1692 struct coro *coro = SvSTATE (coro_sv); 1716 struct coro *coro = SvSTATE (coro_sv);
1693 1717
1694 if (flags & CC_TRACE) 1718 if (flags & CC_TRACE)
1695 { 1719 {
1696 if (!coro->cctx) 1720 if (!coro->cctx)
1763 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 1787 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1764 double now = nvtime (); 1788 double now = nvtime ();
1765 1789
1766 if (now >= self->next) 1790 if (now >= self->next)
1767 { 1791 {
1768 api_cede (); 1792 api_cede (aTHX);
1769 self->next = now + self->every; 1793 self->next = now + self->every;
1770 } 1794 }
1771 1795
1772 return PerlIOBuf_flush (aTHX_ f); 1796 return PerlIOBuf_flush (aTHX_ f);
1773} 1797}
1802 PerlIOBuf_get_ptr, 1826 PerlIOBuf_get_ptr,
1803 PerlIOBuf_get_cnt, 1827 PerlIOBuf_get_cnt,
1804 PerlIOBuf_set_ptrcnt, 1828 PerlIOBuf_set_ptrcnt,
1805}; 1829};
1806 1830
1831/*****************************************************************************/
1832
1833static const CV *slf_cv; /* for quick consistency check */
1834
1835static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */
1836static SV *slf_arg0;
1837static SV *slf_arg1;
1838
1839/* this restores the stack in the case we patched the entersub, to */
1840/* recreate the stack frame as perl will on following calls */
1841/* since entersub cleared the stack */
1842static OP *
1843pp_restore (pTHX)
1844{
1845 dSP;
1846
1847 PUSHMARK (SP);
1848
1849 EXTEND (SP, 3);
1850 if (slf_arg0) PUSHs (sv_2mortal (slf_arg0));
1851 if (slf_arg1) PUSHs (sv_2mortal (slf_arg1));
1852 PUSHs ((SV *)CvGV (slf_cv));
1853
1854 RETURNOP (slf_restore.op_first);
1855}
1856
1857#define OPpENTERSUB_SLF 15 /* the part of op_private entersub hopefully doesn't use */
1858
1859enum {
1860 CORO_SLF_CUSTOM = 0,
1861 CORO_SLF_SET_STACKLEVEL = 1,
1862 CORO_SLF_TRANSFER = 2
1863};
1864
1865/* declare prototype */
1866XS(XS_Coro__State__set_stacklevel);
1867
1868/*
1869 * these not obviously related functions are all rolled into one
1870 * function to increase chances that they all will call transfer with the same
1871 * stack offset
1872 * SLF stands for "schedule-like-function".
1873 */
1874static OP *
1875pp_slf (pTHX)
1876{
1877 dSP;
1878 struct transfer_args ta;
1879 SV **arg = PL_stack_base + TOPMARK + 1;
1880 int items = SP - arg; /* args without function object */
1881 int ix = PL_op->op_private & OPpENTERSUB_SLF;
1882 struct CoroSLF *slf = 0;
1883 SV *gv = *sp;
1884
1885 /* do a quick consistency check on the "function" object, and if it isn't */
1886 /* for us, divert to the real entersub */
1887 if (SvTYPE (gv) != SVt_PVGV || CvXSUB (GvCV (gv)) != XS_Coro__State__set_stacklevel)
1888 return PL_ppaddr[OP_ENTERSUB](aTHX);
1889
1890 /* pop args */
1891 SP = PL_stack_base + POPMARK;
1892
1893 if (!(PL_op->op_flags & OPf_STACKED))
1894 {
1895 /* ampersand-form of call, use @_ instead of stack */
1896 AV *av = GvAV (PL_defgv);
1897 arg = AvARRAY (av);
1898 items = AvFILLp (av) + 1;
1899 }
1900
1901 PUTBACK;
1902
1903 if (!ix)
1904 {
1905 slf = (struct CoroSLF *)CvXSUBANY (GvCV (gv)).any_ptr;
1906 ix = slf->prepare (aTHX_ arg, items);
1907 }
1908
1909 switch (ix)
1910 {
1911 case CORO_SLF_SET_STACKLEVEL:
1912 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1913 break;
1914
1915 case CORO_SLF_TRANSFER:
1916 if (items != 2)
1917 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1918
1919 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1920 break;
1921
1922 case CORO_SLF_SCHEDULE:
1923 prepare_schedule (aTHX_ &ta);
1924 break;
1925
1926 case CORO_SLF_CEDE:
1927 prepare_cede (aTHX_ &ta);
1928 break;
1929
1930 case CORO_SLF_CEDE_NOTSELF:
1931 prepare_cede_notself (aTHX_ &ta);
1932 break;
1933
1934 default:
1935 abort ();
1936 }
1937
1938 do
1939 TRANSFER (ta, 0);
1940 while (slf && slf->check (aTHX));
1941
1942 SPAGAIN;
1943
1944 PUTBACK;
1945 SLF_TAIL;
1946 SPAGAIN;
1947 RETURN;
1948}
1949
1950static void
1951coro_slf_patch (pTHX_ CV *cv, int ix, SV **args, int items)
1952{
1953 assert (("FATAL: SLF call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_slf));
1954
1955 assert (("FATAL: SLF call with illegal CV value", CvGV (cv)));
1956 slf_cv = cv;
1957
1958 /* we patch the op, and then re-run the whole call */
1959 /* we have to put the same argument on the stack for this to work */
1960 /* and this will be done by pp_restore */
1961 slf_restore.op_next = (OP *)&slf_restore;
1962 slf_restore.op_type = OP_NULL;
1963 slf_restore.op_ppaddr = pp_restore;
1964 slf_restore.op_first = PL_op;
1965
1966 slf_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0;
1967 slf_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0;
1968
1969 PL_op->op_ppaddr = pp_slf;
1970 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SLF | ix; /* we potentially share our private flags with entersub */
1971
1972 PL_op = (OP *)&slf_restore;
1973}
1974
1975static void
1976api_execute_slf (pTHX_ CV *cv, const struct CoroSLF *slf, SV **arg, int items)
1977{
1978 CvXSUBANY (cv).any_ptr = (void *)slf;
1979 coro_slf_patch (aTHX_ cv, CORO_SLF_CUSTOM, arg, items);
1980}
1807 1981
1808MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1982MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1809 1983
1810PROTOTYPES: DISABLE 1984PROTOTYPES: DISABLE
1811 1985
1841 main_top_env = PL_top_env; 2015 main_top_env = PL_top_env;
1842 2016
1843 while (main_top_env->je_prev) 2017 while (main_top_env->je_prev)
1844 main_top_env = main_top_env->je_prev; 2018 main_top_env = main_top_env->je_prev;
1845 2019
1846 coroapi.ver = CORO_API_VERSION; 2020 coroapi.ver = CORO_API_VERSION;
1847 coroapi.rev = CORO_API_REVISION; 2021 coroapi.rev = CORO_API_REVISION;
1848 coroapi.transfer = api_transfer; 2022 coroapi.transfer = api_transfer;
2023 coroapi.execute_slf = api_execute_slf;
1849 2024
1850 { 2025 {
1851 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 2026 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1852 2027
1853 if (!svp) croak ("Time::HiRes is required"); 2028 if (!svp) croak ("Time::HiRes is required");
1886 av_push (coro->args, newSVsv (ST (i))); 2061 av_push (coro->args, newSVsv (ST (i)));
1887} 2062}
1888 OUTPUT: 2063 OUTPUT:
1889 RETVAL 2064 RETVAL
1890 2065
1891# these not obviously related functions are all rolled into the same xs
1892# function to increase chances that they all will call transfer with the same
1893# stack offset
1894void 2066void
1895_set_stacklevel (...) 2067_set_stacklevel (...)
1896 ALIAS: 2068 ALIAS:
2069 _set_stacklevel = CORO_SLF_SET_STACKLEVEL
1897 Coro::State::transfer = 1 2070 Coro::State::transfer = CORO_SLF_TRANSFER
1898 Coro::schedule = 2 2071 Coro::schedule = CORO_SLF_SCHEDULE
1899 Coro::cede = 3 2072 Coro::cede = CORO_SLF_CEDE
1900 Coro::cede_notself = 4 2073 Coro::cede_notself = CORO_SLF_CEDE_NOTSELF
1901 CODE: 2074 CODE:
1902{ 2075 coro_slf_patch (aTHX_ cv, ix, &ST (0), items);
1903 struct transfer_args ta;
1904
1905 PUTBACK;
1906 switch (ix)
1907 {
1908 case 0:
1909 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0)));
1910 break;
1911
1912 case 1:
1913 if (items != 2)
1914 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1915
1916 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1917 break;
1918
1919 case 2:
1920 prepare_schedule (aTHX_ &ta);
1921 break;
1922
1923 case 3:
1924 prepare_cede (aTHX_ &ta);
1925 break;
1926
1927 case 4:
1928 if (!prepare_cede_notself (aTHX_ &ta))
1929 XSRETURN_EMPTY;
1930
1931 break;
1932 }
1933 SPAGAIN;
1934
1935 BARRIER;
1936 PUTBACK;
1937 TRANSFER (ta, 0);
1938 SPAGAIN; /* might be the sp of a different coroutine now */
1939 /* be extra careful not to ever do anything after TRANSFER */
1940}
1941 2076
1942bool 2077bool
1943_destroy (SV *coro_sv) 2078_destroy (SV *coro_sv)
1944 CODE: 2079 CODE:
1945 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2080 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
2063 SvREFCNT_dec (self->throw); 2198 SvREFCNT_dec (self->throw);
2064 self->throw = SvOK (throw) ? newSVsv (throw) : 0; 2199 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2065 2200
2066void 2201void
2067api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2202api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2203 C_ARGS: aTHX_ coro, flags
2068 2204
2069SV * 2205SV *
2070has_cctx (Coro::State coro) 2206has_cctx (Coro::State coro)
2071 PROTOTYPE: $ 2207 PROTOTYPE: $
2072 CODE: 2208 CODE:
2142 2278
2143 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2279 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2144 coro_ready[i] = newAV (); 2280 coro_ready[i] = newAV ();
2145 2281
2146 { 2282 {
2147 SV *sv = perl_get_sv ("Coro::API", TRUE); 2283 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2148 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2149 2284
2150 coroapi.schedule = api_schedule; 2285 coroapi.schedule = api_schedule;
2151 coroapi.cede = api_cede; 2286 coroapi.cede = api_cede;
2152 coroapi.cede_notself = api_cede_notself; 2287 coroapi.cede_notself = api_cede_notself;
2153 coroapi.ready = api_ready; 2288 coroapi.ready = api_ready;
2154 coroapi.is_ready = api_is_ready; 2289 coroapi.is_ready = api_is_ready;
2155 coroapi.nready = &coro_nready; 2290 coroapi.nready = coro_nready;
2156 coroapi.current = coro_current; 2291 coroapi.current = coro_current;
2157 2292
2158 GCoroAPI = &coroapi; 2293 GCoroAPI = &coroapi;
2159 sv_setiv (sv, (IV)&coroapi); 2294 sv_setiv (sv, (IV)&coroapi);
2160 SvREADONLY_on (sv); 2295 SvREADONLY_on (sv);
2201 2336
2202SV * 2337SV *
2203ready (SV *self) 2338ready (SV *self)
2204 PROTOTYPE: $ 2339 PROTOTYPE: $
2205 CODE: 2340 CODE:
2206 RETVAL = boolSV (api_ready (self)); 2341 RETVAL = boolSV (api_ready (aTHX_ self));
2207 OUTPUT: 2342 OUTPUT:
2208 RETVAL 2343 RETVAL
2209 2344
2210int 2345int
2211nready (...) 2346nready (...)
2281 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 2416 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2282 2417
2283 coro->prio = 0; 2418 coro->prio = 0;
2284 2419
2285 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2420 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2286 api_trace (coro_current, 0); 2421 api_trace (aTHX_ coro_current, 0);
2287 2422
2288 av_push (av_async_pool, newSVsv (coro_current)); 2423 av_push (av_async_pool, newSVsv (coro_current));
2289} 2424}
2290 2425
2291#if 0 2426#if 0
2363 2498
2364 av_push (av, data_sv); 2499 av_push (av, data_sv);
2365 2500
2366 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 2501 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2367 2502
2368 api_ready (self); 2503 api_ready (aTHX_ self);
2369} 2504}
2370 2505
2371void 2506void
2372_set_state (SV *state) 2507_set_state (SV *state)
2373 PROTOTYPE: $ 2508 PROTOTYPE: $
2398 PROTOTYPE: @ 2533 PROTOTYPE: @
2399 CODE: 2534 CODE:
2400{ 2535{
2401 static int incede; 2536 static int incede;
2402 2537
2403 api_cede_notself (); 2538 api_cede_notself (aTHX);
2404 2539
2405 ++incede; 2540 ++incede;
2406 while (coro_nready >= incede && api_cede ()) 2541 while (coro_nready >= incede && api_cede (aTHX))
2407 ; 2542 ;
2408 2543
2409 sv_setsv (sv_activity, &PL_sv_undef); 2544 sv_setsv (sv_activity, &PL_sv_undef);
2410 if (coro_nready >= incede) 2545 if (coro_nready >= incede)
2411 { 2546 {
2421 2556
2422MODULE = Coro::State PACKAGE = PerlIO::cede 2557MODULE = Coro::State PACKAGE = PerlIO::cede
2423 2558
2424BOOT: 2559BOOT:
2425 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2560 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2561

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines