|
|
1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
|
|
4 | |
1 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
2 | #include "perl.h" |
6 | #include "perl.h" |
3 | #include "XSUB.h" |
7 | #include "XSUB.h" |
4 | |
8 | |
5 | #include "patchlevel.h" |
9 | #include "patchlevel.h" |
6 | |
10 | |
7 | #if PATCHLEVEL < 6 |
11 | #if PERL_VERSION < 6 |
8 | # ifndef PL_ppaddr |
12 | # ifndef PL_ppaddr |
9 | # define PL_ppaddr ppaddr |
13 | # define PL_ppaddr ppaddr |
10 | # endif |
14 | # endif |
11 | # ifndef call_sv |
15 | # ifndef call_sv |
12 | # define call_sv perl_call_sv |
16 | # define call_sv perl_call_sv |
… | |
… | |
23 | # ifndef IS_PADCONST |
27 | # ifndef IS_PADCONST |
24 | # define IS_PADCONST(v) 0 |
28 | # define IS_PADCONST(v) 0 |
25 | # endif |
29 | # endif |
26 | #endif |
30 | #endif |
27 | |
31 | |
28 | #include "libcoro/coro.c" |
|
|
29 | |
|
|
30 | #include <signal.h> |
32 | #include <signal.h> |
31 | |
33 | |
32 | #ifdef HAVE_MMAP |
34 | #ifdef HAVE_MMAP |
33 | # include <unistd.h> |
35 | # include <unistd.h> |
34 | # include <sys/mman.h> |
36 | # include <sys/mman.h> |
… | |
… | |
39 | # undef HAVE_MMAP |
41 | # undef HAVE_MMAP |
40 | # endif |
42 | # endif |
41 | # endif |
43 | # endif |
42 | #endif |
44 | #endif |
43 | |
45 | |
44 | #define MAY_FLUSH /* increases codesize and is rarely used */ |
|
|
45 | |
|
|
46 | #define SUB_INIT "Coro::State::initialize" |
46 | #define SUB_INIT "Coro::State::initialize" |
47 | #define UCORO_STATE "_coro_state" |
47 | #define UCORO_STATE "_coro_state" |
48 | |
48 | |
49 | /* The next macro should declare a variable stacklevel that contains and approximation |
49 | /* The next macro should declare a variable stacklevel that contains and approximation |
50 | * to the current C stack pointer. Its property is that it changes with each call |
50 | * to the current C stack pointer. Its property is that it changes with each call |
… | |
… | |
55 | |
55 | |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
57 | |
57 | |
58 | #include "CoroAPI.h" |
58 | #include "CoroAPI.h" |
59 | |
59 | |
|
|
60 | #ifdef USE_ITHREADS |
|
|
61 | static perl_mutex coro_mutex; |
|
|
62 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
|
|
63 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
|
|
64 | #else |
|
|
65 | # define LOCK (void)0 |
|
|
66 | # define UNLOCK (void)0 |
|
|
67 | #endif |
|
|
68 | |
60 | static struct CoroAPI coroapi; |
69 | static struct CoroAPI coroapi; |
|
|
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
71 | static HV *coro_state_stash; |
|
|
72 | static SV *ucoro_state_sv; |
|
|
73 | static U32 ucoro_state_hash; |
|
|
74 | static SV *coro_mortal; /* will be freed after next transfer */ |
61 | |
75 | |
62 | /* this is actually not only the c stack but also c registers etc... */ |
76 | /* this is actually not only the c stack but also c registers etc... */ |
63 | typedef struct { |
77 | typedef struct { |
64 | int refcnt; /* pointer reference counter */ |
78 | int refcnt; /* pointer reference counter */ |
65 | int usecnt; /* shared by how many coroutines */ |
79 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
95 | AV *mainstack; |
109 | AV *mainstack; |
96 | SV **stack_sp; |
110 | SV **stack_sp; |
97 | OP *op; |
111 | OP *op; |
98 | SV **curpad; |
112 | SV **curpad; |
99 | AV *comppad; |
113 | AV *comppad; |
|
|
114 | CV *compcv; |
100 | SV **stack_base; |
115 | SV **stack_base; |
101 | SV **stack_max; |
116 | SV **stack_max; |
102 | SV **tmps_stack; |
117 | SV **tmps_stack; |
103 | I32 tmps_floor; |
118 | I32 tmps_floor; |
104 | I32 tmps_ix; |
119 | I32 tmps_ix; |
… | |
… | |
113 | I32 savestack_ix; |
128 | I32 savestack_ix; |
114 | I32 savestack_max; |
129 | I32 savestack_max; |
115 | OP **retstack; |
130 | OP **retstack; |
116 | I32 retstack_ix; |
131 | I32 retstack_ix; |
117 | I32 retstack_max; |
132 | I32 retstack_max; |
|
|
133 | PMOP *curpm; |
118 | COP *curcop; |
134 | COP *curcop; |
119 | JMPENV *top_env; |
135 | JMPENV *top_env; |
120 | |
136 | |
121 | /* data associated with this coroutine (initial args) */ |
137 | /* data associated with this coroutine (initial args) */ |
122 | AV *args; |
138 | AV *args; |
123 | }; |
139 | }; |
124 | |
140 | |
125 | typedef struct coro *Coro__State; |
141 | typedef struct coro *Coro__State; |
126 | typedef struct coro *Coro__State_or_hashref; |
142 | typedef struct coro *Coro__State_or_hashref; |
127 | |
143 | |
128 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
129 | static HV *coro_state_stash; |
|
|
130 | static SV *ucoro_state_sv; |
|
|
131 | static U32 ucoro_state_hash; |
|
|
132 | static HV *padlist_cache; |
|
|
133 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
134 | |
|
|
135 | /* mostly copied from op.c:cv_clone2 */ |
144 | /* mostly copied from op.c:cv_clone2 */ |
136 | STATIC AV * |
145 | STATIC AV * |
137 | clone_padlist (AV *protopadlist) |
146 | clone_padlist (pTHX_ AV *protopadlist) |
138 | { |
147 | { |
139 | AV *av; |
148 | AV *av; |
140 | I32 ix; |
149 | I32 ix; |
141 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
150 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
142 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
151 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
… | |
… | |
166 | AvFLAGS (av) = AVf_REIFY; |
175 | AvFLAGS (av) = AVf_REIFY; |
167 | |
176 | |
168 | for (ix = fpad; ix > 0; ix--) |
177 | for (ix = fpad; ix > 0; ix--) |
169 | { |
178 | { |
170 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
179 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
180 | |
171 | if (namesv && namesv != &PL_sv_undef) |
181 | if (namesv && namesv != &PL_sv_undef) |
172 | { |
182 | { |
173 | char *name = SvPVX (namesv); /* XXX */ |
183 | char *name = SvPVX (namesv); /* XXX */ |
|
|
184 | |
174 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
185 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
175 | { /* lexical from outside? */ |
186 | { /* lexical from outside? */ |
176 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
187 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
177 | } |
188 | } |
178 | else |
189 | else |
… | |
… | |
184 | sv = (SV *) newAV (); |
195 | sv = (SV *) newAV (); |
185 | else if (*name == '%') |
196 | else if (*name == '%') |
186 | sv = (SV *) newHV (); |
197 | sv = (SV *) newHV (); |
187 | else |
198 | else |
188 | sv = NEWSV (0, 0); |
199 | sv = NEWSV (0, 0); |
|
|
200 | |
|
|
201 | #ifdef SvPADBUSY |
189 | if (!SvPADBUSY (sv)) |
202 | if (!SvPADBUSY (sv)) |
|
|
203 | #endif |
190 | SvPADMY_on (sv); |
204 | SvPADMY_on (sv); |
|
|
205 | |
191 | npad[ix] = sv; |
206 | npad[ix] = sv; |
192 | } |
207 | } |
193 | } |
208 | } |
194 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
209 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
195 | { |
210 | { |
… | |
… | |
224 | #endif |
239 | #endif |
225 | |
240 | |
226 | return newpadlist; |
241 | return newpadlist; |
227 | } |
242 | } |
228 | |
243 | |
229 | #ifdef MAY_FLUSH |
|
|
230 | STATIC void |
244 | STATIC void |
231 | free_padlist (AV *padlist) |
245 | free_padlist (pTHX_ AV *padlist) |
232 | { |
246 | { |
233 | /* may be during global destruction */ |
247 | /* may be during global destruction */ |
234 | if (SvREFCNT(padlist)) |
248 | if (SvREFCNT (padlist)) |
235 | { |
249 | { |
236 | I32 i = AvFILLp(padlist); |
250 | I32 i = AvFILLp (padlist); |
237 | while (i >= 0) |
251 | while (i >= 0) |
238 | { |
252 | { |
239 | SV **svp = av_fetch(padlist, i--, FALSE); |
253 | SV **svp = av_fetch (padlist, i--, FALSE); |
240 | SV *sv = svp ? *svp : Nullsv; |
|
|
241 | if (sv) |
254 | if (svp) |
|
|
255 | { |
|
|
256 | SV *sv; |
|
|
257 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
242 | SvREFCNT_dec(sv); |
258 | SvREFCNT_dec (sv); |
|
|
259 | |
|
|
260 | SvREFCNT_dec (*svp); |
|
|
261 | } |
243 | } |
262 | } |
244 | |
263 | |
245 | SvREFCNT_dec((SV*)padlist); |
264 | SvREFCNT_dec ((SV*)padlist); |
246 | } |
265 | } |
247 | } |
266 | } |
248 | #endif |
267 | |
|
|
268 | STATIC int |
|
|
269 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
270 | { |
|
|
271 | AV *padlist; |
|
|
272 | AV *av = (AV *)mg->mg_obj; |
|
|
273 | |
|
|
274 | /* casting is fun. */ |
|
|
275 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
276 | free_padlist (aTHX_ padlist); |
|
|
277 | |
|
|
278 | SvREFCNT_dec (av); |
|
|
279 | } |
|
|
280 | |
|
|
281 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
|
|
282 | |
|
|
283 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
249 | |
284 | |
250 | /* the next two functions merely cache the padlists */ |
285 | /* the next two functions merely cache the padlists */ |
251 | STATIC void |
286 | STATIC void |
252 | get_padlist (CV *cv) |
287 | get_padlist (pTHX_ CV *cv) |
253 | { |
288 | { |
254 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); |
289 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
255 | |
290 | |
256 | if (he && AvFILLp ((AV *)*he) >= 0) |
291 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
257 | CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); |
292 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
258 | else |
293 | else |
259 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
294 | CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); |
260 | } |
295 | } |
261 | |
296 | |
262 | STATIC void |
297 | STATIC void |
263 | put_padlist (CV *cv) |
298 | put_padlist (pTHX_ CV *cv) |
264 | { |
299 | { |
265 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); |
300 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
266 | |
301 | |
267 | if (SvTYPE (*he) != SVt_PVAV) |
302 | if (!mg) |
268 | { |
303 | { |
269 | SvREFCNT_dec (*he); |
304 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
305 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
306 | mg->mg_virtual = &vtbl_coro; |
270 | *he = (SV *)newAV (); |
307 | mg->mg_obj = (SV *)newAV (); |
271 | } |
308 | } |
272 | |
309 | |
273 | av_push ((AV *)*he, (SV *)CvPADLIST (cv)); |
310 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
274 | } |
311 | } |
275 | |
|
|
276 | #ifdef MAY_FLUSH |
|
|
277 | STATIC void |
|
|
278 | flush_padlist_cache () |
|
|
279 | { |
|
|
280 | HV *hv = padlist_cache; |
|
|
281 | padlist_cache = newHV (); |
|
|
282 | |
|
|
283 | if (hv_iterinit (hv)) |
|
|
284 | { |
|
|
285 | HE *he; |
|
|
286 | AV *padlist; |
|
|
287 | |
|
|
288 | while (!!(he = hv_iternext (hv))) |
|
|
289 | { |
|
|
290 | AV *av = (AV *)HeVAL(he); |
|
|
291 | |
|
|
292 | /* casting is fun. */ |
|
|
293 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
294 | free_padlist (padlist); |
|
|
295 | } |
|
|
296 | } |
|
|
297 | |
|
|
298 | SvREFCNT_dec (hv); |
|
|
299 | } |
|
|
300 | #endif |
|
|
301 | |
312 | |
302 | #define SB do { |
313 | #define SB do { |
303 | #define SE } while (0) |
314 | #define SE } while (0) |
304 | |
315 | |
305 | #define LOAD(state) load_state(aTHX_ (state)); |
316 | #define LOAD(state) load_state(aTHX_ (state)); |
306 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
317 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
307 | |
318 | |
308 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
319 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
309 | |
320 | |
310 | static void |
321 | static void |
311 | load_state(pTHX_ Coro__State c) |
322 | load_state(pTHX_ Coro__State c) |
312 | { |
323 | { |
313 | PL_dowarn = c->dowarn; |
324 | PL_dowarn = c->dowarn; |
… | |
… | |
318 | PL_mainstack = c->mainstack; |
329 | PL_mainstack = c->mainstack; |
319 | PL_stack_sp = c->stack_sp; |
330 | PL_stack_sp = c->stack_sp; |
320 | PL_op = c->op; |
331 | PL_op = c->op; |
321 | PL_curpad = c->curpad; |
332 | PL_curpad = c->curpad; |
322 | PL_comppad = c->comppad; |
333 | PL_comppad = c->comppad; |
|
|
334 | PL_compcv = c->compcv; |
323 | PL_stack_base = c->stack_base; |
335 | PL_stack_base = c->stack_base; |
324 | PL_stack_max = c->stack_max; |
336 | PL_stack_max = c->stack_max; |
325 | PL_tmps_stack = c->tmps_stack; |
337 | PL_tmps_stack = c->tmps_stack; |
326 | PL_tmps_floor = c->tmps_floor; |
338 | PL_tmps_floor = c->tmps_floor; |
327 | PL_tmps_ix = c->tmps_ix; |
339 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
336 | PL_savestack_ix = c->savestack_ix; |
348 | PL_savestack_ix = c->savestack_ix; |
337 | PL_savestack_max = c->savestack_max; |
349 | PL_savestack_max = c->savestack_max; |
338 | PL_retstack = c->retstack; |
350 | PL_retstack = c->retstack; |
339 | PL_retstack_ix = c->retstack_ix; |
351 | PL_retstack_ix = c->retstack_ix; |
340 | PL_retstack_max = c->retstack_max; |
352 | PL_retstack_max = c->retstack_max; |
|
|
353 | PL_curpm = c->curpm; |
341 | PL_curcop = c->curcop; |
354 | PL_curcop = c->curcop; |
342 | PL_top_env = c->top_env; |
355 | PL_top_env = c->top_env; |
343 | |
356 | |
344 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
357 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
345 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
358 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
… | |
… | |
354 | { |
367 | { |
355 | AV *padlist = (AV *)POPs; |
368 | AV *padlist = (AV *)POPs; |
356 | |
369 | |
357 | if (padlist) |
370 | if (padlist) |
358 | { |
371 | { |
359 | put_padlist (cv); /* mark this padlist as available */ |
372 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
360 | CvPADLIST(cv) = padlist; |
373 | CvPADLIST(cv) = padlist; |
361 | #ifdef USE_THREADS |
|
|
362 | /*CvOWNER(cv) = (struct perl_thread *)POPs;*/ |
|
|
363 | #endif |
|
|
364 | } |
374 | } |
365 | |
375 | |
366 | ++CvDEPTH(cv); |
376 | ++CvDEPTH(cv); |
367 | } |
377 | } |
368 | |
378 | |
… | |
… | |
395 | if (CxTYPE(cx) == CXt_SUB) |
405 | if (CxTYPE(cx) == CXt_SUB) |
396 | { |
406 | { |
397 | CV *cv = cx->blk_sub.cv; |
407 | CV *cv = cx->blk_sub.cv; |
398 | if (CvDEPTH(cv)) |
408 | if (CvDEPTH(cv)) |
399 | { |
409 | { |
400 | #ifdef USE_THREADS |
|
|
401 | /*XPUSHs ((SV *)CvOWNER(cv));*/ |
|
|
402 | /*CvOWNER(cv) = 0;*/ |
|
|
403 | /*error must unlock this cv etc.. etc...*/ |
|
|
404 | #endif |
|
|
405 | EXTEND (SP, CvDEPTH(cv)*2); |
410 | EXTEND (SP, CvDEPTH(cv)*2); |
406 | |
411 | |
407 | while (--CvDEPTH(cv)) |
412 | while (--CvDEPTH(cv)) |
408 | { |
413 | { |
409 | /* this tells the restore code to increment CvDEPTH */ |
414 | /* this tells the restore code to increment CvDEPTH */ |
… | |
… | |
412 | } |
417 | } |
413 | |
418 | |
414 | PUSHs ((SV *)CvPADLIST(cv)); |
419 | PUSHs ((SV *)CvPADLIST(cv)); |
415 | PUSHs ((SV *)cv); |
420 | PUSHs ((SV *)cv); |
416 | |
421 | |
417 | get_padlist (cv); /* this is a monster */ |
422 | get_padlist (aTHX_ cv); /* this is a monster */ |
418 | } |
423 | } |
419 | } |
424 | } |
420 | #ifdef CXt_FORMAT |
425 | #ifdef CXt_FORMAT |
421 | else if (CxTYPE(cx) == CXt_FORMAT) |
426 | else if (CxTYPE(cx) == CXt_FORMAT) |
422 | { |
427 | { |
… | |
… | |
450 | c->mainstack = PL_mainstack; |
455 | c->mainstack = PL_mainstack; |
451 | c->stack_sp = PL_stack_sp; |
456 | c->stack_sp = PL_stack_sp; |
452 | c->op = PL_op; |
457 | c->op = PL_op; |
453 | c->curpad = PL_curpad; |
458 | c->curpad = PL_curpad; |
454 | c->comppad = PL_comppad; |
459 | c->comppad = PL_comppad; |
|
|
460 | c->compcv = PL_compcv; |
455 | c->stack_base = PL_stack_base; |
461 | c->stack_base = PL_stack_base; |
456 | c->stack_max = PL_stack_max; |
462 | c->stack_max = PL_stack_max; |
457 | c->tmps_stack = PL_tmps_stack; |
463 | c->tmps_stack = PL_tmps_stack; |
458 | c->tmps_floor = PL_tmps_floor; |
464 | c->tmps_floor = PL_tmps_floor; |
459 | c->tmps_ix = PL_tmps_ix; |
465 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
468 | c->savestack_ix = PL_savestack_ix; |
474 | c->savestack_ix = PL_savestack_ix; |
469 | c->savestack_max = PL_savestack_max; |
475 | c->savestack_max = PL_savestack_max; |
470 | c->retstack = PL_retstack; |
476 | c->retstack = PL_retstack; |
471 | c->retstack_ix = PL_retstack_ix; |
477 | c->retstack_ix = PL_retstack_ix; |
472 | c->retstack_max = PL_retstack_max; |
478 | c->retstack_max = PL_retstack_max; |
|
|
479 | c->curpm = PL_curpm; |
473 | c->curcop = PL_curcop; |
480 | c->curcop = PL_curcop; |
474 | c->top_env = PL_top_env; |
481 | c->top_env = PL_top_env; |
475 | } |
482 | } |
476 | |
483 | |
477 | /* |
484 | /* |
478 | * allocate various perl stacks. This is an exact copy |
485 | * allocate various perl stacks. This is an exact copy |
479 | * of perl.c:init_stacks, except that it uses less memory |
486 | * of perl.c:init_stacks, except that it uses less memory |
480 | * on the assumption that coroutines do not usually need |
487 | * on the (sometimes correct) assumption that coroutines do |
481 | * a lot of stackspace. |
488 | * not usually need a lot of stackspace. |
482 | */ |
489 | */ |
483 | STATIC void |
490 | STATIC void |
484 | coro_init_stacks (pTHX) |
491 | coro_init_stacks (pTHX) |
485 | { |
492 | { |
|
|
493 | LOCK; |
|
|
494 | |
486 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
495 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
487 | PL_curstackinfo->si_type = PERLSI_MAIN; |
496 | PL_curstackinfo->si_type = PERLSI_MAIN; |
488 | PL_curstack = PL_curstackinfo->si_stack; |
497 | PL_curstack = PL_curstackinfo->si_stack; |
489 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
498 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
490 | |
499 | |
… | |
… | |
514 | PL_savestack_max = 96; |
523 | PL_savestack_max = 96; |
515 | |
524 | |
516 | New(54,PL_retstack,8,OP*); |
525 | New(54,PL_retstack,8,OP*); |
517 | PL_retstack_ix = 0; |
526 | PL_retstack_ix = 0; |
518 | PL_retstack_max = 8; |
527 | PL_retstack_max = 8; |
|
|
528 | |
|
|
529 | UNLOCK; |
519 | } |
530 | } |
520 | |
531 | |
521 | /* |
532 | /* |
522 | * destroy the stacks, the callchain etc... |
533 | * destroy the stacks, the callchain etc... |
523 | * still there is a memleak of 128 bytes... |
|
|
524 | */ |
534 | */ |
525 | STATIC void |
535 | STATIC void |
526 | destroy_stacks(pTHX) |
536 | destroy_stacks(pTHX) |
527 | { |
537 | { |
528 | if (!IN_DESTRUCT) |
538 | if (!IN_DESTRUCT) |
529 | { |
539 | { |
530 | /* is this ugly, I ask? */ |
540 | /* is this ugly, I ask? */ |
531 | while (PL_scopestack_ix) |
541 | LEAVE_SCOPE (0); |
532 | LEAVE; |
|
|
533 | |
542 | |
534 | /* sure it is, but more important: is it correct?? :/ */ |
543 | /* sure it is, but more important: is it correct?? :/ */ |
535 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
|
|
536 | FREETMPS; |
544 | FREETMPS; |
|
|
545 | |
|
|
546 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
537 | } |
547 | } |
538 | |
548 | |
539 | while (PL_curstackinfo->si_next) |
549 | while (PL_curstackinfo->si_next) |
540 | PL_curstackinfo = PL_curstackinfo->si_next; |
550 | PL_curstackinfo = PL_curstackinfo->si_next; |
541 | |
551 | |
542 | while (PL_curstackinfo) |
552 | while (PL_curstackinfo) |
543 | { |
553 | { |
544 | PERL_SI *p = PL_curstackinfo->si_prev; |
554 | PERL_SI *p = PL_curstackinfo->si_prev; |
545 | |
555 | |
546 | { |
556 | { /*D*//*remove*/ |
547 | dSP; |
557 | dSP; |
548 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
558 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
549 | PUTBACK; /* possibly superfluous */ |
559 | PUTBACK; /* possibly superfluous */ |
550 | } |
560 | } |
551 | |
561 | |
552 | if (!IN_DESTRUCT) |
562 | if (!IN_DESTRUCT) |
553 | { |
563 | { |
554 | dounwind(-1); |
564 | dounwind (-1);/*D*//*remove*/ |
555 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
565 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
556 | } |
566 | } |
557 | |
567 | |
558 | Safefree(PL_curstackinfo->si_cxstack); |
568 | Safefree (PL_curstackinfo->si_cxstack); |
559 | Safefree(PL_curstackinfo); |
569 | Safefree (PL_curstackinfo); |
560 | PL_curstackinfo = p; |
570 | PL_curstackinfo = p; |
561 | } |
571 | } |
562 | |
572 | |
563 | Safefree(PL_tmps_stack); |
573 | Safefree (PL_tmps_stack); |
564 | Safefree(PL_markstack); |
574 | Safefree (PL_markstack); |
565 | Safefree(PL_scopestack); |
575 | Safefree (PL_scopestack); |
566 | Safefree(PL_savestack); |
576 | Safefree (PL_savestack); |
567 | Safefree(PL_retstack); |
577 | Safefree (PL_retstack); |
568 | } |
578 | } |
569 | |
579 | |
570 | static void |
580 | static void |
571 | allocate_stack (Coro__State ctx, int alloc) |
581 | allocate_stack (Coro__State ctx, int alloc) |
572 | { |
582 | { |
… | |
… | |
575 | New (0, stack, 1, coro_stack); |
585 | New (0, stack, 1, coro_stack); |
576 | |
586 | |
577 | stack->refcnt = 1; |
587 | stack->refcnt = 1; |
578 | stack->usecnt = 1; |
588 | stack->usecnt = 1; |
579 | stack->gencnt = ctx->gencnt = 0; |
589 | stack->gencnt = ctx->gencnt = 0; |
|
|
590 | |
580 | if (alloc) |
591 | if (alloc) |
581 | { |
592 | { |
582 | #if HAVE_MMAP |
593 | #if HAVE_MMAP |
583 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
594 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
584 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
595 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
585 | if (stack->sptr == (void *)-1) |
596 | if (stack->sptr == (void *)-1) |
586 | #endif |
597 | #endif |
587 | { |
598 | { |
588 | /*FIXME*//*D*//* reasonable stack size! */ |
599 | /*FIXME*//*D*//* reasonable stack size! */ |
589 | stack->ssize = -4096 * sizeof (long); |
600 | stack->ssize = - (STACKSIZE * sizeof (long)); |
590 | New (0, stack->sptr, 4096, long); |
601 | New (0, stack->sptr, STACKSIZE, long); |
591 | } |
602 | } |
592 | } |
603 | } |
593 | else |
604 | else |
594 | stack->sptr = 0; |
605 | stack->sptr = 0; |
595 | |
606 | |
… | |
… | |
609 | { |
620 | { |
610 | #ifdef HAVE_MMAP |
621 | #ifdef HAVE_MMAP |
611 | if (stack->ssize > 0 && stack->sptr) |
622 | if (stack->ssize > 0 && stack->sptr) |
612 | munmap (stack->sptr, stack->ssize); |
623 | munmap (stack->sptr, stack->ssize); |
613 | else |
624 | else |
614 | #else |
625 | #endif |
615 | Safefree (stack->sptr); |
626 | Safefree (stack->sptr); |
616 | #endif |
627 | |
617 | Safefree (stack); |
628 | Safefree (stack); |
618 | } |
629 | } |
619 | else if (ctx->gencnt == stack->gencnt) |
630 | else if (ctx->gencnt == stack->gencnt) |
620 | --stack->usecnt; |
631 | --stack->usecnt; |
621 | } |
632 | } |
… | |
… | |
625 | setup_coro (void *arg) |
636 | setup_coro (void *arg) |
626 | { |
637 | { |
627 | /* |
638 | /* |
628 | * emulate part of the perl startup here. |
639 | * emulate part of the perl startup here. |
629 | */ |
640 | */ |
|
|
641 | dTHX; |
630 | dSP; |
642 | dSP; |
631 | Coro__State ctx = (Coro__State)arg; |
643 | Coro__State ctx = (Coro__State)arg; |
632 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
644 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
633 | |
645 | |
634 | coro_init_stacks (aTHX); |
646 | coro_init_stacks (aTHX); |
635 | /*PL_curcop = 0;*/ |
647 | /*PL_curcop = 0;*/ |
636 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
648 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
637 | SvREFCNT_dec (GvAV (PL_defgv)); |
649 | SvREFCNT_dec (GvAV (PL_defgv)); |
638 | GvAV (PL_defgv) = ctx->args; |
650 | GvAV (PL_defgv) = ctx->args; ctx->args = 0; |
639 | |
651 | |
640 | SPAGAIN; |
652 | SPAGAIN; |
641 | |
653 | |
642 | if (ctx->stack) |
654 | if (ctx->stack) |
643 | { |
655 | { |
… | |
… | |
683 | continue_coro (void *arg) |
695 | continue_coro (void *arg) |
684 | { |
696 | { |
685 | /* |
697 | /* |
686 | * this is a _very_ stripped down perl interpreter ;) |
698 | * this is a _very_ stripped down perl interpreter ;) |
687 | */ |
699 | */ |
|
|
700 | dTHX; |
688 | Coro__State ctx = (Coro__State)arg; |
701 | Coro__State ctx = (Coro__State)arg; |
689 | JMPENV coro_start_env; |
702 | JMPENV coro_start_env; |
690 | |
703 | |
691 | PL_top_env = &ctx->start_env; |
704 | PL_top_env = &ctx->start_env; |
692 | |
705 | |
… | |
… | |
696 | |
709 | |
697 | abort (); |
710 | abort (); |
698 | } |
711 | } |
699 | |
712 | |
700 | STATIC void |
713 | STATIC void |
701 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
714 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
702 | { |
715 | { |
703 | dSTACKLEVEL; |
716 | dSTACKLEVEL; |
704 | static struct coro *xnext; |
|
|
705 | |
717 | |
706 | if (prev != next) |
718 | if (prev != next) |
707 | { |
719 | { |
708 | xnext = next; |
|
|
709 | |
|
|
710 | if (next->mainstack) |
720 | if (next->mainstack) |
711 | { |
721 | { |
|
|
722 | LOCK; |
712 | SAVE (prev, flags); |
723 | SAVE (prev, flags); |
713 | LOAD (next); |
724 | LOAD (next); |
|
|
725 | UNLOCK; |
714 | |
726 | |
715 | /* mark this state as in-use */ |
727 | /* mark this state as in-use */ |
716 | next->mainstack = 0; |
728 | next->mainstack = 0; |
717 | next->tmps_ix = -2; |
729 | next->tmps_ix = -2; |
718 | |
730 | |
… | |
… | |
737 | continue_coro, (void *)next, |
749 | continue_coro, (void *)next, |
738 | next->stack->sptr, labs (next->stack->ssize)); |
750 | next->stack->sptr, labs (next->stack->ssize)); |
739 | } |
751 | } |
740 | |
752 | |
741 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
753 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
754 | prev->cursp = stacklevel; |
742 | /* don't add any code here */ |
755 | /* don't add any code here */ |
743 | } |
756 | } |
744 | |
757 | else |
|
|
758 | next->cursp = stacklevel; |
745 | } |
759 | } |
746 | else if (next->tmps_ix == -2) |
760 | else if (next->tmps_ix == -2) |
747 | croak ("tried to transfer to running coroutine"); |
761 | croak ("tried to transfer to running coroutine"); |
748 | else |
762 | else |
749 | { |
763 | { |
|
|
764 | LOCK; |
750 | SAVE (prev, -1); /* first get rid of the old state */ |
765 | SAVE (prev, -1); /* first get rid of the old state */ |
|
|
766 | UNLOCK; |
751 | |
767 | |
752 | if (flags & TRANSFER_SAVE_CCTXT) |
768 | if (flags & TRANSFER_SAVE_CCTXT) |
753 | { |
769 | { |
754 | if (!prev->stack) |
770 | if (!prev->stack) |
755 | allocate_stack (prev, 0); |
771 | allocate_stack (prev, 0); |
… | |
… | |
757 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
773 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
758 | { |
774 | { |
759 | PL_top_env = &next->start_env; |
775 | PL_top_env = &next->start_env; |
760 | |
776 | |
761 | setup_coro (next); |
777 | setup_coro (next); |
|
|
778 | next->cursp = stacklevel; |
762 | |
779 | |
763 | prev->stack->refcnt++; |
780 | prev->stack->refcnt++; |
764 | prev->stack->usecnt++; |
781 | prev->stack->usecnt++; |
765 | next->stack = prev->stack; |
782 | next->stack = prev->stack; |
766 | next->gencnt = prev->gencnt; |
783 | next->gencnt = prev->gencnt; |
767 | } |
784 | } |
768 | else |
785 | else |
769 | { |
786 | { |
|
|
787 | assert (!next->stack); |
770 | allocate_stack (next, 1); |
788 | allocate_stack (next, 1); |
771 | coro_create (&(next->stack->cctx), |
789 | coro_create (&(next->stack->cctx), |
772 | setup_coro, (void *)next, |
790 | setup_coro, (void *)next, |
773 | next->stack->sptr, labs (next->stack->ssize)); |
791 | next->stack->sptr, labs (next->stack->ssize)); |
774 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
792 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
793 | prev->cursp = stacklevel; |
775 | /* don't add any code here */ |
794 | /* don't add any code here */ |
776 | } |
795 | } |
777 | } |
796 | } |
778 | else |
797 | else |
|
|
798 | { |
779 | setup_coro (next); |
799 | setup_coro (next); |
|
|
800 | next->cursp = stacklevel; |
|
|
801 | } |
780 | } |
802 | } |
781 | |
|
|
782 | /* |
|
|
783 | * xnext is now either prev or next, depending on wether |
|
|
784 | * we switched the c stack or not. that's why I use a global |
|
|
785 | * variable, that should become thread-specific at one point. |
|
|
786 | */ |
|
|
787 | xnext->cursp = stacklevel; |
|
|
788 | } |
803 | } |
789 | |
804 | |
|
|
805 | LOCK; |
790 | if (coro_mortal) |
806 | if (coro_mortal) |
791 | { |
807 | { |
792 | SvREFCNT_dec (coro_mortal); |
808 | SvREFCNT_dec (coro_mortal); |
793 | coro_mortal = 0; |
809 | coro_mortal = 0; |
794 | } |
810 | } |
|
|
811 | UNLOCK; |
795 | } |
812 | } |
796 | |
813 | |
797 | #define SV_CORO(sv,func) \ |
814 | #define SV_CORO(sv,func) \ |
798 | do { \ |
815 | do { \ |
799 | if (SvROK (sv)) \ |
816 | if (SvROK (sv)) \ |
800 | sv = SvRV (sv); \ |
817 | sv = SvRV (sv); \ |
801 | \ |
818 | \ |
802 | if (SvTYPE(sv) == SVt_PVHV) \ |
819 | if (SvTYPE (sv) == SVt_PVHV) \ |
803 | { \ |
820 | { \ |
804 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
821 | HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
805 | \ |
822 | \ |
806 | if (!he) \ |
823 | if (!he) \ |
807 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
824 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
808 | \ |
825 | \ |
809 | (sv) = SvRV (HeVAL(he)); \ |
826 | (sv) = SvRV (HeVAL(he)); \ |
810 | } \ |
827 | } \ |
811 | \ |
828 | \ |
812 | /* must also be changed inside Coro::Cont::yield */ \ |
829 | /* must also be changed inside Coro::Cont::yield */ \ |
813 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
830 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
814 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
831 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
815 | \ |
832 | \ |
816 | } while(0) |
833 | } while(0) |
817 | |
834 | |
818 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
835 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
… | |
… | |
821 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
838 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
822 | { |
839 | { |
823 | SV_CORO (prev, "Coro::transfer"); |
840 | SV_CORO (prev, "Coro::transfer"); |
824 | SV_CORO (next, "Coro::transfer"); |
841 | SV_CORO (next, "Coro::transfer"); |
825 | |
842 | |
826 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
843 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
827 | } |
844 | } |
828 | |
845 | |
829 | /** Coro ********************************************************************/ |
846 | /** Coro ********************************************************************/ |
830 | |
847 | |
831 | #define PRIO_MAX 3 |
848 | #define PRIO_MAX 3 |
… | |
… | |
839 | static GV *coro_current, *coro_idle; |
856 | static GV *coro_current, *coro_idle; |
840 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
857 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
841 | static int coro_nready; |
858 | static int coro_nready; |
842 | |
859 | |
843 | static void |
860 | static void |
844 | coro_enq (SV *sv) |
861 | coro_enq (pTHX_ SV *sv) |
845 | { |
862 | { |
846 | if (SvTYPE (sv) == SVt_PVHV) |
863 | if (SvTYPE (sv) == SVt_PVHV) |
847 | { |
864 | { |
848 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
865 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
849 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
866 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
… | |
… | |
860 | |
877 | |
861 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
878 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
862 | } |
879 | } |
863 | |
880 | |
864 | static SV * |
881 | static SV * |
865 | coro_deq (int min_prio) |
882 | coro_deq (pTHX_ int min_prio) |
866 | { |
883 | { |
867 | int prio = PRIO_MAX - PRIO_MIN; |
884 | int prio = PRIO_MAX - PRIO_MIN; |
868 | |
885 | |
869 | min_prio -= PRIO_MIN; |
886 | min_prio -= PRIO_MIN; |
870 | if (min_prio < 0) |
887 | if (min_prio < 0) |
… | |
… | |
881 | } |
898 | } |
882 | |
899 | |
883 | static void |
900 | static void |
884 | api_ready (SV *coro) |
901 | api_ready (SV *coro) |
885 | { |
902 | { |
|
|
903 | dTHX; |
|
|
904 | |
886 | if (SvROK (coro)) |
905 | if (SvROK (coro)) |
887 | coro = SvRV (coro); |
906 | coro = SvRV (coro); |
888 | |
907 | |
|
|
908 | LOCK; |
889 | coro_enq (SvREFCNT_inc (coro)); |
909 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
|
|
910 | UNLOCK; |
890 | } |
911 | } |
891 | |
912 | |
892 | static void |
913 | static void |
893 | api_schedule (void) |
914 | api_schedule (void) |
894 | { |
915 | { |
|
|
916 | dTHX; |
|
|
917 | |
895 | SV *prev, *next; |
918 | SV *prev, *next; |
896 | |
919 | |
|
|
920 | LOCK; |
|
|
921 | |
897 | prev = SvRV (GvSV (coro_current)); |
922 | prev = SvRV (GvSV (coro_current)); |
898 | next = coro_deq (PRIO_MIN); |
923 | next = coro_deq (aTHX_ PRIO_MIN); |
899 | |
924 | |
900 | if (!next) |
925 | if (!next) |
901 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
926 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
902 | |
927 | |
903 | /* free this only after the transfer */ |
928 | /* free this only after the transfer */ |
… | |
… | |
906 | |
931 | |
907 | SvRV (GvSV (coro_current)) = next; |
932 | SvRV (GvSV (coro_current)) = next; |
908 | |
933 | |
909 | SV_CORO (next, "Coro::schedule"); |
934 | SV_CORO (next, "Coro::schedule"); |
910 | |
935 | |
|
|
936 | UNLOCK; |
|
|
937 | |
911 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
938 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
912 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
939 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
913 | } |
940 | } |
914 | |
941 | |
915 | static void |
942 | static void |
916 | api_cede (void) |
943 | api_cede (void) |
917 | { |
944 | { |
|
|
945 | dTHX; |
|
|
946 | |
|
|
947 | LOCK; |
918 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
948 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
949 | UNLOCK; |
919 | |
950 | |
920 | api_schedule (); |
951 | api_schedule (); |
921 | } |
952 | } |
922 | |
953 | |
923 | MODULE = Coro::State PACKAGE = Coro::State |
954 | MODULE = Coro::State PACKAGE = Coro::State |
924 | |
955 | |
925 | PROTOTYPES: ENABLE |
956 | PROTOTYPES: ENABLE |
926 | |
957 | |
927 | BOOT: |
958 | BOOT: |
928 | { /* {} necessary for stoopid perl-5.6.x */ |
959 | { /* {} necessary for stoopid perl-5.6.x */ |
|
|
960 | #ifdef USE_ITHREADS |
|
|
961 | MUTEX_INIT (&coro_mutex); |
|
|
962 | #endif |
|
|
963 | |
929 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
964 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
930 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
965 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
931 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
966 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
932 | |
967 | |
933 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
968 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
934 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
969 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
935 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
970 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
936 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
971 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
937 | |
|
|
938 | if (!padlist_cache) |
|
|
939 | padlist_cache = newHV (); |
|
|
940 | |
972 | |
941 | main_mainstack = PL_mainstack; |
973 | main_mainstack = PL_mainstack; |
942 | |
974 | |
943 | coroapi.ver = CORO_API_VERSION; |
975 | coroapi.ver = CORO_API_VERSION; |
944 | coroapi.transfer = api_transfer; |
976 | coroapi.transfer = api_transfer; |
… | |
… | |
952 | Coro__State coro; |
984 | Coro__State coro; |
953 | |
985 | |
954 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
986 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
955 | croak ("Coro::State::_newprocess expects an arrayref"); |
987 | croak ("Coro::State::_newprocess expects an arrayref"); |
956 | |
988 | |
957 | New (0, coro, 1, struct coro); |
989 | Newz (0, coro, 1, struct coro); |
958 | |
990 | |
959 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
991 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
960 | coro->mainstack = 0; /* actual work is done inside transfer */ |
992 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
961 | coro->stack = 0; |
993 | /*coro->stack = 0;*/ |
962 | |
994 | |
963 | /* same as JMPENV_BOOTSTRAP */ |
995 | /* same as JMPENV_BOOTSTRAP */ |
964 | /* we might be able to recycle start_env, but safe is safe */ |
996 | /* we might be able to recycle start_env, but safe is safe */ |
965 | Zero(&coro->start_env, 1, JMPENV); |
997 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
966 | coro->start_env.je_ret = -1; |
998 | coro->start_env.je_ret = -1; |
967 | coro->start_env.je_mustcatch = TRUE; |
999 | coro->start_env.je_mustcatch = TRUE; |
968 | |
1000 | |
969 | RETVAL = coro; |
1001 | RETVAL = coro; |
970 | OUTPUT: |
1002 | OUTPUT: |
… | |
… | |
991 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1023 | if (coro->mainstack && coro->mainstack != main_mainstack) |
992 | { |
1024 | { |
993 | struct coro temp; |
1025 | struct coro temp; |
994 | |
1026 | |
995 | PUTBACK; |
1027 | PUTBACK; |
996 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1028 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
997 | LOAD(aTHX_ coro); |
1029 | LOAD (aTHX_ coro); |
998 | SPAGAIN; |
1030 | SPAGAIN; |
999 | |
1031 | |
1000 | destroy_stacks (aTHX); |
1032 | destroy_stacks (aTHX); |
1001 | |
1033 | |
1002 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
1034 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
1003 | SPAGAIN; |
1035 | SPAGAIN; |
1004 | |
1036 | |
1005 | coro->mainstack = 0; |
1037 | coro->mainstack = 0; |
1006 | } |
1038 | } |
1007 | |
1039 | |
1008 | deallocate_stack (coro); |
1040 | deallocate_stack (coro); |
1009 | |
1041 | SvREFCNT_dec (coro->args); |
1010 | Safefree (coro); |
1042 | Safefree (coro); |
1011 | |
|
|
1012 | void |
|
|
1013 | flush() |
|
|
1014 | CODE: |
|
|
1015 | #ifdef MAY_FLUSH |
|
|
1016 | flush_padlist_cache (); |
|
|
1017 | #endif |
|
|
1018 | |
1043 | |
1019 | void |
1044 | void |
1020 | _exit(code) |
1045 | _exit(code) |
1021 | int code |
1046 | int code |
1022 | PROTOTYPE: $ |
1047 | PROTOTYPE: $ |
1023 | CODE: |
1048 | CODE: |
1024 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
1025 | _exit (code); |
1049 | _exit (code); |
1026 | #else |
|
|
1027 | signal (SIGTERM, SIG_DFL); |
|
|
1028 | raise (SIGTERM); |
|
|
1029 | exit (code); |
|
|
1030 | #endif |
|
|
1031 | |
1050 | |
1032 | MODULE = Coro::State PACKAGE = Coro::Cont |
1051 | MODULE = Coro::State PACKAGE = Coro::Cont |
1033 | |
1052 | |
1034 | # this is slightly dirty (should expose a c-level api) |
1053 | # this is slightly dirty (should expose a c-level api) |
1035 | |
1054 | |
1036 | void |
1055 | void |
1037 | yield(...) |
1056 | yield(...) |
1038 | PROTOTYPE: @ |
1057 | PROTOTYPE: @ |
1039 | CODE: |
1058 | CODE: |
1040 | static SV *returnstk; |
1059 | SV *yieldstack; |
1041 | SV *sv; |
1060 | SV *sv; |
1042 | AV *defav = GvAV (PL_defgv); |
1061 | AV *defav = GvAV (PL_defgv); |
1043 | struct coro *prev, *next; |
1062 | struct coro *prev, *next; |
1044 | |
1063 | |
1045 | if (!returnstk) |
1064 | yieldstack = *hv_fetch ( |
1046 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
1065 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1066 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1067 | 0 |
|
|
1068 | ); |
1047 | |
1069 | |
1048 | /* set up @_ -- ugly */ |
1070 | /* set up @_ -- ugly */ |
1049 | av_clear (defav); |
1071 | av_clear (defav); |
1050 | av_fill (defav, items - 1); |
1072 | av_fill (defav, items - 1); |
1051 | while (items--) |
1073 | while (items--) |
1052 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1074 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1053 | |
1075 | |
1054 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
|
|
1055 | sv = av_pop ((AV *)SvRV (returnstk)); |
1076 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1056 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1077 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1057 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1078 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1058 | SvREFCNT_dec (sv); |
1079 | SvREFCNT_dec (sv); |
1059 | |
1080 | |
1060 | transfer(aTHX_ prev, next, 0); |
1081 | transfer (aTHX_ prev, next, 0); |
1061 | |
1082 | |
1062 | MODULE = Coro::State PACKAGE = Coro |
1083 | MODULE = Coro::State PACKAGE = Coro |
1063 | |
1084 | |
1064 | # this is slightly dirty (should expose a c-level api) |
1085 | # this is slightly dirty (should expose a c-level api) |
1065 | |
1086 | |
… | |
… | |
1094 | sv_setiv(sv, (IV)&coroapi); |
1115 | sv_setiv(sv, (IV)&coroapi); |
1095 | SvREADONLY_on(sv); |
1116 | SvREADONLY_on(sv); |
1096 | } |
1117 | } |
1097 | } |
1118 | } |
1098 | |
1119 | |
|
|
1120 | #if !PERL_MICRO |
|
|
1121 | |
1099 | void |
1122 | void |
1100 | ready(self) |
1123 | ready(self) |
1101 | SV * self |
1124 | SV * self |
1102 | PROTOTYPE: $ |
1125 | PROTOTYPE: $ |
1103 | CODE: |
1126 | CODE: |
1104 | api_ready (self); |
1127 | api_ready (self); |
|
|
1128 | |
|
|
1129 | #endif |
1105 | |
1130 | |
1106 | int |
1131 | int |
1107 | nready(...) |
1132 | nready(...) |
1108 | PROTOTYPE: |
1133 | PROTOTYPE: |
1109 | CODE: |
1134 | CODE: |