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.362 by root, Wed Jul 1 07:58:45 2009 UTC vs.
Revision 1.367 by root, Mon Aug 3 09:19:57 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{
1328 /* somebody or something will hit me for both perl_run and PL_restartop */ 1275 /* somebody or something will hit me for both perl_run and PL_restartop */
1329 PL_restartop = PL_op; 1276 PL_restartop = PL_op;
1330 perl_run (PL_curinterp); 1277 perl_run (PL_curinterp);
1331 /* 1278 /*
1332 * Unfortunately, there is no way to get at the return values of the 1279 * Unfortunately, there is no way to get at the return values of the
1333 * coro body here, as perl_run destroys these 1280 * coro body here, as perl_run destroys these. Likewise, we cannot catch
1281 * runtime errors here, as this is just a random interpreter, not a thread.
1334 */ 1282 */
1335 1283
1336 /* 1284 /*
1337 * If perl-run returns we assume exit() was being called or the coro 1285 * If perl-run returns we assume exit() was being called or the coro
1338 * fell off the end, which seems to be the only valid (non-bug) 1286 * fell off the end, which seems to be the only valid (non-bug)
1687 1635
1688 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1636 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1689 TRANSFER (ta, 1); 1637 TRANSFER (ta, 1);
1690} 1638}
1691 1639
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 ********************************************************************/ 1640/** Coro ********************************************************************/
1716 1641
1717INLINE void 1642INLINE void
1718coro_enq (pTHX_ struct coro *coro) 1643coro_enq (pTHX_ struct coro *coro)
1719{ 1644{
1744 } 1669 }
1745 1670
1746 return 0; 1671 return 0;
1747} 1672}
1748 1673
1674static void
1675invoke_sv_ready_hook_helper (void)
1676{
1677 dTHX;
1678 dSP;
1679
1680 ENTER;
1681 SAVETMPS;
1682
1683 PUSHMARK (SP);
1684 PUTBACK;
1685 call_sv (coro_readyhook, G_VOID | G_DISCARD);
1686
1687 FREETMPS;
1688 LEAVE;
1689}
1690
1749static int 1691static int
1750api_ready (pTHX_ SV *coro_sv) 1692api_ready (pTHX_ SV *coro_sv)
1751{ 1693{
1752 struct coro *coro;
1753 SV *sv_hook;
1754 void (*xs_hook)(void);
1755
1756 coro = SvSTATE (coro_sv); 1694 struct coro *coro = SvSTATE (coro_sv);
1757 1695
1758 if (coro->flags & CF_READY) 1696 if (coro->flags & CF_READY)
1759 return 0; 1697 return 0;
1760 1698
1761 coro->flags |= CF_READY; 1699 coro->flags |= CF_READY;
1762 1700
1763 sv_hook = coro_nready ? 0 : coro_readyhook;
1764 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1765
1766 coro_enq (aTHX_ coro); 1701 coro_enq (aTHX_ coro);
1767 ++coro_nready;
1768 1702
1769 if (sv_hook) 1703 if (!coro_nready++)
1770 { 1704 if (coroapi.readyhook)
1771 dSP; 1705 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 1706
1787 return 1; 1707 return 1;
1788} 1708}
1789 1709
1790static int 1710static int
2098 2018
2099static void 2019static void
2100coro_rouse_callback (pTHX_ CV *cv) 2020coro_rouse_callback (pTHX_ CV *cv)
2101{ 2021{
2102 dXSARGS; 2022 dXSARGS;
2103 SV *data = (SV *)GENSUB_ARG; 2023 SV *data = (SV *)S_GENSUB_ARG;
2104 2024
2105 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2025 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2106 { 2026 {
2107 /* first call, set args */ 2027 /* first call, set args */
2108 SV *coro = SvRV (data); 2028 SV *coro = SvRV (data);
2174 || SvTYPE (SvRV (cb)) != SVt_PVCV 2094 || SvTYPE (SvRV (cb)) != SVt_PVCV
2175 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2095 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2176 croak ("Coro::rouse_wait called with illegal callback argument,"); 2096 croak ("Coro::rouse_wait called with illegal callback argument,");
2177 2097
2178 { 2098 {
2179 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ 2099 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
2180 SV *data = (SV *)GENSUB_ARG; 2100 SV *data = (SV *)S_GENSUB_ARG;
2181 2101
2182 frame->data = (void *)data; 2102 frame->data = (void *)data;
2183 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; 2103 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
2184 frame->check = slf_check_rouse_wait; 2104 frame->check = slf_check_rouse_wait;
2185 } 2105 }
2189coro_new_rouse_cb (pTHX) 2109coro_new_rouse_cb (pTHX)
2190{ 2110{
2191 HV *hv = (HV *)SvRV (coro_current); 2111 HV *hv = (HV *)SvRV (coro_current);
2192 struct coro *coro = SvSTATE_hv (hv); 2112 struct coro *coro = SvSTATE_hv (hv);
2193 SV *data = newRV_inc ((SV *)hv); 2113 SV *data = newRV_inc ((SV *)hv);
2194 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); 2114 SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data);
2195 2115
2196 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); 2116 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
2197 SvREFCNT_dec (data); /* magicext increases the refcount */ 2117 SvREFCNT_dec (data); /* magicext increases the refcount */
2198 2118
2199 SvREFCNT_dec (coro->rouse_cb); 2119 SvREFCNT_dec (coro->rouse_cb);
2722{ 2642{
2723 if (items >= 2) 2643 if (items >= 2)
2724 { 2644 {
2725 /* callback form */ 2645 /* callback form */
2726 AV *av = (AV *)SvRV (arg [0]); 2646 AV *av = (AV *)SvRV (arg [0]);
2727 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2647 SV *cb_cv = s_get_cv_croak (arg [1]);
2728 2648
2729 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2649 av_push (av, SvREFCNT_inc_NN (cb_cv));
2730 2650
2731 if (SvIVX (AvARRAY (av)[0]) > 0) 2651 if (SvIVX (AvARRAY (av)[0]) > 0)
2732 coro_semaphore_adjust (aTHX_ av, 0); 2652 coro_semaphore_adjust (aTHX_ av, 0);
2792{ 2712{
2793 AV *av = (AV *)SvRV (arg [0]); 2713 AV *av = (AV *)SvRV (arg [0]);
2794 2714
2795 if (items >= 2) 2715 if (items >= 2)
2796 { 2716 {
2797 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2717 SV *cb_cv = s_get_cv_croak (arg [1]);
2798 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2718 av_push (av, SvREFCNT_inc_NN (cb_cv));
2799 2719
2800 if (SvIVX (AvARRAY (av)[0])) 2720 if (SvIVX (AvARRAY (av)[0]))
2801 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2721 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2802 2722
2837 2757
2838static void 2758static void
2839coro_aio_callback (pTHX_ CV *cv) 2759coro_aio_callback (pTHX_ CV *cv)
2840{ 2760{
2841 dXSARGS; 2761 dXSARGS;
2842 AV *state = (AV *)GENSUB_ARG; 2762 AV *state = (AV *)S_GENSUB_ARG;
2843 SV *coro = av_pop (state); 2763 SV *coro = av_pop (state);
2844 SV *data_sv = newSV (sizeof (struct io_state)); 2764 SV *data_sv = newSV (sizeof (struct io_state));
2845 2765
2846 av_extend (state, items - 1); 2766 av_extend (state, items - 1);
2847 2767
2961 2881
2962 for (i = 0; i < items; ++i) 2882 for (i = 0; i < items; ++i)
2963 PUSHs (arg [i]); 2883 PUSHs (arg [i]);
2964 2884
2965 /* now push the callback closure */ 2885 /* now push the callback closure */
2966 PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); 2886 PUSHs (sv_2mortal (s_gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state))));
2967 2887
2968 /* now call the AIO function - we assume our request is uncancelable */ 2888 /* now call the AIO function - we assume our request is uncancelable */
2969 PUTBACK; 2889 PUTBACK;
2970 call_sv ((SV *)req, G_VOID | G_DISCARD); 2890 call_sv ((SV *)req, G_VOID | G_DISCARD);
2971 } 2891 }
3075 CODE: 2995 CODE:
3076{ 2996{
3077 struct coro *coro; 2997 struct coro *coro;
3078 MAGIC *mg; 2998 MAGIC *mg;
3079 HV *hv; 2999 HV *hv;
3080 CV *cb; 3000 SV *cb;
3081 int i; 3001 int i;
3082 3002
3083 if (items > 1) 3003 if (items > 1)
3084 { 3004 {
3085 cb = coro_sv_2cv (aTHX_ ST (1)); 3005 cb = s_get_cv_croak (ST (1));
3086 3006
3087 if (!ix) 3007 if (!ix)
3088 { 3008 {
3089 if (CvISXSUB (cb)) 3009 if (CvISXSUB (cb))
3090 croak ("Coro::State doesn't support XS functions as coroutine start, caught"); 3010 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3112 av_extend (coro->args, items - 1 + ix - 1); 3032 av_extend (coro->args, items - 1 + ix - 1);
3113 3033
3114 if (ix) 3034 if (ix)
3115 { 3035 {
3116 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); 3036 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3117 cb = cv_coro_run; 3037 cb = (SV *)cv_coro_run;
3118 } 3038 }
3119 3039
3120 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); 3040 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3121 3041
3122 for (i = 2; i < items; i++) 3042 for (i = 2; i < items; i++)
3470_set_readyhook (SV *hook) 3390_set_readyhook (SV *hook)
3471 PROTOTYPE: $ 3391 PROTOTYPE: $
3472 CODE: 3392 CODE:
3473 SvREFCNT_dec (coro_readyhook); 3393 SvREFCNT_dec (coro_readyhook);
3474 SvGETMAGIC (hook); 3394 SvGETMAGIC (hook);
3395 if (SvOK (hook))
3396 {
3475 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3397 coro_readyhook = newSVsv (hook);
3398 CORO_READYHOOK = invoke_sv_ready_hook_helper;
3399 }
3400 else
3401 {
3402 coro_readyhook = 0;
3403 CORO_READYHOOK = 0;
3404 }
3476 3405
3477int 3406int
3478prio (Coro::State coro, int newprio = 0) 3407prio (Coro::State coro, int newprio = 0)
3479 PROTOTYPE: $;$ 3408 PROTOTYPE: $;$
3480 ALIAS: 3409 ALIAS:
3596 CODE: 3525 CODE:
3597{ 3526{
3598 struct coro *coro = SvSTATE_current; 3527 struct coro *coro = SvSTATE_current;
3599 AV **avp = ix ? &coro->on_leave : &coro->on_enter; 3528 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3600 3529
3601 block = (SV *)coro_sv_2cv (aTHX_ block); 3530 block = s_get_cv_croak (block);
3602 3531
3603 if (!*avp) 3532 if (!*avp)
3604 *avp = newAV (); 3533 *avp = newAV ();
3605 3534
3606 av_push (*avp, SvREFCNT_inc (block)); 3535 av_push (*avp, SvREFCNT_inc (block));
3806 3735
3807void 3736void
3808_register (char *target, char *proto, SV *req) 3737_register (char *target, char *proto, SV *req)
3809 CODE: 3738 CODE:
3810{ 3739{
3811 CV *req_cv = coro_sv_2cv (aTHX_ req); 3740 SV *req_cv = s_get_cv_croak (req);
3812 /* newXSproto doesn't return the CV on 5.8 */ 3741 /* newXSproto doesn't return the CV on 5.8 */
3813 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3742 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3814 sv_setpv ((SV *)slf_cv, proto); 3743 sv_setpv ((SV *)slf_cv, proto);
3815 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3744 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3816} 3745}
3817 3746
3747MODULE = Coro::State PACKAGE = Coro::Select
3748
3749void
3750patch_pp_sselect ()
3751 CODE:
3752 if (!coro_old_pp_sselect)
3753 {
3754 coro_select_select = (SV *)get_cv ("Coro::Select::select", 0);
3755 coro_old_pp_sselect = PL_ppaddr [OP_SSELECT];
3756 PL_ppaddr [OP_SSELECT] = coro_pp_sselect;
3757 }
3758
3759void
3760unpatch_pp_sselect ()
3761 CODE:
3762 if (coro_old_pp_sselect)
3763 {
3764 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3765 coro_old_pp_sselect = 0;
3766 }
3767
3768

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines