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.314 by root, Thu Nov 20 03:24:39 2008 UTC vs.
Revision 1.333 by root, Thu Nov 27 12:39:00 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_new; 195static CV *cv_coro_state_new;
189 196
190/* Coro::AnyEvent */ 197/* Coro::AnyEvent */
191static SV *sv_activity; 198static SV *sv_activity;
192 199
193static struct coro_cctx *cctx_first; 200static struct coro_cctx *cctx_first;
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
928 SvREFCNT_dec (GvAV (PL_defgv)); 963 SvREFCNT_dec (GvAV (PL_defgv));
929 SvREFCNT_dec (GvSV (PL_errgv)); 964 SvREFCNT_dec (GvSV (PL_errgv));
930 SvREFCNT_dec (PL_defoutgv); 965 SvREFCNT_dec (PL_defoutgv);
931 SvREFCNT_dec (PL_rs); 966 SvREFCNT_dec (PL_rs);
932 SvREFCNT_dec (GvSV (irsgv)); 967 SvREFCNT_dec (GvSV (irsgv));
968 SvREFCNT_dec (GvHV (PL_hintgv));
933 969
934 SvREFCNT_dec (PL_diehook); 970 SvREFCNT_dec (PL_diehook);
935 SvREFCNT_dec (PL_warnhook); 971 SvREFCNT_dec (PL_warnhook);
936 972
937 SvREFCNT_dec (coro->saved_deffh); 973 SvREFCNT_dec (coro->saved_deffh);
955static int 991static int
956runops_trace (pTHX) 992runops_trace (pTHX)
957{ 993{
958 COP *oldcop = 0; 994 COP *oldcop = 0;
959 int oldcxix = -2; 995 int oldcxix = -2;
960 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
961 coro_cctx *cctx = coro->cctx;
962 996
963 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 997 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
964 { 998 {
965 PERL_ASYNC_CHECK (); 999 PERL_ASYNC_CHECK ();
966 1000
967 if (cctx->flags & CC_TRACE_ALL) 1001 if (cctx_current->flags & CC_TRACE_ALL)
968 { 1002 {
969 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1003 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
970 { 1004 {
971 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1005 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
972 SV **bot, **top; 1006 SV **bot, **top;
973 AV *av = newAV (); /* return values */ 1007 AV *av = newAV (); /* return values */
974 SV **cb; 1008 SV **cb;
1011 1045
1012 if (PL_curcop != &PL_compiling) 1046 if (PL_curcop != &PL_compiling)
1013 { 1047 {
1014 SV **cb; 1048 SV **cb;
1015 1049
1016 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1050 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1017 { 1051 {
1018 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1052 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1019 1053
1020 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1054 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1021 { 1055 {
1022 runops_proc_t old_runops = PL_runops;
1023 dSP; 1056 dSP;
1024 GV *gv = CvGV (cx->blk_sub.cv); 1057 GV *gv = CvGV (cx->blk_sub.cv);
1025 SV *fullname = sv_2mortal (newSV (0)); 1058 SV *fullname = sv_2mortal (newSV (0));
1026 1059
1027 if (isGV (gv)) 1060 if (isGV (gv))
1045 } 1078 }
1046 1079
1047 oldcxix = cxstack_ix; 1080 oldcxix = cxstack_ix;
1048 } 1081 }
1049 1082
1050 if (cctx->flags & CC_TRACE_LINE) 1083 if (cctx_current->flags & CC_TRACE_LINE)
1051 { 1084 {
1052 dSP; 1085 dSP;
1053 1086
1054 PL_runops = RUNOPS_DEFAULT; 1087 PL_runops = RUNOPS_DEFAULT;
1055 ENTER; 1088 ENTER;
1074 1107
1075 TAINT_NOT; 1108 TAINT_NOT;
1076 return 0; 1109 return 0;
1077} 1110}
1078 1111
1079static struct coro_cctx *cctx_ssl_cctx;
1080static struct CoroSLF cctx_ssl_frame; 1112static struct CoroSLF cctx_ssl_frame;
1081 1113
1082static void 1114static void
1083slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1115slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1084{ 1116{
1085 ta->prev = (struct coro *)cctx_ssl_cctx;
1086 ta->next = 0; 1117 ta->prev = 0;
1087} 1118}
1088 1119
1089static int 1120static int
1090slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1121slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1091{ 1122{
1094 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1125 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1095} 1126}
1096 1127
1097/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1128/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1098static void NOINLINE 1129static void NOINLINE
1099cctx_prepare (pTHX_ coro_cctx *cctx) 1130cctx_prepare (pTHX)
1100{ 1131{
1101 PL_top_env = &PL_start_env; 1132 PL_top_env = &PL_start_env;
1102 1133
1103 if (cctx->flags & CC_TRACE) 1134 if (cctx_current->flags & CC_TRACE)
1104 PL_runops = runops_trace; 1135 PL_runops = runops_trace;
1105 1136
1106 /* we already must be executing an SLF op, there is no other valid way 1137 /* we already must be executing an SLF op, there is no other valid way
1107 * that can lead to creation of a new cctx */ 1138 * that can lead to creation of a new cctx */
1108 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1139 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1109 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1140 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1110 1141
1111 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1142 /* 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; 1143 cctx_ssl_frame = slf_frame;
1114 1144
1115 slf_frame.prepare = slf_prepare_set_stacklevel; 1145 slf_frame.prepare = slf_prepare_set_stacklevel;
1116 slf_frame.check = slf_check_set_stacklevel; 1146 slf_frame.check = slf_check_set_stacklevel;
1117} 1147}
1140 /* normally we would need to skip the entersub here */ 1170 /* normally we would need to skip the entersub here */
1141 /* not doing so will re-execute it, which is exactly what we want */ 1171 /* not doing so will re-execute it, which is exactly what we want */
1142 /* PL_nop = PL_nop->op_next */ 1172 /* PL_nop = PL_nop->op_next */
1143 1173
1144 /* inject a fake subroutine call to cctx_init */ 1174 /* inject a fake subroutine call to cctx_init */
1145 cctx_prepare (aTHX_ (coro_cctx *)arg); 1175 cctx_prepare (aTHX);
1146 1176
1147 /* cctx_run is the alternative tail of transfer() */ 1177 /* cctx_run is the alternative tail of transfer() */
1148 transfer_tail (aTHX); 1178 transfer_tail (aTHX);
1149 1179
1150 /* somebody or something will hit me for both perl_run and PL_restartop */ 1180 /* somebody or something will hit me for both perl_run and PL_restartop */
1158 /* 1188 /*
1159 * If perl-run returns we assume exit() was being called or the coro 1189 * 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) 1190 * 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 1191 * 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" 1192 * bootstrap-time "top" top_env, as we cannot restore the "main"
1163 * coroutine as Coro has no such concept 1193 * coroutine as Coro has no such concept.
1194 * This actually isn't valid with the pthread backend, but OSes requiring
1195 * that backend are too broken to do it in a standards-compliant way.
1164 */ 1196 */
1165 PL_top_env = main_top_env; 1197 PL_top_env = main_top_env;
1166 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1198 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1167 } 1199 }
1168} 1200}
1245cctx_destroy (coro_cctx *cctx) 1277cctx_destroy (coro_cctx *cctx)
1246{ 1278{
1247 if (!cctx) 1279 if (!cctx)
1248 return; 1280 return;
1249 1281
1282 assert (cctx != cctx_current);//D temporary
1283
1250 --cctx_count; 1284 --cctx_count;
1251 coro_destroy (&cctx->cctx); 1285 coro_destroy (&cctx->cctx);
1252 1286
1253 /* coro_transfer creates new, empty cctx's */ 1287 /* coro_transfer creates new, empty cctx's */
1254 if (cctx->sptr) 1288 if (cctx->sptr)
1317 /* TODO: throwing up here is considered harmful */ 1351 /* TODO: throwing up here is considered harmful */
1318 1352
1319 if (expect_true (prev != next)) 1353 if (expect_true (prev != next))
1320 { 1354 {
1321 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1355 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,"); 1356 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1323 1357
1324 if (expect_false (next->flags & CF_RUNNING)) 1358 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,"); 1359 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1326 1360
1327 if (expect_false (next->flags & CF_DESTROYED)) 1361 if (expect_false (next->flags & CF_DESTROYED))
1339transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1373transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1340{ 1374{
1341 dSTACKLEVEL; 1375 dSTACKLEVEL;
1342 1376
1343 /* sometimes transfer is only called to set idle_sp */ 1377 /* sometimes transfer is only called to set idle_sp */
1344 if (expect_false (!next)) 1378 if (expect_false (!prev))
1345 { 1379 {
1346 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1380 cctx_current->idle_sp = STACKLEVEL;
1347 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1381 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1348 } 1382 }
1349 else if (expect_true (prev != next)) 1383 else if (expect_true (prev != next))
1350 { 1384 {
1351 coro_cctx *prev__cctx; 1385 coro_cctx *cctx_prev;
1352 1386
1353 if (expect_false (prev->flags & CF_NEW)) 1387 if (expect_false (prev->flags & CF_NEW))
1354 { 1388 {
1355 /* create a new empty/source context */ 1389 /* create a new empty/source context */
1356 prev->cctx = cctx_new_empty ();
1357 prev->flags &= ~CF_NEW; 1390 prev->flags &= ~CF_NEW;
1358 prev->flags |= CF_RUNNING; 1391 prev->flags |= CF_RUNNING;
1359 } 1392 }
1360 1393
1361 prev->flags &= ~CF_RUNNING; 1394 prev->flags &= ~CF_RUNNING;
1372 coro_setup (aTHX_ next); 1405 coro_setup (aTHX_ next);
1373 } 1406 }
1374 else 1407 else
1375 load_perl (aTHX_ next); 1408 load_perl (aTHX_ next);
1376 1409
1377 prev__cctx = prev->cctx; 1410 assert (!prev->cctx);//D temporary
1378 1411
1379 /* possibly untie and reuse the cctx */ 1412 /* possibly untie and reuse the cctx */
1380 if (expect_true ( 1413 if (expect_true (
1381 prev__cctx->idle_sp == STACKLEVEL 1414 cctx_current->idle_sp == STACKLEVEL
1382 && !(prev__cctx->flags & CC_TRACE) 1415 && !(cctx_current->flags & CC_TRACE)
1383 && !force_cctx 1416 && !force_cctx
1384 )) 1417 ))
1385 { 1418 {
1386 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1419 /* 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)); 1420 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1388 1421
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 */ 1422 /* 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 */ 1423 /* without this the next cctx_get might destroy the running cctx while still in use */
1393 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1424 if (expect_false (CCTX_EXPIRED (cctx_current)))
1394 if (!next->cctx) 1425 if (expect_true (!next->cctx))
1395 next->cctx = cctx_get (aTHX); 1426 next->cctx = cctx_get (aTHX);
1396 1427
1397 cctx_put (prev__cctx); 1428 cctx_put (cctx_current);
1398 } 1429 }
1430 else
1431 prev->cctx = cctx_current;
1399 1432
1400 ++next->usecount; 1433 ++next->usecount;
1401 1434
1402 if (expect_true (!next->cctx)) 1435 cctx_prev = cctx_current;
1403 next->cctx = cctx_get (aTHX); 1436 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1404 1437
1405 if (expect_false (prev__cctx != next->cctx)) 1438 next->cctx = 0;
1439
1440 if (expect_false (cctx_prev != cctx_current))
1406 { 1441 {
1407 prev__cctx->top_env = PL_top_env; 1442 cctx_prev->top_env = PL_top_env;
1408 PL_top_env = next->cctx->top_env; 1443 PL_top_env = cctx_current->top_env;
1409 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1444 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1410 } 1445 }
1411 1446
1412 transfer_tail (aTHX); 1447 transfer_tail (aTHX);
1413 } 1448 }
1414} 1449}
1569{ 1604{
1570 struct coro *coro; 1605 struct coro *coro;
1571 SV *sv_hook; 1606 SV *sv_hook;
1572 void (*xs_hook)(void); 1607 void (*xs_hook)(void);
1573 1608
1574 if (SvROK (coro_sv))
1575 coro_sv = SvRV (coro_sv);
1576
1577 coro = SvSTATE (coro_sv); 1609 coro = SvSTATE (coro_sv);
1578 1610
1579 if (coro->flags & CF_READY) 1611 if (coro->flags & CF_READY)
1580 return 0; 1612 return 0;
1581 1613
1612api_is_ready (pTHX_ SV *coro_sv) 1644api_is_ready (pTHX_ SV *coro_sv)
1613{ 1645{
1614 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1646 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1615} 1647}
1616 1648
1649/* expects to own a reference to next->hv */
1617INLINE void 1650INLINE void
1618prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1651prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1619{ 1652{
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); 1653 SV *prev_sv = SvRV (coro_current);
1654
1659 ta->prev = SvSTATE_hv (prev_sv); 1655 ta->prev = SvSTATE_hv (prev_sv);
1656 ta->next = next;
1657
1660 TRANSFER_CHECK (*ta); 1658 TRANSFER_CHECK (*ta);
1661 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); 1659
1662 ta->next->flags &= ~CF_READY;
1663 SvRV_set (coro_current, next_sv); 1660 SvRV_set (coro_current, (SV *)next->hv);
1664 1661
1665 free_coro_mortal (aTHX); 1662 free_coro_mortal (aTHX);
1666 coro_mortal = prev_sv; 1663 coro_mortal = prev_sv;
1667} 1664}
1668 1665
1666static void
1667prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1668{
1669 for (;;)
1670 {
1671 SV *next_sv = coro_deq (aTHX);
1672
1673 if (expect_true (next_sv))
1674 {
1675 struct coro *next = SvSTATE_hv (next_sv);
1676
1677 /* cannot transfer to destroyed coros, skip and look for next */
1678 if (expect_false (next->flags & CF_DESTROYED))
1679 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1680 else
1681 {
1682 next->flags &= ~CF_READY;
1683 --coro_nready;
1684
1685 prepare_schedule_to (aTHX_ ta, next);
1686 break;
1687 }
1688 }
1689 else
1690 {
1691 /* nothing to schedule: call the idle handler */
1692 if (SvROK (sv_idle)
1693 && SvOBJECT (SvRV (sv_idle)))
1694 {
1695 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1696 api_ready (aTHX_ SvRV (sv_idle));
1697 --coro_nready;
1698 }
1699 else
1700 {
1701 dSP;
1702
1703 ENTER;
1704 SAVETMPS;
1705
1706 PUSHMARK (SP);
1707 PUTBACK;
1708 call_sv (sv_idle, G_VOID | G_DISCARD);
1709
1710 FREETMPS;
1711 LEAVE;
1712 }
1713 }
1714 }
1715}
1716
1669INLINE void 1717INLINE void
1670prepare_cede (pTHX_ struct coro_transfer_args *ta) 1718prepare_cede (pTHX_ struct coro_transfer_args *ta)
1671{ 1719{
1672 api_ready (aTHX_ coro_current); 1720 api_ready (aTHX_ coro_current);
1673 prepare_schedule (aTHX_ ta); 1721 prepare_schedule (aTHX_ ta);
1692{ 1740{
1693 struct coro_transfer_args ta; 1741 struct coro_transfer_args ta;
1694 1742
1695 prepare_schedule (aTHX_ &ta); 1743 prepare_schedule (aTHX_ &ta);
1696 TRANSFER (ta, 1); 1744 TRANSFER (ta, 1);
1745}
1746
1747static void
1748api_schedule_to (pTHX_ SV *coro_sv)
1749{
1750 struct coro_transfer_args ta;
1751 struct coro *next = SvSTATE (coro_sv);
1752
1753 SvREFCNT_inc_NN (coro_sv);
1754 prepare_schedule_to (aTHX_ &ta, next);
1697} 1755}
1698 1756
1699static int 1757static int
1700api_cede (pTHX) 1758api_cede (pTHX)
1701{ 1759{
1730static void 1788static void
1731api_trace (pTHX_ SV *coro_sv, int flags) 1789api_trace (pTHX_ SV *coro_sv, int flags)
1732{ 1790{
1733 struct coro *coro = SvSTATE (coro_sv); 1791 struct coro *coro = SvSTATE (coro_sv);
1734 1792
1793 if (coro->flags & CF_RUNNING)
1794 croak ("cannot enable tracing on a running coroutine, caught");
1795
1735 if (flags & CC_TRACE) 1796 if (flags & CC_TRACE)
1736 { 1797 {
1737 if (!coro->cctx) 1798 if (!coro->cctx)
1738 coro->cctx = cctx_new_run (); 1799 coro->cctx = cctx_new_run ();
1739 else if (!(coro->cctx->flags & CC_TRACE)) 1800 else if (!(coro->cctx->flags & CC_TRACE))
1740 croak ("cannot enable tracing on coroutine with custom stack,"); 1801 croak ("cannot enable tracing on coroutine with custom stack, caught");
1741 1802
1742 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1803 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1743 } 1804 }
1744 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1805 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1745 { 1806 {
1748 if (coro->flags & CF_RUNNING) 1809 if (coro->flags & CF_RUNNING)
1749 PL_runops = RUNOPS_DEFAULT; 1810 PL_runops = RUNOPS_DEFAULT;
1750 else 1811 else
1751 coro->slot->runops = RUNOPS_DEFAULT; 1812 coro->slot->runops = RUNOPS_DEFAULT;
1752 } 1813 }
1814}
1815
1816static void
1817coro_call_on_destroy (pTHX_ struct coro *coro)
1818{
1819 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1820 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1821
1822 if (on_destroyp)
1823 {
1824 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1825
1826 while (AvFILLp (on_destroy) >= 0)
1827 {
1828 dSP; /* don't disturb outer sp */
1829 SV *cb = av_pop (on_destroy);
1830
1831 PUSHMARK (SP);
1832
1833 if (statusp)
1834 {
1835 int i;
1836 AV *status = (AV *)SvRV (*statusp);
1837 EXTEND (SP, AvFILLp (status) + 1);
1838
1839 for (i = 0; i <= AvFILLp (status); ++i)
1840 PUSHs (AvARRAY (status)[i]);
1841 }
1842
1843 PUTBACK;
1844 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1845 }
1846 }
1847}
1848
1849static void
1850slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1851{
1852 int i;
1853 HV *hv = (HV *)SvRV (coro_current);
1854 AV *av = newAV ();
1855
1856 av_extend (av, items - 1);
1857 for (i = 0; i < items; ++i)
1858 av_push (av, SvREFCNT_inc_NN (arg [i]));
1859
1860 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1861
1862 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1863 api_ready (aTHX_ sv_manager);
1864
1865 frame->prepare = prepare_schedule;
1866 frame->check = slf_check_repeat;
1753} 1867}
1754 1868
1755/*****************************************************************************/ 1869/*****************************************************************************/
1756/* async pool handler */ 1870/* async pool handler */
1757 1871
1990static void 2104static void
1991slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2105slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1992{ 2106{
1993 frame->prepare = prepare_schedule; 2107 frame->prepare = prepare_schedule;
1994 frame->check = slf_check_nop; 2108 frame->check = slf_check_nop;
2109}
2110
2111static void
2112slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2113{
2114 struct coro *next = (struct coro *)slf_frame.data;
2115
2116 SvREFCNT_inc_NN (next->hv);
2117 prepare_schedule_to (aTHX_ ta, next);
2118}
2119
2120static void
2121slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2122{
2123 if (!items)
2124 croak ("Coro::schedule_to expects a coroutine argument, caught");
2125
2126 frame->data = (void *)SvSTATE (arg [0]);
2127 frame->prepare = slf_prepare_schedule_to;
2128 frame->check = slf_check_nop;
2129}
2130
2131static void
2132slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2133{
2134 api_ready (aTHX_ SvRV (coro_current));
2135
2136 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1995} 2137}
1996 2138
1997static void 2139static void
1998slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2140slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1999{ 2141{
2145 } 2287 }
2146 else 2288 else
2147 slf_argc = 0; 2289 slf_argc = 0;
2148 2290
2149 PL_op->op_ppaddr = pp_slf; 2291 PL_op->op_ppaddr = pp_slf;
2150 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2292 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2151 2293
2152 PL_op = (OP *)&slf_restore; 2294 PL_op = (OP *)&slf_restore;
2153} 2295}
2154 2296
2155/*****************************************************************************/ 2297/*****************************************************************************/
2238 SV **ary; 2380 SV **ary;
2239 2381
2240 /* unfortunately, building manually saves memory */ 2382 /* unfortunately, building manually saves memory */
2241 Newx (ary, 2, SV *); 2383 Newx (ary, 2, SV *);
2242 AvALLOC (av) = ary; 2384 AvALLOC (av) = ary;
2385#if PERL_VERSION_ATLEAST (5,10,0)
2243 /*AvARRAY (av) = ary;*/ 2386 AvARRAY (av) = ary;
2387#else
2244 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2388 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2389 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2390 SvPVX ((SV *)av) = (char *)ary;
2391#endif
2245 AvMAX (av) = 1; 2392 AvMAX (av) = 1;
2246 AvFILLp (av) = 0; 2393 AvFILLp (av) = 0;
2247 ary [0] = newSViv (count); 2394 ary [0] = newSViv (count);
2248 2395
2249 return newRV_noinc ((SV *)av); 2396 return newRV_noinc ((SV *)av);
2619 XSRETURN_EMPTY; 2766 XSRETURN_EMPTY;
2620} 2767}
2621 2768
2622/*****************************************************************************/ 2769/*****************************************************************************/
2623 2770
2771#if CORO_CLONE
2772# include "clone.c"
2773#endif
2774
2624MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2775MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2625 2776
2626PROTOTYPES: DISABLE 2777PROTOTYPES: DISABLE
2627 2778
2628BOOT: 2779BOOT:
2632 coro_thx = PERL_GET_CONTEXT; 2783 coro_thx = PERL_GET_CONTEXT;
2633# endif 2784# endif
2634#endif 2785#endif
2635 BOOT_PAGESIZE; 2786 BOOT_PAGESIZE;
2636 2787
2788 cctx_current = cctx_new_empty ();
2789
2637 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2790 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2638 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2791 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2639 2792
2640 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2793 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; 2794 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2660 2813
2661 { 2814 {
2662 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2815 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2663 2816
2664 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2817 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2665 hv_store_ent (PL_custom_op_names, slf, 2818 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2666 newSVpv ("coro_slf", 0), 0);
2667 2819
2668 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2820 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2669 hv_store_ent (PL_custom_op_descs, slf, 2821 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2670 newSVpv ("coro schedule like function", 0), 0);
2671 } 2822 }
2672 2823
2673 coroapi.ver = CORO_API_VERSION; 2824 coroapi.ver = CORO_API_VERSION;
2674 coroapi.rev = CORO_API_REVISION; 2825 coroapi.rev = CORO_API_REVISION;
2675 2826
2768void 2919void
2769_exit (int code) 2920_exit (int code)
2770 PROTOTYPE: $ 2921 PROTOTYPE: $
2771 CODE: 2922 CODE:
2772 _exit (code); 2923 _exit (code);
2924
2925SV *
2926clone (Coro::State coro)
2927 CODE:
2928{
2929#if CORO_CLONE
2930 struct coro *ncoro = coro_clone (aTHX_ coro);
2931 MAGIC *mg;
2932 /* TODO: too much duplication */
2933 ncoro->hv = newHV ();
2934 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2935 mg->mg_flags |= MGf_DUP;
2936 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2937#else
2938 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2939#endif
2940}
2941 OUTPUT:
2942 RETVAL
2773 2943
2774int 2944int
2775cctx_stacksize (int new_stacksize = 0) 2945cctx_stacksize (int new_stacksize = 0)
2776 PROTOTYPE: ;$ 2946 PROTOTYPE: ;$
2777 CODE: 2947 CODE:
2898 3068
2899SV * 3069SV *
2900has_cctx (Coro::State coro) 3070has_cctx (Coro::State coro)
2901 PROTOTYPE: $ 3071 PROTOTYPE: $
2902 CODE: 3072 CODE:
2903 RETVAL = boolSV (!!coro->cctx); 3073 /* maybe manage the running flag differently */
3074 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2904 OUTPUT: 3075 OUTPUT:
2905 RETVAL 3076 RETVAL
2906 3077
2907int 3078int
2908is_traced (Coro::State coro) 3079is_traced (Coro::State coro)
2928 3099
2929void 3100void
2930force_cctx () 3101force_cctx ()
2931 PROTOTYPE: 3102 PROTOTYPE:
2932 CODE: 3103 CODE:
2933 SvSTATE_current->cctx->idle_sp = 0; 3104 cctx_current->idle_sp = 0;
2934 3105
2935void 3106void
2936swap_defsv (Coro::State self) 3107swap_defsv (Coro::State self)
2937 PROTOTYPE: $ 3108 PROTOTYPE: $
2938 ALIAS: 3109 ALIAS:
2953 3124
2954BOOT: 3125BOOT:
2955{ 3126{
2956 int i; 3127 int i;
2957 3128
2958 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2959 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3129 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2960 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3130 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2961 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3131 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2962 cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); 3132 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2963 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 3133 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2964 SvREADONLY_on (coro_current); 3134 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3135 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3136 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3137 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
2965 3138
2966 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3139 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
2967 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3140 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2968 cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); 3141 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
2969 cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); 3142 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2970 3143
2971 coro_stash = gv_stashpv ("Coro", TRUE); 3144 coro_stash = gv_stashpv ("Coro", TRUE);
2972 3145
2973 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3146 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2974 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3147 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2982 3155
2983 { 3156 {
2984 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3157 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2985 3158
2986 coroapi.schedule = api_schedule; 3159 coroapi.schedule = api_schedule;
3160 coroapi.schedule_to = api_schedule_to;
2987 coroapi.cede = api_cede; 3161 coroapi.cede = api_cede;
2988 coroapi.cede_notself = api_cede_notself; 3162 coroapi.cede_notself = api_cede_notself;
2989 coroapi.ready = api_ready; 3163 coroapi.ready = api_ready;
2990 coroapi.is_ready = api_is_ready; 3164 coroapi.is_ready = api_is_ready;
2991 coroapi.nready = coro_nready; 3165 coroapi.nready = coro_nready;
2996 SvREADONLY_on (sv); 3170 SvREADONLY_on (sv);
2997 } 3171 }
2998} 3172}
2999 3173
3000void 3174void
3175terminate (...)
3176 CODE:
3177 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3178
3179void
3001schedule (...) 3180schedule (...)
3002 CODE: 3181 CODE:
3003 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3182 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3183
3184void
3185schedule_to (...)
3186 CODE:
3187 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3188
3189void
3190cede_to (...)
3191 CODE:
3192 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
3004 3193
3005void 3194void
3006cede (...) 3195cede (...)
3007 CODE: 3196 CODE:
3008 CORO_EXECUTE_SLF_XS (slf_init_cede); 3197 CORO_EXECUTE_SLF_XS (slf_init_cede);
3009 3198
3010void 3199void
3011cede_notself (...) 3200cede_notself (...)
3012 CODE: 3201 CODE:
3013 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3202 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3203
3204void
3205_cancel (Coro::State self)
3206 CODE:
3207 coro_state_destroy (aTHX_ self);
3208 coro_call_on_destroy (aTHX_ self);
3014 3209
3015void 3210void
3016_set_current (SV *current) 3211_set_current (SV *current)
3017 PROTOTYPE: $ 3212 PROTOTYPE: $
3018 CODE: 3213 CODE:
3089 PUSHMARK (SP); 3284 PUSHMARK (SP);
3090 EXTEND (SP, 2); 3285 EXTEND (SP, 2);
3091 PUSHs (sv_Coro); 3286 PUSHs (sv_Coro);
3092 PUSHs ((SV *)cv_pool_handler); 3287 PUSHs ((SV *)cv_pool_handler);
3093 PUTBACK; 3288 PUTBACK;
3094 call_sv ((SV *)cv_coro_new, G_SCALAR); 3289 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3095 SPAGAIN; 3290 SPAGAIN;
3096 3291
3097 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3292 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
3098 } 3293 }
3099 3294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines