1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
4 | |
2 | |
5 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
6 | #include "perl.h" |
4 | #include "perl.h" |
7 | #include "XSUB.h" |
5 | #include "XSUB.h" |
8 | |
6 | |
9 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
10 | |
8 | |
11 | #if PERL_VERSION < 6 |
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
|
|
12 | |
|
|
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
14 | (PERL_REVISION > (a) \ |
|
|
15 | || (PERL_REVISION == (a) \ |
|
|
16 | && (PERL_VERSION > (b) \ |
|
|
17 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
18 | |
|
|
19 | #if !PERL_VERSION_ATLEAST (5,6,0) |
12 | # ifndef PL_ppaddr |
20 | # ifndef PL_ppaddr |
13 | # define PL_ppaddr ppaddr |
21 | # define PL_ppaddr ppaddr |
14 | # endif |
22 | # endif |
15 | # ifndef call_sv |
23 | # ifndef call_sv |
16 | # define call_sv perl_call_sv |
24 | # define call_sv perl_call_sv |
… | |
… | |
54 | # ifndef PAGESIZE |
62 | # ifndef PAGESIZE |
55 | # define PAGESIZE pagesize |
63 | # define PAGESIZE pagesize |
56 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
64 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
57 | static long pagesize; |
65 | static long pagesize; |
58 | # else |
66 | # else |
59 | # define BOOT_PAGESIZE |
67 | # define BOOT_PAGESIZE (void)0 |
60 | # endif |
68 | # endif |
|
|
69 | #else |
|
|
70 | # define PAGESIZE 0 |
|
|
71 | # define BOOT_PAGESIZE (void)0 |
61 | #endif |
72 | #endif |
62 | |
73 | |
63 | /* The next macro should declare a variable stacklevel that contains and approximation |
74 | /* The next macro should declare a variable stacklevel that contains and approximation |
64 | * to the current C stack pointer. Its property is that it changes with each call |
75 | * to the current C stack pointer. Its property is that it changes with each call |
65 | * and should be unique. */ |
76 | * and should be unique. */ |
66 | #define dSTACKLEVEL int stacklevel |
77 | #define dSTACKLEVEL int stacklevel |
67 | #define STACKLEVEL ((void *)&stacklevel) |
78 | #define STACKLEVEL ((void *)&stacklevel) |
68 | |
79 | |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
80 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
70 | |
81 | |
71 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
82 | #if __GNUC__ >= 3 |
|
|
83 | # define attribute(x) __attribute__(x) |
|
|
84 | #else |
|
|
85 | # define attribute(x) |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | #define NOINLINE attribute ((noinline)) |
72 | |
89 | |
73 | #include "CoroAPI.h" |
90 | #include "CoroAPI.h" |
74 | |
91 | |
75 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
92 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
76 | |
93 | |
… | |
… | |
86 | static struct CoroAPI coroapi; |
103 | static struct CoroAPI coroapi; |
87 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
88 | static HV *coro_state_stash, *coro_stash; |
105 | static HV *coro_state_stash, *coro_stash; |
89 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
90 | |
107 | |
|
|
108 | static struct coro_cctx *cctx_first; |
|
|
109 | static int cctx_count, cctx_idle; |
|
|
110 | |
91 | /* this is a structure representing a c-level coroutine */ |
111 | /* this is a structure representing a c-level coroutine */ |
92 | typedef struct coro_stack { |
112 | typedef struct coro_cctx { |
93 | struct coro_stack *next; |
113 | struct coro_cctx *next; |
94 | |
114 | |
95 | void *idle_sp; /* original stacklevel when coroutine was created */ |
115 | /* the stack */ |
96 | void *sptr; |
116 | void *sptr; |
97 | long ssize; /* positive == mmap, otherwise malloc */ |
117 | long ssize; /* positive == mmap, otherwise malloc */ |
98 | |
118 | |
99 | /* cpu state */ |
119 | /* cpu state */ |
|
|
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
121 | JMPENV *top_env; |
100 | coro_context cctx; |
122 | coro_context cctx; |
101 | JMPENV *top_env; |
|
|
102 | } coro_stack; |
|
|
103 | |
123 | |
104 | /* the (fake) coro_stack representing the main program */ |
124 | #if USE_VALGRIND |
105 | static coro_stack *main_stack; |
125 | int valgrind_id; |
|
|
126 | #endif |
|
|
127 | } coro_cctx; |
106 | |
128 | |
107 | /* this is a structure representing a perl-level coroutine */ |
129 | /* this is a structure representing a perl-level coroutine */ |
108 | struct coro { |
130 | struct coro { |
109 | /* the c coroutine allocated to this perl coroutine, if any */ |
131 | /* the c coroutine allocated to this perl coroutine, if any */ |
110 | coro_stack *stack; |
132 | coro_cctx *cctx; |
111 | |
133 | |
112 | /* data associated with this coroutine (initial args) */ |
134 | /* data associated with this coroutine (initial args) */ |
113 | AV *args; |
135 | AV *args; |
114 | int refcnt; |
136 | int refcnt; |
115 | |
137 | |
116 | /* optionally saved, might be zero */ |
138 | /* optionally saved, might be zero */ |
117 | AV *defav; |
139 | AV *defav; |
118 | SV *defsv; |
140 | SV *defsv; |
119 | SV *errsv; |
141 | SV *errsv; |
120 | |
142 | |
121 | /* saved global state not related to stacks */ |
143 | #define VAR(name,type) type name; |
122 | U8 dowarn; |
144 | # include "state.h" |
123 | I32 in_eval; |
145 | #undef VAR |
124 | |
|
|
125 | /* the stacks and related info (callchain etc..) */ |
|
|
126 | PERL_SI *curstackinfo; |
|
|
127 | AV *curstack; |
|
|
128 | AV *mainstack; |
|
|
129 | SV **stack_sp; |
|
|
130 | OP *op; |
|
|
131 | SV **curpad; |
|
|
132 | AV *comppad; |
|
|
133 | CV *compcv; |
|
|
134 | SV **stack_base; |
|
|
135 | SV **stack_max; |
|
|
136 | SV **tmps_stack; |
|
|
137 | I32 tmps_floor; |
|
|
138 | I32 tmps_ix; |
|
|
139 | I32 tmps_max; |
|
|
140 | I32 *markstack; |
|
|
141 | I32 *markstack_ptr; |
|
|
142 | I32 *markstack_max; |
|
|
143 | I32 *scopestack; |
|
|
144 | I32 scopestack_ix; |
|
|
145 | I32 scopestack_max; |
|
|
146 | ANY *savestack; |
|
|
147 | I32 savestack_ix; |
|
|
148 | I32 savestack_max; |
|
|
149 | OP **retstack; |
|
|
150 | I32 retstack_ix; |
|
|
151 | I32 retstack_max; |
|
|
152 | PMOP *curpm; |
|
|
153 | COP *curcop; |
|
|
154 | |
146 | |
155 | /* coro process data */ |
147 | /* coro process data */ |
156 | int prio; |
148 | int prio; |
157 | }; |
149 | }; |
158 | |
150 | |
159 | typedef struct coro *Coro__State; |
151 | typedef struct coro *Coro__State; |
160 | typedef struct coro *Coro__State_or_hashref; |
152 | typedef struct coro *Coro__State_or_hashref; |
161 | |
153 | |
162 | static AV * |
154 | static AV * |
163 | coro_clone_padlist (pTHX_ CV *cv) |
155 | coro_clone_padlist (CV *cv) |
164 | { |
156 | { |
165 | AV *padlist = CvPADLIST (cv); |
157 | AV *padlist = CvPADLIST (cv); |
166 | AV *newpadlist, *newpad; |
158 | AV *newpadlist, *newpad; |
167 | |
159 | |
168 | newpadlist = newAV (); |
160 | newpadlist = newAV (); |
169 | AvREAL_off (newpadlist); |
161 | AvREAL_off (newpadlist); |
170 | #if PERL_VERSION < 9 |
162 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
163 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
164 | #else |
171 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
165 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
172 | #else |
|
|
173 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
174 | #endif |
166 | #endif |
175 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
167 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
176 | --AvFILLp (padlist); |
168 | --AvFILLp (padlist); |
177 | |
169 | |
178 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
170 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
… | |
… | |
180 | |
172 | |
181 | return newpadlist; |
173 | return newpadlist; |
182 | } |
174 | } |
183 | |
175 | |
184 | static void |
176 | static void |
185 | free_padlist (pTHX_ AV *padlist) |
177 | free_padlist (AV *padlist) |
186 | { |
178 | { |
187 | /* may be during global destruction */ |
179 | /* may be during global destruction */ |
188 | if (SvREFCNT (padlist)) |
180 | if (SvREFCNT (padlist)) |
189 | { |
181 | { |
190 | I32 i = AvFILLp (padlist); |
182 | I32 i = AvFILLp (padlist); |
… | |
… | |
211 | AV *padlist; |
203 | AV *padlist; |
212 | AV *av = (AV *)mg->mg_obj; |
204 | AV *av = (AV *)mg->mg_obj; |
213 | |
205 | |
214 | /* casting is fun. */ |
206 | /* casting is fun. */ |
215 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
207 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
216 | free_padlist (aTHX_ padlist); |
208 | free_padlist (padlist); |
217 | |
209 | |
218 | SvREFCNT_dec (av); |
210 | SvREFCNT_dec (av); |
219 | |
211 | |
220 | return 0; |
212 | return 0; |
221 | } |
213 | } |
222 | |
214 | |
223 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
215 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
224 | |
216 | |
225 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
217 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
226 | |
218 | |
|
|
219 | #define CORO_MAGIC(cv) \ |
|
|
220 | SvMAGIC (cv) \ |
|
|
221 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
|
|
222 | ? SvMAGIC (cv) \ |
|
|
223 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
|
|
224 | : 0 |
|
|
225 | |
227 | /* the next two functions merely cache the padlists */ |
226 | /* the next two functions merely cache the padlists */ |
228 | static void |
227 | static void |
229 | get_padlist (pTHX_ CV *cv) |
228 | get_padlist (CV *cv) |
230 | { |
229 | { |
231 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
230 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
231 | AV *av; |
232 | |
232 | |
233 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
233 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
234 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
234 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
235 | else |
235 | else |
236 | { |
236 | { |
237 | #if 0 |
237 | #if 0 |
238 | /* this should work - but it doesn't :( */ |
238 | /* this is probably cleaner, but also slower? */ |
239 | CV *cp = Perl_cv_clone (aTHX_ cv); |
239 | CV *cp = Perl_cv_clone (cv); |
240 | CvPADLIST (cv) = CvPADLIST (cp); |
240 | CvPADLIST (cv) = CvPADLIST (cp); |
241 | CvPADLIST (cp) = 0; |
241 | CvPADLIST (cp) = 0; |
242 | SvREFCNT_dec (cp); |
242 | SvREFCNT_dec (cp); |
243 | #else |
243 | #else |
244 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
244 | CvPADLIST (cv) = coro_clone_padlist (cv); |
245 | #endif |
245 | #endif |
246 | } |
246 | } |
247 | } |
247 | } |
248 | |
248 | |
249 | static void |
249 | static void |
250 | put_padlist (pTHX_ CV *cv) |
250 | put_padlist (CV *cv) |
251 | { |
251 | { |
252 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
252 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
253 | AV *av; |
253 | |
254 | |
254 | if (!mg) |
255 | if (!mg) |
255 | { |
256 | { |
256 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
257 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
257 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
258 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
258 | mg->mg_virtual = &vtbl_coro; |
259 | mg->mg_virtual = &vtbl_coro; |
259 | mg->mg_obj = (SV *)newAV (); |
260 | mg->mg_obj = (SV *)newAV (); |
260 | } |
261 | } |
261 | |
262 | |
262 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
263 | av = (AV *)mg->mg_obj; |
|
|
264 | |
|
|
265 | if (AvFILLp (av) >= AvMAX (av)) |
|
|
266 | av_extend (av, AvMAX (av) + 1); |
|
|
267 | |
|
|
268 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
263 | } |
269 | } |
264 | |
270 | |
265 | #define SB do { |
271 | #define SB do { |
266 | #define SE } while (0) |
272 | #define SE } while (0) |
267 | |
273 | |
268 | #define LOAD(state) load_state(aTHX_ (state)); |
274 | #define LOAD(state) load_state((state)); |
269 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
275 | #define SAVE(state,flags) save_state((state),(flags)); |
270 | |
276 | |
271 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
277 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
272 | |
278 | |
273 | static void |
279 | static void |
274 | load_state(pTHX_ Coro__State c) |
280 | load_state(Coro__State c) |
275 | { |
281 | { |
276 | PL_dowarn = c->dowarn; |
282 | #define VAR(name,type) PL_ ## name = c->name; |
277 | PL_in_eval = c->in_eval; |
283 | # include "state.h" |
278 | |
284 | #undef VAR |
279 | PL_curstackinfo = c->curstackinfo; |
|
|
280 | PL_curstack = c->curstack; |
|
|
281 | PL_mainstack = c->mainstack; |
|
|
282 | PL_stack_sp = c->stack_sp; |
|
|
283 | PL_op = c->op; |
|
|
284 | PL_curpad = c->curpad; |
|
|
285 | PL_comppad = c->comppad; |
|
|
286 | PL_compcv = c->compcv; |
|
|
287 | PL_stack_base = c->stack_base; |
|
|
288 | PL_stack_max = c->stack_max; |
|
|
289 | PL_tmps_stack = c->tmps_stack; |
|
|
290 | PL_tmps_floor = c->tmps_floor; |
|
|
291 | PL_tmps_ix = c->tmps_ix; |
|
|
292 | PL_tmps_max = c->tmps_max; |
|
|
293 | PL_markstack = c->markstack; |
|
|
294 | PL_markstack_ptr = c->markstack_ptr; |
|
|
295 | PL_markstack_max = c->markstack_max; |
|
|
296 | PL_scopestack = c->scopestack; |
|
|
297 | PL_scopestack_ix = c->scopestack_ix; |
|
|
298 | PL_scopestack_max = c->scopestack_max; |
|
|
299 | PL_savestack = c->savestack; |
|
|
300 | PL_savestack_ix = c->savestack_ix; |
|
|
301 | PL_savestack_max = c->savestack_max; |
|
|
302 | #if PERL_VERSION < 9 |
|
|
303 | PL_retstack = c->retstack; |
|
|
304 | PL_retstack_ix = c->retstack_ix; |
|
|
305 | PL_retstack_max = c->retstack_max; |
|
|
306 | #endif |
|
|
307 | PL_curpm = c->curpm; |
|
|
308 | PL_curcop = c->curcop; |
|
|
309 | |
285 | |
310 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
286 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
311 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
287 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
312 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
288 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
313 | |
289 | |
… | |
… | |
316 | CV *cv; |
292 | CV *cv; |
317 | |
293 | |
318 | /* now do the ugly restore mess */ |
294 | /* now do the ugly restore mess */ |
319 | while ((cv = (CV *)POPs)) |
295 | while ((cv = (CV *)POPs)) |
320 | { |
296 | { |
321 | AV *padlist = (AV *)POPs; |
|
|
322 | |
|
|
323 | if (padlist) |
|
|
324 | { |
|
|
325 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
297 | put_padlist (cv); /* mark this padlist as available */ |
326 | CvPADLIST(cv) = padlist; |
298 | CvDEPTH (cv) = PTR2IV (POPs); |
327 | } |
299 | CvPADLIST (cv) = (AV *)POPs; |
328 | |
|
|
329 | ++CvDEPTH(cv); |
|
|
330 | } |
300 | } |
331 | |
301 | |
332 | PUTBACK; |
302 | PUTBACK; |
333 | } |
303 | } |
334 | } |
304 | } |
335 | |
305 | |
336 | static void |
306 | static void |
337 | save_state(pTHX_ Coro__State c, int flags) |
307 | save_state(Coro__State c, int flags) |
338 | { |
308 | { |
339 | { |
309 | { |
340 | dSP; |
310 | dSP; |
341 | I32 cxix = cxstack_ix; |
311 | I32 cxix = cxstack_ix; |
342 | PERL_CONTEXT *ccstk = cxstack; |
312 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
356 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
326 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
357 | |
327 | |
358 | if (CxTYPE(cx) == CXt_SUB) |
328 | if (CxTYPE(cx) == CXt_SUB) |
359 | { |
329 | { |
360 | CV *cv = cx->blk_sub.cv; |
330 | CV *cv = cx->blk_sub.cv; |
|
|
331 | |
361 | if (CvDEPTH(cv)) |
332 | if (CvDEPTH (cv)) |
362 | { |
333 | { |
363 | EXTEND (SP, CvDEPTH(cv)*2); |
334 | EXTEND (SP, 3); |
364 | |
|
|
365 | while (--CvDEPTH(cv)) |
|
|
366 | { |
|
|
367 | /* this tells the restore code to increment CvDEPTH */ |
|
|
368 | PUSHs (Nullsv); |
|
|
369 | PUSHs ((SV *)cv); |
|
|
370 | } |
|
|
371 | |
335 | |
372 | PUSHs ((SV *)CvPADLIST(cv)); |
336 | PUSHs ((SV *)CvPADLIST(cv)); |
|
|
337 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
373 | PUSHs ((SV *)cv); |
338 | PUSHs ((SV *)cv); |
374 | |
339 | |
|
|
340 | CvDEPTH (cv) = 0; |
375 | get_padlist (aTHX_ cv); |
341 | get_padlist (cv); |
376 | } |
342 | } |
377 | } |
343 | } |
378 | #ifdef CXt_FORMAT |
344 | #ifdef CXt_FORMAT |
379 | else if (CxTYPE(cx) == CXt_FORMAT) |
345 | else if (CxTYPE(cx) == CXt_FORMAT) |
380 | { |
346 | { |
… | |
… | |
398 | |
364 | |
399 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
365 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
400 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
366 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
401 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
367 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
402 | |
368 | |
403 | c->dowarn = PL_dowarn; |
369 | #define VAR(name,type)c->name = PL_ ## name; |
404 | c->in_eval = PL_in_eval; |
370 | # include "state.h" |
405 | |
371 | #undef VAR |
406 | c->curstackinfo = PL_curstackinfo; |
|
|
407 | c->curstack = PL_curstack; |
|
|
408 | c->mainstack = PL_mainstack; |
|
|
409 | c->stack_sp = PL_stack_sp; |
|
|
410 | c->op = PL_op; |
|
|
411 | c->curpad = PL_curpad; |
|
|
412 | c->comppad = PL_comppad; |
|
|
413 | c->compcv = PL_compcv; |
|
|
414 | c->stack_base = PL_stack_base; |
|
|
415 | c->stack_max = PL_stack_max; |
|
|
416 | c->tmps_stack = PL_tmps_stack; |
|
|
417 | c->tmps_floor = PL_tmps_floor; |
|
|
418 | c->tmps_ix = PL_tmps_ix; |
|
|
419 | c->tmps_max = PL_tmps_max; |
|
|
420 | c->markstack = PL_markstack; |
|
|
421 | c->markstack_ptr = PL_markstack_ptr; |
|
|
422 | c->markstack_max = PL_markstack_max; |
|
|
423 | c->scopestack = PL_scopestack; |
|
|
424 | c->scopestack_ix = PL_scopestack_ix; |
|
|
425 | c->scopestack_max = PL_scopestack_max; |
|
|
426 | c->savestack = PL_savestack; |
|
|
427 | c->savestack_ix = PL_savestack_ix; |
|
|
428 | c->savestack_max = PL_savestack_max; |
|
|
429 | #if PERL_VERSION < 9 |
|
|
430 | c->retstack = PL_retstack; |
|
|
431 | c->retstack_ix = PL_retstack_ix; |
|
|
432 | c->retstack_max = PL_retstack_max; |
|
|
433 | #endif |
|
|
434 | c->curpm = PL_curpm; |
|
|
435 | c->curcop = PL_curcop; |
|
|
436 | } |
372 | } |
437 | |
373 | |
438 | /* |
374 | /* |
439 | * allocate various perl stacks. This is an exact copy |
375 | * allocate various perl stacks. This is an exact copy |
440 | * of perl.c:init_stacks, except that it uses less memory |
376 | * of perl.c:init_stacks, except that it uses less memory |
441 | * on the (sometimes correct) assumption that coroutines do |
377 | * on the (sometimes correct) assumption that coroutines do |
442 | * not usually need a lot of stackspace. |
378 | * not usually need a lot of stackspace. |
443 | */ |
379 | */ |
444 | static void |
380 | static void |
445 | coro_init_stacks (pTHX) |
381 | coro_init_stacks () |
446 | { |
382 | { |
447 | LOCK; |
|
|
448 | |
|
|
449 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
383 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
450 | PL_curstackinfo->si_type = PERLSI_MAIN; |
384 | PL_curstackinfo->si_type = PERLSI_MAIN; |
451 | PL_curstack = PL_curstackinfo->si_stack; |
385 | PL_curstack = PL_curstackinfo->si_stack; |
452 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
386 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
453 | |
387 | |
… | |
… | |
474 | |
408 | |
475 | New(54,PL_savestack,96,ANY); |
409 | New(54,PL_savestack,96,ANY); |
476 | PL_savestack_ix = 0; |
410 | PL_savestack_ix = 0; |
477 | PL_savestack_max = 96; |
411 | PL_savestack_max = 96; |
478 | |
412 | |
479 | #if PERL_VERSION < 9 |
413 | #if !PERL_VERSION_ATLEAST (5,9,0) |
480 | New(54,PL_retstack,8,OP*); |
414 | New(54,PL_retstack,8,OP*); |
481 | PL_retstack_ix = 0; |
415 | PL_retstack_ix = 0; |
482 | PL_retstack_max = 8; |
416 | PL_retstack_max = 8; |
483 | #endif |
417 | #endif |
484 | |
|
|
485 | UNLOCK; |
|
|
486 | } |
418 | } |
487 | |
419 | |
488 | /* |
420 | /* |
489 | * destroy the stacks, the callchain etc... |
421 | * destroy the stacks, the callchain etc... |
490 | */ |
422 | */ |
491 | static void |
423 | static void |
492 | destroy_stacks(pTHX) |
424 | coro_destroy_stacks () |
493 | { |
425 | { |
494 | if (!IN_DESTRUCT) |
426 | if (!IN_DESTRUCT) |
495 | { |
427 | { |
496 | /* is this ugly, I ask? */ |
428 | /* is this ugly, I ask? */ |
497 | LEAVE_SCOPE (0); |
429 | LEAVE_SCOPE (0); |
… | |
… | |
528 | |
460 | |
529 | Safefree (PL_tmps_stack); |
461 | Safefree (PL_tmps_stack); |
530 | Safefree (PL_markstack); |
462 | Safefree (PL_markstack); |
531 | Safefree (PL_scopestack); |
463 | Safefree (PL_scopestack); |
532 | Safefree (PL_savestack); |
464 | Safefree (PL_savestack); |
533 | #if PERL_VERSION < 9 |
465 | #if !PERL_VERSION_ATLEAST (5,9,0) |
534 | Safefree (PL_retstack); |
466 | Safefree (PL_retstack); |
535 | #endif |
467 | #endif |
536 | } |
468 | } |
537 | |
469 | |
538 | static void |
470 | static void |
539 | setup_coro (struct coro *coro) |
471 | setup_coro (struct coro *coro) |
540 | { |
472 | { |
541 | /* |
473 | /* |
542 | * emulate part of the perl startup here. |
474 | * emulate part of the perl startup here. |
543 | */ |
475 | */ |
544 | dTHX; |
|
|
545 | dSP; |
|
|
546 | UNOP myop; |
|
|
547 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
|
|
548 | |
476 | |
549 | coro_init_stacks (aTHX); |
477 | coro_init_stacks (); |
|
|
478 | |
550 | /*PL_curcop = 0;*/ |
479 | PL_curcop = 0; |
551 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
480 | PL_in_eval = 0; |
|
|
481 | PL_curpm = 0; |
|
|
482 | |
|
|
483 | { |
|
|
484 | dSP; |
|
|
485 | LOGOP myop; |
|
|
486 | |
|
|
487 | /* I have no idea why this is needed, but it is */ |
|
|
488 | PUSHMARK (SP); |
|
|
489 | |
552 | SvREFCNT_dec (GvAV (PL_defgv)); |
490 | SvREFCNT_dec (GvAV (PL_defgv)); |
553 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
491 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
554 | |
492 | |
555 | SPAGAIN; |
|
|
556 | |
|
|
557 | Zero (&myop, 1, UNOP); |
493 | Zero (&myop, 1, LOGOP); |
558 | myop.op_next = Nullop; |
494 | myop.op_next = Nullop; |
559 | myop.op_flags = OPf_WANT_VOID; |
495 | myop.op_flags = OPf_WANT_VOID; |
560 | |
496 | |
561 | PL_op = (OP *)&myop; |
497 | PL_op = (OP *)&myop; |
562 | |
498 | |
563 | PUSHMARK(SP); |
499 | PUSHMARK (SP); |
564 | XPUSHs (sub_init); |
500 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
565 | PUTBACK; |
501 | PUTBACK; |
566 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
502 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
567 | SPAGAIN; |
503 | SPAGAIN; |
568 | |
504 | |
569 | ENTER; /* necessary e.g. for dounwind */ |
505 | ENTER; /* necessary e.g. for dounwind */ |
|
|
506 | } |
570 | } |
507 | } |
571 | |
508 | |
572 | static void |
509 | static void |
573 | free_coro_mortal () |
510 | free_coro_mortal () |
574 | { |
511 | { |
… | |
… | |
577 | SvREFCNT_dec (coro_mortal); |
514 | SvREFCNT_dec (coro_mortal); |
578 | coro_mortal = 0; |
515 | coro_mortal = 0; |
579 | } |
516 | } |
580 | } |
517 | } |
581 | |
518 | |
|
|
519 | static void NOINLINE |
|
|
520 | prepare_cctx (coro_cctx *cctx) |
|
|
521 | { |
|
|
522 | dSP; |
|
|
523 | LOGOP myop; |
|
|
524 | |
|
|
525 | Zero (&myop, 1, LOGOP); |
|
|
526 | myop.op_next = PL_op; |
|
|
527 | myop.op_flags = OPf_WANT_VOID; |
|
|
528 | |
|
|
529 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
530 | |
|
|
531 | PUSHMARK (SP); |
|
|
532 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
|
|
533 | PUTBACK; |
|
|
534 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
535 | SPAGAIN; |
|
|
536 | } |
|
|
537 | |
582 | static void |
538 | static void |
583 | coro_run (void *arg) |
539 | coro_run (void *arg) |
584 | { |
540 | { |
|
|
541 | /* coro_run is the alternative epilogue of transfer() */ |
|
|
542 | UNLOCK; |
|
|
543 | |
585 | /* |
544 | /* |
586 | * this is a _very_ stripped down perl interpreter ;) |
545 | * this is a _very_ stripped down perl interpreter ;) |
587 | */ |
546 | */ |
588 | dTHX; |
|
|
589 | PL_top_env = &PL_start_env; |
547 | PL_top_env = &PL_start_env; |
590 | |
548 | |
591 | UNLOCK; |
549 | /* inject call to cctx_init */ |
|
|
550 | prepare_cctx ((coro_cctx *)arg); |
592 | |
551 | |
593 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); |
|
|
594 | sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); |
|
|
595 | PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
596 | |
|
|
597 | /* somebody will hit me for both perl_run and PL_restart_op */ |
552 | /* somebody will hit me for both perl_run and PL_restartop */ |
598 | perl_run (aTHX_ PERL_GET_CONTEXT); |
553 | perl_run (PL_curinterp); |
599 | |
554 | |
|
|
555 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
600 | abort (); |
556 | abort (); |
601 | } |
557 | } |
602 | |
558 | |
603 | static coro_stack * |
559 | static coro_cctx * |
604 | stack_new () |
560 | cctx_new () |
605 | { |
561 | { |
606 | coro_stack *stack; |
562 | coro_cctx *cctx; |
607 | |
563 | |
|
|
564 | ++cctx_count; |
|
|
565 | |
608 | New (0, stack, 1, coro_stack); |
566 | New (0, cctx, 1, coro_cctx); |
609 | |
567 | |
610 | #if HAVE_MMAP |
568 | #if HAVE_MMAP |
611 | |
569 | |
612 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
570 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
|
|
571 | /* mmap suppsedly does allocate-on-write for us */ |
613 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
572 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
614 | |
573 | |
615 | if (stack->sptr == (void *)-1) |
574 | if (cctx->sptr == (void *)-1) |
616 | { |
575 | { |
617 | perror ("FATAL: unable to mmap stack for coroutine"); |
576 | perror ("FATAL: unable to mmap stack for coroutine"); |
618 | _exit (EXIT_FAILURE); |
577 | _exit (EXIT_FAILURE); |
619 | } |
578 | } |
620 | |
579 | |
|
|
580 | # if STACKGUARD |
621 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
581 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
582 | # endif |
622 | |
583 | |
623 | #else |
584 | #else |
624 | |
585 | |
625 | stack->ssize = STACKSIZE * (long)sizeof (long); |
586 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
626 | New (0, stack->sptr, STACKSIZE, long); |
587 | New (0, cctx->sptr, STACKSIZE, long); |
627 | |
588 | |
628 | if (!stack->sptr) |
589 | if (!cctx->sptr) |
629 | { |
590 | { |
630 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
591 | perror ("FATAL: unable to malloc stack for coroutine"); |
631 | _exit (EXIT_FAILURE); |
592 | _exit (EXIT_FAILURE); |
632 | } |
593 | } |
633 | |
594 | |
634 | #endif |
595 | #endif |
635 | |
596 | |
|
|
597 | #if USE_VALGRIND |
|
|
598 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
599 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
600 | cctx->ssize + (char *)cctx->sptr |
|
|
601 | ); |
|
|
602 | #endif |
|
|
603 | |
636 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
604 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
637 | |
605 | |
638 | return stack; |
606 | return cctx; |
639 | } |
607 | } |
640 | |
608 | |
641 | static void |
609 | static void |
642 | stack_free (coro_stack *stack) |
610 | cctx_free (coro_cctx *cctx) |
643 | { |
611 | { |
644 | if (!stack || stack == main_stack) |
612 | if (!cctx) |
645 | return; |
613 | return; |
646 | |
614 | |
|
|
615 | --cctx_count; |
|
|
616 | |
|
|
617 | #if USE_VALGRIND |
|
|
618 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
619 | #endif |
|
|
620 | |
647 | #if HAVE_MMAP |
621 | #if HAVE_MMAP |
648 | munmap (stack->sptr, stack->ssize); |
622 | munmap (cctx->sptr, cctx->ssize); |
649 | #else |
623 | #else |
650 | Safefree (stack->sptr); |
624 | Safefree (cctx->sptr); |
651 | #endif |
625 | #endif |
652 | |
626 | |
653 | Safefree (stack); |
627 | Safefree (cctx); |
654 | } |
628 | } |
655 | |
629 | |
656 | static coro_stack *stack_first; |
|
|
657 | |
|
|
658 | static coro_stack * |
630 | static coro_cctx * |
659 | stack_get () |
631 | cctx_get () |
660 | { |
632 | { |
661 | coro_stack *stack; |
633 | coro_cctx *cctx; |
662 | |
634 | |
663 | if (stack_first) |
635 | if (cctx_first) |
664 | { |
636 | { |
665 | stack = stack_first; |
637 | --cctx_idle; |
|
|
638 | cctx = cctx_first; |
666 | stack_first = stack->next; |
639 | cctx_first = cctx->next; |
667 | } |
640 | } |
668 | else |
641 | else |
669 | { |
642 | { |
670 | stack = stack_new (); |
643 | cctx = cctx_new (); |
671 | PL_op = PL_op->op_next; |
644 | PL_op = PL_op->op_next; |
672 | } |
645 | } |
673 | |
646 | |
674 | return stack; |
647 | return cctx; |
675 | } |
648 | } |
676 | |
649 | |
677 | static void |
650 | static void |
678 | stack_put (coro_stack *stack) |
651 | cctx_put (coro_cctx *cctx) |
679 | { |
652 | { |
680 | stack->next = stack_first; |
653 | ++cctx_idle; |
681 | stack_first = stack; |
654 | cctx->next = cctx_first; |
|
|
655 | cctx_first = cctx; |
682 | } |
656 | } |
683 | |
657 | |
684 | /* never call directly, always through the coro_state_transfer global variable */ |
658 | /* never call directly, always through the coro_state_transfer global variable */ |
685 | static void |
659 | static void NOINLINE |
686 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
660 | transfer (struct coro *prev, struct coro *next, int flags) |
687 | { |
661 | { |
688 | dSTACKLEVEL; |
662 | dSTACKLEVEL; |
689 | |
663 | |
690 | /* sometimes transfer is only called to set idle_sp */ |
664 | /* sometimes transfer is only called to set idle_sp */ |
691 | if (flags == TRANSFER_SET_STACKLEVEL) |
665 | if (flags == TRANSFER_SET_STACKLEVEL) |
692 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
666 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
693 | else if (prev != next) |
667 | else if (prev != next) |
694 | { |
668 | { |
695 | coro_stack *prev__stack; |
669 | coro_cctx *prev__cctx; |
696 | |
670 | |
697 | LOCK; |
671 | LOCK; |
698 | |
672 | |
699 | if (next->mainstack) |
673 | if (next->mainstack) |
700 | { |
674 | { |
… | |
… | |
708 | /* first get rid of the old state */ |
682 | /* first get rid of the old state */ |
709 | SAVE (prev, -1); |
683 | SAVE (prev, -1); |
710 | /* setup coroutine call */ |
684 | /* setup coroutine call */ |
711 | setup_coro (next); |
685 | setup_coro (next); |
712 | /* need a stack */ |
686 | /* need a stack */ |
713 | next->stack = 0; |
687 | next->cctx = 0; |
714 | } |
688 | } |
715 | |
689 | |
|
|
690 | if (!prev->cctx) |
|
|
691 | /* create a new empty context */ |
|
|
692 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
693 | |
716 | prev__stack = prev->stack; |
694 | prev__cctx = prev->cctx; |
717 | |
695 | |
718 | /* possibly "free" the stack */ |
696 | /* possibly "free" the cctx */ |
719 | if (prev__stack->idle_sp == STACKLEVEL) |
697 | if (prev__cctx->idle_sp == STACKLEVEL) |
720 | { |
698 | { |
721 | stack_put (prev__stack); |
699 | cctx_put (prev__cctx); |
722 | prev->stack = 0; |
700 | prev->cctx = 0; |
723 | } |
701 | } |
724 | |
702 | |
725 | if (!next->stack) |
703 | if (!next->cctx) |
726 | next->stack = stack_get (); |
704 | next->cctx = cctx_get (); |
727 | |
705 | |
728 | if (prev__stack != next->stack) |
706 | if (prev__cctx != next->cctx) |
729 | { |
707 | { |
730 | prev__stack->top_env = PL_top_env; |
708 | prev__cctx->top_env = PL_top_env; |
731 | PL_top_env = next->stack->top_env; |
709 | PL_top_env = next->cctx->top_env; |
732 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
710 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
733 | } |
711 | } |
734 | |
712 | |
735 | free_coro_mortal (); |
713 | free_coro_mortal (); |
736 | |
714 | |
737 | UNLOCK; |
715 | UNLOCK; |
738 | } |
716 | } |
739 | } |
717 | } |
740 | |
|
|
741 | /* use this function pointer to call the above function */ |
|
|
742 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
743 | /* not static to make it even harder for the compiler (and theoretically impossible in most cases */ |
|
|
744 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
745 | |
718 | |
746 | struct transfer_args |
719 | struct transfer_args |
747 | { |
720 | { |
748 | struct coro *prev, *next; |
721 | struct coro *prev, *next; |
749 | int flags; |
722 | int flags; |
750 | }; |
723 | }; |
751 | |
724 | |
752 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
725 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
753 | |
726 | |
754 | static void |
727 | static void |
755 | coro_state_destroy (struct coro *coro) |
728 | coro_state_destroy (struct coro *coro) |
756 | { |
729 | { |
757 | if (coro->refcnt--) |
730 | if (coro->refcnt--) |
… | |
… | |
759 | |
732 | |
760 | if (coro->mainstack && coro->mainstack != main_mainstack) |
733 | if (coro->mainstack && coro->mainstack != main_mainstack) |
761 | { |
734 | { |
762 | struct coro temp; |
735 | struct coro temp; |
763 | |
736 | |
764 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
737 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
765 | LOAD (aTHX_ coro); |
738 | LOAD (coro); |
766 | |
739 | |
767 | destroy_stacks (aTHX); |
740 | coro_destroy_stacks (); |
768 | |
741 | |
769 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
742 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
770 | |
743 | |
771 | coro->mainstack = 0; |
744 | coro->mainstack = 0; |
772 | } |
745 | } |
773 | |
746 | |
774 | stack_free (coro->stack); |
747 | cctx_free (coro->cctx); |
775 | SvREFCNT_dec (coro->args); |
748 | SvREFCNT_dec (coro->args); |
776 | Safefree (coro); |
749 | Safefree (coro); |
777 | } |
750 | } |
778 | |
751 | |
779 | static int |
752 | static int |
780 | coro_state_clear (SV *sv, MAGIC *mg) |
753 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
781 | { |
754 | { |
782 | struct coro *coro = (struct coro *)mg->mg_ptr; |
755 | struct coro *coro = (struct coro *)mg->mg_ptr; |
783 | mg->mg_ptr = 0; |
756 | mg->mg_ptr = 0; |
784 | |
757 | |
785 | coro_state_destroy (coro); |
758 | coro_state_destroy (coro); |
786 | |
759 | |
787 | return 0; |
760 | return 0; |
788 | } |
761 | } |
789 | |
762 | |
790 | static int |
763 | static int |
791 | coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) |
764 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
792 | { |
765 | { |
793 | struct coro *coro = (struct coro *)mg->mg_ptr; |
766 | struct coro *coro = (struct coro *)mg->mg_ptr; |
794 | |
767 | |
795 | ++coro->refcnt; |
768 | ++coro->refcnt; |
796 | |
769 | |
797 | return 0; |
770 | return 0; |
798 | } |
771 | } |
799 | |
772 | |
800 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
773 | static MGVTBL coro_state_vtbl = { |
|
|
774 | 0, 0, 0, 0, |
|
|
775 | coro_state_clear, |
|
|
776 | 0, |
|
|
777 | #ifdef MGf_DUP |
|
|
778 | coro_state_dup, |
|
|
779 | #else |
|
|
780 | # define MGf_DUP 0 |
|
|
781 | #endif |
|
|
782 | }; |
801 | |
783 | |
802 | static struct coro * |
784 | static struct coro * |
803 | SvSTATE (SV *coro) |
785 | SvSTATE (SV *coro) |
804 | { |
786 | { |
805 | HV *stash; |
787 | HV *stash; |
… | |
… | |
820 | assert (mg->mg_type == PERL_MAGIC_ext); |
802 | assert (mg->mg_type == PERL_MAGIC_ext); |
821 | return (struct coro *)mg->mg_ptr; |
803 | return (struct coro *)mg->mg_ptr; |
822 | } |
804 | } |
823 | |
805 | |
824 | static void |
806 | static void |
825 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) |
807 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
826 | { |
808 | { |
827 | ta->prev = SvSTATE (prev); |
809 | ta->prev = SvSTATE (prev); |
828 | ta->next = SvSTATE (next); |
810 | ta->next = SvSTATE (next); |
829 | ta->flags = flags; |
811 | ta->flags = flags; |
830 | } |
812 | } |
… | |
… | |
833 | api_transfer (SV *prev, SV *next, int flags) |
815 | api_transfer (SV *prev, SV *next, int flags) |
834 | { |
816 | { |
835 | dTHX; |
817 | dTHX; |
836 | struct transfer_args ta; |
818 | struct transfer_args ta; |
837 | |
819 | |
838 | prepare_transfer (aTHX_ &ta, prev, next, flags); |
820 | prepare_transfer (&ta, prev, next, flags); |
839 | TRANSFER (ta); |
821 | TRANSFER (ta); |
840 | } |
822 | } |
841 | |
823 | |
842 | /** Coro ********************************************************************/ |
824 | /** Coro ********************************************************************/ |
843 | |
825 | |
… | |
… | |
852 | static GV *coro_current, *coro_idle; |
834 | static GV *coro_current, *coro_idle; |
853 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
835 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
854 | static int coro_nready; |
836 | static int coro_nready; |
855 | |
837 | |
856 | static void |
838 | static void |
857 | coro_enq (pTHX_ SV *sv) |
839 | coro_enq (SV *sv) |
858 | { |
840 | { |
859 | int prio; |
841 | int prio; |
860 | |
842 | |
861 | if (SvTYPE (sv) != SVt_PVHV) |
843 | if (SvTYPE (sv) != SVt_PVHV) |
862 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
844 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
… | |
… | |
866 | av_push (coro_ready [prio - PRIO_MIN], sv); |
848 | av_push (coro_ready [prio - PRIO_MIN], sv); |
867 | coro_nready++; |
849 | coro_nready++; |
868 | } |
850 | } |
869 | |
851 | |
870 | static SV * |
852 | static SV * |
871 | coro_deq (pTHX_ int min_prio) |
853 | coro_deq (int min_prio) |
872 | { |
854 | { |
873 | int prio = PRIO_MAX - PRIO_MIN; |
855 | int prio = PRIO_MAX - PRIO_MIN; |
874 | |
856 | |
875 | min_prio -= PRIO_MIN; |
857 | min_prio -= PRIO_MIN; |
876 | if (min_prio < 0) |
858 | if (min_prio < 0) |
… | |
… | |
893 | |
875 | |
894 | if (SvROK (coro)) |
876 | if (SvROK (coro)) |
895 | coro = SvRV (coro); |
877 | coro = SvRV (coro); |
896 | |
878 | |
897 | LOCK; |
879 | LOCK; |
898 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
880 | coro_enq (SvREFCNT_inc (coro)); |
899 | UNLOCK; |
881 | UNLOCK; |
900 | } |
882 | } |
901 | |
883 | |
902 | static void |
884 | static void |
903 | prepare_schedule (aTHX_ struct transfer_args *ta) |
885 | prepare_schedule (struct transfer_args *ta) |
904 | { |
886 | { |
905 | SV *current, *prev, *next; |
887 | SV *current, *prev, *next; |
906 | |
|
|
907 | LOCK; |
|
|
908 | |
888 | |
909 | current = GvSV (coro_current); |
889 | current = GvSV (coro_current); |
910 | |
890 | |
911 | for (;;) |
891 | for (;;) |
912 | { |
892 | { |
913 | LOCK; |
893 | LOCK; |
914 | |
|
|
915 | next = coro_deq (aTHX_ PRIO_MIN); |
894 | next = coro_deq (PRIO_MIN); |
|
|
895 | UNLOCK; |
916 | |
896 | |
917 | if (next) |
897 | if (next) |
918 | break; |
898 | break; |
919 | |
|
|
920 | UNLOCK; |
|
|
921 | |
899 | |
922 | { |
900 | { |
923 | dSP; |
901 | dSP; |
924 | |
902 | |
925 | ENTER; |
903 | ENTER; |
… | |
… | |
936 | |
914 | |
937 | prev = SvRV (current); |
915 | prev = SvRV (current); |
938 | SvRV (current) = next; |
916 | SvRV (current) = next; |
939 | |
917 | |
940 | /* free this only after the transfer */ |
918 | /* free this only after the transfer */ |
|
|
919 | LOCK; |
941 | free_coro_mortal (); |
920 | free_coro_mortal (); |
|
|
921 | UNLOCK; |
942 | coro_mortal = prev; |
922 | coro_mortal = prev; |
943 | |
923 | |
944 | ta->prev = SvSTATE (prev); |
924 | ta->prev = SvSTATE (prev); |
945 | ta->next = SvSTATE (next); |
925 | ta->next = SvSTATE (next); |
946 | ta->flags = TRANSFER_SAVE_ALL; |
926 | ta->flags = TRANSFER_SAVE_ALL; |
947 | |
|
|
948 | UNLOCK; |
|
|
949 | } |
927 | } |
950 | |
928 | |
951 | static void |
929 | static void |
952 | prepare_cede (aTHX_ struct transfer_args *ta) |
930 | prepare_cede (struct transfer_args *ta) |
953 | { |
931 | { |
954 | LOCK; |
932 | LOCK; |
955 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
933 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
956 | UNLOCK; |
934 | UNLOCK; |
957 | |
935 | |
958 | prepare_schedule (ta); |
936 | prepare_schedule (ta); |
959 | } |
937 | } |
960 | |
938 | |
… | |
… | |
998 | main_mainstack = PL_mainstack; |
976 | main_mainstack = PL_mainstack; |
999 | |
977 | |
1000 | coroapi.ver = CORO_API_VERSION; |
978 | coroapi.ver = CORO_API_VERSION; |
1001 | coroapi.transfer = api_transfer; |
979 | coroapi.transfer = api_transfer; |
1002 | |
980 | |
1003 | Newz (0, main_stack, 1, coro_stack); |
|
|
1004 | main_stack->idle_sp = (void *)-1; |
|
|
1005 | |
|
|
1006 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
981 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1007 | } |
982 | } |
1008 | |
983 | |
1009 | SV * |
984 | SV * |
1010 | new (char *klass, ...) |
985 | new (char *klass, ...) |
… | |
… | |
1021 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
996 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1022 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
997 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1023 | |
998 | |
1024 | for (i = 1; i < items; i++) |
999 | for (i = 1; i < items; i++) |
1025 | av_push (coro->args, newSVsv (ST (i))); |
1000 | av_push (coro->args, newSVsv (ST (i))); |
1026 | |
|
|
1027 | coro->stack = main_stack; |
|
|
1028 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1029 | /*coro->stack = 0;*/ |
|
|
1030 | } |
1001 | } |
1031 | OUTPUT: |
1002 | OUTPUT: |
1032 | RETVAL |
1003 | RETVAL |
1033 | |
1004 | |
1034 | void |
1005 | void |
1035 | transfer (...) |
1006 | _set_stacklevel (...) |
1036 | ALIAS: |
1007 | ALIAS: |
1037 | Coro::schedule = 1 |
1008 | Coro::State::transfer = 1 |
1038 | Coro::cede = 2 |
1009 | Coro::schedule = 2 |
1039 | _set_stacklevel = 3 |
1010 | Coro::cede = 3 |
|
|
1011 | Coro::Cont::yield = 4 |
1040 | CODE: |
1012 | CODE: |
1041 | { |
1013 | { |
1042 | struct transfer_args ta; |
1014 | struct transfer_args ta; |
1043 | |
1015 | |
1044 | switch (ix) |
1016 | switch (ix) |
1045 | { |
1017 | { |
1046 | case 0: |
1018 | case 0: |
1047 | if (items != 3) |
|
|
1048 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
|
|
1049 | |
|
|
1050 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
|
|
1051 | break; |
|
|
1052 | |
|
|
1053 | case 1: |
|
|
1054 | prepare_schedule (&ta); |
|
|
1055 | break; |
|
|
1056 | |
|
|
1057 | case 2: |
|
|
1058 | prepare_cede (&ta); |
|
|
1059 | break; |
|
|
1060 | |
|
|
1061 | case 3: |
|
|
1062 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1019 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1063 | ta.next = 0; |
1020 | ta.next = 0; |
1064 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1021 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1065 | break; |
1022 | break; |
|
|
1023 | |
|
|
1024 | case 1: |
|
|
1025 | if (items != 3) |
|
|
1026 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
|
|
1027 | |
|
|
1028 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
|
|
1029 | break; |
|
|
1030 | |
|
|
1031 | case 2: |
|
|
1032 | prepare_schedule (&ta); |
|
|
1033 | break; |
|
|
1034 | |
|
|
1035 | case 3: |
|
|
1036 | prepare_cede (&ta); |
|
|
1037 | break; |
|
|
1038 | |
|
|
1039 | case 4: |
|
|
1040 | { |
|
|
1041 | SV *yieldstack; |
|
|
1042 | SV *sv; |
|
|
1043 | AV *defav = GvAV (PL_defgv); |
|
|
1044 | |
|
|
1045 | yieldstack = *hv_fetch ( |
|
|
1046 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1047 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1048 | 0 |
|
|
1049 | ); |
|
|
1050 | |
|
|
1051 | /* set up @_ -- ugly */ |
|
|
1052 | av_clear (defav); |
|
|
1053 | av_fill (defav, items - 1); |
|
|
1054 | while (items--) |
|
|
1055 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1056 | |
|
|
1057 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1058 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1059 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1060 | ta.flags = 0; |
|
|
1061 | SvREFCNT_dec (sv); |
|
|
1062 | } |
|
|
1063 | break; |
|
|
1064 | |
1066 | } |
1065 | } |
1067 | |
1066 | |
1068 | TRANSFER (ta); |
1067 | TRANSFER (ta); |
1069 | } |
1068 | } |
1070 | |
1069 | |
… | |
… | |
1077 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1076 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1078 | |
1077 | |
1079 | ++coro_src->refcnt; |
1078 | ++coro_src->refcnt; |
1080 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1079 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1081 | } |
1080 | } |
1082 | |
|
|
1083 | void |
|
|
1084 | _nonlocal_goto (IV nextop) |
|
|
1085 | CODE: |
|
|
1086 | /* uuh, somebody will kill me again for this */ |
|
|
1087 | PL_op->op_next = INT2PTR (OP *, nextop); |
|
|
1088 | |
1081 | |
1089 | void |
1082 | void |
1090 | _exit (code) |
1083 | _exit (code) |
1091 | int code |
1084 | int code |
1092 | PROTOTYPE: $ |
1085 | PROTOTYPE: $ |
1093 | CODE: |
1086 | CODE: |
1094 | _exit (code); |
1087 | _exit (code); |
1095 | |
1088 | |
1096 | MODULE = Coro::State PACKAGE = Coro::Cont |
1089 | int |
|
|
1090 | cctx_count () |
|
|
1091 | CODE: |
|
|
1092 | RETVAL = cctx_count; |
|
|
1093 | OUTPUT: |
|
|
1094 | RETVAL |
1097 | |
1095 | |
1098 | void |
1096 | int |
1099 | yield (...) |
1097 | cctx_idle () |
1100 | PROTOTYPE: @ |
1098 | CODE: |
1101 | CODE: |
1099 | RETVAL = cctx_idle; |
1102 | { |
1100 | OUTPUT: |
1103 | SV *yieldstack; |
1101 | RETVAL |
1104 | SV *sv; |
|
|
1105 | AV *defav = GvAV (PL_defgv); |
|
|
1106 | struct coro *prev, *next; |
|
|
1107 | |
|
|
1108 | yieldstack = *hv_fetch ( |
|
|
1109 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1110 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1111 | 0 |
|
|
1112 | ); |
|
|
1113 | |
|
|
1114 | /* set up @_ -- ugly */ |
|
|
1115 | av_clear (defav); |
|
|
1116 | av_fill (defav, items - 1); |
|
|
1117 | while (items--) |
|
|
1118 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1119 | |
|
|
1120 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1121 | prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1122 | next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1123 | SvREFCNT_dec (sv); |
|
|
1124 | |
|
|
1125 | coro_state_transfer (aTHX_ prev, next, 0); |
|
|
1126 | } |
|
|
1127 | |
1102 | |
1128 | MODULE = Coro::State PACKAGE = Coro |
1103 | MODULE = Coro::State PACKAGE = Coro |
1129 | |
1104 | |
1130 | BOOT: |
1105 | BOOT: |
1131 | { |
1106 | { |