1 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
2 | |
2 | |
3 | #define PERL_NO_GET_CONTEXT |
3 | #define PERL_NO_GET_CONTEXT |
|
|
4 | #define PERL_EXT |
4 | |
5 | |
5 | #include "EXTERN.h" |
6 | #include "EXTERN.h" |
6 | #include "perl.h" |
7 | #include "perl.h" |
7 | #include "XSUB.h" |
8 | #include "XSUB.h" |
8 | |
9 | |
… | |
… | |
75 | /* 5.8.7 */ |
76 | /* 5.8.7 */ |
76 | #ifndef SvRV_set |
77 | #ifndef SvRV_set |
77 | # define SvRV_set(s,v) SvRV(s) = (v) |
78 | # define SvRV_set(s,v) SvRV(s) = (v) |
78 | #endif |
79 | #endif |
79 | |
80 | |
|
|
81 | /* 5.8.8 */ |
|
|
82 | #ifndef GV_NOTQUAL |
|
|
83 | # define GV_NOTQUAL 0 |
|
|
84 | #endif |
|
|
85 | #ifndef newSV |
|
|
86 | # define newSV(l) NEWSV(0,l) |
|
|
87 | #endif |
|
|
88 | |
80 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
89 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
81 | # undef CORO_STACKGUARD |
90 | # undef CORO_STACKGUARD |
82 | #endif |
91 | #endif |
83 | |
92 | |
84 | #ifndef CORO_STACKGUARD |
93 | #ifndef CORO_STACKGUARD |
… | |
… | |
98 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
107 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
99 | |
108 | |
100 | #if __GNUC__ >= 3 |
109 | #if __GNUC__ >= 3 |
101 | # define attribute(x) __attribute__(x) |
110 | # define attribute(x) __attribute__(x) |
102 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
111 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
|
|
112 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
103 | #else |
113 | #else |
104 | # define attribute(x) |
114 | # define attribute(x) |
105 | # define BARRIER |
115 | # define BARRIER |
|
|
116 | # define expect(expr,value) (expr) |
106 | #endif |
117 | #endif |
|
|
118 | |
|
|
119 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
120 | #define expect_true(expr) expect ((expr) != 0, 1) |
107 | |
121 | |
108 | #define NOINLINE attribute ((noinline)) |
122 | #define NOINLINE attribute ((noinline)) |
109 | |
123 | |
110 | #include "CoroAPI.h" |
124 | #include "CoroAPI.h" |
111 | |
125 | |
… | |
… | |
132 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
146 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
133 | static JMPENV *main_top_env; |
147 | static JMPENV *main_top_env; |
134 | static HV *coro_state_stash, *coro_stash; |
148 | static HV *coro_state_stash, *coro_stash; |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
149 | static SV *coro_mortal; /* will be freed after next transfer */ |
136 | |
150 | |
|
|
151 | static GV *irsgv; /* $/ */ |
|
|
152 | static GV *stdoutgv; /* *STDOUT */ |
|
|
153 | static SV *sv_diehook; |
|
|
154 | static SV *sv_warnhook; |
|
|
155 | |
137 | /* async_pool helper stuff */ |
156 | /* async_pool helper stuff */ |
138 | static SV *sv_pool_rss; |
157 | static SV *sv_pool_rss; |
139 | static SV *sv_pool_size; |
158 | static SV *sv_pool_size; |
140 | static AV *av_async_pool; |
159 | static AV *av_async_pool; |
141 | |
160 | |
142 | static struct coro_cctx *cctx_first; |
161 | static struct coro_cctx *cctx_first; |
143 | static int cctx_count, cctx_idle; |
162 | static int cctx_count, cctx_idle; |
|
|
163 | |
|
|
164 | enum { |
|
|
165 | CC_MAPPED = 0x01, |
|
|
166 | CC_NOREUSE = 0x02, /* throw this away after tracing */ |
|
|
167 | CC_TRACE = 0x04, |
|
|
168 | CC_TRACE_SUB = 0x08, /* trace sub calls */ |
|
|
169 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
|
|
170 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
|
|
171 | }; |
144 | |
172 | |
145 | /* this is a structure representing a c-level coroutine */ |
173 | /* this is a structure representing a c-level coroutine */ |
146 | typedef struct coro_cctx { |
174 | typedef struct coro_cctx { |
147 | struct coro_cctx *next; |
175 | struct coro_cctx *next; |
148 | |
176 | |
… | |
… | |
157 | coro_context cctx; |
185 | coro_context cctx; |
158 | |
186 | |
159 | #if CORO_USE_VALGRIND |
187 | #if CORO_USE_VALGRIND |
160 | int valgrind_id; |
188 | int valgrind_id; |
161 | #endif |
189 | #endif |
162 | char inuse, mapped; |
190 | unsigned char flags; |
163 | } coro_cctx; |
191 | } coro_cctx; |
164 | |
192 | |
165 | enum { |
193 | enum { |
166 | CF_RUNNING = 0x0001, /* coroutine is running */ |
194 | CF_RUNNING = 0x0001, /* coroutine is running */ |
167 | CF_READY = 0x0002, /* coroutine is ready */ |
195 | CF_READY = 0x0002, /* coroutine is ready */ |
168 | CF_NEW = 0x0004, /* has never been switched to */ |
196 | CF_NEW = 0x0004, /* has never been switched to */ |
169 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
197 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
170 | }; |
198 | }; |
171 | |
199 | |
|
|
200 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
|
|
201 | typedef struct { |
|
|
202 | SV *defsv; |
|
|
203 | AV *defav; |
|
|
204 | SV *errsv; |
|
|
205 | SV *irsgv; |
|
|
206 | #define VAR(name,type) type name; |
|
|
207 | # include "state.h" |
|
|
208 | #undef VAR |
|
|
209 | } perl_slots; |
|
|
210 | |
|
|
211 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
|
|
212 | |
172 | /* this is a structure representing a perl-level coroutine */ |
213 | /* this is a structure representing a perl-level coroutine */ |
173 | struct coro { |
214 | struct coro { |
174 | /* the c coroutine allocated to this perl coroutine, if any */ |
215 | /* the c coroutine allocated to this perl coroutine, if any */ |
175 | coro_cctx *cctx; |
216 | coro_cctx *cctx; |
176 | |
217 | |
|
|
218 | /* process data */ |
|
|
219 | AV *mainstack; |
|
|
220 | perl_slots *slot; /* basically the saved sp */ |
|
|
221 | |
177 | /* data associated with this coroutine (initial args) */ |
222 | /* data associated with this coroutine (initial args) */ |
178 | AV *args; |
223 | AV *args; |
179 | int refcnt; |
224 | int refcnt; |
180 | int save; /* CORO_SAVE flags */ |
|
|
181 | int flags; /* CF_ flags */ |
225 | int flags; /* CF_ flags */ |
182 | |
226 | |
183 | /* optionally saved, might be zero */ |
227 | /* statistics */ |
184 | AV *defav; /* @_ */ |
228 | int usecount; /* number of transfers to this coro */ |
185 | SV *defsv; /* $_ */ |
|
|
186 | SV *errsv; /* $@ */ |
|
|
187 | GV *deffh; /* default filehandle */ |
|
|
188 | SV *irssv; /* $/ */ |
|
|
189 | SV *irssv_sv; /* real $/ cache */ |
|
|
190 | |
|
|
191 | #define VAR(name,type) type name; |
|
|
192 | # include "state.h" |
|
|
193 | #undef VAR |
|
|
194 | |
229 | |
195 | /* coro process data */ |
230 | /* coro process data */ |
196 | int prio; |
231 | int prio; |
|
|
232 | SV *throw; |
|
|
233 | |
|
|
234 | /* async_pool */ |
|
|
235 | SV *saved_deffh; |
197 | |
236 | |
198 | /* linked list */ |
237 | /* linked list */ |
199 | struct coro *next, *prev; |
238 | struct coro *next, *prev; |
200 | HV *hv; /* the perl hash associated with this coro, if any */ |
239 | HV *hv; /* the perl hash associated with this coro, if any */ |
201 | }; |
240 | }; |
… | |
… | |
217 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
256 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
218 | static int coro_nready; |
257 | static int coro_nready; |
219 | static struct coro *coro_first; |
258 | static struct coro *coro_first; |
220 | |
259 | |
221 | /** lowlevel stuff **********************************************************/ |
260 | /** lowlevel stuff **********************************************************/ |
|
|
261 | |
|
|
262 | static SV * |
|
|
263 | coro_get_sv (const char *name, int create) |
|
|
264 | { |
|
|
265 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
266 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
267 | get_sv (name, create); |
|
|
268 | #endif |
|
|
269 | return get_sv (name, create); |
|
|
270 | } |
|
|
271 | |
|
|
272 | static SV * |
|
|
273 | coro_get_av (const char *name, int create) |
|
|
274 | { |
|
|
275 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
276 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
277 | get_av (name, create); |
|
|
278 | #endif |
|
|
279 | return get_av (name, create); |
|
|
280 | } |
222 | |
281 | |
223 | static AV * |
282 | static AV * |
224 | coro_clone_padlist (pTHX_ CV *cv) |
283 | coro_clone_padlist (pTHX_ CV *cv) |
225 | { |
284 | { |
226 | AV *padlist = CvPADLIST (cv); |
285 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
290 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
349 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
291 | ? SvMAGIC (cv) \ |
350 | ? SvMAGIC (cv) \ |
292 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
351 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
293 | : 0 |
352 | : 0 |
294 | |
353 | |
|
|
354 | static struct coro * |
|
|
355 | SvSTATE_ (pTHX_ SV *coro) |
|
|
356 | { |
|
|
357 | HV *stash; |
|
|
358 | MAGIC *mg; |
|
|
359 | |
|
|
360 | if (SvROK (coro)) |
|
|
361 | coro = SvRV (coro); |
|
|
362 | |
|
|
363 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
364 | croak ("Coro::State object required"); |
|
|
365 | |
|
|
366 | stash = SvSTASH (coro); |
|
|
367 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
368 | { |
|
|
369 | /* very slow, but rare, check */ |
|
|
370 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
371 | croak ("Coro::State object required"); |
|
|
372 | } |
|
|
373 | |
|
|
374 | mg = CORO_MAGIC (coro); |
|
|
375 | return (struct coro *)mg->mg_ptr; |
|
|
376 | } |
|
|
377 | |
|
|
378 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
379 | |
295 | /* the next two functions merely cache the padlists */ |
380 | /* the next two functions merely cache the padlists */ |
296 | static void |
381 | static void |
297 | get_padlist (pTHX_ CV *cv) |
382 | get_padlist (pTHX_ CV *cv) |
298 | { |
383 | { |
299 | MAGIC *mg = CORO_MAGIC (cv); |
384 | MAGIC *mg = CORO_MAGIC (cv); |
300 | AV *av; |
385 | AV *av; |
301 | |
386 | |
302 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
387 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
303 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
388 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
304 | else |
389 | else |
305 | { |
390 | { |
306 | #if CORO_PREFER_PERL_FUNCTIONS |
391 | #if CORO_PREFER_PERL_FUNCTIONS |
307 | /* this is probably cleaner, but also slower? */ |
392 | /* this is probably cleaner, but also slower? */ |
… | |
… | |
319 | put_padlist (pTHX_ CV *cv) |
404 | put_padlist (pTHX_ CV *cv) |
320 | { |
405 | { |
321 | MAGIC *mg = CORO_MAGIC (cv); |
406 | MAGIC *mg = CORO_MAGIC (cv); |
322 | AV *av; |
407 | AV *av; |
323 | |
408 | |
324 | if (!mg) |
409 | if (expect_false (!mg)) |
325 | { |
410 | { |
326 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
411 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
327 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
412 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
328 | mg->mg_virtual = &vtbl_coro; |
413 | mg->mg_virtual = &vtbl_coro; |
329 | mg->mg_obj = (SV *)newAV (); |
414 | mg->mg_obj = (SV *)newAV (); |
330 | } |
415 | } |
331 | |
416 | |
332 | av = (AV *)mg->mg_obj; |
417 | av = (AV *)mg->mg_obj; |
333 | |
418 | |
334 | if (AvFILLp (av) >= AvMAX (av)) |
419 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
335 | av_extend (av, AvMAX (av) + 1); |
420 | av_extend (av, AvMAX (av) + 1); |
336 | |
421 | |
337 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
422 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
338 | } |
423 | } |
339 | |
424 | |
340 | /** load & save, init *******************************************************/ |
425 | /** load & save, init *******************************************************/ |
341 | |
426 | |
342 | #define SB do { |
|
|
343 | #define SE } while (0) |
|
|
344 | |
|
|
345 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
|
|
346 | |
|
|
347 | static void |
427 | static void |
348 | load_perl (pTHX_ Coro__State c) |
428 | load_perl (pTHX_ Coro__State c) |
349 | { |
429 | { |
|
|
430 | perl_slots *slot = c->slot; |
|
|
431 | c->slot = 0; |
|
|
432 | |
|
|
433 | PL_mainstack = c->mainstack; |
|
|
434 | |
|
|
435 | GvSV (PL_defgv) = slot->defsv; |
|
|
436 | GvAV (PL_defgv) = slot->defav; |
|
|
437 | GvSV (PL_errgv) = slot->errsv; |
|
|
438 | GvSV (irsgv) = slot->irsgv; |
|
|
439 | |
350 | #define VAR(name,type) PL_ ## name = c->name; |
440 | #define VAR(name,type) PL_ ## name = slot->name; |
351 | # include "state.h" |
441 | # include "state.h" |
352 | #undef VAR |
442 | #undef VAR |
353 | |
|
|
354 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
|
|
355 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
|
|
356 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
357 | if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); |
|
|
358 | |
|
|
359 | if (c->irssv) |
|
|
360 | { |
|
|
361 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
362 | { |
|
|
363 | SvREFCNT_dec (c->irssv); |
|
|
364 | c->irssv = 0; |
|
|
365 | } |
|
|
366 | else |
|
|
367 | { |
|
|
368 | REPLACE_SV (PL_rs, c->irssv); |
|
|
369 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
|
|
370 | sv_setsv (c->irssv_sv, PL_rs); |
|
|
371 | } |
|
|
372 | } |
|
|
373 | |
443 | |
374 | { |
444 | { |
375 | dSP; |
445 | dSP; |
|
|
446 | |
376 | CV *cv; |
447 | CV *cv; |
377 | |
448 | |
378 | /* now do the ugly restore mess */ |
449 | /* now do the ugly restore mess */ |
379 | while ((cv = (CV *)POPs)) |
450 | while (expect_true (cv = (CV *)POPs)) |
380 | { |
451 | { |
381 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
452 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
382 | CvDEPTH (cv) = PTR2IV (POPs); |
453 | CvDEPTH (cv) = PTR2IV (POPs); |
383 | CvPADLIST (cv) = (AV *)POPs; |
454 | CvPADLIST (cv) = (AV *)POPs; |
384 | } |
455 | } |
… | |
… | |
403 | |
474 | |
404 | XPUSHs (Nullsv); |
475 | XPUSHs (Nullsv); |
405 | /* this loop was inspired by pp_caller */ |
476 | /* this loop was inspired by pp_caller */ |
406 | for (;;) |
477 | for (;;) |
407 | { |
478 | { |
408 | while (cxix >= 0) |
479 | while (expect_true (cxix >= 0)) |
409 | { |
480 | { |
410 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
481 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
411 | |
482 | |
412 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
483 | if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) |
413 | { |
484 | { |
414 | CV *cv = cx->blk_sub.cv; |
485 | CV *cv = cx->blk_sub.cv; |
415 | |
486 | |
416 | if (CvDEPTH (cv)) |
487 | if (expect_true (CvDEPTH (cv))) |
417 | { |
488 | { |
418 | EXTEND (SP, 3); |
489 | EXTEND (SP, 3); |
419 | PUSHs ((SV *)CvPADLIST (cv)); |
490 | PUSHs ((SV *)CvPADLIST (cv)); |
420 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
491 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
421 | PUSHs ((SV *)cv); |
492 | PUSHs ((SV *)cv); |
… | |
… | |
424 | get_padlist (aTHX_ cv); |
495 | get_padlist (aTHX_ cv); |
425 | } |
496 | } |
426 | } |
497 | } |
427 | } |
498 | } |
428 | |
499 | |
429 | if (top_si->si_type == PERLSI_MAIN) |
500 | if (expect_true (top_si->si_type == PERLSI_MAIN)) |
430 | break; |
501 | break; |
431 | |
502 | |
432 | top_si = top_si->si_prev; |
503 | top_si = top_si->si_prev; |
433 | ccstk = top_si->si_cxstack; |
504 | ccstk = top_si->si_cxstack; |
434 | cxix = top_si->si_cxix; |
505 | cxix = top_si->si_cxix; |
435 | } |
506 | } |
436 | |
507 | |
437 | PUTBACK; |
508 | PUTBACK; |
438 | } |
509 | } |
439 | |
510 | |
440 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
511 | /* allocate some space on the context stack for our purposes */ |
441 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
512 | { |
442 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
513 | /* we manually unroll here, as usually 2 slots is enough */ |
443 | c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; |
514 | int i; |
444 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
515 | if (SLOT_COUNT >= 1) CXINC; |
|
|
516 | if (SLOT_COUNT >= 2) CXINC; |
|
|
517 | if (SLOT_COUNT >= 3) CXINC; |
|
|
518 | for (i = 3; i < SLOT_COUNT; ++i) |
|
|
519 | CXINC; |
445 | |
520 | |
|
|
521 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
522 | } |
|
|
523 | |
|
|
524 | c->mainstack = PL_mainstack; |
|
|
525 | |
|
|
526 | { |
|
|
527 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
|
|
528 | |
|
|
529 | slot->defav = GvAV (PL_defgv); |
|
|
530 | slot->defsv = DEFSV; |
|
|
531 | slot->errsv = ERRSV; |
|
|
532 | slot->irsgv = GvSV (irsgv); |
|
|
533 | |
446 | #define VAR(name,type)c->name = PL_ ## name; |
534 | #define VAR(name,type) slot->name = PL_ ## name; |
447 | # include "state.h" |
535 | # include "state.h" |
448 | #undef VAR |
536 | #undef VAR |
|
|
537 | } |
449 | } |
538 | } |
450 | |
539 | |
451 | /* |
540 | /* |
452 | * allocate various perl stacks. This is an exact copy |
541 | * allocate various perl stacks. This is an exact copy |
453 | * of perl.c:init_stacks, except that it uses less memory |
542 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
458 | # define coro_init_stacks init_stacks |
547 | # define coro_init_stacks init_stacks |
459 | #else |
548 | #else |
460 | static void |
549 | static void |
461 | coro_init_stacks (pTHX) |
550 | coro_init_stacks (pTHX) |
462 | { |
551 | { |
463 | PL_curstackinfo = new_stackinfo(64, 6); |
552 | PL_curstackinfo = new_stackinfo(32, 8); |
464 | PL_curstackinfo->si_type = PERLSI_MAIN; |
553 | PL_curstackinfo->si_type = PERLSI_MAIN; |
465 | PL_curstack = PL_curstackinfo->si_stack; |
554 | PL_curstack = PL_curstackinfo->si_stack; |
466 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
555 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
467 | |
556 | |
468 | PL_stack_base = AvARRAY(PL_curstack); |
557 | PL_stack_base = AvARRAY(PL_curstack); |
469 | PL_stack_sp = PL_stack_base; |
558 | PL_stack_sp = PL_stack_base; |
470 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
559 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
471 | |
560 | |
472 | New(50,PL_tmps_stack,64,SV*); |
561 | New(50,PL_tmps_stack,32,SV*); |
473 | PL_tmps_floor = -1; |
562 | PL_tmps_floor = -1; |
474 | PL_tmps_ix = -1; |
563 | PL_tmps_ix = -1; |
475 | PL_tmps_max = 64; |
564 | PL_tmps_max = 32; |
476 | |
565 | |
477 | New(54,PL_markstack,16,I32); |
566 | New(54,PL_markstack,16,I32); |
478 | PL_markstack_ptr = PL_markstack; |
567 | PL_markstack_ptr = PL_markstack; |
479 | PL_markstack_max = PL_markstack + 16; |
568 | PL_markstack_max = PL_markstack + 16; |
480 | |
569 | |
481 | #ifdef SET_MARK_OFFSET |
570 | #ifdef SET_MARK_OFFSET |
482 | SET_MARK_OFFSET; |
571 | SET_MARK_OFFSET; |
483 | #endif |
572 | #endif |
484 | |
573 | |
485 | New(54,PL_scopestack,16,I32); |
574 | New(54,PL_scopestack,8,I32); |
486 | PL_scopestack_ix = 0; |
575 | PL_scopestack_ix = 0; |
487 | PL_scopestack_max = 16; |
576 | PL_scopestack_max = 8; |
488 | |
577 | |
489 | New(54,PL_savestack,64,ANY); |
578 | New(54,PL_savestack,24,ANY); |
490 | PL_savestack_ix = 0; |
579 | PL_savestack_ix = 0; |
491 | PL_savestack_max = 64; |
580 | PL_savestack_max = 24; |
492 | |
581 | |
493 | #if !PERL_VERSION_ATLEAST (5,9,0) |
582 | #if !PERL_VERSION_ATLEAST (5,9,0) |
494 | New(54,PL_retstack,4,OP*); |
583 | New(54,PL_retstack,4,OP*); |
495 | PL_retstack_ix = 0; |
584 | PL_retstack_ix = 0; |
496 | PL_retstack_max = 4; |
585 | PL_retstack_max = 4; |
… | |
… | |
502 | * destroy the stacks, the callchain etc... |
591 | * destroy the stacks, the callchain etc... |
503 | */ |
592 | */ |
504 | static void |
593 | static void |
505 | coro_destroy_stacks (pTHX) |
594 | coro_destroy_stacks (pTHX) |
506 | { |
595 | { |
507 | if (!IN_DESTRUCT) |
|
|
508 | { |
|
|
509 | /* restore all saved variables and stuff */ |
|
|
510 | LEAVE_SCOPE (0); |
|
|
511 | assert (PL_tmps_floor == -1); |
|
|
512 | |
|
|
513 | /* free all temporaries */ |
|
|
514 | FREETMPS; |
|
|
515 | assert (PL_tmps_ix == -1); |
|
|
516 | |
|
|
517 | /* unwind all extra stacks */ |
|
|
518 | POPSTACK_TO (PL_mainstack); |
|
|
519 | |
|
|
520 | /* unwind main stack */ |
|
|
521 | dounwind (-1); |
|
|
522 | } |
|
|
523 | |
|
|
524 | while (PL_curstackinfo->si_next) |
596 | while (PL_curstackinfo->si_next) |
525 | PL_curstackinfo = PL_curstackinfo->si_next; |
597 | PL_curstackinfo = PL_curstackinfo->si_next; |
526 | |
598 | |
527 | while (PL_curstackinfo) |
599 | while (PL_curstackinfo) |
528 | { |
600 | { |
… | |
… | |
544 | Safefree (PL_retstack); |
616 | Safefree (PL_retstack); |
545 | #endif |
617 | #endif |
546 | } |
618 | } |
547 | |
619 | |
548 | static size_t |
620 | static size_t |
549 | coro_rss (struct coro *coro) |
621 | coro_rss (pTHX_ struct coro *coro) |
550 | { |
622 | { |
551 | size_t rss = sizeof (coro); |
623 | size_t rss = sizeof (*coro); |
552 | |
624 | |
553 | if (coro->mainstack) |
625 | if (coro->mainstack) |
554 | { |
626 | { |
|
|
627 | perl_slots tmp_slot; |
|
|
628 | perl_slots *slot; |
|
|
629 | |
555 | if (coro->flags & CF_RUNNING) |
630 | if (coro->flags & CF_RUNNING) |
556 | { |
631 | { |
|
|
632 | slot = &tmp_slot; |
|
|
633 | |
557 | #define VAR(name,type)coro->name = PL_ ## name; |
634 | #define VAR(name,type) slot->name = PL_ ## name; |
558 | # include "state.h" |
635 | # include "state.h" |
559 | #undef VAR |
636 | #undef VAR |
560 | } |
637 | } |
|
|
638 | else |
|
|
639 | slot = coro->slot; |
561 | |
640 | |
562 | rss += sizeof (coro->curstackinfo); |
641 | rss += sizeof (slot->curstackinfo); |
563 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
564 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
642 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
565 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
643 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
566 | rss += coro->tmps_max * sizeof (SV *); |
644 | rss += slot->tmps_max * sizeof (SV *); |
567 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
645 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
568 | rss += coro->scopestack_max * sizeof (I32); |
646 | rss += slot->scopestack_max * sizeof (I32); |
569 | rss += coro->savestack_max * sizeof (ANY); |
647 | rss += slot->savestack_max * sizeof (ANY); |
570 | |
648 | |
571 | #if !PERL_VERSION_ATLEAST (5,9,0) |
649 | #if !PERL_VERSION_ATLEAST (5,9,0) |
572 | rss += coro->retstack_max * sizeof (OP *); |
650 | rss += slot->retstack_max * sizeof (OP *); |
573 | #endif |
651 | #endif |
574 | } |
652 | } |
575 | |
653 | |
576 | return rss; |
654 | return rss; |
577 | } |
655 | } |
578 | |
656 | |
579 | /** coroutine stack handling ************************************************/ |
657 | /** coroutine stack handling ************************************************/ |
580 | |
658 | |
581 | static void |
659 | static void |
582 | setup_coro (pTHX_ struct coro *coro) |
660 | coro_setup (pTHX_ struct coro *coro) |
583 | { |
661 | { |
584 | /* |
662 | /* |
585 | * emulate part of the perl startup here. |
663 | * emulate part of the perl startup here. |
586 | */ |
664 | */ |
587 | coro_init_stacks (aTHX); |
665 | coro_init_stacks (aTHX); |
588 | |
666 | |
|
|
667 | PL_runops = RUNOPS_DEFAULT; |
589 | PL_curcop = &PL_compiling; |
668 | PL_curcop = &PL_compiling; |
590 | PL_in_eval = EVAL_NULL; |
669 | PL_in_eval = EVAL_NULL; |
591 | PL_comppad = 0; |
670 | PL_comppad = 0; |
592 | PL_curpm = 0; |
671 | PL_curpm = 0; |
|
|
672 | PL_curpad = 0; |
593 | PL_localizing = 0; |
673 | PL_localizing = 0; |
594 | PL_dirty = 0; |
674 | PL_dirty = 0; |
595 | PL_restartop = 0; |
675 | PL_restartop = 0; |
|
|
676 | PL_diehook = SvREFCNT_inc (sv_diehook); |
|
|
677 | PL_warnhook = SvREFCNT_inc (sv_warnhook); |
|
|
678 | |
|
|
679 | GvSV (PL_defgv) = newSV (0); |
|
|
680 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
|
|
681 | GvSV (PL_errgv) = newSV (0); |
|
|
682 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
683 | PL_rs = newSVsv (GvSV (irsgv)); |
|
|
684 | PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); |
596 | |
685 | |
597 | { |
686 | { |
598 | dSP; |
687 | dSP; |
599 | LOGOP myop; |
688 | LOGOP myop; |
600 | |
689 | |
601 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
602 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
|
|
603 | |
|
|
604 | Zero (&myop, 1, LOGOP); |
690 | Zero (&myop, 1, LOGOP); |
605 | myop.op_next = Nullop; |
691 | myop.op_next = Nullop; |
606 | myop.op_flags = OPf_WANT_VOID; |
692 | myop.op_flags = OPf_WANT_VOID; |
607 | |
693 | |
608 | PUSHMARK (SP); |
694 | PUSHMARK (SP); |
609 | XPUSHs (av_shift (GvAV (PL_defgv))); |
695 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
610 | PUTBACK; |
696 | PUTBACK; |
611 | PL_op = (OP *)&myop; |
697 | PL_op = (OP *)&myop; |
612 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
698 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
613 | SPAGAIN; |
699 | SPAGAIN; |
614 | } |
700 | } |
615 | |
701 | |
616 | ENTER; /* necessary e.g. for dounwind */ |
702 | ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ |
|
|
703 | } |
|
|
704 | |
|
|
705 | static void |
|
|
706 | coro_destroy (pTHX_ struct coro *coro) |
|
|
707 | { |
|
|
708 | if (!IN_DESTRUCT) |
|
|
709 | { |
|
|
710 | /* restore all saved variables and stuff */ |
|
|
711 | LEAVE_SCOPE (0); |
|
|
712 | assert (PL_tmps_floor == -1); |
|
|
713 | |
|
|
714 | /* free all temporaries */ |
|
|
715 | FREETMPS; |
|
|
716 | assert (PL_tmps_ix == -1); |
|
|
717 | |
|
|
718 | /* unwind all extra stacks */ |
|
|
719 | POPSTACK_TO (PL_mainstack); |
|
|
720 | |
|
|
721 | /* unwind main stack */ |
|
|
722 | dounwind (-1); |
|
|
723 | } |
|
|
724 | |
|
|
725 | SvREFCNT_dec (GvSV (PL_defgv)); |
|
|
726 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
727 | SvREFCNT_dec (GvSV (PL_errgv)); |
|
|
728 | SvREFCNT_dec (PL_defoutgv); |
|
|
729 | SvREFCNT_dec (PL_rs); |
|
|
730 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
731 | |
|
|
732 | SvREFCNT_dec (PL_diehook); |
|
|
733 | SvREFCNT_dec (PL_warnhook); |
|
|
734 | |
|
|
735 | SvREFCNT_dec (coro->saved_deffh); |
|
|
736 | SvREFCNT_dec (coro->throw); |
|
|
737 | |
|
|
738 | coro_destroy_stacks (aTHX); |
617 | } |
739 | } |
618 | |
740 | |
619 | static void |
741 | static void |
620 | free_coro_mortal (pTHX) |
742 | free_coro_mortal (pTHX) |
621 | { |
743 | { |
622 | if (coro_mortal) |
744 | if (expect_true (coro_mortal)) |
623 | { |
745 | { |
624 | SvREFCNT_dec (coro_mortal); |
746 | SvREFCNT_dec (coro_mortal); |
625 | coro_mortal = 0; |
747 | coro_mortal = 0; |
626 | } |
748 | } |
|
|
749 | } |
|
|
750 | |
|
|
751 | static int |
|
|
752 | runops_trace (pTHX) |
|
|
753 | { |
|
|
754 | COP *oldcop = 0; |
|
|
755 | int oldcxix = -2; |
|
|
756 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
757 | coro_cctx *cctx = coro->cctx; |
|
|
758 | |
|
|
759 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
760 | { |
|
|
761 | PERL_ASYNC_CHECK (); |
|
|
762 | |
|
|
763 | if (cctx->flags & CC_TRACE_ALL) |
|
|
764 | { |
|
|
765 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
|
|
766 | { |
|
|
767 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
768 | SV **bot, **top; |
|
|
769 | AV *av = newAV (); /* return values */ |
|
|
770 | SV **cb; |
|
|
771 | dSP; |
|
|
772 | |
|
|
773 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
774 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
775 | if (isGV (gv)) |
|
|
776 | gv_efullname3 (fullname, gv, 0); |
|
|
777 | |
|
|
778 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
779 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
780 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
781 | : bot; |
|
|
782 | |
|
|
783 | av_extend (av, top - bot); |
|
|
784 | while (bot < top) |
|
|
785 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
786 | |
|
|
787 | PL_runops = RUNOPS_DEFAULT; |
|
|
788 | ENTER; |
|
|
789 | SAVETMPS; |
|
|
790 | EXTEND (SP, 3); |
|
|
791 | PUSHMARK (SP); |
|
|
792 | PUSHs (&PL_sv_no); |
|
|
793 | PUSHs (fullname); |
|
|
794 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
795 | PUTBACK; |
|
|
796 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
797 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
798 | SPAGAIN; |
|
|
799 | FREETMPS; |
|
|
800 | LEAVE; |
|
|
801 | PL_runops = runops_trace; |
|
|
802 | } |
|
|
803 | |
|
|
804 | if (oldcop != PL_curcop) |
|
|
805 | { |
|
|
806 | oldcop = PL_curcop; |
|
|
807 | |
|
|
808 | if (PL_curcop != &PL_compiling) |
|
|
809 | { |
|
|
810 | SV **cb; |
|
|
811 | |
|
|
812 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
|
|
813 | { |
|
|
814 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
815 | |
|
|
816 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
|
|
817 | { |
|
|
818 | runops_proc_t old_runops = PL_runops; |
|
|
819 | dSP; |
|
|
820 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
821 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
822 | |
|
|
823 | if (isGV (gv)) |
|
|
824 | gv_efullname3 (fullname, gv, 0); |
|
|
825 | |
|
|
826 | PL_runops = RUNOPS_DEFAULT; |
|
|
827 | ENTER; |
|
|
828 | SAVETMPS; |
|
|
829 | EXTEND (SP, 3); |
|
|
830 | PUSHMARK (SP); |
|
|
831 | PUSHs (&PL_sv_yes); |
|
|
832 | PUSHs (fullname); |
|
|
833 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
834 | PUTBACK; |
|
|
835 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
836 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
837 | SPAGAIN; |
|
|
838 | FREETMPS; |
|
|
839 | LEAVE; |
|
|
840 | PL_runops = runops_trace; |
|
|
841 | } |
|
|
842 | |
|
|
843 | oldcxix = cxstack_ix; |
|
|
844 | } |
|
|
845 | |
|
|
846 | if (cctx->flags & CC_TRACE_LINE) |
|
|
847 | { |
|
|
848 | dSP; |
|
|
849 | |
|
|
850 | PL_runops = RUNOPS_DEFAULT; |
|
|
851 | ENTER; |
|
|
852 | SAVETMPS; |
|
|
853 | EXTEND (SP, 3); |
|
|
854 | PL_runops = RUNOPS_DEFAULT; |
|
|
855 | PUSHMARK (SP); |
|
|
856 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
|
|
857 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
|
|
858 | PUTBACK; |
|
|
859 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
|
|
860 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
861 | SPAGAIN; |
|
|
862 | FREETMPS; |
|
|
863 | LEAVE; |
|
|
864 | PL_runops = runops_trace; |
|
|
865 | } |
|
|
866 | } |
|
|
867 | } |
|
|
868 | } |
|
|
869 | } |
|
|
870 | |
|
|
871 | TAINT_NOT; |
|
|
872 | return 0; |
627 | } |
873 | } |
628 | |
874 | |
629 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
875 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
630 | /* _cctx_init should be careful, as it could be called at almost any time */ |
876 | /* _cctx_init should be careful, as it could be called at almost any time */ |
631 | /* during execution of a perl program */ |
877 | /* during execution of a perl program */ |
632 | static void NOINLINE |
878 | static void NOINLINE |
633 | prepare_cctx (pTHX_ coro_cctx *cctx) |
879 | prepare_cctx (pTHX_ coro_cctx *cctx) |
634 | { |
880 | { |
635 | dSP; |
881 | dSP; |
636 | LOGOP myop; |
882 | LOGOP myop; |
|
|
883 | |
|
|
884 | PL_top_env = &PL_start_env; |
|
|
885 | |
|
|
886 | if (cctx->flags & CC_TRACE) |
|
|
887 | PL_runops = runops_trace; |
637 | |
888 | |
638 | Zero (&myop, 1, LOGOP); |
889 | Zero (&myop, 1, LOGOP); |
639 | myop.op_next = PL_op; |
890 | myop.op_next = PL_op; |
640 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
891 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
641 | |
892 | |
… | |
… | |
658 | dTHX; |
909 | dTHX; |
659 | |
910 | |
660 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
911 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
661 | UNLOCK; |
912 | UNLOCK; |
662 | |
913 | |
663 | PL_top_env = &PL_start_env; |
914 | /* we now skip the entersub that lead to transfer() */ |
|
|
915 | PL_op = PL_op->op_next; |
664 | |
916 | |
665 | /* inject a fake subroutine call to cctx_init */ |
917 | /* inject a fake subroutine call to cctx_init */ |
666 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
918 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
667 | |
919 | |
668 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
920 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
702 | # if CORO_STACKGUARD |
954 | # if CORO_STACKGUARD |
703 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
955 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
704 | # endif |
956 | # endif |
705 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
957 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
706 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
958 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
707 | cctx->mapped = 1; |
959 | cctx->flags |= CC_MAPPED; |
708 | } |
960 | } |
709 | else |
961 | else |
710 | #endif |
962 | #endif |
711 | { |
963 | { |
712 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
964 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
739 | #if CORO_USE_VALGRIND |
991 | #if CORO_USE_VALGRIND |
740 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
992 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
741 | #endif |
993 | #endif |
742 | |
994 | |
743 | #if HAVE_MMAP |
995 | #if HAVE_MMAP |
744 | if (cctx->mapped) |
996 | if (cctx->flags & CC_MAPPED) |
745 | munmap (cctx->sptr, cctx->ssize); |
997 | munmap (cctx->sptr, cctx->ssize); |
746 | else |
998 | else |
747 | #endif |
999 | #endif |
748 | Safefree (cctx->sptr); |
1000 | Safefree (cctx->sptr); |
749 | |
1001 | |
750 | Safefree (cctx); |
1002 | Safefree (cctx); |
751 | } |
1003 | } |
752 | |
1004 | |
|
|
1005 | /* wether this cctx should be destructed */ |
|
|
1006 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
|
|
1007 | |
753 | static coro_cctx * |
1008 | static coro_cctx * |
754 | cctx_get (pTHX) |
1009 | cctx_get (pTHX) |
755 | { |
1010 | { |
756 | while (cctx_first) |
1011 | while (expect_true (cctx_first)) |
757 | { |
1012 | { |
758 | coro_cctx *cctx = cctx_first; |
1013 | coro_cctx *cctx = cctx_first; |
759 | cctx_first = cctx->next; |
1014 | cctx_first = cctx->next; |
760 | --cctx_idle; |
1015 | --cctx_idle; |
761 | |
1016 | |
762 | if (cctx->ssize >= coro_stacksize) |
1017 | if (expect_true (!CCTX_EXPIRED (cctx))) |
763 | return cctx; |
1018 | return cctx; |
764 | |
1019 | |
765 | cctx_destroy (cctx); |
1020 | cctx_destroy (cctx); |
766 | } |
1021 | } |
767 | |
1022 | |
768 | PL_op = PL_op->op_next; |
|
|
769 | return cctx_new (); |
1023 | return cctx_new (); |
770 | } |
1024 | } |
771 | |
1025 | |
772 | static void |
1026 | static void |
773 | cctx_put (coro_cctx *cctx) |
1027 | cctx_put (coro_cctx *cctx) |
774 | { |
1028 | { |
775 | /* free another cctx if overlimit */ |
1029 | /* free another cctx if overlimit */ |
776 | if (cctx_idle >= MAX_IDLE_CCTX) |
1030 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
777 | { |
1031 | { |
778 | coro_cctx *first = cctx_first; |
1032 | coro_cctx *first = cctx_first; |
779 | cctx_first = first->next; |
1033 | cctx_first = first->next; |
780 | --cctx_idle; |
1034 | --cctx_idle; |
781 | |
1035 | |
782 | assert (!first->inuse); |
|
|
783 | cctx_destroy (first); |
1036 | cctx_destroy (first); |
784 | } |
1037 | } |
785 | |
1038 | |
786 | ++cctx_idle; |
1039 | ++cctx_idle; |
787 | cctx->next = cctx_first; |
1040 | cctx->next = cctx_first; |
788 | cctx_first = cctx; |
1041 | cctx_first = cctx; |
789 | } |
1042 | } |
790 | |
1043 | |
791 | /** coroutine switching *****************************************************/ |
1044 | /** coroutine switching *****************************************************/ |
792 | |
1045 | |
793 | /* never call directly, always through the coro_state_transfer global variable */ |
1046 | static void |
|
|
1047 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
|
|
1048 | { |
|
|
1049 | if (expect_true (prev != next)) |
|
|
1050 | { |
|
|
1051 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
|
|
1052 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
|
|
1053 | |
|
|
1054 | if (expect_false (next->flags & CF_RUNNING)) |
|
|
1055 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
1056 | |
|
|
1057 | if (expect_false (next->flags & CF_DESTROYED)) |
|
|
1058 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
1059 | |
|
|
1060 | if ( |
|
|
1061 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
1062 | expect_false (PL_parser) |
|
|
1063 | #else |
|
|
1064 | expect_false (PL_lex_state != LEX_NOTPARSING) |
|
|
1065 | #endif |
|
|
1066 | ) |
|
|
1067 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
|
|
1068 | } |
|
|
1069 | } |
|
|
1070 | |
|
|
1071 | /* always use the TRANSFER macro */ |
794 | static void NOINLINE |
1072 | static void NOINLINE |
795 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1073 | transfer (pTHX_ struct coro *prev, struct coro *next) |
796 | { |
1074 | { |
797 | dSTACKLEVEL; |
1075 | dSTACKLEVEL; |
798 | |
1076 | |
799 | /* sometimes transfer is only called to set idle_sp */ |
1077 | /* sometimes transfer is only called to set idle_sp */ |
800 | if (!next) |
1078 | if (expect_false (!next)) |
801 | { |
1079 | { |
802 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1080 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
803 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1081 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
804 | } |
1082 | } |
805 | else if (prev != next) |
1083 | else if (expect_true (prev != next)) |
806 | { |
1084 | { |
807 | coro_cctx *prev__cctx; |
1085 | coro_cctx *prev__cctx; |
808 | |
1086 | |
809 | if (prev->flags & CF_NEW) |
1087 | if (expect_false (prev->flags & CF_NEW)) |
810 | { |
1088 | { |
811 | /* create a new empty context */ |
1089 | /* create a new empty context */ |
812 | Newz (0, prev->cctx, 1, coro_cctx); |
1090 | Newz (0, prev->cctx, 1, coro_cctx); |
813 | prev->cctx->inuse = 1; |
|
|
814 | prev->flags &= ~CF_NEW; |
1091 | prev->flags &= ~CF_NEW; |
815 | prev->flags |= CF_RUNNING; |
1092 | prev->flags |= CF_RUNNING; |
816 | } |
1093 | } |
817 | |
1094 | |
818 | /*TODO: must not croak here */ |
|
|
819 | if (!prev->flags & CF_RUNNING) |
|
|
820 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
821 | |
|
|
822 | if (next->flags & CF_RUNNING) |
|
|
823 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
824 | |
|
|
825 | if (next->flags & CF_DESTROYED) |
|
|
826 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
827 | |
|
|
828 | prev->flags &= ~CF_RUNNING; |
1095 | prev->flags &= ~CF_RUNNING; |
829 | next->flags |= CF_RUNNING; |
1096 | next->flags |= CF_RUNNING; |
830 | |
1097 | |
831 | LOCK; |
1098 | LOCK; |
832 | |
1099 | |
833 | if (next->flags & CF_NEW) |
1100 | if (expect_false (next->flags & CF_NEW)) |
834 | { |
1101 | { |
835 | /* need to start coroutine */ |
1102 | /* need to start coroutine */ |
836 | next->flags &= ~CF_NEW; |
1103 | next->flags &= ~CF_NEW; |
837 | /* first get rid of the old state */ |
1104 | /* first get rid of the old state */ |
838 | save_perl (aTHX_ prev); |
1105 | save_perl (aTHX_ prev); |
839 | /* setup coroutine call */ |
1106 | /* setup coroutine call */ |
840 | setup_coro (aTHX_ next); |
1107 | coro_setup (aTHX_ next); |
841 | /* need a new stack */ |
|
|
842 | assert (!next->cctx); |
|
|
843 | } |
1108 | } |
844 | else |
1109 | else |
845 | { |
1110 | { |
846 | /* coroutine already started */ |
1111 | /* coroutine already started */ |
847 | save_perl (aTHX_ prev); |
1112 | save_perl (aTHX_ prev); |
… | |
… | |
849 | } |
1114 | } |
850 | |
1115 | |
851 | prev__cctx = prev->cctx; |
1116 | prev__cctx = prev->cctx; |
852 | |
1117 | |
853 | /* possibly "free" the cctx */ |
1118 | /* possibly "free" the cctx */ |
854 | if (prev__cctx->idle_sp == STACKLEVEL) |
1119 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
855 | { |
1120 | { |
856 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1121 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
857 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1122 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
858 | |
1123 | |
859 | prev->cctx = 0; |
1124 | prev->cctx = 0; |
860 | |
1125 | |
|
|
1126 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
|
|
1127 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
|
|
1128 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
|
|
1129 | next->cctx = cctx_get (aTHX); |
|
|
1130 | |
861 | cctx_put (prev__cctx); |
1131 | cctx_put (prev__cctx); |
862 | prev__cctx->inuse = 0; |
|
|
863 | } |
1132 | } |
864 | |
1133 | |
|
|
1134 | ++next->usecount; |
|
|
1135 | |
865 | if (!next->cctx) |
1136 | if (expect_true (!next->cctx)) |
866 | { |
|
|
867 | next->cctx = cctx_get (aTHX); |
1137 | next->cctx = cctx_get (aTHX); |
868 | assert (!next->cctx->inuse); |
|
|
869 | next->cctx->inuse = 1; |
|
|
870 | } |
|
|
871 | |
1138 | |
872 | if (prev__cctx != next->cctx) |
1139 | if (expect_false (prev__cctx != next->cctx)) |
873 | { |
1140 | { |
874 | prev__cctx->top_env = PL_top_env; |
1141 | prev__cctx->top_env = PL_top_env; |
875 | PL_top_env = next->cctx->top_env; |
1142 | PL_top_env = next->cctx->top_env; |
876 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1143 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
877 | } |
1144 | } |
878 | |
1145 | |
879 | free_coro_mortal (aTHX); |
1146 | free_coro_mortal (aTHX); |
880 | UNLOCK; |
1147 | UNLOCK; |
|
|
1148 | |
|
|
1149 | if (expect_false (prev->throw || next->throw)) |
|
|
1150 | { |
|
|
1151 | struct coro *coro = SvSTATE (coro_current); |
|
|
1152 | |
|
|
1153 | if (coro->throw) |
|
|
1154 | { |
|
|
1155 | SV *exception = coro->throw; |
|
|
1156 | coro->throw = 0; |
|
|
1157 | sv_setsv (ERRSV, exception); |
|
|
1158 | croak (0); |
|
|
1159 | } |
|
|
1160 | } |
881 | } |
1161 | } |
882 | } |
1162 | } |
883 | |
1163 | |
884 | struct transfer_args |
1164 | struct transfer_args |
885 | { |
1165 | { |
886 | struct coro *prev, *next; |
1166 | struct coro *prev, *next; |
887 | }; |
1167 | }; |
888 | |
1168 | |
889 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1169 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
|
|
1170 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
890 | |
1171 | |
891 | /** high level stuff ********************************************************/ |
1172 | /** high level stuff ********************************************************/ |
892 | |
1173 | |
893 | static int |
1174 | static int |
894 | coro_state_destroy (pTHX_ struct coro *coro) |
1175 | coro_state_destroy (pTHX_ struct coro *coro) |
… | |
… | |
911 | |
1192 | |
912 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1193 | if (coro->mainstack && coro->mainstack != main_mainstack) |
913 | { |
1194 | { |
914 | struct coro temp; |
1195 | struct coro temp; |
915 | |
1196 | |
916 | assert (!(coro->flags & CF_RUNNING)); |
|
|
917 | |
|
|
918 | Zero (&temp, 1, struct coro); |
|
|
919 | temp.save = CORO_SAVE_ALL; |
|
|
920 | |
|
|
921 | if (coro->flags & CF_RUNNING) |
1197 | if (coro->flags & CF_RUNNING) |
922 | croak ("FATAL: tried to destroy currently running coroutine"); |
1198 | croak ("FATAL: tried to destroy currently running coroutine"); |
923 | |
1199 | |
924 | save_perl (aTHX_ &temp); |
1200 | save_perl (aTHX_ &temp); |
925 | load_perl (aTHX_ coro); |
1201 | load_perl (aTHX_ coro); |
926 | |
1202 | |
927 | coro_destroy_stacks (aTHX); |
1203 | coro_destroy (aTHX_ coro); |
928 | |
1204 | |
929 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
1205 | load_perl (aTHX_ &temp); |
930 | |
1206 | |
931 | coro->mainstack = 0; |
1207 | coro->slot = 0; |
932 | } |
1208 | } |
933 | |
1209 | |
934 | cctx_destroy (coro->cctx); |
1210 | cctx_destroy (coro->cctx); |
935 | SvREFCNT_dec (coro->args); |
1211 | SvREFCNT_dec (coro->args); |
936 | |
1212 | |
… | |
… | |
977 | #else |
1253 | #else |
978 | # define MGf_DUP 0 |
1254 | # define MGf_DUP 0 |
979 | #endif |
1255 | #endif |
980 | }; |
1256 | }; |
981 | |
1257 | |
982 | static struct coro * |
|
|
983 | SvSTATE_ (pTHX_ SV *coro) |
|
|
984 | { |
|
|
985 | HV *stash; |
|
|
986 | MAGIC *mg; |
|
|
987 | |
|
|
988 | if (SvROK (coro)) |
|
|
989 | coro = SvRV (coro); |
|
|
990 | |
|
|
991 | stash = SvSTASH (coro); |
|
|
992 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
993 | { |
|
|
994 | /* very slow, but rare, check */ |
|
|
995 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
996 | croak ("Coro::State object required"); |
|
|
997 | } |
|
|
998 | |
|
|
999 | mg = CORO_MAGIC (coro); |
|
|
1000 | return (struct coro *)mg->mg_ptr; |
|
|
1001 | } |
|
|
1002 | |
|
|
1003 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
1004 | |
|
|
1005 | static void |
1258 | static void |
1006 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1259 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1007 | { |
1260 | { |
1008 | ta->prev = SvSTATE (prev_sv); |
1261 | ta->prev = SvSTATE (prev_sv); |
1009 | ta->next = SvSTATE (next_sv); |
1262 | ta->next = SvSTATE (next_sv); |
|
|
1263 | TRANSFER_CHECK (*ta); |
1010 | } |
1264 | } |
1011 | |
1265 | |
1012 | static void |
1266 | static void |
1013 | api_transfer (SV *prev_sv, SV *next_sv) |
1267 | api_transfer (SV *prev_sv, SV *next_sv) |
1014 | { |
1268 | { |
1015 | dTHX; |
1269 | dTHX; |
1016 | struct transfer_args ta; |
1270 | struct transfer_args ta; |
1017 | |
1271 | |
1018 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1272 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1019 | TRANSFER (ta); |
1273 | TRANSFER (ta); |
1020 | } |
|
|
1021 | |
|
|
1022 | static int |
|
|
1023 | api_save (SV *coro_sv, int new_save) |
|
|
1024 | { |
|
|
1025 | dTHX; |
|
|
1026 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1027 | int old_save = coro->save; |
|
|
1028 | |
|
|
1029 | if (new_save >= 0) |
|
|
1030 | coro->save = new_save; |
|
|
1031 | |
|
|
1032 | return old_save; |
|
|
1033 | } |
1274 | } |
1034 | |
1275 | |
1035 | /** Coro ********************************************************************/ |
1276 | /** Coro ********************************************************************/ |
1036 | |
1277 | |
1037 | static void |
1278 | static void |
… | |
… | |
1096 | { |
1337 | { |
1097 | LOCK; |
1338 | LOCK; |
1098 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1339 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1099 | |
1340 | |
1100 | /* nothing to schedule: call the idle handler */ |
1341 | /* nothing to schedule: call the idle handler */ |
1101 | if (!next_sv) |
1342 | if (expect_false (!next_sv)) |
1102 | { |
1343 | { |
1103 | dSP; |
1344 | dSP; |
1104 | UNLOCK; |
1345 | UNLOCK; |
1105 | |
1346 | |
1106 | ENTER; |
1347 | ENTER; |
… | |
… | |
1116 | } |
1357 | } |
1117 | |
1358 | |
1118 | ta->next = SvSTATE (next_sv); |
1359 | ta->next = SvSTATE (next_sv); |
1119 | |
1360 | |
1120 | /* cannot transfer to destroyed coros, skip and look for next */ |
1361 | /* cannot transfer to destroyed coros, skip and look for next */ |
1121 | if (ta->next->flags & CF_DESTROYED) |
1362 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1122 | { |
1363 | { |
1123 | UNLOCK; |
1364 | UNLOCK; |
1124 | SvREFCNT_dec (next_sv); |
1365 | SvREFCNT_dec (next_sv); |
1125 | /* coro_nready is already taken care of by destroy */ |
1366 | /* coro_nready is already taken care of by destroy */ |
1126 | continue; |
1367 | continue; |
… | |
… | |
1131 | break; |
1372 | break; |
1132 | } |
1373 | } |
1133 | |
1374 | |
1134 | /* free this only after the transfer */ |
1375 | /* free this only after the transfer */ |
1135 | prev_sv = SvRV (coro_current); |
1376 | prev_sv = SvRV (coro_current); |
1136 | SvRV_set (coro_current, next_sv); |
|
|
1137 | ta->prev = SvSTATE (prev_sv); |
1377 | ta->prev = SvSTATE (prev_sv); |
1138 | |
1378 | TRANSFER_CHECK (*ta); |
1139 | assert (ta->next->flags & CF_READY); |
1379 | assert (ta->next->flags & CF_READY); |
1140 | ta->next->flags &= ~CF_READY; |
1380 | ta->next->flags &= ~CF_READY; |
|
|
1381 | SvRV_set (coro_current, next_sv); |
1141 | |
1382 | |
1142 | LOCK; |
1383 | LOCK; |
1143 | free_coro_mortal (aTHX); |
1384 | free_coro_mortal (aTHX); |
1144 | coro_mortal = prev_sv; |
1385 | coro_mortal = prev_sv; |
1145 | UNLOCK; |
1386 | UNLOCK; |
… | |
… | |
1182 | dTHX; |
1423 | dTHX; |
1183 | struct transfer_args ta; |
1424 | struct transfer_args ta; |
1184 | |
1425 | |
1185 | prepare_cede (aTHX_ &ta); |
1426 | prepare_cede (aTHX_ &ta); |
1186 | |
1427 | |
1187 | if (ta.prev != ta.next) |
1428 | if (expect_true (ta.prev != ta.next)) |
1188 | { |
1429 | { |
1189 | TRANSFER (ta); |
1430 | TRANSFER (ta); |
1190 | return 1; |
1431 | return 1; |
1191 | } |
1432 | } |
1192 | else |
1433 | else |
… | |
… | |
1206 | } |
1447 | } |
1207 | else |
1448 | else |
1208 | return 0; |
1449 | return 0; |
1209 | } |
1450 | } |
1210 | |
1451 | |
|
|
1452 | static void |
|
|
1453 | api_trace (SV *coro_sv, int flags) |
|
|
1454 | { |
|
|
1455 | dTHX; |
|
|
1456 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1457 | |
|
|
1458 | if (flags & CC_TRACE) |
|
|
1459 | { |
|
|
1460 | if (!coro->cctx) |
|
|
1461 | coro->cctx = cctx_new (); |
|
|
1462 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1463 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1464 | |
|
|
1465 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
|
|
1466 | } |
|
|
1467 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1468 | { |
|
|
1469 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1470 | |
|
|
1471 | if (coro->flags & CF_RUNNING) |
|
|
1472 | PL_runops = RUNOPS_DEFAULT; |
|
|
1473 | else |
|
|
1474 | coro->slot->runops = RUNOPS_DEFAULT; |
|
|
1475 | } |
|
|
1476 | } |
|
|
1477 | |
1211 | MODULE = Coro::State PACKAGE = Coro::State |
1478 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1212 | |
1479 | |
1213 | PROTOTYPES: DISABLE |
1480 | PROTOTYPES: DISABLE |
1214 | |
1481 | |
1215 | BOOT: |
1482 | BOOT: |
1216 | { |
1483 | { |
1217 | #ifdef USE_ITHREADS |
1484 | #ifdef USE_ITHREADS |
1218 | MUTEX_INIT (&coro_mutex); |
1485 | MUTEX_INIT (&coro_mutex); |
1219 | #endif |
1486 | #endif |
1220 | BOOT_PAGESIZE; |
1487 | BOOT_PAGESIZE; |
1221 | |
1488 | |
|
|
1489 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
|
|
1490 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
|
|
1491 | |
|
|
1492 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
|
|
1493 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
|
|
1494 | |
|
|
1495 | if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook); |
|
|
1496 | if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook); |
|
|
1497 | |
1222 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1498 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1223 | |
1499 | |
1224 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1500 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1225 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1501 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1226 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1227 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1228 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
|
|
1229 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1502 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
1230 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1503 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
1231 | |
1504 | |
1232 | main_mainstack = PL_mainstack; |
1505 | main_mainstack = PL_mainstack; |
1233 | main_top_env = PL_top_env; |
1506 | main_top_env = PL_top_env; |
1234 | |
1507 | |
1235 | while (main_top_env->je_prev) |
1508 | while (main_top_env->je_prev) |
… | |
… | |
1248 | struct coro *coro; |
1521 | struct coro *coro; |
1249 | HV *hv; |
1522 | HV *hv; |
1250 | int i; |
1523 | int i; |
1251 | |
1524 | |
1252 | Newz (0, coro, 1, struct coro); |
1525 | Newz (0, coro, 1, struct coro); |
1253 | coro->args = newAV (); |
1526 | coro->args = newAV (); |
1254 | coro->save = CORO_SAVE_DEF; |
|
|
1255 | coro->flags = CF_NEW; |
1527 | coro->flags = CF_NEW; |
1256 | |
1528 | |
1257 | if (coro_first) coro_first->prev = coro; |
1529 | if (coro_first) coro_first->prev = coro; |
1258 | coro->next = coro_first; |
1530 | coro->next = coro_first; |
1259 | coro_first = coro; |
1531 | coro_first = coro; |
1260 | |
1532 | |
1261 | coro->hv = hv = newHV (); |
1533 | coro->hv = hv = newHV (); |
1262 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1534 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1263 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1535 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1264 | |
1536 | |
|
|
1537 | av_extend (coro->args, items - 1); |
1265 | for (i = 1; i < items; i++) |
1538 | for (i = 1; i < items; i++) |
1266 | av_push (coro->args, newSVsv (ST (i))); |
1539 | av_push (coro->args, newSVsv (ST (i))); |
1267 | } |
1540 | } |
1268 | OUTPUT: |
1541 | OUTPUT: |
1269 | RETVAL |
1542 | RETVAL |
1270 | |
1543 | |
1271 | int |
1544 | # these not obviously related functions are all rolled into the same xs |
1272 | save (SV *coro, int new_save = -1) |
1545 | # function to increase chances that they all will call transfer with the same |
1273 | CODE: |
1546 | # stack offset |
1274 | RETVAL = api_save (coro, new_save); |
|
|
1275 | OUTPUT: |
|
|
1276 | RETVAL |
|
|
1277 | |
|
|
1278 | int |
|
|
1279 | save_also (SV *coro_sv, int save_also) |
|
|
1280 | CODE: |
|
|
1281 | { |
|
|
1282 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1283 | RETVAL = coro->save; |
|
|
1284 | coro->save |= save_also; |
|
|
1285 | } |
|
|
1286 | OUTPUT: |
|
|
1287 | RETVAL |
|
|
1288 | |
|
|
1289 | void |
1547 | void |
1290 | _set_stacklevel (...) |
1548 | _set_stacklevel (...) |
1291 | ALIAS: |
1549 | ALIAS: |
1292 | Coro::State::transfer = 1 |
1550 | Coro::State::transfer = 1 |
1293 | Coro::schedule = 2 |
1551 | Coro::schedule = 2 |
… | |
… | |
1327 | } |
1585 | } |
1328 | |
1586 | |
1329 | BARRIER; |
1587 | BARRIER; |
1330 | TRANSFER (ta); |
1588 | TRANSFER (ta); |
1331 | |
1589 | |
1332 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1590 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1333 | XSRETURN_YES; |
1591 | XSRETURN_YES; |
1334 | } |
1592 | } |
1335 | |
1593 | |
1336 | bool |
1594 | bool |
1337 | _destroy (SV *coro_sv) |
1595 | _destroy (SV *coro_sv) |
… | |
… | |
1379 | if (coro->hv) |
1637 | if (coro->hv) |
1380 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1638 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1381 | } |
1639 | } |
1382 | |
1640 | |
1383 | void |
1641 | void |
1384 | _eval (Coro::State coro, SV *coderef) |
1642 | call (Coro::State coro, SV *coderef) |
|
|
1643 | ALIAS: |
|
|
1644 | eval = 1 |
1385 | CODE: |
1645 | CODE: |
1386 | { |
1646 | { |
1387 | if (coro->mainstack) |
1647 | if (coro->mainstack) |
1388 | { |
1648 | { |
1389 | struct coro temp; |
1649 | struct coro temp; |
1390 | Zero (&temp, 1, struct coro); |
|
|
1391 | temp.save = CORO_SAVE_ALL; |
|
|
1392 | |
1650 | |
1393 | if (!(coro->flags & CF_RUNNING)) |
1651 | if (!(coro->flags & CF_RUNNING)) |
1394 | { |
1652 | { |
1395 | save_perl (aTHX_ &temp); |
1653 | save_perl (aTHX_ &temp); |
1396 | load_perl (aTHX_ coro); |
1654 | load_perl (aTHX_ coro); |
… | |
… | |
1400 | dSP; |
1658 | dSP; |
1401 | ENTER; |
1659 | ENTER; |
1402 | SAVETMPS; |
1660 | SAVETMPS; |
1403 | PUSHMARK (SP); |
1661 | PUSHMARK (SP); |
1404 | PUTBACK; |
1662 | PUTBACK; |
|
|
1663 | |
|
|
1664 | if (ix) |
|
|
1665 | eval_sv (coderef, 0); |
|
|
1666 | else |
1405 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1667 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1668 | |
1406 | SPAGAIN; |
1669 | SPAGAIN; |
1407 | FREETMPS; |
1670 | FREETMPS; |
1408 | LEAVE; |
1671 | LEAVE; |
1409 | PUTBACK; |
1672 | PUTBACK; |
1410 | } |
1673 | } |
… | |
… | |
1414 | save_perl (aTHX_ coro); |
1677 | save_perl (aTHX_ coro); |
1415 | load_perl (aTHX_ &temp); |
1678 | load_perl (aTHX_ &temp); |
1416 | } |
1679 | } |
1417 | } |
1680 | } |
1418 | } |
1681 | } |
1419 | |
1682 | |
1420 | SV * |
1683 | SV * |
1421 | is_ready (Coro::State coro) |
1684 | is_ready (Coro::State coro) |
1422 | PROTOTYPE: $ |
1685 | PROTOTYPE: $ |
1423 | ALIAS: |
1686 | ALIAS: |
1424 | is_ready = CF_READY |
1687 | is_ready = CF_READY |
… | |
… | |
1428 | CODE: |
1691 | CODE: |
1429 | RETVAL = boolSV (coro->flags & ix); |
1692 | RETVAL = boolSV (coro->flags & ix); |
1430 | OUTPUT: |
1693 | OUTPUT: |
1431 | RETVAL |
1694 | RETVAL |
1432 | |
1695 | |
|
|
1696 | void |
|
|
1697 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
1698 | |
|
|
1699 | SV * |
|
|
1700 | has_stack (Coro::State coro) |
|
|
1701 | PROTOTYPE: $ |
|
|
1702 | CODE: |
|
|
1703 | RETVAL = boolSV (!!coro->cctx); |
|
|
1704 | OUTPUT: |
|
|
1705 | RETVAL |
|
|
1706 | |
|
|
1707 | int |
|
|
1708 | is_traced (Coro::State coro) |
|
|
1709 | PROTOTYPE: $ |
|
|
1710 | CODE: |
|
|
1711 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
|
|
1712 | OUTPUT: |
|
|
1713 | RETVAL |
|
|
1714 | |
1433 | IV |
1715 | IV |
1434 | rss (Coro::State coro) |
1716 | rss (Coro::State coro) |
1435 | PROTOTYPE: $ |
1717 | PROTOTYPE: $ |
|
|
1718 | ALIAS: |
|
|
1719 | usecount = 1 |
1436 | CODE: |
1720 | CODE: |
1437 | RETVAL = coro_rss (coro); |
1721 | switch (ix) |
|
|
1722 | { |
|
|
1723 | case 0: RETVAL = coro_rss (aTHX_ coro); break; |
|
|
1724 | case 1: RETVAL = coro->usecount; break; |
|
|
1725 | } |
1438 | OUTPUT: |
1726 | OUTPUT: |
1439 | RETVAL |
1727 | RETVAL |
1440 | |
1728 | |
1441 | |
1729 | |
1442 | MODULE = Coro::State PACKAGE = Coro |
1730 | MODULE = Coro::State PACKAGE = Coro |
1443 | |
1731 | |
1444 | BOOT: |
1732 | BOOT: |
1445 | { |
1733 | { |
1446 | int i; |
1734 | int i; |
1447 | |
1735 | |
1448 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
1736 | sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); |
1449 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
1737 | sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); |
1450 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
1738 | av_async_pool = coro_get_av ("Coro::async_pool", TRUE); |
1451 | |
1739 | |
1452 | coro_current = get_sv ("Coro::current", FALSE); |
1740 | coro_current = coro_get_sv ("Coro::current", FALSE); |
1453 | SvREADONLY_on (coro_current); |
1741 | SvREADONLY_on (coro_current); |
1454 | |
1742 | |
1455 | coro_stash = gv_stashpv ("Coro", TRUE); |
1743 | coro_stash = gv_stashpv ("Coro", TRUE); |
1456 | |
1744 | |
1457 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1745 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1458 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1746 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1459 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1747 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1460 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1748 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
… | |
… | |
1463 | |
1751 | |
1464 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1752 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1465 | coro_ready[i] = newAV (); |
1753 | coro_ready[i] = newAV (); |
1466 | |
1754 | |
1467 | { |
1755 | { |
1468 | SV *sv = perl_get_sv("Coro::API", 1); |
1756 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
|
|
1757 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
1469 | |
1758 | |
1470 | coroapi.schedule = api_schedule; |
1759 | coroapi.schedule = api_schedule; |
1471 | coroapi.save = api_save; |
|
|
1472 | coroapi.cede = api_cede; |
1760 | coroapi.cede = api_cede; |
1473 | coroapi.cede_notself = api_cede_notself; |
1761 | coroapi.cede_notself = api_cede_notself; |
1474 | coroapi.ready = api_ready; |
1762 | coroapi.ready = api_ready; |
1475 | coroapi.is_ready = api_is_ready; |
1763 | coroapi.is_ready = api_is_ready; |
1476 | coroapi.nready = &coro_nready; |
1764 | coroapi.nready = &coro_nready; |
… | |
… | |
1525 | CODE: |
1813 | CODE: |
1526 | RETVAL = coro_nready; |
1814 | RETVAL = coro_nready; |
1527 | OUTPUT: |
1815 | OUTPUT: |
1528 | RETVAL |
1816 | RETVAL |
1529 | |
1817 | |
|
|
1818 | void |
|
|
1819 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
1820 | PROTOTYPE: $;$ |
|
|
1821 | CODE: |
|
|
1822 | SvREFCNT_dec (self->throw); |
|
|
1823 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
1824 | |
1530 | # for async_pool speedup |
1825 | # for async_pool speedup |
1531 | SV * |
1826 | void |
1532 | _pool_1 (...) |
1827 | _pool_1 (SV *cb) |
1533 | CODE: |
1828 | CODE: |
1534 | { |
1829 | { |
1535 | int i, len; |
1830 | struct coro *coro = SvSTATE (coro_current); |
1536 | HV *hv = (HV *)SvRV (coro_current); |
1831 | HV *hv = (HV *)SvRV (coro_current); |
1537 | AV *defav = GvAV (PL_defgv); |
1832 | AV *defav = GvAV (PL_defgv); |
1538 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1833 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1539 | AV *invoke_av; |
1834 | AV *invoke_av; |
|
|
1835 | int i, len; |
1540 | |
1836 | |
1541 | if (!invoke) |
1837 | if (!invoke) |
1542 | XSRETURN_EMPTY; |
1838 | croak ("\3async_pool terminate\2\n"); |
|
|
1839 | |
|
|
1840 | SvREFCNT_dec (coro->saved_deffh); |
|
|
1841 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1543 | |
1842 | |
1544 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1843 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1545 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1844 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1546 | |
1845 | |
1547 | invoke_av = (AV *)SvRV (invoke); |
1846 | invoke_av = (AV *)SvRV (invoke); |
1548 | len = av_len (invoke_av); |
1847 | len = av_len (invoke_av); |
1549 | |
1848 | |
1550 | RETVAL = SvREFCNT_inc (AvARRAY (invoke_av)[0]); |
1849 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
1551 | |
1850 | |
1552 | if (len > 0) |
1851 | if (len > 0) |
1553 | { |
1852 | { |
1554 | av_clear (defav); |
|
|
1555 | av_extend (defav, len); |
1853 | av_fill (defav, len - 1); |
1556 | for (i = 0; i < len; ++i) |
1854 | for (i = 0; i < len; ++i) |
1557 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
1855 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
1558 | } |
1856 | } |
1559 | |
1857 | |
1560 | SvREFCNT_dec (invoke); |
1858 | SvREFCNT_dec (invoke); |
1561 | } |
1859 | } |
1562 | OUTPUT: |
|
|
1563 | RETVAL |
|
|
1564 | |
1860 | |
1565 | void |
1861 | void |
1566 | _pool_2 (...) |
1862 | _pool_2 (SV *cb) |
1567 | CODE: |
1863 | CODE: |
1568 | { |
1864 | { |
1569 | struct coro *coro = SvSTATE (coro_current); |
1865 | struct coro *coro = SvSTATE (coro_current); |
1570 | |
1866 | |
|
|
1867 | sv_setsv (cb, &PL_sv_undef); |
|
|
1868 | |
|
|
1869 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1870 | coro->saved_deffh = 0; |
|
|
1871 | |
1571 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
1872 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1572 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1873 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1573 | XSRETURN_YES; |
1874 | croak ("\3async_pool terminate\2\n"); |
1574 | |
1875 | |
1575 | av_clear (GvAV (PL_defgv)); |
1876 | av_clear (GvAV (PL_defgv)); |
1576 | hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1877 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1577 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1878 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1578 | coro->save = CORO_SAVE_DEF; |
1879 | |
1579 | coro->prio = 0; |
1880 | coro->prio = 0; |
|
|
1881 | |
|
|
1882 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1883 | api_trace (coro_current, 0); |
|
|
1884 | |
1580 | av_push (av_async_pool, newSVsv (coro_current)); |
1885 | av_push (av_async_pool, newSVsv (coro_current)); |
1581 | |
|
|
1582 | XSRETURN_NO; |
|
|
1583 | } |
1886 | } |
1584 | |
1887 | |
1585 | |
1888 | |
1586 | MODULE = Coro::State PACKAGE = Coro::AIO |
1889 | MODULE = Coro::State PACKAGE = Coro::AIO |
1587 | |
1890 | |