ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
Revision: 1.15
Committed: Wed Jul 25 04:14:38 2001 UTC (22 years, 9 months ago) by root
Branch: MAIN
Changes since 1.14: +140 -52 lines
Log Message:
*** empty log message ***

File Contents

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