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.313 by root, Thu Nov 20 03:22:59 2008 UTC vs.
Revision 1.318 by root, Thu Nov 20 07:02:43 2008 UTC

98# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
99#endif 99#endif
100#ifndef CvISXSUB_on 100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv 101# define CvISXSUB_on(cv) (void)cv
102#endif 102#endif
103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif
103 106
104/* 5.8.7 */ 107/* 5.8.7 */
105#ifndef SvRV_set 108#ifndef SvRV_set
106# define SvRV_set(s,v) SvRV(s) = (v) 109# define SvRV_set(s,v) SvRV(s) = (v)
107#endif 110#endif
167static AV *main_mainstack; /* used to differentiate between $main and others */ 170static AV *main_mainstack; /* used to differentiate between $main and others */
168static JMPENV *main_top_env; 171static JMPENV *main_top_env;
169static HV *coro_state_stash, *coro_stash; 172static HV *coro_state_stash, *coro_stash;
170static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
171 174
175static AV *av_destroy; /* destruction queue */
176static SV *sv_manager; /* the manager coro */
177
172static GV *irsgv; /* $/ */ 178static GV *irsgv; /* $/ */
173static GV *stdoutgv; /* *STDOUT */ 179static GV *stdoutgv; /* *STDOUT */
174static SV *rv_diehook; 180static SV *rv_diehook;
175static SV *rv_warnhook; 181static SV *rv_warnhook;
176static HV *hv_sig; /* %SIG */ 182static HV *hv_sig; /* %SIG */
177 183
178/* async_pool helper stuff */ 184/* async_pool helper stuff */
179static SV *sv_pool_rss; 185static SV *sv_pool_rss;
180static SV *sv_pool_size; 186static SV *sv_pool_size;
181static SV *sv_async_pool_idle; 187static SV *sv_async_pool_idle; /* description string */
182static AV *av_async_pool; 188static AV *av_async_pool; /* idle pool */
183static SV *sv_Coro; 189static SV *sv_Coro; /* class string */
184static CV *cv_pool_handler; 190static CV *cv_pool_handler;
185static CV *cv_coro_new; 191static CV *cv_coro_state_new;
186 192
187/* Coro::AnyEvent */ 193/* Coro::AnyEvent */
188static SV *sv_activity; 194static SV *sv_activity;
189 195
190static struct coro_cctx *cctx_first; 196static struct coro_cctx *cctx_first;
829 835
830static int 836static int
831slf_check_nop (pTHX_ struct CoroSLF *frame) 837slf_check_nop (pTHX_ struct CoroSLF *frame)
832{ 838{
833 return 0; 839 return 0;
840}
841
842static int
843slf_check_repeat (pTHX_ struct CoroSLF *frame)
844{
845 return 1;
834} 846}
835 847
836static UNOP coro_setup_op; 848static UNOP coro_setup_op;
837 849
838static 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 */
1609api_is_ready (pTHX_ SV *coro_sv) 1621api_is_ready (pTHX_ SV *coro_sv)
1610{ 1622{
1611 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1623 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1612} 1624}
1613 1625
1626/* expects to own a reference to next->hv */
1614INLINE void 1627INLINE void
1628prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1629{
1630 SV *prev_sv = SvRV (coro_current);
1631
1632 ta->prev = SvSTATE_hv (prev_sv);
1633 ta->next = next;
1634
1635 TRANSFER_CHECK (*ta);
1636
1637 SvRV_set (coro_current, (SV *)next->hv);
1638
1639 free_coro_mortal (aTHX);
1640 coro_mortal = prev_sv;
1641}
1642
1643static void
1615prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1644prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1616{ 1645{
1617 SV *prev_sv, *next_sv;
1618
1619 for (;;) 1646 for (;;)
1620 { 1647 {
1621 next_sv = coro_deq (aTHX); 1648 SV *next_sv = coro_deq (aTHX);
1622 1649
1623 /* nothing to schedule: call the idle handler */
1624 if (expect_false (!next_sv)) 1650 if (expect_true (next_sv))
1625 { 1651 {
1652 struct coro *next = SvSTATE_hv (next_sv);
1653
1654 /* cannot transfer to destroyed coros, skip and look for next */
1655 if (expect_false (next->flags & CF_DESTROYED))
1656 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1657 else
1658 {
1659 next->flags &= ~CF_READY;
1660 --coro_nready;
1661
1662 return prepare_schedule_to (aTHX_ ta, next);
1663 }
1664 }
1665 else
1666 {
1667 /* nothing to schedule: call the idle handler */
1626 dSP; 1668 dSP;
1627 1669
1628 ENTER; 1670 ENTER;
1629 SAVETMPS; 1671 SAVETMPS;
1630 1672
1632 PUTBACK; 1674 PUTBACK;
1633 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1634 1676
1635 FREETMPS; 1677 FREETMPS;
1636 LEAVE; 1678 LEAVE;
1637 continue;
1638 } 1679 }
1639
1640 ta->next = SvSTATE_hv (next_sv);
1641
1642 /* cannot transfer to destroyed coros, skip and look for next */
1643 if (expect_false (ta->next->flags & CF_DESTROYED))
1644 {
1645 SvREFCNT_dec (next_sv);
1646 /* coro_nready has already been taken care of by destroy */
1647 continue;
1648 }
1649
1650 --coro_nready;
1651 break;
1652 } 1680 }
1653
1654 /* free this only after the transfer */
1655 prev_sv = SvRV (coro_current);
1656 ta->prev = SvSTATE_hv (prev_sv);
1657 TRANSFER_CHECK (*ta);
1658 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1659 ta->next->flags &= ~CF_READY;
1660 SvRV_set (coro_current, next_sv);
1661
1662 free_coro_mortal (aTHX);
1663 coro_mortal = prev_sv;
1664} 1681}
1665 1682
1666INLINE void 1683INLINE void
1667prepare_cede (pTHX_ struct coro_transfer_args *ta) 1684prepare_cede (pTHX_ struct coro_transfer_args *ta)
1668{ 1685{
1689{ 1706{
1690 struct coro_transfer_args ta; 1707 struct coro_transfer_args ta;
1691 1708
1692 prepare_schedule (aTHX_ &ta); 1709 prepare_schedule (aTHX_ &ta);
1693 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);
1694} 1721}
1695 1722
1696static int 1723static int
1697api_cede (pTHX) 1724api_cede (pTHX)
1698{ 1725{
1745 if (coro->flags & CF_RUNNING) 1772 if (coro->flags & CF_RUNNING)
1746 PL_runops = RUNOPS_DEFAULT; 1773 PL_runops = RUNOPS_DEFAULT;
1747 else 1774 else
1748 coro->slot->runops = RUNOPS_DEFAULT; 1775 coro->slot->runops = RUNOPS_DEFAULT;
1749 } 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;
1750} 1830}
1751 1831
1752/*****************************************************************************/ 1832/*****************************************************************************/
1753/* async pool handler */ 1833/* async pool handler */
1754 1834
1987static void 2067static void
1988slf_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)
1989{ 2069{
1990 frame->prepare = prepare_schedule; 2070 frame->prepare = prepare_schedule;
1991 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);
1992} 2100}
1993 2101
1994static void 2102static void
1995slf_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)
1996{ 2104{
2950 3058
2951BOOT: 3059BOOT:
2952{ 3060{
2953 int i; 3061 int i;
2954 3062
2955 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2956 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3063 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2957 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3064 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2958 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3065 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2959 cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); 3066 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2960 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 3067 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2961 SvREADONLY_on (coro_current); 3068 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3069 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3070 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
2962 3071
2963 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3072 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
2964 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3073 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2965 cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); 3074 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
2966 cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); 3075 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2967 3076
2968 coro_stash = gv_stashpv ("Coro", TRUE); 3077 coro_stash = gv_stashpv ("Coro", TRUE);
2969 3078
2970 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3079 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2971 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3080 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2979 3088
2980 { 3089 {
2981 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3090 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2982 3091
2983 coroapi.schedule = api_schedule; 3092 coroapi.schedule = api_schedule;
3093 coroapi.schedule_to = api_schedule_to;
2984 coroapi.cede = api_cede; 3094 coroapi.cede = api_cede;
2985 coroapi.cede_notself = api_cede_notself; 3095 coroapi.cede_notself = api_cede_notself;
2986 coroapi.ready = api_ready; 3096 coroapi.ready = api_ready;
2987 coroapi.is_ready = api_is_ready; 3097 coroapi.is_ready = api_is_ready;
2988 coroapi.nready = coro_nready; 3098 coroapi.nready = coro_nready;
2993 SvREADONLY_on (sv); 3103 SvREADONLY_on (sv);
2994 } 3104 }
2995} 3105}
2996 3106
2997void 3107void
3108terminate (...)
3109 CODE:
3110 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3111
3112void
2998schedule (...) 3113schedule (...)
2999 CODE: 3114 CODE:
3000 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3115 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3116
3117void
3118schedule_to (...)
3119 CODE:
3120 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3121
3122void
3123cede_to (...)
3124 CODE:
3125 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
3001 3126
3002void 3127void
3003cede (...) 3128cede (...)
3004 CODE: 3129 CODE:
3005 CORO_EXECUTE_SLF_XS (slf_init_cede); 3130 CORO_EXECUTE_SLF_XS (slf_init_cede);
3006 3131
3007void 3132void
3008cede_notself (...) 3133cede_notself (...)
3009 CODE: 3134 CODE:
3010 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3135 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3136
3137void
3138_cancel (Coro::State self)
3139 CODE:
3140 coro_state_destroy (aTHX_ self);
3141 coro_call_on_destroy (aTHX_ self);
3011 3142
3012void 3143void
3013_set_current (SV *current) 3144_set_current (SV *current)
3014 PROTOTYPE: $ 3145 PROTOTYPE: $
3015 CODE: 3146 CODE:
3086 PUSHMARK (SP); 3217 PUSHMARK (SP);
3087 EXTEND (SP, 2); 3218 EXTEND (SP, 2);
3088 PUSHs (sv_Coro); 3219 PUSHs (sv_Coro);
3089 PUSHs ((SV *)cv_pool_handler); 3220 PUSHs ((SV *)cv_pool_handler);
3090 PUTBACK; 3221 PUTBACK;
3091 call_sv ((SV *)cv_coro_new, G_SCALAR); 3222 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3092 SPAGAIN; 3223 SPAGAIN;
3093 3224
3094 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3225 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
3095 } 3226 }
3096 3227

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines