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.370 by root, Thu Aug 20 21:38:51 2009 UTC vs.
Revision 1.392 by root, Fri Apr 29 15:43: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
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
231 CV *startcv; /* the CV to execute */ 241 CV *startcv; /* the CV to execute */
232 AV *args; /* data associated with this coroutine (initial args) */ 242 AV *args; /* data associated with this coroutine (initial args) */
233 int refcnt; /* coroutines are refcounted, yes */ 243 int refcnt; /* coroutines are refcounted, yes */
234 int flags; /* CF_ flags */ 244 int flags; /* CF_ flags */
235 HV *hv; /* the perl hash associated with this coro, if any */ 245 HV *hv; /* the perl hash associated with this coro, if any */
236 void (*on_destroy)(pTHX_ struct coro *coro); 246 void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */
237 247
238 /* statistics */ 248 /* statistics */
239 int usecount; /* number of transfers to this coro */ 249 int usecount; /* number of transfers to this coro */
240 250
241 /* coro process data */ 251 /* coro process data */
249 AV *invoke_av; 259 AV *invoke_av;
250 260
251 /* on_enter/on_leave */ 261 /* on_enter/on_leave */
252 AV *on_enter; 262 AV *on_enter;
253 AV *on_leave; 263 AV *on_leave;
264
265 /* swap_sv */
266 AV *swap_sv;
254 267
255 /* times */ 268 /* times */
256 coro_ts t_cpu, t_real; 269 coro_ts t_cpu, t_real;
257 270
258 /* linked list */ 271 /* linked list */
289static OP *(*coro_old_pp_sselect) (pTHX); 302static OP *(*coro_old_pp_sselect) (pTHX);
290static SV *coro_select_select; 303static SV *coro_select_select;
291 304
292/* horrible hack, but if it works... */ 305/* horrible hack, but if it works... */
293static OP * 306static OP *
294coro_pp_sselect (aTHX) 307coro_pp_sselect (pTHX)
295{ 308{
296 dSP; 309 dSP;
297 PUSHMARK (SP - 4); /* fake argument list */ 310 PUSHMARK (SP - 4); /* fake argument list */
298 XPUSHs (coro_select_select); 311 XPUSHs (coro_select_select);
299 PUTBACK; 312 PUTBACK;
301 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ 314 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
302 PL_op->op_flags |= OPf_STACKED; 315 PL_op->op_flags |= OPf_STACKED;
303 PL_op->op_private = 0; 316 PL_op->op_private = 0;
304 return PL_ppaddr [OP_ENTERSUB](aTHX); 317 return PL_ppaddr [OP_ENTERSUB](aTHX);
305} 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. Caught");
362 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
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
306 371
307/** lowlevel stuff **********************************************************/ 372/** lowlevel stuff **********************************************************/
308 373
309static SV * 374static SV *
310coro_get_sv (pTHX_ const char *name, int create) 375coro_get_sv (pTHX_ const char *name, int create)
550 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 615 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
551} 616}
552 617
553/** load & save, init *******************************************************/ 618/** load & save, init *******************************************************/
554 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
555static void 671static void
556on_enterleave_call (pTHX_ SV *cb); 672on_enterleave_call (pTHX_ SV *cb);
557 673
558static void 674static void
559load_perl (pTHX_ Coro__State c) 675load_perl (pTHX_ Coro__State c)
605 int i; 721 int i;
606 722
607 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 723 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
608 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 724 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
609 } 725 }
726
727 SWAP_SVS (c);
610} 728}
611 729
612static void 730static void
613save_perl (pTHX_ Coro__State c) 731save_perl (pTHX_ Coro__State c)
614{ 732{
733 SWAP_SVS (c);
734
615 if (expect_false (c->on_leave)) 735 if (expect_false (c->on_leave))
616 { 736 {
617 int i; 737 int i;
618 738
619 for (i = AvFILLp (c->on_leave); i >= 0; --i) 739 for (i = AvFILLp (c->on_leave); i >= 0; --i)
926slf_check_repeat (pTHX_ struct CoroSLF *frame) 1046slf_check_repeat (pTHX_ struct CoroSLF *frame)
927{ 1047{
928 return 1; 1048 return 1;
929} 1049}
930 1050
931static UNOP coro_setup_op; 1051static UNOP init_perl_op;
932 1052
933static 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 */
934coro_setup (pTHX_ struct coro *coro) 1054init_perl (pTHX_ struct coro *coro)
935{ 1055{
936 /* 1056 /*
937 * emulate part of the perl startup here. 1057 * emulate part of the perl startup here.
938 */ 1058 */
939 coro_init_stacks (aTHX); 1059 coro_init_stacks (aTHX);
946 PL_comppad_name_fill = 0; 1066 PL_comppad_name_fill = 0;
947 PL_comppad_name_floor = 0; 1067 PL_comppad_name_floor = 0;
948 PL_curpm = 0; 1068 PL_curpm = 0;
949 PL_curpad = 0; 1069 PL_curpad = 0;
950 PL_localizing = 0; 1070 PL_localizing = 0;
951 PL_dirty = 0;
952 PL_restartop = 0; 1071 PL_restartop = 0;
953#if PERL_VERSION_ATLEAST (5,10,0) 1072#if PERL_VERSION_ATLEAST (5,10,0)
954 PL_parser = 0; 1073 PL_parser = 0;
955#endif 1074#endif
956 PL_hints = 0; 1075 PL_hints = 0;
988 */ 1107 */
989 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 */
990 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 */
991 1110
992 /* 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 */
993 coro_setup_op.op_next = PL_op; 1112 init_perl_op.op_next = PL_op;
994 coro_setup_op.op_type = OP_ENTERSUB; 1113 init_perl_op.op_type = OP_ENTERSUB;
995 coro_setup_op.op_ppaddr = pp_slf; 1114 init_perl_op.op_ppaddr = pp_slf;
996 /* 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 */
997 1116
998 PL_op = (OP *)&coro_setup_op; 1117 PL_op = (OP *)&init_perl_op;
999 1118
1000 /* copy throw, in case it was set before coro_setup */ 1119 /* copy throw, in case it was set before init_perl */
1001 CORO_THROW = coro->except; 1120 CORO_THROW = coro->except;
1121
1122 SWAP_SVS (coro);
1002 1123
1003 if (expect_false (enable_times)) 1124 if (expect_false (enable_times))
1004 { 1125 {
1005 coro_times_update (); 1126 coro_times_update ();
1006 coro_times_sub (coro); 1127 coro_times_sub (coro);
1027 dounwind (-1); 1148 dounwind (-1);
1028 } 1149 }
1029} 1150}
1030 1151
1031static void 1152static void
1032coro_destruct_perl (pTHX_ struct coro *coro) 1153destroy_perl (pTHX_ struct coro *coro)
1033{ 1154{
1034 SV *svf [9]; 1155 SV *svf [9];
1035 1156
1036 { 1157 {
1037 struct coro *current = SvSTATE_current; 1158 struct coro *current = SvSTATE_current;
1041 save_perl (aTHX_ current); 1162 save_perl (aTHX_ current);
1042 load_perl (aTHX_ coro); 1163 load_perl (aTHX_ coro);
1043 1164
1044 coro_unwind_stacks (aTHX); 1165 coro_unwind_stacks (aTHX);
1045 coro_destruct_stacks (aTHX); 1166 coro_destruct_stacks (aTHX);
1167
1168 /* restore swapped sv's */
1169 SWAP_SVS (coro);
1046 1170
1047 // now save some sv's to be free'd later 1171 // now save some sv's to be free'd later
1048 svf [0] = GvSV (PL_defgv); 1172 svf [0] = GvSV (PL_defgv);
1049 svf [1] = (SV *)GvAV (PL_defgv); 1173 svf [1] = (SV *)GvAV (PL_defgv);
1050 svf [2] = GvSV (PL_errgv); 1174 svf [2] = GvSV (PL_errgv);
1492 if (expect_false (next->flags & CF_NEW)) 1616 if (expect_false (next->flags & CF_NEW))
1493 { 1617 {
1494 /* need to start coroutine */ 1618 /* need to start coroutine */
1495 next->flags &= ~CF_NEW; 1619 next->flags &= ~CF_NEW;
1496 /* setup coroutine call */ 1620 /* setup coroutine call */
1497 coro_setup (aTHX_ next); 1621 init_perl (aTHX_ next);
1498 } 1622 }
1499 else 1623 else
1500 load_perl (aTHX_ next); 1624 load_perl (aTHX_ next);
1501 1625
1502 /* possibly untie and reuse the cctx */ 1626 /* possibly untie and reuse the cctx */
1565 1689
1566 if (coro->mainstack 1690 if (coro->mainstack
1567 && coro->mainstack != main_mainstack 1691 && coro->mainstack != main_mainstack
1568 && coro->slot 1692 && coro->slot
1569 && !PL_dirty) 1693 && !PL_dirty)
1570 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;
1571 1699
1572 cctx_destroy (coro->cctx); 1700 cctx_destroy (coro->cctx);
1573 SvREFCNT_dec (coro->startcv); 1701 SvREFCNT_dec (coro->startcv);
1574 SvREFCNT_dec (coro->args); 1702 SvREFCNT_dec (coro->args);
1703 SvREFCNT_dec (coro->swap_sv);
1575 SvREFCNT_dec (CORO_THROW); 1704 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 1705
1581 return 1; 1706 return 1;
1582} 1707}
1583 1708
1584static int 1709static int
1754 { 1879 {
1755 /* nothing to schedule: call the idle handler */ 1880 /* nothing to schedule: call the idle handler */
1756 if (SvROK (sv_idle) 1881 if (SvROK (sv_idle)
1757 && SvOBJECT (SvRV (sv_idle))) 1882 && SvOBJECT (SvRV (sv_idle)))
1758 { 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
1759 ++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 */
1760 api_ready (aTHX_ SvRV (sv_idle)); 1888 api_ready (aTHX_ SvRV (sv_idle));
1761 --coro_nready; 1889 --coro_nready;
1762 } 1890 }
1763 else 1891 else
1764 { 1892 {
1893 /* TODO: deprecated, remove, cannot work reliably *//*D*/
1765 dSP; 1894 dSP;
1766 1895
1767 ENTER; 1896 ENTER;
1768 SAVETMPS; 1897 SAVETMPS;
1769 1898
1879 2008
1880static void 2009static void
1881coro_call_on_destroy (pTHX_ struct coro *coro) 2010coro_call_on_destroy (pTHX_ struct coro *coro)
1882{ 2011{
1883 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); 2012 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1884 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1885 2013
1886 if (on_destroyp) 2014 if (on_destroyp)
1887 { 2015 {
1888 AV *on_destroy = (AV *)SvRV (*on_destroyp); 2016 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
2017 AV *on_destroy = sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*on_destroyp)));
2018 AV *status = statusp ? sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*statusp))) : 0;
1889 2019
1890 while (AvFILLp (on_destroy) >= 0) 2020 while (AvFILLp (on_destroy) >= 0)
1891 { 2021 {
1892 dSP; /* don't disturb outer sp */ 2022 dSP; /* don't disturb outer sp */
1893 SV *cb = av_pop (on_destroy); 2023 SV *cb = av_pop (on_destroy);
1895 PUSHMARK (SP); 2025 PUSHMARK (SP);
1896 2026
1897 if (statusp) 2027 if (statusp)
1898 { 2028 {
1899 int i; 2029 int i;
1900 AV *status = (AV *)SvRV (*statusp);
1901 EXTEND (SP, AvFILLp (status) + 1); 2030 EXTEND (SP, AvFILLp (status) + 1);
1902 2031
1903 for (i = 0; i <= AvFILLp (status); ++i) 2032 for (i = 0; i <= AvFILLp (status); ++i)
1904 PUSHs (AvARRAY (status)[i]); 2033 PUSHs (AvARRAY (status)[i]);
1905 } 2034 }
1909 } 2038 }
1910 } 2039 }
1911} 2040}
1912 2041
1913static void 2042static void
1914slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2043coro_set_status (HV *coro_hv, SV **arg, int items)
1915{ 2044{
1916 int i;
1917 HV *hv = (HV *)SvRV (coro_current);
1918 AV *av = newAV (); 2045 AV *av = newAV ();
1919 2046
1920 /* items are actually not so common, so optimise for this case */ 2047 /* items are actually not so common, so optimise for this case */
1921 if (items) 2048 if (items)
1922 { 2049 {
2050 int i;
2051
1923 av_extend (av, items - 1); 2052 av_extend (av, items - 1);
1924 2053
1925 for (i = 0; i < items; ++i) 2054 for (i = 0; i < items; ++i)
1926 av_push (av, SvREFCNT_inc_NN (arg [i])); 2055 av_push (av, SvREFCNT_inc_NN (arg [i]));
1927 } 2056 }
1928 2057
1929 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); 2058 hv_store (coro_hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
2059}
1930 2060
2061static void
2062slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2063{
1931 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 2064 av_push (av_destroy, (SV *)newRV_inc ((SV *)coro_hv)); /* RVinc for perl */
1932 api_ready (aTHX_ sv_manager); 2065 api_ready (aTHX_ sv_manager);
1933 2066
1934 frame->prepare = prepare_schedule; 2067 frame->prepare = prepare_schedule;
1935 frame->check = slf_check_repeat; 2068 frame->check = slf_check_repeat;
1936 2069
1937 /* as a minor optimisation, we could unwind all stacks here */ 2070 /* as a minor optimisation, we could unwind all stacks here */
1938 /* but that puts extra pressure on pp_slf, and is not worth much */ 2071 /* but that puts extra pressure on pp_slf, and is not worth much */
1939 /*coro_unwind_stacks (aTHX);*/ 2072 /*coro_unwind_stacks (aTHX);*/
2073}
2074
2075static void
2076slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2077{
2078 HV *coro_hv = (HV *)SvRV (coro_current);
2079
2080 coro_set_status (coro_hv, arg, items);
2081 slf_init_terminate_cancel_common (frame, coro_hv);
2082}
2083
2084static void
2085slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2086{
2087 HV *coro_hv;
2088 struct coro *coro;
2089
2090 if (items <= 0)
2091 croak ("Coro::cancel called without coro object,");
2092
2093 coro = SvSTATE (arg [0]);
2094 coro_hv = coro->hv;
2095
2096 coro_set_status (coro_hv, arg + 1, items - 1);
2097
2098 /* cancelling the current coro is allowed, and equals terminate */
2099 if (coro_hv == (HV *)SvRV (coro_current))
2100 slf_init_terminate_cancel_common (frame, coro_hv);
2101 else
2102 {
2103 /* otherwise we cancel ourselves */
2104 coro_state_destroy (aTHX_ coro);
2105 coro_call_on_destroy (aTHX_ coro);
2106
2107 frame->prepare = prepare_nop;
2108 frame->check = slf_check_nop;
2109 }
1940} 2110}
1941 2111
1942/*****************************************************************************/ 2112/*****************************************************************************/
1943/* async pool handler */ 2113/* async pool handler */
1944 2114
2348 if (PL_op->op_flags & OPf_STACKED) 2518 if (PL_op->op_flags & OPf_STACKED)
2349 { 2519 {
2350 if (items > slf_arga) 2520 if (items > slf_arga)
2351 { 2521 {
2352 slf_arga = items; 2522 slf_arga = items;
2353 free (slf_argv); 2523 Safefree (slf_argv);
2354 slf_argv = malloc (slf_arga * sizeof (SV *)); 2524 New (0, slf_argv, slf_arga, SV *);
2355 } 2525 }
2356 2526
2357 slf_argc = items; 2527 slf_argc = items;
2358 2528
2359 for (i = 0; i < items; ++i) 2529 for (i = 0; i < items; ++i)
2720 { 2890 {
2721 SV *cb_cv = s_get_cv_croak (arg [1]); 2891 SV *cb_cv = s_get_cv_croak (arg [1]);
2722 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2892 av_push (av, SvREFCNT_inc_NN (cb_cv));
2723 2893
2724 if (SvIVX (AvARRAY (av)[0])) 2894 if (SvIVX (AvARRAY (av)[0]))
2725 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2895 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2726 2896
2727 frame->prepare = prepare_nop; 2897 frame->prepare = prepare_nop;
2728 frame->check = slf_check_nop; 2898 frame->check = slf_check_nop;
2729 } 2899 }
2730 else if (SvIVX (AvARRAY (av)[0])) 2900 else if (SvIVX (AvARRAY (av)[0]))
2892 /* now call the AIO function - we assume our request is uncancelable */ 3062 /* now call the AIO function - we assume our request is uncancelable */
2893 PUTBACK; 3063 PUTBACK;
2894 call_sv ((SV *)req, G_VOID | G_DISCARD); 3064 call_sv ((SV *)req, G_VOID | G_DISCARD);
2895 } 3065 }
2896 3066
2897 /* now that the requets is going, we loop toll we have a result */ 3067 /* now that the request is going, we loop till we have a result */
2898 frame->data = (void *)state; 3068 frame->data = (void *)state;
2899 frame->prepare = prepare_schedule; 3069 frame->prepare = prepare_schedule;
2900 frame->check = slf_check_aio_req; 3070 frame->check = slf_check_aio_req;
2901} 3071}
2902 3072
3033 coroapi.prepare_nop = prepare_nop; 3203 coroapi.prepare_nop = prepare_nop;
3034 coroapi.prepare_schedule = prepare_schedule; 3204 coroapi.prepare_schedule = prepare_schedule;
3035 coroapi.prepare_cede = prepare_cede; 3205 coroapi.prepare_cede = prepare_cede;
3036 coroapi.prepare_cede_notself = prepare_cede_notself; 3206 coroapi.prepare_cede_notself = prepare_cede_notself;
3037 3207
3038 { 3208 time_init (aTHX);
3039 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
3040
3041 if (!svp) croak ("Time::HiRes is required");
3042 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3043
3044 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3045
3046 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3047 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3048 }
3049 3209
3050 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3210 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3051} 3211}
3052 3212
3053SV * 3213SV *
3062void 3222void
3063transfer (...) 3223transfer (...)
3064 PROTOTYPE: $$ 3224 PROTOTYPE: $$
3065 CODE: 3225 CODE:
3066 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3226 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3067
3068bool
3069_destroy (SV *coro_sv)
3070 CODE:
3071 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
3072 OUTPUT:
3073 RETVAL
3074 3227
3075void 3228void
3076_exit (int code) 3229_exit (int code)
3077 PROTOTYPE: $ 3230 PROTOTYPE: $
3078 CODE: 3231 CODE:
3201 RETVAL = boolSV (coro->flags & ix); 3354 RETVAL = boolSV (coro->flags & ix);
3202 OUTPUT: 3355 OUTPUT:
3203 RETVAL 3356 RETVAL
3204 3357
3205void 3358void
3206throw (Coro::State self, SV *throw = &PL_sv_undef) 3359throw (Coro::State self, SV *exception = &PL_sv_undef)
3207 PROTOTYPE: $;$ 3360 PROTOTYPE: $;$
3208 CODE: 3361 CODE:
3209{ 3362{
3210 struct coro *current = SvSTATE_current; 3363 struct coro *current = SvSTATE_current;
3211 SV **throwp = self == current ? &CORO_THROW : &self->except; 3364 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3212 SvREFCNT_dec (*throwp); 3365 SvREFCNT_dec (*exceptionp);
3213 SvGETMAGIC (throw); 3366 SvGETMAGIC (exception);
3214 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3367 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3215} 3368}
3216 3369
3217void 3370void
3218api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3371api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3219 PROTOTYPE: $;$ 3372 PROTOTYPE: $;$
3274 3427
3275void 3428void
3276cancel (Coro::State self) 3429cancel (Coro::State self)
3277 CODE: 3430 CODE:
3278 coro_state_destroy (aTHX_ self); 3431 coro_state_destroy (aTHX_ self);
3279 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3280
3281 3432
3282SV * 3433SV *
3283enable_times (int enabled = enable_times) 3434enable_times (int enabled = enable_times)
3284 CODE: 3435 CODE:
3285{ 3436{
3313 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); 3464 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3314 3465
3315 if (expect_false (current == self)) 3466 if (expect_false (current == self))
3316 coro_times_sub (SvSTATE (coro_current)); 3467 coro_times_sub (SvSTATE (coro_current));
3317} 3468}
3469
3470void
3471swap_sv (Coro::State coro, SV *sv, SV *swapsv)
3472 CODE:
3473{
3474 struct coro *current = SvSTATE_current;
3475
3476 if (current == coro)
3477 SWAP_SVS (current);
3478
3479 if (!coro->swap_sv)
3480 coro->swap_sv = newAV ();
3481
3482 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv )));
3483 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv)));
3484
3485 if (current == coro)
3486 SWAP_SVS (current);
3487}
3488
3318 3489
3319MODULE = Coro::State PACKAGE = Coro 3490MODULE = Coro::State PACKAGE = Coro
3320 3491
3321BOOT: 3492BOOT:
3322{ 3493{
3365SV * 3536SV *
3366async (...) 3537async (...)
3367 PROTOTYPE: &@ 3538 PROTOTYPE: &@
3368 CODE: 3539 CODE:
3369 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); 3540 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1);
3370 api_ready (RETVAL); 3541 api_ready (aTHX_ RETVAL);
3371 OUTPUT: 3542 OUTPUT:
3372 RETVAL 3543 RETVAL
3544
3545void
3546_destroy (Coro::State coro)
3547 CODE:
3548 /* used by the manager thread */
3549 coro_state_destroy (aTHX_ coro);
3550 coro_call_on_destroy (aTHX_ coro);
3373 3551
3374void 3552void
3375terminate (...) 3553terminate (...)
3376 CODE: 3554 CODE:
3377 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3555 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3556
3557void
3558cancel (...)
3559 CODE:
3560 CORO_EXECUTE_SLF_XS (slf_init_cancel);
3378 3561
3379void 3562void
3380schedule (...) 3563schedule (...)
3381 CODE: 3564 CODE:
3382 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3565 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3779 { 3962 {
3780 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 3963 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3781 coro_old_pp_sselect = 0; 3964 coro_old_pp_sselect = 0;
3782 } 3965 }
3783 3966
3784

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines