… | |
… | |
142 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
143 | |
143 | |
144 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
145 | |
145 | |
146 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
147 | static perl_mutex coro_mutex; |
147 | static perl_mutex coro_lock; |
148 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
148 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
150 | #else |
150 | #else |
151 | # define LOCK (void)0 |
151 | # define LOCK (void)0 |
152 | # define UNLOCK (void)0 |
152 | # define UNLOCK (void)0 |
153 | #endif |
153 | #endif |
154 | |
154 | |
… | |
… | |
383 | static MGVTBL coro_cv_vtbl = { |
383 | static MGVTBL coro_cv_vtbl = { |
384 | 0, 0, 0, 0, |
384 | 0, 0, 0, 0, |
385 | coro_cv_free |
385 | coro_cv_free |
386 | }; |
386 | }; |
387 | |
387 | |
388 | #define CORO_MAGIC(sv,type) \ |
388 | #define CORO_MAGIC(sv, type) \ |
389 | SvMAGIC (sv) \ |
389 | SvMAGIC (sv) \ |
390 | ? SvMAGIC (sv)->mg_type == type \ |
390 | ? SvMAGIC (sv)->mg_type == type \ |
391 | ? SvMAGIC (sv) \ |
391 | ? SvMAGIC (sv) \ |
392 | : mg_find (sv, type) \ |
392 | : mg_find (sv, type) \ |
393 | : 0 |
393 | : 0 |
394 | |
394 | |
395 | #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) |
396 | #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) |
397 | |
397 | |
398 | static struct coro * |
398 | static struct coro * |
… | |
… | |
628 | |
628 | |
629 | /* |
629 | /* |
630 | * destroy the stacks, the callchain etc... |
630 | * destroy the stacks, the callchain etc... |
631 | */ |
631 | */ |
632 | static void |
632 | static void |
633 | coro_destroy_stacks (pTHX) |
633 | coro_destruct_stacks (pTHX) |
634 | { |
634 | { |
635 | while (PL_curstackinfo->si_next) |
635 | while (PL_curstackinfo->si_next) |
636 | PL_curstackinfo = PL_curstackinfo->si_next; |
636 | PL_curstackinfo = PL_curstackinfo->si_next; |
637 | |
637 | |
638 | while (PL_curstackinfo) |
638 | while (PL_curstackinfo) |
… | |
… | |
843 | */ |
843 | */ |
844 | ENTER; |
844 | ENTER; |
845 | } |
845 | } |
846 | |
846 | |
847 | static void |
847 | static void |
848 | coro_destroy (pTHX_ struct coro *coro) |
848 | coro_destruct (pTHX_ struct coro *coro) |
849 | { |
849 | { |
850 | if (!IN_DESTRUCT) |
850 | if (!IN_DESTRUCT) |
851 | { |
851 | { |
852 | /* restore all saved variables and stuff */ |
852 | /* restore all saved variables and stuff */ |
853 | LEAVE_SCOPE (0); |
853 | LEAVE_SCOPE (0); |
… | |
… | |
875 | SvREFCNT_dec (PL_warnhook); |
875 | SvREFCNT_dec (PL_warnhook); |
876 | |
876 | |
877 | SvREFCNT_dec (coro->saved_deffh); |
877 | SvREFCNT_dec (coro->saved_deffh); |
878 | SvREFCNT_dec (coro->throw); |
878 | SvREFCNT_dec (coro->throw); |
879 | |
879 | |
880 | coro_destroy_stacks (aTHX); |
880 | coro_destruct_stacks (aTHX); |
881 | } |
881 | } |
882 | |
882 | |
883 | static void |
883 | static void |
884 | free_coro_mortal (pTHX) |
884 | free_coro_mortal (pTHX) |
885 | { |
885 | { |
… | |
… | |
1080 | coro_cctx *cctx; |
1080 | coro_cctx *cctx; |
1081 | void *stack_start; |
1081 | void *stack_start; |
1082 | size_t stack_size; |
1082 | size_t stack_size; |
1083 | |
1083 | |
1084 | ++cctx_count; |
1084 | ++cctx_count; |
1085 | |
|
|
1086 | Newz (0, cctx, 1, coro_cctx); |
1085 | Newz (0, cctx, 1, coro_cctx); |
1087 | |
1086 | |
1088 | #if HAVE_MMAP |
1087 | #if HAVE_MMAP |
1089 | 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; |
1090 | /* mmap supposedly does allocate-on-write for us */ |
1089 | /* mmap supposedly does allocate-on-write for us */ |
… | |
… | |
1126 | { |
1125 | { |
1127 | if (!cctx) |
1126 | if (!cctx) |
1128 | return; |
1127 | return; |
1129 | |
1128 | |
1130 | --cctx_count; |
1129 | --cctx_count; |
|
|
1130 | coro_destroy (&cctx->cctx); |
1131 | |
1131 | |
|
|
1132 | /* coro_transfer creates new, empty cctx's */ |
|
|
1133 | if (cctx->sptr) |
|
|
1134 | { |
1132 | #if CORO_USE_VALGRIND |
1135 | #if CORO_USE_VALGRIND |
1133 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1136 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1134 | #endif |
1137 | #endif |
1135 | |
1138 | |
1136 | #if HAVE_MMAP |
1139 | #if HAVE_MMAP |
1137 | if (cctx->flags & CC_MAPPED) |
1140 | if (cctx->flags & CC_MAPPED) |
1138 | munmap (cctx->sptr, cctx->ssize); |
1141 | munmap (cctx->sptr, cctx->ssize); |
1139 | else |
1142 | else |
1140 | #endif |
1143 | #endif |
1141 | Safefree (cctx->sptr); |
1144 | Safefree (cctx->sptr); |
|
|
1145 | } |
1142 | |
1146 | |
1143 | Safefree (cctx); |
1147 | Safefree (cctx); |
1144 | } |
1148 | } |
1145 | |
1149 | |
1146 | /* wether this cctx should be destructed */ |
1150 | /* wether this cctx should be destructed */ |
… | |
… | |
1165 | } |
1169 | } |
1166 | |
1170 | |
1167 | static void |
1171 | static void |
1168 | cctx_put (coro_cctx *cctx) |
1172 | cctx_put (coro_cctx *cctx) |
1169 | { |
1173 | { |
|
|
1174 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1175 | |
1170 | /* free another cctx if overlimit */ |
1176 | /* free another cctx if overlimit */ |
1171 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1177 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1172 | { |
1178 | { |
1173 | coro_cctx *first = cctx_first; |
1179 | coro_cctx *first = cctx_first; |
1174 | cctx_first = first->next; |
1180 | cctx_first = first->next; |
… | |
… | |
1222 | static volatile int has_throw; |
1228 | static volatile int has_throw; |
1223 | coro_cctx *prev__cctx; |
1229 | coro_cctx *prev__cctx; |
1224 | |
1230 | |
1225 | if (expect_false (prev->flags & CF_NEW)) |
1231 | if (expect_false (prev->flags & CF_NEW)) |
1226 | { |
1232 | { |
1227 | /* create a new empty context */ |
1233 | /* create a new empty/source context */ |
|
|
1234 | ++cctx_count; |
1228 | 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 | |
1229 | prev->flags &= ~CF_NEW; |
1239 | prev->flags &= ~CF_NEW; |
1230 | prev->flags |= CF_RUNNING; |
1240 | prev->flags |= CF_RUNNING; |
1231 | } |
1241 | } |
1232 | |
1242 | |
1233 | prev->flags &= ~CF_RUNNING; |
1243 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1340 | croak ("FATAL: tried to destroy currently running coroutine"); |
1350 | croak ("FATAL: tried to destroy currently running coroutine"); |
1341 | |
1351 | |
1342 | save_perl (aTHX_ &temp); |
1352 | save_perl (aTHX_ &temp); |
1343 | load_perl (aTHX_ coro); |
1353 | load_perl (aTHX_ coro); |
1344 | |
1354 | |
1345 | coro_destroy (aTHX_ coro); |
1355 | coro_destruct (aTHX_ coro); |
1346 | |
1356 | |
1347 | load_perl (aTHX_ &temp); |
1357 | load_perl (aTHX_ &temp); |
1348 | |
1358 | |
1349 | coro->slot = 0; |
1359 | coro->slot = 0; |
1350 | } |
1360 | } |
… | |
… | |
1737 | PROTOTYPES: DISABLE |
1747 | PROTOTYPES: DISABLE |
1738 | |
1748 | |
1739 | BOOT: |
1749 | BOOT: |
1740 | { |
1750 | { |
1741 | #ifdef USE_ITHREADS |
1751 | #ifdef USE_ITHREADS |
1742 | MUTEX_INIT (&coro_mutex); |
1752 | MUTEX_INIT (&coro_lock); |
1743 | #endif |
1753 | #endif |
1744 | BOOT_PAGESIZE; |
1754 | BOOT_PAGESIZE; |
1745 | |
1755 | |
1746 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1756 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1747 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1757 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |