1 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
|
|
2 | |
|
|
3 | #define PERL_NO_GET_CONTEXT |
2 | |
4 | |
3 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
4 | #include "perl.h" |
6 | #include "perl.h" |
5 | #include "XSUB.h" |
7 | #include "XSUB.h" |
6 | |
8 | |
… | |
… | |
86 | /* prefer perl internal functions over our own? */ |
88 | /* prefer perl internal functions over our own? */ |
87 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
88 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
89 | #endif |
91 | #endif |
90 | |
92 | |
91 | /* The next macro should declare a variable stacklevel that contains and approximation |
93 | /* The next macros try to return the current stack pointer, in an as |
92 | * to the current C stack pointer. Its property is that it changes with each call |
94 | * portable way as possible. */ |
93 | * and should be unique. */ |
|
|
94 | #define dSTACKLEVEL int stacklevel |
95 | #define dSTACKLEVEL volatile char stacklevel |
95 | #define STACKLEVEL ((void *)&stacklevel) |
96 | #define STACKLEVEL ((void *)&stacklevel) |
96 | |
97 | |
97 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
98 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
98 | |
99 | |
99 | #if __GNUC__ >= 3 |
100 | #if __GNUC__ >= 3 |
… | |
… | |
124 | I32 laststype; |
125 | I32 laststype; |
125 | int laststatval; |
126 | int laststatval; |
126 | Stat_t statcache; |
127 | Stat_t statcache; |
127 | }; |
128 | }; |
128 | |
129 | |
|
|
130 | static size_t coro_stacksize = CORO_STACKSIZE; |
129 | static struct CoroAPI coroapi; |
131 | static struct CoroAPI coroapi; |
130 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
132 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
133 | static JMPENV *main_top_env; |
131 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
132 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
133 | |
136 | |
|
|
137 | /* async_pool helper stuff */ |
|
|
138 | static SV *sv_pool_rss; |
|
|
139 | static SV *sv_pool_size; |
|
|
140 | static AV *av_async_pool; |
|
|
141 | |
134 | static struct coro_cctx *cctx_first; |
142 | static struct coro_cctx *cctx_first; |
135 | static int cctx_count, cctx_idle; |
143 | static int cctx_count, cctx_idle; |
|
|
144 | |
|
|
145 | enum { |
|
|
146 | CC_MAPPED = 0x01, |
|
|
147 | CC_NOREUSE = 0x02, /* throw this away after tracing */ |
|
|
148 | CC_TRACE = 0x04, |
|
|
149 | CC_TRACE_SUB = 0x08, /* trace sub calls */ |
|
|
150 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
|
|
151 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
|
|
152 | }; |
136 | |
153 | |
137 | /* this is a structure representing a c-level coroutine */ |
154 | /* this is a structure representing a c-level coroutine */ |
138 | typedef struct coro_cctx { |
155 | typedef struct coro_cctx { |
139 | struct coro_cctx *next; |
156 | struct coro_cctx *next; |
140 | |
157 | |
141 | /* the stack */ |
158 | /* the stack */ |
142 | void *sptr; |
159 | void *sptr; |
143 | ssize_t ssize; /* positive == mmap, otherwise malloc */ |
160 | size_t ssize; |
144 | |
161 | |
145 | /* cpu state */ |
162 | /* cpu state */ |
146 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
163 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
147 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
164 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
148 | JMPENV *top_env; |
165 | JMPENV *top_env; |
149 | coro_context cctx; |
166 | coro_context cctx; |
150 | |
167 | |
151 | int inuse; |
|
|
152 | |
|
|
153 | #if CORO_USE_VALGRIND |
168 | #if CORO_USE_VALGRIND |
154 | int valgrind_id; |
169 | int valgrind_id; |
155 | #endif |
170 | #endif |
|
|
171 | unsigned char flags; |
156 | } coro_cctx; |
172 | } coro_cctx; |
157 | |
173 | |
158 | enum { |
174 | enum { |
159 | CF_RUNNING = 0x0001, /* coroutine is running */ |
175 | CF_RUNNING = 0x0001, /* coroutine is running */ |
160 | CF_READY = 0x0002, /* coroutine is ready */ |
176 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
175 | |
191 | |
176 | /* optionally saved, might be zero */ |
192 | /* optionally saved, might be zero */ |
177 | AV *defav; /* @_ */ |
193 | AV *defav; /* @_ */ |
178 | SV *defsv; /* $_ */ |
194 | SV *defsv; /* $_ */ |
179 | SV *errsv; /* $@ */ |
195 | SV *errsv; /* $@ */ |
|
|
196 | GV *deffh; /* default filehandle */ |
180 | SV *irssv; /* $/ */ |
197 | SV *irssv; /* $/ */ |
181 | SV *irssv_sv; /* real $/ cache */ |
198 | SV *irssv_sv; /* real $/ cache */ |
182 | |
199 | |
183 | #define VAR(name,type) type name; |
200 | #define VAR(name,type) type name; |
184 | # include "state.h" |
201 | # include "state.h" |
185 | #undef VAR |
202 | #undef VAR |
186 | |
203 | |
187 | /* coro process data */ |
204 | /* coro process data */ |
188 | int prio; |
205 | int prio; |
|
|
206 | |
|
|
207 | /* linked list */ |
|
|
208 | struct coro *next, *prev; |
|
|
209 | HV *hv; /* the perl hash associated with this coro, if any */ |
189 | }; |
210 | }; |
190 | |
211 | |
191 | typedef struct coro *Coro__State; |
212 | typedef struct coro *Coro__State; |
192 | typedef struct coro *Coro__State_or_hashref; |
213 | typedef struct coro *Coro__State_or_hashref; |
193 | |
214 | |
… | |
… | |
202 | |
223 | |
203 | /* for Coro.pm */ |
224 | /* for Coro.pm */ |
204 | static SV *coro_current; |
225 | static SV *coro_current; |
205 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
226 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
206 | static int coro_nready; |
227 | static int coro_nready; |
|
|
228 | static struct coro *coro_first; |
207 | |
229 | |
208 | /** lowlevel stuff **********************************************************/ |
230 | /** lowlevel stuff **********************************************************/ |
209 | |
231 | |
210 | static AV * |
232 | static AV * |
211 | coro_clone_padlist (CV *cv) |
233 | coro_clone_padlist (pTHX_ CV *cv) |
212 | { |
234 | { |
213 | AV *padlist = CvPADLIST (cv); |
235 | AV *padlist = CvPADLIST (cv); |
214 | AV *newpadlist, *newpad; |
236 | AV *newpadlist, *newpad; |
215 | |
237 | |
216 | newpadlist = newAV (); |
238 | newpadlist = newAV (); |
… | |
… | |
228 | |
250 | |
229 | return newpadlist; |
251 | return newpadlist; |
230 | } |
252 | } |
231 | |
253 | |
232 | static void |
254 | static void |
233 | free_padlist (AV *padlist) |
255 | free_padlist (pTHX_ AV *padlist) |
234 | { |
256 | { |
235 | /* may be during global destruction */ |
257 | /* may be during global destruction */ |
236 | if (SvREFCNT (padlist)) |
258 | if (SvREFCNT (padlist)) |
237 | { |
259 | { |
238 | I32 i = AvFILLp (padlist); |
260 | I32 i = AvFILLp (padlist); |
… | |
… | |
259 | AV *padlist; |
281 | AV *padlist; |
260 | AV *av = (AV *)mg->mg_obj; |
282 | AV *av = (AV *)mg->mg_obj; |
261 | |
283 | |
262 | /* casting is fun. */ |
284 | /* casting is fun. */ |
263 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
285 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
264 | free_padlist (padlist); |
286 | free_padlist (aTHX_ padlist); |
265 | |
287 | |
266 | SvREFCNT_dec (av); |
288 | SvREFCNT_dec (av); |
267 | |
289 | |
268 | return 0; |
290 | return 0; |
269 | } |
291 | } |
… | |
… | |
277 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
299 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
278 | ? SvMAGIC (cv) \ |
300 | ? SvMAGIC (cv) \ |
279 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
301 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
280 | : 0 |
302 | : 0 |
281 | |
303 | |
|
|
304 | static struct coro * |
|
|
305 | SvSTATE_ (pTHX_ SV *coro) |
|
|
306 | { |
|
|
307 | HV *stash; |
|
|
308 | MAGIC *mg; |
|
|
309 | |
|
|
310 | if (SvROK (coro)) |
|
|
311 | coro = SvRV (coro); |
|
|
312 | |
|
|
313 | stash = SvSTASH (coro); |
|
|
314 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
315 | { |
|
|
316 | /* very slow, but rare, check */ |
|
|
317 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
318 | croak ("Coro::State object required"); |
|
|
319 | } |
|
|
320 | |
|
|
321 | mg = CORO_MAGIC (coro); |
|
|
322 | return (struct coro *)mg->mg_ptr; |
|
|
323 | } |
|
|
324 | |
|
|
325 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
326 | |
282 | /* the next two functions merely cache the padlists */ |
327 | /* the next two functions merely cache the padlists */ |
283 | static void |
328 | static void |
284 | get_padlist (CV *cv) |
329 | get_padlist (pTHX_ CV *cv) |
285 | { |
330 | { |
286 | MAGIC *mg = CORO_MAGIC (cv); |
331 | MAGIC *mg = CORO_MAGIC (cv); |
287 | AV *av; |
332 | AV *av; |
288 | |
333 | |
289 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
334 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
… | |
… | |
295 | CV *cp = Perl_cv_clone (cv); |
340 | CV *cp = Perl_cv_clone (cv); |
296 | CvPADLIST (cv) = CvPADLIST (cp); |
341 | CvPADLIST (cv) = CvPADLIST (cp); |
297 | CvPADLIST (cp) = 0; |
342 | CvPADLIST (cp) = 0; |
298 | SvREFCNT_dec (cp); |
343 | SvREFCNT_dec (cp); |
299 | #else |
344 | #else |
300 | CvPADLIST (cv) = coro_clone_padlist (cv); |
345 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
301 | #endif |
346 | #endif |
302 | } |
347 | } |
303 | } |
348 | } |
304 | |
349 | |
305 | static void |
350 | static void |
306 | put_padlist (CV *cv) |
351 | put_padlist (pTHX_ CV *cv) |
307 | { |
352 | { |
308 | MAGIC *mg = CORO_MAGIC (cv); |
353 | MAGIC *mg = CORO_MAGIC (cv); |
309 | AV *av; |
354 | AV *av; |
310 | |
355 | |
311 | if (!mg) |
356 | if (!mg) |
… | |
… | |
330 | #define SE } while (0) |
375 | #define SE } while (0) |
331 | |
376 | |
332 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
377 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
333 | |
378 | |
334 | static void |
379 | static void |
335 | load_perl (Coro__State c) |
380 | load_perl (pTHX_ Coro__State c) |
336 | { |
381 | { |
337 | #define VAR(name,type) PL_ ## name = c->name; |
382 | #define VAR(name,type) PL_ ## name = c->name; |
338 | # include "state.h" |
383 | # include "state.h" |
339 | #undef VAR |
384 | #undef VAR |
340 | |
385 | |
341 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
386 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
342 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
387 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
343 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
388 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
389 | if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); |
|
|
390 | |
344 | if (c->irssv) |
391 | if (c->irssv) |
345 | { |
392 | { |
346 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
393 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
394 | { |
347 | SvREFCNT_dec (c->irssv); |
395 | SvREFCNT_dec (c->irssv); |
|
|
396 | c->irssv = 0; |
|
|
397 | } |
348 | else |
398 | else |
349 | { |
399 | { |
350 | REPLACE_SV (PL_rs, c->irssv); |
400 | REPLACE_SV (PL_rs, c->irssv); |
351 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
401 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
352 | sv_setsv (c->irssv_sv, PL_rs); |
402 | sv_setsv (c->irssv_sv, PL_rs); |
… | |
… | |
358 | CV *cv; |
408 | CV *cv; |
359 | |
409 | |
360 | /* now do the ugly restore mess */ |
410 | /* now do the ugly restore mess */ |
361 | while ((cv = (CV *)POPs)) |
411 | while ((cv = (CV *)POPs)) |
362 | { |
412 | { |
363 | put_padlist (cv); /* mark this padlist as available */ |
413 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
364 | CvDEPTH (cv) = PTR2IV (POPs); |
414 | CvDEPTH (cv) = PTR2IV (POPs); |
365 | CvPADLIST (cv) = (AV *)POPs; |
415 | CvPADLIST (cv) = (AV *)POPs; |
366 | } |
416 | } |
367 | |
417 | |
368 | PUTBACK; |
418 | PUTBACK; |
369 | } |
419 | } |
370 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
371 | } |
420 | } |
372 | |
421 | |
373 | static void |
422 | static void |
374 | save_perl (Coro__State c) |
423 | save_perl (pTHX_ Coro__State c) |
375 | { |
424 | { |
376 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
377 | { |
425 | { |
378 | dSP; |
426 | dSP; |
379 | I32 cxix = cxstack_ix; |
427 | I32 cxix = cxstack_ix; |
380 | PERL_CONTEXT *ccstk = cxstack; |
428 | PERL_CONTEXT *ccstk = cxstack; |
381 | PERL_SI *top_si = PL_curstackinfo; |
429 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
383 | /* |
431 | /* |
384 | * the worst thing you can imagine happens first - we have to save |
432 | * the worst thing you can imagine happens first - we have to save |
385 | * (and reinitialize) all cv's in the whole callchain :( |
433 | * (and reinitialize) all cv's in the whole callchain :( |
386 | */ |
434 | */ |
387 | |
435 | |
388 | EXTEND (SP, 3 + 1); |
|
|
389 | PUSHs (Nullsv); |
436 | XPUSHs (Nullsv); |
390 | /* this loop was inspired by pp_caller */ |
437 | /* this loop was inspired by pp_caller */ |
391 | for (;;) |
438 | for (;;) |
392 | { |
439 | { |
393 | while (cxix >= 0) |
440 | while (cxix >= 0) |
394 | { |
441 | { |
395 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
442 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
396 | |
443 | |
397 | if (CxTYPE (cx) == CXt_SUB) |
444 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
398 | { |
445 | { |
399 | CV *cv = cx->blk_sub.cv; |
446 | CV *cv = cx->blk_sub.cv; |
400 | |
447 | |
401 | if (CvDEPTH (cv)) |
448 | if (CvDEPTH (cv)) |
402 | { |
449 | { |
… | |
… | |
404 | PUSHs ((SV *)CvPADLIST (cv)); |
451 | PUSHs ((SV *)CvPADLIST (cv)); |
405 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
452 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
406 | PUSHs ((SV *)cv); |
453 | PUSHs ((SV *)cv); |
407 | |
454 | |
408 | CvDEPTH (cv) = 0; |
455 | CvDEPTH (cv) = 0; |
409 | get_padlist (cv); |
456 | get_padlist (aTHX_ cv); |
410 | } |
457 | } |
411 | } |
458 | } |
412 | } |
459 | } |
413 | |
460 | |
414 | if (top_si->si_type == PERLSI_MAIN) |
461 | if (top_si->si_type == PERLSI_MAIN) |
415 | break; |
462 | break; |
416 | |
463 | |
417 | top_si = top_si->si_prev; |
464 | top_si = top_si->si_prev; |
418 | ccstk = top_si->si_cxstack; |
465 | ccstk = top_si->si_cxstack; |
419 | cxix = top_si->si_cxix; |
466 | cxix = top_si->si_cxix; |
420 | } |
467 | } |
421 | |
468 | |
422 | PUTBACK; |
469 | PUTBACK; |
423 | } |
470 | } |
424 | |
471 | |
425 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
472 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
426 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
473 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
427 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
474 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
475 | c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; |
428 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
476 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
429 | |
477 | |
430 | #define VAR(name,type)c->name = PL_ ## name; |
478 | #define VAR(name,type)c->name = PL_ ## name; |
431 | # include "state.h" |
479 | # include "state.h" |
432 | #undef VAR |
480 | #undef VAR |
… | |
… | |
440 | */ |
488 | */ |
441 | #if CORO_PREFER_PERL_FUNCTIONS |
489 | #if CORO_PREFER_PERL_FUNCTIONS |
442 | # define coro_init_stacks init_stacks |
490 | # define coro_init_stacks init_stacks |
443 | #else |
491 | #else |
444 | static void |
492 | static void |
445 | coro_init_stacks () |
493 | coro_init_stacks (pTHX) |
446 | { |
494 | { |
447 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
495 | PL_curstackinfo = new_stackinfo(64, 6); |
448 | PL_curstackinfo->si_type = PERLSI_MAIN; |
496 | PL_curstackinfo->si_type = PERLSI_MAIN; |
449 | PL_curstack = PL_curstackinfo->si_stack; |
497 | PL_curstack = PL_curstackinfo->si_stack; |
450 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
498 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
451 | |
499 | |
452 | PL_stack_base = AvARRAY(PL_curstack); |
500 | PL_stack_base = AvARRAY(PL_curstack); |
453 | PL_stack_sp = PL_stack_base; |
501 | PL_stack_sp = PL_stack_base; |
454 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
502 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
455 | |
503 | |
456 | New(50,PL_tmps_stack,128,SV*); |
504 | New(50,PL_tmps_stack,64,SV*); |
457 | PL_tmps_floor = -1; |
505 | PL_tmps_floor = -1; |
458 | PL_tmps_ix = -1; |
506 | PL_tmps_ix = -1; |
459 | PL_tmps_max = 128; |
507 | PL_tmps_max = 64; |
460 | |
508 | |
461 | New(54,PL_markstack,32,I32); |
509 | New(54,PL_markstack,16,I32); |
462 | PL_markstack_ptr = PL_markstack; |
510 | PL_markstack_ptr = PL_markstack; |
463 | PL_markstack_max = PL_markstack + 32; |
511 | PL_markstack_max = PL_markstack + 16; |
464 | |
512 | |
465 | #ifdef SET_MARK_OFFSET |
513 | #ifdef SET_MARK_OFFSET |
466 | SET_MARK_OFFSET; |
514 | SET_MARK_OFFSET; |
467 | #endif |
515 | #endif |
468 | |
516 | |
469 | New(54,PL_scopestack,32,I32); |
517 | New(54,PL_scopestack,16,I32); |
470 | PL_scopestack_ix = 0; |
518 | PL_scopestack_ix = 0; |
471 | PL_scopestack_max = 32; |
519 | PL_scopestack_max = 16; |
472 | |
520 | |
473 | New(54,PL_savestack,64,ANY); |
521 | New(54,PL_savestack,64,ANY); |
474 | PL_savestack_ix = 0; |
522 | PL_savestack_ix = 0; |
475 | PL_savestack_max = 64; |
523 | PL_savestack_max = 64; |
476 | |
524 | |
477 | #if !PERL_VERSION_ATLEAST (5,9,0) |
525 | #if !PERL_VERSION_ATLEAST (5,9,0) |
478 | New(54,PL_retstack,16,OP*); |
526 | New(54,PL_retstack,4,OP*); |
479 | PL_retstack_ix = 0; |
527 | PL_retstack_ix = 0; |
480 | PL_retstack_max = 16; |
528 | PL_retstack_max = 4; |
481 | #endif |
529 | #endif |
482 | } |
530 | } |
483 | #endif |
531 | #endif |
484 | |
532 | |
485 | /* |
533 | /* |
486 | * destroy the stacks, the callchain etc... |
534 | * destroy the stacks, the callchain etc... |
487 | */ |
535 | */ |
488 | static void |
536 | static void |
489 | coro_destroy_stacks () |
537 | coro_destroy_stacks (pTHX) |
490 | { |
538 | { |
491 | if (!IN_DESTRUCT) |
539 | if (!IN_DESTRUCT) |
492 | { |
540 | { |
493 | /* restore all saved variables and stuff */ |
541 | /* restore all saved variables and stuff */ |
494 | LEAVE_SCOPE (0); |
542 | LEAVE_SCOPE (0); |
… | |
… | |
527 | #if !PERL_VERSION_ATLEAST (5,9,0) |
575 | #if !PERL_VERSION_ATLEAST (5,9,0) |
528 | Safefree (PL_retstack); |
576 | Safefree (PL_retstack); |
529 | #endif |
577 | #endif |
530 | } |
578 | } |
531 | |
579 | |
|
|
580 | static size_t |
|
|
581 | coro_rss (struct coro *coro) |
|
|
582 | { |
|
|
583 | size_t rss = sizeof (coro); |
|
|
584 | |
|
|
585 | if (coro->mainstack) |
|
|
586 | { |
|
|
587 | if (coro->flags & CF_RUNNING) |
|
|
588 | { |
|
|
589 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
590 | # include "state.h" |
|
|
591 | #undef VAR |
|
|
592 | } |
|
|
593 | |
|
|
594 | rss += sizeof (coro->curstackinfo); |
|
|
595 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
596 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
597 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
598 | rss += coro->tmps_max * sizeof (SV *); |
|
|
599 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
600 | rss += coro->scopestack_max * sizeof (I32); |
|
|
601 | rss += coro->savestack_max * sizeof (ANY); |
|
|
602 | |
|
|
603 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
604 | rss += coro->retstack_max * sizeof (OP *); |
|
|
605 | #endif |
|
|
606 | } |
|
|
607 | |
|
|
608 | return rss; |
|
|
609 | } |
|
|
610 | |
532 | /** coroutine stack handling ************************************************/ |
611 | /** coroutine stack handling ************************************************/ |
533 | |
612 | |
534 | static void |
613 | static void |
535 | setup_coro (struct coro *coro) |
614 | setup_coro (pTHX_ struct coro *coro) |
536 | { |
615 | { |
537 | /* |
616 | /* |
538 | * emulate part of the perl startup here. |
617 | * emulate part of the perl startup here. |
539 | */ |
618 | */ |
540 | |
|
|
541 | coro_init_stacks (); |
619 | coro_init_stacks (aTHX); |
542 | |
620 | |
|
|
621 | PL_runops = RUNOPS_DEFAULT; |
543 | PL_curcop = &PL_compiling; |
622 | PL_curcop = &PL_compiling; |
544 | PL_in_eval = EVAL_NULL; |
623 | PL_in_eval = EVAL_NULL; |
545 | PL_comppad = 0; |
624 | PL_comppad = 0; |
546 | PL_curpm = 0; |
625 | PL_curpm = 0; |
547 | PL_localizing = 0; |
626 | PL_localizing = 0; |
… | |
… | |
558 | Zero (&myop, 1, LOGOP); |
637 | Zero (&myop, 1, LOGOP); |
559 | myop.op_next = Nullop; |
638 | myop.op_next = Nullop; |
560 | myop.op_flags = OPf_WANT_VOID; |
639 | myop.op_flags = OPf_WANT_VOID; |
561 | |
640 | |
562 | PUSHMARK (SP); |
641 | PUSHMARK (SP); |
563 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
642 | XPUSHs (av_shift (GvAV (PL_defgv))); |
564 | PUTBACK; |
643 | PUTBACK; |
565 | PL_op = (OP *)&myop; |
644 | PL_op = (OP *)&myop; |
566 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
645 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
567 | SPAGAIN; |
646 | SPAGAIN; |
568 | } |
647 | } |
569 | |
648 | |
570 | ENTER; /* necessary e.g. for dounwind */ |
649 | ENTER; /* necessary e.g. for dounwind */ |
571 | } |
650 | } |
572 | |
651 | |
573 | static void |
652 | static void |
574 | free_coro_mortal () |
653 | free_coro_mortal (pTHX) |
575 | { |
654 | { |
576 | if (coro_mortal) |
655 | if (coro_mortal) |
577 | { |
656 | { |
578 | SvREFCNT_dec (coro_mortal); |
657 | SvREFCNT_dec (coro_mortal); |
579 | coro_mortal = 0; |
658 | coro_mortal = 0; |
580 | } |
659 | } |
581 | } |
660 | } |
582 | |
661 | |
|
|
662 | static int |
|
|
663 | runops_trace (pTHX) |
|
|
664 | { |
|
|
665 | COP *oldcop = 0; |
|
|
666 | int oldcxix = -2; |
|
|
667 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
668 | coro_cctx *cctx = coro->cctx; |
|
|
669 | |
|
|
670 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
671 | { |
|
|
672 | PERL_ASYNC_CHECK (); |
|
|
673 | |
|
|
674 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
|
|
675 | { |
|
|
676 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
677 | SV **bot, **top; |
|
|
678 | AV *av = newAV (); /* return values */ |
|
|
679 | SV **cb; |
|
|
680 | runops_proc_t old_runops = PL_runops; |
|
|
681 | dSP; |
|
|
682 | ENTER; |
|
|
683 | SAVETMPS; |
|
|
684 | EXTEND (SP, 3); |
|
|
685 | PL_runops = RUNOPS_DEFAULT; |
|
|
686 | |
|
|
687 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
688 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
689 | if (isGV (gv)) |
|
|
690 | gv_efullname3 (fullname, gv, 0); |
|
|
691 | |
|
|
692 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
693 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
694 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
695 | : bot; |
|
|
696 | |
|
|
697 | while (bot < top) |
|
|
698 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
699 | |
|
|
700 | PUSHMARK (SP); |
|
|
701 | PUSHs (&PL_sv_no); |
|
|
702 | PUSHs (fullname); |
|
|
703 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
704 | PUTBACK; |
|
|
705 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
706 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
707 | SPAGAIN; |
|
|
708 | |
|
|
709 | FREETMPS; |
|
|
710 | LEAVE; |
|
|
711 | PL_runops = old_runops; |
|
|
712 | } |
|
|
713 | |
|
|
714 | if (oldcop != PL_curcop) |
|
|
715 | { |
|
|
716 | oldcop = PL_curcop; |
|
|
717 | |
|
|
718 | if (PL_curcop != &PL_compiling) |
|
|
719 | { |
|
|
720 | SV **cb; |
|
|
721 | runops_proc_t old_runops = PL_runops; |
|
|
722 | dSP; |
|
|
723 | ENTER; |
|
|
724 | SAVETMPS; |
|
|
725 | EXTEND (SP, 3); |
|
|
726 | PL_runops = RUNOPS_DEFAULT; |
|
|
727 | |
|
|
728 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
|
|
729 | { |
|
|
730 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
731 | |
|
|
732 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
|
|
733 | { |
|
|
734 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
735 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
736 | if (isGV (gv)) |
|
|
737 | gv_efullname3 (fullname, gv, 0); |
|
|
738 | |
|
|
739 | PUSHMARK (SP); |
|
|
740 | PUSHs (&PL_sv_yes); |
|
|
741 | PUSHs (fullname); |
|
|
742 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
743 | PUTBACK; |
|
|
744 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
745 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
746 | SPAGAIN; |
|
|
747 | } |
|
|
748 | |
|
|
749 | oldcxix = cxstack_ix; |
|
|
750 | } |
|
|
751 | |
|
|
752 | if (cctx->flags & CC_TRACE_LINE) |
|
|
753 | { |
|
|
754 | PUSHMARK (SP); |
|
|
755 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
|
|
756 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
|
|
757 | PUTBACK; |
|
|
758 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
|
|
759 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
760 | SPAGAIN; |
|
|
761 | } |
|
|
762 | |
|
|
763 | FREETMPS; |
|
|
764 | LEAVE; |
|
|
765 | PL_runops = old_runops; |
|
|
766 | } |
|
|
767 | } |
|
|
768 | } |
|
|
769 | |
|
|
770 | TAINT_NOT; |
|
|
771 | return 0; |
|
|
772 | } |
|
|
773 | |
583 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
774 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
|
|
775 | /* _cctx_init should be careful, as it could be called at almost any time */ |
|
|
776 | /* during execution of a perl program */ |
584 | static void NOINLINE |
777 | static void NOINLINE |
585 | prepare_cctx (coro_cctx *cctx) |
778 | prepare_cctx (pTHX_ coro_cctx *cctx) |
586 | { |
779 | { |
587 | dSP; |
780 | dSP; |
588 | LOGOP myop; |
781 | LOGOP myop; |
|
|
782 | |
|
|
783 | PL_top_env = &PL_start_env; |
|
|
784 | |
|
|
785 | if (cctx->flags & CC_TRACE) |
|
|
786 | PL_runops = runops_trace; |
589 | |
787 | |
590 | Zero (&myop, 1, LOGOP); |
788 | Zero (&myop, 1, LOGOP); |
591 | myop.op_next = PL_op; |
789 | myop.op_next = PL_op; |
592 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
790 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
593 | |
791 | |
… | |
… | |
599 | PL_op = (OP *)&myop; |
797 | PL_op = (OP *)&myop; |
600 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
798 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
601 | SPAGAIN; |
799 | SPAGAIN; |
602 | } |
800 | } |
603 | |
801 | |
|
|
802 | /* |
|
|
803 | * this is a _very_ stripped down perl interpreter ;) |
|
|
804 | */ |
604 | static void |
805 | static void |
605 | coro_run (void *arg) |
806 | coro_run (void *arg) |
606 | { |
807 | { |
|
|
808 | dTHX; |
|
|
809 | |
607 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
810 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
608 | UNLOCK; |
811 | UNLOCK; |
609 | |
812 | |
610 | /* |
813 | /* we now skip the entersub that lead to transfer() */ |
611 | * this is a _very_ stripped down perl interpreter ;) |
814 | PL_op = PL_op->op_next; |
612 | */ |
|
|
613 | PL_top_env = &PL_start_env; |
|
|
614 | |
815 | |
615 | /* inject call to cctx_init */ |
816 | /* inject a fake subroutine call to cctx_init */ |
616 | prepare_cctx ((coro_cctx *)arg); |
817 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
617 | |
818 | |
618 | /* somebody will hit me for both perl_run and PL_restartop */ |
819 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
619 | PL_restartop = PL_op; |
820 | PL_restartop = PL_op; |
620 | perl_run (PL_curinterp); |
821 | perl_run (PL_curinterp); |
621 | |
822 | |
622 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
823 | /* |
623 | abort (); |
824 | * If perl-run returns we assume exit() was being called or the coro |
|
|
825 | * fell off the end, which seems to be the only valid (non-bug) |
|
|
826 | * reason for perl_run to return. We try to exit by jumping to the |
|
|
827 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
|
|
828 | * coroutine as Coro has no such concept |
|
|
829 | */ |
|
|
830 | PL_top_env = main_top_env; |
|
|
831 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
624 | } |
832 | } |
625 | |
833 | |
626 | static coro_cctx * |
834 | static coro_cctx * |
627 | cctx_new () |
835 | cctx_new () |
628 | { |
836 | { |
629 | coro_cctx *cctx; |
837 | coro_cctx *cctx; |
|
|
838 | void *stack_start; |
|
|
839 | size_t stack_size; |
630 | |
840 | |
631 | ++cctx_count; |
841 | ++cctx_count; |
632 | |
842 | |
633 | Newz (0, cctx, 1, coro_cctx); |
843 | Newz (0, cctx, 1, coro_cctx); |
634 | |
844 | |
635 | #if HAVE_MMAP |
845 | #if HAVE_MMAP |
636 | |
846 | |
637 | cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
847 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
638 | /* mmap supposedly does allocate-on-write for us */ |
848 | /* mmap supposedly does allocate-on-write for us */ |
639 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
849 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
640 | |
850 | |
641 | if (cctx->sptr != (void *)-1) |
851 | if (cctx->sptr != (void *)-1) |
642 | { |
852 | { |
643 | # if CORO_STACKGUARD |
853 | # if CORO_STACKGUARD |
644 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
854 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
645 | # endif |
855 | # endif |
646 | REGISTER_STACK ( |
|
|
647 | cctx, |
|
|
648 | CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
856 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
649 | cctx->ssize + (char *)cctx->sptr |
857 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
650 | ); |
858 | cctx->flags |= CC_MAPPED; |
651 | |
|
|
652 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
|
|
653 | } |
859 | } |
654 | else |
860 | else |
655 | #endif |
861 | #endif |
656 | { |
862 | { |
657 | cctx->ssize = -CORO_STACKSIZE * (long)sizeof (long); |
863 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
658 | New (0, cctx->sptr, CORO_STACKSIZE, long); |
864 | New (0, cctx->sptr, coro_stacksize, long); |
659 | |
865 | |
660 | if (!cctx->sptr) |
866 | if (!cctx->sptr) |
661 | { |
867 | { |
662 | perror ("FATAL: unable to allocate stack for coroutine"); |
868 | perror ("FATAL: unable to allocate stack for coroutine"); |
663 | _exit (EXIT_FAILURE); |
869 | _exit (EXIT_FAILURE); |
664 | } |
870 | } |
665 | |
871 | |
666 | REGISTER_STACK ( |
872 | stack_start = cctx->sptr; |
667 | cctx, |
873 | stack_size = cctx->ssize; |
668 | (char *)cctx->sptr, |
874 | } |
669 | (char *)cctx->sptr - cctx->ssize |
|
|
670 | ); |
|
|
671 | |
875 | |
|
|
876 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
672 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize); |
877 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
673 | } |
|
|
674 | |
878 | |
675 | return cctx; |
879 | return cctx; |
676 | } |
880 | } |
677 | |
881 | |
678 | static void |
882 | static void |
… | |
… | |
686 | #if CORO_USE_VALGRIND |
890 | #if CORO_USE_VALGRIND |
687 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
891 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
688 | #endif |
892 | #endif |
689 | |
893 | |
690 | #if HAVE_MMAP |
894 | #if HAVE_MMAP |
691 | if (cctx->ssize > 0) |
895 | if (cctx->flags & CC_MAPPED) |
692 | munmap (cctx->sptr, cctx->ssize); |
896 | munmap (cctx->sptr, cctx->ssize); |
693 | else |
897 | else |
694 | #endif |
898 | #endif |
695 | Safefree (cctx->sptr); |
899 | Safefree (cctx->sptr); |
696 | |
900 | |
697 | Safefree (cctx); |
901 | Safefree (cctx); |
698 | } |
902 | } |
699 | |
903 | |
700 | static coro_cctx * |
904 | static coro_cctx * |
701 | cctx_get () |
905 | cctx_get (pTHX) |
702 | { |
906 | { |
703 | coro_cctx *cctx; |
|
|
704 | |
|
|
705 | if (cctx_first) |
907 | while (cctx_first) |
706 | { |
908 | { |
707 | cctx = cctx_first; |
909 | coro_cctx *cctx = cctx_first; |
708 | cctx_first = cctx->next; |
910 | cctx_first = cctx->next; |
709 | --cctx_idle; |
911 | --cctx_idle; |
710 | } |
912 | |
711 | else |
913 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
712 | { |
914 | return cctx; |
713 | cctx = cctx_new (); |
915 | |
714 | PL_op = PL_op->op_next; |
916 | cctx_destroy (cctx); |
715 | } |
917 | } |
716 | |
918 | |
717 | return cctx; |
919 | return cctx_new (); |
718 | } |
920 | } |
719 | |
921 | |
720 | static void |
922 | static void |
721 | cctx_put (coro_cctx *cctx) |
923 | cctx_put (coro_cctx *cctx) |
722 | { |
924 | { |
… | |
… | |
725 | { |
927 | { |
726 | coro_cctx *first = cctx_first; |
928 | coro_cctx *first = cctx_first; |
727 | cctx_first = first->next; |
929 | cctx_first = first->next; |
728 | --cctx_idle; |
930 | --cctx_idle; |
729 | |
931 | |
730 | assert (!first->inuse); |
|
|
731 | cctx_destroy (first); |
932 | cctx_destroy (first); |
732 | } |
933 | } |
733 | |
934 | |
734 | ++cctx_idle; |
935 | ++cctx_idle; |
735 | cctx->next = cctx_first; |
936 | cctx->next = cctx_first; |
736 | cctx_first = cctx; |
937 | cctx_first = cctx; |
737 | } |
938 | } |
738 | |
939 | |
739 | /** coroutine switching *****************************************************/ |
940 | /** coroutine switching *****************************************************/ |
740 | |
941 | |
741 | /* never call directly, always through the coro_state_transfer global variable */ |
|
|
742 | static void NOINLINE |
942 | static void NOINLINE |
|
|
943 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
|
|
944 | { |
|
|
945 | if (prev != next) |
|
|
946 | { |
|
|
947 | if (!(prev->flags & (CF_RUNNING | CF_NEW))) |
|
|
948 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
|
|
949 | |
|
|
950 | if (next->flags & CF_RUNNING) |
|
|
951 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
952 | |
|
|
953 | if (next->flags & CF_DESTROYED) |
|
|
954 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
955 | |
|
|
956 | if (PL_lex_state != LEX_NOTPARSING) |
|
|
957 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
|
|
958 | } |
|
|
959 | } |
|
|
960 | |
|
|
961 | /* always use the TRANSFER macro */ |
|
|
962 | static void NOINLINE |
743 | transfer (struct coro *prev, struct coro *next) |
963 | transfer (pTHX_ struct coro *prev, struct coro *next) |
744 | { |
964 | { |
745 | dSTACKLEVEL; |
965 | dSTACKLEVEL; |
746 | |
966 | |
747 | /* sometimes transfer is only called to set idle_sp */ |
967 | /* sometimes transfer is only called to set idle_sp */ |
748 | if (!next) |
968 | if (!next) |
… | |
… | |
756 | |
976 | |
757 | if (prev->flags & CF_NEW) |
977 | if (prev->flags & CF_NEW) |
758 | { |
978 | { |
759 | /* create a new empty context */ |
979 | /* create a new empty context */ |
760 | Newz (0, prev->cctx, 1, coro_cctx); |
980 | Newz (0, prev->cctx, 1, coro_cctx); |
761 | prev->cctx->inuse = 1; |
|
|
762 | prev->flags &= ~CF_NEW; |
981 | prev->flags &= ~CF_NEW; |
763 | prev->flags |= CF_RUNNING; |
982 | prev->flags |= CF_RUNNING; |
764 | } |
983 | } |
765 | |
|
|
766 | /*TODO: must not croak here */ |
|
|
767 | if (!prev->flags & CF_RUNNING) |
|
|
768 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
769 | |
|
|
770 | if (next->flags & CF_RUNNING) |
|
|
771 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
772 | |
|
|
773 | if (next->flags & CF_DESTROYED) |
|
|
774 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
775 | |
984 | |
776 | prev->flags &= ~CF_RUNNING; |
985 | prev->flags &= ~CF_RUNNING; |
777 | next->flags |= CF_RUNNING; |
986 | next->flags |= CF_RUNNING; |
778 | |
987 | |
779 | LOCK; |
988 | LOCK; |
… | |
… | |
781 | if (next->flags & CF_NEW) |
990 | if (next->flags & CF_NEW) |
782 | { |
991 | { |
783 | /* need to start coroutine */ |
992 | /* need to start coroutine */ |
784 | next->flags &= ~CF_NEW; |
993 | next->flags &= ~CF_NEW; |
785 | /* first get rid of the old state */ |
994 | /* first get rid of the old state */ |
786 | save_perl (prev); |
995 | save_perl (aTHX_ prev); |
787 | /* setup coroutine call */ |
996 | /* setup coroutine call */ |
788 | setup_coro (next); |
997 | setup_coro (aTHX_ next); |
789 | /* need a new stack */ |
|
|
790 | assert (!next->cctx); |
|
|
791 | } |
998 | } |
792 | else |
999 | else |
793 | { |
1000 | { |
794 | /* coroutine already started */ |
1001 | /* coroutine already started */ |
795 | save_perl (prev); |
1002 | save_perl (aTHX_ prev); |
796 | load_perl (next); |
1003 | load_perl (aTHX_ next); |
797 | } |
1004 | } |
798 | |
1005 | |
799 | prev__cctx = prev->cctx; |
1006 | prev__cctx = prev->cctx; |
800 | |
1007 | |
801 | /* possibly "free" the cctx */ |
1008 | /* possibly "free" the cctx */ |
802 | if (prev__cctx->idle_sp == STACKLEVEL) |
1009 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
803 | { |
1010 | { |
804 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1011 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
805 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1012 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
806 | |
1013 | |
807 | prev->cctx = 0; |
1014 | prev->cctx = 0; |
808 | |
1015 | |
809 | cctx_put (prev__cctx); |
1016 | cctx_put (prev__cctx); |
810 | prev__cctx->inuse = 0; |
|
|
811 | } |
1017 | } |
812 | |
1018 | |
813 | if (!next->cctx) |
1019 | if (!next->cctx) |
814 | { |
|
|
815 | next->cctx = cctx_get (); |
1020 | next->cctx = cctx_get (aTHX); |
816 | assert (!next->cctx->inuse); |
|
|
817 | next->cctx->inuse = 1; |
|
|
818 | } |
|
|
819 | |
1021 | |
820 | if (prev__cctx != next->cctx) |
1022 | if (prev__cctx != next->cctx) |
821 | { |
1023 | { |
822 | prev__cctx->top_env = PL_top_env; |
1024 | prev__cctx->top_env = PL_top_env; |
823 | PL_top_env = next->cctx->top_env; |
1025 | PL_top_env = next->cctx->top_env; |
824 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1026 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
825 | } |
1027 | } |
826 | |
1028 | |
827 | free_coro_mortal (); |
1029 | free_coro_mortal (aTHX); |
828 | UNLOCK; |
1030 | UNLOCK; |
829 | } |
1031 | } |
830 | } |
1032 | } |
831 | |
1033 | |
832 | struct transfer_args |
1034 | struct transfer_args |
833 | { |
1035 | { |
834 | struct coro *prev, *next; |
1036 | struct coro *prev, *next; |
835 | }; |
1037 | }; |
836 | |
1038 | |
837 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
1039 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
|
|
1040 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
838 | |
1041 | |
839 | /** high level stuff ********************************************************/ |
1042 | /** high level stuff ********************************************************/ |
840 | |
1043 | |
841 | static int |
1044 | static int |
842 | coro_state_destroy (struct coro *coro) |
1045 | coro_state_destroy (pTHX_ struct coro *coro) |
843 | { |
1046 | { |
844 | if (coro->flags & CF_DESTROYED) |
1047 | if (coro->flags & CF_DESTROYED) |
845 | return 0; |
1048 | return 0; |
846 | |
1049 | |
847 | coro->flags |= CF_DESTROYED; |
1050 | coro->flags |= CF_DESTROYED; |
… | |
… | |
867 | temp.save = CORO_SAVE_ALL; |
1070 | temp.save = CORO_SAVE_ALL; |
868 | |
1071 | |
869 | if (coro->flags & CF_RUNNING) |
1072 | if (coro->flags & CF_RUNNING) |
870 | croak ("FATAL: tried to destroy currently running coroutine"); |
1073 | croak ("FATAL: tried to destroy currently running coroutine"); |
871 | |
1074 | |
872 | save_perl (&temp); |
1075 | save_perl (aTHX_ &temp); |
873 | load_perl (coro); |
1076 | load_perl (aTHX_ coro); |
874 | |
1077 | |
875 | coro_destroy_stacks (); |
1078 | coro_destroy_stacks (aTHX); |
876 | |
1079 | |
877 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
1080 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
878 | |
1081 | |
879 | coro->mainstack = 0; |
1082 | coro->mainstack = 0; |
880 | } |
1083 | } |
881 | |
1084 | |
882 | cctx_destroy (coro->cctx); |
1085 | cctx_destroy (coro->cctx); |
883 | SvREFCNT_dec (coro->args); |
1086 | SvREFCNT_dec (coro->args); |
884 | |
1087 | |
|
|
1088 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1089 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1090 | if (coro == coro_first) coro_first = coro->next; |
|
|
1091 | |
885 | return 1; |
1092 | return 1; |
886 | } |
1093 | } |
887 | |
1094 | |
888 | static int |
1095 | static int |
889 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1096 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
890 | { |
1097 | { |
891 | struct coro *coro = (struct coro *)mg->mg_ptr; |
1098 | struct coro *coro = (struct coro *)mg->mg_ptr; |
892 | mg->mg_ptr = 0; |
1099 | mg->mg_ptr = 0; |
893 | |
1100 | |
|
|
1101 | coro->hv = 0; |
|
|
1102 | |
894 | if (--coro->refcnt < 0) |
1103 | if (--coro->refcnt < 0) |
895 | { |
1104 | { |
896 | coro_state_destroy (coro); |
1105 | coro_state_destroy (aTHX_ coro); |
897 | Safefree (coro); |
1106 | Safefree (coro); |
898 | } |
1107 | } |
899 | |
1108 | |
900 | return 0; |
1109 | return 0; |
901 | } |
1110 | } |
… | |
… | |
919 | #else |
1128 | #else |
920 | # define MGf_DUP 0 |
1129 | # define MGf_DUP 0 |
921 | #endif |
1130 | #endif |
922 | }; |
1131 | }; |
923 | |
1132 | |
924 | static struct coro * |
|
|
925 | SvSTATE (SV *coro) |
|
|
926 | { |
|
|
927 | HV *stash; |
|
|
928 | MAGIC *mg; |
|
|
929 | |
|
|
930 | if (SvROK (coro)) |
|
|
931 | coro = SvRV (coro); |
|
|
932 | |
|
|
933 | stash = SvSTASH (coro); |
|
|
934 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
935 | { |
|
|
936 | /* very slow, but rare, check */ |
|
|
937 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
938 | croak ("Coro::State object required"); |
|
|
939 | } |
|
|
940 | |
|
|
941 | mg = CORO_MAGIC (coro); |
|
|
942 | return (struct coro *)mg->mg_ptr; |
|
|
943 | } |
|
|
944 | |
|
|
945 | static void |
1133 | static void |
946 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1134 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
947 | { |
1135 | { |
948 | ta->prev = SvSTATE (prev_sv); |
1136 | ta->prev = SvSTATE (prev_sv); |
949 | ta->next = SvSTATE (next_sv); |
1137 | ta->next = SvSTATE (next_sv); |
|
|
1138 | TRANSFER_CHECK (*ta); |
950 | } |
1139 | } |
951 | |
1140 | |
952 | static void |
1141 | static void |
953 | api_transfer (SV *prev_sv, SV *next_sv) |
1142 | api_transfer (SV *prev_sv, SV *next_sv) |
954 | { |
1143 | { |
|
|
1144 | dTHX; |
955 | struct transfer_args ta; |
1145 | struct transfer_args ta; |
956 | |
1146 | |
957 | prepare_transfer (&ta, prev_sv, next_sv); |
1147 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
958 | TRANSFER (ta); |
1148 | TRANSFER (ta); |
959 | } |
1149 | } |
960 | |
1150 | |
961 | static int |
1151 | static int |
962 | api_save (SV *coro_sv, int new_save) |
1152 | api_save (SV *coro_sv, int new_save) |
963 | { |
1153 | { |
|
|
1154 | dTHX; |
964 | struct coro *coro = SvSTATE (coro_sv); |
1155 | struct coro *coro = SvSTATE (coro_sv); |
965 | int old_save = coro->save; |
1156 | int old_save = coro->save; |
966 | |
1157 | |
967 | if (new_save >= 0) |
1158 | if (new_save >= 0) |
968 | coro->save = new_save; |
1159 | coro->save = new_save; |
… | |
… | |
971 | } |
1162 | } |
972 | |
1163 | |
973 | /** Coro ********************************************************************/ |
1164 | /** Coro ********************************************************************/ |
974 | |
1165 | |
975 | static void |
1166 | static void |
976 | coro_enq (SV *coro_sv) |
1167 | coro_enq (pTHX_ SV *coro_sv) |
977 | { |
1168 | { |
978 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1169 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
979 | } |
1170 | } |
980 | |
1171 | |
981 | static SV * |
1172 | static SV * |
982 | coro_deq (int min_prio) |
1173 | coro_deq (pTHX_ int min_prio) |
983 | { |
1174 | { |
984 | int prio = PRIO_MAX - PRIO_MIN; |
1175 | int prio = PRIO_MAX - PRIO_MIN; |
985 | |
1176 | |
986 | min_prio -= PRIO_MIN; |
1177 | min_prio -= PRIO_MIN; |
987 | if (min_prio < 0) |
1178 | if (min_prio < 0) |
… | |
… | |
995 | } |
1186 | } |
996 | |
1187 | |
997 | static int |
1188 | static int |
998 | api_ready (SV *coro_sv) |
1189 | api_ready (SV *coro_sv) |
999 | { |
1190 | { |
|
|
1191 | dTHX; |
1000 | struct coro *coro; |
1192 | struct coro *coro; |
1001 | |
1193 | |
1002 | if (SvROK (coro_sv)) |
1194 | if (SvROK (coro_sv)) |
1003 | coro_sv = SvRV (coro_sv); |
1195 | coro_sv = SvRV (coro_sv); |
1004 | |
1196 | |
… | |
… | |
1008 | return 0; |
1200 | return 0; |
1009 | |
1201 | |
1010 | coro->flags |= CF_READY; |
1202 | coro->flags |= CF_READY; |
1011 | |
1203 | |
1012 | LOCK; |
1204 | LOCK; |
1013 | coro_enq (SvREFCNT_inc (coro_sv)); |
1205 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1014 | ++coro_nready; |
1206 | ++coro_nready; |
1015 | UNLOCK; |
1207 | UNLOCK; |
1016 | |
1208 | |
1017 | return 1; |
1209 | return 1; |
1018 | } |
1210 | } |
1019 | |
1211 | |
1020 | static int |
1212 | static int |
1021 | api_is_ready (SV *coro_sv) |
1213 | api_is_ready (SV *coro_sv) |
1022 | { |
1214 | { |
|
|
1215 | dTHX; |
1023 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1216 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1024 | } |
1217 | } |
1025 | |
1218 | |
1026 | static void |
1219 | static void |
1027 | prepare_schedule (struct transfer_args *ta) |
1220 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1028 | { |
1221 | { |
1029 | SV *prev_sv, *next_sv; |
1222 | SV *prev_sv, *next_sv; |
1030 | |
1223 | |
1031 | for (;;) |
1224 | for (;;) |
1032 | { |
1225 | { |
1033 | LOCK; |
1226 | LOCK; |
1034 | next_sv = coro_deq (PRIO_MIN); |
1227 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1035 | |
1228 | |
1036 | /* nothing to schedule: call the idle handler */ |
1229 | /* nothing to schedule: call the idle handler */ |
1037 | if (!next_sv) |
1230 | if (!next_sv) |
1038 | { |
1231 | { |
1039 | dSP; |
1232 | dSP; |
… | |
… | |
1067 | break; |
1260 | break; |
1068 | } |
1261 | } |
1069 | |
1262 | |
1070 | /* free this only after the transfer */ |
1263 | /* free this only after the transfer */ |
1071 | prev_sv = SvRV (coro_current); |
1264 | prev_sv = SvRV (coro_current); |
1072 | SvRV_set (coro_current, next_sv); |
|
|
1073 | ta->prev = SvSTATE (prev_sv); |
1265 | ta->prev = SvSTATE (prev_sv); |
1074 | |
1266 | TRANSFER_CHECK (*ta); |
1075 | assert (ta->next->flags & CF_READY); |
1267 | assert (ta->next->flags & CF_READY); |
1076 | ta->next->flags &= ~CF_READY; |
1268 | ta->next->flags &= ~CF_READY; |
|
|
1269 | SvRV_set (coro_current, next_sv); |
1077 | |
1270 | |
1078 | LOCK; |
1271 | LOCK; |
1079 | free_coro_mortal (); |
1272 | free_coro_mortal (aTHX); |
1080 | coro_mortal = prev_sv; |
1273 | coro_mortal = prev_sv; |
1081 | UNLOCK; |
1274 | UNLOCK; |
1082 | } |
1275 | } |
1083 | |
1276 | |
1084 | static void |
1277 | static void |
1085 | prepare_cede (struct transfer_args *ta) |
1278 | prepare_cede (pTHX_ struct transfer_args *ta) |
1086 | { |
1279 | { |
1087 | api_ready (coro_current); |
1280 | api_ready (coro_current); |
1088 | prepare_schedule (ta); |
1281 | prepare_schedule (aTHX_ ta); |
1089 | } |
1282 | } |
1090 | |
1283 | |
1091 | static int |
1284 | static int |
1092 | prepare_cede_notself (struct transfer_args *ta) |
1285 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1093 | { |
1286 | { |
1094 | if (coro_nready) |
1287 | if (coro_nready) |
1095 | { |
1288 | { |
1096 | SV *prev = SvRV (coro_current); |
1289 | SV *prev = SvRV (coro_current); |
1097 | prepare_schedule (ta); |
1290 | prepare_schedule (aTHX_ ta); |
1098 | api_ready (prev); |
1291 | api_ready (prev); |
1099 | return 1; |
1292 | return 1; |
1100 | } |
1293 | } |
1101 | else |
1294 | else |
1102 | return 0; |
1295 | return 0; |
1103 | } |
1296 | } |
1104 | |
1297 | |
1105 | static void |
1298 | static void |
1106 | api_schedule (void) |
1299 | api_schedule (void) |
1107 | { |
1300 | { |
|
|
1301 | dTHX; |
1108 | struct transfer_args ta; |
1302 | struct transfer_args ta; |
1109 | |
1303 | |
1110 | prepare_schedule (&ta); |
1304 | prepare_schedule (aTHX_ &ta); |
1111 | TRANSFER (ta); |
1305 | TRANSFER (ta); |
1112 | } |
1306 | } |
1113 | |
1307 | |
1114 | static int |
1308 | static int |
1115 | api_cede (void) |
1309 | api_cede (void) |
1116 | { |
1310 | { |
|
|
1311 | dTHX; |
1117 | struct transfer_args ta; |
1312 | struct transfer_args ta; |
1118 | |
1313 | |
1119 | prepare_cede (&ta); |
1314 | prepare_cede (aTHX_ &ta); |
1120 | |
1315 | |
1121 | if (ta.prev != ta.next) |
1316 | if (ta.prev != ta.next) |
1122 | { |
1317 | { |
1123 | TRANSFER (ta); |
1318 | TRANSFER (ta); |
1124 | return 1; |
1319 | return 1; |
… | |
… | |
1128 | } |
1323 | } |
1129 | |
1324 | |
1130 | static int |
1325 | static int |
1131 | api_cede_notself (void) |
1326 | api_cede_notself (void) |
1132 | { |
1327 | { |
|
|
1328 | dTHX; |
1133 | struct transfer_args ta; |
1329 | struct transfer_args ta; |
1134 | |
1330 | |
1135 | if (prepare_cede_notself (&ta)) |
1331 | if (prepare_cede_notself (aTHX_ &ta)) |
1136 | { |
1332 | { |
1137 | TRANSFER (ta); |
1333 | TRANSFER (ta); |
1138 | return 1; |
1334 | return 1; |
1139 | } |
1335 | } |
1140 | else |
1336 | else |
… | |
… | |
1151 | MUTEX_INIT (&coro_mutex); |
1347 | MUTEX_INIT (&coro_mutex); |
1152 | #endif |
1348 | #endif |
1153 | BOOT_PAGESIZE; |
1349 | BOOT_PAGESIZE; |
1154 | |
1350 | |
1155 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1351 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
|
|
1352 | |
|
|
1353 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1354 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1355 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1356 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
1156 | |
1357 | |
1157 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1358 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1158 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1359 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1159 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1360 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1160 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1361 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1362 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
|
|
1363 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1161 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1364 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1162 | |
1365 | |
1163 | main_mainstack = PL_mainstack; |
1366 | main_mainstack = PL_mainstack; |
|
|
1367 | main_top_env = PL_top_env; |
|
|
1368 | |
|
|
1369 | while (main_top_env->je_prev) |
|
|
1370 | main_top_env = main_top_env->je_prev; |
1164 | |
1371 | |
1165 | coroapi.ver = CORO_API_VERSION; |
1372 | coroapi.ver = CORO_API_VERSION; |
1166 | coroapi.transfer = api_transfer; |
1373 | coroapi.transfer = api_transfer; |
1167 | |
1374 | |
1168 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1375 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
… | |
… | |
1176 | HV *hv; |
1383 | HV *hv; |
1177 | int i; |
1384 | int i; |
1178 | |
1385 | |
1179 | Newz (0, coro, 1, struct coro); |
1386 | Newz (0, coro, 1, struct coro); |
1180 | coro->args = newAV (); |
1387 | coro->args = newAV (); |
1181 | coro->save = CORO_SAVE_ALL; |
1388 | coro->save = CORO_SAVE_DEF; |
1182 | coro->flags = CF_NEW; |
1389 | coro->flags = CF_NEW; |
1183 | |
1390 | |
|
|
1391 | if (coro_first) coro_first->prev = coro; |
|
|
1392 | coro->next = coro_first; |
|
|
1393 | coro_first = coro; |
|
|
1394 | |
1184 | hv = newHV (); |
1395 | coro->hv = hv = newHV (); |
1185 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1396 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1186 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1397 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1187 | |
1398 | |
1188 | for (i = 1; i < items; i++) |
1399 | for (i = 1; i < items; i++) |
1189 | av_push (coro->args, newSVsv (ST (i))); |
1400 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1193 | |
1404 | |
1194 | int |
1405 | int |
1195 | save (SV *coro, int new_save = -1) |
1406 | save (SV *coro, int new_save = -1) |
1196 | CODE: |
1407 | CODE: |
1197 | RETVAL = api_save (coro, new_save); |
1408 | RETVAL = api_save (coro, new_save); |
|
|
1409 | OUTPUT: |
|
|
1410 | RETVAL |
|
|
1411 | |
|
|
1412 | int |
|
|
1413 | save_also (SV *coro_sv, int save_also) |
|
|
1414 | CODE: |
|
|
1415 | { |
|
|
1416 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1417 | RETVAL = coro->save; |
|
|
1418 | coro->save |= save_also; |
|
|
1419 | } |
1198 | OUTPUT: |
1420 | OUTPUT: |
1199 | RETVAL |
1421 | RETVAL |
1200 | |
1422 | |
1201 | void |
1423 | void |
1202 | _set_stacklevel (...) |
1424 | _set_stacklevel (...) |
… | |
… | |
1218 | |
1440 | |
1219 | case 1: |
1441 | case 1: |
1220 | if (items != 2) |
1442 | if (items != 2) |
1221 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1443 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1222 | |
1444 | |
1223 | prepare_transfer (&ta, ST (0), ST (1)); |
1445 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1224 | break; |
1446 | break; |
1225 | |
1447 | |
1226 | case 2: |
1448 | case 2: |
1227 | prepare_schedule (&ta); |
1449 | prepare_schedule (aTHX_ &ta); |
1228 | break; |
1450 | break; |
1229 | |
1451 | |
1230 | case 3: |
1452 | case 3: |
1231 | prepare_cede (&ta); |
1453 | prepare_cede (aTHX_ &ta); |
1232 | break; |
1454 | break; |
1233 | |
1455 | |
1234 | case 4: |
1456 | case 4: |
1235 | if (!prepare_cede_notself (&ta)) |
1457 | if (!prepare_cede_notself (aTHX_ &ta)) |
1236 | XSRETURN_EMPTY; |
1458 | XSRETURN_EMPTY; |
1237 | |
1459 | |
1238 | break; |
1460 | break; |
1239 | } |
1461 | } |
1240 | |
1462 | |
… | |
… | |
1246 | } |
1468 | } |
1247 | |
1469 | |
1248 | bool |
1470 | bool |
1249 | _destroy (SV *coro_sv) |
1471 | _destroy (SV *coro_sv) |
1250 | CODE: |
1472 | CODE: |
1251 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1473 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1252 | OUTPUT: |
1474 | OUTPUT: |
1253 | RETVAL |
1475 | RETVAL |
1254 | |
1476 | |
1255 | void |
1477 | void |
1256 | _exit (code) |
1478 | _exit (code) |
… | |
… | |
1258 | PROTOTYPE: $ |
1480 | PROTOTYPE: $ |
1259 | CODE: |
1481 | CODE: |
1260 | _exit (code); |
1482 | _exit (code); |
1261 | |
1483 | |
1262 | int |
1484 | int |
|
|
1485 | cctx_stacksize (int new_stacksize = 0) |
|
|
1486 | CODE: |
|
|
1487 | RETVAL = coro_stacksize; |
|
|
1488 | if (new_stacksize) |
|
|
1489 | coro_stacksize = new_stacksize; |
|
|
1490 | OUTPUT: |
|
|
1491 | RETVAL |
|
|
1492 | |
|
|
1493 | int |
1263 | cctx_count () |
1494 | cctx_count () |
1264 | CODE: |
1495 | CODE: |
1265 | RETVAL = cctx_count; |
1496 | RETVAL = cctx_count; |
1266 | OUTPUT: |
1497 | OUTPUT: |
1267 | RETVAL |
1498 | RETVAL |
… | |
… | |
1271 | CODE: |
1502 | CODE: |
1272 | RETVAL = cctx_idle; |
1503 | RETVAL = cctx_idle; |
1273 | OUTPUT: |
1504 | OUTPUT: |
1274 | RETVAL |
1505 | RETVAL |
1275 | |
1506 | |
|
|
1507 | void |
|
|
1508 | list () |
|
|
1509 | PPCODE: |
|
|
1510 | { |
|
|
1511 | struct coro *coro; |
|
|
1512 | for (coro = coro_first; coro; coro = coro->next) |
|
|
1513 | if (coro->hv) |
|
|
1514 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
|
|
1515 | } |
|
|
1516 | |
|
|
1517 | void |
|
|
1518 | call (Coro::State coro, SV *coderef) |
|
|
1519 | ALIAS: |
|
|
1520 | eval = 1 |
|
|
1521 | CODE: |
|
|
1522 | { |
|
|
1523 | if (coro->mainstack) |
|
|
1524 | { |
|
|
1525 | struct coro temp; |
|
|
1526 | Zero (&temp, 1, struct coro); |
|
|
1527 | temp.save = CORO_SAVE_ALL; |
|
|
1528 | |
|
|
1529 | if (!(coro->flags & CF_RUNNING)) |
|
|
1530 | { |
|
|
1531 | save_perl (aTHX_ &temp); |
|
|
1532 | load_perl (aTHX_ coro); |
|
|
1533 | } |
|
|
1534 | |
|
|
1535 | { |
|
|
1536 | dSP; |
|
|
1537 | ENTER; |
|
|
1538 | SAVETMPS; |
|
|
1539 | PUSHMARK (SP); |
|
|
1540 | PUTBACK; |
|
|
1541 | if (ix) |
|
|
1542 | eval_sv (coderef, 0); |
|
|
1543 | else |
|
|
1544 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1545 | SPAGAIN; |
|
|
1546 | FREETMPS; |
|
|
1547 | LEAVE; |
|
|
1548 | PUTBACK; |
|
|
1549 | } |
|
|
1550 | |
|
|
1551 | if (!(coro->flags & CF_RUNNING)) |
|
|
1552 | { |
|
|
1553 | save_perl (aTHX_ coro); |
|
|
1554 | load_perl (aTHX_ &temp); |
|
|
1555 | } |
|
|
1556 | } |
|
|
1557 | } |
|
|
1558 | |
|
|
1559 | SV * |
|
|
1560 | is_ready (Coro::State coro) |
|
|
1561 | PROTOTYPE: $ |
|
|
1562 | ALIAS: |
|
|
1563 | is_ready = CF_READY |
|
|
1564 | is_running = CF_RUNNING |
|
|
1565 | is_new = CF_NEW |
|
|
1566 | is_destroyed = CF_DESTROYED |
|
|
1567 | CODE: |
|
|
1568 | RETVAL = boolSV (coro->flags & ix); |
|
|
1569 | OUTPUT: |
|
|
1570 | RETVAL |
|
|
1571 | |
|
|
1572 | void |
|
|
1573 | trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
1574 | CODE: |
|
|
1575 | if (flags & CC_TRACE) |
|
|
1576 | { |
|
|
1577 | if (!coro->cctx) |
|
|
1578 | coro->cctx = cctx_new (); |
|
|
1579 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1580 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1581 | |
|
|
1582 | coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL); |
|
|
1583 | } |
|
|
1584 | else |
|
|
1585 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1586 | { |
|
|
1587 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1588 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1589 | |
|
|
1590 | if (coro->flags & CF_RUNNING) |
|
|
1591 | PL_runops = RUNOPS_DEFAULT; |
|
|
1592 | else |
|
|
1593 | coro->runops = RUNOPS_DEFAULT; |
|
|
1594 | } |
|
|
1595 | |
|
|
1596 | SV * |
|
|
1597 | has_stack (Coro::State coro) |
|
|
1598 | PROTOTYPE: $ |
|
|
1599 | CODE: |
|
|
1600 | RETVAL = boolSV (!!coro->cctx); |
|
|
1601 | OUTPUT: |
|
|
1602 | RETVAL |
|
|
1603 | |
|
|
1604 | int |
|
|
1605 | is_traced (Coro::State coro) |
|
|
1606 | PROTOTYPE: $ |
|
|
1607 | CODE: |
|
|
1608 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
|
|
1609 | OUTPUT: |
|
|
1610 | RETVAL |
|
|
1611 | |
|
|
1612 | IV |
|
|
1613 | rss (Coro::State coro) |
|
|
1614 | PROTOTYPE: $ |
|
|
1615 | CODE: |
|
|
1616 | RETVAL = coro_rss (coro); |
|
|
1617 | OUTPUT: |
|
|
1618 | RETVAL |
|
|
1619 | |
|
|
1620 | |
1276 | MODULE = Coro::State PACKAGE = Coro |
1621 | MODULE = Coro::State PACKAGE = Coro |
1277 | |
1622 | |
1278 | BOOT: |
1623 | BOOT: |
1279 | { |
1624 | { |
1280 | int i; |
1625 | int i; |
|
|
1626 | |
|
|
1627 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1628 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1629 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1630 | |
|
|
1631 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1632 | SvREADONLY_on (coro_current); |
1281 | |
1633 | |
1282 | coro_stash = gv_stashpv ("Coro", TRUE); |
1634 | coro_stash = gv_stashpv ("Coro", TRUE); |
1283 | |
1635 | |
1284 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1636 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1285 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1637 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1286 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1638 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1287 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1639 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1288 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1640 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1289 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1641 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1290 | |
|
|
1291 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1292 | SvREADONLY_on (coro_current); |
|
|
1293 | |
1642 | |
1294 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1643 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1295 | coro_ready[i] = newAV (); |
1644 | coro_ready[i] = newAV (); |
1296 | |
1645 | |
1297 | { |
1646 | { |
… | |
… | |
1347 | CODE: |
1696 | CODE: |
1348 | RETVAL = boolSV (api_ready (self)); |
1697 | RETVAL = boolSV (api_ready (self)); |
1349 | OUTPUT: |
1698 | OUTPUT: |
1350 | RETVAL |
1699 | RETVAL |
1351 | |
1700 | |
1352 | SV * |
|
|
1353 | is_ready (SV *self) |
|
|
1354 | PROTOTYPE: $ |
|
|
1355 | CODE: |
|
|
1356 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1357 | OUTPUT: |
|
|
1358 | RETVAL |
|
|
1359 | |
|
|
1360 | int |
1701 | int |
1361 | nready (...) |
1702 | nready (...) |
1362 | PROTOTYPE: |
1703 | PROTOTYPE: |
1363 | CODE: |
1704 | CODE: |
1364 | RETVAL = coro_nready; |
1705 | RETVAL = coro_nready; |
1365 | OUTPUT: |
1706 | OUTPUT: |
1366 | RETVAL |
1707 | RETVAL |
1367 | |
1708 | |
|
|
1709 | # for async_pool speedup |
|
|
1710 | void |
|
|
1711 | _pool_1 (SV *cb) |
|
|
1712 | CODE: |
|
|
1713 | { |
|
|
1714 | int i, len; |
|
|
1715 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1716 | AV *defav = GvAV (PL_defgv); |
|
|
1717 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1718 | AV *invoke_av; |
|
|
1719 | |
|
|
1720 | if (!invoke) |
|
|
1721 | croak ("\3terminate\2\n"); |
|
|
1722 | |
|
|
1723 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1724 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1725 | |
|
|
1726 | invoke_av = (AV *)SvRV (invoke); |
|
|
1727 | len = av_len (invoke_av); |
|
|
1728 | |
|
|
1729 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1730 | |
|
|
1731 | if (len > 0) |
|
|
1732 | { |
|
|
1733 | av_fill (defav, len - 1); |
|
|
1734 | for (i = 0; i < len; ++i) |
|
|
1735 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1736 | } |
|
|
1737 | |
|
|
1738 | SvREFCNT_dec (invoke); |
|
|
1739 | } |
|
|
1740 | |
|
|
1741 | void |
|
|
1742 | _pool_2 (SV *cb) |
|
|
1743 | CODE: |
|
|
1744 | { |
|
|
1745 | struct coro *coro = SvSTATE (coro_current); |
|
|
1746 | |
|
|
1747 | sv_setsv (cb, &PL_sv_undef); |
|
|
1748 | |
|
|
1749 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
|
|
1750 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1751 | croak ("\3terminate\2\n"); |
|
|
1752 | |
|
|
1753 | av_clear (GvAV (PL_defgv)); |
|
|
1754 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1755 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1756 | coro->save = CORO_SAVE_DEF; |
|
|
1757 | coro->prio = 0; |
|
|
1758 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1759 | } |
|
|
1760 | |
|
|
1761 | |
1368 | MODULE = Coro::State PACKAGE = Coro::AIO |
1762 | MODULE = Coro::State PACKAGE = Coro::AIO |
1369 | |
1763 | |
1370 | SV * |
1764 | SV * |
1371 | _get_state () |
1765 | _get_state () |
1372 | CODE: |
1766 | CODE: |