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.166 by root, Wed Sep 26 21:11:28 2007 UTC vs.
Revision 1.172 by root, Sun Sep 30 13:15:01 2007 UTC

141 141
142static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
143static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
144 144
145enum { 145enum {
146 CC_MAPPED = 0x01, 146 CC_MAPPED = 0x01,
147 CC_TRACE = 0x02,
148 CC_NOREUSE = 0x04, /* throw this away after tracing */ 147 CC_NOREUSE = 0x02, /* throw this away after tracing */
148 CC_TRACE = 0x04,
149 CC_TRACE_SUB = 0x08, /* trace sub calls */
150 CC_TRACE_LINE = 0x10, /* trace each statement */
151 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
149}; 152};
150 153
151/* this is a structure representing a c-level coroutine */ 154/* this is a structure representing a c-level coroutine */
152typedef struct coro_cctx { 155typedef struct coro_cctx {
153 struct coro_cctx *next; 156 struct coro_cctx *next;
196 199
197#define VAR(name,type) type name; 200#define VAR(name,type) type name;
198# include "state.h" 201# include "state.h"
199#undef VAR 202#undef VAR
200 203
204 /* statistics */
205 int usecount; /* number of switches to this coro */
206
201 /* coro process data */ 207 /* coro process data */
202 int prio; 208 int prio;
203 209
204 /* linked list */ 210 /* linked list */
205 struct coro *next, *prev; 211 struct coro *next, *prev;
295 SvMAGIC (cv) \ 301 SvMAGIC (cv) \
296 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 302 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
297 ? SvMAGIC (cv) \ 303 ? SvMAGIC (cv) \
298 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 304 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
299 : 0 305 : 0
306
307static struct coro *
308SvSTATE_ (pTHX_ SV *coro)
309{
310 HV *stash;
311 MAGIC *mg;
312
313 if (SvROK (coro))
314 coro = SvRV (coro);
315
316 if (SvTYPE (coro) != SVt_PVHV)
317 croak ("Coro::State object required");
318
319 stash = SvSTASH (coro);
320 if (stash != coro_stash && stash != coro_state_stash)
321 {
322 /* very slow, but rare, check */
323 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
324 croak ("Coro::State object required");
325 }
326
327 mg = CORO_MAGIC (coro);
328 return (struct coro *)mg->mg_ptr;
329}
330
331#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
300 332
301/* the next two functions merely cache the padlists */ 333/* the next two functions merely cache the padlists */
302static void 334static void
303get_padlist (pTHX_ CV *cv) 335get_padlist (pTHX_ CV *cv)
304{ 336{
550 Safefree (PL_retstack); 582 Safefree (PL_retstack);
551#endif 583#endif
552} 584}
553 585
554static size_t 586static size_t
555coro_rss (struct coro *coro) 587coro_rss (pTHX_ struct coro *coro)
556{ 588{
557 size_t rss = sizeof (coro); 589 size_t rss = sizeof (coro);
558 590
559 if (coro->mainstack) 591 if (coro->mainstack)
560 { 592 {
590 /* 622 /*
591 * emulate part of the perl startup here. 623 * emulate part of the perl startup here.
592 */ 624 */
593 coro_init_stacks (aTHX); 625 coro_init_stacks (aTHX);
594 626
627 PL_runops = RUNOPS_DEFAULT;
595 PL_curcop = &PL_compiling; 628 PL_curcop = &PL_compiling;
596 PL_in_eval = EVAL_NULL; 629 PL_in_eval = EVAL_NULL;
597 PL_comppad = 0; 630 PL_comppad = 0;
598 PL_curpm = 0; 631 PL_curpm = 0;
599 PL_localizing = 0; 632 PL_localizing = 0;
630 SvREFCNT_dec (coro_mortal); 663 SvREFCNT_dec (coro_mortal);
631 coro_mortal = 0; 664 coro_mortal = 0;
632 } 665 }
633} 666}
634 667
635static void
636do_trace (pTHX)
637{
638 if (PL_curcop != &PL_compiling)
639 {
640 runops_proc_t old_runops = PL_runops;
641 dSP;
642 ENTER;
643 SAVETMPS;
644 PUSHMARK (SP);
645 PUTBACK;
646 PL_runops = RUNOPS_DEFAULT;
647 call_pv ("Coro::_do_trace", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD | G_NOARGS);
648 PL_runops = old_runops;
649 SPAGAIN;
650 FREETMPS;
651 LEAVE;
652 PUTBACK;
653 }
654}
655
656static int 668static int
657runops_coro (pTHX) 669runops_trace (pTHX)
658{ 670{
659 COP *oldcop = 0; 671 COP *oldcop = 0;
672 int oldcxix = -2;
673 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */
674 coro_cctx *cctx = coro->cctx;
660 675
661 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 676 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
662 { 677 {
663 PERL_ASYNC_CHECK (); 678 PERL_ASYNC_CHECK ();
679
680 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
681 {
682 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
683 SV **bot, **top;
684 AV *av = newAV (); /* return values */
685 SV **cb;
686 runops_proc_t old_runops = PL_runops;
687 dSP;
688 ENTER;
689 SAVETMPS;
690 EXTEND (SP, 3);
691 PL_runops = RUNOPS_DEFAULT;
692
693 GV *gv = CvGV (cx->blk_sub.cv);
694 SV *fullname = sv_2mortal (newSV (0));
695 if (isGV (gv))
696 gv_efullname3 (fullname, gv, 0);
697
698 bot = PL_stack_base + cx->blk_oldsp + 1;
699 top = cx->blk_gimme == G_ARRAY ? SP + 1
700 : cx->blk_gimme == G_SCALAR ? bot + 1
701 : bot;
702
703 while (bot < top)
704 av_push (av, SvREFCNT_inc (*bot++));
705
706 PUSHMARK (SP);
707 PUSHs (&PL_sv_no);
708 PUSHs (fullname);
709 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
710 PUTBACK;
711 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
712 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
713 SPAGAIN;
714
715 FREETMPS;
716 LEAVE;
717 PL_runops = old_runops;
718 }
664 719
665 if (oldcop != PL_curcop) 720 if (oldcop != PL_curcop)
666 { 721 {
667 oldcop = PL_curcop; 722 oldcop = PL_curcop;
668 do_trace (aTHX); 723
724 if (PL_curcop != &PL_compiling)
725 {
726 SV **cb;
727 runops_proc_t old_runops = PL_runops;
728 dSP;
729 ENTER;
730 SAVETMPS;
731 EXTEND (SP, 3);
732 PL_runops = RUNOPS_DEFAULT;
733
734 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
735 {
736 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
737
738 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
739 {
740 GV *gv = CvGV (cx->blk_sub.cv);
741 SV *fullname = sv_2mortal (newSV (0));
742 if (isGV (gv))
743 gv_efullname3 (fullname, gv, 0);
744
745 PUSHMARK (SP);
746 PUSHs (&PL_sv_yes);
747 PUSHs (fullname);
748 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
749 PUTBACK;
750 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
751 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
752 SPAGAIN;
753 }
754
755 oldcxix = cxstack_ix;
756 }
757
758 if (cctx->flags & CC_TRACE_LINE)
759 {
760 PUSHMARK (SP);
761 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
762 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
763 PUTBACK;
764 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
765 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
766 SPAGAIN;
767 }
768
769 FREETMPS;
770 LEAVE;
771 PL_runops = old_runops;
772 }
669 } 773 }
670 } 774 }
671 775
672 TAINT_NOT; 776 TAINT_NOT;
673 return 0; 777 return 0;
683 LOGOP myop; 787 LOGOP myop;
684 788
685 PL_top_env = &PL_start_env; 789 PL_top_env = &PL_start_env;
686 790
687 if (cctx->flags & CC_TRACE) 791 if (cctx->flags & CC_TRACE)
688 PL_runops = runops_coro; 792 PL_runops = runops_trace;
689 793
690 Zero (&myop, 1, LOGOP); 794 Zero (&myop, 1, LOGOP);
691 myop.op_next = PL_op; 795 myop.op_next = PL_op;
692 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 796 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
693 797
839 cctx_first = cctx; 943 cctx_first = cctx;
840} 944}
841 945
842/** coroutine switching *****************************************************/ 946/** coroutine switching *****************************************************/
843 947
844/* never call directly, always through the coro_state_transfer global variable */ 948static void NOINLINE
949transfer_check (pTHX_ struct coro *prev, struct coro *next)
950{
951 if (prev != next)
952 {
953 if (!(prev->flags & (CF_RUNNING | CF_NEW)))
954 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
955
956 if (next->flags & CF_RUNNING)
957 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
958
959 if (next->flags & CF_DESTROYED)
960 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
961
962 if (PL_lex_state != LEX_NOTPARSING)
963 croak ("Coro::State::transfer called while parsing, but this is not supported");
964 }
965}
966
967/* always use the TRANSFER macro */
845static void NOINLINE 968static void NOINLINE
846transfer (pTHX_ struct coro *prev, struct coro *next) 969transfer (pTHX_ struct coro *prev, struct coro *next)
847{ 970{
848 dSTACKLEVEL; 971 dSTACKLEVEL;
849 972
862 /* create a new empty context */ 985 /* create a new empty context */
863 Newz (0, prev->cctx, 1, coro_cctx); 986 Newz (0, prev->cctx, 1, coro_cctx);
864 prev->flags &= ~CF_NEW; 987 prev->flags &= ~CF_NEW;
865 prev->flags |= CF_RUNNING; 988 prev->flags |= CF_RUNNING;
866 } 989 }
867
868 /*TODO: must not croak here */
869 if (!prev->flags & CF_RUNNING)
870 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
871
872 if (next->flags & CF_RUNNING)
873 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
874
875 if (next->flags & CF_DESTROYED)
876 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
877 990
878 prev->flags &= ~CF_RUNNING; 991 prev->flags &= ~CF_RUNNING;
879 next->flags |= CF_RUNNING; 992 next->flags |= CF_RUNNING;
880 993
881 LOCK; 994 LOCK;
897 } 1010 }
898 1011
899 prev__cctx = prev->cctx; 1012 prev__cctx = prev->cctx;
900 1013
901 /* possibly "free" the cctx */ 1014 /* possibly "free" the cctx */
902 if (prev__cctx->idle_sp == STACKLEVEL) 1015 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
903 { 1016 {
904 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1017 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
905 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1018 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
906 1019
907 prev->cctx = 0; 1020 prev->cctx = 0;
908 1021
909 cctx_put (prev__cctx); 1022 cctx_put (prev__cctx);
910 } 1023 }
1024
1025 ++next->usecount;
911 1026
912 if (!next->cctx) 1027 if (!next->cctx)
913 next->cctx = cctx_get (aTHX); 1028 next->cctx = cctx_get (aTHX);
914 1029
915 if (prev__cctx != next->cctx) 1030 if (prev__cctx != next->cctx)
928{ 1043{
929 struct coro *prev, *next; 1044 struct coro *prev, *next;
930}; 1045};
931 1046
932#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1047#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
1048#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
933 1049
934/** high level stuff ********************************************************/ 1050/** high level stuff ********************************************************/
935 1051
936static int 1052static int
937coro_state_destroy (pTHX_ struct coro *coro) 1053coro_state_destroy (pTHX_ struct coro *coro)
1020#else 1136#else
1021# define MGf_DUP 0 1137# define MGf_DUP 0
1022#endif 1138#endif
1023}; 1139};
1024 1140
1025static struct coro *
1026SvSTATE_ (pTHX_ SV *coro)
1027{
1028 HV *stash;
1029 MAGIC *mg;
1030
1031 if (SvROK (coro))
1032 coro = SvRV (coro);
1033
1034 stash = SvSTASH (coro);
1035 if (stash != coro_stash && stash != coro_state_stash)
1036 {
1037 /* very slow, but rare, check */
1038 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
1039 croak ("Coro::State object required");
1040 }
1041
1042 mg = CORO_MAGIC (coro);
1043 return (struct coro *)mg->mg_ptr;
1044}
1045
1046#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
1047
1048static void 1141static void
1049prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1142prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1050{ 1143{
1051 ta->prev = SvSTATE (prev_sv); 1144 ta->prev = SvSTATE (prev_sv);
1052 ta->next = SvSTATE (next_sv); 1145 ta->next = SvSTATE (next_sv);
1146 TRANSFER_CHECK (*ta);
1053} 1147}
1054 1148
1055static void 1149static void
1056api_transfer (SV *prev_sv, SV *next_sv) 1150api_transfer (SV *prev_sv, SV *next_sv)
1057{ 1151{
1174 break; 1268 break;
1175 } 1269 }
1176 1270
1177 /* free this only after the transfer */ 1271 /* free this only after the transfer */
1178 prev_sv = SvRV (coro_current); 1272 prev_sv = SvRV (coro_current);
1179 SvRV_set (coro_current, next_sv);
1180 ta->prev = SvSTATE (prev_sv); 1273 ta->prev = SvSTATE (prev_sv);
1181 1274 TRANSFER_CHECK (*ta);
1182 assert (ta->next->flags & CF_READY); 1275 assert (ta->next->flags & CF_READY);
1183 ta->next->flags &= ~CF_READY; 1276 ta->next->flags &= ~CF_READY;
1277 SvRV_set (coro_current, next_sv);
1184 1278
1185 LOCK; 1279 LOCK;
1186 free_coro_mortal (aTHX); 1280 free_coro_mortal (aTHX);
1187 coro_mortal = prev_sv; 1281 coro_mortal = prev_sv;
1188 UNLOCK; 1282 UNLOCK;
1261 MUTEX_INIT (&coro_mutex); 1355 MUTEX_INIT (&coro_mutex);
1262#endif 1356#endif
1263 BOOT_PAGESIZE; 1357 BOOT_PAGESIZE;
1264 1358
1265 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1359 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1360
1361 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1362 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1363 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1364 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1266 1365
1267 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1366 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1268 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1367 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1269 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1368 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1270 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1369 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1462 save_perl (aTHX_ coro); 1561 save_perl (aTHX_ coro);
1463 load_perl (aTHX_ &temp); 1562 load_perl (aTHX_ &temp);
1464 } 1563 }
1465 } 1564 }
1466} 1565}
1467 1566
1468SV * 1567SV *
1469is_ready (Coro::State coro) 1568is_ready (Coro::State coro)
1470 PROTOTYPE: $ 1569 PROTOTYPE: $
1471 ALIAS: 1570 ALIAS:
1472 is_ready = CF_READY 1571 is_ready = CF_READY
1477 RETVAL = boolSV (coro->flags & ix); 1576 RETVAL = boolSV (coro->flags & ix);
1478 OUTPUT: 1577 OUTPUT:
1479 RETVAL 1578 RETVAL
1480 1579
1481void 1580void
1482trace (Coro::State coro, int enable = 0) 1581trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB)
1483 CODE: 1582 CODE:
1484 if (enable) 1583 if (flags & CC_TRACE)
1485 { 1584 {
1486 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1487 XSRETURN_EMPTY;
1488
1489 if (coro->flags & CF_RUNNING)
1490 croak ("cannot enable tracing on running coroutine");
1491
1492 if (coro->cctx) 1585 if (!coro->cctx)
1586 coro->cctx = cctx_new ();
1587 else if (!(coro->cctx->flags & CC_TRACE))
1493 croak ("cannot enable tracing on coroutine with custom stack"); 1588 croak ("cannot enable tracing on coroutine with custom stack");
1494 1589
1495 coro->cctx = cctx_new ();
1496 coro->cctx->flags |= CC_TRACE; 1590 coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL);
1497 } 1591 }
1498 else 1592 else
1499 if (coro->cctx && coro->cctx->flags & CC_TRACE) 1593 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1500 { 1594 {
1501 coro->cctx->flags &= ~CC_TRACE; 1595 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1502 coro->cctx->flags |= CC_NOREUSE; 1596 coro->cctx->flags |= CC_NOREUSE;
1597
1598 if (coro->flags & CF_RUNNING)
1599 PL_runops = RUNOPS_DEFAULT;
1600 else
1601 coro->runops = RUNOPS_DEFAULT;
1503 } 1602 }
1504 1603
1505SV * 1604SV *
1506has_stack (Coro::State coro) 1605has_stack (Coro::State coro)
1507 PROTOTYPE: $ 1606 PROTOTYPE: $
1508 CODE: 1607 CODE:
1509 RETVAL = boolSV (!!coro->cctx); 1608 RETVAL = boolSV (!!coro->cctx);
1510 OUTPUT: 1609 OUTPUT:
1511 RETVAL 1610 RETVAL
1512 1611
1612int
1613is_traced (Coro::State coro)
1614 PROTOTYPE: $
1615 CODE:
1616 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1617 OUTPUT:
1618 RETVAL
1619
1513IV 1620IV
1514rss (Coro::State coro) 1621rss (Coro::State coro)
1515 PROTOTYPE: $ 1622 PROTOTYPE: $
1623 ALIAS:
1624 usecount = 1
1516 CODE: 1625 CODE:
1517 RETVAL = coro_rss (coro); 1626 switch (ix)
1627 {
1628 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1629 case 1: RETVAL = coro->usecount; break;
1630 }
1518 OUTPUT: 1631 OUTPUT:
1519 RETVAL 1632 RETVAL
1520 1633
1521 1634
1522MODULE = Coro::State PACKAGE = Coro 1635MODULE = Coro::State PACKAGE = Coro
1645{ 1758{
1646 struct coro *coro = SvSTATE (coro_current); 1759 struct coro *coro = SvSTATE (coro_current);
1647 1760
1648 sv_setsv (cb, &PL_sv_undef); 1761 sv_setsv (cb, &PL_sv_undef);
1649 1762
1650 if (coro_rss (coro) > SvIV (sv_pool_rss) 1763 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1651 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1764 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1652 croak ("\3terminate\2\n"); 1765 croak ("\3terminate\2\n");
1653 1766
1654 av_clear (GvAV (PL_defgv)); 1767 av_clear (GvAV (PL_defgv));
1655 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 1768 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines