… | |
… | |
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 | GV *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" |
… | |
… | |
208 | /* coro process data */ |
217 | /* coro process data */ |
209 | int prio; |
218 | int prio; |
210 | //SV *throw; |
219 | //SV *throw; |
211 | |
220 | |
212 | /* async_pool */ |
221 | /* async_pool */ |
213 | GV *asp_deffh; |
222 | SV *saved_deffh; |
214 | |
223 | |
215 | /* linked list */ |
224 | /* linked list */ |
216 | struct coro *next, *prev; |
225 | struct coro *next, *prev; |
217 | HV *hv; /* the perl hash associated with this coro, if any */ |
226 | HV *hv; /* the perl hash associated with this coro, if any */ |
218 | }; |
227 | }; |
… | |
… | |
390 | #undef VAR |
399 | #undef VAR |
391 | |
400 | |
392 | GvSV (PL_defgv) = c->defsv; |
401 | GvSV (PL_defgv) = c->defsv; |
393 | GvAV (PL_defgv) = c->defav; |
402 | GvAV (PL_defgv) = c->defav; |
394 | GvSV (PL_errgv) = c->errsv; |
403 | GvSV (PL_errgv) = c->errsv; |
395 | PL_defoutgv = c->deffh; |
|
|
396 | PL_rs = c->irssv; |
|
|
397 | GvSV (irsgv) = c->irssv_sv; |
404 | GvSV (irsgv) = c->irssv_sv; |
398 | |
405 | |
399 | { |
406 | { |
400 | dSP; |
407 | dSP; |
401 | CV *cv; |
408 | CV *cv; |
… | |
… | |
463 | } |
470 | } |
464 | |
471 | |
465 | c->defav = GvAV (PL_defgv); |
472 | c->defav = GvAV (PL_defgv); |
466 | c->defsv = DEFSV; |
473 | c->defsv = DEFSV; |
467 | c->errsv = ERRSV; |
474 | c->errsv = ERRSV; |
468 | c->deffh = PL_defoutgv; |
|
|
469 | c->irssv = PL_rs; |
|
|
470 | c->irssv_sv = GvSV (irsgv); |
475 | c->irssv_sv = GvSV (irsgv); |
471 | |
476 | |
472 | #define VAR(name,type)c->name = PL_ ## name; |
477 | #define VAR(name,type)c->name = PL_ ## name; |
473 | # include "state.h" |
478 | # include "state.h" |
474 | #undef VAR |
479 | #undef VAR |
… | |
… | |
555 | } |
560 | } |
556 | |
561 | |
557 | static size_t |
562 | static size_t |
558 | coro_rss (pTHX_ struct coro *coro) |
563 | coro_rss (pTHX_ struct coro *coro) |
559 | { |
564 | { |
560 | size_t rss = sizeof (coro); |
565 | size_t rss = sizeof (*coro); |
561 | |
566 | |
562 | if (coro->mainstack) |
567 | if (coro->mainstack) |
563 | { |
568 | { |
564 | if (coro->flags & CF_RUNNING) |
569 | if (coro->flags & CF_RUNNING) |
565 | { |
570 | { |
… | |
… | |
567 | # include "state.h" |
572 | # include "state.h" |
568 | #undef VAR |
573 | #undef VAR |
569 | } |
574 | } |
570 | |
575 | |
571 | rss += sizeof (coro->curstackinfo); |
576 | rss += sizeof (coro->curstackinfo); |
572 | 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 *); |
573 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
578 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
574 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
579 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
575 | rss += coro->tmps_max * sizeof (SV *); |
580 | rss += coro->tmps_max * sizeof (SV *); |
576 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
581 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
577 | rss += coro->scopestack_max * sizeof (I32); |
582 | rss += coro->scopestack_max * sizeof (I32); |
578 | rss += coro->savestack_max * sizeof (ANY); |
583 | rss += coro->savestack_max * sizeof (ANY); |
579 | |
584 | |
… | |
… | |
602 | PL_curpm = 0; |
607 | PL_curpm = 0; |
603 | PL_localizing = 0; |
608 | PL_localizing = 0; |
604 | PL_dirty = 0; |
609 | PL_dirty = 0; |
605 | PL_restartop = 0; |
610 | PL_restartop = 0; |
606 | |
611 | |
607 | GvSV (PL_defgv) = NEWSV (0, 0); |
612 | GvSV (PL_defgv) = newSV (0); |
608 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
613 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
609 | GvSV (PL_errgv) = NEWSV (0, 0); |
614 | GvSV (PL_errgv) = newSV (0); |
610 | 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); |
611 | PL_rs = newSVsv (GvSV (irsgv)); |
616 | PL_rs = newSVsv (GvSV (irsgv)); |
612 | |
617 | PL_defoutgv = SvREFCNT_inc (stdoutgv); |
613 | { |
|
|
614 | IO *io = newIO (); |
|
|
615 | PL_defoutgv = newGVgen ("Coro"); |
|
|
616 | GvIOp(PL_defoutgv) = io; |
|
|
617 | IoTYPE (io) = IoTYPE_WRONLY; |
|
|
618 | IoOFP (io) = IoIFP (io) = PerlIO_stdout (); |
|
|
619 | IoFLAGS (io) |= IOf_FLUSH; |
|
|
620 | } |
|
|
621 | |
618 | |
622 | { |
619 | { |
623 | dSP; |
620 | dSP; |
624 | LOGOP myop; |
621 | LOGOP myop; |
625 | |
622 | |
… | |
… | |
663 | SvREFCNT_dec (GvSV (PL_errgv)); |
660 | SvREFCNT_dec (GvSV (PL_errgv)); |
664 | SvREFCNT_dec (PL_defoutgv); |
661 | SvREFCNT_dec (PL_defoutgv); |
665 | SvREFCNT_dec (PL_rs); |
662 | SvREFCNT_dec (PL_rs); |
666 | SvREFCNT_dec (GvSV (irsgv)); |
663 | SvREFCNT_dec (GvSV (irsgv)); |
667 | |
664 | |
668 | SvREFCNT_dec (coro->asp_deffh); |
665 | SvREFCNT_dec (coro->saved_deffh); |
669 | //SvREFCNT_dec (coro->throw); |
666 | //SvREFCNT_dec (coro->throw); |
670 | |
667 | |
671 | coro_destroy_stacks (aTHX); |
668 | coro_destroy_stacks (aTHX); |
672 | } |
669 | } |
673 | |
670 | |
… | |
… | |
1389 | #ifdef USE_ITHREADS |
1386 | #ifdef USE_ITHREADS |
1390 | MUTEX_INIT (&coro_mutex); |
1387 | MUTEX_INIT (&coro_mutex); |
1391 | #endif |
1388 | #endif |
1392 | BOOT_PAGESIZE; |
1389 | BOOT_PAGESIZE; |
1393 | |
1390 | |
1394 | irsgv = gv_fetchpv ("/", 1, SVt_PV); |
1391 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
|
|
1392 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1395 | |
1393 | |
1396 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1394 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1397 | |
1395 | |
1398 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1396 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1399 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1397 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1723 | int i, len; |
1721 | int i, len; |
1724 | |
1722 | |
1725 | if (!invoke) |
1723 | if (!invoke) |
1726 | croak ("\3terminate\2\n"); |
1724 | croak ("\3terminate\2\n"); |
1727 | |
1725 | |
1728 | SvREFCNT_dec (coro->asp_deffh); |
1726 | SvREFCNT_dec (coro->saved_deffh); |
1729 | coro->asp_deffh = SvREFCNT_inc (PL_defoutgv); |
1727 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1730 | |
1728 | |
1731 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1729 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1732 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1730 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1733 | |
1731 | |
1734 | invoke_av = (AV *)SvRV (invoke); |
1732 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
1752 | { |
1750 | { |
1753 | struct coro *coro = SvSTATE (coro_current); |
1751 | struct coro *coro = SvSTATE (coro_current); |
1754 | |
1752 | |
1755 | sv_setsv (cb, &PL_sv_undef); |
1753 | sv_setsv (cb, &PL_sv_undef); |
1756 | |
1754 | |
1757 | SvREFCNT_dec (PL_defoutgv); PL_defoutgv = coro->asp_deffh; |
1755 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1758 | coro->asp_deffh = 0; |
1756 | coro->saved_deffh = 0; |
1759 | |
1757 | |
1760 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1758 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1761 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1759 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1762 | croak ("\3terminate\2\n"); |
1760 | croak ("\3terminate\2\n"); |
1763 | |
1761 | |