… | |
… | |
132 | 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; |
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 | static GV *irsgv; /* $/ */ |
|
|
138 | |
137 | /* async_pool helper stuff */ |
139 | /* async_pool helper stuff */ |
138 | static SV *sv_pool_rss; |
140 | static SV *sv_pool_rss; |
139 | static SV *sv_pool_size; |
141 | static SV *sv_pool_size; |
140 | static AV *av_async_pool; |
142 | static AV *av_async_pool; |
141 | |
143 | |
142 | static struct coro_cctx *cctx_first; |
144 | static struct coro_cctx *cctx_first; |
143 | static int cctx_count, cctx_idle; |
145 | static int cctx_count, cctx_idle; |
|
|
146 | |
|
|
147 | enum { |
|
|
148 | CC_MAPPED = 0x01, |
|
|
149 | CC_NOREUSE = 0x02, /* throw this away after tracing */ |
|
|
150 | CC_TRACE = 0x04, |
|
|
151 | CC_TRACE_SUB = 0x08, /* trace sub calls */ |
|
|
152 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
|
|
153 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
|
|
154 | }; |
144 | |
155 | |
145 | /* this is a structure representing a c-level coroutine */ |
156 | /* this is a structure representing a c-level coroutine */ |
146 | typedef struct coro_cctx { |
157 | typedef struct coro_cctx { |
147 | struct coro_cctx *next; |
158 | struct coro_cctx *next; |
148 | |
159 | |
… | |
… | |
157 | coro_context cctx; |
168 | coro_context cctx; |
158 | |
169 | |
159 | #if CORO_USE_VALGRIND |
170 | #if CORO_USE_VALGRIND |
160 | int valgrind_id; |
171 | int valgrind_id; |
161 | #endif |
172 | #endif |
162 | char inuse, mapped; |
173 | unsigned char flags; |
163 | } coro_cctx; |
174 | } coro_cctx; |
164 | |
175 | |
165 | enum { |
176 | enum { |
166 | CF_RUNNING = 0x0001, /* coroutine is running */ |
177 | CF_RUNNING = 0x0001, /* coroutine is running */ |
167 | CF_READY = 0x0002, /* coroutine is ready */ |
178 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
175 | coro_cctx *cctx; |
186 | coro_cctx *cctx; |
176 | |
187 | |
177 | /* data associated with this coroutine (initial args) */ |
188 | /* data associated with this coroutine (initial args) */ |
178 | AV *args; |
189 | AV *args; |
179 | int refcnt; |
190 | int refcnt; |
180 | int save; /* CORO_SAVE flags */ |
|
|
181 | int flags; /* CF_ flags */ |
191 | int flags; /* CF_ flags */ |
182 | |
192 | |
183 | /* optionally saved, might be zero */ |
193 | /* optionally saved, might be zero */ |
184 | AV *defav; /* @_ */ |
194 | AV *defav; /* @_ */ |
185 | SV *defsv; /* $_ */ |
195 | SV *defsv; /* $_ */ |
186 | SV *errsv; /* $@ */ |
196 | SV *errsv; /* $@ */ |
187 | GV *deffh; /* default filehandle */ |
197 | SV *deffh; /* default filehandle */ |
188 | SV *irssv; /* $/ */ |
198 | SV *irssv; /* $/ */ |
189 | SV *irssv_sv; /* real $/ cache */ |
199 | SV *irssv_sv; /* real $/ cache */ |
190 | |
200 | |
191 | #define VAR(name,type) type name; |
201 | #define VAR(name,type) type name; |
192 | # include "state.h" |
202 | # include "state.h" |
193 | #undef VAR |
203 | #undef VAR |
194 | |
204 | |
|
|
205 | /* statistics */ |
|
|
206 | int usecount; /* number of transfers to this coro */ |
|
|
207 | |
195 | /* coro process data */ |
208 | /* coro process data */ |
196 | int prio; |
209 | int prio; |
|
|
210 | //SV *throw; |
|
|
211 | |
|
|
212 | /* async_pool */ |
|
|
213 | SV *saved_deffh; |
197 | |
214 | |
198 | /* linked list */ |
215 | /* linked list */ |
199 | struct coro *next, *prev; |
216 | struct coro *next, *prev; |
200 | HV *hv; /* the perl hash associated with this coro, if any */ |
217 | HV *hv; /* the perl hash associated with this coro, if any */ |
201 | }; |
218 | }; |
… | |
… | |
289 | SvMAGIC (cv) \ |
306 | SvMAGIC (cv) \ |
290 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
307 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
291 | ? SvMAGIC (cv) \ |
308 | ? SvMAGIC (cv) \ |
292 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
309 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
293 | : 0 |
310 | : 0 |
|
|
311 | |
|
|
312 | static struct coro * |
|
|
313 | SvSTATE_ (pTHX_ SV *coro) |
|
|
314 | { |
|
|
315 | HV *stash; |
|
|
316 | MAGIC *mg; |
|
|
317 | |
|
|
318 | if (SvROK (coro)) |
|
|
319 | coro = SvRV (coro); |
|
|
320 | |
|
|
321 | if (SvTYPE (coro) != SVt_PVHV) |
|
|
322 | croak ("Coro::State object required"); |
|
|
323 | |
|
|
324 | stash = SvSTASH (coro); |
|
|
325 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
326 | { |
|
|
327 | /* very slow, but rare, check */ |
|
|
328 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
329 | croak ("Coro::State object required"); |
|
|
330 | } |
|
|
331 | |
|
|
332 | mg = CORO_MAGIC (coro); |
|
|
333 | return (struct coro *)mg->mg_ptr; |
|
|
334 | } |
|
|
335 | |
|
|
336 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
294 | |
337 | |
295 | /* the next two functions merely cache the padlists */ |
338 | /* the next two functions merely cache the padlists */ |
296 | static void |
339 | static void |
297 | get_padlist (pTHX_ CV *cv) |
340 | get_padlist (pTHX_ CV *cv) |
298 | { |
341 | { |
… | |
… | |
337 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
380 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
338 | } |
381 | } |
339 | |
382 | |
340 | /** load & save, init *******************************************************/ |
383 | /** load & save, init *******************************************************/ |
341 | |
384 | |
342 | #define SB do { |
|
|
343 | #define SE } while (0) |
|
|
344 | |
|
|
345 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
|
|
346 | |
|
|
347 | static void |
385 | static void |
348 | load_perl (pTHX_ Coro__State c) |
386 | load_perl (pTHX_ Coro__State c) |
349 | { |
387 | { |
350 | #define VAR(name,type) PL_ ## name = c->name; |
388 | #define VAR(name,type) PL_ ## name = c->name; |
351 | # include "state.h" |
389 | # include "state.h" |
352 | #undef VAR |
390 | #undef VAR |
353 | |
391 | |
354 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
392 | GvSV (PL_defgv) = c->defsv; |
355 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
393 | GvAV (PL_defgv) = c->defav; |
356 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
394 | GvSV (PL_errgv) = c->errsv; |
357 | if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); |
395 | GvSV (irsgv) = c->irssv_sv; |
358 | |
|
|
359 | if (c->irssv) |
|
|
360 | { |
|
|
361 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
362 | { |
|
|
363 | SvREFCNT_dec (c->irssv); |
|
|
364 | c->irssv = 0; |
|
|
365 | } |
|
|
366 | else |
|
|
367 | { |
|
|
368 | REPLACE_SV (PL_rs, c->irssv); |
|
|
369 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
|
|
370 | sv_setsv (c->irssv_sv, PL_rs); |
|
|
371 | } |
|
|
372 | } |
|
|
373 | |
396 | |
374 | { |
397 | { |
375 | dSP; |
398 | dSP; |
376 | CV *cv; |
399 | CV *cv; |
377 | |
400 | |
… | |
… | |
435 | } |
458 | } |
436 | |
459 | |
437 | PUTBACK; |
460 | PUTBACK; |
438 | } |
461 | } |
439 | |
462 | |
440 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
463 | c->defav = GvAV (PL_defgv); |
441 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
464 | c->defsv = DEFSV; |
442 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
465 | c->errsv = ERRSV; |
443 | c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; |
466 | c->irssv_sv = GvSV (irsgv); |
444 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
|
|
445 | |
467 | |
446 | #define VAR(name,type)c->name = PL_ ## name; |
468 | #define VAR(name,type)c->name = PL_ ## name; |
447 | # include "state.h" |
469 | # include "state.h" |
448 | #undef VAR |
470 | #undef VAR |
449 | } |
471 | } |
… | |
… | |
502 | * destroy the stacks, the callchain etc... |
524 | * destroy the stacks, the callchain etc... |
503 | */ |
525 | */ |
504 | static void |
526 | static void |
505 | coro_destroy_stacks (pTHX) |
527 | coro_destroy_stacks (pTHX) |
506 | { |
528 | { |
507 | if (!IN_DESTRUCT) |
|
|
508 | { |
|
|
509 | /* restore all saved variables and stuff */ |
|
|
510 | LEAVE_SCOPE (0); |
|
|
511 | assert (PL_tmps_floor == -1); |
|
|
512 | |
|
|
513 | /* free all temporaries */ |
|
|
514 | FREETMPS; |
|
|
515 | assert (PL_tmps_ix == -1); |
|
|
516 | |
|
|
517 | /* unwind all extra stacks */ |
|
|
518 | POPSTACK_TO (PL_mainstack); |
|
|
519 | |
|
|
520 | /* unwind main stack */ |
|
|
521 | dounwind (-1); |
|
|
522 | } |
|
|
523 | |
|
|
524 | while (PL_curstackinfo->si_next) |
529 | while (PL_curstackinfo->si_next) |
525 | PL_curstackinfo = PL_curstackinfo->si_next; |
530 | PL_curstackinfo = PL_curstackinfo->si_next; |
526 | |
531 | |
527 | while (PL_curstackinfo) |
532 | while (PL_curstackinfo) |
528 | { |
533 | { |
… | |
… | |
544 | Safefree (PL_retstack); |
549 | Safefree (PL_retstack); |
545 | #endif |
550 | #endif |
546 | } |
551 | } |
547 | |
552 | |
548 | static size_t |
553 | static size_t |
549 | coro_rss (struct coro *coro) |
554 | coro_rss (pTHX_ struct coro *coro) |
550 | { |
555 | { |
551 | size_t rss = sizeof (coro); |
556 | size_t rss = sizeof (*coro); |
552 | |
557 | |
553 | if (coro->mainstack) |
558 | if (coro->mainstack) |
554 | { |
559 | { |
555 | if (coro->flags & CF_RUNNING) |
560 | if (coro->flags & CF_RUNNING) |
556 | { |
561 | { |
… | |
… | |
558 | # include "state.h" |
563 | # include "state.h" |
559 | #undef VAR |
564 | #undef VAR |
560 | } |
565 | } |
561 | |
566 | |
562 | rss += sizeof (coro->curstackinfo); |
567 | rss += sizeof (coro->curstackinfo); |
563 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
568 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
564 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
569 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
565 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
570 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
566 | rss += coro->tmps_max * sizeof (SV *); |
571 | rss += coro->tmps_max * sizeof (SV *); |
567 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
572 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
568 | rss += coro->scopestack_max * sizeof (I32); |
573 | rss += coro->scopestack_max * sizeof (I32); |
569 | rss += coro->savestack_max * sizeof (ANY); |
574 | rss += coro->savestack_max * sizeof (ANY); |
570 | |
575 | |
… | |
… | |
577 | } |
582 | } |
578 | |
583 | |
579 | /** coroutine stack handling ************************************************/ |
584 | /** coroutine stack handling ************************************************/ |
580 | |
585 | |
581 | static void |
586 | static void |
582 | setup_coro (pTHX_ struct coro *coro) |
587 | coro_setup (pTHX_ struct coro *coro) |
583 | { |
588 | { |
584 | /* |
589 | /* |
585 | * emulate part of the perl startup here. |
590 | * emulate part of the perl startup here. |
586 | */ |
591 | */ |
587 | coro_init_stacks (aTHX); |
592 | coro_init_stacks (aTHX); |
588 | |
593 | |
|
|
594 | PL_runops = RUNOPS_DEFAULT; |
589 | PL_curcop = &PL_compiling; |
595 | PL_curcop = &PL_compiling; |
590 | PL_in_eval = EVAL_NULL; |
596 | PL_in_eval = EVAL_NULL; |
591 | PL_comppad = 0; |
597 | PL_comppad = 0; |
592 | PL_curpm = 0; |
598 | PL_curpm = 0; |
593 | PL_localizing = 0; |
599 | PL_localizing = 0; |
594 | PL_dirty = 0; |
600 | PL_dirty = 0; |
595 | PL_restartop = 0; |
601 | PL_restartop = 0; |
|
|
602 | |
|
|
603 | GvSV (PL_defgv) = NEWSV (0, 0); |
|
|
604 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
|
|
605 | GvSV (PL_errgv) = NEWSV (0, 0); |
|
|
606 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
607 | PL_rs = newSVsv (GvSV (irsgv)); |
|
|
608 | |
|
|
609 | SvREFCNT_inc (PL_defoutgv); |
596 | |
610 | |
597 | { |
611 | { |
598 | dSP; |
612 | dSP; |
599 | LOGOP myop; |
613 | LOGOP myop; |
600 | |
|
|
601 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
602 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
|
|
603 | |
614 | |
604 | Zero (&myop, 1, LOGOP); |
615 | Zero (&myop, 1, LOGOP); |
605 | myop.op_next = Nullop; |
616 | myop.op_next = Nullop; |
606 | myop.op_flags = OPf_WANT_VOID; |
617 | myop.op_flags = OPf_WANT_VOID; |
607 | |
618 | |
… | |
… | |
615 | |
626 | |
616 | ENTER; /* necessary e.g. for dounwind */ |
627 | ENTER; /* necessary e.g. for dounwind */ |
617 | } |
628 | } |
618 | |
629 | |
619 | static void |
630 | static void |
|
|
631 | coro_destroy (pTHX_ struct coro *coro) |
|
|
632 | { |
|
|
633 | if (!IN_DESTRUCT) |
|
|
634 | { |
|
|
635 | /* restore all saved variables and stuff */ |
|
|
636 | LEAVE_SCOPE (0); |
|
|
637 | assert (PL_tmps_floor == -1); |
|
|
638 | |
|
|
639 | /* free all temporaries */ |
|
|
640 | FREETMPS; |
|
|
641 | assert (PL_tmps_ix == -1); |
|
|
642 | |
|
|
643 | /* unwind all extra stacks */ |
|
|
644 | POPSTACK_TO (PL_mainstack); |
|
|
645 | |
|
|
646 | /* unwind main stack */ |
|
|
647 | dounwind (-1); |
|
|
648 | } |
|
|
649 | |
|
|
650 | SvREFCNT_dec (GvSV (PL_defgv)); |
|
|
651 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
652 | SvREFCNT_dec (GvSV (PL_errgv)); |
|
|
653 | SvREFCNT_dec (PL_defoutgv); |
|
|
654 | SvREFCNT_dec (PL_rs); |
|
|
655 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
656 | |
|
|
657 | SvREFCNT_dec (coro->saved_deffh); |
|
|
658 | //SvREFCNT_dec (coro->throw); |
|
|
659 | |
|
|
660 | coro_destroy_stacks (aTHX); |
|
|
661 | } |
|
|
662 | |
|
|
663 | static void |
620 | free_coro_mortal (pTHX) |
664 | free_coro_mortal (pTHX) |
621 | { |
665 | { |
622 | if (coro_mortal) |
666 | if (coro_mortal) |
623 | { |
667 | { |
624 | SvREFCNT_dec (coro_mortal); |
668 | SvREFCNT_dec (coro_mortal); |
625 | coro_mortal = 0; |
669 | coro_mortal = 0; |
626 | } |
670 | } |
|
|
671 | } |
|
|
672 | |
|
|
673 | static int |
|
|
674 | runops_trace (pTHX) |
|
|
675 | { |
|
|
676 | COP *oldcop = 0; |
|
|
677 | int oldcxix = -2; |
|
|
678 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
679 | coro_cctx *cctx = coro->cctx; |
|
|
680 | |
|
|
681 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
682 | { |
|
|
683 | PERL_ASYNC_CHECK (); |
|
|
684 | |
|
|
685 | if (cctx->flags & CC_TRACE_ALL) |
|
|
686 | { |
|
|
687 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
|
|
688 | { |
|
|
689 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
690 | SV **bot, **top; |
|
|
691 | AV *av = newAV (); /* return values */ |
|
|
692 | SV **cb; |
|
|
693 | dSP; |
|
|
694 | |
|
|
695 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
696 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
697 | if (isGV (gv)) |
|
|
698 | gv_efullname3 (fullname, gv, 0); |
|
|
699 | |
|
|
700 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
701 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
702 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
703 | : bot; |
|
|
704 | |
|
|
705 | while (bot < top) |
|
|
706 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
707 | |
|
|
708 | PL_runops = RUNOPS_DEFAULT; |
|
|
709 | ENTER; |
|
|
710 | SAVETMPS; |
|
|
711 | EXTEND (SP, 3); |
|
|
712 | PUSHMARK (SP); |
|
|
713 | PUSHs (&PL_sv_no); |
|
|
714 | PUSHs (fullname); |
|
|
715 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
716 | PUTBACK; |
|
|
717 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
718 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
719 | SPAGAIN; |
|
|
720 | FREETMPS; |
|
|
721 | LEAVE; |
|
|
722 | PL_runops = runops_trace; |
|
|
723 | } |
|
|
724 | |
|
|
725 | if (oldcop != PL_curcop) |
|
|
726 | { |
|
|
727 | oldcop = PL_curcop; |
|
|
728 | |
|
|
729 | if (PL_curcop != &PL_compiling) |
|
|
730 | { |
|
|
731 | SV **cb; |
|
|
732 | |
|
|
733 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
|
|
734 | { |
|
|
735 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
736 | |
|
|
737 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
|
|
738 | { |
|
|
739 | runops_proc_t old_runops = PL_runops; |
|
|
740 | dSP; |
|
|
741 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
742 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
743 | |
|
|
744 | if (isGV (gv)) |
|
|
745 | gv_efullname3 (fullname, gv, 0); |
|
|
746 | |
|
|
747 | PL_runops = RUNOPS_DEFAULT; |
|
|
748 | ENTER; |
|
|
749 | SAVETMPS; |
|
|
750 | EXTEND (SP, 3); |
|
|
751 | PUSHMARK (SP); |
|
|
752 | PUSHs (&PL_sv_yes); |
|
|
753 | PUSHs (fullname); |
|
|
754 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
755 | PUTBACK; |
|
|
756 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
757 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
758 | SPAGAIN; |
|
|
759 | FREETMPS; |
|
|
760 | LEAVE; |
|
|
761 | PL_runops = runops_trace; |
|
|
762 | } |
|
|
763 | |
|
|
764 | oldcxix = cxstack_ix; |
|
|
765 | } |
|
|
766 | |
|
|
767 | if (cctx->flags & CC_TRACE_LINE) |
|
|
768 | { |
|
|
769 | dSP; |
|
|
770 | |
|
|
771 | PL_runops = RUNOPS_DEFAULT; |
|
|
772 | ENTER; |
|
|
773 | SAVETMPS; |
|
|
774 | EXTEND (SP, 3); |
|
|
775 | PL_runops = RUNOPS_DEFAULT; |
|
|
776 | PUSHMARK (SP); |
|
|
777 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
|
|
778 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
|
|
779 | PUTBACK; |
|
|
780 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
|
|
781 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
782 | SPAGAIN; |
|
|
783 | FREETMPS; |
|
|
784 | LEAVE; |
|
|
785 | PL_runops = runops_trace; |
|
|
786 | } |
|
|
787 | } |
|
|
788 | } |
|
|
789 | } |
|
|
790 | } |
|
|
791 | |
|
|
792 | TAINT_NOT; |
|
|
793 | return 0; |
627 | } |
794 | } |
628 | |
795 | |
629 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
796 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
630 | /* _cctx_init should be careful, as it could be called at almost any time */ |
797 | /* _cctx_init should be careful, as it could be called at almost any time */ |
631 | /* during execution of a perl program */ |
798 | /* during execution of a perl program */ |
632 | static void NOINLINE |
799 | static void NOINLINE |
633 | prepare_cctx (pTHX_ coro_cctx *cctx) |
800 | prepare_cctx (pTHX_ coro_cctx *cctx) |
634 | { |
801 | { |
635 | dSP; |
802 | dSP; |
636 | LOGOP myop; |
803 | LOGOP myop; |
|
|
804 | |
|
|
805 | PL_top_env = &PL_start_env; |
|
|
806 | |
|
|
807 | if (cctx->flags & CC_TRACE) |
|
|
808 | PL_runops = runops_trace; |
637 | |
809 | |
638 | Zero (&myop, 1, LOGOP); |
810 | Zero (&myop, 1, LOGOP); |
639 | myop.op_next = PL_op; |
811 | myop.op_next = PL_op; |
640 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
812 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
641 | |
813 | |
… | |
… | |
658 | dTHX; |
830 | dTHX; |
659 | |
831 | |
660 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
832 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
661 | UNLOCK; |
833 | UNLOCK; |
662 | |
834 | |
663 | PL_top_env = &PL_start_env; |
835 | /* we now skip the entersub that lead to transfer() */ |
|
|
836 | PL_op = PL_op->op_next; |
664 | |
837 | |
665 | /* inject a fake subroutine call to cctx_init */ |
838 | /* inject a fake subroutine call to cctx_init */ |
666 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
839 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
667 | |
840 | |
668 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
841 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
702 | # if CORO_STACKGUARD |
875 | # if CORO_STACKGUARD |
703 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
876 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
704 | # endif |
877 | # endif |
705 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
878 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
706 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
879 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
707 | cctx->mapped = 1; |
880 | cctx->flags |= CC_MAPPED; |
708 | } |
881 | } |
709 | else |
882 | else |
710 | #endif |
883 | #endif |
711 | { |
884 | { |
712 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
885 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
739 | #if CORO_USE_VALGRIND |
912 | #if CORO_USE_VALGRIND |
740 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
913 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
741 | #endif |
914 | #endif |
742 | |
915 | |
743 | #if HAVE_MMAP |
916 | #if HAVE_MMAP |
744 | if (cctx->mapped) |
917 | if (cctx->flags & CC_MAPPED) |
745 | munmap (cctx->sptr, cctx->ssize); |
918 | munmap (cctx->sptr, cctx->ssize); |
746 | else |
919 | else |
747 | #endif |
920 | #endif |
748 | Safefree (cctx->sptr); |
921 | Safefree (cctx->sptr); |
749 | |
922 | |
… | |
… | |
757 | { |
930 | { |
758 | coro_cctx *cctx = cctx_first; |
931 | coro_cctx *cctx = cctx_first; |
759 | cctx_first = cctx->next; |
932 | cctx_first = cctx->next; |
760 | --cctx_idle; |
933 | --cctx_idle; |
761 | |
934 | |
762 | if (cctx->ssize >= coro_stacksize) |
935 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
763 | return cctx; |
936 | return cctx; |
764 | |
937 | |
765 | cctx_destroy (cctx); |
938 | cctx_destroy (cctx); |
766 | } |
939 | } |
767 | |
940 | |
768 | PL_op = PL_op->op_next; |
|
|
769 | return cctx_new (); |
941 | return cctx_new (); |
770 | } |
942 | } |
771 | |
943 | |
772 | static void |
944 | static void |
773 | cctx_put (coro_cctx *cctx) |
945 | cctx_put (coro_cctx *cctx) |
… | |
… | |
777 | { |
949 | { |
778 | coro_cctx *first = cctx_first; |
950 | coro_cctx *first = cctx_first; |
779 | cctx_first = first->next; |
951 | cctx_first = first->next; |
780 | --cctx_idle; |
952 | --cctx_idle; |
781 | |
953 | |
782 | assert (!first->inuse); |
|
|
783 | cctx_destroy (first); |
954 | cctx_destroy (first); |
784 | } |
955 | } |
785 | |
956 | |
786 | ++cctx_idle; |
957 | ++cctx_idle; |
787 | cctx->next = cctx_first; |
958 | cctx->next = cctx_first; |
788 | cctx_first = cctx; |
959 | cctx_first = cctx; |
789 | } |
960 | } |
790 | |
961 | |
791 | /** coroutine switching *****************************************************/ |
962 | /** coroutine switching *****************************************************/ |
792 | |
963 | |
793 | /* never call directly, always through the coro_state_transfer global variable */ |
964 | static void NOINLINE |
|
|
965 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
|
|
966 | { |
|
|
967 | if (prev != next) |
|
|
968 | { |
|
|
969 | if (!(prev->flags & (CF_RUNNING | CF_NEW))) |
|
|
970 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
|
|
971 | |
|
|
972 | if (next->flags & CF_RUNNING) |
|
|
973 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
974 | |
|
|
975 | if (next->flags & CF_DESTROYED) |
|
|
976 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
977 | |
|
|
978 | if (PL_lex_state != LEX_NOTPARSING) |
|
|
979 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
|
|
980 | } |
|
|
981 | } |
|
|
982 | |
|
|
983 | /* always use the TRANSFER macro */ |
794 | static void NOINLINE |
984 | static void NOINLINE |
795 | transfer (pTHX_ struct coro *prev, struct coro *next) |
985 | transfer (pTHX_ struct coro *prev, struct coro *next) |
796 | { |
986 | { |
797 | dSTACKLEVEL; |
987 | dSTACKLEVEL; |
798 | |
988 | |
… | |
… | |
808 | |
998 | |
809 | if (prev->flags & CF_NEW) |
999 | if (prev->flags & CF_NEW) |
810 | { |
1000 | { |
811 | /* create a new empty context */ |
1001 | /* create a new empty context */ |
812 | Newz (0, prev->cctx, 1, coro_cctx); |
1002 | Newz (0, prev->cctx, 1, coro_cctx); |
813 | prev->cctx->inuse = 1; |
|
|
814 | prev->flags &= ~CF_NEW; |
1003 | prev->flags &= ~CF_NEW; |
815 | prev->flags |= CF_RUNNING; |
1004 | prev->flags |= CF_RUNNING; |
816 | } |
1005 | } |
817 | |
|
|
818 | /*TODO: must not croak here */ |
|
|
819 | if (!prev->flags & CF_RUNNING) |
|
|
820 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
821 | |
|
|
822 | if (next->flags & CF_RUNNING) |
|
|
823 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
824 | |
|
|
825 | if (next->flags & CF_DESTROYED) |
|
|
826 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
827 | |
1006 | |
828 | prev->flags &= ~CF_RUNNING; |
1007 | prev->flags &= ~CF_RUNNING; |
829 | next->flags |= CF_RUNNING; |
1008 | next->flags |= CF_RUNNING; |
830 | |
1009 | |
831 | LOCK; |
1010 | LOCK; |
… | |
… | |
835 | /* need to start coroutine */ |
1014 | /* need to start coroutine */ |
836 | next->flags &= ~CF_NEW; |
1015 | next->flags &= ~CF_NEW; |
837 | /* first get rid of the old state */ |
1016 | /* first get rid of the old state */ |
838 | save_perl (aTHX_ prev); |
1017 | save_perl (aTHX_ prev); |
839 | /* setup coroutine call */ |
1018 | /* setup coroutine call */ |
840 | setup_coro (aTHX_ next); |
1019 | coro_setup (aTHX_ next); |
841 | /* need a new stack */ |
|
|
842 | assert (!next->cctx); |
|
|
843 | } |
1020 | } |
844 | else |
1021 | else |
845 | { |
1022 | { |
846 | /* coroutine already started */ |
1023 | /* coroutine already started */ |
847 | save_perl (aTHX_ prev); |
1024 | save_perl (aTHX_ prev); |
… | |
… | |
849 | } |
1026 | } |
850 | |
1027 | |
851 | prev__cctx = prev->cctx; |
1028 | prev__cctx = prev->cctx; |
852 | |
1029 | |
853 | /* possibly "free" the cctx */ |
1030 | /* possibly "free" the cctx */ |
854 | if (prev__cctx->idle_sp == STACKLEVEL) |
1031 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
855 | { |
1032 | { |
856 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1033 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
857 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1034 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
858 | |
1035 | |
859 | prev->cctx = 0; |
1036 | prev->cctx = 0; |
860 | |
1037 | |
861 | cctx_put (prev__cctx); |
1038 | cctx_put (prev__cctx); |
862 | prev__cctx->inuse = 0; |
|
|
863 | } |
1039 | } |
864 | |
1040 | |
|
|
1041 | ++next->usecount; |
|
|
1042 | |
865 | if (!next->cctx) |
1043 | if (!next->cctx) |
866 | { |
|
|
867 | next->cctx = cctx_get (aTHX); |
1044 | next->cctx = cctx_get (aTHX); |
868 | assert (!next->cctx->inuse); |
|
|
869 | next->cctx->inuse = 1; |
|
|
870 | } |
|
|
871 | |
1045 | |
872 | if (prev__cctx != next->cctx) |
1046 | if (prev__cctx != next->cctx) |
873 | { |
1047 | { |
874 | prev__cctx->top_env = PL_top_env; |
1048 | prev__cctx->top_env = PL_top_env; |
875 | PL_top_env = next->cctx->top_env; |
1049 | PL_top_env = next->cctx->top_env; |
… | |
… | |
885 | { |
1059 | { |
886 | struct coro *prev, *next; |
1060 | struct coro *prev, *next; |
887 | }; |
1061 | }; |
888 | |
1062 | |
889 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1063 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
|
|
1064 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
890 | |
1065 | |
891 | /** high level stuff ********************************************************/ |
1066 | /** high level stuff ********************************************************/ |
892 | |
1067 | |
893 | static int |
1068 | static int |
894 | coro_state_destroy (pTHX_ struct coro *coro) |
1069 | coro_state_destroy (pTHX_ struct coro *coro) |
… | |
… | |
911 | |
1086 | |
912 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1087 | if (coro->mainstack && coro->mainstack != main_mainstack) |
913 | { |
1088 | { |
914 | struct coro temp; |
1089 | struct coro temp; |
915 | |
1090 | |
916 | assert (!(coro->flags & CF_RUNNING)); |
|
|
917 | |
|
|
918 | Zero (&temp, 1, struct coro); |
|
|
919 | temp.save = CORO_SAVE_ALL; |
|
|
920 | |
|
|
921 | if (coro->flags & CF_RUNNING) |
1091 | if (coro->flags & CF_RUNNING) |
922 | croak ("FATAL: tried to destroy currently running coroutine"); |
1092 | croak ("FATAL: tried to destroy currently running coroutine"); |
923 | |
1093 | |
924 | save_perl (aTHX_ &temp); |
1094 | save_perl (aTHX_ &temp); |
925 | load_perl (aTHX_ coro); |
1095 | load_perl (aTHX_ coro); |
926 | |
1096 | |
927 | coro_destroy_stacks (aTHX); |
1097 | coro_destroy (aTHX_ coro); |
928 | |
1098 | |
929 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
1099 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
930 | |
1100 | |
931 | coro->mainstack = 0; |
1101 | coro->mainstack = 0; |
932 | } |
1102 | } |
… | |
… | |
977 | #else |
1147 | #else |
978 | # define MGf_DUP 0 |
1148 | # define MGf_DUP 0 |
979 | #endif |
1149 | #endif |
980 | }; |
1150 | }; |
981 | |
1151 | |
982 | static struct coro * |
|
|
983 | SvSTATE_ (pTHX_ SV *coro) |
|
|
984 | { |
|
|
985 | HV *stash; |
|
|
986 | MAGIC *mg; |
|
|
987 | |
|
|
988 | if (SvROK (coro)) |
|
|
989 | coro = SvRV (coro); |
|
|
990 | |
|
|
991 | stash = SvSTASH (coro); |
|
|
992 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
993 | { |
|
|
994 | /* very slow, but rare, check */ |
|
|
995 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
996 | croak ("Coro::State object required"); |
|
|
997 | } |
|
|
998 | |
|
|
999 | mg = CORO_MAGIC (coro); |
|
|
1000 | return (struct coro *)mg->mg_ptr; |
|
|
1001 | } |
|
|
1002 | |
|
|
1003 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
1004 | |
|
|
1005 | static void |
1152 | static void |
1006 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1153 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1007 | { |
1154 | { |
1008 | ta->prev = SvSTATE (prev_sv); |
1155 | ta->prev = SvSTATE (prev_sv); |
1009 | ta->next = SvSTATE (next_sv); |
1156 | ta->next = SvSTATE (next_sv); |
|
|
1157 | TRANSFER_CHECK (*ta); |
1010 | } |
1158 | } |
1011 | |
1159 | |
1012 | static void |
1160 | static void |
1013 | api_transfer (SV *prev_sv, SV *next_sv) |
1161 | api_transfer (SV *prev_sv, SV *next_sv) |
1014 | { |
1162 | { |
1015 | dTHX; |
1163 | dTHX; |
1016 | struct transfer_args ta; |
1164 | struct transfer_args ta; |
1017 | |
1165 | |
1018 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1166 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1019 | TRANSFER (ta); |
1167 | TRANSFER (ta); |
1020 | } |
|
|
1021 | |
|
|
1022 | static int |
|
|
1023 | api_save (SV *coro_sv, int new_save) |
|
|
1024 | { |
|
|
1025 | dTHX; |
|
|
1026 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1027 | int old_save = coro->save; |
|
|
1028 | |
|
|
1029 | if (new_save >= 0) |
|
|
1030 | coro->save = new_save; |
|
|
1031 | |
|
|
1032 | return old_save; |
|
|
1033 | } |
1168 | } |
1034 | |
1169 | |
1035 | /** Coro ********************************************************************/ |
1170 | /** Coro ********************************************************************/ |
1036 | |
1171 | |
1037 | static void |
1172 | static void |
… | |
… | |
1131 | break; |
1266 | break; |
1132 | } |
1267 | } |
1133 | |
1268 | |
1134 | /* free this only after the transfer */ |
1269 | /* free this only after the transfer */ |
1135 | prev_sv = SvRV (coro_current); |
1270 | prev_sv = SvRV (coro_current); |
1136 | SvRV_set (coro_current, next_sv); |
|
|
1137 | ta->prev = SvSTATE (prev_sv); |
1271 | ta->prev = SvSTATE (prev_sv); |
1138 | |
1272 | TRANSFER_CHECK (*ta); |
1139 | assert (ta->next->flags & CF_READY); |
1273 | assert (ta->next->flags & CF_READY); |
1140 | ta->next->flags &= ~CF_READY; |
1274 | ta->next->flags &= ~CF_READY; |
|
|
1275 | SvRV_set (coro_current, next_sv); |
1141 | |
1276 | |
1142 | LOCK; |
1277 | LOCK; |
1143 | free_coro_mortal (aTHX); |
1278 | free_coro_mortal (aTHX); |
1144 | coro_mortal = prev_sv; |
1279 | coro_mortal = prev_sv; |
1145 | UNLOCK; |
1280 | UNLOCK; |
… | |
… | |
1206 | } |
1341 | } |
1207 | else |
1342 | else |
1208 | return 0; |
1343 | return 0; |
1209 | } |
1344 | } |
1210 | |
1345 | |
|
|
1346 | static void |
|
|
1347 | api_trace (SV *coro_sv, int flags) |
|
|
1348 | { |
|
|
1349 | dTHX; |
|
|
1350 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1351 | |
|
|
1352 | if (flags & CC_TRACE) |
|
|
1353 | { |
|
|
1354 | if (!coro->cctx) |
|
|
1355 | coro->cctx = cctx_new (); |
|
|
1356 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1357 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1358 | |
|
|
1359 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
|
|
1360 | } |
|
|
1361 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1362 | { |
|
|
1363 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1364 | |
|
|
1365 | if (coro->flags & CF_RUNNING) |
|
|
1366 | PL_runops = RUNOPS_DEFAULT; |
|
|
1367 | else |
|
|
1368 | coro->runops = RUNOPS_DEFAULT; |
|
|
1369 | } |
|
|
1370 | } |
|
|
1371 | |
1211 | MODULE = Coro::State PACKAGE = Coro::State |
1372 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1212 | |
1373 | |
1213 | PROTOTYPES: DISABLE |
1374 | PROTOTYPES: DISABLE |
1214 | |
1375 | |
1215 | BOOT: |
1376 | BOOT: |
1216 | { |
1377 | { |
1217 | #ifdef USE_ITHREADS |
1378 | #ifdef USE_ITHREADS |
1218 | MUTEX_INIT (&coro_mutex); |
1379 | MUTEX_INIT (&coro_mutex); |
1219 | #endif |
1380 | #endif |
1220 | BOOT_PAGESIZE; |
1381 | BOOT_PAGESIZE; |
1221 | |
1382 | |
|
|
1383 | irsgv = gv_fetchpv ("/", 1, SVt_PV); |
|
|
1384 | |
1222 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1385 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1223 | |
1386 | |
1224 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1387 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1225 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1388 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1226 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1227 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1228 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
|
|
1229 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1389 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
1230 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1390 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
1231 | |
1391 | |
1232 | main_mainstack = PL_mainstack; |
1392 | main_mainstack = PL_mainstack; |
1233 | main_top_env = PL_top_env; |
1393 | main_top_env = PL_top_env; |
1234 | |
1394 | |
1235 | while (main_top_env->je_prev) |
1395 | while (main_top_env->je_prev) |
… | |
… | |
1248 | struct coro *coro; |
1408 | struct coro *coro; |
1249 | HV *hv; |
1409 | HV *hv; |
1250 | int i; |
1410 | int i; |
1251 | |
1411 | |
1252 | Newz (0, coro, 1, struct coro); |
1412 | Newz (0, coro, 1, struct coro); |
1253 | coro->args = newAV (); |
1413 | coro->args = newAV (); |
1254 | coro->save = CORO_SAVE_DEF; |
|
|
1255 | coro->flags = CF_NEW; |
1414 | coro->flags = CF_NEW; |
1256 | |
1415 | |
1257 | if (coro_first) coro_first->prev = coro; |
1416 | if (coro_first) coro_first->prev = coro; |
1258 | coro->next = coro_first; |
1417 | coro->next = coro_first; |
1259 | coro_first = coro; |
1418 | coro_first = coro; |
… | |
… | |
1266 | av_push (coro->args, newSVsv (ST (i))); |
1425 | av_push (coro->args, newSVsv (ST (i))); |
1267 | } |
1426 | } |
1268 | OUTPUT: |
1427 | OUTPUT: |
1269 | RETVAL |
1428 | RETVAL |
1270 | |
1429 | |
1271 | int |
1430 | # these not obviously related functions are all rolled into the same xs |
1272 | save (SV *coro, int new_save = -1) |
1431 | # function to increase chances that they all will call transfer with the same |
1273 | CODE: |
1432 | # stack offset |
1274 | RETVAL = api_save (coro, new_save); |
|
|
1275 | OUTPUT: |
|
|
1276 | RETVAL |
|
|
1277 | |
|
|
1278 | int |
|
|
1279 | save_also (SV *coro_sv, int save_also) |
|
|
1280 | CODE: |
|
|
1281 | { |
|
|
1282 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1283 | RETVAL = coro->save; |
|
|
1284 | coro->save |= save_also; |
|
|
1285 | } |
|
|
1286 | OUTPUT: |
|
|
1287 | RETVAL |
|
|
1288 | |
|
|
1289 | void |
1433 | void |
1290 | _set_stacklevel (...) |
1434 | _set_stacklevel (...) |
1291 | ALIAS: |
1435 | ALIAS: |
1292 | Coro::State::transfer = 1 |
1436 | Coro::State::transfer = 1 |
1293 | Coro::schedule = 2 |
1437 | Coro::schedule = 2 |
… | |
… | |
1379 | if (coro->hv) |
1523 | if (coro->hv) |
1380 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1524 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1381 | } |
1525 | } |
1382 | |
1526 | |
1383 | void |
1527 | void |
1384 | _eval (Coro::State coro, SV *coderef) |
1528 | call (Coro::State coro, SV *coderef) |
|
|
1529 | ALIAS: |
|
|
1530 | eval = 1 |
1385 | CODE: |
1531 | CODE: |
1386 | { |
1532 | { |
1387 | if (coro->mainstack) |
1533 | if (coro->mainstack) |
1388 | { |
1534 | { |
1389 | struct coro temp; |
1535 | struct coro temp; |
1390 | Zero (&temp, 1, struct coro); |
1536 | Zero (&temp, 1, struct coro); |
1391 | temp.save = CORO_SAVE_ALL; |
|
|
1392 | |
1537 | |
1393 | if (!(coro->flags & CF_RUNNING)) |
1538 | if (!(coro->flags & CF_RUNNING)) |
1394 | { |
1539 | { |
1395 | save_perl (aTHX_ &temp); |
1540 | save_perl (aTHX_ &temp); |
1396 | load_perl (aTHX_ coro); |
1541 | load_perl (aTHX_ coro); |
… | |
… | |
1400 | dSP; |
1545 | dSP; |
1401 | ENTER; |
1546 | ENTER; |
1402 | SAVETMPS; |
1547 | SAVETMPS; |
1403 | PUSHMARK (SP); |
1548 | PUSHMARK (SP); |
1404 | PUTBACK; |
1549 | PUTBACK; |
|
|
1550 | if (ix) |
|
|
1551 | eval_sv (coderef, 0); |
|
|
1552 | else |
1405 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1553 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1406 | SPAGAIN; |
1554 | SPAGAIN; |
1407 | FREETMPS; |
1555 | FREETMPS; |
1408 | LEAVE; |
1556 | LEAVE; |
1409 | PUTBACK; |
1557 | PUTBACK; |
1410 | } |
1558 | } |
… | |
… | |
1414 | save_perl (aTHX_ coro); |
1562 | save_perl (aTHX_ coro); |
1415 | load_perl (aTHX_ &temp); |
1563 | load_perl (aTHX_ &temp); |
1416 | } |
1564 | } |
1417 | } |
1565 | } |
1418 | } |
1566 | } |
1419 | |
1567 | |
1420 | SV * |
1568 | SV * |
1421 | is_ready (Coro::State coro) |
1569 | is_ready (Coro::State coro) |
1422 | PROTOTYPE: $ |
1570 | PROTOTYPE: $ |
1423 | ALIAS: |
1571 | ALIAS: |
1424 | is_ready = CF_READY |
1572 | is_ready = CF_READY |
… | |
… | |
1428 | CODE: |
1576 | CODE: |
1429 | RETVAL = boolSV (coro->flags & ix); |
1577 | RETVAL = boolSV (coro->flags & ix); |
1430 | OUTPUT: |
1578 | OUTPUT: |
1431 | RETVAL |
1579 | RETVAL |
1432 | |
1580 | |
|
|
1581 | void |
|
|
1582 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
1583 | |
|
|
1584 | SV * |
|
|
1585 | has_stack (Coro::State coro) |
|
|
1586 | PROTOTYPE: $ |
|
|
1587 | CODE: |
|
|
1588 | RETVAL = boolSV (!!coro->cctx); |
|
|
1589 | OUTPUT: |
|
|
1590 | RETVAL |
|
|
1591 | |
|
|
1592 | int |
|
|
1593 | is_traced (Coro::State coro) |
|
|
1594 | PROTOTYPE: $ |
|
|
1595 | CODE: |
|
|
1596 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
|
|
1597 | OUTPUT: |
|
|
1598 | RETVAL |
|
|
1599 | |
1433 | IV |
1600 | IV |
1434 | rss (Coro::State coro) |
1601 | rss (Coro::State coro) |
1435 | PROTOTYPE: $ |
1602 | PROTOTYPE: $ |
|
|
1603 | ALIAS: |
|
|
1604 | usecount = 1 |
1436 | CODE: |
1605 | CODE: |
1437 | RETVAL = coro_rss (coro); |
1606 | switch (ix) |
|
|
1607 | { |
|
|
1608 | case 0: RETVAL = coro_rss (aTHX_ coro); break; |
|
|
1609 | case 1: RETVAL = coro->usecount; break; |
|
|
1610 | } |
1438 | OUTPUT: |
1611 | OUTPUT: |
1439 | RETVAL |
1612 | RETVAL |
1440 | |
1613 | |
1441 | |
1614 | |
1442 | MODULE = Coro::State PACKAGE = Coro |
1615 | MODULE = Coro::State PACKAGE = Coro |
… | |
… | |
1447 | |
1620 | |
1448 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
1621 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
1449 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
1622 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
1450 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
1623 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
1451 | |
1624 | |
1452 | coro_current = get_sv ("Coro::current", FALSE); |
1625 | coro_current = get_sv ("Coro::current", FALSE); |
1453 | SvREADONLY_on (coro_current); |
1626 | SvREADONLY_on (coro_current); |
1454 | |
1627 | |
1455 | coro_stash = gv_stashpv ("Coro", TRUE); |
1628 | coro_stash = gv_stashpv ("Coro", TRUE); |
1456 | |
1629 | |
1457 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1630 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
1466 | |
1639 | |
1467 | { |
1640 | { |
1468 | SV *sv = perl_get_sv("Coro::API", 1); |
1641 | SV *sv = perl_get_sv("Coro::API", 1); |
1469 | |
1642 | |
1470 | coroapi.schedule = api_schedule; |
1643 | coroapi.schedule = api_schedule; |
1471 | coroapi.save = api_save; |
|
|
1472 | coroapi.cede = api_cede; |
1644 | coroapi.cede = api_cede; |
1473 | coroapi.cede_notself = api_cede_notself; |
1645 | coroapi.cede_notself = api_cede_notself; |
1474 | coroapi.ready = api_ready; |
1646 | coroapi.ready = api_ready; |
1475 | coroapi.is_ready = api_is_ready; |
1647 | coroapi.is_ready = api_is_ready; |
1476 | coroapi.nready = &coro_nready; |
1648 | coroapi.nready = &coro_nready; |
… | |
… | |
1526 | RETVAL = coro_nready; |
1698 | RETVAL = coro_nready; |
1527 | OUTPUT: |
1699 | OUTPUT: |
1528 | RETVAL |
1700 | RETVAL |
1529 | |
1701 | |
1530 | # for async_pool speedup |
1702 | # for async_pool speedup |
1531 | SV * |
1703 | void |
1532 | _pool_1 (...) |
1704 | _pool_1 (SV *cb) |
1533 | CODE: |
1705 | CODE: |
1534 | { |
1706 | { |
1535 | int i, len; |
1707 | struct coro *coro = SvSTATE (coro_current); |
1536 | HV *hv = (HV *)SvRV (coro_current); |
1708 | HV *hv = (HV *)SvRV (coro_current); |
1537 | AV *defav = GvAV (PL_defgv); |
1709 | AV *defav = GvAV (PL_defgv); |
1538 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1710 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1539 | AV *invoke_av; |
1711 | AV *invoke_av; |
|
|
1712 | int i, len; |
1540 | |
1713 | |
1541 | if (!invoke) |
1714 | if (!invoke) |
1542 | XSRETURN_EMPTY; |
1715 | croak ("\3terminate\2\n"); |
|
|
1716 | |
|
|
1717 | SvREFCNT_dec (coro->saved_deffh); |
|
|
1718 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1543 | |
1719 | |
1544 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1720 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1545 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1721 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1546 | |
1722 | |
1547 | invoke_av = (AV *)SvRV (invoke); |
1723 | invoke_av = (AV *)SvRV (invoke); |
1548 | len = av_len (invoke_av); |
1724 | len = av_len (invoke_av); |
1549 | |
1725 | |
1550 | RETVAL = SvREFCNT_inc (AvARRAY (invoke_av)[0]); |
1726 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
1551 | |
1727 | |
1552 | if (len > 0) |
1728 | if (len > 0) |
1553 | { |
1729 | { |
1554 | av_clear (defav); |
|
|
1555 | av_extend (defav, len); |
1730 | av_fill (defav, len - 1); |
1556 | for (i = 0; i < len; ++i) |
1731 | for (i = 0; i < len; ++i) |
1557 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
1732 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
1558 | } |
1733 | } |
1559 | |
1734 | |
1560 | SvREFCNT_dec (invoke); |
1735 | SvREFCNT_dec (invoke); |
1561 | } |
1736 | } |
1562 | OUTPUT: |
|
|
1563 | RETVAL |
|
|
1564 | |
1737 | |
1565 | void |
1738 | void |
1566 | _pool_2 (...) |
1739 | _pool_2 (SV *cb) |
1567 | CODE: |
1740 | CODE: |
1568 | { |
1741 | { |
1569 | struct coro *coro = SvSTATE (coro_current); |
1742 | struct coro *coro = SvSTATE (coro_current); |
1570 | |
1743 | |
|
|
1744 | sv_setsv (cb, &PL_sv_undef); |
|
|
1745 | |
|
|
1746 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1747 | coro->saved_deffh = 0; |
|
|
1748 | |
1571 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
1749 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1572 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1750 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1573 | XSRETURN_YES; |
1751 | croak ("\3terminate\2\n"); |
1574 | |
1752 | |
1575 | av_clear (GvAV (PL_defgv)); |
1753 | av_clear (GvAV (PL_defgv)); |
1576 | hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1754 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1577 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1755 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1578 | coro->save = CORO_SAVE_DEF; |
1756 | |
1579 | coro->prio = 0; |
1757 | coro->prio = 0; |
|
|
1758 | |
|
|
1759 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1760 | api_trace (coro_current, 0); |
|
|
1761 | |
1580 | av_push (av_async_pool, newSVsv (coro_current)); |
1762 | av_push (av_async_pool, newSVsv (coro_current)); |
1581 | |
|
|
1582 | XSRETURN_NO; |
|
|
1583 | } |
1763 | } |
1584 | |
1764 | |
1585 | |
1765 | |
1586 | MODULE = Coro::State PACKAGE = Coro::AIO |
1766 | MODULE = Coro::State PACKAGE = Coro::AIO |
1587 | |
1767 | |