… | |
… | |
81 | # ifndef IS_PADCONST |
81 | # ifndef IS_PADCONST |
82 | # define IS_PADCONST(v) 0 |
82 | # define IS_PADCONST(v) 0 |
83 | # endif |
83 | # endif |
84 | #endif |
84 | #endif |
85 | |
85 | |
|
|
86 | /* 5.11 */ |
|
|
87 | #ifndef CxHASARGS |
|
|
88 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | /* 5.10.0 */ |
|
|
92 | #ifndef SvREFCNT_inc_NN |
|
|
93 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
94 | #endif |
|
|
95 | |
86 | /* 5.8.8 */ |
96 | /* 5.8.8 */ |
87 | #ifndef GV_NOTQUAL |
97 | #ifndef GV_NOTQUAL |
88 | # define GV_NOTQUAL 0 |
98 | # define GV_NOTQUAL 0 |
89 | #endif |
99 | #endif |
90 | #ifndef newSV |
100 | #ifndef newSV |
91 | # define newSV(l) NEWSV(0,l) |
101 | # define newSV(l) NEWSV(0,l) |
92 | #endif |
|
|
93 | #ifndef SvREFCNT_inc_NN |
|
|
94 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
95 | #endif |
|
|
96 | |
|
|
97 | /* 5.11 */ |
|
|
98 | #ifndef CxHASARGS |
|
|
99 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
100 | #endif |
102 | #endif |
101 | |
103 | |
102 | /* 5.8.7 */ |
104 | /* 5.8.7 */ |
103 | #ifndef SvRV_set |
105 | #ifndef SvRV_set |
104 | # define SvRV_set(s,v) SvRV(s) = (v) |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
… | |
… | |
140 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
141 | |
143 | |
142 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
143 | |
145 | |
144 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
145 | static perl_mutex coro_mutex; |
147 | static perl_mutex coro_lock; |
146 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
148 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
147 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
148 | #else |
150 | #else |
149 | # define LOCK (void)0 |
151 | # define LOCK (void)0 |
150 | # define UNLOCK (void)0 |
152 | # define UNLOCK (void)0 |
151 | #endif |
153 | #endif |
152 | |
154 | |
… | |
… | |
165 | static size_t coro_stacksize = CORO_STACKSIZE; |
167 | static size_t coro_stacksize = CORO_STACKSIZE; |
166 | static struct CoroAPI coroapi; |
168 | static struct CoroAPI coroapi; |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
169 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
168 | static JMPENV *main_top_env; |
170 | static JMPENV *main_top_env; |
169 | static HV *coro_state_stash, *coro_stash; |
171 | static HV *coro_state_stash, *coro_stash; |
170 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
172 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
171 | |
173 | |
172 | static GV *irsgv; /* $/ */ |
174 | static GV *irsgv; /* $/ */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
175 | static GV *stdoutgv; /* *STDOUT */ |
174 | static SV *rv_diehook; |
176 | static SV *rv_diehook; |
175 | static SV *rv_warnhook; |
177 | static SV *rv_warnhook; |
… | |
… | |
381 | static MGVTBL coro_cv_vtbl = { |
383 | static MGVTBL coro_cv_vtbl = { |
382 | 0, 0, 0, 0, |
384 | 0, 0, 0, 0, |
383 | coro_cv_free |
385 | coro_cv_free |
384 | }; |
386 | }; |
385 | |
387 | |
386 | #define CORO_MAGIC(sv,type) \ |
388 | #define CORO_MAGIC(sv, type) \ |
387 | SvMAGIC (sv) \ |
389 | SvMAGIC (sv) \ |
388 | ? SvMAGIC (sv)->mg_type == type \ |
390 | ? SvMAGIC (sv)->mg_type == type \ |
389 | ? SvMAGIC (sv) \ |
391 | ? SvMAGIC (sv) \ |
390 | : mg_find (sv, type) \ |
392 | : mg_find (sv, type) \ |
391 | : 0 |
393 | : 0 |
392 | |
394 | |
393 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
395 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
394 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
396 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
395 | |
397 | |
396 | static struct coro * |
398 | static struct coro * |
… | |
… | |
626 | |
628 | |
627 | /* |
629 | /* |
628 | * destroy the stacks, the callchain etc... |
630 | * destroy the stacks, the callchain etc... |
629 | */ |
631 | */ |
630 | static void |
632 | static void |
631 | coro_destroy_stacks (pTHX) |
633 | coro_destruct_stacks (pTHX) |
632 | { |
634 | { |
633 | while (PL_curstackinfo->si_next) |
635 | while (PL_curstackinfo->si_next) |
634 | PL_curstackinfo = PL_curstackinfo->si_next; |
636 | PL_curstackinfo = PL_curstackinfo->si_next; |
635 | |
637 | |
636 | while (PL_curstackinfo) |
638 | while (PL_curstackinfo) |
… | |
… | |
841 | */ |
843 | */ |
842 | ENTER; |
844 | ENTER; |
843 | } |
845 | } |
844 | |
846 | |
845 | static void |
847 | static void |
846 | coro_destroy (pTHX_ struct coro *coro) |
848 | coro_destruct (pTHX_ struct coro *coro) |
847 | { |
849 | { |
848 | if (!IN_DESTRUCT) |
850 | if (!IN_DESTRUCT) |
849 | { |
851 | { |
850 | /* restore all saved variables and stuff */ |
852 | /* restore all saved variables and stuff */ |
851 | LEAVE_SCOPE (0); |
853 | LEAVE_SCOPE (0); |
… | |
… | |
873 | SvREFCNT_dec (PL_warnhook); |
875 | SvREFCNT_dec (PL_warnhook); |
874 | |
876 | |
875 | SvREFCNT_dec (coro->saved_deffh); |
877 | SvREFCNT_dec (coro->saved_deffh); |
876 | SvREFCNT_dec (coro->throw); |
878 | SvREFCNT_dec (coro->throw); |
877 | |
879 | |
878 | coro_destroy_stacks (aTHX); |
880 | coro_destruct_stacks (aTHX); |
879 | } |
881 | } |
880 | |
882 | |
881 | static void |
883 | static void |
882 | free_coro_mortal (pTHX) |
884 | free_coro_mortal (pTHX) |
883 | { |
885 | { |
… | |
… | |
1078 | coro_cctx *cctx; |
1080 | coro_cctx *cctx; |
1079 | void *stack_start; |
1081 | void *stack_start; |
1080 | size_t stack_size; |
1082 | size_t stack_size; |
1081 | |
1083 | |
1082 | ++cctx_count; |
1084 | ++cctx_count; |
1083 | |
|
|
1084 | Newz (0, cctx, 1, coro_cctx); |
1085 | Newz (0, cctx, 1, coro_cctx); |
1085 | |
1086 | |
1086 | #if HAVE_MMAP |
1087 | #if HAVE_MMAP |
1087 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1088 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1088 | /* mmap supposedly does allocate-on-write for us */ |
1089 | /* mmap supposedly does allocate-on-write for us */ |
… | |
… | |
1124 | { |
1125 | { |
1125 | if (!cctx) |
1126 | if (!cctx) |
1126 | return; |
1127 | return; |
1127 | |
1128 | |
1128 | --cctx_count; |
1129 | --cctx_count; |
|
|
1130 | coro_destroy (&cctx->cctx); |
1129 | |
1131 | |
|
|
1132 | /* coro_transfer creates new, empty cctx's */ |
|
|
1133 | if (cctx->sptr) |
|
|
1134 | { |
1130 | #if CORO_USE_VALGRIND |
1135 | #if CORO_USE_VALGRIND |
1131 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1136 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1132 | #endif |
1137 | #endif |
1133 | |
1138 | |
1134 | #if HAVE_MMAP |
1139 | #if HAVE_MMAP |
1135 | if (cctx->flags & CC_MAPPED) |
1140 | if (cctx->flags & CC_MAPPED) |
1136 | munmap (cctx->sptr, cctx->ssize); |
1141 | munmap (cctx->sptr, cctx->ssize); |
1137 | else |
1142 | else |
1138 | #endif |
1143 | #endif |
1139 | Safefree (cctx->sptr); |
1144 | Safefree (cctx->sptr); |
|
|
1145 | } |
1140 | |
1146 | |
1141 | Safefree (cctx); |
1147 | Safefree (cctx); |
1142 | } |
1148 | } |
1143 | |
1149 | |
1144 | /* wether this cctx should be destructed */ |
1150 | /* wether this cctx should be destructed */ |
… | |
… | |
1163 | } |
1169 | } |
1164 | |
1170 | |
1165 | static void |
1171 | static void |
1166 | cctx_put (coro_cctx *cctx) |
1172 | cctx_put (coro_cctx *cctx) |
1167 | { |
1173 | { |
|
|
1174 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1175 | |
1168 | /* free another cctx if overlimit */ |
1176 | /* free another cctx if overlimit */ |
1169 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1177 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1170 | { |
1178 | { |
1171 | coro_cctx *first = cctx_first; |
1179 | coro_cctx *first = cctx_first; |
1172 | cctx_first = first->next; |
1180 | cctx_first = first->next; |
… | |
… | |
1206 | /* always use the TRANSFER macro */ |
1214 | /* always use the TRANSFER macro */ |
1207 | static void NOINLINE |
1215 | static void NOINLINE |
1208 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1216 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1209 | { |
1217 | { |
1210 | dSTACKLEVEL; |
1218 | dSTACKLEVEL; |
1211 | static volatile int has_throw; |
|
|
1212 | |
1219 | |
1213 | /* sometimes transfer is only called to set idle_sp */ |
1220 | /* sometimes transfer is only called to set idle_sp */ |
1214 | if (expect_false (!next)) |
1221 | if (expect_false (!next)) |
1215 | { |
1222 | { |
1216 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1223 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1217 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1224 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1218 | } |
1225 | } |
1219 | else if (expect_true (prev != next)) |
1226 | else if (expect_true (prev != next)) |
1220 | { |
1227 | { |
|
|
1228 | static volatile int has_throw; |
1221 | coro_cctx *prev__cctx; |
1229 | coro_cctx *prev__cctx; |
1222 | |
1230 | |
1223 | if (expect_false (prev->flags & CF_NEW)) |
1231 | if (expect_false (prev->flags & CF_NEW)) |
1224 | { |
1232 | { |
1225 | /* create a new empty context */ |
1233 | /* create a new empty/source context */ |
|
|
1234 | ++cctx_count; |
1226 | Newz (0, prev->cctx, 1, coro_cctx); |
1235 | New (0, prev->cctx, 1, coro_cctx); |
|
|
1236 | prev->cctx->sptr = 0; |
|
|
1237 | coro_create (&prev->cctx->cctx, 0, 0, 0, 0); |
|
|
1238 | |
1227 | prev->flags &= ~CF_NEW; |
1239 | prev->flags &= ~CF_NEW; |
1228 | prev->flags |= CF_RUNNING; |
1240 | prev->flags |= CF_RUNNING; |
1229 | } |
1241 | } |
1230 | |
1242 | |
1231 | prev->flags &= ~CF_RUNNING; |
1243 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1338 | croak ("FATAL: tried to destroy currently running coroutine"); |
1350 | croak ("FATAL: tried to destroy currently running coroutine"); |
1339 | |
1351 | |
1340 | save_perl (aTHX_ &temp); |
1352 | save_perl (aTHX_ &temp); |
1341 | load_perl (aTHX_ coro); |
1353 | load_perl (aTHX_ coro); |
1342 | |
1354 | |
1343 | coro_destroy (aTHX_ coro); |
1355 | coro_destruct (aTHX_ coro); |
1344 | |
1356 | |
1345 | load_perl (aTHX_ &temp); |
1357 | load_perl (aTHX_ &temp); |
1346 | |
1358 | |
1347 | coro->slot = 0; |
1359 | coro->slot = 0; |
1348 | } |
1360 | } |
… | |
… | |
1735 | PROTOTYPES: DISABLE |
1747 | PROTOTYPES: DISABLE |
1736 | |
1748 | |
1737 | BOOT: |
1749 | BOOT: |
1738 | { |
1750 | { |
1739 | #ifdef USE_ITHREADS |
1751 | #ifdef USE_ITHREADS |
1740 | MUTEX_INIT (&coro_mutex); |
1752 | MUTEX_INIT (&coro_lock); |
1741 | #endif |
1753 | #endif |
1742 | BOOT_PAGESIZE; |
1754 | BOOT_PAGESIZE; |
1743 | |
1755 | |
1744 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1756 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1745 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1757 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1832 | ta.next = 0; |
1844 | ta.next = 0; |
1833 | break; |
1845 | break; |
1834 | |
1846 | |
1835 | case 1: |
1847 | case 1: |
1836 | if (items != 2) |
1848 | if (items != 2) |
1837 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1849 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
1838 | |
1850 | |
1839 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1851 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1840 | break; |
1852 | break; |
1841 | |
1853 | |
1842 | case 2: |
1854 | case 2: |
… | |
… | |
2005 | CODE: |
2017 | CODE: |
2006 | struct coro *coro = SvSTATE (coro_current); |
2018 | struct coro *coro = SvSTATE (coro_current); |
2007 | coro->cctx->idle_sp = 0; |
2019 | coro->cctx->idle_sp = 0; |
2008 | |
2020 | |
2009 | void |
2021 | void |
2010 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2011 | PROTOTYPE: $;$ |
|
|
2012 | CODE: |
|
|
2013 | SvREFCNT_dec (self->throw); |
|
|
2014 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2015 | |
|
|
2016 | void |
|
|
2017 | swap_defsv (Coro::State self) |
2022 | swap_defsv (Coro::State self) |
2018 | PROTOTYPE: $ |
2023 | PROTOTYPE: $ |
2019 | ALIAS: |
2024 | ALIAS: |
2020 | swap_defav = 1 |
2025 | swap_defav = 1 |
2021 | CODE: |
2026 | CODE: |
… | |
… | |
2124 | CODE: |
2129 | CODE: |
2125 | RETVAL = coro_nready; |
2130 | RETVAL = coro_nready; |
2126 | OUTPUT: |
2131 | OUTPUT: |
2127 | RETVAL |
2132 | RETVAL |
2128 | |
2133 | |
|
|
2134 | void |
|
|
2135 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2136 | PROTOTYPE: $;$ |
|
|
2137 | CODE: |
|
|
2138 | SvREFCNT_dec (self->throw); |
|
|
2139 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2140 | |
2129 | # for async_pool speedup |
2141 | # for async_pool speedup |
2130 | void |
2142 | void |
2131 | _pool_1 (SV *cb) |
2143 | _pool_1 (SV *cb) |
2132 | CODE: |
2144 | CODE: |
2133 | { |
2145 | { |