… | |
… | |
4 | #define PERL_EXT |
4 | #define PERL_EXT |
5 | |
5 | |
6 | #include "EXTERN.h" |
6 | #include "EXTERN.h" |
7 | #include "perl.h" |
7 | #include "perl.h" |
8 | #include "XSUB.h" |
8 | #include "XSUB.h" |
|
|
9 | #include "perliol.h" |
9 | |
10 | |
10 | #include "patchlevel.h" |
11 | #include "patchlevel.h" |
11 | |
12 | |
12 | #include <stdio.h> |
13 | #include <stdio.h> |
13 | #include <errno.h> |
14 | #include <errno.h> |
… | |
… | |
51 | #else |
52 | #else |
52 | # define REGISTER_STACK(cctx,start,end) |
53 | # define REGISTER_STACK(cctx,start,end) |
53 | #endif |
54 | #endif |
54 | |
55 | |
55 | /* the maximum number of idle cctx that will be pooled */ |
56 | /* the maximum number of idle cctx that will be pooled */ |
56 | #define MAX_IDLE_CCTX 8 |
57 | static int cctx_max_idle = 4; |
57 | |
58 | |
58 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
59 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
59 | (PERL_REVISION > (a) \ |
60 | (PERL_REVISION > (a) \ |
60 | || (PERL_REVISION == (a) \ |
61 | || (PERL_REVISION == (a) \ |
61 | && (PERL_VERSION > (b) \ |
62 | && (PERL_VERSION > (b) \ |
… | |
… | |
80 | # ifndef IS_PADCONST |
81 | # ifndef IS_PADCONST |
81 | # define IS_PADCONST(v) 0 |
82 | # define IS_PADCONST(v) 0 |
82 | # endif |
83 | # endif |
83 | #endif |
84 | #endif |
84 | |
85 | |
|
|
86 | /* 5.11 */ |
|
|
87 | #ifndef CxHASARGS |
|
|
88 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | /* 5.10.0 */ |
|
|
92 | #ifndef SvREFCNT_inc_NN |
|
|
93 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
94 | #endif |
|
|
95 | |
85 | /* 5.8.8 */ |
96 | /* 5.8.8 */ |
86 | #ifndef GV_NOTQUAL |
97 | #ifndef GV_NOTQUAL |
87 | # define GV_NOTQUAL 0 |
98 | # define GV_NOTQUAL 0 |
88 | #endif |
99 | #endif |
89 | #ifndef newSV |
100 | #ifndef newSV |
90 | # define newSV(l) NEWSV(0,l) |
101 | # define newSV(l) NEWSV(0,l) |
91 | #endif |
|
|
92 | |
|
|
93 | /* 5.11 */ |
|
|
94 | #ifndef CxHASARGS |
|
|
95 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
96 | #endif |
102 | #endif |
97 | |
103 | |
98 | /* 5.8.7 */ |
104 | /* 5.8.7 */ |
99 | #ifndef SvRV_set |
105 | #ifndef SvRV_set |
100 | # define SvRV_set(s,v) SvRV(s) = (v) |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
… | |
… | |
136 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
137 | |
143 | |
138 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
139 | |
145 | |
140 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
|
|
147 | |
141 | static perl_mutex coro_mutex; |
148 | static perl_mutex coro_lock; |
142 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
143 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
150 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
151 | # if CORO_PTHREAD |
|
|
152 | static void *coro_thx; |
|
|
153 | # endif |
|
|
154 | |
144 | #else |
155 | #else |
|
|
156 | |
145 | # define LOCK (void)0 |
157 | # define LOCK (void)0 |
146 | # define UNLOCK (void)0 |
158 | # define UNLOCK (void)0 |
|
|
159 | |
147 | #endif |
160 | #endif |
|
|
161 | |
|
|
162 | # undef LOCK |
|
|
163 | # define LOCK (void)0 |
|
|
164 | # undef UNLOCK |
|
|
165 | # define UNLOCK (void)0 |
148 | |
166 | |
149 | /* helper storage struct for Coro::AIO */ |
167 | /* helper storage struct for Coro::AIO */ |
150 | struct io_state |
168 | struct io_state |
151 | { |
169 | { |
|
|
170 | AV *res; |
152 | int errorno; |
171 | int errorno; |
153 | I32 laststype; |
172 | I32 laststype; /* U16 in 5.10.0 */ |
154 | int laststatval; |
173 | int laststatval; |
155 | Stat_t statcache; |
174 | Stat_t statcache; |
156 | }; |
175 | }; |
157 | |
176 | |
|
|
177 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
178 | |
|
|
179 | static U32 cctx_gen; |
158 | static size_t coro_stacksize = CORO_STACKSIZE; |
180 | static size_t cctx_stacksize = CORO_STACKSIZE; |
159 | static struct CoroAPI coroapi; |
181 | static struct CoroAPI coroapi; |
160 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
161 | static JMPENV *main_top_env; |
183 | static JMPENV *main_top_env; |
162 | static HV *coro_state_stash, *coro_stash; |
184 | static HV *coro_state_stash, *coro_stash; |
163 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
185 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
|
|
186 | static volatile char next_has_throw; /* speedup flag for next->throw check */ |
164 | |
187 | |
165 | static GV *irsgv; /* $/ */ |
188 | static GV *irsgv; /* $/ */ |
166 | static GV *stdoutgv; /* *STDOUT */ |
189 | static GV *stdoutgv; /* *STDOUT */ |
167 | static SV *rv_diehook; |
190 | static SV *rv_diehook; |
168 | static SV *rv_warnhook; |
191 | static SV *rv_warnhook; |
… | |
… | |
200 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
223 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
201 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
224 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
202 | JMPENV *top_env; |
225 | JMPENV *top_env; |
203 | coro_context cctx; |
226 | coro_context cctx; |
204 | |
227 | |
|
|
228 | U32 gen; |
205 | #if CORO_USE_VALGRIND |
229 | #if CORO_USE_VALGRIND |
206 | int valgrind_id; |
230 | int valgrind_id; |
207 | #endif |
231 | #endif |
208 | unsigned char flags; |
232 | unsigned char flags; |
209 | } coro_cctx; |
233 | } coro_cctx; |
… | |
… | |
269 | #define PRIO_MIN -4 |
293 | #define PRIO_MIN -4 |
270 | |
294 | |
271 | /* for Coro.pm */ |
295 | /* for Coro.pm */ |
272 | static SV *coro_current; |
296 | static SV *coro_current; |
273 | static SV *coro_readyhook; |
297 | static SV *coro_readyhook; |
274 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
298 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
275 | static int coro_nready; |
299 | static int coro_nready; |
276 | static struct coro *coro_first; |
300 | static struct coro *coro_first; |
277 | |
301 | |
278 | /** lowlevel stuff **********************************************************/ |
302 | /** lowlevel stuff **********************************************************/ |
279 | |
303 | |
… | |
… | |
321 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
345 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
322 | #endif |
346 | #endif |
323 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
347 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
324 | --AvFILLp (padlist); |
348 | --AvFILLp (padlist); |
325 | |
349 | |
326 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
350 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
327 | av_store (newpadlist, 1, (SV *)newpad); |
351 | av_store (newpadlist, 1, (SV *)newpad); |
328 | |
352 | |
329 | return newpadlist; |
353 | return newpadlist; |
330 | } |
354 | } |
331 | |
355 | |
… | |
… | |
361 | |
385 | |
362 | /* casting is fun. */ |
386 | /* casting is fun. */ |
363 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
387 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
364 | free_padlist (aTHX_ padlist); |
388 | free_padlist (aTHX_ padlist); |
365 | |
389 | |
|
|
390 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
|
|
391 | |
366 | return 0; |
392 | return 0; |
367 | } |
393 | } |
368 | |
394 | |
369 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
395 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
370 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
396 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
… | |
… | |
372 | static MGVTBL coro_cv_vtbl = { |
398 | static MGVTBL coro_cv_vtbl = { |
373 | 0, 0, 0, 0, |
399 | 0, 0, 0, 0, |
374 | coro_cv_free |
400 | coro_cv_free |
375 | }; |
401 | }; |
376 | |
402 | |
377 | #define CORO_MAGIC(sv,type) \ |
403 | #define CORO_MAGIC(sv, type) \ |
378 | SvMAGIC (sv) \ |
404 | SvMAGIC (sv) \ |
379 | ? SvMAGIC (sv)->mg_type == type \ |
405 | ? SvMAGIC (sv)->mg_type == type \ |
380 | ? SvMAGIC (sv) \ |
406 | ? SvMAGIC (sv) \ |
381 | : mg_find (sv, type) \ |
407 | : mg_find (sv, type) \ |
382 | : 0 |
408 | : 0 |
383 | |
409 | |
384 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
410 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
385 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
411 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
386 | |
412 | |
387 | static struct coro * |
413 | static struct coro * |
… | |
… | |
420 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
446 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
421 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
447 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
422 | else |
448 | else |
423 | { |
449 | { |
424 | #if CORO_PREFER_PERL_FUNCTIONS |
450 | #if CORO_PREFER_PERL_FUNCTIONS |
425 | /* this is probably cleaner, but also slower? */ |
451 | /* this is probably cleaner? but also slower! */ |
|
|
452 | /* in practise, it seems to be less stable */ |
426 | CV *cp = Perl_cv_clone (cv); |
453 | CV *cp = Perl_cv_clone (cv); |
427 | CvPADLIST (cv) = CvPADLIST (cp); |
454 | CvPADLIST (cv) = CvPADLIST (cp); |
428 | CvPADLIST (cp) = 0; |
455 | CvPADLIST (cp) = 0; |
429 | SvREFCNT_dec (cp); |
456 | SvREFCNT_dec (cp); |
430 | #else |
457 | #else |
… | |
… | |
616 | |
643 | |
617 | /* |
644 | /* |
618 | * destroy the stacks, the callchain etc... |
645 | * destroy the stacks, the callchain etc... |
619 | */ |
646 | */ |
620 | static void |
647 | static void |
621 | coro_destroy_stacks (pTHX) |
648 | coro_destruct_stacks (pTHX) |
622 | { |
649 | { |
623 | while (PL_curstackinfo->si_next) |
650 | while (PL_curstackinfo->si_next) |
624 | PL_curstackinfo = PL_curstackinfo->si_next; |
651 | PL_curstackinfo = PL_curstackinfo->si_next; |
625 | |
652 | |
626 | while (PL_curstackinfo) |
653 | while (PL_curstackinfo) |
… | |
… | |
663 | #undef VAR |
690 | #undef VAR |
664 | } |
691 | } |
665 | else |
692 | else |
666 | slot = coro->slot; |
693 | slot = coro->slot; |
667 | |
694 | |
|
|
695 | if (slot) |
|
|
696 | { |
668 | rss += sizeof (slot->curstackinfo); |
697 | rss += sizeof (slot->curstackinfo); |
669 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
698 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
670 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
699 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
671 | rss += slot->tmps_max * sizeof (SV *); |
700 | rss += slot->tmps_max * sizeof (SV *); |
672 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
701 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
673 | rss += slot->scopestack_max * sizeof (I32); |
702 | rss += slot->scopestack_max * sizeof (I32); |
674 | rss += slot->savestack_max * sizeof (ANY); |
703 | rss += slot->savestack_max * sizeof (ANY); |
675 | |
704 | |
676 | #if !PERL_VERSION_ATLEAST (5,10,0) |
705 | #if !PERL_VERSION_ATLEAST (5,10,0) |
677 | rss += slot->retstack_max * sizeof (OP *); |
706 | rss += slot->retstack_max * sizeof (OP *); |
678 | #endif |
707 | #endif |
|
|
708 | } |
679 | } |
709 | } |
680 | |
710 | |
681 | return rss; |
711 | return rss; |
682 | } |
712 | } |
683 | |
713 | |
… | |
… | |
801 | GvSV (PL_defgv) = newSV (0); |
831 | GvSV (PL_defgv) = newSV (0); |
802 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
832 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
803 | GvSV (PL_errgv) = newSV (0); |
833 | GvSV (PL_errgv) = newSV (0); |
804 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
834 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
805 | PL_rs = newSVsv (GvSV (irsgv)); |
835 | PL_rs = newSVsv (GvSV (irsgv)); |
806 | PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); |
836 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
807 | |
837 | |
808 | { |
838 | { |
809 | dSP; |
839 | dSP; |
810 | LOGOP myop; |
840 | LOGOP myop; |
811 | |
841 | |
… | |
… | |
828 | */ |
858 | */ |
829 | ENTER; |
859 | ENTER; |
830 | } |
860 | } |
831 | |
861 | |
832 | static void |
862 | static void |
833 | coro_destroy (pTHX_ struct coro *coro) |
863 | coro_destruct (pTHX_ struct coro *coro) |
834 | { |
864 | { |
835 | if (!IN_DESTRUCT) |
865 | if (!IN_DESTRUCT) |
836 | { |
866 | { |
837 | /* restore all saved variables and stuff */ |
867 | /* restore all saved variables and stuff */ |
838 | LEAVE_SCOPE (0); |
868 | LEAVE_SCOPE (0); |
… | |
… | |
860 | SvREFCNT_dec (PL_warnhook); |
890 | SvREFCNT_dec (PL_warnhook); |
861 | |
891 | |
862 | SvREFCNT_dec (coro->saved_deffh); |
892 | SvREFCNT_dec (coro->saved_deffh); |
863 | SvREFCNT_dec (coro->throw); |
893 | SvREFCNT_dec (coro->throw); |
864 | |
894 | |
865 | coro_destroy_stacks (aTHX); |
895 | coro_destruct_stacks (aTHX); |
866 | } |
896 | } |
867 | |
897 | |
868 | static void |
898 | static void |
869 | free_coro_mortal (pTHX) |
899 | free_coro_mortal (pTHX) |
870 | { |
900 | { |
… | |
… | |
907 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
937 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
908 | : bot; |
938 | : bot; |
909 | |
939 | |
910 | av_extend (av, top - bot); |
940 | av_extend (av, top - bot); |
911 | while (bot < top) |
941 | while (bot < top) |
912 | av_push (av, SvREFCNT_inc (*bot++)); |
942 | av_push (av, SvREFCNT_inc_NN (*bot++)); |
913 | |
943 | |
914 | PL_runops = RUNOPS_DEFAULT; |
944 | PL_runops = RUNOPS_DEFAULT; |
915 | ENTER; |
945 | ENTER; |
916 | SAVETMPS; |
946 | SAVETMPS; |
917 | EXTEND (SP, 3); |
947 | EXTEND (SP, 3); |
… | |
… | |
1000 | } |
1030 | } |
1001 | |
1031 | |
1002 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1032 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1003 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1033 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1004 | /* during execution of a perl program */ |
1034 | /* during execution of a perl program */ |
|
|
1035 | /* also initialises PL_top_env */ |
1005 | static void NOINLINE |
1036 | static void NOINLINE |
1006 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1037 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1007 | { |
1038 | { |
1008 | dSP; |
1039 | dSP; |
1009 | LOGOP myop; |
1040 | LOGOP myop; |
… | |
… | |
1025 | PL_op = (OP *)&myop; |
1056 | PL_op = (OP *)&myop; |
1026 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1057 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1027 | SPAGAIN; |
1058 | SPAGAIN; |
1028 | } |
1059 | } |
1029 | |
1060 | |
|
|
1061 | /* the tail of transfer: execute stuff we can onyl do afetr a transfer */ |
|
|
1062 | static void |
|
|
1063 | transfer_tail (void) |
|
|
1064 | { |
|
|
1065 | UNLOCK; |
|
|
1066 | |
|
|
1067 | if (expect_false (next_has_throw)) |
|
|
1068 | { |
|
|
1069 | struct coro *coro = SvSTATE (coro_current); |
|
|
1070 | |
|
|
1071 | if (coro->throw) |
|
|
1072 | { |
|
|
1073 | SV *exception = coro->throw; |
|
|
1074 | coro->throw = 0; |
|
|
1075 | sv_setsv (ERRSV, exception); |
|
|
1076 | croak (0); |
|
|
1077 | } |
|
|
1078 | } |
|
|
1079 | } |
|
|
1080 | |
1030 | /* |
1081 | /* |
1031 | * this is a _very_ stripped down perl interpreter ;) |
1082 | * this is a _very_ stripped down perl interpreter ;) |
1032 | */ |
1083 | */ |
1033 | static void |
1084 | static void |
1034 | cctx_run (void *arg) |
1085 | cctx_run (void *arg) |
1035 | { |
1086 | { |
|
|
1087 | #ifdef USE_ITHREADS |
|
|
1088 | # if CORO_PTHREAD |
|
|
1089 | PERL_SET_CONTEXT (coro_thx); |
|
|
1090 | # endif |
|
|
1091 | #endif |
|
|
1092 | { |
1036 | dTHX; |
1093 | dTHX; |
1037 | |
1094 | |
1038 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
|
|
1039 | UNLOCK; |
|
|
1040 | |
|
|
1041 | /* we now skip the entersub that lead to transfer() */ |
1095 | /* we now skip the entersub that lead to transfer () */ |
1042 | PL_op = PL_op->op_next; |
1096 | PL_op = PL_op->op_next; |
1043 | |
1097 | |
1044 | /* inject a fake subroutine call to cctx_init */ |
1098 | /* inject a fake subroutine call to cctx_init */ |
1045 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1099 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1046 | |
1100 | |
|
|
1101 | /* cctx_run is the alternative tail of transfer () */ |
|
|
1102 | transfer_tail (); |
|
|
1103 | |
1047 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1104 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1048 | PL_restartop = PL_op; |
1105 | PL_restartop = PL_op; |
1049 | perl_run (PL_curinterp); |
1106 | perl_run (PL_curinterp); |
1050 | |
1107 | |
1051 | /* |
1108 | /* |
1052 | * If perl-run returns we assume exit() was being called or the coro |
1109 | * If perl-run returns we assume exit() was being called or the coro |
1053 | * fell off the end, which seems to be the only valid (non-bug) |
1110 | * fell off the end, which seems to be the only valid (non-bug) |
1054 | * reason for perl_run to return. We try to exit by jumping to the |
1111 | * reason for perl_run to return. We try to exit by jumping to the |
1055 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1112 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1056 | * coroutine as Coro has no such concept |
1113 | * coroutine as Coro has no such concept |
1057 | */ |
1114 | */ |
1058 | PL_top_env = main_top_env; |
1115 | PL_top_env = main_top_env; |
1059 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1116 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1117 | } |
1060 | } |
1118 | } |
1061 | |
1119 | |
1062 | static coro_cctx * |
1120 | static coro_cctx * |
1063 | cctx_new () |
1121 | cctx_new () |
1064 | { |
1122 | { |
1065 | coro_cctx *cctx; |
1123 | coro_cctx *cctx; |
|
|
1124 | |
|
|
1125 | ++cctx_count; |
|
|
1126 | New (0, cctx, 1, coro_cctx); |
|
|
1127 | |
|
|
1128 | cctx->gen = cctx_gen; |
|
|
1129 | cctx->flags = 0; |
|
|
1130 | cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel */ |
|
|
1131 | |
|
|
1132 | return cctx; |
|
|
1133 | } |
|
|
1134 | |
|
|
1135 | /* create a new cctx only suitable as source */ |
|
|
1136 | static coro_cctx * |
|
|
1137 | cctx_new_empty () |
|
|
1138 | { |
|
|
1139 | coro_cctx *cctx = cctx_new (); |
|
|
1140 | |
|
|
1141 | cctx->sptr = 0; |
|
|
1142 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
|
|
1143 | |
|
|
1144 | return cctx; |
|
|
1145 | } |
|
|
1146 | |
|
|
1147 | /* create a new cctx suitable as destination/running a perl interpreter */ |
|
|
1148 | static coro_cctx * |
|
|
1149 | cctx_new_run () |
|
|
1150 | { |
|
|
1151 | coro_cctx *cctx = cctx_new (); |
1066 | void *stack_start; |
1152 | void *stack_start; |
1067 | size_t stack_size; |
1153 | size_t stack_size; |
1068 | |
1154 | |
1069 | ++cctx_count; |
|
|
1070 | |
|
|
1071 | Newz (0, cctx, 1, coro_cctx); |
|
|
1072 | |
|
|
1073 | #if HAVE_MMAP |
1155 | #if HAVE_MMAP |
1074 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1156 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1075 | /* mmap supposedly does allocate-on-write for us */ |
1157 | /* mmap supposedly does allocate-on-write for us */ |
1076 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1158 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1077 | |
1159 | |
1078 | if (cctx->sptr != (void *)-1) |
1160 | if (cctx->sptr != (void *)-1) |
1079 | { |
1161 | { |
… | |
… | |
1085 | cctx->flags |= CC_MAPPED; |
1167 | cctx->flags |= CC_MAPPED; |
1086 | } |
1168 | } |
1087 | else |
1169 | else |
1088 | #endif |
1170 | #endif |
1089 | { |
1171 | { |
1090 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1172 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1091 | New (0, cctx->sptr, coro_stacksize, long); |
1173 | New (0, cctx->sptr, cctx_stacksize, long); |
1092 | |
1174 | |
1093 | if (!cctx->sptr) |
1175 | if (!cctx->sptr) |
1094 | { |
1176 | { |
1095 | perror ("FATAL: unable to allocate stack for coroutine"); |
1177 | perror ("FATAL: unable to allocate stack for coroutine"); |
1096 | _exit (EXIT_FAILURE); |
1178 | _exit (EXIT_FAILURE); |
… | |
… | |
1111 | { |
1193 | { |
1112 | if (!cctx) |
1194 | if (!cctx) |
1113 | return; |
1195 | return; |
1114 | |
1196 | |
1115 | --cctx_count; |
1197 | --cctx_count; |
|
|
1198 | coro_destroy (&cctx->cctx); |
1116 | |
1199 | |
|
|
1200 | /* coro_transfer creates new, empty cctx's */ |
|
|
1201 | if (cctx->sptr) |
|
|
1202 | { |
1117 | #if CORO_USE_VALGRIND |
1203 | #if CORO_USE_VALGRIND |
1118 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1204 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1119 | #endif |
1205 | #endif |
1120 | |
1206 | |
1121 | #if HAVE_MMAP |
1207 | #if HAVE_MMAP |
1122 | if (cctx->flags & CC_MAPPED) |
1208 | if (cctx->flags & CC_MAPPED) |
1123 | munmap (cctx->sptr, cctx->ssize); |
1209 | munmap (cctx->sptr, cctx->ssize); |
1124 | else |
1210 | else |
1125 | #endif |
1211 | #endif |
1126 | Safefree (cctx->sptr); |
1212 | Safefree (cctx->sptr); |
|
|
1213 | } |
1127 | |
1214 | |
1128 | Safefree (cctx); |
1215 | Safefree (cctx); |
1129 | } |
1216 | } |
1130 | |
1217 | |
1131 | /* wether this cctx should be destructed */ |
1218 | /* wether this cctx should be destructed */ |
1132 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1219 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1133 | |
1220 | |
1134 | static coro_cctx * |
1221 | static coro_cctx * |
1135 | cctx_get (pTHX) |
1222 | cctx_get (pTHX) |
1136 | { |
1223 | { |
1137 | while (expect_true (cctx_first)) |
1224 | while (expect_true (cctx_first)) |
… | |
… | |
1144 | return cctx; |
1231 | return cctx; |
1145 | |
1232 | |
1146 | cctx_destroy (cctx); |
1233 | cctx_destroy (cctx); |
1147 | } |
1234 | } |
1148 | |
1235 | |
1149 | return cctx_new (); |
1236 | return cctx_new_run (); |
1150 | } |
1237 | } |
1151 | |
1238 | |
1152 | static void |
1239 | static void |
1153 | cctx_put (coro_cctx *cctx) |
1240 | cctx_put (coro_cctx *cctx) |
1154 | { |
1241 | { |
|
|
1242 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1243 | |
1155 | /* free another cctx if overlimit */ |
1244 | /* free another cctx if overlimit */ |
1156 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1245 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1157 | { |
1246 | { |
1158 | coro_cctx *first = cctx_first; |
1247 | coro_cctx *first = cctx_first; |
1159 | cctx_first = first->next; |
1248 | cctx_first = first->next; |
1160 | --cctx_idle; |
1249 | --cctx_idle; |
1161 | |
1250 | |
… | |
… | |
1193 | /* always use the TRANSFER macro */ |
1282 | /* always use the TRANSFER macro */ |
1194 | static void NOINLINE |
1283 | static void NOINLINE |
1195 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1284 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1196 | { |
1285 | { |
1197 | dSTACKLEVEL; |
1286 | dSTACKLEVEL; |
1198 | static volatile int has_throw; |
|
|
1199 | |
1287 | |
1200 | /* sometimes transfer is only called to set idle_sp */ |
1288 | /* sometimes transfer is only called to set idle_sp */ |
1201 | if (expect_false (!next)) |
1289 | if (expect_false (!next)) |
1202 | { |
1290 | { |
1203 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1291 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1207 | { |
1295 | { |
1208 | coro_cctx *prev__cctx; |
1296 | coro_cctx *prev__cctx; |
1209 | |
1297 | |
1210 | if (expect_false (prev->flags & CF_NEW)) |
1298 | if (expect_false (prev->flags & CF_NEW)) |
1211 | { |
1299 | { |
1212 | /* create a new empty context */ |
1300 | /* create a new empty/source context */ |
1213 | Newz (0, prev->cctx, 1, coro_cctx); |
1301 | prev->cctx = cctx_new_empty (); |
1214 | prev->flags &= ~CF_NEW; |
1302 | prev->flags &= ~CF_NEW; |
1215 | prev->flags |= CF_RUNNING; |
1303 | prev->flags |= CF_RUNNING; |
1216 | } |
1304 | } |
1217 | |
1305 | |
1218 | prev->flags &= ~CF_RUNNING; |
1306 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1233 | else |
1321 | else |
1234 | load_perl (aTHX_ next); |
1322 | load_perl (aTHX_ next); |
1235 | |
1323 | |
1236 | prev__cctx = prev->cctx; |
1324 | prev__cctx = prev->cctx; |
1237 | |
1325 | |
|
|
1326 | if (prev__cctx->idle_sp == STACKLEVEL) asm volatile("");//D |
|
|
1327 | |
1238 | /* possibly "free" the cctx */ |
1328 | /* possibly "free" the cctx */ |
1239 | if (expect_true ( |
1329 | if (expect_true ( |
1240 | prev__cctx->idle_sp == STACKLEVEL |
1330 | prev__cctx->idle_sp == STACKLEVEL |
1241 | && !(prev__cctx->flags & CC_TRACE) |
1331 | && !(prev__cctx->flags & CC_TRACE) |
1242 | && !force_cctx |
1332 | && !force_cctx |
… | |
… | |
1259 | ++next->usecount; |
1349 | ++next->usecount; |
1260 | |
1350 | |
1261 | if (expect_true (!next->cctx)) |
1351 | if (expect_true (!next->cctx)) |
1262 | next->cctx = cctx_get (aTHX); |
1352 | next->cctx = cctx_get (aTHX); |
1263 | |
1353 | |
1264 | has_throw = !!next->throw; |
1354 | next_has_throw = !!next->throw; |
1265 | |
1355 | |
1266 | if (expect_false (prev__cctx != next->cctx)) |
1356 | if (expect_false (prev__cctx != next->cctx)) |
1267 | { |
1357 | { |
1268 | prev__cctx->top_env = PL_top_env; |
1358 | prev__cctx->top_env = PL_top_env; |
1269 | PL_top_env = next->cctx->top_env; |
1359 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1271 | } |
1361 | } |
1272 | |
1362 | |
1273 | free_coro_mortal (aTHX); |
1363 | free_coro_mortal (aTHX); |
1274 | UNLOCK; |
1364 | UNLOCK; |
1275 | |
1365 | |
1276 | if (expect_false (has_throw)) |
1366 | transfer_tail (); |
1277 | { |
|
|
1278 | struct coro *coro = SvSTATE (coro_current); |
|
|
1279 | |
|
|
1280 | if (coro->throw) |
|
|
1281 | { |
|
|
1282 | SV *exception = coro->throw; |
|
|
1283 | coro->throw = 0; |
|
|
1284 | sv_setsv (ERRSV, exception); |
|
|
1285 | croak (0); |
|
|
1286 | } |
|
|
1287 | } |
|
|
1288 | } |
1367 | } |
1289 | } |
1368 | } |
1290 | |
1369 | |
1291 | struct transfer_args |
1370 | struct transfer_args |
1292 | { |
1371 | { |
… | |
… | |
1325 | croak ("FATAL: tried to destroy currently running coroutine"); |
1404 | croak ("FATAL: tried to destroy currently running coroutine"); |
1326 | |
1405 | |
1327 | save_perl (aTHX_ &temp); |
1406 | save_perl (aTHX_ &temp); |
1328 | load_perl (aTHX_ coro); |
1407 | load_perl (aTHX_ coro); |
1329 | |
1408 | |
1330 | coro_destroy (aTHX_ coro); |
1409 | coro_destruct (aTHX_ coro); |
1331 | |
1410 | |
1332 | load_perl (aTHX_ &temp); |
1411 | load_perl (aTHX_ &temp); |
1333 | |
1412 | |
1334 | coro->slot = 0; |
1413 | coro->slot = 0; |
1335 | } |
1414 | } |
… | |
… | |
1441 | LOCK; |
1520 | LOCK; |
1442 | |
1521 | |
1443 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1522 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1444 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1523 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1445 | |
1524 | |
1446 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1525 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1447 | ++coro_nready; |
1526 | ++coro_nready; |
1448 | |
1527 | |
1449 | UNLOCK; |
1528 | UNLOCK; |
1450 | |
1529 | |
1451 | if (sv_hook) |
1530 | if (sv_hook) |
… | |
… | |
1606 | struct coro *coro = SvSTATE (coro_sv); |
1685 | struct coro *coro = SvSTATE (coro_sv); |
1607 | |
1686 | |
1608 | if (flags & CC_TRACE) |
1687 | if (flags & CC_TRACE) |
1609 | { |
1688 | { |
1610 | if (!coro->cctx) |
1689 | if (!coro->cctx) |
1611 | coro->cctx = cctx_new (); |
1690 | coro->cctx = cctx_new_run (); |
1612 | else if (!(coro->cctx->flags & CC_TRACE)) |
1691 | else if (!(coro->cctx->flags & CC_TRACE)) |
1613 | croak ("cannot enable tracing on coroutine with custom stack"); |
1692 | croak ("cannot enable tracing on coroutine with custom stack"); |
1614 | |
1693 | |
1615 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1694 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1616 | } |
1695 | } |
… | |
… | |
1623 | else |
1702 | else |
1624 | coro->slot->runops = RUNOPS_DEFAULT; |
1703 | coro->slot->runops = RUNOPS_DEFAULT; |
1625 | } |
1704 | } |
1626 | } |
1705 | } |
1627 | |
1706 | |
|
|
1707 | #if 0 |
|
|
1708 | static int |
|
|
1709 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
1710 | { |
|
|
1711 | AV *padlist; |
|
|
1712 | AV *av = (AV *)mg->mg_obj; |
|
|
1713 | |
|
|
1714 | abort (); |
|
|
1715 | |
|
|
1716 | return 0; |
|
|
1717 | } |
|
|
1718 | |
|
|
1719 | static MGVTBL coro_gensub_vtbl = { |
|
|
1720 | 0, 0, 0, 0, |
|
|
1721 | coro_gensub_free |
|
|
1722 | }; |
|
|
1723 | #endif |
|
|
1724 | |
|
|
1725 | /*****************************************************************************/ |
|
|
1726 | /* PerlIO::cede */ |
|
|
1727 | |
|
|
1728 | typedef struct |
|
|
1729 | { |
|
|
1730 | PerlIOBuf base; |
|
|
1731 | NV next, every; |
|
|
1732 | } PerlIOCede; |
|
|
1733 | |
|
|
1734 | static IV |
|
|
1735 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
|
|
1736 | { |
|
|
1737 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1738 | |
|
|
1739 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
|
|
1740 | self->next = nvtime () + self->every; |
|
|
1741 | |
|
|
1742 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
|
|
1743 | } |
|
|
1744 | |
|
|
1745 | static SV * |
|
|
1746 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
|
|
1747 | { |
|
|
1748 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1749 | |
|
|
1750 | return newSVnv (self->every); |
|
|
1751 | } |
|
|
1752 | |
|
|
1753 | static IV |
|
|
1754 | PerlIOCede_flush (pTHX_ PerlIO *f) |
|
|
1755 | { |
|
|
1756 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1757 | double now = nvtime (); |
|
|
1758 | |
|
|
1759 | if (now >= self->next) |
|
|
1760 | { |
|
|
1761 | api_cede (); |
|
|
1762 | self->next = now + self->every; |
|
|
1763 | } |
|
|
1764 | |
|
|
1765 | return PerlIOBuf_flush (aTHX_ f); |
|
|
1766 | } |
|
|
1767 | |
|
|
1768 | static PerlIO_funcs PerlIO_cede = |
|
|
1769 | { |
|
|
1770 | sizeof(PerlIO_funcs), |
|
|
1771 | "cede", |
|
|
1772 | sizeof(PerlIOCede), |
|
|
1773 | PERLIO_K_DESTRUCT | PERLIO_K_RAW, |
|
|
1774 | PerlIOCede_pushed, |
|
|
1775 | PerlIOBuf_popped, |
|
|
1776 | PerlIOBuf_open, |
|
|
1777 | PerlIOBase_binmode, |
|
|
1778 | PerlIOCede_getarg, |
|
|
1779 | PerlIOBase_fileno, |
|
|
1780 | PerlIOBuf_dup, |
|
|
1781 | PerlIOBuf_read, |
|
|
1782 | PerlIOBuf_unread, |
|
|
1783 | PerlIOBuf_write, |
|
|
1784 | PerlIOBuf_seek, |
|
|
1785 | PerlIOBuf_tell, |
|
|
1786 | PerlIOBuf_close, |
|
|
1787 | PerlIOCede_flush, |
|
|
1788 | PerlIOBuf_fill, |
|
|
1789 | PerlIOBase_eof, |
|
|
1790 | PerlIOBase_error, |
|
|
1791 | PerlIOBase_clearerr, |
|
|
1792 | PerlIOBase_setlinebuf, |
|
|
1793 | PerlIOBuf_get_base, |
|
|
1794 | PerlIOBuf_bufsiz, |
|
|
1795 | PerlIOBuf_get_ptr, |
|
|
1796 | PerlIOBuf_get_cnt, |
|
|
1797 | PerlIOBuf_set_ptrcnt, |
|
|
1798 | }; |
|
|
1799 | |
|
|
1800 | |
1628 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1801 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1629 | |
1802 | |
1630 | PROTOTYPES: DISABLE |
1803 | PROTOTYPES: DISABLE |
1631 | |
1804 | |
1632 | BOOT: |
1805 | BOOT: |
1633 | { |
1806 | { |
1634 | #ifdef USE_ITHREADS |
1807 | #ifdef USE_ITHREADS |
1635 | MUTEX_INIT (&coro_mutex); |
1808 | MUTEX_INIT (&coro_lock); |
|
|
1809 | # if CORO_PTHREAD |
|
|
1810 | coro_thx = PERL_GET_CONTEXT; |
|
|
1811 | # endif |
1636 | #endif |
1812 | #endif |
1637 | BOOT_PAGESIZE; |
1813 | BOOT_PAGESIZE; |
1638 | |
1814 | |
1639 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1815 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1640 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1816 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
1661 | main_top_env = main_top_env->je_prev; |
1837 | main_top_env = main_top_env->je_prev; |
1662 | |
1838 | |
1663 | coroapi.ver = CORO_API_VERSION; |
1839 | coroapi.ver = CORO_API_VERSION; |
1664 | coroapi.rev = CORO_API_REVISION; |
1840 | coroapi.rev = CORO_API_REVISION; |
1665 | coroapi.transfer = api_transfer; |
1841 | coroapi.transfer = api_transfer; |
|
|
1842 | |
|
|
1843 | { |
|
|
1844 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
1845 | |
|
|
1846 | if (!svp) croak ("Time::HiRes is required"); |
|
|
1847 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
1848 | |
|
|
1849 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
1850 | } |
1666 | |
1851 | |
1667 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1852 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1668 | } |
1853 | } |
1669 | |
1854 | |
1670 | SV * |
1855 | SV * |
… | |
… | |
1718 | ta.next = 0; |
1903 | ta.next = 0; |
1719 | break; |
1904 | break; |
1720 | |
1905 | |
1721 | case 1: |
1906 | case 1: |
1722 | if (items != 2) |
1907 | if (items != 2) |
1723 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1908 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
1724 | |
1909 | |
1725 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1910 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1726 | break; |
1911 | break; |
1727 | |
1912 | |
1728 | case 2: |
1913 | case 2: |
… | |
… | |
1762 | _exit (code); |
1947 | _exit (code); |
1763 | |
1948 | |
1764 | int |
1949 | int |
1765 | cctx_stacksize (int new_stacksize = 0) |
1950 | cctx_stacksize (int new_stacksize = 0) |
1766 | CODE: |
1951 | CODE: |
1767 | RETVAL = coro_stacksize; |
1952 | RETVAL = cctx_stacksize; |
1768 | if (new_stacksize) |
1953 | if (new_stacksize) |
|
|
1954 | { |
1769 | coro_stacksize = new_stacksize; |
1955 | cctx_stacksize = new_stacksize; |
|
|
1956 | ++cctx_gen; |
|
|
1957 | } |
|
|
1958 | OUTPUT: |
|
|
1959 | RETVAL |
|
|
1960 | |
|
|
1961 | int |
|
|
1962 | cctx_max_idle (int max_idle = 0) |
|
|
1963 | CODE: |
|
|
1964 | RETVAL = cctx_max_idle; |
|
|
1965 | if (max_idle > 1) |
|
|
1966 | cctx_max_idle = max_idle; |
1770 | OUTPUT: |
1967 | OUTPUT: |
1771 | RETVAL |
1968 | RETVAL |
1772 | |
1969 | |
1773 | int |
1970 | int |
1774 | cctx_count () |
1971 | cctx_count () |
… | |
… | |
1798 | call (Coro::State coro, SV *coderef) |
1995 | call (Coro::State coro, SV *coderef) |
1799 | ALIAS: |
1996 | ALIAS: |
1800 | eval = 1 |
1997 | eval = 1 |
1801 | CODE: |
1998 | CODE: |
1802 | { |
1999 | { |
1803 | if (coro->mainstack) |
2000 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
1804 | { |
2001 | { |
1805 | struct coro temp; |
2002 | struct coro temp; |
1806 | |
2003 | |
1807 | if (!(coro->flags & CF_RUNNING)) |
2004 | if (!(coro->flags & CF_RUNNING)) |
1808 | { |
2005 | { |
… | |
… | |
1870 | CODE: |
2067 | CODE: |
1871 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2068 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1872 | OUTPUT: |
2069 | OUTPUT: |
1873 | RETVAL |
2070 | RETVAL |
1874 | |
2071 | |
1875 | IV |
2072 | UV |
1876 | rss (Coro::State coro) |
2073 | rss (Coro::State coro) |
1877 | PROTOTYPE: $ |
2074 | PROTOTYPE: $ |
1878 | ALIAS: |
2075 | ALIAS: |
1879 | usecount = 1 |
2076 | usecount = 1 |
1880 | CODE: |
2077 | CODE: |
… | |
… | |
1891 | CODE: |
2088 | CODE: |
1892 | struct coro *coro = SvSTATE (coro_current); |
2089 | struct coro *coro = SvSTATE (coro_current); |
1893 | coro->cctx->idle_sp = 0; |
2090 | coro->cctx->idle_sp = 0; |
1894 | |
2091 | |
1895 | void |
2092 | void |
1896 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
1897 | PROTOTYPE: $;$ |
|
|
1898 | CODE: |
|
|
1899 | SvREFCNT_dec (self->throw); |
|
|
1900 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
1901 | |
|
|
1902 | void |
|
|
1903 | swap_defsv (Coro::State self) |
2093 | swap_defsv (Coro::State self) |
1904 | PROTOTYPE: $ |
2094 | PROTOTYPE: $ |
1905 | ALIAS: |
2095 | ALIAS: |
1906 | swap_defav = 1 |
2096 | swap_defav = 1 |
1907 | CODE: |
2097 | CODE: |
… | |
… | |
1961 | void |
2151 | void |
1962 | _set_current (SV *current) |
2152 | _set_current (SV *current) |
1963 | PROTOTYPE: $ |
2153 | PROTOTYPE: $ |
1964 | CODE: |
2154 | CODE: |
1965 | SvREFCNT_dec (SvRV (coro_current)); |
2155 | SvREFCNT_dec (SvRV (coro_current)); |
1966 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
2156 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
1967 | |
2157 | |
1968 | void |
2158 | void |
1969 | _set_readyhook (SV *hook) |
2159 | _set_readyhook (SV *hook) |
1970 | PROTOTYPE: $ |
2160 | PROTOTYPE: $ |
1971 | CODE: |
2161 | CODE: |
… | |
… | |
2010 | CODE: |
2200 | CODE: |
2011 | RETVAL = coro_nready; |
2201 | RETVAL = coro_nready; |
2012 | OUTPUT: |
2202 | OUTPUT: |
2013 | RETVAL |
2203 | RETVAL |
2014 | |
2204 | |
|
|
2205 | void |
|
|
2206 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2207 | PROTOTYPE: $;$ |
|
|
2208 | CODE: |
|
|
2209 | SvREFCNT_dec (self->throw); |
|
|
2210 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2211 | |
2015 | # for async_pool speedup |
2212 | # for async_pool speedup |
2016 | void |
2213 | void |
2017 | _pool_1 (SV *cb) |
2214 | _pool_1 (SV *cb) |
2018 | CODE: |
2215 | CODE: |
2019 | { |
2216 | { |
… | |
… | |
2031 | SvREFCNT_dec (old); |
2228 | SvREFCNT_dec (old); |
2032 | croak ("\3async_pool terminate\2\n"); |
2229 | croak ("\3async_pool terminate\2\n"); |
2033 | } |
2230 | } |
2034 | |
2231 | |
2035 | SvREFCNT_dec (coro->saved_deffh); |
2232 | SvREFCNT_dec (coro->saved_deffh); |
2036 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
2233 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
2037 | |
2234 | |
2038 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2235 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2039 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2236 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2040 | |
2237 | |
2041 | invoke_av = (AV *)SvRV (invoke); |
2238 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
2045 | |
2242 | |
2046 | if (len > 0) |
2243 | if (len > 0) |
2047 | { |
2244 | { |
2048 | av_fill (defav, len - 1); |
2245 | av_fill (defav, len - 1); |
2049 | for (i = 0; i < len; ++i) |
2246 | for (i = 0; i < len; ++i) |
2050 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
2247 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2051 | } |
2248 | } |
2052 | |
2249 | |
2053 | SvREFCNT_dec (invoke); |
2250 | SvREFCNT_dec (invoke); |
2054 | } |
2251 | } |
2055 | |
2252 | |
… | |
… | |
2062 | sv_setsv (cb, &PL_sv_undef); |
2259 | sv_setsv (cb, &PL_sv_undef); |
2063 | |
2260 | |
2064 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2261 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2065 | coro->saved_deffh = 0; |
2262 | coro->saved_deffh = 0; |
2066 | |
2263 | |
2067 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2264 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2068 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2265 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2069 | { |
2266 | { |
2070 | SV *old = PL_diehook; |
2267 | SV *old = PL_diehook; |
2071 | PL_diehook = 0; |
2268 | PL_diehook = 0; |
2072 | SvREFCNT_dec (old); |
2269 | SvREFCNT_dec (old); |
… | |
… | |
2083 | api_trace (coro_current, 0); |
2280 | api_trace (coro_current, 0); |
2084 | |
2281 | |
2085 | av_push (av_async_pool, newSVsv (coro_current)); |
2282 | av_push (av_async_pool, newSVsv (coro_current)); |
2086 | } |
2283 | } |
2087 | |
2284 | |
|
|
2285 | #if 0 |
|
|
2286 | |
|
|
2287 | void |
|
|
2288 | _generator_call (...) |
|
|
2289 | PROTOTYPE: @ |
|
|
2290 | PPCODE: |
|
|
2291 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2292 | xxxx |
|
|
2293 | abort (); |
|
|
2294 | |
|
|
2295 | SV * |
|
|
2296 | gensub (SV *sub, ...) |
|
|
2297 | PROTOTYPE: &;@ |
|
|
2298 | CODE: |
|
|
2299 | { |
|
|
2300 | struct coro *coro; |
|
|
2301 | MAGIC *mg; |
|
|
2302 | CV *xcv; |
|
|
2303 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2304 | int i; |
|
|
2305 | |
|
|
2306 | CvGV (ncv) = CvGV (cv); |
|
|
2307 | CvFILE (ncv) = CvFILE (cv); |
|
|
2308 | |
|
|
2309 | Newz (0, coro, 1, struct coro); |
|
|
2310 | coro->args = newAV (); |
|
|
2311 | coro->flags = CF_NEW; |
|
|
2312 | |
|
|
2313 | av_extend (coro->args, items - 1); |
|
|
2314 | for (i = 1; i < items; i++) |
|
|
2315 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2316 | |
|
|
2317 | CvISXSUB_on (ncv); |
|
|
2318 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2319 | |
|
|
2320 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2321 | |
|
|
2322 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2323 | CvANON_on (ncv); |
|
|
2324 | |
|
|
2325 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2326 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2327 | } |
|
|
2328 | OUTPUT: |
|
|
2329 | RETVAL |
|
|
2330 | |
|
|
2331 | #endif |
|
|
2332 | |
2088 | |
2333 | |
2089 | MODULE = Coro::State PACKAGE = Coro::AIO |
2334 | MODULE = Coro::State PACKAGE = Coro::AIO |
2090 | |
2335 | |
2091 | SV * |
2336 | void |
2092 | _get_state () |
2337 | _get_state (SV *self) |
2093 | CODE: |
2338 | PPCODE: |
2094 | { |
2339 | { |
2095 | struct io_state *data; |
2340 | AV *defav = GvAV (PL_defgv); |
2096 | |
2341 | AV *av = newAV (); |
|
|
2342 | int i; |
2097 | RETVAL = newSV (sizeof (struct io_state)); |
2343 | SV *data_sv = newSV (sizeof (struct io_state)); |
2098 | data = (struct io_state *)SvPVX (RETVAL); |
2344 | struct io_state *data = (struct io_state *)SvPVX (data_sv); |
2099 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
2345 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2100 | SvPOK_only (RETVAL); |
2346 | SvPOK_only (data_sv); |
2101 | |
2347 | |
2102 | data->errorno = errno; |
2348 | data->errorno = errno; |
2103 | data->laststype = PL_laststype; |
2349 | data->laststype = PL_laststype; |
2104 | data->laststatval = PL_laststatval; |
2350 | data->laststatval = PL_laststatval; |
2105 | data->statcache = PL_statcache; |
2351 | data->statcache = PL_statcache; |
|
|
2352 | |
|
|
2353 | av_extend (av, AvFILLp (defav) + 1 + 1); |
|
|
2354 | |
|
|
2355 | for (i = 0; i <= AvFILLp (defav); ++i) |
|
|
2356 | av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i])); |
|
|
2357 | |
|
|
2358 | av_push (av, data_sv); |
|
|
2359 | |
|
|
2360 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
2361 | |
|
|
2362 | api_ready (self); |
2106 | } |
2363 | } |
2107 | OUTPUT: |
|
|
2108 | RETVAL |
|
|
2109 | |
2364 | |
2110 | void |
2365 | void |
2111 | _set_state (char *data_) |
2366 | _set_state (SV *state) |
2112 | PROTOTYPE: $ |
2367 | PROTOTYPE: $ |
2113 | CODE: |
2368 | PPCODE: |
2114 | { |
2369 | { |
2115 | struct io_state *data = (void *)data_; |
2370 | AV *av = (AV *)SvRV (state); |
|
|
2371 | struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]); |
|
|
2372 | int i; |
2116 | |
2373 | |
2117 | errno = data->errorno; |
2374 | errno = data->errorno; |
2118 | PL_laststype = data->laststype; |
2375 | PL_laststype = data->laststype; |
2119 | PL_laststatval = data->laststatval; |
2376 | PL_laststatval = data->laststatval; |
2120 | PL_statcache = data->statcache; |
2377 | PL_statcache = data->statcache; |
|
|
2378 | |
|
|
2379 | EXTEND (SP, AvFILLp (av)); |
|
|
2380 | for (i = 0; i < AvFILLp (av); ++i) |
|
|
2381 | PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i]))); |
2121 | } |
2382 | } |
2122 | |
2383 | |
2123 | |
2384 | |
2124 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2385 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2125 | |
2386 | |
… | |
… | |
2149 | } |
2410 | } |
2150 | |
2411 | |
2151 | --incede; |
2412 | --incede; |
2152 | } |
2413 | } |
2153 | |
2414 | |
|
|
2415 | |
|
|
2416 | MODULE = Coro::State PACKAGE = PerlIO::cede |
|
|
2417 | |
|
|
2418 | BOOT: |
|
|
2419 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |