… | |
… | |
52 | #else |
52 | #else |
53 | # define REGISTER_STACK(cctx,start,end) |
53 | # define REGISTER_STACK(cctx,start,end) |
54 | #endif |
54 | #endif |
55 | |
55 | |
56 | /* the maximum number of idle cctx that will be pooled */ |
56 | /* the maximum number of idle cctx that will be pooled */ |
57 | #define MAX_IDLE_CCTX 8 |
57 | static int cctx_max_idle = 4; |
58 | |
58 | |
59 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
59 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
60 | (PERL_REVISION > (a) \ |
60 | (PERL_REVISION > (a) \ |
61 | || (PERL_REVISION == (a) \ |
61 | || (PERL_REVISION == (a) \ |
62 | && (PERL_VERSION > (b) \ |
62 | && (PERL_VERSION > (b) \ |
… | |
… | |
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 | |
… | |
… | |
160 | Stat_t statcache; |
162 | Stat_t statcache; |
161 | }; |
163 | }; |
162 | |
164 | |
163 | static double (*nvtime)(); /* so why doesn't it take void? */ |
165 | static double (*nvtime)(); /* so why doesn't it take void? */ |
164 | |
166 | |
|
|
167 | static U32 cctx_gen; |
165 | static size_t coro_stacksize = CORO_STACKSIZE; |
168 | static size_t cctx_stacksize = CORO_STACKSIZE; |
166 | static struct CoroAPI coroapi; |
169 | static struct CoroAPI coroapi; |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
168 | static JMPENV *main_top_env; |
171 | static JMPENV *main_top_env; |
169 | static HV *coro_state_stash, *coro_stash; |
172 | static HV *coro_state_stash, *coro_stash; |
170 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
173 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
171 | |
174 | |
172 | static GV *irsgv; /* $/ */ |
175 | static GV *irsgv; /* $/ */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
176 | static GV *stdoutgv; /* *STDOUT */ |
174 | static SV *rv_diehook; |
177 | static SV *rv_diehook; |
175 | static SV *rv_warnhook; |
178 | static SV *rv_warnhook; |
… | |
… | |
207 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
210 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
208 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
211 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
209 | JMPENV *top_env; |
212 | JMPENV *top_env; |
210 | coro_context cctx; |
213 | coro_context cctx; |
211 | |
214 | |
|
|
215 | U32 gen; |
212 | #if CORO_USE_VALGRIND |
216 | #if CORO_USE_VALGRIND |
213 | int valgrind_id; |
217 | int valgrind_id; |
214 | #endif |
218 | #endif |
215 | unsigned char flags; |
219 | unsigned char flags; |
216 | } coro_cctx; |
220 | } coro_cctx; |
… | |
… | |
381 | static MGVTBL coro_cv_vtbl = { |
385 | static MGVTBL coro_cv_vtbl = { |
382 | 0, 0, 0, 0, |
386 | 0, 0, 0, 0, |
383 | coro_cv_free |
387 | coro_cv_free |
384 | }; |
388 | }; |
385 | |
389 | |
386 | #define CORO_MAGIC(sv,type) \ |
390 | #define CORO_MAGIC(sv, type) \ |
387 | SvMAGIC (sv) \ |
391 | SvMAGIC (sv) \ |
388 | ? SvMAGIC (sv)->mg_type == type \ |
392 | ? SvMAGIC (sv)->mg_type == type \ |
389 | ? SvMAGIC (sv) \ |
393 | ? SvMAGIC (sv) \ |
390 | : mg_find (sv, type) \ |
394 | : mg_find (sv, type) \ |
391 | : 0 |
395 | : 0 |
392 | |
396 | |
393 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
397 | #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) |
398 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
395 | |
399 | |
396 | static struct coro * |
400 | static struct coro * |
… | |
… | |
626 | |
630 | |
627 | /* |
631 | /* |
628 | * destroy the stacks, the callchain etc... |
632 | * destroy the stacks, the callchain etc... |
629 | */ |
633 | */ |
630 | static void |
634 | static void |
631 | coro_destroy_stacks (pTHX) |
635 | coro_destruct_stacks (pTHX) |
632 | { |
636 | { |
633 | while (PL_curstackinfo->si_next) |
637 | while (PL_curstackinfo->si_next) |
634 | PL_curstackinfo = PL_curstackinfo->si_next; |
638 | PL_curstackinfo = PL_curstackinfo->si_next; |
635 | |
639 | |
636 | while (PL_curstackinfo) |
640 | while (PL_curstackinfo) |
… | |
… | |
841 | */ |
845 | */ |
842 | ENTER; |
846 | ENTER; |
843 | } |
847 | } |
844 | |
848 | |
845 | static void |
849 | static void |
846 | coro_destroy (pTHX_ struct coro *coro) |
850 | coro_destruct (pTHX_ struct coro *coro) |
847 | { |
851 | { |
848 | if (!IN_DESTRUCT) |
852 | if (!IN_DESTRUCT) |
849 | { |
853 | { |
850 | /* restore all saved variables and stuff */ |
854 | /* restore all saved variables and stuff */ |
851 | LEAVE_SCOPE (0); |
855 | LEAVE_SCOPE (0); |
… | |
… | |
873 | SvREFCNT_dec (PL_warnhook); |
877 | SvREFCNT_dec (PL_warnhook); |
874 | |
878 | |
875 | SvREFCNT_dec (coro->saved_deffh); |
879 | SvREFCNT_dec (coro->saved_deffh); |
876 | SvREFCNT_dec (coro->throw); |
880 | SvREFCNT_dec (coro->throw); |
877 | |
881 | |
878 | coro_destroy_stacks (aTHX); |
882 | coro_destruct_stacks (aTHX); |
879 | } |
883 | } |
880 | |
884 | |
881 | static void |
885 | static void |
882 | free_coro_mortal (pTHX) |
886 | free_coro_mortal (pTHX) |
883 | { |
887 | { |
… | |
… | |
1078 | coro_cctx *cctx; |
1082 | coro_cctx *cctx; |
1079 | void *stack_start; |
1083 | void *stack_start; |
1080 | size_t stack_size; |
1084 | size_t stack_size; |
1081 | |
1085 | |
1082 | ++cctx_count; |
1086 | ++cctx_count; |
1083 | |
|
|
1084 | Newz (0, cctx, 1, coro_cctx); |
1087 | Newz (0, cctx, 1, coro_cctx); |
1085 | |
1088 | |
|
|
1089 | cctx->gen = cctx_gen; |
|
|
1090 | |
1086 | #if HAVE_MMAP |
1091 | #if HAVE_MMAP |
1087 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1092 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1088 | /* mmap supposedly does allocate-on-write for us */ |
1093 | /* mmap supposedly does allocate-on-write for us */ |
1089 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1094 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1090 | |
1095 | |
1091 | if (cctx->sptr != (void *)-1) |
1096 | if (cctx->sptr != (void *)-1) |
1092 | { |
1097 | { |
… | |
… | |
1098 | cctx->flags |= CC_MAPPED; |
1103 | cctx->flags |= CC_MAPPED; |
1099 | } |
1104 | } |
1100 | else |
1105 | else |
1101 | #endif |
1106 | #endif |
1102 | { |
1107 | { |
1103 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1108 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1104 | New (0, cctx->sptr, coro_stacksize, long); |
1109 | New (0, cctx->sptr, cctx_stacksize, long); |
1105 | |
1110 | |
1106 | if (!cctx->sptr) |
1111 | if (!cctx->sptr) |
1107 | { |
1112 | { |
1108 | perror ("FATAL: unable to allocate stack for coroutine"); |
1113 | perror ("FATAL: unable to allocate stack for coroutine"); |
1109 | _exit (EXIT_FAILURE); |
1114 | _exit (EXIT_FAILURE); |
… | |
… | |
1124 | { |
1129 | { |
1125 | if (!cctx) |
1130 | if (!cctx) |
1126 | return; |
1131 | return; |
1127 | |
1132 | |
1128 | --cctx_count; |
1133 | --cctx_count; |
|
|
1134 | coro_destroy (&cctx->cctx); |
1129 | |
1135 | |
|
|
1136 | /* coro_transfer creates new, empty cctx's */ |
|
|
1137 | if (cctx->sptr) |
|
|
1138 | { |
1130 | #if CORO_USE_VALGRIND |
1139 | #if CORO_USE_VALGRIND |
1131 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1140 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1132 | #endif |
1141 | #endif |
1133 | |
1142 | |
1134 | #if HAVE_MMAP |
1143 | #if HAVE_MMAP |
1135 | if (cctx->flags & CC_MAPPED) |
1144 | if (cctx->flags & CC_MAPPED) |
1136 | munmap (cctx->sptr, cctx->ssize); |
1145 | munmap (cctx->sptr, cctx->ssize); |
1137 | else |
1146 | else |
1138 | #endif |
1147 | #endif |
1139 | Safefree (cctx->sptr); |
1148 | Safefree (cctx->sptr); |
|
|
1149 | } |
1140 | |
1150 | |
1141 | Safefree (cctx); |
1151 | Safefree (cctx); |
1142 | } |
1152 | } |
1143 | |
1153 | |
1144 | /* wether this cctx should be destructed */ |
1154 | /* wether this cctx should be destructed */ |
1145 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1155 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1146 | |
1156 | |
1147 | static coro_cctx * |
1157 | static coro_cctx * |
1148 | cctx_get (pTHX) |
1158 | cctx_get (pTHX) |
1149 | { |
1159 | { |
1150 | while (expect_true (cctx_first)) |
1160 | while (expect_true (cctx_first)) |
… | |
… | |
1163 | } |
1173 | } |
1164 | |
1174 | |
1165 | static void |
1175 | static void |
1166 | cctx_put (coro_cctx *cctx) |
1176 | cctx_put (coro_cctx *cctx) |
1167 | { |
1177 | { |
|
|
1178 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1179 | |
1168 | /* free another cctx if overlimit */ |
1180 | /* free another cctx if overlimit */ |
1169 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1181 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1170 | { |
1182 | { |
1171 | coro_cctx *first = cctx_first; |
1183 | coro_cctx *first = cctx_first; |
1172 | cctx_first = first->next; |
1184 | cctx_first = first->next; |
1173 | --cctx_idle; |
1185 | --cctx_idle; |
1174 | |
1186 | |
… | |
… | |
1206 | /* always use the TRANSFER macro */ |
1218 | /* always use the TRANSFER macro */ |
1207 | static void NOINLINE |
1219 | static void NOINLINE |
1208 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1220 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1209 | { |
1221 | { |
1210 | dSTACKLEVEL; |
1222 | dSTACKLEVEL; |
1211 | static volatile int has_throw; |
|
|
1212 | |
1223 | |
1213 | /* sometimes transfer is only called to set idle_sp */ |
1224 | /* sometimes transfer is only called to set idle_sp */ |
1214 | if (expect_false (!next)) |
1225 | if (expect_false (!next)) |
1215 | { |
1226 | { |
1216 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1227 | ((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 */ |
1228 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1218 | } |
1229 | } |
1219 | else if (expect_true (prev != next)) |
1230 | else if (expect_true (prev != next)) |
1220 | { |
1231 | { |
|
|
1232 | static volatile int has_throw; |
1221 | coro_cctx *prev__cctx; |
1233 | coro_cctx *prev__cctx; |
1222 | |
1234 | |
1223 | if (expect_false (prev->flags & CF_NEW)) |
1235 | if (expect_false (prev->flags & CF_NEW)) |
1224 | { |
1236 | { |
1225 | /* create a new empty context */ |
1237 | /* create a new empty/source context */ |
|
|
1238 | ++cctx_count; |
1226 | Newz (0, prev->cctx, 1, coro_cctx); |
1239 | New (0, prev->cctx, 1, coro_cctx); |
|
|
1240 | prev->cctx->sptr = 0; |
|
|
1241 | coro_create (&prev->cctx->cctx, 0, 0, 0, 0); |
|
|
1242 | |
1227 | prev->flags &= ~CF_NEW; |
1243 | prev->flags &= ~CF_NEW; |
1228 | prev->flags |= CF_RUNNING; |
1244 | prev->flags |= CF_RUNNING; |
1229 | } |
1245 | } |
1230 | |
1246 | |
1231 | prev->flags &= ~CF_RUNNING; |
1247 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1338 | croak ("FATAL: tried to destroy currently running coroutine"); |
1354 | croak ("FATAL: tried to destroy currently running coroutine"); |
1339 | |
1355 | |
1340 | save_perl (aTHX_ &temp); |
1356 | save_perl (aTHX_ &temp); |
1341 | load_perl (aTHX_ coro); |
1357 | load_perl (aTHX_ coro); |
1342 | |
1358 | |
1343 | coro_destroy (aTHX_ coro); |
1359 | coro_destruct (aTHX_ coro); |
1344 | |
1360 | |
1345 | load_perl (aTHX_ &temp); |
1361 | load_perl (aTHX_ &temp); |
1346 | |
1362 | |
1347 | coro->slot = 0; |
1363 | coro->slot = 0; |
1348 | } |
1364 | } |
… | |
… | |
1735 | PROTOTYPES: DISABLE |
1751 | PROTOTYPES: DISABLE |
1736 | |
1752 | |
1737 | BOOT: |
1753 | BOOT: |
1738 | { |
1754 | { |
1739 | #ifdef USE_ITHREADS |
1755 | #ifdef USE_ITHREADS |
1740 | MUTEX_INIT (&coro_mutex); |
1756 | MUTEX_INIT (&coro_lock); |
1741 | #endif |
1757 | #endif |
1742 | BOOT_PAGESIZE; |
1758 | BOOT_PAGESIZE; |
1743 | |
1759 | |
1744 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1760 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1745 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1761 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1832 | ta.next = 0; |
1848 | ta.next = 0; |
1833 | break; |
1849 | break; |
1834 | |
1850 | |
1835 | case 1: |
1851 | case 1: |
1836 | if (items != 2) |
1852 | if (items != 2) |
1837 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1853 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
1838 | |
1854 | |
1839 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1855 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1840 | break; |
1856 | break; |
1841 | |
1857 | |
1842 | case 2: |
1858 | case 2: |
… | |
… | |
1876 | _exit (code); |
1892 | _exit (code); |
1877 | |
1893 | |
1878 | int |
1894 | int |
1879 | cctx_stacksize (int new_stacksize = 0) |
1895 | cctx_stacksize (int new_stacksize = 0) |
1880 | CODE: |
1896 | CODE: |
1881 | RETVAL = coro_stacksize; |
1897 | RETVAL = cctx_stacksize; |
1882 | if (new_stacksize) |
1898 | if (new_stacksize) |
|
|
1899 | { |
1883 | coro_stacksize = new_stacksize; |
1900 | cctx_stacksize = new_stacksize; |
|
|
1901 | ++cctx_gen; |
|
|
1902 | } |
|
|
1903 | OUTPUT: |
|
|
1904 | RETVAL |
|
|
1905 | |
|
|
1906 | int |
|
|
1907 | cctx_max_idle (int max_idle = 0) |
|
|
1908 | CODE: |
|
|
1909 | RETVAL = cctx_max_idle; |
|
|
1910 | if (max_idle > 1) |
|
|
1911 | cctx_max_idle = max_idle; |
1884 | OUTPUT: |
1912 | OUTPUT: |
1885 | RETVAL |
1913 | RETVAL |
1886 | |
1914 | |
1887 | int |
1915 | int |
1888 | cctx_count () |
1916 | cctx_count () |
… | |
… | |
2005 | CODE: |
2033 | CODE: |
2006 | struct coro *coro = SvSTATE (coro_current); |
2034 | struct coro *coro = SvSTATE (coro_current); |
2007 | coro->cctx->idle_sp = 0; |
2035 | coro->cctx->idle_sp = 0; |
2008 | |
2036 | |
2009 | void |
2037 | 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) |
2038 | swap_defsv (Coro::State self) |
2018 | PROTOTYPE: $ |
2039 | PROTOTYPE: $ |
2019 | ALIAS: |
2040 | ALIAS: |
2020 | swap_defav = 1 |
2041 | swap_defav = 1 |
2021 | CODE: |
2042 | CODE: |
… | |
… | |
2124 | CODE: |
2145 | CODE: |
2125 | RETVAL = coro_nready; |
2146 | RETVAL = coro_nready; |
2126 | OUTPUT: |
2147 | OUTPUT: |
2127 | RETVAL |
2148 | RETVAL |
2128 | |
2149 | |
|
|
2150 | void |
|
|
2151 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2152 | PROTOTYPE: $;$ |
|
|
2153 | CODE: |
|
|
2154 | SvREFCNT_dec (self->throw); |
|
|
2155 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2156 | |
2129 | # for async_pool speedup |
2157 | # for async_pool speedup |
2130 | void |
2158 | void |
2131 | _pool_1 (SV *cb) |
2159 | _pool_1 (SV *cb) |
2132 | CODE: |
2160 | CODE: |
2133 | { |
2161 | { |