… | |
… | |
116 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
116 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
117 | #endif |
117 | #endif |
118 | |
118 | |
119 | /* The next macros try to return the current stack pointer, in an as |
119 | /* The next macros try to return the current stack pointer, in an as |
120 | * portable way as possible. */ |
120 | * portable way as possible. */ |
121 | #define dSTACKLEVEL volatile char stacklevel |
121 | #if __GNUC__ >= 4 |
122 | #define STACKLEVEL ((void *)&stacklevel) |
122 | # define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) |
|
|
123 | #else |
|
|
124 | # define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel |
|
|
125 | #endif |
123 | |
126 | |
124 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
127 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
125 | |
128 | |
126 | #if __GNUC__ >= 3 |
129 | #if __GNUC__ >= 3 |
127 | # define attribute(x) __attribute__(x) |
130 | # define attribute(x) __attribute__(x) |
… | |
… | |
139 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
140 | |
143 | |
141 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
142 | |
145 | |
143 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
144 | |
|
|
145 | static perl_mutex coro_lock; |
|
|
146 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
|
|
147 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
148 | # if CORO_PTHREAD |
147 | # if CORO_PTHREAD |
149 | static void *coro_thx; |
148 | static void *coro_thx; |
150 | # endif |
149 | # endif |
151 | |
|
|
152 | #else |
|
|
153 | |
|
|
154 | # define LOCK (void)0 |
|
|
155 | # define UNLOCK (void)0 |
|
|
156 | |
|
|
157 | #endif |
150 | #endif |
158 | |
|
|
159 | # undef LOCK |
|
|
160 | # define LOCK (void)0 |
|
|
161 | # undef UNLOCK |
|
|
162 | # define UNLOCK (void)0 |
|
|
163 | |
151 | |
164 | /* helper storage struct for Coro::AIO */ |
152 | /* helper storage struct for Coro::AIO */ |
165 | struct io_state |
153 | struct io_state |
166 | { |
154 | { |
167 | AV *res; |
155 | AV *res; |
… | |
… | |
180 | static JMPENV *main_top_env; |
168 | static JMPENV *main_top_env; |
181 | static HV *coro_state_stash, *coro_stash; |
169 | static HV *coro_state_stash, *coro_stash; |
182 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
170 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
183 | static volatile struct coro *transfer_next; |
171 | static volatile struct coro *transfer_next; |
184 | |
172 | |
185 | struct transfer_args |
|
|
186 | { |
|
|
187 | struct coro *prev, *next; |
|
|
188 | }; |
|
|
189 | |
|
|
190 | static GV *irsgv; /* $/ */ |
173 | static GV *irsgv; /* $/ */ |
191 | static GV *stdoutgv; /* *STDOUT */ |
174 | static GV *stdoutgv; /* *STDOUT */ |
192 | static SV *rv_diehook; |
175 | static SV *rv_diehook; |
193 | static SV *rv_warnhook; |
176 | static SV *rv_warnhook; |
194 | static HV *hv_sig; /* %SIG */ |
177 | static HV *hv_sig; /* %SIG */ |
… | |
… | |
212 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
195 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
213 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
196 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
214 | }; |
197 | }; |
215 | |
198 | |
216 | /* this is a structure representing a c-level coroutine */ |
199 | /* this is a structure representing a c-level coroutine */ |
217 | typedef struct coro_cctx { |
200 | typedef struct coro_cctx |
|
|
201 | { |
218 | struct coro_cctx *next; |
202 | struct coro_cctx *next; |
219 | |
203 | |
220 | /* the stack */ |
204 | /* the stack */ |
221 | void *sptr; |
205 | void *sptr; |
222 | size_t ssize; |
206 | size_t ssize; |
… | |
… | |
240 | CF_NEW = 0x0004, /* has never been switched to */ |
224 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
225 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
242 | }; |
226 | }; |
243 | |
227 | |
244 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
228 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
245 | typedef struct { |
229 | typedef struct |
|
|
230 | { |
246 | SV *defsv; |
231 | SV *defsv; |
247 | AV *defav; |
232 | AV *defav; |
248 | SV *errsv; |
233 | SV *errsv; |
249 | SV *irsgv; |
234 | SV *irsgv; |
250 | #define VAR(name,type) type name; |
235 | #define VAR(name,type) type name; |
… | |
… | |
254 | |
239 | |
255 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
240 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
256 | |
241 | |
257 | /* this is a structure representing a perl-level coroutine */ |
242 | /* this is a structure representing a perl-level coroutine */ |
258 | struct coro { |
243 | struct coro { |
259 | /* the c coroutine allocated to this perl coroutine, if any */ |
244 | /* the C coroutine allocated to this perl coroutine, if any */ |
260 | coro_cctx *cctx; |
245 | coro_cctx *cctx; |
261 | |
246 | |
262 | /* process data */ |
247 | /* process data */ |
|
|
248 | struct CoroSLF slf_frame; /* saved slf frame */ |
263 | AV *mainstack; |
249 | AV *mainstack; |
264 | perl_slots *slot; /* basically the saved sp */ |
250 | perl_slots *slot; /* basically the saved sp */ |
265 | |
251 | |
266 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | AV *args; /* data associated with this coroutine (initial args) */ |
267 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int refcnt; /* coroutines are refcounted, yes */ |
268 | int flags; /* CF_ flags */ |
254 | int flags; /* CF_ flags */ |
269 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
256 | void (*on_destroy)(pTHX_ struct coro *coro); |
270 | |
257 | |
271 | /* statistics */ |
258 | /* statistics */ |
272 | int usecount; /* number of transfers to this coro */ |
259 | int usecount; /* number of transfers to this coro */ |
273 | |
260 | |
274 | /* coro process data */ |
261 | /* coro process data */ |
… | |
… | |
282 | struct coro *next, *prev; |
269 | struct coro *next, *prev; |
283 | }; |
270 | }; |
284 | |
271 | |
285 | typedef struct coro *Coro__State; |
272 | typedef struct coro *Coro__State; |
286 | typedef struct coro *Coro__State_or_hashref; |
273 | typedef struct coro *Coro__State_or_hashref; |
|
|
274 | |
|
|
275 | static struct CoroSLF slf_frame; /* the current slf frame */ |
287 | |
276 | |
288 | /** Coro ********************************************************************/ |
277 | /** Coro ********************************************************************/ |
289 | |
278 | |
290 | #define PRIO_MAX 3 |
279 | #define PRIO_MAX 3 |
291 | #define PRIO_HIGH 1 |
280 | #define PRIO_HIGH 1 |
… | |
… | |
296 | |
285 | |
297 | /* for Coro.pm */ |
286 | /* for Coro.pm */ |
298 | static SV *coro_current; |
287 | static SV *coro_current; |
299 | static SV *coro_readyhook; |
288 | static SV *coro_readyhook; |
300 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
289 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
301 | static int coro_nready; |
|
|
302 | static struct coro *coro_first; |
290 | static struct coro *coro_first; |
|
|
291 | #define coro_nready coroapi.nready |
303 | |
292 | |
304 | /** lowlevel stuff **********************************************************/ |
293 | /** lowlevel stuff **********************************************************/ |
305 | |
294 | |
306 | static SV * |
295 | static SV * |
307 | coro_get_sv (pTHX_ const char *name, int create) |
296 | coro_get_sv (pTHX_ const char *name, int create) |
… | |
… | |
400 | static MGVTBL coro_cv_vtbl = { |
389 | static MGVTBL coro_cv_vtbl = { |
401 | 0, 0, 0, 0, |
390 | 0, 0, 0, 0, |
402 | coro_cv_free |
391 | coro_cv_free |
403 | }; |
392 | }; |
404 | |
393 | |
405 | #define CORO_MAGIC(sv, type) \ |
394 | #define CORO_MAGIC(sv, type) \ |
406 | SvMAGIC (sv) \ |
395 | expect_true (SvMAGIC (sv)) \ |
407 | ? SvMAGIC (sv)->mg_type == type \ |
396 | ? expect_true (SvMAGIC (sv)->mg_type == type) \ |
408 | ? SvMAGIC (sv) \ |
397 | ? SvMAGIC (sv) \ |
409 | : mg_find (sv, type) \ |
398 | : mg_find (sv, type) \ |
410 | : 0 |
399 | : 0 |
411 | |
400 | |
412 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
401 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
413 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
402 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
414 | |
403 | |
… | |
… | |
435 | mg = CORO_MAGIC_state (coro); |
424 | mg = CORO_MAGIC_state (coro); |
436 | return (struct coro *)mg->mg_ptr; |
425 | return (struct coro *)mg->mg_ptr; |
437 | } |
426 | } |
438 | |
427 | |
439 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
428 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
429 | |
|
|
430 | /* fastert than SvSTATE, but expects a coroutine hv */ |
|
|
431 | INLINE struct coro * |
|
|
432 | SvSTATE_hv (SV *sv) |
|
|
433 | { |
|
|
434 | MAGIC *mg = expect_true (SvMAGIC (sv)->mg_type == CORO_MAGIC_type_state) |
|
|
435 | ? SvMAGIC (sv) |
|
|
436 | : mg_find (sv, CORO_MAGIC_type_state); |
|
|
437 | |
|
|
438 | return (struct coro *)mg->mg_ptr; |
|
|
439 | } |
|
|
440 | |
|
|
441 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
440 | |
442 | |
441 | /* the next two functions merely cache the padlists */ |
443 | /* the next two functions merely cache the padlists */ |
442 | static void |
444 | static void |
443 | get_padlist (pTHX_ CV *cv) |
445 | get_padlist (pTHX_ CV *cv) |
444 | { |
446 | { |
… | |
… | |
511 | CvPADLIST (cv) = (AV *)POPs; |
513 | CvPADLIST (cv) = (AV *)POPs; |
512 | } |
514 | } |
513 | |
515 | |
514 | PUTBACK; |
516 | PUTBACK; |
515 | } |
517 | } |
|
|
518 | |
|
|
519 | slf_frame = c->slf_frame; |
516 | } |
520 | } |
517 | |
521 | |
518 | static void |
522 | static void |
519 | save_perl (pTHX_ Coro__State c) |
523 | save_perl (pTHX_ Coro__State c) |
520 | { |
524 | { |
|
|
525 | c->slf_frame = slf_frame; |
|
|
526 | |
521 | { |
527 | { |
522 | dSP; |
528 | dSP; |
523 | I32 cxix = cxstack_ix; |
529 | I32 cxix = cxstack_ix; |
524 | PERL_CONTEXT *ccstk = cxstack; |
530 | PERL_CONTEXT *ccstk = cxstack; |
525 | PERL_SI *top_si = PL_curstackinfo; |
531 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
592 | #undef VAR |
598 | #undef VAR |
593 | } |
599 | } |
594 | } |
600 | } |
595 | |
601 | |
596 | /* |
602 | /* |
597 | * allocate various perl stacks. This is an exact copy |
603 | * allocate various perl stacks. This is almost an exact copy |
598 | * of perl.c:init_stacks, except that it uses less memory |
604 | * of perl.c:init_stacks, except that it uses less memory |
599 | * on the (sometimes correct) assumption that coroutines do |
605 | * on the (sometimes correct) assumption that coroutines do |
600 | * not usually need a lot of stackspace. |
606 | * not usually need a lot of stackspace. |
601 | */ |
607 | */ |
602 | #if CORO_PREFER_PERL_FUNCTIONS |
608 | #if CORO_PREFER_PERL_FUNCTIONS |
… | |
… | |
709 | #endif |
715 | #endif |
710 | } |
716 | } |
711 | } |
717 | } |
712 | |
718 | |
713 | return rss; |
719 | return rss; |
714 | } |
|
|
715 | |
|
|
716 | /** set stacklevel support **************************************************/ |
|
|
717 | |
|
|
718 | /* we sometimes need to create the effect of pp_set_stacklevel calling us */ |
|
|
719 | #define SSL_HEAD (void)0 |
|
|
720 | /* we sometimes need to create the effect of leaving via pp_set_stacklevel */ |
|
|
721 | #define SSL_TAIL set_stacklevel_tail (aTHX) |
|
|
722 | |
|
|
723 | INLINE void |
|
|
724 | set_stacklevel_tail (pTHX) |
|
|
725 | { |
|
|
726 | dSP; |
|
|
727 | SV **bot = SP; |
|
|
728 | |
|
|
729 | int gimme = GIMME_V; |
|
|
730 | |
|
|
731 | /* make sure we put something on the stack in scalar context */ |
|
|
732 | if (gimme == G_SCALAR) |
|
|
733 | { |
|
|
734 | if (sp == bot) |
|
|
735 | XPUSHs (&PL_sv_undef); |
|
|
736 | |
|
|
737 | SP = bot + 1; |
|
|
738 | } |
|
|
739 | |
|
|
740 | PUTBACK; |
|
|
741 | } |
720 | } |
742 | |
721 | |
743 | /** coroutine stack handling ************************************************/ |
722 | /** coroutine stack handling ************************************************/ |
744 | |
723 | |
745 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
724 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
… | |
… | |
831 | |
810 | |
832 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
811 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
833 | } |
812 | } |
834 | |
813 | |
835 | static void |
814 | static void |
|
|
815 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
|
|
816 | { |
|
|
817 | /* kind of mega-hacky, but works */ |
|
|
818 | ta->next = ta->prev = (struct coro *)ta; |
|
|
819 | } |
|
|
820 | |
|
|
821 | static int |
|
|
822 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
|
|
823 | { |
|
|
824 | return 0; |
|
|
825 | } |
|
|
826 | |
|
|
827 | static void |
836 | coro_setup (pTHX_ struct coro *coro) |
828 | coro_setup (pTHX_ struct coro *coro) |
837 | { |
829 | { |
838 | /* |
830 | /* |
839 | * emulate part of the perl startup here. |
831 | * emulate part of the perl startup here. |
840 | */ |
832 | */ |
… | |
… | |
879 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
871 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
880 | SPAGAIN; |
872 | SPAGAIN; |
881 | } |
873 | } |
882 | |
874 | |
883 | /* this newly created coroutine might be run on an existing cctx which most |
875 | /* this newly created coroutine might be run on an existing cctx which most |
884 | * likely was suspended in set_stacklevel, called from pp_set_stacklevel, |
876 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
885 | * so we have to emulate entering pp_set_stacklevel here. |
|
|
886 | */ |
877 | */ |
887 | SSL_HEAD; |
878 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
|
|
879 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
888 | } |
880 | } |
889 | |
881 | |
890 | static void |
882 | static void |
891 | coro_destruct (pTHX_ struct coro *coro) |
883 | coro_destruct (pTHX_ struct coro *coro) |
892 | { |
884 | { |
… | |
… | |
936 | static int |
928 | static int |
937 | runops_trace (pTHX) |
929 | runops_trace (pTHX) |
938 | { |
930 | { |
939 | COP *oldcop = 0; |
931 | COP *oldcop = 0; |
940 | int oldcxix = -2; |
932 | int oldcxix = -2; |
941 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
933 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
942 | coro_cctx *cctx = coro->cctx; |
934 | coro_cctx *cctx = coro->cctx; |
943 | |
935 | |
944 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
936 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
945 | { |
937 | { |
946 | PERL_ASYNC_CHECK (); |
938 | PERL_ASYNC_CHECK (); |
… | |
… | |
1056 | TAINT_NOT; |
1048 | TAINT_NOT; |
1057 | return 0; |
1049 | return 0; |
1058 | } |
1050 | } |
1059 | |
1051 | |
1060 | static void |
1052 | static void |
1061 | prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) |
1053 | prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) |
1062 | { |
1054 | { |
1063 | ta->prev = (struct coro *)cctx; |
1055 | ta->prev = (struct coro *)cctx; |
1064 | ta->next = 0; |
1056 | ta->next = 0; |
1065 | } |
1057 | } |
1066 | |
1058 | |
… | |
… | |
1100 | struct coro *next = (struct coro *)transfer_next; |
1092 | struct coro *next = (struct coro *)transfer_next; |
1101 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
1093 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
1102 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
1094 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
1103 | |
1095 | |
1104 | free_coro_mortal (aTHX); |
1096 | free_coro_mortal (aTHX); |
1105 | UNLOCK; |
|
|
1106 | |
1097 | |
1107 | if (expect_false (next->throw)) |
1098 | if (expect_false (next->throw)) |
1108 | { |
1099 | { |
1109 | SV *exception = sv_2mortal (next->throw); |
1100 | SV *exception = sv_2mortal (next->throw); |
1110 | |
1101 | |
… | |
… | |
1126 | # endif |
1117 | # endif |
1127 | #endif |
1118 | #endif |
1128 | { |
1119 | { |
1129 | dTHX; |
1120 | dTHX; |
1130 | |
1121 | |
1131 | /* we are the alternative tail to pp_set_stacklevel */ |
1122 | /* normally we would need to skip the entersub here */ |
1132 | /* so do the same things here */ |
1123 | /* not doing so will re-execute it, which is exactly what we want */ |
1133 | SSL_TAIL; |
|
|
1134 | |
|
|
1135 | /* we now skip the op that did lead to transfer() */ |
|
|
1136 | PL_op = PL_op->op_next; |
1124 | /* PL_nop = PL_nop->op_next */ |
1137 | |
1125 | |
1138 | /* inject a fake subroutine call to cctx_init */ |
1126 | /* inject a fake subroutine call to cctx_init */ |
1139 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1127 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1140 | |
1128 | |
1141 | /* cctx_run is the alternative tail of transfer() */ |
1129 | /* cctx_run is the alternative tail of transfer() */ |
|
|
1130 | /* TODO: throwing an exception here might be deadly, VERIFY */ |
1142 | transfer_tail (aTHX); |
1131 | transfer_tail (aTHX); |
1143 | |
1132 | |
1144 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1133 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1145 | PL_restartop = PL_op; |
1134 | PL_restartop = PL_op; |
1146 | perl_run (PL_curinterp); |
1135 | perl_run (PL_curinterp); |
… | |
… | |
1305 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1294 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1306 | { |
1295 | { |
1307 | if (expect_true (prev != next)) |
1296 | if (expect_true (prev != next)) |
1308 | { |
1297 | { |
1309 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1298 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1310 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
1299 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1311 | |
1300 | |
1312 | if (expect_false (next->flags & CF_RUNNING)) |
1301 | if (expect_false (next->flags & CF_RUNNING)) |
1313 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
1302 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1314 | |
1303 | |
1315 | if (expect_false (next->flags & CF_DESTROYED)) |
1304 | if (expect_false (next->flags & CF_DESTROYED)) |
1316 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1305 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); |
1317 | |
1306 | |
1318 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1307 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1319 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1308 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1320 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version"); |
1309 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1321 | #endif |
1310 | #endif |
1322 | } |
1311 | } |
1323 | } |
1312 | } |
1324 | |
1313 | |
1325 | /* always use the TRANSFER macro */ |
1314 | /* always use the TRANSFER macro */ |
… | |
… | |
1329 | dSTACKLEVEL; |
1318 | dSTACKLEVEL; |
1330 | |
1319 | |
1331 | /* sometimes transfer is only called to set idle_sp */ |
1320 | /* sometimes transfer is only called to set idle_sp */ |
1332 | if (expect_false (!next)) |
1321 | if (expect_false (!next)) |
1333 | { |
1322 | { |
1334 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1323 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1335 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1324 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1336 | } |
1325 | } |
1337 | else if (expect_true (prev != next)) |
1326 | else if (expect_true (prev != next)) |
1338 | { |
1327 | { |
1339 | coro_cctx *prev__cctx; |
1328 | coro_cctx *prev__cctx; |
… | |
… | |
1346 | prev->flags |= CF_RUNNING; |
1335 | prev->flags |= CF_RUNNING; |
1347 | } |
1336 | } |
1348 | |
1337 | |
1349 | prev->flags &= ~CF_RUNNING; |
1338 | prev->flags &= ~CF_RUNNING; |
1350 | next->flags |= CF_RUNNING; |
1339 | next->flags |= CF_RUNNING; |
1351 | |
|
|
1352 | LOCK; |
|
|
1353 | |
1340 | |
1354 | /* first get rid of the old state */ |
1341 | /* first get rid of the old state */ |
1355 | save_perl (aTHX_ prev); |
1342 | save_perl (aTHX_ prev); |
1356 | |
1343 | |
1357 | if (expect_false (next->flags & CF_NEW)) |
1344 | if (expect_false (next->flags & CF_NEW)) |
… | |
… | |
1366 | |
1353 | |
1367 | prev__cctx = prev->cctx; |
1354 | prev__cctx = prev->cctx; |
1368 | |
1355 | |
1369 | /* possibly untie and reuse the cctx */ |
1356 | /* possibly untie and reuse the cctx */ |
1370 | if (expect_true ( |
1357 | if (expect_true ( |
1371 | prev__cctx->idle_sp == STACKLEVEL |
1358 | prev__cctx->idle_sp == (void *)stacklevel |
1372 | && !(prev__cctx->flags & CC_TRACE) |
1359 | && !(prev__cctx->flags & CC_TRACE) |
1373 | && !force_cctx |
1360 | && !force_cctx |
1374 | )) |
1361 | )) |
1375 | { |
1362 | { |
1376 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1363 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1377 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1364 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1378 | |
1365 | |
1379 | prev->cctx = 0; |
1366 | prev->cctx = 0; |
1380 | |
1367 | |
1381 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1368 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
… | |
… | |
1415 | coro_state_destroy (pTHX_ struct coro *coro) |
1402 | coro_state_destroy (pTHX_ struct coro *coro) |
1416 | { |
1403 | { |
1417 | if (coro->flags & CF_DESTROYED) |
1404 | if (coro->flags & CF_DESTROYED) |
1418 | return 0; |
1405 | return 0; |
1419 | |
1406 | |
|
|
1407 | if (coro->on_destroy) |
|
|
1408 | coro->on_destroy (aTHX_ coro); |
|
|
1409 | |
1420 | coro->flags |= CF_DESTROYED; |
1410 | coro->flags |= CF_DESTROYED; |
1421 | |
1411 | |
1422 | if (coro->flags & CF_READY) |
1412 | if (coro->flags & CF_READY) |
1423 | { |
1413 | { |
1424 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1414 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1425 | /* alternative: look through all ready queues and remove the coro */ |
1415 | /* alternative: look through all ready queues and remove the coro */ |
1426 | LOCK; |
|
|
1427 | --coro_nready; |
1416 | --coro_nready; |
1428 | UNLOCK; |
|
|
1429 | } |
1417 | } |
1430 | else |
1418 | else |
1431 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1419 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1432 | |
1420 | |
1433 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1421 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1434 | { |
1422 | { |
1435 | struct coro temp; |
1423 | struct coro temp; |
1436 | |
1424 | |
1437 | if (coro->flags & CF_RUNNING) |
1425 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
1438 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
1439 | |
1426 | |
1440 | save_perl (aTHX_ &temp); |
1427 | save_perl (aTHX_ &temp); |
1441 | load_perl (aTHX_ coro); |
1428 | load_perl (aTHX_ coro); |
1442 | |
1429 | |
1443 | coro_destruct (aTHX_ coro); |
1430 | coro_destruct (aTHX_ coro); |
… | |
… | |
1494 | # define MGf_DUP 0 |
1481 | # define MGf_DUP 0 |
1495 | #endif |
1482 | #endif |
1496 | }; |
1483 | }; |
1497 | |
1484 | |
1498 | static void |
1485 | static void |
1499 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1486 | prepare_transfer (pTHX_ struct coro_transfer_args *ta, SV *prev_sv, SV *next_sv) |
1500 | { |
1487 | { |
1501 | ta->prev = SvSTATE (prev_sv); |
1488 | ta->prev = SvSTATE (prev_sv); |
1502 | ta->next = SvSTATE (next_sv); |
1489 | ta->next = SvSTATE (next_sv); |
1503 | TRANSFER_CHECK (*ta); |
1490 | TRANSFER_CHECK (*ta); |
1504 | } |
1491 | } |
1505 | |
1492 | |
1506 | static void |
1493 | static void |
1507 | api_transfer (SV *prev_sv, SV *next_sv) |
1494 | api_transfer (pTHX_ SV *prev_sv, SV *next_sv) |
1508 | { |
1495 | { |
1509 | dTHX; |
|
|
1510 | struct transfer_args ta; |
1496 | struct coro_transfer_args ta; |
1511 | |
1497 | |
1512 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1498 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1513 | TRANSFER (ta, 1); |
1499 | TRANSFER (ta, 1); |
1514 | } |
1500 | } |
1515 | |
1501 | |
1516 | /** Coro ********************************************************************/ |
1502 | /** Coro ********************************************************************/ |
1517 | |
1503 | |
1518 | static void |
1504 | INLINE void |
1519 | coro_enq (pTHX_ SV *coro_sv) |
1505 | coro_enq (pTHX_ struct coro *coro) |
1520 | { |
1506 | { |
1521 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1507 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1522 | } |
1508 | } |
1523 | |
1509 | |
1524 | static SV * |
1510 | INLINE SV * |
1525 | coro_deq (pTHX) |
1511 | coro_deq (pTHX) |
1526 | { |
1512 | { |
1527 | int prio; |
1513 | int prio; |
1528 | |
1514 | |
1529 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1515 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1532 | |
1518 | |
1533 | return 0; |
1519 | return 0; |
1534 | } |
1520 | } |
1535 | |
1521 | |
1536 | static int |
1522 | static int |
1537 | api_ready (SV *coro_sv) |
1523 | api_ready (pTHX_ SV *coro_sv) |
1538 | { |
1524 | { |
1539 | dTHX; |
|
|
1540 | struct coro *coro; |
1525 | struct coro *coro; |
1541 | SV *sv_hook; |
1526 | SV *sv_hook; |
1542 | void (*xs_hook)(void); |
1527 | void (*xs_hook)(void); |
1543 | |
1528 | |
1544 | if (SvROK (coro_sv)) |
1529 | if (SvROK (coro_sv)) |
… | |
… | |
1549 | if (coro->flags & CF_READY) |
1534 | if (coro->flags & CF_READY) |
1550 | return 0; |
1535 | return 0; |
1551 | |
1536 | |
1552 | coro->flags |= CF_READY; |
1537 | coro->flags |= CF_READY; |
1553 | |
1538 | |
1554 | LOCK; |
|
|
1555 | |
|
|
1556 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1539 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1557 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1540 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1558 | |
1541 | |
1559 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1542 | coro_enq (aTHX_ coro); |
1560 | ++coro_nready; |
1543 | ++coro_nready; |
1561 | |
1544 | |
1562 | UNLOCK; |
|
|
1563 | |
|
|
1564 | if (sv_hook) |
1545 | if (sv_hook) |
1565 | { |
1546 | { |
1566 | dSP; |
1547 | dSP; |
1567 | |
1548 | |
1568 | ENTER; |
1549 | ENTER; |
… | |
… | |
1582 | |
1563 | |
1583 | return 1; |
1564 | return 1; |
1584 | } |
1565 | } |
1585 | |
1566 | |
1586 | static int |
1567 | static int |
1587 | api_is_ready (SV *coro_sv) |
1568 | api_is_ready (pTHX_ SV *coro_sv) |
1588 | { |
1569 | { |
1589 | dTHX; |
|
|
1590 | |
|
|
1591 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1570 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1592 | } |
1571 | } |
1593 | |
1572 | |
1594 | INLINE void |
1573 | INLINE void |
1595 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1574 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1596 | { |
1575 | { |
1597 | SV *prev_sv, *next_sv; |
1576 | SV *prev_sv, *next_sv; |
1598 | |
1577 | |
1599 | for (;;) |
1578 | for (;;) |
1600 | { |
1579 | { |
1601 | LOCK; |
|
|
1602 | next_sv = coro_deq (aTHX); |
1580 | next_sv = coro_deq (aTHX); |
1603 | |
1581 | |
1604 | /* nothing to schedule: call the idle handler */ |
1582 | /* nothing to schedule: call the idle handler */ |
1605 | if (expect_false (!next_sv)) |
1583 | if (expect_false (!next_sv)) |
1606 | { |
1584 | { |
1607 | dSP; |
1585 | dSP; |
1608 | UNLOCK; |
|
|
1609 | |
1586 | |
1610 | ENTER; |
1587 | ENTER; |
1611 | SAVETMPS; |
1588 | SAVETMPS; |
1612 | |
1589 | |
1613 | PUSHMARK (SP); |
1590 | PUSHMARK (SP); |
… | |
… | |
1618 | FREETMPS; |
1595 | FREETMPS; |
1619 | LEAVE; |
1596 | LEAVE; |
1620 | continue; |
1597 | continue; |
1621 | } |
1598 | } |
1622 | |
1599 | |
1623 | ta->next = SvSTATE (next_sv); |
1600 | ta->next = SvSTATE_hv (next_sv); |
1624 | |
1601 | |
1625 | /* cannot transfer to destroyed coros, skip and look for next */ |
1602 | /* cannot transfer to destroyed coros, skip and look for next */ |
1626 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1603 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1627 | { |
1604 | { |
1628 | UNLOCK; |
|
|
1629 | SvREFCNT_dec (next_sv); |
1605 | SvREFCNT_dec (next_sv); |
1630 | /* coro_nready has already been taken care of by destroy */ |
1606 | /* coro_nready has already been taken care of by destroy */ |
1631 | continue; |
1607 | continue; |
1632 | } |
1608 | } |
1633 | |
1609 | |
1634 | --coro_nready; |
1610 | --coro_nready; |
1635 | UNLOCK; |
|
|
1636 | break; |
1611 | break; |
1637 | } |
1612 | } |
1638 | |
1613 | |
1639 | /* free this only after the transfer */ |
1614 | /* free this only after the transfer */ |
1640 | prev_sv = SvRV (coro_current); |
1615 | prev_sv = SvRV (coro_current); |
1641 | ta->prev = SvSTATE (prev_sv); |
1616 | ta->prev = SvSTATE_hv (prev_sv); |
1642 | TRANSFER_CHECK (*ta); |
1617 | TRANSFER_CHECK (*ta); |
1643 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1618 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1644 | ta->next->flags &= ~CF_READY; |
1619 | ta->next->flags &= ~CF_READY; |
1645 | SvRV_set (coro_current, next_sv); |
1620 | SvRV_set (coro_current, next_sv); |
1646 | |
1621 | |
1647 | LOCK; |
|
|
1648 | free_coro_mortal (aTHX); |
1622 | free_coro_mortal (aTHX); |
1649 | coro_mortal = prev_sv; |
1623 | coro_mortal = prev_sv; |
1650 | UNLOCK; |
|
|
1651 | } |
1624 | } |
1652 | |
1625 | |
1653 | INLINE void |
1626 | INLINE void |
1654 | prepare_cede (pTHX_ struct transfer_args *ta) |
1627 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1655 | { |
1628 | { |
1656 | api_ready (coro_current); |
1629 | api_ready (aTHX_ coro_current); |
1657 | prepare_schedule (aTHX_ ta); |
1630 | prepare_schedule (aTHX_ ta); |
1658 | } |
1631 | } |
1659 | |
1632 | |
1660 | static void |
1633 | INLINE void |
1661 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1634 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1662 | { |
1635 | { |
1663 | SV *prev = SvRV (coro_current); |
1636 | SV *prev = SvRV (coro_current); |
1664 | |
1637 | |
1665 | if (coro_nready) |
1638 | if (coro_nready) |
1666 | { |
1639 | { |
1667 | prepare_schedule (aTHX_ ta); |
1640 | prepare_schedule (aTHX_ ta); |
1668 | api_ready (prev); |
1641 | api_ready (aTHX_ prev); |
1669 | } |
1642 | } |
1670 | else |
1643 | else |
1671 | ta->prev = ta->next = SvSTATE (prev); |
1644 | prepare_nop (aTHX_ ta); |
1672 | } |
1645 | } |
1673 | |
1646 | |
1674 | static void |
1647 | static void |
1675 | api_schedule (void) |
1648 | api_schedule (pTHX) |
1676 | { |
1649 | { |
1677 | dTHX; |
|
|
1678 | struct transfer_args ta; |
1650 | struct coro_transfer_args ta; |
1679 | |
1651 | |
1680 | prepare_schedule (aTHX_ &ta); |
1652 | prepare_schedule (aTHX_ &ta); |
1681 | TRANSFER (ta, 1); |
1653 | TRANSFER (ta, 1); |
1682 | } |
1654 | } |
1683 | |
1655 | |
1684 | static int |
1656 | static int |
1685 | api_cede (void) |
1657 | api_cede (pTHX) |
1686 | { |
1658 | { |
1687 | dTHX; |
|
|
1688 | struct transfer_args ta; |
1659 | struct coro_transfer_args ta; |
1689 | |
1660 | |
1690 | prepare_cede (aTHX_ &ta); |
1661 | prepare_cede (aTHX_ &ta); |
1691 | |
1662 | |
1692 | if (expect_true (ta.prev != ta.next)) |
1663 | if (expect_true (ta.prev != ta.next)) |
1693 | { |
1664 | { |
… | |
… | |
1697 | else |
1668 | else |
1698 | return 0; |
1669 | return 0; |
1699 | } |
1670 | } |
1700 | |
1671 | |
1701 | static int |
1672 | static int |
1702 | api_cede_notself (void) |
1673 | api_cede_notself (pTHX) |
1703 | { |
1674 | { |
1704 | if (coro_nready) |
1675 | if (coro_nready) |
1705 | { |
1676 | { |
1706 | dTHX; |
|
|
1707 | struct transfer_args ta; |
1677 | struct coro_transfer_args ta; |
1708 | |
1678 | |
1709 | prepare_cede_notself (aTHX_ &ta); |
1679 | prepare_cede_notself (aTHX_ &ta); |
1710 | TRANSFER (ta, 1); |
1680 | TRANSFER (ta, 1); |
1711 | return 1; |
1681 | return 1; |
1712 | } |
1682 | } |
1713 | else |
1683 | else |
1714 | return 0; |
1684 | return 0; |
1715 | } |
1685 | } |
1716 | |
1686 | |
1717 | static void |
1687 | static void |
1718 | api_trace (SV *coro_sv, int flags) |
1688 | api_trace (pTHX_ SV *coro_sv, int flags) |
1719 | { |
1689 | { |
1720 | dTHX; |
|
|
1721 | struct coro *coro = SvSTATE (coro_sv); |
1690 | struct coro *coro = SvSTATE (coro_sv); |
1722 | |
1691 | |
1723 | if (flags & CC_TRACE) |
1692 | if (flags & CC_TRACE) |
1724 | { |
1693 | { |
1725 | if (!coro->cctx) |
1694 | if (!coro->cctx) |
1726 | coro->cctx = cctx_new_run (); |
1695 | coro->cctx = cctx_new_run (); |
1727 | else if (!(coro->cctx->flags & CC_TRACE)) |
1696 | else if (!(coro->cctx->flags & CC_TRACE)) |
1728 | croak ("cannot enable tracing on coroutine with custom stack"); |
1697 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1729 | |
1698 | |
1730 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1699 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1731 | } |
1700 | } |
1732 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1701 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1733 | { |
1702 | { |
… | |
… | |
1738 | else |
1707 | else |
1739 | coro->slot->runops = RUNOPS_DEFAULT; |
1708 | coro->slot->runops = RUNOPS_DEFAULT; |
1740 | } |
1709 | } |
1741 | } |
1710 | } |
1742 | |
1711 | |
1743 | #if 0 |
|
|
1744 | static int |
|
|
1745 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
1746 | { |
|
|
1747 | AV *padlist; |
|
|
1748 | AV *av = (AV *)mg->mg_obj; |
|
|
1749 | |
|
|
1750 | abort (); |
|
|
1751 | |
|
|
1752 | return 0; |
|
|
1753 | } |
|
|
1754 | |
|
|
1755 | static MGVTBL coro_gensub_vtbl = { |
|
|
1756 | 0, 0, 0, 0, |
|
|
1757 | coro_gensub_free |
|
|
1758 | }; |
|
|
1759 | #endif |
|
|
1760 | |
|
|
1761 | /*****************************************************************************/ |
1712 | /*****************************************************************************/ |
1762 | /* PerlIO::cede */ |
1713 | /* PerlIO::cede */ |
1763 | |
1714 | |
1764 | typedef struct |
1715 | typedef struct |
1765 | { |
1716 | { |
… | |
… | |
1792 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
1743 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
1793 | double now = nvtime (); |
1744 | double now = nvtime (); |
1794 | |
1745 | |
1795 | if (now >= self->next) |
1746 | if (now >= self->next) |
1796 | { |
1747 | { |
1797 | api_cede (); |
1748 | api_cede (aTHX); |
1798 | self->next = now + self->every; |
1749 | self->next = now + self->every; |
1799 | } |
1750 | } |
1800 | |
1751 | |
1801 | return PerlIOBuf_flush (aTHX_ f); |
1752 | return PerlIOBuf_flush (aTHX_ f); |
1802 | } |
1753 | } |
… | |
… | |
1833 | PerlIOBuf_set_ptrcnt, |
1784 | PerlIOBuf_set_ptrcnt, |
1834 | }; |
1785 | }; |
1835 | |
1786 | |
1836 | /*****************************************************************************/ |
1787 | /*****************************************************************************/ |
1837 | |
1788 | |
1838 | static const CV *ssl_cv; /* for quick consistency check */ |
1789 | static const CV *slf_cv; /* for quick consistency check */ |
1839 | |
1790 | |
1840 | static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */ |
1791 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
1841 | static SV *ssl_arg0; |
1792 | static SV *slf_arg0; |
1842 | static SV *ssl_arg1; |
1793 | static SV *slf_arg1; |
|
|
1794 | static SV *slf_arg2; |
1843 | |
1795 | |
1844 | /* this restores the stack in the case we patched the entersub, to */ |
1796 | /* this restores the stack in the case we patched the entersub, to */ |
1845 | /* recreate the stack frame as perl will on following calls */ |
1797 | /* recreate the stack frame as perl will on following calls */ |
1846 | /* since entersub cleared the stack */ |
1798 | /* since entersub cleared the stack */ |
1847 | static OP * |
1799 | static OP * |
… | |
… | |
1850 | dSP; |
1802 | dSP; |
1851 | |
1803 | |
1852 | PUSHMARK (SP); |
1804 | PUSHMARK (SP); |
1853 | |
1805 | |
1854 | EXTEND (SP, 3); |
1806 | EXTEND (SP, 3); |
1855 | if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0; |
1807 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
1856 | if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0; |
1808 | if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); |
|
|
1809 | if (slf_arg2) PUSHs (sv_2mortal (slf_arg2)); |
1857 | PUSHs ((SV *)CvGV (ssl_cv)); |
1810 | PUSHs ((SV *)CvGV (slf_cv)); |
1858 | |
1811 | |
1859 | RETURNOP (ssl_restore.op_first); |
1812 | RETURNOP (slf_restore.op_first); |
1860 | } |
1813 | } |
1861 | |
1814 | |
1862 | #define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */ |
1815 | static void |
|
|
1816 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
|
|
1817 | { |
|
|
1818 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
|
|
1819 | } |
1863 | |
1820 | |
1864 | /* declare prototype */ |
1821 | static void |
1865 | XS(XS_Coro__State__set_stacklevel); |
1822 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1823 | { |
|
|
1824 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
|
|
1825 | |
|
|
1826 | frame->prepare = slf_prepare_set_stacklevel; |
|
|
1827 | frame->check = slf_check_nop; |
|
|
1828 | frame->data = (void *)SvIV (arg [0]); |
|
|
1829 | } |
|
|
1830 | |
|
|
1831 | static void |
|
|
1832 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
|
|
1833 | { |
|
|
1834 | SV **arg = (SV **)slf_frame.data; |
|
|
1835 | |
|
|
1836 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
|
|
1837 | } |
|
|
1838 | |
|
|
1839 | static void |
|
|
1840 | slf_init_transfer (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1841 | { |
|
|
1842 | if (items != 2) |
|
|
1843 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); |
|
|
1844 | |
|
|
1845 | frame->prepare = slf_prepare_transfer; |
|
|
1846 | frame->check = slf_check_nop; |
|
|
1847 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
|
|
1848 | } |
|
|
1849 | |
|
|
1850 | static void |
|
|
1851 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1852 | { |
|
|
1853 | frame->prepare = prepare_schedule; |
|
|
1854 | frame->check = slf_check_nop; |
|
|
1855 | } |
|
|
1856 | |
|
|
1857 | static void |
|
|
1858 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1859 | { |
|
|
1860 | frame->prepare = prepare_cede; |
|
|
1861 | frame->check = slf_check_nop; |
|
|
1862 | } |
|
|
1863 | |
|
|
1864 | static void |
|
|
1865 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1866 | { |
|
|
1867 | frame->prepare = prepare_cede_notself; |
|
|
1868 | frame->check = slf_check_nop; |
|
|
1869 | } |
|
|
1870 | |
|
|
1871 | /* we hijack an hopefully unused CV flag for our purposes */ |
|
|
1872 | #define CVf_SLF 0x4000 |
1866 | |
1873 | |
1867 | /* |
1874 | /* |
1868 | * these not obviously related functions are all rolled into one |
1875 | * these not obviously related functions are all rolled into one |
1869 | * function to increase chances that they all will call transfer with the same |
1876 | * function to increase chances that they all will call transfer with the same |
1870 | * stack offset |
1877 | * stack offset |
|
|
1878 | * SLF stands for "schedule-like-function". |
1871 | */ |
1879 | */ |
1872 | static OP * |
1880 | static OP * |
1873 | pp_set_stacklevel (pTHX) |
1881 | pp_slf (pTHX) |
1874 | { |
1882 | { |
|
|
1883 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
|
|
1884 | |
|
|
1885 | /* set up the slf frame, unless it has already been set-up */ |
|
|
1886 | /* the latter happens when a new coro has been started */ |
|
|
1887 | /* or when a new cctx was attached to an existing coroutine */ |
|
|
1888 | if (expect_true (!slf_frame.prepare)) |
|
|
1889 | { |
|
|
1890 | /* first iteration */ |
1875 | dSP; |
1891 | dSP; |
1876 | struct transfer_args ta; |
|
|
1877 | SV **arg = PL_stack_base + TOPMARK + 1; |
1892 | SV **arg = PL_stack_base + TOPMARK + 1; |
1878 | int items = SP - arg; /* args without function object */ |
1893 | int items = SP - arg; /* args without function object */ |
|
|
1894 | SV *gv = *sp; |
1879 | |
1895 | |
1880 | /* do a quick consistency check on the "function" object, and if it isn't */ |
1896 | /* do a quick consistency check on the "function" object, and if it isn't */ |
1881 | /* for us, divert to the real entersub */ |
1897 | /* for us, divert to the real entersub */ |
1882 | if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel) |
1898 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1883 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1899 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1884 | |
1900 | |
1885 | /* pop args */ |
1901 | /* pop args */ |
1886 | SP = PL_stack_base + POPMARK; |
1902 | SP = PL_stack_base + POPMARK; |
1887 | |
1903 | |
1888 | if (!(PL_op->op_flags & OPf_STACKED)) |
1904 | if (!(PL_op->op_flags & OPf_STACKED)) |
1889 | { |
1905 | { |
1890 | /* ampersand-form of call, use @_ instead of stack */ |
1906 | /* ampersand-form of call, use @_ instead of stack */ |
1891 | AV *av = GvAV (PL_defgv); |
1907 | AV *av = GvAV (PL_defgv); |
1892 | arg = AvARRAY (av); |
1908 | arg = AvARRAY (av); |
1893 | items = AvFILLp (av) + 1; |
1909 | items = AvFILLp (av) + 1; |
|
|
1910 | } |
|
|
1911 | |
|
|
1912 | PUTBACK; |
|
|
1913 | |
|
|
1914 | /* now call the init function, which needs to set up slf_frame */ |
|
|
1915 | ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) |
|
|
1916 | (aTHX_ &slf_frame, GvCV (gv), arg, items); |
|
|
1917 | } |
|
|
1918 | |
|
|
1919 | /* now that we have a slf_frame, interpret it! */ |
|
|
1920 | /* we use a callback system not to make the code needlessly */ |
|
|
1921 | /* complicated, but so we can run multiple perl coros from one cctx */ |
|
|
1922 | |
|
|
1923 | do |
|
|
1924 | { |
|
|
1925 | struct coro_transfer_args ta; |
|
|
1926 | |
|
|
1927 | slf_frame.prepare (aTHX_ &ta); |
|
|
1928 | TRANSFER (ta, 0); |
|
|
1929 | |
|
|
1930 | checkmark = PL_stack_sp - PL_stack_base; |
|
|
1931 | } |
|
|
1932 | while (slf_frame.check (aTHX_ &slf_frame)); |
|
|
1933 | |
|
|
1934 | { |
|
|
1935 | dSP; |
|
|
1936 | SV **bot = PL_stack_base + checkmark; |
|
|
1937 | int gimme = GIMME_V; |
|
|
1938 | |
|
|
1939 | slf_frame.prepare = 0; /* invalidate the frame, so it gets initialised again next time */ |
|
|
1940 | |
|
|
1941 | /* make sure we put something on the stack in scalar context */ |
|
|
1942 | if (gimme == G_SCALAR) |
|
|
1943 | { |
|
|
1944 | if (sp == bot) |
|
|
1945 | XPUSHs (&PL_sv_undef); |
|
|
1946 | |
|
|
1947 | SP = bot + 1; |
1894 | } |
1948 | } |
1895 | |
1949 | |
1896 | PUTBACK; |
1950 | PUTBACK; |
1897 | switch (PL_op->op_private & OPpENTERSUB_SSL) |
|
|
1898 | { |
|
|
1899 | case 0: |
|
|
1900 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0])); |
|
|
1901 | break; |
|
|
1902 | |
|
|
1903 | case 1: |
|
|
1904 | if (items != 2) |
|
|
1905 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items); |
|
|
1906 | |
|
|
1907 | prepare_transfer (aTHX_ &ta, arg [0], arg [1]); |
|
|
1908 | break; |
|
|
1909 | |
|
|
1910 | case 2: |
|
|
1911 | prepare_schedule (aTHX_ &ta); |
|
|
1912 | break; |
|
|
1913 | |
|
|
1914 | case 3: |
|
|
1915 | prepare_cede (aTHX_ &ta); |
|
|
1916 | break; |
|
|
1917 | |
|
|
1918 | case 4: |
|
|
1919 | prepare_cede_notself (aTHX_ &ta); |
|
|
1920 | break; |
|
|
1921 | } |
1951 | } |
1922 | |
1952 | |
1923 | TRANSFER (ta, 0); |
1953 | return NORMAL; |
1924 | SPAGAIN; |
|
|
1925 | |
|
|
1926 | skip: |
|
|
1927 | PUTBACK; |
|
|
1928 | SSL_TAIL; |
|
|
1929 | SPAGAIN; |
|
|
1930 | RETURN; |
|
|
1931 | } |
1954 | } |
1932 | |
1955 | |
1933 | static void |
1956 | static void |
1934 | coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items) |
1957 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items) |
1935 | { |
1958 | { |
1936 | assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel)); |
|
|
1937 | |
|
|
1938 | assert (("FATAL: ssl call with illegal CV value", CvGV (cv))); |
1959 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
|
|
1960 | |
|
|
1961 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
|
|
1962 | && PL_op->op_ppaddr != pp_slf) |
|
|
1963 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
|
|
1964 | |
|
|
1965 | if (items > 3) |
|
|
1966 | croak ("Coro only supports up to three arguments to SLF functions currently (not %d), caught", items); |
|
|
1967 | |
|
|
1968 | CvFLAGS (cv) |= CVf_SLF; |
|
|
1969 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
1939 | ssl_cv = cv; |
1970 | slf_cv = cv; |
1940 | |
1971 | |
1941 | /* we patch the op, and then re-run the whole call */ |
1972 | /* we patch the op, and then re-run the whole call */ |
1942 | /* we have to put some dummy argument on the stack for this to work */ |
1973 | /* we have to put the same argument on the stack for this to work */ |
|
|
1974 | /* and this will be done by pp_restore */ |
1943 | ssl_restore.op_next = (OP *)&ssl_restore; |
1975 | slf_restore.op_next = (OP *)&slf_restore; |
1944 | ssl_restore.op_type = OP_NULL; |
1976 | slf_restore.op_type = OP_CUSTOM; |
1945 | ssl_restore.op_ppaddr = pp_restore; |
1977 | slf_restore.op_ppaddr = pp_restore; |
1946 | ssl_restore.op_first = PL_op; |
1978 | slf_restore.op_first = PL_op; |
1947 | |
1979 | |
1948 | ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0; |
1980 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
1949 | ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0; |
1981 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
|
|
1982 | slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0; |
1950 | |
1983 | |
1951 | PL_op->op_ppaddr = pp_set_stacklevel; |
1984 | PL_op->op_ppaddr = pp_slf; |
1952 | PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */ |
|
|
1953 | |
1985 | |
1954 | PL_op = (OP *)&ssl_restore; |
1986 | PL_op = (OP *)&slf_restore; |
1955 | } |
1987 | } |
|
|
1988 | |
|
|
1989 | /*****************************************************************************/ |
|
|
1990 | |
|
|
1991 | static void |
|
|
1992 | coro_semaphore_adjust (pTHX_ AV *av, int adjust) |
|
|
1993 | { |
|
|
1994 | SV *count_sv = AvARRAY (av)[0]; |
|
|
1995 | IV count = SvIVX (count_sv); |
|
|
1996 | |
|
|
1997 | count += adjust; |
|
|
1998 | SvIVX (count_sv) = count; |
|
|
1999 | |
|
|
2000 | /* now wake up as many waiters as possible */ |
|
|
2001 | while (count > 0 && AvFILLp (av) >= count) |
|
|
2002 | { |
|
|
2003 | SV *cb; |
|
|
2004 | |
|
|
2005 | /* swap first two elements so we can shift a waiter */ |
|
|
2006 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2007 | AvARRAY (av)[1] = count_sv; |
|
|
2008 | cb = av_shift (av); |
|
|
2009 | |
|
|
2010 | if (SvOBJECT (cb)) |
|
|
2011 | api_ready (aTHX_ cb); |
|
|
2012 | else |
|
|
2013 | croak ("callbacks not yet supported"); |
|
|
2014 | |
|
|
2015 | SvREFCNT_dec (cb); |
|
|
2016 | |
|
|
2017 | --count; |
|
|
2018 | } |
|
|
2019 | } |
|
|
2020 | |
|
|
2021 | static void |
|
|
2022 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
|
|
2023 | { |
|
|
2024 | /* call $sem->adjust (0) to possibly wake up some waiters */ |
|
|
2025 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
|
|
2026 | } |
|
|
2027 | |
|
|
2028 | static int |
|
|
2029 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
|
|
2030 | { |
|
|
2031 | AV *av = (AV *)frame->data; |
|
|
2032 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2033 | |
|
|
2034 | if (SvIVX (count_sv) > 0) |
|
|
2035 | { |
|
|
2036 | SvSTATE_current->on_destroy = 0; |
|
|
2037 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
|
|
2038 | return 0; |
|
|
2039 | } |
|
|
2040 | else |
|
|
2041 | { |
|
|
2042 | int i; |
|
|
2043 | /* if we were woken up but can't down, we look through the whole */ |
|
|
2044 | /* waiters list and only add us if we aren't in there already */ |
|
|
2045 | /* this avoids some degenerate memory usage cases */ |
|
|
2046 | |
|
|
2047 | for (i = 1; i <= AvFILLp (av); ++i) |
|
|
2048 | if (AvARRAY (av)[i] == SvRV (coro_current)) |
|
|
2049 | return 1; |
|
|
2050 | |
|
|
2051 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
|
|
2052 | return 1; |
|
|
2053 | } |
|
|
2054 | } |
|
|
2055 | |
|
|
2056 | static void |
|
|
2057 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2058 | { |
|
|
2059 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2060 | |
|
|
2061 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2062 | { |
|
|
2063 | frame->data = (void *)av; |
|
|
2064 | frame->prepare = prepare_nop; |
|
|
2065 | } |
|
|
2066 | else |
|
|
2067 | { |
|
|
2068 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
|
|
2069 | |
|
|
2070 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
|
|
2071 | frame->prepare = prepare_schedule; |
|
|
2072 | |
|
|
2073 | /* to avoid race conditions when a woken-up coro gets terminated */ |
|
|
2074 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
|
|
2075 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2076 | } |
|
|
2077 | |
|
|
2078 | frame->check = slf_check_semaphore_down; |
|
|
2079 | |
|
|
2080 | } |
|
|
2081 | |
|
|
2082 | /*****************************************************************************/ |
|
|
2083 | |
|
|
2084 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2085 | |
|
|
2086 | /* create a closure from XS, returns a code reference */ |
|
|
2087 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2088 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2089 | static SV * |
|
|
2090 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2091 | { |
|
|
2092 | CV *cv = (CV *)NEWSV (0, 0); |
|
|
2093 | |
|
|
2094 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
2095 | |
|
|
2096 | CvANON_on (cv); |
|
|
2097 | CvISXSUB_on (cv); |
|
|
2098 | CvXSUB (cv) = xsub; |
|
|
2099 | GENSUB_ARG = arg; |
|
|
2100 | |
|
|
2101 | return newRV_noinc ((SV *)cv); |
|
|
2102 | } |
|
|
2103 | |
|
|
2104 | /*****************************************************************************/ |
1956 | |
2105 | |
1957 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2106 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1958 | |
2107 | |
1959 | PROTOTYPES: DISABLE |
2108 | PROTOTYPES: DISABLE |
1960 | |
2109 | |
1961 | BOOT: |
2110 | BOOT: |
1962 | { |
2111 | { |
1963 | #ifdef USE_ITHREADS |
2112 | #ifdef USE_ITHREADS |
1964 | MUTEX_INIT (&coro_lock); |
|
|
1965 | # if CORO_PTHREAD |
2113 | # if CORO_PTHREAD |
1966 | coro_thx = PERL_GET_CONTEXT; |
2114 | coro_thx = PERL_GET_CONTEXT; |
1967 | # endif |
2115 | # endif |
1968 | #endif |
2116 | #endif |
1969 | BOOT_PAGESIZE; |
2117 | BOOT_PAGESIZE; |
… | |
… | |
1990 | main_top_env = PL_top_env; |
2138 | main_top_env = PL_top_env; |
1991 | |
2139 | |
1992 | while (main_top_env->je_prev) |
2140 | while (main_top_env->je_prev) |
1993 | main_top_env = main_top_env->je_prev; |
2141 | main_top_env = main_top_env->je_prev; |
1994 | |
2142 | |
|
|
2143 | { |
|
|
2144 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
|
|
2145 | |
|
|
2146 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
|
|
2147 | hv_store_ent (PL_custom_op_names, slf, |
|
|
2148 | newSVpv ("coro_slf", 0), 0); |
|
|
2149 | |
|
|
2150 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
|
|
2151 | hv_store_ent (PL_custom_op_descs, slf, |
|
|
2152 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2153 | } |
|
|
2154 | |
1995 | coroapi.ver = CORO_API_VERSION; |
2155 | coroapi.ver = CORO_API_VERSION; |
1996 | coroapi.rev = CORO_API_REVISION; |
2156 | coroapi.rev = CORO_API_REVISION; |
|
|
2157 | |
1997 | coroapi.transfer = api_transfer; |
2158 | coroapi.transfer = api_transfer; |
|
|
2159 | |
|
|
2160 | coroapi.sv_state = SvSTATE_; |
|
|
2161 | coroapi.execute_slf = api_execute_slf; |
|
|
2162 | coroapi.prepare_nop = prepare_nop; |
|
|
2163 | coroapi.prepare_schedule = prepare_schedule; |
|
|
2164 | coroapi.prepare_cede = prepare_cede; |
|
|
2165 | coroapi.prepare_cede_notself = prepare_cede_notself; |
1998 | |
2166 | |
1999 | { |
2167 | { |
2000 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
2168 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
2001 | |
2169 | |
2002 | if (!svp) croak ("Time::HiRes is required"); |
2170 | if (!svp) croak ("Time::HiRes is required"); |
… | |
… | |
2037 | OUTPUT: |
2205 | OUTPUT: |
2038 | RETVAL |
2206 | RETVAL |
2039 | |
2207 | |
2040 | void |
2208 | void |
2041 | _set_stacklevel (...) |
2209 | _set_stacklevel (...) |
2042 | ALIAS: |
2210 | CODE: |
2043 | Coro::State::transfer = 1 |
2211 | api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items); |
2044 | Coro::schedule = 2 |
2212 | |
2045 | Coro::cede = 3 |
2213 | void |
2046 | Coro::cede_notself = 4 |
2214 | transfer (...) |
2047 | CODE: |
2215 | PROTOTYPE: $$ |
2048 | coro_ssl_patch (aTHX_ cv, ix, &ST (0), items); |
2216 | CODE: |
|
|
2217 | api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items); |
2049 | |
2218 | |
2050 | bool |
2219 | bool |
2051 | _destroy (SV *coro_sv) |
2220 | _destroy (SV *coro_sv) |
2052 | CODE: |
2221 | CODE: |
2053 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2222 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
2060 | CODE: |
2229 | CODE: |
2061 | _exit (code); |
2230 | _exit (code); |
2062 | |
2231 | |
2063 | int |
2232 | int |
2064 | cctx_stacksize (int new_stacksize = 0) |
2233 | cctx_stacksize (int new_stacksize = 0) |
|
|
2234 | PROTOTYPE: ;$ |
2065 | CODE: |
2235 | CODE: |
2066 | RETVAL = cctx_stacksize; |
2236 | RETVAL = cctx_stacksize; |
2067 | if (new_stacksize) |
2237 | if (new_stacksize) |
2068 | { |
2238 | { |
2069 | cctx_stacksize = new_stacksize; |
2239 | cctx_stacksize = new_stacksize; |
… | |
… | |
2072 | OUTPUT: |
2242 | OUTPUT: |
2073 | RETVAL |
2243 | RETVAL |
2074 | |
2244 | |
2075 | int |
2245 | int |
2076 | cctx_max_idle (int max_idle = 0) |
2246 | cctx_max_idle (int max_idle = 0) |
|
|
2247 | PROTOTYPE: ;$ |
2077 | CODE: |
2248 | CODE: |
2078 | RETVAL = cctx_max_idle; |
2249 | RETVAL = cctx_max_idle; |
2079 | if (max_idle > 1) |
2250 | if (max_idle > 1) |
2080 | cctx_max_idle = max_idle; |
2251 | cctx_max_idle = max_idle; |
2081 | OUTPUT: |
2252 | OUTPUT: |
2082 | RETVAL |
2253 | RETVAL |
2083 | |
2254 | |
2084 | int |
2255 | int |
2085 | cctx_count () |
2256 | cctx_count () |
|
|
2257 | PROTOTYPE: |
2086 | CODE: |
2258 | CODE: |
2087 | RETVAL = cctx_count; |
2259 | RETVAL = cctx_count; |
2088 | OUTPUT: |
2260 | OUTPUT: |
2089 | RETVAL |
2261 | RETVAL |
2090 | |
2262 | |
2091 | int |
2263 | int |
2092 | cctx_idle () |
2264 | cctx_idle () |
|
|
2265 | PROTOTYPE: |
2093 | CODE: |
2266 | CODE: |
2094 | RETVAL = cctx_idle; |
2267 | RETVAL = cctx_idle; |
2095 | OUTPUT: |
2268 | OUTPUT: |
2096 | RETVAL |
2269 | RETVAL |
2097 | |
2270 | |
2098 | void |
2271 | void |
2099 | list () |
2272 | list () |
|
|
2273 | PROTOTYPE: |
2100 | PPCODE: |
2274 | PPCODE: |
2101 | { |
2275 | { |
2102 | struct coro *coro; |
2276 | struct coro *coro; |
2103 | for (coro = coro_first; coro; coro = coro->next) |
2277 | for (coro = coro_first; coro; coro = coro->next) |
2104 | if (coro->hv) |
2278 | if (coro->hv) |
… | |
… | |
2171 | SvREFCNT_dec (self->throw); |
2345 | SvREFCNT_dec (self->throw); |
2172 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2346 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2173 | |
2347 | |
2174 | void |
2348 | void |
2175 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2349 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
2350 | PROTOTYPE: $;$ |
|
|
2351 | C_ARGS: aTHX_ coro, flags |
2176 | |
2352 | |
2177 | SV * |
2353 | SV * |
2178 | has_cctx (Coro::State coro) |
2354 | has_cctx (Coro::State coro) |
2179 | PROTOTYPE: $ |
2355 | PROTOTYPE: $ |
2180 | CODE: |
2356 | CODE: |
… | |
… | |
2204 | OUTPUT: |
2380 | OUTPUT: |
2205 | RETVAL |
2381 | RETVAL |
2206 | |
2382 | |
2207 | void |
2383 | void |
2208 | force_cctx () |
2384 | force_cctx () |
|
|
2385 | PROTOTYPE: |
2209 | CODE: |
2386 | CODE: |
2210 | struct coro *coro = SvSTATE (coro_current); |
|
|
2211 | coro->cctx->idle_sp = 0; |
2387 | SvSTATE_current->cctx->idle_sp = 0; |
2212 | |
2388 | |
2213 | void |
2389 | void |
2214 | swap_defsv (Coro::State self) |
2390 | swap_defsv (Coro::State self) |
2215 | PROTOTYPE: $ |
2391 | PROTOTYPE: $ |
2216 | ALIAS: |
2392 | ALIAS: |
2217 | swap_defav = 1 |
2393 | swap_defav = 1 |
2218 | CODE: |
2394 | CODE: |
2219 | if (!self->slot) |
2395 | if (!self->slot) |
2220 | croak ("cannot swap state with coroutine that has no saved state"); |
2396 | croak ("cannot swap state with coroutine that has no saved state,"); |
2221 | else |
2397 | else |
2222 | { |
2398 | { |
2223 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
2399 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
2224 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2400 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2225 | |
2401 | |
… | |
… | |
2257 | coroapi.schedule = api_schedule; |
2433 | coroapi.schedule = api_schedule; |
2258 | coroapi.cede = api_cede; |
2434 | coroapi.cede = api_cede; |
2259 | coroapi.cede_notself = api_cede_notself; |
2435 | coroapi.cede_notself = api_cede_notself; |
2260 | coroapi.ready = api_ready; |
2436 | coroapi.ready = api_ready; |
2261 | coroapi.is_ready = api_is_ready; |
2437 | coroapi.is_ready = api_is_ready; |
2262 | coroapi.nready = &coro_nready; |
2438 | coroapi.nready = coro_nready; |
2263 | coroapi.current = coro_current; |
2439 | coroapi.current = coro_current; |
2264 | |
2440 | |
2265 | GCoroAPI = &coroapi; |
2441 | GCoroAPI = &coroapi; |
2266 | sv_setiv (sv, (IV)&coroapi); |
2442 | sv_setiv (sv, (IV)&coroapi); |
2267 | SvREADONLY_on (sv); |
2443 | SvREADONLY_on (sv); |
2268 | } |
2444 | } |
2269 | } |
2445 | } |
|
|
2446 | |
|
|
2447 | void |
|
|
2448 | schedule (...) |
|
|
2449 | CODE: |
|
|
2450 | api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), 0); |
|
|
2451 | |
|
|
2452 | void |
|
|
2453 | cede (...) |
|
|
2454 | CODE: |
|
|
2455 | api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), 0); |
|
|
2456 | |
|
|
2457 | void |
|
|
2458 | cede_notself (...) |
|
|
2459 | CODE: |
|
|
2460 | api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), 0); |
2270 | |
2461 | |
2271 | void |
2462 | void |
2272 | _set_current (SV *current) |
2463 | _set_current (SV *current) |
2273 | PROTOTYPE: $ |
2464 | PROTOTYPE: $ |
2274 | CODE: |
2465 | CODE: |
… | |
… | |
2277 | |
2468 | |
2278 | void |
2469 | void |
2279 | _set_readyhook (SV *hook) |
2470 | _set_readyhook (SV *hook) |
2280 | PROTOTYPE: $ |
2471 | PROTOTYPE: $ |
2281 | CODE: |
2472 | CODE: |
2282 | LOCK; |
|
|
2283 | SvREFCNT_dec (coro_readyhook); |
2473 | SvREFCNT_dec (coro_readyhook); |
2284 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2474 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2285 | UNLOCK; |
|
|
2286 | |
2475 | |
2287 | int |
2476 | int |
2288 | prio (Coro::State coro, int newprio = 0) |
2477 | prio (Coro::State coro, int newprio = 0) |
|
|
2478 | PROTOTYPE: $;$ |
2289 | ALIAS: |
2479 | ALIAS: |
2290 | nice = 1 |
2480 | nice = 1 |
2291 | CODE: |
2481 | CODE: |
2292 | { |
2482 | { |
2293 | RETVAL = coro->prio; |
2483 | RETVAL = coro->prio; |
… | |
… | |
2308 | |
2498 | |
2309 | SV * |
2499 | SV * |
2310 | ready (SV *self) |
2500 | ready (SV *self) |
2311 | PROTOTYPE: $ |
2501 | PROTOTYPE: $ |
2312 | CODE: |
2502 | CODE: |
2313 | RETVAL = boolSV (api_ready (self)); |
2503 | RETVAL = boolSV (api_ready (aTHX_ self)); |
2314 | OUTPUT: |
2504 | OUTPUT: |
2315 | RETVAL |
2505 | RETVAL |
2316 | |
2506 | |
2317 | int |
2507 | int |
2318 | nready (...) |
2508 | nready (...) |
… | |
… | |
2325 | # for async_pool speedup |
2515 | # for async_pool speedup |
2326 | void |
2516 | void |
2327 | _pool_1 (SV *cb) |
2517 | _pool_1 (SV *cb) |
2328 | CODE: |
2518 | CODE: |
2329 | { |
2519 | { |
2330 | struct coro *coro = SvSTATE (coro_current); |
|
|
2331 | HV *hv = (HV *)SvRV (coro_current); |
2520 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2521 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2332 | AV *defav = GvAV (PL_defgv); |
2522 | AV *defav = GvAV (PL_defgv); |
2333 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2523 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2334 | AV *invoke_av; |
2524 | AV *invoke_av; |
2335 | int i, len; |
2525 | int i, len; |
2336 | |
2526 | |
… | |
… | |
2357 | { |
2547 | { |
2358 | av_fill (defav, len - 1); |
2548 | av_fill (defav, len - 1); |
2359 | for (i = 0; i < len; ++i) |
2549 | for (i = 0; i < len; ++i) |
2360 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2550 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2361 | } |
2551 | } |
2362 | |
|
|
2363 | SvREFCNT_dec (invoke); |
|
|
2364 | } |
2552 | } |
2365 | |
2553 | |
2366 | void |
2554 | void |
2367 | _pool_2 (SV *cb) |
2555 | _pool_2 (SV *cb) |
2368 | CODE: |
2556 | CODE: |
2369 | { |
2557 | { |
2370 | struct coro *coro = SvSTATE (coro_current); |
2558 | struct coro *coro = SvSTATE_current; |
2371 | |
2559 | |
2372 | sv_setsv (cb, &PL_sv_undef); |
2560 | sv_setsv (cb, &PL_sv_undef); |
2373 | |
2561 | |
2374 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2562 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2375 | coro->saved_deffh = 0; |
2563 | coro->saved_deffh = 0; |
… | |
… | |
2388 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2576 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2389 | |
2577 | |
2390 | coro->prio = 0; |
2578 | coro->prio = 0; |
2391 | |
2579 | |
2392 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2580 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2393 | api_trace (coro_current, 0); |
2581 | api_trace (aTHX_ coro_current, 0); |
2394 | |
2582 | |
2395 | av_push (av_async_pool, newSVsv (coro_current)); |
2583 | av_push (av_async_pool, newSVsv (coro_current)); |
2396 | } |
2584 | } |
2397 | |
|
|
2398 | #if 0 |
|
|
2399 | |
|
|
2400 | void |
|
|
2401 | _generator_call (...) |
|
|
2402 | PROTOTYPE: @ |
|
|
2403 | PPCODE: |
|
|
2404 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2405 | xxxx |
|
|
2406 | abort (); |
|
|
2407 | |
|
|
2408 | SV * |
|
|
2409 | gensub (SV *sub, ...) |
|
|
2410 | PROTOTYPE: &;@ |
|
|
2411 | CODE: |
|
|
2412 | { |
|
|
2413 | struct coro *coro; |
|
|
2414 | MAGIC *mg; |
|
|
2415 | CV *xcv; |
|
|
2416 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2417 | int i; |
|
|
2418 | |
|
|
2419 | CvGV (ncv) = CvGV (cv); |
|
|
2420 | CvFILE (ncv) = CvFILE (cv); |
|
|
2421 | |
|
|
2422 | Newz (0, coro, 1, struct coro); |
|
|
2423 | coro->args = newAV (); |
|
|
2424 | coro->flags = CF_NEW; |
|
|
2425 | |
|
|
2426 | av_extend (coro->args, items - 1); |
|
|
2427 | for (i = 1; i < items; i++) |
|
|
2428 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2429 | |
|
|
2430 | CvISXSUB_on (ncv); |
|
|
2431 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2432 | |
|
|
2433 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2434 | |
|
|
2435 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2436 | CvANON_on (ncv); |
|
|
2437 | |
|
|
2438 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2439 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2440 | } |
|
|
2441 | OUTPUT: |
|
|
2442 | RETVAL |
|
|
2443 | |
|
|
2444 | #endif |
|
|
2445 | |
2585 | |
2446 | |
2586 | |
2447 | MODULE = Coro::State PACKAGE = Coro::AIO |
2587 | MODULE = Coro::State PACKAGE = Coro::AIO |
2448 | |
2588 | |
2449 | void |
2589 | void |
2450 | _get_state (SV *self) |
2590 | _get_state (SV *self) |
|
|
2591 | PROTOTYPE: $ |
2451 | PPCODE: |
2592 | PPCODE: |
2452 | { |
2593 | { |
2453 | AV *defav = GvAV (PL_defgv); |
2594 | AV *defav = GvAV (PL_defgv); |
2454 | AV *av = newAV (); |
2595 | AV *av = newAV (); |
2455 | int i; |
2596 | int i; |
… | |
… | |
2470 | |
2611 | |
2471 | av_push (av, data_sv); |
2612 | av_push (av, data_sv); |
2472 | |
2613 | |
2473 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
2614 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
2474 | |
2615 | |
2475 | api_ready (self); |
2616 | api_ready (aTHX_ self); |
2476 | } |
2617 | } |
2477 | |
2618 | |
2478 | void |
2619 | void |
2479 | _set_state (SV *state) |
2620 | _set_state (SV *state) |
2480 | PROTOTYPE: $ |
2621 | PROTOTYPE: $ |
… | |
… | |
2498 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2639 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2499 | |
2640 | |
2500 | BOOT: |
2641 | BOOT: |
2501 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2642 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2502 | |
2643 | |
2503 | SV * |
2644 | void |
2504 | _schedule (...) |
2645 | _schedule (...) |
2505 | PROTOTYPE: @ |
|
|
2506 | CODE: |
2646 | CODE: |
2507 | { |
2647 | { |
2508 | static int incede; |
2648 | static int incede; |
2509 | |
2649 | |
2510 | api_cede_notself (); |
2650 | api_cede_notself (aTHX); |
2511 | |
2651 | |
2512 | ++incede; |
2652 | ++incede; |
2513 | while (coro_nready >= incede && api_cede ()) |
2653 | while (coro_nready >= incede && api_cede (aTHX)) |
2514 | ; |
2654 | ; |
2515 | |
2655 | |
2516 | sv_setsv (sv_activity, &PL_sv_undef); |
2656 | sv_setsv (sv_activity, &PL_sv_undef); |
2517 | if (coro_nready >= incede) |
2657 | if (coro_nready >= incede) |
2518 | { |
2658 | { |
… | |
… | |
2529 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2669 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2530 | |
2670 | |
2531 | BOOT: |
2671 | BOOT: |
2532 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2672 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2533 | |
2673 | |
|
|
2674 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
|
|
2675 | |
|
|
2676 | SV * |
|
|
2677 | new (SV *klass, SV *count_ = 0) |
|
|
2678 | CODE: |
|
|
2679 | { |
|
|
2680 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2681 | AV *av = newAV (); |
|
|
2682 | av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); |
|
|
2683 | RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); |
|
|
2684 | } |
|
|
2685 | OUTPUT: |
|
|
2686 | RETVAL |
|
|
2687 | |
|
|
2688 | SV * |
|
|
2689 | count (SV *self) |
|
|
2690 | CODE: |
|
|
2691 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
|
|
2692 | OUTPUT: |
|
|
2693 | RETVAL |
|
|
2694 | |
|
|
2695 | void |
|
|
2696 | up (SV *self, int adjust = 1) |
|
|
2697 | ALIAS: |
|
|
2698 | adjust = 1 |
|
|
2699 | CODE: |
|
|
2700 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
|
|
2701 | |
|
|
2702 | void |
|
|
2703 | down (SV *self) |
|
|
2704 | CODE: |
|
|
2705 | api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1); |
|
|
2706 | |
|
|
2707 | void |
|
|
2708 | try (SV *self) |
|
|
2709 | PPCODE: |
|
|
2710 | { |
|
|
2711 | AV *av = (AV *)SvRV (self); |
|
|
2712 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2713 | IV count = SvIVX (count_sv); |
|
|
2714 | |
|
|
2715 | if (count > 0) |
|
|
2716 | { |
|
|
2717 | --count; |
|
|
2718 | SvIVX (count_sv) = count; |
|
|
2719 | XSRETURN_YES; |
|
|
2720 | } |
|
|
2721 | else |
|
|
2722 | XSRETURN_NO; |
|
|
2723 | } |
|
|
2724 | |
|
|
2725 | void |
|
|
2726 | waiters (SV *self) |
|
|
2727 | CODE: |
|
|
2728 | { |
|
|
2729 | AV *av = (AV *)SvRV (self); |
|
|
2730 | |
|
|
2731 | if (GIMME_V == G_SCALAR) |
|
|
2732 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
|
|
2733 | else |
|
|
2734 | { |
|
|
2735 | int i; |
|
|
2736 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
|
|
2737 | for (i = 1; i <= AvFILLp (av); ++i) |
|
|
2738 | PUSHs (newSVsv (AvARRAY (av)[i])); |
|
|
2739 | } |
|
|
2740 | } |
|
|
2741 | |