… | |
… | |
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) |
128 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
|
|
129 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
131 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
130 | # define INLINE static inline |
132 | # define INLINE static inline |
131 | #else |
133 | #else |
132 | # define attribute(x) |
134 | # define attribute(x) |
133 | # define BARRIER |
|
|
134 | # define expect(expr,value) (expr) |
135 | # define expect(expr,value) (expr) |
135 | # define INLINE static |
136 | # define INLINE static |
136 | #endif |
137 | #endif |
137 | |
138 | |
138 | #define expect_false(expr) expect ((expr) != 0, 0) |
139 | #define expect_false(expr) expect ((expr) != 0, 0) |
… | |
… | |
141 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
142 | |
143 | |
143 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
144 | |
145 | |
145 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
146 | |
|
|
147 | static perl_mutex coro_lock; |
|
|
148 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
|
|
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
150 | # if CORO_PTHREAD |
147 | # if CORO_PTHREAD |
151 | static void *coro_thx; |
148 | static void *coro_thx; |
152 | # endif |
149 | # endif |
153 | |
|
|
154 | #else |
|
|
155 | |
|
|
156 | # define LOCK (void)0 |
|
|
157 | # define UNLOCK (void)0 |
|
|
158 | |
|
|
159 | #endif |
150 | #endif |
160 | |
|
|
161 | # undef LOCK |
|
|
162 | # define LOCK (void)0 |
|
|
163 | # undef UNLOCK |
|
|
164 | # define UNLOCK (void)0 |
|
|
165 | |
151 | |
166 | /* helper storage struct for Coro::AIO */ |
152 | /* helper storage struct for Coro::AIO */ |
167 | struct io_state |
153 | struct io_state |
168 | { |
154 | { |
169 | AV *res; |
155 | AV *res; |
… | |
… | |
182 | static JMPENV *main_top_env; |
168 | static JMPENV *main_top_env; |
183 | static HV *coro_state_stash, *coro_stash; |
169 | static HV *coro_state_stash, *coro_stash; |
184 | 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 */ |
185 | static volatile struct coro *transfer_next; |
171 | static volatile struct coro *transfer_next; |
186 | |
172 | |
187 | struct transfer_args |
|
|
188 | { |
|
|
189 | struct coro *prev, *next; |
|
|
190 | }; |
|
|
191 | |
|
|
192 | static GV *irsgv; /* $/ */ |
173 | static GV *irsgv; /* $/ */ |
193 | static GV *stdoutgv; /* *STDOUT */ |
174 | static GV *stdoutgv; /* *STDOUT */ |
194 | static SV *rv_diehook; |
175 | static SV *rv_diehook; |
195 | static SV *rv_warnhook; |
176 | static SV *rv_warnhook; |
196 | static HV *hv_sig; /* %SIG */ |
177 | static HV *hv_sig; /* %SIG */ |
… | |
… | |
214 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
195 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
215 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
196 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
216 | }; |
197 | }; |
217 | |
198 | |
218 | /* this is a structure representing a c-level coroutine */ |
199 | /* this is a structure representing a c-level coroutine */ |
219 | typedef struct coro_cctx { |
200 | typedef struct coro_cctx |
|
|
201 | { |
220 | struct coro_cctx *next; |
202 | struct coro_cctx *next; |
221 | |
203 | |
222 | /* the stack */ |
204 | /* the stack */ |
223 | void *sptr; |
205 | void *sptr; |
224 | size_t ssize; |
206 | size_t ssize; |
… | |
… | |
242 | CF_NEW = 0x0004, /* has never been switched to */ |
224 | CF_NEW = 0x0004, /* has never been switched to */ |
243 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
225 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
244 | }; |
226 | }; |
245 | |
227 | |
246 | /* 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 */ |
247 | typedef struct { |
229 | typedef struct |
|
|
230 | { |
248 | SV *defsv; |
231 | SV *defsv; |
249 | AV *defav; |
232 | AV *defav; |
250 | SV *errsv; |
233 | SV *errsv; |
251 | SV *irsgv; |
234 | SV *irsgv; |
252 | #define VAR(name,type) type name; |
235 | #define VAR(name,type) type name; |
… | |
… | |
256 | |
239 | |
257 | #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)) |
258 | |
241 | |
259 | /* this is a structure representing a perl-level coroutine */ |
242 | /* this is a structure representing a perl-level coroutine */ |
260 | struct coro { |
243 | struct coro { |
261 | /* the c coroutine allocated to this perl coroutine, if any */ |
244 | /* the C coroutine allocated to this perl coroutine, if any */ |
262 | coro_cctx *cctx; |
245 | coro_cctx *cctx; |
263 | |
246 | |
264 | /* process data */ |
247 | /* process data */ |
|
|
248 | struct CoroSLF slf_frame; /* saved slf frame */ |
265 | AV *mainstack; |
249 | AV *mainstack; |
266 | perl_slots *slot; /* basically the saved sp */ |
250 | perl_slots *slot; /* basically the saved sp */ |
267 | |
251 | |
268 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | AV *args; /* data associated with this coroutine (initial args) */ |
269 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int refcnt; /* coroutines are refcounted, yes */ |
270 | int flags; /* CF_ flags */ |
254 | int flags; /* CF_ flags */ |
271 | 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); |
272 | |
257 | |
273 | /* statistics */ |
258 | /* statistics */ |
274 | int usecount; /* number of transfers to this coro */ |
259 | int usecount; /* number of transfers to this coro */ |
275 | |
260 | |
276 | /* coro process data */ |
261 | /* coro process data */ |
… | |
… | |
284 | struct coro *next, *prev; |
269 | struct coro *next, *prev; |
285 | }; |
270 | }; |
286 | |
271 | |
287 | typedef struct coro *Coro__State; |
272 | typedef struct coro *Coro__State; |
288 | 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 */ |
289 | |
276 | |
290 | /** Coro ********************************************************************/ |
277 | /** Coro ********************************************************************/ |
291 | |
278 | |
292 | #define PRIO_MAX 3 |
279 | #define PRIO_MAX 3 |
293 | #define PRIO_HIGH 1 |
280 | #define PRIO_HIGH 1 |
… | |
… | |
298 | |
285 | |
299 | /* for Coro.pm */ |
286 | /* for Coro.pm */ |
300 | static SV *coro_current; |
287 | static SV *coro_current; |
301 | static SV *coro_readyhook; |
288 | static SV *coro_readyhook; |
302 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
289 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
303 | static int coro_nready; |
|
|
304 | static struct coro *coro_first; |
290 | static struct coro *coro_first; |
|
|
291 | #define coro_nready coroapi.nready |
305 | |
292 | |
306 | /** lowlevel stuff **********************************************************/ |
293 | /** lowlevel stuff **********************************************************/ |
307 | |
294 | |
308 | static SV * |
295 | static SV * |
309 | coro_get_sv (pTHX_ const char *name, int create) |
296 | coro_get_sv (pTHX_ const char *name, int create) |
… | |
… | |
402 | static MGVTBL coro_cv_vtbl = { |
389 | static MGVTBL coro_cv_vtbl = { |
403 | 0, 0, 0, 0, |
390 | 0, 0, 0, 0, |
404 | coro_cv_free |
391 | coro_cv_free |
405 | }; |
392 | }; |
406 | |
393 | |
407 | #define CORO_MAGIC(sv, type) \ |
394 | #define CORO_MAGIC(sv, type) \ |
408 | SvMAGIC (sv) \ |
395 | expect_true (SvMAGIC (sv)) \ |
409 | ? SvMAGIC (sv)->mg_type == type \ |
396 | ? expect_true (SvMAGIC (sv)->mg_type == type) \ |
410 | ? SvMAGIC (sv) \ |
397 | ? SvMAGIC (sv) \ |
411 | : mg_find (sv, type) \ |
398 | : mg_find (sv, type) \ |
412 | : 0 |
399 | : 0 |
413 | |
400 | |
414 | #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) |
415 | #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) |
416 | |
403 | |
… | |
… | |
437 | mg = CORO_MAGIC_state (coro); |
424 | mg = CORO_MAGIC_state (coro); |
438 | return (struct coro *)mg->mg_ptr; |
425 | return (struct coro *)mg->mg_ptr; |
439 | } |
426 | } |
440 | |
427 | |
441 | #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)) |
442 | |
442 | |
443 | /* the next two functions merely cache the padlists */ |
443 | /* the next two functions merely cache the padlists */ |
444 | static void |
444 | static void |
445 | get_padlist (pTHX_ CV *cv) |
445 | get_padlist (pTHX_ CV *cv) |
446 | { |
446 | { |
… | |
… | |
513 | CvPADLIST (cv) = (AV *)POPs; |
513 | CvPADLIST (cv) = (AV *)POPs; |
514 | } |
514 | } |
515 | |
515 | |
516 | PUTBACK; |
516 | PUTBACK; |
517 | } |
517 | } |
|
|
518 | |
|
|
519 | slf_frame = c->slf_frame; |
518 | } |
520 | } |
519 | |
521 | |
520 | static void |
522 | static void |
521 | save_perl (pTHX_ Coro__State c) |
523 | save_perl (pTHX_ Coro__State c) |
522 | { |
524 | { |
|
|
525 | c->slf_frame = slf_frame; |
|
|
526 | |
523 | { |
527 | { |
524 | dSP; |
528 | dSP; |
525 | I32 cxix = cxstack_ix; |
529 | I32 cxix = cxstack_ix; |
526 | PERL_CONTEXT *ccstk = cxstack; |
530 | PERL_CONTEXT *ccstk = cxstack; |
527 | PERL_SI *top_si = PL_curstackinfo; |
531 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
594 | #undef VAR |
598 | #undef VAR |
595 | } |
599 | } |
596 | } |
600 | } |
597 | |
601 | |
598 | /* |
602 | /* |
599 | * allocate various perl stacks. This is an exact copy |
603 | * allocate various perl stacks. This is almost an exact copy |
600 | * of perl.c:init_stacks, except that it uses less memory |
604 | * of perl.c:init_stacks, except that it uses less memory |
601 | * on the (sometimes correct) assumption that coroutines do |
605 | * on the (sometimes correct) assumption that coroutines do |
602 | * not usually need a lot of stackspace. |
606 | * not usually need a lot of stackspace. |
603 | */ |
607 | */ |
604 | #if CORO_PREFER_PERL_FUNCTIONS |
608 | #if CORO_PREFER_PERL_FUNCTIONS |
… | |
… | |
725 | #ifndef MgPV_nolen_const |
729 | #ifndef MgPV_nolen_const |
726 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
730 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
727 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
731 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
728 | (const char*)(mg)->mg_ptr) |
732 | (const char*)(mg)->mg_ptr) |
729 | #endif |
733 | #endif |
730 | |
|
|
731 | /* we sometimes need to create the effect of entersub calling us */ |
|
|
732 | #define ENTERSUB_HEAD ENTER; SAVETMPS |
|
|
733 | /* we somtimes need to create the effect of leaving via entersub */ |
|
|
734 | #define ENTERSUB_TAIL LEAVE |
|
|
735 | |
734 | |
736 | /* |
735 | /* |
737 | * This overrides the default magic get method of %SIG elements. |
736 | * This overrides the default magic get method of %SIG elements. |
738 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
737 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
739 | * and instead of tryign to save and restore the hash elements, we just provide |
738 | * and instead of tryign to save and restore the hash elements, we just provide |
… | |
… | |
811 | |
810 | |
812 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
811 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
813 | } |
812 | } |
814 | |
813 | |
815 | 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 |
816 | coro_setup (pTHX_ struct coro *coro) |
828 | coro_setup (pTHX_ struct coro *coro) |
817 | { |
829 | { |
818 | /* |
830 | /* |
819 | * emulate part of the perl startup here. |
831 | * emulate part of the perl startup here. |
820 | */ |
832 | */ |
… | |
… | |
859 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
871 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
860 | SPAGAIN; |
872 | SPAGAIN; |
861 | } |
873 | } |
862 | |
874 | |
863 | /* 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 |
864 | * likely was suspended in set_stacklevel, called from entersub. |
876 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
865 | * set_stacklevel doesn't do anything on return, but entersub does LEAVE, |
|
|
866 | * so we ENTER here for symmetry. |
|
|
867 | */ |
877 | */ |
868 | ENTERSUB_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 */ |
869 | } |
880 | } |
870 | |
881 | |
871 | static void |
882 | static void |
872 | coro_destruct (pTHX_ struct coro *coro) |
883 | coro_destruct (pTHX_ struct coro *coro) |
873 | { |
884 | { |
… | |
… | |
917 | static int |
928 | static int |
918 | runops_trace (pTHX) |
929 | runops_trace (pTHX) |
919 | { |
930 | { |
920 | COP *oldcop = 0; |
931 | COP *oldcop = 0; |
921 | int oldcxix = -2; |
932 | int oldcxix = -2; |
922 | 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 */ |
923 | coro_cctx *cctx = coro->cctx; |
934 | coro_cctx *cctx = coro->cctx; |
924 | |
935 | |
925 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
936 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
926 | { |
937 | { |
927 | PERL_ASYNC_CHECK (); |
938 | PERL_ASYNC_CHECK (); |
… | |
… | |
1037 | TAINT_NOT; |
1048 | TAINT_NOT; |
1038 | return 0; |
1049 | return 0; |
1039 | } |
1050 | } |
1040 | |
1051 | |
1041 | static void |
1052 | static void |
1042 | prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) |
1053 | prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) |
1043 | { |
1054 | { |
1044 | ta->prev = (struct coro *)cctx; |
1055 | ta->prev = (struct coro *)cctx; |
1045 | ta->next = 0; |
1056 | ta->next = 0; |
1046 | } |
1057 | } |
1047 | |
1058 | |
… | |
… | |
1078 | INLINE void |
1089 | INLINE void |
1079 | transfer_tail (pTHX) |
1090 | transfer_tail (pTHX) |
1080 | { |
1091 | { |
1081 | struct coro *next = (struct coro *)transfer_next; |
1092 | struct coro *next = (struct coro *)transfer_next; |
1082 | 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 */ |
1083 | assert (("FATAL: transfer_next was zero in transfer_tail (please report)", next)); |
1094 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
1084 | |
1095 | |
1085 | free_coro_mortal (aTHX); |
1096 | free_coro_mortal (aTHX); |
1086 | UNLOCK; |
|
|
1087 | |
1097 | |
1088 | if (expect_false (next->throw)) |
1098 | if (expect_false (next->throw)) |
1089 | { |
1099 | { |
1090 | SV *exception = sv_2mortal (next->throw); |
1100 | SV *exception = sv_2mortal (next->throw); |
1091 | |
1101 | |
… | |
… | |
1107 | # endif |
1117 | # endif |
1108 | #endif |
1118 | #endif |
1109 | { |
1119 | { |
1110 | dTHX; |
1120 | dTHX; |
1111 | |
1121 | |
1112 | /* entersub called ENTER, but we never 'returned', undo that here */ |
1122 | /* normally we would need to skip the entersub here */ |
1113 | ENTERSUB_TAIL; |
1123 | /* not doing so will re-execute it, which is exactly what we want */ |
1114 | |
|
|
1115 | /* we now skip the entersub that did lead to transfer() */ |
|
|
1116 | PL_op = PL_op->op_next; |
1124 | /* PL_nop = PL_nop->op_next */ |
1117 | |
1125 | |
1118 | /* inject a fake subroutine call to cctx_init */ |
1126 | /* inject a fake subroutine call to cctx_init */ |
1119 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1127 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1120 | |
1128 | |
1121 | /* 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 */ |
1122 | transfer_tail (aTHX); |
1131 | transfer_tail (aTHX); |
1123 | |
1132 | |
1124 | /* 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 */ |
1125 | PL_restartop = PL_op; |
1134 | PL_restartop = PL_op; |
1126 | perl_run (PL_curinterp); |
1135 | perl_run (PL_curinterp); |
… | |
… | |
1192 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1201 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1193 | New (0, cctx->sptr, cctx_stacksize, long); |
1202 | New (0, cctx->sptr, cctx_stacksize, long); |
1194 | |
1203 | |
1195 | if (!cctx->sptr) |
1204 | if (!cctx->sptr) |
1196 | { |
1205 | { |
1197 | perror ("FATAL: unable to allocate stack for coroutine"); |
1206 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1198 | _exit (EXIT_FAILURE); |
1207 | _exit (EXIT_FAILURE); |
1199 | } |
1208 | } |
1200 | |
1209 | |
1201 | stack_start = cctx->sptr; |
1210 | stack_start = cctx->sptr; |
1202 | stack_size = cctx->ssize; |
1211 | stack_size = cctx->ssize; |
… | |
… | |
1285 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1294 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1286 | { |
1295 | { |
1287 | if (expect_true (prev != next)) |
1296 | if (expect_true (prev != next)) |
1288 | { |
1297 | { |
1289 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1298 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1290 | 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,"); |
1291 | |
1300 | |
1292 | if (expect_false (next->flags & CF_RUNNING)) |
1301 | if (expect_false (next->flags & CF_RUNNING)) |
1293 | 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,"); |
1294 | |
1303 | |
1295 | if (expect_false (next->flags & CF_DESTROYED)) |
1304 | if (expect_false (next->flags & CF_DESTROYED)) |
1296 | 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,"); |
1297 | |
1306 | |
1298 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1307 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1299 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1308 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1300 | 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,"); |
1301 | #endif |
1310 | #endif |
1302 | } |
1311 | } |
1303 | } |
1312 | } |
1304 | |
1313 | |
1305 | /* always use the TRANSFER macro */ |
1314 | /* always use the TRANSFER macro */ |
… | |
… | |
1309 | dSTACKLEVEL; |
1318 | dSTACKLEVEL; |
1310 | |
1319 | |
1311 | /* sometimes transfer is only called to set idle_sp */ |
1320 | /* sometimes transfer is only called to set idle_sp */ |
1312 | if (expect_false (!next)) |
1321 | if (expect_false (!next)) |
1313 | { |
1322 | { |
1314 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1323 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1315 | 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 */ |
1316 | } |
1325 | } |
1317 | else if (expect_true (prev != next)) |
1326 | else if (expect_true (prev != next)) |
1318 | { |
1327 | { |
1319 | coro_cctx *prev__cctx; |
1328 | coro_cctx *prev__cctx; |
… | |
… | |
1326 | prev->flags |= CF_RUNNING; |
1335 | prev->flags |= CF_RUNNING; |
1327 | } |
1336 | } |
1328 | |
1337 | |
1329 | prev->flags &= ~CF_RUNNING; |
1338 | prev->flags &= ~CF_RUNNING; |
1330 | next->flags |= CF_RUNNING; |
1339 | next->flags |= CF_RUNNING; |
1331 | |
|
|
1332 | LOCK; |
|
|
1333 | |
1340 | |
1334 | /* first get rid of the old state */ |
1341 | /* first get rid of the old state */ |
1335 | save_perl (aTHX_ prev); |
1342 | save_perl (aTHX_ prev); |
1336 | |
1343 | |
1337 | if (expect_false (next->flags & CF_NEW)) |
1344 | if (expect_false (next->flags & CF_NEW)) |
… | |
… | |
1346 | |
1353 | |
1347 | prev__cctx = prev->cctx; |
1354 | prev__cctx = prev->cctx; |
1348 | |
1355 | |
1349 | /* possibly untie and reuse the cctx */ |
1356 | /* possibly untie and reuse the cctx */ |
1350 | if (expect_true ( |
1357 | if (expect_true ( |
1351 | prev__cctx->idle_sp == STACKLEVEL |
1358 | prev__cctx->idle_sp == (void *)stacklevel |
1352 | && !(prev__cctx->flags & CC_TRACE) |
1359 | && !(prev__cctx->flags & CC_TRACE) |
1353 | && !force_cctx |
1360 | && !force_cctx |
1354 | )) |
1361 | )) |
1355 | { |
1362 | { |
1356 | /* 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 */ |
1357 | 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)); |
1358 | |
1365 | |
1359 | prev->cctx = 0; |
1366 | prev->cctx = 0; |
1360 | |
1367 | |
1361 | /* 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 */ |
… | |
… | |
1395 | coro_state_destroy (pTHX_ struct coro *coro) |
1402 | coro_state_destroy (pTHX_ struct coro *coro) |
1396 | { |
1403 | { |
1397 | if (coro->flags & CF_DESTROYED) |
1404 | if (coro->flags & CF_DESTROYED) |
1398 | return 0; |
1405 | return 0; |
1399 | |
1406 | |
|
|
1407 | if (coro->on_destroy) |
|
|
1408 | coro->on_destroy (aTHX_ coro); |
|
|
1409 | |
1400 | coro->flags |= CF_DESTROYED; |
1410 | coro->flags |= CF_DESTROYED; |
1401 | |
1411 | |
1402 | if (coro->flags & CF_READY) |
1412 | if (coro->flags & CF_READY) |
1403 | { |
1413 | { |
1404 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1414 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1405 | /* alternative: look through all ready queues and remove the coro */ |
1415 | /* alternative: look through all ready queues and remove the coro */ |
1406 | LOCK; |
|
|
1407 | --coro_nready; |
1416 | --coro_nready; |
1408 | UNLOCK; |
|
|
1409 | } |
1417 | } |
1410 | else |
1418 | else |
1411 | 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 */ |
1412 | |
1420 | |
1413 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1421 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1414 | { |
1422 | { |
1415 | struct coro temp; |
1423 | struct coro temp; |
1416 | |
1424 | |
1417 | if (coro->flags & CF_RUNNING) |
1425 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
1418 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
1419 | |
1426 | |
1420 | save_perl (aTHX_ &temp); |
1427 | save_perl (aTHX_ &temp); |
1421 | load_perl (aTHX_ coro); |
1428 | load_perl (aTHX_ coro); |
1422 | |
1429 | |
1423 | coro_destruct (aTHX_ coro); |
1430 | coro_destruct (aTHX_ coro); |
… | |
… | |
1474 | # define MGf_DUP 0 |
1481 | # define MGf_DUP 0 |
1475 | #endif |
1482 | #endif |
1476 | }; |
1483 | }; |
1477 | |
1484 | |
1478 | static void |
1485 | static void |
1479 | 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) |
1480 | { |
1487 | { |
1481 | ta->prev = SvSTATE (prev_sv); |
1488 | ta->prev = SvSTATE (prev_sv); |
1482 | ta->next = SvSTATE (next_sv); |
1489 | ta->next = SvSTATE (next_sv); |
1483 | TRANSFER_CHECK (*ta); |
1490 | TRANSFER_CHECK (*ta); |
1484 | } |
1491 | } |
1485 | |
1492 | |
1486 | static void |
1493 | static void |
1487 | api_transfer (SV *prev_sv, SV *next_sv) |
1494 | api_transfer (pTHX_ SV *prev_sv, SV *next_sv) |
1488 | { |
1495 | { |
1489 | dTHX; |
|
|
1490 | struct transfer_args ta; |
1496 | struct coro_transfer_args ta; |
1491 | |
1497 | |
1492 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1498 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1493 | TRANSFER (ta, 1); |
1499 | TRANSFER (ta, 1); |
1494 | } |
1500 | } |
1495 | |
1501 | |
1496 | /** Coro ********************************************************************/ |
1502 | /** Coro ********************************************************************/ |
1497 | |
1503 | |
1498 | static void |
1504 | INLINE void |
1499 | coro_enq (pTHX_ SV *coro_sv) |
1505 | coro_enq (pTHX_ struct coro *coro) |
1500 | { |
1506 | { |
1501 | 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)); |
1502 | } |
1508 | } |
1503 | |
1509 | |
1504 | static SV * |
1510 | INLINE SV * |
1505 | coro_deq (pTHX) |
1511 | coro_deq (pTHX) |
1506 | { |
1512 | { |
1507 | int prio; |
1513 | int prio; |
1508 | |
1514 | |
1509 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1515 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1512 | |
1518 | |
1513 | return 0; |
1519 | return 0; |
1514 | } |
1520 | } |
1515 | |
1521 | |
1516 | static int |
1522 | static int |
1517 | api_ready (SV *coro_sv) |
1523 | api_ready (pTHX_ SV *coro_sv) |
1518 | { |
1524 | { |
1519 | dTHX; |
|
|
1520 | struct coro *coro; |
1525 | struct coro *coro; |
1521 | SV *sv_hook; |
1526 | SV *sv_hook; |
1522 | void (*xs_hook)(void); |
1527 | void (*xs_hook)(void); |
1523 | |
1528 | |
1524 | if (SvROK (coro_sv)) |
1529 | if (SvROK (coro_sv)) |
… | |
… | |
1529 | if (coro->flags & CF_READY) |
1534 | if (coro->flags & CF_READY) |
1530 | return 0; |
1535 | return 0; |
1531 | |
1536 | |
1532 | coro->flags |= CF_READY; |
1537 | coro->flags |= CF_READY; |
1533 | |
1538 | |
1534 | LOCK; |
|
|
1535 | |
|
|
1536 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1539 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1537 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1540 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1538 | |
1541 | |
1539 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1542 | coro_enq (aTHX_ coro); |
1540 | ++coro_nready; |
1543 | ++coro_nready; |
1541 | |
1544 | |
1542 | UNLOCK; |
|
|
1543 | |
|
|
1544 | if (sv_hook) |
1545 | if (sv_hook) |
1545 | { |
1546 | { |
1546 | dSP; |
1547 | dSP; |
1547 | |
1548 | |
1548 | ENTER; |
1549 | ENTER; |
… | |
… | |
1562 | |
1563 | |
1563 | return 1; |
1564 | return 1; |
1564 | } |
1565 | } |
1565 | |
1566 | |
1566 | static int |
1567 | static int |
1567 | api_is_ready (SV *coro_sv) |
1568 | api_is_ready (pTHX_ SV *coro_sv) |
1568 | { |
1569 | { |
1569 | dTHX; |
|
|
1570 | |
|
|
1571 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1570 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1572 | } |
1571 | } |
1573 | |
1572 | |
1574 | INLINE void |
1573 | INLINE void |
1575 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1574 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1576 | { |
1575 | { |
1577 | SV *prev_sv, *next_sv; |
1576 | SV *prev_sv, *next_sv; |
1578 | |
1577 | |
1579 | for (;;) |
1578 | for (;;) |
1580 | { |
1579 | { |
1581 | LOCK; |
|
|
1582 | next_sv = coro_deq (aTHX); |
1580 | next_sv = coro_deq (aTHX); |
1583 | |
1581 | |
1584 | /* nothing to schedule: call the idle handler */ |
1582 | /* nothing to schedule: call the idle handler */ |
1585 | if (expect_false (!next_sv)) |
1583 | if (expect_false (!next_sv)) |
1586 | { |
1584 | { |
1587 | dSP; |
1585 | dSP; |
1588 | UNLOCK; |
|
|
1589 | |
1586 | |
1590 | ENTER; |
1587 | ENTER; |
1591 | SAVETMPS; |
1588 | SAVETMPS; |
1592 | |
1589 | |
1593 | PUSHMARK (SP); |
1590 | PUSHMARK (SP); |
… | |
… | |
1598 | FREETMPS; |
1595 | FREETMPS; |
1599 | LEAVE; |
1596 | LEAVE; |
1600 | continue; |
1597 | continue; |
1601 | } |
1598 | } |
1602 | |
1599 | |
1603 | ta->next = SvSTATE (next_sv); |
1600 | ta->next = SvSTATE_hv (next_sv); |
1604 | |
1601 | |
1605 | /* cannot transfer to destroyed coros, skip and look for next */ |
1602 | /* cannot transfer to destroyed coros, skip and look for next */ |
1606 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1603 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1607 | { |
1604 | { |
1608 | UNLOCK; |
|
|
1609 | SvREFCNT_dec (next_sv); |
1605 | SvREFCNT_dec (next_sv); |
1610 | /* coro_nready has already been taken care of by destroy */ |
1606 | /* coro_nready has already been taken care of by destroy */ |
1611 | continue; |
1607 | continue; |
1612 | } |
1608 | } |
1613 | |
1609 | |
1614 | --coro_nready; |
1610 | --coro_nready; |
1615 | UNLOCK; |
|
|
1616 | break; |
1611 | break; |
1617 | } |
1612 | } |
1618 | |
1613 | |
1619 | /* free this only after the transfer */ |
1614 | /* free this only after the transfer */ |
1620 | prev_sv = SvRV (coro_current); |
1615 | prev_sv = SvRV (coro_current); |
1621 | ta->prev = SvSTATE (prev_sv); |
1616 | ta->prev = SvSTATE_hv (prev_sv); |
1622 | TRANSFER_CHECK (*ta); |
1617 | TRANSFER_CHECK (*ta); |
1623 | 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)); |
1624 | ta->next->flags &= ~CF_READY; |
1619 | ta->next->flags &= ~CF_READY; |
1625 | SvRV_set (coro_current, next_sv); |
1620 | SvRV_set (coro_current, next_sv); |
1626 | |
1621 | |
1627 | LOCK; |
|
|
1628 | free_coro_mortal (aTHX); |
1622 | free_coro_mortal (aTHX); |
1629 | coro_mortal = prev_sv; |
1623 | coro_mortal = prev_sv; |
1630 | UNLOCK; |
|
|
1631 | } |
1624 | } |
1632 | |
1625 | |
1633 | INLINE void |
1626 | INLINE void |
1634 | prepare_cede (pTHX_ struct transfer_args *ta) |
1627 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1635 | { |
1628 | { |
1636 | api_ready (coro_current); |
1629 | api_ready (aTHX_ coro_current); |
1637 | prepare_schedule (aTHX_ ta); |
1630 | prepare_schedule (aTHX_ ta); |
1638 | } |
1631 | } |
1639 | |
1632 | |
|
|
1633 | INLINE void |
|
|
1634 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
|
|
1635 | { |
|
|
1636 | SV *prev = SvRV (coro_current); |
|
|
1637 | |
|
|
1638 | if (coro_nready) |
|
|
1639 | { |
|
|
1640 | prepare_schedule (aTHX_ ta); |
|
|
1641 | api_ready (aTHX_ prev); |
|
|
1642 | } |
|
|
1643 | else |
|
|
1644 | prepare_nop (aTHX_ ta); |
|
|
1645 | } |
|
|
1646 | |
|
|
1647 | static void |
|
|
1648 | api_schedule (pTHX) |
|
|
1649 | { |
|
|
1650 | struct coro_transfer_args ta; |
|
|
1651 | |
|
|
1652 | prepare_schedule (aTHX_ &ta); |
|
|
1653 | TRANSFER (ta, 1); |
|
|
1654 | } |
|
|
1655 | |
1640 | static int |
1656 | static int |
1641 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1657 | api_cede (pTHX) |
1642 | { |
1658 | { |
1643 | if (coro_nready) |
1659 | struct coro_transfer_args ta; |
1644 | { |
1660 | |
1645 | SV *prev = SvRV (coro_current); |
|
|
1646 | prepare_schedule (aTHX_ ta); |
1661 | prepare_cede (aTHX_ &ta); |
1647 | api_ready (prev); |
1662 | |
|
|
1663 | if (expect_true (ta.prev != ta.next)) |
|
|
1664 | { |
|
|
1665 | TRANSFER (ta, 1); |
1648 | return 1; |
1666 | return 1; |
1649 | } |
1667 | } |
1650 | else |
1668 | else |
1651 | return 0; |
1669 | return 0; |
1652 | } |
1670 | } |
1653 | |
1671 | |
1654 | static void |
|
|
1655 | api_schedule (void) |
|
|
1656 | { |
|
|
1657 | dTHX; |
|
|
1658 | struct transfer_args ta; |
|
|
1659 | |
|
|
1660 | prepare_schedule (aTHX_ &ta); |
|
|
1661 | TRANSFER (ta, 1); |
|
|
1662 | } |
|
|
1663 | |
|
|
1664 | static int |
1672 | static int |
1665 | api_cede (void) |
1673 | api_cede_notself (pTHX) |
1666 | { |
1674 | { |
1667 | dTHX; |
1675 | if (coro_nready) |
|
|
1676 | { |
1668 | struct transfer_args ta; |
1677 | struct coro_transfer_args ta; |
1669 | |
1678 | |
1670 | prepare_cede (aTHX_ &ta); |
1679 | prepare_cede_notself (aTHX_ &ta); |
1671 | |
|
|
1672 | if (expect_true (ta.prev != ta.next)) |
|
|
1673 | { |
|
|
1674 | TRANSFER (ta, 1); |
1680 | TRANSFER (ta, 1); |
1675 | return 1; |
1681 | return 1; |
1676 | } |
1682 | } |
1677 | else |
1683 | else |
1678 | return 0; |
1684 | return 0; |
1679 | } |
1685 | } |
1680 | |
1686 | |
1681 | static int |
1687 | static void |
1682 | api_cede_notself (void) |
|
|
1683 | { |
|
|
1684 | dTHX; |
|
|
1685 | struct transfer_args ta; |
|
|
1686 | |
|
|
1687 | if (prepare_cede_notself (aTHX_ &ta)) |
|
|
1688 | { |
|
|
1689 | TRANSFER (ta, 1); |
|
|
1690 | return 1; |
|
|
1691 | } |
|
|
1692 | else |
|
|
1693 | return 0; |
|
|
1694 | } |
|
|
1695 | |
|
|
1696 | static void |
|
|
1697 | api_trace (SV *coro_sv, int flags) |
1688 | api_trace (pTHX_ SV *coro_sv, int flags) |
1698 | { |
1689 | { |
1699 | dTHX; |
|
|
1700 | struct coro *coro = SvSTATE (coro_sv); |
1690 | struct coro *coro = SvSTATE (coro_sv); |
1701 | |
1691 | |
1702 | if (flags & CC_TRACE) |
1692 | if (flags & CC_TRACE) |
1703 | { |
1693 | { |
1704 | if (!coro->cctx) |
1694 | if (!coro->cctx) |
1705 | coro->cctx = cctx_new_run (); |
1695 | coro->cctx = cctx_new_run (); |
1706 | else if (!(coro->cctx->flags & CC_TRACE)) |
1696 | else if (!(coro->cctx->flags & CC_TRACE)) |
1707 | croak ("cannot enable tracing on coroutine with custom stack"); |
1697 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1708 | |
1698 | |
1709 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1699 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1710 | } |
1700 | } |
1711 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1701 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1712 | { |
1702 | { |
… | |
… | |
1717 | else |
1707 | else |
1718 | coro->slot->runops = RUNOPS_DEFAULT; |
1708 | coro->slot->runops = RUNOPS_DEFAULT; |
1719 | } |
1709 | } |
1720 | } |
1710 | } |
1721 | |
1711 | |
1722 | #if 0 |
|
|
1723 | static int |
|
|
1724 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
1725 | { |
|
|
1726 | AV *padlist; |
|
|
1727 | AV *av = (AV *)mg->mg_obj; |
|
|
1728 | |
|
|
1729 | abort (); |
|
|
1730 | |
|
|
1731 | return 0; |
|
|
1732 | } |
|
|
1733 | |
|
|
1734 | static MGVTBL coro_gensub_vtbl = { |
|
|
1735 | 0, 0, 0, 0, |
|
|
1736 | coro_gensub_free |
|
|
1737 | }; |
|
|
1738 | #endif |
|
|
1739 | |
|
|
1740 | /*****************************************************************************/ |
1712 | /*****************************************************************************/ |
1741 | /* PerlIO::cede */ |
1713 | /* PerlIO::cede */ |
1742 | |
1714 | |
1743 | typedef struct |
1715 | typedef struct |
1744 | { |
1716 | { |
… | |
… | |
1771 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
1743 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
1772 | double now = nvtime (); |
1744 | double now = nvtime (); |
1773 | |
1745 | |
1774 | if (now >= self->next) |
1746 | if (now >= self->next) |
1775 | { |
1747 | { |
1776 | api_cede (); |
1748 | api_cede (aTHX); |
1777 | self->next = now + self->every; |
1749 | self->next = now + self->every; |
1778 | } |
1750 | } |
1779 | |
1751 | |
1780 | return PerlIOBuf_flush (aTHX_ f); |
1752 | return PerlIOBuf_flush (aTHX_ f); |
1781 | } |
1753 | } |
… | |
… | |
1810 | PerlIOBuf_get_ptr, |
1782 | PerlIOBuf_get_ptr, |
1811 | PerlIOBuf_get_cnt, |
1783 | PerlIOBuf_get_cnt, |
1812 | PerlIOBuf_set_ptrcnt, |
1784 | PerlIOBuf_set_ptrcnt, |
1813 | }; |
1785 | }; |
1814 | |
1786 | |
|
|
1787 | /*****************************************************************************/ |
|
|
1788 | |
|
|
1789 | static const CV *slf_cv; /* for quick consistency check */ |
|
|
1790 | |
|
|
1791 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
|
|
1792 | static SV *slf_arg0; |
|
|
1793 | static SV *slf_arg1; |
|
|
1794 | static SV *slf_arg2; |
|
|
1795 | |
|
|
1796 | /* this restores the stack in the case we patched the entersub, to */ |
|
|
1797 | /* recreate the stack frame as perl will on following calls */ |
|
|
1798 | /* since entersub cleared the stack */ |
|
|
1799 | static OP * |
|
|
1800 | pp_restore (pTHX) |
|
|
1801 | { |
|
|
1802 | dSP; |
|
|
1803 | |
|
|
1804 | PUSHMARK (SP); |
|
|
1805 | |
|
|
1806 | EXTEND (SP, 3); |
|
|
1807 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
|
|
1808 | if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); |
|
|
1809 | if (slf_arg2) PUSHs (sv_2mortal (slf_arg2)); |
|
|
1810 | PUSHs ((SV *)CvGV (slf_cv)); |
|
|
1811 | |
|
|
1812 | RETURNOP (slf_restore.op_first); |
|
|
1813 | } |
|
|
1814 | |
|
|
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 | } |
|
|
1820 | |
|
|
1821 | static void |
|
|
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 |
|
|
1873 | |
|
|
1874 | /* |
|
|
1875 | * these not obviously related functions are all rolled into one |
|
|
1876 | * function to increase chances that they all will call transfer with the same |
|
|
1877 | * stack offset |
|
|
1878 | * SLF stands for "schedule-like-function". |
|
|
1879 | */ |
|
|
1880 | static OP * |
|
|
1881 | pp_slf (pTHX) |
|
|
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 */ |
|
|
1891 | dSP; |
|
|
1892 | SV **arg = PL_stack_base + TOPMARK + 1; |
|
|
1893 | int items = SP - arg; /* args without function object */ |
|
|
1894 | SV *gv = *sp; |
|
|
1895 | |
|
|
1896 | /* do a quick consistency check on the "function" object, and if it isn't */ |
|
|
1897 | /* for us, divert to the real entersub */ |
|
|
1898 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
|
|
1899 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
1900 | |
|
|
1901 | /* pop args */ |
|
|
1902 | SP = PL_stack_base + POPMARK; |
|
|
1903 | |
|
|
1904 | if (!(PL_op->op_flags & OPf_STACKED)) |
|
|
1905 | { |
|
|
1906 | /* ampersand-form of call, use @_ instead of stack */ |
|
|
1907 | AV *av = GvAV (PL_defgv); |
|
|
1908 | arg = AvARRAY (av); |
|
|
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; |
|
|
1948 | } |
|
|
1949 | |
|
|
1950 | PUTBACK; |
|
|
1951 | } |
|
|
1952 | |
|
|
1953 | return NORMAL; |
|
|
1954 | } |
|
|
1955 | |
|
|
1956 | static void |
|
|
1957 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items) |
|
|
1958 | { |
|
|
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; |
|
|
1970 | slf_cv = cv; |
|
|
1971 | |
|
|
1972 | /* we patch the op, and then re-run the whole call */ |
|
|
1973 | /* we have to put the same argument on the stack for this to work */ |
|
|
1974 | /* and this will be done by pp_restore */ |
|
|
1975 | slf_restore.op_next = (OP *)&slf_restore; |
|
|
1976 | slf_restore.op_type = OP_CUSTOM; |
|
|
1977 | slf_restore.op_ppaddr = pp_restore; |
|
|
1978 | slf_restore.op_first = PL_op; |
|
|
1979 | |
|
|
1980 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
|
|
1981 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
|
|
1982 | slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0; |
|
|
1983 | |
|
|
1984 | PL_op->op_ppaddr = pp_slf; |
|
|
1985 | |
|
|
1986 | PL_op = (OP *)&slf_restore; |
|
|
1987 | } |
|
|
1988 | |
|
|
1989 | /*****************************************************************************/ |
|
|
1990 | |
|
|
1991 | static void |
|
|
1992 | coro_semaphore_adjust (pTHX_ AV *av, IV 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 are expected to lock */ |
|
|
2001 | while (count > 0 && AvFILLp (av) > 0) |
|
|
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 other 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 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2066 | } |
|
|
2067 | else |
|
|
2068 | { |
|
|
2069 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
|
|
2070 | |
|
|
2071 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
|
|
2072 | frame->prepare = prepare_schedule; |
|
|
2073 | |
|
|
2074 | /* to avoid race conditions when a woken-up coro gets terminated */ |
|
|
2075 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
|
|
2076 | assert (!SvSTATE_current->on_destroy);//D |
|
|
2077 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2078 | } |
|
|
2079 | |
|
|
2080 | frame->check = slf_check_semaphore_down; |
|
|
2081 | |
|
|
2082 | } |
|
|
2083 | |
|
|
2084 | /*****************************************************************************/ |
|
|
2085 | |
|
|
2086 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2087 | |
|
|
2088 | /* create a closure from XS, returns a code reference */ |
|
|
2089 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2090 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2091 | static SV * |
|
|
2092 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2093 | { |
|
|
2094 | CV *cv = (CV *)NEWSV (0, 0); |
|
|
2095 | |
|
|
2096 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
2097 | |
|
|
2098 | CvANON_on (cv); |
|
|
2099 | CvISXSUB_on (cv); |
|
|
2100 | CvXSUB (cv) = xsub; |
|
|
2101 | GENSUB_ARG = arg; |
|
|
2102 | |
|
|
2103 | return newRV_noinc ((SV *)cv); |
|
|
2104 | } |
|
|
2105 | |
|
|
2106 | /*****************************************************************************/ |
1815 | |
2107 | |
1816 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2108 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1817 | |
2109 | |
1818 | PROTOTYPES: DISABLE |
2110 | PROTOTYPES: DISABLE |
1819 | |
2111 | |
1820 | BOOT: |
2112 | BOOT: |
1821 | { |
2113 | { |
1822 | #ifdef USE_ITHREADS |
2114 | #ifdef USE_ITHREADS |
1823 | MUTEX_INIT (&coro_lock); |
|
|
1824 | # if CORO_PTHREAD |
2115 | # if CORO_PTHREAD |
1825 | coro_thx = PERL_GET_CONTEXT; |
2116 | coro_thx = PERL_GET_CONTEXT; |
1826 | # endif |
2117 | # endif |
1827 | #endif |
2118 | #endif |
1828 | BOOT_PAGESIZE; |
2119 | BOOT_PAGESIZE; |
… | |
… | |
1849 | main_top_env = PL_top_env; |
2140 | main_top_env = PL_top_env; |
1850 | |
2141 | |
1851 | while (main_top_env->je_prev) |
2142 | while (main_top_env->je_prev) |
1852 | main_top_env = main_top_env->je_prev; |
2143 | main_top_env = main_top_env->je_prev; |
1853 | |
2144 | |
|
|
2145 | { |
|
|
2146 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
|
|
2147 | |
|
|
2148 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
|
|
2149 | hv_store_ent (PL_custom_op_names, slf, |
|
|
2150 | newSVpv ("coro_slf", 0), 0); |
|
|
2151 | |
|
|
2152 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
|
|
2153 | hv_store_ent (PL_custom_op_descs, slf, |
|
|
2154 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2155 | } |
|
|
2156 | |
1854 | coroapi.ver = CORO_API_VERSION; |
2157 | coroapi.ver = CORO_API_VERSION; |
1855 | coroapi.rev = CORO_API_REVISION; |
2158 | coroapi.rev = CORO_API_REVISION; |
|
|
2159 | |
1856 | coroapi.transfer = api_transfer; |
2160 | coroapi.transfer = api_transfer; |
|
|
2161 | |
|
|
2162 | coroapi.sv_state = SvSTATE_; |
|
|
2163 | coroapi.execute_slf = api_execute_slf; |
|
|
2164 | coroapi.prepare_nop = prepare_nop; |
|
|
2165 | coroapi.prepare_schedule = prepare_schedule; |
|
|
2166 | coroapi.prepare_cede = prepare_cede; |
|
|
2167 | coroapi.prepare_cede_notself = prepare_cede_notself; |
1857 | |
2168 | |
1858 | { |
2169 | { |
1859 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
2170 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
1860 | |
2171 | |
1861 | if (!svp) croak ("Time::HiRes is required"); |
2172 | if (!svp) croak ("Time::HiRes is required"); |
… | |
… | |
1894 | av_push (coro->args, newSVsv (ST (i))); |
2205 | av_push (coro->args, newSVsv (ST (i))); |
1895 | } |
2206 | } |
1896 | OUTPUT: |
2207 | OUTPUT: |
1897 | RETVAL |
2208 | RETVAL |
1898 | |
2209 | |
1899 | # these not obviously related functions are all rolled into the same xs |
|
|
1900 | # function to increase chances that they all will call transfer with the same |
|
|
1901 | # stack offset |
|
|
1902 | void |
2210 | void |
1903 | _set_stacklevel (...) |
2211 | _set_stacklevel (...) |
1904 | ALIAS: |
2212 | CODE: |
1905 | Coro::State::transfer = 1 |
2213 | api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items); |
1906 | Coro::schedule = 2 |
|
|
1907 | Coro::cede = 3 |
|
|
1908 | Coro::cede_notself = 4 |
|
|
1909 | CODE: |
|
|
1910 | { |
|
|
1911 | struct transfer_args ta; |
|
|
1912 | |
2214 | |
1913 | PUTBACK; |
2215 | void |
1914 | switch (ix) |
2216 | transfer (...) |
1915 | { |
2217 | PROTOTYPE: $$ |
1916 | case 0: |
2218 | CODE: |
1917 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0))); |
2219 | api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items); |
1918 | break; |
|
|
1919 | |
|
|
1920 | case 1: |
|
|
1921 | if (items != 2) |
|
|
1922 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
|
|
1923 | |
|
|
1924 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
|
|
1925 | break; |
|
|
1926 | |
|
|
1927 | case 2: |
|
|
1928 | prepare_schedule (aTHX_ &ta); |
|
|
1929 | break; |
|
|
1930 | |
|
|
1931 | case 3: |
|
|
1932 | prepare_cede (aTHX_ &ta); |
|
|
1933 | break; |
|
|
1934 | |
|
|
1935 | case 4: |
|
|
1936 | if (!prepare_cede_notself (aTHX_ &ta)) |
|
|
1937 | XSRETURN_EMPTY; |
|
|
1938 | |
|
|
1939 | break; |
|
|
1940 | } |
|
|
1941 | SPAGAIN; |
|
|
1942 | |
|
|
1943 | BARRIER; |
|
|
1944 | PUTBACK; |
|
|
1945 | TRANSFER (ta, 0); |
|
|
1946 | SPAGAIN; /* might be the sp of a different coroutine now */ |
|
|
1947 | /* be extra careful not to ever do anything after TRANSFER */ |
|
|
1948 | } |
|
|
1949 | |
2220 | |
1950 | bool |
2221 | bool |
1951 | _destroy (SV *coro_sv) |
2222 | _destroy (SV *coro_sv) |
1952 | CODE: |
2223 | CODE: |
1953 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2224 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
1960 | CODE: |
2231 | CODE: |
1961 | _exit (code); |
2232 | _exit (code); |
1962 | |
2233 | |
1963 | int |
2234 | int |
1964 | cctx_stacksize (int new_stacksize = 0) |
2235 | cctx_stacksize (int new_stacksize = 0) |
|
|
2236 | PROTOTYPE: ;$ |
1965 | CODE: |
2237 | CODE: |
1966 | RETVAL = cctx_stacksize; |
2238 | RETVAL = cctx_stacksize; |
1967 | if (new_stacksize) |
2239 | if (new_stacksize) |
1968 | { |
2240 | { |
1969 | cctx_stacksize = new_stacksize; |
2241 | cctx_stacksize = new_stacksize; |
… | |
… | |
1972 | OUTPUT: |
2244 | OUTPUT: |
1973 | RETVAL |
2245 | RETVAL |
1974 | |
2246 | |
1975 | int |
2247 | int |
1976 | cctx_max_idle (int max_idle = 0) |
2248 | cctx_max_idle (int max_idle = 0) |
|
|
2249 | PROTOTYPE: ;$ |
1977 | CODE: |
2250 | CODE: |
1978 | RETVAL = cctx_max_idle; |
2251 | RETVAL = cctx_max_idle; |
1979 | if (max_idle > 1) |
2252 | if (max_idle > 1) |
1980 | cctx_max_idle = max_idle; |
2253 | cctx_max_idle = max_idle; |
1981 | OUTPUT: |
2254 | OUTPUT: |
1982 | RETVAL |
2255 | RETVAL |
1983 | |
2256 | |
1984 | int |
2257 | int |
1985 | cctx_count () |
2258 | cctx_count () |
|
|
2259 | PROTOTYPE: |
1986 | CODE: |
2260 | CODE: |
1987 | RETVAL = cctx_count; |
2261 | RETVAL = cctx_count; |
1988 | OUTPUT: |
2262 | OUTPUT: |
1989 | RETVAL |
2263 | RETVAL |
1990 | |
2264 | |
1991 | int |
2265 | int |
1992 | cctx_idle () |
2266 | cctx_idle () |
|
|
2267 | PROTOTYPE: |
1993 | CODE: |
2268 | CODE: |
1994 | RETVAL = cctx_idle; |
2269 | RETVAL = cctx_idle; |
1995 | OUTPUT: |
2270 | OUTPUT: |
1996 | RETVAL |
2271 | RETVAL |
1997 | |
2272 | |
1998 | void |
2273 | void |
1999 | list () |
2274 | list () |
|
|
2275 | PROTOTYPE: |
2000 | PPCODE: |
2276 | PPCODE: |
2001 | { |
2277 | { |
2002 | struct coro *coro; |
2278 | struct coro *coro; |
2003 | for (coro = coro_first; coro; coro = coro->next) |
2279 | for (coro = coro_first; coro; coro = coro->next) |
2004 | if (coro->hv) |
2280 | if (coro->hv) |
… | |
… | |
2071 | SvREFCNT_dec (self->throw); |
2347 | SvREFCNT_dec (self->throw); |
2072 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2348 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2073 | |
2349 | |
2074 | void |
2350 | void |
2075 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2351 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
2352 | PROTOTYPE: $;$ |
|
|
2353 | C_ARGS: aTHX_ coro, flags |
2076 | |
2354 | |
2077 | SV * |
2355 | SV * |
2078 | has_cctx (Coro::State coro) |
2356 | has_cctx (Coro::State coro) |
2079 | PROTOTYPE: $ |
2357 | PROTOTYPE: $ |
2080 | CODE: |
2358 | CODE: |
… | |
… | |
2104 | OUTPUT: |
2382 | OUTPUT: |
2105 | RETVAL |
2383 | RETVAL |
2106 | |
2384 | |
2107 | void |
2385 | void |
2108 | force_cctx () |
2386 | force_cctx () |
|
|
2387 | PROTOTYPE: |
2109 | CODE: |
2388 | CODE: |
2110 | struct coro *coro = SvSTATE (coro_current); |
|
|
2111 | coro->cctx->idle_sp = 0; |
2389 | SvSTATE_current->cctx->idle_sp = 0; |
2112 | |
2390 | |
2113 | void |
2391 | void |
2114 | swap_defsv (Coro::State self) |
2392 | swap_defsv (Coro::State self) |
2115 | PROTOTYPE: $ |
2393 | PROTOTYPE: $ |
2116 | ALIAS: |
2394 | ALIAS: |
2117 | swap_defav = 1 |
2395 | swap_defav = 1 |
2118 | CODE: |
2396 | CODE: |
2119 | if (!self->slot) |
2397 | if (!self->slot) |
2120 | croak ("cannot swap state with coroutine that has no saved state"); |
2398 | croak ("cannot swap state with coroutine that has no saved state,"); |
2121 | else |
2399 | else |
2122 | { |
2400 | { |
2123 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
2401 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
2124 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2402 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2125 | |
2403 | |
… | |
… | |
2157 | coroapi.schedule = api_schedule; |
2435 | coroapi.schedule = api_schedule; |
2158 | coroapi.cede = api_cede; |
2436 | coroapi.cede = api_cede; |
2159 | coroapi.cede_notself = api_cede_notself; |
2437 | coroapi.cede_notself = api_cede_notself; |
2160 | coroapi.ready = api_ready; |
2438 | coroapi.ready = api_ready; |
2161 | coroapi.is_ready = api_is_ready; |
2439 | coroapi.is_ready = api_is_ready; |
2162 | coroapi.nready = &coro_nready; |
2440 | coroapi.nready = coro_nready; |
2163 | coroapi.current = coro_current; |
2441 | coroapi.current = coro_current; |
2164 | |
2442 | |
2165 | GCoroAPI = &coroapi; |
2443 | GCoroAPI = &coroapi; |
2166 | sv_setiv (sv, (IV)&coroapi); |
2444 | sv_setiv (sv, (IV)&coroapi); |
2167 | SvREADONLY_on (sv); |
2445 | SvREADONLY_on (sv); |
2168 | } |
2446 | } |
2169 | } |
2447 | } |
|
|
2448 | |
|
|
2449 | void |
|
|
2450 | schedule (...) |
|
|
2451 | CODE: |
|
|
2452 | api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), 0); |
|
|
2453 | |
|
|
2454 | void |
|
|
2455 | cede (...) |
|
|
2456 | CODE: |
|
|
2457 | api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), 0); |
|
|
2458 | |
|
|
2459 | void |
|
|
2460 | cede_notself (...) |
|
|
2461 | CODE: |
|
|
2462 | api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), 0); |
2170 | |
2463 | |
2171 | void |
2464 | void |
2172 | _set_current (SV *current) |
2465 | _set_current (SV *current) |
2173 | PROTOTYPE: $ |
2466 | PROTOTYPE: $ |
2174 | CODE: |
2467 | CODE: |
… | |
… | |
2177 | |
2470 | |
2178 | void |
2471 | void |
2179 | _set_readyhook (SV *hook) |
2472 | _set_readyhook (SV *hook) |
2180 | PROTOTYPE: $ |
2473 | PROTOTYPE: $ |
2181 | CODE: |
2474 | CODE: |
2182 | LOCK; |
|
|
2183 | SvREFCNT_dec (coro_readyhook); |
2475 | SvREFCNT_dec (coro_readyhook); |
2184 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2476 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2185 | UNLOCK; |
|
|
2186 | |
2477 | |
2187 | int |
2478 | int |
2188 | prio (Coro::State coro, int newprio = 0) |
2479 | prio (Coro::State coro, int newprio = 0) |
|
|
2480 | PROTOTYPE: $;$ |
2189 | ALIAS: |
2481 | ALIAS: |
2190 | nice = 1 |
2482 | nice = 1 |
2191 | CODE: |
2483 | CODE: |
2192 | { |
2484 | { |
2193 | RETVAL = coro->prio; |
2485 | RETVAL = coro->prio; |
… | |
… | |
2208 | |
2500 | |
2209 | SV * |
2501 | SV * |
2210 | ready (SV *self) |
2502 | ready (SV *self) |
2211 | PROTOTYPE: $ |
2503 | PROTOTYPE: $ |
2212 | CODE: |
2504 | CODE: |
2213 | RETVAL = boolSV (api_ready (self)); |
2505 | RETVAL = boolSV (api_ready (aTHX_ self)); |
2214 | OUTPUT: |
2506 | OUTPUT: |
2215 | RETVAL |
2507 | RETVAL |
2216 | |
2508 | |
2217 | int |
2509 | int |
2218 | nready (...) |
2510 | nready (...) |
… | |
… | |
2225 | # for async_pool speedup |
2517 | # for async_pool speedup |
2226 | void |
2518 | void |
2227 | _pool_1 (SV *cb) |
2519 | _pool_1 (SV *cb) |
2228 | CODE: |
2520 | CODE: |
2229 | { |
2521 | { |
2230 | struct coro *coro = SvSTATE (coro_current); |
|
|
2231 | HV *hv = (HV *)SvRV (coro_current); |
2522 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2523 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2232 | AV *defav = GvAV (PL_defgv); |
2524 | AV *defav = GvAV (PL_defgv); |
2233 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2525 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2234 | AV *invoke_av; |
2526 | AV *invoke_av; |
2235 | int i, len; |
2527 | int i, len; |
2236 | |
2528 | |
… | |
… | |
2257 | { |
2549 | { |
2258 | av_fill (defav, len - 1); |
2550 | av_fill (defav, len - 1); |
2259 | for (i = 0; i < len; ++i) |
2551 | for (i = 0; i < len; ++i) |
2260 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2552 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2261 | } |
2553 | } |
2262 | |
|
|
2263 | SvREFCNT_dec (invoke); |
|
|
2264 | } |
2554 | } |
2265 | |
2555 | |
2266 | void |
2556 | void |
2267 | _pool_2 (SV *cb) |
2557 | _pool_2 (SV *cb) |
2268 | CODE: |
2558 | CODE: |
2269 | { |
2559 | { |
2270 | struct coro *coro = SvSTATE (coro_current); |
2560 | struct coro *coro = SvSTATE_current; |
2271 | |
2561 | |
2272 | sv_setsv (cb, &PL_sv_undef); |
2562 | sv_setsv (cb, &PL_sv_undef); |
2273 | |
2563 | |
2274 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2564 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2275 | coro->saved_deffh = 0; |
2565 | coro->saved_deffh = 0; |
… | |
… | |
2288 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2578 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2289 | |
2579 | |
2290 | coro->prio = 0; |
2580 | coro->prio = 0; |
2291 | |
2581 | |
2292 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2582 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2293 | api_trace (coro_current, 0); |
2583 | api_trace (aTHX_ coro_current, 0); |
2294 | |
2584 | |
2295 | av_push (av_async_pool, newSVsv (coro_current)); |
2585 | av_push (av_async_pool, newSVsv (coro_current)); |
2296 | } |
2586 | } |
2297 | |
|
|
2298 | #if 0 |
|
|
2299 | |
|
|
2300 | void |
|
|
2301 | _generator_call (...) |
|
|
2302 | PROTOTYPE: @ |
|
|
2303 | PPCODE: |
|
|
2304 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2305 | xxxx |
|
|
2306 | abort (); |
|
|
2307 | |
|
|
2308 | SV * |
|
|
2309 | gensub (SV *sub, ...) |
|
|
2310 | PROTOTYPE: &;@ |
|
|
2311 | CODE: |
|
|
2312 | { |
|
|
2313 | struct coro *coro; |
|
|
2314 | MAGIC *mg; |
|
|
2315 | CV *xcv; |
|
|
2316 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2317 | int i; |
|
|
2318 | |
|
|
2319 | CvGV (ncv) = CvGV (cv); |
|
|
2320 | CvFILE (ncv) = CvFILE (cv); |
|
|
2321 | |
|
|
2322 | Newz (0, coro, 1, struct coro); |
|
|
2323 | coro->args = newAV (); |
|
|
2324 | coro->flags = CF_NEW; |
|
|
2325 | |
|
|
2326 | av_extend (coro->args, items - 1); |
|
|
2327 | for (i = 1; i < items; i++) |
|
|
2328 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2329 | |
|
|
2330 | CvISXSUB_on (ncv); |
|
|
2331 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2332 | |
|
|
2333 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2334 | |
|
|
2335 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2336 | CvANON_on (ncv); |
|
|
2337 | |
|
|
2338 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2339 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2340 | } |
|
|
2341 | OUTPUT: |
|
|
2342 | RETVAL |
|
|
2343 | |
|
|
2344 | #endif |
|
|
2345 | |
2587 | |
2346 | |
2588 | |
2347 | MODULE = Coro::State PACKAGE = Coro::AIO |
2589 | MODULE = Coro::State PACKAGE = Coro::AIO |
2348 | |
2590 | |
2349 | void |
2591 | void |
2350 | _get_state (SV *self) |
2592 | _get_state (SV *self) |
|
|
2593 | PROTOTYPE: $ |
2351 | PPCODE: |
2594 | PPCODE: |
2352 | { |
2595 | { |
2353 | AV *defav = GvAV (PL_defgv); |
2596 | AV *defav = GvAV (PL_defgv); |
2354 | AV *av = newAV (); |
2597 | AV *av = newAV (); |
2355 | int i; |
2598 | int i; |
… | |
… | |
2370 | |
2613 | |
2371 | av_push (av, data_sv); |
2614 | av_push (av, data_sv); |
2372 | |
2615 | |
2373 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
2616 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
2374 | |
2617 | |
2375 | api_ready (self); |
2618 | api_ready (aTHX_ self); |
2376 | } |
2619 | } |
2377 | |
2620 | |
2378 | void |
2621 | void |
2379 | _set_state (SV *state) |
2622 | _set_state (SV *state) |
2380 | PROTOTYPE: $ |
2623 | PROTOTYPE: $ |
… | |
… | |
2398 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2641 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2399 | |
2642 | |
2400 | BOOT: |
2643 | BOOT: |
2401 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2644 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2402 | |
2645 | |
2403 | SV * |
2646 | void |
2404 | _schedule (...) |
2647 | _schedule (...) |
2405 | PROTOTYPE: @ |
|
|
2406 | CODE: |
2648 | CODE: |
2407 | { |
2649 | { |
2408 | static int incede; |
2650 | static int incede; |
2409 | |
2651 | |
2410 | api_cede_notself (); |
2652 | api_cede_notself (aTHX); |
2411 | |
2653 | |
2412 | ++incede; |
2654 | ++incede; |
2413 | while (coro_nready >= incede && api_cede ()) |
2655 | while (coro_nready >= incede && api_cede (aTHX)) |
2414 | ; |
2656 | ; |
2415 | |
2657 | |
2416 | sv_setsv (sv_activity, &PL_sv_undef); |
2658 | sv_setsv (sv_activity, &PL_sv_undef); |
2417 | if (coro_nready >= incede) |
2659 | if (coro_nready >= incede) |
2418 | { |
2660 | { |
… | |
… | |
2429 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2671 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2430 | |
2672 | |
2431 | BOOT: |
2673 | BOOT: |
2432 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2674 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
2433 | |
2675 | |
|
|
2676 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
|
|
2677 | |
|
|
2678 | SV * |
|
|
2679 | new (SV *klass, SV *count_ = 0) |
|
|
2680 | CODE: |
|
|
2681 | { |
|
|
2682 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2683 | AV *av = newAV (); |
|
|
2684 | av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); |
|
|
2685 | RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); |
|
|
2686 | } |
|
|
2687 | OUTPUT: |
|
|
2688 | RETVAL |
|
|
2689 | |
|
|
2690 | SV * |
|
|
2691 | count (SV *self) |
|
|
2692 | CODE: |
|
|
2693 | RETVAL = newSVsv (AvARRAY ((AV *)SvRV (self))[0]); |
|
|
2694 | OUTPUT: |
|
|
2695 | RETVAL |
|
|
2696 | |
|
|
2697 | void |
|
|
2698 | up (SV *self, int adjust = 1) |
|
|
2699 | ALIAS: |
|
|
2700 | adjust = 1 |
|
|
2701 | CODE: |
|
|
2702 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
|
|
2703 | |
|
|
2704 | void |
|
|
2705 | down (SV *self) |
|
|
2706 | CODE: |
|
|
2707 | api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1); |
|
|
2708 | |
|
|
2709 | void |
|
|
2710 | try (SV *self) |
|
|
2711 | PPCODE: |
|
|
2712 | { |
|
|
2713 | AV *av = (AV *)SvRV (self); |
|
|
2714 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2715 | IV count = SvIVX (count_sv); |
|
|
2716 | |
|
|
2717 | if (count > 0) |
|
|
2718 | { |
|
|
2719 | --count; |
|
|
2720 | SvIVX (count_sv) = count; |
|
|
2721 | XSRETURN_YES; |
|
|
2722 | } |
|
|
2723 | else |
|
|
2724 | XSRETURN_NO; |
|
|
2725 | } |
|
|
2726 | |
|
|
2727 | void |
|
|
2728 | waiters (SV *self) |
|
|
2729 | CODE: |
|
|
2730 | { |
|
|
2731 | AV *av = (AV *)SvRV (self); |
|
|
2732 | |
|
|
2733 | if (GIMME_V == G_SCALAR) |
|
|
2734 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
|
|
2735 | else |
|
|
2736 | { |
|
|
2737 | int i; |
|
|
2738 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
|
|
2739 | for (i = 1; i <= AvFILLp (av); ++i) |
|
|
2740 | PUSHs (newSVsv (AvARRAY (av)[i])); |
|
|
2741 | } |
|
|
2742 | } |
|
|
2743 | |