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.197 by root, Sat Oct 6 19:25:00 2007 UTC vs.
Revision 1.198 by root, Sun Oct 7 03:58:37 2007 UTC

1#include "libcoro/coro.c" 1#include "libcoro/coro.c"
2 2
3#define PERL_NO_GET_CONTEXT 3#define PERL_NO_GET_CONTEXT
4#define PERL_EXT
4 5
5#include "EXTERN.h" 6#include "EXTERN.h"
6#include "perl.h" 7#include "perl.h"
7#include "XSUB.h" 8#include "XSUB.h"
8 9
192 CF_READY = 0x0002, /* coroutine is ready */ 193 CF_READY = 0x0002, /* coroutine is ready */
193 CF_NEW = 0x0004, /* has never been switched to */ 194 CF_NEW = 0x0004, /* has never been switched to */
194 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 195 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
195}; 196};
196 197
198/* the structure where most of the perl state is stored, overlaid on the cxstack */
199typedef struct {
200 SV *defsv;
201 AV *defav;
202 SV *errsv;
203 SV *irsgv;
204#define VAR(name,type) type name;
205# include "state.h"
206#undef VAR
207} perl_slots;
208
209#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
210
197/* this is a structure representing a perl-level coroutine */ 211/* this is a structure representing a perl-level coroutine */
198struct coro { 212struct coro {
199 /* the c coroutine allocated to this perl coroutine, if any */ 213 /* the c coroutine allocated to this perl coroutine, if any */
200 coro_cctx *cctx; 214 coro_cctx *cctx;
201 215
216 /* process data */
217 AV *mainstack;
218 perl_slots *slot; /* basically the saved sp */
219
202 /* data associated with this coroutine (initial args) */ 220 /* data associated with this coroutine (initial args) */
203 AV *args; 221 AV *args;
204 int refcnt; 222 int refcnt;
205 int flags; /* CF_ flags */ 223 int flags; /* CF_ flags */
206
207 /* optionally saved, might be zero */
208 AV *defav; /* @_ */
209 SV *defsv; /* $_ */
210 SV *errsv; /* $@ */
211 SV *deffh; /* default filehandle */
212 SV *irssv; /* $/ */
213 SV *irssv_sv; /* real $/ cache */
214
215#define VAR(name,type) type name;
216# include "state.h"
217#undef VAR
218 224
219 /* statistics */ 225 /* statistics */
220 int usecount; /* number of transfers to this coro */ 226 int usecount; /* number of transfers to this coro */
221 227
222 /* coro process data */ 228 /* coro process data */
397/** load & save, init *******************************************************/ 403/** load & save, init *******************************************************/
398 404
399static void 405static void
400load_perl (pTHX_ Coro__State c) 406load_perl (pTHX_ Coro__State c)
401{ 407{
408 perl_slots *slot = c->slot;
409 c->slot = 0;
410
411 PL_mainstack = c->mainstack;
412
413 GvSV (PL_defgv) = slot->defsv;
414 GvAV (PL_defgv) = slot->defav;
415 GvSV (PL_errgv) = slot->errsv;
416 GvSV (irsgv) = slot->irsgv;
417
402#define VAR(name,type) PL_ ## name = c->name; 418 #define VAR(name,type) PL_ ## name = slot->name;
403# include "state.h" 419 # include "state.h"
404#undef VAR 420 #undef VAR
405
406 GvSV (PL_defgv) = c->defsv;
407 GvAV (PL_defgv) = c->defav;
408 GvSV (PL_errgv) = c->errsv;
409 GvSV (irsgv) = c->irssv_sv;
410 421
411 { 422 {
412 dSP; 423 dSP;
424
413 CV *cv; 425 CV *cv;
414 426
415 /* now do the ugly restore mess */ 427 /* now do the ugly restore mess */
416 while (expect_true (cv = (CV *)POPs)) 428 while (expect_true (cv = (CV *)POPs))
417 { 429 {
472 } 484 }
473 485
474 PUTBACK; 486 PUTBACK;
475 } 487 }
476 488
489 /* allocate some space on the context stack for our purposes */
490 {
491 /* we manually unroll here, as usually 2 slots is enough */
492 int i;
493 if (SLOT_COUNT >= 1) CXINC;
494 if (SLOT_COUNT >= 2) CXINC;
495 if (SLOT_COUNT >= 3) CXINC;
496 for (i = 3; i < SLOT_COUNT; ++i)
497 CXINC;
498
499 cxstack_ix -= SLOT_COUNT; /* undo allocation */
500 }
501
502 c->mainstack = PL_mainstack;
503
504 {
505 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
506
477 c->defav = GvAV (PL_defgv); 507 slot->defav = GvAV (PL_defgv);
478 c->defsv = DEFSV; 508 slot->defsv = DEFSV;
479 c->errsv = ERRSV; 509 slot->errsv = ERRSV;
480 c->irssv_sv = GvSV (irsgv); 510 slot->irsgv = GvSV (irsgv);
481 511
482#define VAR(name,type)c->name = PL_ ## name; 512 #define VAR(name,type) slot->name = PL_ ## name;
483# include "state.h" 513 # include "state.h"
484#undef VAR 514 #undef VAR
515 }
485} 516}
486 517
487/* 518/*
488 * allocate various perl stacks. This is an exact copy 519 * allocate various perl stacks. This is an exact copy
489 * of perl.c:init_stacks, except that it uses less memory 520 * of perl.c:init_stacks, except that it uses less memory
494# define coro_init_stacks init_stacks 525# define coro_init_stacks init_stacks
495#else 526#else
496static void 527static void
497coro_init_stacks (pTHX) 528coro_init_stacks (pTHX)
498{ 529{
499 PL_curstackinfo = new_stackinfo(64, 6); 530 PL_curstackinfo = new_stackinfo(32, 8);
500 PL_curstackinfo->si_type = PERLSI_MAIN; 531 PL_curstackinfo->si_type = PERLSI_MAIN;
501 PL_curstack = PL_curstackinfo->si_stack; 532 PL_curstack = PL_curstackinfo->si_stack;
502 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 533 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
503 534
504 PL_stack_base = AvARRAY(PL_curstack); 535 PL_stack_base = AvARRAY(PL_curstack);
505 PL_stack_sp = PL_stack_base; 536 PL_stack_sp = PL_stack_base;
506 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 537 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
507 538
508 New(50,PL_tmps_stack,64,SV*); 539 New(50,PL_tmps_stack,32,SV*);
509 PL_tmps_floor = -1; 540 PL_tmps_floor = -1;
510 PL_tmps_ix = -1; 541 PL_tmps_ix = -1;
511 PL_tmps_max = 64; 542 PL_tmps_max = 32;
512 543
513 New(54,PL_markstack,16,I32); 544 New(54,PL_markstack,16,I32);
514 PL_markstack_ptr = PL_markstack; 545 PL_markstack_ptr = PL_markstack;
515 PL_markstack_max = PL_markstack + 16; 546 PL_markstack_max = PL_markstack + 16;
516 547
517#ifdef SET_MARK_OFFSET 548#ifdef SET_MARK_OFFSET
518 SET_MARK_OFFSET; 549 SET_MARK_OFFSET;
519#endif 550#endif
520 551
521 New(54,PL_scopestack,16,I32); 552 New(54,PL_scopestack,8,I32);
522 PL_scopestack_ix = 0; 553 PL_scopestack_ix = 0;
523 PL_scopestack_max = 16; 554 PL_scopestack_max = 8;
524 555
525 New(54,PL_savestack,64,ANY); 556 New(54,PL_savestack,24,ANY);
526 PL_savestack_ix = 0; 557 PL_savestack_ix = 0;
527 PL_savestack_max = 64; 558 PL_savestack_max = 24;
528 559
529#if !PERL_VERSION_ATLEAST (5,9,0) 560#if !PERL_VERSION_ATLEAST (5,9,0)
530 New(54,PL_retstack,4,OP*); 561 New(54,PL_retstack,4,OP*);
531 PL_retstack_ix = 0; 562 PL_retstack_ix = 0;
532 PL_retstack_max = 4; 563 PL_retstack_max = 4;
569{ 600{
570 size_t rss = sizeof (*coro); 601 size_t rss = sizeof (*coro);
571 602
572 if (coro->mainstack) 603 if (coro->mainstack)
573 { 604 {
605 perl_slots tmp_slot;
606 perl_slots *slot;
607
574 if (coro->flags & CF_RUNNING) 608 if (coro->flags & CF_RUNNING)
575 { 609 {
610 slot = &tmp_slot;
611
576 #define VAR(name,type)coro->name = PL_ ## name; 612 #define VAR(name,type) slot->name = PL_ ## name;
577 # include "state.h" 613 # include "state.h"
578 #undef VAR 614 #undef VAR
579 } 615 }
616 else
617 slot = coro->slot;
580 618
581 rss += sizeof (coro->curstackinfo); 619 rss += sizeof (slot->curstackinfo);
582 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
583 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 620 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
584 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 621 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
585 rss += coro->tmps_max * sizeof (SV *); 622 rss += slot->tmps_max * sizeof (SV *);
586 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 623 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
587 rss += coro->scopestack_max * sizeof (I32); 624 rss += slot->scopestack_max * sizeof (I32);
588 rss += coro->savestack_max * sizeof (ANY); 625 rss += slot->savestack_max * sizeof (ANY);
589 626
590#if !PERL_VERSION_ATLEAST (5,9,0) 627#if !PERL_VERSION_ATLEAST (5,9,0)
591 rss += coro->retstack_max * sizeof (OP *); 628 rss += slot->retstack_max * sizeof (OP *);
592#endif 629#endif
593 } 630 }
594 631
595 return rss; 632 return rss;
596} 633}
612 PL_curpm = 0; 649 PL_curpm = 0;
613 PL_curpad = 0; 650 PL_curpad = 0;
614 PL_localizing = 0; 651 PL_localizing = 0;
615 PL_dirty = 0; 652 PL_dirty = 0;
616 PL_restartop = 0; 653 PL_restartop = 0;
654 PL_diehook = 0;
655 PL_warnhook = 0;
617 656
618 GvSV (PL_defgv) = newSV (0); 657 GvSV (PL_defgv) = newSV (0);
619 GvAV (PL_defgv) = coro->args; coro->args = 0; 658 GvAV (PL_defgv) = coro->args; coro->args = 0;
620 GvSV (PL_errgv) = newSV (0); 659 GvSV (PL_errgv) = newSV (0);
621 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 660 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
666 SvREFCNT_dec (GvSV (PL_errgv)); 705 SvREFCNT_dec (GvSV (PL_errgv));
667 SvREFCNT_dec (PL_defoutgv); 706 SvREFCNT_dec (PL_defoutgv);
668 SvREFCNT_dec (PL_rs); 707 SvREFCNT_dec (PL_rs);
669 SvREFCNT_dec (GvSV (irsgv)); 708 SvREFCNT_dec (GvSV (irsgv));
670 709
710 SvREFCNT_dec (PL_diehook);
711 SvREFCNT_dec (PL_warnhook);
712
671 SvREFCNT_dec (coro->saved_deffh); 713 SvREFCNT_dec (coro->saved_deffh);
672 SvREFCNT_dec (coro->throw); 714 SvREFCNT_dec (coro->throw);
673 715
674 coro_destroy_stacks (aTHX); 716 coro_destroy_stacks (aTHX);
675} 717}
991 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1033 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
992 1034
993 if (expect_false (next->flags & CF_DESTROYED)) 1035 if (expect_false (next->flags & CF_DESTROYED))
994 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 1036 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
995 1037
1038 if (
1039#if PERL_VERSION_ATLEAST (5,9,0)
1040 expect_false (PL_parser)
1041#else
996 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1042 expect_false (PL_lex_state != LEX_NOTPARSING)
1043#endif
1044 )
997 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1045 croak ("Coro::State::transfer called while parsing, but this is not supported");
998 } 1046 }
999} 1047}
1000 1048
1001/* always use the TRANSFER macro */ 1049/* always use the TRANSFER macro */
1130 save_perl (aTHX_ &temp); 1178 save_perl (aTHX_ &temp);
1131 load_perl (aTHX_ coro); 1179 load_perl (aTHX_ coro);
1132 1180
1133 coro_destroy (aTHX_ coro); 1181 coro_destroy (aTHX_ coro);
1134 1182
1135 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1183 load_perl (aTHX_ &temp);
1136 1184
1137 coro->mainstack = 0; 1185 coro->slot = 0;
1138 } 1186 }
1139 1187
1140 cctx_destroy (coro->cctx); 1188 cctx_destroy (coro->cctx);
1141 SvREFCNT_dec (coro->args); 1189 SvREFCNT_dec (coro->args);
1142 1190
1399 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); 1447 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1400 1448
1401 if (coro->flags & CF_RUNNING) 1449 if (coro->flags & CF_RUNNING)
1402 PL_runops = RUNOPS_DEFAULT; 1450 PL_runops = RUNOPS_DEFAULT;
1403 else 1451 else
1404 coro->runops = RUNOPS_DEFAULT; 1452 coro->slot->runops = RUNOPS_DEFAULT;
1405 } 1453 }
1406} 1454}
1407 1455
1408MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1456MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1409 1457
1569 CODE: 1617 CODE:
1570{ 1618{
1571 if (coro->mainstack) 1619 if (coro->mainstack)
1572 { 1620 {
1573 struct coro temp; 1621 struct coro temp;
1574 Zero (&temp, 1, struct coro);
1575 1622
1576 if (!(coro->flags & CF_RUNNING)) 1623 if (!(coro->flags & CF_RUNNING))
1577 { 1624 {
1578 save_perl (aTHX_ &temp); 1625 save_perl (aTHX_ &temp);
1579 load_perl (aTHX_ coro); 1626 load_perl (aTHX_ coro);
1583 dSP; 1630 dSP;
1584 ENTER; 1631 ENTER;
1585 SAVETMPS; 1632 SAVETMPS;
1586 PUSHMARK (SP); 1633 PUSHMARK (SP);
1587 PUTBACK; 1634 PUTBACK;
1635
1588 if (ix) 1636 if (ix)
1589 eval_sv (coderef, 0); 1637 eval_sv (coderef, 0);
1590 else 1638 else
1591 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1639 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1640
1592 SPAGAIN; 1641 SPAGAIN;
1593 FREETMPS; 1642 FREETMPS;
1594 LEAVE; 1643 LEAVE;
1595 PUTBACK; 1644 PUTBACK;
1596 } 1645 }
1655BOOT: 1704BOOT:
1656{ 1705{
1657 int i; 1706 int i;
1658 1707
1659 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1708 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1709 get_sv ("Coro::POOL_RSS" , TRUE); /* silence stupid 5.10 warning */
1660 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1710 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1711 get_sv ("Coro::POOL_SIZE" , TRUE); /* silence stupid 5.10 warning */
1661 av_async_pool = get_av ("Coro::async_pool", TRUE); 1712 av_async_pool = get_av ("Coro::async_pool", TRUE);
1713 get_av ("Coro::async_pool", TRUE); /* silence stupid 5.10 warning */
1662 1714
1663 coro_current = get_sv ("Coro::current", FALSE); 1715 coro_current = get_sv ("Coro::current", FALSE);
1664 SvREADONLY_on (coro_current); 1716 SvREADONLY_on (coro_current);
1665 1717
1666 coro_stash = gv_stashpv ("Coro", TRUE); 1718 coro_stash = gv_stashpv ("Coro", TRUE);
1667 1719
1668 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1720 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1669 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1721 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1670 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1722 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1671 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1723 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1674 1726
1675 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1727 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1676 coro_ready[i] = newAV (); 1728 coro_ready[i] = newAV ();
1677 1729
1678 { 1730 {
1679 SV *sv = perl_get_sv("Coro::API", 1); 1731 SV *sv = perl_get_sv ("Coro::API", TRUE);
1732 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1680 1733
1681 coroapi.schedule = api_schedule; 1734 coroapi.schedule = api_schedule;
1682 coroapi.cede = api_cede; 1735 coroapi.cede = api_cede;
1683 coroapi.cede_notself = api_cede_notself; 1736 coroapi.cede_notself = api_cede_notself;
1684 coroapi.ready = api_ready; 1737 coroapi.ready = api_ready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines