… | |
… | |
192 | |
192 | |
193 | /* optionally saved, might be zero */ |
193 | /* optionally saved, might be zero */ |
194 | AV *defav; /* @_ */ |
194 | AV *defav; /* @_ */ |
195 | SV *defsv; /* $_ */ |
195 | SV *defsv; /* $_ */ |
196 | SV *errsv; /* $@ */ |
196 | SV *errsv; /* $@ */ |
197 | IO *deffh; /* default filehandle */ |
197 | SV *deffh; /* default filehandle */ |
198 | SV *irssv; /* $/ */ |
198 | SV *irssv; /* $/ */ |
199 | SV *irssv_sv; /* real $/ cache */ |
199 | SV *irssv_sv; /* real $/ cache */ |
200 | |
200 | |
201 | #define VAR(name,type) type name; |
201 | #define VAR(name,type) type name; |
202 | # include "state.h" |
202 | # include "state.h" |
203 | #undef VAR |
203 | #undef VAR |
204 | |
204 | |
205 | /* statistics */ |
205 | /* statistics */ |
206 | int usecount; /* number of switches to this coro */ |
206 | int usecount; /* number of transfers to this coro */ |
207 | |
207 | |
208 | /* coro process data */ |
208 | /* coro process data */ |
209 | int prio; |
209 | int prio; |
|
|
210 | //SV *throw; |
|
|
211 | |
|
|
212 | /* async_pool */ |
|
|
213 | SV *saved_deffh; |
210 | |
214 | |
211 | /* linked list */ |
215 | /* linked list */ |
212 | struct coro *next, *prev; |
216 | struct coro *next, *prev; |
213 | HV *hv; /* the perl hash associated with this coro, if any */ |
217 | HV *hv; /* the perl hash associated with this coro, if any */ |
214 | }; |
218 | }; |
… | |
… | |
383 | { |
387 | { |
384 | #define VAR(name,type) PL_ ## name = c->name; |
388 | #define VAR(name,type) PL_ ## name = c->name; |
385 | # include "state.h" |
389 | # include "state.h" |
386 | #undef VAR |
390 | #undef VAR |
387 | |
391 | |
388 | GvSV (PL_defgv) = c->defsv; |
392 | GvSV (PL_defgv) = c->defsv; |
389 | GvAV (PL_defgv) = c->defav; |
393 | GvAV (PL_defgv) = c->defav; |
390 | GvSV (PL_errgv) = c->errsv; |
394 | GvSV (PL_errgv) = c->errsv; |
391 | GvIOp(PL_defoutgv) = c->deffh; |
|
|
392 | PL_rs = c->irssv; |
|
|
393 | GvSV (irsgv) = c->irssv_sv; |
395 | GvSV (irsgv) = c->irssv_sv; |
394 | |
396 | |
395 | { |
397 | { |
396 | dSP; |
398 | dSP; |
397 | CV *cv; |
399 | CV *cv; |
398 | |
400 | |
… | |
… | |
459 | } |
461 | } |
460 | |
462 | |
461 | c->defav = GvAV (PL_defgv); |
463 | c->defav = GvAV (PL_defgv); |
462 | c->defsv = DEFSV; |
464 | c->defsv = DEFSV; |
463 | c->errsv = ERRSV; |
465 | c->errsv = ERRSV; |
464 | c->deffh = GvIOp (PL_defoutgv); |
|
|
465 | c->irssv = PL_rs; |
|
|
466 | c->irssv_sv = GvSV (irsgv); |
466 | c->irssv_sv = GvSV (irsgv); |
467 | |
467 | |
468 | #define VAR(name,type)c->name = PL_ ## name; |
468 | #define VAR(name,type)c->name = PL_ ## name; |
469 | # include "state.h" |
469 | # include "state.h" |
470 | #undef VAR |
470 | #undef VAR |
… | |
… | |
551 | } |
551 | } |
552 | |
552 | |
553 | static size_t |
553 | static size_t |
554 | coro_rss (pTHX_ struct coro *coro) |
554 | coro_rss (pTHX_ struct coro *coro) |
555 | { |
555 | { |
556 | size_t rss = sizeof (coro); |
556 | size_t rss = sizeof (*coro); |
557 | |
557 | |
558 | if (coro->mainstack) |
558 | if (coro->mainstack) |
559 | { |
559 | { |
560 | if (coro->flags & CF_RUNNING) |
560 | if (coro->flags & CF_RUNNING) |
561 | { |
561 | { |
… | |
… | |
563 | # include "state.h" |
563 | # include "state.h" |
564 | #undef VAR |
564 | #undef VAR |
565 | } |
565 | } |
566 | |
566 | |
567 | rss += sizeof (coro->curstackinfo); |
567 | rss += sizeof (coro->curstackinfo); |
568 | 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 *); |
569 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
569 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
570 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
570 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
571 | rss += coro->tmps_max * sizeof (SV *); |
571 | rss += coro->tmps_max * sizeof (SV *); |
572 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
572 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
573 | rss += coro->scopestack_max * sizeof (I32); |
573 | rss += coro->scopestack_max * sizeof (I32); |
574 | rss += coro->savestack_max * sizeof (ANY); |
574 | rss += coro->savestack_max * sizeof (ANY); |
575 | |
575 | |
… | |
… | |
582 | } |
582 | } |
583 | |
583 | |
584 | /** coroutine stack handling ************************************************/ |
584 | /** coroutine stack handling ************************************************/ |
585 | |
585 | |
586 | static void |
586 | static void |
587 | setup_coro (pTHX_ struct coro *coro) |
587 | coro_setup (pTHX_ struct coro *coro) |
588 | { |
588 | { |
589 | /* |
589 | /* |
590 | * emulate part of the perl startup here. |
590 | * emulate part of the perl startup here. |
591 | */ |
591 | */ |
592 | coro_init_stacks (aTHX); |
592 | coro_init_stacks (aTHX); |
… | |
… | |
601 | PL_restartop = 0; |
601 | PL_restartop = 0; |
602 | |
602 | |
603 | GvSV (PL_defgv) = NEWSV (0, 0); |
603 | GvSV (PL_defgv) = NEWSV (0, 0); |
604 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
604 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
605 | GvSV (PL_errgv) = NEWSV (0, 0); |
605 | GvSV (PL_errgv) = NEWSV (0, 0); |
606 | GvSV (irsgv) = newSVsv (PL_rs); |
606 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
607 | PL_rs = newSVpvn ("\n", 1); |
607 | PL_rs = newSVsv (GvSV (irsgv)); |
608 | |
608 | |
609 | { |
609 | { |
610 | IO *io = newIO (); |
610 | IO *io = newIO (); |
|
|
611 | PL_defoutgv = newGVgen ("Coro"); |
611 | GvIOp(PL_defoutgv) = io; |
612 | GvIOp(PL_defoutgv) = io; |
612 | IoTYPE (io) = IoTYPE_WRONLY; |
613 | IoTYPE (io) = IoTYPE_WRONLY; |
613 | IoOFP (io) = IoIFP (io) = PerlIO_stdout (); |
614 | IoOFP (io) = IoIFP (io) = PerlIO_stdout (); |
614 | IoFLAGS (io) |= IOf_FLUSH; |
615 | IoFLAGS (io) |= IOf_FLUSH; |
615 | } |
616 | } |
… | |
… | |
632 | |
633 | |
633 | ENTER; /* necessary e.g. for dounwind */ |
634 | ENTER; /* necessary e.g. for dounwind */ |
634 | } |
635 | } |
635 | |
636 | |
636 | static void |
637 | static void |
637 | destroy_coro (pTHX_ struct coro *coro) |
638 | coro_destroy (pTHX_ struct coro *coro) |
638 | { |
639 | { |
639 | if (!IN_DESTRUCT) |
640 | if (!IN_DESTRUCT) |
640 | { |
641 | { |
641 | /* restore all saved variables and stuff */ |
642 | /* restore all saved variables and stuff */ |
642 | LEAVE_SCOPE (0); |
643 | LEAVE_SCOPE (0); |
… | |
… | |
654 | } |
655 | } |
655 | |
656 | |
656 | SvREFCNT_dec (GvSV (PL_defgv)); |
657 | SvREFCNT_dec (GvSV (PL_defgv)); |
657 | SvREFCNT_dec (GvAV (PL_defgv)); |
658 | SvREFCNT_dec (GvAV (PL_defgv)); |
658 | SvREFCNT_dec (GvSV (PL_errgv)); |
659 | SvREFCNT_dec (GvSV (PL_errgv)); |
659 | SvREFCNT_dec (GvIOp(PL_defoutgv)); |
660 | SvREFCNT_dec (PL_defoutgv); |
660 | SvREFCNT_dec (PL_rs); |
661 | SvREFCNT_dec (PL_rs); |
661 | SvREFCNT_dec (GvSV (irsgv)); |
662 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
663 | |
|
|
664 | SvREFCNT_dec (coro->saved_deffh); |
|
|
665 | //SvREFCNT_dec (coro->throw); |
662 | |
666 | |
663 | coro_destroy_stacks (aTHX); |
667 | coro_destroy_stacks (aTHX); |
664 | } |
668 | } |
665 | |
669 | |
666 | static void |
670 | static void |
… | |
… | |
1017 | /* need to start coroutine */ |
1021 | /* need to start coroutine */ |
1018 | next->flags &= ~CF_NEW; |
1022 | next->flags &= ~CF_NEW; |
1019 | /* first get rid of the old state */ |
1023 | /* first get rid of the old state */ |
1020 | save_perl (aTHX_ prev); |
1024 | save_perl (aTHX_ prev); |
1021 | /* setup coroutine call */ |
1025 | /* setup coroutine call */ |
1022 | setup_coro (aTHX_ next); |
1026 | coro_setup (aTHX_ next); |
1023 | } |
1027 | } |
1024 | else |
1028 | else |
1025 | { |
1029 | { |
1026 | /* coroutine already started */ |
1030 | /* coroutine already started */ |
1027 | save_perl (aTHX_ prev); |
1031 | save_perl (aTHX_ prev); |
… | |
… | |
1095 | croak ("FATAL: tried to destroy currently running coroutine"); |
1099 | croak ("FATAL: tried to destroy currently running coroutine"); |
1096 | |
1100 | |
1097 | save_perl (aTHX_ &temp); |
1101 | save_perl (aTHX_ &temp); |
1098 | load_perl (aTHX_ coro); |
1102 | load_perl (aTHX_ coro); |
1099 | |
1103 | |
1100 | destroy_coro (aTHX_ coro); |
1104 | coro_destroy (aTHX_ coro); |
1101 | |
1105 | |
1102 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
1106 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
1103 | |
1107 | |
1104 | coro->mainstack = 0; |
1108 | coro->mainstack = 0; |
1105 | } |
1109 | } |
… | |
… | |
1623 | |
1627 | |
1624 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
1628 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
1625 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
1629 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
1626 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
1630 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
1627 | |
1631 | |
1628 | coro_current = get_sv ("Coro::current", FALSE); |
1632 | coro_current = get_sv ("Coro::current", FALSE); |
1629 | SvREADONLY_on (coro_current); |
1633 | SvREADONLY_on (coro_current); |
1630 | |
1634 | |
1631 | coro_stash = gv_stashpv ("Coro", TRUE); |
1635 | coro_stash = gv_stashpv ("Coro", TRUE); |
1632 | |
1636 | |
1633 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1637 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
1705 | # for async_pool speedup |
1709 | # for async_pool speedup |
1706 | void |
1710 | void |
1707 | _pool_1 (SV *cb) |
1711 | _pool_1 (SV *cb) |
1708 | CODE: |
1712 | CODE: |
1709 | { |
1713 | { |
1710 | int i, len; |
1714 | struct coro *coro = SvSTATE (coro_current); |
1711 | HV *hv = (HV *)SvRV (coro_current); |
1715 | HV *hv = (HV *)SvRV (coro_current); |
1712 | AV *defav = GvAV (PL_defgv); |
1716 | AV *defav = GvAV (PL_defgv); |
1713 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1717 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1714 | AV *invoke_av; |
1718 | AV *invoke_av; |
|
|
1719 | int i, len; |
1715 | |
1720 | |
1716 | if (!invoke) |
1721 | if (!invoke) |
1717 | croak ("\3terminate\2\n"); |
1722 | croak ("\3terminate\2\n"); |
|
|
1723 | |
|
|
1724 | SvREFCNT_dec (coro->saved_deffh); |
|
|
1725 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1718 | |
1726 | |
1719 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1727 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1720 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1728 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1721 | |
1729 | |
1722 | invoke_av = (AV *)SvRV (invoke); |
1730 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
1740 | { |
1748 | { |
1741 | struct coro *coro = SvSTATE (coro_current); |
1749 | struct coro *coro = SvSTATE (coro_current); |
1742 | |
1750 | |
1743 | sv_setsv (cb, &PL_sv_undef); |
1751 | sv_setsv (cb, &PL_sv_undef); |
1744 | |
1752 | |
|
|
1753 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1754 | coro->saved_deffh = 0; |
|
|
1755 | |
1745 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1756 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1746 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1757 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1747 | croak ("\3terminate\2\n"); |
1758 | croak ("\3terminate\2\n"); |
1748 | |
1759 | |
1749 | av_clear (GvAV (PL_defgv)); |
1760 | av_clear (GvAV (PL_defgv)); |