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 | #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 | |
5 | struct coro { |
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 */ |
6 | U8 dowarn; |
35 | U8 dowarn; |
7 | |
36 | |
|
|
37 | /* the stacks and related info (callchain etc..) */ |
8 | PERL_SI *curstackinfo; |
38 | PERL_SI *curstackinfo; |
9 | AV *curstack; |
39 | AV *curstack; |
10 | AV *mainstack; |
40 | AV *mainstack; |
11 | SV **stack_sp; |
41 | SV **stack_sp; |
12 | OP *op; |
42 | OP *op; |
… | |
… | |
28 | I32 savestack_max; |
58 | I32 savestack_max; |
29 | OP **retstack; |
59 | OP **retstack; |
30 | I32 retstack_ix; |
60 | I32 retstack_ix; |
31 | I32 retstack_max; |
61 | I32 retstack_max; |
32 | COP *curcop; |
62 | COP *curcop; |
|
|
63 | JMPENV start_env; |
|
|
64 | JMPENV *top_env; |
33 | |
65 | |
34 | AV *defav; |
66 | /* data associated with this coroutine (initial args) */ |
35 | |
67 | AV *args; |
36 | SV *proc; |
|
|
37 | }; |
68 | }; |
38 | |
69 | |
39 | typedef struct coro *Coro__State; |
70 | typedef struct coro *Coro__State; |
40 | typedef struct coro *Coro__State_or_hashref; |
71 | typedef struct coro *Coro__State_or_hashref; |
41 | |
72 | |
42 | #define SAVE(c) \ |
73 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
43 | c->dowarn = PL_dowarn; \ |
74 | static HV *coro_state_stash; |
44 | c->curstackinfo = PL_curstackinfo; \ |
75 | static HV *padlist_cache; |
45 | c->curstack = PL_curstack; \ |
|
|
46 | c->mainstack = PL_mainstack; \ |
|
|
47 | c->stack_sp = PL_stack_sp; \ |
|
|
48 | c->op = PL_op; \ |
|
|
49 | c->curpad = PL_curpad; \ |
|
|
50 | c->stack_base = PL_stack_base; \ |
|
|
51 | c->stack_max = PL_stack_max; \ |
|
|
52 | c->tmps_stack = PL_tmps_stack; \ |
|
|
53 | c->tmps_floor = PL_tmps_floor; \ |
|
|
54 | c->tmps_ix = PL_tmps_ix; \ |
|
|
55 | c->tmps_max = PL_tmps_max; \ |
|
|
56 | c->markstack = PL_markstack; \ |
|
|
57 | c->markstack_ptr = PL_markstack_ptr; \ |
|
|
58 | c->markstack_max = PL_markstack_max; \ |
|
|
59 | c->scopestack = PL_scopestack; \ |
|
|
60 | c->scopestack_ix = PL_scopestack_ix; \ |
|
|
61 | c->scopestack_max = PL_scopestack_max;\ |
|
|
62 | c->savestack = PL_savestack; \ |
|
|
63 | c->savestack_ix = PL_savestack_ix; \ |
|
|
64 | c->savestack_max = PL_savestack_max; \ |
|
|
65 | c->retstack = PL_retstack; \ |
|
|
66 | c->retstack_ix = PL_retstack_ix; \ |
|
|
67 | c->retstack_max = PL_retstack_max; \ |
|
|
68 | c->curcop = PL_curcop; \ |
|
|
69 | c->defav = GvAV (PL_defgv); |
|
|
70 | |
76 | |
71 | #define LOAD(c) \ |
77 | /* mostly copied from op.c:cv_clone2 */ |
72 | PL_dowarn = c->dowarn; \ |
78 | STATIC AV * |
73 | PL_curstackinfo = c->curstackinfo; \ |
79 | clone_padlist (AV *protopadlist) |
74 | PL_curstack = c->curstack; \ |
80 | { |
75 | PL_mainstack = c->mainstack; \ |
81 | AV *av; |
76 | PL_stack_sp = c->stack_sp; \ |
82 | I32 ix; |
77 | PL_op = c->op; \ |
83 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
78 | PL_curpad = c->curpad; \ |
84 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
79 | PL_stack_base = c->stack_base; \ |
85 | SV **pname = AvARRAY (protopad_name); |
80 | PL_stack_max = c->stack_max; \ |
86 | SV **ppad = AvARRAY (protopad); |
81 | PL_tmps_stack = c->tmps_stack; \ |
87 | I32 fname = AvFILLp (protopad_name); |
82 | PL_tmps_floor = c->tmps_floor; \ |
88 | I32 fpad = AvFILLp (protopad); |
83 | PL_tmps_ix = c->tmps_ix; \ |
89 | AV *newpadlist, *newpad_name, *newpad; |
84 | PL_tmps_max = c->tmps_max; \ |
90 | SV **npad; |
85 | PL_markstack = c->markstack; \ |
|
|
86 | PL_markstack_ptr = c->markstack_ptr; \ |
|
|
87 | PL_markstack_max = c->markstack_max; \ |
|
|
88 | PL_scopestack = c->scopestack; \ |
|
|
89 | PL_scopestack_ix = c->scopestack_ix; \ |
|
|
90 | PL_scopestack_max = c->scopestack_max;\ |
|
|
91 | PL_savestack = c->savestack; \ |
|
|
92 | PL_savestack_ix = c->savestack_ix; \ |
|
|
93 | PL_savestack_max = c->savestack_max; \ |
|
|
94 | PL_retstack = c->retstack; \ |
|
|
95 | PL_retstack_ix = c->retstack_ix; \ |
|
|
96 | PL_retstack_max = c->retstack_max; \ |
|
|
97 | PL_curcop = c->curcop; \ |
|
|
98 | GvAV (PL_defgv) = c->defav; |
|
|
99 | |
91 | |
100 | /* this is an EXACT copy of S_nuke_stacks in perl.c, which is unfortunately static */ |
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 */ |
101 | STATIC void |
193 | STATIC void |
102 | S_nuke_stacks(pTHX) |
194 | get_padlist (CV *cv) |
103 | { |
195 | { |
104 | while (PL_curstackinfo->si_next) |
196 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); |
105 | PL_curstackinfo = PL_curstackinfo->si_next; |
197 | |
106 | while (PL_curstackinfo) { |
198 | if (he && AvFILLp ((AV *)*he) >= 0) |
107 | PERL_SI *p = PL_curstackinfo->si_prev; |
199 | CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); |
108 | /* curstackinfo->si_stack got nuked by sv_free_arenas() */ |
200 | else |
109 | Safefree(PL_curstackinfo->si_cxstack); |
201 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
110 | Safefree(PL_curstackinfo); |
202 | } |
111 | PL_curstackinfo = p; |
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 (); |
112 | } |
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 | |
113 | Safefree(PL_tmps_stack); |
499 | Safefree(PL_tmps_stack); |
114 | Safefree(PL_markstack); |
500 | Safefree(PL_markstack); |
115 | Safefree(PL_scopestack); |
501 | Safefree(PL_scopestack); |
116 | Safefree(PL_savestack); |
502 | Safefree(PL_savestack); |
117 | Safefree(PL_retstack); |
503 | Safefree(PL_retstack); |
118 | } |
504 | } |
119 | |
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 | |
120 | MODULE = Coro::State PACKAGE = Coro::State |
645 | MODULE = Coro::State PACKAGE = Coro::State |
121 | |
646 | |
122 | PROTOTYPES: ENABLE |
647 | PROTOTYPES: ENABLE |
123 | |
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 | |
124 | Coro::State |
664 | Coro::State |
125 | newprocess(proc) |
665 | _newprocess(args) |
126 | SV * proc |
666 | SV * args |
127 | PROTOTYPE: & |
667 | PROTOTYPE: $ |
128 | CODE: |
668 | CODE: |
129 | Coro__State coro; |
669 | Coro__State coro; |
|
|
670 | |
|
|
671 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
|
|
672 | croak ("Coro::State::_newprocess expects an arrayref"); |
130 | |
673 | |
131 | New (0, coro, 1, struct coro); |
674 | New (0, coro, 1, struct coro); |
132 | |
675 | |
|
|
676 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
133 | coro->mainstack = 0; /* actual work is done inside transfer */ |
677 | coro->mainstack = 0; /* actual work is done inside transfer */ |
134 | coro->proc = SvREFCNT_inc (proc); |
678 | coro->sptr = 0; |
|
|
679 | coro->ssize = 0; |
135 | |
680 | |
136 | RETVAL = coro; |
681 | RETVAL = coro; |
137 | OUTPUT: |
682 | OUTPUT: |
138 | RETVAL |
683 | RETVAL |
139 | |
684 | |
140 | void |
685 | void |
141 | transfer(prev,next) |
686 | transfer(prev, next, flags = TRANSFER_SAVE_ALL) |
142 | Coro::State_or_hashref prev |
687 | Coro::State_or_hashref prev |
143 | Coro::State_or_hashref next |
688 | Coro::State_or_hashref next |
|
|
689 | int flags |
|
|
690 | PROTOTYPE: @ |
144 | CODE: |
691 | CODE: |
145 | |
692 | transfer (aTHX_ prev, next, flags); |
146 | if (prev != next) |
|
|
147 | { |
|
|
148 | PUTBACK; |
|
|
149 | SAVE (prev); |
|
|
150 | |
|
|
151 | /* |
|
|
152 | * this could be done in newprocess which would to |
|
|
153 | * extremely elegant and fast (just PUTBACK/SAVE/LOAD/SPAGAIN) |
|
|
154 | * code here, but lazy allocation of stacks has also |
|
|
155 | * some virtues and the overhead of the if() is nil. |
|
|
156 | */ |
|
|
157 | if (next->mainstack) |
|
|
158 | { |
|
|
159 | LOAD (next); |
|
|
160 | next->mainstack = 0; /* unnecessary but much cleaner */ |
|
|
161 | SPAGAIN; |
|
|
162 | } |
|
|
163 | else |
|
|
164 | { |
|
|
165 | /* |
|
|
166 | * emulate part of the perl startup here. |
|
|
167 | */ |
|
|
168 | UNOP myop; |
|
|
169 | |
|
|
170 | init_stacks (); |
|
|
171 | PL_op = (OP *)&myop; |
|
|
172 | /*PL_curcop = 0;*/ |
|
|
173 | GvAV (PL_defgv) = newAV (); |
|
|
174 | |
|
|
175 | SPAGAIN; |
|
|
176 | Zero(&myop, 1, UNOP); |
|
|
177 | myop.op_next = Nullop; |
|
|
178 | myop.op_flags = OPf_WANT_VOID; |
|
|
179 | |
|
|
180 | EXTEND (SP,1); |
|
|
181 | PUSHs (next->proc); |
|
|
182 | |
|
|
183 | PUTBACK; |
|
|
184 | /* |
|
|
185 | * the next line is slightly wrong, as PL_op->op_next |
|
|
186 | * is actually being executed so we skip the first op |
|
|
187 | * that doens't matter, though, since it is only |
|
|
188 | * pp_nextstate and we never return... |
|
|
189 | */ |
|
|
190 | PL_op = Perl_pp_entersub(aTHX); |
|
|
191 | SPAGAIN; |
|
|
192 | |
|
|
193 | ENTER; |
|
|
194 | } |
|
|
195 | } |
|
|
196 | |
693 | |
197 | void |
694 | void |
198 | DESTROY(coro) |
695 | DESTROY(coro) |
199 | Coro::State coro |
696 | Coro::State coro |
200 | CODE: |
697 | CODE: |
201 | |
698 | |
202 | if (coro->mainstack) |
699 | if (coro->mainstack && coro->mainstack != main_mainstack) |
203 | { |
700 | { |
204 | struct coro temp; |
701 | struct coro temp; |
205 | |
702 | |
206 | PUTBACK; |
703 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
207 | SAVE((&temp)); |
|
|
208 | LOAD(coro); |
704 | LOAD(aTHX_ coro); |
209 | |
705 | |
210 | S_nuke_stacks (); |
706 | destroy_stacks (aTHX); |
211 | SvREFCNT_dec ((SV *)GvAV (PL_defgv)); |
|
|
212 | |
707 | |
213 | LOAD((&temp)); |
708 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
214 | SPAGAIN; |
709 | |
|
|
710 | coro->mainstack = 0; |
215 | } |
711 | } |
216 | |
712 | |
217 | SvREFCNT_dec (coro->proc); |
713 | if (coro->sptr) |
|
|
714 | { |
|
|
715 | deallocate_stack (coro); |
|
|
716 | coro->sptr = 0; |
|
|
717 | } |
|
|
718 | |
218 | Safefree (coro); |
719 | Safefree (coro); |
219 | |
720 | |
|
|
721 | void |
|
|
722 | flush() |
|
|
723 | CODE: |
|
|
724 | #ifdef MAY_FLUSH |
|
|
725 | flush_padlist_cache (); |
|
|
726 | #endif |
220 | |
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 | |