ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.182 by root, Wed Oct 3 18:37:48 2007 UTC vs.
Revision 1.193 by root, Fri Oct 5 23:38:40 2007 UTC

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
132static AV *main_mainstack; /* used to differentiate between $main and others */ 140static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env; 141static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 142static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 143static SV *coro_mortal; /* will be freed after next transfer */
136 144
137static GV *irsgv; /* $/ */ 145static GV *irsgv; /* $/ */
146static GV *stdoutgv; /* *STDOUT */
138 147
139/* async_pool helper stuff */ 148/* async_pool helper stuff */
140static SV *sv_pool_rss; 149static SV *sv_pool_rss;
141static SV *sv_pool_size; 150static SV *sv_pool_size;
142static AV *av_async_pool; 151static 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
557static size_t 562static size_t
558coro_rss (pTHX_ struct coro *coro) 563coro_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));
617 PL_defoutgv = SvREFCNT_inc (stdoutgv);
612 618
613 { 619 ENTER; /* necessary e.g. for dounwind */
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 620
622 { 621 {
623 dSP; 622 dSP;
624 LOGOP myop; 623 LOGOP myop;
625 624
626 Zero (&myop, 1, LOGOP); 625 Zero (&myop, 1, LOGOP);
627 myop.op_next = Nullop; 626 myop.op_next = Nullop;
628 myop.op_flags = OPf_WANT_VOID; 627 myop.op_flags = OPf_WANT_VOID;
629 628
630 PUSHMARK (SP); 629 PUSHMARK (SP);
631 XPUSHs (av_shift (GvAV (PL_defgv))); 630 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
632 PUTBACK; 631 PUTBACK;
633 PL_op = (OP *)&myop; 632 PL_op = (OP *)&myop;
634 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 633 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
635 SPAGAIN; 634 SPAGAIN;
636 } 635 }
637
638 ENTER; /* necessary e.g. for dounwind */
639} 636}
640 637
641static void 638static void
642coro_destroy (pTHX_ struct coro *coro) 639coro_destroy (pTHX_ struct coro *coro)
643{ 640{
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
711 bot = PL_stack_base + cx->blk_oldsp + 1; 708 bot = PL_stack_base + cx->blk_oldsp + 1;
712 top = cx->blk_gimme == G_ARRAY ? SP + 1 709 top = cx->blk_gimme == G_ARRAY ? SP + 1
713 : cx->blk_gimme == G_SCALAR ? bot + 1 710 : cx->blk_gimme == G_SCALAR ? bot + 1
714 : bot; 711 : bot;
715 712
713 av_extend (av, top - bot);
716 while (bot < top) 714 while (bot < top)
717 av_push (av, SvREFCNT_inc (*bot++)); 715 av_push (av, SvREFCNT_inc (*bot++));
718 716
719 PL_runops = RUNOPS_DEFAULT; 717 PL_runops = RUNOPS_DEFAULT;
720 ENTER; 718 ENTER;
932 Safefree (cctx->sptr); 930 Safefree (cctx->sptr);
933 931
934 Safefree (cctx); 932 Safefree (cctx);
935} 933}
936 934
935/* wether this cctx should be destructed */
936#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
937
937static coro_cctx * 938static coro_cctx *
938cctx_get (pTHX) 939cctx_get (pTHX)
939{ 940{
940 while (cctx_first) 941 while (cctx_first)
941 { 942 {
942 coro_cctx *cctx = cctx_first; 943 coro_cctx *cctx = cctx_first;
943 cctx_first = cctx->next; 944 cctx_first = cctx->next;
944 --cctx_idle; 945 --cctx_idle;
945 946
946 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) 947 if (!CCTX_EXPIRED (cctx))
947 return cctx; 948 return cctx;
948 949
949 cctx_destroy (cctx); 950 cctx_destroy (cctx);
950 } 951 }
951 952
1044 /* 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 */
1045 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));
1046 1047
1047 prev->cctx = 0; 1048 prev->cctx = 0;
1048 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
1049 cctx_put (prev__cctx); 1054 cctx_put (prev__cctx);
1050 } 1055 }
1051 1056
1052 ++next->usecount; 1057 ++next->usecount;
1053 1058
1389#ifdef USE_ITHREADS 1394#ifdef USE_ITHREADS
1390 MUTEX_INIT (&coro_mutex); 1395 MUTEX_INIT (&coro_mutex);
1391#endif 1396#endif
1392 BOOT_PAGESIZE; 1397 BOOT_PAGESIZE;
1393 1398
1394 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);
1395 1401
1396 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1402 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1397 1403
1398 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1404 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1399 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1405 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1430 1436
1431 coro->hv = hv = newHV (); 1437 coro->hv = hv = newHV ();
1432 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;
1433 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1439 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1434 1440
1441 av_extend (coro->args, items - 1);
1435 for (i = 1; i < items; i++) 1442 for (i = 1; i < items; i++)
1436 av_push (coro->args, newSVsv (ST (i))); 1443 av_push (coro->args, newSVsv (ST (i)));
1437} 1444}
1438 OUTPUT: 1445 OUTPUT:
1439 RETVAL 1446 RETVAL
1723 int i, len; 1730 int i, len;
1724 1731
1725 if (!invoke) 1732 if (!invoke)
1726 croak ("\3terminate\2\n"); 1733 croak ("\3terminate\2\n");
1727 1734
1728 SvREFCNT_dec (coro->asp_deffh); 1735 SvREFCNT_dec (coro->saved_deffh);
1729 coro->asp_deffh = SvREFCNT_inc (PL_defoutgv); 1736 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1730 1737
1731 hv_store (hv, "desc", sizeof ("desc") - 1, 1738 hv_store (hv, "desc", sizeof ("desc") - 1,
1732 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1739 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1733 1740
1734 invoke_av = (AV *)SvRV (invoke); 1741 invoke_av = (AV *)SvRV (invoke);
1752{ 1759{
1753 struct coro *coro = SvSTATE (coro_current); 1760 struct coro *coro = SvSTATE (coro_current);
1754 1761
1755 sv_setsv (cb, &PL_sv_undef); 1762 sv_setsv (cb, &PL_sv_undef);
1756 1763
1757 SvREFCNT_dec (PL_defoutgv); PL_defoutgv = coro->asp_deffh; 1764 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1758 coro->asp_deffh = 0; 1765 coro->saved_deffh = 0;
1759 1766
1760 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1767 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1761 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1768 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1762 croak ("\3terminate\2\n"); 1769 croak ("\3terminate\2\n");
1763 1770

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines