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.318 by root, Thu Nov 20 07:02:43 2008 UTC vs.
Revision 1.343 by root, Mon Dec 15 16:41:43 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
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
175static AV *av_destroy; /* destruction queue */ 178static AV *av_destroy; /* destruction queue */
176static SV *sv_manager; /* the manager coro */ 179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
177 181
178static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
179static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
180static SV *rv_diehook; 184static SV *rv_diehook;
181static SV *rv_warnhook; 185static SV *rv_warnhook;
225 int valgrind_id; 229 int valgrind_id;
226#endif 230#endif
227 unsigned char flags; 231 unsigned char flags;
228} coro_cctx; 232} coro_cctx;
229 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
230enum { 238enum {
231 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
232 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
233 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
234 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
243 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
235}; 244};
236 245
237/* the structure where most of the perl state is stored, overlaid on the cxstack */ 246/* the structure where most of the perl state is stored, overlaid on the cxstack */
238typedef struct 247typedef struct
239{ 248{
240 SV *defsv; 249 SV *defsv;
241 AV *defav; 250 AV *defav;
242 SV *errsv; 251 SV *errsv;
243 SV *irsgv; 252 SV *irsgv;
253 HV *hinthv;
244#define VAR(name,type) type name; 254#define VAR(name,type) type name;
245# include "state.h" 255# include "state.h"
246#undef VAR 256#undef VAR
247} perl_slots; 257} perl_slots;
248 258
275 285
276 /* async_pool */ 286 /* async_pool */
277 SV *saved_deffh; 287 SV *saved_deffh;
278 SV *invoke_cb; 288 SV *invoke_cb;
279 AV *invoke_av; 289 AV *invoke_av;
290
291 /* on_enter/on_leave */
292 AV *on_enter;
293 AV *on_leave;
280 294
281 /* linked list */ 295 /* linked list */
282 struct coro *next, *prev; 296 struct coro *next, *prev;
283}; 297};
284 298
343INLINE CV * 357INLINE CV *
344coro_sv_2cv (pTHX_ SV *sv) 358coro_sv_2cv (pTHX_ SV *sv)
345{ 359{
346 HV *st; 360 HV *st;
347 GV *gvp; 361 GV *gvp;
348 return sv_2cv (sv, &st, &gvp, 0); 362 CV *cv = sv_2cv (sv, &st, &gvp, 0);
363
364 if (!cv)
365 croak ("code reference expected");
366
367 return cv;
349} 368}
369
370/*****************************************************************************/
371/* magic glue */
372
373#define CORO_MAGIC_type_cv 26
374#define CORO_MAGIC_type_state PERL_MAGIC_ext
375
376#define CORO_MAGIC_NN(sv, type) \
377 (expect_true (SvMAGIC (sv)->mg_type == type) \
378 ? SvMAGIC (sv) \
379 : mg_find (sv, type))
380
381#define CORO_MAGIC(sv, type) \
382 (expect_true (SvMAGIC (sv)) \
383 ? CORO_MAGIC_NN (sv, type) \
384 : 0)
385
386#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
387#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
388
389INLINE struct coro *
390SvSTATE_ (pTHX_ SV *coro)
391{
392 HV *stash;
393 MAGIC *mg;
394
395 if (SvROK (coro))
396 coro = SvRV (coro);
397
398 if (expect_false (SvTYPE (coro) != SVt_PVHV))
399 croak ("Coro::State object required");
400
401 stash = SvSTASH (coro);
402 if (expect_false (stash != coro_stash && stash != coro_state_stash))
403 {
404 /* very slow, but rare, check */
405 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
406 croak ("Coro::State object required");
407 }
408
409 mg = CORO_MAGIC_state (coro);
410 return (struct coro *)mg->mg_ptr;
411}
412
413#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
414
415/* faster than SvSTATE, but expects a coroutine hv */
416#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
417#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
418
419/*****************************************************************************/
420/* padlist management and caching */
350 421
351static AV * 422static AV *
352coro_clone_padlist (pTHX_ CV *cv) 423coro_derive_padlist (pTHX_ CV *cv)
353{ 424{
354 AV *padlist = CvPADLIST (cv); 425 AV *padlist = CvPADLIST (cv);
355 AV *newpadlist, *newpad; 426 AV *newpadlist, *newpad;
356 427
357 newpadlist = newAV (); 428 newpadlist = newAV ();
362 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 433 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
363#endif 434#endif
364 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 435 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
365 --AvFILLp (padlist); 436 --AvFILLp (padlist);
366 437
367 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 438 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
368 av_store (newpadlist, 1, (SV *)newpad); 439 av_store (newpadlist, 1, (SV *)newpad);
369 440
370 return newpadlist; 441 return newpadlist;
371} 442}
372 443
373static void 444static void
374free_padlist (pTHX_ AV *padlist) 445free_padlist (pTHX_ AV *padlist)
375{ 446{
376 /* may be during global destruction */ 447 /* may be during global destruction */
377 if (SvREFCNT (padlist)) 448 if (!IN_DESTRUCT)
378 { 449 {
379 I32 i = AvFILLp (padlist); 450 I32 i = AvFILLp (padlist);
380 while (i >= 0) 451
452 while (i > 0) /* special-case index 0 */
381 { 453 {
382 SV **svp = av_fetch (padlist, i--, FALSE); 454 /* we try to be extra-careful here */
383 if (svp) 455 AV *av = (AV *)AvARRAY (padlist)[i--];
384 { 456 I32 j = AvFILLp (av);
385 SV *sv; 457
386 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 458 while (j >= 0)
459 SvREFCNT_dec (AvARRAY (av)[j--]);
460
461 AvFILLp (av) = -1;
387 SvREFCNT_dec (sv); 462 SvREFCNT_dec (av);
388
389 SvREFCNT_dec (*svp);
390 }
391 } 463 }
392 464
465 SvREFCNT_dec (AvARRAY (padlist)[0]);
466
467 AvFILLp (padlist) = -1;
393 SvREFCNT_dec ((SV*)padlist); 468 SvREFCNT_dec ((SV*)padlist);
394 } 469 }
395} 470}
396 471
397static int 472static int
406 481
407 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 482 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
408 483
409 return 0; 484 return 0;
410} 485}
411
412#define CORO_MAGIC_type_cv 26
413#define CORO_MAGIC_type_state PERL_MAGIC_ext
414 486
415static MGVTBL coro_cv_vtbl = { 487static MGVTBL coro_cv_vtbl = {
416 0, 0, 0, 0, 488 0, 0, 0, 0,
417 coro_cv_free 489 coro_cv_free
418}; 490};
419
420#define CORO_MAGIC_NN(sv, type) \
421 (expect_true (SvMAGIC (sv)->mg_type == type) \
422 ? SvMAGIC (sv) \
423 : mg_find (sv, type))
424
425#define CORO_MAGIC(sv, type) \
426 (expect_true (SvMAGIC (sv)) \
427 ? CORO_MAGIC_NN (sv, type) \
428 : 0)
429
430#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
431#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
432
433INLINE struct coro *
434SvSTATE_ (pTHX_ SV *coro)
435{
436 HV *stash;
437 MAGIC *mg;
438
439 if (SvROK (coro))
440 coro = SvRV (coro);
441
442 if (expect_false (SvTYPE (coro) != SVt_PVHV))
443 croak ("Coro::State object required");
444
445 stash = SvSTASH (coro);
446 if (expect_false (stash != coro_stash && stash != coro_state_stash))
447 {
448 /* very slow, but rare, check */
449 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
450 croak ("Coro::State object required");
451 }
452
453 mg = CORO_MAGIC_state (coro);
454 return (struct coro *)mg->mg_ptr;
455}
456
457#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
458
459/* faster than SvSTATE, but expects a coroutine hv */
460#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
461#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
462 491
463/* the next two functions merely cache the padlists */ 492/* the next two functions merely cache the padlists */
464static void 493static void
465get_padlist (pTHX_ CV *cv) 494get_padlist (pTHX_ CV *cv)
466{ 495{
477 CV *cp = Perl_cv_clone (aTHX_ cv); 506 CV *cp = Perl_cv_clone (aTHX_ cv);
478 CvPADLIST (cv) = CvPADLIST (cp); 507 CvPADLIST (cv) = CvPADLIST (cp);
479 CvPADLIST (cp) = 0; 508 CvPADLIST (cp) = 0;
480 SvREFCNT_dec (cp); 509 SvREFCNT_dec (cp);
481#else 510#else
482 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 511 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
483#endif 512#endif
484 } 513 }
485} 514}
486 515
487static void 516static void
494 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 523 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
495 524
496 av = (AV *)mg->mg_obj; 525 av = (AV *)mg->mg_obj;
497 526
498 if (expect_false (AvFILLp (av) >= AvMAX (av))) 527 if (expect_false (AvFILLp (av) >= AvMAX (av)))
499 av_extend (av, AvMAX (av) + 1); 528 av_extend (av, AvFILLp (av) + 1);
500 529
501 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 530 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
502} 531}
503 532
504/** load & save, init *******************************************************/ 533/** load & save, init *******************************************************/
534
535static void
536on_enterleave_call (pTHX_ SV *cb);
505 537
506static void 538static void
507load_perl (pTHX_ Coro__State c) 539load_perl (pTHX_ Coro__State c)
508{ 540{
509 perl_slots *slot = c->slot; 541 perl_slots *slot = c->slot;
510 c->slot = 0; 542 c->slot = 0;
511 543
512 PL_mainstack = c->mainstack; 544 PL_mainstack = c->mainstack;
513 545
514 GvSV (PL_defgv) = slot->defsv; 546 GvSV (PL_defgv) = slot->defsv;
515 GvAV (PL_defgv) = slot->defav; 547 GvAV (PL_defgv) = slot->defav;
516 GvSV (PL_errgv) = slot->errsv; 548 GvSV (PL_errgv) = slot->errsv;
517 GvSV (irsgv) = slot->irsgv; 549 GvSV (irsgv) = slot->irsgv;
550 GvHV (PL_hintgv) = slot->hinthv;
518 551
519 #define VAR(name,type) PL_ ## name = slot->name; 552 #define VAR(name,type) PL_ ## name = slot->name;
520 # include "state.h" 553 # include "state.h"
521 #undef VAR 554 #undef VAR
522 555
536 PUTBACK; 569 PUTBACK;
537 } 570 }
538 571
539 slf_frame = c->slf_frame; 572 slf_frame = c->slf_frame;
540 CORO_THROW = c->except; 573 CORO_THROW = c->except;
574
575 if (expect_false (c->on_enter))
576 {
577 int i;
578
579 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
580 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
581 }
541} 582}
542 583
543static void 584static void
544save_perl (pTHX_ Coro__State c) 585save_perl (pTHX_ Coro__State c)
545{ 586{
587 if (expect_false (c->on_leave))
588 {
589 int i;
590
591 for (i = AvFILLp (c->on_leave); i >= 0; --i)
592 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
593 }
594
546 c->except = CORO_THROW; 595 c->except = CORO_THROW;
547 c->slf_frame = slf_frame; 596 c->slf_frame = slf_frame;
548 597
549 { 598 {
550 dSP; 599 dSP;
608 c->mainstack = PL_mainstack; 657 c->mainstack = PL_mainstack;
609 658
610 { 659 {
611 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 660 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
612 661
613 slot->defav = GvAV (PL_defgv); 662 slot->defav = GvAV (PL_defgv);
614 slot->defsv = DEFSV; 663 slot->defsv = DEFSV;
615 slot->errsv = ERRSV; 664 slot->errsv = ERRSV;
616 slot->irsgv = GvSV (irsgv); 665 slot->irsgv = GvSV (irsgv);
666 slot->hinthv = GvHV (PL_hintgv);
617 667
618 #define VAR(name,type) slot->name = PL_ ## name; 668 #define VAR(name,type) slot->name = PL_ ## name;
619 # include "state.h" 669 # include "state.h"
620 #undef VAR 670 #undef VAR
621 } 671 }
748#endif 798#endif
749 799
750/* 800/*
751 * This overrides the default magic get method of %SIG elements. 801 * This overrides the default magic get method of %SIG elements.
752 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 802 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
753 * and instead of tryign to save and restore the hash elements, we just provide 803 * and instead of trying to save and restore the hash elements, we just provide
754 * readback here. 804 * readback here.
755 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
756 * not expecting this to actually change the hook. This is a potential problem
757 * when a schedule happens then, but we ignore this.
758 */ 805 */
759static int 806static int
760coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 807coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
761{ 808{
762 const char *s = MgPV_nolen_const (mg); 809 const char *s = MgPV_nolen_const (mg);
815 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 862 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
816 863
817 if (svp) 864 if (svp)
818 { 865 {
819 SV *old = *svp; 866 SV *old = *svp;
820 *svp = newSVsv (sv); 867 *svp = SvOK (sv) ? newSVsv (sv) : 0;
821 SvREFCNT_dec (old); 868 SvREFCNT_dec (old);
822 return 0; 869 return 0;
823 } 870 }
824 } 871 }
825 872
857 904
858 PL_runops = RUNOPS_DEFAULT; 905 PL_runops = RUNOPS_DEFAULT;
859 PL_curcop = &PL_compiling; 906 PL_curcop = &PL_compiling;
860 PL_in_eval = EVAL_NULL; 907 PL_in_eval = EVAL_NULL;
861 PL_comppad = 0; 908 PL_comppad = 0;
909 PL_comppad_name = 0;
910 PL_comppad_name_fill = 0;
911 PL_comppad_name_floor = 0;
862 PL_curpm = 0; 912 PL_curpm = 0;
863 PL_curpad = 0; 913 PL_curpad = 0;
864 PL_localizing = 0; 914 PL_localizing = 0;
865 PL_dirty = 0; 915 PL_dirty = 0;
866 PL_restartop = 0; 916 PL_restartop = 0;
867#if PERL_VERSION_ATLEAST (5,10,0) 917#if PERL_VERSION_ATLEAST (5,10,0)
868 PL_parser = 0; 918 PL_parser = 0;
869#endif 919#endif
920 PL_hints = 0;
870 921
871 /* recreate the die/warn hooks */ 922 /* recreate the die/warn hooks */
872 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 923 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
873 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 924 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
874 925
875 GvSV (PL_defgv) = newSV (0); 926 GvSV (PL_defgv) = newSV (0);
876 GvAV (PL_defgv) = coro->args; coro->args = 0; 927 GvAV (PL_defgv) = coro->args; coro->args = 0;
877 GvSV (PL_errgv) = newSV (0); 928 GvSV (PL_errgv) = newSV (0);
878 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 929 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
930 GvHV (PL_hintgv) = 0;
879 PL_rs = newSVsv (GvSV (irsgv)); 931 PL_rs = newSVsv (GvSV (irsgv));
880 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 932 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
881 933
882 { 934 {
883 dSP; 935 dSP;
901 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 953 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
902 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 954 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
903 955
904 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 956 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
905 coro_setup_op.op_next = PL_op; 957 coro_setup_op.op_next = PL_op;
906 coro_setup_op.op_type = OP_CUSTOM; 958 coro_setup_op.op_type = OP_ENTERSUB;
907 coro_setup_op.op_ppaddr = pp_slf; 959 coro_setup_op.op_ppaddr = pp_slf;
908 /* no flags etc. required, as an init function won't be called */ 960 /* no flags etc. required, as an init function won't be called */
909 961
910 PL_op = (OP *)&coro_setup_op; 962 PL_op = (OP *)&coro_setup_op;
911 963
912 /* copy throw, in case it was set before coro_setup */ 964 /* copy throw, in case it was set before coro_setup */
913 CORO_THROW = coro->except; 965 CORO_THROW = coro->except;
914} 966}
915 967
916static void 968static void
917coro_destruct (pTHX_ struct coro *coro) 969coro_unwind_stacks (pTHX)
918{ 970{
919 if (!IN_DESTRUCT) 971 if (!IN_DESTRUCT)
920 { 972 {
921 /* restore all saved variables and stuff */ 973 /* restore all saved variables and stuff */
922 LEAVE_SCOPE (0); 974 LEAVE_SCOPE (0);
930 POPSTACK_TO (PL_mainstack); 982 POPSTACK_TO (PL_mainstack);
931 983
932 /* unwind main stack */ 984 /* unwind main stack */
933 dounwind (-1); 985 dounwind (-1);
934 } 986 }
987}
988
989static void
990coro_destruct_perl (pTHX_ struct coro *coro)
991{
992 coro_unwind_stacks (aTHX);
935 993
936 SvREFCNT_dec (GvSV (PL_defgv)); 994 SvREFCNT_dec (GvSV (PL_defgv));
937 SvREFCNT_dec (GvAV (PL_defgv)); 995 SvREFCNT_dec (GvAV (PL_defgv));
938 SvREFCNT_dec (GvSV (PL_errgv)); 996 SvREFCNT_dec (GvSV (PL_errgv));
939 SvREFCNT_dec (PL_defoutgv); 997 SvREFCNT_dec (PL_defoutgv);
940 SvREFCNT_dec (PL_rs); 998 SvREFCNT_dec (PL_rs);
941 SvREFCNT_dec (GvSV (irsgv)); 999 SvREFCNT_dec (GvSV (irsgv));
1000 SvREFCNT_dec (GvHV (PL_hintgv));
942 1001
943 SvREFCNT_dec (PL_diehook); 1002 SvREFCNT_dec (PL_diehook);
944 SvREFCNT_dec (PL_warnhook); 1003 SvREFCNT_dec (PL_warnhook);
945 1004
946 SvREFCNT_dec (coro->saved_deffh); 1005 SvREFCNT_dec (coro->saved_deffh);
964static int 1023static int
965runops_trace (pTHX) 1024runops_trace (pTHX)
966{ 1025{
967 COP *oldcop = 0; 1026 COP *oldcop = 0;
968 int oldcxix = -2; 1027 int oldcxix = -2;
969 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
970 coro_cctx *cctx = coro->cctx;
971 1028
972 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1029 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
973 { 1030 {
974 PERL_ASYNC_CHECK (); 1031 PERL_ASYNC_CHECK ();
975 1032
976 if (cctx->flags & CC_TRACE_ALL) 1033 if (cctx_current->flags & CC_TRACE_ALL)
977 { 1034 {
978 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1035 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
979 { 1036 {
980 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1037 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
981 SV **bot, **top; 1038 SV **bot, **top;
982 AV *av = newAV (); /* return values */ 1039 AV *av = newAV (); /* return values */
983 SV **cb; 1040 SV **cb;
1020 1077
1021 if (PL_curcop != &PL_compiling) 1078 if (PL_curcop != &PL_compiling)
1022 { 1079 {
1023 SV **cb; 1080 SV **cb;
1024 1081
1025 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1082 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1026 { 1083 {
1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1084 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1028 1085
1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1086 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1030 { 1087 {
1031 runops_proc_t old_runops = PL_runops;
1032 dSP; 1088 dSP;
1033 GV *gv = CvGV (cx->blk_sub.cv); 1089 GV *gv = CvGV (cx->blk_sub.cv);
1034 SV *fullname = sv_2mortal (newSV (0)); 1090 SV *fullname = sv_2mortal (newSV (0));
1035 1091
1036 if (isGV (gv)) 1092 if (isGV (gv))
1054 } 1110 }
1055 1111
1056 oldcxix = cxstack_ix; 1112 oldcxix = cxstack_ix;
1057 } 1113 }
1058 1114
1059 if (cctx->flags & CC_TRACE_LINE) 1115 if (cctx_current->flags & CC_TRACE_LINE)
1060 { 1116 {
1061 dSP; 1117 dSP;
1062 1118
1063 PL_runops = RUNOPS_DEFAULT; 1119 PL_runops = RUNOPS_DEFAULT;
1064 ENTER; 1120 ENTER;
1083 1139
1084 TAINT_NOT; 1140 TAINT_NOT;
1085 return 0; 1141 return 0;
1086} 1142}
1087 1143
1088static struct coro_cctx *cctx_ssl_cctx;
1089static struct CoroSLF cctx_ssl_frame; 1144static struct CoroSLF cctx_ssl_frame;
1090 1145
1091static void 1146static void
1092slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1147slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1093{ 1148{
1094 ta->prev = (struct coro *)cctx_ssl_cctx;
1095 ta->next = 0; 1149 ta->prev = 0;
1096} 1150}
1097 1151
1098static int 1152static int
1099slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1153slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1100{ 1154{
1103 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1157 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1104} 1158}
1105 1159
1106/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1160/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1107static void NOINLINE 1161static void NOINLINE
1108cctx_prepare (pTHX_ coro_cctx *cctx) 1162cctx_prepare (pTHX)
1109{ 1163{
1110 PL_top_env = &PL_start_env; 1164 PL_top_env = &PL_start_env;
1111 1165
1112 if (cctx->flags & CC_TRACE) 1166 if (cctx_current->flags & CC_TRACE)
1113 PL_runops = runops_trace; 1167 PL_runops = runops_trace;
1114 1168
1115 /* we already must be executing an SLF op, there is no other valid way 1169 /* we already must be executing an SLF op, there is no other valid way
1116 * that can lead to creation of a new cctx */ 1170 * that can lead to creation of a new cctx */
1117 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1171 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1118 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1172 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1119 1173
1120 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1174 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1121 cctx_ssl_cctx = cctx;
1122 cctx_ssl_frame = slf_frame; 1175 cctx_ssl_frame = slf_frame;
1123 1176
1124 slf_frame.prepare = slf_prepare_set_stacklevel; 1177 slf_frame.prepare = slf_prepare_set_stacklevel;
1125 slf_frame.check = slf_check_set_stacklevel; 1178 slf_frame.check = slf_check_set_stacklevel;
1126} 1179}
1149 /* normally we would need to skip the entersub here */ 1202 /* normally we would need to skip the entersub here */
1150 /* not doing so will re-execute it, which is exactly what we want */ 1203 /* not doing so will re-execute it, which is exactly what we want */
1151 /* PL_nop = PL_nop->op_next */ 1204 /* PL_nop = PL_nop->op_next */
1152 1205
1153 /* inject a fake subroutine call to cctx_init */ 1206 /* inject a fake subroutine call to cctx_init */
1154 cctx_prepare (aTHX_ (coro_cctx *)arg); 1207 cctx_prepare (aTHX);
1155 1208
1156 /* cctx_run is the alternative tail of transfer() */ 1209 /* cctx_run is the alternative tail of transfer() */
1157 transfer_tail (aTHX); 1210 transfer_tail (aTHX);
1158 1211
1159 /* somebody or something will hit me for both perl_run and PL_restartop */ 1212 /* somebody or something will hit me for both perl_run and PL_restartop */
1167 /* 1220 /*
1168 * If perl-run returns we assume exit() was being called or the coro 1221 * If perl-run returns we assume exit() was being called or the coro
1169 * fell off the end, which seems to be the only valid (non-bug) 1222 * fell off the end, which seems to be the only valid (non-bug)
1170 * reason for perl_run to return. We try to exit by jumping to the 1223 * reason for perl_run to return. We try to exit by jumping to the
1171 * bootstrap-time "top" top_env, as we cannot restore the "main" 1224 * bootstrap-time "top" top_env, as we cannot restore the "main"
1172 * coroutine as Coro has no such concept 1225 * coroutine as Coro has no such concept.
1226 * This actually isn't valid with the pthread backend, but OSes requiring
1227 * that backend are too broken to do it in a standards-compliant way.
1173 */ 1228 */
1174 PL_top_env = main_top_env; 1229 PL_top_env = main_top_env;
1175 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1230 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1176 } 1231 }
1177} 1232}
1254cctx_destroy (coro_cctx *cctx) 1309cctx_destroy (coro_cctx *cctx)
1255{ 1310{
1256 if (!cctx) 1311 if (!cctx)
1257 return; 1312 return;
1258 1313
1314 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1315
1259 --cctx_count; 1316 --cctx_count;
1260 coro_destroy (&cctx->cctx); 1317 coro_destroy (&cctx->cctx);
1261 1318
1262 /* coro_transfer creates new, empty cctx's */ 1319 /* coro_transfer creates new, empty cctx's */
1263 if (cctx->sptr) 1320 if (cctx->sptr)
1326 /* TODO: throwing up here is considered harmful */ 1383 /* TODO: throwing up here is considered harmful */
1327 1384
1328 if (expect_true (prev != next)) 1385 if (expect_true (prev != next))
1329 { 1386 {
1330 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1387 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1331 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1388 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1332 1389
1333 if (expect_false (next->flags & CF_RUNNING))
1334 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1335
1336 if (expect_false (next->flags & CF_DESTROYED)) 1390 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1337 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1391 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1338 1392
1339#if !PERL_VERSION_ATLEAST (5,10,0) 1393#if !PERL_VERSION_ATLEAST (5,10,0)
1340 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1394 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1341 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1395 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1342#endif 1396#endif
1348transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1402transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1349{ 1403{
1350 dSTACKLEVEL; 1404 dSTACKLEVEL;
1351 1405
1352 /* sometimes transfer is only called to set idle_sp */ 1406 /* sometimes transfer is only called to set idle_sp */
1353 if (expect_false (!next)) 1407 if (expect_false (!prev))
1354 { 1408 {
1355 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1409 cctx_current->idle_sp = STACKLEVEL;
1356 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1410 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1357 } 1411 }
1358 else if (expect_true (prev != next)) 1412 else if (expect_true (prev != next))
1359 { 1413 {
1360 coro_cctx *prev__cctx; 1414 coro_cctx *cctx_prev;
1361 1415
1362 if (expect_false (prev->flags & CF_NEW)) 1416 if (expect_false (prev->flags & CF_NEW))
1363 { 1417 {
1364 /* create a new empty/source context */ 1418 /* create a new empty/source context */
1365 prev->cctx = cctx_new_empty ();
1366 prev->flags &= ~CF_NEW; 1419 prev->flags &= ~CF_NEW;
1367 prev->flags |= CF_RUNNING; 1420 prev->flags |= CF_RUNNING;
1368 } 1421 }
1369 1422
1370 prev->flags &= ~CF_RUNNING; 1423 prev->flags &= ~CF_RUNNING;
1381 coro_setup (aTHX_ next); 1434 coro_setup (aTHX_ next);
1382 } 1435 }
1383 else 1436 else
1384 load_perl (aTHX_ next); 1437 load_perl (aTHX_ next);
1385 1438
1386 prev__cctx = prev->cctx;
1387
1388 /* possibly untie and reuse the cctx */ 1439 /* possibly untie and reuse the cctx */
1389 if (expect_true ( 1440 if (expect_true (
1390 prev__cctx->idle_sp == STACKLEVEL 1441 cctx_current->idle_sp == STACKLEVEL
1391 && !(prev__cctx->flags & CC_TRACE) 1442 && !(cctx_current->flags & CC_TRACE)
1392 && !force_cctx 1443 && !force_cctx
1393 )) 1444 ))
1394 { 1445 {
1395 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1446 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1396 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1447 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1397 1448
1398 prev->cctx = 0;
1399
1400 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1449 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1401 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1450 /* without this the next cctx_get might destroy the running cctx while still in use */
1402 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1451 if (expect_false (CCTX_EXPIRED (cctx_current)))
1403 if (!next->cctx) 1452 if (expect_true (!next->cctx))
1404 next->cctx = cctx_get (aTHX); 1453 next->cctx = cctx_get (aTHX);
1405 1454
1406 cctx_put (prev__cctx); 1455 cctx_put (cctx_current);
1407 } 1456 }
1457 else
1458 prev->cctx = cctx_current;
1408 1459
1409 ++next->usecount; 1460 ++next->usecount;
1410 1461
1411 if (expect_true (!next->cctx)) 1462 cctx_prev = cctx_current;
1412 next->cctx = cctx_get (aTHX); 1463 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1413 1464
1414 if (expect_false (prev__cctx != next->cctx)) 1465 next->cctx = 0;
1466
1467 if (expect_false (cctx_prev != cctx_current))
1415 { 1468 {
1416 prev__cctx->top_env = PL_top_env; 1469 cctx_prev->top_env = PL_top_env;
1417 PL_top_env = next->cctx->top_env; 1470 PL_top_env = cctx_current->top_env;
1418 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1471 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1419 } 1472 }
1420 1473
1421 transfer_tail (aTHX); 1474 transfer_tail (aTHX);
1422 } 1475 }
1423} 1476}
1445 --coro_nready; 1498 --coro_nready;
1446 } 1499 }
1447 else 1500 else
1448 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1501 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1449 1502
1450 if (coro->mainstack && coro->mainstack != main_mainstack) 1503 if (coro->mainstack
1504 && coro->mainstack != main_mainstack
1505 && coro->slot
1506 && !PL_dirty)
1451 { 1507 {
1452 struct coro temp; 1508 struct coro *current = SvSTATE_current;
1453 1509
1454 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1510 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1455 1511
1456 save_perl (aTHX_ &temp); 1512 save_perl (aTHX_ current);
1457 load_perl (aTHX_ coro); 1513 load_perl (aTHX_ coro);
1458 1514
1459 coro_destruct (aTHX_ coro); 1515 coro_destruct_perl (aTHX_ coro);
1460 1516
1461 load_perl (aTHX_ &temp); 1517 load_perl (aTHX_ current);
1462 1518
1463 coro->slot = 0; 1519 coro->slot = 0;
1464 } 1520 }
1465 1521
1466 cctx_destroy (coro->cctx); 1522 cctx_destroy (coro->cctx);
1578{ 1634{
1579 struct coro *coro; 1635 struct coro *coro;
1580 SV *sv_hook; 1636 SV *sv_hook;
1581 void (*xs_hook)(void); 1637 void (*xs_hook)(void);
1582 1638
1583 if (SvROK (coro_sv))
1584 coro_sv = SvRV (coro_sv);
1585
1586 coro = SvSTATE (coro_sv); 1639 coro = SvSTATE (coro_sv);
1587 1640
1588 if (coro->flags & CF_READY) 1641 if (coro->flags & CF_READY)
1589 return 0; 1642 return 0;
1590 1643
1650 if (expect_true (next_sv)) 1703 if (expect_true (next_sv))
1651 { 1704 {
1652 struct coro *next = SvSTATE_hv (next_sv); 1705 struct coro *next = SvSTATE_hv (next_sv);
1653 1706
1654 /* cannot transfer to destroyed coros, skip and look for next */ 1707 /* cannot transfer to destroyed coros, skip and look for next */
1655 if (expect_false (next->flags & CF_DESTROYED)) 1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1656 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1709 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1657 else 1710 else
1658 { 1711 {
1659 next->flags &= ~CF_READY; 1712 next->flags &= ~CF_READY;
1660 --coro_nready; 1713 --coro_nready;
1661 1714
1662 return prepare_schedule_to (aTHX_ ta, next); 1715 prepare_schedule_to (aTHX_ ta, next);
1716 break;
1663 } 1717 }
1664 } 1718 }
1665 else 1719 else
1666 { 1720 {
1667 /* nothing to schedule: call the idle handler */ 1721 /* nothing to schedule: call the idle handler */
1722 if (SvROK (sv_idle)
1723 && SvOBJECT (SvRV (sv_idle)))
1724 {
1725 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1726 api_ready (aTHX_ SvRV (sv_idle));
1727 --coro_nready;
1728 }
1729 else
1730 {
1668 dSP; 1731 dSP;
1669 1732
1670 ENTER; 1733 ENTER;
1671 SAVETMPS; 1734 SAVETMPS;
1672 1735
1673 PUSHMARK (SP); 1736 PUSHMARK (SP);
1674 PUTBACK; 1737 PUTBACK;
1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1738 call_sv (sv_idle, G_VOID | G_DISCARD);
1676 1739
1677 FREETMPS; 1740 FREETMPS;
1678 LEAVE; 1741 LEAVE;
1742 }
1679 } 1743 }
1680 } 1744 }
1681} 1745}
1682 1746
1683INLINE void 1747INLINE void
1754static void 1818static void
1755api_trace (pTHX_ SV *coro_sv, int flags) 1819api_trace (pTHX_ SV *coro_sv, int flags)
1756{ 1820{
1757 struct coro *coro = SvSTATE (coro_sv); 1821 struct coro *coro = SvSTATE (coro_sv);
1758 1822
1823 if (coro->flags & CF_RUNNING)
1824 croak ("cannot enable tracing on a running coroutine, caught");
1825
1759 if (flags & CC_TRACE) 1826 if (flags & CC_TRACE)
1760 { 1827 {
1761 if (!coro->cctx) 1828 if (!coro->cctx)
1762 coro->cctx = cctx_new_run (); 1829 coro->cctx = cctx_new_run ();
1763 else if (!(coro->cctx->flags & CC_TRACE)) 1830 else if (!(coro->cctx->flags & CC_TRACE))
1764 croak ("cannot enable tracing on coroutine with custom stack,"); 1831 croak ("cannot enable tracing on coroutine with custom stack, caught");
1765 1832
1766 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1833 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1767 } 1834 }
1768 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1835 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1769 { 1836 {
1825 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1892 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1826 api_ready (aTHX_ sv_manager); 1893 api_ready (aTHX_ sv_manager);
1827 1894
1828 frame->prepare = prepare_schedule; 1895 frame->prepare = prepare_schedule;
1829 frame->check = slf_check_repeat; 1896 frame->check = slf_check_repeat;
1897
1898 /* as a minor optimisation, we could unwind all stacks here */
1899 /* but that puts extra pressure on pp_slf, and is not worth much */
1900 /*coro_unwind_stacks (aTHX);*/
1830} 1901}
1831 1902
1832/*****************************************************************************/ 1903/*****************************************************************************/
1833/* async pool handler */ 1904/* async pool handler */
1834 1905
2250 } 2321 }
2251 else 2322 else
2252 slf_argc = 0; 2323 slf_argc = 0;
2253 2324
2254 PL_op->op_ppaddr = pp_slf; 2325 PL_op->op_ppaddr = pp_slf;
2255 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2326 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2256 2327
2257 PL_op = (OP *)&slf_restore; 2328 PL_op = (OP *)&slf_restore;
2329}
2330
2331/*****************************************************************************/
2332/* dynamic wind */
2333
2334static void
2335on_enterleave_call (pTHX_ SV *cb)
2336{
2337 dSP;
2338
2339 PUSHSTACK;
2340
2341 PUSHMARK (SP);
2342 PUTBACK;
2343 call_sv (cb, G_VOID | G_DISCARD);
2344 SPAGAIN;
2345
2346 POPSTACK;
2347}
2348
2349static SV *
2350coro_avp_pop_and_free (pTHX_ AV **avp)
2351{
2352 AV *av = *avp;
2353 SV *res = av_pop (av);
2354
2355 if (AvFILLp (av) < 0)
2356 {
2357 *avp = 0;
2358 SvREFCNT_dec (av);
2359 }
2360
2361 return res;
2362}
2363
2364static void
2365coro_pop_on_enter (pTHX_ void *coro)
2366{
2367 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2368 SvREFCNT_dec (cb);
2369}
2370
2371static void
2372coro_pop_on_leave (pTHX_ void *coro)
2373{
2374 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2375 on_enterleave_call (aTHX_ sv_2mortal (cb));
2258} 2376}
2259 2377
2260/*****************************************************************************/ 2378/*****************************************************************************/
2261/* PerlIO::cede */ 2379/* PerlIO::cede */
2262 2380
2343 SV **ary; 2461 SV **ary;
2344 2462
2345 /* unfortunately, building manually saves memory */ 2463 /* unfortunately, building manually saves memory */
2346 Newx (ary, 2, SV *); 2464 Newx (ary, 2, SV *);
2347 AvALLOC (av) = ary; 2465 AvALLOC (av) = ary;
2466#if PERL_VERSION_ATLEAST (5,10,0)
2348 /*AvARRAY (av) = ary;*/ 2467 AvARRAY (av) = ary;
2468#else
2349 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2469 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2470 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2471 SvPVX ((SV *)av) = (char *)ary;
2472#endif
2350 AvMAX (av) = 1; 2473 AvMAX (av) = 1;
2351 AvFILLp (av) = 0; 2474 AvFILLp (av) = 0;
2352 ary [0] = newSViv (count); 2475 ary [0] = newSViv (count);
2353 2476
2354 return newRV_noinc ((SV *)av); 2477 return newRV_noinc ((SV *)av);
2724 XSRETURN_EMPTY; 2847 XSRETURN_EMPTY;
2725} 2848}
2726 2849
2727/*****************************************************************************/ 2850/*****************************************************************************/
2728 2851
2852#if CORO_CLONE
2853# include "clone.c"
2854#endif
2855
2729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2856MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2730 2857
2731PROTOTYPES: DISABLE 2858PROTOTYPES: DISABLE
2732 2859
2733BOOT: 2860BOOT:
2737 coro_thx = PERL_GET_CONTEXT; 2864 coro_thx = PERL_GET_CONTEXT;
2738# endif 2865# endif
2739#endif 2866#endif
2740 BOOT_PAGESIZE; 2867 BOOT_PAGESIZE;
2741 2868
2869 cctx_current = cctx_new_empty ();
2870
2742 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2871 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2743 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2872 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2744 2873
2745 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2874 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2746 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2875 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2765 2894
2766 { 2895 {
2767 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2896 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2768 2897
2769 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2898 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2770 hv_store_ent (PL_custom_op_names, slf, 2899 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2771 newSVpv ("coro_slf", 0), 0);
2772 2900
2773 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2901 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2774 hv_store_ent (PL_custom_op_descs, slf, 2902 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2775 newSVpv ("coro schedule like function", 0), 0);
2776 } 2903 }
2777 2904
2778 coroapi.ver = CORO_API_VERSION; 2905 coroapi.ver = CORO_API_VERSION;
2779 coroapi.rev = CORO_API_REVISION; 2906 coroapi.rev = CORO_API_REVISION;
2780 2907
2873void 3000void
2874_exit (int code) 3001_exit (int code)
2875 PROTOTYPE: $ 3002 PROTOTYPE: $
2876 CODE: 3003 CODE:
2877 _exit (code); 3004 _exit (code);
3005
3006SV *
3007clone (Coro::State coro)
3008 CODE:
3009{
3010#if CORO_CLONE
3011 struct coro *ncoro = coro_clone (aTHX_ coro);
3012 MAGIC *mg;
3013 /* TODO: too much duplication */
3014 ncoro->hv = newHV ();
3015 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
3016 mg->mg_flags |= MGf_DUP;
3017 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3018#else
3019 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3020#endif
3021}
3022 OUTPUT:
3023 RETVAL
2878 3024
2879int 3025int
2880cctx_stacksize (int new_stacksize = 0) 3026cctx_stacksize (int new_stacksize = 0)
2881 PROTOTYPE: ;$ 3027 PROTOTYPE: ;$
2882 CODE: 3028 CODE:
2932 eval = 1 3078 eval = 1
2933 CODE: 3079 CODE:
2934{ 3080{
2935 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3081 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2936 { 3082 {
2937 struct coro temp; 3083 struct coro *current = SvSTATE_current;
2938 3084
2939 if (!(coro->flags & CF_RUNNING)) 3085 if (current != coro)
2940 { 3086 {
2941 PUTBACK; 3087 PUTBACK;
2942 save_perl (aTHX_ &temp); 3088 save_perl (aTHX_ current);
2943 load_perl (aTHX_ coro); 3089 load_perl (aTHX_ coro);
3090 SPAGAIN;
2944 } 3091 }
2945 3092
2946 {
2947 dSP;
2948 ENTER;
2949 SAVETMPS;
2950 PUTBACK;
2951 PUSHSTACK; 3093 PUSHSTACK;
3094
2952 PUSHMARK (SP); 3095 PUSHMARK (SP);
3096 PUTBACK;
2953 3097
2954 if (ix) 3098 if (ix)
2955 eval_sv (coderef, 0); 3099 eval_sv (coderef, 0);
2956 else 3100 else
2957 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3101 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2958 3102
2959 POPSTACK; 3103 POPSTACK;
2960 SPAGAIN; 3104 SPAGAIN;
2961 FREETMPS;
2962 LEAVE;
2963 PUTBACK;
2964 }
2965 3105
2966 if (!(coro->flags & CF_RUNNING)) 3106 if (current != coro)
2967 { 3107 {
3108 PUTBACK;
2968 save_perl (aTHX_ coro); 3109 save_perl (aTHX_ coro);
2969 load_perl (aTHX_ &temp); 3110 load_perl (aTHX_ current);
2970 SPAGAIN; 3111 SPAGAIN;
2971 } 3112 }
2972 } 3113 }
2973} 3114}
2974 3115
2978 ALIAS: 3119 ALIAS:
2979 is_ready = CF_READY 3120 is_ready = CF_READY
2980 is_running = CF_RUNNING 3121 is_running = CF_RUNNING
2981 is_new = CF_NEW 3122 is_new = CF_NEW
2982 is_destroyed = CF_DESTROYED 3123 is_destroyed = CF_DESTROYED
3124 is_suspended = CF_SUSPENDED
2983 CODE: 3125 CODE:
2984 RETVAL = boolSV (coro->flags & ix); 3126 RETVAL = boolSV (coro->flags & ix);
2985 OUTPUT: 3127 OUTPUT:
2986 RETVAL 3128 RETVAL
2987 3129
3003 3145
3004SV * 3146SV *
3005has_cctx (Coro::State coro) 3147has_cctx (Coro::State coro)
3006 PROTOTYPE: $ 3148 PROTOTYPE: $
3007 CODE: 3149 CODE:
3008 RETVAL = boolSV (!!coro->cctx); 3150 /* maybe manage the running flag differently */
3151 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3009 OUTPUT: 3152 OUTPUT:
3010 RETVAL 3153 RETVAL
3011 3154
3012int 3155int
3013is_traced (Coro::State coro) 3156is_traced (Coro::State coro)
3033 3176
3034void 3177void
3035force_cctx () 3178force_cctx ()
3036 PROTOTYPE: 3179 PROTOTYPE:
3037 CODE: 3180 CODE:
3038 SvSTATE_current->cctx->idle_sp = 0; 3181 cctx_current->idle_sp = 0;
3039 3182
3040void 3183void
3041swap_defsv (Coro::State self) 3184swap_defsv (Coro::State self)
3042 PROTOTYPE: $ 3185 PROTOTYPE: $
3043 ALIAS: 3186 ALIAS:
3051 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3194 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3052 3195
3053 SV *tmp = *src; *src = *dst; *dst = tmp; 3196 SV *tmp = *src; *src = *dst; *dst = tmp;
3054 } 3197 }
3055 3198
3199void
3200cancel (Coro::State self)
3201 CODE:
3202 coro_state_destroy (aTHX_ self);
3203 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3204
3056 3205
3057MODULE = Coro::State PACKAGE = Coro 3206MODULE = Coro::State PACKAGE = Coro
3058 3207
3059BOOT: 3208BOOT:
3060{ 3209{
3061 int i; 3210 int i;
3062 3211
3063 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3212 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3064 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3213 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3065 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3214 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3066 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3215 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3067 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3216 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3068 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3217 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3069 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3218 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3070 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3219 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3220 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3071 3221
3072 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3222 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3073 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3223 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3074 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3224 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3075 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3225 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3131 3281
3132void 3282void
3133cede_notself (...) 3283cede_notself (...)
3134 CODE: 3284 CODE:
3135 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3285 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3136
3137void
3138_cancel (Coro::State self)
3139 CODE:
3140 coro_state_destroy (aTHX_ self);
3141 coro_call_on_destroy (aTHX_ self);
3142 3286
3143void 3287void
3144_set_current (SV *current) 3288_set_current (SV *current)
3145 PROTOTYPE: $ 3289 PROTOTYPE: $
3146 CODE: 3290 CODE:
3254rouse_wait (...) 3398rouse_wait (...)
3255 PROTOTYPE: ;$ 3399 PROTOTYPE: ;$
3256 PPCODE: 3400 PPCODE:
3257 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3401 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3258 3402
3403void
3404on_enter (SV *block)
3405 ALIAS:
3406 on_leave = 1
3407 PROTOTYPE: &
3408 CODE:
3409{
3410 struct coro *coro = SvSTATE_current;
3411 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3412
3413 block = (SV *)coro_sv_2cv (aTHX_ block);
3414
3415 if (!*avp)
3416 *avp = newAV ();
3417
3418 av_push (*avp, SvREFCNT_inc (block));
3419
3420 if (!ix)
3421 on_enterleave_call (aTHX_ block);
3422
3423 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3424 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3425 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3426}
3427
3259 3428
3260MODULE = Coro::State PACKAGE = PerlIO::cede 3429MODULE = Coro::State PACKAGE = PerlIO::cede
3261 3430
3262BOOT: 3431BOOT:
3263 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3432 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3273 GvSTASH (CvGV (cv)) 3442 GvSTASH (CvGV (cv))
3274 ); 3443 );
3275 OUTPUT: 3444 OUTPUT:
3276 RETVAL 3445 RETVAL
3277 3446
3278# helper for Coro::Channel 3447# helper for Coro::Channel and others
3279SV * 3448SV *
3280_alloc (int count) 3449_alloc (int count)
3281 CODE: 3450 CODE:
3282 RETVAL = coro_waitarray_new (aTHX_ count); 3451 RETVAL = coro_waitarray_new (aTHX_ count);
3283 OUTPUT: 3452 OUTPUT:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines