ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.324 by root, Sat Nov 22 07:02:28 2008 UTC vs.
Revision 1.340 by root, Mon Dec 15 00:30:40 2008 UTC

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
175static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
177 177
178static AV *av_destroy; /* destruction queue */ 178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */ 179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
180 181
181static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
182static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
183static SV *rv_diehook; 184static SV *rv_diehook;
184static SV *rv_warnhook; 185static 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
282 284
283 /* async_pool */ 285 /* async_pool */
284 SV *saved_deffh; 286 SV *saved_deffh;
285 SV *invoke_cb; 287 SV *invoke_cb;
286 AV *invoke_av; 288 AV *invoke_av;
289
290 /* on_enter/on_leave */
291 AV *on_enter;
292 AV *on_leave;
287 293
288 /* linked list */ 294 /* linked list */
289 struct coro *next, *prev; 295 struct coro *next, *prev;
290}; 296};
291 297
350INLINE CV * 356INLINE CV *
351coro_sv_2cv (pTHX_ SV *sv) 357coro_sv_2cv (pTHX_ SV *sv)
352{ 358{
353 HV *st; 359 HV *st;
354 GV *gvp; 360 GV *gvp;
355 return sv_2cv (sv, &st, &gvp, 0); 361 CV *cv = sv_2cv (sv, &st, &gvp, 0);
362
363 if (!cv)
364 croak ("code reference expected");
365
366 return cv;
356} 367}
368
369/*****************************************************************************/
370/* magic glue */
371
372#define CORO_MAGIC_type_cv 26
373#define CORO_MAGIC_type_state PERL_MAGIC_ext
374
375#define CORO_MAGIC_NN(sv, type) \
376 (expect_true (SvMAGIC (sv)->mg_type == type) \
377 ? SvMAGIC (sv) \
378 : mg_find (sv, type))
379
380#define CORO_MAGIC(sv, type) \
381 (expect_true (SvMAGIC (sv)) \
382 ? CORO_MAGIC_NN (sv, type) \
383 : 0)
384
385#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
386#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
387
388INLINE struct coro *
389SvSTATE_ (pTHX_ SV *coro)
390{
391 HV *stash;
392 MAGIC *mg;
393
394 if (SvROK (coro))
395 coro = SvRV (coro);
396
397 if (expect_false (SvTYPE (coro) != SVt_PVHV))
398 croak ("Coro::State object required");
399
400 stash = SvSTASH (coro);
401 if (expect_false (stash != coro_stash && stash != coro_state_stash))
402 {
403 /* very slow, but rare, check */
404 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
405 croak ("Coro::State object required");
406 }
407
408 mg = CORO_MAGIC_state (coro);
409 return (struct coro *)mg->mg_ptr;
410}
411
412#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
413
414/* faster than SvSTATE, but expects a coroutine hv */
415#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
416#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
417
418/*****************************************************************************/
419/* padlist management and caching */
357 420
358static AV * 421static AV *
359coro_derive_padlist (pTHX_ CV *cv) 422coro_derive_padlist (pTHX_ CV *cv)
360{ 423{
361 AV *padlist = CvPADLIST (cv); 424 AV *padlist = CvPADLIST (cv);
369 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 432 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
370#endif 433#endif
371 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 434 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
372 --AvFILLp (padlist); 435 --AvFILLp (padlist);
373 436
374 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 437 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
375 av_store (newpadlist, 1, (SV *)newpad); 438 av_store (newpadlist, 1, (SV *)newpad);
376 439
377 return newpadlist; 440 return newpadlist;
378} 441}
379 442
380static void 443static void
381free_padlist (pTHX_ AV *padlist) 444free_padlist (pTHX_ AV *padlist)
382{ 445{
383 /* may be during global destruction */ 446 /* may be during global destruction */
384 if (SvREFCNT (padlist)) 447 if (!IN_DESTRUCT)
385 { 448 {
386 I32 i = AvFILLp (padlist); 449 I32 i = AvFILLp (padlist);
387 while (i >= 0) 450
451 while (i > 0) /* special-case index 0 */
388 { 452 {
389 SV **svp = av_fetch (padlist, i--, FALSE); 453 /* we try to be extra-careful here */
390 if (svp) 454 AV *av = (AV *)AvARRAY (padlist)[i--];
391 { 455 I32 j = AvFILLp (av);
392 SV *sv; 456
393 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 457 while (j >= 0)
458 SvREFCNT_dec (AvARRAY (av)[j--]);
459
460 AvFILLp (av) = -1;
394 SvREFCNT_dec (sv); 461 SvREFCNT_dec (av);
395
396 SvREFCNT_dec (*svp);
397 }
398 } 462 }
399 463
464 SvREFCNT_dec (AvARRAY (padlist)[0]);
465
466 AvFILLp (padlist) = -1;
400 SvREFCNT_dec ((SV*)padlist); 467 SvREFCNT_dec ((SV*)padlist);
401 } 468 }
402} 469}
403 470
404static int 471static int
413 480
414 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 481 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
415 482
416 return 0; 483 return 0;
417} 484}
418
419#define CORO_MAGIC_type_cv 26
420#define CORO_MAGIC_type_state PERL_MAGIC_ext
421 485
422static MGVTBL coro_cv_vtbl = { 486static MGVTBL coro_cv_vtbl = {
423 0, 0, 0, 0, 487 0, 0, 0, 0,
424 coro_cv_free 488 coro_cv_free
425}; 489};
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
440INLINE struct coro *
441SvSTATE_ (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 490
470/* the next two functions merely cache the padlists */ 491/* the next two functions merely cache the padlists */
471static void 492static void
472get_padlist (pTHX_ CV *cv) 493get_padlist (pTHX_ CV *cv)
473{ 494{
501 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 522 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
502 523
503 av = (AV *)mg->mg_obj; 524 av = (AV *)mg->mg_obj;
504 525
505 if (expect_false (AvFILLp (av) >= AvMAX (av))) 526 if (expect_false (AvFILLp (av) >= AvMAX (av)))
506 av_extend (av, AvMAX (av) + 1); 527 av_extend (av, AvFILLp (av) + 1);
507 528
508 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 529 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
509} 530}
510 531
511/** load & save, init *******************************************************/ 532/** load & save, init *******************************************************/
533
534static void
535on_enterleave_call (pTHX_ SV *cb);
512 536
513static void 537static void
514load_perl (pTHX_ Coro__State c) 538load_perl (pTHX_ Coro__State c)
515{ 539{
516 perl_slots *slot = c->slot; 540 perl_slots *slot = c->slot;
517 c->slot = 0; 541 c->slot = 0;
518 542
519 PL_mainstack = c->mainstack; 543 PL_mainstack = c->mainstack;
520 544
521 GvSV (PL_defgv) = slot->defsv; 545 GvSV (PL_defgv) = slot->defsv;
522 GvAV (PL_defgv) = slot->defav; 546 GvAV (PL_defgv) = slot->defav;
523 GvSV (PL_errgv) = slot->errsv; 547 GvSV (PL_errgv) = slot->errsv;
524 GvSV (irsgv) = slot->irsgv; 548 GvSV (irsgv) = slot->irsgv;
549 GvHV (PL_hintgv) = slot->hinthv;
525 550
526 #define VAR(name,type) PL_ ## name = slot->name; 551 #define VAR(name,type) PL_ ## name = slot->name;
527 # include "state.h" 552 # include "state.h"
528 #undef VAR 553 #undef VAR
529 554
543 PUTBACK; 568 PUTBACK;
544 } 569 }
545 570
546 slf_frame = c->slf_frame; 571 slf_frame = c->slf_frame;
547 CORO_THROW = c->except; 572 CORO_THROW = c->except;
573
574 if (expect_false (c->on_enter))
575 {
576 int i;
577
578 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
579 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
580 }
548} 581}
549 582
550static void 583static void
551save_perl (pTHX_ Coro__State c) 584save_perl (pTHX_ Coro__State c)
552{ 585{
586 if (expect_false (c->on_leave))
587 {
588 int i;
589
590 for (i = AvFILLp (c->on_leave); i >= 0; --i)
591 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
592 }
593
553 c->except = CORO_THROW; 594 c->except = CORO_THROW;
554 c->slf_frame = slf_frame; 595 c->slf_frame = slf_frame;
555 596
556 { 597 {
557 dSP; 598 dSP;
615 c->mainstack = PL_mainstack; 656 c->mainstack = PL_mainstack;
616 657
617 { 658 {
618 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 659 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
619 660
620 slot->defav = GvAV (PL_defgv); 661 slot->defav = GvAV (PL_defgv);
621 slot->defsv = DEFSV; 662 slot->defsv = DEFSV;
622 slot->errsv = ERRSV; 663 slot->errsv = ERRSV;
623 slot->irsgv = GvSV (irsgv); 664 slot->irsgv = GvSV (irsgv);
665 slot->hinthv = GvHV (PL_hintgv);
624 666
625 #define VAR(name,type) slot->name = PL_ ## name; 667 #define VAR(name,type) slot->name = PL_ ## name;
626 # include "state.h" 668 # include "state.h"
627 #undef VAR 669 #undef VAR
628 } 670 }
755#endif 797#endif
756 798
757/* 799/*
758 * This overrides the default magic get method of %SIG elements. 800 * This overrides the default magic get method of %SIG elements.
759 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 801 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
760 * and instead of tryign to save and restore the hash elements, we just provide 802 * and instead of trying to save and restore the hash elements, we just provide
761 * readback here. 803 * readback here.
762 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
763 * not expecting this to actually change the hook. This is a potential problem
764 * when a schedule happens then, but we ignore this.
765 */ 804 */
766static int 805static int
767coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 806coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
768{ 807{
769 const char *s = MgPV_nolen_const (mg); 808 const char *s = MgPV_nolen_const (mg);
822 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 861 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
823 862
824 if (svp) 863 if (svp)
825 { 864 {
826 SV *old = *svp; 865 SV *old = *svp;
827 *svp = newSVsv (sv); 866 *svp = SvOK (sv) ? newSVsv (sv) : 0;
828 SvREFCNT_dec (old); 867 SvREFCNT_dec (old);
829 return 0; 868 return 0;
830 } 869 }
831 } 870 }
832 871
864 903
865 PL_runops = RUNOPS_DEFAULT; 904 PL_runops = RUNOPS_DEFAULT;
866 PL_curcop = &PL_compiling; 905 PL_curcop = &PL_compiling;
867 PL_in_eval = EVAL_NULL; 906 PL_in_eval = EVAL_NULL;
868 PL_comppad = 0; 907 PL_comppad = 0;
908 PL_comppad_name = 0;
909 PL_comppad_name_fill = 0;
910 PL_comppad_name_floor = 0;
869 PL_curpm = 0; 911 PL_curpm = 0;
870 PL_curpad = 0; 912 PL_curpad = 0;
871 PL_localizing = 0; 913 PL_localizing = 0;
872 PL_dirty = 0; 914 PL_dirty = 0;
873 PL_restartop = 0; 915 PL_restartop = 0;
874#if PERL_VERSION_ATLEAST (5,10,0) 916#if PERL_VERSION_ATLEAST (5,10,0)
875 PL_parser = 0; 917 PL_parser = 0;
876#endif 918#endif
919 PL_hints = 0;
877 920
878 /* recreate the die/warn hooks */ 921 /* recreate the die/warn hooks */
879 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 922 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); 923 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
881 924
882 GvSV (PL_defgv) = newSV (0); 925 GvSV (PL_defgv) = newSV (0);
883 GvAV (PL_defgv) = coro->args; coro->args = 0; 926 GvAV (PL_defgv) = coro->args; coro->args = 0;
884 GvSV (PL_errgv) = newSV (0); 927 GvSV (PL_errgv) = newSV (0);
885 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 928 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
929 GvHV (PL_hintgv) = 0;
886 PL_rs = newSVsv (GvSV (irsgv)); 930 PL_rs = newSVsv (GvSV (irsgv));
887 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 931 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
888 932
889 { 933 {
890 dSP; 934 dSP;
919 /* copy throw, in case it was set before coro_setup */ 963 /* copy throw, in case it was set before coro_setup */
920 CORO_THROW = coro->except; 964 CORO_THROW = coro->except;
921} 965}
922 966
923static void 967static void
924coro_destruct (pTHX_ struct coro *coro) 968coro_unwind_stacks (pTHX)
925{ 969{
926 if (!IN_DESTRUCT) 970 if (!IN_DESTRUCT)
927 { 971 {
928 /* restore all saved variables and stuff */ 972 /* restore all saved variables and stuff */
929 LEAVE_SCOPE (0); 973 LEAVE_SCOPE (0);
937 POPSTACK_TO (PL_mainstack); 981 POPSTACK_TO (PL_mainstack);
938 982
939 /* unwind main stack */ 983 /* unwind main stack */
940 dounwind (-1); 984 dounwind (-1);
941 } 985 }
986}
987
988static void
989coro_destruct_perl (pTHX_ struct coro *coro)
990{
991 coro_unwind_stacks (aTHX);
942 992
943 SvREFCNT_dec (GvSV (PL_defgv)); 993 SvREFCNT_dec (GvSV (PL_defgv));
944 SvREFCNT_dec (GvAV (PL_defgv)); 994 SvREFCNT_dec (GvAV (PL_defgv));
945 SvREFCNT_dec (GvSV (PL_errgv)); 995 SvREFCNT_dec (GvSV (PL_errgv));
946 SvREFCNT_dec (PL_defoutgv); 996 SvREFCNT_dec (PL_defoutgv);
947 SvREFCNT_dec (PL_rs); 997 SvREFCNT_dec (PL_rs);
948 SvREFCNT_dec (GvSV (irsgv)); 998 SvREFCNT_dec (GvSV (irsgv));
999 SvREFCNT_dec (GvHV (PL_hintgv));
949 1000
950 SvREFCNT_dec (PL_diehook); 1001 SvREFCNT_dec (PL_diehook);
951 SvREFCNT_dec (PL_warnhook); 1002 SvREFCNT_dec (PL_warnhook);
952 1003
953 SvREFCNT_dec (coro->saved_deffh); 1004 SvREFCNT_dec (coro->saved_deffh);
1168 /* 1219 /*
1169 * If perl-run returns we assume exit() was being called or the coro 1220 * 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) 1221 * 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 1222 * 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" 1223 * bootstrap-time "top" top_env, as we cannot restore the "main"
1173 * coroutine as Coro has no such concept 1224 * coroutine as Coro has no such concept.
1225 * This actually isn't valid with the pthread backend, but OSes requiring
1226 * that backend are too broken to do it in a standards-compliant way.
1174 */ 1227 */
1175 PL_top_env = main_top_env; 1228 PL_top_env = main_top_env;
1176 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1229 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1177 } 1230 }
1178} 1231}
1255cctx_destroy (coro_cctx *cctx) 1308cctx_destroy (coro_cctx *cctx)
1256{ 1309{
1257 if (!cctx) 1310 if (!cctx)
1258 return; 1311 return;
1259 1312
1260 assert (cctx != cctx_current);//D temporary 1313 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1261 1314
1262 --cctx_count; 1315 --cctx_count;
1263 coro_destroy (&cctx->cctx); 1316 coro_destroy (&cctx->cctx);
1264 1317
1265 /* coro_transfer creates new, empty cctx's */ 1318 /* coro_transfer creates new, empty cctx's */
1329 /* TODO: throwing up here is considered harmful */ 1382 /* TODO: throwing up here is considered harmful */
1330 1383
1331 if (expect_true (prev != next)) 1384 if (expect_true (prev != next))
1332 { 1385 {
1333 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1386 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,"); 1387 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1335 1388
1336 if (expect_false (next->flags & CF_RUNNING)) 1389 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,"); 1390 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1338 1391
1339 if (expect_false (next->flags & CF_DESTROYED)) 1392 if (expect_false (next->flags & CF_DESTROYED))
1383 coro_setup (aTHX_ next); 1436 coro_setup (aTHX_ next);
1384 } 1437 }
1385 else 1438 else
1386 load_perl (aTHX_ next); 1439 load_perl (aTHX_ next);
1387 1440
1388 assert (!prev->cctx);//D temporary
1389
1390 /* possibly untie and reuse the cctx */ 1441 /* possibly untie and reuse the cctx */
1391 if (expect_true ( 1442 if (expect_true (
1392 cctx_current->idle_sp == STACKLEVEL 1443 cctx_current->idle_sp == STACKLEVEL
1393 && !(cctx_current->flags & CC_TRACE) 1444 && !(cctx_current->flags & CC_TRACE)
1394 && !force_cctx 1445 && !force_cctx
1449 --coro_nready; 1500 --coro_nready;
1450 } 1501 }
1451 else 1502 else
1452 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1503 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1453 1504
1454 if (coro->mainstack && coro->mainstack != main_mainstack) 1505 if (coro->mainstack
1506 && coro->mainstack != main_mainstack
1507 && coro->slot
1508 && !PL_dirty)
1455 { 1509 {
1456 struct coro temp; 1510 struct coro *current = SvSTATE_current;
1457 1511
1458 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1512 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1459 1513
1460 save_perl (aTHX_ &temp); 1514 save_perl (aTHX_ current);
1461 load_perl (aTHX_ coro); 1515 load_perl (aTHX_ coro);
1462 1516
1463 coro_destruct (aTHX_ coro); 1517 coro_destruct_perl (aTHX_ coro);
1464 1518
1465 load_perl (aTHX_ &temp); 1519 load_perl (aTHX_ current);
1466 1520
1467 coro->slot = 0; 1521 coro->slot = 0;
1468 } 1522 }
1469 1523
1470 cctx_destroy (coro->cctx); 1524 cctx_destroy (coro->cctx);
1582{ 1636{
1583 struct coro *coro; 1637 struct coro *coro;
1584 SV *sv_hook; 1638 SV *sv_hook;
1585 void (*xs_hook)(void); 1639 void (*xs_hook)(void);
1586 1640
1587 if (SvROK (coro_sv))
1588 coro_sv = SvRV (coro_sv);
1589
1590 coro = SvSTATE (coro_sv); 1641 coro = SvSTATE (coro_sv);
1591 1642
1592 if (coro->flags & CF_READY) 1643 if (coro->flags & CF_READY)
1593 return 0; 1644 return 0;
1594 1645
1668 } 1719 }
1669 } 1720 }
1670 else 1721 else
1671 { 1722 {
1672 /* nothing to schedule: call the idle handler */ 1723 /* nothing to schedule: call the idle handler */
1724 if (SvROK (sv_idle)
1725 && SvOBJECT (SvRV (sv_idle)))
1726 {
1727 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1728 api_ready (aTHX_ SvRV (sv_idle));
1729 --coro_nready;
1730 }
1731 else
1732 {
1673 dSP; 1733 dSP;
1674 1734
1675 ENTER; 1735 ENTER;
1676 SAVETMPS; 1736 SAVETMPS;
1677 1737
1678 PUSHMARK (SP); 1738 PUSHMARK (SP);
1679 PUTBACK; 1739 PUTBACK;
1680 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1740 call_sv (sv_idle, G_VOID | G_DISCARD);
1681 1741
1682 FREETMPS; 1742 FREETMPS;
1683 LEAVE; 1743 LEAVE;
1744 }
1684 } 1745 }
1685 } 1746 }
1686} 1747}
1687 1748
1688INLINE void 1749INLINE void
1833 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1894 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1834 api_ready (aTHX_ sv_manager); 1895 api_ready (aTHX_ sv_manager);
1835 1896
1836 frame->prepare = prepare_schedule; 1897 frame->prepare = prepare_schedule;
1837 frame->check = slf_check_repeat; 1898 frame->check = slf_check_repeat;
1899
1900 /* as a minor optimisation, we could unwind all stacks here */
1901 /* but that puts extra pressure on pp_slf, and is not worth much */
1902 /*coro_unwind_stacks (aTHX);*/
1838} 1903}
1839 1904
1840/*****************************************************************************/ 1905/*****************************************************************************/
1841/* async pool handler */ 1906/* async pool handler */
1842 1907
2261 2326
2262 PL_op->op_ppaddr = pp_slf; 2327 PL_op->op_ppaddr = pp_slf;
2263 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2328 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2264 2329
2265 PL_op = (OP *)&slf_restore; 2330 PL_op = (OP *)&slf_restore;
2331}
2332
2333/*****************************************************************************/
2334/* dynamic wind */
2335
2336static void
2337on_enterleave_call (pTHX_ SV *cb)
2338{
2339 dSP;
2340
2341 PUSHSTACK;
2342
2343 PUSHMARK (SP);
2344 PUTBACK;
2345 call_sv (cb, G_VOID | G_DISCARD);
2346 SPAGAIN;
2347
2348 POPSTACK;
2349}
2350
2351static SV *
2352coro_avp_pop_and_free (pTHX_ AV **avp)
2353{
2354 AV *av = *avp;
2355 SV *res = av_pop (av);
2356
2357 if (AvFILLp (av) < 0)
2358 {
2359 *avp = 0;
2360 SvREFCNT_dec (av);
2361 }
2362
2363 return res;
2364}
2365
2366static void
2367coro_pop_on_enter (pTHX_ void *coro)
2368{
2369 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2370 SvREFCNT_dec (cb);
2371}
2372
2373static void
2374coro_pop_on_leave (pTHX_ void *coro)
2375{
2376 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2377 on_enterleave_call (aTHX_ sv_2mortal (cb));
2266} 2378}
2267 2379
2268/*****************************************************************************/ 2380/*****************************************************************************/
2269/* PerlIO::cede */ 2381/* PerlIO::cede */
2270 2382
2351 SV **ary; 2463 SV **ary;
2352 2464
2353 /* unfortunately, building manually saves memory */ 2465 /* unfortunately, building manually saves memory */
2354 Newx (ary, 2, SV *); 2466 Newx (ary, 2, SV *);
2355 AvALLOC (av) = ary; 2467 AvALLOC (av) = ary;
2468#if PERL_VERSION_ATLEAST (5,10,0)
2356 /*AvARRAY (av) = ary;*/ 2469 AvARRAY (av) = ary;
2470#else
2357 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2471 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2472 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2473 SvPVX ((SV *)av) = (char *)ary;
2474#endif
2358 AvMAX (av) = 1; 2475 AvMAX (av) = 1;
2359 AvFILLp (av) = 0; 2476 AvFILLp (av) = 0;
2360 ary [0] = newSViv (count); 2477 ary [0] = newSViv (count);
2361 2478
2362 return newRV_noinc ((SV *)av); 2479 return newRV_noinc ((SV *)av);
2891SV * 3008SV *
2892clone (Coro::State coro) 3009clone (Coro::State coro)
2893 CODE: 3010 CODE:
2894{ 3011{
2895#if CORO_CLONE 3012#if CORO_CLONE
2896 struct coro *ncoro = coro_clone (coro); 3013 struct coro *ncoro = coro_clone (aTHX_ coro);
2897 MAGIC *mg; 3014 MAGIC *mg;
2898 /* TODO: too much duplication */ 3015 /* TODO: too much duplication */
2899 ncoro->hv = newHV (); 3016 ncoro->hv = newHV ();
2900 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3017 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2901 mg->mg_flags |= MGf_DUP; 3018 mg->mg_flags |= MGf_DUP;
2963 eval = 1 3080 eval = 1
2964 CODE: 3081 CODE:
2965{ 3082{
2966 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3083 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2967 { 3084 {
2968 struct coro temp; 3085 struct coro *current = SvSTATE_current;
2969 3086
2970 if (!(coro->flags & CF_RUNNING)) 3087 if (current != coro)
2971 { 3088 {
2972 PUTBACK; 3089 PUTBACK;
2973 save_perl (aTHX_ &temp); 3090 save_perl (aTHX_ current);
2974 load_perl (aTHX_ coro); 3091 load_perl (aTHX_ coro);
3092 SPAGAIN;
2975 } 3093 }
2976 3094
2977 {
2978 dSP;
2979 ENTER;
2980 SAVETMPS;
2981 PUTBACK;
2982 PUSHSTACK; 3095 PUSHSTACK;
3096
2983 PUSHMARK (SP); 3097 PUSHMARK (SP);
3098 PUTBACK;
2984 3099
2985 if (ix) 3100 if (ix)
2986 eval_sv (coderef, 0); 3101 eval_sv (coderef, 0);
2987 else 3102 else
2988 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3103 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2989 3104
3105 SPAGAIN;
2990 POPSTACK; 3106 POPSTACK;
2991 SPAGAIN;
2992 FREETMPS;
2993 LEAVE;
2994 PUTBACK;
2995 }
2996 3107
2997 if (!(coro->flags & CF_RUNNING)) 3108 if (current != coro)
2998 { 3109 {
3110 PUTBACK;
2999 save_perl (aTHX_ coro); 3111 save_perl (aTHX_ coro);
3000 load_perl (aTHX_ &temp); 3112 load_perl (aTHX_ current);
3001 SPAGAIN; 3113 SPAGAIN;
3002 } 3114 }
3003 } 3115 }
3004} 3116}
3005 3117
3092{ 3204{
3093 int i; 3205 int i;
3094 3206
3095 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3207 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3096 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3208 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3097 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3209 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3098 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3210 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3099 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3211 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3100 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3212 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3101 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3213 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3102 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3214 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3215 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3103 3216
3104 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3217 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3105 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3218 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3106 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3219 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3107 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3220 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3286rouse_wait (...) 3399rouse_wait (...)
3287 PROTOTYPE: ;$ 3400 PROTOTYPE: ;$
3288 PPCODE: 3401 PPCODE:
3289 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3402 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3290 3403
3404void
3405on_enter (SV *block)
3406 ALIAS:
3407 on_leave = 1
3408 PROTOTYPE: &
3409 CODE:
3410{
3411 struct coro *coro = SvSTATE_current;
3412 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3413
3414 block = (SV *)coro_sv_2cv (aTHX_ block);
3415
3416 if (!*avp)
3417 *avp = newAV ();
3418
3419 av_push (*avp, SvREFCNT_inc (block));
3420
3421 if (!ix)
3422 on_enterleave_call (aTHX_ block);
3423
3424 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3425 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3426 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3427}
3428
3291 3429
3292MODULE = Coro::State PACKAGE = PerlIO::cede 3430MODULE = Coro::State PACKAGE = PerlIO::cede
3293 3431
3294BOOT: 3432BOOT:
3295 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3433 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines