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.326 by root, Mon Nov 24 04:56:38 2008 UTC vs.
Revision 1.339 by root, Mon Dec 15 00:28:30 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
247{ 247{
248 SV *defsv; 248 SV *defsv;
249 AV *defav; 249 AV *defav;
250 SV *errsv; 250 SV *errsv;
251 SV *irsgv; 251 SV *irsgv;
252 HV *hinthv;
252#define VAR(name,type) type name; 253#define VAR(name,type) type name;
253# include "state.h" 254# include "state.h"
254#undef VAR 255#undef VAR
255} perl_slots; 256} perl_slots;
256 257
283 284
284 /* async_pool */ 285 /* async_pool */
285 SV *saved_deffh; 286 SV *saved_deffh;
286 SV *invoke_cb; 287 SV *invoke_cb;
287 AV *invoke_av; 288 AV *invoke_av;
289
290 /* on_enter/on_leave */
291 AV *on_enter;
292 AV *on_leave;
288 293
289 /* linked list */ 294 /* linked list */
290 struct coro *next, *prev; 295 struct coro *next, *prev;
291}; 296};
292 297
351INLINE CV * 356INLINE CV *
352coro_sv_2cv (pTHX_ SV *sv) 357coro_sv_2cv (pTHX_ SV *sv)
353{ 358{
354 HV *st; 359 HV *st;
355 GV *gvp; 360 GV *gvp;
356 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;
357} 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 */
358 420
359static AV * 421static AV *
360coro_derive_padlist (pTHX_ CV *cv) 422coro_derive_padlist (pTHX_ CV *cv)
361{ 423{
362 AV *padlist = CvPADLIST (cv); 424 AV *padlist = CvPADLIST (cv);
370 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 432 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
371#endif 433#endif
372 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 434 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
373 --AvFILLp (padlist); 435 --AvFILLp (padlist);
374 436
375 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 437 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
376 av_store (newpadlist, 1, (SV *)newpad); 438 av_store (newpadlist, 1, (SV *)newpad);
377 439
378 return newpadlist; 440 return newpadlist;
379} 441}
380 442
381static void 443static void
382free_padlist (pTHX_ AV *padlist) 444free_padlist (pTHX_ AV *padlist)
383{ 445{
384 /* may be during global destruction */ 446 /* may be during global destruction */
385 if (SvREFCNT (padlist)) 447 if (!IN_DESTRUCT)
386 { 448 {
387 I32 i = AvFILLp (padlist); 449 I32 i = AvFILLp (padlist);
388 while (i >= 0) 450
451 while (i > 0) /* special-case index 0 */
389 { 452 {
390 SV **svp = av_fetch (padlist, i--, FALSE); 453 /* we try to be extra-careful here */
391 if (svp) 454 AV *av = (AV *)AvARRAY (padlist)[i--];
392 { 455 I32 j = AvFILLp (av);
393 SV *sv; 456
394 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 457 while (j >= 0)
458 SvREFCNT_dec (AvARRAY (av)[j--]);
459
460 AvFILLp (av) = -1;
395 SvREFCNT_dec (sv); 461 SvREFCNT_dec (av);
396
397 SvREFCNT_dec (*svp);
398 }
399 } 462 }
400 463
464 SvREFCNT_dec (AvARRAY (padlist)[0]);
465
466 AvFILLp (padlist) = -1;
401 SvREFCNT_dec ((SV*)padlist); 467 SvREFCNT_dec ((SV*)padlist);
402 } 468 }
403} 469}
404 470
405static int 471static int
414 480
415 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 481 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
416 482
417 return 0; 483 return 0;
418} 484}
419
420#define CORO_MAGIC_type_cv 26
421#define CORO_MAGIC_type_state PERL_MAGIC_ext
422 485
423static MGVTBL coro_cv_vtbl = { 486static MGVTBL coro_cv_vtbl = {
424 0, 0, 0, 0, 487 0, 0, 0, 0,
425 coro_cv_free 488 coro_cv_free
426}; 489};
427
428#define CORO_MAGIC_NN(sv, type) \
429 (expect_true (SvMAGIC (sv)->mg_type == type) \
430 ? SvMAGIC (sv) \
431 : mg_find (sv, type))
432
433#define CORO_MAGIC(sv, type) \
434 (expect_true (SvMAGIC (sv)) \
435 ? CORO_MAGIC_NN (sv, type) \
436 : 0)
437
438#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
439#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
440
441INLINE struct coro *
442SvSTATE_ (pTHX_ SV *coro)
443{
444 HV *stash;
445 MAGIC *mg;
446
447 if (SvROK (coro))
448 coro = SvRV (coro);
449
450 if (expect_false (SvTYPE (coro) != SVt_PVHV))
451 croak ("Coro::State object required");
452
453 stash = SvSTASH (coro);
454 if (expect_false (stash != coro_stash && stash != coro_state_stash))
455 {
456 /* very slow, but rare, check */
457 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
458 croak ("Coro::State object required");
459 }
460
461 mg = CORO_MAGIC_state (coro);
462 return (struct coro *)mg->mg_ptr;
463}
464
465#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
466
467/* faster than SvSTATE, but expects a coroutine hv */
468#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
469#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
470 490
471/* the next two functions merely cache the padlists */ 491/* the next two functions merely cache the padlists */
472static void 492static void
473get_padlist (pTHX_ CV *cv) 493get_padlist (pTHX_ CV *cv)
474{ 494{
502 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);
503 523
504 av = (AV *)mg->mg_obj; 524 av = (AV *)mg->mg_obj;
505 525
506 if (expect_false (AvFILLp (av) >= AvMAX (av))) 526 if (expect_false (AvFILLp (av) >= AvMAX (av)))
507 av_extend (av, AvMAX (av) + 1); 527 av_extend (av, AvFILLp (av) + 1);
508 528
509 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 529 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
510} 530}
511 531
512/** load & save, init *******************************************************/ 532/** load & save, init *******************************************************/
533
534static void
535on_enterleave_call (pTHX_ SV *cb);
513 536
514static void 537static void
515load_perl (pTHX_ Coro__State c) 538load_perl (pTHX_ Coro__State c)
516{ 539{
517 perl_slots *slot = c->slot; 540 perl_slots *slot = c->slot;
518 c->slot = 0; 541 c->slot = 0;
519 542
520 PL_mainstack = c->mainstack; 543 PL_mainstack = c->mainstack;
521 544
522 GvSV (PL_defgv) = slot->defsv; 545 GvSV (PL_defgv) = slot->defsv;
523 GvAV (PL_defgv) = slot->defav; 546 GvAV (PL_defgv) = slot->defav;
524 GvSV (PL_errgv) = slot->errsv; 547 GvSV (PL_errgv) = slot->errsv;
525 GvSV (irsgv) = slot->irsgv; 548 GvSV (irsgv) = slot->irsgv;
549 GvHV (PL_hintgv) = slot->hinthv;
526 550
527 #define VAR(name,type) PL_ ## name = slot->name; 551 #define VAR(name,type) PL_ ## name = slot->name;
528 # include "state.h" 552 # include "state.h"
529 #undef VAR 553 #undef VAR
530 554
544 PUTBACK; 568 PUTBACK;
545 } 569 }
546 570
547 slf_frame = c->slf_frame; 571 slf_frame = c->slf_frame;
548 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 (AvARRAY (c->on_enter)[i]);
580 }
549} 581}
550 582
551static void 583static void
552save_perl (pTHX_ Coro__State c) 584save_perl (pTHX_ Coro__State c)
553{ 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 (AvARRAY (c->on_leave)[i]);
592 }
593
554 c->except = CORO_THROW; 594 c->except = CORO_THROW;
555 c->slf_frame = slf_frame; 595 c->slf_frame = slf_frame;
556 596
557 { 597 {
558 dSP; 598 dSP;
616 c->mainstack = PL_mainstack; 656 c->mainstack = PL_mainstack;
617 657
618 { 658 {
619 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 659 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
620 660
621 slot->defav = GvAV (PL_defgv); 661 slot->defav = GvAV (PL_defgv);
622 slot->defsv = DEFSV; 662 slot->defsv = DEFSV;
623 slot->errsv = ERRSV; 663 slot->errsv = ERRSV;
624 slot->irsgv = GvSV (irsgv); 664 slot->irsgv = GvSV (irsgv);
665 slot->hinthv = GvHV (PL_hintgv);
625 666
626 #define VAR(name,type) slot->name = PL_ ## name; 667 #define VAR(name,type) slot->name = PL_ ## name;
627 # include "state.h" 668 # include "state.h"
628 #undef VAR 669 #undef VAR
629 } 670 }
756#endif 797#endif
757 798
758/* 799/*
759 * This overrides the default magic get method of %SIG elements. 800 * This overrides the default magic get method of %SIG elements.
760 * 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
761 * 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
762 * readback here. 803 * readback here.
763 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
764 * not expecting this to actually change the hook. This is a potential problem
765 * when a schedule happens then, but we ignore this.
766 */ 804 */
767static int 805static int
768coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 806coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
769{ 807{
770 const char *s = MgPV_nolen_const (mg); 808 const char *s = MgPV_nolen_const (mg);
823 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 861 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
824 862
825 if (svp) 863 if (svp)
826 { 864 {
827 SV *old = *svp; 865 SV *old = *svp;
828 *svp = newSVsv (sv); 866 *svp = SvOK (sv) ? newSVsv (sv) : 0;
829 SvREFCNT_dec (old); 867 SvREFCNT_dec (old);
830 return 0; 868 return 0;
831 } 869 }
832 } 870 }
833 871
865 903
866 PL_runops = RUNOPS_DEFAULT; 904 PL_runops = RUNOPS_DEFAULT;
867 PL_curcop = &PL_compiling; 905 PL_curcop = &PL_compiling;
868 PL_in_eval = EVAL_NULL; 906 PL_in_eval = EVAL_NULL;
869 PL_comppad = 0; 907 PL_comppad = 0;
908 PL_comppad_name = 0;
909 PL_comppad_name_fill = 0;
910 PL_comppad_name_floor = 0;
870 PL_curpm = 0; 911 PL_curpm = 0;
871 PL_curpad = 0; 912 PL_curpad = 0;
872 PL_localizing = 0; 913 PL_localizing = 0;
873 PL_dirty = 0; 914 PL_dirty = 0;
874 PL_restartop = 0; 915 PL_restartop = 0;
875#if PERL_VERSION_ATLEAST (5,10,0) 916#if PERL_VERSION_ATLEAST (5,10,0)
876 PL_parser = 0; 917 PL_parser = 0;
877#endif 918#endif
919 PL_hints = 0;
878 920
879 /* recreate the die/warn hooks */ 921 /* recreate the die/warn hooks */
880 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 );
881 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);
882 924
883 GvSV (PL_defgv) = newSV (0); 925 GvSV (PL_defgv) = newSV (0);
884 GvAV (PL_defgv) = coro->args; coro->args = 0; 926 GvAV (PL_defgv) = coro->args; coro->args = 0;
885 GvSV (PL_errgv) = newSV (0); 927 GvSV (PL_errgv) = newSV (0);
886 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;
887 PL_rs = newSVsv (GvSV (irsgv)); 930 PL_rs = newSVsv (GvSV (irsgv));
888 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 931 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
889 932
890 { 933 {
891 dSP; 934 dSP;
920 /* copy throw, in case it was set before coro_setup */ 963 /* copy throw, in case it was set before coro_setup */
921 CORO_THROW = coro->except; 964 CORO_THROW = coro->except;
922} 965}
923 966
924static void 967static void
925coro_destruct (pTHX_ struct coro *coro) 968coro_unwind_stacks (pTHX)
926{ 969{
927 if (!IN_DESTRUCT) 970 if (!IN_DESTRUCT)
928 { 971 {
929 /* restore all saved variables and stuff */ 972 /* restore all saved variables and stuff */
930 LEAVE_SCOPE (0); 973 LEAVE_SCOPE (0);
938 POPSTACK_TO (PL_mainstack); 981 POPSTACK_TO (PL_mainstack);
939 982
940 /* unwind main stack */ 983 /* unwind main stack */
941 dounwind (-1); 984 dounwind (-1);
942 } 985 }
986}
987
988static void
989coro_destruct_perl (pTHX_ struct coro *coro)
990{
991 coro_unwind_stacks (aTHX);
943 992
944 SvREFCNT_dec (GvSV (PL_defgv)); 993 SvREFCNT_dec (GvSV (PL_defgv));
945 SvREFCNT_dec (GvAV (PL_defgv)); 994 SvREFCNT_dec (GvAV (PL_defgv));
946 SvREFCNT_dec (GvSV (PL_errgv)); 995 SvREFCNT_dec (GvSV (PL_errgv));
947 SvREFCNT_dec (PL_defoutgv); 996 SvREFCNT_dec (PL_defoutgv);
948 SvREFCNT_dec (PL_rs); 997 SvREFCNT_dec (PL_rs);
949 SvREFCNT_dec (GvSV (irsgv)); 998 SvREFCNT_dec (GvSV (irsgv));
999 SvREFCNT_dec (GvHV (PL_hintgv));
950 1000
951 SvREFCNT_dec (PL_diehook); 1001 SvREFCNT_dec (PL_diehook);
952 SvREFCNT_dec (PL_warnhook); 1002 SvREFCNT_dec (PL_warnhook);
953 1003
954 SvREFCNT_dec (coro->saved_deffh); 1004 SvREFCNT_dec (coro->saved_deffh);
1169 /* 1219 /*
1170 * 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
1171 * 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)
1172 * 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
1173 * bootstrap-time "top" top_env, as we cannot restore the "main" 1223 * bootstrap-time "top" top_env, as we cannot restore the "main"
1174 * 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.
1175 */ 1227 */
1176 PL_top_env = main_top_env; 1228 PL_top_env = main_top_env;
1177 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 */
1178 } 1230 }
1179} 1231}
1256cctx_destroy (coro_cctx *cctx) 1308cctx_destroy (coro_cctx *cctx)
1257{ 1309{
1258 if (!cctx) 1310 if (!cctx)
1259 return; 1311 return;
1260 1312
1261 assert (cctx != cctx_current);//D temporary 1313 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1262 1314
1263 --cctx_count; 1315 --cctx_count;
1264 coro_destroy (&cctx->cctx); 1316 coro_destroy (&cctx->cctx);
1265 1317
1266 /* coro_transfer creates new, empty cctx's */ 1318 /* coro_transfer creates new, empty cctx's */
1384 coro_setup (aTHX_ next); 1436 coro_setup (aTHX_ next);
1385 } 1437 }
1386 else 1438 else
1387 load_perl (aTHX_ next); 1439 load_perl (aTHX_ next);
1388 1440
1389 assert (!prev->cctx);//D temporary
1390
1391 /* possibly untie and reuse the cctx */ 1441 /* possibly untie and reuse the cctx */
1392 if (expect_true ( 1442 if (expect_true (
1393 cctx_current->idle_sp == STACKLEVEL 1443 cctx_current->idle_sp == STACKLEVEL
1394 && !(cctx_current->flags & CC_TRACE) 1444 && !(cctx_current->flags & CC_TRACE)
1395 && !force_cctx 1445 && !force_cctx
1450 --coro_nready; 1500 --coro_nready;
1451 } 1501 }
1452 else 1502 else
1453 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 */
1454 1504
1455 if (coro->mainstack && coro->mainstack != main_mainstack) 1505 if (coro->mainstack
1506 && coro->mainstack != main_mainstack
1507 && coro->slot
1508 && !PL_dirty)
1456 { 1509 {
1457 struct coro temp; 1510 struct coro *current = SvSTATE_current;
1458 1511
1459 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));
1460 1513
1461 save_perl (aTHX_ &temp); 1514 save_perl (aTHX_ current);
1462 load_perl (aTHX_ coro); 1515 load_perl (aTHX_ coro);
1463 1516
1464 coro_destruct (aTHX_ coro); 1517 coro_destruct_perl (aTHX_ coro);
1465 1518
1466 load_perl (aTHX_ &temp); 1519 load_perl (aTHX_ current);
1467 1520
1468 coro->slot = 0; 1521 coro->slot = 0;
1469 } 1522 }
1470 1523
1471 cctx_destroy (coro->cctx); 1524 cctx_destroy (coro->cctx);
1670 /* nothing to schedule: call the idle handler */ 1723 /* nothing to schedule: call the idle handler */
1671 if (SvROK (sv_idle) 1724 if (SvROK (sv_idle)
1672 && SvOBJECT (SvRV (sv_idle))) 1725 && SvOBJECT (SvRV (sv_idle)))
1673 { 1726 {
1674 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1727 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1675 api_ready (SvRV (sv_idle)); 1728 api_ready (aTHX_ SvRV (sv_idle));
1676 --coro_nready; 1729 --coro_nready;
1677 } 1730 }
1678 else 1731 else
1679 { 1732 {
1680 dSP; 1733 dSP;
1841 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 */
1842 api_ready (aTHX_ sv_manager); 1895 api_ready (aTHX_ sv_manager);
1843 1896
1844 frame->prepare = prepare_schedule; 1897 frame->prepare = prepare_schedule;
1845 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);*/
1846} 1903}
1847 1904
1848/*****************************************************************************/ 1905/*****************************************************************************/
1849/* async pool handler */ 1906/* async pool handler */
1850 1907
2269 2326
2270 PL_op->op_ppaddr = pp_slf; 2327 PL_op->op_ppaddr = pp_slf;
2271 /*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 */
2272 2329
2273 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 (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 (&((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 (&((struct coro *)coro)->on_leave);
2377 on_enterleave_call (sv_2mortal (cb));
2274} 2378}
2275 2379
2276/*****************************************************************************/ 2380/*****************************************************************************/
2277/* PerlIO::cede */ 2381/* PerlIO::cede */
2278 2382
2359 SV **ary; 2463 SV **ary;
2360 2464
2361 /* unfortunately, building manually saves memory */ 2465 /* unfortunately, building manually saves memory */
2362 Newx (ary, 2, SV *); 2466 Newx (ary, 2, SV *);
2363 AvALLOC (av) = ary; 2467 AvALLOC (av) = ary;
2468#if PERL_VERSION_ATLEAST (5,10,0)
2364 /*AvARRAY (av) = ary;*/ 2469 AvARRAY (av) = ary;
2470#else
2365 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
2366 AvMAX (av) = 1; 2475 AvMAX (av) = 1;
2367 AvFILLp (av) = 0; 2476 AvFILLp (av) = 0;
2368 ary [0] = newSViv (count); 2477 ary [0] = newSViv (count);
2369 2478
2370 return newRV_noinc ((SV *)av); 2479 return newRV_noinc ((SV *)av);
2899SV * 3008SV *
2900clone (Coro::State coro) 3009clone (Coro::State coro)
2901 CODE: 3010 CODE:
2902{ 3011{
2903#if CORO_CLONE 3012#if CORO_CLONE
2904 struct coro *ncoro = coro_clone (coro); 3013 struct coro *ncoro = coro_clone (aTHX_ coro);
2905 MAGIC *mg; 3014 MAGIC *mg;
2906 /* TODO: too much duplication */ 3015 /* TODO: too much duplication */
2907 ncoro->hv = newHV (); 3016 ncoro->hv = newHV ();
2908 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);
2909 mg->mg_flags |= MGf_DUP; 3018 mg->mg_flags |= MGf_DUP;
2971 eval = 1 3080 eval = 1
2972 CODE: 3081 CODE:
2973{ 3082{
2974 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3083 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2975 { 3084 {
2976 struct coro temp; 3085 struct coro *current = SvSTATE_current;
2977 3086
2978 if (!(coro->flags & CF_RUNNING)) 3087 if (current != coro)
2979 { 3088 {
2980 PUTBACK; 3089 PUTBACK;
2981 save_perl (aTHX_ &temp); 3090 save_perl (aTHX_ current);
2982 load_perl (aTHX_ coro); 3091 load_perl (aTHX_ coro);
3092 SPAGAIN;
2983 } 3093 }
2984 3094
2985 {
2986 dSP;
2987 ENTER;
2988 SAVETMPS;
2989 PUTBACK;
2990 PUSHSTACK; 3095 PUSHSTACK;
3096
2991 PUSHMARK (SP); 3097 PUSHMARK (SP);
3098 PUTBACK;
2992 3099
2993 if (ix) 3100 if (ix)
2994 eval_sv (coderef, 0); 3101 eval_sv (coderef, 0);
2995 else 3102 else
2996 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3103 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2997 3104
3105 SPAGAIN;
2998 POPSTACK; 3106 POPSTACK;
2999 SPAGAIN;
3000 FREETMPS;
3001 LEAVE;
3002 PUTBACK;
3003 }
3004 3107
3005 if (!(coro->flags & CF_RUNNING)) 3108 if (current != coro)
3006 { 3109 {
3110 PUTBACK;
3007 save_perl (aTHX_ coro); 3111 save_perl (aTHX_ coro);
3008 load_perl (aTHX_ &temp); 3112 load_perl (aTHX_ current);
3009 SPAGAIN; 3113 SPAGAIN;
3010 } 3114 }
3011 } 3115 }
3012} 3116}
3013 3117
3100{ 3204{
3101 int i; 3205 int i;
3102 3206
3103 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3207 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3104 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3208 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3105 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3209 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3106 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3210 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3107 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);
3108 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3212 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3109 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3213 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3110 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3214 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3295rouse_wait (...) 3399rouse_wait (...)
3296 PROTOTYPE: ;$ 3400 PROTOTYPE: ;$
3297 PPCODE: 3401 PPCODE:
3298 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3402 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3299 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 (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
3300 3429
3301MODULE = Coro::State PACKAGE = PerlIO::cede 3430MODULE = Coro::State PACKAGE = PerlIO::cede
3302 3431
3303BOOT: 3432BOOT:
3304 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3433 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines