… | |
… | |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
171 | static JMPENV *main_top_env; |
171 | static JMPENV *main_top_env; |
172 | static HV *coro_state_stash, *coro_stash; |
172 | static HV *coro_state_stash, *coro_stash; |
173 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
173 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
174 | |
174 | |
|
|
175 | static AV *av_destroy; /* destruction queue */ |
|
|
176 | static SV *sv_manager; /* the manager coro */ |
|
|
177 | |
175 | static GV *irsgv; /* $/ */ |
178 | static GV *irsgv; /* $/ */ |
176 | static GV *stdoutgv; /* *STDOUT */ |
179 | static GV *stdoutgv; /* *STDOUT */ |
177 | static SV *rv_diehook; |
180 | static SV *rv_diehook; |
178 | static SV *rv_warnhook; |
181 | static SV *rv_warnhook; |
179 | static HV *hv_sig; /* %SIG */ |
182 | static HV *hv_sig; /* %SIG */ |
180 | |
183 | |
181 | /* async_pool helper stuff */ |
184 | /* async_pool helper stuff */ |
182 | static SV *sv_pool_rss; |
185 | static SV *sv_pool_rss; |
183 | static SV *sv_pool_size; |
186 | static SV *sv_pool_size; |
184 | static SV *sv_async_pool_idle; |
187 | static SV *sv_async_pool_idle; /* description string */ |
185 | static AV *av_async_pool; |
188 | static AV *av_async_pool; /* idle pool */ |
186 | static SV *sv_Coro; |
189 | static SV *sv_Coro; /* class string */ |
187 | static CV *cv_pool_handler; |
190 | static CV *cv_pool_handler; |
188 | static CV *cv_coro_state_new; |
191 | static CV *cv_coro_state_new; |
189 | |
192 | |
190 | /* Coro::AnyEvent */ |
193 | /* Coro::AnyEvent */ |
191 | static SV *sv_activity; |
194 | static SV *sv_activity; |
… | |
… | |
832 | |
835 | |
833 | static int |
836 | static int |
834 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
837 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
835 | { |
838 | { |
836 | return 0; |
839 | return 0; |
|
|
840 | } |
|
|
841 | |
|
|
842 | static int |
|
|
843 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
|
|
844 | { |
|
|
845 | return 1; |
837 | } |
846 | } |
838 | |
847 | |
839 | static UNOP coro_setup_op; |
848 | static UNOP coro_setup_op; |
840 | |
849 | |
841 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
850 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
… | |
… | |
1612 | api_is_ready (pTHX_ SV *coro_sv) |
1621 | api_is_ready (pTHX_ SV *coro_sv) |
1613 | { |
1622 | { |
1614 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1623 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1615 | } |
1624 | } |
1616 | |
1625 | |
|
|
1626 | /* expects to own a reference to next->hv */ |
1617 | INLINE void |
1627 | INLINE void |
|
|
1628 | prepare_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 | |
|
|
1643 | static void |
1618 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1644 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1619 | { |
1645 | { |
1620 | SV *prev_sv, *next_sv; |
|
|
1621 | |
|
|
1622 | for (;;) |
1646 | for (;;) |
1623 | { |
1647 | { |
1624 | next_sv = coro_deq (aTHX); |
1648 | SV *next_sv = coro_deq (aTHX); |
1625 | |
1649 | |
1626 | /* nothing to schedule: call the idle handler */ |
|
|
1627 | if (expect_false (!next_sv)) |
1650 | if (expect_true (next_sv)) |
1628 | { |
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 */ |
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 | |
1669 | INLINE void |
1683 | INLINE void |
1670 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1684 | prepare_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 | |
|
|
1713 | static void |
|
|
1714 | api_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 | |
1699 | static int |
1723 | static int |
1700 | api_cede (pTHX) |
1724 | api_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 | |
|
|
1779 | static void |
|
|
1780 | coro_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 | |
|
|
1812 | static void |
|
|
1813 | slf_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 | |
… | |
… | |
1990 | static void |
2067 | static void |
1991 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2068 | slf_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 | |
|
|
2074 | static void |
|
|
2075 | slf_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 | |
|
|
2083 | static void |
|
|
2084 | slf_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 | |
|
|
2094 | static void |
|
|
2095 | slf_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 | |
1997 | static void |
2102 | static void |
1998 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2103 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1999 | { |
2104 | { |
… | |
… | |
2953 | |
3058 | |
2954 | BOOT: |
3059 | BOOT: |
2955 | { |
3060 | { |
2956 | int i; |
3061 | int i; |
2957 | |
3062 | |
2958 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2959 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3063 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2960 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3064 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2961 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3065 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
2962 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3066 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2963 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3067 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 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); |
2964 | |
3071 | |
2965 | 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); |
2966 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3073 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
2967 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3074 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
2968 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3075 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
… | |
… | |
2981 | |
3088 | |
2982 | { |
3089 | { |
2983 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3090 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2984 | |
3091 | |
2985 | coroapi.schedule = api_schedule; |
3092 | coroapi.schedule = api_schedule; |
|
|
3093 | coroapi.schedule_to = api_schedule_to; |
2986 | coroapi.cede = api_cede; |
3094 | coroapi.cede = api_cede; |
2987 | coroapi.cede_notself = api_cede_notself; |
3095 | coroapi.cede_notself = api_cede_notself; |
2988 | coroapi.ready = api_ready; |
3096 | coroapi.ready = api_ready; |
2989 | coroapi.is_ready = api_is_ready; |
3097 | coroapi.is_ready = api_is_ready; |
2990 | coroapi.nready = coro_nready; |
3098 | coroapi.nready = coro_nready; |
… | |
… | |
2995 | SvREADONLY_on (sv); |
3103 | SvREADONLY_on (sv); |
2996 | } |
3104 | } |
2997 | } |
3105 | } |
2998 | |
3106 | |
2999 | void |
3107 | void |
|
|
3108 | terminate (...) |
|
|
3109 | CODE: |
|
|
3110 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3111 | |
|
|
3112 | void |
3000 | schedule (...) |
3113 | schedule (...) |
3001 | CODE: |
3114 | CODE: |
3002 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3115 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3116 | |
|
|
3117 | void |
|
|
3118 | schedule_to (...) |
|
|
3119 | CODE: |
|
|
3120 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3121 | |
|
|
3122 | void |
|
|
3123 | cede_to (...) |
|
|
3124 | CODE: |
|
|
3125 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
3003 | |
3126 | |
3004 | void |
3127 | void |
3005 | cede (...) |
3128 | cede (...) |
3006 | CODE: |
3129 | CODE: |
3007 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3130 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3008 | |
3131 | |
3009 | void |
3132 | void |
3010 | cede_notself (...) |
3133 | cede_notself (...) |
3011 | CODE: |
3134 | CODE: |
3012 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3135 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
|
|
3136 | |
|
|
3137 | void |
|
|
3138 | _cancel (Coro::State self) |
|
|
3139 | CODE: |
|
|
3140 | coro_state_destroy (aTHX_ self); |
|
|
3141 | coro_call_on_destroy (aTHX_ self); |
3013 | |
3142 | |
3014 | void |
3143 | void |
3015 | _set_current (SV *current) |
3144 | _set_current (SV *current) |
3016 | PROTOTYPE: $ |
3145 | PROTOTYPE: $ |
3017 | CODE: |
3146 | CODE: |