… | |
… | |
88 | /* prefer perl internal functions over our own? */ |
88 | /* prefer perl internal functions over our own? */ |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
91 | #endif |
91 | #endif |
92 | |
92 | |
93 | /* 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 |
94 | * to the current C stack pointer. Its property is that it changes with each call |
94 | * portable way as possible. */ |
95 | * and should be unique. */ |
|
|
96 | #define dSTACKLEVEL int stacklevel |
95 | #define dSTACKLEVEL volatile char stacklevel |
97 | #define STACKLEVEL ((void *)&stacklevel) |
96 | #define STACKLEVEL ((void *)&stacklevel) |
98 | |
97 | |
99 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
98 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
100 | |
99 | |
101 | #if __GNUC__ >= 3 |
100 | #if __GNUC__ >= 3 |
… | |
… | |
129 | }; |
128 | }; |
130 | |
129 | |
131 | static size_t coro_stacksize = CORO_STACKSIZE; |
130 | static size_t coro_stacksize = CORO_STACKSIZE; |
132 | static struct CoroAPI coroapi; |
131 | static struct CoroAPI coroapi; |
133 | 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; |
134 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
136 | |
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 | |
137 | static struct coro_cctx *cctx_first; |
142 | static struct coro_cctx *cctx_first; |
138 | 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 | }; |
139 | |
153 | |
140 | /* this is a structure representing a c-level coroutine */ |
154 | /* this is a structure representing a c-level coroutine */ |
141 | typedef struct coro_cctx { |
155 | typedef struct coro_cctx { |
142 | struct coro_cctx *next; |
156 | struct coro_cctx *next; |
143 | |
157 | |
… | |
… | |
152 | coro_context cctx; |
166 | coro_context cctx; |
153 | |
167 | |
154 | #if CORO_USE_VALGRIND |
168 | #if CORO_USE_VALGRIND |
155 | int valgrind_id; |
169 | int valgrind_id; |
156 | #endif |
170 | #endif |
157 | char inuse, mapped; |
171 | unsigned char flags; |
158 | } coro_cctx; |
172 | } coro_cctx; |
159 | |
173 | |
160 | enum { |
174 | enum { |
161 | CF_RUNNING = 0x0001, /* coroutine is running */ |
175 | CF_RUNNING = 0x0001, /* coroutine is running */ |
162 | CF_READY = 0x0002, /* coroutine is ready */ |
176 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
177 | |
191 | |
178 | /* optionally saved, might be zero */ |
192 | /* optionally saved, might be zero */ |
179 | AV *defav; /* @_ */ |
193 | AV *defav; /* @_ */ |
180 | SV *defsv; /* $_ */ |
194 | SV *defsv; /* $_ */ |
181 | SV *errsv; /* $@ */ |
195 | SV *errsv; /* $@ */ |
|
|
196 | GV *deffh; /* default filehandle */ |
182 | SV *irssv; /* $/ */ |
197 | SV *irssv; /* $/ */ |
183 | SV *irssv_sv; /* real $/ cache */ |
198 | SV *irssv_sv; /* real $/ cache */ |
184 | |
199 | |
185 | #define VAR(name,type) type name; |
200 | #define VAR(name,type) type name; |
186 | # include "state.h" |
201 | # include "state.h" |
187 | #undef VAR |
202 | #undef VAR |
188 | |
203 | |
|
|
204 | /* statistics */ |
|
|
205 | int usecount; /* number of switches to this coro */ |
|
|
206 | |
189 | /* coro process data */ |
207 | /* coro process data */ |
190 | int prio; |
208 | int prio; |
|
|
209 | |
|
|
210 | /* linked list */ |
|
|
211 | struct coro *next, *prev; |
|
|
212 | HV *hv; /* the perl hash associated with this coro, if any */ |
191 | }; |
213 | }; |
192 | |
214 | |
193 | typedef struct coro *Coro__State; |
215 | typedef struct coro *Coro__State; |
194 | typedef struct coro *Coro__State_or_hashref; |
216 | typedef struct coro *Coro__State_or_hashref; |
195 | |
217 | |
… | |
… | |
204 | |
226 | |
205 | /* for Coro.pm */ |
227 | /* for Coro.pm */ |
206 | static SV *coro_current; |
228 | static SV *coro_current; |
207 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
229 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
208 | static int coro_nready; |
230 | static int coro_nready; |
|
|
231 | static struct coro *coro_first; |
209 | |
232 | |
210 | /** lowlevel stuff **********************************************************/ |
233 | /** lowlevel stuff **********************************************************/ |
211 | |
234 | |
212 | static AV * |
235 | static AV * |
213 | coro_clone_padlist (pTHX_ CV *cv) |
236 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
278 | SvMAGIC (cv) \ |
301 | SvMAGIC (cv) \ |
279 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
302 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
280 | ? SvMAGIC (cv) \ |
303 | ? SvMAGIC (cv) \ |
281 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
304 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
282 | : 0 |
305 | : 0 |
|
|
306 | |
|
|
307 | static struct coro * |
|
|
308 | SvSTATE_ (pTHX_ SV *coro) |
|
|
309 | { |
|
|
310 | HV *stash; |
|
|
311 | MAGIC *mg; |
|
|
312 | |
|
|
313 | if (SvROK (coro)) |
|
|
314 | coro = SvRV (coro); |
|
|
315 | |
|
|
316 | if (SvTYPE (coro) != SVt_PVHV) |
|
|
317 | croak ("Coro::State object required"); |
|
|
318 | |
|
|
319 | stash = SvSTASH (coro); |
|
|
320 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
321 | { |
|
|
322 | /* very slow, but rare, check */ |
|
|
323 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
324 | croak ("Coro::State object required"); |
|
|
325 | } |
|
|
326 | |
|
|
327 | mg = CORO_MAGIC (coro); |
|
|
328 | return (struct coro *)mg->mg_ptr; |
|
|
329 | } |
|
|
330 | |
|
|
331 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
283 | |
332 | |
284 | /* the next two functions merely cache the padlists */ |
333 | /* the next two functions merely cache the padlists */ |
285 | static void |
334 | static void |
286 | get_padlist (pTHX_ CV *cv) |
335 | get_padlist (pTHX_ CV *cv) |
287 | { |
336 | { |
… | |
… | |
341 | #undef VAR |
390 | #undef VAR |
342 | |
391 | |
343 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
392 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
344 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
393 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
345 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
394 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
395 | if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); |
|
|
396 | |
346 | if (c->irssv) |
397 | if (c->irssv) |
347 | { |
398 | { |
348 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
399 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
400 | { |
349 | SvREFCNT_dec (c->irssv); |
401 | SvREFCNT_dec (c->irssv); |
|
|
402 | c->irssv = 0; |
|
|
403 | } |
350 | else |
404 | else |
351 | { |
405 | { |
352 | REPLACE_SV (PL_rs, c->irssv); |
406 | REPLACE_SV (PL_rs, c->irssv); |
353 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
407 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
354 | sv_setsv (c->irssv_sv, PL_rs); |
408 | sv_setsv (c->irssv_sv, PL_rs); |
… | |
… | |
367 | CvPADLIST (cv) = (AV *)POPs; |
421 | CvPADLIST (cv) = (AV *)POPs; |
368 | } |
422 | } |
369 | |
423 | |
370 | PUTBACK; |
424 | PUTBACK; |
371 | } |
425 | } |
372 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
373 | } |
426 | } |
374 | |
427 | |
375 | static void |
428 | static void |
376 | save_perl (pTHX_ Coro__State c) |
429 | save_perl (pTHX_ Coro__State c) |
377 | { |
430 | { |
378 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
379 | { |
431 | { |
380 | dSP; |
432 | dSP; |
381 | I32 cxix = cxstack_ix; |
433 | I32 cxix = cxstack_ix; |
382 | PERL_CONTEXT *ccstk = cxstack; |
434 | PERL_CONTEXT *ccstk = cxstack; |
383 | PERL_SI *top_si = PL_curstackinfo; |
435 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
385 | /* |
437 | /* |
386 | * the worst thing you can imagine happens first - we have to save |
438 | * the worst thing you can imagine happens first - we have to save |
387 | * (and reinitialize) all cv's in the whole callchain :( |
439 | * (and reinitialize) all cv's in the whole callchain :( |
388 | */ |
440 | */ |
389 | |
441 | |
390 | EXTEND (SP, 3 + 1); |
|
|
391 | PUSHs (Nullsv); |
442 | XPUSHs (Nullsv); |
392 | /* this loop was inspired by pp_caller */ |
443 | /* this loop was inspired by pp_caller */ |
393 | for (;;) |
444 | for (;;) |
394 | { |
445 | { |
395 | while (cxix >= 0) |
446 | while (cxix >= 0) |
396 | { |
447 | { |
397 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
448 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
398 | |
449 | |
399 | if (CxTYPE (cx) == CXt_SUB) |
450 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
400 | { |
451 | { |
401 | CV *cv = cx->blk_sub.cv; |
452 | CV *cv = cx->blk_sub.cv; |
402 | |
453 | |
403 | if (CvDEPTH (cv)) |
454 | if (CvDEPTH (cv)) |
404 | { |
455 | { |
… | |
… | |
415 | |
466 | |
416 | if (top_si->si_type == PERLSI_MAIN) |
467 | if (top_si->si_type == PERLSI_MAIN) |
417 | break; |
468 | break; |
418 | |
469 | |
419 | top_si = top_si->si_prev; |
470 | top_si = top_si->si_prev; |
420 | ccstk = top_si->si_cxstack; |
471 | ccstk = top_si->si_cxstack; |
421 | cxix = top_si->si_cxix; |
472 | cxix = top_si->si_cxix; |
422 | } |
473 | } |
423 | |
474 | |
424 | PUTBACK; |
475 | PUTBACK; |
425 | } |
476 | } |
426 | |
477 | |
427 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
478 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
428 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
479 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
429 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
480 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
481 | c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; |
430 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
482 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
431 | |
483 | |
432 | #define VAR(name,type)c->name = PL_ ## name; |
484 | #define VAR(name,type)c->name = PL_ ## name; |
433 | # include "state.h" |
485 | # include "state.h" |
434 | #undef VAR |
486 | #undef VAR |
… | |
… | |
444 | # define coro_init_stacks init_stacks |
496 | # define coro_init_stacks init_stacks |
445 | #else |
497 | #else |
446 | static void |
498 | static void |
447 | coro_init_stacks (pTHX) |
499 | coro_init_stacks (pTHX) |
448 | { |
500 | { |
449 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
501 | PL_curstackinfo = new_stackinfo(64, 6); |
450 | PL_curstackinfo->si_type = PERLSI_MAIN; |
502 | PL_curstackinfo->si_type = PERLSI_MAIN; |
451 | PL_curstack = PL_curstackinfo->si_stack; |
503 | PL_curstack = PL_curstackinfo->si_stack; |
452 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
504 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
453 | |
505 | |
454 | PL_stack_base = AvARRAY(PL_curstack); |
506 | PL_stack_base = AvARRAY(PL_curstack); |
455 | PL_stack_sp = PL_stack_base; |
507 | PL_stack_sp = PL_stack_base; |
456 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
508 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
457 | |
509 | |
458 | New(50,PL_tmps_stack,128,SV*); |
510 | New(50,PL_tmps_stack,64,SV*); |
459 | PL_tmps_floor = -1; |
511 | PL_tmps_floor = -1; |
460 | PL_tmps_ix = -1; |
512 | PL_tmps_ix = -1; |
461 | PL_tmps_max = 128; |
513 | PL_tmps_max = 64; |
462 | |
514 | |
463 | New(54,PL_markstack,32,I32); |
515 | New(54,PL_markstack,16,I32); |
464 | PL_markstack_ptr = PL_markstack; |
516 | PL_markstack_ptr = PL_markstack; |
465 | PL_markstack_max = PL_markstack + 32; |
517 | PL_markstack_max = PL_markstack + 16; |
466 | |
518 | |
467 | #ifdef SET_MARK_OFFSET |
519 | #ifdef SET_MARK_OFFSET |
468 | SET_MARK_OFFSET; |
520 | SET_MARK_OFFSET; |
469 | #endif |
521 | #endif |
470 | |
522 | |
471 | New(54,PL_scopestack,32,I32); |
523 | New(54,PL_scopestack,16,I32); |
472 | PL_scopestack_ix = 0; |
524 | PL_scopestack_ix = 0; |
473 | PL_scopestack_max = 32; |
525 | PL_scopestack_max = 16; |
474 | |
526 | |
475 | New(54,PL_savestack,64,ANY); |
527 | New(54,PL_savestack,64,ANY); |
476 | PL_savestack_ix = 0; |
528 | PL_savestack_ix = 0; |
477 | PL_savestack_max = 64; |
529 | PL_savestack_max = 64; |
478 | |
530 | |
479 | #if !PERL_VERSION_ATLEAST (5,9,0) |
531 | #if !PERL_VERSION_ATLEAST (5,9,0) |
480 | New(54,PL_retstack,16,OP*); |
532 | New(54,PL_retstack,4,OP*); |
481 | PL_retstack_ix = 0; |
533 | PL_retstack_ix = 0; |
482 | PL_retstack_max = 16; |
534 | PL_retstack_max = 4; |
483 | #endif |
535 | #endif |
484 | } |
536 | } |
485 | #endif |
537 | #endif |
486 | |
538 | |
487 | /* |
539 | /* |
… | |
… | |
529 | #if !PERL_VERSION_ATLEAST (5,9,0) |
581 | #if !PERL_VERSION_ATLEAST (5,9,0) |
530 | Safefree (PL_retstack); |
582 | Safefree (PL_retstack); |
531 | #endif |
583 | #endif |
532 | } |
584 | } |
533 | |
585 | |
|
|
586 | static size_t |
|
|
587 | coro_rss (pTHX_ struct coro *coro) |
|
|
588 | { |
|
|
589 | size_t rss = sizeof (coro); |
|
|
590 | |
|
|
591 | if (coro->mainstack) |
|
|
592 | { |
|
|
593 | if (coro->flags & CF_RUNNING) |
|
|
594 | { |
|
|
595 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
596 | # include "state.h" |
|
|
597 | #undef VAR |
|
|
598 | } |
|
|
599 | |
|
|
600 | rss += sizeof (coro->curstackinfo); |
|
|
601 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
602 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
603 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
604 | rss += coro->tmps_max * sizeof (SV *); |
|
|
605 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
606 | rss += coro->scopestack_max * sizeof (I32); |
|
|
607 | rss += coro->savestack_max * sizeof (ANY); |
|
|
608 | |
|
|
609 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
610 | rss += coro->retstack_max * sizeof (OP *); |
|
|
611 | #endif |
|
|
612 | } |
|
|
613 | |
|
|
614 | return rss; |
|
|
615 | } |
|
|
616 | |
534 | /** coroutine stack handling ************************************************/ |
617 | /** coroutine stack handling ************************************************/ |
535 | |
618 | |
536 | static void |
619 | static void |
537 | setup_coro (pTHX_ struct coro *coro) |
620 | setup_coro (pTHX_ struct coro *coro) |
538 | { |
621 | { |
539 | /* |
622 | /* |
540 | * emulate part of the perl startup here. |
623 | * emulate part of the perl startup here. |
541 | */ |
624 | */ |
542 | coro_init_stacks (aTHX); |
625 | coro_init_stacks (aTHX); |
543 | |
626 | |
|
|
627 | PL_runops = RUNOPS_DEFAULT; |
544 | PL_curcop = &PL_compiling; |
628 | PL_curcop = &PL_compiling; |
545 | PL_in_eval = EVAL_NULL; |
629 | PL_in_eval = EVAL_NULL; |
546 | PL_comppad = 0; |
630 | PL_comppad = 0; |
547 | PL_curpm = 0; |
631 | PL_curpm = 0; |
548 | PL_localizing = 0; |
632 | PL_localizing = 0; |
… | |
… | |
559 | Zero (&myop, 1, LOGOP); |
643 | Zero (&myop, 1, LOGOP); |
560 | myop.op_next = Nullop; |
644 | myop.op_next = Nullop; |
561 | myop.op_flags = OPf_WANT_VOID; |
645 | myop.op_flags = OPf_WANT_VOID; |
562 | |
646 | |
563 | PUSHMARK (SP); |
647 | PUSHMARK (SP); |
564 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
648 | XPUSHs (av_shift (GvAV (PL_defgv))); |
565 | PUTBACK; |
649 | PUTBACK; |
566 | PL_op = (OP *)&myop; |
650 | PL_op = (OP *)&myop; |
567 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
651 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
568 | SPAGAIN; |
652 | SPAGAIN; |
569 | } |
653 | } |
… | |
… | |
579 | SvREFCNT_dec (coro_mortal); |
663 | SvREFCNT_dec (coro_mortal); |
580 | coro_mortal = 0; |
664 | coro_mortal = 0; |
581 | } |
665 | } |
582 | } |
666 | } |
583 | |
667 | |
|
|
668 | static int |
|
|
669 | runops_trace (pTHX) |
|
|
670 | { |
|
|
671 | COP *oldcop = 0; |
|
|
672 | int oldcxix = -2; |
|
|
673 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
674 | coro_cctx *cctx = coro->cctx; |
|
|
675 | |
|
|
676 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
677 | { |
|
|
678 | PERL_ASYNC_CHECK (); |
|
|
679 | |
|
|
680 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
|
|
681 | { |
|
|
682 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
683 | SV **bot, **top; |
|
|
684 | AV *av = newAV (); /* return values */ |
|
|
685 | SV **cb; |
|
|
686 | runops_proc_t old_runops = PL_runops; |
|
|
687 | dSP; |
|
|
688 | ENTER; |
|
|
689 | SAVETMPS; |
|
|
690 | EXTEND (SP, 3); |
|
|
691 | PL_runops = RUNOPS_DEFAULT; |
|
|
692 | |
|
|
693 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
694 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
695 | if (isGV (gv)) |
|
|
696 | gv_efullname3 (fullname, gv, 0); |
|
|
697 | |
|
|
698 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
699 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
700 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
701 | : bot; |
|
|
702 | |
|
|
703 | while (bot < top) |
|
|
704 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
705 | |
|
|
706 | PUSHMARK (SP); |
|
|
707 | PUSHs (&PL_sv_no); |
|
|
708 | PUSHs (fullname); |
|
|
709 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
710 | PUTBACK; |
|
|
711 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
712 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
713 | SPAGAIN; |
|
|
714 | |
|
|
715 | FREETMPS; |
|
|
716 | LEAVE; |
|
|
717 | PL_runops = old_runops; |
|
|
718 | } |
|
|
719 | |
|
|
720 | if (oldcop != PL_curcop) |
|
|
721 | { |
|
|
722 | oldcop = PL_curcop; |
|
|
723 | |
|
|
724 | if (PL_curcop != &PL_compiling) |
|
|
725 | { |
|
|
726 | SV **cb; |
|
|
727 | runops_proc_t old_runops = PL_runops; |
|
|
728 | dSP; |
|
|
729 | ENTER; |
|
|
730 | SAVETMPS; |
|
|
731 | EXTEND (SP, 3); |
|
|
732 | PL_runops = RUNOPS_DEFAULT; |
|
|
733 | |
|
|
734 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
|
|
735 | { |
|
|
736 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
737 | |
|
|
738 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
|
|
739 | { |
|
|
740 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
741 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
742 | if (isGV (gv)) |
|
|
743 | gv_efullname3 (fullname, gv, 0); |
|
|
744 | |
|
|
745 | PUSHMARK (SP); |
|
|
746 | PUSHs (&PL_sv_yes); |
|
|
747 | PUSHs (fullname); |
|
|
748 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
749 | PUTBACK; |
|
|
750 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
751 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
752 | SPAGAIN; |
|
|
753 | } |
|
|
754 | |
|
|
755 | oldcxix = cxstack_ix; |
|
|
756 | } |
|
|
757 | |
|
|
758 | if (cctx->flags & CC_TRACE_LINE) |
|
|
759 | { |
|
|
760 | PUSHMARK (SP); |
|
|
761 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
|
|
762 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
|
|
763 | PUTBACK; |
|
|
764 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
|
|
765 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
766 | SPAGAIN; |
|
|
767 | } |
|
|
768 | |
|
|
769 | FREETMPS; |
|
|
770 | LEAVE; |
|
|
771 | PL_runops = old_runops; |
|
|
772 | } |
|
|
773 | } |
|
|
774 | } |
|
|
775 | |
|
|
776 | TAINT_NOT; |
|
|
777 | return 0; |
|
|
778 | } |
|
|
779 | |
584 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
780 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
|
|
781 | /* _cctx_init should be careful, as it could be called at almost any time */ |
|
|
782 | /* during execution of a perl program */ |
585 | static void NOINLINE |
783 | static void NOINLINE |
586 | prepare_cctx (pTHX_ coro_cctx *cctx) |
784 | prepare_cctx (pTHX_ coro_cctx *cctx) |
587 | { |
785 | { |
588 | dSP; |
786 | dSP; |
589 | LOGOP myop; |
787 | LOGOP myop; |
|
|
788 | |
|
|
789 | PL_top_env = &PL_start_env; |
|
|
790 | |
|
|
791 | if (cctx->flags & CC_TRACE) |
|
|
792 | PL_runops = runops_trace; |
590 | |
793 | |
591 | Zero (&myop, 1, LOGOP); |
794 | Zero (&myop, 1, LOGOP); |
592 | myop.op_next = PL_op; |
795 | myop.op_next = PL_op; |
593 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
796 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
594 | |
797 | |
… | |
… | |
600 | PL_op = (OP *)&myop; |
803 | PL_op = (OP *)&myop; |
601 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
804 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
602 | SPAGAIN; |
805 | SPAGAIN; |
603 | } |
806 | } |
604 | |
807 | |
|
|
808 | /* |
|
|
809 | * this is a _very_ stripped down perl interpreter ;) |
|
|
810 | */ |
605 | static void |
811 | static void |
606 | coro_run (void *arg) |
812 | coro_run (void *arg) |
607 | { |
813 | { |
608 | dTHX; |
814 | dTHX; |
609 | |
815 | |
610 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
816 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
611 | UNLOCK; |
817 | UNLOCK; |
612 | |
818 | |
613 | /* |
819 | /* we now skip the entersub that lead to transfer() */ |
614 | * this is a _very_ stripped down perl interpreter ;) |
820 | PL_op = PL_op->op_next; |
615 | */ |
|
|
616 | PL_top_env = &PL_start_env; |
|
|
617 | |
821 | |
618 | /* inject call to cctx_init */ |
822 | /* inject a fake subroutine call to cctx_init */ |
619 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
823 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
620 | |
824 | |
621 | /* somebody will hit me for both perl_run and PL_restartop */ |
825 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
622 | PL_restartop = PL_op; |
826 | PL_restartop = PL_op; |
623 | perl_run (PL_curinterp); |
827 | perl_run (PL_curinterp); |
624 | |
828 | |
625 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
829 | /* |
626 | abort (); |
830 | * If perl-run returns we assume exit() was being called or the coro |
|
|
831 | * fell off the end, which seems to be the only valid (non-bug) |
|
|
832 | * reason for perl_run to return. We try to exit by jumping to the |
|
|
833 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
|
|
834 | * coroutine as Coro has no such concept |
|
|
835 | */ |
|
|
836 | PL_top_env = main_top_env; |
|
|
837 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
627 | } |
838 | } |
628 | |
839 | |
629 | static coro_cctx * |
840 | static coro_cctx * |
630 | cctx_new () |
841 | cctx_new () |
631 | { |
842 | { |
… | |
… | |
648 | # if CORO_STACKGUARD |
859 | # if CORO_STACKGUARD |
649 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
860 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
650 | # endif |
861 | # endif |
651 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
862 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
652 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
863 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
653 | cctx->mapped = 1; |
864 | cctx->flags |= CC_MAPPED; |
654 | } |
865 | } |
655 | else |
866 | else |
656 | #endif |
867 | #endif |
657 | { |
868 | { |
658 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
869 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
685 | #if CORO_USE_VALGRIND |
896 | #if CORO_USE_VALGRIND |
686 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
897 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
687 | #endif |
898 | #endif |
688 | |
899 | |
689 | #if HAVE_MMAP |
900 | #if HAVE_MMAP |
690 | if (cctx->mapped) |
901 | if (cctx->flags & CC_MAPPED) |
691 | munmap (cctx->sptr, cctx->ssize); |
902 | munmap (cctx->sptr, cctx->ssize); |
692 | else |
903 | else |
693 | #endif |
904 | #endif |
694 | Safefree (cctx->sptr); |
905 | Safefree (cctx->sptr); |
695 | |
906 | |
… | |
… | |
703 | { |
914 | { |
704 | coro_cctx *cctx = cctx_first; |
915 | coro_cctx *cctx = cctx_first; |
705 | cctx_first = cctx->next; |
916 | cctx_first = cctx->next; |
706 | --cctx_idle; |
917 | --cctx_idle; |
707 | |
918 | |
708 | if (cctx->ssize >= coro_stacksize) |
919 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
709 | return cctx; |
920 | return cctx; |
710 | |
921 | |
711 | cctx_destroy (cctx); |
922 | cctx_destroy (cctx); |
712 | } |
923 | } |
713 | |
924 | |
714 | PL_op = PL_op->op_next; |
|
|
715 | return cctx_new (); |
925 | return cctx_new (); |
716 | } |
926 | } |
717 | |
927 | |
718 | static void |
928 | static void |
719 | cctx_put (coro_cctx *cctx) |
929 | cctx_put (coro_cctx *cctx) |
… | |
… | |
723 | { |
933 | { |
724 | coro_cctx *first = cctx_first; |
934 | coro_cctx *first = cctx_first; |
725 | cctx_first = first->next; |
935 | cctx_first = first->next; |
726 | --cctx_idle; |
936 | --cctx_idle; |
727 | |
937 | |
728 | assert (!first->inuse); |
|
|
729 | cctx_destroy (first); |
938 | cctx_destroy (first); |
730 | } |
939 | } |
731 | |
940 | |
732 | ++cctx_idle; |
941 | ++cctx_idle; |
733 | cctx->next = cctx_first; |
942 | cctx->next = cctx_first; |
734 | cctx_first = cctx; |
943 | cctx_first = cctx; |
735 | } |
944 | } |
736 | |
945 | |
737 | /** coroutine switching *****************************************************/ |
946 | /** coroutine switching *****************************************************/ |
738 | |
947 | |
739 | /* never call directly, always through the coro_state_transfer global variable */ |
948 | static void NOINLINE |
|
|
949 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
|
|
950 | { |
|
|
951 | if (prev != next) |
|
|
952 | { |
|
|
953 | if (!(prev->flags & (CF_RUNNING | CF_NEW))) |
|
|
954 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
|
|
955 | |
|
|
956 | if (next->flags & CF_RUNNING) |
|
|
957 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
958 | |
|
|
959 | if (next->flags & CF_DESTROYED) |
|
|
960 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
961 | |
|
|
962 | if (PL_lex_state != LEX_NOTPARSING) |
|
|
963 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
|
|
964 | } |
|
|
965 | } |
|
|
966 | |
|
|
967 | /* always use the TRANSFER macro */ |
740 | static void NOINLINE |
968 | static void NOINLINE |
741 | transfer (pTHX_ struct coro *prev, struct coro *next) |
969 | transfer (pTHX_ struct coro *prev, struct coro *next) |
742 | { |
970 | { |
743 | dSTACKLEVEL; |
971 | dSTACKLEVEL; |
744 | |
972 | |
… | |
… | |
754 | |
982 | |
755 | if (prev->flags & CF_NEW) |
983 | if (prev->flags & CF_NEW) |
756 | { |
984 | { |
757 | /* create a new empty context */ |
985 | /* create a new empty context */ |
758 | Newz (0, prev->cctx, 1, coro_cctx); |
986 | Newz (0, prev->cctx, 1, coro_cctx); |
759 | prev->cctx->inuse = 1; |
|
|
760 | prev->flags &= ~CF_NEW; |
987 | prev->flags &= ~CF_NEW; |
761 | prev->flags |= CF_RUNNING; |
988 | prev->flags |= CF_RUNNING; |
762 | } |
989 | } |
763 | |
|
|
764 | /*TODO: must not croak here */ |
|
|
765 | if (!prev->flags & CF_RUNNING) |
|
|
766 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
767 | |
|
|
768 | if (next->flags & CF_RUNNING) |
|
|
769 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
770 | |
|
|
771 | if (next->flags & CF_DESTROYED) |
|
|
772 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
773 | |
990 | |
774 | prev->flags &= ~CF_RUNNING; |
991 | prev->flags &= ~CF_RUNNING; |
775 | next->flags |= CF_RUNNING; |
992 | next->flags |= CF_RUNNING; |
776 | |
993 | |
777 | LOCK; |
994 | LOCK; |
… | |
… | |
782 | next->flags &= ~CF_NEW; |
999 | next->flags &= ~CF_NEW; |
783 | /* first get rid of the old state */ |
1000 | /* first get rid of the old state */ |
784 | save_perl (aTHX_ prev); |
1001 | save_perl (aTHX_ prev); |
785 | /* setup coroutine call */ |
1002 | /* setup coroutine call */ |
786 | setup_coro (aTHX_ next); |
1003 | setup_coro (aTHX_ next); |
787 | /* need a new stack */ |
|
|
788 | assert (!next->cctx); |
|
|
789 | } |
1004 | } |
790 | else |
1005 | else |
791 | { |
1006 | { |
792 | /* coroutine already started */ |
1007 | /* coroutine already started */ |
793 | save_perl (aTHX_ prev); |
1008 | save_perl (aTHX_ prev); |
… | |
… | |
795 | } |
1010 | } |
796 | |
1011 | |
797 | prev__cctx = prev->cctx; |
1012 | prev__cctx = prev->cctx; |
798 | |
1013 | |
799 | /* possibly "free" the cctx */ |
1014 | /* possibly "free" the cctx */ |
800 | if (prev__cctx->idle_sp == STACKLEVEL) |
1015 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
801 | { |
1016 | { |
802 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1017 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
803 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1018 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
804 | |
1019 | |
805 | prev->cctx = 0; |
1020 | prev->cctx = 0; |
806 | |
1021 | |
807 | cctx_put (prev__cctx); |
1022 | cctx_put (prev__cctx); |
808 | prev__cctx->inuse = 0; |
|
|
809 | } |
1023 | } |
810 | |
1024 | |
|
|
1025 | ++next->usecount; |
|
|
1026 | |
811 | if (!next->cctx) |
1027 | if (!next->cctx) |
812 | { |
|
|
813 | next->cctx = cctx_get (aTHX); |
1028 | next->cctx = cctx_get (aTHX); |
814 | assert (!next->cctx->inuse); |
|
|
815 | next->cctx->inuse = 1; |
|
|
816 | } |
|
|
817 | |
1029 | |
818 | if (prev__cctx != next->cctx) |
1030 | if (prev__cctx != next->cctx) |
819 | { |
1031 | { |
820 | prev__cctx->top_env = PL_top_env; |
1032 | prev__cctx->top_env = PL_top_env; |
821 | PL_top_env = next->cctx->top_env; |
1033 | PL_top_env = next->cctx->top_env; |
… | |
… | |
831 | { |
1043 | { |
832 | struct coro *prev, *next; |
1044 | struct coro *prev, *next; |
833 | }; |
1045 | }; |
834 | |
1046 | |
835 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1047 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
|
|
1048 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
836 | |
1049 | |
837 | /** high level stuff ********************************************************/ |
1050 | /** high level stuff ********************************************************/ |
838 | |
1051 | |
839 | static int |
1052 | static int |
840 | coro_state_destroy (pTHX_ struct coro *coro) |
1053 | coro_state_destroy (pTHX_ struct coro *coro) |
… | |
… | |
878 | } |
1091 | } |
879 | |
1092 | |
880 | cctx_destroy (coro->cctx); |
1093 | cctx_destroy (coro->cctx); |
881 | SvREFCNT_dec (coro->args); |
1094 | SvREFCNT_dec (coro->args); |
882 | |
1095 | |
|
|
1096 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1097 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1098 | if (coro == coro_first) coro_first = coro->next; |
|
|
1099 | |
883 | return 1; |
1100 | return 1; |
884 | } |
1101 | } |
885 | |
1102 | |
886 | static int |
1103 | static int |
887 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1104 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
888 | { |
1105 | { |
889 | struct coro *coro = (struct coro *)mg->mg_ptr; |
1106 | struct coro *coro = (struct coro *)mg->mg_ptr; |
890 | mg->mg_ptr = 0; |
1107 | mg->mg_ptr = 0; |
|
|
1108 | |
|
|
1109 | coro->hv = 0; |
891 | |
1110 | |
892 | if (--coro->refcnt < 0) |
1111 | if (--coro->refcnt < 0) |
893 | { |
1112 | { |
894 | coro_state_destroy (aTHX_ coro); |
1113 | coro_state_destroy (aTHX_ coro); |
895 | Safefree (coro); |
1114 | Safefree (coro); |
… | |
… | |
917 | #else |
1136 | #else |
918 | # define MGf_DUP 0 |
1137 | # define MGf_DUP 0 |
919 | #endif |
1138 | #endif |
920 | }; |
1139 | }; |
921 | |
1140 | |
922 | static struct coro * |
|
|
923 | SvSTATE_ (pTHX_ SV *coro) |
|
|
924 | { |
|
|
925 | HV *stash; |
|
|
926 | MAGIC *mg; |
|
|
927 | |
|
|
928 | if (SvROK (coro)) |
|
|
929 | coro = SvRV (coro); |
|
|
930 | |
|
|
931 | stash = SvSTASH (coro); |
|
|
932 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
933 | { |
|
|
934 | /* very slow, but rare, check */ |
|
|
935 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
936 | croak ("Coro::State object required"); |
|
|
937 | } |
|
|
938 | |
|
|
939 | mg = CORO_MAGIC (coro); |
|
|
940 | return (struct coro *)mg->mg_ptr; |
|
|
941 | } |
|
|
942 | |
|
|
943 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
944 | |
|
|
945 | static void |
1141 | static void |
946 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1142 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
947 | { |
1143 | { |
948 | ta->prev = SvSTATE (prev_sv); |
1144 | ta->prev = SvSTATE (prev_sv); |
949 | ta->next = SvSTATE (next_sv); |
1145 | ta->next = SvSTATE (next_sv); |
|
|
1146 | TRANSFER_CHECK (*ta); |
950 | } |
1147 | } |
951 | |
1148 | |
952 | static void |
1149 | static void |
953 | api_transfer (SV *prev_sv, SV *next_sv) |
1150 | api_transfer (SV *prev_sv, SV *next_sv) |
954 | { |
1151 | { |
… | |
… | |
1071 | break; |
1268 | break; |
1072 | } |
1269 | } |
1073 | |
1270 | |
1074 | /* free this only after the transfer */ |
1271 | /* free this only after the transfer */ |
1075 | prev_sv = SvRV (coro_current); |
1272 | prev_sv = SvRV (coro_current); |
1076 | SvRV_set (coro_current, next_sv); |
|
|
1077 | ta->prev = SvSTATE (prev_sv); |
1273 | ta->prev = SvSTATE (prev_sv); |
1078 | |
1274 | TRANSFER_CHECK (*ta); |
1079 | assert (ta->next->flags & CF_READY); |
1275 | assert (ta->next->flags & CF_READY); |
1080 | ta->next->flags &= ~CF_READY; |
1276 | ta->next->flags &= ~CF_READY; |
|
|
1277 | SvRV_set (coro_current, next_sv); |
1081 | |
1278 | |
1082 | LOCK; |
1279 | LOCK; |
1083 | free_coro_mortal (aTHX); |
1280 | free_coro_mortal (aTHX); |
1084 | coro_mortal = prev_sv; |
1281 | coro_mortal = prev_sv; |
1085 | UNLOCK; |
1282 | UNLOCK; |
… | |
… | |
1159 | #endif |
1356 | #endif |
1160 | BOOT_PAGESIZE; |
1357 | BOOT_PAGESIZE; |
1161 | |
1358 | |
1162 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1359 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1163 | |
1360 | |
|
|
1361 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1362 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1363 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1364 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
|
|
1365 | |
1164 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1366 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1165 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1367 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1166 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1368 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1167 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1369 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1370 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
|
|
1371 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1168 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1372 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1169 | |
1373 | |
1170 | main_mainstack = PL_mainstack; |
1374 | main_mainstack = PL_mainstack; |
|
|
1375 | main_top_env = PL_top_env; |
|
|
1376 | |
|
|
1377 | while (main_top_env->je_prev) |
|
|
1378 | main_top_env = main_top_env->je_prev; |
1171 | |
1379 | |
1172 | coroapi.ver = CORO_API_VERSION; |
1380 | coroapi.ver = CORO_API_VERSION; |
1173 | coroapi.transfer = api_transfer; |
1381 | coroapi.transfer = api_transfer; |
1174 | |
1382 | |
1175 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1383 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
… | |
… | |
1183 | HV *hv; |
1391 | HV *hv; |
1184 | int i; |
1392 | int i; |
1185 | |
1393 | |
1186 | Newz (0, coro, 1, struct coro); |
1394 | Newz (0, coro, 1, struct coro); |
1187 | coro->args = newAV (); |
1395 | coro->args = newAV (); |
1188 | coro->save = CORO_SAVE_ALL; |
1396 | coro->save = CORO_SAVE_DEF; |
1189 | coro->flags = CF_NEW; |
1397 | coro->flags = CF_NEW; |
1190 | |
1398 | |
|
|
1399 | if (coro_first) coro_first->prev = coro; |
|
|
1400 | coro->next = coro_first; |
|
|
1401 | coro_first = coro; |
|
|
1402 | |
1191 | hv = newHV (); |
1403 | coro->hv = hv = newHV (); |
1192 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1404 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1193 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1405 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1194 | |
1406 | |
1195 | for (i = 1; i < items; i++) |
1407 | for (i = 1; i < items; i++) |
1196 | av_push (coro->args, newSVsv (ST (i))); |
1408 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1200 | |
1412 | |
1201 | int |
1413 | int |
1202 | save (SV *coro, int new_save = -1) |
1414 | save (SV *coro, int new_save = -1) |
1203 | CODE: |
1415 | CODE: |
1204 | RETVAL = api_save (coro, new_save); |
1416 | RETVAL = api_save (coro, new_save); |
|
|
1417 | OUTPUT: |
|
|
1418 | RETVAL |
|
|
1419 | |
|
|
1420 | int |
|
|
1421 | save_also (SV *coro_sv, int save_also) |
|
|
1422 | CODE: |
|
|
1423 | { |
|
|
1424 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1425 | RETVAL = coro->save; |
|
|
1426 | coro->save |= save_also; |
|
|
1427 | } |
1205 | OUTPUT: |
1428 | OUTPUT: |
1206 | RETVAL |
1429 | RETVAL |
1207 | |
1430 | |
1208 | void |
1431 | void |
1209 | _set_stacklevel (...) |
1432 | _set_stacklevel (...) |
… | |
… | |
1287 | CODE: |
1510 | CODE: |
1288 | RETVAL = cctx_idle; |
1511 | RETVAL = cctx_idle; |
1289 | OUTPUT: |
1512 | OUTPUT: |
1290 | RETVAL |
1513 | RETVAL |
1291 | |
1514 | |
|
|
1515 | void |
|
|
1516 | list () |
|
|
1517 | PPCODE: |
|
|
1518 | { |
|
|
1519 | struct coro *coro; |
|
|
1520 | for (coro = coro_first; coro; coro = coro->next) |
|
|
1521 | if (coro->hv) |
|
|
1522 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
|
|
1523 | } |
|
|
1524 | |
|
|
1525 | void |
|
|
1526 | call (Coro::State coro, SV *coderef) |
|
|
1527 | ALIAS: |
|
|
1528 | eval = 1 |
|
|
1529 | CODE: |
|
|
1530 | { |
|
|
1531 | if (coro->mainstack) |
|
|
1532 | { |
|
|
1533 | struct coro temp; |
|
|
1534 | Zero (&temp, 1, struct coro); |
|
|
1535 | temp.save = CORO_SAVE_ALL; |
|
|
1536 | |
|
|
1537 | if (!(coro->flags & CF_RUNNING)) |
|
|
1538 | { |
|
|
1539 | save_perl (aTHX_ &temp); |
|
|
1540 | load_perl (aTHX_ coro); |
|
|
1541 | } |
|
|
1542 | |
|
|
1543 | { |
|
|
1544 | dSP; |
|
|
1545 | ENTER; |
|
|
1546 | SAVETMPS; |
|
|
1547 | PUSHMARK (SP); |
|
|
1548 | PUTBACK; |
|
|
1549 | if (ix) |
|
|
1550 | eval_sv (coderef, 0); |
|
|
1551 | else |
|
|
1552 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1553 | SPAGAIN; |
|
|
1554 | FREETMPS; |
|
|
1555 | LEAVE; |
|
|
1556 | PUTBACK; |
|
|
1557 | } |
|
|
1558 | |
|
|
1559 | if (!(coro->flags & CF_RUNNING)) |
|
|
1560 | { |
|
|
1561 | save_perl (aTHX_ coro); |
|
|
1562 | load_perl (aTHX_ &temp); |
|
|
1563 | } |
|
|
1564 | } |
|
|
1565 | } |
|
|
1566 | |
|
|
1567 | SV * |
|
|
1568 | is_ready (Coro::State coro) |
|
|
1569 | PROTOTYPE: $ |
|
|
1570 | ALIAS: |
|
|
1571 | is_ready = CF_READY |
|
|
1572 | is_running = CF_RUNNING |
|
|
1573 | is_new = CF_NEW |
|
|
1574 | is_destroyed = CF_DESTROYED |
|
|
1575 | CODE: |
|
|
1576 | RETVAL = boolSV (coro->flags & ix); |
|
|
1577 | OUTPUT: |
|
|
1578 | RETVAL |
|
|
1579 | |
|
|
1580 | void |
|
|
1581 | trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
1582 | CODE: |
|
|
1583 | if (flags & CC_TRACE) |
|
|
1584 | { |
|
|
1585 | if (!coro->cctx) |
|
|
1586 | coro->cctx = cctx_new (); |
|
|
1587 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1588 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1589 | |
|
|
1590 | coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL); |
|
|
1591 | } |
|
|
1592 | else |
|
|
1593 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1594 | { |
|
|
1595 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1596 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1597 | |
|
|
1598 | if (coro->flags & CF_RUNNING) |
|
|
1599 | PL_runops = RUNOPS_DEFAULT; |
|
|
1600 | else |
|
|
1601 | coro->runops = RUNOPS_DEFAULT; |
|
|
1602 | } |
|
|
1603 | |
|
|
1604 | SV * |
|
|
1605 | has_stack (Coro::State coro) |
|
|
1606 | PROTOTYPE: $ |
|
|
1607 | CODE: |
|
|
1608 | RETVAL = boolSV (!!coro->cctx); |
|
|
1609 | OUTPUT: |
|
|
1610 | RETVAL |
|
|
1611 | |
|
|
1612 | int |
|
|
1613 | is_traced (Coro::State coro) |
|
|
1614 | PROTOTYPE: $ |
|
|
1615 | CODE: |
|
|
1616 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
|
|
1617 | OUTPUT: |
|
|
1618 | RETVAL |
|
|
1619 | |
|
|
1620 | IV |
|
|
1621 | rss (Coro::State coro) |
|
|
1622 | PROTOTYPE: $ |
|
|
1623 | ALIAS: |
|
|
1624 | usecount = 1 |
|
|
1625 | CODE: |
|
|
1626 | switch (ix) |
|
|
1627 | { |
|
|
1628 | case 0: RETVAL = coro_rss (aTHX_ coro); break; |
|
|
1629 | case 1: RETVAL = coro->usecount; break; |
|
|
1630 | } |
|
|
1631 | OUTPUT: |
|
|
1632 | RETVAL |
|
|
1633 | |
|
|
1634 | |
1292 | MODULE = Coro::State PACKAGE = Coro |
1635 | MODULE = Coro::State PACKAGE = Coro |
1293 | |
1636 | |
1294 | BOOT: |
1637 | BOOT: |
1295 | { |
1638 | { |
1296 | int i; |
1639 | int i; |
|
|
1640 | |
|
|
1641 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1642 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1643 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1644 | |
|
|
1645 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1646 | SvREADONLY_on (coro_current); |
1297 | |
1647 | |
1298 | coro_stash = gv_stashpv ("Coro", TRUE); |
1648 | coro_stash = gv_stashpv ("Coro", TRUE); |
1299 | |
1649 | |
1300 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1650 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1301 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1651 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1302 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1652 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1303 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1653 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1304 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1654 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1305 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1655 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1306 | |
|
|
1307 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1308 | SvREADONLY_on (coro_current); |
|
|
1309 | |
1656 | |
1310 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1657 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1311 | coro_ready[i] = newAV (); |
1658 | coro_ready[i] = newAV (); |
1312 | |
1659 | |
1313 | { |
1660 | { |
… | |
… | |
1363 | CODE: |
1710 | CODE: |
1364 | RETVAL = boolSV (api_ready (self)); |
1711 | RETVAL = boolSV (api_ready (self)); |
1365 | OUTPUT: |
1712 | OUTPUT: |
1366 | RETVAL |
1713 | RETVAL |
1367 | |
1714 | |
1368 | SV * |
|
|
1369 | is_ready (SV *self) |
|
|
1370 | PROTOTYPE: $ |
|
|
1371 | CODE: |
|
|
1372 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1373 | OUTPUT: |
|
|
1374 | RETVAL |
|
|
1375 | |
|
|
1376 | int |
1715 | int |
1377 | nready (...) |
1716 | nready (...) |
1378 | PROTOTYPE: |
1717 | PROTOTYPE: |
1379 | CODE: |
1718 | CODE: |
1380 | RETVAL = coro_nready; |
1719 | RETVAL = coro_nready; |
1381 | OUTPUT: |
1720 | OUTPUT: |
1382 | RETVAL |
1721 | RETVAL |
1383 | |
1722 | |
|
|
1723 | # for async_pool speedup |
|
|
1724 | void |
|
|
1725 | _pool_1 (SV *cb) |
|
|
1726 | CODE: |
|
|
1727 | { |
|
|
1728 | int i, len; |
|
|
1729 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1730 | AV *defav = GvAV (PL_defgv); |
|
|
1731 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1732 | AV *invoke_av; |
|
|
1733 | |
|
|
1734 | if (!invoke) |
|
|
1735 | croak ("\3terminate\2\n"); |
|
|
1736 | |
|
|
1737 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1738 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1739 | |
|
|
1740 | invoke_av = (AV *)SvRV (invoke); |
|
|
1741 | len = av_len (invoke_av); |
|
|
1742 | |
|
|
1743 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1744 | |
|
|
1745 | if (len > 0) |
|
|
1746 | { |
|
|
1747 | av_fill (defav, len - 1); |
|
|
1748 | for (i = 0; i < len; ++i) |
|
|
1749 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1750 | } |
|
|
1751 | |
|
|
1752 | SvREFCNT_dec (invoke); |
|
|
1753 | } |
|
|
1754 | |
|
|
1755 | void |
|
|
1756 | _pool_2 (SV *cb) |
|
|
1757 | CODE: |
|
|
1758 | { |
|
|
1759 | struct coro *coro = SvSTATE (coro_current); |
|
|
1760 | |
|
|
1761 | sv_setsv (cb, &PL_sv_undef); |
|
|
1762 | |
|
|
1763 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
|
|
1764 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1765 | croak ("\3terminate\2\n"); |
|
|
1766 | |
|
|
1767 | av_clear (GvAV (PL_defgv)); |
|
|
1768 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1769 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1770 | coro->save = CORO_SAVE_DEF; |
|
|
1771 | coro->prio = 0; |
|
|
1772 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1773 | } |
|
|
1774 | |
|
|
1775 | |
1384 | MODULE = Coro::State PACKAGE = Coro::AIO |
1776 | MODULE = Coro::State PACKAGE = Coro::AIO |
1385 | |
1777 | |
1386 | SV * |
1778 | SV * |
1387 | _get_state () |
1779 | _get_state () |
1388 | CODE: |
1780 | CODE: |