… | |
… | |
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 | |
147 | static perl_mutex coro_lock; |
148 | static perl_mutex coro_lock; |
148 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
150 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
151 | # if CORO_PTHREAD |
|
|
152 | static void *coro_thx; |
|
|
153 | # endif |
|
|
154 | |
150 | #else |
155 | #else |
|
|
156 | |
151 | # define LOCK (void)0 |
157 | # define LOCK (void)0 |
152 | # define UNLOCK (void)0 |
158 | # define UNLOCK (void)0 |
|
|
159 | |
153 | #endif |
160 | #endif |
|
|
161 | |
|
|
162 | # undef LOCK |
|
|
163 | # define LOCK (void)0 |
|
|
164 | # undef UNLOCK |
|
|
165 | # define UNLOCK (void)0 |
154 | |
166 | |
155 | /* helper storage struct for Coro::AIO */ |
167 | /* helper storage struct for Coro::AIO */ |
156 | struct io_state |
168 | struct io_state |
157 | { |
169 | { |
158 | AV *res; |
170 | AV *res; |
159 | int errorno; |
171 | int errorno; |
160 | I32 laststype; |
172 | I32 laststype; /* U16 in 5.10.0 */ |
161 | int laststatval; |
173 | int laststatval; |
162 | Stat_t statcache; |
174 | Stat_t statcache; |
163 | }; |
175 | }; |
164 | |
176 | |
165 | static double (*nvtime)(); /* so why doesn't it take void? */ |
177 | static double (*nvtime)(); /* so why doesn't it take void? */ |
… | |
… | |
280 | #define PRIO_MIN -4 |
292 | #define PRIO_MIN -4 |
281 | |
293 | |
282 | /* for Coro.pm */ |
294 | /* for Coro.pm */ |
283 | static SV *coro_current; |
295 | static SV *coro_current; |
284 | static SV *coro_readyhook; |
296 | static SV *coro_readyhook; |
285 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
297 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
286 | static int coro_nready; |
298 | static int coro_nready; |
287 | static struct coro *coro_first; |
299 | static struct coro *coro_first; |
288 | |
300 | |
289 | /** lowlevel stuff **********************************************************/ |
301 | /** lowlevel stuff **********************************************************/ |
290 | |
302 | |
… | |
… | |
1048 | * this is a _very_ stripped down perl interpreter ;) |
1060 | * this is a _very_ stripped down perl interpreter ;) |
1049 | */ |
1061 | */ |
1050 | static void |
1062 | static void |
1051 | cctx_run (void *arg) |
1063 | cctx_run (void *arg) |
1052 | { |
1064 | { |
|
|
1065 | #ifdef USE_ITHREADS |
|
|
1066 | # if CORO_PTHREAD |
|
|
1067 | PERL_SET_CONTEXT (coro_thx); |
|
|
1068 | # endif |
|
|
1069 | #endif |
|
|
1070 | { |
1053 | dTHX; |
1071 | dTHX; |
1054 | |
1072 | |
1055 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1073 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1056 | UNLOCK; |
1074 | UNLOCK; |
1057 | |
1075 | |
1058 | /* we now skip the entersub that lead to transfer() */ |
1076 | /* we now skip the entersub that lead to transfer() */ |
1059 | PL_op = PL_op->op_next; |
1077 | PL_op = PL_op->op_next; |
1060 | |
1078 | |
1061 | /* inject a fake subroutine call to cctx_init */ |
1079 | /* inject a fake subroutine call to cctx_init */ |
1062 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1080 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1063 | |
1081 | |
1064 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1082 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1065 | PL_restartop = PL_op; |
1083 | PL_restartop = PL_op; |
1066 | perl_run (PL_curinterp); |
1084 | perl_run (PL_curinterp); |
1067 | |
1085 | |
1068 | /* |
1086 | /* |
1069 | * If perl-run returns we assume exit() was being called or the coro |
1087 | * If perl-run returns we assume exit() was being called or the coro |
1070 | * fell off the end, which seems to be the only valid (non-bug) |
1088 | * fell off the end, which seems to be the only valid (non-bug) |
1071 | * reason for perl_run to return. We try to exit by jumping to the |
1089 | * reason for perl_run to return. We try to exit by jumping to the |
1072 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1090 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1073 | * coroutine as Coro has no such concept |
1091 | * coroutine as Coro has no such concept |
1074 | */ |
1092 | */ |
1075 | PL_top_env = main_top_env; |
1093 | PL_top_env = main_top_env; |
1076 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1094 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1095 | } |
1077 | } |
1096 | } |
1078 | |
1097 | |
1079 | static coro_cctx * |
1098 | static coro_cctx * |
1080 | cctx_new () |
1099 | cctx_new () |
1081 | { |
1100 | { |
… | |
… | |
1652 | else |
1671 | else |
1653 | coro->slot->runops = RUNOPS_DEFAULT; |
1672 | coro->slot->runops = RUNOPS_DEFAULT; |
1654 | } |
1673 | } |
1655 | } |
1674 | } |
1656 | |
1675 | |
|
|
1676 | #if 0 |
1657 | static int |
1677 | static int |
1658 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1678 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
1659 | { |
1679 | { |
1660 | AV *padlist; |
1680 | AV *padlist; |
1661 | AV *av = (AV *)mg->mg_obj; |
1681 | AV *av = (AV *)mg->mg_obj; |
… | |
… | |
1667 | |
1687 | |
1668 | static MGVTBL coro_gensub_vtbl = { |
1688 | static MGVTBL coro_gensub_vtbl = { |
1669 | 0, 0, 0, 0, |
1689 | 0, 0, 0, 0, |
1670 | coro_gensub_free |
1690 | coro_gensub_free |
1671 | }; |
1691 | }; |
|
|
1692 | #endif |
1672 | |
1693 | |
1673 | /*****************************************************************************/ |
1694 | /*****************************************************************************/ |
1674 | /* PerlIO::cede */ |
1695 | /* PerlIO::cede */ |
1675 | |
1696 | |
1676 | typedef struct |
1697 | typedef struct |
… | |
… | |
1752 | |
1773 | |
1753 | BOOT: |
1774 | BOOT: |
1754 | { |
1775 | { |
1755 | #ifdef USE_ITHREADS |
1776 | #ifdef USE_ITHREADS |
1756 | MUTEX_INIT (&coro_lock); |
1777 | MUTEX_INIT (&coro_lock); |
|
|
1778 | # if CORO_PTHREAD |
|
|
1779 | coro_thx = PERL_GET_CONTEXT; |
|
|
1780 | # endif |
1757 | #endif |
1781 | #endif |
1758 | BOOT_PAGESIZE; |
1782 | BOOT_PAGESIZE; |
1759 | |
1783 | |
1760 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1784 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1761 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1785 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
2012 | CODE: |
2036 | CODE: |
2013 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2037 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2014 | OUTPUT: |
2038 | OUTPUT: |
2015 | RETVAL |
2039 | RETVAL |
2016 | |
2040 | |
2017 | IV |
2041 | UV |
2018 | rss (Coro::State coro) |
2042 | rss (Coro::State coro) |
2019 | PROTOTYPE: $ |
2043 | PROTOTYPE: $ |
2020 | ALIAS: |
2044 | ALIAS: |
2021 | usecount = 1 |
2045 | usecount = 1 |
2022 | CODE: |
2046 | CODE: |
… | |
… | |
2204 | sv_setsv (cb, &PL_sv_undef); |
2228 | sv_setsv (cb, &PL_sv_undef); |
2205 | |
2229 | |
2206 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2230 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2207 | coro->saved_deffh = 0; |
2231 | coro->saved_deffh = 0; |
2208 | |
2232 | |
2209 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2233 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2210 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2234 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2211 | { |
2235 | { |
2212 | SV *old = PL_diehook; |
2236 | SV *old = PL_diehook; |
2213 | PL_diehook = 0; |
2237 | PL_diehook = 0; |
2214 | SvREFCNT_dec (old); |
2238 | SvREFCNT_dec (old); |