… | |
… | |
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 */ |
57 | static int cctx_max_idle = 4; |
54 | static int cctx_max_idle = 4; |
58 | |
55 | |
… | |
… | |
126 | |
123 | |
127 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
124 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
128 | |
125 | |
129 | #if __GNUC__ >= 3 |
126 | #if __GNUC__ >= 3 |
130 | # define attribute(x) __attribute__(x) |
127 | # define attribute(x) __attribute__(x) |
131 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
|
|
132 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
128 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
129 | # define INLINE static inline |
133 | #else |
130 | #else |
134 | # define attribute(x) |
131 | # define attribute(x) |
135 | # define BARRIER |
|
|
136 | # define expect(expr,value) (expr) |
132 | # define expect(expr,value) (expr) |
|
|
133 | # define INLINE static |
137 | #endif |
134 | #endif |
138 | |
135 | |
139 | #define expect_false(expr) expect ((expr) != 0, 0) |
136 | #define expect_false(expr) expect ((expr) != 0, 0) |
140 | #define expect_true(expr) expect ((expr) != 0, 1) |
137 | #define expect_true(expr) expect ((expr) != 0, 1) |
141 | |
138 | |
… | |
… | |
413 | : 0 |
410 | : 0 |
414 | |
411 | |
415 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
412 | #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) |
413 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
417 | |
414 | |
418 | static struct coro * |
415 | INLINE struct coro * |
419 | SvSTATE_ (pTHX_ SV *coro) |
416 | SvSTATE_ (pTHX_ SV *coro) |
420 | { |
417 | { |
421 | HV *stash; |
418 | HV *stash; |
422 | MAGIC *mg; |
419 | MAGIC *mg; |
423 | |
420 | |
… | |
… | |
714 | } |
711 | } |
715 | |
712 | |
716 | return rss; |
713 | return rss; |
717 | } |
714 | } |
718 | |
715 | |
|
|
716 | /** set stacklevel support **************************************************/ |
|
|
717 | |
|
|
718 | /* we sometimes need to create the effect of pp_set_stacklevel calling us */ |
|
|
719 | #define SSL_HEAD (void)0 |
|
|
720 | /* we sometimes need to create the effect of leaving via pp_set_stacklevel */ |
|
|
721 | #define SSL_TAIL set_stacklevel_tail (aTHX) |
|
|
722 | |
|
|
723 | INLINE void |
|
|
724 | set_stacklevel_tail (pTHX) |
|
|
725 | { |
|
|
726 | dSP; |
|
|
727 | SV **bot = SP; |
|
|
728 | |
|
|
729 | int gimme = GIMME_V; |
|
|
730 | |
|
|
731 | /* make sure we put something on the stack in scalar context */ |
|
|
732 | if (gimme == G_SCALAR) |
|
|
733 | { |
|
|
734 | if (sp == bot) |
|
|
735 | XPUSHs (&PL_sv_undef); |
|
|
736 | |
|
|
737 | SP = bot + 1; |
|
|
738 | } |
|
|
739 | |
|
|
740 | PUTBACK; |
|
|
741 | } |
|
|
742 | |
719 | /** coroutine stack handling ************************************************/ |
743 | /** coroutine stack handling ************************************************/ |
720 | |
744 | |
721 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
745 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
722 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
746 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
723 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
747 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
… | |
… | |
855 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
879 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
856 | SPAGAIN; |
880 | SPAGAIN; |
857 | } |
881 | } |
858 | |
882 | |
859 | /* this newly created coroutine might be run on an existing cctx which most |
883 | /* this newly created coroutine might be run on an existing cctx which most |
860 | * likely was suspended in set_stacklevel, called from entersub. |
884 | * likely was suspended in set_stacklevel, called from pp_set_stacklevel, |
861 | * set_stacklevel doesn't do anything on return, but entersub does LEAVE, |
885 | * so we have to emulate entering pp_set_stacklevel here. |
862 | * so we ENTER here for symmetry. |
|
|
863 | */ |
886 | */ |
864 | ENTER; |
887 | SSL_HEAD; |
865 | } |
888 | } |
866 | |
889 | |
867 | static void |
890 | static void |
868 | coro_destruct (pTHX_ struct coro *coro) |
891 | coro_destruct (pTHX_ struct coro *coro) |
869 | { |
892 | { |
… | |
… | |
898 | SvREFCNT_dec (coro->throw); |
921 | SvREFCNT_dec (coro->throw); |
899 | |
922 | |
900 | coro_destruct_stacks (aTHX); |
923 | coro_destruct_stacks (aTHX); |
901 | } |
924 | } |
902 | |
925 | |
903 | static void |
926 | INLINE void |
904 | free_coro_mortal (pTHX) |
927 | free_coro_mortal (pTHX) |
905 | { |
928 | { |
906 | if (expect_true (coro_mortal)) |
929 | if (expect_true (coro_mortal)) |
907 | { |
930 | { |
908 | SvREFCNT_dec (coro_mortal); |
931 | SvREFCNT_dec (coro_mortal); |
… | |
… | |
1069 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1092 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1070 | SPAGAIN; |
1093 | SPAGAIN; |
1071 | } |
1094 | } |
1072 | |
1095 | |
1073 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1096 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1074 | static void |
1097 | INLINE void |
1075 | transfer_tail (pTHX) |
1098 | transfer_tail (pTHX) |
1076 | { |
1099 | { |
1077 | struct coro *next = (struct coro *)transfer_next; |
1100 | struct coro *next = (struct coro *)transfer_next; |
1078 | transfer_next = 0; //D for temporary assertion in transfer |
1101 | 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 |
1102 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
1080 | |
1103 | |
1081 | free_coro_mortal (aTHX); |
1104 | free_coro_mortal (aTHX); |
1082 | UNLOCK; |
1105 | UNLOCK; |
1083 | |
1106 | |
1084 | if (expect_false (next->throw)) |
1107 | if (expect_false (next->throw)) |
… | |
… | |
1103 | # endif |
1126 | # endif |
1104 | #endif |
1127 | #endif |
1105 | { |
1128 | { |
1106 | dTHX; |
1129 | dTHX; |
1107 | |
1130 | |
1108 | /* entersub called ENTER, but we never 'returned', undo that here */ |
1131 | /* we are the alternative tail to pp_set_stacklevel */ |
1109 | LEAVE; |
1132 | /* so do the same things here */ |
|
|
1133 | SSL_TAIL; |
1110 | |
1134 | |
1111 | /* we now skip the entersub that did lead to transfer() */ |
1135 | /* we now skip the op that did lead to transfer() */ |
1112 | PL_op = PL_op->op_next; |
1136 | PL_op = PL_op->op_next; |
1113 | |
1137 | |
1114 | /* inject a fake subroutine call to cctx_init */ |
1138 | /* inject a fake subroutine call to cctx_init */ |
1115 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1139 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1116 | |
1140 | |
… | |
… | |
1173 | /* mmap supposedly does allocate-on-write for us */ |
1197 | /* 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); |
1198 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1175 | |
1199 | |
1176 | if (cctx->sptr != (void *)-1) |
1200 | if (cctx->sptr != (void *)-1) |
1177 | { |
1201 | { |
1178 | # if CORO_STACKGUARD |
1202 | #if CORO_STACKGUARD |
1179 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1203 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1180 | # endif |
1204 | #endif |
1181 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
1205 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
1182 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1206 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1183 | cctx->flags |= CC_MAPPED; |
1207 | cctx->flags |= CC_MAPPED; |
1184 | } |
1208 | } |
1185 | else |
1209 | else |
1186 | #endif |
1210 | #endif |
1187 | { |
1211 | { |
1188 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1212 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1189 | New (0, cctx->sptr, cctx_stacksize, long); |
1213 | New (0, cctx->sptr, cctx_stacksize, long); |
1190 | |
1214 | |
1191 | if (!cctx->sptr) |
1215 | if (!cctx->sptr) |
1192 | { |
1216 | { |
1193 | perror ("FATAL: unable to allocate stack for coroutine"); |
1217 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1194 | _exit (EXIT_FAILURE); |
1218 | _exit (EXIT_FAILURE); |
1195 | } |
1219 | } |
1196 | |
1220 | |
1197 | stack_start = cctx->sptr; |
1221 | stack_start = cctx->sptr; |
1198 | stack_size = cctx->ssize; |
1222 | stack_size = cctx->ssize; |
1199 | } |
1223 | } |
1200 | |
1224 | |
1201 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1225 | #if CORO_USE_VALGRIND |
|
|
1226 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1227 | #endif |
|
|
1228 | |
1202 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1229 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1203 | |
1230 | |
1204 | return cctx; |
1231 | return cctx; |
1205 | } |
1232 | } |
1206 | |
1233 | |
… | |
… | |
1214 | coro_destroy (&cctx->cctx); |
1241 | coro_destroy (&cctx->cctx); |
1215 | |
1242 | |
1216 | /* coro_transfer creates new, empty cctx's */ |
1243 | /* coro_transfer creates new, empty cctx's */ |
1217 | if (cctx->sptr) |
1244 | if (cctx->sptr) |
1218 | { |
1245 | { |
1219 | #if CORO_USE_VALGRIND |
1246 | #if CORO_USE_VALGRIND |
1220 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1247 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1221 | #endif |
1248 | #endif |
1222 | |
1249 | |
1223 | #if HAVE_MMAP |
1250 | #if HAVE_MMAP |
1224 | if (cctx->flags & CC_MAPPED) |
1251 | if (cctx->flags & CC_MAPPED) |
1225 | munmap (cctx->sptr, cctx->ssize); |
1252 | munmap (cctx->sptr, cctx->ssize); |
1226 | else |
1253 | else |
… | |
… | |
1253 | } |
1280 | } |
1254 | |
1281 | |
1255 | static void |
1282 | static void |
1256 | cctx_put (coro_cctx *cctx) |
1283 | cctx_put (coro_cctx *cctx) |
1257 | { |
1284 | { |
1258 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
1285 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1259 | |
1286 | |
1260 | /* free another cctx if overlimit */ |
1287 | /* free another cctx if overlimit */ |
1261 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1288 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1262 | { |
1289 | { |
1263 | coro_cctx *first = cctx_first; |
1290 | coro_cctx *first = cctx_first; |
… | |
… | |
1345 | && !(prev__cctx->flags & CC_TRACE) |
1372 | && !(prev__cctx->flags & CC_TRACE) |
1346 | && !force_cctx |
1373 | && !force_cctx |
1347 | )) |
1374 | )) |
1348 | { |
1375 | { |
1349 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1376 | /* 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)); |
1377 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1351 | |
1378 | |
1352 | prev->cctx = 0; |
1379 | prev->cctx = 0; |
1353 | |
1380 | |
1354 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1381 | /* 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 */ |
1382 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
… | |
… | |
1363 | ++next->usecount; |
1390 | ++next->usecount; |
1364 | |
1391 | |
1365 | if (expect_true (!next->cctx)) |
1392 | if (expect_true (!next->cctx)) |
1366 | next->cctx = cctx_get (aTHX); |
1393 | next->cctx = cctx_get (aTHX); |
1367 | |
1394 | |
1368 | assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D |
1395 | assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); |
1369 | transfer_next = next; |
1396 | transfer_next = next; |
1370 | |
1397 | |
1371 | if (expect_false (prev__cctx != next->cctx)) |
1398 | if (expect_false (prev__cctx != next->cctx)) |
1372 | { |
1399 | { |
1373 | prev__cctx->top_env = PL_top_env; |
1400 | prev__cctx->top_env = PL_top_env; |
… | |
… | |
1558 | |
1585 | |
1559 | static int |
1586 | static int |
1560 | api_is_ready (SV *coro_sv) |
1587 | api_is_ready (SV *coro_sv) |
1561 | { |
1588 | { |
1562 | dTHX; |
1589 | dTHX; |
|
|
1590 | |
1563 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1591 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1564 | } |
1592 | } |
1565 | |
1593 | |
1566 | static void |
1594 | INLINE void |
1567 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1595 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1568 | { |
1596 | { |
1569 | SV *prev_sv, *next_sv; |
1597 | SV *prev_sv, *next_sv; |
1570 | |
1598 | |
1571 | for (;;) |
1599 | for (;;) |
… | |
… | |
1610 | |
1638 | |
1611 | /* free this only after the transfer */ |
1639 | /* free this only after the transfer */ |
1612 | prev_sv = SvRV (coro_current); |
1640 | prev_sv = SvRV (coro_current); |
1613 | ta->prev = SvSTATE (prev_sv); |
1641 | ta->prev = SvSTATE (prev_sv); |
1614 | TRANSFER_CHECK (*ta); |
1642 | TRANSFER_CHECK (*ta); |
1615 | assert (ta->next->flags & CF_READY); |
1643 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1616 | ta->next->flags &= ~CF_READY; |
1644 | ta->next->flags &= ~CF_READY; |
1617 | SvRV_set (coro_current, next_sv); |
1645 | SvRV_set (coro_current, next_sv); |
1618 | |
1646 | |
1619 | LOCK; |
1647 | LOCK; |
1620 | free_coro_mortal (aTHX); |
1648 | free_coro_mortal (aTHX); |
1621 | coro_mortal = prev_sv; |
1649 | coro_mortal = prev_sv; |
1622 | UNLOCK; |
1650 | UNLOCK; |
1623 | } |
1651 | } |
1624 | |
1652 | |
1625 | static void |
1653 | INLINE void |
1626 | prepare_cede (pTHX_ struct transfer_args *ta) |
1654 | prepare_cede (pTHX_ struct transfer_args *ta) |
1627 | { |
1655 | { |
1628 | api_ready (coro_current); |
1656 | api_ready (coro_current); |
1629 | prepare_schedule (aTHX_ ta); |
1657 | prepare_schedule (aTHX_ ta); |
1630 | } |
1658 | } |
1631 | |
1659 | |
1632 | static int |
1660 | static void |
1633 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1661 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1634 | { |
1662 | { |
|
|
1663 | SV *prev = SvRV (coro_current); |
|
|
1664 | |
1635 | if (coro_nready) |
1665 | if (coro_nready) |
1636 | { |
1666 | { |
1637 | SV *prev = SvRV (coro_current); |
|
|
1638 | prepare_schedule (aTHX_ ta); |
1667 | prepare_schedule (aTHX_ ta); |
1639 | api_ready (prev); |
1668 | api_ready (prev); |
|
|
1669 | } |
|
|
1670 | else |
|
|
1671 | ta->prev = ta->next = SvSTATE (prev); |
|
|
1672 | } |
|
|
1673 | |
|
|
1674 | static void |
|
|
1675 | api_schedule (void) |
|
|
1676 | { |
|
|
1677 | dTHX; |
|
|
1678 | struct transfer_args ta; |
|
|
1679 | |
|
|
1680 | prepare_schedule (aTHX_ &ta); |
|
|
1681 | TRANSFER (ta, 1); |
|
|
1682 | } |
|
|
1683 | |
|
|
1684 | static int |
|
|
1685 | api_cede (void) |
|
|
1686 | { |
|
|
1687 | dTHX; |
|
|
1688 | struct transfer_args ta; |
|
|
1689 | |
|
|
1690 | prepare_cede (aTHX_ &ta); |
|
|
1691 | |
|
|
1692 | if (expect_true (ta.prev != ta.next)) |
|
|
1693 | { |
|
|
1694 | TRANSFER (ta, 1); |
1640 | return 1; |
1695 | return 1; |
1641 | } |
1696 | } |
1642 | else |
1697 | else |
1643 | return 0; |
1698 | return 0; |
1644 | } |
1699 | } |
1645 | |
1700 | |
1646 | static void |
|
|
1647 | api_schedule (void) |
|
|
1648 | { |
|
|
1649 | dTHX; |
|
|
1650 | struct transfer_args ta; |
|
|
1651 | |
|
|
1652 | prepare_schedule (aTHX_ &ta); |
|
|
1653 | TRANSFER (ta, 1); |
|
|
1654 | } |
|
|
1655 | |
|
|
1656 | static int |
|
|
1657 | api_cede (void) |
|
|
1658 | { |
|
|
1659 | dTHX; |
|
|
1660 | struct transfer_args ta; |
|
|
1661 | |
|
|
1662 | prepare_cede (aTHX_ &ta); |
|
|
1663 | |
|
|
1664 | if (expect_true (ta.prev != ta.next)) |
|
|
1665 | { |
|
|
1666 | TRANSFER (ta, 1); |
|
|
1667 | return 1; |
|
|
1668 | } |
|
|
1669 | else |
|
|
1670 | return 0; |
|
|
1671 | } |
|
|
1672 | |
|
|
1673 | static int |
1701 | static int |
1674 | api_cede_notself (void) |
1702 | api_cede_notself (void) |
1675 | { |
1703 | { |
|
|
1704 | if (coro_nready) |
|
|
1705 | { |
1676 | dTHX; |
1706 | dTHX; |
1677 | struct transfer_args ta; |
1707 | struct transfer_args ta; |
1678 | |
1708 | |
1679 | if (prepare_cede_notself (aTHX_ &ta)) |
1709 | prepare_cede_notself (aTHX_ &ta); |
1680 | { |
|
|
1681 | TRANSFER (ta, 1); |
1710 | TRANSFER (ta, 1); |
1682 | return 1; |
1711 | return 1; |
1683 | } |
1712 | } |
1684 | else |
1713 | else |
1685 | return 0; |
1714 | return 0; |
… | |
… | |
1802 | PerlIOBuf_get_ptr, |
1831 | PerlIOBuf_get_ptr, |
1803 | PerlIOBuf_get_cnt, |
1832 | PerlIOBuf_get_cnt, |
1804 | PerlIOBuf_set_ptrcnt, |
1833 | PerlIOBuf_set_ptrcnt, |
1805 | }; |
1834 | }; |
1806 | |
1835 | |
|
|
1836 | /*****************************************************************************/ |
|
|
1837 | |
|
|
1838 | static const CV *ssl_cv; /* for quick consistency check */ |
|
|
1839 | |
|
|
1840 | static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */ |
|
|
1841 | static SV *ssl_arg0; |
|
|
1842 | static SV *ssl_arg1; |
|
|
1843 | |
|
|
1844 | /* this restores the stack in the case we patched the entersub, to */ |
|
|
1845 | /* recreate the stack frame as perl will on following calls */ |
|
|
1846 | /* since entersub cleared the stack */ |
|
|
1847 | static OP * |
|
|
1848 | pp_restore (pTHX) |
|
|
1849 | { |
|
|
1850 | dSP; |
|
|
1851 | |
|
|
1852 | PUSHMARK (SP); |
|
|
1853 | |
|
|
1854 | EXTEND (SP, 3); |
|
|
1855 | if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0; |
|
|
1856 | if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0; |
|
|
1857 | PUSHs ((SV *)CvGV (ssl_cv)); |
|
|
1858 | |
|
|
1859 | RETURNOP (ssl_restore.op_first); |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | #define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */ |
|
|
1863 | |
|
|
1864 | /* declare prototype */ |
|
|
1865 | XS(XS_Coro__State__set_stacklevel); |
|
|
1866 | |
|
|
1867 | /* |
|
|
1868 | * these not obviously related functions are all rolled into one |
|
|
1869 | * function to increase chances that they all will call transfer with the same |
|
|
1870 | * stack offset |
|
|
1871 | */ |
|
|
1872 | static OP * |
|
|
1873 | pp_set_stacklevel (pTHX) |
|
|
1874 | { |
|
|
1875 | dSP; |
|
|
1876 | struct transfer_args ta; |
|
|
1877 | SV **arg = PL_stack_base + TOPMARK + 1; |
|
|
1878 | int items = SP - arg; /* args without function object */ |
|
|
1879 | |
|
|
1880 | /* do a quick consistency check on the "function" object, and if it isn't */ |
|
|
1881 | /* for us, divert to the real entersub */ |
|
|
1882 | if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel) |
|
|
1883 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
1884 | |
|
|
1885 | /* pop args */ |
|
|
1886 | SP = PL_stack_base + POPMARK; |
|
|
1887 | |
|
|
1888 | if (!(PL_op->op_flags & OPf_STACKED)) |
|
|
1889 | { |
|
|
1890 | /* ampersand-form of call, use @_ instead of stack */ |
|
|
1891 | AV *av = GvAV (PL_defgv); |
|
|
1892 | arg = AvARRAY (av); |
|
|
1893 | items = AvFILLp (av) + 1; |
|
|
1894 | } |
|
|
1895 | |
|
|
1896 | PUTBACK; |
|
|
1897 | switch (PL_op->op_private & OPpENTERSUB_SSL) |
|
|
1898 | { |
|
|
1899 | case 0: |
|
|
1900 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0])); |
|
|
1901 | break; |
|
|
1902 | |
|
|
1903 | case 1: |
|
|
1904 | if (items != 2) |
|
|
1905 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items); |
|
|
1906 | |
|
|
1907 | prepare_transfer (aTHX_ &ta, arg [0], arg [1]); |
|
|
1908 | break; |
|
|
1909 | |
|
|
1910 | case 2: |
|
|
1911 | prepare_schedule (aTHX_ &ta); |
|
|
1912 | break; |
|
|
1913 | |
|
|
1914 | case 3: |
|
|
1915 | prepare_cede (aTHX_ &ta); |
|
|
1916 | break; |
|
|
1917 | |
|
|
1918 | case 4: |
|
|
1919 | prepare_cede_notself (aTHX_ &ta); |
|
|
1920 | break; |
|
|
1921 | } |
|
|
1922 | |
|
|
1923 | TRANSFER (ta, 0); |
|
|
1924 | SPAGAIN; |
|
|
1925 | |
|
|
1926 | skip: |
|
|
1927 | PUTBACK; |
|
|
1928 | SSL_TAIL; |
|
|
1929 | SPAGAIN; |
|
|
1930 | RETURN; |
|
|
1931 | } |
|
|
1932 | |
|
|
1933 | static void |
|
|
1934 | coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items) |
|
|
1935 | { |
|
|
1936 | assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel)); |
|
|
1937 | |
|
|
1938 | assert (("FATAL: ssl call with illegal CV value", CvGV (cv))); |
|
|
1939 | ssl_cv = cv; |
|
|
1940 | |
|
|
1941 | /* we patch the op, and then re-run the whole call */ |
|
|
1942 | /* we have to put some dummy argument on the stack for this to work */ |
|
|
1943 | ssl_restore.op_next = (OP *)&ssl_restore; |
|
|
1944 | ssl_restore.op_type = OP_NULL; |
|
|
1945 | ssl_restore.op_ppaddr = pp_restore; |
|
|
1946 | ssl_restore.op_first = PL_op; |
|
|
1947 | |
|
|
1948 | ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0; |
|
|
1949 | ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0; |
|
|
1950 | |
|
|
1951 | PL_op->op_ppaddr = pp_set_stacklevel; |
|
|
1952 | PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */ |
|
|
1953 | |
|
|
1954 | PL_op = (OP *)&ssl_restore; |
|
|
1955 | } |
1807 | |
1956 | |
1808 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1957 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1809 | |
1958 | |
1810 | PROTOTYPES: DISABLE |
1959 | PROTOTYPES: DISABLE |
1811 | |
1960 | |
… | |
… | |
1886 | av_push (coro->args, newSVsv (ST (i))); |
2035 | av_push (coro->args, newSVsv (ST (i))); |
1887 | } |
2036 | } |
1888 | OUTPUT: |
2037 | OUTPUT: |
1889 | RETVAL |
2038 | RETVAL |
1890 | |
2039 | |
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 |
|
|
1894 | void |
2040 | void |
1895 | _set_stacklevel (...) |
2041 | _set_stacklevel (...) |
1896 | ALIAS: |
2042 | ALIAS: |
1897 | Coro::State::transfer = 1 |
2043 | Coro::State::transfer = 1 |
1898 | Coro::schedule = 2 |
2044 | Coro::schedule = 2 |
1899 | Coro::cede = 3 |
2045 | Coro::cede = 3 |
1900 | Coro::cede_notself = 4 |
2046 | Coro::cede_notself = 4 |
1901 | CODE: |
2047 | CODE: |
1902 | { |
2048 | coro_ssl_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 | |
2049 | |
1942 | bool |
2050 | bool |
1943 | _destroy (SV *coro_sv) |
2051 | _destroy (SV *coro_sv) |
1944 | CODE: |
2052 | CODE: |
1945 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2053 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
2142 | |
2250 | |
2143 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
2251 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
2144 | coro_ready[i] = newAV (); |
2252 | coro_ready[i] = newAV (); |
2145 | |
2253 | |
2146 | { |
2254 | { |
2147 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
2255 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2148 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
|
|
2149 | |
2256 | |
2150 | coroapi.schedule = api_schedule; |
2257 | coroapi.schedule = api_schedule; |
2151 | coroapi.cede = api_cede; |
2258 | coroapi.cede = api_cede; |
2152 | coroapi.cede_notself = api_cede_notself; |
2259 | coroapi.cede_notself = api_cede_notself; |
2153 | coroapi.ready = api_ready; |
2260 | coroapi.ready = api_ready; |
… | |
… | |
2421 | |
2528 | |
2422 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2529 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2423 | |
2530 | |
2424 | BOOT: |
2531 | BOOT: |
2425 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2532 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
|
|
2533 | |