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.382 by root, Thu Feb 10 08:06:27 2011 UTC

1/* this works around a bug in mingw32 providing a non-working setjmp */
2#define USE_NO_MINGW_SETJMP_TWO_ARGS
3
4#define NDEBUG 1
5
1#include "libcoro/coro.c" 6#include "libcoro/coro.c"
2 7
3#define PERL_NO_GET_CONTEXT 8#define PERL_NO_GET_CONTEXT
4#define PERL_EXT 9#define PERL_EXT
5 10
11#include "schmorp.h" 16#include "schmorp.h"
12 17
13#include <stdio.h> 18#include <stdio.h>
14#include <errno.h> 19#include <errno.h>
15#include <assert.h> 20#include <assert.h>
21
22#ifndef SVs_PADSTALE
23# define SVs_PADSTALE 0
24#endif
16 25
17#ifdef WIN32 26#ifdef WIN32
18# undef setjmp 27# undef setjmp
19# undef longjmp 28# undef longjmp
20# undef _exit 29# undef _exit
142static SV *sv_pool_size; 151static SV *sv_pool_size;
143static SV *sv_async_pool_idle; /* description string */ 152static SV *sv_async_pool_idle; /* description string */
144static AV *av_async_pool; /* idle pool */ 153static AV *av_async_pool; /* idle pool */
145static SV *sv_Coro; /* class string */ 154static SV *sv_Coro; /* class string */
146static CV *cv_pool_handler; 155static CV *cv_pool_handler;
147static CV *cv_coro_state_new;
148 156
149/* Coro::AnyEvent */ 157/* Coro::AnyEvent */
150static SV *sv_activity; 158static SV *sv_activity;
151 159
152/* enable processtime/realtime profiling */ 160/* enable processtime/realtime profiling */
251 259
252 /* on_enter/on_leave */ 260 /* on_enter/on_leave */
253 AV *on_enter; 261 AV *on_enter;
254 AV *on_leave; 262 AV *on_leave;
255 263
264 /* swap_sv */
265 AV *swap_sv;
266
256 /* times */ 267 /* times */
257 coro_ts t_cpu, t_real; 268 coro_ts t_cpu, t_real;
258 269
259 /* linked list */ 270 /* linked list */
260 struct coro *next, *prev; 271 struct coro *next, *prev;
282static SV *coro_readyhook; 293static SV *coro_readyhook;
283static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 294static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
284static CV *cv_coro_run, *cv_coro_terminate; 295static CV *cv_coro_run, *cv_coro_terminate;
285static struct coro *coro_first; 296static struct coro *coro_first;
286#define coro_nready coroapi.nready 297#define coro_nready coroapi.nready
298
299/** Coro::Select ************************************************************/
300
301static OP *(*coro_old_pp_sselect) (pTHX);
302static SV *coro_select_select;
303
304/* horrible hack, but if it works... */
305static OP *
306coro_pp_sselect (pTHX)
307{
308 dSP;
309 PUSHMARK (SP - 4); /* fake argument list */
310 XPUSHs (coro_select_select);
311 PUTBACK;
312
313 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
314 PL_op->op_flags |= OPf_STACKED;
315 PL_op->op_private = 0;
316 return PL_ppaddr [OP_ENTERSUB](aTHX);
317}
287 318
288/** lowlevel stuff **********************************************************/ 319/** lowlevel stuff **********************************************************/
289 320
290static SV * 321static SV *
291coro_get_sv (pTHX_ const char *name, int create) 322coro_get_sv (pTHX_ const char *name, int create)
531 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 562 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
532} 563}
533 564
534/** load & save, init *******************************************************/ 565/** load & save, init *******************************************************/
535 566
567/* swap sv heads, at least logically */
568static void
569swap_svs (pTHX_ Coro__State c)
570{
571 int i;
572
573 for (i = 0; i <= AvFILLp (c->swap_sv); )
574 {
575 SV *a = AvARRAY (c->swap_sv)[i++];
576 SV *b = AvARRAY (c->swap_sv)[i++];
577
578 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
579 SV tmp;
580
581 /* swap sv_any */
582 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
583
584 /* swap sv_flags */
585 SvFLAGS (&tmp) = SvFLAGS (a);
586 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
587 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
588
589#if PERL_VERSION_ATLEAST (5,10,0)
590 /* perl 5.10 complicates this _quite_ a bit, but it also is
591 * is much faster, so no quarrels here. alternatively, we could
592 * sv_upgrade to avoid this.
593 */
594 {
595 /* swap sv_u */
596 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
597
598 /* if SvANY points to the head, we need to adjust the pointers,
599 * as the pointer for a still points to b, and maybe vice versa.
600 */
601 #define svany_in_head(type) \
602 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
603
604 if (svany_in_head (SvTYPE (a)))
605 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
606
607 if (svany_in_head (SvTYPE (b)))
608 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
609 }
610#endif
611 }
612}
613
614#define SWAP_SVS(coro) \
615 if (expect_false ((coro)->swap_sv)) \
616 swap_svs (aTHX_ (coro))
617
536static void 618static void
537on_enterleave_call (pTHX_ SV *cb); 619on_enterleave_call (pTHX_ SV *cb);
538 620
539static void 621static void
540load_perl (pTHX_ Coro__State c) 622load_perl (pTHX_ Coro__State c)
586 int i; 668 int i;
587 669
588 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 670 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
589 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 671 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
590 } 672 }
673
674 SWAP_SVS (c);
591} 675}
592 676
593static void 677static void
594save_perl (pTHX_ Coro__State c) 678save_perl (pTHX_ Coro__State c)
595{ 679{
680 SWAP_SVS (c);
681
596 if (expect_false (c->on_leave)) 682 if (expect_false (c->on_leave))
597 { 683 {
598 int i; 684 int i;
599 685
600 for (i = AvFILLp (c->on_leave); i >= 0; --i) 686 for (i = AvFILLp (c->on_leave); i >= 0; --i)
907slf_check_repeat (pTHX_ struct CoroSLF *frame) 993slf_check_repeat (pTHX_ struct CoroSLF *frame)
908{ 994{
909 return 1; 995 return 1;
910} 996}
911 997
912static UNOP coro_setup_op; 998static UNOP init_perl_op;
913 999
914static void NOINLINE /* noinline to keep it out of the transfer fast path */ 1000static void NOINLINE /* noinline to keep it out of the transfer fast path */
915coro_setup (pTHX_ struct coro *coro) 1001init_perl (pTHX_ struct coro *coro)
916{ 1002{
917 /* 1003 /*
918 * emulate part of the perl startup here. 1004 * emulate part of the perl startup here.
919 */ 1005 */
920 coro_init_stacks (aTHX); 1006 coro_init_stacks (aTHX);
969 */ 1055 */
970 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 1056 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 */ 1057 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
972 1058
973 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 1059 /* 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; 1060 init_perl_op.op_next = PL_op;
975 coro_setup_op.op_type = OP_ENTERSUB; 1061 init_perl_op.op_type = OP_ENTERSUB;
976 coro_setup_op.op_ppaddr = pp_slf; 1062 init_perl_op.op_ppaddr = pp_slf;
977 /* no flags etc. required, as an init function won't be called */ 1063 /* no flags etc. required, as an init function won't be called */
978 1064
979 PL_op = (OP *)&coro_setup_op; 1065 PL_op = (OP *)&init_perl_op;
980 1066
981 /* copy throw, in case it was set before coro_setup */ 1067 /* copy throw, in case it was set before init_perl */
982 CORO_THROW = coro->except; 1068 CORO_THROW = coro->except;
1069
1070 SWAP_SVS (coro);
983 1071
984 if (expect_false (enable_times)) 1072 if (expect_false (enable_times))
985 { 1073 {
986 coro_times_update (); 1074 coro_times_update ();
987 coro_times_sub (coro); 1075 coro_times_sub (coro);
1008 dounwind (-1); 1096 dounwind (-1);
1009 } 1097 }
1010} 1098}
1011 1099
1012static void 1100static void
1013coro_destruct_perl (pTHX_ struct coro *coro) 1101destroy_perl (pTHX_ struct coro *coro)
1014{ 1102{
1015 SV *svf [9]; 1103 SV *svf [9];
1016 1104
1017 { 1105 {
1018 struct coro *current = SvSTATE_current; 1106 struct coro *current = SvSTATE_current;
1022 save_perl (aTHX_ current); 1110 save_perl (aTHX_ current);
1023 load_perl (aTHX_ coro); 1111 load_perl (aTHX_ coro);
1024 1112
1025 coro_unwind_stacks (aTHX); 1113 coro_unwind_stacks (aTHX);
1026 coro_destruct_stacks (aTHX); 1114 coro_destruct_stacks (aTHX);
1115
1116 /* restore swapped sv's */
1117 SWAP_SVS (coro);
1027 1118
1028 // now save some sv's to be free'd later 1119 // now save some sv's to be free'd later
1029 svf [0] = GvSV (PL_defgv); 1120 svf [0] = GvSV (PL_defgv);
1030 svf [1] = (SV *)GvAV (PL_defgv); 1121 svf [1] = (SV *)GvAV (PL_defgv);
1031 svf [2] = GvSV (PL_errgv); 1122 svf [2] = GvSV (PL_errgv);
1255 /* somebody or something will hit me for both perl_run and PL_restartop */ 1346 /* somebody or something will hit me for both perl_run and PL_restartop */
1256 PL_restartop = PL_op; 1347 PL_restartop = PL_op;
1257 perl_run (PL_curinterp); 1348 perl_run (PL_curinterp);
1258 /* 1349 /*
1259 * Unfortunately, there is no way to get at the return values of the 1350 * Unfortunately, there is no way to get at the return values of the
1260 * coro body here, as perl_run destroys these 1351 * coro body here, as perl_run destroys these. Likewise, we cannot catch
1352 * runtime errors here, as this is just a random interpreter, not a thread.
1261 */ 1353 */
1262 1354
1263 /* 1355 /*
1264 * If perl-run returns we assume exit() was being called or the coro 1356 * 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) 1357 * fell off the end, which seems to be the only valid (non-bug)
1472 if (expect_false (next->flags & CF_NEW)) 1564 if (expect_false (next->flags & CF_NEW))
1473 { 1565 {
1474 /* need to start coroutine */ 1566 /* need to start coroutine */
1475 next->flags &= ~CF_NEW; 1567 next->flags &= ~CF_NEW;
1476 /* setup coroutine call */ 1568 /* setup coroutine call */
1477 coro_setup (aTHX_ next); 1569 init_perl (aTHX_ next);
1478 } 1570 }
1479 else 1571 else
1480 load_perl (aTHX_ next); 1572 load_perl (aTHX_ next);
1481 1573
1482 /* possibly untie and reuse the cctx */ 1574 /* possibly untie and reuse the cctx */
1545 1637
1546 if (coro->mainstack 1638 if (coro->mainstack
1547 && coro->mainstack != main_mainstack 1639 && coro->mainstack != main_mainstack
1548 && coro->slot 1640 && coro->slot
1549 && !PL_dirty) 1641 && !PL_dirty)
1550 coro_destruct_perl (aTHX_ coro); 1642 destroy_perl (aTHX_ coro);
1643
1644 if (coro->next) coro->next->prev = coro->prev;
1645 if (coro->prev) coro->prev->next = coro->next;
1646 if (coro == coro_first) coro_first = coro->next;
1551 1647
1552 cctx_destroy (coro->cctx); 1648 cctx_destroy (coro->cctx);
1553 SvREFCNT_dec (coro->startcv); 1649 SvREFCNT_dec (coro->startcv);
1554 SvREFCNT_dec (coro->args); 1650 SvREFCNT_dec (coro->args);
1651 SvREFCNT_dec (coro->swap_sv);
1555 SvREFCNT_dec (CORO_THROW); 1652 SvREFCNT_dec (CORO_THROW);
1556
1557 if (coro->next) coro->next->prev = coro->prev;
1558 if (coro->prev) coro->prev->next = coro->next;
1559 if (coro == coro_first) coro_first = coro->next;
1560 1653
1561 return 1; 1654 return 1;
1562} 1655}
1563 1656
1564static int 1657static int
1648 } 1741 }
1649 1742
1650 return 0; 1743 return 0;
1651} 1744}
1652 1745
1746static void
1747invoke_sv_ready_hook_helper (void)
1748{
1749 dTHX;
1750 dSP;
1751
1752 ENTER;
1753 SAVETMPS;
1754
1755 PUSHMARK (SP);
1756 PUTBACK;
1757 call_sv (coro_readyhook, G_VOID | G_DISCARD);
1758
1759 FREETMPS;
1760 LEAVE;
1761}
1762
1653static int 1763static int
1654api_ready (pTHX_ SV *coro_sv) 1764api_ready (pTHX_ SV *coro_sv)
1655{ 1765{
1656 struct coro *coro;
1657 SV *sv_hook;
1658 void (*xs_hook)(void);
1659
1660 coro = SvSTATE (coro_sv); 1766 struct coro *coro = SvSTATE (coro_sv);
1661 1767
1662 if (coro->flags & CF_READY) 1768 if (coro->flags & CF_READY)
1663 return 0; 1769 return 0;
1664 1770
1665 coro->flags |= CF_READY; 1771 coro->flags |= CF_READY;
1666 1772
1667 sv_hook = coro_nready ? 0 : coro_readyhook;
1668 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1669
1670 coro_enq (aTHX_ coro); 1773 coro_enq (aTHX_ coro);
1671 ++coro_nready;
1672 1774
1673 if (sv_hook) 1775 if (!coro_nready++)
1674 { 1776 if (coroapi.readyhook)
1675 dSP; 1777 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 1778
1691 return 1; 1779 return 1;
1692} 1780}
1693 1781
1694static int 1782static int
1739 { 1827 {
1740 /* nothing to schedule: call the idle handler */ 1828 /* nothing to schedule: call the idle handler */
1741 if (SvROK (sv_idle) 1829 if (SvROK (sv_idle)
1742 && SvOBJECT (SvRV (sv_idle))) 1830 && SvOBJECT (SvRV (sv_idle)))
1743 { 1831 {
1832 if (SvRV (sv_idle) == SvRV (coro_current))
1833 croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught");
1834
1744 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1835 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1745 api_ready (aTHX_ SvRV (sv_idle)); 1836 api_ready (aTHX_ SvRV (sv_idle));
1746 --coro_nready; 1837 --coro_nready;
1747 } 1838 }
1748 else 1839 else
1749 { 1840 {
1841 /* TODO: deprecated, remove, cannot work reliably *//*D*/
1750 dSP; 1842 dSP;
1751 1843
1752 ENTER; 1844 ENTER;
1753 SAVETMPS; 1845 SAVETMPS;
1754 1846
1900{ 1992{
1901 int i; 1993 int i;
1902 HV *hv = (HV *)SvRV (coro_current); 1994 HV *hv = (HV *)SvRV (coro_current);
1903 AV *av = newAV (); 1995 AV *av = newAV ();
1904 1996
1997 /* items are actually not so common, so optimise for this case */
1998 if (items)
1999 {
1905 av_extend (av, items - 1); 2000 av_extend (av, items - 1);
2001
1906 for (i = 0; i < items; ++i) 2002 for (i = 0; i < items; ++i)
1907 av_push (av, SvREFCNT_inc_NN (arg [i])); 2003 av_push (av, SvREFCNT_inc_NN (arg [i]));
2004 }
1908 2005
1909 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); 2006 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1910 2007
1911 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 2008 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1912 api_ready (aTHX_ sv_manager); 2009 api_ready (aTHX_ sv_manager);
2328 if (PL_op->op_flags & OPf_STACKED) 2425 if (PL_op->op_flags & OPf_STACKED)
2329 { 2426 {
2330 if (items > slf_arga) 2427 if (items > slf_arga)
2331 { 2428 {
2332 slf_arga = items; 2429 slf_arga = items;
2333 free (slf_argv); 2430 Safefree (slf_argv);
2334 slf_argv = malloc (slf_arga * sizeof (SV *)); 2431 New (0, slf_argv, slf_arga, SV *);
2335 } 2432 }
2336 2433
2337 slf_argc = items; 2434 slf_argc = items;
2338 2435
2339 for (i = 0; i < items; ++i) 2436 for (i = 0; i < items; ++i)
2700 { 2797 {
2701 SV *cb_cv = s_get_cv_croak (arg [1]); 2798 SV *cb_cv = s_get_cv_croak (arg [1]);
2702 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2799 av_push (av, SvREFCNT_inc_NN (cb_cv));
2703 2800
2704 if (SvIVX (AvARRAY (av)[0])) 2801 if (SvIVX (AvARRAY (av)[0]))
2705 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2802 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2706 2803
2707 frame->prepare = prepare_nop; 2804 frame->prepare = prepare_nop;
2708 frame->check = slf_check_nop; 2805 frame->check = slf_check_nop;
2709 } 2806 }
2710 else if (SvIVX (AvARRAY (av)[0])) 2807 else if (SvIVX (AvARRAY (av)[0]))
2872 /* now call the AIO function - we assume our request is uncancelable */ 2969 /* now call the AIO function - we assume our request is uncancelable */
2873 PUTBACK; 2970 PUTBACK;
2874 call_sv ((SV *)req, G_VOID | G_DISCARD); 2971 call_sv ((SV *)req, G_VOID | G_DISCARD);
2875 } 2972 }
2876 2973
2877 /* now that the requets is going, we loop toll we have a result */ 2974 /* now that the request is going, we loop till we have a result */
2878 frame->data = (void *)state; 2975 frame->data = (void *)state;
2879 frame->prepare = prepare_schedule; 2976 frame->prepare = prepare_schedule;
2880 frame->check = slf_check_aio_req; 2977 frame->check = slf_check_aio_req;
2881} 2978}
2882 2979
2893/*****************************************************************************/ 2990/*****************************************************************************/
2894 2991
2895#if CORO_CLONE 2992#if CORO_CLONE
2896# include "clone.c" 2993# include "clone.c"
2897#endif 2994#endif
2995
2996/*****************************************************************************/
2997
2998static SV *
2999coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro)
3000{
3001 SV *coro_sv;
3002 struct coro *coro;
3003 MAGIC *mg;
3004 HV *hv;
3005 SV *cb;
3006 int i;
3007
3008 if (argc > 0)
3009 {
3010 cb = s_get_cv_croak (argv [0]);
3011
3012 if (!is_coro)
3013 {
3014 if (CvISXSUB (cb))
3015 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3016
3017 if (!CvROOT (cb))
3018 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
3019 }
3020 }
3021
3022 Newz (0, coro, 1, struct coro);
3023 coro->args = newAV ();
3024 coro->flags = CF_NEW;
3025
3026 if (coro_first) coro_first->prev = coro;
3027 coro->next = coro_first;
3028 coro_first = coro;
3029
3030 coro->hv = hv = newHV ();
3031 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3032 mg->mg_flags |= MGf_DUP;
3033 coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash);
3034
3035 if (argc > 0)
3036 {
3037 av_extend (coro->args, argc + is_coro - 1);
3038
3039 if (is_coro)
3040 {
3041 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3042 cb = (SV *)cv_coro_run;
3043 }
3044
3045 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3046
3047 for (i = 1; i < argc; i++)
3048 av_push (coro->args, newSVsv (argv [i]));
3049 }
3050
3051 return coro_sv;
3052}
2898 3053
2899MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3054MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2900 3055
2901PROTOTYPES: DISABLE 3056PROTOTYPES: DISABLE
2902 3057
2971 3126
2972 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3127 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
2973} 3128}
2974 3129
2975SV * 3130SV *
2976new (char *klass, ...) 3131new (SV *klass, ...)
2977 ALIAS: 3132 ALIAS:
2978 Coro::new = 1 3133 Coro::new = 1
2979 CODE: 3134 CODE:
2980{ 3135 RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix);
2981 struct coro *coro; 3136 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 3137 RETVAL
3032 3138
3033void 3139void
3034transfer (...) 3140transfer (...)
3035 PROTOTYPE: $$ 3141 PROTOTYPE: $$
3172 RETVAL = boolSV (coro->flags & ix); 3278 RETVAL = boolSV (coro->flags & ix);
3173 OUTPUT: 3279 OUTPUT:
3174 RETVAL 3280 RETVAL
3175 3281
3176void 3282void
3177throw (Coro::State self, SV *throw = &PL_sv_undef) 3283throw (Coro::State self, SV *exception = &PL_sv_undef)
3178 PROTOTYPE: $;$ 3284 PROTOTYPE: $;$
3179 CODE: 3285 CODE:
3180{ 3286{
3181 struct coro *current = SvSTATE_current; 3287 struct coro *current = SvSTATE_current;
3182 SV **throwp = self == current ? &CORO_THROW : &self->except; 3288 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3183 SvREFCNT_dec (*throwp); 3289 SvREFCNT_dec (*exceptionp);
3184 SvGETMAGIC (throw); 3290 SvGETMAGIC (exception);
3185 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3291 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3186} 3292}
3187 3293
3188void 3294void
3189api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3295api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3190 PROTOTYPE: $;$ 3296 PROTOTYPE: $;$
3285 3391
3286 if (expect_false (current == self)) 3392 if (expect_false (current == self))
3287 coro_times_sub (SvSTATE (coro_current)); 3393 coro_times_sub (SvSTATE (coro_current));
3288} 3394}
3289 3395
3396void
3397swap_sv (Coro::State coro, SV *sv, SV *swapsv)
3398 CODE:
3399{
3400 struct coro *current = SvSTATE_current;
3401
3402 if (current == coro)
3403 SWAP_SVS (current);
3404
3405 if (!coro->swap_sv)
3406 coro->swap_sv = newAV ();
3407
3408 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv )));
3409 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv)));
3410
3411 if (current == coro)
3412 SWAP_SVS (current);
3413}
3414
3415
3290MODULE = Coro::State PACKAGE = Coro 3416MODULE = Coro::State PACKAGE = Coro
3291 3417
3292BOOT: 3418BOOT:
3293{ 3419{
3294 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3420 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3302 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3428 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3303 3429
3304 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3430 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); 3431 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); 3432 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); 3433 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3308 3434
3309 coro_stash = gv_stashpv ("Coro", TRUE); 3435 coro_stash = gv_stashpv ("Coro", TRUE);
3310 3436
3311 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3437 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3312 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3438 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3313 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3439 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3331 sv_setiv (sv, (IV)&coroapi); 3457 sv_setiv (sv, (IV)&coroapi);
3332 SvREADONLY_on (sv); 3458 SvREADONLY_on (sv);
3333 } 3459 }
3334} 3460}
3335 3461
3462SV *
3463async (...)
3464 PROTOTYPE: &@
3465 CODE:
3466 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1);
3467 api_ready (aTHX_ RETVAL);
3468 OUTPUT:
3469 RETVAL
3470
3336void 3471void
3337terminate (...) 3472terminate (...)
3338 CODE: 3473 CODE:
3339 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3474 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3340 3475
3374_set_readyhook (SV *hook) 3509_set_readyhook (SV *hook)
3375 PROTOTYPE: $ 3510 PROTOTYPE: $
3376 CODE: 3511 CODE:
3377 SvREFCNT_dec (coro_readyhook); 3512 SvREFCNT_dec (coro_readyhook);
3378 SvGETMAGIC (hook); 3513 SvGETMAGIC (hook);
3514 if (SvOK (hook))
3515 {
3379 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3516 coro_readyhook = newSVsv (hook);
3517 CORO_READYHOOK = invoke_sv_ready_hook_helper;
3518 }
3519 else
3520 {
3521 coro_readyhook = 0;
3522 CORO_READYHOOK = 0;
3523 }
3380 3524
3381int 3525int
3382prio (Coro::State coro, int newprio = 0) 3526prio (Coro::State coro, int newprio = 0)
3383 PROTOTYPE: $;$ 3527 PROTOTYPE: $;$
3384 ALIAS: 3528 ALIAS:
3448 for (i = 1; i < items; ++i) 3592 for (i = 1; i < items; ++i)
3449 av_push (av, SvREFCNT_inc_NN (ST (i))); 3593 av_push (av, SvREFCNT_inc_NN (ST (i)));
3450 3594
3451 if ((SV *)hv == &PL_sv_undef) 3595 if ((SV *)hv == &PL_sv_undef)
3452 { 3596 {
3453 PUSHMARK (SP); 3597 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)); 3598 hv = (HV *)SvREFCNT_inc_NN (SvRV (sv));
3599 SvREFCNT_dec (sv);
3462 } 3600 }
3463 3601
3464 { 3602 {
3465 struct coro *coro = SvSTATE_hv (hv); 3603 struct coro *coro = SvSTATE_hv (hv);
3466 3604
3717 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3855 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3718 sv_setpv ((SV *)slf_cv, proto); 3856 sv_setpv ((SV *)slf_cv, proto);
3719 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3857 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3720} 3858}
3721 3859
3860MODULE = Coro::State PACKAGE = Coro::Select
3861
3862void
3863patch_pp_sselect ()
3864 CODE:
3865 if (!coro_old_pp_sselect)
3866 {
3867 coro_select_select = (SV *)get_cv ("Coro::Select::select", 0);
3868 coro_old_pp_sselect = PL_ppaddr [OP_SSELECT];
3869 PL_ppaddr [OP_SSELECT] = coro_pp_sselect;
3870 }
3871
3872void
3873unpatch_pp_sselect ()
3874 CODE:
3875 if (coro_old_pp_sselect)
3876 {
3877 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3878 coro_old_pp_sselect = 0;
3879 }
3880

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines