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.76 by root, Thu Oct 26 08:45:56 2006 UTC vs.
Revision 1.88 by root, Fri Nov 24 15:34:33 2006 UTC

29# endif 29# endif
30#endif 30#endif
31 31
32#include <errno.h> 32#include <errno.h>
33 33
34#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
35# undef STACKGUARD
36#endif
37
38#ifndef STACKGUARD
39# define STACKGUARD 0
40#endif
41
34#ifdef HAVE_MMAP 42#ifdef HAVE_MMAP
35# include <unistd.h> 43# include <unistd.h>
36# include <sys/mman.h> 44# include <sys/mman.h>
37# ifndef MAP_ANONYMOUS 45# ifndef MAP_ANONYMOUS
38# ifdef MAP_ANON 46# ifdef MAP_ANON
39# define MAP_ANONYMOUS MAP_ANON 47# define MAP_ANONYMOUS MAP_ANON
40# else 48# else
41# undef HAVE_MMAP 49# undef HAVE_MMAP
42# endif 50# endif
43# endif 51# endif
52# include <limits.h>
53# ifndef PAGESIZE
54# define PAGESIZE pagesize
55# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
56static long pagesize;
57# else
58# define BOOT_PAGESIZE
44#endif 59# endif
60#endif
45 61
46#define SUB_INIT "Coro::State::initialize" 62#define SUB_INIT "Coro::State::initialize"
47#define UCORO_STATE "_coro_state"
48 63
49/* The next macro should declare a variable stacklevel that contains and approximation 64/* The next macro should declare a variable stacklevel that contains and approximation
50 * to the current C stack pointer. Its property is that it changes with each call 65 * to the current C stack pointer. Its property is that it changes with each call
51 * and should be unique. */ 66 * and should be unique. */
52#define dSTACKLEVEL void *stacklevel = &stacklevel 67#define dSTACKLEVEL void *stacklevel = &stacklevel
66# define UNLOCK (void)0 81# define UNLOCK (void)0
67#endif 82#endif
68 83
69static struct CoroAPI coroapi; 84static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */ 85static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash; 86static HV *coro_state_stash, *coro_stash;
72static SV *ucoro_state_sv;
73static U32 ucoro_state_hash;
74static SV *coro_mortal; /* will be freed after next transfer */ 87static SV *coro_mortal; /* will be freed after next transfer */
75 88
76/* this is actually not only the c stack but also c registers etc... */ 89/* this is actually not only the c stack but also c registers etc... */
77typedef struct { 90typedef struct {
78 int refcnt; /* pointer reference counter */ 91 int refcnt; /* pointer reference counter */
84 void *sptr; 97 void *sptr;
85 long ssize; /* positive == mmap, otherwise malloc */ 98 long ssize; /* positive == mmap, otherwise malloc */
86} coro_stack; 99} coro_stack;
87 100
88struct coro { 101struct coro {
89 /* the top-level JMPENV for each coroutine, needed to catch dies. */
90 JMPENV start_env;
91
92 /* the optional C context */ 102 /* the optional C context */
93 coro_stack *stack; 103 coro_stack *stack;
94 void *cursp; 104 void *cursp;
95 int gencnt; 105 int gencnt;
96 106
132 I32 retstack_max; 142 I32 retstack_max;
133 PMOP *curpm; 143 PMOP *curpm;
134 COP *curcop; 144 COP *curcop;
135 JMPENV *top_env; 145 JMPENV *top_env;
136 146
147 /* coro process data */
148 int prio;
149
137 /* data associated with this coroutine (initial args) */ 150 /* data associated with this coroutine (initial args) */
138 AV *args; 151 AV *args;
152 int refcnt;
139}; 153};
140 154
141typedef struct coro *Coro__State; 155typedef struct coro *Coro__State;
142typedef struct coro *Coro__State_or_hashref; 156typedef struct coro *Coro__State_or_hashref;
143 157
144/* mostly copied from op.c:cv_clone2 */ 158static AV *
145STATIC AV * 159coro_clone_padlist (pTHX_ CV *cv)
146clone_padlist (pTHX_ AV *protopadlist)
147{ 160{
148 AV *av; 161 AV *padlist = CvPADLIST (cv);
149 I32 ix;
150 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
151 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
152 SV **pname = AvARRAY (protopad_name);
153 SV **ppad = AvARRAY (protopad);
154 I32 fname = AvFILLp (protopad_name);
155 I32 fpad = AvFILLp (protopad);
156 AV *newpadlist, *newpad_name, *newpad; 162 AV *newpadlist, *newpad;
157 SV **npad;
158
159 newpad_name = newAV ();
160 for (ix = fname; ix >= 0; ix--)
161 av_store (newpad_name, ix, SvREFCNT_inc (pname[ix]));
162
163 newpad = newAV ();
164 av_fill (newpad, AvFILLp (protopad));
165 npad = AvARRAY (newpad);
166 163
167 newpadlist = newAV (); 164 newpadlist = newAV ();
168 AvREAL_off (newpadlist); 165 AvREAL_off (newpadlist);
169 av_store (newpadlist, 0, (SV *) newpad_name); 166#if PERL_VERSION < 9
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
168#else
169 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
170#endif
171 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
172 --AvFILLp (padlist);
173
174 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
170 av_store (newpadlist, 1, (SV *) newpad); 175 av_store (newpadlist, 1, (SV *)newpad);
171
172 av = newAV (); /* will be @_ */
173 av_extend (av, 0);
174 av_store (newpad, 0, (SV *) av);
175 AvREIFY_on (av);
176
177 for (ix = fpad; ix > 0; ix--)
178 {
179 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
180
181 if (namesv && namesv != &PL_sv_undef)
182 {
183 char *name = SvPVX (namesv); /* XXX */
184
185 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
186 { /* lexical from outside? */
187 npad[ix] = SvREFCNT_inc (ppad[ix]);
188 }
189 else
190 { /* our own lexical */
191 SV *sv;
192 if (*name == '&')
193 sv = SvREFCNT_inc (ppad[ix]);
194 else if (*name == '@')
195 sv = (SV *) newAV ();
196 else if (*name == '%')
197 sv = (SV *) newHV ();
198 else
199 sv = NEWSV (0, 0);
200
201#ifdef SvPADBUSY
202 if (!SvPADBUSY (sv))
203#endif
204 SvPADMY_on (sv);
205
206 npad[ix] = sv;
207 }
208 }
209 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
210 {
211 npad[ix] = SvREFCNT_inc (ppad[ix]);
212 }
213 else
214 {
215 SV *sv = NEWSV (0, 0);
216 SvPADTMP_on (sv);
217 npad[ix] = sv;
218 }
219 }
220
221#if 0 /* return -ENOTUNDERSTOOD */
222 /* Now that vars are all in place, clone nested closures. */
223
224 for (ix = fpad; ix > 0; ix--) {
225 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
226 if (namesv
227 && namesv != &PL_sv_undef
228 && !(SvFLAGS(namesv) & SVf_FAKE)
229 && *SvPVX(namesv) == '&'
230 && CvCLONE(ppad[ix]))
231 {
232 CV *kid = cv_clone((CV*)ppad[ix]);
233 SvREFCNT_dec(ppad[ix]);
234 CvCLONE_on(kid);
235 SvPADMY_on(kid);
236 npad[ix] = (SV*)kid;
237 }
238 }
239#endif
240 176
241 return newpadlist; 177 return newpadlist;
242} 178}
243 179
244STATIC void 180static void
245free_padlist (pTHX_ AV *padlist) 181free_padlist (pTHX_ AV *padlist)
246{ 182{
247 /* may be during global destruction */ 183 /* may be during global destruction */
248 if (SvREFCNT (padlist)) 184 if (SvREFCNT (padlist))
249 { 185 {
263 199
264 SvREFCNT_dec ((SV*)padlist); 200 SvREFCNT_dec ((SV*)padlist);
265 } 201 }
266} 202}
267 203
268STATIC int 204static int
269coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 205coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
270{ 206{
271 AV *padlist; 207 AV *padlist;
272 AV *av = (AV *)mg->mg_obj; 208 AV *av = (AV *)mg->mg_obj;
273 209
283#define PERL_MAGIC_coro PERL_MAGIC_ext 219#define PERL_MAGIC_coro PERL_MAGIC_ext
284 220
285static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 221static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
286 222
287/* the next two functions merely cache the padlists */ 223/* the next two functions merely cache the padlists */
288STATIC void 224static void
289get_padlist (pTHX_ CV *cv) 225get_padlist (pTHX_ CV *cv)
290{ 226{
291 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 227 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
292 228
293 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
294 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
295 else 231 else
232 {
233#if 0
234 /* this should work - but it doesn't :( */
235 CV *cp = Perl_cv_clone (aTHX_ cv);
236 CvPADLIST (cv) = CvPADLIST (cp);
237 CvPADLIST (cp) = 0;
238 SvREFCNT_dec (cp);
239#else
296 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); 240 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
241#endif
242 }
297} 243}
298 244
299STATIC void 245static void
300put_padlist (pTHX_ CV *cv) 246put_padlist (pTHX_ CV *cv)
301{ 247{
302 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 248 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
303 249
304 if (!mg) 250 if (!mg)
421 } 367 }
422 368
423 PUSHs ((SV *)CvPADLIST(cv)); 369 PUSHs ((SV *)CvPADLIST(cv));
424 PUSHs ((SV *)cv); 370 PUSHs ((SV *)cv);
425 371
426 get_padlist (aTHX_ cv); /* this is a monster */ 372 get_padlist (aTHX_ cv);
427 } 373 }
428 } 374 }
429#ifdef CXt_FORMAT 375#ifdef CXt_FORMAT
430 else if (CxTYPE(cx) == CXt_FORMAT) 376 else if (CxTYPE(cx) == CXt_FORMAT)
431 { 377 {
491 * allocate various perl stacks. This is an exact copy 437 * allocate various perl stacks. This is an exact copy
492 * of perl.c:init_stacks, except that it uses less memory 438 * of perl.c:init_stacks, except that it uses less memory
493 * on the (sometimes correct) assumption that coroutines do 439 * on the (sometimes correct) assumption that coroutines do
494 * not usually need a lot of stackspace. 440 * not usually need a lot of stackspace.
495 */ 441 */
496STATIC void 442static void
497coro_init_stacks (pTHX) 443coro_init_stacks (pTHX)
498{ 444{
499 LOCK; 445 LOCK;
500 446
501 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 447 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
538} 484}
539 485
540/* 486/*
541 * destroy the stacks, the callchain etc... 487 * destroy the stacks, the callchain etc...
542 */ 488 */
543STATIC void 489static void
544destroy_stacks(pTHX) 490destroy_stacks(pTHX)
545{ 491{
546 if (!IN_DESTRUCT) 492 if (!IN_DESTRUCT)
547 { 493 {
548 /* is this ugly, I ask? */ 494 /* is this ugly, I ask? */
599 stack->gencnt = ctx->gencnt = 0; 545 stack->gencnt = ctx->gencnt = 0;
600 546
601 if (alloc) 547 if (alloc)
602 { 548 {
603#if HAVE_MMAP 549#if HAVE_MMAP
604 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ 550 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
605 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 551 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
606 if (stack->sptr == (void *)-1) 552 if (stack->sptr != (void *)-1)
553 {
554# if STACKGUARD
555 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
556# endif
557 }
558 else
607#endif 559#endif
608 { 560 {
609 stack->ssize = - (STACKSIZE * (long)sizeof (long)); 561 stack->ssize = - (STACKSIZE * (long)sizeof (long));
610 New (0, stack->sptr, STACKSIZE, long); 562 New (0, stack->sptr, STACKSIZE, long);
611 } 563 }
675 } 627 }
676 else 628 else
677 { 629 {
678 UNOP myop; 630 UNOP myop;
679 631
680 PL_op = (OP *)&myop;
681
682 Zero(&myop, 1, UNOP); 632 Zero(&myop, 1, UNOP);
683 myop.op_next = Nullop; 633 myop.op_next = Nullop;
684 myop.op_flags = OPf_WANT_VOID; 634 myop.op_flags = OPf_WANT_VOID;
635
636 PL_op = (OP *)&myop;
685 637
686 PUSHMARK(SP); 638 PUSHMARK(SP);
687 XPUSHs (sub_init); 639 XPUSHs (sub_init);
688 /* 640 /*
689 * the next line is slightly wrong, as PL_op->op_next 641 * the next line is slightly wrong, as PL_op->op_next
707 * this is a _very_ stripped down perl interpreter ;) 659 * this is a _very_ stripped down perl interpreter ;)
708 */ 660 */
709 dTHX; 661 dTHX;
710 Coro__State ctx = (Coro__State)arg; 662 Coro__State ctx = (Coro__State)arg;
711 663
712 PL_top_env = &ctx->start_env; 664 PL_top_env = &PL_start_env;
713 665
714 ctx->cursp = 0; 666 ctx->cursp = 0;
715 PL_op = PL_op->op_next; 667 PL_op = PL_op->op_next;
716 CALLRUNOPS(aTHX); 668 CALLRUNOPS(aTHX);
717 669
718 abort (); 670 abort ();
719} 671}
720 672
721STATIC void 673/* never call directly, always through the coro_state_transfer global variable */
674static void
722transfer (pTHX_ struct coro *prev, struct coro *next, int flags) 675transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
723{ 676{
724 dSTACKLEVEL; 677 dSTACKLEVEL;
725 678
726 if (prev != next) 679 if (prev != next)
727 { 680 {
681 /* has this coro been created yet? */
728 if (next->mainstack) 682 if (next->mainstack)
729 { 683 {
730 LOCK; 684 LOCK;
731 SAVE (prev, flags); 685 SAVE (prev, flags);
732 LOAD (next); 686 LOAD (next);
771 { 725 {
772 LOCK; 726 LOCK;
773 SAVE (prev, -1); /* first get rid of the old state */ 727 SAVE (prev, -1); /* first get rid of the old state */
774 UNLOCK; 728 UNLOCK;
775 729
730 /* create the coroutine for the first time */
776 if (flags & TRANSFER_SAVE_CCTXT) 731 if (flags & TRANSFER_SAVE_CCTXT)
777 { 732 {
778 if (!prev->stack) 733 if (!prev->stack)
779 allocate_stack (prev, 0); 734 allocate_stack (prev, 0);
780 735
736 /* the new coroutine starts with start_env again */
737 PL_top_env = &PL_start_env;
738
781 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 739 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
782 { 740 {
783 PL_top_env = &next->start_env;
784
785 setup_coro (next); 741 setup_coro (next);
786 next->cursp = stacklevel; 742 next->cursp = stacklevel;
787 743
788 prev->stack->refcnt++; 744 prev->stack->refcnt++;
789 prev->stack->usecnt++; 745 prev->stack->usecnt++;
817 coro_mortal = 0; 773 coro_mortal = 0;
818 } 774 }
819 UNLOCK; 775 UNLOCK;
820} 776}
821 777
822#define SV_CORO(sv,func) \ 778/* use this function pointer to call the above function */
823 do { \ 779/* this is done to increase chances of the compiler not inlining the call */
824 if (SvROK (sv)) \ 780void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
825 sv = SvRV (sv); \
826 \
827 if (SvTYPE (sv) == SVt_PVHV) \
828 { \
829 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
830 \
831 if (!he) \
832 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
833 \
834 (sv) = SvRV (HeVAL(he)); \
835 } \
836 \
837 /* must also be changed inside Coro::Cont::yield */ \
838 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
839 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
840 \
841 } while(0)
842 781
843#define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) 782static void
783coro_state_destroy (struct coro *coro)
784{
785 if (coro->refcnt--)
786 return;
844 787
788 if (coro->mainstack && coro->mainstack != main_mainstack)
789 {
790 struct coro temp;
791
792 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
793 LOAD (aTHX_ coro);
794
795 destroy_stacks (aTHX);
796
797 LOAD ((&temp)); /* this will get rid of defsv etc.. */
798
799 coro->mainstack = 0;
800 }
801
802 deallocate_stack (coro);
803 SvREFCNT_dec (coro->args);
804 Safefree (coro);
805}
806
845static void 807static int
808coro_state_clear (SV *sv, MAGIC *mg)
809{
810 struct coro *coro = (struct coro *)mg->mg_ptr;
811 mg->mg_ptr = 0;
812
813 coro_state_destroy (coro);
814
815 return 0;
816}
817
818static int
819coro_state_dup (MAGIC *mg, CLONE_PARAMS *params)
820{
821 struct coro *coro = (struct coro *)mg->mg_ptr;
822
823 ++coro->refcnt;
824
825 return 0;
826}
827
828static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 };
829
830static struct coro *
831SvSTATE (SV *coro)
832{
833 HV *stash;
834 MAGIC *mg;
835
836 if (SvROK (coro))
837 coro = SvRV (coro);
838
839 stash = SvSTASH (coro);
840 if (stash != coro_stash && stash != coro_state_stash)
841 {
842 /* very slow, but rare, check */
843 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
844 croak ("Coro::State object required");
845 }
846
847 mg = SvMAGIC (coro);
848 assert (mg->mg_type == PERL_MAGIC_ext);
849 return (struct coro *)mg->mg_ptr;
850}
851
852static void
846api_transfer(pTHX_ SV *prev, SV *next, int flags) 853api_transfer (pTHX_ SV *prev, SV *next, int flags)
847{ 854{
848 SV_CORO (prev, "Coro::transfer");
849 SV_CORO (next, "Coro::transfer");
850
851 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 855 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
852} 856}
853 857
854/** Coro ********************************************************************/ 858/** Coro ********************************************************************/
855 859
856#define PRIO_MAX 3 860#define PRIO_MAX 3
860#define PRIO_IDLE -3 864#define PRIO_IDLE -3
861#define PRIO_MIN -4 865#define PRIO_MIN -4
862 866
863/* for Coro.pm */ 867/* for Coro.pm */
864static GV *coro_current, *coro_idle; 868static GV *coro_current, *coro_idle;
865static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 869static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
866static int coro_nready; 870static int coro_nready;
867 871
868static void 872static void
869coro_enq (pTHX_ SV *sv) 873coro_enq (pTHX_ SV *sv)
870{ 874{
871 SV **xprio;
872 int prio; 875 int prio;
873 876
874 if (SvTYPE (sv) != SVt_PVHV) 877 if (SvTYPE (sv) != SVt_PVHV)
875 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 878 croak ("Coro::ready tried to enqueue something that is not a coroutine");
876 879
877 xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 880 prio = SvSTATE (sv)->prio;
878 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
879
880 prio = prio > PRIO_MAX ? PRIO_MAX
881 : prio < PRIO_MIN ? PRIO_MIN
882 : prio;
883 881
884 av_push (coro_ready [prio - PRIO_MIN], sv); 882 av_push (coro_ready [prio - PRIO_MIN], sv);
885 coro_nready++; 883 coro_nready++;
886} 884}
887 885
893 min_prio -= PRIO_MIN; 891 min_prio -= PRIO_MIN;
894 if (min_prio < 0) 892 if (min_prio < 0)
895 min_prio = 0; 893 min_prio = 0;
896 894
897 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 895 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
898 if (av_len (coro_ready[prio]) >= 0) 896 if (AvFILLp (coro_ready [prio]) >= 0)
899 { 897 {
900 coro_nready--; 898 coro_nready--;
901 return av_shift (coro_ready[prio]); 899 return av_shift (coro_ready [prio]);
902 } 900 }
903 901
904 return 0; 902 return 0;
905} 903}
906 904
921api_schedule (void) 919api_schedule (void)
922{ 920{
923 dTHX; 921 dTHX;
924 922
925 SV *prev, *next; 923 SV *prev, *next;
924 SV *current = GvSV (coro_current);
926 925
926 for (;;)
927 {
927 LOCK; 928 LOCK;
928 929
929 prev = SvRV (GvSV (coro_current));
930 next = coro_deq (aTHX_ PRIO_MIN); 930 next = coro_deq (aTHX_ PRIO_MIN);
931 931
932 if (!next) 932 if (next)
933 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 933 break;
934
935 UNLOCK;
936
937 {
938 dSP;
939
940 ENTER;
941 SAVETMPS;
942
943 PUSHMARK (SP);
944 PUTBACK;
945 call_sv (GvSV (coro_idle), G_DISCARD);
946
947 FREETMPS;
948 LEAVE;
949 }
950 }
951
952 prev = SvRV (current);
953 SvRV (current) = next;
934 954
935 /* free this only after the transfer */ 955 /* free this only after the transfer */
936 coro_mortal = prev; 956 coro_mortal = prev;
937 SV_CORO (prev, "Coro::schedule");
938
939 SvRV (GvSV (coro_current)) = next;
940
941 SV_CORO (next, "Coro::schedule");
942 957
943 UNLOCK; 958 UNLOCK;
944 959
945 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 960 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
946 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 961 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
947} 962}
948 963
949static void 964static void
950api_cede (void) 965api_cede (void)
951{ 966{
958 api_schedule (); 973 api_schedule ();
959} 974}
960 975
961MODULE = Coro::State PACKAGE = Coro::State 976MODULE = Coro::State PACKAGE = Coro::State
962 977
963PROTOTYPES: ENABLE 978PROTOTYPES: DISABLE
964 979
965BOOT: 980BOOT:
966{ /* {} necessary for stoopid perl-5.6.x */ 981{
967#ifdef USE_ITHREADS 982#ifdef USE_ITHREADS
968 MUTEX_INIT (&coro_mutex); 983 MUTEX_INIT (&coro_mutex);
969#endif 984#endif
985 BOOT_PAGESIZE;
970 986
971 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
972 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
973 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 987 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
974 988
975 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 989 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
976 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 990 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
977 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 991 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
979 993
980 main_mainstack = PL_mainstack; 994 main_mainstack = PL_mainstack;
981 995
982 coroapi.ver = CORO_API_VERSION; 996 coroapi.ver = CORO_API_VERSION;
983 coroapi.transfer = api_transfer; 997 coroapi.transfer = api_transfer;
984}
985 998
986Coro::State 999 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
987_newprocess(args) 1000}
988 SV * args 1001
989 PROTOTYPE: $ 1002SV *
1003new (char *klass, ...)
990 CODE: 1004 CODE:
991 Coro__State coro; 1005{
1006 struct coro *coro;
1007 HV *hv;
1008 int i;
992 1009
993 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
994 croak ("Coro::State::_newprocess expects an arrayref");
995
996 Newz (0, coro, 1, struct coro); 1010 Newz (0, coro, 1, struct coro);
1011 coro->args = newAV ();
997 1012
998 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 1013 for (i = 1; i < items; i++)
1014 av_push (coro->args, newSVsv (ST (i)));
1015
999 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 1016 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1000 /*coro->stack = 0;*/ 1017 /*coro->stack = 0;*/
1001 1018
1002 /* same as JMPENV_BOOTSTRAP */ 1019 hv = newHV ();
1003 /* we might be able to recycle start_env, but safe is safe */ 1020 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1004 /*Zero(&coro->start_env, 1, JMPENV);*/ 1021 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1005 coro->start_env.je_ret = -1; 1022}
1006 coro->start_env.je_mustcatch = TRUE;
1007
1008 RETVAL = coro;
1009 OUTPUT: 1023 OUTPUT:
1010 RETVAL 1024 RETVAL
1011 1025
1012void 1026void
1013transfer(prev, next, flags) 1027transfer(prev, next, flags)
1014 SV *prev 1028 SV *prev
1015 SV *next 1029 SV *next
1016 int flags 1030 int flags
1017 PROTOTYPE: @
1018 CODE: 1031 CODE:
1019 PUTBACK; 1032 PUTBACK;
1020 SV_CORO (next, "Coro::transfer");
1021 SV_CORO (prev, "Coro::transfer");
1022 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 1033 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1023 SPAGAIN; 1034 SPAGAIN;
1024 1035
1036int
1037prio (Coro::State coro, int newprio = 0)
1038 ALIAS:
1039 nice = 1
1040 CODE:
1041{
1042 RETVAL = coro->prio;
1043
1044 if (items > 1)
1045 {
1046 if (ix)
1047 newprio += coro->prio;
1048
1049 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1050 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1051
1052 coro->prio = newprio;
1053 }
1054}
1055
1025void 1056void
1026DESTROY(coro) 1057_clone_state_from (SV *dst, SV *src)
1027 Coro::State coro 1058 CODE:
1028 CODE: 1059{
1060 struct coro *coro_src = SvSTATE (src);
1029 1061
1030 if (coro->mainstack && coro->mainstack != main_mainstack) 1062 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1031 {
1032 struct coro temp;
1033 1063
1034 PUTBACK; 1064 ++coro_src->refcnt;
1035 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 1065 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1036 LOAD (aTHX_ coro); 1066}
1037 SPAGAIN;
1038
1039 destroy_stacks (aTHX);
1040
1041 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1042 SPAGAIN;
1043
1044 coro->mainstack = 0;
1045 }
1046
1047 deallocate_stack (coro);
1048 SvREFCNT_dec (coro->args);
1049 Safefree (coro);
1050 1067
1051void 1068void
1052_exit(code) 1069_exit (code)
1053 int code 1070 int code
1054 PROTOTYPE: $ 1071 PROTOTYPE: $
1055 CODE: 1072 CODE:
1056 _exit (code); 1073 _exit (code);
1057 1074
1058MODULE = Coro::State PACKAGE = Coro::Cont 1075MODULE = Coro::State PACKAGE = Coro::Cont
1059 1076
1060# this is slightly dirty (should expose a c-level api)
1061
1062void 1077void
1063yield(...) 1078yield (...)
1064 PROTOTYPE: @ 1079 PROTOTYPE: @
1065 CODE: 1080 CODE:
1081{
1066 SV *yieldstack; 1082 SV *yieldstack;
1067 SV *sv; 1083 SV *sv;
1068 AV *defav = GvAV (PL_defgv); 1084 AV *defav = GvAV (PL_defgv);
1069 struct coro *prev, *next; 1085 struct coro *prev, *next;
1070 1086
1079 av_fill (defav, items - 1); 1095 av_fill (defav, items - 1);
1080 while (items--) 1096 while (items--)
1081 av_store (defav, items, SvREFCNT_inc (ST(items))); 1097 av_store (defav, items, SvREFCNT_inc (ST(items)));
1082 1098
1083 sv = av_pop ((AV *)SvRV (yieldstack)); 1099 sv = av_pop ((AV *)SvRV (yieldstack));
1084 prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)))); 1100 prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1085 next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)))); 1101 next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1086 SvREFCNT_dec (sv); 1102 SvREFCNT_dec (sv);
1087 1103
1088 transfer (aTHX_ prev, next, 0); 1104 coro_state_transfer (aTHX_ prev, next, 0);
1105}
1089 1106
1090MODULE = Coro::State PACKAGE = Coro 1107MODULE = Coro::State PACKAGE = Coro
1091 1108
1092# this is slightly dirty (should expose a c-level api)
1093
1094BOOT: 1109BOOT:
1095{ 1110{
1096 int i; 1111 int i;
1112
1097 HV *stash = gv_stashpv ("Coro", TRUE); 1113 coro_stash = gv_stashpv ("Coro", TRUE);
1098 1114
1099 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1115 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1100 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1116 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1101 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1117 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1102 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1118 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1103 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1119 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1104 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1120 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1105 1121
1106 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1122 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1107 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1123 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1108 1124
1109 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1125 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1117 coroapi.ready = api_ready; 1133 coroapi.ready = api_ready;
1118 coroapi.nready = &coro_nready; 1134 coroapi.nready = &coro_nready;
1119 coroapi.current = coro_current; 1135 coroapi.current = coro_current;
1120 1136
1121 GCoroAPI = &coroapi; 1137 GCoroAPI = &coroapi;
1122 sv_setiv(sv, (IV)&coroapi); 1138 sv_setiv (sv, (IV)&coroapi);
1123 SvREADONLY_on(sv); 1139 SvREADONLY_on (sv);
1124 } 1140 }
1125} 1141}
1126 1142
1127#if !PERL_MICRO 1143#if !PERL_MICRO
1128 1144
1129void 1145void
1130ready(self) 1146ready (self)
1131 SV * self 1147 SV * self
1132 PROTOTYPE: $ 1148 PROTOTYPE: $
1133 CODE: 1149 CODE:
1134 api_ready (self); 1150 api_ready (self);
1135 1151
1136#endif 1152#endif
1137 1153
1138int 1154int
1139nready(...) 1155nready (...)
1140 PROTOTYPE: 1156 PROTOTYPE:
1141 CODE: 1157 CODE:
1142 RETVAL = coro_nready; 1158 RETVAL = coro_nready;
1143 OUTPUT: 1159 OUTPUT:
1144 RETVAL 1160 RETVAL
1145 1161
1146void 1162void
1147schedule(...) 1163schedule (...)
1148 PROTOTYPE: 1164 PROTOTYPE:
1149 CODE: 1165 CODE:
1150 api_schedule (); 1166 api_schedule ();
1151 1167
1152void 1168void
1153cede(...) 1169cede (...)
1154 PROTOTYPE: 1170 PROTOTYPE:
1155 CODE: 1171 CODE:
1156 api_cede (); 1172 api_cede ();
1157 1173
1158# and these are hacks 1174# and these are hacks

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines