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.99 by root, Sun Nov 26 23:53:20 2006 UTC vs.
Revision 1.106 by root, Mon Nov 27 02:08:55 2006 UTC

4#include "perl.h" 4#include "perl.h"
5#include "XSUB.h" 5#include "XSUB.h"
6 6
7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if PERL_VERSION < 6 9#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
12
13#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \
17 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
18
19#if !PERL_VERSION_ATLEAST (5,6,0)
10# ifndef PL_ppaddr 20# ifndef PL_ppaddr
11# define PL_ppaddr ppaddr 21# define PL_ppaddr ppaddr
12# endif 22# endif
13# ifndef call_sv 23# ifndef call_sv
14# define call_sv perl_call_sv 24# define call_sv perl_call_sv
52# ifndef PAGESIZE 62# ifndef PAGESIZE
53# define PAGESIZE pagesize 63# define PAGESIZE pagesize
54# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
55static long pagesize; 65static long pagesize;
56# else 66# else
57# define BOOT_PAGESIZE 67# define BOOT_PAGESIZE (void)0
58# endif 68# endif
69#else
70# define PAGESIZE 0
71# define BOOT_PAGESIZE (void)0
59#endif 72#endif
60 73
61/* The next macro should declare a variable stacklevel that contains and approximation 74/* The next macro should declare a variable stacklevel that contains and approximation
62 * to the current C stack pointer. Its property is that it changes with each call 75 * to the current C stack pointer. Its property is that it changes with each call
63 * and should be unique. */ 76 * and should be unique. */
64#define dSTACKLEVEL int stacklevel 77#define dSTACKLEVEL int stacklevel
65#define STACKLEVEL ((void *)&stacklevel) 78#define STACKLEVEL ((void *)&stacklevel)
66 79
67#define IN_DESTRUCT (PL_main_cv == Nullcv) 80#define IN_DESTRUCT (PL_main_cv == Nullcv)
81
82#if __GNUC__ >= 3
83# define attribute(x) __attribute__(x)
84#else
85# define attribute(x)
86#endif
87
88#define NOINLINE attribute ((noinline))
68 89
69#include "CoroAPI.h" 90#include "CoroAPI.h"
70 91
71#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ 92#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
72 93
83static AV *main_mainstack; /* used to differentiate between $main and others */ 104static AV *main_mainstack; /* used to differentiate between $main and others */
84static HV *coro_state_stash, *coro_stash; 105static HV *coro_state_stash, *coro_stash;
85static SV *coro_mortal; /* will be freed after next transfer */ 106static SV *coro_mortal; /* will be freed after next transfer */
86 107
87/* this is a structure representing a c-level coroutine */ 108/* this is a structure representing a c-level coroutine */
88typedef struct coro_stack { 109typedef struct coro_cctx {
89 struct coro_stack *next; 110 struct coro_cctx *next;
90 111
91 /* the stack */ 112 /* the stack */
92 void *sptr; 113 void *sptr;
93 long ssize; /* positive == mmap, otherwise malloc */ 114 long ssize; /* positive == mmap, otherwise malloc */
94 115
95 /* cpu state */ 116 /* cpu state */
96 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 117 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
97 JMPENV *top_env; 118 JMPENV *top_env;
98 coro_context cctx; 119 coro_context cctx;
99} coro_stack; 120
121#if USE_VALGRIND
122 int valgrind_id;
123#endif
124} coro_cctx;
100 125
101/* this is a structure representing a perl-level coroutine */ 126/* this is a structure representing a perl-level coroutine */
102struct coro { 127struct coro {
103 /* the c coroutine allocated to this perl coroutine, if any */ 128 /* the c coroutine allocated to this perl coroutine, if any */
104 coro_stack *stack; 129 coro_cctx *cctx;
105 130
106 /* data associated with this coroutine (initial args) */ 131 /* data associated with this coroutine (initial args) */
107 AV *args; 132 AV *args;
108 int refcnt; 133 int refcnt;
109 134
159 AV *padlist = CvPADLIST (cv); 184 AV *padlist = CvPADLIST (cv);
160 AV *newpadlist, *newpad; 185 AV *newpadlist, *newpad;
161 186
162 newpadlist = newAV (); 187 newpadlist = newAV ();
163 AvREAL_off (newpadlist); 188 AvREAL_off (newpadlist);
164#if PERL_VERSION < 9 189#if PERL_VERSION_ATLEAST (5,9,0)
190 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
191#else
165 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 192 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
166#else
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
168#endif 193#endif
169 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 194 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
170 --AvFILLp (padlist); 195 --AvFILLp (padlist);
171 196
172 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 197 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
291 PL_scopestack_ix = c->scopestack_ix; 316 PL_scopestack_ix = c->scopestack_ix;
292 PL_scopestack_max = c->scopestack_max; 317 PL_scopestack_max = c->scopestack_max;
293 PL_savestack = c->savestack; 318 PL_savestack = c->savestack;
294 PL_savestack_ix = c->savestack_ix; 319 PL_savestack_ix = c->savestack_ix;
295 PL_savestack_max = c->savestack_max; 320 PL_savestack_max = c->savestack_max;
296#if PERL_VERSION < 9 321#if !PERL_VERSION_ATLEAST (5,9,0)
297 PL_retstack = c->retstack; 322 PL_retstack = c->retstack;
298 PL_retstack_ix = c->retstack_ix; 323 PL_retstack_ix = c->retstack_ix;
299 PL_retstack_max = c->retstack_max; 324 PL_retstack_max = c->retstack_max;
300#endif 325#endif
301 PL_curpm = c->curpm; 326 PL_curpm = c->curpm;
418 c->scopestack_ix = PL_scopestack_ix; 443 c->scopestack_ix = PL_scopestack_ix;
419 c->scopestack_max = PL_scopestack_max; 444 c->scopestack_max = PL_scopestack_max;
420 c->savestack = PL_savestack; 445 c->savestack = PL_savestack;
421 c->savestack_ix = PL_savestack_ix; 446 c->savestack_ix = PL_savestack_ix;
422 c->savestack_max = PL_savestack_max; 447 c->savestack_max = PL_savestack_max;
423#if PERL_VERSION < 9 448#if !PERL_VERSION_ATLEAST (5,9,0)
424 c->retstack = PL_retstack; 449 c->retstack = PL_retstack;
425 c->retstack_ix = PL_retstack_ix; 450 c->retstack_ix = PL_retstack_ix;
426 c->retstack_max = PL_retstack_max; 451 c->retstack_max = PL_retstack_max;
427#endif 452#endif
428 c->curpm = PL_curpm; 453 c->curpm = PL_curpm;
466 491
467 New(54,PL_savestack,96,ANY); 492 New(54,PL_savestack,96,ANY);
468 PL_savestack_ix = 0; 493 PL_savestack_ix = 0;
469 PL_savestack_max = 96; 494 PL_savestack_max = 96;
470 495
471#if PERL_VERSION < 9 496#if !PERL_VERSION_ATLEAST (5,9,0)
472 New(54,PL_retstack,8,OP*); 497 New(54,PL_retstack,8,OP*);
473 PL_retstack_ix = 0; 498 PL_retstack_ix = 0;
474 PL_retstack_max = 8; 499 PL_retstack_max = 8;
475#endif 500#endif
476} 501}
477 502
478/* 503/*
479 * destroy the stacks, the callchain etc... 504 * destroy the stacks, the callchain etc...
480 */ 505 */
481static void 506static void
482destroy_stacks() 507coro_destroy_stacks ()
483{ 508{
484 if (!IN_DESTRUCT) 509 if (!IN_DESTRUCT)
485 { 510 {
486 /* is this ugly, I ask? */ 511 /* is this ugly, I ask? */
487 LEAVE_SCOPE (0); 512 LEAVE_SCOPE (0);
518 543
519 Safefree (PL_tmps_stack); 544 Safefree (PL_tmps_stack);
520 Safefree (PL_markstack); 545 Safefree (PL_markstack);
521 Safefree (PL_scopestack); 546 Safefree (PL_scopestack);
522 Safefree (PL_savestack); 547 Safefree (PL_savestack);
523#if PERL_VERSION < 9 548#if !PERL_VERSION_ATLEAST (5,9,0)
524 Safefree (PL_retstack); 549 Safefree (PL_retstack);
525#endif 550#endif
526} 551}
527 552
528static void 553static void
529setup_coro (struct coro *coro) 554setup_coro (struct coro *coro)
530{ 555{
531 /* 556 /*
532 * emulate part of the perl startup here. 557 * emulate part of the perl startup here.
533 */ 558 */
534 dTHX;
535 dSP;
536 UNOP myop;
537 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
538 559
539 coro_init_stacks (); 560 coro_init_stacks ();
561
540 /*PL_curcop = 0;*/ 562 PL_curcop = 0;
541 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 563 PL_in_eval = 0;
564 PL_curpm = 0;
565
566 {
567 dSP;
568 LOGOP myop;
569
570 /* I have no idea why this is needed, but it is */
571 PUSHMARK (SP);
572
542 SvREFCNT_dec (GvAV (PL_defgv)); 573 SvREFCNT_dec (GvAV (PL_defgv));
543 GvAV (PL_defgv) = coro->args; coro->args = 0; 574 GvAV (PL_defgv) = coro->args; coro->args = 0;
544 575
545 SPAGAIN;
546
547 Zero (&myop, 1, UNOP); 576 Zero (&myop, 1, LOGOP);
548 myop.op_next = Nullop; 577 myop.op_next = Nullop;
549 myop.op_flags = OPf_WANT_VOID; 578 myop.op_flags = OPf_WANT_VOID;
550 579
551 PL_op = (OP *)&myop; 580 PL_op = (OP *)&myop;
552 581
553 PUSHMARK(SP); 582 PUSHMARK (SP);
554 XPUSHs (sub_init); 583 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE));
555 PUTBACK; 584 PUTBACK;
556 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 585 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
557 SPAGAIN; 586 SPAGAIN;
558 587
559 ENTER; /* necessary e.g. for dounwind */ 588 ENTER; /* necessary e.g. for dounwind */
589 }
560} 590}
561 591
562static void 592static void
563free_coro_mortal () 593free_coro_mortal ()
564{ 594{
567 SvREFCNT_dec (coro_mortal); 597 SvREFCNT_dec (coro_mortal);
568 coro_mortal = 0; 598 coro_mortal = 0;
569 } 599 }
570} 600}
571 601
572static void 602static void NOINLINE
573prepare_cctx (coro_stack *cctx) 603prepare_cctx (coro_cctx *cctx)
574{ 604{
575 dSP; 605 dSP;
576 UNOP myop; 606 LOGOP myop;
577 607
578 Zero (&myop, 1, UNOP); 608 Zero (&myop, 1, LOGOP);
579 myop.op_next = PL_op; 609 myop.op_next = PL_op;
580 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 610 myop.op_flags = OPf_WANT_VOID;
581 611
612 sv_setiv (get_sv ("Coro::State::cctx", FALSE), PTR2IV (cctx));
613
582 PUSHMARK(SP); 614 PUSHMARK (SP);
583 EXTEND (SP, 2);
584 PUSHs (newSViv (PTR2IV (cctx)));
585 PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 615 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
586 PUTBACK; 616 PUTBACK;
587 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 617 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
588 SPAGAIN; 618 SPAGAIN;
589} 619}
590 620
591static void 621static void
592coro_run (void *arg) 622coro_run (void *arg)
593{ 623{
624 /* coro_run is the alternative epilogue of transfer() */
625 UNLOCK;
626
594 /* 627 /*
595 * this is a _very_ stripped down perl interpreter ;) 628 * this is a _very_ stripped down perl interpreter ;)
596 */ 629 */
597 UNLOCK;
598
599 PL_top_env = &PL_start_env; 630 PL_top_env = &PL_start_env;
631 /* inject call to cctx_init */
600 prepare_cctx ((coro_stack *)arg); 632 prepare_cctx ((coro_cctx *)arg);
601 633
602 /* somebody will hit me for both perl_run and PL_restartop */ 634 /* somebody will hit me for both perl_run and PL_restartop */
603 perl_run (PERL_GET_CONTEXT); 635 perl_run (PL_curinterp);
604 636
605 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 637 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
606 abort (); 638 abort ();
607} 639}
608 640
609static coro_stack * 641static coro_cctx *
610stack_new () 642cctx_new ()
611{ 643{
612 coro_stack *stack; 644 coro_cctx *cctx;
613 645
614 New (0, stack, 1, coro_stack); 646 New (0, cctx, 1, coro_cctx);
615 647
616#if HAVE_MMAP 648#if HAVE_MMAP
617 649
618 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 650 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
619 /* mmap suppsedly does allocate-on-write for us */ 651 /* mmap suppsedly does allocate-on-write for us */
620 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 652 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
621 653
622 if (stack->sptr == (void *)-1) 654 if (cctx->sptr == (void *)-1)
623 { 655 {
624 perror ("FATAL: unable to mmap stack for coroutine"); 656 perror ("FATAL: unable to mmap stack for coroutine");
625 _exit (EXIT_FAILURE); 657 _exit (EXIT_FAILURE);
626 } 658 }
627 659
628# if STACKGUARD 660# if STACKGUARD
629 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 661 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
630# endif 662# endif
631 663
632#else 664#else
633 665
634 stack->ssize = STACKSIZE * (long)sizeof (long); 666 cctx->ssize = STACKSIZE * (long)sizeof (long);
635 New (0, stack->sptr, STACKSIZE, long); 667 New (0, cctx->sptr, STACKSIZE, long);
636 668
637 if (!stack->sptr) 669 if (!cctx->sptr)
638 { 670 {
639 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 671 perror ("FATAL: unable to malloc stack for coroutine");
640 _exit (EXIT_FAILURE); 672 _exit (EXIT_FAILURE);
641 } 673 }
642 674
643#endif 675#endif
644 676
677#if USE_VALGRIND
678 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
679 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
680 cctx->ssize + (char *)cctx->sptr
681 );
682#endif
683
645 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 684 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
646 685
647 return stack; 686 return cctx;
648} 687}
649 688
650static void 689static void
651stack_free (coro_stack *stack) 690cctx_free (coro_cctx *cctx)
652{ 691{
653 if (!stack) 692 if (!cctx)
654 return; 693 return;
655 694
695#if USE_VALGRIND
696 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
697#endif
698
656#if HAVE_MMAP 699#if HAVE_MMAP
657 munmap (stack->sptr, stack->ssize); 700 munmap (cctx->sptr, cctx->ssize);
658#else 701#else
659 Safefree (stack->sptr); 702 Safefree (cctx->sptr);
660#endif 703#endif
661 704
662 Safefree (stack); 705 Safefree (cctx);
663} 706}
664 707
665static coro_stack *stack_first; 708static coro_cctx *cctx_first;
709static int cctx_count, cctx_idle;
666 710
667static coro_stack * 711static coro_cctx *
668stack_get () 712cctx_get ()
669{ 713{
670 coro_stack *stack; 714 coro_cctx *cctx;
671 715
672 if (stack_first) 716 if (cctx_first)
673 { 717 {
674 stack = stack_first; 718 --cctx_idle;
719 cctx = cctx_first;
675 stack_first = stack->next; 720 cctx_first = cctx->next;
676 } 721 }
677 else 722 else
678 { 723 {
679 stack = stack_new (); 724 ++cctx_count;
725 cctx = cctx_new ();
680 PL_op = PL_op->op_next; 726 PL_op = PL_op->op_next;
681 } 727 }
682 728
683 return stack; 729 return cctx;
684} 730}
685 731
686static void 732static void
687stack_put (coro_stack *stack) 733cctx_put (coro_cctx *cctx)
688{ 734{
689 stack->next = stack_first; 735 ++cctx_idle;
690 stack_first = stack; 736 cctx->next = cctx_first;
737 cctx_first = cctx;
691} 738}
692 739
693/* never call directly, always through the coro_state_transfer global variable */ 740/* never call directly, always through the coro_state_transfer global variable */
694static void 741static void NOINLINE
695transfer_impl (struct coro *prev, struct coro *next, int flags) 742transfer (struct coro *prev, struct coro *next, int flags)
696{ 743{
697 dSTACKLEVEL; 744 dSTACKLEVEL;
698 745
699 /* sometimes transfer is only called to set idle_sp */ 746 /* sometimes transfer is only called to set idle_sp */
700 if (flags == TRANSFER_SET_STACKLEVEL) 747 if (flags == TRANSFER_SET_STACKLEVEL)
701 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 748 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
702 else if (prev != next) 749 else if (prev != next)
703 { 750 {
704 coro_stack *prev__stack; 751 coro_cctx *prev__cctx;
705 752
706 LOCK; 753 LOCK;
707 754
708 if (next->mainstack) 755 if (next->mainstack)
709 { 756 {
717 /* first get rid of the old state */ 764 /* first get rid of the old state */
718 SAVE (prev, -1); 765 SAVE (prev, -1);
719 /* setup coroutine call */ 766 /* setup coroutine call */
720 setup_coro (next); 767 setup_coro (next);
721 /* need a stack */ 768 /* need a stack */
722 next->stack = 0; 769 next->cctx = 0;
723 } 770 }
724 771
725 if (!prev->stack) 772 if (!prev->cctx)
726 /* create a new empty context */ 773 /* create a new empty context */
727 Newz (0, prev->stack, 1, coro_stack); 774 Newz (0, prev->cctx, 1, coro_cctx);
728 775
729 prev__stack = prev->stack; 776 prev__cctx = prev->cctx;
730 777
731 /* possibly "free" the stack */ 778 /* possibly "free" the cctx */
732 if (prev__stack->idle_sp == STACKLEVEL) 779 if (prev__cctx->idle_sp == STACKLEVEL)
733 { 780 {
734 stack_put (prev__stack); 781 cctx_put (prev__cctx);
735 prev->stack = 0; 782 prev->cctx = 0;
736 } 783 }
737 784
738 if (!next->stack) 785 if (!next->cctx)
739 next->stack = stack_get (); 786 next->cctx = cctx_get ();
740 787
741 if (prev__stack != next->stack) 788 if (prev__cctx != next->cctx)
742 { 789 {
743 prev__stack->top_env = PL_top_env; 790 prev__cctx->top_env = PL_top_env;
744 PL_top_env = next->stack->top_env; 791 PL_top_env = next->cctx->top_env;
745 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 792 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
746 } 793 }
747 794
748 free_coro_mortal (); 795 free_coro_mortal ();
749 796
750 UNLOCK; 797 UNLOCK;
751 } 798 }
752} 799}
753
754/* use this function pointer to call the above function */
755/* this is done to increase chances of the compiler not inlining the call */
756/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
757void (*coro_state_transfer)(struct coro *prev, struct coro *next, int flags) = transfer_impl;
758 800
759struct transfer_args 801struct transfer_args
760{ 802{
761 struct coro *prev, *next; 803 struct coro *prev, *next;
762 int flags; 804 int flags;
763}; 805};
764 806
765#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 807#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
766 808
767static void 809static void
768coro_state_destroy (struct coro *coro) 810coro_state_destroy (struct coro *coro)
769{ 811{
770 if (coro->refcnt--) 812 if (coro->refcnt--)
775 struct coro temp; 817 struct coro temp;
776 818
777 SAVE ((&temp), TRANSFER_SAVE_ALL); 819 SAVE ((&temp), TRANSFER_SAVE_ALL);
778 LOAD (coro); 820 LOAD (coro);
779 821
780 destroy_stacks (); 822 coro_destroy_stacks ();
781 823
782 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 824 LOAD ((&temp)); /* this will get rid of defsv etc.. */
783 825
784 coro->mainstack = 0; 826 coro->mainstack = 0;
785 } 827 }
786 828
787 stack_free (coro->stack); 829 cctx_free (coro->cctx);
788 SvREFCNT_dec (coro->args); 830 SvREFCNT_dec (coro->args);
789 Safefree (coro); 831 Safefree (coro);
790} 832}
791 833
792static int 834static int
808 ++coro->refcnt; 850 ++coro->refcnt;
809 851
810 return 0; 852 return 0;
811} 853}
812 854
813static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 855static MGVTBL coro_state_vtbl = {
856 0, 0, 0, 0,
857 coro_state_clear,
858 0,
859#ifdef MGf_DUP
860 coro_state_dup,
861#else
862# define MGf_DUP 0
863#endif
864};
814 865
815static struct coro * 866static struct coro *
816SvSTATE (SV *coro) 867SvSTATE (SV *coro)
817{ 868{
818 HV *stash; 869 HV *stash;
1027 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1078 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1028 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1079 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1029 1080
1030 for (i = 1; i < items; i++) 1081 for (i = 1; i < items; i++)
1031 av_push (coro->args, newSVsv (ST (i))); 1082 av_push (coro->args, newSVsv (ST (i)));
1032
1033 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1034 /*coro->stack = 0;*/
1035} 1083}
1036 OUTPUT: 1084 OUTPUT:
1037 RETVAL 1085 RETVAL
1038 1086
1039void 1087void
1048 struct transfer_args ta; 1096 struct transfer_args ta;
1049 1097
1050 switch (ix) 1098 switch (ix)
1051 { 1099 {
1052 case 0: 1100 case 0:
1053 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1101 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1054 ta.next = 0; 1102 ta.next = 0;
1055 ta.flags = TRANSFER_SET_STACKLEVEL; 1103 ta.flags = TRANSFER_SET_STACKLEVEL;
1056 break; 1104 break;
1057 1105
1058 case 1: 1106 case 1:
1112 ++coro_src->refcnt; 1160 ++coro_src->refcnt;
1113 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1161 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1114} 1162}
1115 1163
1116void 1164void
1117_nonlocal_goto (IV nextop)
1118 CODE:
1119 /* uuh, somebody will kill me again for this */
1120 PL_op->op_next = INT2PTR (OP *, nextop);
1121
1122void
1123_exit (code) 1165_exit (code)
1124 int code 1166 int code
1125 PROTOTYPE: $ 1167 PROTOTYPE: $
1126 CODE: 1168 CODE:
1127 _exit (code); 1169 _exit (code);
1170
1171int
1172cctx_count ()
1173 CODE:
1174 RETVAL = cctx_count;
1175 OUTPUT:
1176 RETVAL
1177
1178int
1179cctx_idle ()
1180 CODE:
1181 RETVAL = cctx_idle;
1182 OUTPUT:
1183 RETVAL
1128 1184
1129MODULE = Coro::State PACKAGE = Coro 1185MODULE = Coro::State PACKAGE = Coro
1130 1186
1131BOOT: 1187BOOT:
1132{ 1188{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines