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.363 by root, Tue Jul 14 00:55:10 2009 UTC vs.
Revision 1.375 by root, Fri Oct 2 19:58:02 2009 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;
282static SV *coro_readyhook; 290static SV *coro_readyhook;
283static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 291static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
284static CV *cv_coro_run, *cv_coro_terminate; 292static CV *cv_coro_run, *cv_coro_terminate;
285static struct coro *coro_first; 293static struct coro *coro_first;
286#define coro_nready coroapi.nready 294#define coro_nready coroapi.nready
295
296/** Coro::Select ************************************************************/
297
298static OP *(*coro_old_pp_sselect) (pTHX);
299static SV *coro_select_select;
300
301/* horrible hack, but if it works... */
302static OP *
303coro_pp_sselect (pTHX)
304{
305 dSP;
306 PUSHMARK (SP - 4); /* fake argument list */
307 XPUSHs (coro_select_select);
308 PUTBACK;
309
310 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
311 PL_op->op_flags |= OPf_STACKED;
312 PL_op->op_private = 0;
313 return PL_ppaddr [OP_ENTERSUB](aTHX);
314}
287 315
288/** lowlevel stuff **********************************************************/ 316/** lowlevel stuff **********************************************************/
289 317
290static SV * 318static SV *
291coro_get_sv (pTHX_ const char *name, int create) 319coro_get_sv (pTHX_ const char *name, int create)
531 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 559 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
532} 560}
533 561
534/** load & save, init *******************************************************/ 562/** load & save, init *******************************************************/
535 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
536static void 615static void
537on_enterleave_call (pTHX_ SV *cb); 616on_enterleave_call (pTHX_ SV *cb);
538 617
539static void 618static void
540load_perl (pTHX_ Coro__State c) 619load_perl (pTHX_ Coro__State c)
586 int i; 665 int i;
587 666
588 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 667 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
589 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 668 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
590 } 669 }
670
671 SWAP_SVS (c);
591} 672}
592 673
593static void 674static void
594save_perl (pTHX_ Coro__State c) 675save_perl (pTHX_ Coro__State c)
595{ 676{
677 SWAP_SVS (c);
678
596 if (expect_false (c->on_leave)) 679 if (expect_false (c->on_leave))
597 { 680 {
598 int i; 681 int i;
599 682
600 for (i = AvFILLp (c->on_leave); i >= 0; --i) 683 for (i = AvFILLp (c->on_leave); i >= 0; --i)
635 { 718 {
636 CV *cv = cx->blk_sub.cv; 719 CV *cv = cx->blk_sub.cv;
637 720
638 if (expect_true (CvDEPTH (cv))) 721 if (expect_true (CvDEPTH (cv)))
639 { 722 {
640 EXTEND (SP, 3);
641 PUSHs ((SV *)CvPADLIST (cv)); 723 PUSHs ((SV *)CvPADLIST (cv));
642 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 724 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
643 PUSHs ((SV *)cv); 725 PUSHs ((SV *)cv);
644 726
645 CvDEPTH (cv) = 0; 727 CvDEPTH (cv) = 0;
907slf_check_repeat (pTHX_ struct CoroSLF *frame) 989slf_check_repeat (pTHX_ struct CoroSLF *frame)
908{ 990{
909 return 1; 991 return 1;
910} 992}
911 993
912static UNOP coro_setup_op; 994static UNOP init_perl_op;
913 995
914static void NOINLINE /* noinline to keep it out of the transfer fast path */ 996static void NOINLINE /* noinline to keep it out of the transfer fast path */
915coro_setup (pTHX_ struct coro *coro) 997init_perl (pTHX_ struct coro *coro)
916{ 998{
917 /* 999 /*
918 * emulate part of the perl startup here. 1000 * emulate part of the perl startup here.
919 */ 1001 */
920 coro_init_stacks (aTHX); 1002 coro_init_stacks (aTHX);
969 */ 1051 */
970 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 1052 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
971 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 1053 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
972 1054
973 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 1055 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
974 coro_setup_op.op_next = PL_op; 1056 init_perl_op.op_next = PL_op;
975 coro_setup_op.op_type = OP_ENTERSUB; 1057 init_perl_op.op_type = OP_ENTERSUB;
976 coro_setup_op.op_ppaddr = pp_slf; 1058 init_perl_op.op_ppaddr = pp_slf;
977 /* no flags etc. required, as an init function won't be called */ 1059 /* no flags etc. required, as an init function won't be called */
978 1060
979 PL_op = (OP *)&coro_setup_op; 1061 PL_op = (OP *)&init_perl_op;
980 1062
981 /* copy throw, in case it was set before coro_setup */ 1063 /* copy throw, in case it was set before init_perl */
982 CORO_THROW = coro->except; 1064 CORO_THROW = coro->except;
1065
1066 SWAP_SVS (coro);
983 1067
984 if (expect_false (enable_times)) 1068 if (expect_false (enable_times))
985 { 1069 {
986 coro_times_update (); 1070 coro_times_update ();
987 coro_times_sub (coro); 1071 coro_times_sub (coro);
1008 dounwind (-1); 1092 dounwind (-1);
1009 } 1093 }
1010} 1094}
1011 1095
1012static void 1096static void
1013coro_destruct_perl (pTHX_ struct coro *coro) 1097destroy_perl (pTHX_ struct coro *coro)
1014{ 1098{
1015 SV *svf [9]; 1099 SV *svf [9];
1016 1100
1017 { 1101 {
1018 struct coro *current = SvSTATE_current; 1102 struct coro *current = SvSTATE_current;
1022 save_perl (aTHX_ current); 1106 save_perl (aTHX_ current);
1023 load_perl (aTHX_ coro); 1107 load_perl (aTHX_ coro);
1024 1108
1025 coro_unwind_stacks (aTHX); 1109 coro_unwind_stacks (aTHX);
1026 coro_destruct_stacks (aTHX); 1110 coro_destruct_stacks (aTHX);
1111
1112 /* restore swapped sv's */
1113 SWAP_SVS (coro);
1027 1114
1028 // now save some sv's to be free'd later 1115 // now save some sv's to be free'd later
1029 svf [0] = GvSV (PL_defgv); 1116 svf [0] = GvSV (PL_defgv);
1030 svf [1] = (SV *)GvAV (PL_defgv); 1117 svf [1] = (SV *)GvAV (PL_defgv);
1031 svf [2] = GvSV (PL_errgv); 1118 svf [2] = GvSV (PL_errgv);
1048 1135
1049 SvREFCNT_dec (coro->saved_deffh); 1136 SvREFCNT_dec (coro->saved_deffh);
1050 SvREFCNT_dec (coro->rouse_cb); 1137 SvREFCNT_dec (coro->rouse_cb);
1051 SvREFCNT_dec (coro->invoke_cb); 1138 SvREFCNT_dec (coro->invoke_cb);
1052 SvREFCNT_dec (coro->invoke_av); 1139 SvREFCNT_dec (coro->invoke_av);
1140 SvREFCNT_dec (coro->swap_sv);
1053 } 1141 }
1054} 1142}
1055 1143
1056INLINE void 1144INLINE void
1057free_coro_mortal (pTHX) 1145free_coro_mortal (pTHX)
1255 /* 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 */
1256 PL_restartop = PL_op; 1344 PL_restartop = PL_op;
1257 perl_run (PL_curinterp); 1345 perl_run (PL_curinterp);
1258 /* 1346 /*
1259 * 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
1260 * 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.
1261 */ 1350 */
1262 1351
1263 /* 1352 /*
1264 * 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
1265 * 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)
1472 if (expect_false (next->flags & CF_NEW)) 1561 if (expect_false (next->flags & CF_NEW))
1473 { 1562 {
1474 /* need to start coroutine */ 1563 /* need to start coroutine */
1475 next->flags &= ~CF_NEW; 1564 next->flags &= ~CF_NEW;
1476 /* setup coroutine call */ 1565 /* setup coroutine call */
1477 coro_setup (aTHX_ next); 1566 init_perl (aTHX_ next);
1478 } 1567 }
1479 else 1568 else
1480 load_perl (aTHX_ next); 1569 load_perl (aTHX_ next);
1481 1570
1482 /* possibly untie and reuse the cctx */ 1571 /* possibly untie and reuse the cctx */
1545 1634
1546 if (coro->mainstack 1635 if (coro->mainstack
1547 && coro->mainstack != main_mainstack 1636 && coro->mainstack != main_mainstack
1548 && coro->slot 1637 && coro->slot
1549 && !PL_dirty) 1638 && !PL_dirty)
1550 coro_destruct_perl (aTHX_ coro); 1639 destroy_perl (aTHX_ coro);
1551 1640
1552 cctx_destroy (coro->cctx); 1641 cctx_destroy (coro->cctx);
1553 SvREFCNT_dec (coro->startcv); 1642 SvREFCNT_dec (coro->startcv);
1554 SvREFCNT_dec (coro->args); 1643 SvREFCNT_dec (coro->args);
1555 SvREFCNT_dec (CORO_THROW); 1644 SvREFCNT_dec (CORO_THROW);
1648 } 1737 }
1649 1738
1650 return 0; 1739 return 0;
1651} 1740}
1652 1741
1742static void
1743invoke_sv_ready_hook_helper (void)
1744{
1745 dTHX;
1746 dSP;
1747
1748 ENTER;
1749 SAVETMPS;
1750
1751 PUSHMARK (SP);
1752 PUTBACK;
1753 call_sv (coro_readyhook, G_VOID | G_DISCARD);
1754
1755 FREETMPS;
1756 LEAVE;
1757}
1758
1653static int 1759static int
1654api_ready (pTHX_ SV *coro_sv) 1760api_ready (pTHX_ SV *coro_sv)
1655{ 1761{
1656 struct coro *coro;
1657 SV *sv_hook;
1658 void (*xs_hook)(void);
1659
1660 coro = SvSTATE (coro_sv); 1762 struct coro *coro = SvSTATE (coro_sv);
1661 1763
1662 if (coro->flags & CF_READY) 1764 if (coro->flags & CF_READY)
1663 return 0; 1765 return 0;
1664 1766
1665 coro->flags |= CF_READY; 1767 coro->flags |= CF_READY;
1666 1768
1667 sv_hook = coro_nready ? 0 : coro_readyhook;
1668 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1669
1670 coro_enq (aTHX_ coro); 1769 coro_enq (aTHX_ coro);
1671 ++coro_nready;
1672 1770
1673 if (sv_hook) 1771 if (!coro_nready++)
1674 { 1772 if (coroapi.readyhook)
1675 dSP; 1773 coroapi.readyhook ();
1676
1677 ENTER;
1678 SAVETMPS;
1679
1680 PUSHMARK (SP);
1681 PUTBACK;
1682 call_sv (sv_hook, G_VOID | G_DISCARD);
1683
1684 FREETMPS;
1685 LEAVE;
1686 }
1687
1688 if (xs_hook)
1689 xs_hook ();
1690 1774
1691 return 1; 1775 return 1;
1692} 1776}
1693 1777
1694static int 1778static int
1745 api_ready (aTHX_ SvRV (sv_idle)); 1829 api_ready (aTHX_ SvRV (sv_idle));
1746 --coro_nready; 1830 --coro_nready;
1747 } 1831 }
1748 else 1832 else
1749 { 1833 {
1834 /* TODO: deprecated, remove, cannot work reliably *//*D*/
1750 dSP; 1835 dSP;
1751 1836
1752 ENTER; 1837 ENTER;
1753 SAVETMPS; 1838 SAVETMPS;
1754 1839
1900{ 1985{
1901 int i; 1986 int i;
1902 HV *hv = (HV *)SvRV (coro_current); 1987 HV *hv = (HV *)SvRV (coro_current);
1903 AV *av = newAV (); 1988 AV *av = newAV ();
1904 1989
1990 /* items are actually not so common, so optimise for this case */
1991 if (items)
1992 {
1905 av_extend (av, items - 1); 1993 av_extend (av, items - 1);
1994
1906 for (i = 0; i < items; ++i) 1995 for (i = 0; i < items; ++i)
1907 av_push (av, SvREFCNT_inc_NN (arg [i])); 1996 av_push (av, SvREFCNT_inc_NN (arg [i]));
1997 }
1908 1998
1909 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); 1999 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1910 2000
1911 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 2001 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1912 api_ready (aTHX_ sv_manager); 2002 api_ready (aTHX_ sv_manager);
2894 2984
2895#if CORO_CLONE 2985#if CORO_CLONE
2896# include "clone.c" 2986# include "clone.c"
2897#endif 2987#endif
2898 2988
2989/*****************************************************************************/
2990
2991static SV *
2992coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro)
2993{
2994 SV *coro_sv;
2995 struct coro *coro;
2996 MAGIC *mg;
2997 HV *hv;
2998 SV *cb;
2999 int i;
3000
3001 if (argc > 0)
3002 {
3003 cb = s_get_cv_croak (argv [0]);
3004
3005 if (!is_coro)
3006 {
3007 if (CvISXSUB (cb))
3008 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3009
3010 if (!CvROOT (cb))
3011 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
3012 }
3013 }
3014
3015 Newz (0, coro, 1, struct coro);
3016 coro->args = newAV ();
3017 coro->flags = CF_NEW;
3018
3019 if (coro_first) coro_first->prev = coro;
3020 coro->next = coro_first;
3021 coro_first = coro;
3022
3023 coro->hv = hv = newHV ();
3024 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3025 mg->mg_flags |= MGf_DUP;
3026 coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash);
3027
3028 if (argc > 0)
3029 {
3030 av_extend (coro->args, argc + is_coro - 1);
3031
3032 if (is_coro)
3033 {
3034 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3035 cb = (SV *)cv_coro_run;
3036 }
3037
3038 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3039
3040 for (i = 1; i < argc; i++)
3041 av_push (coro->args, newSVsv (argv [i]));
3042 }
3043
3044 return coro_sv;
3045}
3046
2899MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3047MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2900 3048
2901PROTOTYPES: DISABLE 3049PROTOTYPES: DISABLE
2902 3050
2903BOOT: 3051BOOT:
2971 3119
2972 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3120 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
2973} 3121}
2974 3122
2975SV * 3123SV *
2976new (char *klass, ...) 3124new (SV *klass, ...)
2977 ALIAS: 3125 ALIAS:
2978 Coro::new = 1 3126 Coro::new = 1
2979 CODE: 3127 CODE:
2980{ 3128 RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix);
2981 struct coro *coro; 3129 OUTPUT:
2982 MAGIC *mg;
2983 HV *hv;
2984 SV *cb;
2985 int i;
2986
2987 if (items > 1)
2988 {
2989 cb = s_get_cv_croak (ST (1));
2990
2991 if (!ix)
2992 {
2993 if (CvISXSUB (cb))
2994 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2995
2996 if (!CvROOT (cb))
2997 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2998 }
2999 }
3000
3001 Newz (0, coro, 1, struct coro);
3002 coro->args = newAV ();
3003 coro->flags = CF_NEW;
3004
3005 if (coro_first) coro_first->prev = coro;
3006 coro->next = coro_first;
3007 coro_first = coro;
3008
3009 coro->hv = hv = newHV ();
3010 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3011 mg->mg_flags |= MGf_DUP;
3012 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
3013
3014 if (items > 1)
3015 {
3016 av_extend (coro->args, items - 1 + ix - 1);
3017
3018 if (ix)
3019 {
3020 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3021 cb = (SV *)cv_coro_run;
3022 }
3023
3024 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3025
3026 for (i = 2; i < items; i++)
3027 av_push (coro->args, newSVsv (ST (i)));
3028 }
3029}
3030 OUTPUT:
3031 RETVAL 3130 RETVAL
3032 3131
3033void 3132void
3034transfer (...) 3133transfer (...)
3035 PROTOTYPE: $$ 3134 PROTOTYPE: $$
3285 3384
3286 if (expect_false (current == self)) 3385 if (expect_false (current == self))
3287 coro_times_sub (SvSTATE (coro_current)); 3386 coro_times_sub (SvSTATE (coro_current));
3288} 3387}
3289 3388
3389void
3390swap_sv (Coro::State coro, SV *sv, SV *swapsv)
3391 CODE:
3392{
3393 struct coro *current = SvSTATE_current;
3394
3395 if (current == coro)
3396 SWAP_SVS (current);
3397
3398 if (!coro->swap_sv)
3399 coro->swap_sv = newAV ();
3400
3401 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv )));
3402 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv)));
3403
3404 if (current == coro)
3405 SWAP_SVS (current);
3406}
3407
3408
3290MODULE = Coro::State PACKAGE = Coro 3409MODULE = Coro::State PACKAGE = Coro
3291 3410
3292BOOT: 3411BOOT:
3293{ 3412{
3294 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3413 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3302 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3421 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3303 3422
3304 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3423 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3305 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3424 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3306 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3425 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3307 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3426 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3308 3427
3309 coro_stash = gv_stashpv ("Coro", TRUE); 3428 coro_stash = gv_stashpv ("Coro", TRUE);
3310 3429
3311 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3430 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3312 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3431 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3313 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3432 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3331 sv_setiv (sv, (IV)&coroapi); 3450 sv_setiv (sv, (IV)&coroapi);
3332 SvREADONLY_on (sv); 3451 SvREADONLY_on (sv);
3333 } 3452 }
3334} 3453}
3335 3454
3455SV *
3456async (...)
3457 PROTOTYPE: &@
3458 CODE:
3459 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1);
3460 api_ready (aTHX_ RETVAL);
3461 OUTPUT:
3462 RETVAL
3463
3336void 3464void
3337terminate (...) 3465terminate (...)
3338 CODE: 3466 CODE:
3339 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3467 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3340 3468
3374_set_readyhook (SV *hook) 3502_set_readyhook (SV *hook)
3375 PROTOTYPE: $ 3503 PROTOTYPE: $
3376 CODE: 3504 CODE:
3377 SvREFCNT_dec (coro_readyhook); 3505 SvREFCNT_dec (coro_readyhook);
3378 SvGETMAGIC (hook); 3506 SvGETMAGIC (hook);
3507 if (SvOK (hook))
3508 {
3379 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3509 coro_readyhook = newSVsv (hook);
3510 CORO_READYHOOK = invoke_sv_ready_hook_helper;
3511 }
3512 else
3513 {
3514 coro_readyhook = 0;
3515 CORO_READYHOOK = 0;
3516 }
3380 3517
3381int 3518int
3382prio (Coro::State coro, int newprio = 0) 3519prio (Coro::State coro, int newprio = 0)
3383 PROTOTYPE: $;$ 3520 PROTOTYPE: $;$
3384 ALIAS: 3521 ALIAS:
3448 for (i = 1; i < items; ++i) 3585 for (i = 1; i < items; ++i)
3449 av_push (av, SvREFCNT_inc_NN (ST (i))); 3586 av_push (av, SvREFCNT_inc_NN (ST (i)));
3450 3587
3451 if ((SV *)hv == &PL_sv_undef) 3588 if ((SV *)hv == &PL_sv_undef)
3452 { 3589 {
3453 PUSHMARK (SP); 3590 SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1);
3454 EXTEND (SP, 2);
3455 PUSHs (sv_Coro);
3456 PUSHs ((SV *)cv_pool_handler);
3457 PUTBACK;
3458 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3459 SPAGAIN;
3460
3461 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3591 hv = (HV *)SvREFCNT_inc_NN (SvRV (sv));
3592 SvREFCNT_dec (sv);
3462 } 3593 }
3463 3594
3464 { 3595 {
3465 struct coro *coro = SvSTATE_hv (hv); 3596 struct coro *coro = SvSTATE_hv (hv);
3466 3597
3717 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3848 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3718 sv_setpv ((SV *)slf_cv, proto); 3849 sv_setpv ((SV *)slf_cv, proto);
3719 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3850 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3720} 3851}
3721 3852
3853MODULE = Coro::State PACKAGE = Coro::Select
3854
3855void
3856patch_pp_sselect ()
3857 CODE:
3858 if (!coro_old_pp_sselect)
3859 {
3860 coro_select_select = (SV *)get_cv ("Coro::Select::select", 0);
3861 coro_old_pp_sselect = PL_ppaddr [OP_SSELECT];
3862 PL_ppaddr [OP_SSELECT] = coro_pp_sselect;
3863 }
3864
3865void
3866unpatch_pp_sselect ()
3867 CODE:
3868 if (coro_old_pp_sselect)
3869 {
3870 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3871 coro_old_pp_sselect = 0;
3872 }
3873
3874

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines