… | |
… | |
133 | #else |
133 | #else |
134 | # define dSTACKLEVEL volatile void *stacklevel |
134 | # define dSTACKLEVEL volatile void *stacklevel |
135 | # define STACKLEVEL ((void *)&stacklevel) |
135 | # define STACKLEVEL ((void *)&stacklevel) |
136 | #endif |
136 | #endif |
137 | |
137 | |
138 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
138 | #define IN_DESTRUCT PL_dirty |
139 | |
139 | |
140 | #if __GNUC__ >= 3 |
140 | #if __GNUC__ >= 3 |
141 | # define attribute(x) __attribute__(x) |
141 | # define attribute(x) __attribute__(x) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
143 | # define INLINE static inline |
143 | # define INLINE static inline |
… | |
… | |
175 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
177 | |
177 | |
178 | static AV *av_destroy; /* destruction queue */ |
178 | static AV *av_destroy; /* destruction queue */ |
179 | static SV *sv_manager; /* the manager coro */ |
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
180 | |
181 | |
181 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
182 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
183 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
184 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
… | |
… | |
246 | { |
247 | { |
247 | SV *defsv; |
248 | SV *defsv; |
248 | AV *defav; |
249 | AV *defav; |
249 | SV *errsv; |
250 | SV *errsv; |
250 | SV *irsgv; |
251 | SV *irsgv; |
|
|
252 | HV *hinthv; |
251 | #define VAR(name,type) type name; |
253 | #define VAR(name,type) type name; |
252 | # include "state.h" |
254 | # include "state.h" |
253 | #undef VAR |
255 | #undef VAR |
254 | } perl_slots; |
256 | } perl_slots; |
255 | |
257 | |
… | |
… | |
353 | HV *st; |
355 | HV *st; |
354 | GV *gvp; |
356 | GV *gvp; |
355 | return sv_2cv (sv, &st, &gvp, 0); |
357 | return sv_2cv (sv, &st, &gvp, 0); |
356 | } |
358 | } |
357 | |
359 | |
|
|
360 | /*****************************************************************************/ |
|
|
361 | /* magic glue */ |
|
|
362 | |
|
|
363 | #define CORO_MAGIC_type_cv 26 |
|
|
364 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
365 | |
|
|
366 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
367 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
368 | ? SvMAGIC (sv) \ |
|
|
369 | : mg_find (sv, type)) |
|
|
370 | |
|
|
371 | #define CORO_MAGIC(sv, type) \ |
|
|
372 | (expect_true (SvMAGIC (sv)) \ |
|
|
373 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
374 | : 0) |
|
|
375 | |
|
|
376 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
377 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
378 | |
|
|
379 | INLINE struct coro * |
|
|
380 | SvSTATE_ (pTHX_ SV *coro) |
|
|
381 | { |
|
|
382 | HV *stash; |
|
|
383 | MAGIC *mg; |
|
|
384 | |
|
|
385 | if (SvROK (coro)) |
|
|
386 | coro = SvRV (coro); |
|
|
387 | |
|
|
388 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
389 | croak ("Coro::State object required"); |
|
|
390 | |
|
|
391 | stash = SvSTASH (coro); |
|
|
392 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
393 | { |
|
|
394 | /* very slow, but rare, check */ |
|
|
395 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
396 | croak ("Coro::State object required"); |
|
|
397 | } |
|
|
398 | |
|
|
399 | mg = CORO_MAGIC_state (coro); |
|
|
400 | return (struct coro *)mg->mg_ptr; |
|
|
401 | } |
|
|
402 | |
|
|
403 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
404 | |
|
|
405 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
406 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
407 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
408 | |
|
|
409 | /*****************************************************************************/ |
|
|
410 | /* padlist management and caching */ |
|
|
411 | |
358 | static AV * |
412 | static AV * |
359 | coro_derive_padlist (pTHX_ CV *cv) |
413 | coro_derive_padlist (pTHX_ CV *cv) |
360 | { |
414 | { |
361 | AV *padlist = CvPADLIST (cv); |
415 | AV *padlist = CvPADLIST (cv); |
362 | AV *newpadlist, *newpad; |
416 | AV *newpadlist, *newpad; |
… | |
… | |
369 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
423 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
370 | #endif |
424 | #endif |
371 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
425 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
372 | --AvFILLp (padlist); |
426 | --AvFILLp (padlist); |
373 | |
427 | |
374 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
428 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
375 | av_store (newpadlist, 1, (SV *)newpad); |
429 | av_store (newpadlist, 1, (SV *)newpad); |
376 | |
430 | |
377 | return newpadlist; |
431 | return newpadlist; |
378 | } |
432 | } |
379 | |
433 | |
380 | static void |
434 | static void |
381 | free_padlist (pTHX_ AV *padlist) |
435 | free_padlist (pTHX_ AV *padlist) |
382 | { |
436 | { |
383 | /* may be during global destruction */ |
437 | /* may be during global destruction */ |
384 | if (SvREFCNT (padlist)) |
438 | if (!IN_DESTRUCT) |
385 | { |
439 | { |
386 | I32 i = AvFILLp (padlist); |
440 | I32 i = AvFILLp (padlist); |
387 | while (i >= 0) |
441 | |
|
|
442 | while (i > 0) /* special-case index 0 */ |
388 | { |
443 | { |
389 | SV **svp = av_fetch (padlist, i--, FALSE); |
444 | /* we try to be extra-careful here */ |
390 | if (svp) |
445 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
391 | { |
446 | I32 j = AvFILLp (av); |
392 | SV *sv; |
447 | |
393 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
448 | while (j >= 0) |
|
|
449 | SvREFCNT_dec (AvARRAY (av)[j--]); |
|
|
450 | |
|
|
451 | AvFILLp (av) = -1; |
394 | SvREFCNT_dec (sv); |
452 | SvREFCNT_dec (av); |
395 | |
|
|
396 | SvREFCNT_dec (*svp); |
|
|
397 | } |
|
|
398 | } |
453 | } |
399 | |
454 | |
|
|
455 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
|
|
456 | |
|
|
457 | AvFILLp (padlist) = -1; |
400 | SvREFCNT_dec ((SV*)padlist); |
458 | SvREFCNT_dec ((SV*)padlist); |
401 | } |
459 | } |
402 | } |
460 | } |
403 | |
461 | |
404 | static int |
462 | static int |
… | |
… | |
413 | |
471 | |
414 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
472 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
415 | |
473 | |
416 | return 0; |
474 | return 0; |
417 | } |
475 | } |
418 | |
|
|
419 | #define CORO_MAGIC_type_cv 26 |
|
|
420 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
421 | |
476 | |
422 | static MGVTBL coro_cv_vtbl = { |
477 | static MGVTBL coro_cv_vtbl = { |
423 | 0, 0, 0, 0, |
478 | 0, 0, 0, 0, |
424 | coro_cv_free |
479 | coro_cv_free |
425 | }; |
480 | }; |
426 | |
|
|
427 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
428 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
429 | ? SvMAGIC (sv) \ |
|
|
430 | : mg_find (sv, type)) |
|
|
431 | |
|
|
432 | #define CORO_MAGIC(sv, type) \ |
|
|
433 | (expect_true (SvMAGIC (sv)) \ |
|
|
434 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
435 | : 0) |
|
|
436 | |
|
|
437 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
438 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
439 | |
|
|
440 | INLINE struct coro * |
|
|
441 | SvSTATE_ (pTHX_ SV *coro) |
|
|
442 | { |
|
|
443 | HV *stash; |
|
|
444 | MAGIC *mg; |
|
|
445 | |
|
|
446 | if (SvROK (coro)) |
|
|
447 | coro = SvRV (coro); |
|
|
448 | |
|
|
449 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
450 | croak ("Coro::State object required"); |
|
|
451 | |
|
|
452 | stash = SvSTASH (coro); |
|
|
453 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
454 | { |
|
|
455 | /* very slow, but rare, check */ |
|
|
456 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
457 | croak ("Coro::State object required"); |
|
|
458 | } |
|
|
459 | |
|
|
460 | mg = CORO_MAGIC_state (coro); |
|
|
461 | return (struct coro *)mg->mg_ptr; |
|
|
462 | } |
|
|
463 | |
|
|
464 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
465 | |
|
|
466 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
467 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
468 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
469 | |
481 | |
470 | /* the next two functions merely cache the padlists */ |
482 | /* the next two functions merely cache the padlists */ |
471 | static void |
483 | static void |
472 | get_padlist (pTHX_ CV *cv) |
484 | get_padlist (pTHX_ CV *cv) |
473 | { |
485 | { |
… | |
… | |
501 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
513 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
502 | |
514 | |
503 | av = (AV *)mg->mg_obj; |
515 | av = (AV *)mg->mg_obj; |
504 | |
516 | |
505 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
517 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
506 | av_extend (av, AvMAX (av) + 1); |
518 | av_extend (av, AvFILLp (av) + 1); |
507 | |
519 | |
508 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
520 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
509 | } |
521 | } |
510 | |
522 | |
511 | /** load & save, init *******************************************************/ |
523 | /** load & save, init *******************************************************/ |
… | |
… | |
516 | perl_slots *slot = c->slot; |
528 | perl_slots *slot = c->slot; |
517 | c->slot = 0; |
529 | c->slot = 0; |
518 | |
530 | |
519 | PL_mainstack = c->mainstack; |
531 | PL_mainstack = c->mainstack; |
520 | |
532 | |
521 | GvSV (PL_defgv) = slot->defsv; |
533 | GvSV (PL_defgv) = slot->defsv; |
522 | GvAV (PL_defgv) = slot->defav; |
534 | GvAV (PL_defgv) = slot->defav; |
523 | GvSV (PL_errgv) = slot->errsv; |
535 | GvSV (PL_errgv) = slot->errsv; |
524 | GvSV (irsgv) = slot->irsgv; |
536 | GvSV (irsgv) = slot->irsgv; |
|
|
537 | GvHV (PL_hintgv) = slot->hinthv; |
525 | |
538 | |
526 | #define VAR(name,type) PL_ ## name = slot->name; |
539 | #define VAR(name,type) PL_ ## name = slot->name; |
527 | # include "state.h" |
540 | # include "state.h" |
528 | #undef VAR |
541 | #undef VAR |
529 | |
542 | |
… | |
… | |
615 | c->mainstack = PL_mainstack; |
628 | c->mainstack = PL_mainstack; |
616 | |
629 | |
617 | { |
630 | { |
618 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
631 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
619 | |
632 | |
620 | slot->defav = GvAV (PL_defgv); |
633 | slot->defav = GvAV (PL_defgv); |
621 | slot->defsv = DEFSV; |
634 | slot->defsv = DEFSV; |
622 | slot->errsv = ERRSV; |
635 | slot->errsv = ERRSV; |
623 | slot->irsgv = GvSV (irsgv); |
636 | slot->irsgv = GvSV (irsgv); |
|
|
637 | slot->hinthv = GvHV (PL_hintgv); |
624 | |
638 | |
625 | #define VAR(name,type) slot->name = PL_ ## name; |
639 | #define VAR(name,type) slot->name = PL_ ## name; |
626 | # include "state.h" |
640 | # include "state.h" |
627 | #undef VAR |
641 | #undef VAR |
628 | } |
642 | } |
… | |
… | |
864 | |
878 | |
865 | PL_runops = RUNOPS_DEFAULT; |
879 | PL_runops = RUNOPS_DEFAULT; |
866 | PL_curcop = &PL_compiling; |
880 | PL_curcop = &PL_compiling; |
867 | PL_in_eval = EVAL_NULL; |
881 | PL_in_eval = EVAL_NULL; |
868 | PL_comppad = 0; |
882 | PL_comppad = 0; |
|
|
883 | PL_comppad_name = 0; |
|
|
884 | PL_comppad_name_fill = 0; |
|
|
885 | PL_comppad_name_floor = 0; |
869 | PL_curpm = 0; |
886 | PL_curpm = 0; |
870 | PL_curpad = 0; |
887 | PL_curpad = 0; |
871 | PL_localizing = 0; |
888 | PL_localizing = 0; |
872 | PL_dirty = 0; |
889 | PL_dirty = 0; |
873 | PL_restartop = 0; |
890 | PL_restartop = 0; |
874 | #if PERL_VERSION_ATLEAST (5,10,0) |
891 | #if PERL_VERSION_ATLEAST (5,10,0) |
875 | PL_parser = 0; |
892 | PL_parser = 0; |
876 | #endif |
893 | #endif |
|
|
894 | PL_hints = 0; |
877 | |
895 | |
878 | /* recreate the die/warn hooks */ |
896 | /* recreate the die/warn hooks */ |
879 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
897 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
880 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
898 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
881 | |
899 | |
882 | GvSV (PL_defgv) = newSV (0); |
900 | GvSV (PL_defgv) = newSV (0); |
883 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
901 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
884 | GvSV (PL_errgv) = newSV (0); |
902 | GvSV (PL_errgv) = newSV (0); |
885 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
903 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
904 | GvHV (PL_hintgv) = 0; |
886 | PL_rs = newSVsv (GvSV (irsgv)); |
905 | PL_rs = newSVsv (GvSV (irsgv)); |
887 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
906 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
888 | |
907 | |
889 | { |
908 | { |
890 | dSP; |
909 | dSP; |
… | |
… | |
919 | /* copy throw, in case it was set before coro_setup */ |
938 | /* copy throw, in case it was set before coro_setup */ |
920 | CORO_THROW = coro->except; |
939 | CORO_THROW = coro->except; |
921 | } |
940 | } |
922 | |
941 | |
923 | static void |
942 | static void |
924 | coro_destruct (pTHX_ struct coro *coro) |
943 | coro_unwind_stacks (pTHX) |
925 | { |
944 | { |
926 | if (!IN_DESTRUCT) |
945 | if (!IN_DESTRUCT) |
927 | { |
946 | { |
928 | /* restore all saved variables and stuff */ |
947 | /* restore all saved variables and stuff */ |
929 | LEAVE_SCOPE (0); |
948 | LEAVE_SCOPE (0); |
… | |
… | |
937 | POPSTACK_TO (PL_mainstack); |
956 | POPSTACK_TO (PL_mainstack); |
938 | |
957 | |
939 | /* unwind main stack */ |
958 | /* unwind main stack */ |
940 | dounwind (-1); |
959 | dounwind (-1); |
941 | } |
960 | } |
|
|
961 | } |
|
|
962 | |
|
|
963 | static void |
|
|
964 | coro_destruct_perl (pTHX_ struct coro *coro) |
|
|
965 | { |
|
|
966 | coro_unwind_stacks (aTHX); |
942 | |
967 | |
943 | SvREFCNT_dec (GvSV (PL_defgv)); |
968 | SvREFCNT_dec (GvSV (PL_defgv)); |
944 | SvREFCNT_dec (GvAV (PL_defgv)); |
969 | SvREFCNT_dec (GvAV (PL_defgv)); |
945 | SvREFCNT_dec (GvSV (PL_errgv)); |
970 | SvREFCNT_dec (GvSV (PL_errgv)); |
946 | SvREFCNT_dec (PL_defoutgv); |
971 | SvREFCNT_dec (PL_defoutgv); |
947 | SvREFCNT_dec (PL_rs); |
972 | SvREFCNT_dec (PL_rs); |
948 | SvREFCNT_dec (GvSV (irsgv)); |
973 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
974 | SvREFCNT_dec (GvHV (PL_hintgv)); |
949 | |
975 | |
950 | SvREFCNT_dec (PL_diehook); |
976 | SvREFCNT_dec (PL_diehook); |
951 | SvREFCNT_dec (PL_warnhook); |
977 | SvREFCNT_dec (PL_warnhook); |
952 | |
978 | |
953 | SvREFCNT_dec (coro->saved_deffh); |
979 | SvREFCNT_dec (coro->saved_deffh); |
… | |
… | |
1168 | /* |
1194 | /* |
1169 | * If perl-run returns we assume exit() was being called or the coro |
1195 | * If perl-run returns we assume exit() was being called or the coro |
1170 | * fell off the end, which seems to be the only valid (non-bug) |
1196 | * fell off the end, which seems to be the only valid (non-bug) |
1171 | * reason for perl_run to return. We try to exit by jumping to the |
1197 | * reason for perl_run to return. We try to exit by jumping to the |
1172 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1198 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1173 | * coroutine as Coro has no such concept |
1199 | * coroutine as Coro has no such concept. |
|
|
1200 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1201 | * that backend are too broken to do it in a standards-compliant way. |
1174 | */ |
1202 | */ |
1175 | PL_top_env = main_top_env; |
1203 | PL_top_env = main_top_env; |
1176 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1204 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1177 | } |
1205 | } |
1178 | } |
1206 | } |
… | |
… | |
1255 | cctx_destroy (coro_cctx *cctx) |
1283 | cctx_destroy (coro_cctx *cctx) |
1256 | { |
1284 | { |
1257 | if (!cctx) |
1285 | if (!cctx) |
1258 | return; |
1286 | return; |
1259 | |
1287 | |
1260 | assert (cctx != cctx_current);//D temporary |
1288 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
1261 | |
1289 | |
1262 | --cctx_count; |
1290 | --cctx_count; |
1263 | coro_destroy (&cctx->cctx); |
1291 | coro_destroy (&cctx->cctx); |
1264 | |
1292 | |
1265 | /* coro_transfer creates new, empty cctx's */ |
1293 | /* coro_transfer creates new, empty cctx's */ |
… | |
… | |
1329 | /* TODO: throwing up here is considered harmful */ |
1357 | /* TODO: throwing up here is considered harmful */ |
1330 | |
1358 | |
1331 | if (expect_true (prev != next)) |
1359 | if (expect_true (prev != next)) |
1332 | { |
1360 | { |
1333 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1361 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1334 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1362 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1335 | |
1363 | |
1336 | if (expect_false (next->flags & CF_RUNNING)) |
1364 | if (expect_false (next->flags & CF_RUNNING)) |
1337 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1365 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1338 | |
1366 | |
1339 | if (expect_false (next->flags & CF_DESTROYED)) |
1367 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1393 | )) |
1421 | )) |
1394 | { |
1422 | { |
1395 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1423 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1396 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1424 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1397 | |
1425 | |
1398 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1426 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1399 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1427 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1400 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1428 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1401 | if (!next->cctx) |
1429 | if (expect_true (!next->cctx)) |
1402 | next->cctx = cctx_get (aTHX); |
1430 | next->cctx = cctx_get (aTHX); |
1403 | |
1431 | |
1404 | cctx_put (cctx_current); |
1432 | cctx_put (cctx_current); |
1405 | assert (!prev->cctx);//D temporary |
|
|
1406 | } |
1433 | } |
1407 | else |
1434 | else |
1408 | prev->cctx = cctx_current; |
1435 | prev->cctx = cctx_current; |
1409 | |
1436 | |
1410 | ++next->usecount; |
1437 | ++next->usecount; |
… | |
… | |
1448 | --coro_nready; |
1475 | --coro_nready; |
1449 | } |
1476 | } |
1450 | else |
1477 | else |
1451 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1478 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1452 | |
1479 | |
1453 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1480 | if (coro->mainstack |
|
|
1481 | && coro->mainstack != main_mainstack |
|
|
1482 | && coro->slot |
|
|
1483 | && !PL_dirty) |
1454 | { |
1484 | { |
1455 | struct coro temp; |
1485 | struct coro temp; |
1456 | |
1486 | |
1457 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
1487 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
1458 | |
1488 | |
1459 | save_perl (aTHX_ &temp); |
1489 | save_perl (aTHX_ &temp); |
1460 | load_perl (aTHX_ coro); |
1490 | load_perl (aTHX_ coro); |
1461 | |
1491 | |
1462 | coro_destruct (aTHX_ coro); |
1492 | coro_destruct_perl (aTHX_ coro); |
1463 | |
1493 | |
1464 | load_perl (aTHX_ &temp); |
1494 | load_perl (aTHX_ &temp); |
1465 | |
1495 | |
1466 | coro->slot = 0; |
1496 | coro->slot = 0; |
1467 | } |
1497 | } |
… | |
… | |
1581 | { |
1611 | { |
1582 | struct coro *coro; |
1612 | struct coro *coro; |
1583 | SV *sv_hook; |
1613 | SV *sv_hook; |
1584 | void (*xs_hook)(void); |
1614 | void (*xs_hook)(void); |
1585 | |
1615 | |
1586 | if (SvROK (coro_sv)) |
|
|
1587 | coro_sv = SvRV (coro_sv); |
|
|
1588 | |
|
|
1589 | coro = SvSTATE (coro_sv); |
1616 | coro = SvSTATE (coro_sv); |
1590 | |
1617 | |
1591 | if (coro->flags & CF_READY) |
1618 | if (coro->flags & CF_READY) |
1592 | return 0; |
1619 | return 0; |
1593 | |
1620 | |
… | |
… | |
1667 | } |
1694 | } |
1668 | } |
1695 | } |
1669 | else |
1696 | else |
1670 | { |
1697 | { |
1671 | /* nothing to schedule: call the idle handler */ |
1698 | /* nothing to schedule: call the idle handler */ |
|
|
1699 | if (SvROK (sv_idle) |
|
|
1700 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1701 | { |
|
|
1702 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1703 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1704 | --coro_nready; |
|
|
1705 | } |
|
|
1706 | else |
|
|
1707 | { |
1672 | dSP; |
1708 | dSP; |
1673 | |
1709 | |
1674 | ENTER; |
1710 | ENTER; |
1675 | SAVETMPS; |
1711 | SAVETMPS; |
1676 | |
1712 | |
1677 | PUSHMARK (SP); |
1713 | PUSHMARK (SP); |
1678 | PUTBACK; |
1714 | PUTBACK; |
1679 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1715 | call_sv (sv_idle, G_VOID | G_DISCARD); |
1680 | |
1716 | |
1681 | FREETMPS; |
1717 | FREETMPS; |
1682 | LEAVE; |
1718 | LEAVE; |
|
|
1719 | } |
1683 | } |
1720 | } |
1684 | } |
1721 | } |
1685 | } |
1722 | } |
1686 | |
1723 | |
1687 | INLINE void |
1724 | INLINE void |
… | |
… | |
1758 | static void |
1795 | static void |
1759 | api_trace (pTHX_ SV *coro_sv, int flags) |
1796 | api_trace (pTHX_ SV *coro_sv, int flags) |
1760 | { |
1797 | { |
1761 | struct coro *coro = SvSTATE (coro_sv); |
1798 | struct coro *coro = SvSTATE (coro_sv); |
1762 | |
1799 | |
|
|
1800 | if (coro->flags & CF_RUNNING) |
|
|
1801 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1802 | |
1763 | if (flags & CC_TRACE) |
1803 | if (flags & CC_TRACE) |
1764 | { |
1804 | { |
1765 | if (!coro->cctx) |
1805 | if (!coro->cctx) |
1766 | coro->cctx = cctx_new_run (); |
1806 | coro->cctx = cctx_new_run (); |
1767 | else if (!(coro->cctx->flags & CC_TRACE)) |
1807 | else if (!(coro->cctx->flags & CC_TRACE)) |
1768 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1808 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1769 | |
1809 | |
1770 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1810 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1771 | } |
1811 | } |
1772 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1812 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1773 | { |
1813 | { |
… | |
… | |
1829 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1869 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1830 | api_ready (aTHX_ sv_manager); |
1870 | api_ready (aTHX_ sv_manager); |
1831 | |
1871 | |
1832 | frame->prepare = prepare_schedule; |
1872 | frame->prepare = prepare_schedule; |
1833 | frame->check = slf_check_repeat; |
1873 | frame->check = slf_check_repeat; |
|
|
1874 | |
|
|
1875 | /* as a minor optimisation, we could unwind all stacks here */ |
|
|
1876 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
|
|
1877 | /*coro_unwind_stacks (aTHX);*/ |
1834 | } |
1878 | } |
1835 | |
1879 | |
1836 | /*****************************************************************************/ |
1880 | /*****************************************************************************/ |
1837 | /* async pool handler */ |
1881 | /* async pool handler */ |
1838 | |
1882 | |
… | |
… | |
2347 | SV **ary; |
2391 | SV **ary; |
2348 | |
2392 | |
2349 | /* unfortunately, building manually saves memory */ |
2393 | /* unfortunately, building manually saves memory */ |
2350 | Newx (ary, 2, SV *); |
2394 | Newx (ary, 2, SV *); |
2351 | AvALLOC (av) = ary; |
2395 | AvALLOC (av) = ary; |
|
|
2396 | #if PERL_VERSION_ATLEAST (5,10,0) |
2352 | /*AvARRAY (av) = ary;*/ |
2397 | AvARRAY (av) = ary; |
|
|
2398 | #else |
2353 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2399 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2400 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2401 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2402 | #endif |
2354 | AvMAX (av) = 1; |
2403 | AvMAX (av) = 1; |
2355 | AvFILLp (av) = 0; |
2404 | AvFILLp (av) = 0; |
2356 | ary [0] = newSViv (count); |
2405 | ary [0] = newSViv (count); |
2357 | |
2406 | |
2358 | return newRV_noinc ((SV *)av); |
2407 | return newRV_noinc ((SV *)av); |
… | |
… | |
2887 | SV * |
2936 | SV * |
2888 | clone (Coro::State coro) |
2937 | clone (Coro::State coro) |
2889 | CODE: |
2938 | CODE: |
2890 | { |
2939 | { |
2891 | #if CORO_CLONE |
2940 | #if CORO_CLONE |
2892 | struct coro *ncoro = coro_clone (coro); |
2941 | struct coro *ncoro = coro_clone (aTHX_ coro); |
2893 | MAGIC *mg; |
2942 | MAGIC *mg; |
2894 | /* TODO: too much duplication */ |
2943 | /* TODO: too much duplication */ |
2895 | ncoro->hv = newHV (); |
2944 | ncoro->hv = newHV (); |
2896 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
2945 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
2897 | mg->mg_flags |= MGf_DUP; |
2946 | mg->mg_flags |= MGf_DUP; |
… | |
… | |
3088 | { |
3137 | { |
3089 | int i; |
3138 | int i; |
3090 | |
3139 | |
3091 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3140 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3092 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3141 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3093 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3142 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3094 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3143 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3095 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3144 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3096 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3145 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3097 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3146 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3098 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3147 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3148 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3099 | |
3149 | |
3100 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3150 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3101 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3151 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3102 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3152 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3103 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3153 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |