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.199 by root, Sun Oct 7 13:53: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
147static HV *coro_state_stash, *coro_stash; 148static HV *coro_state_stash, *coro_stash;
148static SV *coro_mortal; /* will be freed after next transfer */ 149static SV *coro_mortal; /* will be freed after next transfer */
149 150
150static GV *irsgv; /* $/ */ 151static GV *irsgv; /* $/ */
151static GV *stdoutgv; /* *STDOUT */ 152static GV *stdoutgv; /* *STDOUT */
153static SV *sv_diehook;
154static SV *sv_warnhook;
152 155
153/* async_pool helper stuff */ 156/* async_pool helper stuff */
154static SV *sv_pool_rss; 157static SV *sv_pool_rss;
155static SV *sv_pool_size; 158static SV *sv_pool_size;
156static AV *av_async_pool; 159static AV *av_async_pool;
192 CF_READY = 0x0002, /* coroutine is ready */ 195 CF_READY = 0x0002, /* coroutine is ready */
193 CF_NEW = 0x0004, /* has never been switched to */ 196 CF_NEW = 0x0004, /* has never been switched to */
194 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 197 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
195}; 198};
196 199
200/* the structure where most of the perl state is stored, overlaid on the cxstack */
201typedef struct {
202 SV *defsv;
203 AV *defav;
204 SV *errsv;
205 SV *irsgv;
206#define VAR(name,type) type name;
207# include "state.h"
208#undef VAR
209} perl_slots;
210
211#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
212
197/* this is a structure representing a perl-level coroutine */ 213/* this is a structure representing a perl-level coroutine */
198struct coro { 214struct coro {
199 /* the c coroutine allocated to this perl coroutine, if any */ 215 /* the c coroutine allocated to this perl coroutine, if any */
200 coro_cctx *cctx; 216 coro_cctx *cctx;
201 217
218 /* process data */
219 AV *mainstack;
220 perl_slots *slot; /* basically the saved sp */
221
202 /* data associated with this coroutine (initial args) */ 222 /* data associated with this coroutine (initial args) */
203 AV *args; 223 AV *args;
204 int refcnt; 224 int refcnt;
205 int flags; /* CF_ flags */ 225 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 226
219 /* statistics */ 227 /* statistics */
220 int usecount; /* number of transfers to this coro */ 228 int usecount; /* number of transfers to this coro */
221 229
222 /* coro process data */ 230 /* coro process data */
248static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 256static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
249static int coro_nready; 257static int coro_nready;
250static struct coro *coro_first; 258static struct coro *coro_first;
251 259
252/** lowlevel stuff **********************************************************/ 260/** lowlevel stuff **********************************************************/
261
262static SV *
263coro_get_sv (const char *name, int create)
264{
265#if PERL_VERSION_ATLEAST (5,9,0)
266 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
267 get_sv (name, create);
268#endif
269 return get_sv (name, create);
270}
271
272static SV *
273coro_get_av (const char *name, int create)
274{
275#if PERL_VERSION_ATLEAST (5,9,0)
276 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
277 get_av (name, create);
278#endif
279 return get_av (name, create);
280}
253 281
254static AV * 282static AV *
255coro_clone_padlist (pTHX_ CV *cv) 283coro_clone_padlist (pTHX_ CV *cv)
256{ 284{
257 AV *padlist = CvPADLIST (cv); 285 AV *padlist = CvPADLIST (cv);
397/** load & save, init *******************************************************/ 425/** load & save, init *******************************************************/
398 426
399static void 427static void
400load_perl (pTHX_ Coro__State c) 428load_perl (pTHX_ Coro__State c)
401{ 429{
430 perl_slots *slot = c->slot;
431 c->slot = 0;
432
433 PL_mainstack = c->mainstack;
434
435 GvSV (PL_defgv) = slot->defsv;
436 GvAV (PL_defgv) = slot->defav;
437 GvSV (PL_errgv) = slot->errsv;
438 GvSV (irsgv) = slot->irsgv;
439
402#define VAR(name,type) PL_ ## name = c->name; 440 #define VAR(name,type) PL_ ## name = slot->name;
403# include "state.h" 441 # include "state.h"
404#undef VAR 442 #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 443
411 { 444 {
412 dSP; 445 dSP;
446
413 CV *cv; 447 CV *cv;
414 448
415 /* now do the ugly restore mess */ 449 /* now do the ugly restore mess */
416 while (expect_true (cv = (CV *)POPs)) 450 while (expect_true (cv = (CV *)POPs))
417 { 451 {
472 } 506 }
473 507
474 PUTBACK; 508 PUTBACK;
475 } 509 }
476 510
511 /* allocate some space on the context stack for our purposes */
512 {
513 /* we manually unroll here, as usually 2 slots is enough */
514 int i;
515 if (SLOT_COUNT >= 1) CXINC;
516 if (SLOT_COUNT >= 2) CXINC;
517 if (SLOT_COUNT >= 3) CXINC;
518 for (i = 3; i < SLOT_COUNT; ++i)
519 CXINC;
520
521 cxstack_ix -= SLOT_COUNT; /* undo allocation */
522 }
523
524 c->mainstack = PL_mainstack;
525
526 {
527 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
528
477 c->defav = GvAV (PL_defgv); 529 slot->defav = GvAV (PL_defgv);
478 c->defsv = DEFSV; 530 slot->defsv = DEFSV;
479 c->errsv = ERRSV; 531 slot->errsv = ERRSV;
480 c->irssv_sv = GvSV (irsgv); 532 slot->irsgv = GvSV (irsgv);
481 533
482#define VAR(name,type)c->name = PL_ ## name; 534 #define VAR(name,type) slot->name = PL_ ## name;
483# include "state.h" 535 # include "state.h"
484#undef VAR 536 #undef VAR
537 }
485} 538}
486 539
487/* 540/*
488 * allocate various perl stacks. This is an exact copy 541 * allocate various perl stacks. This is an exact copy
489 * of perl.c:init_stacks, except that it uses less memory 542 * of perl.c:init_stacks, except that it uses less memory
494# define coro_init_stacks init_stacks 547# define coro_init_stacks init_stacks
495#else 548#else
496static void 549static void
497coro_init_stacks (pTHX) 550coro_init_stacks (pTHX)
498{ 551{
499 PL_curstackinfo = new_stackinfo(64, 6); 552 PL_curstackinfo = new_stackinfo(32, 8);
500 PL_curstackinfo->si_type = PERLSI_MAIN; 553 PL_curstackinfo->si_type = PERLSI_MAIN;
501 PL_curstack = PL_curstackinfo->si_stack; 554 PL_curstack = PL_curstackinfo->si_stack;
502 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 555 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
503 556
504 PL_stack_base = AvARRAY(PL_curstack); 557 PL_stack_base = AvARRAY(PL_curstack);
505 PL_stack_sp = PL_stack_base; 558 PL_stack_sp = PL_stack_base;
506 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 559 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
507 560
508 New(50,PL_tmps_stack,64,SV*); 561 New(50,PL_tmps_stack,32,SV*);
509 PL_tmps_floor = -1; 562 PL_tmps_floor = -1;
510 PL_tmps_ix = -1; 563 PL_tmps_ix = -1;
511 PL_tmps_max = 64; 564 PL_tmps_max = 32;
512 565
513 New(54,PL_markstack,16,I32); 566 New(54,PL_markstack,16,I32);
514 PL_markstack_ptr = PL_markstack; 567 PL_markstack_ptr = PL_markstack;
515 PL_markstack_max = PL_markstack + 16; 568 PL_markstack_max = PL_markstack + 16;
516 569
517#ifdef SET_MARK_OFFSET 570#ifdef SET_MARK_OFFSET
518 SET_MARK_OFFSET; 571 SET_MARK_OFFSET;
519#endif 572#endif
520 573
521 New(54,PL_scopestack,16,I32); 574 New(54,PL_scopestack,8,I32);
522 PL_scopestack_ix = 0; 575 PL_scopestack_ix = 0;
523 PL_scopestack_max = 16; 576 PL_scopestack_max = 8;
524 577
525 New(54,PL_savestack,64,ANY); 578 New(54,PL_savestack,24,ANY);
526 PL_savestack_ix = 0; 579 PL_savestack_ix = 0;
527 PL_savestack_max = 64; 580 PL_savestack_max = 24;
528 581
529#if !PERL_VERSION_ATLEAST (5,9,0) 582#if !PERL_VERSION_ATLEAST (5,9,0)
530 New(54,PL_retstack,4,OP*); 583 New(54,PL_retstack,4,OP*);
531 PL_retstack_ix = 0; 584 PL_retstack_ix = 0;
532 PL_retstack_max = 4; 585 PL_retstack_max = 4;
569{ 622{
570 size_t rss = sizeof (*coro); 623 size_t rss = sizeof (*coro);
571 624
572 if (coro->mainstack) 625 if (coro->mainstack)
573 { 626 {
627 perl_slots tmp_slot;
628 perl_slots *slot;
629
574 if (coro->flags & CF_RUNNING) 630 if (coro->flags & CF_RUNNING)
575 { 631 {
632 slot = &tmp_slot;
633
576 #define VAR(name,type)coro->name = PL_ ## name; 634 #define VAR(name,type) slot->name = PL_ ## name;
577 # include "state.h" 635 # include "state.h"
578 #undef VAR 636 #undef VAR
579 } 637 }
638 else
639 slot = coro->slot;
580 640
581 rss += sizeof (coro->curstackinfo); 641 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); 642 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
584 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 643 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
585 rss += coro->tmps_max * sizeof (SV *); 644 rss += slot->tmps_max * sizeof (SV *);
586 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 645 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
587 rss += coro->scopestack_max * sizeof (I32); 646 rss += slot->scopestack_max * sizeof (I32);
588 rss += coro->savestack_max * sizeof (ANY); 647 rss += slot->savestack_max * sizeof (ANY);
589 648
590#if !PERL_VERSION_ATLEAST (5,9,0) 649#if !PERL_VERSION_ATLEAST (5,9,0)
591 rss += coro->retstack_max * sizeof (OP *); 650 rss += slot->retstack_max * sizeof (OP *);
592#endif 651#endif
593 } 652 }
594 653
595 return rss; 654 return rss;
596} 655}
612 PL_curpm = 0; 671 PL_curpm = 0;
613 PL_curpad = 0; 672 PL_curpad = 0;
614 PL_localizing = 0; 673 PL_localizing = 0;
615 PL_dirty = 0; 674 PL_dirty = 0;
616 PL_restartop = 0; 675 PL_restartop = 0;
676 PL_diehook = SvREFCNT_inc (sv_diehook);
677 PL_warnhook = SvREFCNT_inc (sv_warnhook);
617 678
618 GvSV (PL_defgv) = newSV (0); 679 GvSV (PL_defgv) = newSV (0);
619 GvAV (PL_defgv) = coro->args; coro->args = 0; 680 GvAV (PL_defgv) = coro->args; coro->args = 0;
620 GvSV (PL_errgv) = newSV (0); 681 GvSV (PL_errgv) = newSV (0);
621 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 682 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
666 SvREFCNT_dec (GvSV (PL_errgv)); 727 SvREFCNT_dec (GvSV (PL_errgv));
667 SvREFCNT_dec (PL_defoutgv); 728 SvREFCNT_dec (PL_defoutgv);
668 SvREFCNT_dec (PL_rs); 729 SvREFCNT_dec (PL_rs);
669 SvREFCNT_dec (GvSV (irsgv)); 730 SvREFCNT_dec (GvSV (irsgv));
670 731
732 SvREFCNT_dec (PL_diehook);
733 SvREFCNT_dec (PL_warnhook);
734
671 SvREFCNT_dec (coro->saved_deffh); 735 SvREFCNT_dec (coro->saved_deffh);
672 SvREFCNT_dec (coro->throw); 736 SvREFCNT_dec (coro->throw);
673 737
674 coro_destroy_stacks (aTHX); 738 coro_destroy_stacks (aTHX);
675} 739}
991 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1055 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
992 1056
993 if (expect_false (next->flags & CF_DESTROYED)) 1057 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"); 1058 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
995 1059
1060 if (
1061#if PERL_VERSION_ATLEAST (5,9,0)
1062 expect_false (PL_parser)
1063#else
996 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1064 expect_false (PL_lex_state != LEX_NOTPARSING)
1065#endif
1066 )
997 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1067 croak ("Coro::State::transfer called while parsing, but this is not supported");
998 } 1068 }
999} 1069}
1000 1070
1001/* always use the TRANSFER macro */ 1071/* always use the TRANSFER macro */
1130 save_perl (aTHX_ &temp); 1200 save_perl (aTHX_ &temp);
1131 load_perl (aTHX_ coro); 1201 load_perl (aTHX_ coro);
1132 1202
1133 coro_destroy (aTHX_ coro); 1203 coro_destroy (aTHX_ coro);
1134 1204
1135 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1205 load_perl (aTHX_ &temp);
1136 1206
1137 coro->mainstack = 0; 1207 coro->slot = 0;
1138 } 1208 }
1139 1209
1140 cctx_destroy (coro->cctx); 1210 cctx_destroy (coro->cctx);
1141 SvREFCNT_dec (coro->args); 1211 SvREFCNT_dec (coro->args);
1142 1212
1399 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); 1469 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1400 1470
1401 if (coro->flags & CF_RUNNING) 1471 if (coro->flags & CF_RUNNING)
1402 PL_runops = RUNOPS_DEFAULT; 1472 PL_runops = RUNOPS_DEFAULT;
1403 else 1473 else
1404 coro->runops = RUNOPS_DEFAULT; 1474 coro->slot->runops = RUNOPS_DEFAULT;
1405 } 1475 }
1406} 1476}
1407 1477
1408MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1478MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1409 1479
1416#endif 1486#endif
1417 BOOT_PAGESIZE; 1487 BOOT_PAGESIZE;
1418 1488
1419 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1489 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1420 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1490 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1491
1492 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE);
1493 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE);
1494
1495 if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook);
1496 if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook);
1421 1497
1422 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1498 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1423 1499
1424 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1500 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1425 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1501 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1569 CODE: 1645 CODE:
1570{ 1646{
1571 if (coro->mainstack) 1647 if (coro->mainstack)
1572 { 1648 {
1573 struct coro temp; 1649 struct coro temp;
1574 Zero (&temp, 1, struct coro);
1575 1650
1576 if (!(coro->flags & CF_RUNNING)) 1651 if (!(coro->flags & CF_RUNNING))
1577 { 1652 {
1578 save_perl (aTHX_ &temp); 1653 save_perl (aTHX_ &temp);
1579 load_perl (aTHX_ coro); 1654 load_perl (aTHX_ coro);
1583 dSP; 1658 dSP;
1584 ENTER; 1659 ENTER;
1585 SAVETMPS; 1660 SAVETMPS;
1586 PUSHMARK (SP); 1661 PUSHMARK (SP);
1587 PUTBACK; 1662 PUTBACK;
1663
1588 if (ix) 1664 if (ix)
1589 eval_sv (coderef, 0); 1665 eval_sv (coderef, 0);
1590 else 1666 else
1591 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1667 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1668
1592 SPAGAIN; 1669 SPAGAIN;
1593 FREETMPS; 1670 FREETMPS;
1594 LEAVE; 1671 LEAVE;
1595 PUTBACK; 1672 PUTBACK;
1596 } 1673 }
1654 1731
1655BOOT: 1732BOOT:
1656{ 1733{
1657 int i; 1734 int i;
1658 1735
1659 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1736 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE);
1660 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1737 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE);
1661 av_async_pool = get_av ("Coro::async_pool", TRUE); 1738 av_async_pool = coro_get_av ("Coro::async_pool", TRUE);
1662 1739
1663 coro_current = get_sv ("Coro::current", FALSE); 1740 coro_current = coro_get_sv ("Coro::current", FALSE);
1664 SvREADONLY_on (coro_current); 1741 SvREADONLY_on (coro_current);
1665 1742
1666 coro_stash = gv_stashpv ("Coro", TRUE); 1743 coro_stash = gv_stashpv ("Coro", TRUE);
1667 1744
1668 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1745 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1669 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1746 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1670 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1747 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1671 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1748 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1674 1751
1675 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1752 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1676 coro_ready[i] = newAV (); 1753 coro_ready[i] = newAV ();
1677 1754
1678 { 1755 {
1679 SV *sv = perl_get_sv("Coro::API", 1); 1756 SV *sv = perl_get_sv ("Coro::API", TRUE);
1757 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1680 1758
1681 coroapi.schedule = api_schedule; 1759 coroapi.schedule = api_schedule;
1682 coroapi.cede = api_cede; 1760 coroapi.cede = api_cede;
1683 coroapi.cede_notself = api_cede_notself; 1761 coroapi.cede_notself = api_cede_notself;
1684 coroapi.ready = api_ready; 1762 coroapi.ready = api_ready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines