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.360 by root, Mon Jun 29 06:14:23 2009 UTC vs.
Revision 1.368 by root, Thu Aug 13 02:35:41 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{
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} 338}
378 339
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}
392
393INLINE void 340INLINE void
394coro_times_update () 341coro_times_update ()
395{ 342{
396#ifdef coro_clock_gettime 343#ifdef coro_clock_gettime
397 struct timespec ts; 344 struct timespec ts;
402 349
403 ts.tv_sec = ts.tv_nsec = 0; 350 ts.tv_sec = ts.tv_nsec = 0;
404 coro_clock_gettime (CORO_CLOCK_MONOTONIC, &ts); 351 coro_clock_gettime (CORO_CLOCK_MONOTONIC, &ts);
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
354 dTHX;
407 UV tv[2]; 355 UV tv[2];
408 356
409 u2time (aTHX_ &tv); 357 u2time (aTHX_ tv);
410 time_real [0] = tv [0]; 358 time_real [0] = tv [0];
411 time_real [1] = tv [1] * 1000; 359 time_real [1] = tv [1] * 1000;
412#endif 360#endif
413} 361}
414 362
544 AV *padlist; 492 AV *padlist;
545 AV *av = (AV *)mg->mg_obj; 493 AV *av = (AV *)mg->mg_obj;
546 494
547 /* perl manages to free our internal AV and _then_ call us */ 495 /* perl manages to free our internal AV and _then_ call us */
548 if (IN_DESTRUCT) 496 if (IN_DESTRUCT)
549 return; 497 return 0;
550 498
551 /* casting is fun. */ 499 /* casting is fun. */
552 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 500 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
553 free_padlist (aTHX_ padlist); 501 free_padlist (aTHX_ padlist);
554 502
735 /* we manually unroll here, as usually 2 slots is enough */ 683 /* we manually unroll here, as usually 2 slots is enough */
736 if (SLOT_COUNT >= 1) CXINC; 684 if (SLOT_COUNT >= 1) CXINC;
737 if (SLOT_COUNT >= 2) CXINC; 685 if (SLOT_COUNT >= 2) CXINC;
738 if (SLOT_COUNT >= 3) CXINC; 686 if (SLOT_COUNT >= 3) CXINC;
739 { 687 {
740 int i; 688 unsigned int i;
741 for (i = 3; i < SLOT_COUNT; ++i) 689 for (i = 3; i < SLOT_COUNT; ++i)
742 CXINC; 690 CXINC;
743 } 691 }
744 cxstack_ix -= SLOT_COUNT; /* undo allocation */ 692 cxstack_ix -= SLOT_COUNT; /* undo allocation */
745 693
1111 1059
1112 load_perl (aTHX_ current); 1060 load_perl (aTHX_ current);
1113 } 1061 }
1114 1062
1115 { 1063 {
1116 int i; 1064 unsigned int i;
1117 1065
1118 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) 1066 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1119 SvREFCNT_dec (svf [i]); 1067 SvREFCNT_dec (svf [i]);
1120 1068
1121 SvREFCNT_dec (coro->saved_deffh); 1069 SvREFCNT_dec (coro->saved_deffh);
1327 /* 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 */
1328 PL_restartop = PL_op; 1276 PL_restartop = PL_op;
1329 perl_run (PL_curinterp); 1277 perl_run (PL_curinterp);
1330 /* 1278 /*
1331 * 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
1332 * 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.
1333 */ 1282 */
1334 1283
1335 /* 1284 /*
1336 * 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
1337 * 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)
1686 1635
1687 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1636 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1688 TRANSFER (ta, 1); 1637 TRANSFER (ta, 1);
1689} 1638}
1690 1639
1691/*****************************************************************************/
1692/* gensub: simple closure generation utility */
1693
1694#define GENSUB_ARG CvXSUBANY (cv).any_ptr
1695
1696/* create a closure from XS, returns a code reference */
1697/* the arg can be accessed via GENSUB_ARG from the callback */
1698/* the callback must use dXSARGS/XSRETURN */
1699static SV *
1700gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
1701{
1702 CV *cv = (CV *)newSV (0);
1703
1704 sv_upgrade ((SV *)cv, SVt_PVCV);
1705
1706 CvANON_on (cv);
1707 CvISXSUB_on (cv);
1708 CvXSUB (cv) = xsub;
1709 GENSUB_ARG = arg;
1710
1711 return newRV_noinc ((SV *)cv);
1712}
1713
1714/** Coro ********************************************************************/ 1640/** Coro ********************************************************************/
1715 1641
1716INLINE void 1642INLINE void
1717coro_enq (pTHX_ struct coro *coro) 1643coro_enq (pTHX_ struct coro *coro)
1718{ 1644{
1743 } 1669 }
1744 1670
1745 return 0; 1671 return 0;
1746} 1672}
1747 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
1748static int 1691static int
1749api_ready (pTHX_ SV *coro_sv) 1692api_ready (pTHX_ SV *coro_sv)
1750{ 1693{
1751 struct coro *coro;
1752 SV *sv_hook;
1753 void (*xs_hook)(void);
1754
1755 coro = SvSTATE (coro_sv); 1694 struct coro *coro = SvSTATE (coro_sv);
1756 1695
1757 if (coro->flags & CF_READY) 1696 if (coro->flags & CF_READY)
1758 return 0; 1697 return 0;
1759 1698
1760 coro->flags |= CF_READY; 1699 coro->flags |= CF_READY;
1761 1700
1762 sv_hook = coro_nready ? 0 : coro_readyhook;
1763 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1764
1765 coro_enq (aTHX_ coro); 1701 coro_enq (aTHX_ coro);
1766 ++coro_nready;
1767 1702
1768 if (sv_hook) 1703 if (!coro_nready++)
1769 { 1704 if (coroapi.readyhook)
1770 dSP; 1705 coroapi.readyhook ();
1771
1772 ENTER;
1773 SAVETMPS;
1774
1775 PUSHMARK (SP);
1776 PUTBACK;
1777 call_sv (sv_hook, G_VOID | G_DISCARD);
1778
1779 FREETMPS;
1780 LEAVE;
1781 }
1782
1783 if (xs_hook)
1784 xs_hook ();
1785 1706
1786 return 1; 1707 return 1;
1787} 1708}
1788 1709
1789static int 1710static int
1995{ 1916{
1996 int i; 1917 int i;
1997 HV *hv = (HV *)SvRV (coro_current); 1918 HV *hv = (HV *)SvRV (coro_current);
1998 AV *av = newAV (); 1919 AV *av = newAV ();
1999 1920
1921 /* items are actually not so common, so optimise for this case */
1922 if (items)
1923 {
2000 av_extend (av, items - 1); 1924 av_extend (av, items - 1);
1925
2001 for (i = 0; i < items; ++i) 1926 for (i = 0; i < items; ++i)
2002 av_push (av, SvREFCNT_inc_NN (arg [i])); 1927 av_push (av, SvREFCNT_inc_NN (arg [i]));
1928 }
2003 1929
2004 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); 1930 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
2005 1931
2006 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1932 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
2007 api_ready (aTHX_ sv_manager); 1933 api_ready (aTHX_ sv_manager);
2097 2023
2098static void 2024static void
2099coro_rouse_callback (pTHX_ CV *cv) 2025coro_rouse_callback (pTHX_ CV *cv)
2100{ 2026{
2101 dXSARGS; 2027 dXSARGS;
2102 SV *data = (SV *)GENSUB_ARG; 2028 SV *data = (SV *)S_GENSUB_ARG;
2103 2029
2104 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2030 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2105 { 2031 {
2106 /* first call, set args */ 2032 /* first call, set args */
2107 SV *coro = SvRV (data); 2033 SV *coro = SvRV (data);
2173 || SvTYPE (SvRV (cb)) != SVt_PVCV 2099 || SvTYPE (SvRV (cb)) != SVt_PVCV
2174 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2100 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2175 croak ("Coro::rouse_wait called with illegal callback argument,"); 2101 croak ("Coro::rouse_wait called with illegal callback argument,");
2176 2102
2177 { 2103 {
2178 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ 2104 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
2179 SV *data = (SV *)GENSUB_ARG; 2105 SV *data = (SV *)S_GENSUB_ARG;
2180 2106
2181 frame->data = (void *)data; 2107 frame->data = (void *)data;
2182 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; 2108 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
2183 frame->check = slf_check_rouse_wait; 2109 frame->check = slf_check_rouse_wait;
2184 } 2110 }
2188coro_new_rouse_cb (pTHX) 2114coro_new_rouse_cb (pTHX)
2189{ 2115{
2190 HV *hv = (HV *)SvRV (coro_current); 2116 HV *hv = (HV *)SvRV (coro_current);
2191 struct coro *coro = SvSTATE_hv (hv); 2117 struct coro *coro = SvSTATE_hv (hv);
2192 SV *data = newRV_inc ((SV *)hv); 2118 SV *data = newRV_inc ((SV *)hv);
2193 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); 2119 SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data);
2194 2120
2195 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); 2121 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
2196 SvREFCNT_dec (data); /* magicext increases the refcount */ 2122 SvREFCNT_dec (data); /* magicext increases the refcount */
2197 2123
2198 SvREFCNT_dec (coro->rouse_cb); 2124 SvREFCNT_dec (coro->rouse_cb);
2721{ 2647{
2722 if (items >= 2) 2648 if (items >= 2)
2723 { 2649 {
2724 /* callback form */ 2650 /* callback form */
2725 AV *av = (AV *)SvRV (arg [0]); 2651 AV *av = (AV *)SvRV (arg [0]);
2726 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2652 SV *cb_cv = s_get_cv_croak (arg [1]);
2727 2653
2728 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2654 av_push (av, SvREFCNT_inc_NN (cb_cv));
2729 2655
2730 if (SvIVX (AvARRAY (av)[0]) > 0) 2656 if (SvIVX (AvARRAY (av)[0]) > 0)
2731 coro_semaphore_adjust (aTHX_ av, 0); 2657 coro_semaphore_adjust (aTHX_ av, 0);
2791{ 2717{
2792 AV *av = (AV *)SvRV (arg [0]); 2718 AV *av = (AV *)SvRV (arg [0]);
2793 2719
2794 if (items >= 2) 2720 if (items >= 2)
2795 { 2721 {
2796 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2722 SV *cb_cv = s_get_cv_croak (arg [1]);
2797 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2723 av_push (av, SvREFCNT_inc_NN (cb_cv));
2798 2724
2799 if (SvIVX (AvARRAY (av)[0])) 2725 if (SvIVX (AvARRAY (av)[0]))
2800 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2726 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2801 2727
2836 2762
2837static void 2763static void
2838coro_aio_callback (pTHX_ CV *cv) 2764coro_aio_callback (pTHX_ CV *cv)
2839{ 2765{
2840 dXSARGS; 2766 dXSARGS;
2841 AV *state = (AV *)GENSUB_ARG; 2767 AV *state = (AV *)S_GENSUB_ARG;
2842 SV *coro = av_pop (state); 2768 SV *coro = av_pop (state);
2843 SV *data_sv = newSV (sizeof (struct io_state)); 2769 SV *data_sv = newSV (sizeof (struct io_state));
2844 2770
2845 av_extend (state, items - 1); 2771 av_extend (state, items - 1);
2846 2772
2960 2886
2961 for (i = 0; i < items; ++i) 2887 for (i = 0; i < items; ++i)
2962 PUSHs (arg [i]); 2888 PUSHs (arg [i]);
2963 2889
2964 /* now push the callback closure */ 2890 /* now push the callback closure */
2965 PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); 2891 PUSHs (sv_2mortal (s_gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state))));
2966 2892
2967 /* now call the AIO function - we assume our request is uncancelable */ 2893 /* now call the AIO function - we assume our request is uncancelable */
2968 PUTBACK; 2894 PUTBACK;
2969 call_sv ((SV *)req, G_VOID | G_DISCARD); 2895 call_sv ((SV *)req, G_VOID | G_DISCARD);
2970 } 2896 }
2988/*****************************************************************************/ 2914/*****************************************************************************/
2989 2915
2990#if CORO_CLONE 2916#if CORO_CLONE
2991# include "clone.c" 2917# include "clone.c"
2992#endif 2918#endif
2919
2920/*****************************************************************************/
2921
2922static SV *
2923coro_new (HV *stash, SV **argv, int argc, int is_coro)
2924{
2925 SV *coro_sv;
2926 struct coro *coro;
2927 MAGIC *mg;
2928 HV *hv;
2929 SV *cb;
2930 int i;
2931
2932 if (argc > 0)
2933 {
2934 cb = s_get_cv_croak (argv [0]);
2935
2936 if (!is_coro)
2937 {
2938 if (CvISXSUB (cb))
2939 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2940
2941 if (!CvROOT (cb))
2942 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2943 }
2944 }
2945
2946 Newz (0, coro, 1, struct coro);
2947 coro->args = newAV ();
2948 coro->flags = CF_NEW;
2949
2950 if (coro_first) coro_first->prev = coro;
2951 coro->next = coro_first;
2952 coro_first = coro;
2953
2954 coro->hv = hv = newHV ();
2955 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2956 mg->mg_flags |= MGf_DUP;
2957 coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash);
2958
2959 if (argc > 0)
2960 {
2961 av_extend (coro->args, argc + is_coro - 1);
2962
2963 if (is_coro)
2964 {
2965 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2966 cb = (SV *)cv_coro_run;
2967 }
2968
2969 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2970
2971 for (i = 1; i < argc; i++)
2972 av_push (coro->args, newSVsv (argv [i]));
2973 }
2974
2975 return coro_sv;
2976}
2993 2977
2994MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2978MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2995 2979
2996PROTOTYPES: DISABLE 2980PROTOTYPES: DISABLE
2997 2981
3059 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); 3043 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3060 3044
3061 nvtime = INT2PTR (double (*)(), SvIV (*svp)); 3045 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3062 3046
3063 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); 3047 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3064 u2time = INT2PTR (double (*)(), SvIV (*svp)); 3048 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3065 } 3049 }
3066 3050
3067 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3051 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3068} 3052}
3069 3053
3070SV * 3054SV *
3071new (char *klass, ...) 3055new (SV *klass, ...)
3072 ALIAS: 3056 ALIAS:
3073 Coro::new = 1 3057 Coro::new = 1
3074 CODE: 3058 CODE:
3075{ 3059 RETVAL = coro_new (ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix);
3076 struct coro *coro; 3060 OUTPUT:
3077 MAGIC *mg;
3078 HV *hv;
3079 CV *cb;
3080 int i;
3081
3082 if (items > 1)
3083 {
3084 cb = coro_sv_2cv (aTHX_ ST (1));
3085
3086 if (!ix)
3087 {
3088 if (CvISXSUB (cb))
3089 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3090
3091 if (!CvROOT (cb))
3092 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
3093 }
3094 }
3095
3096 Newz (0, coro, 1, struct coro);
3097 coro->args = newAV ();
3098 coro->flags = CF_NEW;
3099
3100 if (coro_first) coro_first->prev = coro;
3101 coro->next = coro_first;
3102 coro_first = coro;
3103
3104 coro->hv = hv = newHV ();
3105 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3106 mg->mg_flags |= MGf_DUP;
3107 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
3108
3109 if (items > 1)
3110 {
3111 av_extend (coro->args, items - 1 + ix - 1);
3112
3113 if (ix)
3114 {
3115 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3116 cb = cv_coro_run;
3117 }
3118
3119 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3120
3121 for (i = 2; i < items; i++)
3122 av_push (coro->args, newSVsv (ST (i)));
3123 }
3124}
3125 OUTPUT:
3126 RETVAL 3061 RETVAL
3127 3062
3128void 3063void
3129transfer (...) 3064transfer (...)
3130 PROTOTYPE: $$ 3065 PROTOTYPE: $$
3426 sv_setiv (sv, (IV)&coroapi); 3361 sv_setiv (sv, (IV)&coroapi);
3427 SvREADONLY_on (sv); 3362 SvREADONLY_on (sv);
3428 } 3363 }
3429} 3364}
3430 3365
3366SV *
3367async (...)
3368 PROTOTYPE: &@
3369 CODE:
3370 RETVAL = coro_new (coro_stash, &ST (0), items, 1);
3371 api_ready (RETVAL);
3372 OUTPUT:
3373 RETVAL
3374
3431void 3375void
3432terminate (...) 3376terminate (...)
3433 CODE: 3377 CODE:
3434 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3378 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3435 3379
3469_set_readyhook (SV *hook) 3413_set_readyhook (SV *hook)
3470 PROTOTYPE: $ 3414 PROTOTYPE: $
3471 CODE: 3415 CODE:
3472 SvREFCNT_dec (coro_readyhook); 3416 SvREFCNT_dec (coro_readyhook);
3473 SvGETMAGIC (hook); 3417 SvGETMAGIC (hook);
3418 if (SvOK (hook))
3419 {
3474 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3420 coro_readyhook = newSVsv (hook);
3421 CORO_READYHOOK = invoke_sv_ready_hook_helper;
3422 }
3423 else
3424 {
3425 coro_readyhook = 0;
3426 CORO_READYHOOK = 0;
3427 }
3475 3428
3476int 3429int
3477prio (Coro::State coro, int newprio = 0) 3430prio (Coro::State coro, int newprio = 0)
3478 PROTOTYPE: $;$ 3431 PROTOTYPE: $;$
3479 ALIAS: 3432 ALIAS:
3595 CODE: 3548 CODE:
3596{ 3549{
3597 struct coro *coro = SvSTATE_current; 3550 struct coro *coro = SvSTATE_current;
3598 AV **avp = ix ? &coro->on_leave : &coro->on_enter; 3551 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3599 3552
3600 block = (SV *)coro_sv_2cv (aTHX_ block); 3553 block = s_get_cv_croak (block);
3601 3554
3602 if (!*avp) 3555 if (!*avp)
3603 *avp = newAV (); 3556 *avp = newAV ();
3604 3557
3605 av_push (*avp, SvREFCNT_inc (block)); 3558 av_push (*avp, SvREFCNT_inc (block));
3805 3758
3806void 3759void
3807_register (char *target, char *proto, SV *req) 3760_register (char *target, char *proto, SV *req)
3808 CODE: 3761 CODE:
3809{ 3762{
3810 CV *req_cv = coro_sv_2cv (aTHX_ req); 3763 SV *req_cv = s_get_cv_croak (req);
3811 /* newXSproto doesn't return the CV on 5.8 */ 3764 /* newXSproto doesn't return the CV on 5.8 */
3812 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3765 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3813 sv_setpv ((SV *)slf_cv, proto); 3766 sv_setpv ((SV *)slf_cv, proto);
3814 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3767 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3815} 3768}
3816 3769
3770MODULE = Coro::State PACKAGE = Coro::Select
3771
3772void
3773patch_pp_sselect ()
3774 CODE:
3775 if (!coro_old_pp_sselect)
3776 {
3777 coro_select_select = (SV *)get_cv ("Coro::Select::select", 0);
3778 coro_old_pp_sselect = PL_ppaddr [OP_SSELECT];
3779 PL_ppaddr [OP_SSELECT] = coro_pp_sselect;
3780 }
3781
3782void
3783unpatch_pp_sselect ()
3784 CODE:
3785 if (coro_old_pp_sselect)
3786 {
3787 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3788 coro_old_pp_sselect = 0;
3789 }
3790
3791

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines