1 |
#include "EXTERN.h" |
2 |
#include "perl.h" |
3 |
#include "XSUB.h" |
4 |
|
5 |
#include "libcoro/coro.c" |
6 |
|
7 |
#ifdef HAVE_MMAP |
8 |
# include <unistd.h> |
9 |
# include <sys/mman.h> |
10 |
#endif |
11 |
|
12 |
#define MAY_FLUSH /* increases codesize */ |
13 |
|
14 |
#define SUB_INIT "Coro::State::initialize" |
15 |
|
16 |
#define TRANSFER_SAVE_DEFAV 0x00000001 |
17 |
#define TRANSFER_SAVE_DEFSV 0x00000002 |
18 |
#define TRANSFER_SAVE_ERRSV 0x00000004 |
19 |
#define TRANSFER_SAVE_CCTXT 0x00000008 |
20 |
|
21 |
#define TRANSFER_SAVE_ALL -1 |
22 |
|
23 |
struct coro { |
24 |
/* the optional C context */ |
25 |
coro_context cctx; |
26 |
void *sptr; |
27 |
long ssize; |
28 |
|
29 |
/* optionally saved, might be zero */ |
30 |
AV *defav; |
31 |
SV *defsv; |
32 |
SV *errsv; |
33 |
|
34 |
/* saved global state not related to stacks */ |
35 |
U8 dowarn; |
36 |
|
37 |
/* the stacks and related info (callchain etc..) */ |
38 |
PERL_SI *curstackinfo; |
39 |
AV *curstack; |
40 |
AV *mainstack; |
41 |
SV **stack_sp; |
42 |
OP *op; |
43 |
SV **curpad; |
44 |
SV **stack_base; |
45 |
SV **stack_max; |
46 |
SV **tmps_stack; |
47 |
I32 tmps_floor; |
48 |
I32 tmps_ix; |
49 |
I32 tmps_max; |
50 |
I32 *markstack; |
51 |
I32 *markstack_ptr; |
52 |
I32 *markstack_max; |
53 |
I32 *scopestack; |
54 |
I32 scopestack_ix; |
55 |
I32 scopestack_max; |
56 |
ANY *savestack; |
57 |
I32 savestack_ix; |
58 |
I32 savestack_max; |
59 |
OP **retstack; |
60 |
I32 retstack_ix; |
61 |
I32 retstack_max; |
62 |
COP *curcop; |
63 |
JMPENV start_env; |
64 |
JMPENV *top_env; |
65 |
|
66 |
/* data associated with this coroutine (initial args) */ |
67 |
AV *args; |
68 |
}; |
69 |
|
70 |
typedef struct coro *Coro__State; |
71 |
typedef struct coro *Coro__State_or_hashref; |
72 |
|
73 |
static AV *main_mainstack; /* used to differentiate between $main and others */ |
74 |
static HV *coro_state_stash; |
75 |
static HV *padlist_cache; |
76 |
|
77 |
/* mostly copied from op.c:cv_clone2 */ |
78 |
STATIC AV * |
79 |
clone_padlist (AV *protopadlist) |
80 |
{ |
81 |
AV *av; |
82 |
I32 ix; |
83 |
AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
84 |
AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
85 |
SV **pname = AvARRAY (protopad_name); |
86 |
SV **ppad = AvARRAY (protopad); |
87 |
I32 fname = AvFILLp (protopad_name); |
88 |
I32 fpad = AvFILLp (protopad); |
89 |
AV *newpadlist, *newpad_name, *newpad; |
90 |
SV **npad; |
91 |
|
92 |
newpad_name = newAV (); |
93 |
for (ix = fname; ix >= 0; ix--) |
94 |
av_store (newpad_name, ix, SvREFCNT_inc (pname[ix])); |
95 |
|
96 |
newpad = newAV (); |
97 |
av_fill (newpad, AvFILLp (protopad)); |
98 |
npad = AvARRAY (newpad); |
99 |
|
100 |
newpadlist = newAV (); |
101 |
AvREAL_off (newpadlist); |
102 |
av_store (newpadlist, 0, (SV *) newpad_name); |
103 |
av_store (newpadlist, 1, (SV *) newpad); |
104 |
|
105 |
av = newAV (); /* will be @_ */ |
106 |
av_extend (av, 0); |
107 |
av_store (newpad, 0, (SV *) av); |
108 |
AvFLAGS (av) = AVf_REIFY; |
109 |
|
110 |
for (ix = fpad; ix > 0; ix--) |
111 |
{ |
112 |
SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
113 |
if (namesv && namesv != &PL_sv_undef) |
114 |
{ |
115 |
char *name = SvPVX (namesv); /* XXX */ |
116 |
if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
117 |
{ /* lexical from outside? */ |
118 |
npad[ix] = SvREFCNT_inc (ppad[ix]); |
119 |
} |
120 |
else |
121 |
{ /* our own lexical */ |
122 |
SV *sv; |
123 |
if (*name == '&') |
124 |
sv = SvREFCNT_inc (ppad[ix]); |
125 |
else if (*name == '@') |
126 |
sv = (SV *) newAV (); |
127 |
else if (*name == '%') |
128 |
sv = (SV *) newHV (); |
129 |
else |
130 |
sv = NEWSV (0, 0); |
131 |
if (!SvPADBUSY (sv)) |
132 |
SvPADMY_on (sv); |
133 |
npad[ix] = sv; |
134 |
} |
135 |
} |
136 |
else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
137 |
{ |
138 |
npad[ix] = SvREFCNT_inc (ppad[ix]); |
139 |
} |
140 |
else |
141 |
{ |
142 |
SV *sv = NEWSV (0, 0); |
143 |
SvPADTMP_on (sv); |
144 |
npad[ix] = sv; |
145 |
} |
146 |
} |
147 |
|
148 |
#if 0 /* return -ENOTUNDERSTOOD */ |
149 |
/* Now that vars are all in place, clone nested closures. */ |
150 |
|
151 |
for (ix = fpad; ix > 0; ix--) { |
152 |
SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; |
153 |
if (namesv |
154 |
&& namesv != &PL_sv_undef |
155 |
&& !(SvFLAGS(namesv) & SVf_FAKE) |
156 |
&& *SvPVX(namesv) == '&' |
157 |
&& CvCLONE(ppad[ix])) |
158 |
{ |
159 |
CV *kid = cv_clone((CV*)ppad[ix]); |
160 |
SvREFCNT_dec(ppad[ix]); |
161 |
CvCLONE_on(kid); |
162 |
SvPADMY_on(kid); |
163 |
npad[ix] = (SV*)kid; |
164 |
} |
165 |
} |
166 |
#endif |
167 |
|
168 |
return newpadlist; |
169 |
} |
170 |
|
171 |
#ifdef MAY_FLUSH |
172 |
STATIC AV * |
173 |
free_padlist (AV *padlist) |
174 |
{ |
175 |
/* may be during global destruction */ |
176 |
if (SvREFCNT(padlist)) |
177 |
{ |
178 |
I32 i = AvFILLp(padlist); |
179 |
while (i >= 0) |
180 |
{ |
181 |
SV **svp = av_fetch(padlist, i--, FALSE); |
182 |
SV *sv = svp ? *svp : Nullsv; |
183 |
if (sv) |
184 |
SvREFCNT_dec(sv); |
185 |
} |
186 |
|
187 |
SvREFCNT_dec((SV*)padlist); |
188 |
} |
189 |
} |
190 |
#endif |
191 |
|
192 |
/* the next two functions merely cache the padlists */ |
193 |
STATIC void |
194 |
get_padlist (CV *cv) |
195 |
{ |
196 |
SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); |
197 |
|
198 |
if (he && AvFILLp ((AV *)*he) >= 0) |
199 |
CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); |
200 |
else |
201 |
CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
202 |
} |
203 |
|
204 |
STATIC void |
205 |
put_padlist (CV *cv) |
206 |
{ |
207 |
SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); |
208 |
|
209 |
if (SvTYPE (*he) != SVt_PVAV) |
210 |
{ |
211 |
SvREFCNT_dec (*he); |
212 |
*he = (SV *)newAV (); |
213 |
} |
214 |
|
215 |
av_push ((AV *)*he, (SV *)CvPADLIST (cv)); |
216 |
} |
217 |
|
218 |
#ifdef MAY_FLUSH |
219 |
STATIC void |
220 |
flush_padlist_cache () |
221 |
{ |
222 |
HV *hv = padlist_cache; |
223 |
padlist_cache = newHV (); |
224 |
|
225 |
if (hv_iterinit (hv)) |
226 |
{ |
227 |
HE *he; |
228 |
AV *padlist; |
229 |
|
230 |
while (!!(he = hv_iternext (hv))) |
231 |
{ |
232 |
AV *av = (AV *)HeVAL(he); |
233 |
|
234 |
/* casting is fun. */ |
235 |
while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
236 |
free_padlist (padlist); |
237 |
} |
238 |
} |
239 |
|
240 |
SvREFCNT_dec (hv); |
241 |
} |
242 |
#endif |
243 |
|
244 |
#define SB do { |
245 |
#define SE } while (0) |
246 |
|
247 |
#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE |
248 |
#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE |
249 |
|
250 |
#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
251 |
|
252 |
static void |
253 |
load_state(pTHX_ Coro__State c) |
254 |
{ |
255 |
PL_dowarn = c->dowarn; |
256 |
|
257 |
PL_curstackinfo = c->curstackinfo; |
258 |
PL_curstack = c->curstack; |
259 |
PL_mainstack = c->mainstack; |
260 |
PL_stack_sp = c->stack_sp; |
261 |
PL_op = c->op; |
262 |
PL_curpad = c->curpad; |
263 |
PL_stack_base = c->stack_base; |
264 |
PL_stack_max = c->stack_max; |
265 |
PL_tmps_stack = c->tmps_stack; |
266 |
PL_tmps_floor = c->tmps_floor; |
267 |
PL_tmps_ix = c->tmps_ix; |
268 |
PL_tmps_max = c->tmps_max; |
269 |
PL_markstack = c->markstack; |
270 |
PL_markstack_ptr = c->markstack_ptr; |
271 |
PL_markstack_max = c->markstack_max; |
272 |
PL_scopestack = c->scopestack; |
273 |
PL_scopestack_ix = c->scopestack_ix; |
274 |
PL_scopestack_max = c->scopestack_max; |
275 |
PL_savestack = c->savestack; |
276 |
PL_savestack_ix = c->savestack_ix; |
277 |
PL_savestack_max = c->savestack_max; |
278 |
PL_retstack = c->retstack; |
279 |
PL_retstack_ix = c->retstack_ix; |
280 |
PL_retstack_max = c->retstack_max; |
281 |
PL_curcop = c->curcop; |
282 |
PL_start_env = c->start_env; |
283 |
PL_top_env = c->top_env; |
284 |
|
285 |
if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
286 |
if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
287 |
if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
288 |
|
289 |
{ |
290 |
dSP; |
291 |
CV *cv; |
292 |
|
293 |
/* now do the ugly restore mess */ |
294 |
while ((cv = (CV *)POPs)) |
295 |
{ |
296 |
AV *padlist = (AV *)POPs; |
297 |
|
298 |
if (padlist) |
299 |
{ |
300 |
put_padlist (cv); /* mark this padlist as available */ |
301 |
CvPADLIST(cv) = padlist; |
302 |
#ifdef USE_THREADS |
303 |
/*CvOWNER(cv) = (struct perl_thread *)POPs;*/ |
304 |
#endif |
305 |
} |
306 |
|
307 |
++CvDEPTH(cv); |
308 |
} |
309 |
|
310 |
PUTBACK; |
311 |
} |
312 |
} |
313 |
|
314 |
static void |
315 |
save_state(pTHX_ Coro__State c, int flags) |
316 |
{ |
317 |
{ |
318 |
dSP; |
319 |
I32 cxix = cxstack_ix; |
320 |
PERL_CONTEXT *ccstk = cxstack; |
321 |
PERL_SI *top_si = PL_curstackinfo; |
322 |
|
323 |
/* |
324 |
* the worst thing you can imagine happens first - we have to save |
325 |
* (and reinitialize) all cv's in the whole callchain :( |
326 |
*/ |
327 |
|
328 |
PUSHs (Nullsv); |
329 |
/* this loop was inspired by pp_caller */ |
330 |
for (;;) |
331 |
{ |
332 |
do |
333 |
{ |
334 |
PERL_CONTEXT *cx = &ccstk[cxix--]; |
335 |
|
336 |
if (CxTYPE(cx) == CXt_SUB) |
337 |
{ |
338 |
CV *cv = cx->blk_sub.cv; |
339 |
if (CvDEPTH(cv)) |
340 |
{ |
341 |
#ifdef USE_THREADS |
342 |
/*XPUSHs ((SV *)CvOWNER(cv));*/ |
343 |
/*CvOWNER(cv) = 0;*/ |
344 |
/*error must unlock this cv etc.. etc...*/ |
345 |
#endif |
346 |
EXTEND (SP, CvDEPTH(cv)*2); |
347 |
|
348 |
while (--CvDEPTH(cv)) |
349 |
{ |
350 |
/* this tells the restore code to increment CvDEPTH */ |
351 |
PUSHs (Nullsv); |
352 |
PUSHs ((SV *)cv); |
353 |
} |
354 |
|
355 |
PUSHs ((SV *)CvPADLIST(cv)); |
356 |
PUSHs ((SV *)cv); |
357 |
|
358 |
get_padlist (cv); /* this is a monster */ |
359 |
} |
360 |
} |
361 |
else if (CxTYPE(cx) == CXt_FORMAT) |
362 |
{ |
363 |
/* I never used formats, so how should I know how these are implemented? */ |
364 |
/* my bold guess is as a simple, plain sub... */ |
365 |
croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
366 |
} |
367 |
} |
368 |
while (cxix >= 0); |
369 |
|
370 |
if (top_si->si_type == PERLSI_MAIN) |
371 |
break; |
372 |
|
373 |
top_si = top_si->si_prev; |
374 |
ccstk = top_si->si_cxstack; |
375 |
cxix = top_si->si_cxix; |
376 |
} |
377 |
|
378 |
PUTBACK; |
379 |
} |
380 |
|
381 |
c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
382 |
c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
383 |
c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
384 |
|
385 |
/* I have not the slightest idea of why av_reify is necessary */ |
386 |
/* but if it's missing the defav contents magically get replaced sometimes */ |
387 |
if (c->defav) |
388 |
av_reify (c->defav); |
389 |
|
390 |
c->dowarn = PL_dowarn; |
391 |
|
392 |
c->curstackinfo = PL_curstackinfo; |
393 |
c->curstack = PL_curstack; |
394 |
c->mainstack = PL_mainstack; |
395 |
c->stack_sp = PL_stack_sp; |
396 |
c->op = PL_op; |
397 |
c->curpad = PL_curpad; |
398 |
c->stack_base = PL_stack_base; |
399 |
c->stack_max = PL_stack_max; |
400 |
c->tmps_stack = PL_tmps_stack; |
401 |
c->tmps_floor = PL_tmps_floor; |
402 |
c->tmps_ix = PL_tmps_ix; |
403 |
c->tmps_max = PL_tmps_max; |
404 |
c->markstack = PL_markstack; |
405 |
c->markstack_ptr = PL_markstack_ptr; |
406 |
c->markstack_max = PL_markstack_max; |
407 |
c->scopestack = PL_scopestack; |
408 |
c->scopestack_ix = PL_scopestack_ix; |
409 |
c->scopestack_max = PL_scopestack_max; |
410 |
c->savestack = PL_savestack; |
411 |
c->savestack_ix = PL_savestack_ix; |
412 |
c->savestack_max = PL_savestack_max; |
413 |
c->retstack = PL_retstack; |
414 |
c->retstack_ix = PL_retstack_ix; |
415 |
c->retstack_max = PL_retstack_max; |
416 |
c->curcop = PL_curcop; |
417 |
c->start_env = PL_start_env; |
418 |
c->top_env = PL_top_env; |
419 |
} |
420 |
|
421 |
/* |
422 |
* allocate various perl stacks. This is an exact copy |
423 |
* of perl.c:init_stacks, except that it uses less memory |
424 |
* on the assumption that coroutines do not usually need |
425 |
* a lot of stackspace. |
426 |
*/ |
427 |
STATIC void |
428 |
coro_init_stacks (pTHX) |
429 |
{ |
430 |
PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
431 |
PL_curstackinfo->si_type = PERLSI_MAIN; |
432 |
PL_curstack = PL_curstackinfo->si_stack; |
433 |
PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
434 |
|
435 |
PL_stack_base = AvARRAY(PL_curstack); |
436 |
PL_stack_sp = PL_stack_base; |
437 |
PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
438 |
|
439 |
New(50,PL_tmps_stack,64,SV*); |
440 |
PL_tmps_floor = -1; |
441 |
PL_tmps_ix = -1; |
442 |
PL_tmps_max = 64; |
443 |
|
444 |
New(54,PL_markstack,12,I32); |
445 |
PL_markstack_ptr = PL_markstack; |
446 |
PL_markstack_max = PL_markstack + 12; |
447 |
|
448 |
SET_MARK_OFFSET; |
449 |
|
450 |
New(54,PL_scopestack,12,I32); |
451 |
PL_scopestack_ix = 0; |
452 |
PL_scopestack_max = 12; |
453 |
|
454 |
New(54,PL_savestack,64,ANY); |
455 |
PL_savestack_ix = 0; |
456 |
PL_savestack_max = 64; |
457 |
|
458 |
New(54,PL_retstack,8,OP*); |
459 |
PL_retstack_ix = 0; |
460 |
PL_retstack_max = 8; |
461 |
} |
462 |
|
463 |
/* |
464 |
* destroy the stacks, the callchain etc... |
465 |
* still there is a memleak of 128 bytes... |
466 |
*/ |
467 |
STATIC void |
468 |
destroy_stacks(pTHX) |
469 |
{ |
470 |
/* is this ugly, I ask? */ |
471 |
while (PL_scopestack_ix) |
472 |
LEAVE; |
473 |
|
474 |
/* sure it is, but more important: is it correct?? :/ */ |
475 |
while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
476 |
FREETMPS; |
477 |
|
478 |
while (PL_curstackinfo->si_next) |
479 |
PL_curstackinfo = PL_curstackinfo->si_next; |
480 |
|
481 |
while (PL_curstackinfo) |
482 |
{ |
483 |
PERL_SI *p = PL_curstackinfo->si_prev; |
484 |
|
485 |
{ |
486 |
dSP; |
487 |
SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
488 |
PUTBACK; /* possibly superfluous */ |
489 |
} |
490 |
|
491 |
dounwind(-1); |
492 |
|
493 |
SvREFCNT_dec(PL_curstackinfo->si_stack); |
494 |
Safefree(PL_curstackinfo->si_cxstack); |
495 |
Safefree(PL_curstackinfo); |
496 |
PL_curstackinfo = p; |
497 |
} |
498 |
|
499 |
Safefree(PL_tmps_stack); |
500 |
Safefree(PL_markstack); |
501 |
Safefree(PL_scopestack); |
502 |
Safefree(PL_savestack); |
503 |
Safefree(PL_retstack); |
504 |
} |
505 |
|
506 |
static void |
507 |
allocate_stack (Coro__State ctx) |
508 |
{ |
509 |
#ifdef HAVE_MMAP |
510 |
ctx->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-use */ |
511 |
ctx->sptr = mmap (0, ctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
512 |
if (ctx->sptr == (void *)-1) |
513 |
#endif |
514 |
{ |
515 |
/*FIXME*//*D*//* reasonable stack size! */ |
516 |
ctx->ssize = 4096 * sizeof (long); |
517 |
New (0, ctx->sptr, 4096, long); |
518 |
} |
519 |
} |
520 |
|
521 |
static void |
522 |
deallocate_stack (Coro__State ctx) |
523 |
{ |
524 |
#ifdef HAVE_MMAP |
525 |
munmap (ctx->sptr, ctx->ssize); |
526 |
#else |
527 |
Safefree (ctx->sptr); |
528 |
#endif |
529 |
} |
530 |
|
531 |
/* might go away together with optional SAVE_CCTXT */ |
532 |
static void |
533 |
setup_coro (void *arg) |
534 |
{ |
535 |
/* |
536 |
* emulate part of the perl startup here. |
537 |
*/ |
538 |
dSP; |
539 |
Coro__State ctx = (Coro__State)arg; |
540 |
SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
541 |
|
542 |
coro_init_stacks (aTHX); |
543 |
JMPENV_BOOTSTRAP; |
544 |
SPAGAIN; |
545 |
|
546 |
/*PL_curcop = 0;*/ |
547 |
SvREFCNT_dec (GvAV (PL_defgv)); |
548 |
GvAV (PL_defgv) = ctx->args; |
549 |
|
550 |
if (ctx->sptr) |
551 |
{ |
552 |
PUSHMARK(SP); |
553 |
PUTBACK; |
554 |
(void) call_sv (sub_init, G_VOID|G_NOARGS); |
555 |
croak ("FATAL: CCTXT coroutine returned!"); |
556 |
} |
557 |
else |
558 |
{ |
559 |
UNOP myop; |
560 |
|
561 |
PL_op = (OP *)&myop; |
562 |
|
563 |
Zero(&myop, 1, UNOP); |
564 |
myop.op_next = Nullop; |
565 |
myop.op_flags = OPf_WANT_VOID; |
566 |
|
567 |
PUSHMARK(SP); |
568 |
XPUSHs (sub_init); |
569 |
/* |
570 |
* the next line is slightly wrong, as PL_op->op_next |
571 |
* is actually being executed so we skip the first op. |
572 |
* that doesn't matter, though, since it is only |
573 |
* pp_nextstate and we never return... |
574 |
* ah yes, and I don't care anyways ;) |
575 |
*/ |
576 |
PUTBACK; |
577 |
PL_op = pp_entersub(); |
578 |
SPAGAIN; |
579 |
|
580 |
ENTER; /* necessary e.g. for dounwind */ |
581 |
} |
582 |
} |
583 |
|
584 |
STATIC void |
585 |
transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
586 |
{ |
587 |
dSP; |
588 |
|
589 |
if (prev != next) |
590 |
{ |
591 |
/* |
592 |
* this could be done in newprocess which would lead to |
593 |
* extremely elegant and fast (basically just SAVE/LOAD) |
594 |
* code here, but lazy allocation of stacks has also |
595 |
* some virtues and the overhead of the if() is nil. |
596 |
*/ |
597 |
if (next->mainstack) |
598 |
{ |
599 |
SAVE (prev, flags); |
600 |
LOAD (next); |
601 |
|
602 |
/* mark this state as in-use */ |
603 |
next->mainstack = 0; |
604 |
next->tmps_ix = -2; |
605 |
|
606 |
if (flags & TRANSFER_SAVE_CCTXT) |
607 |
{ |
608 |
if (!next->ssize) |
609 |
croak ("destination coroutine has no CCTXT (%p, %d)", next->sptr, next->ssize); |
610 |
|
611 |
if (!prev->ssize) |
612 |
prev->ssize = 1; /* mark cctx as valid ;) */ |
613 |
|
614 |
coro_transfer (&(prev->cctx), &(next->cctx)); |
615 |
} |
616 |
|
617 |
} |
618 |
else if (next->tmps_ix == -2) |
619 |
croak ("tried to transfer to running coroutine"); |
620 |
else |
621 |
{ |
622 |
SAVE (prev, -1); /* first get rid of the old state */ |
623 |
|
624 |
if (flags & TRANSFER_SAVE_CCTXT) |
625 |
{ |
626 |
if (!next->ssize) |
627 |
{ |
628 |
allocate_stack (next); |
629 |
coro_create (&(next->cctx), |
630 |
setup_coro, (void *)next, |
631 |
next->sptr, next->ssize); |
632 |
} |
633 |
|
634 |
if (!prev->ssize) |
635 |
prev->ssize = 1; /* mark cctx as valid ;) */ |
636 |
|
637 |
coro_transfer (&(prev->cctx), &(next->cctx)); |
638 |
} |
639 |
else |
640 |
setup_coro (next); |
641 |
} |
642 |
} |
643 |
} |
644 |
|
645 |
MODULE = Coro::State PACKAGE = Coro::State |
646 |
|
647 |
PROTOTYPES: ENABLE |
648 |
|
649 |
BOOT: |
650 |
{ /* {} necessary for stoopid perl-5.6.x */ |
651 |
coro_state_stash = gv_stashpvn ("Coro::State", 10, TRUE); |
652 |
|
653 |
newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
654 |
newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
655 |
newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
656 |
newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
657 |
|
658 |
if (!padlist_cache) |
659 |
padlist_cache = newHV (); |
660 |
|
661 |
main_mainstack = PL_mainstack; |
662 |
} |
663 |
|
664 |
Coro::State |
665 |
_newprocess(args) |
666 |
SV * args |
667 |
PROTOTYPE: $ |
668 |
CODE: |
669 |
Coro__State coro; |
670 |
|
671 |
if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
672 |
croak ("Coro::State::_newprocess expects an arrayref"); |
673 |
|
674 |
New (0, coro, 1, struct coro); |
675 |
|
676 |
coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
677 |
coro->mainstack = 0; /* actual work is done inside transfer */ |
678 |
coro->sptr = 0; |
679 |
coro->ssize = 0; |
680 |
|
681 |
RETVAL = coro; |
682 |
OUTPUT: |
683 |
RETVAL |
684 |
|
685 |
void |
686 |
transfer(prev, next, flags = TRANSFER_SAVE_ALL) |
687 |
Coro::State_or_hashref prev |
688 |
Coro::State_or_hashref next |
689 |
int flags |
690 |
PROTOTYPE: @ |
691 |
CODE: |
692 |
transfer (aTHX_ prev, next, flags); |
693 |
|
694 |
void |
695 |
DESTROY(coro) |
696 |
Coro::State coro |
697 |
CODE: |
698 |
|
699 |
if (coro->mainstack && coro->mainstack != main_mainstack) |
700 |
{ |
701 |
struct coro temp; |
702 |
|
703 |
SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
704 |
LOAD(aTHX_ coro); |
705 |
|
706 |
destroy_stacks (aTHX); |
707 |
|
708 |
LOAD((&temp)); /* this will get rid of defsv etc.. */ |
709 |
|
710 |
coro->mainstack = 0; |
711 |
} |
712 |
|
713 |
if (coro->sptr) |
714 |
{ |
715 |
deallocate_stack (coro); |
716 |
coro->sptr = 0; |
717 |
} |
718 |
|
719 |
Safefree (coro); |
720 |
|
721 |
void |
722 |
flush() |
723 |
CODE: |
724 |
#ifdef MAY_FLUSH |
725 |
flush_padlist_cache (); |
726 |
#endif |
727 |
|
728 |
MODULE = Coro::State PACKAGE = Coro::Cont |
729 |
|
730 |
# this is slightly dirty |
731 |
|
732 |
void |
733 |
yield(...) |
734 |
PROTOTYPE: @ |
735 |
CODE: |
736 |
static SV *returnstk; |
737 |
SV *sv; |
738 |
AV *defav = GvAV (PL_defgv); |
739 |
struct coro *prev, *next; |
740 |
|
741 |
if (!returnstk) |
742 |
returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); |
743 |
|
744 |
/* set up @_ -- ugly */ |
745 |
av_clear (defav); |
746 |
av_fill (defav, items - 1); |
747 |
while (items--) |
748 |
av_store (defav, items, SvREFCNT_inc (ST(items))); |
749 |
|
750 |
mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
751 |
sv = av_pop ((AV *)SvRV (returnstk)); |
752 |
prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
753 |
next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
754 |
SvREFCNT_dec (sv); |
755 |
|
756 |
transfer(aTHX_ prev, next, 0); |
757 |
|