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.373 by root, Thu Oct 1 23:51:33 2009 UTC vs.
Revision 1.388 by root, Wed Feb 23 07:14:22 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
22#ifndef SVs_PADSTALE
23# define SVs_PADSTALE 0
24#endif
25
17#ifdef WIN32 26#ifdef 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 */
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.");
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
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
2349 if (PL_op->op_flags & OPf_STACKED) 2477 if (PL_op->op_flags & OPf_STACKED)
2350 { 2478 {
2351 if (items > slf_arga) 2479 if (items > slf_arga)
2352 { 2480 {
2353 slf_arga = items; 2481 slf_arga = items;
2354 free (slf_argv); 2482 Safefree (slf_argv);
2355 slf_argv = malloc (slf_arga * sizeof (SV *)); 2483 New (0, slf_argv, slf_arga, SV *);
2356 } 2484 }
2357 2485
2358 slf_argc = items; 2486 slf_argc = items;
2359 2487
2360 for (i = 0; i < items; ++i) 2488 for (i = 0; i < items; ++i)
2721 { 2849 {
2722 SV *cb_cv = s_get_cv_croak (arg [1]); 2850 SV *cb_cv = s_get_cv_croak (arg [1]);
2723 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2851 av_push (av, SvREFCNT_inc_NN (cb_cv));
2724 2852
2725 if (SvIVX (AvARRAY (av)[0])) 2853 if (SvIVX (AvARRAY (av)[0]))
2726 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2854 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2727 2855
2728 frame->prepare = prepare_nop; 2856 frame->prepare = prepare_nop;
2729 frame->check = slf_check_nop; 2857 frame->check = slf_check_nop;
2730 } 2858 }
2731 else if (SvIVX (AvARRAY (av)[0])) 2859 else if (SvIVX (AvARRAY (av)[0]))
2893 /* now call the AIO function - we assume our request is uncancelable */ 3021 /* now call the AIO function - we assume our request is uncancelable */
2894 PUTBACK; 3022 PUTBACK;
2895 call_sv ((SV *)req, G_VOID | G_DISCARD); 3023 call_sv ((SV *)req, G_VOID | G_DISCARD);
2896 } 3024 }
2897 3025
2898 /* 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 */
2899 frame->data = (void *)state; 3027 frame->data = (void *)state;
2900 frame->prepare = prepare_schedule; 3028 frame->prepare = prepare_schedule;
2901 frame->check = slf_check_aio_req; 3029 frame->check = slf_check_aio_req;
2902} 3030}
2903 3031
3034 coroapi.prepare_nop = prepare_nop; 3162 coroapi.prepare_nop = prepare_nop;
3035 coroapi.prepare_schedule = prepare_schedule; 3163 coroapi.prepare_schedule = prepare_schedule;
3036 coroapi.prepare_cede = prepare_cede; 3164 coroapi.prepare_cede = prepare_cede;
3037 coroapi.prepare_cede_notself = prepare_cede_notself; 3165 coroapi.prepare_cede_notself = prepare_cede_notself;
3038 3166
3039 { 3167 time_init (aTHX);
3040 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
3041
3042 if (!svp) croak ("Time::HiRes is required");
3043 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3044
3045 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3046
3047 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3048 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3049 }
3050 3168
3051 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3169 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3052} 3170}
3053 3171
3054SV * 3172SV *
3202 RETVAL = boolSV (coro->flags & ix); 3320 RETVAL = boolSV (coro->flags & ix);
3203 OUTPUT: 3321 OUTPUT:
3204 RETVAL 3322 RETVAL
3205 3323
3206void 3324void
3207throw (Coro::State self, SV *throw = &PL_sv_undef) 3325throw (Coro::State self, SV *exception = &PL_sv_undef)
3208 PROTOTYPE: $;$ 3326 PROTOTYPE: $;$
3209 CODE: 3327 CODE:
3210{ 3328{
3211 struct coro *current = SvSTATE_current; 3329 struct coro *current = SvSTATE_current;
3212 SV **throwp = self == current ? &CORO_THROW : &self->except; 3330 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3213 SvREFCNT_dec (*throwp); 3331 SvREFCNT_dec (*exceptionp);
3214 SvGETMAGIC (throw); 3332 SvGETMAGIC (exception);
3215 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3333 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3216} 3334}
3217 3335
3218void 3336void
3219api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3337api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3220 PROTOTYPE: $;$ 3338 PROTOTYPE: $;$
3314 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); 3432 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3315 3433
3316 if (expect_false (current == self)) 3434 if (expect_false (current == self))
3317 coro_times_sub (SvSTATE (coro_current)); 3435 coro_times_sub (SvSTATE (coro_current));
3318} 3436}
3437
3438void
3439swap_sv (Coro::State coro, SV *sv, SV *swapsv)
3440 CODE:
3441{
3442 struct coro *current = SvSTATE_current;
3443
3444 if (current == coro)
3445 SWAP_SVS (current);
3446
3447 if (!coro->swap_sv)
3448 coro->swap_sv = newAV ();
3449
3450 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv )));
3451 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv)));
3452
3453 if (current == coro)
3454 SWAP_SVS (current);
3455}
3456
3319 3457
3320MODULE = Coro::State PACKAGE = Coro 3458MODULE = Coro::State PACKAGE = Coro
3321 3459
3322BOOT: 3460BOOT:
3323{ 3461{
3780 { 3918 {
3781 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 3919 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3782 coro_old_pp_sselect = 0; 3920 coro_old_pp_sselect = 0;
3783 } 3921 }
3784 3922
3785

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines