… | |
… | |
75 | /* 5.8.7 */ |
75 | /* 5.8.7 */ |
76 | #ifndef SvRV_set |
76 | #ifndef SvRV_set |
77 | # define SvRV_set(s,v) SvRV(s) = (v) |
77 | # define SvRV_set(s,v) SvRV(s) = (v) |
78 | #endif |
78 | #endif |
79 | |
79 | |
|
|
80 | /* 5.8.8 */ |
|
|
81 | #ifndef GV_NOTQUAL |
|
|
82 | # define GV_NOTQUAL 0 |
|
|
83 | #endif |
|
|
84 | #ifndef newSV |
|
|
85 | # define newSV(l) NEWSV(0,l) |
|
|
86 | #endif |
|
|
87 | |
80 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
88 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
81 | # undef CORO_STACKGUARD |
89 | # undef CORO_STACKGUARD |
82 | #endif |
90 | #endif |
83 | |
91 | |
84 | #ifndef CORO_STACKGUARD |
92 | #ifndef CORO_STACKGUARD |
… | |
… | |
132 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
140 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
133 | static JMPENV *main_top_env; |
141 | static JMPENV *main_top_env; |
134 | static HV *coro_state_stash, *coro_stash; |
142 | static HV *coro_state_stash, *coro_stash; |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
143 | static SV *coro_mortal; /* will be freed after next transfer */ |
136 | |
144 | |
137 | static GV *irsgv; /* $/ */ |
145 | static GV *irsgv; /* $/ */ |
|
|
146 | static GV *stdoutgv; /* *STDOUT */ |
138 | |
147 | |
139 | /* async_pool helper stuff */ |
148 | /* async_pool helper stuff */ |
140 | static SV *sv_pool_rss; |
149 | static SV *sv_pool_rss; |
141 | static SV *sv_pool_size; |
150 | static SV *sv_pool_size; |
142 | static AV *av_async_pool; |
151 | static AV *av_async_pool; |
… | |
… | |
192 | |
201 | |
193 | /* optionally saved, might be zero */ |
202 | /* optionally saved, might be zero */ |
194 | AV *defav; /* @_ */ |
203 | AV *defav; /* @_ */ |
195 | SV *defsv; /* $_ */ |
204 | SV *defsv; /* $_ */ |
196 | SV *errsv; /* $@ */ |
205 | SV *errsv; /* $@ */ |
197 | IO *deffh; /* default filehandle */ |
206 | SV *deffh; /* default filehandle */ |
198 | SV *irssv; /* $/ */ |
207 | SV *irssv; /* $/ */ |
199 | SV *irssv_sv; /* real $/ cache */ |
208 | SV *irssv_sv; /* real $/ cache */ |
200 | |
209 | |
201 | #define VAR(name,type) type name; |
210 | #define VAR(name,type) type name; |
202 | # include "state.h" |
211 | # include "state.h" |
203 | #undef VAR |
212 | #undef VAR |
204 | |
213 | |
205 | /* statistics */ |
214 | /* statistics */ |
206 | int usecount; /* number of switches to this coro */ |
215 | int usecount; /* number of transfers to this coro */ |
207 | |
216 | |
208 | /* coro process data */ |
217 | /* coro process data */ |
209 | int prio; |
218 | int prio; |
|
|
219 | //SV *throw; |
|
|
220 | |
|
|
221 | /* async_pool */ |
|
|
222 | SV *saved_deffh; |
210 | |
223 | |
211 | /* linked list */ |
224 | /* linked list */ |
212 | struct coro *next, *prev; |
225 | struct coro *next, *prev; |
213 | HV *hv; /* the perl hash associated with this coro, if any */ |
226 | HV *hv; /* the perl hash associated with this coro, if any */ |
214 | }; |
227 | }; |
… | |
… | |
386 | #undef VAR |
399 | #undef VAR |
387 | |
400 | |
388 | GvSV (PL_defgv) = c->defsv; |
401 | GvSV (PL_defgv) = c->defsv; |
389 | GvAV (PL_defgv) = c->defav; |
402 | GvAV (PL_defgv) = c->defav; |
390 | GvSV (PL_errgv) = c->errsv; |
403 | GvSV (PL_errgv) = c->errsv; |
391 | PL_defoutgv = c->deffh; |
|
|
392 | PL_rs = c->irssv; |
|
|
393 | GvSV (irsgv) = c->irssv_sv; |
404 | GvSV (irsgv) = c->irssv_sv; |
394 | |
405 | |
395 | { |
406 | { |
396 | dSP; |
407 | dSP; |
397 | CV *cv; |
408 | CV *cv; |
… | |
… | |
459 | } |
470 | } |
460 | |
471 | |
461 | c->defav = GvAV (PL_defgv); |
472 | c->defav = GvAV (PL_defgv); |
462 | c->defsv = DEFSV; |
473 | c->defsv = DEFSV; |
463 | c->errsv = ERRSV; |
474 | c->errsv = ERRSV; |
464 | c->deffh = PL_defoutgv; |
|
|
465 | c->irssv = PL_rs; |
|
|
466 | c->irssv_sv = GvSV (irsgv); |
475 | c->irssv_sv = GvSV (irsgv); |
467 | |
476 | |
468 | #define VAR(name,type)c->name = PL_ ## name; |
477 | #define VAR(name,type)c->name = PL_ ## name; |
469 | # include "state.h" |
478 | # include "state.h" |
470 | #undef VAR |
479 | #undef VAR |
… | |
… | |
551 | } |
560 | } |
552 | |
561 | |
553 | static size_t |
562 | static size_t |
554 | coro_rss (pTHX_ struct coro *coro) |
563 | coro_rss (pTHX_ struct coro *coro) |
555 | { |
564 | { |
556 | size_t rss = sizeof (coro); |
565 | size_t rss = sizeof (*coro); |
557 | |
566 | |
558 | if (coro->mainstack) |
567 | if (coro->mainstack) |
559 | { |
568 | { |
560 | if (coro->flags & CF_RUNNING) |
569 | if (coro->flags & CF_RUNNING) |
561 | { |
570 | { |
… | |
… | |
563 | # include "state.h" |
572 | # include "state.h" |
564 | #undef VAR |
573 | #undef VAR |
565 | } |
574 | } |
566 | |
575 | |
567 | rss += sizeof (coro->curstackinfo); |
576 | rss += sizeof (coro->curstackinfo); |
568 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
577 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
569 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
578 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
570 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
579 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
571 | rss += coro->tmps_max * sizeof (SV *); |
580 | rss += coro->tmps_max * sizeof (SV *); |
572 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
581 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
573 | rss += coro->scopestack_max * sizeof (I32); |
582 | rss += coro->scopestack_max * sizeof (I32); |
574 | rss += coro->savestack_max * sizeof (ANY); |
583 | rss += coro->savestack_max * sizeof (ANY); |
575 | |
584 | |
… | |
… | |
598 | PL_curpm = 0; |
607 | PL_curpm = 0; |
599 | PL_localizing = 0; |
608 | PL_localizing = 0; |
600 | PL_dirty = 0; |
609 | PL_dirty = 0; |
601 | PL_restartop = 0; |
610 | PL_restartop = 0; |
602 | |
611 | |
603 | GvSV (PL_defgv) = NEWSV (0, 0); |
612 | GvSV (PL_defgv) = newSV (0); |
604 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
613 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
605 | GvSV (PL_errgv) = NEWSV (0, 0); |
614 | GvSV (PL_errgv) = newSV (0); |
606 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
615 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
607 | PL_rs = newSVsv (GvSV (irsgv)); |
616 | PL_rs = newSVsv (GvSV (irsgv)); |
|
|
617 | PL_defoutgv = SvREFCNT_inc (stdoutgv); |
608 | |
618 | |
609 | { |
619 | ENTER; /* necessary e.g. for dounwind */ |
610 | IO *io = newIO (); |
|
|
611 | PL_defoutgv = newGVgen ("Coro"); |
|
|
612 | GvIOp(PL_defoutgv) = io; |
|
|
613 | IoTYPE (io) = IoTYPE_WRONLY; |
|
|
614 | IoOFP (io) = IoIFP (io) = PerlIO_stdout (); |
|
|
615 | IoFLAGS (io) |= IOf_FLUSH; |
|
|
616 | } |
|
|
617 | |
620 | |
618 | { |
621 | { |
619 | dSP; |
622 | dSP; |
620 | LOGOP myop; |
623 | LOGOP myop; |
621 | |
624 | |
622 | Zero (&myop, 1, LOGOP); |
625 | Zero (&myop, 1, LOGOP); |
623 | myop.op_next = Nullop; |
626 | myop.op_next = Nullop; |
624 | myop.op_flags = OPf_WANT_VOID; |
627 | myop.op_flags = OPf_WANT_VOID; |
625 | |
628 | |
626 | PUSHMARK (SP); |
629 | PUSHMARK (SP); |
627 | XPUSHs (av_shift (GvAV (PL_defgv))); |
630 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
628 | PUTBACK; |
631 | PUTBACK; |
629 | PL_op = (OP *)&myop; |
632 | PL_op = (OP *)&myop; |
630 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
633 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
631 | SPAGAIN; |
634 | SPAGAIN; |
632 | } |
635 | } |
633 | |
|
|
634 | ENTER; /* necessary e.g. for dounwind */ |
|
|
635 | } |
636 | } |
636 | |
637 | |
637 | static void |
638 | static void |
638 | coro_destroy (pTHX_ struct coro *coro) |
639 | coro_destroy (pTHX_ struct coro *coro) |
639 | { |
640 | { |
… | |
… | |
658 | SvREFCNT_dec (GvAV (PL_defgv)); |
659 | SvREFCNT_dec (GvAV (PL_defgv)); |
659 | SvREFCNT_dec (GvSV (PL_errgv)); |
660 | SvREFCNT_dec (GvSV (PL_errgv)); |
660 | SvREFCNT_dec (PL_defoutgv); |
661 | SvREFCNT_dec (PL_defoutgv); |
661 | SvREFCNT_dec (PL_rs); |
662 | SvREFCNT_dec (PL_rs); |
662 | SvREFCNT_dec (GvSV (irsgv)); |
663 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
664 | |
|
|
665 | SvREFCNT_dec (coro->saved_deffh); |
|
|
666 | //SvREFCNT_dec (coro->throw); |
663 | |
667 | |
664 | coro_destroy_stacks (aTHX); |
668 | coro_destroy_stacks (aTHX); |
665 | } |
669 | } |
666 | |
670 | |
667 | static void |
671 | static void |
… | |
… | |
704 | bot = PL_stack_base + cx->blk_oldsp + 1; |
708 | bot = PL_stack_base + cx->blk_oldsp + 1; |
705 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
709 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
706 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
710 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
707 | : bot; |
711 | : bot; |
708 | |
712 | |
|
|
713 | av_extend (av, top - bot); |
709 | while (bot < top) |
714 | while (bot < top) |
710 | av_push (av, SvREFCNT_inc (*bot++)); |
715 | av_push (av, SvREFCNT_inc (*bot++)); |
711 | |
716 | |
712 | PL_runops = RUNOPS_DEFAULT; |
717 | PL_runops = RUNOPS_DEFAULT; |
713 | ENTER; |
718 | ENTER; |
… | |
… | |
925 | Safefree (cctx->sptr); |
930 | Safefree (cctx->sptr); |
926 | |
931 | |
927 | Safefree (cctx); |
932 | Safefree (cctx); |
928 | } |
933 | } |
929 | |
934 | |
|
|
935 | /* wether this cctx should be destructed */ |
|
|
936 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
|
|
937 | |
930 | static coro_cctx * |
938 | static coro_cctx * |
931 | cctx_get (pTHX) |
939 | cctx_get (pTHX) |
932 | { |
940 | { |
933 | while (cctx_first) |
941 | while (cctx_first) |
934 | { |
942 | { |
935 | coro_cctx *cctx = cctx_first; |
943 | coro_cctx *cctx = cctx_first; |
936 | cctx_first = cctx->next; |
944 | cctx_first = cctx->next; |
937 | --cctx_idle; |
945 | --cctx_idle; |
938 | |
946 | |
939 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
947 | if (!CCTX_EXPIRED (cctx)) |
940 | return cctx; |
948 | return cctx; |
941 | |
949 | |
942 | cctx_destroy (cctx); |
950 | cctx_destroy (cctx); |
943 | } |
951 | } |
944 | |
952 | |
… | |
… | |
1037 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1045 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1038 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1046 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1039 | |
1047 | |
1040 | prev->cctx = 0; |
1048 | prev->cctx = 0; |
1041 | |
1049 | |
|
|
1050 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
|
|
1051 | if (CCTX_EXPIRED (prev__cctx)) |
|
|
1052 | next->cctx = cctx_get (aTHX); |
|
|
1053 | |
1042 | cctx_put (prev__cctx); |
1054 | cctx_put (prev__cctx); |
1043 | } |
1055 | } |
1044 | |
1056 | |
1045 | ++next->usecount; |
1057 | ++next->usecount; |
1046 | |
1058 | |
… | |
… | |
1382 | #ifdef USE_ITHREADS |
1394 | #ifdef USE_ITHREADS |
1383 | MUTEX_INIT (&coro_mutex); |
1395 | MUTEX_INIT (&coro_mutex); |
1384 | #endif |
1396 | #endif |
1385 | BOOT_PAGESIZE; |
1397 | BOOT_PAGESIZE; |
1386 | |
1398 | |
1387 | irsgv = gv_fetchpv ("/", 1, SVt_PV); |
1399 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
|
|
1400 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1388 | |
1401 | |
1389 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1402 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1390 | |
1403 | |
1391 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1404 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1392 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1405 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1423 | |
1436 | |
1424 | coro->hv = hv = newHV (); |
1437 | coro->hv = hv = newHV (); |
1425 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1438 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1426 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1439 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1427 | |
1440 | |
|
|
1441 | av_extend (coro->args, items - 1); |
1428 | for (i = 1; i < items; i++) |
1442 | for (i = 1; i < items; i++) |
1429 | av_push (coro->args, newSVsv (ST (i))); |
1443 | av_push (coro->args, newSVsv (ST (i))); |
1430 | } |
1444 | } |
1431 | OUTPUT: |
1445 | OUTPUT: |
1432 | RETVAL |
1446 | RETVAL |
… | |
… | |
1624 | |
1638 | |
1625 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
1639 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
1626 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
1640 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
1627 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
1641 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
1628 | |
1642 | |
1629 | coro_current = get_sv ("Coro::current", FALSE); |
1643 | coro_current = get_sv ("Coro::current", FALSE); |
1630 | SvREADONLY_on (coro_current); |
1644 | SvREADONLY_on (coro_current); |
1631 | |
1645 | |
1632 | coro_stash = gv_stashpv ("Coro", TRUE); |
1646 | coro_stash = gv_stashpv ("Coro", TRUE); |
1633 | |
1647 | |
1634 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1648 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
1706 | # for async_pool speedup |
1720 | # for async_pool speedup |
1707 | void |
1721 | void |
1708 | _pool_1 (SV *cb) |
1722 | _pool_1 (SV *cb) |
1709 | CODE: |
1723 | CODE: |
1710 | { |
1724 | { |
1711 | int i, len; |
1725 | struct coro *coro = SvSTATE (coro_current); |
1712 | HV *hv = (HV *)SvRV (coro_current); |
1726 | HV *hv = (HV *)SvRV (coro_current); |
1713 | AV *defav = GvAV (PL_defgv); |
1727 | AV *defav = GvAV (PL_defgv); |
1714 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1728 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1715 | AV *invoke_av; |
1729 | AV *invoke_av; |
|
|
1730 | int i, len; |
1716 | |
1731 | |
1717 | if (!invoke) |
1732 | if (!invoke) |
1718 | croak ("\3terminate\2\n"); |
1733 | croak ("\3terminate\2\n"); |
|
|
1734 | |
|
|
1735 | SvREFCNT_dec (coro->saved_deffh); |
|
|
1736 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1719 | |
1737 | |
1720 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1738 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1721 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1739 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1722 | |
1740 | |
1723 | invoke_av = (AV *)SvRV (invoke); |
1741 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
1741 | { |
1759 | { |
1742 | struct coro *coro = SvSTATE (coro_current); |
1760 | struct coro *coro = SvSTATE (coro_current); |
1743 | |
1761 | |
1744 | sv_setsv (cb, &PL_sv_undef); |
1762 | sv_setsv (cb, &PL_sv_undef); |
1745 | |
1763 | |
|
|
1764 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1765 | coro->saved_deffh = 0; |
|
|
1766 | |
1746 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1767 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1747 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1768 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1748 | croak ("\3terminate\2\n"); |
1769 | croak ("\3terminate\2\n"); |
1749 | |
1770 | |
1750 | av_clear (GvAV (PL_defgv)); |
1771 | av_clear (GvAV (PL_defgv)); |