1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #if 0 |
5 | #if 1 |
6 | # define CHK(x) (void *)0 |
6 | # define CHK(x) (void *)0 |
7 | #else |
7 | #else |
8 | # define CHK(x) if (!(x)) croak("FATAL, CHK: " #x) |
8 | # define CHK(x) if (!(x)) croak("FATAL, CHK: " #x) |
9 | #endif |
9 | #endif |
10 | |
10 | |
|
|
11 | #define MAY_FLUSH /* increases codesize */ |
|
|
12 | |
|
|
13 | #define SUB_INIT "Coro::State::_newcoro" |
|
|
14 | |
|
|
15 | #define SAVE_DEFAV 0x00000001 |
|
|
16 | #define SAVE_DEFSV 0x00000002 |
|
|
17 | #define SAVE_ERRSV 0x00000004 |
|
|
18 | |
|
|
19 | #define SAVE_ALL -1 |
|
|
20 | |
11 | struct coro { |
21 | struct coro { |
|
|
22 | /* optionally saved, might be zero */ |
|
|
23 | AV *defav; |
|
|
24 | SV *defsv; |
|
|
25 | SV *errsv; |
|
|
26 | |
|
|
27 | /* saved global state not related to stacks */ |
12 | U8 dowarn; |
28 | U8 dowarn; |
13 | AV *defav; |
29 | |
14 | |
30 | /* the stacks and related info (callchain etc..) */ |
15 | PERL_SI *curstackinfo; |
31 | PERL_SI *curstackinfo; |
16 | AV *curstack; |
32 | AV *curstack; |
17 | AV *mainstack; |
33 | AV *mainstack; |
18 | SV **stack_sp; |
34 | SV **stack_sp; |
19 | OP *op; |
35 | OP *op; |
… | |
… | |
36 | OP **retstack; |
52 | OP **retstack; |
37 | I32 retstack_ix; |
53 | I32 retstack_ix; |
38 | I32 retstack_max; |
54 | I32 retstack_max; |
39 | COP *curcop; |
55 | COP *curcop; |
40 | |
56 | |
|
|
57 | /* data associated with this coroutine (initial args) */ |
41 | AV *args; |
58 | AV *args; |
42 | }; |
59 | }; |
43 | |
60 | |
44 | typedef struct coro *Coro__State; |
61 | typedef struct coro *Coro__State; |
45 | typedef struct coro *Coro__State_or_hashref; |
62 | typedef struct coro *Coro__State_or_hashref; |
… | |
… | |
115 | SvPADTMP_on (sv); |
132 | SvPADTMP_on (sv); |
116 | npad[ix] = sv; |
133 | npad[ix] = sv; |
117 | } |
134 | } |
118 | } |
135 | } |
119 | |
136 | |
120 | #if 0 /* NONOTUNDERSTOOD */ |
137 | #if 0 /* return -ENOTUNDERSTOOD */ |
121 | /* Now that vars are all in place, clone nested closures. */ |
138 | /* Now that vars are all in place, clone nested closures. */ |
122 | |
139 | |
123 | for (ix = fpad; ix > 0; ix--) { |
140 | for (ix = fpad; ix > 0; ix--) { |
124 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; |
141 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; |
125 | if (namesv |
142 | if (namesv |
… | |
… | |
138 | #endif |
155 | #endif |
139 | |
156 | |
140 | return newpadlist; |
157 | return newpadlist; |
141 | } |
158 | } |
142 | |
159 | |
|
|
160 | #ifdef MAY_FLUSH |
143 | STATIC AV * |
161 | STATIC AV * |
144 | free_padlist (AV *padlist) |
162 | free_padlist (AV *padlist) |
145 | { |
163 | { |
146 | /* may be during global destruction */ |
164 | /* may be during global destruction */ |
147 | if (SvREFCNT(padlist)) |
165 | if (SvREFCNT(padlist)) |
… | |
… | |
156 | } |
174 | } |
157 | |
175 | |
158 | SvREFCNT_dec((SV*)padlist); |
176 | SvREFCNT_dec((SV*)padlist); |
159 | } |
177 | } |
160 | } |
178 | } |
|
|
179 | #endif |
161 | |
180 | |
162 | /* the next tow functions merely cache the padlists */ |
181 | /* the next two functions merely cache the padlists */ |
163 | STATIC void |
182 | STATIC void |
164 | get_padlist (CV *cv) |
183 | get_padlist (CV *cv) |
165 | { |
184 | { |
166 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); |
185 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); |
167 | |
186 | |
… | |
… | |
183 | } |
202 | } |
184 | |
203 | |
185 | av_push ((AV *)*he, (SV *)CvPADLIST (cv)); |
204 | av_push ((AV *)*he, (SV *)CvPADLIST (cv)); |
186 | } |
205 | } |
187 | |
206 | |
|
|
207 | #ifdef MAY_FLUSH |
|
|
208 | STATIC void |
|
|
209 | flush_padlist_cache () |
|
|
210 | { |
|
|
211 | HV *hv = padlist_cache; |
|
|
212 | padlist_cache = newHV (); |
|
|
213 | |
|
|
214 | if (hv_iterinit (hv)) |
|
|
215 | { |
|
|
216 | HE *he; |
|
|
217 | AV *padlist; |
|
|
218 | |
|
|
219 | while (!!(he = hv_iternext (hv))) |
|
|
220 | { |
|
|
221 | AV *av = (AV *)HeVAL(he); |
|
|
222 | |
|
|
223 | /* casting is fun. */ |
|
|
224 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
225 | free_padlist (padlist); |
|
|
226 | } |
|
|
227 | } |
|
|
228 | |
|
|
229 | SvREFCNT_dec (hv); |
|
|
230 | } |
|
|
231 | #endif |
|
|
232 | |
|
|
233 | #define SB do { |
|
|
234 | #define SE } while (0) |
|
|
235 | |
|
|
236 | #define LOAD(state) SB load_state(aTHX_ state); SPAGAIN; SE |
|
|
237 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ state,flags); SE |
|
|
238 | |
|
|
239 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
|
|
240 | |
188 | static void |
241 | static void |
189 | SAVE(pTHX_ Coro__State c) |
242 | load_state(pTHX_ Coro__State c) |
|
|
243 | { |
|
|
244 | PL_dowarn = c->dowarn; |
|
|
245 | |
|
|
246 | PL_curstackinfo = c->curstackinfo; |
|
|
247 | PL_curstack = c->curstack; |
|
|
248 | PL_mainstack = c->mainstack; |
|
|
249 | PL_stack_sp = c->stack_sp; |
|
|
250 | PL_op = c->op; |
|
|
251 | PL_curpad = c->curpad; |
|
|
252 | PL_stack_base = c->stack_base; |
|
|
253 | PL_stack_max = c->stack_max; |
|
|
254 | PL_tmps_stack = c->tmps_stack; |
|
|
255 | PL_tmps_floor = c->tmps_floor; |
|
|
256 | PL_tmps_ix = c->tmps_ix; |
|
|
257 | PL_tmps_max = c->tmps_max; |
|
|
258 | PL_markstack = c->markstack; |
|
|
259 | PL_markstack_ptr = c->markstack_ptr; |
|
|
260 | PL_markstack_max = c->markstack_max; |
|
|
261 | PL_scopestack = c->scopestack; |
|
|
262 | PL_scopestack_ix = c->scopestack_ix; |
|
|
263 | PL_scopestack_max = c->scopestack_max; |
|
|
264 | PL_savestack = c->savestack; |
|
|
265 | PL_savestack_ix = c->savestack_ix; |
|
|
266 | PL_savestack_max = c->savestack_max; |
|
|
267 | PL_retstack = c->retstack; |
|
|
268 | PL_retstack_ix = c->retstack_ix; |
|
|
269 | PL_retstack_max = c->retstack_max; |
|
|
270 | PL_curcop = c->curcop; |
|
|
271 | |
|
|
272 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
|
|
273 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
|
|
274 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
275 | |
|
|
276 | { |
|
|
277 | dSP; |
|
|
278 | CV *cv; |
|
|
279 | |
|
|
280 | /* now do the ugly restore mess */ |
|
|
281 | while ((cv = (CV *)POPs)) |
|
|
282 | { |
|
|
283 | AV *padlist = (AV *)POPs; |
|
|
284 | |
|
|
285 | if (padlist) |
|
|
286 | { |
|
|
287 | put_padlist (cv); /* mark this padlist as available */ |
|
|
288 | CvPADLIST(cv) = padlist; |
|
|
289 | #ifdef USE_THREADS |
|
|
290 | /*CvOWNER(cv) = (struct perl_thread *)POPs;*/ |
|
|
291 | #endif |
|
|
292 | } |
|
|
293 | |
|
|
294 | ++CvDEPTH(cv); |
|
|
295 | } |
|
|
296 | |
|
|
297 | PUTBACK; |
|
|
298 | } |
|
|
299 | } |
|
|
300 | |
|
|
301 | static void |
|
|
302 | save_state(pTHX_ Coro__State c, int flags) |
190 | { |
303 | { |
191 | { |
304 | { |
192 | dSP; |
305 | dSP; |
193 | I32 cxix = cxstack_ix; |
306 | I32 cxix = cxstack_ix; |
194 | PERL_SI *top_si = PL_curstackinfo; |
307 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
201 | |
314 | |
202 | PUSHs (Nullsv); |
315 | PUSHs (Nullsv); |
203 | /* this loop was inspired by pp_caller */ |
316 | /* this loop was inspired by pp_caller */ |
204 | for (;;) |
317 | for (;;) |
205 | { |
318 | { |
206 | while (cxix >= 0) |
319 | do |
207 | { |
320 | { |
208 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
321 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
209 | |
322 | |
210 | if (CxTYPE(cx) == CXt_SUB) |
323 | if (CxTYPE(cx) == CXt_SUB) |
211 | { |
324 | { |
212 | CV *cv = cx->blk_sub.cv; |
325 | CV *cv = cx->blk_sub.cv; |
213 | if (CvDEPTH(cv)) |
326 | if (CvDEPTH(cv)) |
214 | { |
327 | { |
215 | #ifdef USE_THREADS |
328 | #ifdef USE_THREADS |
216 | XPUSHs ((SV *)CvOWNER(cv)); |
329 | /*XPUSHs ((SV *)CvOWNER(cv));*/ |
|
|
330 | /*CvOWNER(cv) = 0;*/ |
|
|
331 | /*error must unlock this cv etc.. etc...*/ |
217 | #endif |
332 | #endif |
218 | EXTEND (SP, 3); |
333 | EXTEND (SP, CvDEPTH(cv)*2); |
|
|
334 | |
|
|
335 | while (--CvDEPTH(cv)) |
|
|
336 | { |
|
|
337 | /* this tells the restore code to increment CvDEPTH */ |
|
|
338 | PUSHs (Nullsv); |
219 | PUSHs ((SV *)CvDEPTH(cv)); |
339 | PUSHs ((SV *)cv); |
|
|
340 | } |
|
|
341 | |
220 | PUSHs ((SV *)CvPADLIST(cv)); |
342 | PUSHs ((SV *)CvPADLIST(cv)); |
221 | PUSHs ((SV *)cv); |
343 | PUSHs ((SV *)cv); |
222 | |
344 | |
223 | get_padlist (cv); |
345 | get_padlist (cv); /* this is a monster */ |
224 | |
|
|
225 | CvDEPTH(cv) = 0; |
|
|
226 | #ifdef USE_THREADS |
|
|
227 | CvOWNER(cv) = 0; |
|
|
228 | error must unlock this cv etc.. etc... |
|
|
229 | if you are here wondering about this error message then |
|
|
230 | the reason is that it will not work as advertised yet |
|
|
231 | #endif |
|
|
232 | } |
346 | } |
233 | } |
347 | } |
234 | else if (CxTYPE(cx) == CXt_FORMAT) |
348 | else if (CxTYPE(cx) == CXt_FORMAT) |
235 | { |
349 | { |
236 | /* I never used formats, so how should I know how these are implemented? */ |
350 | /* I never used formats, so how should I know how these are implemented? */ |
237 | /* my bold guess is as a simple, plain sub... */ |
351 | /* my bold guess is as a simple, plain sub... */ |
238 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
352 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
239 | } |
353 | } |
240 | } |
354 | } |
|
|
355 | while (cxix >= 0); |
241 | |
356 | |
242 | if (top_si->si_type == PERLSI_MAIN) |
357 | if (top_si->si_type == PERLSI_MAIN) |
243 | break; |
358 | break; |
244 | |
359 | |
245 | top_si = top_si->si_prev; |
360 | top_si = top_si->si_prev; |
… | |
… | |
248 | } |
363 | } |
249 | |
364 | |
250 | PUTBACK; |
365 | PUTBACK; |
251 | } |
366 | } |
252 | |
367 | |
|
|
368 | c->defav = flags & SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
|
|
369 | c->defsv = flags & SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
|
|
370 | c->errsv = flags & SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
371 | |
253 | c->dowarn = PL_dowarn; |
372 | c->dowarn = PL_dowarn; |
254 | c->defav = GvAV (PL_defgv); |
373 | |
255 | c->curstackinfo = PL_curstackinfo; |
374 | c->curstackinfo = PL_curstackinfo; |
256 | c->curstack = PL_curstack; |
375 | c->curstack = PL_curstack; |
257 | c->mainstack = PL_mainstack; |
376 | c->mainstack = PL_mainstack; |
258 | c->stack_sp = PL_stack_sp; |
377 | c->stack_sp = PL_stack_sp; |
259 | c->op = PL_op; |
378 | c->op = PL_op; |
… | |
… | |
277 | c->retstack_ix = PL_retstack_ix; |
396 | c->retstack_ix = PL_retstack_ix; |
278 | c->retstack_max = PL_retstack_max; |
397 | c->retstack_max = PL_retstack_max; |
279 | c->curcop = PL_curcop; |
398 | c->curcop = PL_curcop; |
280 | } |
399 | } |
281 | |
400 | |
282 | static void |
401 | /* |
283 | LOAD(pTHX_ Coro__State c) |
402 | * destroy the stacks, the callchain etc... |
284 | { |
403 | * still there is a memleak of 128 bytes... |
285 | PL_dowarn = c->dowarn; |
404 | */ |
286 | GvAV (PL_defgv) = c->defav; |
|
|
287 | PL_curstackinfo = c->curstackinfo; |
|
|
288 | PL_curstack = c->curstack; |
|
|
289 | PL_mainstack = c->mainstack; |
|
|
290 | PL_stack_sp = c->stack_sp; |
|
|
291 | PL_op = c->op; |
|
|
292 | PL_curpad = c->curpad; |
|
|
293 | PL_stack_base = c->stack_base; |
|
|
294 | PL_stack_max = c->stack_max; |
|
|
295 | PL_tmps_stack = c->tmps_stack; |
|
|
296 | PL_tmps_floor = c->tmps_floor; |
|
|
297 | PL_tmps_ix = c->tmps_ix; |
|
|
298 | PL_tmps_max = c->tmps_max; |
|
|
299 | PL_markstack = c->markstack; |
|
|
300 | PL_markstack_ptr = c->markstack_ptr; |
|
|
301 | PL_markstack_max = c->markstack_max; |
|
|
302 | PL_scopestack = c->scopestack; |
|
|
303 | PL_scopestack_ix = c->scopestack_ix; |
|
|
304 | PL_scopestack_max = c->scopestack_max; |
|
|
305 | PL_savestack = c->savestack; |
|
|
306 | PL_savestack_ix = c->savestack_ix; |
|
|
307 | PL_savestack_max = c->savestack_max; |
|
|
308 | PL_retstack = c->retstack; |
|
|
309 | PL_retstack_ix = c->retstack_ix; |
|
|
310 | PL_retstack_max = c->retstack_max; |
|
|
311 | PL_curcop = c->curcop; |
|
|
312 | |
|
|
313 | { |
|
|
314 | dSP; |
|
|
315 | CV *cv; |
|
|
316 | |
|
|
317 | /* now do the ugly restore mess */ |
|
|
318 | while ((cv = (CV *)POPs)) |
|
|
319 | { |
|
|
320 | AV *padlist = (AV *)POPs; |
|
|
321 | |
|
|
322 | put_padlist (cv); |
|
|
323 | CvPADLIST(cv) = padlist; |
|
|
324 | CvDEPTH(cv) = (I32)POPs; |
|
|
325 | |
|
|
326 | #ifdef USE_THREADS |
|
|
327 | CvOWNER(cv) = (struct perl_thread *)POPs; |
|
|
328 | error does not work either |
|
|
329 | #endif |
|
|
330 | } |
|
|
331 | |
|
|
332 | PUTBACK; |
|
|
333 | } |
|
|
334 | } |
|
|
335 | |
|
|
336 | /* this is an EXACT copy of S_nuke_stacks in perl.c, which is unfortunately static */ |
|
|
337 | STATIC void |
405 | STATIC void |
338 | destroy_stacks(pTHX) |
406 | destroy_stacks(pTHX) |
339 | { |
407 | { |
340 | dSP; |
|
|
341 | |
|
|
342 | /* die does this while calling POPSTACK, but I just don't see why. */ |
|
|
343 | dounwind(-1); |
|
|
344 | |
|
|
345 | /* is this ugly, I ask? */ |
408 | /* is this ugly, I ask? */ |
346 | while (PL_scopestack_ix) |
409 | while (PL_scopestack_ix) |
347 | LEAVE; |
410 | LEAVE; |
348 | |
411 | |
|
|
412 | /* sure it is, but more important: is it correct?? :/ */ |
|
|
413 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
|
|
414 | FREETMPS; |
|
|
415 | |
349 | while (PL_curstackinfo->si_next) |
416 | while (PL_curstackinfo->si_next) |
350 | PL_curstackinfo = PL_curstackinfo->si_next; |
417 | PL_curstackinfo = PL_curstackinfo->si_next; |
351 | |
418 | |
352 | while (PL_curstackinfo) |
419 | while (PL_curstackinfo) |
353 | { |
420 | { |
354 | PERL_SI *p = PL_curstackinfo->si_prev; |
421 | PERL_SI *p = PL_curstackinfo->si_prev; |
|
|
422 | |
|
|
423 | { |
|
|
424 | dSP; |
|
|
425 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
|
|
426 | PUTBACK; /* possibly superfluous */ |
|
|
427 | } |
|
|
428 | |
|
|
429 | dounwind(-1); |
355 | |
430 | |
356 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
431 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
357 | Safefree(PL_curstackinfo->si_cxstack); |
432 | Safefree(PL_curstackinfo->si_cxstack); |
358 | Safefree(PL_curstackinfo); |
433 | Safefree(PL_curstackinfo); |
359 | PL_curstackinfo = p; |
434 | PL_curstackinfo = p; |
360 | } |
435 | } |
361 | |
436 | |
362 | if (PL_scopestack_ix != 0) |
|
|
363 | Perl_warner(aTHX_ WARN_INTERNAL, |
|
|
364 | "Unbalanced scopes: %ld more ENTERs than LEAVEs\n", |
|
|
365 | (long)PL_scopestack_ix); |
|
|
366 | if (PL_savestack_ix != 0) |
|
|
367 | Perl_warner(aTHX_ WARN_INTERNAL, |
|
|
368 | "Unbalanced saves: %ld more saves than restores\n", |
|
|
369 | (long)PL_savestack_ix); |
|
|
370 | if (PL_tmps_floor != -1) |
|
|
371 | Perl_warner(aTHX_ WARN_INTERNAL,"Unbalanced tmps: %ld more allocs than frees\n", |
|
|
372 | (long)PL_tmps_floor + 1); |
|
|
373 | /* |
|
|
374 | */ |
|
|
375 | Safefree(PL_tmps_stack); |
437 | Safefree(PL_tmps_stack); |
376 | Safefree(PL_markstack); |
438 | Safefree(PL_markstack); |
377 | Safefree(PL_scopestack); |
439 | Safefree(PL_scopestack); |
378 | Safefree(PL_savestack); |
440 | Safefree(PL_savestack); |
379 | Safefree(PL_retstack); |
441 | Safefree(PL_retstack); |
380 | } |
442 | } |
381 | |
443 | |
382 | #define SUB_INIT "Coro::State::_newcoro" |
444 | STATIC void |
|
|
445 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
|
|
446 | { |
|
|
447 | dSP; |
|
|
448 | |
|
|
449 | if (prev != next) |
|
|
450 | { |
|
|
451 | /* |
|
|
452 | * this could be done in newprocess which would lead to |
|
|
453 | * extremely elegant and fast (just SAVE/LOAD) |
|
|
454 | * code here, but lazy allocation of stacks has also |
|
|
455 | * some virtues and the overhead of the if() is nil. |
|
|
456 | */ |
|
|
457 | if (next->mainstack) |
|
|
458 | { |
|
|
459 | SAVE (prev, flags); |
|
|
460 | LOAD (next); |
|
|
461 | /* mark this state as in-use */ |
|
|
462 | next->mainstack = 0; |
|
|
463 | next->tmps_ix = -2; |
|
|
464 | } |
|
|
465 | else if (next->tmps_ix == -2) |
|
|
466 | { |
|
|
467 | croak ("tried to transfer to running coroutine"); |
|
|
468 | } |
|
|
469 | else |
|
|
470 | { |
|
|
471 | /* |
|
|
472 | * emulate part of the perl startup here. |
|
|
473 | */ |
|
|
474 | UNOP myop; |
|
|
475 | |
|
|
476 | SAVE (prev, -1); /* first get rid of the old state */ |
|
|
477 | |
|
|
478 | init_stacks (); /* from perl.c */ |
|
|
479 | SPAGAIN; |
|
|
480 | |
|
|
481 | PL_op = (OP *)&myop; |
|
|
482 | /*PL_curcop = 0;*/ |
|
|
483 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
484 | GvAV (PL_defgv) = next->args; |
|
|
485 | |
|
|
486 | Zero(&myop, 1, UNOP); |
|
|
487 | myop.op_next = Nullop; |
|
|
488 | myop.op_flags = OPf_WANT_VOID; |
|
|
489 | |
|
|
490 | PUSHMARK(SP); |
|
|
491 | XPUSHs ((SV*)get_cv(SUB_INIT, TRUE)); |
|
|
492 | /* |
|
|
493 | * the next line is slightly wrong, as PL_op->op_next |
|
|
494 | * is actually being executed so we skip the first op. |
|
|
495 | * that doesn't matter, though, since it is only |
|
|
496 | * pp_nextstate and we never return... |
|
|
497 | * ah yes, and I don't care anyways ;) |
|
|
498 | */ |
|
|
499 | PUTBACK; |
|
|
500 | PL_op = pp_entersub(aTHX); |
|
|
501 | SPAGAIN; |
|
|
502 | |
|
|
503 | ENTER; /* necessary e.g. for dounwind */ |
|
|
504 | } |
|
|
505 | } |
|
|
506 | } |
383 | |
507 | |
384 | MODULE = Coro::State PACKAGE = Coro::State |
508 | MODULE = Coro::State PACKAGE = Coro::State |
385 | |
509 | |
386 | PROTOTYPES: ENABLE |
510 | PROTOTYPES: ENABLE |
387 | |
511 | |
388 | BOOT: |
512 | BOOT: |
|
|
513 | HV * stash = gv_stashpvn("Coro::State", 10, TRUE); |
|
|
514 | |
|
|
515 | newCONSTSUB (stash, "SAVE_DEFAV", newSViv (SAVE_DEFAV)); |
|
|
516 | newCONSTSUB (stash, "SAVE_DEFSV", newSViv (SAVE_DEFSV)); |
|
|
517 | newCONSTSUB (stash, "SAVE_ERRSV", newSViv (SAVE_ERRSV)); |
|
|
518 | |
389 | if (!padlist_cache) |
519 | if (!padlist_cache) |
390 | padlist_cache = newHV (); |
520 | padlist_cache = newHV (); |
391 | |
521 | |
392 | Coro::State |
522 | Coro::State |
393 | _newprocess(args) |
523 | _newprocess(args) |
… | |
… | |
395 | PROTOTYPE: $ |
525 | PROTOTYPE: $ |
396 | CODE: |
526 | CODE: |
397 | Coro__State coro; |
527 | Coro__State coro; |
398 | |
528 | |
399 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
529 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
400 | croak ("Coro::State::newprocess expects an arrayref"); |
530 | croak ("Coro::State::_newprocess expects an arrayref"); |
401 | |
531 | |
402 | New (0, coro, 1, struct coro); |
532 | New (0, coro, 1, struct coro); |
403 | |
533 | |
404 | coro->mainstack = 0; /* actual work is done inside transfer */ |
534 | coro->mainstack = 0; /* actual work is done inside transfer */ |
405 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
535 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
… | |
… | |
407 | RETVAL = coro; |
537 | RETVAL = coro; |
408 | OUTPUT: |
538 | OUTPUT: |
409 | RETVAL |
539 | RETVAL |
410 | |
540 | |
411 | void |
541 | void |
412 | transfer(prev,next) |
542 | transfer(prev, next, flags = SAVE_DEFAV) |
413 | Coro::State_or_hashref prev |
543 | Coro::State_or_hashref prev |
414 | Coro::State_or_hashref next |
544 | Coro::State_or_hashref next |
|
|
545 | int flags |
|
|
546 | PROTOTYPE: @ |
415 | CODE: |
547 | CODE: |
416 | |
548 | |
417 | if (prev != next) |
549 | transfer (aTHX_ prev, next, flags); |
418 | { |
|
|
419 | PUTBACK; |
|
|
420 | SAVE (aTHX_ prev); |
|
|
421 | |
|
|
422 | /* |
|
|
423 | * this could be done in newprocess which would lead to |
|
|
424 | * extremely elegant and fast (just PUTBACK/SAVE/LOAD/SPAGAIN) |
|
|
425 | * code here, but lazy allocation of stacks has also |
|
|
426 | * some virtues and the overhead of the if() is nil. |
|
|
427 | */ |
|
|
428 | if (next->mainstack) |
|
|
429 | { |
|
|
430 | LOAD (aTHX_ next); |
|
|
431 | next->mainstack = 0; /* unnecessary but much cleaner */ |
|
|
432 | SPAGAIN; |
|
|
433 | } |
|
|
434 | else |
|
|
435 | { |
|
|
436 | /* |
|
|
437 | * emulate part of the perl startup here. |
|
|
438 | */ |
|
|
439 | UNOP myop; |
|
|
440 | |
|
|
441 | init_stacks (); /* from perl.c */ |
|
|
442 | PL_op = (OP *)&myop; |
|
|
443 | /*PL_curcop = 0;*/ |
|
|
444 | GvAV (PL_defgv) = (SV *)SvREFCNT_inc (next->args); |
|
|
445 | |
|
|
446 | SPAGAIN; |
|
|
447 | Zero(&myop, 1, UNOP); |
|
|
448 | myop.op_next = Nullop; |
|
|
449 | myop.op_flags = OPf_WANT_VOID; |
|
|
450 | |
|
|
451 | PUSHMARK(SP); |
|
|
452 | XPUSHs ((SV*)get_cv(SUB_INIT, TRUE)); |
|
|
453 | PUTBACK; |
|
|
454 | /* |
|
|
455 | * the next line is slightly wrong, as PL_op->op_next |
|
|
456 | * is actually being executed so we skip the first op. |
|
|
457 | * that doesn't matter, though, since it is only |
|
|
458 | * pp_nextstate and we never return... |
|
|
459 | */ |
|
|
460 | PL_op = Perl_pp_entersub(aTHX); |
|
|
461 | SPAGAIN; |
|
|
462 | |
|
|
463 | ENTER; |
|
|
464 | } |
|
|
465 | } |
|
|
466 | |
550 | |
467 | void |
551 | void |
468 | DESTROY(coro) |
552 | DESTROY(coro) |
469 | Coro::State coro |
553 | Coro::State coro |
470 | CODE: |
554 | CODE: |
471 | |
555 | |
472 | if (coro->mainstack) |
556 | if (coro->mainstack) |
473 | { |
557 | { |
474 | struct coro temp; |
558 | struct coro temp; |
475 | |
559 | |
476 | PUTBACK; |
|
|
477 | SAVE(aTHX_ (&temp)); |
560 | SAVE(aTHX_ (&temp), SAVE_ALL); |
478 | LOAD(aTHX_ coro); |
561 | LOAD(aTHX_ coro); |
479 | |
562 | |
480 | destroy_stacks (); |
563 | destroy_stacks (); |
481 | SvREFCNT_dec ((SV *)GvAV (PL_defgv)); |
|
|
482 | |
564 | |
483 | LOAD((&temp)); |
565 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
484 | SPAGAIN; |
|
|
485 | } |
566 | } |
486 | |
567 | |
487 | SvREFCNT_dec (coro->args); |
|
|
488 | Safefree (coro); |
568 | Safefree (coro); |
489 | |
569 | |
|
|
570 | void |
|
|
571 | flush() |
|
|
572 | CODE: |
|
|
573 | #ifdef MAY_FLUSH |
|
|
574 | flush_padlist_cache (); |
|
|
575 | #endif |
490 | |
576 | |
|
|
577 | MODULE = Coro::State PACKAGE = Coro::Cont |
|
|
578 | |
|
|
579 | # this is dirty and should be in it's own .xs |
|
|
580 | |
|
|
581 | void |
|
|
582 | result(...) |
|
|
583 | PROTOTYPE: @ |
|
|
584 | CODE: |
|
|
585 | static SV *returnstk; |
|
|
586 | SV *sv; |
|
|
587 | AV *defav = GvAV (PL_defgv); |
|
|
588 | struct coro *prev, *next; |
|
|
589 | |
|
|
590 | if (!returnstk) |
|
|
591 | returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); |
|
|
592 | |
|
|
593 | /* set up @_ */ |
|
|
594 | av_clear (defav); |
|
|
595 | av_fill (defav, items - 1); |
|
|
596 | while (items--) |
|
|
597 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
598 | |
|
|
599 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
|
|
600 | sv = av_pop ((AV *)SvRV (returnstk)); |
|
|
601 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
|
|
602 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
|
|
603 | SvREFCNT_dec (sv); |
|
|
604 | transfer(prev, next, 0); |
|
|
605 | |