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.315 by root, Thu Nov 20 05:25:17 2008 UTC vs.
Revision 1.337 by root, Fri Dec 5 18:26:10 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines