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.453 by root, Tue Jul 7 00:33:25 2015 UTC vs.
Revision 1.465 by root, Sat Jun 25 19:04:04 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 */
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 U32 svany_in_head_set = (1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV); 719 U32 svany_in_head_set = (1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV);
715 #if NVSIZE <= IVSIZE && PERL_VERSION_ATLEAST(5,20,0) 720 #if NVSIZE <= IVSIZE && PERL_VERSION_ATLEAST(5,22,0)
716 svany_in_head_set |= 1 << SVt_NV; 721 svany_in_head_set |= 1 << SVt_NV;
717 #endif 722 #endif
718 723
719 #define svany_in_head(type) (svany_in_head_set & (1 << (type))) 724 #define svany_in_head(type) (svany_in_head_set & (1 << (type)))
720 725
727#endif 732#endif
728} 733}
729 734
730/* swap sv heads, at least logically */ 735/* swap sv heads, at least logically */
731static void 736static void
732swap_svs (pTHX_ Coro__State c) 737swap_svs_enter (pTHX_ Coro__State c)
733{ 738{
734 int i; 739 int i;
735 740
736 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) 741 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
737 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]);
738} 743}
739 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
740#define SWAP_SVS(coro) \ 754#define SWAP_SVS_ENTER(coro) \
741 if (ecb_expect_false ((coro)->swap_sv)) \ 755 if (ecb_expect_false ((coro)->swap_sv)) \
742 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))
743 761
744static void 762static void
745on_enterleave_call (pTHX_ SV *cb); 763on_enterleave_call (pTHX_ SV *cb);
746 764
747static void 765static void
800 818
801 for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2) 819 for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2)
802 ((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]);
803 } 821 }
804 822
805 SWAP_SVS (c); 823 SWAP_SVS_ENTER (c);
806} 824}
807 825
808static void 826static void
809save_perl (pTHX_ Coro__State c) 827save_perl (pTHX_ Coro__State c)
810{ 828{
811 SWAP_SVS (c); 829 SWAP_SVS_LEAVE (c);
812 830
813 if (ecb_expect_false (c->on_leave_xs)) 831 if (ecb_expect_false (c->on_leave_xs))
814 { 832 {
815 int i; 833 int i;
816 834
951#endif 969#endif
952 970
953 New(54,PL_savestack,24,ANY); 971 New(54,PL_savestack,24,ANY);
954 PL_savestack_ix = 0; 972 PL_savestack_ix = 0;
955 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
956 979
957#if !PERL_VERSION_ATLEAST (5,10,0) 980#if !PERL_VERSION_ATLEAST (5,10,0)
958 New(54,PL_retstack,4,OP*); 981 New(54,PL_retstack,4,OP*);
959 PL_retstack_ix = 0; 982 PL_retstack_ix = 0;
960 PL_retstack_max = 4; 983 PL_retstack_max = 4;
1026 } 1049 }
1027 1050
1028 return rss; 1051 return rss;
1029} 1052}
1030 1053
1031/** coroutine stack handling ************************************************/ 1054/** provide custom get/set/clear methods for %SIG elements ******************/
1032
1033static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
1034static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
1035static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
1036 1055
1037/* apparently < 5.8.8 */ 1056/* apparently < 5.8.8 */
1038#ifndef MgPV_nolen_const 1057#ifndef MgPV_nolen_const
1039#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 1058#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
1040 SvPV_nolen((SV*)((mg)->mg_ptr)) : \ 1059 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
1041 (const char*)(mg)->mg_ptr) 1060 (const char*)(mg)->mg_ptr)
1042#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};
1043 1081
1044/* 1082/*
1045 * This overrides the default magic get method of %SIG elements. 1083 * This overrides the default magic get method of %SIG elements.
1046 * 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
1047 * 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),
1073 sv_setsv (sv, ssv); 1111 sv_setsv (sv, ssv);
1074 return 0; 1112 return 0;
1075 } 1113 }
1076 } 1114 }
1077 1115
1078 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;
1079} 1117}
1080 1118
1081static int ecb_cold 1119static int ecb_cold
1082coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1120coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
1083{ 1121{
1097 SvREFCNT_dec (old); 1135 SvREFCNT_dec (old);
1098 return 0; 1136 return 0;
1099 } 1137 }
1100 } 1138 }
1101 1139
1102 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;
1103} 1141}
1104 1142
1105static int ecb_cold 1143static int ecb_cold
1106coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1144coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
1107{ 1145{
1121 SvREFCNT_dec (old); 1159 SvREFCNT_dec (old);
1122 return 0; 1160 return 0;
1123 } 1161 }
1124 } 1162 }
1125 1163
1126 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;
1127} 1165}
1128 1166
1129static void 1167static void
1130prepare_nop (pTHX_ struct coro_transfer_args *ta) 1168prepare_nop (pTHX_ struct coro_transfer_args *ta)
1131{ 1169{
1142static int 1180static int
1143slf_check_repeat (pTHX_ struct CoroSLF *frame) 1181slf_check_repeat (pTHX_ struct CoroSLF *frame)
1144{ 1182{
1145 return 1; 1183 return 1;
1146} 1184}
1185
1186/** coroutine stack handling ************************************************/
1147 1187
1148static UNOP init_perl_op; 1188static UNOP init_perl_op;
1149 1189
1150ecb_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 */
1151init_perl (pTHX_ struct coro *coro) 1191init_perl (pTHX_ struct coro *coro)
1218 PL_op = (OP *)&init_perl_op; 1258 PL_op = (OP *)&init_perl_op;
1219 1259
1220 /* copy throw, in case it was set before init_perl */ 1260 /* copy throw, in case it was set before init_perl */
1221 CORO_THROW = coro->except; 1261 CORO_THROW = coro->except;
1222 1262
1223 SWAP_SVS (coro); 1263 SWAP_SVS_ENTER (coro);
1224 1264
1225 if (ecb_expect_false (enable_times)) 1265 if (ecb_expect_false (enable_times))
1226 { 1266 {
1227 coro_times_update (); 1267 coro_times_update ();
1228 coro_times_sub (coro); 1268 coro_times_sub (coro);
1266 /* this will cause transfer_check to croak on block*/ 1306 /* this will cause transfer_check to croak on block*/
1267 SvRV_set (coro_current, (SV *)coro->hv); 1307 SvRV_set (coro_current, (SV *)coro->hv);
1268 1308
1269 load_perl (aTHX_ coro); 1309 load_perl (aTHX_ coro);
1270 1310
1311 /* restore swapped sv's */
1312 SWAP_SVS_LEAVE (coro);
1313
1271 coro_unwind_stacks (aTHX); 1314 coro_unwind_stacks (aTHX);
1272
1273 /* restore swapped sv's */
1274 SWAP_SVS (coro);
1275 1315
1276 coro_destruct_stacks (aTHX); 1316 coro_destruct_stacks (aTHX);
1277 1317
1278 /* now save some sv's to be free'd later */ 1318 /* now save some sv's to be free'd later */
1279 svf [0] = GvSV (PL_defgv); 1319 svf [0] = GvSV (PL_defgv);
1526 * 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
1527 * 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.
1528 */ 1568 */
1529 1569
1530 /* 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 /*
1531 * 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
1532 * 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)
1533 * 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
1534 * doing in that case. YMMV. 1581 * doing in that case. YMMV.
1535 */ 1582 */
2414 else 2461 else
2415 { 2462 {
2416 av_clear (GvAV (PL_defgv)); 2463 av_clear (GvAV (PL_defgv));
2417 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);
2418 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
2419 coro->prio = 0; 2473 coro->prio = 0;
2420 2474
2421 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2475 if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE))
2422 api_trace (aTHX_ coro_current, 0); 2476 api_trace (aTHX_ coro_current, 0);
2423 2477
2424 frame->prepare = prepare_schedule; 2478 frame->prepare = prepare_schedule;
2425 av_push (av_async_pool, SvREFCNT_inc (hv)); 2479 av_push (av_async_pool, SvREFCNT_inc (hv));
2426 } 2480 }
3600 cctx_current = cctx_new_empty (); 3654 cctx_current = cctx_new_empty ();
3601 3655
3602 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3656 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3603 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3657 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3604 3658
3605 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 3659 {
3606 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 3660 /*
3607 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 }
3608 3683
3609 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
3610 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));
3611 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));
3612 3686
3613 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 3687 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
3614 3688
3920 if (ecb_expect_false (current == self)) 3994 if (ecb_expect_false (current == self))
3921 coro_times_sub (SvSTATE (coro_current)); 3995 coro_times_sub (SvSTATE (coro_current));
3922} 3996}
3923 3997
3924void 3998void
3925swap_sv (Coro::State coro, SV *sv, SV *swapsv) 3999swap_sv (Coro::State coro, SV *sva, SV *svb)
3926 CODE: 4000 CODE:
3927{ 4001{
3928 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);
3929 4008
3930 if (current == coro) 4009 if (current == coro)
3931 SWAP_SVS (current); 4010 SWAP_SVS_LEAVE (current);
3932 4011
3933 if (!coro->swap_sv) 4012 if (!coro->swap_sv)
3934 coro->swap_sv = newAV (); 4013 coro->swap_sv = newAV ();
3935 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
3936 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); 4036 av_push (swap_sv, SvREFCNT_inc_NN (sva));
3937 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); 4037 av_push (swap_sv, SvREFCNT_inc_NN (svb));
4038
4039 removed:
3938 4040
3939 if (current == coro) 4041 if (current == coro)
3940 SWAP_SVS (current); 4042 SWAP_SVS_ENTER (current);
3941} 4043}
3942 4044
3943 4045
3944MODULE = Coro::State PACKAGE = Coro 4046MODULE = Coro::State PACKAGE = Coro
3945 4047
4259 RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); 4361 RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]);
4260 OUTPUT: 4362 OUTPUT:
4261 RETVAL 4363 RETVAL
4262 4364
4263void 4365void
4264up (SV *self, int adjust = 1) 4366up (SV *self)
4265 ALIAS:
4266 adjust = 1
4267 CODE: 4367 CODE:
4368 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), 1);
4369
4370void
4371adjust (SV *self, int adjust)
4372 CODE:
4268 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 4373 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), adjust);
4269 4374
4270void 4375void
4271down (...) 4376down (...)
4272 CODE: 4377 CODE:
4273 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