… | |
… | |
21 | |
21 | |
22 | #include <stddef.h> |
22 | #include <stddef.h> |
23 | #include <stdio.h> |
23 | #include <stdio.h> |
24 | #include <errno.h> |
24 | #include <errno.h> |
25 | #include <assert.h> |
25 | #include <assert.h> |
|
|
26 | |
|
|
27 | #ifndef SvREFCNT_dec_NN |
|
|
28 | #define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
29 | #endif |
|
|
30 | |
|
|
31 | #ifndef SvREFCNT_inc_NN |
|
|
32 | #define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
33 | #endif |
26 | |
34 | |
27 | #ifndef SVs_PADSTALE |
35 | #ifndef SVs_PADSTALE |
28 | # define SVs_PADSTALE 0 |
36 | # define SVs_PADSTALE 0 |
29 | #endif |
37 | #endif |
30 | |
38 | |
… | |
… | |
42 | # define PadlistNAMES(pl) (*PadlistARRAY (pl)) |
50 | # define PadlistNAMES(pl) (*PadlistARRAY (pl)) |
43 | # define PadARRAY AvARRAY |
51 | # define PadARRAY AvARRAY |
44 | # define PadMAX AvFILLp |
52 | # define PadMAX AvFILLp |
45 | # define newPADLIST(var) ((var) = newAV (), av_extend (var, 1)) |
53 | # define newPADLIST(var) ((var) = newAV (), av_extend (var, 1)) |
46 | #endif |
54 | #endif |
|
|
55 | #ifndef PadnamelistREFCNT |
|
|
56 | # define PadnamelistREFCNT(pnl) SvREFCNT (pnl) |
|
|
57 | #endif |
|
|
58 | #ifndef PadnamelistREFCNT_dec |
|
|
59 | # define PadnamelistREFCNT_dec(pnl) SvREFCNT_dec (pnl) |
|
|
60 | #endif |
47 | |
61 | |
48 | /* 5.19.something has replaced SVt_BIND by SVt_INVLIST */ |
62 | /* 5.19.something has replaced SVt_BIND by SVt_INVLIST */ |
49 | /* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */ |
63 | /* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */ |
50 | #if PERL_VERSION_ATLEAST(5,19,0) |
64 | #if PERL_VERSION_ATLEAST(5,19,0) |
51 | # define SVt_BIND SVt_IV |
65 | # define SVt_BIND SVt_IV |
… | |
… | |
102 | # define CORO_CLOCK_MONOTONIC 1 |
116 | # define CORO_CLOCK_MONOTONIC 1 |
103 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
117 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
104 | # endif |
118 | # endif |
105 | #endif |
119 | #endif |
106 | |
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 | |
107 | static double (*nvtime)(); /* so why doesn't it take void? */ |
127 | static double (*nvtime)(); /* so why doesn't it take void? */ |
108 | static void (*u2time)(pTHX_ UV ret[2]); |
128 | static void (*u2time)(pTHX_ UV ret[2]); |
109 | |
129 | |
110 | /* we hijack an hopefully unused CV flag for our purposes */ |
130 | /* we hijack an hopefully unused CV flag for our purposes */ |
111 | #define CVf_SLF 0x4000 |
131 | #define CVf_SLF 0x4000 |
… | |
… | |
126 | |
146 | |
127 | static GV *irsgv; /* $/ */ |
147 | static GV *irsgv; /* $/ */ |
128 | static GV *stdoutgv; /* *STDOUT */ |
148 | static GV *stdoutgv; /* *STDOUT */ |
129 | static SV *rv_diehook; |
149 | static SV *rv_diehook; |
130 | static SV *rv_warnhook; |
150 | static SV *rv_warnhook; |
131 | static HV *hv_sig; /* %SIG */ |
|
|
132 | |
151 | |
133 | /* async_pool helper stuff */ |
152 | /* async_pool helper stuff */ |
134 | static SV *sv_pool_rss; |
153 | static SV *sv_pool_rss; |
135 | static SV *sv_pool_size; |
154 | static SV *sv_pool_size; |
136 | static SV *sv_async_pool_idle; /* description string */ |
155 | static SV *sv_async_pool_idle; /* description string */ |
… | |
… | |
242 | SV *saved_deffh; |
261 | SV *saved_deffh; |
243 | SV *invoke_cb; |
262 | SV *invoke_cb; |
244 | AV *invoke_av; |
263 | AV *invoke_av; |
245 | |
264 | |
246 | /* on_enter/on_leave */ |
265 | /* on_enter/on_leave */ |
247 | AV *on_enter; |
266 | AV *on_enter; AV *on_enter_xs; |
248 | AV *on_leave; |
267 | AV *on_leave; AV *on_leave_xs; |
249 | |
268 | |
250 | /* swap_sv */ |
269 | /* swap_sv */ |
251 | AV *swap_sv; |
270 | AV *swap_sv; |
252 | |
271 | |
253 | /* times */ |
272 | /* times */ |
… | |
… | |
489 | |
508 | |
490 | return 0; |
509 | return 0; |
491 | } |
510 | } |
492 | |
511 | |
493 | ecb_inline struct coro * |
512 | ecb_inline struct coro * |
494 | SvSTATE_ (pTHX_ SV *coro) |
513 | SvSTATE_ (pTHX_ SV *coro_sv) |
495 | { |
514 | { |
496 | MAGIC *mg; |
515 | MAGIC *mg; |
497 | |
516 | |
498 | if (SvROK (coro)) |
517 | if (SvROK (coro_sv)) |
499 | coro = SvRV (coro); |
518 | coro_sv = SvRV (coro_sv); |
500 | |
519 | |
501 | mg = SvSTATEhv_p (aTHX_ coro); |
520 | mg = SvSTATEhv_p (aTHX_ coro_sv); |
502 | if (!mg) |
521 | if (!mg) |
503 | croak ("Coro::State object required"); |
522 | croak ("Coro::State object required"); |
504 | |
523 | |
505 | return (struct coro *)mg->mg_ptr; |
524 | return (struct coro *)mg->mg_ptr; |
506 | } |
525 | } |
… | |
… | |
517 | ecb_inline PADLIST * |
536 | ecb_inline PADLIST * |
518 | coro_derive_padlist (pTHX_ CV *cv) |
537 | coro_derive_padlist (pTHX_ CV *cv) |
519 | { |
538 | { |
520 | PADLIST *padlist = CvPADLIST (cv); |
539 | PADLIST *padlist = CvPADLIST (cv); |
521 | PADLIST *newpadlist; |
540 | PADLIST *newpadlist; |
|
|
541 | PADNAMELIST *padnames; |
522 | PAD *newpad; |
542 | PAD *newpad; |
523 | PADOFFSET off = PadlistMAX (padlist) + 1; |
543 | PADOFFSET off = PadlistMAX (padlist) + 1; |
524 | |
544 | |
525 | #if NEWPADAPI |
545 | #if NEWPADAPI |
526 | |
546 | |
… | |
… | |
553 | AvREAL_off (newpadlist); |
573 | AvREAL_off (newpadlist); |
554 | #endif |
574 | #endif |
555 | |
575 | |
556 | /* Already extended to 2 elements by newPADLIST. */ |
576 | /* Already extended to 2 elements by newPADLIST. */ |
557 | PadlistMAX (newpadlist) = 1; |
577 | PadlistMAX (newpadlist) = 1; |
558 | PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); |
578 | |
|
|
579 | padnames = PadlistNAMES (padlist); |
|
|
580 | ++PadnamelistREFCNT (padnames); |
|
|
581 | PadlistNAMES (newpadlist) = padnames; |
|
|
582 | |
559 | PadlistARRAY (newpadlist)[1] = newpad; |
583 | PadlistARRAY (newpadlist)[1] = newpad; |
560 | |
584 | |
561 | return newpadlist; |
585 | return newpadlist; |
562 | } |
586 | } |
563 | |
587 | |
… | |
… | |
584 | PadMAX (pad) = -1; |
608 | PadMAX (pad) = -1; |
585 | SvREFCNT_dec (pad); |
609 | SvREFCNT_dec (pad); |
586 | } |
610 | } |
587 | } |
611 | } |
588 | |
612 | |
589 | SvREFCNT_dec (PadlistNAMES (padlist)); |
613 | PadnamelistREFCNT_dec (PadlistNAMES (padlist)); |
590 | |
614 | |
591 | #if NEWPADAPI |
615 | #if NEWPADAPI |
592 | Safefree (PadlistARRAY (padlist)); |
616 | Safefree (PadlistARRAY (padlist)); |
593 | Safefree (padlist); |
617 | Safefree (padlist); |
594 | #else |
618 | #else |
… | |
… | |
690 | 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; |
691 | |
715 | |
692 | /* 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, |
693 | * 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. |
694 | */ |
718 | */ |
695 | #define svany_in_head(type) \ |
719 | U32 svany_in_head_set = (1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV); |
696 | (((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))) |
697 | |
725 | |
698 | if (svany_in_head (SvTYPE (a))) |
726 | if (svany_in_head (SvTYPE (a))) |
699 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
727 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
700 | |
728 | |
701 | if (svany_in_head (SvTYPE (b))) |
729 | if (svany_in_head (SvTYPE (b))) |
… | |
… | |
704 | #endif |
732 | #endif |
705 | } |
733 | } |
706 | |
734 | |
707 | /* swap sv heads, at least logically */ |
735 | /* swap sv heads, at least logically */ |
708 | static void |
736 | static void |
709 | swap_svs (pTHX_ Coro__State c) |
737 | swap_svs_enter (pTHX_ Coro__State c) |
710 | { |
738 | { |
711 | int i; |
739 | int i; |
712 | |
740 | |
713 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
741 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
714 | 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]); |
715 | } |
743 | } |
716 | |
744 | |
|
|
745 | static void |
|
|
746 | swap_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 | |
717 | #define SWAP_SVS(coro) \ |
754 | #define SWAP_SVS_ENTER(coro) \ |
718 | if (ecb_expect_false ((coro)->swap_sv)) \ |
755 | if (ecb_expect_false ((coro)->swap_sv)) \ |
719 | 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)) |
720 | |
761 | |
721 | static void |
762 | static void |
722 | on_enterleave_call (pTHX_ SV *cb); |
763 | on_enterleave_call (pTHX_ SV *cb); |
723 | |
764 | |
724 | static void |
765 | static void |
… | |
… | |
769 | |
810 | |
770 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
811 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
771 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
812 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
772 | } |
813 | } |
773 | |
814 | |
|
|
815 | if (ecb_expect_false (c->on_enter_xs)) |
|
|
816 | { |
|
|
817 | int i; |
|
|
818 | |
|
|
819 | for (i = 0; i <= AvFILLp (c->on_enter_xs); i += 2) |
|
|
820 | ((coro_enterleave_hook)AvARRAY (c->on_enter_xs)[i]) (aTHX_ AvARRAY (c->on_enter_xs)[i + 1]); |
|
|
821 | } |
|
|
822 | |
774 | SWAP_SVS (c); |
823 | SWAP_SVS_ENTER (c); |
775 | } |
824 | } |
776 | |
825 | |
777 | static void |
826 | static void |
778 | save_perl (pTHX_ Coro__State c) |
827 | save_perl (pTHX_ Coro__State c) |
779 | { |
828 | { |
780 | SWAP_SVS (c); |
829 | SWAP_SVS_LEAVE (c); |
|
|
830 | |
|
|
831 | if (ecb_expect_false (c->on_leave_xs)) |
|
|
832 | { |
|
|
833 | int i; |
|
|
834 | |
|
|
835 | for (i = AvFILLp (c->on_leave_xs) - 1; i >= 0; i -= 2) |
|
|
836 | ((coro_enterleave_hook)AvARRAY (c->on_leave_xs)[i]) (aTHX_ AvARRAY (c->on_leave_xs)[i + 1]); |
|
|
837 | } |
781 | |
838 | |
782 | if (ecb_expect_false (c->on_leave)) |
839 | if (ecb_expect_false (c->on_leave)) |
783 | { |
840 | { |
784 | int i; |
841 | int i; |
785 | |
842 | |
… | |
… | |
912 | #endif |
969 | #endif |
913 | |
970 | |
914 | New(54,PL_savestack,24,ANY); |
971 | New(54,PL_savestack,24,ANY); |
915 | PL_savestack_ix = 0; |
972 | PL_savestack_ix = 0; |
916 | 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 |
917 | |
979 | |
918 | #if !PERL_VERSION_ATLEAST (5,10,0) |
980 | #if !PERL_VERSION_ATLEAST (5,10,0) |
919 | New(54,PL_retstack,4,OP*); |
981 | New(54,PL_retstack,4,OP*); |
920 | PL_retstack_ix = 0; |
982 | PL_retstack_ix = 0; |
921 | PL_retstack_max = 4; |
983 | PL_retstack_max = 4; |
… | |
… | |
987 | } |
1049 | } |
988 | |
1050 | |
989 | return rss; |
1051 | return rss; |
990 | } |
1052 | } |
991 | |
1053 | |
992 | /** coroutine stack handling ************************************************/ |
1054 | /** provide custom get/set/clear methods for %SIG elements ******************/ |
993 | |
|
|
994 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
995 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
996 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
|
|
997 | |
1055 | |
998 | /* apparently < 5.8.8 */ |
1056 | /* apparently < 5.8.8 */ |
999 | #ifndef MgPV_nolen_const |
1057 | #ifndef MgPV_nolen_const |
1000 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1058 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
1001 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1059 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
1002 | (const char*)(mg)->mg_ptr) |
1060 | (const char*)(mg)->mg_ptr) |
1003 | #endif |
1061 | #endif |
|
|
1062 | |
|
|
1063 | /* this will be a patched copy of PL_vtbl_sigelem */ |
|
|
1064 | static MGVTBL coro_sigelem_vtbl; |
|
|
1065 | |
|
|
1066 | static int ecb_cold |
|
|
1067 | coro_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 */ |
|
|
1077 | static const MGVTBL coro_sig_vtbl = { |
|
|
1078 | 0, 0, 0, 0, 0, |
|
|
1079 | coro_sig_copy |
|
|
1080 | }; |
1004 | |
1081 | |
1005 | /* |
1082 | /* |
1006 | * This overrides the default magic get method of %SIG elements. |
1083 | * This overrides the default magic get method of %SIG elements. |
1007 | * 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 |
1008 | * 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), |
… | |
… | |
1034 | sv_setsv (sv, ssv); |
1111 | sv_setsv (sv, ssv); |
1035 | return 0; |
1112 | return 0; |
1036 | } |
1113 | } |
1037 | } |
1114 | } |
1038 | |
1115 | |
1039 | 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; |
1040 | } |
1117 | } |
1041 | |
1118 | |
1042 | static int ecb_cold |
1119 | static int ecb_cold |
1043 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1120 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1044 | { |
1121 | { |
… | |
… | |
1058 | SvREFCNT_dec (old); |
1135 | SvREFCNT_dec (old); |
1059 | return 0; |
1136 | return 0; |
1060 | } |
1137 | } |
1061 | } |
1138 | } |
1062 | |
1139 | |
1063 | 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; |
1064 | } |
1141 | } |
1065 | |
1142 | |
1066 | static int ecb_cold |
1143 | static int ecb_cold |
1067 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1144 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1068 | { |
1145 | { |
… | |
… | |
1082 | SvREFCNT_dec (old); |
1159 | SvREFCNT_dec (old); |
1083 | return 0; |
1160 | return 0; |
1084 | } |
1161 | } |
1085 | } |
1162 | } |
1086 | |
1163 | |
1087 | 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; |
1088 | } |
1165 | } |
1089 | |
1166 | |
1090 | static void |
1167 | static void |
1091 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1168 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
1092 | { |
1169 | { |
… | |
… | |
1103 | static int |
1180 | static int |
1104 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1181 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
1105 | { |
1182 | { |
1106 | return 1; |
1183 | return 1; |
1107 | } |
1184 | } |
|
|
1185 | |
|
|
1186 | /** coroutine stack handling ************************************************/ |
1108 | |
1187 | |
1109 | static UNOP init_perl_op; |
1188 | static UNOP init_perl_op; |
1110 | |
1189 | |
1111 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1190 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1112 | init_perl (pTHX_ struct coro *coro) |
1191 | init_perl (pTHX_ struct coro *coro) |
… | |
… | |
1138 | |
1217 | |
1139 | GvSV (PL_defgv) = newSV (0); |
1218 | GvSV (PL_defgv) = newSV (0); |
1140 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1219 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1141 | GvSV (PL_errgv) = newSV (0); |
1220 | GvSV (PL_errgv) = newSV (0); |
1142 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1221 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1143 | GvHV (PL_hintgv) = 0; |
1222 | GvHV (PL_hintgv) = newHV (); |
|
|
1223 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
1224 | hv_magic (GvHV (PL_hintgv), 0, PERL_MAGIC_hints); |
|
|
1225 | #endif |
1144 | PL_rs = newSVsv (GvSV (irsgv)); |
1226 | PL_rs = newSVsv (GvSV (irsgv)); |
1145 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1227 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1146 | |
1228 | |
1147 | { |
1229 | { |
1148 | dSP; |
1230 | dSP; |
… | |
… | |
1176 | PL_op = (OP *)&init_perl_op; |
1258 | PL_op = (OP *)&init_perl_op; |
1177 | |
1259 | |
1178 | /* copy throw, in case it was set before init_perl */ |
1260 | /* copy throw, in case it was set before init_perl */ |
1179 | CORO_THROW = coro->except; |
1261 | CORO_THROW = coro->except; |
1180 | |
1262 | |
1181 | SWAP_SVS (coro); |
1263 | SWAP_SVS_ENTER (coro); |
1182 | |
1264 | |
1183 | if (ecb_expect_false (enable_times)) |
1265 | if (ecb_expect_false (enable_times)) |
1184 | { |
1266 | { |
1185 | coro_times_update (); |
1267 | coro_times_update (); |
1186 | coro_times_sub (coro); |
1268 | coro_times_sub (coro); |
… | |
… | |
1224 | /* this will cause transfer_check to croak on block*/ |
1306 | /* this will cause transfer_check to croak on block*/ |
1225 | SvRV_set (coro_current, (SV *)coro->hv); |
1307 | SvRV_set (coro_current, (SV *)coro->hv); |
1226 | |
1308 | |
1227 | load_perl (aTHX_ coro); |
1309 | load_perl (aTHX_ coro); |
1228 | |
1310 | |
|
|
1311 | /* restore swapped sv's */ |
|
|
1312 | SWAP_SVS_LEAVE (coro); |
|
|
1313 | |
1229 | coro_unwind_stacks (aTHX); |
1314 | coro_unwind_stacks (aTHX); |
1230 | |
|
|
1231 | /* restore swapped sv's */ |
|
|
1232 | SWAP_SVS (coro); |
|
|
1233 | |
1315 | |
1234 | coro_destruct_stacks (aTHX); |
1316 | coro_destruct_stacks (aTHX); |
1235 | |
1317 | |
1236 | /* now save some sv's to be free'd later */ |
1318 | /* now save some sv's to be free'd later */ |
1237 | svf [0] = GvSV (PL_defgv); |
1319 | svf [0] = GvSV (PL_defgv); |
… | |
… | |
1258 | |
1340 | |
1259 | SvREFCNT_dec (coro->saved_deffh); |
1341 | SvREFCNT_dec (coro->saved_deffh); |
1260 | SvREFCNT_dec (coro->rouse_cb); |
1342 | SvREFCNT_dec (coro->rouse_cb); |
1261 | SvREFCNT_dec (coro->invoke_cb); |
1343 | SvREFCNT_dec (coro->invoke_cb); |
1262 | SvREFCNT_dec (coro->invoke_av); |
1344 | SvREFCNT_dec (coro->invoke_av); |
|
|
1345 | SvREFCNT_dec (coro->on_enter_xs); |
|
|
1346 | SvREFCNT_dec (coro->on_leave_xs); |
1263 | } |
1347 | } |
1264 | } |
1348 | } |
1265 | |
1349 | |
1266 | ecb_inline void |
1350 | ecb_inline void |
1267 | free_coro_mortal (pTHX) |
1351 | free_coro_mortal (pTHX) |
… | |
… | |
1330 | |
1414 | |
1331 | if (PL_curcop != &PL_compiling) |
1415 | if (PL_curcop != &PL_compiling) |
1332 | { |
1416 | { |
1333 | SV **cb; |
1417 | SV **cb; |
1334 | |
1418 | |
1335 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1419 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB && cxstack_ix >= 0) |
1336 | { |
1420 | { |
1337 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1421 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1338 | |
1422 | |
1339 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1423 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1340 | { |
1424 | { |
… | |
… | |
1482 | * 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 |
1483 | * 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. |
1484 | */ |
1568 | */ |
1485 | |
1569 | |
1486 | /* |
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 | /* |
1487 | * 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 |
1488 | * 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) |
1489 | * 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 |
1490 | * doing in that case. YMMV. |
1581 | * doing in that case. YMMV. |
1491 | */ |
1582 | */ |
… | |
… | |
1927 | |
2018 | |
1928 | ENTER; |
2019 | ENTER; |
1929 | SAVETMPS; |
2020 | SAVETMPS; |
1930 | |
2021 | |
1931 | PUSHMARK (SP); |
2022 | PUSHMARK (SP); |
1932 | XPUSHs (sv_2mortal (newSVpv ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught", 0))); |
2023 | XPUSHs (sv_2mortal (newSVpv ("FATAL: $Coro::idle blocked itself - did you try to block inside an event loop callback? Caught", 0))); |
1933 | PUTBACK; |
2024 | PUTBACK; |
1934 | call_pv ("Carp::confess", G_VOID | G_DISCARD); |
2025 | call_pv ("Carp::confess", G_VOID | G_DISCARD); |
1935 | |
2026 | |
1936 | FREETMPS; |
2027 | FREETMPS; |
1937 | LEAVE; |
2028 | LEAVE; |
… | |
… | |
2248 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2339 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2249 | } |
2340 | } |
2250 | else |
2341 | else |
2251 | { |
2342 | { |
2252 | struct coro *self = SvSTATE_current; |
2343 | struct coro *self = SvSTATE_current; |
|
|
2344 | |
|
|
2345 | if (!self) |
|
|
2346 | croak ("Coro::cancel called outside of thread content,"); |
2253 | |
2347 | |
2254 | /* otherwise we cancel directly, purely for speed reasons |
2348 | /* otherwise we cancel directly, purely for speed reasons |
2255 | * unfortunately, this requires some magic trickery, as |
2349 | * unfortunately, this requires some magic trickery, as |
2256 | * somebody else could cancel us, so we have to fight the cancellation. |
2350 | * somebody else could cancel us, so we have to fight the cancellation. |
2257 | * this is ugly, and hopefully fully worth the extra speed. |
2351 | * this is ugly, and hopefully fully worth the extra speed. |
… | |
… | |
2367 | else |
2461 | else |
2368 | { |
2462 | { |
2369 | av_clear (GvAV (PL_defgv)); |
2463 | av_clear (GvAV (PL_defgv)); |
2370 | 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); |
2371 | |
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 | |
2372 | coro->prio = 0; |
2473 | coro->prio = 0; |
2373 | |
2474 | |
2374 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2475 | if (ecb_expect_false (coro->cctx) && ecb_expect_false (coro->cctx->flags & CC_TRACE)) |
2375 | api_trace (aTHX_ coro_current, 0); |
2476 | api_trace (aTHX_ coro_current, 0); |
2376 | |
2477 | |
2377 | frame->prepare = prepare_schedule; |
2478 | frame->prepare = prepare_schedule; |
2378 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
2479 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
2379 | } |
2480 | } |
… | |
… | |
2608 | * Since by definition the SLF call will not finish when we destroy |
2709 | * Since by definition the SLF call will not finish when we destroy |
2609 | * the coro, we will have to force-finish it here, otherwise |
2710 | * the coro, we will have to force-finish it here, otherwise |
2610 | * cleanup functions cannot call SLF functions. |
2711 | * cleanup functions cannot call SLF functions. |
2611 | */ |
2712 | */ |
2612 | coro->slf_frame.prepare = 0; |
2713 | coro->slf_frame.prepare = 0; |
2613 | coro->slf_frame.destroy = 0; |
|
|
2614 | |
2714 | |
2615 | /* this callback is reserved for slf functions needing to do cleanup */ |
2715 | /* this callback is reserved for slf functions needing to do cleanup */ |
2616 | if (frame.destroy && frame.prepare && !PL_dirty) |
2716 | if (frame.destroy && frame.prepare && !PL_dirty) |
2617 | frame.destroy (aTHX_ &frame); |
2717 | frame.destroy (aTHX_ &frame); |
2618 | } |
2718 | } |
… | |
… | |
2805 | static void |
2905 | static void |
2806 | coro_pop_on_leave (pTHX_ void *coro) |
2906 | coro_pop_on_leave (pTHX_ void *coro) |
2807 | { |
2907 | { |
2808 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); |
2908 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); |
2809 | on_enterleave_call (aTHX_ sv_2mortal (cb)); |
2909 | on_enterleave_call (aTHX_ sv_2mortal (cb)); |
|
|
2910 | } |
|
|
2911 | |
|
|
2912 | static void |
|
|
2913 | enterleave_hook_xs (pTHX_ struct coro *coro, AV **avp, coro_enterleave_hook hook, void *arg) |
|
|
2914 | { |
|
|
2915 | if (!hook) |
|
|
2916 | return; |
|
|
2917 | |
|
|
2918 | if (!*avp) |
|
|
2919 | { |
|
|
2920 | *avp = newAV (); |
|
|
2921 | AvREAL_off (*avp); |
|
|
2922 | } |
|
|
2923 | |
|
|
2924 | av_push (*avp, (SV *)hook); |
|
|
2925 | av_push (*avp, (SV *)arg); |
|
|
2926 | } |
|
|
2927 | |
|
|
2928 | static void |
|
|
2929 | enterleave_unhook_xs (pTHX_ struct coro *coro, AV **avp, coro_enterleave_hook hook, int execute) |
|
|
2930 | { |
|
|
2931 | AV *av = *avp; |
|
|
2932 | int i; |
|
|
2933 | |
|
|
2934 | if (!av) |
|
|
2935 | return; |
|
|
2936 | |
|
|
2937 | for (i = AvFILLp (av) - 1; i >= 0; i -= 2) |
|
|
2938 | if (AvARRAY (av)[i] == (SV *)hook) |
|
|
2939 | { |
|
|
2940 | if (execute) |
|
|
2941 | hook (aTHX_ (void *)AvARRAY (av)[i + 1]); |
|
|
2942 | |
|
|
2943 | memmove (AvARRAY (av) + i, AvARRAY (av) + i + 2, AvFILLp (av) - i - 1); |
|
|
2944 | av_pop (av); |
|
|
2945 | av_pop (av); |
|
|
2946 | break; |
|
|
2947 | } |
|
|
2948 | |
|
|
2949 | if (AvFILLp (av) >= 0) |
|
|
2950 | { |
|
|
2951 | *avp = 0; |
|
|
2952 | SvREFCNT_dec_NN (av); |
|
|
2953 | } |
|
|
2954 | } |
|
|
2955 | |
|
|
2956 | static void |
|
|
2957 | api_enterleave_hook (pTHX_ SV *coro_sv, coro_enterleave_hook enter, void *enter_arg, coro_enterleave_hook leave, void *leave_arg) |
|
|
2958 | { |
|
|
2959 | struct coro *coro = SvSTATE (coro_sv); |
|
|
2960 | |
|
|
2961 | if (SvSTATE_current == coro) |
|
|
2962 | if (enter) |
|
|
2963 | enter (aTHX_ enter_arg); |
|
|
2964 | |
|
|
2965 | enterleave_hook_xs (aTHX_ coro, &coro->on_enter_xs, enter, enter_arg); |
|
|
2966 | enterleave_hook_xs (aTHX_ coro, &coro->on_leave_xs, leave, leave_arg); |
|
|
2967 | } |
|
|
2968 | |
|
|
2969 | static void |
|
|
2970 | api_enterleave_unhook (pTHX_ SV *coro_sv, coro_enterleave_hook enter, coro_enterleave_hook leave) |
|
|
2971 | { |
|
|
2972 | struct coro *coro = SvSTATE (coro_sv); |
|
|
2973 | |
|
|
2974 | enterleave_unhook_xs (aTHX_ coro, &coro->on_enter_xs, enter, 0); |
|
|
2975 | enterleave_unhook_xs (aTHX_ coro, &coro->on_leave_xs, leave, SvSTATE_current == coro); |
|
|
2976 | } |
|
|
2977 | |
|
|
2978 | static void |
|
|
2979 | savedestructor_unhook_enter (pTHX_ coro_enterleave_hook enter) |
|
|
2980 | { |
|
|
2981 | struct coro *coro = SvSTATE_current; |
|
|
2982 | |
|
|
2983 | enterleave_unhook_xs (aTHX_ coro, &coro->on_enter_xs, enter, 0); |
|
|
2984 | } |
|
|
2985 | |
|
|
2986 | static void |
|
|
2987 | savedestructor_unhook_leave (pTHX_ coro_enterleave_hook leave) |
|
|
2988 | { |
|
|
2989 | struct coro *coro = SvSTATE_current; |
|
|
2990 | |
|
|
2991 | enterleave_unhook_xs (aTHX_ coro, &coro->on_leave_xs, leave, 1); |
|
|
2992 | } |
|
|
2993 | |
|
|
2994 | static void |
|
|
2995 | api_enterleave_scope_hook (pTHX_ coro_enterleave_hook enter, void *enter_arg, coro_enterleave_hook leave, void *leave_arg) |
|
|
2996 | { |
|
|
2997 | api_enterleave_hook (aTHX_ coro_current, enter, enter_arg, leave, leave_arg); |
|
|
2998 | |
|
|
2999 | /* this ought to be much cheaper than malloc + a single destructor call */ |
|
|
3000 | if (enter) SAVEDESTRUCTOR_X (savedestructor_unhook_enter, enter); |
|
|
3001 | if (leave) SAVEDESTRUCTOR_X (savedestructor_unhook_leave, leave); |
2810 | } |
3002 | } |
2811 | |
3003 | |
2812 | /*****************************************************************************/ |
3004 | /*****************************************************************************/ |
2813 | /* PerlIO::cede */ |
3005 | /* PerlIO::cede */ |
2814 | |
3006 | |
… | |
… | |
2944 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
3136 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
2945 | PUTBACK; |
3137 | PUTBACK; |
2946 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
3138 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
2947 | } |
3139 | } |
2948 | |
3140 | |
2949 | SvREFCNT_dec (cb); |
3141 | SvREFCNT_dec_NN (cb); |
2950 | } |
3142 | } |
2951 | } |
3143 | } |
2952 | |
3144 | |
2953 | static void |
3145 | static void |
2954 | coro_semaphore_destroy (pTHX_ struct CoroSLF *frame) |
3146 | coro_semaphore_destroy (pTHX_ struct CoroSLF *frame) |
… | |
… | |
2962 | { |
3154 | { |
2963 | AV *av = (AV *)frame->data; |
3155 | AV *av = (AV *)frame->data; |
2964 | SV *count_sv = AvARRAY (av)[0]; |
3156 | SV *count_sv = AvARRAY (av)[0]; |
2965 | SV *coro_hv = SvRV (coro_current); |
3157 | SV *coro_hv = SvRV (coro_current); |
2966 | |
3158 | |
|
|
3159 | frame->destroy = 0; |
|
|
3160 | |
2967 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
3161 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2968 | if (CORO_THROW) |
3162 | if (ecb_expect_false (CORO_THROW)) |
|
|
3163 | { |
|
|
3164 | /* we still might be responsible for the semaphore, so wake up others */ |
|
|
3165 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
3166 | |
2969 | return 0; |
3167 | return 0; |
|
|
3168 | } |
2970 | else if (SvIVX (count_sv) > 0) |
3169 | else if (SvIVX (count_sv) > 0) |
2971 | { |
3170 | { |
2972 | frame->destroy = 0; |
|
|
2973 | |
|
|
2974 | if (acquire) |
3171 | if (acquire) |
2975 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
3172 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2976 | else |
3173 | else |
2977 | coro_semaphore_adjust (aTHX_ av, 0); |
3174 | coro_semaphore_adjust (aTHX_ av, 0); |
2978 | |
3175 | |
… | |
… | |
3089 | { |
3286 | { |
3090 | api_ready (aTHX_ cb); |
3287 | api_ready (aTHX_ cb); |
3091 | sv_setiv (cb, 0); /* signal waiter */ |
3288 | sv_setiv (cb, 0); /* signal waiter */ |
3092 | } |
3289 | } |
3093 | |
3290 | |
3094 | SvREFCNT_dec (cb); |
3291 | SvREFCNT_dec_NN (cb); |
3095 | |
3292 | |
3096 | --count; |
3293 | --count; |
3097 | } |
3294 | } |
3098 | } |
3295 | } |
3099 | |
3296 | |
… | |
… | |
3184 | } |
3381 | } |
3185 | |
3382 | |
3186 | av_push (state, data_sv); |
3383 | av_push (state, data_sv); |
3187 | |
3384 | |
3188 | api_ready (aTHX_ coro); |
3385 | api_ready (aTHX_ coro); |
3189 | SvREFCNT_dec (coro); |
3386 | SvREFCNT_dec_NN (coro); |
3190 | SvREFCNT_dec ((AV *)state); |
3387 | SvREFCNT_dec_NN ((AV *)state); |
3191 | } |
3388 | } |
3192 | |
3389 | |
3193 | static int |
3390 | static int |
3194 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
3391 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
3195 | { |
3392 | { |
… | |
… | |
3213 | errno = data->errorno; |
3410 | errno = data->errorno; |
3214 | PL_laststype = data->laststype; |
3411 | PL_laststype = data->laststype; |
3215 | PL_laststatval = data->laststatval; |
3412 | PL_laststatval = data->laststatval; |
3216 | PL_statcache = data->statcache; |
3413 | PL_statcache = data->statcache; |
3217 | |
3414 | |
3218 | SvREFCNT_dec (data_sv); |
3415 | SvREFCNT_dec_NN (data_sv); |
3219 | } |
3416 | } |
3220 | |
3417 | |
3221 | /* push result values */ |
3418 | /* push result values */ |
3222 | { |
3419 | { |
3223 | dSP; |
3420 | dSP; |
… | |
… | |
3438 | |
3635 | |
3439 | PROTOTYPES: DISABLE |
3636 | PROTOTYPES: DISABLE |
3440 | |
3637 | |
3441 | BOOT: |
3638 | BOOT: |
3442 | { |
3639 | { |
|
|
3640 | #define VARx(name,expr,type) if (sizeof (type) < sizeof (expr)) croak ("FATAL: Coro thread context slot '" # name "' too small for this version of perl."); |
|
|
3641 | #include "state.h" |
3443 | #ifdef USE_ITHREADS |
3642 | #ifdef USE_ITHREADS |
3444 | # if CORO_PTHREAD |
3643 | # if CORO_PTHREAD |
3445 | coro_thx = PERL_GET_CONTEXT; |
3644 | coro_thx = PERL_GET_CONTEXT; |
3446 | # endif |
3645 | # endif |
3447 | #endif |
3646 | #endif |
… | |
… | |
3455 | cctx_current = cctx_new_empty (); |
3654 | cctx_current = cctx_new_empty (); |
3456 | |
3655 | |
3457 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3656 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3458 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3657 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3459 | |
3658 | |
3460 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
3659 | { |
3461 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
3660 | /* |
3462 | 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 | } |
3463 | |
3683 | |
3464 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
|
|
3465 | 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)); |
3466 | 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)); |
3467 | |
3686 | |
3468 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3687 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
3469 | |
3688 | |
… | |
… | |
3775 | if (ecb_expect_false (current == self)) |
3994 | if (ecb_expect_false (current == self)) |
3776 | coro_times_sub (SvSTATE (coro_current)); |
3995 | coro_times_sub (SvSTATE (coro_current)); |
3777 | } |
3996 | } |
3778 | |
3997 | |
3779 | void |
3998 | void |
3780 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
3999 | swap_sv (Coro::State coro, SV *sva, SV *svb) |
3781 | CODE: |
4000 | CODE: |
3782 | { |
4001 | { |
3783 | 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); |
3784 | |
4008 | |
3785 | if (current == coro) |
4009 | if (current == coro) |
3786 | SWAP_SVS (current); |
4010 | SWAP_SVS_LEAVE (current); |
3787 | |
4011 | |
3788 | if (!coro->swap_sv) |
4012 | if (!coro->swap_sv) |
3789 | coro->swap_sv = newAV (); |
4013 | coro->swap_sv = newAV (); |
3790 | |
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 | |
3791 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); |
4036 | av_push (swap_sv, SvREFCNT_inc_NN (sva)); |
3792 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); |
4037 | av_push (swap_sv, SvREFCNT_inc_NN (svb)); |
|
|
4038 | |
|
|
4039 | removed: |
3793 | |
4040 | |
3794 | if (current == coro) |
4041 | if (current == coro) |
3795 | SWAP_SVS (current); |
4042 | SWAP_SVS_ENTER (current); |
3796 | } |
4043 | } |
3797 | |
4044 | |
3798 | |
4045 | |
3799 | MODULE = Coro::State PACKAGE = Coro |
4046 | MODULE = Coro::State PACKAGE = Coro |
3800 | |
4047 | |
3801 | BOOT: |
4048 | BOOT: |
3802 | { |
4049 | { |
|
|
4050 | if (SVt_LAST > 32) |
|
|
4051 | croak ("Coro internal error: SVt_LAST > 32, swap_sv might need adjustment"); |
|
|
4052 | |
3803 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
4053 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3804 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
4054 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3805 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
4055 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3806 | 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); |
3807 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
4057 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
… | |
… | |
3833 | coroapi.ready = api_ready; |
4083 | coroapi.ready = api_ready; |
3834 | coroapi.is_ready = api_is_ready; |
4084 | coroapi.is_ready = api_is_ready; |
3835 | coroapi.nready = coro_nready; |
4085 | coroapi.nready = coro_nready; |
3836 | coroapi.current = coro_current; |
4086 | coroapi.current = coro_current; |
3837 | |
4087 | |
|
|
4088 | coroapi.enterleave_hook = api_enterleave_hook; |
|
|
4089 | coroapi.enterleave_unhook = api_enterleave_unhook; |
|
|
4090 | coroapi.enterleave_scope_hook = api_enterleave_scope_hook; |
|
|
4091 | |
3838 | /*GCoroAPI = &coroapi;*/ |
4092 | /*GCoroAPI = &coroapi;*/ |
3839 | sv_setiv (sv, (IV)&coroapi); |
4093 | sv_setiv (sv, (IV)&coroapi); |
3840 | SvREADONLY_on (sv); |
4094 | SvREADONLY_on (sv); |
3841 | } |
4095 | } |
3842 | } |
4096 | } |
… | |
… | |
3907 | |
4161 | |
3908 | void |
4162 | void |
3909 | _set_current (SV *current) |
4163 | _set_current (SV *current) |
3910 | PROTOTYPE: $ |
4164 | PROTOTYPE: $ |
3911 | CODE: |
4165 | CODE: |
3912 | SvREFCNT_dec (SvRV (coro_current)); |
4166 | SvREFCNT_dec_NN (SvRV (coro_current)); |
3913 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
4167 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
3914 | |
4168 | |
3915 | void |
4169 | void |
3916 | _set_readyhook (SV *hook) |
4170 | _set_readyhook (SV *hook) |
3917 | PROTOTYPE: $ |
4171 | PROTOTYPE: $ |
… | |
… | |
4001 | |
4255 | |
4002 | if ((SV *)hv == &PL_sv_undef) |
4256 | if ((SV *)hv == &PL_sv_undef) |
4003 | { |
4257 | { |
4004 | SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); |
4258 | SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); |
4005 | hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); |
4259 | hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); |
4006 | SvREFCNT_dec (sv); |
4260 | SvREFCNT_dec_NN (sv); |
4007 | } |
4261 | } |
4008 | |
4262 | |
4009 | { |
4263 | { |
4010 | struct coro *coro = SvSTATE_hv (hv); |
4264 | struct coro *coro = SvSTATE_hv (hv); |
4011 | |
4265 | |
… | |
… | |
4018 | api_ready (aTHX_ (SV *)hv); |
4272 | api_ready (aTHX_ (SV *)hv); |
4019 | |
4273 | |
4020 | if (GIMME_V != G_VOID) |
4274 | if (GIMME_V != G_VOID) |
4021 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
4275 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
4022 | else |
4276 | else |
4023 | SvREFCNT_dec (hv); |
4277 | SvREFCNT_dec_NN (hv); |
4024 | } |
4278 | } |
4025 | |
4279 | |
4026 | SV * |
4280 | SV * |
4027 | rouse_cb () |
4281 | rouse_cb () |
4028 | PROTOTYPE: |
4282 | PROTOTYPE: |
… | |
… | |
4107 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
4361 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
4108 | OUTPUT: |
4362 | OUTPUT: |
4109 | RETVAL |
4363 | RETVAL |
4110 | |
4364 | |
4111 | void |
4365 | void |
4112 | up (SV *self, int adjust = 1) |
4366 | up (SV *self) |
4113 | ALIAS: |
|
|
4114 | adjust = 1 |
|
|
4115 | CODE: |
4367 | CODE: |
|
|
4368 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), 1); |
|
|
4369 | |
|
|
4370 | void |
|
|
4371 | adjust (SV *self, int adjust) |
|
|
4372 | CODE: |
4116 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
4373 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), adjust); |
4117 | |
4374 | |
4118 | void |
4375 | void |
4119 | down (...) |
4376 | down (...) |
4120 | CODE: |
4377 | CODE: |
4121 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
4378 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |