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.178 by root, Wed Oct 3 01:48:06 2007 UTC vs.
Revision 1.190 by root, Fri Oct 5 20:39:39 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
80#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 85#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
81# undef CORO_STACKGUARD 86# undef CORO_STACKGUARD
82#endif 87#endif
83 88
84#ifndef CORO_STACKGUARD 89#ifndef CORO_STACKGUARD
132static AV *main_mainstack; /* used to differentiate between $main and others */ 137static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env; 138static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 139static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 140static SV *coro_mortal; /* will be freed after next transfer */
136 141
137static GV *irsgv; /* $/ */ 142static GV *irsgv; /* $/ */
143static GV *stdoutgv; /* *STDOUT */
138 144
139/* async_pool helper stuff */ 145/* async_pool helper stuff */
140static SV *sv_pool_rss; 146static SV *sv_pool_rss;
141static SV *sv_pool_size; 147static SV *sv_pool_size;
142static AV *av_async_pool; 148static AV *av_async_pool;
192 198
193 /* optionally saved, might be zero */ 199 /* optionally saved, might be zero */
194 AV *defav; /* @_ */ 200 AV *defav; /* @_ */
195 SV *defsv; /* $_ */ 201 SV *defsv; /* $_ */
196 SV *errsv; /* $@ */ 202 SV *errsv; /* $@ */
197 IO *deffh; /* default filehandle */ 203 SV *deffh; /* default filehandle */
198 SV *irssv; /* $/ */ 204 SV *irssv; /* $/ */
199 SV *irssv_sv; /* real $/ cache */ 205 SV *irssv_sv; /* real $/ cache */
200 206
201#define VAR(name,type) type name; 207#define VAR(name,type) type name;
202# include "state.h" 208# include "state.h"
203#undef VAR 209#undef VAR
204 210
205 /* statistics */ 211 /* statistics */
206 int usecount; /* number of switches to this coro */ 212 int usecount; /* number of transfers to this coro */
207 213
208 /* coro process data */ 214 /* coro process data */
209 int prio; 215 int prio;
216 //SV *throw;
217
218 /* async_pool */
219 SV *saved_deffh;
210 220
211 /* linked list */ 221 /* linked list */
212 struct coro *next, *prev; 222 struct coro *next, *prev;
213 HV *hv; /* the perl hash associated with this coro, if any */ 223 HV *hv; /* the perl hash associated with this coro, if any */
214}; 224};
383{ 393{
384#define VAR(name,type) PL_ ## name = c->name; 394#define VAR(name,type) PL_ ## name = c->name;
385# include "state.h" 395# include "state.h"
386#undef VAR 396#undef VAR
387 397
388 GvSV (PL_defgv) = c->defsv; 398 GvSV (PL_defgv) = c->defsv;
389 GvAV (PL_defgv) = c->defav; 399 GvAV (PL_defgv) = c->defav;
390 GvSV (PL_errgv) = c->errsv; 400 GvSV (PL_errgv) = c->errsv;
391 GvIOp(PL_defoutgv) = c->deffh;
392 PL_rs = c->irssv;
393 GvSV (irsgv) = c->irssv_sv; 401 GvSV (irsgv) = c->irssv_sv;
394 402
395 { 403 {
396 dSP; 404 dSP;
397 CV *cv; 405 CV *cv;
398 406
459 } 467 }
460 468
461 c->defav = GvAV (PL_defgv); 469 c->defav = GvAV (PL_defgv);
462 c->defsv = DEFSV; 470 c->defsv = DEFSV;
463 c->errsv = ERRSV; 471 c->errsv = ERRSV;
464 c->deffh = GvIOp (PL_defoutgv);
465 c->irssv = PL_rs;
466 c->irssv_sv = GvSV (irsgv); 472 c->irssv_sv = GvSV (irsgv);
467 473
468#define VAR(name,type)c->name = PL_ ## name; 474#define VAR(name,type)c->name = PL_ ## name;
469# include "state.h" 475# include "state.h"
470#undef VAR 476#undef VAR
551} 557}
552 558
553static size_t 559static size_t
554coro_rss (pTHX_ struct coro *coro) 560coro_rss (pTHX_ struct coro *coro)
555{ 561{
556 size_t rss = sizeof (coro); 562 size_t rss = sizeof (*coro);
557 563
558 if (coro->mainstack) 564 if (coro->mainstack)
559 { 565 {
560 if (coro->flags & CF_RUNNING) 566 if (coro->flags & CF_RUNNING)
561 { 567 {
563 # include "state.h" 569 # include "state.h"
564 #undef VAR 570 #undef VAR
565 } 571 }
566 572
567 rss += sizeof (coro->curstackinfo); 573 rss += sizeof (coro->curstackinfo);
568 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); 574 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
569 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 575 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
570 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 576 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
571 rss += coro->tmps_max * sizeof (SV *); 577 rss += coro->tmps_max * sizeof (SV *);
572 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 578 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
573 rss += coro->scopestack_max * sizeof (I32); 579 rss += coro->scopestack_max * sizeof (I32);
574 rss += coro->savestack_max * sizeof (ANY); 580 rss += coro->savestack_max * sizeof (ANY);
575 581
582} 588}
583 589
584/** coroutine stack handling ************************************************/ 590/** coroutine stack handling ************************************************/
585 591
586static void 592static void
587setup_coro (pTHX_ struct coro *coro) 593coro_setup (pTHX_ struct coro *coro)
588{ 594{
589 /* 595 /*
590 * emulate part of the perl startup here. 596 * emulate part of the perl startup here.
591 */ 597 */
592 coro_init_stacks (aTHX); 598 coro_init_stacks (aTHX);
601 PL_restartop = 0; 607 PL_restartop = 0;
602 608
603 GvSV (PL_defgv) = NEWSV (0, 0); 609 GvSV (PL_defgv) = NEWSV (0, 0);
604 GvAV (PL_defgv) = coro->args; coro->args = 0; 610 GvAV (PL_defgv) = coro->args; coro->args = 0;
605 GvSV (PL_errgv) = NEWSV (0, 0); 611 GvSV (PL_errgv) = NEWSV (0, 0);
606 GvSV (irsgv) = newSVsv (PL_rs); 612 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
607 PL_rs = newSVpvn ("\n", 1); 613 PL_rs = newSVsv (GvSV (irsgv));
608 614 PL_defoutgv = SvREFCNT_inc (stdoutgv);
609 {
610 IO *io = newIO ();
611 GvIOp(PL_defoutgv) = io;
612 IoTYPE (io) = IoTYPE_WRONLY;
613 IoOFP (io) = IoIFP (io) = PerlIO_stdout ();
614 IoFLAGS (io) |= IOf_FLUSH;
615 }
616 615
617 { 616 {
618 dSP; 617 dSP;
619 LOGOP myop; 618 LOGOP myop;
620 619
632 631
633 ENTER; /* necessary e.g. for dounwind */ 632 ENTER; /* necessary e.g. for dounwind */
634} 633}
635 634
636static void 635static void
637destroy_coro (pTHX_ struct coro *coro) 636coro_destroy (pTHX_ struct coro *coro)
638{ 637{
639 if (!IN_DESTRUCT) 638 if (!IN_DESTRUCT)
640 { 639 {
641 /* restore all saved variables and stuff */ 640 /* restore all saved variables and stuff */
642 LEAVE_SCOPE (0); 641 LEAVE_SCOPE (0);
654 } 653 }
655 654
656 SvREFCNT_dec (GvSV (PL_defgv)); 655 SvREFCNT_dec (GvSV (PL_defgv));
657 SvREFCNT_dec (GvAV (PL_defgv)); 656 SvREFCNT_dec (GvAV (PL_defgv));
658 SvREFCNT_dec (GvSV (PL_errgv)); 657 SvREFCNT_dec (GvSV (PL_errgv));
659 SvREFCNT_dec (GvIOp(PL_defoutgv)); 658 SvREFCNT_dec (PL_defoutgv);
660 SvREFCNT_dec (PL_rs); 659 SvREFCNT_dec (PL_rs);
661 SvREFCNT_dec (GvSV (irsgv)); 660 SvREFCNT_dec (GvSV (irsgv));
661
662 SvREFCNT_dec (coro->saved_deffh);
663 //SvREFCNT_dec (coro->throw);
662 664
663 coro_destroy_stacks (aTHX); 665 coro_destroy_stacks (aTHX);
664} 666}
665 667
666static void 668static void
1017 /* need to start coroutine */ 1019 /* need to start coroutine */
1018 next->flags &= ~CF_NEW; 1020 next->flags &= ~CF_NEW;
1019 /* first get rid of the old state */ 1021 /* first get rid of the old state */
1020 save_perl (aTHX_ prev); 1022 save_perl (aTHX_ prev);
1021 /* setup coroutine call */ 1023 /* setup coroutine call */
1022 setup_coro (aTHX_ next); 1024 coro_setup (aTHX_ next);
1023 } 1025 }
1024 else 1026 else
1025 { 1027 {
1026 /* coroutine already started */ 1028 /* coroutine already started */
1027 save_perl (aTHX_ prev); 1029 save_perl (aTHX_ prev);
1095 croak ("FATAL: tried to destroy currently running coroutine"); 1097 croak ("FATAL: tried to destroy currently running coroutine");
1096 1098
1097 save_perl (aTHX_ &temp); 1099 save_perl (aTHX_ &temp);
1098 load_perl (aTHX_ coro); 1100 load_perl (aTHX_ coro);
1099 1101
1100 destroy_coro (aTHX_ coro); 1102 coro_destroy (aTHX_ coro);
1101 1103
1102 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1104 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
1103 1105
1104 coro->mainstack = 0; 1106 coro->mainstack = 0;
1105 } 1107 }
1381#ifdef USE_ITHREADS 1383#ifdef USE_ITHREADS
1382 MUTEX_INIT (&coro_mutex); 1384 MUTEX_INIT (&coro_mutex);
1383#endif 1385#endif
1384 BOOT_PAGESIZE; 1386 BOOT_PAGESIZE;
1385 1387
1386 irsgv = gv_fetchpv ("/", 1, SVt_PV); 1388 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1389 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1387 1390
1388 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1391 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1389 1392
1390 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1393 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1391 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1394 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1623 1626
1624 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1627 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1625 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1628 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1626 av_async_pool = get_av ("Coro::async_pool", TRUE); 1629 av_async_pool = get_av ("Coro::async_pool", TRUE);
1627 1630
1628 coro_current = get_sv ("Coro::current", FALSE); 1631 coro_current = get_sv ("Coro::current", FALSE);
1629 SvREADONLY_on (coro_current); 1632 SvREADONLY_on (coro_current);
1630 1633
1631 coro_stash = gv_stashpv ("Coro", TRUE); 1634 coro_stash = gv_stashpv ("Coro", TRUE);
1632 1635
1633 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1636 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1705# for async_pool speedup 1708# for async_pool speedup
1706void 1709void
1707_pool_1 (SV *cb) 1710_pool_1 (SV *cb)
1708 CODE: 1711 CODE:
1709{ 1712{
1710 int i, len; 1713 struct coro *coro = SvSTATE (coro_current);
1711 HV *hv = (HV *)SvRV (coro_current); 1714 HV *hv = (HV *)SvRV (coro_current);
1712 AV *defav = GvAV (PL_defgv); 1715 AV *defav = GvAV (PL_defgv);
1713 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1716 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1714 AV *invoke_av; 1717 AV *invoke_av;
1718 int i, len;
1715 1719
1716 if (!invoke) 1720 if (!invoke)
1717 croak ("\3terminate\2\n"); 1721 croak ("\3terminate\2\n");
1722
1723 SvREFCNT_dec (coro->saved_deffh);
1724 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1718 1725
1719 hv_store (hv, "desc", sizeof ("desc") - 1, 1726 hv_store (hv, "desc", sizeof ("desc") - 1,
1720 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1727 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1721 1728
1722 invoke_av = (AV *)SvRV (invoke); 1729 invoke_av = (AV *)SvRV (invoke);
1740{ 1747{
1741 struct coro *coro = SvSTATE (coro_current); 1748 struct coro *coro = SvSTATE (coro_current);
1742 1749
1743 sv_setsv (cb, &PL_sv_undef); 1750 sv_setsv (cb, &PL_sv_undef);
1744 1751
1752 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1753 coro->saved_deffh = 0;
1754
1745 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1755 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1746 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1756 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1747 croak ("\3terminate\2\n"); 1757 croak ("\3terminate\2\n");
1748 1758
1749 av_clear (GvAV (PL_defgv)); 1759 av_clear (GvAV (PL_defgv));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines