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.368 by root, Thu Aug 13 02:35:41 2009 UTC vs.
Revision 1.375 by root, Fri Oct 2 19:58:02 2009 UTC

1#define NDEBUG 1
2
1#include "libcoro/coro.c" 3#include "libcoro/coro.c"
2 4
3#define PERL_NO_GET_CONTEXT 5#define PERL_NO_GET_CONTEXT
4#define PERL_EXT 6#define PERL_EXT
5 7
11#include "schmorp.h" 13#include "schmorp.h"
12 14
13#include <stdio.h> 15#include <stdio.h>
14#include <errno.h> 16#include <errno.h>
15#include <assert.h> 17#include <assert.h>
18
19#ifndef SVs_PADSTALE
20# define SVs_PADSTALE 0
21#endif
16 22
17#ifdef WIN32 23#ifdef WIN32
18# undef setjmp 24# undef setjmp
19# undef longjmp 25# undef longjmp
20# undef _exit 26# undef _exit
142static SV *sv_pool_size; 148static SV *sv_pool_size;
143static SV *sv_async_pool_idle; /* description string */ 149static SV *sv_async_pool_idle; /* description string */
144static AV *av_async_pool; /* idle pool */ 150static AV *av_async_pool; /* idle pool */
145static SV *sv_Coro; /* class string */ 151static SV *sv_Coro; /* class string */
146static CV *cv_pool_handler; 152static CV *cv_pool_handler;
147static CV *cv_coro_state_new;
148 153
149/* Coro::AnyEvent */ 154/* Coro::AnyEvent */
150static SV *sv_activity; 155static SV *sv_activity;
151 156
152/* enable processtime/realtime profiling */ 157/* enable processtime/realtime profiling */
251 256
252 /* on_enter/on_leave */ 257 /* on_enter/on_leave */
253 AV *on_enter; 258 AV *on_enter;
254 AV *on_leave; 259 AV *on_leave;
255 260
261 /* swap_sv */
262 AV *swap_sv;
263
256 /* times */ 264 /* times */
257 coro_ts t_cpu, t_real; 265 coro_ts t_cpu, t_real;
258 266
259 /* linked list */ 267 /* linked list */
260 struct coro *next, *prev; 268 struct coro *next, *prev;
290static OP *(*coro_old_pp_sselect) (pTHX); 298static OP *(*coro_old_pp_sselect) (pTHX);
291static SV *coro_select_select; 299static SV *coro_select_select;
292 300
293/* horrible hack, but if it works... */ 301/* horrible hack, but if it works... */
294static OP * 302static OP *
295coro_pp_sselect (aTHX) 303coro_pp_sselect (pTHX)
296{ 304{
297 dSP; 305 dSP;
298 PUSHMARK (SP - 4); /* fake argument list */ 306 PUSHMARK (SP - 4); /* fake argument list */
299 XPUSHs (coro_select_select); 307 XPUSHs (coro_select_select);
300 PUTBACK; 308 PUTBACK;
551 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 559 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
552} 560}
553 561
554/** load & save, init *******************************************************/ 562/** load & save, init *******************************************************/
555 563
564/* swap sv heads, at least logically */
565static void
566swap_svs (pTHX_ Coro__State c)
567{
568 int i;
569
570 for (i = 0; i <= AvFILLp (c->swap_sv); )
571 {
572 SV *a = AvARRAY (c->swap_sv)[i++];
573 SV *b = AvARRAY (c->swap_sv)[i++];
574
575 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
576 SV tmp;
577
578 /* swap sv_any */
579 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
580
581 /* swap sv_flags */
582 SvFLAGS (&tmp) = SvFLAGS (a);
583 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
584 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
585
586#if PERL_VERSION_ATLEAST (5,10,0)
587 /* perl 5.10 complicates this _quite_ a bit, but it also is
588 * is much faster, so no quarrels here. alternatively, we could
589 * sv_upgrade to avoid this.
590 */
591 {
592 /* swap sv_u */
593 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
594
595 /* if SvANY points to the head, we need to adjust the pointers,
596 * as the pointer for a still points to b, and maybe vice versa.
597 */
598 #define svany_in_head(type) \
599 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
600
601 if (svany_in_head (SvTYPE (a)))
602 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
603
604 if (svany_in_head (SvTYPE (b)))
605 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
606 }
607#endif
608 }
609}
610
611#define SWAP_SVS(coro) \
612 if (expect_false ((coro)->swap_sv)) \
613 swap_svs (aTHX_ (coro))
614
556static void 615static void
557on_enterleave_call (pTHX_ SV *cb); 616on_enterleave_call (pTHX_ SV *cb);
558 617
559static void 618static void
560load_perl (pTHX_ Coro__State c) 619load_perl (pTHX_ Coro__State c)
606 int i; 665 int i;
607 666
608 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 667 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
609 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 668 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
610 } 669 }
670
671 SWAP_SVS (c);
611} 672}
612 673
613static void 674static void
614save_perl (pTHX_ Coro__State c) 675save_perl (pTHX_ Coro__State c)
615{ 676{
677 SWAP_SVS (c);
678
616 if (expect_false (c->on_leave)) 679 if (expect_false (c->on_leave))
617 { 680 {
618 int i; 681 int i;
619 682
620 for (i = AvFILLp (c->on_leave); i >= 0; --i) 683 for (i = AvFILLp (c->on_leave); i >= 0; --i)
655 { 718 {
656 CV *cv = cx->blk_sub.cv; 719 CV *cv = cx->blk_sub.cv;
657 720
658 if (expect_true (CvDEPTH (cv))) 721 if (expect_true (CvDEPTH (cv)))
659 { 722 {
660 EXTEND (SP, 3);
661 PUSHs ((SV *)CvPADLIST (cv)); 723 PUSHs ((SV *)CvPADLIST (cv));
662 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 724 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
663 PUSHs ((SV *)cv); 725 PUSHs ((SV *)cv);
664 726
665 CvDEPTH (cv) = 0; 727 CvDEPTH (cv) = 0;
927slf_check_repeat (pTHX_ struct CoroSLF *frame) 989slf_check_repeat (pTHX_ struct CoroSLF *frame)
928{ 990{
929 return 1; 991 return 1;
930} 992}
931 993
932static UNOP coro_setup_op; 994static UNOP init_perl_op;
933 995
934static void NOINLINE /* noinline to keep it out of the transfer fast path */ 996static void NOINLINE /* noinline to keep it out of the transfer fast path */
935coro_setup (pTHX_ struct coro *coro) 997init_perl (pTHX_ struct coro *coro)
936{ 998{
937 /* 999 /*
938 * emulate part of the perl startup here. 1000 * emulate part of the perl startup here.
939 */ 1001 */
940 coro_init_stacks (aTHX); 1002 coro_init_stacks (aTHX);
989 */ 1051 */
990 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 1052 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
991 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 1053 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
992 1054
993 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 1055 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
994 coro_setup_op.op_next = PL_op; 1056 init_perl_op.op_next = PL_op;
995 coro_setup_op.op_type = OP_ENTERSUB; 1057 init_perl_op.op_type = OP_ENTERSUB;
996 coro_setup_op.op_ppaddr = pp_slf; 1058 init_perl_op.op_ppaddr = pp_slf;
997 /* no flags etc. required, as an init function won't be called */ 1059 /* no flags etc. required, as an init function won't be called */
998 1060
999 PL_op = (OP *)&coro_setup_op; 1061 PL_op = (OP *)&init_perl_op;
1000 1062
1001 /* copy throw, in case it was set before coro_setup */ 1063 /* copy throw, in case it was set before init_perl */
1002 CORO_THROW = coro->except; 1064 CORO_THROW = coro->except;
1065
1066 SWAP_SVS (coro);
1003 1067
1004 if (expect_false (enable_times)) 1068 if (expect_false (enable_times))
1005 { 1069 {
1006 coro_times_update (); 1070 coro_times_update ();
1007 coro_times_sub (coro); 1071 coro_times_sub (coro);
1028 dounwind (-1); 1092 dounwind (-1);
1029 } 1093 }
1030} 1094}
1031 1095
1032static void 1096static void
1033coro_destruct_perl (pTHX_ struct coro *coro) 1097destroy_perl (pTHX_ struct coro *coro)
1034{ 1098{
1035 SV *svf [9]; 1099 SV *svf [9];
1036 1100
1037 { 1101 {
1038 struct coro *current = SvSTATE_current; 1102 struct coro *current = SvSTATE_current;
1042 save_perl (aTHX_ current); 1106 save_perl (aTHX_ current);
1043 load_perl (aTHX_ coro); 1107 load_perl (aTHX_ coro);
1044 1108
1045 coro_unwind_stacks (aTHX); 1109 coro_unwind_stacks (aTHX);
1046 coro_destruct_stacks (aTHX); 1110 coro_destruct_stacks (aTHX);
1111
1112 /* restore swapped sv's */
1113 SWAP_SVS (coro);
1047 1114
1048 // now save some sv's to be free'd later 1115 // now save some sv's to be free'd later
1049 svf [0] = GvSV (PL_defgv); 1116 svf [0] = GvSV (PL_defgv);
1050 svf [1] = (SV *)GvAV (PL_defgv); 1117 svf [1] = (SV *)GvAV (PL_defgv);
1051 svf [2] = GvSV (PL_errgv); 1118 svf [2] = GvSV (PL_errgv);
1068 1135
1069 SvREFCNT_dec (coro->saved_deffh); 1136 SvREFCNT_dec (coro->saved_deffh);
1070 SvREFCNT_dec (coro->rouse_cb); 1137 SvREFCNT_dec (coro->rouse_cb);
1071 SvREFCNT_dec (coro->invoke_cb); 1138 SvREFCNT_dec (coro->invoke_cb);
1072 SvREFCNT_dec (coro->invoke_av); 1139 SvREFCNT_dec (coro->invoke_av);
1140 SvREFCNT_dec (coro->swap_sv);
1073 } 1141 }
1074} 1142}
1075 1143
1076INLINE void 1144INLINE void
1077free_coro_mortal (pTHX) 1145free_coro_mortal (pTHX)
1493 if (expect_false (next->flags & CF_NEW)) 1561 if (expect_false (next->flags & CF_NEW))
1494 { 1562 {
1495 /* need to start coroutine */ 1563 /* need to start coroutine */
1496 next->flags &= ~CF_NEW; 1564 next->flags &= ~CF_NEW;
1497 /* setup coroutine call */ 1565 /* setup coroutine call */
1498 coro_setup (aTHX_ next); 1566 init_perl (aTHX_ next);
1499 } 1567 }
1500 else 1568 else
1501 load_perl (aTHX_ next); 1569 load_perl (aTHX_ next);
1502 1570
1503 /* possibly untie and reuse the cctx */ 1571 /* possibly untie and reuse the cctx */
1566 1634
1567 if (coro->mainstack 1635 if (coro->mainstack
1568 && coro->mainstack != main_mainstack 1636 && coro->mainstack != main_mainstack
1569 && coro->slot 1637 && coro->slot
1570 && !PL_dirty) 1638 && !PL_dirty)
1571 coro_destruct_perl (aTHX_ coro); 1639 destroy_perl (aTHX_ coro);
1572 1640
1573 cctx_destroy (coro->cctx); 1641 cctx_destroy (coro->cctx);
1574 SvREFCNT_dec (coro->startcv); 1642 SvREFCNT_dec (coro->startcv);
1575 SvREFCNT_dec (coro->args); 1643 SvREFCNT_dec (coro->args);
1576 SvREFCNT_dec (CORO_THROW); 1644 SvREFCNT_dec (CORO_THROW);
1761 api_ready (aTHX_ SvRV (sv_idle)); 1829 api_ready (aTHX_ SvRV (sv_idle));
1762 --coro_nready; 1830 --coro_nready;
1763 } 1831 }
1764 else 1832 else
1765 { 1833 {
1834 /* TODO: deprecated, remove, cannot work reliably *//*D*/
1766 dSP; 1835 dSP;
1767 1836
1768 ENTER; 1837 ENTER;
1769 SAVETMPS; 1838 SAVETMPS;
1770 1839
2918#endif 2987#endif
2919 2988
2920/*****************************************************************************/ 2989/*****************************************************************************/
2921 2990
2922static SV * 2991static SV *
2923coro_new (HV *stash, SV **argv, int argc, int is_coro) 2992coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro)
2924{ 2993{
2925 SV *coro_sv; 2994 SV *coro_sv;
2926 struct coro *coro; 2995 struct coro *coro;
2927 MAGIC *mg; 2996 MAGIC *mg;
2928 HV *hv; 2997 HV *hv;
3054SV * 3123SV *
3055new (SV *klass, ...) 3124new (SV *klass, ...)
3056 ALIAS: 3125 ALIAS:
3057 Coro::new = 1 3126 Coro::new = 1
3058 CODE: 3127 CODE:
3059 RETVAL = coro_new (ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); 3128 RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix);
3060 OUTPUT: 3129 OUTPUT:
3061 RETVAL 3130 RETVAL
3062 3131
3063void 3132void
3064transfer (...) 3133transfer (...)
3315 3384
3316 if (expect_false (current == self)) 3385 if (expect_false (current == self))
3317 coro_times_sub (SvSTATE (coro_current)); 3386 coro_times_sub (SvSTATE (coro_current));
3318} 3387}
3319 3388
3389void
3390swap_sv (Coro::State coro, SV *sv, SV *swapsv)
3391 CODE:
3392{
3393 struct coro *current = SvSTATE_current;
3394
3395 if (current == coro)
3396 SWAP_SVS (current);
3397
3398 if (!coro->swap_sv)
3399 coro->swap_sv = newAV ();
3400
3401 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv )));
3402 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv)));
3403
3404 if (current == coro)
3405 SWAP_SVS (current);
3406}
3407
3408
3320MODULE = Coro::State PACKAGE = Coro 3409MODULE = Coro::State PACKAGE = Coro
3321 3410
3322BOOT: 3411BOOT:
3323{ 3412{
3324 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3413 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3332 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3421 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3333 3422
3334 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3423 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3335 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3424 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3336 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3425 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3337 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3426 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3338 3427
3339 coro_stash = gv_stashpv ("Coro", TRUE); 3428 coro_stash = gv_stashpv ("Coro", TRUE);
3340 3429
3341 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3430 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3342 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3431 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3343 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3432 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3365 3454
3366SV * 3455SV *
3367async (...) 3456async (...)
3368 PROTOTYPE: &@ 3457 PROTOTYPE: &@
3369 CODE: 3458 CODE:
3370 RETVAL = coro_new (coro_stash, &ST (0), items, 1); 3459 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1);
3371 api_ready (RETVAL); 3460 api_ready (aTHX_ RETVAL);
3372 OUTPUT: 3461 OUTPUT:
3373 RETVAL 3462 RETVAL
3374 3463
3375void 3464void
3376terminate (...) 3465terminate (...)
3496 for (i = 1; i < items; ++i) 3585 for (i = 1; i < items; ++i)
3497 av_push (av, SvREFCNT_inc_NN (ST (i))); 3586 av_push (av, SvREFCNT_inc_NN (ST (i)));
3498 3587
3499 if ((SV *)hv == &PL_sv_undef) 3588 if ((SV *)hv == &PL_sv_undef)
3500 { 3589 {
3501 PUSHMARK (SP); 3590 SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1);
3502 EXTEND (SP, 2);
3503 PUSHs (sv_Coro);
3504 PUSHs ((SV *)cv_pool_handler);
3505 PUTBACK;
3506 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3507 SPAGAIN;
3508
3509 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3591 hv = (HV *)SvREFCNT_inc_NN (SvRV (sv));
3592 SvREFCNT_dec (sv);
3510 } 3593 }
3511 3594
3512 { 3595 {
3513 struct coro *coro = SvSTATE_hv (hv); 3596 struct coro *coro = SvSTATE_hv (hv);
3514 3597

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines