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.365 by root, Mon Jul 20 16:18:30 2009 UTC vs.
Revision 1.380 by root, Sun Sep 12 03:06:38 2010 UTC

1#define NDEBUG 1
2
1#include "libcoro/coro.c" 3#include "libcoro/coro.c"
2 4
3#define PERL_NO_GET_CONTEXT 5#define PERL_NO_GET_CONTEXT
4#define PERL_EXT 6#define PERL_EXT
5 7
11#include "schmorp.h" 13#include "schmorp.h"
12 14
13#include <stdio.h> 15#include <stdio.h>
14#include <errno.h> 16#include <errno.h>
15#include <assert.h> 17#include <assert.h>
18
19#ifndef SVs_PADSTALE
20# define SVs_PADSTALE 0
21#endif
16 22
17#ifdef WIN32 23#ifdef WIN32
18# undef setjmp 24# undef setjmp
19# undef longjmp 25# undef longjmp
20# undef _exit 26# undef _exit
142static SV *sv_pool_size; 148static SV *sv_pool_size;
143static SV *sv_async_pool_idle; /* description string */ 149static SV *sv_async_pool_idle; /* description string */
144static AV *av_async_pool; /* idle pool */ 150static AV *av_async_pool; /* idle pool */
145static SV *sv_Coro; /* class string */ 151static SV *sv_Coro; /* class string */
146static CV *cv_pool_handler; 152static CV *cv_pool_handler;
147static CV *cv_coro_state_new;
148 153
149/* Coro::AnyEvent */ 154/* Coro::AnyEvent */
150static SV *sv_activity; 155static SV *sv_activity;
151 156
152/* enable processtime/realtime profiling */ 157/* enable processtime/realtime profiling */
251 256
252 /* on_enter/on_leave */ 257 /* on_enter/on_leave */
253 AV *on_enter; 258 AV *on_enter;
254 AV *on_leave; 259 AV *on_leave;
255 260
261 /* swap_sv */
262 AV *swap_sv;
263
256 /* times */ 264 /* times */
257 coro_ts t_cpu, t_real; 265 coro_ts t_cpu, t_real;
258 266
259 /* linked list */ 267 /* linked list */
260 struct coro *next, *prev; 268 struct coro *next, *prev;
290static OP *(*coro_old_pp_sselect) (pTHX); 298static OP *(*coro_old_pp_sselect) (pTHX);
291static SV *coro_select_select; 299static SV *coro_select_select;
292 300
293/* horrible hack, but if it works... */ 301/* horrible hack, but if it works... */
294static OP * 302static OP *
295coro_pp_sselect (aTHX) 303coro_pp_sselect (pTHX)
296{ 304{
297 dSP; 305 dSP;
298 PUSHMARK (SP - 4); /* fake argument list */ 306 PUSHMARK (SP - 4); /* fake argument list */
299 XPUSHs (coro_select_select); 307 XPUSHs (coro_select_select);
300 PUTBACK; 308 PUTBACK;
551 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 559 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
552} 560}
553 561
554/** load & save, init *******************************************************/ 562/** load & save, init *******************************************************/
555 563
564/* swap sv heads, at least logically */
565static void
566swap_svs (pTHX_ Coro__State c)
567{
568 int i;
569
570 for (i = 0; i <= AvFILLp (c->swap_sv); )
571 {
572 SV *a = AvARRAY (c->swap_sv)[i++];
573 SV *b = AvARRAY (c->swap_sv)[i++];
574
575 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
576 SV tmp;
577
578 /* swap sv_any */
579 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
580
581 /* swap sv_flags */
582 SvFLAGS (&tmp) = SvFLAGS (a);
583 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
584 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
585
586#if PERL_VERSION_ATLEAST (5,10,0)
587 /* perl 5.10 complicates this _quite_ a bit, but it also is
588 * is much faster, so no quarrels here. alternatively, we could
589 * sv_upgrade to avoid this.
590 */
591 {
592 /* swap sv_u */
593 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
594
595 /* if SvANY points to the head, we need to adjust the pointers,
596 * as the pointer for a still points to b, and maybe vice versa.
597 */
598 #define svany_in_head(type) \
599 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
600
601 if (svany_in_head (SvTYPE (a)))
602 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
603
604 if (svany_in_head (SvTYPE (b)))
605 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
606 }
607#endif
608 }
609}
610
611#define SWAP_SVS(coro) \
612 if (expect_false ((coro)->swap_sv)) \
613 swap_svs (aTHX_ (coro))
614
556static void 615static void
557on_enterleave_call (pTHX_ SV *cb); 616on_enterleave_call (pTHX_ SV *cb);
558 617
559static void 618static void
560load_perl (pTHX_ Coro__State c) 619load_perl (pTHX_ Coro__State c)
606 int i; 665 int i;
607 666
608 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 667 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
609 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 668 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
610 } 669 }
670
671 SWAP_SVS (c);
611} 672}
612 673
613static void 674static void
614save_perl (pTHX_ Coro__State c) 675save_perl (pTHX_ Coro__State c)
615{ 676{
677 SWAP_SVS (c);
678
616 if (expect_false (c->on_leave)) 679 if (expect_false (c->on_leave))
617 { 680 {
618 int i; 681 int i;
619 682
620 for (i = AvFILLp (c->on_leave); i >= 0; --i) 683 for (i = AvFILLp (c->on_leave); i >= 0; --i)
927slf_check_repeat (pTHX_ struct CoroSLF *frame) 990slf_check_repeat (pTHX_ struct CoroSLF *frame)
928{ 991{
929 return 1; 992 return 1;
930} 993}
931 994
932static UNOP coro_setup_op; 995static UNOP init_perl_op;
933 996
934static void NOINLINE /* noinline to keep it out of the transfer fast path */ 997static void NOINLINE /* noinline to keep it out of the transfer fast path */
935coro_setup (pTHX_ struct coro *coro) 998init_perl (pTHX_ struct coro *coro)
936{ 999{
937 /* 1000 /*
938 * emulate part of the perl startup here. 1001 * emulate part of the perl startup here.
939 */ 1002 */
940 coro_init_stacks (aTHX); 1003 coro_init_stacks (aTHX);
989 */ 1052 */
990 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 1053 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
991 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 1054 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
992 1055
993 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 1056 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
994 coro_setup_op.op_next = PL_op; 1057 init_perl_op.op_next = PL_op;
995 coro_setup_op.op_type = OP_ENTERSUB; 1058 init_perl_op.op_type = OP_ENTERSUB;
996 coro_setup_op.op_ppaddr = pp_slf; 1059 init_perl_op.op_ppaddr = pp_slf;
997 /* no flags etc. required, as an init function won't be called */ 1060 /* no flags etc. required, as an init function won't be called */
998 1061
999 PL_op = (OP *)&coro_setup_op; 1062 PL_op = (OP *)&init_perl_op;
1000 1063
1001 /* copy throw, in case it was set before coro_setup */ 1064 /* copy throw, in case it was set before init_perl */
1002 CORO_THROW = coro->except; 1065 CORO_THROW = coro->except;
1066
1067 SWAP_SVS (coro);
1003 1068
1004 if (expect_false (enable_times)) 1069 if (expect_false (enable_times))
1005 { 1070 {
1006 coro_times_update (); 1071 coro_times_update ();
1007 coro_times_sub (coro); 1072 coro_times_sub (coro);
1028 dounwind (-1); 1093 dounwind (-1);
1029 } 1094 }
1030} 1095}
1031 1096
1032static void 1097static void
1033coro_destruct_perl (pTHX_ struct coro *coro) 1098destroy_perl (pTHX_ struct coro *coro)
1034{ 1099{
1035 SV *svf [9]; 1100 SV *svf [9];
1036 1101
1037 { 1102 {
1038 struct coro *current = SvSTATE_current; 1103 struct coro *current = SvSTATE_current;
1042 save_perl (aTHX_ current); 1107 save_perl (aTHX_ current);
1043 load_perl (aTHX_ coro); 1108 load_perl (aTHX_ coro);
1044 1109
1045 coro_unwind_stacks (aTHX); 1110 coro_unwind_stacks (aTHX);
1046 coro_destruct_stacks (aTHX); 1111 coro_destruct_stacks (aTHX);
1112
1113 /* restore swapped sv's */
1114 SWAP_SVS (coro);
1047 1115
1048 // now save some sv's to be free'd later 1116 // now save some sv's to be free'd later
1049 svf [0] = GvSV (PL_defgv); 1117 svf [0] = GvSV (PL_defgv);
1050 svf [1] = (SV *)GvAV (PL_defgv); 1118 svf [1] = (SV *)GvAV (PL_defgv);
1051 svf [2] = GvSV (PL_errgv); 1119 svf [2] = GvSV (PL_errgv);
1275 /* somebody or something will hit me for both perl_run and PL_restartop */ 1343 /* somebody or something will hit me for both perl_run and PL_restartop */
1276 PL_restartop = PL_op; 1344 PL_restartop = PL_op;
1277 perl_run (PL_curinterp); 1345 perl_run (PL_curinterp);
1278 /* 1346 /*
1279 * Unfortunately, there is no way to get at the return values of the 1347 * Unfortunately, there is no way to get at the return values of the
1280 * coro body here, as perl_run destroys these 1348 * coro body here, as perl_run destroys these. Likewise, we cannot catch
1349 * runtime errors here, as this is just a random interpreter, not a thread.
1281 */ 1350 */
1282 1351
1283 /* 1352 /*
1284 * If perl-run returns we assume exit() was being called or the coro 1353 * If perl-run returns we assume exit() was being called or the coro
1285 * fell off the end, which seems to be the only valid (non-bug) 1354 * fell off the end, which seems to be the only valid (non-bug)
1492 if (expect_false (next->flags & CF_NEW)) 1561 if (expect_false (next->flags & CF_NEW))
1493 { 1562 {
1494 /* need to start coroutine */ 1563 /* need to start coroutine */
1495 next->flags &= ~CF_NEW; 1564 next->flags &= ~CF_NEW;
1496 /* setup coroutine call */ 1565 /* setup coroutine call */
1497 coro_setup (aTHX_ next); 1566 init_perl (aTHX_ next);
1498 } 1567 }
1499 else 1568 else
1500 load_perl (aTHX_ next); 1569 load_perl (aTHX_ next);
1501 1570
1502 /* possibly untie and reuse the cctx */ 1571 /* possibly untie and reuse the cctx */
1565 1634
1566 if (coro->mainstack 1635 if (coro->mainstack
1567 && coro->mainstack != main_mainstack 1636 && coro->mainstack != main_mainstack
1568 && coro->slot 1637 && coro->slot
1569 && !PL_dirty) 1638 && !PL_dirty)
1570 coro_destruct_perl (aTHX_ coro); 1639 destroy_perl (aTHX_ coro);
1640
1641 if (coro->next) coro->next->prev = coro->prev;
1642 if (coro->prev) coro->prev->next = coro->next;
1643 if (coro == coro_first) coro_first = coro->next;
1571 1644
1572 cctx_destroy (coro->cctx); 1645 cctx_destroy (coro->cctx);
1573 SvREFCNT_dec (coro->startcv); 1646 SvREFCNT_dec (coro->startcv);
1574 SvREFCNT_dec (coro->args); 1647 SvREFCNT_dec (coro->args);
1648 SvREFCNT_dec (coro->swap_sv);
1575 SvREFCNT_dec (CORO_THROW); 1649 SvREFCNT_dec (CORO_THROW);
1576
1577 if (coro->next) coro->next->prev = coro->prev;
1578 if (coro->prev) coro->prev->next = coro->next;
1579 if (coro == coro_first) coro_first = coro->next;
1580 1650
1581 return 1; 1651 return 1;
1582} 1652}
1583 1653
1584static int 1654static int
1754 { 1824 {
1755 /* nothing to schedule: call the idle handler */ 1825 /* nothing to schedule: call the idle handler */
1756 if (SvROK (sv_idle) 1826 if (SvROK (sv_idle)
1757 && SvOBJECT (SvRV (sv_idle))) 1827 && SvOBJECT (SvRV (sv_idle)))
1758 { 1828 {
1829 if (SvRV (sv_idle) == SvRV (coro_current))
1830 croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught");
1831
1759 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1832 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1760 api_ready (aTHX_ SvRV (sv_idle)); 1833 api_ready (aTHX_ SvRV (sv_idle));
1761 --coro_nready; 1834 --coro_nready;
1762 } 1835 }
1763 else 1836 else
1764 { 1837 {
1838 /* TODO: deprecated, remove, cannot work reliably *//*D*/
1765 dSP; 1839 dSP;
1766 1840
1767 ENTER; 1841 ENTER;
1768 SAVETMPS; 1842 SAVETMPS;
1769 1843
1915{ 1989{
1916 int i; 1990 int i;
1917 HV *hv = (HV *)SvRV (coro_current); 1991 HV *hv = (HV *)SvRV (coro_current);
1918 AV *av = newAV (); 1992 AV *av = newAV ();
1919 1993
1994 /* items are actually not so common, so optimise for this case */
1995 if (items)
1996 {
1920 av_extend (av, items - 1); 1997 av_extend (av, items - 1);
1998
1921 for (i = 0; i < items; ++i) 1999 for (i = 0; i < items; ++i)
1922 av_push (av, SvREFCNT_inc_NN (arg [i])); 2000 av_push (av, SvREFCNT_inc_NN (arg [i]));
2001 }
1923 2002
1924 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); 2003 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1925 2004
1926 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 2005 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1927 api_ready (aTHX_ sv_manager); 2006 api_ready (aTHX_ sv_manager);
2343 if (PL_op->op_flags & OPf_STACKED) 2422 if (PL_op->op_flags & OPf_STACKED)
2344 { 2423 {
2345 if (items > slf_arga) 2424 if (items > slf_arga)
2346 { 2425 {
2347 slf_arga = items; 2426 slf_arga = items;
2348 free (slf_argv); 2427 Safefree (slf_argv);
2349 slf_argv = malloc (slf_arga * sizeof (SV *)); 2428 New (0, slf_argv, slf_arga, SV *);
2350 } 2429 }
2351 2430
2352 slf_argc = items; 2431 slf_argc = items;
2353 2432
2354 for (i = 0; i < items; ++i) 2433 for (i = 0; i < items; ++i)
2887 /* now call the AIO function - we assume our request is uncancelable */ 2966 /* now call the AIO function - we assume our request is uncancelable */
2888 PUTBACK; 2967 PUTBACK;
2889 call_sv ((SV *)req, G_VOID | G_DISCARD); 2968 call_sv ((SV *)req, G_VOID | G_DISCARD);
2890 } 2969 }
2891 2970
2892 /* now that the requets is going, we loop toll we have a result */ 2971 /* now that the request is going, we loop till we have a result */
2893 frame->data = (void *)state; 2972 frame->data = (void *)state;
2894 frame->prepare = prepare_schedule; 2973 frame->prepare = prepare_schedule;
2895 frame->check = slf_check_aio_req; 2974 frame->check = slf_check_aio_req;
2896} 2975}
2897 2976
2908/*****************************************************************************/ 2987/*****************************************************************************/
2909 2988
2910#if CORO_CLONE 2989#if CORO_CLONE
2911# include "clone.c" 2990# include "clone.c"
2912#endif 2991#endif
2992
2993/*****************************************************************************/
2994
2995static SV *
2996coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro)
2997{
2998 SV *coro_sv;
2999 struct coro *coro;
3000 MAGIC *mg;
3001 HV *hv;
3002 SV *cb;
3003 int i;
3004
3005 if (argc > 0)
3006 {
3007 cb = s_get_cv_croak (argv [0]);
3008
3009 if (!is_coro)
3010 {
3011 if (CvISXSUB (cb))
3012 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3013
3014 if (!CvROOT (cb))
3015 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
3016 }
3017 }
3018
3019 Newz (0, coro, 1, struct coro);
3020 coro->args = newAV ();
3021 coro->flags = CF_NEW;
3022
3023 if (coro_first) coro_first->prev = coro;
3024 coro->next = coro_first;
3025 coro_first = coro;
3026
3027 coro->hv = hv = newHV ();
3028 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3029 mg->mg_flags |= MGf_DUP;
3030 coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash);
3031
3032 if (argc > 0)
3033 {
3034 av_extend (coro->args, argc + is_coro - 1);
3035
3036 if (is_coro)
3037 {
3038 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3039 cb = (SV *)cv_coro_run;
3040 }
3041
3042 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3043
3044 for (i = 1; i < argc; i++)
3045 av_push (coro->args, newSVsv (argv [i]));
3046 }
3047
3048 return coro_sv;
3049}
2913 3050
2914MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3051MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2915 3052
2916PROTOTYPES: DISABLE 3053PROTOTYPES: DISABLE
2917 3054
2986 3123
2987 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3124 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
2988} 3125}
2989 3126
2990SV * 3127SV *
2991new (char *klass, ...) 3128new (SV *klass, ...)
2992 ALIAS: 3129 ALIAS:
2993 Coro::new = 1 3130 Coro::new = 1
2994 CODE: 3131 CODE:
2995{ 3132 RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix);
2996 struct coro *coro; 3133 OUTPUT:
2997 MAGIC *mg;
2998 HV *hv;
2999 SV *cb;
3000 int i;
3001
3002 if (items > 1)
3003 {
3004 cb = s_get_cv_croak (ST (1));
3005
3006 if (!ix)
3007 {
3008 if (CvISXSUB (cb))
3009 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3010
3011 if (!CvROOT (cb))
3012 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
3013 }
3014 }
3015
3016 Newz (0, coro, 1, struct coro);
3017 coro->args = newAV ();
3018 coro->flags = CF_NEW;
3019
3020 if (coro_first) coro_first->prev = coro;
3021 coro->next = coro_first;
3022 coro_first = coro;
3023
3024 coro->hv = hv = newHV ();
3025 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3026 mg->mg_flags |= MGf_DUP;
3027 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
3028
3029 if (items > 1)
3030 {
3031 av_extend (coro->args, items - 1 + ix - 1);
3032
3033 if (ix)
3034 {
3035 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3036 cb = (SV *)cv_coro_run;
3037 }
3038
3039 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3040
3041 for (i = 2; i < items; i++)
3042 av_push (coro->args, newSVsv (ST (i)));
3043 }
3044}
3045 OUTPUT:
3046 RETVAL 3134 RETVAL
3047 3135
3048void 3136void
3049transfer (...) 3137transfer (...)
3050 PROTOTYPE: $$ 3138 PROTOTYPE: $$
3187 RETVAL = boolSV (coro->flags & ix); 3275 RETVAL = boolSV (coro->flags & ix);
3188 OUTPUT: 3276 OUTPUT:
3189 RETVAL 3277 RETVAL
3190 3278
3191void 3279void
3192throw (Coro::State self, SV *throw = &PL_sv_undef) 3280throw (Coro::State self, SV *exception = &PL_sv_undef)
3193 PROTOTYPE: $;$ 3281 PROTOTYPE: $;$
3194 CODE: 3282 CODE:
3195{ 3283{
3196 struct coro *current = SvSTATE_current; 3284 struct coro *current = SvSTATE_current;
3197 SV **throwp = self == current ? &CORO_THROW : &self->except; 3285 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3198 SvREFCNT_dec (*throwp); 3286 SvREFCNT_dec (*exceptionp);
3199 SvGETMAGIC (throw); 3287 SvGETMAGIC (exception);
3200 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3288 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3201} 3289}
3202 3290
3203void 3291void
3204api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3292api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3205 PROTOTYPE: $;$ 3293 PROTOTYPE: $;$
3300 3388
3301 if (expect_false (current == self)) 3389 if (expect_false (current == self))
3302 coro_times_sub (SvSTATE (coro_current)); 3390 coro_times_sub (SvSTATE (coro_current));
3303} 3391}
3304 3392
3393void
3394swap_sv (Coro::State coro, SV *sv, SV *swapsv)
3395 CODE:
3396{
3397 struct coro *current = SvSTATE_current;
3398
3399 if (current == coro)
3400 SWAP_SVS (current);
3401
3402 if (!coro->swap_sv)
3403 coro->swap_sv = newAV ();
3404
3405 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv )));
3406 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv)));
3407
3408 if (current == coro)
3409 SWAP_SVS (current);
3410}
3411
3412
3305MODULE = Coro::State PACKAGE = Coro 3413MODULE = Coro::State PACKAGE = Coro
3306 3414
3307BOOT: 3415BOOT:
3308{ 3416{
3309 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3417 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3317 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3425 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3318 3426
3319 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3427 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3320 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3428 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3321 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3429 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3322 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3430 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3323 3431
3324 coro_stash = gv_stashpv ("Coro", TRUE); 3432 coro_stash = gv_stashpv ("Coro", TRUE);
3325 3433
3326 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3434 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3327 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3435 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3328 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3436 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3345 /*GCoroAPI = &coroapi;*/ 3453 /*GCoroAPI = &coroapi;*/
3346 sv_setiv (sv, (IV)&coroapi); 3454 sv_setiv (sv, (IV)&coroapi);
3347 SvREADONLY_on (sv); 3455 SvREADONLY_on (sv);
3348 } 3456 }
3349} 3457}
3458
3459SV *
3460async (...)
3461 PROTOTYPE: &@
3462 CODE:
3463 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1);
3464 api_ready (aTHX_ RETVAL);
3465 OUTPUT:
3466 RETVAL
3350 3467
3351void 3468void
3352terminate (...) 3469terminate (...)
3353 CODE: 3470 CODE:
3354 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3471 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3393 SvGETMAGIC (hook); 3510 SvGETMAGIC (hook);
3394 if (SvOK (hook)) 3511 if (SvOK (hook))
3395 { 3512 {
3396 coro_readyhook = newSVsv (hook); 3513 coro_readyhook = newSVsv (hook);
3397 CORO_READYHOOK = invoke_sv_ready_hook_helper; 3514 CORO_READYHOOK = invoke_sv_ready_hook_helper;
3398 CORO_READYHOOK ();
3399 } 3515 }
3400 else 3516 else
3401 { 3517 {
3402 coro_readyhook = 0; 3518 coro_readyhook = 0;
3403 CORO_READYHOOK = 0; 3519 CORO_READYHOOK = 0;
3473 for (i = 1; i < items; ++i) 3589 for (i = 1; i < items; ++i)
3474 av_push (av, SvREFCNT_inc_NN (ST (i))); 3590 av_push (av, SvREFCNT_inc_NN (ST (i)));
3475 3591
3476 if ((SV *)hv == &PL_sv_undef) 3592 if ((SV *)hv == &PL_sv_undef)
3477 { 3593 {
3478 PUSHMARK (SP); 3594 SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1);
3479 EXTEND (SP, 2);
3480 PUSHs (sv_Coro);
3481 PUSHs ((SV *)cv_pool_handler);
3482 PUTBACK;
3483 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3484 SPAGAIN;
3485
3486 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3595 hv = (HV *)SvREFCNT_inc_NN (SvRV (sv));
3596 SvREFCNT_dec (sv);
3487 } 3597 }
3488 3598
3489 { 3599 {
3490 struct coro *coro = SvSTATE_hv (hv); 3600 struct coro *coro = SvSTATE_hv (hv);
3491 3601

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines