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