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.395 by root, Fri May 6 07:22:14 2011 UTC vs.
Revision 1.398 by root, Sat May 7 14:11:10 2011 UTC

126static void (*u2time)(pTHX_ UV ret[2]); 126static void (*u2time)(pTHX_ UV ret[2]);
127 127
128/* we hijack an hopefully unused CV flag for our purposes */ 128/* we hijack an hopefully unused CV flag for our purposes */
129#define CVf_SLF 0x4000 129#define CVf_SLF 0x4000
130static OP *pp_slf (pTHX); 130static OP *pp_slf (pTHX);
131static void slf_destroy (pTHX_ struct coro *coro);
131 132
132static U32 cctx_gen; 133static U32 cctx_gen;
133static size_t cctx_stacksize = CORO_STACKSIZE; 134static size_t cctx_stacksize = CORO_STACKSIZE;
134static struct CoroAPI coroapi; 135static struct CoroAPI coroapi;
135static AV *main_mainstack; /* used to differentiate between $main and others */ 136static AV *main_mainstack; /* used to differentiate between $main and others */
222#define VAR(name,type) type name; 223#define VAR(name,type) type name;
223# include "state.h" 224# include "state.h"
224#undef VAR 225#undef VAR
225} perl_slots; 226} perl_slots;
226 227
228// how many context stack entries do we need for perl_slots
227#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 229#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
228 230
229/* this is a structure representing a perl-level coroutine */ 231/* this is a structure representing a perl-level coroutine */
230struct coro { 232struct coro {
231 /* the C coroutine allocated to this perl coroutine, if any */ 233 /* the C coroutine allocated to this perl coroutine, if any */
242 CV *startcv; /* the CV to execute */ 244 CV *startcv; /* the CV to execute */
243 AV *args; /* data associated with this coroutine (initial args) */ 245 AV *args; /* data associated with this coroutine (initial args) */
244 int refcnt; /* coroutines are refcounted, yes */ 246 int refcnt; /* coroutines are refcounted, yes */
245 int flags; /* CF_ flags */ 247 int flags; /* CF_ flags */
246 HV *hv; /* the perl hash associated with this coro, if any */ 248 HV *hv; /* the perl hash associated with this coro, if any */
247 void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */
248 249
249 /* statistics */ 250 /* statistics */
250 int usecount; /* number of transfers to this coro */ 251 int usecount; /* number of transfers to this coro */
251 252
252 /* coro process data */ 253 /* coro process data */
292 293
293/* for Coro.pm */ 294/* for Coro.pm */
294static SV *coro_current; 295static SV *coro_current;
295static SV *coro_readyhook; 296static SV *coro_readyhook;
296static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 297static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
297static CV *cv_coro_run, *cv_coro_terminate; 298static CV *cv_coro_run;
298static struct coro *coro_first; 299static struct coro *coro_first;
299#define coro_nready coroapi.nready 300#define coro_nready coroapi.nready
300 301
301/** Coro::Select ************************************************************/ 302/** Coro::Select ************************************************************/
302 303
798 799
799 PUTBACK; 800 PUTBACK;
800 } 801 }
801 802
802 /* allocate some space on the context stack for our purposes */ 803 /* allocate some space on the context stack for our purposes */
803 /* we manually unroll here, as usually 2 slots is enough */ 804 if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max))
804 if (SLOT_COUNT >= 1) CXINC;
805 if (SLOT_COUNT >= 2) CXINC;
806 if (SLOT_COUNT >= 3) CXINC;
807 { 805 {
808 unsigned int i; 806 unsigned int i;
807
809 for (i = 3; i < SLOT_COUNT; ++i) 808 for (i = 0; i < SLOT_COUNT; ++i)
810 CXINC; 809 CXINC;
811 } 810
812 cxstack_ix -= SLOT_COUNT; /* undo allocation */ 811 cxstack_ix -= SLOT_COUNT; /* undo allocation */
812 }
813 813
814 c->mainstack = PL_mainstack; 814 c->mainstack = PL_mainstack;
815 815
816 { 816 {
817 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 817 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
838# define coro_init_stacks(thx) init_stacks () 838# define coro_init_stacks(thx) init_stacks ()
839#else 839#else
840static void 840static void
841coro_init_stacks (pTHX) 841coro_init_stacks (pTHX)
842{ 842{
843 PL_curstackinfo = new_stackinfo(32, 8); 843 PL_curstackinfo = new_stackinfo(32, 4 + SLOT_COUNT); /* 3 is minimum due to perl rounding down in scope.c:GROW() */
844 PL_curstackinfo->si_type = PERLSI_MAIN; 844 PL_curstackinfo->si_type = PERLSI_MAIN;
845 PL_curstack = PL_curstackinfo->si_stack; 845 PL_curstack = PL_curstackinfo->si_stack;
846 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 846 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
847 847
848 PL_stack_base = AvARRAY(PL_curstack); 848 PL_stack_base = AvARRAY(PL_curstack);
1154destroy_perl (pTHX_ struct coro *coro) 1154destroy_perl (pTHX_ struct coro *coro)
1155{ 1155{
1156 SV *svf [9]; 1156 SV *svf [9];
1157 1157
1158 { 1158 {
1159 SV *old_current = SvRV (coro_current);
1159 struct coro *current = SvSTATE_current; 1160 struct coro *current = SvSTATE (old_current);
1160 1161
1161 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); 1162 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1162 1163
1163 save_perl (aTHX_ current); 1164 save_perl (aTHX_ current);
1165
1166 /* this will cause transfer_check to croak on block*/
1164 /*SvRV_set (coro_current, (SV *)coro->hv);*/ 1167 SvRV_set (coro_current, (SV *)coro->hv);
1168
1165 load_perl (aTHX_ coro); 1169 load_perl (aTHX_ coro);
1166 1170
1167 coro_unwind_stacks (aTHX); 1171 coro_unwind_stacks (aTHX);
1168 coro_destruct_stacks (aTHX); 1172 coro_destruct_stacks (aTHX);
1169 1173
1180 svf [6] = (SV *)GvHV (PL_hintgv); 1184 svf [6] = (SV *)GvHV (PL_hintgv);
1181 svf [7] = PL_diehook; 1185 svf [7] = PL_diehook;
1182 svf [8] = PL_warnhook; 1186 svf [8] = PL_warnhook;
1183 assert (9 == sizeof (svf) / sizeof (*svf)); 1187 assert (9 == sizeof (svf) / sizeof (*svf));
1184 1188
1185 /*SvRV_set (coro_current, (SV *)current->hv);*/ 1189 SvRV_set (coro_current, old_current);
1190
1186 load_perl (aTHX_ current); 1191 load_perl (aTHX_ current);
1187 } 1192 }
1188 1193
1189 { 1194 {
1190 unsigned int i; 1195 unsigned int i;
1674coro_state_destroy (pTHX_ struct coro *coro) 1679coro_state_destroy (pTHX_ struct coro *coro)
1675{ 1680{
1676 if (coro->flags & CF_DESTROYED) 1681 if (coro->flags & CF_DESTROYED)
1677 return; 1682 return;
1678 1683
1679 /* this callback is reserved for slf functions needing to do cleanup */
1680 if (coro->on_destroy && !PL_dirty)
1681 coro->on_destroy (aTHX_ coro); 1684 slf_destroy (aTHX_ coro);
1682
1683 /*
1684 * The on_destroy above most likely is from an SLF call.
1685 * Since by definition the SLF call will not finish when we destroy
1686 * the coro, we will have to force-finish it here, otherwise
1687 * cleanup functions cannot call SLF functions.
1688 */
1689 coro->slf_frame.prepare = 0;
1690 1685
1691 coro->flags |= CF_DESTROYED; 1686 coro->flags |= CF_DESTROYED;
1692 1687
1693 if (coro->flags & CF_READY) 1688 if (coro->flags & CF_READY)
1694 { 1689 {
2048 } 2043 }
2049 } 2044 }
2050} 2045}
2051 2046
2052static void 2047static void
2053coro_set_status (HV *coro_hv, SV **arg, int items) 2048coro_set_status (pTHX_ HV *coro_hv, SV **arg, int items)
2054{ 2049{
2055 AV *av = newAV (); 2050 AV *av = newAV ();
2056 2051
2057 /* items are actually not so common, so optimise for this case */ 2052 /* items are actually not so common, so optimise for this case */
2058 if (items) 2053 if (items)
2085static void 2080static void
2086slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2081slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2087{ 2082{
2088 HV *coro_hv = (HV *)SvRV (coro_current); 2083 HV *coro_hv = (HV *)SvRV (coro_current);
2089 2084
2090 coro_set_status (coro_hv, arg, items); 2085 coro_set_status (aTHX_ coro_hv, arg, items);
2091 slf_init_terminate_cancel_common (frame, coro_hv); 2086 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2092} 2087}
2093 2088
2094static void 2089static void
2095slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2090slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2096{ 2091{
2101 croak ("Coro::cancel called without coro object,"); 2096 croak ("Coro::cancel called without coro object,");
2102 2097
2103 coro = SvSTATE (arg [0]); 2098 coro = SvSTATE (arg [0]);
2104 coro_hv = coro->hv; 2099 coro_hv = coro->hv;
2105 2100
2106 coro_set_status (coro_hv, arg + 1, items - 1); 2101 coro_set_status (aTHX_ coro_hv, arg + 1, items - 1);
2107 2102
2108 if (expect_false (coro->flags & CF_NOCANCEL)) 2103 if (expect_false (coro->flags & CF_NOCANCEL))
2109 { 2104 {
2110 /* coro currently busy cancelling something, so just notify it */ 2105 /* coro currently busy cancelling something, so just notify it */
2111 coro->slf_frame.data = (void *)coro; 2106 coro->slf_frame.data = (void *)coro;
2114 frame->check = slf_check_nop; 2109 frame->check = slf_check_nop;
2115 } 2110 }
2116 else if (coro_hv == (HV *)SvRV (coro_current)) 2111 else if (coro_hv == (HV *)SvRV (coro_current))
2117 { 2112 {
2118 /* cancelling the current coro is allowed, and equals terminate */ 2113 /* cancelling the current coro is allowed, and equals terminate */
2119 slf_init_terminate_cancel_common (frame, coro_hv); 2114 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2120 } 2115 }
2121 else 2116 else
2122 { 2117 {
2123 struct coro *self = SvSTATE_current; 2118 struct coro *self = SvSTATE_current;
2124 2119
2137 self->flags &= ~CF_NOCANCEL; 2132 self->flags &= ~CF_NOCANCEL;
2138 2133
2139 if (slf_frame.data) 2134 if (slf_frame.data)
2140 { 2135 {
2141 /* while we were busy we have been cancelled, so terminate */ 2136 /* while we were busy we have been cancelled, so terminate */
2142 slf_init_terminate_cancel_common (frame, self->hv); 2137 slf_init_terminate_cancel_common (aTHX_ frame, self->hv);
2143 } 2138 }
2144 else 2139 else
2145 { 2140 {
2146 frame->prepare = prepare_nop; 2141 frame->prepare = prepare_nop;
2147 frame->check = slf_check_nop; 2142 frame->check = slf_check_nop;
2148 } 2143 }
2149 } 2144 }
2150} 2145}
2151 2146
2147static int
2148slf_check_safe_cancel (pTHX_ struct CoroSLF *frame)
2149{
2150 frame->prepare = 0;
2151 coro_unwind_stacks (aTHX);
2152
2153 slf_init_terminate_cancel_common (aTHX_ frame, (HV *)SvRV (coro_current));
2154
2155 return 1;
2156}
2157
2158static int
2159safe_cancel (pTHX_ struct coro *coro, SV **arg, int items)
2160{
2161 if (coro->cctx)
2162 croak ("coro inside C callback, unable to cancel at this time, caught");
2163
2164 if (coro->flags & CF_NEW)
2165 {
2166 coro_set_status (aTHX_ coro->hv, arg, items);
2167 coro_state_destroy (aTHX_ coro);
2168 coro_call_on_destroy (aTHX_ coro);
2169 }
2170 else
2171 {
2172 if (!coro->slf_frame.prepare)
2173 croak ("coro outside an SLF function, unable to cancel at this time, caught");
2174
2175 slf_destroy (aTHX_ coro);
2176
2177 coro_set_status (aTHX_ coro->hv, arg, items);
2178 coro->slf_frame.prepare = prepare_nop;
2179 coro->slf_frame.check = slf_check_safe_cancel;
2180
2181 api_ready (aTHX_ coro->hv);
2182 }
2183
2184 return 1;
2185}
2186
2152/*****************************************************************************/ 2187/*****************************************************************************/
2153/* async pool handler */ 2188/* async pool handler */
2154 2189
2155static int 2190static int
2156slf_check_pool_handler (pTHX_ struct CoroSLF *frame) 2191slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
2194 coro->saved_deffh = 0; 2229 coro->saved_deffh = 0;
2195 2230
2196 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) 2231 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2197 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2232 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2198 { 2233 {
2199 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); 2234 slf_init_terminate_cancel_common (aTHX_ frame, hv);
2200 coro->invoke_av = newAV (); 2235 return;
2201
2202 frame->prepare = prepare_nop;
2203 } 2236 }
2204 else 2237 else
2205 { 2238 {
2206 av_clear (GvAV (PL_defgv)); 2239 av_clear (GvAV (PL_defgv));
2207 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); 2240 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
2430static void 2463static void
2431slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2464slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2432{ 2465{
2433 frame->prepare = prepare_cede_notself; 2466 frame->prepare = prepare_cede_notself;
2434 frame->check = slf_check_nop; 2467 frame->check = slf_check_nop;
2468}
2469
2470/* "undo"/cancel a running slf call - used when cancelling a coro, mainly */
2471static void
2472slf_destroy (pTHX_ struct coro *coro)
2473{
2474 /* this callback is reserved for slf functions needing to do cleanup */
2475 if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty)
2476 coro->slf_frame.destroy (aTHX_ coro);
2477
2478 /*
2479 * The on_destroy above most likely is from an SLF call.
2480 * Since by definition the SLF call will not finish when we destroy
2481 * the coro, we will have to force-finish it here, otherwise
2482 * cleanup functions cannot call SLF functions.
2483 */
2484 coro->slf_frame.prepare = 0;
2435} 2485}
2436 2486
2437/* 2487/*
2438 * these not obviously related functions are all rolled into one 2488 * these not obviously related functions are all rolled into one
2439 * function to increase chances that they all will call transfer with the same 2489 * function to increase chances that they all will call transfer with the same
2765 SvREFCNT_dec (cb); 2815 SvREFCNT_dec (cb);
2766 } 2816 }
2767} 2817}
2768 2818
2769static void 2819static void
2770coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2820coro_semaphore_destroy (pTHX_ struct coro *coro)
2771{ 2821{
2772 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2822 /* call $sem->adjust (0) to possibly wake up some other waiters */
2773 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2823 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2774} 2824}
2775 2825
2782 /* if we are about to throw, don't actually acquire the lock, just throw */ 2832 /* if we are about to throw, don't actually acquire the lock, just throw */
2783 if (CORO_THROW) 2833 if (CORO_THROW)
2784 return 0; 2834 return 0;
2785 else if (SvIVX (count_sv) > 0) 2835 else if (SvIVX (count_sv) > 0)
2786 { 2836 {
2787 SvSTATE_current->on_destroy = 0; 2837 frame->destroy = 0;
2788 2838
2789 if (acquire) 2839 if (acquire)
2790 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2840 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2791 else 2841 else
2792 coro_semaphore_adjust (aTHX_ av, 0); 2842 coro_semaphore_adjust (aTHX_ av, 0);
2835 { 2885 {
2836 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2886 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2837 2887
2838 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2888 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2839 frame->prepare = prepare_schedule; 2889 frame->prepare = prepare_schedule;
2840
2841 /* to avoid race conditions when a woken-up coro gets terminated */ 2890 /* to avoid race conditions when a woken-up coro gets terminated */
2842 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2891 /* we arrange for a temporary on_destroy that calls adjust (0) */
2843 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2892 frame->destroy = coro_semaphore_destroy;
2844 } 2893 }
2845} 2894}
2846 2895
2847static void 2896static void
2848slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2897slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
3543BOOT: 3592BOOT:
3544{ 3593{
3545 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3594 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3546 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3595 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3547 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); 3596 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3548 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3549 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3597 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3550 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3598 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3551 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3599 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3552 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3600 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3553 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3601 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3607 3655
3608void 3656void
3609cancel (...) 3657cancel (...)
3610 CODE: 3658 CODE:
3611 CORO_EXECUTE_SLF_XS (slf_init_cancel); 3659 CORO_EXECUTE_SLF_XS (slf_init_cancel);
3660
3661int
3662safe_cancel (Coro::State self, ...)
3663 C_ARGS: aTHX_ self, &ST (1), items - 1
3612 3664
3613void 3665void
3614schedule (...) 3666schedule (...)
3615 CODE: 3667 CODE:
3616 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3668 CORO_EXECUTE_SLF_XS (slf_init_schedule);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines