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.361 by root, Wed Jul 1 07:26:40 2009 UTC vs.
Revision 1.366 by root, Tue Jul 21 03:39:58 2009 UTC

6#include "EXTERN.h" 6#include "EXTERN.h"
7#include "perl.h" 7#include "perl.h"
8#include "XSUB.h" 8#include "XSUB.h"
9#include "perliol.h" 9#include "perliol.h"
10 10
11#include "patchlevel.h" 11#include "schmorp.h"
12 12
13#include <stdio.h> 13#include <stdio.h>
14#include <errno.h> 14#include <errno.h>
15#include <assert.h> 15#include <assert.h>
16 16
51#endif 51#endif
52 52
53/* the maximum number of idle cctx that will be pooled */ 53/* the maximum number of idle cctx that will be pooled */
54static int cctx_max_idle = 4; 54static int cctx_max_idle = 4;
55 55
56#define PERL_VERSION_ATLEAST(a,b,c) \
57 (PERL_REVISION > (a) \
58 || (PERL_REVISION == (a) \
59 && (PERL_VERSION > (b) \
60 || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c)))))
61
62#if !PERL_VERSION_ATLEAST (5,6,0)
63# ifndef PL_ppaddr
64# define PL_ppaddr ppaddr
65# endif
66# ifndef call_sv
67# define call_sv perl_call_sv
68# endif
69# ifndef get_sv
70# define get_sv perl_get_sv
71# endif
72# ifndef get_cv
73# define get_cv perl_get_cv
74# endif
75# ifndef IS_PADGV
76# define IS_PADGV(v) 0
77# endif
78# ifndef IS_PADCONST
79# define IS_PADCONST(v) 0
80# endif
81#endif
82
83/* 5.11 */
84#ifndef CxHASARGS
85# define CxHASARGS(cx) (cx)->blk_sub.hasargs
86#endif
87
88/* 5.10.0 */
89#ifndef SvREFCNT_inc_NN
90# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
91#endif
92
93/* 5.8.8 */
94#ifndef GV_NOTQUAL
95# define GV_NOTQUAL 0
96#endif
97#ifndef newSV
98# define newSV(l) NEWSV(0,l)
99#endif
100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv
102#endif
103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
109
110/* 5.8.7 */
111#ifndef SvRV_set
112# define SvRV_set(s,v) SvRV(s) = (v)
113#endif
114
115#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 56#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
116# undef CORO_STACKGUARD 57# undef CORO_STACKGUARD
117#endif 58#endif
118 59
119#ifndef CORO_STACKGUARD 60#ifndef CORO_STACKGUARD
342static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 283static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
343static CV *cv_coro_run, *cv_coro_terminate; 284static CV *cv_coro_run, *cv_coro_terminate;
344static struct coro *coro_first; 285static struct coro *coro_first;
345#define coro_nready coroapi.nready 286#define coro_nready coroapi.nready
346 287
288/** Coro::Select ************************************************************/
289
290static OP *(*coro_old_pp_sselect) (pTHX);
291static SV *coro_select_select;
292
293/* horrible hack, but if it works... */
294static OP *
295coro_pp_sselect (aTHX)
296{
297 dSP;
298 PUSHMARK (SP - 4); /* fake argument list */
299 XPUSHs (coro_select_select);
300 PUTBACK;
301
302 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
303 PL_op->op_flags |= OPf_STACKED;
304 PL_op->op_private = 0;
305 return PL_ppaddr [OP_ENTERSUB](aTHX);
306}
307
347/** lowlevel stuff **********************************************************/ 308/** lowlevel stuff **********************************************************/
348 309
349static SV * 310static SV *
350coro_get_sv (pTHX_ const char *name, int create) 311coro_get_sv (pTHX_ const char *name, int create)
351{ 312{
372#if PERL_VERSION_ATLEAST (5,10,0) 333#if PERL_VERSION_ATLEAST (5,10,0)
373 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 334 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
374 get_hv (name, create); 335 get_hv (name, create);
375#endif 336#endif
376 return get_hv (name, create); 337 return get_hv (name, create);
377}
378
379/* may croak */
380INLINE CV *
381coro_sv_2cv (pTHX_ SV *sv)
382{
383 HV *st;
384 GV *gvp;
385 CV *cv = sv_2cv (sv, &st, &gvp, 0);
386
387 if (!cv)
388 croak ("code reference expected");
389
390 return cv;
391} 338}
392 339
393INLINE void 340INLINE void
394coro_times_update () 341coro_times_update ()
395{ 342{
405 time_real [0] = ts.tv_sec; time_real [1] = ts.tv_nsec; 352 time_real [0] = ts.tv_sec; time_real [1] = ts.tv_nsec;
406#else 353#else
407 dTHX; 354 dTHX;
408 UV tv[2]; 355 UV tv[2];
409 356
410 u2time (aTHX_ &tv); 357 u2time (aTHX_ tv);
411 time_real [0] = tv [0]; 358 time_real [0] = tv [0];
412 time_real [1] = tv [1] * 1000; 359 time_real [1] = tv [1] * 1000;
413#endif 360#endif
414} 361}
415 362
545 AV *padlist; 492 AV *padlist;
546 AV *av = (AV *)mg->mg_obj; 493 AV *av = (AV *)mg->mg_obj;
547 494
548 /* perl manages to free our internal AV and _then_ call us */ 495 /* perl manages to free our internal AV and _then_ call us */
549 if (IN_DESTRUCT) 496 if (IN_DESTRUCT)
550 return; 497 return 0;
551 498
552 /* casting is fun. */ 499 /* casting is fun. */
553 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 500 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
554 free_padlist (aTHX_ padlist); 501 free_padlist (aTHX_ padlist);
555 502
736 /* we manually unroll here, as usually 2 slots is enough */ 683 /* we manually unroll here, as usually 2 slots is enough */
737 if (SLOT_COUNT >= 1) CXINC; 684 if (SLOT_COUNT >= 1) CXINC;
738 if (SLOT_COUNT >= 2) CXINC; 685 if (SLOT_COUNT >= 2) CXINC;
739 if (SLOT_COUNT >= 3) CXINC; 686 if (SLOT_COUNT >= 3) CXINC;
740 { 687 {
741 int i; 688 unsigned int i;
742 for (i = 3; i < SLOT_COUNT; ++i) 689 for (i = 3; i < SLOT_COUNT; ++i)
743 CXINC; 690 CXINC;
744 } 691 }
745 cxstack_ix -= SLOT_COUNT; /* undo allocation */ 692 cxstack_ix -= SLOT_COUNT; /* undo allocation */
746 693
1112 1059
1113 load_perl (aTHX_ current); 1060 load_perl (aTHX_ current);
1114 } 1061 }
1115 1062
1116 { 1063 {
1117 int i; 1064 unsigned int i;
1118 1065
1119 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) 1066 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1120 SvREFCNT_dec (svf [i]); 1067 SvREFCNT_dec (svf [i]);
1121 1068
1122 SvREFCNT_dec (coro->saved_deffh); 1069 SvREFCNT_dec (coro->saved_deffh);
1687 1634
1688 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1635 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1689 TRANSFER (ta, 1); 1636 TRANSFER (ta, 1);
1690} 1637}
1691 1638
1692/*****************************************************************************/
1693/* gensub: simple closure generation utility */
1694
1695#define GENSUB_ARG CvXSUBANY (cv).any_ptr
1696
1697/* create a closure from XS, returns a code reference */
1698/* the arg can be accessed via GENSUB_ARG from the callback */
1699/* the callback must use dXSARGS/XSRETURN */
1700static SV *
1701gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
1702{
1703 CV *cv = (CV *)newSV (0);
1704
1705 sv_upgrade ((SV *)cv, SVt_PVCV);
1706
1707 CvANON_on (cv);
1708 CvISXSUB_on (cv);
1709 CvXSUB (cv) = xsub;
1710 GENSUB_ARG = arg;
1711
1712 return newRV_noinc ((SV *)cv);
1713}
1714
1715/** Coro ********************************************************************/ 1639/** Coro ********************************************************************/
1716 1640
1717INLINE void 1641INLINE void
1718coro_enq (pTHX_ struct coro *coro) 1642coro_enq (pTHX_ struct coro *coro)
1719{ 1643{
1744 } 1668 }
1745 1669
1746 return 0; 1670 return 0;
1747} 1671}
1748 1672
1673static void
1674invoke_sv_ready_hook_helper (void)
1675{
1676 dTHX;
1677 dSP;
1678
1679 ENTER;
1680 SAVETMPS;
1681
1682 PUSHMARK (SP);
1683 PUTBACK;
1684 call_sv (coro_readyhook, G_VOID | G_DISCARD);
1685
1686 FREETMPS;
1687 LEAVE;
1688}
1689
1749static int 1690static int
1750api_ready (pTHX_ SV *coro_sv) 1691api_ready (pTHX_ SV *coro_sv)
1751{ 1692{
1752 struct coro *coro;
1753 SV *sv_hook;
1754 void (*xs_hook)(void);
1755
1756 coro = SvSTATE (coro_sv); 1693 struct coro *coro = SvSTATE (coro_sv);
1757 1694
1758 if (coro->flags & CF_READY) 1695 if (coro->flags & CF_READY)
1759 return 0; 1696 return 0;
1760 1697
1761 coro->flags |= CF_READY; 1698 coro->flags |= CF_READY;
1762 1699
1763 sv_hook = coro_nready ? 0 : coro_readyhook;
1764 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1765
1766 coro_enq (aTHX_ coro); 1700 coro_enq (aTHX_ coro);
1767 ++coro_nready;
1768 1701
1769 if (sv_hook) 1702 if (!coro_nready++)
1770 { 1703 if (coroapi.readyhook)
1771 dSP; 1704 coroapi.readyhook ();
1772
1773 ENTER;
1774 SAVETMPS;
1775
1776 PUSHMARK (SP);
1777 PUTBACK;
1778 call_sv (sv_hook, G_VOID | G_DISCARD);
1779
1780 FREETMPS;
1781 LEAVE;
1782 }
1783
1784 if (xs_hook)
1785 xs_hook ();
1786 1705
1787 return 1; 1706 return 1;
1788} 1707}
1789 1708
1790static int 1709static int
2098 2017
2099static void 2018static void
2100coro_rouse_callback (pTHX_ CV *cv) 2019coro_rouse_callback (pTHX_ CV *cv)
2101{ 2020{
2102 dXSARGS; 2021 dXSARGS;
2103 SV *data = (SV *)GENSUB_ARG; 2022 SV *data = (SV *)S_GENSUB_ARG;
2104 2023
2105 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2024 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2106 { 2025 {
2107 /* first call, set args */ 2026 /* first call, set args */
2108 SV *coro = SvRV (data); 2027 SV *coro = SvRV (data);
2174 || SvTYPE (SvRV (cb)) != SVt_PVCV 2093 || SvTYPE (SvRV (cb)) != SVt_PVCV
2175 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2094 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2176 croak ("Coro::rouse_wait called with illegal callback argument,"); 2095 croak ("Coro::rouse_wait called with illegal callback argument,");
2177 2096
2178 { 2097 {
2179 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ 2098 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
2180 SV *data = (SV *)GENSUB_ARG; 2099 SV *data = (SV *)S_GENSUB_ARG;
2181 2100
2182 frame->data = (void *)data; 2101 frame->data = (void *)data;
2183 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; 2102 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
2184 frame->check = slf_check_rouse_wait; 2103 frame->check = slf_check_rouse_wait;
2185 } 2104 }
2189coro_new_rouse_cb (pTHX) 2108coro_new_rouse_cb (pTHX)
2190{ 2109{
2191 HV *hv = (HV *)SvRV (coro_current); 2110 HV *hv = (HV *)SvRV (coro_current);
2192 struct coro *coro = SvSTATE_hv (hv); 2111 struct coro *coro = SvSTATE_hv (hv);
2193 SV *data = newRV_inc ((SV *)hv); 2112 SV *data = newRV_inc ((SV *)hv);
2194 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); 2113 SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data);
2195 2114
2196 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); 2115 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
2197 SvREFCNT_dec (data); /* magicext increases the refcount */ 2116 SvREFCNT_dec (data); /* magicext increases the refcount */
2198 2117
2199 SvREFCNT_dec (coro->rouse_cb); 2118 SvREFCNT_dec (coro->rouse_cb);
2722{ 2641{
2723 if (items >= 2) 2642 if (items >= 2)
2724 { 2643 {
2725 /* callback form */ 2644 /* callback form */
2726 AV *av = (AV *)SvRV (arg [0]); 2645 AV *av = (AV *)SvRV (arg [0]);
2727 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2646 SV *cb_cv = s_get_cv_croak (arg [1]);
2728 2647
2729 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2648 av_push (av, SvREFCNT_inc_NN (cb_cv));
2730 2649
2731 if (SvIVX (AvARRAY (av)[0]) > 0) 2650 if (SvIVX (AvARRAY (av)[0]) > 0)
2732 coro_semaphore_adjust (aTHX_ av, 0); 2651 coro_semaphore_adjust (aTHX_ av, 0);
2792{ 2711{
2793 AV *av = (AV *)SvRV (arg [0]); 2712 AV *av = (AV *)SvRV (arg [0]);
2794 2713
2795 if (items >= 2) 2714 if (items >= 2)
2796 { 2715 {
2797 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2716 SV *cb_cv = s_get_cv_croak (arg [1]);
2798 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2717 av_push (av, SvREFCNT_inc_NN (cb_cv));
2799 2718
2800 if (SvIVX (AvARRAY (av)[0])) 2719 if (SvIVX (AvARRAY (av)[0]))
2801 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2720 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2802 2721
2837 2756
2838static void 2757static void
2839coro_aio_callback (pTHX_ CV *cv) 2758coro_aio_callback (pTHX_ CV *cv)
2840{ 2759{
2841 dXSARGS; 2760 dXSARGS;
2842 AV *state = (AV *)GENSUB_ARG; 2761 AV *state = (AV *)S_GENSUB_ARG;
2843 SV *coro = av_pop (state); 2762 SV *coro = av_pop (state);
2844 SV *data_sv = newSV (sizeof (struct io_state)); 2763 SV *data_sv = newSV (sizeof (struct io_state));
2845 2764
2846 av_extend (state, items - 1); 2765 av_extend (state, items - 1);
2847 2766
2961 2880
2962 for (i = 0; i < items; ++i) 2881 for (i = 0; i < items; ++i)
2963 PUSHs (arg [i]); 2882 PUSHs (arg [i]);
2964 2883
2965 /* now push the callback closure */ 2884 /* now push the callback closure */
2966 PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); 2885 PUSHs (sv_2mortal (s_gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state))));
2967 2886
2968 /* now call the AIO function - we assume our request is uncancelable */ 2887 /* now call the AIO function - we assume our request is uncancelable */
2969 PUTBACK; 2888 PUTBACK;
2970 call_sv ((SV *)req, G_VOID | G_DISCARD); 2889 call_sv ((SV *)req, G_VOID | G_DISCARD);
2971 } 2890 }
3060 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); 2979 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3061 2980
3062 nvtime = INT2PTR (double (*)(), SvIV (*svp)); 2981 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3063 2982
3064 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); 2983 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3065 u2time = INT2PTR (double (*)(), SvIV (*svp)); 2984 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3066 } 2985 }
3067 2986
3068 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 2987 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3069} 2988}
3070 2989
3075 CODE: 2994 CODE:
3076{ 2995{
3077 struct coro *coro; 2996 struct coro *coro;
3078 MAGIC *mg; 2997 MAGIC *mg;
3079 HV *hv; 2998 HV *hv;
3080 CV *cb; 2999 SV *cb;
3081 int i; 3000 int i;
3082 3001
3083 if (items > 1) 3002 if (items > 1)
3084 { 3003 {
3085 cb = coro_sv_2cv (aTHX_ ST (1)); 3004 cb = s_get_cv_croak (ST (1));
3086 3005
3087 if (!ix) 3006 if (!ix)
3088 { 3007 {
3089 if (CvISXSUB (cb)) 3008 if (CvISXSUB (cb))
3090 croak ("Coro::State doesn't support XS functions as coroutine start, caught"); 3009 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3112 av_extend (coro->args, items - 1 + ix - 1); 3031 av_extend (coro->args, items - 1 + ix - 1);
3113 3032
3114 if (ix) 3033 if (ix)
3115 { 3034 {
3116 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); 3035 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3117 cb = cv_coro_run; 3036 cb = (SV *)cv_coro_run;
3118 } 3037 }
3119 3038
3120 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); 3039 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3121 3040
3122 for (i = 2; i < items; i++) 3041 for (i = 2; i < items; i++)
3470_set_readyhook (SV *hook) 3389_set_readyhook (SV *hook)
3471 PROTOTYPE: $ 3390 PROTOTYPE: $
3472 CODE: 3391 CODE:
3473 SvREFCNT_dec (coro_readyhook); 3392 SvREFCNT_dec (coro_readyhook);
3474 SvGETMAGIC (hook); 3393 SvGETMAGIC (hook);
3394 if (SvOK (hook))
3395 {
3475 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3396 coro_readyhook = newSVsv (hook);
3397 CORO_READYHOOK = invoke_sv_ready_hook_helper;
3398 }
3399 else
3400 {
3401 coro_readyhook = 0;
3402 CORO_READYHOOK = 0;
3403 }
3476 3404
3477int 3405int
3478prio (Coro::State coro, int newprio = 0) 3406prio (Coro::State coro, int newprio = 0)
3479 PROTOTYPE: $;$ 3407 PROTOTYPE: $;$
3480 ALIAS: 3408 ALIAS:
3596 CODE: 3524 CODE:
3597{ 3525{
3598 struct coro *coro = SvSTATE_current; 3526 struct coro *coro = SvSTATE_current;
3599 AV **avp = ix ? &coro->on_leave : &coro->on_enter; 3527 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3600 3528
3601 block = (SV *)coro_sv_2cv (aTHX_ block); 3529 block = s_get_cv_croak (block);
3602 3530
3603 if (!*avp) 3531 if (!*avp)
3604 *avp = newAV (); 3532 *avp = newAV ();
3605 3533
3606 av_push (*avp, SvREFCNT_inc (block)); 3534 av_push (*avp, SvREFCNT_inc (block));
3806 3734
3807void 3735void
3808_register (char *target, char *proto, SV *req) 3736_register (char *target, char *proto, SV *req)
3809 CODE: 3737 CODE:
3810{ 3738{
3811 CV *req_cv = coro_sv_2cv (aTHX_ req); 3739 SV *req_cv = s_get_cv_croak (req);
3812 /* newXSproto doesn't return the CV on 5.8 */ 3740 /* newXSproto doesn't return the CV on 5.8 */
3813 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3741 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3814 sv_setpv ((SV *)slf_cv, proto); 3742 sv_setpv ((SV *)slf_cv, proto);
3815 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3743 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3816} 3744}
3817 3745
3746MODULE = Coro::State PACKAGE = Coro::Select
3747
3748void
3749patch_pp_sselect ()
3750 CODE:
3751 if (!coro_old_pp_sselect)
3752 {
3753 coro_select_select = (SV *)get_cv ("Coro::Select::select", 0);
3754 coro_old_pp_sselect = PL_ppaddr [OP_SSELECT];
3755 PL_ppaddr [OP_SSELECT] = coro_pp_sselect;
3756 }
3757
3758void
3759unpatch_pp_sselect ()
3760 CODE:
3761 if (coro_old_pp_sselect)
3762 {
3763 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3764 coro_old_pp_sselect = 0;
3765 }
3766
3767

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines