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.314 by root, Thu Nov 20 03:24:39 2008 UTC vs.
Revision 1.320 by root, Fri Nov 21 07:29:04 2008 UTC

170static AV *main_mainstack; /* used to differentiate between $main and others */ 170static AV *main_mainstack; /* used to differentiate between $main and others */
171static JMPENV *main_top_env; 171static JMPENV *main_top_env;
172static HV *coro_state_stash, *coro_stash; 172static HV *coro_state_stash, *coro_stash;
173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
174 174
175static AV *av_destroy; /* destruction queue */
176static SV *sv_manager; /* the manager coro */
177
175static GV *irsgv; /* $/ */ 178static GV *irsgv; /* $/ */
176static GV *stdoutgv; /* *STDOUT */ 179static GV *stdoutgv; /* *STDOUT */
177static SV *rv_diehook; 180static SV *rv_diehook;
178static SV *rv_warnhook; 181static SV *rv_warnhook;
179static HV *hv_sig; /* %SIG */ 182static HV *hv_sig; /* %SIG */
180 183
181/* async_pool helper stuff */ 184/* async_pool helper stuff */
182static SV *sv_pool_rss; 185static SV *sv_pool_rss;
183static SV *sv_pool_size; 186static SV *sv_pool_size;
184static SV *sv_async_pool_idle; 187static SV *sv_async_pool_idle; /* description string */
185static AV *av_async_pool; 188static AV *av_async_pool; /* idle pool */
186static SV *sv_Coro; 189static SV *sv_Coro; /* class string */
187static CV *cv_pool_handler; 190static CV *cv_pool_handler;
188static CV *cv_coro_new; 191static CV *cv_coro_state_new;
189 192
190/* Coro::AnyEvent */ 193/* Coro::AnyEvent */
191static SV *sv_activity; 194static SV *sv_activity;
192 195
193static struct coro_cctx *cctx_first; 196static struct coro_cctx *cctx_first;
834slf_check_nop (pTHX_ struct CoroSLF *frame) 837slf_check_nop (pTHX_ struct CoroSLF *frame)
835{ 838{
836 return 0; 839 return 0;
837} 840}
838 841
842static int
843slf_check_repeat (pTHX_ struct CoroSLF *frame)
844{
845 return 1;
846}
847
839static UNOP coro_setup_op; 848static UNOP coro_setup_op;
840 849
841static void NOINLINE /* noinline to keep it out of the transfer fast path */ 850static void NOINLINE /* noinline to keep it out of the transfer fast path */
842coro_setup (pTHX_ struct coro *coro) 851coro_setup (pTHX_ struct coro *coro)
843{ 852{
892 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 901 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
893 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 902 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
894 903
895 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 904 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
896 coro_setup_op.op_next = PL_op; 905 coro_setup_op.op_next = PL_op;
897 coro_setup_op.op_type = OP_CUSTOM; 906 coro_setup_op.op_type = OP_ENTERSUB;
898 coro_setup_op.op_ppaddr = pp_slf; 907 coro_setup_op.op_ppaddr = pp_slf;
899 /* no flags etc. required, as an init function won't be called */ 908 /* no flags etc. required, as an init function won't be called */
900 909
901 PL_op = (OP *)&coro_setup_op; 910 PL_op = (OP *)&coro_setup_op;
902 911
1017 { 1026 {
1018 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1019 1028
1020 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1021 { 1030 {
1022 runops_proc_t old_runops = PL_runops;
1023 dSP; 1031 dSP;
1024 GV *gv = CvGV (cx->blk_sub.cv); 1032 GV *gv = CvGV (cx->blk_sub.cv);
1025 SV *fullname = sv_2mortal (newSV (0)); 1033 SV *fullname = sv_2mortal (newSV (0));
1026 1034
1027 if (isGV (gv)) 1035 if (isGV (gv))
1612api_is_ready (pTHX_ SV *coro_sv) 1620api_is_ready (pTHX_ SV *coro_sv)
1613{ 1621{
1614 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1622 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1615} 1623}
1616 1624
1625/* expects to own a reference to next->hv */
1617INLINE void 1626INLINE void
1627prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1628{
1629 SV *prev_sv = SvRV (coro_current);
1630
1631 ta->prev = SvSTATE_hv (prev_sv);
1632 ta->next = next;
1633
1634 TRANSFER_CHECK (*ta);
1635
1636 SvRV_set (coro_current, (SV *)next->hv);
1637
1638 free_coro_mortal (aTHX);
1639 coro_mortal = prev_sv;
1640}
1641
1642static void
1618prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1643prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1619{ 1644{
1620 SV *prev_sv, *next_sv;
1621
1622 for (;;) 1645 for (;;)
1623 { 1646 {
1624 next_sv = coro_deq (aTHX); 1647 SV *next_sv = coro_deq (aTHX);
1625 1648
1626 /* nothing to schedule: call the idle handler */
1627 if (expect_false (!next_sv)) 1649 if (expect_true (next_sv))
1628 { 1650 {
1651 struct coro *next = SvSTATE_hv (next_sv);
1652
1653 /* cannot transfer to destroyed coros, skip and look for next */
1654 if (expect_false (next->flags & CF_DESTROYED))
1655 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1656 else
1657 {
1658 next->flags &= ~CF_READY;
1659 --coro_nready;
1660
1661 prepare_schedule_to (aTHX_ ta, next);
1662 break;
1663 }
1664 }
1665 else
1666 {
1667 /* nothing to schedule: call the idle handler */
1629 dSP; 1668 dSP;
1630 1669
1631 ENTER; 1670 ENTER;
1632 SAVETMPS; 1671 SAVETMPS;
1633 1672
1635 PUTBACK; 1674 PUTBACK;
1636 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1637 1676
1638 FREETMPS; 1677 FREETMPS;
1639 LEAVE; 1678 LEAVE;
1640 continue;
1641 } 1679 }
1642
1643 ta->next = SvSTATE_hv (next_sv);
1644
1645 /* cannot transfer to destroyed coros, skip and look for next */
1646 if (expect_false (ta->next->flags & CF_DESTROYED))
1647 {
1648 SvREFCNT_dec (next_sv);
1649 /* coro_nready has already been taken care of by destroy */
1650 continue;
1651 }
1652
1653 --coro_nready;
1654 break;
1655 } 1680 }
1656
1657 /* free this only after the transfer */
1658 prev_sv = SvRV (coro_current);
1659 ta->prev = SvSTATE_hv (prev_sv);
1660 TRANSFER_CHECK (*ta);
1661 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1662 ta->next->flags &= ~CF_READY;
1663 SvRV_set (coro_current, next_sv);
1664
1665 free_coro_mortal (aTHX);
1666 coro_mortal = prev_sv;
1667} 1681}
1668 1682
1669INLINE void 1683INLINE void
1670prepare_cede (pTHX_ struct coro_transfer_args *ta) 1684prepare_cede (pTHX_ struct coro_transfer_args *ta)
1671{ 1685{
1692{ 1706{
1693 struct coro_transfer_args ta; 1707 struct coro_transfer_args ta;
1694 1708
1695 prepare_schedule (aTHX_ &ta); 1709 prepare_schedule (aTHX_ &ta);
1696 TRANSFER (ta, 1); 1710 TRANSFER (ta, 1);
1711}
1712
1713static void
1714api_schedule_to (pTHX_ SV *coro_sv)
1715{
1716 struct coro_transfer_args ta;
1717 struct coro *next = SvSTATE (coro_sv);
1718
1719 SvREFCNT_inc_NN (coro_sv);
1720 prepare_schedule_to (aTHX_ &ta, next);
1697} 1721}
1698 1722
1699static int 1723static int
1700api_cede (pTHX) 1724api_cede (pTHX)
1701{ 1725{
1748 if (coro->flags & CF_RUNNING) 1772 if (coro->flags & CF_RUNNING)
1749 PL_runops = RUNOPS_DEFAULT; 1773 PL_runops = RUNOPS_DEFAULT;
1750 else 1774 else
1751 coro->slot->runops = RUNOPS_DEFAULT; 1775 coro->slot->runops = RUNOPS_DEFAULT;
1752 } 1776 }
1777}
1778
1779static void
1780coro_call_on_destroy (pTHX_ struct coro *coro)
1781{
1782 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1783 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1784
1785 if (on_destroyp)
1786 {
1787 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1788
1789 while (AvFILLp (on_destroy) >= 0)
1790 {
1791 dSP; /* don't disturb outer sp */
1792 SV *cb = av_pop (on_destroy);
1793
1794 PUSHMARK (SP);
1795
1796 if (statusp)
1797 {
1798 int i;
1799 AV *status = (AV *)SvRV (*statusp);
1800 EXTEND (SP, AvFILLp (status) + 1);
1801
1802 for (i = 0; i <= AvFILLp (status); ++i)
1803 PUSHs (AvARRAY (status)[i]);
1804 }
1805
1806 PUTBACK;
1807 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1808 }
1809 }
1810}
1811
1812static void
1813slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1814{
1815 int i;
1816 HV *hv = (HV *)SvRV (coro_current);
1817 AV *av = newAV ();
1818
1819 av_extend (av, items - 1);
1820 for (i = 0; i < items; ++i)
1821 av_push (av, SvREFCNT_inc_NN (arg [i]));
1822
1823 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1824
1825 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1826 api_ready (aTHX_ sv_manager);
1827
1828 frame->prepare = prepare_schedule;
1829 frame->check = slf_check_repeat;
1753} 1830}
1754 1831
1755/*****************************************************************************/ 1832/*****************************************************************************/
1756/* async pool handler */ 1833/* async pool handler */
1757 1834
1990static void 2067static void
1991slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2068slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1992{ 2069{
1993 frame->prepare = prepare_schedule; 2070 frame->prepare = prepare_schedule;
1994 frame->check = slf_check_nop; 2071 frame->check = slf_check_nop;
2072}
2073
2074static void
2075slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2076{
2077 struct coro *next = (struct coro *)slf_frame.data;
2078
2079 SvREFCNT_inc_NN (next->hv);
2080 prepare_schedule_to (aTHX_ ta, next);
2081}
2082
2083static void
2084slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2085{
2086 if (!items)
2087 croak ("Coro::schedule_to expects a coroutine argument, caught");
2088
2089 frame->data = (void *)SvSTATE (arg [0]);
2090 frame->prepare = slf_prepare_schedule_to;
2091 frame->check = slf_check_nop;
2092}
2093
2094static void
2095slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2096{
2097 api_ready (aTHX_ SvRV (coro_current));
2098
2099 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1995} 2100}
1996 2101
1997static void 2102static void
1998slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2103slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1999{ 2104{
2145 } 2250 }
2146 else 2251 else
2147 slf_argc = 0; 2252 slf_argc = 0;
2148 2253
2149 PL_op->op_ppaddr = pp_slf; 2254 PL_op->op_ppaddr = pp_slf;
2150 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2255 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2151 2256
2152 PL_op = (OP *)&slf_restore; 2257 PL_op = (OP *)&slf_restore;
2153} 2258}
2154 2259
2155/*****************************************************************************/ 2260/*****************************************************************************/
2660 2765
2661 { 2766 {
2662 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2767 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2663 2768
2664 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2769 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2665 hv_store_ent (PL_custom_op_names, slf, 2770 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2666 newSVpv ("coro_slf", 0), 0);
2667 2771
2668 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2772 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2669 hv_store_ent (PL_custom_op_descs, slf, 2773 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2670 newSVpv ("coro schedule like function", 0), 0);
2671 } 2774 }
2672 2775
2673 coroapi.ver = CORO_API_VERSION; 2776 coroapi.ver = CORO_API_VERSION;
2674 coroapi.rev = CORO_API_REVISION; 2777 coroapi.rev = CORO_API_REVISION;
2675 2778
2953 3056
2954BOOT: 3057BOOT:
2955{ 3058{
2956 int i; 3059 int i;
2957 3060
2958 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2959 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3061 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2960 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3062 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2961 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3063 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2962 cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); 3064 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2963 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 3065 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2964 SvREADONLY_on (coro_current); 3066 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3067 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3068 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
2965 3069
2966 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3070 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
2967 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3071 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2968 cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); 3072 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
2969 cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); 3073 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2970 3074
2971 coro_stash = gv_stashpv ("Coro", TRUE); 3075 coro_stash = gv_stashpv ("Coro", TRUE);
2972 3076
2973 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3077 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2974 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3078 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2982 3086
2983 { 3087 {
2984 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3088 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2985 3089
2986 coroapi.schedule = api_schedule; 3090 coroapi.schedule = api_schedule;
3091 coroapi.schedule_to = api_schedule_to;
2987 coroapi.cede = api_cede; 3092 coroapi.cede = api_cede;
2988 coroapi.cede_notself = api_cede_notself; 3093 coroapi.cede_notself = api_cede_notself;
2989 coroapi.ready = api_ready; 3094 coroapi.ready = api_ready;
2990 coroapi.is_ready = api_is_ready; 3095 coroapi.is_ready = api_is_ready;
2991 coroapi.nready = coro_nready; 3096 coroapi.nready = coro_nready;
2996 SvREADONLY_on (sv); 3101 SvREADONLY_on (sv);
2997 } 3102 }
2998} 3103}
2999 3104
3000void 3105void
3106terminate (...)
3107 CODE:
3108 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3109
3110void
3001schedule (...) 3111schedule (...)
3002 CODE: 3112 CODE:
3003 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3113 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3114
3115void
3116schedule_to (...)
3117 CODE:
3118 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3119
3120void
3121cede_to (...)
3122 CODE:
3123 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
3004 3124
3005void 3125void
3006cede (...) 3126cede (...)
3007 CODE: 3127 CODE:
3008 CORO_EXECUTE_SLF_XS (slf_init_cede); 3128 CORO_EXECUTE_SLF_XS (slf_init_cede);
3009 3129
3010void 3130void
3011cede_notself (...) 3131cede_notself (...)
3012 CODE: 3132 CODE:
3013 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3133 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3134
3135void
3136_cancel (Coro::State self)
3137 CODE:
3138 coro_state_destroy (aTHX_ self);
3139 coro_call_on_destroy (aTHX_ self);
3014 3140
3015void 3141void
3016_set_current (SV *current) 3142_set_current (SV *current)
3017 PROTOTYPE: $ 3143 PROTOTYPE: $
3018 CODE: 3144 CODE:
3089 PUSHMARK (SP); 3215 PUSHMARK (SP);
3090 EXTEND (SP, 2); 3216 EXTEND (SP, 2);
3091 PUSHs (sv_Coro); 3217 PUSHs (sv_Coro);
3092 PUSHs ((SV *)cv_pool_handler); 3218 PUSHs ((SV *)cv_pool_handler);
3093 PUTBACK; 3219 PUTBACK;
3094 call_sv ((SV *)cv_coro_new, G_SCALAR); 3220 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3095 SPAGAIN; 3221 SPAGAIN;
3096 3222
3097 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3223 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
3098 } 3224 }
3099 3225

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines