… | |
… | |
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 | |
… | |
… | |
727 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
724 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
728 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
725 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
729 | (const char*)(mg)->mg_ptr) |
726 | (const char*)(mg)->mg_ptr) |
730 | #endif |
727 | #endif |
731 | |
728 | |
|
|
729 | /* we sometimes need to create the effect of entersub calling us */ |
|
|
730 | #define SSL_HEAD (void)0 |
|
|
731 | /* we somtimes need to create the effect of leaving via entersub */ |
|
|
732 | #define SSL_TAIL (void)0 |
|
|
733 | |
732 | /* |
734 | /* |
733 | * This overrides the default magic get method of %SIG elements. |
735 | * This overrides the default magic get method of %SIG elements. |
734 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
736 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
735 | * and instead of tryign to save and restore the hash elements, we just provide |
737 | * and instead of tryign to save and restore the hash elements, we just provide |
736 | * readback here. |
738 | * readback here. |
… | |
… | |
859 | /* this newly created coroutine might be run on an existing cctx which most |
861 | /* this newly created coroutine might be run on an existing cctx which most |
860 | * likely was suspended in set_stacklevel, called from entersub. |
862 | * likely was suspended in set_stacklevel, called from entersub. |
861 | * set_stacklevel doesn't do anything on return, but entersub does LEAVE, |
863 | * set_stacklevel doesn't do anything on return, but entersub does LEAVE, |
862 | * so we ENTER here for symmetry. |
864 | * so we ENTER here for symmetry. |
863 | */ |
865 | */ |
864 | ENTER; |
866 | SSL_HEAD; |
865 | } |
867 | } |
866 | |
868 | |
867 | static void |
869 | static void |
868 | coro_destruct (pTHX_ struct coro *coro) |
870 | coro_destruct (pTHX_ struct coro *coro) |
869 | { |
871 | { |
… | |
… | |
898 | SvREFCNT_dec (coro->throw); |
900 | SvREFCNT_dec (coro->throw); |
899 | |
901 | |
900 | coro_destruct_stacks (aTHX); |
902 | coro_destruct_stacks (aTHX); |
901 | } |
903 | } |
902 | |
904 | |
903 | static void |
905 | INLINE void |
904 | free_coro_mortal (pTHX) |
906 | free_coro_mortal (pTHX) |
905 | { |
907 | { |
906 | if (expect_true (coro_mortal)) |
908 | if (expect_true (coro_mortal)) |
907 | { |
909 | { |
908 | SvREFCNT_dec (coro_mortal); |
910 | SvREFCNT_dec (coro_mortal); |
… | |
… | |
1068 | PL_op = (OP *)&myop; |
1070 | PL_op = (OP *)&myop; |
1069 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1071 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1070 | SPAGAIN; |
1072 | SPAGAIN; |
1071 | } |
1073 | } |
1072 | |
1074 | |
1073 | /* the tail of transfer: execute stuff we can onyl do afetr a transfer */ |
1075 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1074 | static void |
1076 | INLINE void |
1075 | transfer_tail (void) |
1077 | transfer_tail (pTHX) |
1076 | { |
1078 | { |
1077 | struct coro *next = (struct coro *)transfer_next; |
1079 | struct coro *next = (struct coro *)transfer_next; |
1078 | transfer_next = 0; //D for temporary assertion in transfer |
1080 | 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 |
1081 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
1080 | |
1082 | |
1081 | free_coro_mortal (aTHX); |
1083 | free_coro_mortal (aTHX); |
1082 | UNLOCK; |
1084 | UNLOCK; |
1083 | |
1085 | |
1084 | if (expect_false (next->throw)) |
1086 | if (expect_false (next->throw)) |
… | |
… | |
1104 | #endif |
1106 | #endif |
1105 | { |
1107 | { |
1106 | dTHX; |
1108 | dTHX; |
1107 | |
1109 | |
1108 | /* entersub called ENTER, but we never 'returned', undo that here */ |
1110 | /* entersub called ENTER, but we never 'returned', undo that here */ |
1109 | LEAVE; |
1111 | SSL_TAIL; |
1110 | |
1112 | |
1111 | /* we now skip the entersub that did lead to transfer() */ |
1113 | /* we now skip the op that did lead to transfer() */ |
1112 | PL_op = PL_op->op_next; |
1114 | PL_op = PL_op->op_next; |
1113 | |
1115 | |
1114 | /* inject a fake subroutine call to cctx_init */ |
1116 | /* inject a fake subroutine call to cctx_init */ |
1115 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1117 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1116 | |
1118 | |
1117 | /* cctx_run is the alternative tail of transfer() */ |
1119 | /* cctx_run is the alternative tail of transfer() */ |
1118 | transfer_tail (); |
1120 | transfer_tail (aTHX); |
1119 | |
1121 | |
1120 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1122 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1121 | PL_restartop = PL_op; |
1123 | PL_restartop = PL_op; |
1122 | perl_run (PL_curinterp); |
1124 | perl_run (PL_curinterp); |
1123 | |
1125 | |
… | |
… | |
1173 | /* mmap supposedly does allocate-on-write for us */ |
1175 | /* 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); |
1176 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1175 | |
1177 | |
1176 | if (cctx->sptr != (void *)-1) |
1178 | if (cctx->sptr != (void *)-1) |
1177 | { |
1179 | { |
1178 | # if CORO_STACKGUARD |
1180 | #if CORO_STACKGUARD |
1179 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1181 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1180 | # endif |
1182 | #endif |
1181 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
1183 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
1182 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1184 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1183 | cctx->flags |= CC_MAPPED; |
1185 | cctx->flags |= CC_MAPPED; |
1184 | } |
1186 | } |
1185 | else |
1187 | else |
1186 | #endif |
1188 | #endif |
1187 | { |
1189 | { |
1188 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1190 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1189 | New (0, cctx->sptr, cctx_stacksize, long); |
1191 | New (0, cctx->sptr, cctx_stacksize, long); |
1190 | |
1192 | |
1191 | if (!cctx->sptr) |
1193 | if (!cctx->sptr) |
1192 | { |
1194 | { |
1193 | perror ("FATAL: unable to allocate stack for coroutine"); |
1195 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1194 | _exit (EXIT_FAILURE); |
1196 | _exit (EXIT_FAILURE); |
1195 | } |
1197 | } |
1196 | |
1198 | |
1197 | stack_start = cctx->sptr; |
1199 | stack_start = cctx->sptr; |
1198 | stack_size = cctx->ssize; |
1200 | stack_size = cctx->ssize; |
1199 | } |
1201 | } |
1200 | |
1202 | |
1201 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1203 | #if CORO_USE_VALGRIND |
|
|
1204 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1205 | #endif |
|
|
1206 | |
1202 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1207 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1203 | |
1208 | |
1204 | return cctx; |
1209 | return cctx; |
1205 | } |
1210 | } |
1206 | |
1211 | |
… | |
… | |
1214 | coro_destroy (&cctx->cctx); |
1219 | coro_destroy (&cctx->cctx); |
1215 | |
1220 | |
1216 | /* coro_transfer creates new, empty cctx's */ |
1221 | /* coro_transfer creates new, empty cctx's */ |
1217 | if (cctx->sptr) |
1222 | if (cctx->sptr) |
1218 | { |
1223 | { |
1219 | #if CORO_USE_VALGRIND |
1224 | #if CORO_USE_VALGRIND |
1220 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1225 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1221 | #endif |
1226 | #endif |
1222 | |
1227 | |
1223 | #if HAVE_MMAP |
1228 | #if HAVE_MMAP |
1224 | if (cctx->flags & CC_MAPPED) |
1229 | if (cctx->flags & CC_MAPPED) |
1225 | munmap (cctx->sptr, cctx->ssize); |
1230 | munmap (cctx->sptr, cctx->ssize); |
1226 | else |
1231 | else |
… | |
… | |
1253 | } |
1258 | } |
1254 | |
1259 | |
1255 | static void |
1260 | static void |
1256 | cctx_put (coro_cctx *cctx) |
1261 | cctx_put (coro_cctx *cctx) |
1257 | { |
1262 | { |
1258 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
1263 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1259 | |
1264 | |
1260 | /* free another cctx if overlimit */ |
1265 | /* free another cctx if overlimit */ |
1261 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1266 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1262 | { |
1267 | { |
1263 | coro_cctx *first = cctx_first; |
1268 | coro_cctx *first = cctx_first; |
… | |
… | |
1345 | && !(prev__cctx->flags & CC_TRACE) |
1350 | && !(prev__cctx->flags & CC_TRACE) |
1346 | && !force_cctx |
1351 | && !force_cctx |
1347 | )) |
1352 | )) |
1348 | { |
1353 | { |
1349 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1354 | /* 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)); |
1355 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1351 | |
1356 | |
1352 | prev->cctx = 0; |
1357 | prev->cctx = 0; |
1353 | |
1358 | |
1354 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1359 | /* 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 */ |
1360 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
… | |
… | |
1363 | ++next->usecount; |
1368 | ++next->usecount; |
1364 | |
1369 | |
1365 | if (expect_true (!next->cctx)) |
1370 | if (expect_true (!next->cctx)) |
1366 | next->cctx = cctx_get (aTHX); |
1371 | next->cctx = cctx_get (aTHX); |
1367 | |
1372 | |
1368 | assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D |
1373 | assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); |
1369 | transfer_next = next; |
1374 | transfer_next = next; |
1370 | |
1375 | |
1371 | if (expect_false (prev__cctx != next->cctx)) |
1376 | if (expect_false (prev__cctx != next->cctx)) |
1372 | { |
1377 | { |
1373 | prev__cctx->top_env = PL_top_env; |
1378 | prev__cctx->top_env = PL_top_env; |
1374 | PL_top_env = next->cctx->top_env; |
1379 | PL_top_env = next->cctx->top_env; |
1375 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1380 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1376 | } |
1381 | } |
1377 | |
1382 | |
1378 | transfer_tail (); |
1383 | transfer_tail (aTHX); |
1379 | } |
1384 | } |
1380 | } |
1385 | } |
1381 | |
1386 | |
1382 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1387 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1383 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1388 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
… | |
… | |
1558 | |
1563 | |
1559 | static int |
1564 | static int |
1560 | api_is_ready (SV *coro_sv) |
1565 | api_is_ready (SV *coro_sv) |
1561 | { |
1566 | { |
1562 | dTHX; |
1567 | dTHX; |
|
|
1568 | |
1563 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1569 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1564 | } |
1570 | } |
1565 | |
1571 | |
1566 | static void |
1572 | INLINE void |
1567 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1573 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1568 | { |
1574 | { |
1569 | SV *prev_sv, *next_sv; |
1575 | SV *prev_sv, *next_sv; |
1570 | |
1576 | |
1571 | for (;;) |
1577 | for (;;) |
… | |
… | |
1610 | |
1616 | |
1611 | /* free this only after the transfer */ |
1617 | /* free this only after the transfer */ |
1612 | prev_sv = SvRV (coro_current); |
1618 | prev_sv = SvRV (coro_current); |
1613 | ta->prev = SvSTATE (prev_sv); |
1619 | ta->prev = SvSTATE (prev_sv); |
1614 | TRANSFER_CHECK (*ta); |
1620 | TRANSFER_CHECK (*ta); |
1615 | assert (ta->next->flags & CF_READY); |
1621 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1616 | ta->next->flags &= ~CF_READY; |
1622 | ta->next->flags &= ~CF_READY; |
1617 | SvRV_set (coro_current, next_sv); |
1623 | SvRV_set (coro_current, next_sv); |
1618 | |
1624 | |
1619 | LOCK; |
1625 | LOCK; |
1620 | free_coro_mortal (aTHX); |
1626 | free_coro_mortal (aTHX); |
1621 | coro_mortal = prev_sv; |
1627 | coro_mortal = prev_sv; |
1622 | UNLOCK; |
1628 | UNLOCK; |
1623 | } |
1629 | } |
1624 | |
1630 | |
1625 | static void |
1631 | INLINE void |
1626 | prepare_cede (pTHX_ struct transfer_args *ta) |
1632 | prepare_cede (pTHX_ struct transfer_args *ta) |
1627 | { |
1633 | { |
1628 | api_ready (coro_current); |
1634 | api_ready (coro_current); |
1629 | prepare_schedule (aTHX_ ta); |
1635 | prepare_schedule (aTHX_ ta); |
1630 | } |
1636 | } |
… | |
… | |
1802 | PerlIOBuf_get_ptr, |
1808 | PerlIOBuf_get_ptr, |
1803 | PerlIOBuf_get_cnt, |
1809 | PerlIOBuf_get_cnt, |
1804 | PerlIOBuf_set_ptrcnt, |
1810 | PerlIOBuf_set_ptrcnt, |
1805 | }; |
1811 | }; |
1806 | |
1812 | |
|
|
1813 | /*****************************************************************************/ |
|
|
1814 | |
|
|
1815 | static const CV *ssl_cv; /* for quick consistency check */ |
|
|
1816 | |
|
|
1817 | static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */ |
|
|
1818 | static SV *ssl_arg0; |
|
|
1819 | static SV *ssl_arg1; |
|
|
1820 | |
|
|
1821 | /* this restores the stack in the case we patched the entersub, to */ |
|
|
1822 | /* recreate the stack frame as perl will on following calls */ |
|
|
1823 | /* since entersub cleared the stack */ |
|
|
1824 | static OP * |
|
|
1825 | pp_restore (pTHX) |
|
|
1826 | { |
|
|
1827 | dSP; |
|
|
1828 | |
|
|
1829 | PUSHMARK (SP); |
|
|
1830 | |
|
|
1831 | EXTEND (SP, 3); |
|
|
1832 | if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0; |
|
|
1833 | if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0; |
|
|
1834 | PUSHs ((SV *)CvGV (ssl_cv)); |
|
|
1835 | |
|
|
1836 | RETURNOP (ssl_restore.op_first); |
|
|
1837 | } |
|
|
1838 | |
|
|
1839 | /* declare prototype */ |
|
|
1840 | XS(XS_Coro__State__set_stacklevel); |
|
|
1841 | |
|
|
1842 | static OP * |
|
|
1843 | pp_set_stacklevel (pTHX) |
|
|
1844 | { |
|
|
1845 | dSP; |
|
|
1846 | struct transfer_args ta; |
|
|
1847 | SV **arg = PL_stack_base + TOPMARK + 1; |
|
|
1848 | int items = SP - arg; /* args without function object */ |
|
|
1849 | |
|
|
1850 | /* do a quick consistency check on the "function" object, and if it isn't */ |
|
|
1851 | /* for us, divert to the real entersub */ |
|
|
1852 | if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel) |
|
|
1853 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
1854 | |
|
|
1855 | /* pop args */ |
|
|
1856 | SP = PL_stack_base + POPMARK; |
|
|
1857 | |
|
|
1858 | if (!(PL_op->op_flags & OPf_STACKED)) |
|
|
1859 | { |
|
|
1860 | /* ampersand-form of call, use @_ instead of stack */ |
|
|
1861 | AV *av = GvAV (PL_defgv); |
|
|
1862 | arg = AvARRAY (av); |
|
|
1863 | items = AvFILLp (av) + 1; |
|
|
1864 | } |
|
|
1865 | |
|
|
1866 | PUTBACK; |
|
|
1867 | switch (PL_op->op_private & 7) |
|
|
1868 | { |
|
|
1869 | case 0: |
|
|
1870 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0])); |
|
|
1871 | break; |
|
|
1872 | |
|
|
1873 | case 1: |
|
|
1874 | if (items != 2) |
|
|
1875 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items); |
|
|
1876 | |
|
|
1877 | prepare_transfer (aTHX_ &ta, arg [0], arg [1]); |
|
|
1878 | break; |
|
|
1879 | |
|
|
1880 | case 2: |
|
|
1881 | prepare_schedule (aTHX_ &ta); |
|
|
1882 | break; |
|
|
1883 | |
|
|
1884 | case 3: |
|
|
1885 | prepare_cede (aTHX_ &ta); |
|
|
1886 | break; |
|
|
1887 | |
|
|
1888 | case 4: |
|
|
1889 | if (!prepare_cede_notself (aTHX_ &ta)) |
|
|
1890 | RETURN; |
|
|
1891 | |
|
|
1892 | break; |
|
|
1893 | } |
|
|
1894 | |
|
|
1895 | TRANSFER (ta, 0); |
|
|
1896 | SPAGAIN; |
|
|
1897 | |
|
|
1898 | skip: |
|
|
1899 | |
|
|
1900 | RETURN; |
|
|
1901 | } |
1807 | |
1902 | |
1808 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1903 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1809 | |
1904 | |
1810 | PROTOTYPES: DISABLE |
1905 | PROTOTYPES: DISABLE |
1811 | |
|
|
1812 | BOOT: |
|
|
1813 | { |
|
|
1814 | #ifdef USE_ITHREADS |
|
|
1815 | MUTEX_INIT (&coro_lock); |
|
|
1816 | # if CORO_PTHREAD |
|
|
1817 | coro_thx = PERL_GET_CONTEXT; |
|
|
1818 | # endif |
|
|
1819 | #endif |
|
|
1820 | BOOT_PAGESIZE; |
|
|
1821 | |
|
|
1822 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
|
|
1823 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
|
|
1824 | |
|
|
1825 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1826 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
|
|
1827 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
|
|
1828 | |
|
|
1829 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
|
|
1830 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
|
|
1831 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
|
|
1832 | |
|
|
1833 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
|
|
1834 | |
|
|
1835 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1836 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1837 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1838 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
|
|
1839 | |
|
|
1840 | main_mainstack = PL_mainstack; |
|
|
1841 | main_top_env = PL_top_env; |
|
|
1842 | |
|
|
1843 | while (main_top_env->je_prev) |
|
|
1844 | main_top_env = main_top_env->je_prev; |
|
|
1845 | |
|
|
1846 | coroapi.ver = CORO_API_VERSION; |
|
|
1847 | coroapi.rev = CORO_API_REVISION; |
|
|
1848 | coroapi.transfer = api_transfer; |
|
|
1849 | |
|
|
1850 | { |
|
|
1851 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
1852 | |
|
|
1853 | if (!svp) croak ("Time::HiRes is required"); |
|
|
1854 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
1855 | |
|
|
1856 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
1857 | } |
|
|
1858 | |
|
|
1859 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | SV * |
|
|
1863 | new (char *klass, ...) |
|
|
1864 | CODE: |
|
|
1865 | { |
|
|
1866 | struct coro *coro; |
|
|
1867 | MAGIC *mg; |
|
|
1868 | HV *hv; |
|
|
1869 | int i; |
|
|
1870 | |
|
|
1871 | Newz (0, coro, 1, struct coro); |
|
|
1872 | coro->args = newAV (); |
|
|
1873 | coro->flags = CF_NEW; |
|
|
1874 | |
|
|
1875 | if (coro_first) coro_first->prev = coro; |
|
|
1876 | coro->next = coro_first; |
|
|
1877 | coro_first = coro; |
|
|
1878 | |
|
|
1879 | coro->hv = hv = newHV (); |
|
|
1880 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
1881 | mg->mg_flags |= MGf_DUP; |
|
|
1882 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
1883 | |
|
|
1884 | av_extend (coro->args, items - 1); |
|
|
1885 | for (i = 1; i < items; i++) |
|
|
1886 | av_push (coro->args, newSVsv (ST (i))); |
|
|
1887 | } |
|
|
1888 | OUTPUT: |
|
|
1889 | RETVAL |
|
|
1890 | |
1906 | |
1891 | # these not obviously related functions are all rolled into the same xs |
1907 | # 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 |
1908 | # function to increase chances that they all will call transfer with the same |
1893 | # stack offset |
1909 | # stack offset |
1894 | void |
1910 | void |
… | |
… | |
1898 | Coro::schedule = 2 |
1914 | Coro::schedule = 2 |
1899 | Coro::cede = 3 |
1915 | Coro::cede = 3 |
1900 | Coro::cede_notself = 4 |
1916 | Coro::cede_notself = 4 |
1901 | CODE: |
1917 | CODE: |
1902 | { |
1918 | { |
1903 | struct transfer_args ta; |
1919 | assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel)); |
1904 | |
1920 | |
1905 | PUTBACK; |
1921 | /* we patch the op, and then re-run the whole call */ |
1906 | switch (ix) |
1922 | /* we have to put some dummy argument on the stack for this to work */ |
|
|
1923 | /* TODO: walk back the opcode chain (but how?), nuke the pp_gv etc. */ |
|
|
1924 | ssl_restore.op_next = (OP *)&ssl_restore; |
|
|
1925 | ssl_restore.op_type = OP_NULL; |
|
|
1926 | ssl_restore.op_ppaddr = pp_restore; |
|
|
1927 | ssl_restore.op_first = PL_op; |
|
|
1928 | |
|
|
1929 | ssl_arg0 = items > 0 ? SvREFCNT_inc (ST (0)) : 0; |
|
|
1930 | ssl_arg1 = items > 1 ? SvREFCNT_inc (ST (1)) : 0; |
|
|
1931 | |
|
|
1932 | PL_op->op_ppaddr = pp_set_stacklevel; |
|
|
1933 | PL_op->op_private = PL_op->op_private & ~7 | ix; /* we potentially share our private flags with entersub */ |
|
|
1934 | |
|
|
1935 | PL_op = (OP *)&ssl_restore; |
|
|
1936 | } |
|
|
1937 | |
|
|
1938 | BOOT: |
|
|
1939 | { |
|
|
1940 | #ifdef USE_ITHREADS |
|
|
1941 | MUTEX_INIT (&coro_lock); |
|
|
1942 | # if CORO_PTHREAD |
|
|
1943 | coro_thx = PERL_GET_CONTEXT; |
|
|
1944 | # endif |
|
|
1945 | #endif |
|
|
1946 | BOOT_PAGESIZE; |
|
|
1947 | |
|
|
1948 | ssl_cv = get_cv ("Coro::State::_set_stacklevel", 0); |
|
|
1949 | |
|
|
1950 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
|
|
1951 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
|
|
1952 | |
|
|
1953 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1954 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
|
|
1955 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
|
|
1956 | |
|
|
1957 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
|
|
1958 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
|
|
1959 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
|
|
1960 | |
|
|
1961 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
|
|
1962 | |
|
|
1963 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1964 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1965 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1966 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
|
|
1967 | |
|
|
1968 | main_mainstack = PL_mainstack; |
|
|
1969 | main_top_env = PL_top_env; |
|
|
1970 | |
|
|
1971 | while (main_top_env->je_prev) |
|
|
1972 | main_top_env = main_top_env->je_prev; |
|
|
1973 | |
|
|
1974 | coroapi.ver = CORO_API_VERSION; |
|
|
1975 | coroapi.rev = CORO_API_REVISION; |
|
|
1976 | coroapi.transfer = api_transfer; |
|
|
1977 | |
1907 | { |
1978 | { |
1908 | case 0: |
1979 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
1909 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0))); |
|
|
1910 | break; |
|
|
1911 | |
1980 | |
1912 | case 1: |
1981 | if (!svp) croak ("Time::HiRes is required"); |
1913 | if (items != 2) |
1982 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
1914 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
|
|
1915 | |
1983 | |
1916 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1984 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
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 | } |
1985 | } |
1933 | SPAGAIN; |
|
|
1934 | |
1986 | |
1935 | BARRIER; |
1987 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
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 | } |
1988 | } |
|
|
1989 | |
|
|
1990 | SV * |
|
|
1991 | new (char *klass, ...) |
|
|
1992 | CODE: |
|
|
1993 | { |
|
|
1994 | struct coro *coro; |
|
|
1995 | MAGIC *mg; |
|
|
1996 | HV *hv; |
|
|
1997 | int i; |
|
|
1998 | |
|
|
1999 | Newz (0, coro, 1, struct coro); |
|
|
2000 | coro->args = newAV (); |
|
|
2001 | coro->flags = CF_NEW; |
|
|
2002 | |
|
|
2003 | if (coro_first) coro_first->prev = coro; |
|
|
2004 | coro->next = coro_first; |
|
|
2005 | coro_first = coro; |
|
|
2006 | |
|
|
2007 | coro->hv = hv = newHV (); |
|
|
2008 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
2009 | mg->mg_flags |= MGf_DUP; |
|
|
2010 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
2011 | |
|
|
2012 | av_extend (coro->args, items - 1); |
|
|
2013 | for (i = 1; i < items; i++) |
|
|
2014 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2015 | } |
|
|
2016 | OUTPUT: |
|
|
2017 | RETVAL |
1941 | |
2018 | |
1942 | bool |
2019 | bool |
1943 | _destroy (SV *coro_sv) |
2020 | _destroy (SV *coro_sv) |
1944 | CODE: |
2021 | CODE: |
1945 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2022 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
2142 | |
2219 | |
2143 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
2220 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
2144 | coro_ready[i] = newAV (); |
2221 | coro_ready[i] = newAV (); |
2145 | |
2222 | |
2146 | { |
2223 | { |
2147 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
2224 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2148 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
|
|
2149 | |
2225 | |
2150 | coroapi.schedule = api_schedule; |
2226 | coroapi.schedule = api_schedule; |
2151 | coroapi.cede = api_cede; |
2227 | coroapi.cede = api_cede; |
2152 | coroapi.cede_notself = api_cede_notself; |
2228 | coroapi.cede_notself = api_cede_notself; |
2153 | coroapi.ready = api_ready; |
2229 | coroapi.ready = api_ready; |
… | |
… | |
2421 | |
2497 | |
2422 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2498 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2423 | |
2499 | |
2424 | BOOT: |
2500 | BOOT: |
2425 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2501 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
|
|
2502 | |