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.316 by root, Thu Nov 20 06:01:40 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_state_new; 191static CV *cv_coro_state_new;
189 192
190/* Coro::AnyEvent */ 193/* Coro::AnyEvent */
191static SV *sv_activity; 194static SV *sv_activity;
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))
1614 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1622 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1615} 1623}
1616 1624
1617/* expects to own a reference to next->hv */ 1625/* expects to own a reference to next->hv */
1618INLINE void 1626INLINE void
1619prepare_cede_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1627prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1620{ 1628{
1621 SV *prev_sv = SvRV (coro_current); 1629 SV *prev_sv = SvRV (coro_current);
1622 1630
1623 ta->prev = SvSTATE_hv (prev_sv); 1631 ta->prev = SvSTATE_hv (prev_sv);
1624 ta->next = next; 1632 ta->next = next;
1625 1633
1626 TRANSFER_CHECK (*ta); 1634 TRANSFER_CHECK (*ta);
1627 1635
1628 SvRV_set (coro_current, next->hv); 1636 SvRV_set (coro_current, (SV *)next->hv);
1629 1637
1630 free_coro_mortal (aTHX); 1638 free_coro_mortal (aTHX);
1631 coro_mortal = prev_sv; 1639 coro_mortal = prev_sv;
1632} 1640}
1633 1641
1636{ 1644{
1637 for (;;) 1645 for (;;)
1638 { 1646 {
1639 SV *next_sv = coro_deq (aTHX); 1647 SV *next_sv = coro_deq (aTHX);
1640 1648
1641 if (expect_false (!next_sv)) 1649 if (expect_true (next_sv))
1642 {
1643 /* nothing to schedule: call the idle handler */
1644 dSP;
1645
1646 ENTER;
1647 SAVETMPS;
1648
1649 PUSHMARK (SP);
1650 PUTBACK;
1651 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1652
1653 FREETMPS;
1654 LEAVE;
1655 }
1656 else
1657 { 1650 {
1658 struct coro *next = SvSTATE_hv (next_sv); 1651 struct coro *next = SvSTATE_hv (next_sv);
1659 1652
1660 /* cannot transfer to destroyed coros, skip and look for next */ 1653 /* cannot transfer to destroyed coros, skip and look for next */
1661 if (expect_false (next->flags & CF_DESTROYED)) 1654 if (expect_false (next->flags & CF_DESTROYED))
1663 else 1656 else
1664 { 1657 {
1665 next->flags &= ~CF_READY; 1658 next->flags &= ~CF_READY;
1666 --coro_nready; 1659 --coro_nready;
1667 1660
1668 return prepare_cede_to (aTHX_ ta, next); 1661 prepare_schedule_to (aTHX_ ta, next);
1662 break;
1669 } 1663 }
1670 } 1664 }
1665 else
1666 {
1667 /* nothing to schedule: call the idle handler */
1668 dSP;
1669
1670 ENTER;
1671 SAVETMPS;
1672
1673 PUSHMARK (SP);
1674 PUTBACK;
1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1676
1677 FREETMPS;
1678 LEAVE;
1679 }
1671 } 1680 }
1672} 1681}
1673 1682
1674INLINE void 1683INLINE void
1675prepare_cede (pTHX_ struct coro_transfer_args *ta) 1684prepare_cede (pTHX_ struct coro_transfer_args *ta)
1697{ 1706{
1698 struct coro_transfer_args ta; 1707 struct coro_transfer_args ta;
1699 1708
1700 prepare_schedule (aTHX_ &ta); 1709 prepare_schedule (aTHX_ &ta);
1701 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);
1702} 1721}
1703 1722
1704static int 1723static int
1705api_cede (pTHX) 1724api_cede (pTHX)
1706{ 1725{
1753 if (coro->flags & CF_RUNNING) 1772 if (coro->flags & CF_RUNNING)
1754 PL_runops = RUNOPS_DEFAULT; 1773 PL_runops = RUNOPS_DEFAULT;
1755 else 1774 else
1756 coro->slot->runops = RUNOPS_DEFAULT; 1775 coro->slot->runops = RUNOPS_DEFAULT;
1757 } 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;
1758} 1830}
1759 1831
1760/*****************************************************************************/ 1832/*****************************************************************************/
1761/* async pool handler */ 1833/* async pool handler */
1762 1834
1995static void 2067static void
1996slf_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)
1997{ 2069{
1998 frame->prepare = prepare_schedule; 2070 frame->prepare = prepare_schedule;
1999 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);
2000} 2100}
2001 2101
2002static void 2102static void
2003slf_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)
2004{ 2104{
2150 } 2250 }
2151 else 2251 else
2152 slf_argc = 0; 2252 slf_argc = 0;
2153 2253
2154 PL_op->op_ppaddr = pp_slf; 2254 PL_op->op_ppaddr = pp_slf;
2155 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 */
2156 2256
2157 PL_op = (OP *)&slf_restore; 2257 PL_op = (OP *)&slf_restore;
2158} 2258}
2159 2259
2160/*****************************************************************************/ 2260/*****************************************************************************/
2665 2765
2666 { 2766 {
2667 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2767 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2668 2768
2669 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2769 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2670 hv_store_ent (PL_custom_op_names, slf, 2770 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2671 newSVpv ("coro_slf", 0), 0);
2672 2771
2673 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2772 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2674 hv_store_ent (PL_custom_op_descs, slf, 2773 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2675 newSVpv ("coro schedule like function", 0), 0);
2676 } 2774 }
2677 2775
2678 coroapi.ver = CORO_API_VERSION; 2776 coroapi.ver = CORO_API_VERSION;
2679 coroapi.rev = CORO_API_REVISION; 2777 coroapi.rev = CORO_API_REVISION;
2680 2778
2958 3056
2959BOOT: 3057BOOT:
2960{ 3058{
2961 int i; 3059 int i;
2962 3060
2963 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2964 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3061 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2965 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3062 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2966 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3063 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2967 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3064 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2968 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3065 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 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);
2969 3069
2970 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);
2971 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3071 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2972 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3072 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
2973 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3073 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2986 3086
2987 { 3087 {
2988 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3088 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2989 3089
2990 coroapi.schedule = api_schedule; 3090 coroapi.schedule = api_schedule;
3091 coroapi.schedule_to = api_schedule_to;
2991 coroapi.cede = api_cede; 3092 coroapi.cede = api_cede;
2992 coroapi.cede_notself = api_cede_notself; 3093 coroapi.cede_notself = api_cede_notself;
2993 coroapi.ready = api_ready; 3094 coroapi.ready = api_ready;
2994 coroapi.is_ready = api_is_ready; 3095 coroapi.is_ready = api_is_ready;
2995 coroapi.nready = coro_nready; 3096 coroapi.nready = coro_nready;
3000 SvREADONLY_on (sv); 3101 SvREADONLY_on (sv);
3001 } 3102 }
3002} 3103}
3003 3104
3004void 3105void
3106terminate (...)
3107 CODE:
3108 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3109
3110void
3005schedule (...) 3111schedule (...)
3006 CODE: 3112 CODE:
3007 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);
3008 3124
3009void 3125void
3010cede (...) 3126cede (...)
3011 CODE: 3127 CODE:
3012 CORO_EXECUTE_SLF_XS (slf_init_cede); 3128 CORO_EXECUTE_SLF_XS (slf_init_cede);
3013 3129
3014void 3130void
3015cede_notself (...) 3131cede_notself (...)
3016 CODE: 3132 CODE:
3017 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);
3018 3140
3019void 3141void
3020_set_current (SV *current) 3142_set_current (SV *current)
3021 PROTOTYPE: $ 3143 PROTOTYPE: $
3022 CODE: 3144 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines