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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines