… | |
… | |
10 | #include "patchlevel.h" |
10 | #include "patchlevel.h" |
11 | |
11 | |
12 | #include <stdio.h> |
12 | #include <stdio.h> |
13 | #include <errno.h> |
13 | #include <errno.h> |
14 | #include <assert.h> |
14 | #include <assert.h> |
|
|
15 | |
|
|
16 | #ifdef WIN32 |
|
|
17 | # undef setjmp |
|
|
18 | # undef longjmp |
|
|
19 | # undef _exit |
|
|
20 | # define setjmp _setjmp // deep magic, don't ask |
|
|
21 | #else |
|
|
22 | # include <inttypes.h> /* most portable stdint.h */ |
|
|
23 | #endif |
15 | |
24 | |
16 | #ifdef HAVE_MMAP |
25 | #ifdef HAVE_MMAP |
17 | # include <unistd.h> |
26 | # include <unistd.h> |
18 | # include <sys/mman.h> |
27 | # include <sys/mman.h> |
19 | # ifndef MAP_ANONYMOUS |
28 | # ifndef MAP_ANONYMOUS |
… | |
… | |
71 | # ifndef IS_PADCONST |
80 | # ifndef IS_PADCONST |
72 | # define IS_PADCONST(v) 0 |
81 | # define IS_PADCONST(v) 0 |
73 | # endif |
82 | # endif |
74 | #endif |
83 | #endif |
75 | |
84 | |
|
|
85 | /* 5.8.8 */ |
|
|
86 | #ifndef GV_NOTQUAL |
|
|
87 | # define GV_NOTQUAL 0 |
|
|
88 | #endif |
|
|
89 | #ifndef newSV |
|
|
90 | # define newSV(l) NEWSV(0,l) |
|
|
91 | #endif |
|
|
92 | |
|
|
93 | /* 5.11 */ |
|
|
94 | #ifndef CxHASARGS |
|
|
95 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
96 | #endif |
|
|
97 | |
76 | /* 5.8.7 */ |
98 | /* 5.8.7 */ |
77 | #ifndef SvRV_set |
99 | #ifndef SvRV_set |
78 | # define SvRV_set(s,v) SvRV(s) = (v) |
100 | # define SvRV_set(s,v) SvRV(s) = (v) |
79 | #endif |
|
|
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 |
101 | #endif |
88 | |
102 | |
89 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
103 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
90 | # undef CORO_STACKGUARD |
104 | # undef CORO_STACKGUARD |
91 | #endif |
105 | #endif |
… | |
… | |
130 | #else |
144 | #else |
131 | # define LOCK (void)0 |
145 | # define LOCK (void)0 |
132 | # define UNLOCK (void)0 |
146 | # define UNLOCK (void)0 |
133 | #endif |
147 | #endif |
134 | |
148 | |
135 | #define strpair(const) const, sizeof (const) - 1 |
|
|
136 | |
|
|
137 | /* helper storage struct for Coro::AIO */ |
149 | /* helper storage struct for Coro::AIO */ |
138 | struct io_state |
150 | struct io_state |
139 | { |
151 | { |
140 | int errorno; |
152 | int errorno; |
141 | I32 laststype; |
153 | I32 laststype; |
… | |
… | |
146 | static size_t coro_stacksize = CORO_STACKSIZE; |
158 | static size_t coro_stacksize = CORO_STACKSIZE; |
147 | static struct CoroAPI coroapi; |
159 | static struct CoroAPI coroapi; |
148 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
160 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
149 | static JMPENV *main_top_env; |
161 | static JMPENV *main_top_env; |
150 | static HV *coro_state_stash, *coro_stash; |
162 | static HV *coro_state_stash, *coro_stash; |
151 | static SV *coro_mortal; /* will be freed after next transfer */ |
163 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
152 | |
164 | |
153 | static GV *irsgv; /* $/ */ |
165 | static GV *irsgv; /* $/ */ |
154 | static GV *stdoutgv; /* *STDOUT */ |
166 | static GV *stdoutgv; /* *STDOUT */ |
155 | |
167 | static SV *rv_diehook; |
|
|
168 | static SV *rv_warnhook; |
156 | static HV *hv_sig; /* %SIG */ |
169 | static HV *hv_sig; /* %SIG */ |
157 | static SV *sv_diehook; |
|
|
158 | static SV *sv_warnhook; |
|
|
159 | |
170 | |
160 | /* async_pool helper stuff */ |
171 | /* async_pool helper stuff */ |
161 | static SV *sv_pool_rss; |
172 | static SV *sv_pool_rss; |
162 | static SV *sv_pool_size; |
173 | static SV *sv_pool_size; |
163 | static AV *av_async_pool; |
174 | static AV *av_async_pool; |
… | |
… | |
261 | static struct coro *coro_first; |
272 | static struct coro *coro_first; |
262 | |
273 | |
263 | /** lowlevel stuff **********************************************************/ |
274 | /** lowlevel stuff **********************************************************/ |
264 | |
275 | |
265 | static SV * |
276 | static SV * |
266 | coro_get_sv (const char *name, int create) |
277 | coro_get_sv (pTHX_ const char *name, int create) |
267 | { |
278 | { |
268 | #if PERL_VERSION_ATLEAST (5,9,0) |
279 | #if PERL_VERSION_ATLEAST (5,10,0) |
269 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
280 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
270 | get_sv (name, create); |
281 | get_sv (name, create); |
271 | #endif |
282 | #endif |
272 | return get_sv (name, create); |
283 | return get_sv (name, create); |
273 | } |
284 | } |
274 | |
285 | |
275 | static AV * |
286 | static AV * |
276 | coro_get_av (const char *name, int create) |
287 | coro_get_av (pTHX_ const char *name, int create) |
277 | { |
288 | { |
278 | #if PERL_VERSION_ATLEAST (5,9,0) |
289 | #if PERL_VERSION_ATLEAST (5,10,0) |
279 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
290 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
280 | get_av (name, create); |
291 | get_av (name, create); |
281 | #endif |
292 | #endif |
282 | return get_av (name, create); |
293 | return get_av (name, create); |
283 | } |
294 | } |
284 | |
295 | |
285 | static HV * |
296 | static HV * |
286 | coro_get_hv (const char *name, int create) |
297 | coro_get_hv (pTHX_ const char *name, int create) |
287 | { |
298 | { |
288 | #if PERL_VERSION_ATLEAST (5,9,0) |
299 | #if PERL_VERSION_ATLEAST (5,10,0) |
289 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
300 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
290 | get_hv (name, create); |
301 | get_hv (name, create); |
291 | #endif |
302 | #endif |
292 | return get_hv (name, create); |
303 | return get_hv (name, create); |
293 | } |
304 | } |
… | |
… | |
298 | AV *padlist = CvPADLIST (cv); |
309 | AV *padlist = CvPADLIST (cv); |
299 | AV *newpadlist, *newpad; |
310 | AV *newpadlist, *newpad; |
300 | |
311 | |
301 | newpadlist = newAV (); |
312 | newpadlist = newAV (); |
302 | AvREAL_off (newpadlist); |
313 | AvREAL_off (newpadlist); |
303 | #if PERL_VERSION_ATLEAST (5,9,0) |
314 | #if PERL_VERSION_ATLEAST (5,10,0) |
304 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
315 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
305 | #else |
316 | #else |
306 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
317 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
307 | #endif |
318 | #endif |
308 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
319 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
… | |
… | |
346 | |
357 | |
347 | /* casting is fun. */ |
358 | /* casting is fun. */ |
348 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
359 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
349 | free_padlist (aTHX_ padlist); |
360 | free_padlist (aTHX_ padlist); |
350 | |
361 | |
351 | SvREFCNT_dec (av); |
|
|
352 | |
|
|
353 | return 0; |
362 | return 0; |
354 | } |
363 | } |
355 | |
364 | |
356 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
365 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
|
|
366 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
357 | |
367 | |
358 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
368 | static MGVTBL coro_cv_vtbl = { |
|
|
369 | 0, 0, 0, 0, |
|
|
370 | coro_cv_free |
|
|
371 | }; |
359 | |
372 | |
360 | #define CORO_MAGIC(cv) \ |
373 | #define CORO_MAGIC(sv,type) \ |
361 | SvMAGIC (cv) \ |
374 | SvMAGIC (sv) \ |
362 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
375 | ? SvMAGIC (sv)->mg_type == type \ |
363 | ? SvMAGIC (cv) \ |
376 | ? SvMAGIC (sv) \ |
364 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
377 | : mg_find (sv, type) \ |
365 | : 0 |
378 | : 0 |
|
|
379 | |
|
|
380 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
381 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
366 | |
382 | |
367 | static struct coro * |
383 | static struct coro * |
368 | SvSTATE_ (pTHX_ SV *coro) |
384 | SvSTATE_ (pTHX_ SV *coro) |
369 | { |
385 | { |
370 | HV *stash; |
386 | HV *stash; |
… | |
… | |
382 | /* very slow, but rare, check */ |
398 | /* very slow, but rare, check */ |
383 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
399 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
384 | croak ("Coro::State object required"); |
400 | croak ("Coro::State object required"); |
385 | } |
401 | } |
386 | |
402 | |
387 | mg = CORO_MAGIC (coro); |
403 | mg = CORO_MAGIC_state (coro); |
388 | return (struct coro *)mg->mg_ptr; |
404 | return (struct coro *)mg->mg_ptr; |
389 | } |
405 | } |
390 | |
406 | |
391 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
407 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
392 | |
408 | |
393 | /* the next two functions merely cache the padlists */ |
409 | /* the next two functions merely cache the padlists */ |
394 | static void |
410 | static void |
395 | get_padlist (pTHX_ CV *cv) |
411 | get_padlist (pTHX_ CV *cv) |
396 | { |
412 | { |
397 | MAGIC *mg = CORO_MAGIC (cv); |
413 | MAGIC *mg = CORO_MAGIC_cv (cv); |
398 | AV *av; |
414 | AV *av; |
399 | |
415 | |
400 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
416 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
401 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
417 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
402 | else |
418 | else |
… | |
… | |
414 | } |
430 | } |
415 | |
431 | |
416 | static void |
432 | static void |
417 | put_padlist (pTHX_ CV *cv) |
433 | put_padlist (pTHX_ CV *cv) |
418 | { |
434 | { |
419 | MAGIC *mg = CORO_MAGIC (cv); |
435 | MAGIC *mg = CORO_MAGIC_cv (cv); |
420 | AV *av; |
436 | AV *av; |
421 | |
437 | |
422 | if (expect_false (!mg)) |
438 | if (expect_false (!mg)) |
423 | { |
439 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
424 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
425 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
426 | mg->mg_virtual = &vtbl_coro; |
|
|
427 | mg->mg_obj = (SV *)newAV (); |
|
|
428 | } |
|
|
429 | |
440 | |
430 | av = (AV *)mg->mg_obj; |
441 | av = (AV *)mg->mg_obj; |
431 | |
442 | |
432 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
443 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
433 | av_extend (av, AvMAX (av) + 1); |
444 | av_extend (av, AvMAX (av) + 1); |
… | |
… | |
451 | GvSV (irsgv) = slot->irsgv; |
462 | GvSV (irsgv) = slot->irsgv; |
452 | |
463 | |
453 | #define VAR(name,type) PL_ ## name = slot->name; |
464 | #define VAR(name,type) PL_ ## name = slot->name; |
454 | # include "state.h" |
465 | # include "state.h" |
455 | #undef VAR |
466 | #undef VAR |
456 | |
|
|
457 | /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/ |
|
|
458 | /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/ |
|
|
459 | |
467 | |
460 | { |
468 | { |
461 | dSP; |
469 | dSP; |
462 | |
470 | |
463 | CV *cv; |
471 | CV *cv; |
… | |
… | |
592 | |
600 | |
593 | New(54,PL_savestack,24,ANY); |
601 | New(54,PL_savestack,24,ANY); |
594 | PL_savestack_ix = 0; |
602 | PL_savestack_ix = 0; |
595 | PL_savestack_max = 24; |
603 | PL_savestack_max = 24; |
596 | |
604 | |
597 | #if !PERL_VERSION_ATLEAST (5,9,0) |
605 | #if !PERL_VERSION_ATLEAST (5,10,0) |
598 | New(54,PL_retstack,4,OP*); |
606 | New(54,PL_retstack,4,OP*); |
599 | PL_retstack_ix = 0; |
607 | PL_retstack_ix = 0; |
600 | PL_retstack_max = 4; |
608 | PL_retstack_max = 4; |
601 | #endif |
609 | #endif |
602 | } |
610 | } |
… | |
… | |
625 | |
633 | |
626 | Safefree (PL_tmps_stack); |
634 | Safefree (PL_tmps_stack); |
627 | Safefree (PL_markstack); |
635 | Safefree (PL_markstack); |
628 | Safefree (PL_scopestack); |
636 | Safefree (PL_scopestack); |
629 | Safefree (PL_savestack); |
637 | Safefree (PL_savestack); |
630 | #if !PERL_VERSION_ATLEAST (5,9,0) |
638 | #if !PERL_VERSION_ATLEAST (5,10,0) |
631 | Safefree (PL_retstack); |
639 | Safefree (PL_retstack); |
632 | #endif |
640 | #endif |
633 | } |
641 | } |
634 | |
642 | |
635 | static size_t |
643 | static size_t |
… | |
… | |
659 | rss += slot->tmps_max * sizeof (SV *); |
667 | rss += slot->tmps_max * sizeof (SV *); |
660 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
668 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
661 | rss += slot->scopestack_max * sizeof (I32); |
669 | rss += slot->scopestack_max * sizeof (I32); |
662 | rss += slot->savestack_max * sizeof (ANY); |
670 | rss += slot->savestack_max * sizeof (ANY); |
663 | |
671 | |
664 | #if !PERL_VERSION_ATLEAST (5,9,0) |
672 | #if !PERL_VERSION_ATLEAST (5,10,0) |
665 | rss += slot->retstack_max * sizeof (OP *); |
673 | rss += slot->retstack_max * sizeof (OP *); |
666 | #endif |
674 | #endif |
667 | } |
675 | } |
668 | |
676 | |
669 | return rss; |
677 | return rss; |
670 | } |
678 | } |
671 | |
679 | |
672 | /** coroutine stack handling ************************************************/ |
680 | /** coroutine stack handling ************************************************/ |
|
|
681 | |
|
|
682 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
683 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
684 | |
|
|
685 | /* |
|
|
686 | * This overrides the default magic get method of %SIG elements. |
|
|
687 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
|
|
688 | * and instead of tryign to save and restore the hash elements, we just provide |
|
|
689 | * readback here. |
|
|
690 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
691 | * not expecting this to actually change the hook. This is a potential problem |
|
|
692 | * when a schedule happens then, but we ignore this. |
|
|
693 | */ |
|
|
694 | static int |
|
|
695 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
|
|
696 | { |
|
|
697 | const char *s = MgPV_nolen_const (mg); |
|
|
698 | |
|
|
699 | if (*s == '_') |
|
|
700 | { |
|
|
701 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
|
|
702 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
|
|
703 | } |
|
|
704 | |
|
|
705 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
|
|
706 | } |
|
|
707 | |
|
|
708 | static int |
|
|
709 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
|
|
710 | { |
|
|
711 | const char *s = MgPV_nolen_const (mg); |
|
|
712 | |
|
|
713 | if (*s == '_') |
|
|
714 | { |
|
|
715 | SV **svp = 0; |
|
|
716 | |
|
|
717 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
718 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
719 | |
|
|
720 | if (svp) |
|
|
721 | { |
|
|
722 | SV *old = *svp; |
|
|
723 | *svp = newSVsv (sv); |
|
|
724 | SvREFCNT_dec (old); |
|
|
725 | return 0; |
|
|
726 | } |
|
|
727 | } |
|
|
728 | |
|
|
729 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
|
|
730 | } |
673 | |
731 | |
674 | static void |
732 | static void |
675 | coro_setup (pTHX_ struct coro *coro) |
733 | coro_setup (pTHX_ struct coro *coro) |
676 | { |
734 | { |
677 | /* |
735 | /* |
… | |
… | |
686 | PL_curpm = 0; |
744 | PL_curpm = 0; |
687 | PL_curpad = 0; |
745 | PL_curpad = 0; |
688 | PL_localizing = 0; |
746 | PL_localizing = 0; |
689 | PL_dirty = 0; |
747 | PL_dirty = 0; |
690 | PL_restartop = 0; |
748 | PL_restartop = 0; |
691 | PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
749 | #if PERL_VERSION_ATLEAST (5,10,0) |
692 | PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
750 | PL_parser = 0; |
|
|
751 | #endif |
|
|
752 | |
|
|
753 | /* recreate the die/warn hooks */ |
|
|
754 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
|
|
755 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
693 | |
756 | |
694 | GvSV (PL_defgv) = newSV (0); |
757 | GvSV (PL_defgv) = newSV (0); |
695 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
758 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
696 | GvSV (PL_errgv) = newSV (0); |
759 | GvSV (PL_errgv) = newSV (0); |
697 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
760 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
712 | PL_op = (OP *)&myop; |
775 | PL_op = (OP *)&myop; |
713 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
776 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
714 | SPAGAIN; |
777 | SPAGAIN; |
715 | } |
778 | } |
716 | |
779 | |
717 | ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ |
780 | /* this newly created coroutine might be run on an existing cctx which most |
|
|
781 | * likely was suspended in set_stacklevel, called from entersub. |
|
|
782 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
|
|
783 | * so we ENTER here for symmetry |
|
|
784 | */ |
|
|
785 | ENTER; |
718 | } |
786 | } |
719 | |
787 | |
720 | static void |
788 | static void |
721 | coro_destroy (pTHX_ struct coro *coro) |
789 | coro_destroy (pTHX_ struct coro *coro) |
722 | { |
790 | { |
… | |
… | |
806 | PUSHMARK (SP); |
874 | PUSHMARK (SP); |
807 | PUSHs (&PL_sv_no); |
875 | PUSHs (&PL_sv_no); |
808 | PUSHs (fullname); |
876 | PUSHs (fullname); |
809 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
877 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
810 | PUTBACK; |
878 | PUTBACK; |
811 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
879 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
812 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
880 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
813 | SPAGAIN; |
881 | SPAGAIN; |
814 | FREETMPS; |
882 | FREETMPS; |
815 | LEAVE; |
883 | LEAVE; |
816 | PL_runops = runops_trace; |
884 | PL_runops = runops_trace; |
… | |
… | |
843 | SAVETMPS; |
911 | SAVETMPS; |
844 | EXTEND (SP, 3); |
912 | EXTEND (SP, 3); |
845 | PUSHMARK (SP); |
913 | PUSHMARK (SP); |
846 | PUSHs (&PL_sv_yes); |
914 | PUSHs (&PL_sv_yes); |
847 | PUSHs (fullname); |
915 | PUSHs (fullname); |
848 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
916 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
849 | PUTBACK; |
917 | PUTBACK; |
850 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
918 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
851 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
919 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
852 | SPAGAIN; |
920 | SPAGAIN; |
853 | FREETMPS; |
921 | FREETMPS; |
854 | LEAVE; |
922 | LEAVE; |
855 | PL_runops = runops_trace; |
923 | PL_runops = runops_trace; |
… | |
… | |
869 | PL_runops = RUNOPS_DEFAULT; |
937 | PL_runops = RUNOPS_DEFAULT; |
870 | PUSHMARK (SP); |
938 | PUSHMARK (SP); |
871 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
939 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
872 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
940 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
873 | PUTBACK; |
941 | PUTBACK; |
874 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); |
942 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
875 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
943 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
876 | SPAGAIN; |
944 | SPAGAIN; |
877 | FREETMPS; |
945 | FREETMPS; |
878 | LEAVE; |
946 | LEAVE; |
879 | PL_runops = runops_trace; |
947 | PL_runops = runops_trace; |
… | |
… | |
1069 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
1137 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
1070 | |
1138 | |
1071 | if (expect_false (next->flags & CF_DESTROYED)) |
1139 | if (expect_false (next->flags & CF_DESTROYED)) |
1072 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1140 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1073 | |
1141 | |
1074 | if ( |
|
|
1075 | #if PERL_VERSION_ATLEAST (5,9,0) |
1142 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1076 | expect_false (PL_parser) |
|
|
1077 | #else |
|
|
1078 | expect_false (PL_lex_state != LEX_NOTPARSING) |
1143 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1079 | #endif |
|
|
1080 | ) |
|
|
1081 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
1144 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version"); |
|
|
1145 | #endif |
1082 | } |
1146 | } |
1083 | } |
1147 | } |
1084 | |
1148 | |
1085 | /* always use the TRANSFER macro */ |
1149 | /* always use the TRANSFER macro */ |
1086 | static void NOINLINE |
1150 | static void NOINLINE |
1087 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1151 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1088 | { |
1152 | { |
1089 | dSTACKLEVEL; |
1153 | dSTACKLEVEL; |
|
|
1154 | static volatile int has_throw; |
1090 | |
1155 | |
1091 | /* sometimes transfer is only called to set idle_sp */ |
1156 | /* sometimes transfer is only called to set idle_sp */ |
1092 | if (expect_false (!next)) |
1157 | if (expect_false (!next)) |
1093 | { |
1158 | { |
1094 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1159 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1109 | prev->flags &= ~CF_RUNNING; |
1174 | prev->flags &= ~CF_RUNNING; |
1110 | next->flags |= CF_RUNNING; |
1175 | next->flags |= CF_RUNNING; |
1111 | |
1176 | |
1112 | LOCK; |
1177 | LOCK; |
1113 | |
1178 | |
|
|
1179 | /* first get rid of the old state */ |
|
|
1180 | save_perl (aTHX_ prev); |
|
|
1181 | |
1114 | if (expect_false (next->flags & CF_NEW)) |
1182 | if (expect_false (next->flags & CF_NEW)) |
1115 | { |
1183 | { |
1116 | /* need to start coroutine */ |
1184 | /* need to start coroutine */ |
1117 | next->flags &= ~CF_NEW; |
1185 | next->flags &= ~CF_NEW; |
1118 | /* first get rid of the old state */ |
|
|
1119 | save_perl (aTHX_ prev); |
|
|
1120 | /* setup coroutine call */ |
1186 | /* setup coroutine call */ |
1121 | coro_setup (aTHX_ next); |
1187 | coro_setup (aTHX_ next); |
1122 | } |
1188 | } |
1123 | else |
1189 | else |
1124 | { |
|
|
1125 | /* coroutine already started */ |
|
|
1126 | save_perl (aTHX_ prev); |
|
|
1127 | load_perl (aTHX_ next); |
1190 | load_perl (aTHX_ next); |
1128 | } |
|
|
1129 | |
1191 | |
1130 | prev__cctx = prev->cctx; |
1192 | prev__cctx = prev->cctx; |
1131 | |
1193 | |
1132 | /* possibly "free" the cctx */ |
1194 | /* possibly "free" the cctx */ |
1133 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
1195 | if (expect_true ( |
|
|
1196 | prev__cctx->idle_sp == STACKLEVEL |
|
|
1197 | && !(prev__cctx->flags & CC_TRACE) |
|
|
1198 | && !force_cctx |
|
|
1199 | )) |
1134 | { |
1200 | { |
1135 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1201 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1136 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1202 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1137 | |
1203 | |
1138 | prev->cctx = 0; |
1204 | prev->cctx = 0; |
… | |
… | |
1148 | |
1214 | |
1149 | ++next->usecount; |
1215 | ++next->usecount; |
1150 | |
1216 | |
1151 | if (expect_true (!next->cctx)) |
1217 | if (expect_true (!next->cctx)) |
1152 | next->cctx = cctx_get (aTHX); |
1218 | next->cctx = cctx_get (aTHX); |
|
|
1219 | |
|
|
1220 | has_throw = !!next->throw; |
1153 | |
1221 | |
1154 | if (expect_false (prev__cctx != next->cctx)) |
1222 | if (expect_false (prev__cctx != next->cctx)) |
1155 | { |
1223 | { |
1156 | prev__cctx->top_env = PL_top_env; |
1224 | prev__cctx->top_env = PL_top_env; |
1157 | PL_top_env = next->cctx->top_env; |
1225 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1159 | } |
1227 | } |
1160 | |
1228 | |
1161 | free_coro_mortal (aTHX); |
1229 | free_coro_mortal (aTHX); |
1162 | UNLOCK; |
1230 | UNLOCK; |
1163 | |
1231 | |
1164 | if (expect_false (prev->throw || next->throw)) |
1232 | if (expect_false (has_throw)) |
1165 | { |
1233 | { |
1166 | struct coro *coro = SvSTATE (coro_current); |
1234 | struct coro *coro = SvSTATE (coro_current); |
1167 | |
1235 | |
1168 | if (coro->throw) |
1236 | if (coro->throw) |
1169 | { |
1237 | { |
… | |
… | |
1179 | struct transfer_args |
1247 | struct transfer_args |
1180 | { |
1248 | { |
1181 | struct coro *prev, *next; |
1249 | struct coro *prev, *next; |
1182 | }; |
1250 | }; |
1183 | |
1251 | |
1184 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1252 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1185 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1253 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1186 | |
1254 | |
1187 | /** high level stuff ********************************************************/ |
1255 | /** high level stuff ********************************************************/ |
1188 | |
1256 | |
1189 | static int |
1257 | static int |
… | |
… | |
1283 | { |
1351 | { |
1284 | dTHX; |
1352 | dTHX; |
1285 | struct transfer_args ta; |
1353 | struct transfer_args ta; |
1286 | |
1354 | |
1287 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1355 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1288 | TRANSFER (ta); |
1356 | TRANSFER (ta, 1); |
1289 | } |
1357 | } |
1290 | |
1358 | |
1291 | /** Coro ********************************************************************/ |
1359 | /** Coro ********************************************************************/ |
1292 | |
1360 | |
1293 | static void |
1361 | static void |
… | |
… | |
1295 | { |
1363 | { |
1296 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1364 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1297 | } |
1365 | } |
1298 | |
1366 | |
1299 | static SV * |
1367 | static SV * |
1300 | coro_deq (pTHX_ int min_prio) |
1368 | coro_deq (pTHX) |
1301 | { |
1369 | { |
1302 | int prio = PRIO_MAX - PRIO_MIN; |
1370 | int prio; |
1303 | |
1371 | |
1304 | min_prio -= PRIO_MIN; |
|
|
1305 | if (min_prio < 0) |
|
|
1306 | min_prio = 0; |
|
|
1307 | |
|
|
1308 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
1372 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1309 | if (AvFILLp (coro_ready [prio]) >= 0) |
1373 | if (AvFILLp (coro_ready [prio]) >= 0) |
1310 | return av_shift (coro_ready [prio]); |
1374 | return av_shift (coro_ready [prio]); |
1311 | |
1375 | |
1312 | return 0; |
1376 | return 0; |
1313 | } |
1377 | } |
… | |
… | |
1349 | SV *prev_sv, *next_sv; |
1413 | SV *prev_sv, *next_sv; |
1350 | |
1414 | |
1351 | for (;;) |
1415 | for (;;) |
1352 | { |
1416 | { |
1353 | LOCK; |
1417 | LOCK; |
1354 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1418 | next_sv = coro_deq (aTHX); |
1355 | |
1419 | |
1356 | /* nothing to schedule: call the idle handler */ |
1420 | /* nothing to schedule: call the idle handler */ |
1357 | if (expect_false (!next_sv)) |
1421 | if (expect_false (!next_sv)) |
1358 | { |
1422 | { |
1359 | dSP; |
1423 | dSP; |
… | |
… | |
1363 | SAVETMPS; |
1427 | SAVETMPS; |
1364 | |
1428 | |
1365 | PUSHMARK (SP); |
1429 | PUSHMARK (SP); |
1366 | PUTBACK; |
1430 | PUTBACK; |
1367 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1431 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1432 | SPAGAIN; |
1368 | |
1433 | |
1369 | FREETMPS; |
1434 | FREETMPS; |
1370 | LEAVE; |
1435 | LEAVE; |
1371 | continue; |
1436 | continue; |
1372 | } |
1437 | } |
… | |
… | |
1427 | { |
1492 | { |
1428 | dTHX; |
1493 | dTHX; |
1429 | struct transfer_args ta; |
1494 | struct transfer_args ta; |
1430 | |
1495 | |
1431 | prepare_schedule (aTHX_ &ta); |
1496 | prepare_schedule (aTHX_ &ta); |
1432 | TRANSFER (ta); |
1497 | TRANSFER (ta, 1); |
1433 | } |
1498 | } |
1434 | |
1499 | |
1435 | static int |
1500 | static int |
1436 | api_cede (void) |
1501 | api_cede (void) |
1437 | { |
1502 | { |
… | |
… | |
1440 | |
1505 | |
1441 | prepare_cede (aTHX_ &ta); |
1506 | prepare_cede (aTHX_ &ta); |
1442 | |
1507 | |
1443 | if (expect_true (ta.prev != ta.next)) |
1508 | if (expect_true (ta.prev != ta.next)) |
1444 | { |
1509 | { |
1445 | TRANSFER (ta); |
1510 | TRANSFER (ta, 1); |
1446 | return 1; |
1511 | return 1; |
1447 | } |
1512 | } |
1448 | else |
1513 | else |
1449 | return 0; |
1514 | return 0; |
1450 | } |
1515 | } |
… | |
… | |
1455 | dTHX; |
1520 | dTHX; |
1456 | struct transfer_args ta; |
1521 | struct transfer_args ta; |
1457 | |
1522 | |
1458 | if (prepare_cede_notself (aTHX_ &ta)) |
1523 | if (prepare_cede_notself (aTHX_ &ta)) |
1459 | { |
1524 | { |
1460 | TRANSFER (ta); |
1525 | TRANSFER (ta, 1); |
1461 | return 1; |
1526 | return 1; |
1462 | } |
1527 | } |
1463 | else |
1528 | else |
1464 | return 0; |
1529 | return 0; |
1465 | } |
1530 | } |
… | |
… | |
1502 | BOOT_PAGESIZE; |
1567 | BOOT_PAGESIZE; |
1503 | |
1568 | |
1504 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1569 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1505 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1570 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1506 | |
1571 | |
|
|
1572 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
|
|
1573 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1574 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; |
|
|
1575 | PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
|
|
1576 | |
1507 | hv_sig = coro_get_hv ("SIG", TRUE); |
1577 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1508 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1578 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
1509 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1579 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1510 | |
|
|
1511 | if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
|
|
1512 | if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
|
|
1513 | |
1580 | |
1514 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1581 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1515 | |
1582 | |
1516 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1583 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1517 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1584 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1523 | |
1590 | |
1524 | while (main_top_env->je_prev) |
1591 | while (main_top_env->je_prev) |
1525 | main_top_env = main_top_env->je_prev; |
1592 | main_top_env = main_top_env->je_prev; |
1526 | |
1593 | |
1527 | coroapi.ver = CORO_API_VERSION; |
1594 | coroapi.ver = CORO_API_VERSION; |
|
|
1595 | coroapi.rev = CORO_API_REVISION; |
1528 | coroapi.transfer = api_transfer; |
1596 | coroapi.transfer = api_transfer; |
1529 | |
1597 | |
1530 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1598 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1531 | } |
1599 | } |
1532 | |
1600 | |
1533 | SV * |
1601 | SV * |
1534 | new (char *klass, ...) |
1602 | new (char *klass, ...) |
1535 | CODE: |
1603 | CODE: |
1536 | { |
1604 | { |
1537 | struct coro *coro; |
1605 | struct coro *coro; |
|
|
1606 | MAGIC *mg; |
1538 | HV *hv; |
1607 | HV *hv; |
1539 | int i; |
1608 | int i; |
1540 | |
1609 | |
1541 | Newz (0, coro, 1, struct coro); |
1610 | Newz (0, coro, 1, struct coro); |
1542 | coro->args = newAV (); |
1611 | coro->args = newAV (); |
… | |
… | |
1545 | if (coro_first) coro_first->prev = coro; |
1614 | if (coro_first) coro_first->prev = coro; |
1546 | coro->next = coro_first; |
1615 | coro->next = coro_first; |
1547 | coro_first = coro; |
1616 | coro_first = coro; |
1548 | |
1617 | |
1549 | coro->hv = hv = newHV (); |
1618 | coro->hv = hv = newHV (); |
1550 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1619 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
1620 | mg->mg_flags |= MGf_DUP; |
1551 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1621 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1552 | |
1622 | |
1553 | av_extend (coro->args, items - 1); |
1623 | av_extend (coro->args, items - 1); |
1554 | for (i = 1; i < items; i++) |
1624 | for (i = 1; i < items; i++) |
1555 | av_push (coro->args, newSVsv (ST (i))); |
1625 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1561 | # function to increase chances that they all will call transfer with the same |
1631 | # function to increase chances that they all will call transfer with the same |
1562 | # stack offset |
1632 | # stack offset |
1563 | void |
1633 | void |
1564 | _set_stacklevel (...) |
1634 | _set_stacklevel (...) |
1565 | ALIAS: |
1635 | ALIAS: |
1566 | Coro::State::transfer = 1 |
1636 | Coro::State::transfer = 1 |
1567 | Coro::schedule = 2 |
1637 | Coro::schedule = 2 |
1568 | Coro::cede = 3 |
1638 | Coro::cede = 3 |
1569 | Coro::cede_notself = 4 |
1639 | Coro::cede_notself = 4 |
1570 | Coro::Event::next = 5 |
|
|
1571 | Coro::Event::next_cancel = 6 |
|
|
1572 | PPCODE: |
1640 | CODE: |
1573 | { |
1641 | { |
1574 | struct transfer_args ta; |
1642 | struct transfer_args ta; |
1575 | int again = 0; |
|
|
1576 | |
1643 | |
1577 | do |
1644 | PUTBACK; |
|
|
1645 | switch (ix) |
1578 | { |
1646 | { |
1579 | switch (ix) |
|
|
1580 | { |
|
|
1581 | case 0: |
1647 | case 0: |
1582 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1648 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1583 | ta.next = 0; |
1649 | ta.next = 0; |
1584 | break; |
1650 | break; |
1585 | |
1651 | |
1586 | case 1: |
1652 | case 1: |
1587 | if (items != 2) |
1653 | if (items != 2) |
1588 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1654 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1589 | |
1655 | |
1590 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1656 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1591 | break; |
1657 | break; |
1592 | |
1658 | |
1593 | case 2: |
1659 | case 2: |
1594 | prepare_schedule (aTHX_ &ta); |
1660 | prepare_schedule (aTHX_ &ta); |
1595 | break; |
1661 | break; |
1596 | |
1662 | |
1597 | case 3: |
1663 | case 3: |
1598 | prepare_cede (aTHX_ &ta); |
1664 | prepare_cede (aTHX_ &ta); |
1599 | break; |
1665 | break; |
1600 | |
1666 | |
1601 | case 4: |
1667 | case 4: |
1602 | if (!prepare_cede_notself (aTHX_ &ta)) |
1668 | if (!prepare_cede_notself (aTHX_ &ta)) |
1603 | XSRETURN_EMPTY; |
1669 | XSRETURN_EMPTY; |
1604 | |
1670 | |
1605 | break; |
1671 | break; |
1606 | |
|
|
1607 | case 5: |
|
|
1608 | case 6: |
|
|
1609 | if (items != 1) |
|
|
1610 | croak ("Coro::Event::next (watcher) expects one argument, not %d", items); |
|
|
1611 | |
|
|
1612 | { |
|
|
1613 | SV *ev = coroapi.coro_event_next (ST (0), ix == 6, GIMME_V != G_VOID); |
|
|
1614 | |
|
|
1615 | if (ev) |
|
|
1616 | { |
|
|
1617 | if (GIMME_V != G_VOID) |
|
|
1618 | { |
|
|
1619 | XPUSHs (ev); |
|
|
1620 | XSRETURN (1); |
|
|
1621 | } |
|
|
1622 | else |
|
|
1623 | XSRETURN_EMPTY; |
|
|
1624 | } |
|
|
1625 | } |
|
|
1626 | |
|
|
1627 | prepare_schedule (aTHX_ &ta); |
|
|
1628 | again = 1; |
|
|
1629 | break; |
|
|
1630 | } |
|
|
1631 | |
|
|
1632 | BARRIER; |
|
|
1633 | TRANSFER (ta); |
|
|
1634 | BARRIER; |
|
|
1635 | } |
1672 | } |
1636 | while (again); |
1673 | SPAGAIN; |
1637 | |
1674 | |
1638 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1675 | BARRIER; |
1639 | XSRETURN_YES; |
1676 | PUTBACK; |
1640 | |
1677 | TRANSFER (ta, 0); |
1641 | XSRETURN_EMPTY; /* not understood why this is necessary, likely some stack handling bug */ |
1678 | SPAGAIN; /* might be the sp of a different coroutine now */ |
|
|
1679 | /* be extra careful not to ever do anything after TRANSFER */ |
|
|
1680 | } |
1642 | |
1681 | |
1643 | bool |
1682 | bool |
1644 | _destroy (SV *coro_sv) |
1683 | _destroy (SV *coro_sv) |
1645 | CODE: |
1684 | CODE: |
1646 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1685 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
1697 | { |
1736 | { |
1698 | struct coro temp; |
1737 | struct coro temp; |
1699 | |
1738 | |
1700 | if (!(coro->flags & CF_RUNNING)) |
1739 | if (!(coro->flags & CF_RUNNING)) |
1701 | { |
1740 | { |
|
|
1741 | PUTBACK; |
1702 | save_perl (aTHX_ &temp); |
1742 | save_perl (aTHX_ &temp); |
1703 | load_perl (aTHX_ coro); |
1743 | load_perl (aTHX_ coro); |
1704 | } |
1744 | } |
1705 | |
1745 | |
1706 | { |
1746 | { |
… | |
… | |
1715 | eval_sv (coderef, 0); |
1755 | eval_sv (coderef, 0); |
1716 | else |
1756 | else |
1717 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1757 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1718 | |
1758 | |
1719 | POPSTACK; |
1759 | POPSTACK; |
|
|
1760 | SPAGAIN; |
1720 | FREETMPS; |
1761 | FREETMPS; |
1721 | LEAVE; |
1762 | LEAVE; |
|
|
1763 | PUTBACK; |
1722 | } |
1764 | } |
1723 | |
1765 | |
1724 | if (!(coro->flags & CF_RUNNING)) |
1766 | if (!(coro->flags & CF_RUNNING)) |
1725 | { |
1767 | { |
1726 | save_perl (aTHX_ coro); |
1768 | save_perl (aTHX_ coro); |
1727 | load_perl (aTHX_ &temp); |
1769 | load_perl (aTHX_ &temp); |
|
|
1770 | SPAGAIN; |
1728 | } |
1771 | } |
1729 | } |
1772 | } |
1730 | } |
1773 | } |
1731 | |
1774 | |
1732 | SV * |
1775 | SV * |
… | |
… | |
1773 | case 1: RETVAL = coro->usecount; break; |
1816 | case 1: RETVAL = coro->usecount; break; |
1774 | } |
1817 | } |
1775 | OUTPUT: |
1818 | OUTPUT: |
1776 | RETVAL |
1819 | RETVAL |
1777 | |
1820 | |
|
|
1821 | void |
|
|
1822 | force_cctx () |
|
|
1823 | CODE: |
|
|
1824 | struct coro *coro = SvSTATE (coro_current); |
|
|
1825 | coro->cctx->idle_sp = 0; |
1778 | |
1826 | |
1779 | MODULE = Coro::State PACKAGE = Coro |
1827 | MODULE = Coro::State PACKAGE = Coro |
1780 | |
1828 | |
1781 | BOOT: |
1829 | BOOT: |
1782 | { |
1830 | { |
1783 | int i; |
1831 | int i; |
1784 | |
1832 | |
1785 | sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); |
1833 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
1786 | sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); |
1834 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
1787 | av_async_pool = coro_get_av ("Coro::async_pool", TRUE); |
1835 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
1788 | |
1836 | |
1789 | coro_current = coro_get_sv ("Coro::current", FALSE); |
1837 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
1790 | SvREADONLY_on (coro_current); |
1838 | SvREADONLY_on (coro_current); |
1791 | |
1839 | |
1792 | coro_stash = gv_stashpv ("Coro", TRUE); |
1840 | coro_stash = gv_stashpv ("Coro", TRUE); |
1793 | |
1841 | |
1794 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1842 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
1869 | PROTOTYPE: $;$ |
1917 | PROTOTYPE: $;$ |
1870 | CODE: |
1918 | CODE: |
1871 | SvREFCNT_dec (self->throw); |
1919 | SvREFCNT_dec (self->throw); |
1872 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
1920 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
1873 | |
1921 | |
|
|
1922 | void |
|
|
1923 | swap_defsv (Coro::State self) |
|
|
1924 | PROTOTYPE: $ |
|
|
1925 | ALIAS: |
|
|
1926 | swap_defav = 1 |
|
|
1927 | CODE: |
|
|
1928 | if (!self->slot) |
|
|
1929 | croak ("cannot swap state with coroutine that has no saved state"); |
|
|
1930 | else |
|
|
1931 | { |
|
|
1932 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
|
|
1933 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
|
|
1934 | |
|
|
1935 | SV *tmp = *src; *src = *dst; *dst = tmp; |
|
|
1936 | } |
|
|
1937 | |
1874 | # for async_pool speedup |
1938 | # for async_pool speedup |
1875 | void |
1939 | void |
1876 | _pool_1 (SV *cb) |
1940 | _pool_1 (SV *cb) |
1877 | CODE: |
1941 | CODE: |
1878 | { |
1942 | { |
1879 | struct coro *coro = SvSTATE (coro_current); |
1943 | struct coro *coro = SvSTATE (coro_current); |
1880 | HV *hv = (HV *)SvRV (coro_current); |
1944 | HV *hv = (HV *)SvRV (coro_current); |
1881 | AV *defav = GvAV (PL_defgv); |
1945 | AV *defav = GvAV (PL_defgv); |
1882 | SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); |
1946 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1883 | AV *invoke_av; |
1947 | AV *invoke_av; |
1884 | int i, len; |
1948 | int i, len; |
1885 | |
1949 | |
1886 | if (!invoke) |
1950 | if (!invoke) |
|
|
1951 | { |
|
|
1952 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
1887 | croak ("\3async_pool terminate\2\n"); |
1953 | croak ("\3async_pool terminate\2\n"); |
|
|
1954 | } |
1888 | |
1955 | |
1889 | SvREFCNT_dec (coro->saved_deffh); |
1956 | SvREFCNT_dec (coro->saved_deffh); |
1890 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1957 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1891 | |
1958 | |
1892 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1959 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1893 | newSVpvn (strpair ("[async_pool]")), 0); |
1960 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1894 | |
1961 | |
1895 | invoke_av = (AV *)SvRV (invoke); |
1962 | invoke_av = (AV *)SvRV (invoke); |
1896 | len = av_len (invoke_av); |
1963 | len = av_len (invoke_av); |
1897 | |
1964 | |
1898 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
1965 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
… | |
… | |
1918 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1985 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1919 | coro->saved_deffh = 0; |
1986 | coro->saved_deffh = 0; |
1920 | |
1987 | |
1921 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1988 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1922 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1989 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1990 | { |
|
|
1991 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
1923 | croak ("\3async_pool terminate\2\n"); |
1992 | croak ("\3async_pool terminate\2\n"); |
|
|
1993 | } |
1924 | |
1994 | |
1925 | av_clear (GvAV (PL_defgv)); |
1995 | av_clear (GvAV (PL_defgv)); |
1926 | hv_store ((HV *)SvRV (coro_current), strpair ("desc"), |
1996 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1927 | newSVpvn (strpair ("[async_pool idle]")), 0); |
1997 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1928 | |
1998 | |
1929 | coro->prio = 0; |
1999 | coro->prio = 0; |
1930 | |
2000 | |
1931 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2001 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1932 | api_trace (coro_current, 0); |
2002 | api_trace (coro_current, 0); |