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.188 by root, Fri Oct 5 20:36:31 2007 UTC

133static JMPENV *main_top_env; 133static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
136 136
137static GV *irsgv; /* $/ */ 137static GV *irsgv; /* $/ */
138static GV *stdoutgv; /* STDOUT */
138 139
139/* async_pool helper stuff */ 140/* async_pool helper stuff */
140static SV *sv_pool_rss; 141static SV *sv_pool_rss;
141static SV *sv_pool_size; 142static SV *sv_pool_size;
142static AV *av_async_pool; 143static AV *av_async_pool;
192 193
193 /* optionally saved, might be zero */ 194 /* optionally saved, might be zero */
194 AV *defav; /* @_ */ 195 AV *defav; /* @_ */
195 SV *defsv; /* $_ */ 196 SV *defsv; /* $_ */
196 SV *errsv; /* $@ */ 197 SV *errsv; /* $@ */
197 IO *deffh; /* default filehandle */ 198 SV *deffh; /* default filehandle */
198 SV *irssv; /* $/ */ 199 SV *irssv; /* $/ */
199 SV *irssv_sv; /* real $/ cache */ 200 SV *irssv_sv; /* real $/ cache */
200 201
201#define VAR(name,type) type name; 202#define VAR(name,type) type name;
202# include "state.h" 203# include "state.h"
203#undef VAR 204#undef VAR
204 205
205 /* statistics */ 206 /* statistics */
206 int usecount; /* number of switches to this coro */ 207 int usecount; /* number of transfers to this coro */
207 208
208 /* coro process data */ 209 /* coro process data */
209 int prio; 210 int prio;
211 //SV *throw;
212
213 /* async_pool */
214 SV *saved_deffh;
210 215
211 /* linked list */ 216 /* linked list */
212 struct coro *next, *prev; 217 struct coro *next, *prev;
213 HV *hv; /* the perl hash associated with this coro, if any */ 218 HV *hv; /* the perl hash associated with this coro, if any */
214}; 219};
383{ 388{
384#define VAR(name,type) PL_ ## name = c->name; 389#define VAR(name,type) PL_ ## name = c->name;
385# include "state.h" 390# include "state.h"
386#undef VAR 391#undef VAR
387 392
388 GvSV (PL_defgv) = c->defsv; 393 GvSV (PL_defgv) = c->defsv;
389 GvAV (PL_defgv) = c->defav; 394 GvAV (PL_defgv) = c->defav;
390 GvSV (PL_errgv) = c->errsv; 395 GvSV (PL_errgv) = c->errsv;
391 GvIOp(PL_defoutgv) = c->deffh;
392 PL_rs = c->irssv;
393 GvSV (irsgv) = c->irssv_sv; 396 GvSV (irsgv) = c->irssv_sv;
394 397
395 { 398 {
396 dSP; 399 dSP;
397 CV *cv; 400 CV *cv;
398 401
459 } 462 }
460 463
461 c->defav = GvAV (PL_defgv); 464 c->defav = GvAV (PL_defgv);
462 c->defsv = DEFSV; 465 c->defsv = DEFSV;
463 c->errsv = ERRSV; 466 c->errsv = ERRSV;
464 c->deffh = GvIOp (PL_defoutgv);
465 c->irssv = PL_rs;
466 c->irssv_sv = GvSV (irsgv); 467 c->irssv_sv = GvSV (irsgv);
467 468
468#define VAR(name,type)c->name = PL_ ## name; 469#define VAR(name,type)c->name = PL_ ## name;
469# include "state.h" 470# include "state.h"
470#undef VAR 471#undef VAR
551} 552}
552 553
553static size_t 554static size_t
554coro_rss (pTHX_ struct coro *coro) 555coro_rss (pTHX_ struct coro *coro)
555{ 556{
556 size_t rss = sizeof (coro); 557 size_t rss = sizeof (*coro);
557 558
558 if (coro->mainstack) 559 if (coro->mainstack)
559 { 560 {
560 if (coro->flags & CF_RUNNING) 561 if (coro->flags & CF_RUNNING)
561 { 562 {
563 # include "state.h" 564 # include "state.h"
564 #undef VAR 565 #undef VAR
565 } 566 }
566 567
567 rss += sizeof (coro->curstackinfo); 568 rss += sizeof (coro->curstackinfo);
568 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); 569 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
569 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 570 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
570 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 571 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
571 rss += coro->tmps_max * sizeof (SV *); 572 rss += coro->tmps_max * sizeof (SV *);
572 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 573 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
573 rss += coro->scopestack_max * sizeof (I32); 574 rss += coro->scopestack_max * sizeof (I32);
574 rss += coro->savestack_max * sizeof (ANY); 575 rss += coro->savestack_max * sizeof (ANY);
575 576
582} 583}
583 584
584/** coroutine stack handling ************************************************/ 585/** coroutine stack handling ************************************************/
585 586
586static void 587static void
587setup_coro (pTHX_ struct coro *coro) 588coro_setup (pTHX_ struct coro *coro)
588{ 589{
589 /* 590 /*
590 * emulate part of the perl startup here. 591 * emulate part of the perl startup here.
591 */ 592 */
592 coro_init_stacks (aTHX); 593 coro_init_stacks (aTHX);
601 PL_restartop = 0; 602 PL_restartop = 0;
602 603
603 GvSV (PL_defgv) = NEWSV (0, 0); 604 GvSV (PL_defgv) = NEWSV (0, 0);
604 GvAV (PL_defgv) = coro->args; coro->args = 0; 605 GvAV (PL_defgv) = coro->args; coro->args = 0;
605 GvSV (PL_errgv) = NEWSV (0, 0); 606 GvSV (PL_errgv) = NEWSV (0, 0);
606 GvSV (irsgv) = newSVsv (PL_rs); 607 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
607 PL_rs = newSVpvn ("\n", 1); 608 PL_rs = newSVsv (GvSV (irsgv));
608 609 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 610
617 { 611 {
618 dSP; 612 dSP;
619 LOGOP myop; 613 LOGOP myop;
620 614
632 626
633 ENTER; /* necessary e.g. for dounwind */ 627 ENTER; /* necessary e.g. for dounwind */
634} 628}
635 629
636static void 630static void
637destroy_coro (pTHX_ struct coro *coro) 631coro_destroy (pTHX_ struct coro *coro)
638{ 632{
639 if (!IN_DESTRUCT) 633 if (!IN_DESTRUCT)
640 { 634 {
641 /* restore all saved variables and stuff */ 635 /* restore all saved variables and stuff */
642 LEAVE_SCOPE (0); 636 LEAVE_SCOPE (0);
654 } 648 }
655 649
656 SvREFCNT_dec (GvSV (PL_defgv)); 650 SvREFCNT_dec (GvSV (PL_defgv));
657 SvREFCNT_dec (GvAV (PL_defgv)); 651 SvREFCNT_dec (GvAV (PL_defgv));
658 SvREFCNT_dec (GvSV (PL_errgv)); 652 SvREFCNT_dec (GvSV (PL_errgv));
659 SvREFCNT_dec (GvIOp(PL_defoutgv)); 653 SvREFCNT_dec (PL_defoutgv);
660 SvREFCNT_dec (PL_rs); 654 SvREFCNT_dec (PL_rs);
661 SvREFCNT_dec (GvSV (irsgv)); 655 SvREFCNT_dec (GvSV (irsgv));
656
657 SvREFCNT_dec (coro->saved_deffh);
658 //SvREFCNT_dec (coro->throw);
662 659
663 coro_destroy_stacks (aTHX); 660 coro_destroy_stacks (aTHX);
664} 661}
665 662
666static void 663static void
1017 /* need to start coroutine */ 1014 /* need to start coroutine */
1018 next->flags &= ~CF_NEW; 1015 next->flags &= ~CF_NEW;
1019 /* first get rid of the old state */ 1016 /* first get rid of the old state */
1020 save_perl (aTHX_ prev); 1017 save_perl (aTHX_ prev);
1021 /* setup coroutine call */ 1018 /* setup coroutine call */
1022 setup_coro (aTHX_ next); 1019 coro_setup (aTHX_ next);
1023 } 1020 }
1024 else 1021 else
1025 { 1022 {
1026 /* coroutine already started */ 1023 /* coroutine already started */
1027 save_perl (aTHX_ prev); 1024 save_perl (aTHX_ prev);
1095 croak ("FATAL: tried to destroy currently running coroutine"); 1092 croak ("FATAL: tried to destroy currently running coroutine");
1096 1093
1097 save_perl (aTHX_ &temp); 1094 save_perl (aTHX_ &temp);
1098 load_perl (aTHX_ coro); 1095 load_perl (aTHX_ coro);
1099 1096
1100 destroy_coro (aTHX_ coro); 1097 coro_destroy (aTHX_ coro);
1101 1098
1102 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1099 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
1103 1100
1104 coro->mainstack = 0; 1101 coro->mainstack = 0;
1105 } 1102 }
1381#ifdef USE_ITHREADS 1378#ifdef USE_ITHREADS
1382 MUTEX_INIT (&coro_mutex); 1379 MUTEX_INIT (&coro_mutex);
1383#endif 1380#endif
1384 BOOT_PAGESIZE; 1381 BOOT_PAGESIZE;
1385 1382
1386 irsgv = gv_fetchpv ("/", 1, SVt_PV); 1383 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1384 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1387 1385
1388 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1386 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1389 1387
1390 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1388 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1391 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1389 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1623 1621
1624 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1622 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1625 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1623 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1626 av_async_pool = get_av ("Coro::async_pool", TRUE); 1624 av_async_pool = get_av ("Coro::async_pool", TRUE);
1627 1625
1628 coro_current = get_sv ("Coro::current", FALSE); 1626 coro_current = get_sv ("Coro::current", FALSE);
1629 SvREADONLY_on (coro_current); 1627 SvREADONLY_on (coro_current);
1630 1628
1631 coro_stash = gv_stashpv ("Coro", TRUE); 1629 coro_stash = gv_stashpv ("Coro", TRUE);
1632 1630
1633 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1631 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1705# for async_pool speedup 1703# for async_pool speedup
1706void 1704void
1707_pool_1 (SV *cb) 1705_pool_1 (SV *cb)
1708 CODE: 1706 CODE:
1709{ 1707{
1710 int i, len; 1708 struct coro *coro = SvSTATE (coro_current);
1711 HV *hv = (HV *)SvRV (coro_current); 1709 HV *hv = (HV *)SvRV (coro_current);
1712 AV *defav = GvAV (PL_defgv); 1710 AV *defav = GvAV (PL_defgv);
1713 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1711 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1714 AV *invoke_av; 1712 AV *invoke_av;
1713 int i, len;
1715 1714
1716 if (!invoke) 1715 if (!invoke)
1717 croak ("\3terminate\2\n"); 1716 croak ("\3terminate\2\n");
1717
1718 SvREFCNT_dec (coro->saved_deffh);
1719 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1718 1720
1719 hv_store (hv, "desc", sizeof ("desc") - 1, 1721 hv_store (hv, "desc", sizeof ("desc") - 1,
1720 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1722 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1721 1723
1722 invoke_av = (AV *)SvRV (invoke); 1724 invoke_av = (AV *)SvRV (invoke);
1740{ 1742{
1741 struct coro *coro = SvSTATE (coro_current); 1743 struct coro *coro = SvSTATE (coro_current);
1742 1744
1743 sv_setsv (cb, &PL_sv_undef); 1745 sv_setsv (cb, &PL_sv_undef);
1744 1746
1747 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1748 coro->saved_deffh = 0;
1749
1745 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1750 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1746 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1751 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1747 croak ("\3terminate\2\n"); 1752 croak ("\3terminate\2\n");
1748 1753
1749 av_clear (GvAV (PL_defgv)); 1754 av_clear (GvAV (PL_defgv));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines