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.317 by root, Thu Nov 20 06:28:52 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))
1648 else 1656 else
1649 { 1657 {
1650 next->flags &= ~CF_READY; 1658 next->flags &= ~CF_READY;
1651 --coro_nready; 1659 --coro_nready;
1652 1660
1653 return prepare_schedule_to (aTHX_ ta, next); 1661 prepare_schedule_to (aTHX_ ta, next);
1662 break;
1654 } 1663 }
1655 } 1664 }
1656 else 1665 else
1657 { 1666 {
1658 /* nothing to schedule: call the idle handler */ 1667 /* nothing to schedule: call the idle handler */
1763 if (coro->flags & CF_RUNNING) 1772 if (coro->flags & CF_RUNNING)
1764 PL_runops = RUNOPS_DEFAULT; 1773 PL_runops = RUNOPS_DEFAULT;
1765 else 1774 else
1766 coro->slot->runops = RUNOPS_DEFAULT; 1775 coro->slot->runops = RUNOPS_DEFAULT;
1767 } 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;
1768} 1830}
1769 1831
1770/*****************************************************************************/ 1832/*****************************************************************************/
1771/* async pool handler */ 1833/* async pool handler */
1772 1834
2188 } 2250 }
2189 else 2251 else
2190 slf_argc = 0; 2252 slf_argc = 0;
2191 2253
2192 PL_op->op_ppaddr = pp_slf; 2254 PL_op->op_ppaddr = pp_slf;
2193 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 */
2194 2256
2195 PL_op = (OP *)&slf_restore; 2257 PL_op = (OP *)&slf_restore;
2196} 2258}
2197 2259
2198/*****************************************************************************/ 2260/*****************************************************************************/
2703 2765
2704 { 2766 {
2705 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2767 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2706 2768
2707 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2769 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2708 hv_store_ent (PL_custom_op_names, slf, 2770 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2709 newSVpv ("coro_slf", 0), 0);
2710 2771
2711 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2772 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2712 hv_store_ent (PL_custom_op_descs, slf, 2773 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2713 newSVpv ("coro schedule like function", 0), 0);
2714 } 2774 }
2715 2775
2716 coroapi.ver = CORO_API_VERSION; 2776 coroapi.ver = CORO_API_VERSION;
2717 coroapi.rev = CORO_API_REVISION; 2777 coroapi.rev = CORO_API_REVISION;
2718 2778
2996 3056
2997BOOT: 3057BOOT:
2998{ 3058{
2999 int i; 3059 int i;
3000 3060
3001 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3002 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3061 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3003 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3062 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3004 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3063 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
3005 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3064 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3006 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);
3007 3069
3008 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);
3009 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3071 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3010 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);
3011 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);
3039 SvREADONLY_on (sv); 3101 SvREADONLY_on (sv);
3040 } 3102 }
3041} 3103}
3042 3104
3043void 3105void
3106terminate (...)
3107 CODE:
3108 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3109
3110void
3044schedule (...) 3111schedule (...)
3045 CODE: 3112 CODE:
3046 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3113 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3047 3114
3048void 3115void
3062 3129
3063void 3130void
3064cede_notself (...) 3131cede_notself (...)
3065 CODE: 3132 CODE:
3066 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);
3067 3140
3068void 3141void
3069_set_current (SV *current) 3142_set_current (SV *current)
3070 PROTOTYPE: $ 3143 PROTOTYPE: $
3071 CODE: 3144 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines