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.369 by root, Thu Aug 13 02:48:32 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
250 259
251 /* on_enter/on_leave */ 260 /* on_enter/on_leave */
252 AV *on_enter; 261 AV *on_enter;
253 AV *on_leave; 262 AV *on_leave;
254 263
264 /* swap_sv */
265 AV *swap_sv;
266
255 /* times */ 267 /* times */
256 coro_ts t_cpu, t_real; 268 coro_ts t_cpu, t_real;
257 269
258 /* linked list */ 270 /* linked list */
259 struct coro *next, *prev; 271 struct coro *next, *prev;
289static OP *(*coro_old_pp_sselect) (pTHX); 301static OP *(*coro_old_pp_sselect) (pTHX);
290static SV *coro_select_select; 302static SV *coro_select_select;
291 303
292/* horrible hack, but if it works... */ 304/* horrible hack, but if it works... */
293static OP * 305static OP *
294coro_pp_sselect (aTHX) 306coro_pp_sselect (pTHX)
295{ 307{
296 dSP; 308 dSP;
297 PUSHMARK (SP - 4); /* fake argument list */ 309 PUSHMARK (SP - 4); /* fake argument list */
298 XPUSHs (coro_select_select); 310 XPUSHs (coro_select_select);
299 PUTBACK; 311 PUTBACK;
550 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 562 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
551} 563}
552 564
553/** load & save, init *******************************************************/ 565/** load & save, init *******************************************************/
554 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
555static void 618static void
556on_enterleave_call (pTHX_ SV *cb); 619on_enterleave_call (pTHX_ SV *cb);
557 620
558static void 621static void
559load_perl (pTHX_ Coro__State c) 622load_perl (pTHX_ Coro__State c)
605 int i; 668 int i;
606 669
607 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 670 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
608 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 671 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
609 } 672 }
673
674 SWAP_SVS (c);
610} 675}
611 676
612static void 677static void
613save_perl (pTHX_ Coro__State c) 678save_perl (pTHX_ Coro__State c)
614{ 679{
680 SWAP_SVS (c);
681
615 if (expect_false (c->on_leave)) 682 if (expect_false (c->on_leave))
616 { 683 {
617 int i; 684 int i;
618 685
619 for (i = AvFILLp (c->on_leave); i >= 0; --i) 686 for (i = AvFILLp (c->on_leave); i >= 0; --i)
926slf_check_repeat (pTHX_ struct CoroSLF *frame) 993slf_check_repeat (pTHX_ struct CoroSLF *frame)
927{ 994{
928 return 1; 995 return 1;
929} 996}
930 997
931static UNOP coro_setup_op; 998static UNOP init_perl_op;
932 999
933static 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 */
934coro_setup (pTHX_ struct coro *coro) 1001init_perl (pTHX_ struct coro *coro)
935{ 1002{
936 /* 1003 /*
937 * emulate part of the perl startup here. 1004 * emulate part of the perl startup here.
938 */ 1005 */
939 coro_init_stacks (aTHX); 1006 coro_init_stacks (aTHX);
988 */ 1055 */
989 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 */
990 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 */
991 1058
992 /* 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 */
993 coro_setup_op.op_next = PL_op; 1060 init_perl_op.op_next = PL_op;
994 coro_setup_op.op_type = OP_ENTERSUB; 1061 init_perl_op.op_type = OP_ENTERSUB;
995 coro_setup_op.op_ppaddr = pp_slf; 1062 init_perl_op.op_ppaddr = pp_slf;
996 /* 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 */
997 1064
998 PL_op = (OP *)&coro_setup_op; 1065 PL_op = (OP *)&init_perl_op;
999 1066
1000 /* copy throw, in case it was set before coro_setup */ 1067 /* copy throw, in case it was set before init_perl */
1001 CORO_THROW = coro->except; 1068 CORO_THROW = coro->except;
1069
1070 SWAP_SVS (coro);
1002 1071
1003 if (expect_false (enable_times)) 1072 if (expect_false (enable_times))
1004 { 1073 {
1005 coro_times_update (); 1074 coro_times_update ();
1006 coro_times_sub (coro); 1075 coro_times_sub (coro);
1027 dounwind (-1); 1096 dounwind (-1);
1028 } 1097 }
1029} 1098}
1030 1099
1031static void 1100static void
1032coro_destruct_perl (pTHX_ struct coro *coro) 1101destroy_perl (pTHX_ struct coro *coro)
1033{ 1102{
1034 SV *svf [9]; 1103 SV *svf [9];
1035 1104
1036 { 1105 {
1037 struct coro *current = SvSTATE_current; 1106 struct coro *current = SvSTATE_current;
1041 save_perl (aTHX_ current); 1110 save_perl (aTHX_ current);
1042 load_perl (aTHX_ coro); 1111 load_perl (aTHX_ coro);
1043 1112
1044 coro_unwind_stacks (aTHX); 1113 coro_unwind_stacks (aTHX);
1045 coro_destruct_stacks (aTHX); 1114 coro_destruct_stacks (aTHX);
1115
1116 /* restore swapped sv's */
1117 SWAP_SVS (coro);
1046 1118
1047 // now save some sv's to be free'd later 1119 // now save some sv's to be free'd later
1048 svf [0] = GvSV (PL_defgv); 1120 svf [0] = GvSV (PL_defgv);
1049 svf [1] = (SV *)GvAV (PL_defgv); 1121 svf [1] = (SV *)GvAV (PL_defgv);
1050 svf [2] = GvSV (PL_errgv); 1122 svf [2] = GvSV (PL_errgv);
1492 if (expect_false (next->flags & CF_NEW)) 1564 if (expect_false (next->flags & CF_NEW))
1493 { 1565 {
1494 /* need to start coroutine */ 1566 /* need to start coroutine */
1495 next->flags &= ~CF_NEW; 1567 next->flags &= ~CF_NEW;
1496 /* setup coroutine call */ 1568 /* setup coroutine call */
1497 coro_setup (aTHX_ next); 1569 init_perl (aTHX_ next);
1498 } 1570 }
1499 else 1571 else
1500 load_perl (aTHX_ next); 1572 load_perl (aTHX_ next);
1501 1573
1502 /* possibly untie and reuse the cctx */ 1574 /* possibly untie and reuse the cctx */
1565 1637
1566 if (coro->mainstack 1638 if (coro->mainstack
1567 && coro->mainstack != main_mainstack 1639 && coro->mainstack != main_mainstack
1568 && coro->slot 1640 && coro->slot
1569 && !PL_dirty) 1641 && !PL_dirty)
1570 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;
1571 1647
1572 cctx_destroy (coro->cctx); 1648 cctx_destroy (coro->cctx);
1573 SvREFCNT_dec (coro->startcv); 1649 SvREFCNT_dec (coro->startcv);
1574 SvREFCNT_dec (coro->args); 1650 SvREFCNT_dec (coro->args);
1651 SvREFCNT_dec (coro->swap_sv);
1575 SvREFCNT_dec (CORO_THROW); 1652 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 1653
1581 return 1; 1654 return 1;
1582} 1655}
1583 1656
1584static int 1657static int
1754 { 1827 {
1755 /* nothing to schedule: call the idle handler */ 1828 /* nothing to schedule: call the idle handler */
1756 if (SvROK (sv_idle) 1829 if (SvROK (sv_idle)
1757 && SvOBJECT (SvRV (sv_idle))) 1830 && SvOBJECT (SvRV (sv_idle)))
1758 { 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
1759 ++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 */
1760 api_ready (aTHX_ SvRV (sv_idle)); 1836 api_ready (aTHX_ SvRV (sv_idle));
1761 --coro_nready; 1837 --coro_nready;
1762 } 1838 }
1763 else 1839 else
1764 { 1840 {
1841 /* TODO: deprecated, remove, cannot work reliably *//*D*/
1765 dSP; 1842 dSP;
1766 1843
1767 ENTER; 1844 ENTER;
1768 SAVETMPS; 1845 SAVETMPS;
1769 1846
2348 if (PL_op->op_flags & OPf_STACKED) 2425 if (PL_op->op_flags & OPf_STACKED)
2349 { 2426 {
2350 if (items > slf_arga) 2427 if (items > slf_arga)
2351 { 2428 {
2352 slf_arga = items; 2429 slf_arga = items;
2353 free (slf_argv); 2430 Safefree (slf_argv);
2354 slf_argv = malloc (slf_arga * sizeof (SV *)); 2431 New (0, slf_argv, slf_arga, SV *);
2355 } 2432 }
2356 2433
2357 slf_argc = items; 2434 slf_argc = items;
2358 2435
2359 for (i = 0; i < items; ++i) 2436 for (i = 0; i < items; ++i)
2720 { 2797 {
2721 SV *cb_cv = s_get_cv_croak (arg [1]); 2798 SV *cb_cv = s_get_cv_croak (arg [1]);
2722 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2799 av_push (av, SvREFCNT_inc_NN (cb_cv));
2723 2800
2724 if (SvIVX (AvARRAY (av)[0])) 2801 if (SvIVX (AvARRAY (av)[0]))
2725 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2802 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2726 2803
2727 frame->prepare = prepare_nop; 2804 frame->prepare = prepare_nop;
2728 frame->check = slf_check_nop; 2805 frame->check = slf_check_nop;
2729 } 2806 }
2730 else if (SvIVX (AvARRAY (av)[0])) 2807 else if (SvIVX (AvARRAY (av)[0]))
2892 /* now call the AIO function - we assume our request is uncancelable */ 2969 /* now call the AIO function - we assume our request is uncancelable */
2893 PUTBACK; 2970 PUTBACK;
2894 call_sv ((SV *)req, G_VOID | G_DISCARD); 2971 call_sv ((SV *)req, G_VOID | G_DISCARD);
2895 } 2972 }
2896 2973
2897 /* 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 */
2898 frame->data = (void *)state; 2975 frame->data = (void *)state;
2899 frame->prepare = prepare_schedule; 2976 frame->prepare = prepare_schedule;
2900 frame->check = slf_check_aio_req; 2977 frame->check = slf_check_aio_req;
2901} 2978}
2902 2979
3201 RETVAL = boolSV (coro->flags & ix); 3278 RETVAL = boolSV (coro->flags & ix);
3202 OUTPUT: 3279 OUTPUT:
3203 RETVAL 3280 RETVAL
3204 3281
3205void 3282void
3206throw (Coro::State self, SV *throw = &PL_sv_undef) 3283throw (Coro::State self, SV *exception = &PL_sv_undef)
3207 PROTOTYPE: $;$ 3284 PROTOTYPE: $;$
3208 CODE: 3285 CODE:
3209{ 3286{
3210 struct coro *current = SvSTATE_current; 3287 struct coro *current = SvSTATE_current;
3211 SV **throwp = self == current ? &CORO_THROW : &self->except; 3288 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3212 SvREFCNT_dec (*throwp); 3289 SvREFCNT_dec (*exceptionp);
3213 SvGETMAGIC (throw); 3290 SvGETMAGIC (exception);
3214 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3291 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3215} 3292}
3216 3293
3217void 3294void
3218api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3295api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3219 PROTOTYPE: $;$ 3296 PROTOTYPE: $;$
3314 3391
3315 if (expect_false (current == self)) 3392 if (expect_false (current == self))
3316 coro_times_sub (SvSTATE (coro_current)); 3393 coro_times_sub (SvSTATE (coro_current));
3317} 3394}
3318 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
3319MODULE = Coro::State PACKAGE = Coro 3416MODULE = Coro::State PACKAGE = Coro
3320 3417
3321BOOT: 3418BOOT:
3322{ 3419{
3323 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3420 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3331 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3428 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3332 3429
3333 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);
3334 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3431 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3335 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);
3336 3433 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3434
3337 coro_stash = gv_stashpv ("Coro", TRUE); 3435 coro_stash = gv_stashpv ("Coro", TRUE);
3338 3436
3339 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3437 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3340 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3438 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3341 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3439 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3364SV * 3462SV *
3365async (...) 3463async (...)
3366 PROTOTYPE: &@ 3464 PROTOTYPE: &@
3367 CODE: 3465 CODE:
3368 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); 3466 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1);
3369 api_ready (RETVAL); 3467 api_ready (aTHX_ RETVAL);
3370 OUTPUT: 3468 OUTPUT:
3371 RETVAL 3469 RETVAL
3372 3470
3373void 3471void
3374terminate (...) 3472terminate (...)
3778 { 3876 {
3779 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 3877 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3780 coro_old_pp_sselect = 0; 3878 coro_old_pp_sselect = 0;
3781 } 3879 }
3782 3880
3783

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines