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.74 by root, Mon Oct 23 22:49:09 2006 UTC vs.
Revision 1.88 by root, Fri Nov 24 15:34:33 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines