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.374 by root, Fri Oct 2 19:55:59 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
250 256
251 /* on_enter/on_leave */ 257 /* on_enter/on_leave */
252 AV *on_enter; 258 AV *on_enter;
253 AV *on_leave; 259 AV *on_leave;
254 260
261 /* swap_sv */
262 AV *swap_sv;
263
255 /* times */ 264 /* times */
256 coro_ts t_cpu, t_real; 265 coro_ts t_cpu, t_real;
257 266
258 /* linked list */ 267 /* linked list */
259 struct coro *next, *prev; 268 struct coro *next, *prev;
550 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 559 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
551} 560}
552 561
553/** load & save, init *******************************************************/ 562/** load & save, init *******************************************************/
554 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
555static void 615static void
556on_enterleave_call (pTHX_ SV *cb); 616on_enterleave_call (pTHX_ SV *cb);
557 617
558static void 618static void
559load_perl (pTHX_ Coro__State c) 619load_perl (pTHX_ Coro__State c)
605 int i; 665 int i;
606 666
607 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 667 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
608 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 668 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
609 } 669 }
670
671 SWAP_SVS (c);
610} 672}
611 673
612static void 674static void
613save_perl (pTHX_ Coro__State c) 675save_perl (pTHX_ Coro__State c)
614{ 676{
677 SWAP_SVS (c);
678
615 if (expect_false (c->on_leave)) 679 if (expect_false (c->on_leave))
616 { 680 {
617 int i; 681 int i;
618 682
619 for (i = AvFILLp (c->on_leave); i >= 0; --i) 683 for (i = AvFILLp (c->on_leave); i >= 0; --i)
654 { 718 {
655 CV *cv = cx->blk_sub.cv; 719 CV *cv = cx->blk_sub.cv;
656 720
657 if (expect_true (CvDEPTH (cv))) 721 if (expect_true (CvDEPTH (cv)))
658 { 722 {
659 EXTEND (SP, 3);
660 PUSHs ((SV *)CvPADLIST (cv)); 723 PUSHs ((SV *)CvPADLIST (cv));
661 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 724 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
662 PUSHs ((SV *)cv); 725 PUSHs ((SV *)cv);
663 726
664 CvDEPTH (cv) = 0; 727 CvDEPTH (cv) = 0;
926slf_check_repeat (pTHX_ struct CoroSLF *frame) 989slf_check_repeat (pTHX_ struct CoroSLF *frame)
927{ 990{
928 return 1; 991 return 1;
929} 992}
930 993
931static UNOP coro_setup_op; 994static UNOP init_perl_op;
932 995
933static 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 */
934coro_setup (pTHX_ struct coro *coro) 997init_perl (pTHX_ struct coro *coro)
935{ 998{
936 /* 999 /*
937 * emulate part of the perl startup here. 1000 * emulate part of the perl startup here.
938 */ 1001 */
939 coro_init_stacks (aTHX); 1002 coro_init_stacks (aTHX);
988 */ 1051 */
989 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 */
990 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 */
991 1054
992 /* 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 */
993 coro_setup_op.op_next = PL_op; 1056 init_perl_op.op_next = PL_op;
994 coro_setup_op.op_type = OP_ENTERSUB; 1057 init_perl_op.op_type = OP_ENTERSUB;
995 coro_setup_op.op_ppaddr = pp_slf; 1058 init_perl_op.op_ppaddr = pp_slf;
996 /* 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 */
997 1060
998 PL_op = (OP *)&coro_setup_op; 1061 PL_op = (OP *)&init_perl_op;
999 1062
1000 /* copy throw, in case it was set before coro_setup */ 1063 /* copy throw, in case it was set before init_perl */
1001 CORO_THROW = coro->except; 1064 CORO_THROW = coro->except;
1065
1066 SWAP_SVS (coro);
1002 1067
1003 if (expect_false (enable_times)) 1068 if (expect_false (enable_times))
1004 { 1069 {
1005 coro_times_update (); 1070 coro_times_update ();
1006 coro_times_sub (coro); 1071 coro_times_sub (coro);
1027 dounwind (-1); 1092 dounwind (-1);
1028 } 1093 }
1029} 1094}
1030 1095
1031static void 1096static void
1032coro_destruct_perl (pTHX_ struct coro *coro) 1097destroy_perl (pTHX_ struct coro *coro)
1033{ 1098{
1034 SV *svf [9]; 1099 SV *svf [9];
1035 1100
1036 { 1101 {
1037 struct coro *current = SvSTATE_current; 1102 struct coro *current = SvSTATE_current;
1041 save_perl (aTHX_ current); 1106 save_perl (aTHX_ current);
1042 load_perl (aTHX_ coro); 1107 load_perl (aTHX_ coro);
1043 1108
1044 coro_unwind_stacks (aTHX); 1109 coro_unwind_stacks (aTHX);
1045 coro_destruct_stacks (aTHX); 1110 coro_destruct_stacks (aTHX);
1111
1112 /* restore swapped sv's */
1113 SWAP_SVS (coro);
1046 1114
1047 // now save some sv's to be free'd later 1115 // now save some sv's to be free'd later
1048 svf [0] = GvSV (PL_defgv); 1116 svf [0] = GvSV (PL_defgv);
1049 svf [1] = (SV *)GvAV (PL_defgv); 1117 svf [1] = (SV *)GvAV (PL_defgv);
1050 svf [2] = GvSV (PL_errgv); 1118 svf [2] = GvSV (PL_errgv);
1067 1135
1068 SvREFCNT_dec (coro->saved_deffh); 1136 SvREFCNT_dec (coro->saved_deffh);
1069 SvREFCNT_dec (coro->rouse_cb); 1137 SvREFCNT_dec (coro->rouse_cb);
1070 SvREFCNT_dec (coro->invoke_cb); 1138 SvREFCNT_dec (coro->invoke_cb);
1071 SvREFCNT_dec (coro->invoke_av); 1139 SvREFCNT_dec (coro->invoke_av);
1140 SvREFCNT_dec (coro->swap_sv);
1072 } 1141 }
1073} 1142}
1074 1143
1075INLINE void 1144INLINE void
1076free_coro_mortal (pTHX) 1145free_coro_mortal (pTHX)
1492 if (expect_false (next->flags & CF_NEW)) 1561 if (expect_false (next->flags & CF_NEW))
1493 { 1562 {
1494 /* need to start coroutine */ 1563 /* need to start coroutine */
1495 next->flags &= ~CF_NEW; 1564 next->flags &= ~CF_NEW;
1496 /* setup coroutine call */ 1565 /* setup coroutine call */
1497 coro_setup (aTHX_ next); 1566 init_perl (aTHX_ next);
1498 } 1567 }
1499 else 1568 else
1500 load_perl (aTHX_ next); 1569 load_perl (aTHX_ next);
1501 1570
1502 /* possibly untie and reuse the cctx */ 1571 /* possibly untie and reuse the cctx */
1565 1634
1566 if (coro->mainstack 1635 if (coro->mainstack
1567 && coro->mainstack != main_mainstack 1636 && coro->mainstack != main_mainstack
1568 && coro->slot 1637 && coro->slot
1569 && !PL_dirty) 1638 && !PL_dirty)
1570 coro_destruct_perl (aTHX_ coro); 1639 destroy_perl (aTHX_ coro);
1571 1640
1572 cctx_destroy (coro->cctx); 1641 cctx_destroy (coro->cctx);
1573 SvREFCNT_dec (coro->startcv); 1642 SvREFCNT_dec (coro->startcv);
1574 SvREFCNT_dec (coro->args); 1643 SvREFCNT_dec (coro->args);
1575 SvREFCNT_dec (CORO_THROW); 1644 SvREFCNT_dec (CORO_THROW);
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 (sv));
3402 av_push (coro->swap_sv, SvREFCNT_inc_NN (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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines