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.390 by root, Thu Mar 31 01:36:58 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
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>
16 21
17#ifdef WIN32 22#ifndef SVs_PADSTALE
23# define SVs_PADSTALE 0
24#endif
25
26#if defined(_WIN32)
27# undef HAS_GETTIMEOFDAY
18# undef setjmp 28# undef setjmp
19# undef longjmp 29# undef longjmp
20# undef _exit 30# undef _exit
21# define setjmp _setjmp /* deep magic */ 31# define setjmp _setjmp /* deep magic */
22#else 32#else
142static SV *sv_pool_size; 152static SV *sv_pool_size;
143static SV *sv_async_pool_idle; /* description string */ 153static SV *sv_async_pool_idle; /* description string */
144static AV *av_async_pool; /* idle pool */ 154static AV *av_async_pool; /* idle pool */
145static SV *sv_Coro; /* class string */ 155static SV *sv_Coro; /* class string */
146static CV *cv_pool_handler; 156static CV *cv_pool_handler;
147static CV *cv_coro_state_new;
148 157
149/* Coro::AnyEvent */ 158/* Coro::AnyEvent */
150static SV *sv_activity; 159static SV *sv_activity;
151 160
152/* enable processtime/realtime profiling */ 161/* enable processtime/realtime profiling */
232 CV *startcv; /* the CV to execute */ 241 CV *startcv; /* the CV to execute */
233 AV *args; /* data associated with this coroutine (initial args) */ 242 AV *args; /* data associated with this coroutine (initial args) */
234 int refcnt; /* coroutines are refcounted, yes */ 243 int refcnt; /* coroutines are refcounted, yes */
235 int flags; /* CF_ flags */ 244 int flags; /* CF_ flags */
236 HV *hv; /* the perl hash associated with this coro, if any */ 245 HV *hv; /* the perl hash associated with this coro, if any */
237 void (*on_destroy)(pTHX_ struct coro *coro); 246 void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */
238 247
239 /* statistics */ 248 /* statistics */
240 int usecount; /* number of transfers to this coro */ 249 int usecount; /* number of transfers to this coro */
241 250
242 /* coro process data */ 251 /* coro process data */
250 AV *invoke_av; 259 AV *invoke_av;
251 260
252 /* on_enter/on_leave */ 261 /* on_enter/on_leave */
253 AV *on_enter; 262 AV *on_enter;
254 AV *on_leave; 263 AV *on_leave;
264
265 /* swap_sv */
266 AV *swap_sv;
255 267
256 /* times */ 268 /* times */
257 coro_ts t_cpu, t_real; 269 coro_ts t_cpu, t_real;
258 270
259 /* linked list */ 271 /* linked list */
282static SV *coro_readyhook; 294static SV *coro_readyhook;
283static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 295static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
284static CV *cv_coro_run, *cv_coro_terminate; 296static CV *cv_coro_run, *cv_coro_terminate;
285static struct coro *coro_first; 297static struct coro *coro_first;
286#define coro_nready coroapi.nready 298#define coro_nready coroapi.nready
299
300/** Coro::Select ************************************************************/
301
302static OP *(*coro_old_pp_sselect) (pTHX);
303static SV *coro_select_select;
304
305/* horrible hack, but if it works... */
306static OP *
307coro_pp_sselect (pTHX)
308{
309 dSP;
310 PUSHMARK (SP - 4); /* fake argument list */
311 XPUSHs (coro_select_select);
312 PUTBACK;
313
314 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
315 PL_op->op_flags |= OPf_STACKED;
316 PL_op->op_private = 0;
317 return PL_ppaddr [OP_ENTERSUB](aTHX);
318}
319
320/** time stuff **************************************************************/
321
322#ifdef HAS_GETTIMEOFDAY
323
324static void
325coro_u2time (pTHX_ UV ret[2])
326{
327 struct timeval tv;
328 gettimeofday (&tv, 0);
329
330 ret [0] = tv.tv_sec;
331 ret [1] = tv.tv_usec;
332}
333
334static double
335coro_nvtime ()
336{
337 struct timeval tv;
338 gettimeofday (&tv, 0);
339
340 return tv.tv_sec + tv.tv_usec * 1e-6;
341}
342
343static void
344time_init (pTHX)
345{
346 nvtime = coro_nvtime;
347 u2time = coro_u2time;
348}
349
350#else
351
352static void
353time_init (pTHX)
354{
355 SV **svp;
356
357 require_pv ("Time/HiRes.pm");
358
359 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
360
361 if (!svp) croak ("Time::HiRes is required, but missing.");
362 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
363
364 nvtime = INT2PTR (double (*)(), SvIV (*svp));
365
366 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
367 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
368}
369
370#endif
287 371
288/** lowlevel stuff **********************************************************/ 372/** lowlevel stuff **********************************************************/
289 373
290static SV * 374static SV *
291coro_get_sv (pTHX_ const char *name, int create) 375coro_get_sv (pTHX_ const char *name, int create)
531 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 615 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
532} 616}
533 617
534/** load & save, init *******************************************************/ 618/** load & save, init *******************************************************/
535 619
620/* swap sv heads, at least logically */
621static void
622swap_svs (pTHX_ Coro__State c)
623{
624 int i;
625
626 for (i = 0; i <= AvFILLp (c->swap_sv); )
627 {
628 SV *a = AvARRAY (c->swap_sv)[i++];
629 SV *b = AvARRAY (c->swap_sv)[i++];
630
631 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
632 SV tmp;
633
634 /* swap sv_any */
635 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
636
637 /* swap sv_flags */
638 SvFLAGS (&tmp) = SvFLAGS (a);
639 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
640 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
641
642#if PERL_VERSION_ATLEAST (5,10,0)
643 /* perl 5.10 complicates this _quite_ a bit, but it also is
644 * much faster, so no quarrels here. alternatively, we could
645 * sv_upgrade to avoid this.
646 */
647 {
648 /* swap sv_u */
649 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
650
651 /* if SvANY points to the head, we need to adjust the pointers,
652 * as the pointer for a still points to b, and maybe vice versa.
653 */
654 #define svany_in_head(type) \
655 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
656
657 if (svany_in_head (SvTYPE (a)))
658 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
659
660 if (svany_in_head (SvTYPE (b)))
661 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
662 }
663#endif
664 }
665}
666
667#define SWAP_SVS(coro) \
668 if (expect_false ((coro)->swap_sv)) \
669 swap_svs (aTHX_ (coro))
670
536static void 671static void
537on_enterleave_call (pTHX_ SV *cb); 672on_enterleave_call (pTHX_ SV *cb);
538 673
539static void 674static void
540load_perl (pTHX_ Coro__State c) 675load_perl (pTHX_ Coro__State c)
586 int i; 721 int i;
587 722
588 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 723 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
589 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 724 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
590 } 725 }
726
727 SWAP_SVS (c);
591} 728}
592 729
593static void 730static void
594save_perl (pTHX_ Coro__State c) 731save_perl (pTHX_ Coro__State c)
595{ 732{
733 SWAP_SVS (c);
734
596 if (expect_false (c->on_leave)) 735 if (expect_false (c->on_leave))
597 { 736 {
598 int i; 737 int i;
599 738
600 for (i = AvFILLp (c->on_leave); i >= 0; --i) 739 for (i = AvFILLp (c->on_leave); i >= 0; --i)
907slf_check_repeat (pTHX_ struct CoroSLF *frame) 1046slf_check_repeat (pTHX_ struct CoroSLF *frame)
908{ 1047{
909 return 1; 1048 return 1;
910} 1049}
911 1050
912static UNOP coro_setup_op; 1051static UNOP init_perl_op;
913 1052
914static void NOINLINE /* noinline to keep it out of the transfer fast path */ 1053static void NOINLINE /* noinline to keep it out of the transfer fast path */
915coro_setup (pTHX_ struct coro *coro) 1054init_perl (pTHX_ struct coro *coro)
916{ 1055{
917 /* 1056 /*
918 * emulate part of the perl startup here. 1057 * emulate part of the perl startup here.
919 */ 1058 */
920 coro_init_stacks (aTHX); 1059 coro_init_stacks (aTHX);
927 PL_comppad_name_fill = 0; 1066 PL_comppad_name_fill = 0;
928 PL_comppad_name_floor = 0; 1067 PL_comppad_name_floor = 0;
929 PL_curpm = 0; 1068 PL_curpm = 0;
930 PL_curpad = 0; 1069 PL_curpad = 0;
931 PL_localizing = 0; 1070 PL_localizing = 0;
932 PL_dirty = 0;
933 PL_restartop = 0; 1071 PL_restartop = 0;
934#if PERL_VERSION_ATLEAST (5,10,0) 1072#if PERL_VERSION_ATLEAST (5,10,0)
935 PL_parser = 0; 1073 PL_parser = 0;
936#endif 1074#endif
937 PL_hints = 0; 1075 PL_hints = 0;
969 */ 1107 */
970 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 1108 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 */ 1109 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
972 1110
973 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 1111 /* 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; 1112 init_perl_op.op_next = PL_op;
975 coro_setup_op.op_type = OP_ENTERSUB; 1113 init_perl_op.op_type = OP_ENTERSUB;
976 coro_setup_op.op_ppaddr = pp_slf; 1114 init_perl_op.op_ppaddr = pp_slf;
977 /* no flags etc. required, as an init function won't be called */ 1115 /* no flags etc. required, as an init function won't be called */
978 1116
979 PL_op = (OP *)&coro_setup_op; 1117 PL_op = (OP *)&init_perl_op;
980 1118
981 /* copy throw, in case it was set before coro_setup */ 1119 /* copy throw, in case it was set before init_perl */
982 CORO_THROW = coro->except; 1120 CORO_THROW = coro->except;
1121
1122 SWAP_SVS (coro);
983 1123
984 if (expect_false (enable_times)) 1124 if (expect_false (enable_times))
985 { 1125 {
986 coro_times_update (); 1126 coro_times_update ();
987 coro_times_sub (coro); 1127 coro_times_sub (coro);
1008 dounwind (-1); 1148 dounwind (-1);
1009 } 1149 }
1010} 1150}
1011 1151
1012static void 1152static void
1013coro_destruct_perl (pTHX_ struct coro *coro) 1153destroy_perl (pTHX_ struct coro *coro)
1014{ 1154{
1015 SV *svf [9]; 1155 SV *svf [9];
1016 1156
1017 { 1157 {
1018 struct coro *current = SvSTATE_current; 1158 struct coro *current = SvSTATE_current;
1022 save_perl (aTHX_ current); 1162 save_perl (aTHX_ current);
1023 load_perl (aTHX_ coro); 1163 load_perl (aTHX_ coro);
1024 1164
1025 coro_unwind_stacks (aTHX); 1165 coro_unwind_stacks (aTHX);
1026 coro_destruct_stacks (aTHX); 1166 coro_destruct_stacks (aTHX);
1167
1168 /* restore swapped sv's */
1169 SWAP_SVS (coro);
1027 1170
1028 // now save some sv's to be free'd later 1171 // now save some sv's to be free'd later
1029 svf [0] = GvSV (PL_defgv); 1172 svf [0] = GvSV (PL_defgv);
1030 svf [1] = (SV *)GvAV (PL_defgv); 1173 svf [1] = (SV *)GvAV (PL_defgv);
1031 svf [2] = GvSV (PL_errgv); 1174 svf [2] = GvSV (PL_errgv);
1255 /* somebody or something will hit me for both perl_run and PL_restartop */ 1398 /* somebody or something will hit me for both perl_run and PL_restartop */
1256 PL_restartop = PL_op; 1399 PL_restartop = PL_op;
1257 perl_run (PL_curinterp); 1400 perl_run (PL_curinterp);
1258 /* 1401 /*
1259 * Unfortunately, there is no way to get at the return values of the 1402 * Unfortunately, there is no way to get at the return values of the
1260 * coro body here, as perl_run destroys these 1403 * coro body here, as perl_run destroys these. Likewise, we cannot catch
1404 * runtime errors here, as this is just a random interpreter, not a thread.
1261 */ 1405 */
1262 1406
1263 /* 1407 /*
1264 * If perl-run returns we assume exit() was being called or the coro 1408 * 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) 1409 * fell off the end, which seems to be the only valid (non-bug)
1472 if (expect_false (next->flags & CF_NEW)) 1616 if (expect_false (next->flags & CF_NEW))
1473 { 1617 {
1474 /* need to start coroutine */ 1618 /* need to start coroutine */
1475 next->flags &= ~CF_NEW; 1619 next->flags &= ~CF_NEW;
1476 /* setup coroutine call */ 1620 /* setup coroutine call */
1477 coro_setup (aTHX_ next); 1621 init_perl (aTHX_ next);
1478 } 1622 }
1479 else 1623 else
1480 load_perl (aTHX_ next); 1624 load_perl (aTHX_ next);
1481 1625
1482 /* possibly untie and reuse the cctx */ 1626 /* possibly untie and reuse the cctx */
1545 1689
1546 if (coro->mainstack 1690 if (coro->mainstack
1547 && coro->mainstack != main_mainstack 1691 && coro->mainstack != main_mainstack
1548 && coro->slot 1692 && coro->slot
1549 && !PL_dirty) 1693 && !PL_dirty)
1550 coro_destruct_perl (aTHX_ coro); 1694 destroy_perl (aTHX_ coro);
1695
1696 if (coro->next) coro->next->prev = coro->prev;
1697 if (coro->prev) coro->prev->next = coro->next;
1698 if (coro == coro_first) coro_first = coro->next;
1551 1699
1552 cctx_destroy (coro->cctx); 1700 cctx_destroy (coro->cctx);
1553 SvREFCNT_dec (coro->startcv); 1701 SvREFCNT_dec (coro->startcv);
1554 SvREFCNT_dec (coro->args); 1702 SvREFCNT_dec (coro->args);
1703 SvREFCNT_dec (coro->swap_sv);
1555 SvREFCNT_dec (CORO_THROW); 1704 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 1705
1561 return 1; 1706 return 1;
1562} 1707}
1563 1708
1564static int 1709static int
1648 } 1793 }
1649 1794
1650 return 0; 1795 return 0;
1651} 1796}
1652 1797
1798static void
1799invoke_sv_ready_hook_helper (void)
1800{
1801 dTHX;
1802 dSP;
1803
1804 ENTER;
1805 SAVETMPS;
1806
1807 PUSHMARK (SP);
1808 PUTBACK;
1809 call_sv (coro_readyhook, G_VOID | G_DISCARD);
1810
1811 FREETMPS;
1812 LEAVE;
1813}
1814
1653static int 1815static int
1654api_ready (pTHX_ SV *coro_sv) 1816api_ready (pTHX_ SV *coro_sv)
1655{ 1817{
1656 struct coro *coro;
1657 SV *sv_hook;
1658 void (*xs_hook)(void);
1659
1660 coro = SvSTATE (coro_sv); 1818 struct coro *coro = SvSTATE (coro_sv);
1661 1819
1662 if (coro->flags & CF_READY) 1820 if (coro->flags & CF_READY)
1663 return 0; 1821 return 0;
1664 1822
1665 coro->flags |= CF_READY; 1823 coro->flags |= CF_READY;
1666 1824
1667 sv_hook = coro_nready ? 0 : coro_readyhook;
1668 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1669
1670 coro_enq (aTHX_ coro); 1825 coro_enq (aTHX_ coro);
1671 ++coro_nready;
1672 1826
1673 if (sv_hook) 1827 if (!coro_nready++)
1674 { 1828 if (coroapi.readyhook)
1675 dSP; 1829 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 1830
1691 return 1; 1831 return 1;
1692} 1832}
1693 1833
1694static int 1834static int
1739 { 1879 {
1740 /* nothing to schedule: call the idle handler */ 1880 /* nothing to schedule: call the idle handler */
1741 if (SvROK (sv_idle) 1881 if (SvROK (sv_idle)
1742 && SvOBJECT (SvRV (sv_idle))) 1882 && SvOBJECT (SvRV (sv_idle)))
1743 { 1883 {
1884 if (SvRV (sv_idle) == SvRV (coro_current))
1885 croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught");
1886
1744 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1887 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1745 api_ready (aTHX_ SvRV (sv_idle)); 1888 api_ready (aTHX_ SvRV (sv_idle));
1746 --coro_nready; 1889 --coro_nready;
1747 } 1890 }
1748 else 1891 else
1749 { 1892 {
1893 /* TODO: deprecated, remove, cannot work reliably *//*D*/
1750 dSP; 1894 dSP;
1751 1895
1752 ENTER; 1896 ENTER;
1753 SAVETMPS; 1897 SAVETMPS;
1754 1898
1900{ 2044{
1901 int i; 2045 int i;
1902 HV *hv = (HV *)SvRV (coro_current); 2046 HV *hv = (HV *)SvRV (coro_current);
1903 AV *av = newAV (); 2047 AV *av = newAV ();
1904 2048
2049 /* items are actually not so common, so optimise for this case */
2050 if (items)
2051 {
1905 av_extend (av, items - 1); 2052 av_extend (av, items - 1);
2053
1906 for (i = 0; i < items; ++i) 2054 for (i = 0; i < items; ++i)
1907 av_push (av, SvREFCNT_inc_NN (arg [i])); 2055 av_push (av, SvREFCNT_inc_NN (arg [i]));
2056 }
1908 2057
1909 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); 2058 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1910 2059
1911 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 2060 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1912 api_ready (aTHX_ sv_manager); 2061 api_ready (aTHX_ sv_manager);
2328 if (PL_op->op_flags & OPf_STACKED) 2477 if (PL_op->op_flags & OPf_STACKED)
2329 { 2478 {
2330 if (items > slf_arga) 2479 if (items > slf_arga)
2331 { 2480 {
2332 slf_arga = items; 2481 slf_arga = items;
2333 free (slf_argv); 2482 Safefree (slf_argv);
2334 slf_argv = malloc (slf_arga * sizeof (SV *)); 2483 New (0, slf_argv, slf_arga, SV *);
2335 } 2484 }
2336 2485
2337 slf_argc = items; 2486 slf_argc = items;
2338 2487
2339 for (i = 0; i < items; ++i) 2488 for (i = 0; i < items; ++i)
2700 { 2849 {
2701 SV *cb_cv = s_get_cv_croak (arg [1]); 2850 SV *cb_cv = s_get_cv_croak (arg [1]);
2702 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2851 av_push (av, SvREFCNT_inc_NN (cb_cv));
2703 2852
2704 if (SvIVX (AvARRAY (av)[0])) 2853 if (SvIVX (AvARRAY (av)[0]))
2705 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2854 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2706 2855
2707 frame->prepare = prepare_nop; 2856 frame->prepare = prepare_nop;
2708 frame->check = slf_check_nop; 2857 frame->check = slf_check_nop;
2709 } 2858 }
2710 else if (SvIVX (AvARRAY (av)[0])) 2859 else if (SvIVX (AvARRAY (av)[0]))
2872 /* now call the AIO function - we assume our request is uncancelable */ 3021 /* now call the AIO function - we assume our request is uncancelable */
2873 PUTBACK; 3022 PUTBACK;
2874 call_sv ((SV *)req, G_VOID | G_DISCARD); 3023 call_sv ((SV *)req, G_VOID | G_DISCARD);
2875 } 3024 }
2876 3025
2877 /* now that the requets is going, we loop toll we have a result */ 3026 /* now that the request is going, we loop till we have a result */
2878 frame->data = (void *)state; 3027 frame->data = (void *)state;
2879 frame->prepare = prepare_schedule; 3028 frame->prepare = prepare_schedule;
2880 frame->check = slf_check_aio_req; 3029 frame->check = slf_check_aio_req;
2881} 3030}
2882 3031
2893/*****************************************************************************/ 3042/*****************************************************************************/
2894 3043
2895#if CORO_CLONE 3044#if CORO_CLONE
2896# include "clone.c" 3045# include "clone.c"
2897#endif 3046#endif
3047
3048/*****************************************************************************/
3049
3050static SV *
3051coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro)
3052{
3053 SV *coro_sv;
3054 struct coro *coro;
3055 MAGIC *mg;
3056 HV *hv;
3057 SV *cb;
3058 int i;
3059
3060 if (argc > 0)
3061 {
3062 cb = s_get_cv_croak (argv [0]);
3063
3064 if (!is_coro)
3065 {
3066 if (CvISXSUB (cb))
3067 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3068
3069 if (!CvROOT (cb))
3070 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
3071 }
3072 }
3073
3074 Newz (0, coro, 1, struct coro);
3075 coro->args = newAV ();
3076 coro->flags = CF_NEW;
3077
3078 if (coro_first) coro_first->prev = coro;
3079 coro->next = coro_first;
3080 coro_first = coro;
3081
3082 coro->hv = hv = newHV ();
3083 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3084 mg->mg_flags |= MGf_DUP;
3085 coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash);
3086
3087 if (argc > 0)
3088 {
3089 av_extend (coro->args, argc + is_coro - 1);
3090
3091 if (is_coro)
3092 {
3093 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3094 cb = (SV *)cv_coro_run;
3095 }
3096
3097 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3098
3099 for (i = 1; i < argc; i++)
3100 av_push (coro->args, newSVsv (argv [i]));
3101 }
3102
3103 return coro_sv;
3104}
2898 3105
2899MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3106MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2900 3107
2901PROTOTYPES: DISABLE 3108PROTOTYPES: DISABLE
2902 3109
2955 coroapi.prepare_nop = prepare_nop; 3162 coroapi.prepare_nop = prepare_nop;
2956 coroapi.prepare_schedule = prepare_schedule; 3163 coroapi.prepare_schedule = prepare_schedule;
2957 coroapi.prepare_cede = prepare_cede; 3164 coroapi.prepare_cede = prepare_cede;
2958 coroapi.prepare_cede_notself = prepare_cede_notself; 3165 coroapi.prepare_cede_notself = prepare_cede_notself;
2959 3166
2960 { 3167 time_init (aTHX);
2961 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
2962
2963 if (!svp) croak ("Time::HiRes is required");
2964 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2965
2966 nvtime = INT2PTR (double (*)(), SvIV (*svp));
2967
2968 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
2969 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
2970 }
2971 3168
2972 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3169 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
2973} 3170}
2974 3171
2975SV * 3172SV *
2976new (char *klass, ...) 3173new (SV *klass, ...)
2977 ALIAS: 3174 ALIAS:
2978 Coro::new = 1 3175 Coro::new = 1
2979 CODE: 3176 CODE:
2980{ 3177 RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix);
2981 struct coro *coro; 3178 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 3179 RETVAL
3032 3180
3033void 3181void
3034transfer (...) 3182transfer (...)
3035 PROTOTYPE: $$ 3183 PROTOTYPE: $$
3172 RETVAL = boolSV (coro->flags & ix); 3320 RETVAL = boolSV (coro->flags & ix);
3173 OUTPUT: 3321 OUTPUT:
3174 RETVAL 3322 RETVAL
3175 3323
3176void 3324void
3177throw (Coro::State self, SV *throw = &PL_sv_undef) 3325throw (Coro::State self, SV *exception = &PL_sv_undef)
3178 PROTOTYPE: $;$ 3326 PROTOTYPE: $;$
3179 CODE: 3327 CODE:
3180{ 3328{
3181 struct coro *current = SvSTATE_current; 3329 struct coro *current = SvSTATE_current;
3182 SV **throwp = self == current ? &CORO_THROW : &self->except; 3330 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3183 SvREFCNT_dec (*throwp); 3331 SvREFCNT_dec (*exceptionp);
3184 SvGETMAGIC (throw); 3332 SvGETMAGIC (exception);
3185 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3333 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3186} 3334}
3187 3335
3188void 3336void
3189api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3337api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3190 PROTOTYPE: $;$ 3338 PROTOTYPE: $;$
3247cancel (Coro::State self) 3395cancel (Coro::State self)
3248 CODE: 3396 CODE:
3249 coro_state_destroy (aTHX_ self); 3397 coro_state_destroy (aTHX_ self);
3250 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ 3398 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3251 3399
3252
3253SV * 3400SV *
3254enable_times (int enabled = enable_times) 3401enable_times (int enabled = enable_times)
3255 CODE: 3402 CODE:
3256{ 3403{
3257 RETVAL = boolSV (enable_times); 3404 RETVAL = boolSV (enable_times);
3284 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); 3431 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3285 3432
3286 if (expect_false (current == self)) 3433 if (expect_false (current == self))
3287 coro_times_sub (SvSTATE (coro_current)); 3434 coro_times_sub (SvSTATE (coro_current));
3288} 3435}
3436
3437void
3438swap_sv (Coro::State coro, SV *sv, SV *swapsv)
3439 CODE:
3440{
3441 struct coro *current = SvSTATE_current;
3442
3443 if (current == coro)
3444 SWAP_SVS (current);
3445
3446 if (!coro->swap_sv)
3447 coro->swap_sv = newAV ();
3448
3449 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv )));
3450 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv)));
3451
3452 if (current == coro)
3453 SWAP_SVS (current);
3454}
3455
3289 3456
3290MODULE = Coro::State PACKAGE = Coro 3457MODULE = Coro::State PACKAGE = Coro
3291 3458
3292BOOT: 3459BOOT:
3293{ 3460{
3302 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3469 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3303 3470
3304 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3471 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); 3472 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); 3473 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); 3474 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3308 3475
3309 coro_stash = gv_stashpv ("Coro", TRUE); 3476 coro_stash = gv_stashpv ("Coro", TRUE);
3310 3477
3311 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3478 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3312 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3479 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3313 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3480 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3331 sv_setiv (sv, (IV)&coroapi); 3498 sv_setiv (sv, (IV)&coroapi);
3332 SvREADONLY_on (sv); 3499 SvREADONLY_on (sv);
3333 } 3500 }
3334} 3501}
3335 3502
3503SV *
3504async (...)
3505 PROTOTYPE: &@
3506 CODE:
3507 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1);
3508 api_ready (aTHX_ RETVAL);
3509 OUTPUT:
3510 RETVAL
3511
3336void 3512void
3337terminate (...) 3513terminate (...)
3338 CODE: 3514 CODE:
3339 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3515 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3340 3516
3374_set_readyhook (SV *hook) 3550_set_readyhook (SV *hook)
3375 PROTOTYPE: $ 3551 PROTOTYPE: $
3376 CODE: 3552 CODE:
3377 SvREFCNT_dec (coro_readyhook); 3553 SvREFCNT_dec (coro_readyhook);
3378 SvGETMAGIC (hook); 3554 SvGETMAGIC (hook);
3555 if (SvOK (hook))
3556 {
3379 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3557 coro_readyhook = newSVsv (hook);
3558 CORO_READYHOOK = invoke_sv_ready_hook_helper;
3559 }
3560 else
3561 {
3562 coro_readyhook = 0;
3563 CORO_READYHOOK = 0;
3564 }
3380 3565
3381int 3566int
3382prio (Coro::State coro, int newprio = 0) 3567prio (Coro::State coro, int newprio = 0)
3383 PROTOTYPE: $;$ 3568 PROTOTYPE: $;$
3384 ALIAS: 3569 ALIAS:
3448 for (i = 1; i < items; ++i) 3633 for (i = 1; i < items; ++i)
3449 av_push (av, SvREFCNT_inc_NN (ST (i))); 3634 av_push (av, SvREFCNT_inc_NN (ST (i)));
3450 3635
3451 if ((SV *)hv == &PL_sv_undef) 3636 if ((SV *)hv == &PL_sv_undef)
3452 { 3637 {
3453 PUSHMARK (SP); 3638 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)); 3639 hv = (HV *)SvREFCNT_inc_NN (SvRV (sv));
3640 SvREFCNT_dec (sv);
3462 } 3641 }
3463 3642
3464 { 3643 {
3465 struct coro *coro = SvSTATE_hv (hv); 3644 struct coro *coro = SvSTATE_hv (hv);
3466 3645
3717 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3896 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3718 sv_setpv ((SV *)slf_cv, proto); 3897 sv_setpv ((SV *)slf_cv, proto);
3719 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3898 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3720} 3899}
3721 3900
3901MODULE = Coro::State PACKAGE = Coro::Select
3902
3903void
3904patch_pp_sselect ()
3905 CODE:
3906 if (!coro_old_pp_sselect)
3907 {
3908 coro_select_select = (SV *)get_cv ("Coro::Select::select", 0);
3909 coro_old_pp_sselect = PL_ppaddr [OP_SSELECT];
3910 PL_ppaddr [OP_SSELECT] = coro_pp_sselect;
3911 }
3912
3913void
3914unpatch_pp_sselect ()
3915 CODE:
3916 if (coro_old_pp_sselect)
3917 {
3918 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3919 coro_old_pp_sselect = 0;
3920 }
3921

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines