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.452 by root, Tue Jun 30 12:42:45 2015 UTC vs.
Revision 1.464 by root, Wed Jun 22 22:53:49 2016 UTC

116# define CORO_CLOCK_MONOTONIC 1 116# define CORO_CLOCK_MONOTONIC 1
117# define CORO_CLOCK_THREAD_CPUTIME_ID 3 117# define CORO_CLOCK_THREAD_CPUTIME_ID 3
118# endif 118# endif
119#endif 119#endif
120 120
121/* perl usually suppressed asserts. for debugging, we sometimes force it to be on */
122#if 0
123# undef NDEBUG
124# include <assert.h>
125#endif
126
121static double (*nvtime)(); /* so why doesn't it take void? */ 127static double (*nvtime)(); /* so why doesn't it take void? */
122static void (*u2time)(pTHX_ UV ret[2]); 128static void (*u2time)(pTHX_ UV ret[2]);
123 129
124/* we hijack an hopefully unused CV flag for our purposes */ 130/* we hijack an hopefully unused CV flag for our purposes */
125#define CVf_SLF 0x4000 131#define CVf_SLF 0x4000
140 146
141static GV *irsgv; /* $/ */ 147static GV *irsgv; /* $/ */
142static GV *stdoutgv; /* *STDOUT */ 148static GV *stdoutgv; /* *STDOUT */
143static SV *rv_diehook; 149static SV *rv_diehook;
144static SV *rv_warnhook; 150static SV *rv_warnhook;
145static HV *hv_sig; /* %SIG */
146 151
147/* async_pool helper stuff */ 152/* async_pool helper stuff */
148static SV *sv_pool_rss; 153static SV *sv_pool_rss;
149static SV *sv_pool_size; 154static SV *sv_pool_size;
150static SV *sv_async_pool_idle; /* description string */ 155static SV *sv_async_pool_idle; /* description string */
709 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; 714 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
710 715
711 /* if SvANY points to the head, we need to adjust the pointers, 716 /* if SvANY points to the head, we need to adjust the pointers,
712 * as the pointer for a still points to b, and maybe vice versa. 717 * as the pointer for a still points to b, and maybe vice versa.
713 */ 718 */
714 #define svany_in_head(type) \ 719 U32 svany_in_head_set = (1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV);
715 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) 720 #if NVSIZE <= IVSIZE && PERL_VERSION_ATLEAST(5,22,0)
721 svany_in_head_set |= 1 << SVt_NV;
722 #endif
723
724 #define svany_in_head(type) (svany_in_head_set & (1 << (type)))
716 725
717 if (svany_in_head (SvTYPE (a))) 726 if (svany_in_head (SvTYPE (a)))
718 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); 727 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
719 728
720 if (svany_in_head (SvTYPE (b))) 729 if (svany_in_head (SvTYPE (b)))
723#endif 732#endif
724} 733}
725 734
726/* swap sv heads, at least logically */ 735/* swap sv heads, at least logically */
727static void 736static void
728swap_svs (pTHX_ Coro__State c) 737swap_svs_enter (pTHX_ Coro__State c)
729{ 738{
730 int i; 739 int i;
731 740
732 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) 741 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
733 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); 742 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
734} 743}
735 744
745static void
746swap_svs_leave (pTHX_ Coro__State c)
747{
748 int i;
749
750 for (i = AvFILLp (c->swap_sv) - 1; i >= 0; i -= 2)
751 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
752}
753
736#define SWAP_SVS(coro) \ 754#define SWAP_SVS_ENTER(coro) \
737 if (ecb_expect_false ((coro)->swap_sv)) \ 755 if (ecb_expect_false ((coro)->swap_sv)) \
738 swap_svs (aTHX_ (coro)) 756 swap_svs_enter (aTHX_ (coro))
757
758#define SWAP_SVS_LEAVE(coro) \
759 if (ecb_expect_false ((coro)->swap_sv)) \
760 swap_svs_leave (aTHX_ (coro))
739 761
740static void 762static void
741on_enterleave_call (pTHX_ SV *cb); 763on_enterleave_call (pTHX_ SV *cb);
742 764
743static void 765static void
796 818
797 for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2) 819 for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2)
798 ((coro_enterleave_hook)AvARRAY (c->on_enter_xs)[i]) (aTHX_ AvARRAY (c->on_enter_xs)[i + 1]); 820 ((coro_enterleave_hook)AvARRAY (c->on_enter_xs)[i]) (aTHX_ AvARRAY (c->on_enter_xs)[i + 1]);
799 } 821 }
800 822
801 SWAP_SVS (c); 823 SWAP_SVS_ENTER (c);
802} 824}
803 825
804static void 826static void
805save_perl (pTHX_ Coro__State c) 827save_perl (pTHX_ Coro__State c)
806{ 828{
807 SWAP_SVS (c); 829 SWAP_SVS_LEAVE (c);
808 830
809 if (ecb_expect_false (c->on_leave_xs)) 831 if (ecb_expect_false (c->on_leave_xs))
810 { 832 {
811 int i; 833 int i;
812 834
947#endif 969#endif
948 970
949 New(54,PL_savestack,24,ANY); 971 New(54,PL_savestack,24,ANY);
950 PL_savestack_ix = 0; 972 PL_savestack_ix = 0;
951 PL_savestack_max = 24; 973 PL_savestack_max = 24;
974#if !PERL_VERSION_ATLEAST (5,24,0)
975 /* perl 5.24 moves SS_MAXPUSH optimisation from */
976 /* the header macros to PL_savestack_max */
977 PL_savestack_max -= SS_MAXPUSH;
978#endif
952 979
953#if !PERL_VERSION_ATLEAST (5,10,0) 980#if !PERL_VERSION_ATLEAST (5,10,0)
954 New(54,PL_retstack,4,OP*); 981 New(54,PL_retstack,4,OP*);
955 PL_retstack_ix = 0; 982 PL_retstack_ix = 0;
956 PL_retstack_max = 4; 983 PL_retstack_max = 4;
1022 } 1049 }
1023 1050
1024 return rss; 1051 return rss;
1025} 1052}
1026 1053
1027/** coroutine stack handling ************************************************/ 1054/** provide custom get/set/clear methods for %SIG elements ******************/
1028
1029static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
1030static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
1031static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
1032 1055
1033/* apparently < 5.8.8 */ 1056/* apparently < 5.8.8 */
1034#ifndef MgPV_nolen_const 1057#ifndef MgPV_nolen_const
1035#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 1058#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
1036 SvPV_nolen((SV*)((mg)->mg_ptr)) : \ 1059 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
1037 (const char*)(mg)->mg_ptr) 1060 (const char*)(mg)->mg_ptr)
1038#endif 1061#endif
1062
1063/* this will be a patched copy of PL_vtbl_sigelem */
1064static MGVTBL coro_sigelem_vtbl;
1065
1066static int ecb_cold
1067coro_sig_copy (pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *name, I32 namlen)
1068{
1069 sv_magic (nsv, mg->mg_obj, PERL_MAGIC_sigelem, name, namlen);
1070 assert (mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual == &PL_vtbl_sigelem);
1071 mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual = &coro_sigelem_vtbl;
1072 return 1;
1073}
1074
1075/* perl does not have a %SIG vtbl, we provide one so we can override */
1076/* the cwvtblagic for %SIG members */
1077static const MGVTBL coro_sig_vtbl = {
1078 0, 0, 0, 0, 0,
1079 coro_sig_copy
1080};
1039 1081
1040/* 1082/*
1041 * This overrides the default magic get method of %SIG elements. 1083 * This overrides the default magic get method of %SIG elements.
1042 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 1084 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
1043 * and instead of trying to save and restore the hash elements (extremely slow), 1085 * and instead of trying to save and restore the hash elements (extremely slow),
1069 sv_setsv (sv, ssv); 1111 sv_setsv (sv, ssv);
1070 return 0; 1112 return 0;
1071 } 1113 }
1072 } 1114 }
1073 1115
1074 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1116 return PL_vtbl_sigelem.svt_get ? PL_vtbl_sigelem.svt_get (aTHX_ sv, mg) : 0;
1075} 1117}
1076 1118
1077static int ecb_cold 1119static int ecb_cold
1078coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1120coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
1079{ 1121{
1093 SvREFCNT_dec (old); 1135 SvREFCNT_dec (old);
1094 return 0; 1136 return 0;
1095 } 1137 }
1096 } 1138 }
1097 1139
1098 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; 1140 return PL_vtbl_sigelem.svt_clear ? PL_vtbl_sigelem.svt_clear (aTHX_ sv, mg) : 0;
1099} 1141}
1100 1142
1101static int ecb_cold 1143static int ecb_cold
1102coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1144coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
1103{ 1145{
1117 SvREFCNT_dec (old); 1159 SvREFCNT_dec (old);
1118 return 0; 1160 return 0;
1119 } 1161 }
1120 } 1162 }
1121 1163
1122 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 1164 return PL_vtbl_sigelem.svt_set ? PL_vtbl_sigelem.svt_set (aTHX_ sv, mg) : 0;
1123} 1165}
1124 1166
1125static void 1167static void
1126prepare_nop (pTHX_ struct coro_transfer_args *ta) 1168prepare_nop (pTHX_ struct coro_transfer_args *ta)
1127{ 1169{
1138static int 1180static int
1139slf_check_repeat (pTHX_ struct CoroSLF *frame) 1181slf_check_repeat (pTHX_ struct CoroSLF *frame)
1140{ 1182{
1141 return 1; 1183 return 1;
1142} 1184}
1185
1186/** coroutine stack handling ************************************************/
1143 1187
1144static UNOP init_perl_op; 1188static UNOP init_perl_op;
1145 1189
1146ecb_noinline static void /* noinline to keep it out of the transfer fast path */ 1190ecb_noinline static void /* noinline to keep it out of the transfer fast path */
1147init_perl (pTHX_ struct coro *coro) 1191init_perl (pTHX_ struct coro *coro)
1214 PL_op = (OP *)&init_perl_op; 1258 PL_op = (OP *)&init_perl_op;
1215 1259
1216 /* copy throw, in case it was set before init_perl */ 1260 /* copy throw, in case it was set before init_perl */
1217 CORO_THROW = coro->except; 1261 CORO_THROW = coro->except;
1218 1262
1219 SWAP_SVS (coro); 1263 SWAP_SVS_ENTER (coro);
1220 1264
1221 if (ecb_expect_false (enable_times)) 1265 if (ecb_expect_false (enable_times))
1222 { 1266 {
1223 coro_times_update (); 1267 coro_times_update ();
1224 coro_times_sub (coro); 1268 coro_times_sub (coro);
1262 /* this will cause transfer_check to croak on block*/ 1306 /* this will cause transfer_check to croak on block*/
1263 SvRV_set (coro_current, (SV *)coro->hv); 1307 SvRV_set (coro_current, (SV *)coro->hv);
1264 1308
1265 load_perl (aTHX_ coro); 1309 load_perl (aTHX_ coro);
1266 1310
1311 /* restore swapped sv's */
1312 SWAP_SVS_LEAVE (coro);
1313
1267 coro_unwind_stacks (aTHX); 1314 coro_unwind_stacks (aTHX);
1268
1269 /* restore swapped sv's */
1270 SWAP_SVS (coro);
1271 1315
1272 coro_destruct_stacks (aTHX); 1316 coro_destruct_stacks (aTHX);
1273 1317
1274 /* now save some sv's to be free'd later */ 1318 /* now save some sv's to be free'd later */
1275 svf [0] = GvSV (PL_defgv); 1319 svf [0] = GvSV (PL_defgv);
1522 * coro body here, as perl_run destroys these. Likewise, we cannot catch 1566 * coro body here, as perl_run destroys these. Likewise, we cannot catch
1523 * runtime errors here, as this is just a random interpreter, not a thread. 1567 * runtime errors here, as this is just a random interpreter, not a thread.
1524 */ 1568 */
1525 1569
1526 /* 1570 /*
1571 * pp_entersub in 5.24 no longer ENTERs, but perl_destruct
1572 * requires PL_scopestack_ix, so do it here if required.
1573 */
1574 if (!PL_scopestack_ix)
1575 ENTER;
1576
1577 /*
1527 * If perl-run returns we assume exit() was being called or the coro 1578 * If perl-run returns we assume exit() was being called or the coro
1528 * fell off the end, which seems to be the only valid (non-bug) 1579 * fell off the end, which seems to be the only valid (non-bug)
1529 * reason for perl_run to return. We try to mimic whatever perl is normally 1580 * reason for perl_run to return. We try to mimic whatever perl is normally
1530 * doing in that case. YMMV. 1581 * doing in that case. YMMV.
1531 */ 1582 */
2410 else 2461 else
2411 { 2462 {
2412 av_clear (GvAV (PL_defgv)); 2463 av_clear (GvAV (PL_defgv));
2413 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); 2464 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
2414 2465
2466 if (ecb_expect_false (coro->swap_sv))
2467 {
2468 swap_svs_leave (coro);
2469 SvREFCNT_dec_NN (coro->swap_sv);
2470 coro->swap_sv = 0;
2471 }
2472
2415 coro->prio = 0; 2473 coro->prio = 0;
2416 2474
2417 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2475 if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE))
2418 api_trace (aTHX_ coro_current, 0); 2476 api_trace (aTHX_ coro_current, 0);
2419 2477
2420 frame->prepare = prepare_schedule; 2478 frame->prepare = prepare_schedule;
2421 av_push (av_async_pool, SvREFCNT_inc (hv)); 2479 av_push (av_async_pool, SvREFCNT_inc (hv));
2422 } 2480 }
3596 cctx_current = cctx_new_empty (); 3654 cctx_current = cctx_new_empty ();
3597 3655
3598 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3656 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3599 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3657 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3600 3658
3601 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 3659 {
3602 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 3660 /*
3603 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; 3661 * we provide a vtbvl for %SIG magic that replaces PL_vtbl_sig
3662 * by coro_sig_vtbl in hash values.
3663 */
3664 MAGIC *mg = mg_find ((SV *)GvHV (gv_fetchpv ("SIG", GV_ADD | GV_NOTQUAL, SVt_PVHV)), PERL_MAGIC_sig);
3665
3666 /* this only works if perl doesn't have a vtbl for %SIG */
3667 assert (!mg->mg_virtual);
3668
3669 /*
3670 * The irony is that the perl API itself asserts that mg_virtual
3671 * must be non-const, yet perl5porters insisted on marking their
3672 * vtbls as read-only, just to thwart perl modules from patching
3673 * them.
3674 */
3675 mg->mg_virtual = (MGVTBL *)&coro_sig_vtbl;
3676 mg->mg_flags |= MGf_COPY;
3677
3678 coro_sigelem_vtbl = PL_vtbl_sigelem;
3679 coro_sigelem_vtbl.svt_get = coro_sigelem_get;
3680 coro_sigelem_vtbl.svt_set = coro_sigelem_set;
3681 coro_sigelem_vtbl.svt_clear = coro_sigelem_clr;
3682 }
3604 3683
3605 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
3606 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 3684 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
3607 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); 3685 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
3608 3686
3609 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 3687 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
3610 3688
3916 if (ecb_expect_false (current == self)) 3994 if (ecb_expect_false (current == self))
3917 coro_times_sub (SvSTATE (coro_current)); 3995 coro_times_sub (SvSTATE (coro_current));
3918} 3996}
3919 3997
3920void 3998void
3921swap_sv (Coro::State coro, SV *sv, SV *swapsv) 3999swap_sv (Coro::State coro, SV *sva, SV *svb)
3922 CODE: 4000 CODE:
3923{ 4001{
3924 struct coro *current = SvSTATE_current; 4002 struct coro *current = SvSTATE_current;
4003 AV *swap_sv;
4004 int i;
4005
4006 sva = SvRV (sva);
4007 svb = SvRV (svb);
3925 4008
3926 if (current == coro) 4009 if (current == coro)
3927 SWAP_SVS (current); 4010 SWAP_SVS_LEAVE (current);
3928 4011
3929 if (!coro->swap_sv) 4012 if (!coro->swap_sv)
3930 coro->swap_sv = newAV (); 4013 coro->swap_sv = newAV ();
3931 4014
4015 swap_sv = coro->swap_sv;
4016
4017 for (i = AvFILLp (swap_sv) - 1; i >= 0; i -= 2)
4018 {
4019 SV *a = AvARRAY (swap_sv)[i ];
4020 SV *b = AvARRAY (swap_sv)[i + 1];
4021
4022 if (a == sva && b == svb)
4023 {
4024 SvREFCNT_dec_NN (a);
4025 SvREFCNT_dec_NN (b);
4026
4027 for (; i <= AvFILLp (swap_sv) - 2; i++)
4028 AvARRAY (swap_sv)[i] = AvARRAY (swap_sv)[i + 2];
4029
4030 AvFILLp (swap_sv) -= 2;
4031
4032 goto removed;
4033 }
4034 }
4035
3932 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); 4036 av_push (swap_sv, SvREFCNT_inc_NN (sva));
3933 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); 4037 av_push (swap_sv, SvREFCNT_inc_NN (svb));
4038
4039 removed:
3934 4040
3935 if (current == coro) 4041 if (current == coro)
3936 SWAP_SVS (current); 4042 SWAP_SVS_ENTER (current);
3937} 4043}
3938 4044
3939 4045
3940MODULE = Coro::State PACKAGE = Coro 4046MODULE = Coro::State PACKAGE = Coro
3941 4047
3942BOOT: 4048BOOT:
3943{ 4049{
4050 if (SVt_LAST > 32)
4051 croak ("Coro internal error: SVt_LAST > 32, swap_sv might need adjustment");
4052
3944 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 4053 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3945 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 4054 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3946 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); 4055 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3947 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 4056 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3948 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 4057 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
4252 RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); 4361 RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]);
4253 OUTPUT: 4362 OUTPUT:
4254 RETVAL 4363 RETVAL
4255 4364
4256void 4365void
4257up (SV *self, int adjust = 1) 4366up (SV *self)
4258 ALIAS:
4259 adjust = 1
4260 CODE: 4367 CODE:
4368 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), 1);
4369
4370void
4371adjust (SV *self, int adjust)
4372 CODE:
4261 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 4373 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), adjust);
4262 4374
4263void 4375void
4264down (...) 4376down (...)
4265 CODE: 4377 CODE:
4266 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 4378 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines