… | |
… | |
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 | |
|
|
94 | /* 5.11 */ |
|
|
95 | #ifndef CxHASARGS |
|
|
96 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
97 | #endif |
102 | #endif |
98 | |
103 | |
99 | /* 5.8.7 */ |
104 | /* 5.8.7 */ |
100 | #ifndef SvRV_set |
105 | #ifndef SvRV_set |
101 | # define SvRV_set(s,v) SvRV(s) = (v) |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
… | |
… | |
137 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
138 | |
143 | |
139 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
140 | |
145 | |
141 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
142 | static perl_mutex coro_mutex; |
147 | static perl_mutex coro_lock; |
143 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
148 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
144 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
145 | #else |
150 | #else |
146 | # define LOCK (void)0 |
151 | # define LOCK (void)0 |
147 | # define UNLOCK (void)0 |
152 | # define UNLOCK (void)0 |
148 | #endif |
153 | #endif |
149 | |
154 | |
150 | /* helper storage struct for Coro::AIO */ |
155 | /* helper storage struct for Coro::AIO */ |
151 | struct io_state |
156 | struct io_state |
152 | { |
157 | { |
|
|
158 | AV *res; |
153 | int errorno; |
159 | int errorno; |
154 | I32 laststype; |
160 | I32 laststype; |
155 | int laststatval; |
161 | int laststatval; |
156 | Stat_t statcache; |
162 | Stat_t statcache; |
157 | }; |
163 | }; |
158 | |
164 | |
159 | static double (*nvtime)(); /* so why doesn't it take void? */ |
165 | static double (*nvtime)(); /* so why doesn't it take void? */ |
160 | |
166 | |
|
|
167 | static U32 cctx_gen; |
161 | static size_t coro_stacksize = CORO_STACKSIZE; |
168 | static size_t cctx_stacksize = CORO_STACKSIZE; |
162 | static struct CoroAPI coroapi; |
169 | static struct CoroAPI coroapi; |
163 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
164 | static JMPENV *main_top_env; |
171 | static JMPENV *main_top_env; |
165 | static HV *coro_state_stash, *coro_stash; |
172 | static HV *coro_state_stash, *coro_stash; |
166 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
173 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
167 | |
174 | |
168 | static GV *irsgv; /* $/ */ |
175 | static GV *irsgv; /* $/ */ |
169 | static GV *stdoutgv; /* *STDOUT */ |
176 | static GV *stdoutgv; /* *STDOUT */ |
170 | static SV *rv_diehook; |
177 | static SV *rv_diehook; |
171 | static SV *rv_warnhook; |
178 | static SV *rv_warnhook; |
… | |
… | |
203 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
210 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
204 | 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 */ |
205 | JMPENV *top_env; |
212 | JMPENV *top_env; |
206 | coro_context cctx; |
213 | coro_context cctx; |
207 | |
214 | |
|
|
215 | U32 gen; |
208 | #if CORO_USE_VALGRIND |
216 | #if CORO_USE_VALGRIND |
209 | int valgrind_id; |
217 | int valgrind_id; |
210 | #endif |
218 | #endif |
211 | unsigned char flags; |
219 | unsigned char flags; |
212 | } coro_cctx; |
220 | } coro_cctx; |
… | |
… | |
324 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
332 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
325 | #endif |
333 | #endif |
326 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
334 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
327 | --AvFILLp (padlist); |
335 | --AvFILLp (padlist); |
328 | |
336 | |
329 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
337 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
330 | av_store (newpadlist, 1, (SV *)newpad); |
338 | av_store (newpadlist, 1, (SV *)newpad); |
331 | |
339 | |
332 | return newpadlist; |
340 | return newpadlist; |
333 | } |
341 | } |
334 | |
342 | |
… | |
… | |
377 | static MGVTBL coro_cv_vtbl = { |
385 | static MGVTBL coro_cv_vtbl = { |
378 | 0, 0, 0, 0, |
386 | 0, 0, 0, 0, |
379 | coro_cv_free |
387 | coro_cv_free |
380 | }; |
388 | }; |
381 | |
389 | |
382 | #define CORO_MAGIC(sv,type) \ |
390 | #define CORO_MAGIC(sv, type) \ |
383 | SvMAGIC (sv) \ |
391 | SvMAGIC (sv) \ |
384 | ? SvMAGIC (sv)->mg_type == type \ |
392 | ? SvMAGIC (sv)->mg_type == type \ |
385 | ? SvMAGIC (sv) \ |
393 | ? SvMAGIC (sv) \ |
386 | : mg_find (sv, type) \ |
394 | : mg_find (sv, type) \ |
387 | : 0 |
395 | : 0 |
388 | |
396 | |
389 | #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) |
390 | #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) |
391 | |
399 | |
392 | static struct coro * |
400 | static struct coro * |
… | |
… | |
622 | |
630 | |
623 | /* |
631 | /* |
624 | * destroy the stacks, the callchain etc... |
632 | * destroy the stacks, the callchain etc... |
625 | */ |
633 | */ |
626 | static void |
634 | static void |
627 | coro_destroy_stacks (pTHX) |
635 | coro_destruct_stacks (pTHX) |
628 | { |
636 | { |
629 | while (PL_curstackinfo->si_next) |
637 | while (PL_curstackinfo->si_next) |
630 | PL_curstackinfo = PL_curstackinfo->si_next; |
638 | PL_curstackinfo = PL_curstackinfo->si_next; |
631 | |
639 | |
632 | while (PL_curstackinfo) |
640 | while (PL_curstackinfo) |
… | |
… | |
810 | GvSV (PL_defgv) = newSV (0); |
818 | GvSV (PL_defgv) = newSV (0); |
811 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
819 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
812 | GvSV (PL_errgv) = newSV (0); |
820 | GvSV (PL_errgv) = newSV (0); |
813 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
821 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
814 | PL_rs = newSVsv (GvSV (irsgv)); |
822 | PL_rs = newSVsv (GvSV (irsgv)); |
815 | PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); |
823 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
816 | |
824 | |
817 | { |
825 | { |
818 | dSP; |
826 | dSP; |
819 | LOGOP myop; |
827 | LOGOP myop; |
820 | |
828 | |
… | |
… | |
837 | */ |
845 | */ |
838 | ENTER; |
846 | ENTER; |
839 | } |
847 | } |
840 | |
848 | |
841 | static void |
849 | static void |
842 | coro_destroy (pTHX_ struct coro *coro) |
850 | coro_destruct (pTHX_ struct coro *coro) |
843 | { |
851 | { |
844 | if (!IN_DESTRUCT) |
852 | if (!IN_DESTRUCT) |
845 | { |
853 | { |
846 | /* restore all saved variables and stuff */ |
854 | /* restore all saved variables and stuff */ |
847 | LEAVE_SCOPE (0); |
855 | LEAVE_SCOPE (0); |
… | |
… | |
869 | SvREFCNT_dec (PL_warnhook); |
877 | SvREFCNT_dec (PL_warnhook); |
870 | |
878 | |
871 | SvREFCNT_dec (coro->saved_deffh); |
879 | SvREFCNT_dec (coro->saved_deffh); |
872 | SvREFCNT_dec (coro->throw); |
880 | SvREFCNT_dec (coro->throw); |
873 | |
881 | |
874 | coro_destroy_stacks (aTHX); |
882 | coro_destruct_stacks (aTHX); |
875 | } |
883 | } |
876 | |
884 | |
877 | static void |
885 | static void |
878 | free_coro_mortal (pTHX) |
886 | free_coro_mortal (pTHX) |
879 | { |
887 | { |
… | |
… | |
916 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
924 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
917 | : bot; |
925 | : bot; |
918 | |
926 | |
919 | av_extend (av, top - bot); |
927 | av_extend (av, top - bot); |
920 | while (bot < top) |
928 | while (bot < top) |
921 | av_push (av, SvREFCNT_inc (*bot++)); |
929 | av_push (av, SvREFCNT_inc_NN (*bot++)); |
922 | |
930 | |
923 | PL_runops = RUNOPS_DEFAULT; |
931 | PL_runops = RUNOPS_DEFAULT; |
924 | ENTER; |
932 | ENTER; |
925 | SAVETMPS; |
933 | SAVETMPS; |
926 | EXTEND (SP, 3); |
934 | EXTEND (SP, 3); |
… | |
… | |
1074 | coro_cctx *cctx; |
1082 | coro_cctx *cctx; |
1075 | void *stack_start; |
1083 | void *stack_start; |
1076 | size_t stack_size; |
1084 | size_t stack_size; |
1077 | |
1085 | |
1078 | ++cctx_count; |
1086 | ++cctx_count; |
1079 | |
|
|
1080 | Newz (0, cctx, 1, coro_cctx); |
1087 | Newz (0, cctx, 1, coro_cctx); |
1081 | |
1088 | |
|
|
1089 | cctx->gen = cctx_gen; |
|
|
1090 | |
1082 | #if HAVE_MMAP |
1091 | #if HAVE_MMAP |
1083 | 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; |
1084 | /* mmap supposedly does allocate-on-write for us */ |
1093 | /* mmap supposedly does allocate-on-write for us */ |
1085 | 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); |
1086 | |
1095 | |
1087 | if (cctx->sptr != (void *)-1) |
1096 | if (cctx->sptr != (void *)-1) |
1088 | { |
1097 | { |
… | |
… | |
1094 | cctx->flags |= CC_MAPPED; |
1103 | cctx->flags |= CC_MAPPED; |
1095 | } |
1104 | } |
1096 | else |
1105 | else |
1097 | #endif |
1106 | #endif |
1098 | { |
1107 | { |
1099 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1108 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1100 | New (0, cctx->sptr, coro_stacksize, long); |
1109 | New (0, cctx->sptr, cctx_stacksize, long); |
1101 | |
1110 | |
1102 | if (!cctx->sptr) |
1111 | if (!cctx->sptr) |
1103 | { |
1112 | { |
1104 | perror ("FATAL: unable to allocate stack for coroutine"); |
1113 | perror ("FATAL: unable to allocate stack for coroutine"); |
1105 | _exit (EXIT_FAILURE); |
1114 | _exit (EXIT_FAILURE); |
… | |
… | |
1120 | { |
1129 | { |
1121 | if (!cctx) |
1130 | if (!cctx) |
1122 | return; |
1131 | return; |
1123 | |
1132 | |
1124 | --cctx_count; |
1133 | --cctx_count; |
|
|
1134 | coro_destroy (&cctx->cctx); |
1125 | |
1135 | |
|
|
1136 | /* coro_transfer creates new, empty cctx's */ |
|
|
1137 | if (cctx->sptr) |
|
|
1138 | { |
1126 | #if CORO_USE_VALGRIND |
1139 | #if CORO_USE_VALGRIND |
1127 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1140 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1128 | #endif |
1141 | #endif |
1129 | |
1142 | |
1130 | #if HAVE_MMAP |
1143 | #if HAVE_MMAP |
1131 | if (cctx->flags & CC_MAPPED) |
1144 | if (cctx->flags & CC_MAPPED) |
1132 | munmap (cctx->sptr, cctx->ssize); |
1145 | munmap (cctx->sptr, cctx->ssize); |
1133 | else |
1146 | else |
1134 | #endif |
1147 | #endif |
1135 | Safefree (cctx->sptr); |
1148 | Safefree (cctx->sptr); |
|
|
1149 | } |
1136 | |
1150 | |
1137 | Safefree (cctx); |
1151 | Safefree (cctx); |
1138 | } |
1152 | } |
1139 | |
1153 | |
1140 | /* wether this cctx should be destructed */ |
1154 | /* wether this cctx should be destructed */ |
1141 | #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)) |
1142 | |
1156 | |
1143 | static coro_cctx * |
1157 | static coro_cctx * |
1144 | cctx_get (pTHX) |
1158 | cctx_get (pTHX) |
1145 | { |
1159 | { |
1146 | while (expect_true (cctx_first)) |
1160 | while (expect_true (cctx_first)) |
… | |
… | |
1159 | } |
1173 | } |
1160 | |
1174 | |
1161 | static void |
1175 | static void |
1162 | cctx_put (coro_cctx *cctx) |
1176 | cctx_put (coro_cctx *cctx) |
1163 | { |
1177 | { |
|
|
1178 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1179 | |
1164 | /* free another cctx if overlimit */ |
1180 | /* free another cctx if overlimit */ |
1165 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1181 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1166 | { |
1182 | { |
1167 | coro_cctx *first = cctx_first; |
1183 | coro_cctx *first = cctx_first; |
1168 | cctx_first = first->next; |
1184 | cctx_first = first->next; |
1169 | --cctx_idle; |
1185 | --cctx_idle; |
1170 | |
1186 | |
… | |
… | |
1202 | /* always use the TRANSFER macro */ |
1218 | /* always use the TRANSFER macro */ |
1203 | static void NOINLINE |
1219 | static void NOINLINE |
1204 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1220 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1205 | { |
1221 | { |
1206 | dSTACKLEVEL; |
1222 | dSTACKLEVEL; |
1207 | static volatile int has_throw; |
|
|
1208 | |
1223 | |
1209 | /* sometimes transfer is only called to set idle_sp */ |
1224 | /* sometimes transfer is only called to set idle_sp */ |
1210 | if (expect_false (!next)) |
1225 | if (expect_false (!next)) |
1211 | { |
1226 | { |
1212 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1227 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1213 | 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 */ |
1214 | } |
1229 | } |
1215 | else if (expect_true (prev != next)) |
1230 | else if (expect_true (prev != next)) |
1216 | { |
1231 | { |
|
|
1232 | static volatile int has_throw; |
1217 | coro_cctx *prev__cctx; |
1233 | coro_cctx *prev__cctx; |
1218 | |
1234 | |
1219 | if (expect_false (prev->flags & CF_NEW)) |
1235 | if (expect_false (prev->flags & CF_NEW)) |
1220 | { |
1236 | { |
1221 | /* create a new empty context */ |
1237 | /* create a new empty/source context */ |
|
|
1238 | ++cctx_count; |
1222 | 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 | |
1223 | prev->flags &= ~CF_NEW; |
1243 | prev->flags &= ~CF_NEW; |
1224 | prev->flags |= CF_RUNNING; |
1244 | prev->flags |= CF_RUNNING; |
1225 | } |
1245 | } |
1226 | |
1246 | |
1227 | prev->flags &= ~CF_RUNNING; |
1247 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1334 | croak ("FATAL: tried to destroy currently running coroutine"); |
1354 | croak ("FATAL: tried to destroy currently running coroutine"); |
1335 | |
1355 | |
1336 | save_perl (aTHX_ &temp); |
1356 | save_perl (aTHX_ &temp); |
1337 | load_perl (aTHX_ coro); |
1357 | load_perl (aTHX_ coro); |
1338 | |
1358 | |
1339 | coro_destroy (aTHX_ coro); |
1359 | coro_destruct (aTHX_ coro); |
1340 | |
1360 | |
1341 | load_perl (aTHX_ &temp); |
1361 | load_perl (aTHX_ &temp); |
1342 | |
1362 | |
1343 | coro->slot = 0; |
1363 | coro->slot = 0; |
1344 | } |
1364 | } |
… | |
… | |
1450 | LOCK; |
1470 | LOCK; |
1451 | |
1471 | |
1452 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1472 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1453 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1473 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1454 | |
1474 | |
1455 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1475 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1456 | ++coro_nready; |
1476 | ++coro_nready; |
1457 | |
1477 | |
1458 | UNLOCK; |
1478 | UNLOCK; |
1459 | |
1479 | |
1460 | if (sv_hook) |
1480 | if (sv_hook) |
… | |
… | |
1688 | { |
1708 | { |
1689 | api_cede (); |
1709 | api_cede (); |
1690 | self->next = now + self->every; |
1710 | self->next = now + self->every; |
1691 | } |
1711 | } |
1692 | |
1712 | |
1693 | return PerlIOBuf_flush (f); |
1713 | return PerlIOBuf_flush (aTHX_ f); |
1694 | } |
1714 | } |
1695 | |
1715 | |
1696 | static PerlIO_funcs PerlIO_cede = |
1716 | static PerlIO_funcs PerlIO_cede = |
1697 | { |
1717 | { |
1698 | sizeof(PerlIO_funcs), |
1718 | sizeof(PerlIO_funcs), |
… | |
… | |
1731 | PROTOTYPES: DISABLE |
1751 | PROTOTYPES: DISABLE |
1732 | |
1752 | |
1733 | BOOT: |
1753 | BOOT: |
1734 | { |
1754 | { |
1735 | #ifdef USE_ITHREADS |
1755 | #ifdef USE_ITHREADS |
1736 | MUTEX_INIT (&coro_mutex); |
1756 | MUTEX_INIT (&coro_lock); |
1737 | #endif |
1757 | #endif |
1738 | BOOT_PAGESIZE; |
1758 | BOOT_PAGESIZE; |
1739 | |
1759 | |
1740 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1760 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1741 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1761 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1828 | ta.next = 0; |
1848 | ta.next = 0; |
1829 | break; |
1849 | break; |
1830 | |
1850 | |
1831 | case 1: |
1851 | case 1: |
1832 | if (items != 2) |
1852 | if (items != 2) |
1833 | 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); |
1834 | |
1854 | |
1835 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1855 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1836 | break; |
1856 | break; |
1837 | |
1857 | |
1838 | case 2: |
1858 | case 2: |
… | |
… | |
1872 | _exit (code); |
1892 | _exit (code); |
1873 | |
1893 | |
1874 | int |
1894 | int |
1875 | cctx_stacksize (int new_stacksize = 0) |
1895 | cctx_stacksize (int new_stacksize = 0) |
1876 | CODE: |
1896 | CODE: |
1877 | RETVAL = coro_stacksize; |
1897 | RETVAL = cctx_stacksize; |
1878 | if (new_stacksize) |
1898 | if (new_stacksize) |
|
|
1899 | { |
1879 | 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; |
1880 | OUTPUT: |
1912 | OUTPUT: |
1881 | RETVAL |
1913 | RETVAL |
1882 | |
1914 | |
1883 | int |
1915 | int |
1884 | cctx_count () |
1916 | cctx_count () |
… | |
… | |
2001 | CODE: |
2033 | CODE: |
2002 | struct coro *coro = SvSTATE (coro_current); |
2034 | struct coro *coro = SvSTATE (coro_current); |
2003 | coro->cctx->idle_sp = 0; |
2035 | coro->cctx->idle_sp = 0; |
2004 | |
2036 | |
2005 | void |
2037 | void |
2006 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2007 | PROTOTYPE: $;$ |
|
|
2008 | CODE: |
|
|
2009 | SvREFCNT_dec (self->throw); |
|
|
2010 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2011 | |
|
|
2012 | void |
|
|
2013 | swap_defsv (Coro::State self) |
2038 | swap_defsv (Coro::State self) |
2014 | PROTOTYPE: $ |
2039 | PROTOTYPE: $ |
2015 | ALIAS: |
2040 | ALIAS: |
2016 | swap_defav = 1 |
2041 | swap_defav = 1 |
2017 | CODE: |
2042 | CODE: |
… | |
… | |
2071 | void |
2096 | void |
2072 | _set_current (SV *current) |
2097 | _set_current (SV *current) |
2073 | PROTOTYPE: $ |
2098 | PROTOTYPE: $ |
2074 | CODE: |
2099 | CODE: |
2075 | SvREFCNT_dec (SvRV (coro_current)); |
2100 | SvREFCNT_dec (SvRV (coro_current)); |
2076 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
2101 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
2077 | |
2102 | |
2078 | void |
2103 | void |
2079 | _set_readyhook (SV *hook) |
2104 | _set_readyhook (SV *hook) |
2080 | PROTOTYPE: $ |
2105 | PROTOTYPE: $ |
2081 | CODE: |
2106 | CODE: |
… | |
… | |
2120 | CODE: |
2145 | CODE: |
2121 | RETVAL = coro_nready; |
2146 | RETVAL = coro_nready; |
2122 | OUTPUT: |
2147 | OUTPUT: |
2123 | RETVAL |
2148 | RETVAL |
2124 | |
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 | |
2125 | # for async_pool speedup |
2157 | # for async_pool speedup |
2126 | void |
2158 | void |
2127 | _pool_1 (SV *cb) |
2159 | _pool_1 (SV *cb) |
2128 | CODE: |
2160 | CODE: |
2129 | { |
2161 | { |
… | |
… | |
2141 | SvREFCNT_dec (old); |
2173 | SvREFCNT_dec (old); |
2142 | croak ("\3async_pool terminate\2\n"); |
2174 | croak ("\3async_pool terminate\2\n"); |
2143 | } |
2175 | } |
2144 | |
2176 | |
2145 | SvREFCNT_dec (coro->saved_deffh); |
2177 | SvREFCNT_dec (coro->saved_deffh); |
2146 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
2178 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
2147 | |
2179 | |
2148 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2180 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2149 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2181 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2150 | |
2182 | |
2151 | invoke_av = (AV *)SvRV (invoke); |
2183 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
2155 | |
2187 | |
2156 | if (len > 0) |
2188 | if (len > 0) |
2157 | { |
2189 | { |
2158 | av_fill (defav, len - 1); |
2190 | av_fill (defav, len - 1); |
2159 | for (i = 0; i < len; ++i) |
2191 | for (i = 0; i < len; ++i) |
2160 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
2192 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2161 | } |
2193 | } |
2162 | |
2194 | |
2163 | SvREFCNT_dec (invoke); |
2195 | SvREFCNT_dec (invoke); |
2164 | } |
2196 | } |
2165 | |
2197 | |
… | |
… | |
2244 | #endif |
2276 | #endif |
2245 | |
2277 | |
2246 | |
2278 | |
2247 | MODULE = Coro::State PACKAGE = Coro::AIO |
2279 | MODULE = Coro::State PACKAGE = Coro::AIO |
2248 | |
2280 | |
2249 | SV * |
2281 | void |
2250 | _get_state () |
2282 | _get_state (SV *self) |
2251 | CODE: |
2283 | PPCODE: |
2252 | { |
2284 | { |
2253 | struct io_state *data; |
2285 | AV *defav = GvAV (PL_defgv); |
2254 | |
2286 | AV *av = newAV (); |
|
|
2287 | int i; |
2255 | RETVAL = newSV (sizeof (struct io_state)); |
2288 | SV *data_sv = newSV (sizeof (struct io_state)); |
2256 | data = (struct io_state *)SvPVX (RETVAL); |
2289 | struct io_state *data = (struct io_state *)SvPVX (data_sv); |
2257 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
2290 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2258 | SvPOK_only (RETVAL); |
2291 | SvPOK_only (data_sv); |
2259 | |
2292 | |
2260 | data->errorno = errno; |
2293 | data->errorno = errno; |
2261 | data->laststype = PL_laststype; |
2294 | data->laststype = PL_laststype; |
2262 | data->laststatval = PL_laststatval; |
2295 | data->laststatval = PL_laststatval; |
2263 | data->statcache = PL_statcache; |
2296 | data->statcache = PL_statcache; |
|
|
2297 | |
|
|
2298 | av_extend (av, AvFILLp (defav) + 1 + 1); |
|
|
2299 | |
|
|
2300 | for (i = 0; i <= AvFILLp (defav); ++i) |
|
|
2301 | av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i])); |
|
|
2302 | |
|
|
2303 | av_push (av, data_sv); |
|
|
2304 | |
|
|
2305 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
2306 | |
|
|
2307 | api_ready (self); |
2264 | } |
2308 | } |
2265 | OUTPUT: |
|
|
2266 | RETVAL |
|
|
2267 | |
2309 | |
2268 | void |
2310 | void |
2269 | _set_state (char *data_) |
2311 | _set_state (SV *state) |
2270 | PROTOTYPE: $ |
2312 | PROTOTYPE: $ |
2271 | CODE: |
2313 | PPCODE: |
2272 | { |
2314 | { |
2273 | struct io_state *data = (void *)data_; |
2315 | AV *av = (AV *)SvRV (state); |
|
|
2316 | struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]); |
|
|
2317 | int i; |
2274 | |
2318 | |
2275 | errno = data->errorno; |
2319 | errno = data->errorno; |
2276 | PL_laststype = data->laststype; |
2320 | PL_laststype = data->laststype; |
2277 | PL_laststatval = data->laststatval; |
2321 | PL_laststatval = data->laststatval; |
2278 | PL_statcache = data->statcache; |
2322 | PL_statcache = data->statcache; |
|
|
2323 | |
|
|
2324 | EXTEND (SP, AvFILLp (av)); |
|
|
2325 | for (i = 0; i < AvFILLp (av); ++i) |
|
|
2326 | PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i]))); |
2279 | } |
2327 | } |
2280 | |
2328 | |
2281 | |
2329 | |
2282 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2330 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2283 | |
2331 | |