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.455 by root, Sun Oct 4 13:07:55 2015 UTC vs.
Revision 1.466 by root, Sat Jun 25 19:22:33 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
726#endif 732#endif
727} 733}
728 734
729/* swap sv heads, at least logically */ 735/* swap sv heads, at least logically */
730static void 736static void
731swap_svs (pTHX_ Coro__State c) 737swap_svs_enter (pTHX_ Coro__State c)
732{ 738{
733 int i; 739 int i;
734 740
735 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) 741 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
736 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]);
737} 743}
738 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
739#define SWAP_SVS(coro) \ 754#define SWAP_SVS_ENTER(coro) \
740 if (ecb_expect_false ((coro)->swap_sv)) \ 755 if (ecb_expect_false ((coro)->swap_sv)) \
741 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))
742 761
743static void 762static void
744on_enterleave_call (pTHX_ SV *cb); 763on_enterleave_call (pTHX_ SV *cb);
745 764
746static void 765static void
799 818
800 for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2) 819 for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2)
801 ((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]);
802 } 821 }
803 822
804 SWAP_SVS (c); 823 SWAP_SVS_ENTER (c);
805} 824}
806 825
807static void 826static void
808save_perl (pTHX_ Coro__State c) 827save_perl (pTHX_ Coro__State c)
809{ 828{
810 SWAP_SVS (c); 829 SWAP_SVS_LEAVE (c);
811 830
812 if (ecb_expect_false (c->on_leave_xs)) 831 if (ecb_expect_false (c->on_leave_xs))
813 { 832 {
814 int i; 833 int i;
815 834
950#endif 969#endif
951 970
952 New(54,PL_savestack,24,ANY); 971 New(54,PL_savestack,24,ANY);
953 PL_savestack_ix = 0; 972 PL_savestack_ix = 0;
954 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
955 979
956#if !PERL_VERSION_ATLEAST (5,10,0) 980#if !PERL_VERSION_ATLEAST (5,10,0)
957 New(54,PL_retstack,4,OP*); 981 New(54,PL_retstack,4,OP*);
958 PL_retstack_ix = 0; 982 PL_retstack_ix = 0;
959 PL_retstack_max = 4; 983 PL_retstack_max = 4;
1025 } 1049 }
1026 1050
1027 return rss; 1051 return rss;
1028} 1052}
1029 1053
1030/** coroutine stack handling ************************************************/ 1054/** provide custom get/set/clear methods for %SIG elements ******************/
1031
1032static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
1033static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
1034static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
1035 1055
1036/* apparently < 5.8.8 */ 1056/* apparently < 5.8.8 */
1037#ifndef MgPV_nolen_const 1057#ifndef MgPV_nolen_const
1038#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 1058#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
1039 SvPV_nolen((SV*)((mg)->mg_ptr)) : \ 1059 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
1040 (const char*)(mg)->mg_ptr) 1060 (const char*)(mg)->mg_ptr)
1041#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 char *key = SvPV_nolen ((SV *)name);
1070
1071 /* do what mg_copy normally does */
1072 sv_magic (nsv, mg->mg_obj, PERL_MAGIC_sigelem, name, namlen);
1073 assert (mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual == &PL_vtbl_sigelem);
1074
1075 /* patch sigelem vtbl, but only for __WARN__ and __DIE__ */
1076 if (*key == '_'
1077 && (strEQ (key, "__DIE__")
1078 || strEQ (key, "__WARN__")))
1079 mg_find (nsv, PERL_MAGIC_sigelem)->mg_virtual = &coro_sigelem_vtbl;
1080
1081 return 1;
1082}
1083
1084/* perl does not have a %SIG vtbl, we provide one so we can override */
1085/* the magic vtbl for the __DIE__ and __WARN__ members */
1086static const MGVTBL coro_sig_vtbl = {
1087 0, 0, 0, 0, 0,
1088 coro_sig_copy
1089};
1042 1090
1043/* 1091/*
1044 * This overrides the default magic get method of %SIG elements. 1092 * This overrides the default magic get method of %SIG elements.
1045 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 1093 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
1046 * and instead of trying to save and restore the hash elements (extremely slow), 1094 * and instead of trying to save and restore the hash elements (extremely slow),
1048 */ 1096 */
1049static int ecb_cold 1097static int ecb_cold
1050coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 1098coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
1051{ 1099{
1052 const char *s = MgPV_nolen_const (mg); 1100 const char *s = MgPV_nolen_const (mg);
1101 /* the key must be either __DIE__ or __WARN__ here */
1102 SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook;
1053 1103
1054 if (*s == '_') 1104 SV *ssv;
1055 {
1056 SV **svp = 0;
1057 1105
1058 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
1059 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
1060
1061 if (svp) 1106 if (!*svp)
1062 {
1063 SV *ssv;
1064
1065 if (!*svp)
1066 ssv = &PL_sv_undef; 1107 ssv = &PL_sv_undef;
1067 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ 1108 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1068 ssv = sv_2mortal (newRV_inc (*svp)); 1109 ssv = sv_2mortal (newRV_inc (*svp));
1069 else 1110 else
1070 ssv = *svp; 1111 ssv = *svp;
1071 1112
1072 sv_setsv (sv, ssv); 1113 sv_setsv (sv, ssv);
1073 return 0; 1114 return 0;
1074 }
1075 }
1076
1077 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
1078} 1115}
1079 1116
1080static int ecb_cold 1117static int ecb_cold
1081coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1118coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
1082{ 1119{
1083 const char *s = MgPV_nolen_const (mg); 1120 const char *s = MgPV_nolen_const (mg);
1121 /* the key must be either __DIE__ or __WARN__ here */
1122 SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook;
1084 1123
1085 if (*s == '_')
1086 {
1087 SV **svp = 0;
1088
1089 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
1090 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
1091
1092 if (svp)
1093 {
1094 SV *old = *svp; 1124 SV *old = *svp;
1095 *svp = 0; 1125 *svp = 0;
1096 SvREFCNT_dec (old); 1126 SvREFCNT_dec (old);
1097 return 0; 1127 return 0;
1098 }
1099 }
1100
1101 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
1102} 1128}
1103 1129
1104static int ecb_cold 1130static int ecb_cold
1105coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1131coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
1106{ 1132{
1107 const char *s = MgPV_nolen_const (mg); 1133 const char *s = MgPV_nolen_const (mg);
1134 /* the key must be either __DIE__ or __WARN__ here */
1135 SV **svp = s[2] == 'D' ? &PL_diehook : &PL_warnhook;
1108 1136
1109 if (*s == '_')
1110 {
1111 SV **svp = 0;
1112
1113 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
1114 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
1115
1116 if (svp)
1117 {
1118 SV *old = *svp; 1137 SV *old = *svp;
1119 *svp = SvOK (sv) ? newSVsv (sv) : 0; 1138 *svp = SvOK (sv) ? newSVsv (sv) : 0;
1120 SvREFCNT_dec (old); 1139 SvREFCNT_dec (old);
1121 return 0; 1140 return 0;
1122 }
1123 }
1124
1125 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
1126} 1141}
1127 1142
1128static void 1143static void
1129prepare_nop (pTHX_ struct coro_transfer_args *ta) 1144prepare_nop (pTHX_ struct coro_transfer_args *ta)
1130{ 1145{
1141static int 1156static int
1142slf_check_repeat (pTHX_ struct CoroSLF *frame) 1157slf_check_repeat (pTHX_ struct CoroSLF *frame)
1143{ 1158{
1144 return 1; 1159 return 1;
1145} 1160}
1161
1162/** coroutine stack handling ************************************************/
1146 1163
1147static UNOP init_perl_op; 1164static UNOP init_perl_op;
1148 1165
1149ecb_noinline static void /* noinline to keep it out of the transfer fast path */ 1166ecb_noinline static void /* noinline to keep it out of the transfer fast path */
1150init_perl (pTHX_ struct coro *coro) 1167init_perl (pTHX_ struct coro *coro)
1217 PL_op = (OP *)&init_perl_op; 1234 PL_op = (OP *)&init_perl_op;
1218 1235
1219 /* copy throw, in case it was set before init_perl */ 1236 /* copy throw, in case it was set before init_perl */
1220 CORO_THROW = coro->except; 1237 CORO_THROW = coro->except;
1221 1238
1222 SWAP_SVS (coro); 1239 SWAP_SVS_ENTER (coro);
1223 1240
1224 if (ecb_expect_false (enable_times)) 1241 if (ecb_expect_false (enable_times))
1225 { 1242 {
1226 coro_times_update (); 1243 coro_times_update ();
1227 coro_times_sub (coro); 1244 coro_times_sub (coro);
1265 /* this will cause transfer_check to croak on block*/ 1282 /* this will cause transfer_check to croak on block*/
1266 SvRV_set (coro_current, (SV *)coro->hv); 1283 SvRV_set (coro_current, (SV *)coro->hv);
1267 1284
1268 load_perl (aTHX_ coro); 1285 load_perl (aTHX_ coro);
1269 1286
1287 /* restore swapped sv's */
1288 SWAP_SVS_LEAVE (coro);
1289
1270 coro_unwind_stacks (aTHX); 1290 coro_unwind_stacks (aTHX);
1271
1272 /* restore swapped sv's */
1273 SWAP_SVS (coro);
1274 1291
1275 coro_destruct_stacks (aTHX); 1292 coro_destruct_stacks (aTHX);
1276 1293
1277 /* now save some sv's to be free'd later */ 1294 /* now save some sv's to be free'd later */
1278 svf [0] = GvSV (PL_defgv); 1295 svf [0] = GvSV (PL_defgv);
1525 * coro body here, as perl_run destroys these. Likewise, we cannot catch 1542 * coro body here, as perl_run destroys these. Likewise, we cannot catch
1526 * runtime errors here, as this is just a random interpreter, not a thread. 1543 * runtime errors here, as this is just a random interpreter, not a thread.
1527 */ 1544 */
1528 1545
1529 /* 1546 /*
1547 * pp_entersub in 5.24 no longer ENTERs, but perl_destruct
1548 * requires PL_scopestack_ix, so do it here if required.
1549 */
1550 if (!PL_scopestack_ix)
1551 ENTER;
1552
1553 /*
1530 * If perl-run returns we assume exit() was being called or the coro 1554 * If perl-run returns we assume exit() was being called or the coro
1531 * fell off the end, which seems to be the only valid (non-bug) 1555 * fell off the end, which seems to be the only valid (non-bug)
1532 * reason for perl_run to return. We try to mimic whatever perl is normally 1556 * reason for perl_run to return. We try to mimic whatever perl is normally
1533 * doing in that case. YMMV. 1557 * doing in that case. YMMV.
1534 */ 1558 */
2413 else 2437 else
2414 { 2438 {
2415 av_clear (GvAV (PL_defgv)); 2439 av_clear (GvAV (PL_defgv));
2416 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); 2440 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
2417 2441
2442 if (ecb_expect_false (coro->swap_sv))
2443 {
2444 swap_svs_leave (coro);
2445 SvREFCNT_dec_NN (coro->swap_sv);
2446 coro->swap_sv = 0;
2447 }
2448
2418 coro->prio = 0; 2449 coro->prio = 0;
2419 2450
2420 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2451 if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE))
2421 api_trace (aTHX_ coro_current, 0); 2452 api_trace (aTHX_ coro_current, 0);
2422 2453
2423 frame->prepare = prepare_schedule; 2454 frame->prepare = prepare_schedule;
2424 av_push (av_async_pool, SvREFCNT_inc (hv)); 2455 av_push (av_async_pool, SvREFCNT_inc (hv));
2425 } 2456 }
3599 cctx_current = cctx_new_empty (); 3630 cctx_current = cctx_new_empty ();
3600 3631
3601 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3632 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3602 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3633 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3603 3634
3604 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 3635 {
3605 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 3636 /*
3606 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; 3637 * we provide a vtbvl for %SIG magic that replaces PL_vtbl_sig
3638 * by coro_sig_vtbl in hash values.
3639 */
3640 MAGIC *mg = mg_find ((SV *)GvHV (gv_fetchpv ("SIG", GV_ADD | GV_NOTQUAL, SVt_PVHV)), PERL_MAGIC_sig);
3641
3642 /* this only works if perl doesn't have a vtbl for %SIG */
3643 assert (!mg->mg_virtual);
3644
3645 /*
3646 * The irony is that the perl API itself asserts that mg_virtual
3647 * must be non-const, yet perl5porters insisted on marking their
3648 * vtbls as read-only, just to thwart perl modules from patching
3649 * them.
3650 */
3651 mg->mg_virtual = (MGVTBL *)&coro_sig_vtbl;
3652 mg->mg_flags |= MGf_COPY;
3653
3654 coro_sigelem_vtbl = PL_vtbl_sigelem;
3655 coro_sigelem_vtbl.svt_get = coro_sigelem_get;
3656 coro_sigelem_vtbl.svt_set = coro_sigelem_set;
3657 coro_sigelem_vtbl.svt_clear = coro_sigelem_clr;
3658 }
3607 3659
3608 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 3660 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
3609 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); 3661 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
3610 3662
3611 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 3663 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
3918 if (ecb_expect_false (current == self)) 3970 if (ecb_expect_false (current == self))
3919 coro_times_sub (SvSTATE (coro_current)); 3971 coro_times_sub (SvSTATE (coro_current));
3920} 3972}
3921 3973
3922void 3974void
3923swap_sv (Coro::State coro, SV *sv, SV *swapsv) 3975swap_sv (Coro::State coro, SV *sva, SV *svb)
3924 CODE: 3976 CODE:
3925{ 3977{
3926 struct coro *current = SvSTATE_current; 3978 struct coro *current = SvSTATE_current;
3979 AV *swap_sv;
3980 int i;
3981
3982 sva = SvRV (sva);
3983 svb = SvRV (svb);
3927 3984
3928 if (current == coro) 3985 if (current == coro)
3929 SWAP_SVS (current); 3986 SWAP_SVS_LEAVE (current);
3930 3987
3931 if (!coro->swap_sv) 3988 if (!coro->swap_sv)
3932 coro->swap_sv = newAV (); 3989 coro->swap_sv = newAV ();
3933 3990
3991 swap_sv = coro->swap_sv;
3992
3993 for (i = AvFILLp (swap_sv) - 1; i >= 0; i -= 2)
3994 {
3995 SV *a = AvARRAY (swap_sv)[i ];
3996 SV *b = AvARRAY (swap_sv)[i + 1];
3997
3998 if (a == sva && b == svb)
3999 {
4000 SvREFCNT_dec_NN (a);
4001 SvREFCNT_dec_NN (b);
4002
4003 for (; i <= AvFILLp (swap_sv) - 2; i++)
4004 AvARRAY (swap_sv)[i] = AvARRAY (swap_sv)[i + 2];
4005
4006 AvFILLp (swap_sv) -= 2;
4007
4008 goto removed;
4009 }
4010 }
4011
3934 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); 4012 av_push (swap_sv, SvREFCNT_inc_NN (sva));
3935 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); 4013 av_push (swap_sv, SvREFCNT_inc_NN (svb));
4014
4015 removed:
3936 4016
3937 if (current == coro) 4017 if (current == coro)
3938 SWAP_SVS (current); 4018 SWAP_SVS_ENTER (current);
3939} 4019}
3940 4020
3941 4021
3942MODULE = Coro::State PACKAGE = Coro 4022MODULE = Coro::State PACKAGE = Coro
3943 4023
4257 RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); 4337 RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]);
4258 OUTPUT: 4338 OUTPUT:
4259 RETVAL 4339 RETVAL
4260 4340
4261void 4341void
4262up (SV *self, int adjust = 1) 4342up (SV *self)
4263 ALIAS:
4264 adjust = 1
4265 CODE: 4343 CODE:
4344 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), 1);
4345
4346void
4347adjust (SV *self, int adjust)
4348 CODE:
4266 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 4349 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), adjust);
4267 4350
4268void 4351void
4269down (...) 4352down (...)
4270 CODE: 4353 CODE:
4271 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 4354 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines