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.312 by root, Thu Nov 20 03:10:30 2008 UTC vs.
Revision 1.338 by root, Sun Dec 7 15:33:21 2008 UTC

98# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
99#endif 99#endif
100#ifndef CvISXSUB_on 100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv 101# define CvISXSUB_on(cv) (void)cv
102#endif 102#endif
103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
103 109
104/* 5.8.7 */ 110/* 5.8.7 */
105#ifndef SvRV_set 111#ifndef SvRV_set
106# define SvRV_set(s,v) SvRV(s) = (v) 112# define SvRV_set(s,v) SvRV(s) = (v)
107#endif 113#endif
127#else 133#else
128# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
129# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
130#endif 136#endif
131 137
132#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
133 139
134#if __GNUC__ >= 3 140#if __GNUC__ >= 3
135# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
136# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr),(value))
137# define INLINE static inline 143# define INLINE static inline
167static AV *main_mainstack; /* used to differentiate between $main and others */ 173static AV *main_mainstack; /* used to differentiate between $main and others */
168static JMPENV *main_top_env; 174static JMPENV *main_top_env;
169static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
170static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
171 177
178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
181
172static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
173static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
174static SV *rv_diehook; 184static SV *rv_diehook;
175static SV *rv_warnhook; 185static SV *rv_warnhook;
176static HV *hv_sig; /* %SIG */ 186static HV *hv_sig; /* %SIG */
177 187
178/* async_pool helper stuff */ 188/* async_pool helper stuff */
179static SV *sv_pool_rss; 189static SV *sv_pool_rss;
180static SV *sv_pool_size; 190static SV *sv_pool_size;
181static SV *sv_async_pool_idle; 191static SV *sv_async_pool_idle; /* description string */
182static AV *av_async_pool; 192static AV *av_async_pool; /* idle pool */
183static SV *sv_Coro; 193static SV *sv_Coro; /* class string */
184static CV *cv_pool_handler; 194static CV *cv_pool_handler;
185static CV *cv_coro_new; 195static CV *cv_coro_state_new;
186 196
187/* Coro::AnyEvent */ 197/* Coro::AnyEvent */
188static SV *sv_activity; 198static SV *sv_activity;
189 199
190static struct coro_cctx *cctx_first; 200static struct coro_cctx *cctx_first;
219 int valgrind_id; 229 int valgrind_id;
220#endif 230#endif
221 unsigned char flags; 231 unsigned char flags;
222} coro_cctx; 232} coro_cctx;
223 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
224enum { 238enum {
225 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
226 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
227 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
228 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
233{ 247{
234 SV *defsv; 248 SV *defsv;
235 AV *defav; 249 AV *defav;
236 SV *errsv; 250 SV *errsv;
237 SV *irsgv; 251 SV *irsgv;
252 HV *hinthv;
238#define VAR(name,type) type name; 253#define VAR(name,type) type name;
239# include "state.h" 254# include "state.h"
240#undef VAR 255#undef VAR
241} perl_slots; 256} perl_slots;
242 257
340 HV *st; 355 HV *st;
341 GV *gvp; 356 GV *gvp;
342 return sv_2cv (sv, &st, &gvp, 0); 357 return sv_2cv (sv, &st, &gvp, 0);
343} 358}
344 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
379INLINE struct coro *
380SvSTATE_ (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
345static AV * 412static AV *
346coro_clone_padlist (pTHX_ CV *cv) 413coro_derive_padlist (pTHX_ CV *cv)
347{ 414{
348 AV *padlist = CvPADLIST (cv); 415 AV *padlist = CvPADLIST (cv);
349 AV *newpadlist, *newpad; 416 AV *newpadlist, *newpad;
350 417
351 newpadlist = newAV (); 418 newpadlist = newAV ();
356 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 423 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
357#endif 424#endif
358 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 425 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
359 --AvFILLp (padlist); 426 --AvFILLp (padlist);
360 427
361 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 428 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
362 av_store (newpadlist, 1, (SV *)newpad); 429 av_store (newpadlist, 1, (SV *)newpad);
363 430
364 return newpadlist; 431 return newpadlist;
365} 432}
366 433
367static void 434static void
368free_padlist (pTHX_ AV *padlist) 435free_padlist (pTHX_ AV *padlist)
369{ 436{
370 /* may be during global destruction */ 437 /* may be during global destruction */
371 if (SvREFCNT (padlist)) 438 if (!IN_DESTRUCT)
372 { 439 {
373 I32 i = AvFILLp (padlist); 440 I32 i = AvFILLp (padlist);
374 while (i >= 0) 441
442 while (i > 0) /* special-case index 0 */
375 { 443 {
376 SV **svp = av_fetch (padlist, i--, FALSE); 444 /* we try to be extra-careful here */
377 if (svp) 445 AV *av = (AV *)AvARRAY (padlist)[i--];
378 { 446 I32 j = AvFILLp (av);
379 SV *sv; 447
380 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 448 while (j >= 0)
449 SvREFCNT_dec (AvARRAY (av)[j--]);
450
451 AvFILLp (av) = -1;
381 SvREFCNT_dec (sv); 452 SvREFCNT_dec (av);
382
383 SvREFCNT_dec (*svp);
384 }
385 } 453 }
386 454
455 SvREFCNT_dec (AvARRAY (padlist)[0]);
456
457 AvFILLp (padlist) = -1;
387 SvREFCNT_dec ((SV*)padlist); 458 SvREFCNT_dec ((SV*)padlist);
388 } 459 }
389} 460}
390 461
391static int 462static int
400 471
401 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 472 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
402 473
403 return 0; 474 return 0;
404} 475}
405
406#define CORO_MAGIC_type_cv 26
407#define CORO_MAGIC_type_state PERL_MAGIC_ext
408 476
409static MGVTBL coro_cv_vtbl = { 477static MGVTBL coro_cv_vtbl = {
410 0, 0, 0, 0, 478 0, 0, 0, 0,
411 coro_cv_free 479 coro_cv_free
412}; 480};
413
414#define CORO_MAGIC_NN(sv, type) \
415 (expect_true (SvMAGIC (sv)->mg_type == type) \
416 ? SvMAGIC (sv) \
417 : mg_find (sv, type))
418
419#define CORO_MAGIC(sv, type) \
420 (expect_true (SvMAGIC (sv)) \
421 ? CORO_MAGIC_NN (sv, type) \
422 : 0)
423
424#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
425#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
426
427INLINE struct coro *
428SvSTATE_ (pTHX_ SV *coro)
429{
430 HV *stash;
431 MAGIC *mg;
432
433 if (SvROK (coro))
434 coro = SvRV (coro);
435
436 if (expect_false (SvTYPE (coro) != SVt_PVHV))
437 croak ("Coro::State object required");
438
439 stash = SvSTASH (coro);
440 if (expect_false (stash != coro_stash && stash != coro_state_stash))
441 {
442 /* very slow, but rare, check */
443 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
444 croak ("Coro::State object required");
445 }
446
447 mg = CORO_MAGIC_state (coro);
448 return (struct coro *)mg->mg_ptr;
449}
450
451#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
452
453/* faster than SvSTATE, but expects a coroutine hv */
454#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
455#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
456 481
457/* the next two functions merely cache the padlists */ 482/* the next two functions merely cache the padlists */
458static void 483static void
459get_padlist (pTHX_ CV *cv) 484get_padlist (pTHX_ CV *cv)
460{ 485{
471 CV *cp = Perl_cv_clone (aTHX_ cv); 496 CV *cp = Perl_cv_clone (aTHX_ cv);
472 CvPADLIST (cv) = CvPADLIST (cp); 497 CvPADLIST (cv) = CvPADLIST (cp);
473 CvPADLIST (cp) = 0; 498 CvPADLIST (cp) = 0;
474 SvREFCNT_dec (cp); 499 SvREFCNT_dec (cp);
475#else 500#else
476 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 501 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
477#endif 502#endif
478 } 503 }
479} 504}
480 505
481static void 506static void
488 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);
489 514
490 av = (AV *)mg->mg_obj; 515 av = (AV *)mg->mg_obj;
491 516
492 if (expect_false (AvFILLp (av) >= AvMAX (av))) 517 if (expect_false (AvFILLp (av) >= AvMAX (av)))
493 av_extend (av, AvMAX (av) + 1); 518 av_extend (av, AvFILLp (av) + 1);
494 519
495 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 520 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
496} 521}
497 522
498/** load & save, init *******************************************************/ 523/** load & save, init *******************************************************/
503 perl_slots *slot = c->slot; 528 perl_slots *slot = c->slot;
504 c->slot = 0; 529 c->slot = 0;
505 530
506 PL_mainstack = c->mainstack; 531 PL_mainstack = c->mainstack;
507 532
508 GvSV (PL_defgv) = slot->defsv; 533 GvSV (PL_defgv) = slot->defsv;
509 GvAV (PL_defgv) = slot->defav; 534 GvAV (PL_defgv) = slot->defav;
510 GvSV (PL_errgv) = slot->errsv; 535 GvSV (PL_errgv) = slot->errsv;
511 GvSV (irsgv) = slot->irsgv; 536 GvSV (irsgv) = slot->irsgv;
537 GvHV (PL_hintgv) = slot->hinthv;
512 538
513 #define VAR(name,type) PL_ ## name = slot->name; 539 #define VAR(name,type) PL_ ## name = slot->name;
514 # include "state.h" 540 # include "state.h"
515 #undef VAR 541 #undef VAR
516 542
602 c->mainstack = PL_mainstack; 628 c->mainstack = PL_mainstack;
603 629
604 { 630 {
605 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 631 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
606 632
607 slot->defav = GvAV (PL_defgv); 633 slot->defav = GvAV (PL_defgv);
608 slot->defsv = DEFSV; 634 slot->defsv = DEFSV;
609 slot->errsv = ERRSV; 635 slot->errsv = ERRSV;
610 slot->irsgv = GvSV (irsgv); 636 slot->irsgv = GvSV (irsgv);
637 slot->hinthv = GvHV (PL_hintgv);
611 638
612 #define VAR(name,type) slot->name = PL_ ## name; 639 #define VAR(name,type) slot->name = PL_ ## name;
613 # include "state.h" 640 # include "state.h"
614 #undef VAR 641 #undef VAR
615 } 642 }
742#endif 769#endif
743 770
744/* 771/*
745 * This overrides the default magic get method of %SIG elements. 772 * This overrides the default magic get method of %SIG elements.
746 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 773 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
747 * and instead of tryign to save and restore the hash elements, we just provide 774 * and instead of trying to save and restore the hash elements, we just provide
748 * readback here. 775 * readback here.
749 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
750 * not expecting this to actually change the hook. This is a potential problem
751 * when a schedule happens then, but we ignore this.
752 */ 776 */
753static int 777static int
754coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 778coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
755{ 779{
756 const char *s = MgPV_nolen_const (mg); 780 const char *s = MgPV_nolen_const (mg);
809 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 833 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
810 834
811 if (svp) 835 if (svp)
812 { 836 {
813 SV *old = *svp; 837 SV *old = *svp;
814 *svp = newSVsv (sv); 838 *svp = SvOK (sv) ? newSVsv (sv) : 0;
815 SvREFCNT_dec (old); 839 SvREFCNT_dec (old);
816 return 0; 840 return 0;
817 } 841 }
818 } 842 }
819 843
829 853
830static int 854static int
831slf_check_nop (pTHX_ struct CoroSLF *frame) 855slf_check_nop (pTHX_ struct CoroSLF *frame)
832{ 856{
833 return 0; 857 return 0;
858}
859
860static int
861slf_check_repeat (pTHX_ struct CoroSLF *frame)
862{
863 return 1;
834} 864}
835 865
836static UNOP coro_setup_op; 866static UNOP coro_setup_op;
837 867
838static void NOINLINE /* noinline to keep it out of the transfer fast path */ 868static void NOINLINE /* noinline to keep it out of the transfer fast path */
845 875
846 PL_runops = RUNOPS_DEFAULT; 876 PL_runops = RUNOPS_DEFAULT;
847 PL_curcop = &PL_compiling; 877 PL_curcop = &PL_compiling;
848 PL_in_eval = EVAL_NULL; 878 PL_in_eval = EVAL_NULL;
849 PL_comppad = 0; 879 PL_comppad = 0;
880 PL_comppad_name = 0;
881 PL_comppad_name_fill = 0;
882 PL_comppad_name_floor = 0;
850 PL_curpm = 0; 883 PL_curpm = 0;
851 PL_curpad = 0; 884 PL_curpad = 0;
852 PL_localizing = 0; 885 PL_localizing = 0;
853 PL_dirty = 0; 886 PL_dirty = 0;
854 PL_restartop = 0; 887 PL_restartop = 0;
855#if PERL_VERSION_ATLEAST (5,10,0) 888#if PERL_VERSION_ATLEAST (5,10,0)
856 PL_parser = 0; 889 PL_parser = 0;
857#endif 890#endif
891 PL_hints = 0;
858 892
859 /* recreate the die/warn hooks */ 893 /* recreate the die/warn hooks */
860 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 894 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
861 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 895 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
862 896
863 GvSV (PL_defgv) = newSV (0); 897 GvSV (PL_defgv) = newSV (0);
864 GvAV (PL_defgv) = coro->args; coro->args = 0; 898 GvAV (PL_defgv) = coro->args; coro->args = 0;
865 GvSV (PL_errgv) = newSV (0); 899 GvSV (PL_errgv) = newSV (0);
866 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 900 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
901 GvHV (PL_hintgv) = 0;
867 PL_rs = newSVsv (GvSV (irsgv)); 902 PL_rs = newSVsv (GvSV (irsgv));
868 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 903 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
869 904
870 { 905 {
871 dSP; 906 dSP;
889 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 924 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
890 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 925 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
891 926
892 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 927 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
893 coro_setup_op.op_next = PL_op; 928 coro_setup_op.op_next = PL_op;
894 coro_setup_op.op_type = OP_CUSTOM; 929 coro_setup_op.op_type = OP_ENTERSUB;
895 coro_setup_op.op_ppaddr = pp_slf; 930 coro_setup_op.op_ppaddr = pp_slf;
896 /* no flags etc. required, as an init function won't be called */ 931 /* no flags etc. required, as an init function won't be called */
897 932
898 PL_op = (OP *)&coro_setup_op; 933 PL_op = (OP *)&coro_setup_op;
899 934
900 /* copy throw, in case it was set before coro_setup */ 935 /* copy throw, in case it was set before coro_setup */
901 CORO_THROW = coro->except; 936 CORO_THROW = coro->except;
902} 937}
903 938
904static void 939static void
905coro_destruct (pTHX_ struct coro *coro) 940coro_unwind_stacks (pTHX)
906{ 941{
907 if (!IN_DESTRUCT) 942 if (!IN_DESTRUCT)
908 { 943 {
909 /* restore all saved variables and stuff */ 944 /* restore all saved variables and stuff */
910 LEAVE_SCOPE (0); 945 LEAVE_SCOPE (0);
918 POPSTACK_TO (PL_mainstack); 953 POPSTACK_TO (PL_mainstack);
919 954
920 /* unwind main stack */ 955 /* unwind main stack */
921 dounwind (-1); 956 dounwind (-1);
922 } 957 }
958}
959
960static void
961coro_destruct_perl (pTHX_ struct coro *coro)
962{
963 coro_unwind_stacks (aTHX);
923 964
924 SvREFCNT_dec (GvSV (PL_defgv)); 965 SvREFCNT_dec (GvSV (PL_defgv));
925 SvREFCNT_dec (GvAV (PL_defgv)); 966 SvREFCNT_dec (GvAV (PL_defgv));
926 SvREFCNT_dec (GvSV (PL_errgv)); 967 SvREFCNT_dec (GvSV (PL_errgv));
927 SvREFCNT_dec (PL_defoutgv); 968 SvREFCNT_dec (PL_defoutgv);
928 SvREFCNT_dec (PL_rs); 969 SvREFCNT_dec (PL_rs);
929 SvREFCNT_dec (GvSV (irsgv)); 970 SvREFCNT_dec (GvSV (irsgv));
971 SvREFCNT_dec (GvHV (PL_hintgv));
930 972
931 SvREFCNT_dec (PL_diehook); 973 SvREFCNT_dec (PL_diehook);
932 SvREFCNT_dec (PL_warnhook); 974 SvREFCNT_dec (PL_warnhook);
933 975
934 SvREFCNT_dec (coro->saved_deffh); 976 SvREFCNT_dec (coro->saved_deffh);
952static int 994static int
953runops_trace (pTHX) 995runops_trace (pTHX)
954{ 996{
955 COP *oldcop = 0; 997 COP *oldcop = 0;
956 int oldcxix = -2; 998 int oldcxix = -2;
957 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
958 coro_cctx *cctx = coro->cctx;
959 999
960 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1000 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
961 { 1001 {
962 PERL_ASYNC_CHECK (); 1002 PERL_ASYNC_CHECK ();
963 1003
964 if (cctx->flags & CC_TRACE_ALL) 1004 if (cctx_current->flags & CC_TRACE_ALL)
965 { 1005 {
966 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1006 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
967 { 1007 {
968 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1008 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
969 SV **bot, **top; 1009 SV **bot, **top;
970 AV *av = newAV (); /* return values */ 1010 AV *av = newAV (); /* return values */
971 SV **cb; 1011 SV **cb;
1008 1048
1009 if (PL_curcop != &PL_compiling) 1049 if (PL_curcop != &PL_compiling)
1010 { 1050 {
1011 SV **cb; 1051 SV **cb;
1012 1052
1013 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1053 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1014 { 1054 {
1015 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1055 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1016 1056
1017 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1057 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1018 { 1058 {
1019 runops_proc_t old_runops = PL_runops;
1020 dSP; 1059 dSP;
1021 GV *gv = CvGV (cx->blk_sub.cv); 1060 GV *gv = CvGV (cx->blk_sub.cv);
1022 SV *fullname = sv_2mortal (newSV (0)); 1061 SV *fullname = sv_2mortal (newSV (0));
1023 1062
1024 if (isGV (gv)) 1063 if (isGV (gv))
1042 } 1081 }
1043 1082
1044 oldcxix = cxstack_ix; 1083 oldcxix = cxstack_ix;
1045 } 1084 }
1046 1085
1047 if (cctx->flags & CC_TRACE_LINE) 1086 if (cctx_current->flags & CC_TRACE_LINE)
1048 { 1087 {
1049 dSP; 1088 dSP;
1050 1089
1051 PL_runops = RUNOPS_DEFAULT; 1090 PL_runops = RUNOPS_DEFAULT;
1052 ENTER; 1091 ENTER;
1071 1110
1072 TAINT_NOT; 1111 TAINT_NOT;
1073 return 0; 1112 return 0;
1074} 1113}
1075 1114
1076static struct coro_cctx *cctx_ssl_cctx;
1077static struct CoroSLF cctx_ssl_frame; 1115static struct CoroSLF cctx_ssl_frame;
1078 1116
1079static void 1117static void
1080slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1118slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1081{ 1119{
1082 ta->prev = (struct coro *)cctx_ssl_cctx;
1083 ta->next = 0; 1120 ta->prev = 0;
1084} 1121}
1085 1122
1086static int 1123static int
1087slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1124slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1088{ 1125{
1091 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1128 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1092} 1129}
1093 1130
1094/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1131/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1095static void NOINLINE 1132static void NOINLINE
1096cctx_prepare (pTHX_ coro_cctx *cctx) 1133cctx_prepare (pTHX)
1097{ 1134{
1098 PL_top_env = &PL_start_env; 1135 PL_top_env = &PL_start_env;
1099 1136
1100 if (cctx->flags & CC_TRACE) 1137 if (cctx_current->flags & CC_TRACE)
1101 PL_runops = runops_trace; 1138 PL_runops = runops_trace;
1102 1139
1103 /* we already must be executing an SLF op, there is no other valid way 1140 /* we already must be executing an SLF op, there is no other valid way
1104 * that can lead to creation of a new cctx */ 1141 * that can lead to creation of a new cctx */
1105 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1142 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1106 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1143 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1107 1144
1108 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1145 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1109 cctx_ssl_cctx = cctx;
1110 cctx_ssl_frame = slf_frame; 1146 cctx_ssl_frame = slf_frame;
1111 1147
1112 slf_frame.prepare = slf_prepare_set_stacklevel; 1148 slf_frame.prepare = slf_prepare_set_stacklevel;
1113 slf_frame.check = slf_check_set_stacklevel; 1149 slf_frame.check = slf_check_set_stacklevel;
1114} 1150}
1137 /* normally we would need to skip the entersub here */ 1173 /* normally we would need to skip the entersub here */
1138 /* not doing so will re-execute it, which is exactly what we want */ 1174 /* not doing so will re-execute it, which is exactly what we want */
1139 /* PL_nop = PL_nop->op_next */ 1175 /* PL_nop = PL_nop->op_next */
1140 1176
1141 /* inject a fake subroutine call to cctx_init */ 1177 /* inject a fake subroutine call to cctx_init */
1142 cctx_prepare (aTHX_ (coro_cctx *)arg); 1178 cctx_prepare (aTHX);
1143 1179
1144 /* cctx_run is the alternative tail of transfer() */ 1180 /* cctx_run is the alternative tail of transfer() */
1145 transfer_tail (aTHX); 1181 transfer_tail (aTHX);
1146 1182
1147 /* somebody or something will hit me for both perl_run and PL_restartop */ 1183 /* somebody or something will hit me for both perl_run and PL_restartop */
1155 /* 1191 /*
1156 * If perl-run returns we assume exit() was being called or the coro 1192 * If perl-run returns we assume exit() was being called or the coro
1157 * fell off the end, which seems to be the only valid (non-bug) 1193 * fell off the end, which seems to be the only valid (non-bug)
1158 * reason for perl_run to return. We try to exit by jumping to the 1194 * reason for perl_run to return. We try to exit by jumping to the
1159 * bootstrap-time "top" top_env, as we cannot restore the "main" 1195 * bootstrap-time "top" top_env, as we cannot restore the "main"
1160 * coroutine as Coro has no such concept 1196 * coroutine as Coro has no such concept.
1197 * This actually isn't valid with the pthread backend, but OSes requiring
1198 * that backend are too broken to do it in a standards-compliant way.
1161 */ 1199 */
1162 PL_top_env = main_top_env; 1200 PL_top_env = main_top_env;
1163 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1201 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1164 } 1202 }
1165} 1203}
1242cctx_destroy (coro_cctx *cctx) 1280cctx_destroy (coro_cctx *cctx)
1243{ 1281{
1244 if (!cctx) 1282 if (!cctx)
1245 return; 1283 return;
1246 1284
1285 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1286
1247 --cctx_count; 1287 --cctx_count;
1248 coro_destroy (&cctx->cctx); 1288 coro_destroy (&cctx->cctx);
1249 1289
1250 /* coro_transfer creates new, empty cctx's */ 1290 /* coro_transfer creates new, empty cctx's */
1251 if (cctx->sptr) 1291 if (cctx->sptr)
1314 /* TODO: throwing up here is considered harmful */ 1354 /* TODO: throwing up here is considered harmful */
1315 1355
1316 if (expect_true (prev != next)) 1356 if (expect_true (prev != next))
1317 { 1357 {
1318 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1358 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1319 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1359 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1320 1360
1321 if (expect_false (next->flags & CF_RUNNING)) 1361 if (expect_false (next->flags & CF_RUNNING))
1322 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); 1362 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1323 1363
1324 if (expect_false (next->flags & CF_DESTROYED)) 1364 if (expect_false (next->flags & CF_DESTROYED))
1336transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1376transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1337{ 1377{
1338 dSTACKLEVEL; 1378 dSTACKLEVEL;
1339 1379
1340 /* sometimes transfer is only called to set idle_sp */ 1380 /* sometimes transfer is only called to set idle_sp */
1341 if (expect_false (!next)) 1381 if (expect_false (!prev))
1342 { 1382 {
1343 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1383 cctx_current->idle_sp = STACKLEVEL;
1344 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1384 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1345 } 1385 }
1346 else if (expect_true (prev != next)) 1386 else if (expect_true (prev != next))
1347 { 1387 {
1348 coro_cctx *prev__cctx; 1388 coro_cctx *cctx_prev;
1349 1389
1350 if (expect_false (prev->flags & CF_NEW)) 1390 if (expect_false (prev->flags & CF_NEW))
1351 { 1391 {
1352 /* create a new empty/source context */ 1392 /* create a new empty/source context */
1353 prev->cctx = cctx_new_empty ();
1354 prev->flags &= ~CF_NEW; 1393 prev->flags &= ~CF_NEW;
1355 prev->flags |= CF_RUNNING; 1394 prev->flags |= CF_RUNNING;
1356 } 1395 }
1357 1396
1358 prev->flags &= ~CF_RUNNING; 1397 prev->flags &= ~CF_RUNNING;
1369 coro_setup (aTHX_ next); 1408 coro_setup (aTHX_ next);
1370 } 1409 }
1371 else 1410 else
1372 load_perl (aTHX_ next); 1411 load_perl (aTHX_ next);
1373 1412
1374 prev__cctx = prev->cctx;
1375
1376 /* possibly untie and reuse the cctx */ 1413 /* possibly untie and reuse the cctx */
1377 if (expect_true ( 1414 if (expect_true (
1378 prev__cctx->idle_sp == STACKLEVEL 1415 cctx_current->idle_sp == STACKLEVEL
1379 && !(prev__cctx->flags & CC_TRACE) 1416 && !(cctx_current->flags & CC_TRACE)
1380 && !force_cctx 1417 && !force_cctx
1381 )) 1418 ))
1382 { 1419 {
1383 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1420 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1384 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1421 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1385 1422
1386 prev->cctx = 0;
1387
1388 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1423 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1389 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1424 /* without this the next cctx_get might destroy the running cctx while still in use */
1390 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1425 if (expect_false (CCTX_EXPIRED (cctx_current)))
1391 if (!next->cctx) 1426 if (expect_true (!next->cctx))
1392 next->cctx = cctx_get (aTHX); 1427 next->cctx = cctx_get (aTHX);
1393 1428
1394 cctx_put (prev__cctx); 1429 cctx_put (cctx_current);
1395 } 1430 }
1431 else
1432 prev->cctx = cctx_current;
1396 1433
1397 ++next->usecount; 1434 ++next->usecount;
1398 1435
1399 if (expect_true (!next->cctx)) 1436 cctx_prev = cctx_current;
1400 next->cctx = cctx_get (aTHX); 1437 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1401 1438
1402 if (expect_false (prev__cctx != next->cctx)) 1439 next->cctx = 0;
1440
1441 if (expect_false (cctx_prev != cctx_current))
1403 { 1442 {
1404 prev__cctx->top_env = PL_top_env; 1443 cctx_prev->top_env = PL_top_env;
1405 PL_top_env = next->cctx->top_env; 1444 PL_top_env = cctx_current->top_env;
1406 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1445 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1407 } 1446 }
1408 1447
1409 transfer_tail (aTHX); 1448 transfer_tail (aTHX);
1410 } 1449 }
1411} 1450}
1433 --coro_nready; 1472 --coro_nready;
1434 } 1473 }
1435 else 1474 else
1436 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1475 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1437 1476
1438 if (coro->mainstack && coro->mainstack != main_mainstack) 1477 if (coro->mainstack
1478 && coro->mainstack != main_mainstack
1479 && coro->slot
1480 && !PL_dirty)
1439 { 1481 {
1440 struct coro temp; 1482 struct coro temp;
1441 1483
1442 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1484 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1443 1485
1444 save_perl (aTHX_ &temp); 1486 save_perl (aTHX_ &temp);
1445 load_perl (aTHX_ coro); 1487 load_perl (aTHX_ coro);
1446 1488
1447 coro_destruct (aTHX_ coro); 1489 coro_destruct_perl (aTHX_ coro);
1448 1490
1449 load_perl (aTHX_ &temp); 1491 load_perl (aTHX_ &temp);
1450 1492
1451 coro->slot = 0; 1493 coro->slot = 0;
1452 } 1494 }
1566{ 1608{
1567 struct coro *coro; 1609 struct coro *coro;
1568 SV *sv_hook; 1610 SV *sv_hook;
1569 void (*xs_hook)(void); 1611 void (*xs_hook)(void);
1570 1612
1571 if (SvROK (coro_sv))
1572 coro_sv = SvRV (coro_sv);
1573
1574 coro = SvSTATE (coro_sv); 1613 coro = SvSTATE (coro_sv);
1575 1614
1576 if (coro->flags & CF_READY) 1615 if (coro->flags & CF_READY)
1577 return 0; 1616 return 0;
1578 1617
1609api_is_ready (pTHX_ SV *coro_sv) 1648api_is_ready (pTHX_ SV *coro_sv)
1610{ 1649{
1611 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1650 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1612} 1651}
1613 1652
1653/* expects to own a reference to next->hv */
1614INLINE void 1654INLINE void
1615prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1655prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1616{ 1656{
1617 SV *prev_sv, *next_sv;
1618
1619 for (;;)
1620 {
1621 next_sv = coro_deq (aTHX);
1622
1623 /* nothing to schedule: call the idle handler */
1624 if (expect_false (!next_sv))
1625 {
1626 dSP;
1627
1628 ENTER;
1629 SAVETMPS;
1630
1631 PUSHMARK (SP);
1632 PUTBACK;
1633 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1634
1635 FREETMPS;
1636 LEAVE;
1637 continue;
1638 }
1639
1640 ta->next = SvSTATE_hv (next_sv);
1641
1642 /* cannot transfer to destroyed coros, skip and look for next */
1643 if (expect_false (ta->next->flags & CF_DESTROYED))
1644 {
1645 SvREFCNT_dec (next_sv);
1646 /* coro_nready has already been taken care of by destroy */
1647 continue;
1648 }
1649
1650 --coro_nready;
1651 break;
1652 }
1653
1654 /* free this only after the transfer */
1655 prev_sv = SvRV (coro_current); 1657 SV *prev_sv = SvRV (coro_current);
1658
1656 ta->prev = SvSTATE_hv (prev_sv); 1659 ta->prev = SvSTATE_hv (prev_sv);
1660 ta->next = next;
1661
1657 TRANSFER_CHECK (*ta); 1662 TRANSFER_CHECK (*ta);
1658 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); 1663
1659 ta->next->flags &= ~CF_READY;
1660 SvRV_set (coro_current, next_sv); 1664 SvRV_set (coro_current, (SV *)next->hv);
1661 1665
1662 free_coro_mortal (aTHX); 1666 free_coro_mortal (aTHX);
1663 coro_mortal = prev_sv; 1667 coro_mortal = prev_sv;
1664} 1668}
1665 1669
1670static void
1671prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1672{
1673 for (;;)
1674 {
1675 SV *next_sv = coro_deq (aTHX);
1676
1677 if (expect_true (next_sv))
1678 {
1679 struct coro *next = SvSTATE_hv (next_sv);
1680
1681 /* cannot transfer to destroyed coros, skip and look for next */
1682 if (expect_false (next->flags & CF_DESTROYED))
1683 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1684 else
1685 {
1686 next->flags &= ~CF_READY;
1687 --coro_nready;
1688
1689 prepare_schedule_to (aTHX_ ta, next);
1690 break;
1691 }
1692 }
1693 else
1694 {
1695 /* nothing to schedule: call the idle handler */
1696 if (SvROK (sv_idle)
1697 && SvOBJECT (SvRV (sv_idle)))
1698 {
1699 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1700 api_ready (aTHX_ SvRV (sv_idle));
1701 --coro_nready;
1702 }
1703 else
1704 {
1705 dSP;
1706
1707 ENTER;
1708 SAVETMPS;
1709
1710 PUSHMARK (SP);
1711 PUTBACK;
1712 call_sv (sv_idle, G_VOID | G_DISCARD);
1713
1714 FREETMPS;
1715 LEAVE;
1716 }
1717 }
1718 }
1719}
1720
1666INLINE void 1721INLINE void
1667prepare_cede (pTHX_ struct coro_transfer_args *ta) 1722prepare_cede (pTHX_ struct coro_transfer_args *ta)
1668{ 1723{
1669 api_ready (aTHX_ coro_current); 1724 api_ready (aTHX_ coro_current);
1670 prepare_schedule (aTHX_ ta); 1725 prepare_schedule (aTHX_ ta);
1689{ 1744{
1690 struct coro_transfer_args ta; 1745 struct coro_transfer_args ta;
1691 1746
1692 prepare_schedule (aTHX_ &ta); 1747 prepare_schedule (aTHX_ &ta);
1693 TRANSFER (ta, 1); 1748 TRANSFER (ta, 1);
1749}
1750
1751static void
1752api_schedule_to (pTHX_ SV *coro_sv)
1753{
1754 struct coro_transfer_args ta;
1755 struct coro *next = SvSTATE (coro_sv);
1756
1757 SvREFCNT_inc_NN (coro_sv);
1758 prepare_schedule_to (aTHX_ &ta, next);
1694} 1759}
1695 1760
1696static int 1761static int
1697api_cede (pTHX) 1762api_cede (pTHX)
1698{ 1763{
1727static void 1792static void
1728api_trace (pTHX_ SV *coro_sv, int flags) 1793api_trace (pTHX_ SV *coro_sv, int flags)
1729{ 1794{
1730 struct coro *coro = SvSTATE (coro_sv); 1795 struct coro *coro = SvSTATE (coro_sv);
1731 1796
1797 if (coro->flags & CF_RUNNING)
1798 croak ("cannot enable tracing on a running coroutine, caught");
1799
1732 if (flags & CC_TRACE) 1800 if (flags & CC_TRACE)
1733 { 1801 {
1734 if (!coro->cctx) 1802 if (!coro->cctx)
1735 coro->cctx = cctx_new_run (); 1803 coro->cctx = cctx_new_run ();
1736 else if (!(coro->cctx->flags & CC_TRACE)) 1804 else if (!(coro->cctx->flags & CC_TRACE))
1737 croak ("cannot enable tracing on coroutine with custom stack,"); 1805 croak ("cannot enable tracing on coroutine with custom stack, caught");
1738 1806
1739 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1807 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1740 } 1808 }
1741 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1809 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1742 { 1810 {
1747 else 1815 else
1748 coro->slot->runops = RUNOPS_DEFAULT; 1816 coro->slot->runops = RUNOPS_DEFAULT;
1749 } 1817 }
1750} 1818}
1751 1819
1820static void
1821coro_call_on_destroy (pTHX_ struct coro *coro)
1822{
1823 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1824 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1825
1826 if (on_destroyp)
1827 {
1828 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1829
1830 while (AvFILLp (on_destroy) >= 0)
1831 {
1832 dSP; /* don't disturb outer sp */
1833 SV *cb = av_pop (on_destroy);
1834
1835 PUSHMARK (SP);
1836
1837 if (statusp)
1838 {
1839 int i;
1840 AV *status = (AV *)SvRV (*statusp);
1841 EXTEND (SP, AvFILLp (status) + 1);
1842
1843 for (i = 0; i <= AvFILLp (status); ++i)
1844 PUSHs (AvARRAY (status)[i]);
1845 }
1846
1847 PUTBACK;
1848 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1849 }
1850 }
1851}
1852
1853static void
1854slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1855{
1856 int i;
1857 HV *hv = (HV *)SvRV (coro_current);
1858 AV *av = newAV ();
1859
1860 av_extend (av, items - 1);
1861 for (i = 0; i < items; ++i)
1862 av_push (av, SvREFCNT_inc_NN (arg [i]));
1863
1864 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1865
1866 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1867 api_ready (aTHX_ sv_manager);
1868
1869 frame->prepare = prepare_schedule;
1870 frame->check = slf_check_repeat;
1871
1872 /* as a minor optimisation, we could unwind all stacks here */
1873 /* but that puts extra pressure on pp_slf, and is not worth much */
1874 /*coro_unwind_stacks (aTHX);*/
1875}
1876
1752/*****************************************************************************/ 1877/*****************************************************************************/
1753/* async pool handler */ 1878/* async pool handler */
1754 1879
1755static int 1880static int
1756slf_check_pool_handler (pTHX_ struct CoroSLF *frame) 1881slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1760 1885
1761 if (!coro->invoke_cb) 1886 if (!coro->invoke_cb)
1762 return 1; /* loop till we have invoke */ 1887 return 1; /* loop till we have invoke */
1763 else 1888 else
1764 { 1889 {
1765 int i, len;
1766
1767 hv_store (hv, "desc", sizeof ("desc") - 1, 1890 hv_store (hv, "desc", sizeof ("desc") - 1,
1768 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1891 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1769 1892
1770 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); 1893 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1771
1772 len = av_len (coro->invoke_av);
1773 1894
1774 { 1895 {
1775 dSP; 1896 dSP;
1776 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; 1897 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1777 PUTBACK; 1898 PUTBACK;
1841 SV *data = (SV *)GENSUB_ARG; 1962 SV *data = (SV *)GENSUB_ARG;
1842 1963
1843 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1964 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1844 { 1965 {
1845 /* first call, set args */ 1966 /* first call, set args */
1846 int i;
1847 AV *av = newAV (); 1967 AV *av = newAV ();
1848 SV *coro = SvRV (data); 1968 SV *coro = SvRV (data);
1849 1969
1850 SvRV_set (data, (SV *)av); 1970 SvRV_set (data, (SV *)av);
1851 api_ready (aTHX_ coro); 1971 api_ready (aTHX_ coro);
1992static void 2112static void
1993slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2113slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1994{ 2114{
1995 frame->prepare = prepare_schedule; 2115 frame->prepare = prepare_schedule;
1996 frame->check = slf_check_nop; 2116 frame->check = slf_check_nop;
2117}
2118
2119static void
2120slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2121{
2122 struct coro *next = (struct coro *)slf_frame.data;
2123
2124 SvREFCNT_inc_NN (next->hv);
2125 prepare_schedule_to (aTHX_ ta, next);
2126}
2127
2128static void
2129slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2130{
2131 if (!items)
2132 croak ("Coro::schedule_to expects a coroutine argument, caught");
2133
2134 frame->data = (void *)SvSTATE (arg [0]);
2135 frame->prepare = slf_prepare_schedule_to;
2136 frame->check = slf_check_nop;
2137}
2138
2139static void
2140slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2141{
2142 api_ready (aTHX_ SvRV (coro_current));
2143
2144 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1997} 2145}
1998 2146
1999static void 2147static void
2000slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2148slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2001{ 2149{
2147 } 2295 }
2148 else 2296 else
2149 slf_argc = 0; 2297 slf_argc = 0;
2150 2298
2151 PL_op->op_ppaddr = pp_slf; 2299 PL_op->op_ppaddr = pp_slf;
2152 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2300 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2153 2301
2154 PL_op = (OP *)&slf_restore; 2302 PL_op = (OP *)&slf_restore;
2155} 2303}
2156 2304
2157/*****************************************************************************/ 2305/*****************************************************************************/
2240 SV **ary; 2388 SV **ary;
2241 2389
2242 /* unfortunately, building manually saves memory */ 2390 /* unfortunately, building manually saves memory */
2243 Newx (ary, 2, SV *); 2391 Newx (ary, 2, SV *);
2244 AvALLOC (av) = ary; 2392 AvALLOC (av) = ary;
2393#if PERL_VERSION_ATLEAST (5,10,0)
2245 /*AvARRAY (av) = ary;*/ 2394 AvARRAY (av) = ary;
2395#else
2246 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2396 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2397 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2398 SvPVX ((SV *)av) = (char *)ary;
2399#endif
2247 AvMAX (av) = 1; 2400 AvMAX (av) = 1;
2248 AvFILLp (av) = 0; 2401 AvFILLp (av) = 0;
2249 ary [0] = newSViv (count); 2402 ary [0] = newSViv (count);
2250 2403
2251 return newRV_noinc ((SV *)av); 2404 return newRV_noinc ((SV *)av);
2621 XSRETURN_EMPTY; 2774 XSRETURN_EMPTY;
2622} 2775}
2623 2776
2624/*****************************************************************************/ 2777/*****************************************************************************/
2625 2778
2779#if CORO_CLONE
2780# include "clone.c"
2781#endif
2782
2626MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2783MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2627 2784
2628PROTOTYPES: DISABLE 2785PROTOTYPES: DISABLE
2629 2786
2630BOOT: 2787BOOT:
2634 coro_thx = PERL_GET_CONTEXT; 2791 coro_thx = PERL_GET_CONTEXT;
2635# endif 2792# endif
2636#endif 2793#endif
2637 BOOT_PAGESIZE; 2794 BOOT_PAGESIZE;
2638 2795
2796 cctx_current = cctx_new_empty ();
2797
2639 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2798 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2640 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2799 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2641 2800
2642 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2801 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2643 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2802 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2662 2821
2663 { 2822 {
2664 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2823 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2665 2824
2666 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2825 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2667 hv_store_ent (PL_custom_op_names, slf, 2826 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2668 newSVpv ("coro_slf", 0), 0);
2669 2827
2670 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2828 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2671 hv_store_ent (PL_custom_op_descs, slf, 2829 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2672 newSVpv ("coro schedule like function", 0), 0);
2673 } 2830 }
2674 2831
2675 coroapi.ver = CORO_API_VERSION; 2832 coroapi.ver = CORO_API_VERSION;
2676 coroapi.rev = CORO_API_REVISION; 2833 coroapi.rev = CORO_API_REVISION;
2677 2834
2770void 2927void
2771_exit (int code) 2928_exit (int code)
2772 PROTOTYPE: $ 2929 PROTOTYPE: $
2773 CODE: 2930 CODE:
2774 _exit (code); 2931 _exit (code);
2932
2933SV *
2934clone (Coro::State coro)
2935 CODE:
2936{
2937#if CORO_CLONE
2938 struct coro *ncoro = coro_clone (aTHX_ coro);
2939 MAGIC *mg;
2940 /* TODO: too much duplication */
2941 ncoro->hv = newHV ();
2942 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2943 mg->mg_flags |= MGf_DUP;
2944 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2945#else
2946 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2947#endif
2948}
2949 OUTPUT:
2950 RETVAL
2775 2951
2776int 2952int
2777cctx_stacksize (int new_stacksize = 0) 2953cctx_stacksize (int new_stacksize = 0)
2778 PROTOTYPE: ;$ 2954 PROTOTYPE: ;$
2779 CODE: 2955 CODE:
2900 3076
2901SV * 3077SV *
2902has_cctx (Coro::State coro) 3078has_cctx (Coro::State coro)
2903 PROTOTYPE: $ 3079 PROTOTYPE: $
2904 CODE: 3080 CODE:
2905 RETVAL = boolSV (!!coro->cctx); 3081 /* maybe manage the running flag differently */
3082 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2906 OUTPUT: 3083 OUTPUT:
2907 RETVAL 3084 RETVAL
2908 3085
2909int 3086int
2910is_traced (Coro::State coro) 3087is_traced (Coro::State coro)
2930 3107
2931void 3108void
2932force_cctx () 3109force_cctx ()
2933 PROTOTYPE: 3110 PROTOTYPE:
2934 CODE: 3111 CODE:
2935 SvSTATE_current->cctx->idle_sp = 0; 3112 cctx_current->idle_sp = 0;
2936 3113
2937void 3114void
2938swap_defsv (Coro::State self) 3115swap_defsv (Coro::State self)
2939 PROTOTYPE: $ 3116 PROTOTYPE: $
2940 ALIAS: 3117 ALIAS:
2955 3132
2956BOOT: 3133BOOT:
2957{ 3134{
2958 int i; 3135 int i;
2959 3136
2960 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2961 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3137 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2962 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3138 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2963 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3139 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
2964 cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); 3140 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2965 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 3141 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2966 SvREADONLY_on (coro_current); 3142 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3143 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3144 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3145 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
2967 3146
2968 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3147 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
2969 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3148 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2970 cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); 3149 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
2971 cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); 3150 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2972 3151
2973 coro_stash = gv_stashpv ("Coro", TRUE); 3152 coro_stash = gv_stashpv ("Coro", TRUE);
2974 3153
2975 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3154 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2976 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3155 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2984 3163
2985 { 3164 {
2986 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3165 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2987 3166
2988 coroapi.schedule = api_schedule; 3167 coroapi.schedule = api_schedule;
3168 coroapi.schedule_to = api_schedule_to;
2989 coroapi.cede = api_cede; 3169 coroapi.cede = api_cede;
2990 coroapi.cede_notself = api_cede_notself; 3170 coroapi.cede_notself = api_cede_notself;
2991 coroapi.ready = api_ready; 3171 coroapi.ready = api_ready;
2992 coroapi.is_ready = api_is_ready; 3172 coroapi.is_ready = api_is_ready;
2993 coroapi.nready = coro_nready; 3173 coroapi.nready = coro_nready;
2998 SvREADONLY_on (sv); 3178 SvREADONLY_on (sv);
2999 } 3179 }
3000} 3180}
3001 3181
3002void 3182void
3183terminate (...)
3184 CODE:
3185 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3186
3187void
3003schedule (...) 3188schedule (...)
3004 CODE: 3189 CODE:
3005 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3190 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3191
3192void
3193schedule_to (...)
3194 CODE:
3195 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3196
3197void
3198cede_to (...)
3199 CODE:
3200 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
3006 3201
3007void 3202void
3008cede (...) 3203cede (...)
3009 CODE: 3204 CODE:
3010 CORO_EXECUTE_SLF_XS (slf_init_cede); 3205 CORO_EXECUTE_SLF_XS (slf_init_cede);
3011 3206
3012void 3207void
3013cede_notself (...) 3208cede_notself (...)
3014 CODE: 3209 CODE:
3015 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3210 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3211
3212void
3213_cancel (Coro::State self)
3214 CODE:
3215 coro_state_destroy (aTHX_ self);
3216 coro_call_on_destroy (aTHX_ self);
3016 3217
3017void 3218void
3018_set_current (SV *current) 3219_set_current (SV *current)
3019 PROTOTYPE: $ 3220 PROTOTYPE: $
3020 CODE: 3221 CODE:
3091 PUSHMARK (SP); 3292 PUSHMARK (SP);
3092 EXTEND (SP, 2); 3293 EXTEND (SP, 2);
3093 PUSHs (sv_Coro); 3294 PUSHs (sv_Coro);
3094 PUSHs ((SV *)cv_pool_handler); 3295 PUSHs ((SV *)cv_pool_handler);
3095 PUTBACK; 3296 PUTBACK;
3096 call_sv (cv_coro_new, G_SCALAR); 3297 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3097 SPAGAIN; 3298 SPAGAIN;
3098 3299
3099 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3300 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
3100 } 3301 }
3101 3302
3106 assert (!coro->invoke_av); 3307 assert (!coro->invoke_av);
3107 coro->invoke_cb = SvREFCNT_inc (cb); 3308 coro->invoke_cb = SvREFCNT_inc (cb);
3108 coro->invoke_av = av; 3309 coro->invoke_av = av;
3109 } 3310 }
3110 3311
3111 api_ready ((SV *)hv); 3312 api_ready (aTHX_ (SV *)hv);
3112 3313
3113 if (GIMME_V != G_VOID) 3314 if (GIMME_V != G_VOID)
3114 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 3315 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3115 else 3316 else
3116 SvREFCNT_dec (hv); 3317 SvREFCNT_dec (hv);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines