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.70 by root, Mon Dec 26 19:26:52 2005 UTC vs.
Revision 1.85 by root, Fri Nov 24 00:32:04 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
59# endif
45#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" 63#define UCORO_STATE "_coro_state"
49 64
85 void *sptr; 100 void *sptr;
86 long ssize; /* positive == mmap, otherwise malloc */ 101 long ssize; /* positive == mmap, otherwise malloc */
87} coro_stack; 102} coro_stack;
88 103
89struct coro { 104struct coro {
90 /* the top-level JMPENV for each coroutine, needed to catch dies. */
91 JMPENV start_env;
92
93 /* the optional C context */ 105 /* the optional C context */
94 coro_stack *stack; 106 coro_stack *stack;
95 void *cursp; 107 void *cursp;
96 int gencnt; 108 int gencnt;
97 109
140}; 152};
141 153
142typedef struct coro *Coro__State; 154typedef struct coro *Coro__State;
143typedef struct coro *Coro__State_or_hashref; 155typedef struct coro *Coro__State_or_hashref;
144 156
145/* mostly copied from op.c:cv_clone2 */ 157static AV *
146STATIC AV * 158coro_clone_padlist (pTHX_ CV *cv)
147clone_padlist (pTHX_ AV *protopadlist)
148{ 159{
149 AV *av; 160 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; 161 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 162
168 newpadlist = newAV (); 163 newpadlist = newAV ();
169 AvREAL_off (newpadlist); 164 AvREAL_off (newpadlist);
170 av_store (newpadlist, 0, (SV *) newpad_name); 165#if PERL_VERSION < 9
166 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
167#else
168 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
169#endif
170 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
171 --AvFILLp (padlist);
172
173 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
171 av_store (newpadlist, 1, (SV *) newpad); 174 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 AvFLAGS (av) = AVf_REIFY;
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 175
242 return newpadlist; 176 return newpadlist;
243} 177}
244 178
245STATIC void 179static void
246free_padlist (pTHX_ AV *padlist) 180free_padlist (pTHX_ AV *padlist)
247{ 181{
248 /* may be during global destruction */ 182 /* may be during global destruction */
249 if (SvREFCNT (padlist)) 183 if (SvREFCNT (padlist))
250 { 184 {
264 198
265 SvREFCNT_dec ((SV*)padlist); 199 SvREFCNT_dec ((SV*)padlist);
266 } 200 }
267} 201}
268 202
269STATIC int 203static int
270coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 204coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
271{ 205{
272 AV *padlist; 206 AV *padlist;
273 AV *av = (AV *)mg->mg_obj; 207 AV *av = (AV *)mg->mg_obj;
274 208
275 /* casting is fun. */ 209 /* casting is fun. */
276 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 210 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
277 free_padlist (aTHX_ padlist); 211 free_padlist (aTHX_ padlist);
278 212
279 SvREFCNT_dec (av); 213 SvREFCNT_dec (av);
214
215 return 0;
280} 216}
281 217
282#define PERL_MAGIC_coro PERL_MAGIC_ext 218#define PERL_MAGIC_coro PERL_MAGIC_ext
283 219
284static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 220static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
285 221
286/* the next two functions merely cache the padlists */ 222/* the next two functions merely cache the padlists */
287STATIC void 223static void
288get_padlist (pTHX_ CV *cv) 224get_padlist (pTHX_ CV *cv)
289{ 225{
290 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 226 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
291 227
292 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 228 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
293 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 229 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
294 else 230 else
231 {
232#if 0
233 /* this should work - but it doesn't :( */
234 CV *cp = Perl_cv_clone (aTHX_ cv);
235 CvPADLIST (cv) = CvPADLIST (cp);
236 CvPADLIST (cp) = 0;
237 SvREFCNT_dec (cp);
238#else
295 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); 239 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
240#endif
241 }
296} 242}
297 243
298STATIC void 244static void
299put_padlist (pTHX_ CV *cv) 245put_padlist (pTHX_ CV *cv)
300{ 246{
301 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 247 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
302 248
303 if (!mg) 249 if (!mg)
346 PL_scopestack_ix = c->scopestack_ix; 292 PL_scopestack_ix = c->scopestack_ix;
347 PL_scopestack_max = c->scopestack_max; 293 PL_scopestack_max = c->scopestack_max;
348 PL_savestack = c->savestack; 294 PL_savestack = c->savestack;
349 PL_savestack_ix = c->savestack_ix; 295 PL_savestack_ix = c->savestack_ix;
350 PL_savestack_max = c->savestack_max; 296 PL_savestack_max = c->savestack_max;
297#if PERL_VERSION < 9
351 PL_retstack = c->retstack; 298 PL_retstack = c->retstack;
352 PL_retstack_ix = c->retstack_ix; 299 PL_retstack_ix = c->retstack_ix;
353 PL_retstack_max = c->retstack_max; 300 PL_retstack_max = c->retstack_max;
301#endif
354 PL_curpm = c->curpm; 302 PL_curpm = c->curpm;
355 PL_curcop = c->curcop; 303 PL_curcop = c->curcop;
356 PL_top_env = c->top_env; 304 PL_top_env = c->top_env;
357 305
358 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
418 } 366 }
419 367
420 PUSHs ((SV *)CvPADLIST(cv)); 368 PUSHs ((SV *)CvPADLIST(cv));
421 PUSHs ((SV *)cv); 369 PUSHs ((SV *)cv);
422 370
423 get_padlist (aTHX_ cv); /* this is a monster */ 371 get_padlist (aTHX_ cv);
424 } 372 }
425 } 373 }
426#ifdef CXt_FORMAT 374#ifdef CXt_FORMAT
427 else if (CxTYPE(cx) == CXt_FORMAT) 375 else if (CxTYPE(cx) == CXt_FORMAT)
428 { 376 {
472 c->scopestack_ix = PL_scopestack_ix; 420 c->scopestack_ix = PL_scopestack_ix;
473 c->scopestack_max = PL_scopestack_max; 421 c->scopestack_max = PL_scopestack_max;
474 c->savestack = PL_savestack; 422 c->savestack = PL_savestack;
475 c->savestack_ix = PL_savestack_ix; 423 c->savestack_ix = PL_savestack_ix;
476 c->savestack_max = PL_savestack_max; 424 c->savestack_max = PL_savestack_max;
425#if PERL_VERSION < 9
477 c->retstack = PL_retstack; 426 c->retstack = PL_retstack;
478 c->retstack_ix = PL_retstack_ix; 427 c->retstack_ix = PL_retstack_ix;
479 c->retstack_max = PL_retstack_max; 428 c->retstack_max = PL_retstack_max;
429#endif
480 c->curpm = PL_curpm; 430 c->curpm = PL_curpm;
481 c->curcop = PL_curcop; 431 c->curcop = PL_curcop;
482 c->top_env = PL_top_env; 432 c->top_env = PL_top_env;
483} 433}
484 434
486 * allocate various perl stacks. This is an exact copy 436 * allocate various perl stacks. This is an exact copy
487 * of perl.c:init_stacks, except that it uses less memory 437 * of perl.c:init_stacks, except that it uses less memory
488 * on the (sometimes correct) assumption that coroutines do 438 * on the (sometimes correct) assumption that coroutines do
489 * not usually need a lot of stackspace. 439 * not usually need a lot of stackspace.
490 */ 440 */
491STATIC void 441static void
492coro_init_stacks (pTHX) 442coro_init_stacks (pTHX)
493{ 443{
494 LOCK; 444 LOCK;
495 445
496 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 446 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
521 471
522 New(54,PL_savestack,96,ANY); 472 New(54,PL_savestack,96,ANY);
523 PL_savestack_ix = 0; 473 PL_savestack_ix = 0;
524 PL_savestack_max = 96; 474 PL_savestack_max = 96;
525 475
476#if PERL_VERSION < 9
526 New(54,PL_retstack,8,OP*); 477 New(54,PL_retstack,8,OP*);
527 PL_retstack_ix = 0; 478 PL_retstack_ix = 0;
528 PL_retstack_max = 8; 479 PL_retstack_max = 8;
480#endif
529 481
530 UNLOCK; 482 UNLOCK;
531} 483}
532 484
533/* 485/*
534 * destroy the stacks, the callchain etc... 486 * destroy the stacks, the callchain etc...
535 */ 487 */
536STATIC void 488static void
537destroy_stacks(pTHX) 489destroy_stacks(pTHX)
538{ 490{
539 if (!IN_DESTRUCT) 491 if (!IN_DESTRUCT)
540 { 492 {
541 /* is this ugly, I ask? */ 493 /* is this ugly, I ask? */
573 525
574 Safefree (PL_tmps_stack); 526 Safefree (PL_tmps_stack);
575 Safefree (PL_markstack); 527 Safefree (PL_markstack);
576 Safefree (PL_scopestack); 528 Safefree (PL_scopestack);
577 Safefree (PL_savestack); 529 Safefree (PL_savestack);
530#if PERL_VERSION < 9
578 Safefree (PL_retstack); 531 Safefree (PL_retstack);
532#endif
579} 533}
580 534
581static void 535static void
582allocate_stack (Coro__State ctx, int alloc) 536allocate_stack (Coro__State ctx, int alloc)
583{ 537{
590 stack->gencnt = ctx->gencnt = 0; 544 stack->gencnt = ctx->gencnt = 0;
591 545
592 if (alloc) 546 if (alloc)
593 { 547 {
594#if HAVE_MMAP 548#if HAVE_MMAP
595 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ 549 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
596 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 550 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
597 if (stack->sptr == (void *)-1) 551 if (stack->sptr != (void *)-1)
598#endif
599 { 552 {
600 /*FIXME*//*D*//* reasonable stack size! */ 553# if STACKGUARD
554 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
555# endif
556 }
557 else
558#endif
559 {
601 stack->ssize = - (STACKSIZE * sizeof (long)); 560 stack->ssize = - (STACKSIZE * (long)sizeof (long));
602 New (0, stack->sptr, STACKSIZE, long); 561 New (0, stack->sptr, STACKSIZE, long);
603 } 562 }
604 } 563 }
605 else 564 else
606 stack->sptr = 0; 565 stack->sptr = 0;
667 } 626 }
668 else 627 else
669 { 628 {
670 UNOP myop; 629 UNOP myop;
671 630
672 PL_op = (OP *)&myop;
673
674 Zero(&myop, 1, UNOP); 631 Zero(&myop, 1, UNOP);
675 myop.op_next = Nullop; 632 myop.op_next = Nullop;
676 myop.op_flags = OPf_WANT_VOID; 633 myop.op_flags = OPf_WANT_VOID;
634
635 PL_op = (OP *)&myop;
677 636
678 PUSHMARK(SP); 637 PUSHMARK(SP);
679 XPUSHs (sub_init); 638 XPUSHs (sub_init);
680 /* 639 /*
681 * the next line is slightly wrong, as PL_op->op_next 640 * the next line is slightly wrong, as PL_op->op_next
698 /* 657 /*
699 * this is a _very_ stripped down perl interpreter ;) 658 * this is a _very_ stripped down perl interpreter ;)
700 */ 659 */
701 dTHX; 660 dTHX;
702 Coro__State ctx = (Coro__State)arg; 661 Coro__State ctx = (Coro__State)arg;
703 JMPENV coro_start_env;
704 662
705 PL_top_env = &ctx->start_env; 663 PL_top_env = &PL_start_env;
706 664
707 ctx->cursp = 0; 665 ctx->cursp = 0;
708 PL_op = PL_op->op_next; 666 PL_op = PL_op->op_next;
709 CALLRUNOPS(aTHX); 667 CALLRUNOPS(aTHX);
710 668
711 abort (); 669 abort ();
712} 670}
713 671
714STATIC void 672/* never call directly, always through the coro_state_transfer global variable */
673static void
715transfer (pTHX_ struct coro *prev, struct coro *next, int flags) 674transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
716{ 675{
717 dSTACKLEVEL; 676 dSTACKLEVEL;
718 677
719 if (prev != next) 678 if (prev != next)
720 { 679 {
680 /* has this coro been created yet? */
721 if (next->mainstack) 681 if (next->mainstack)
722 { 682 {
723 LOCK; 683 LOCK;
724 SAVE (prev, flags); 684 SAVE (prev, flags);
725 LOAD (next); 685 LOAD (next);
764 { 724 {
765 LOCK; 725 LOCK;
766 SAVE (prev, -1); /* first get rid of the old state */ 726 SAVE (prev, -1); /* first get rid of the old state */
767 UNLOCK; 727 UNLOCK;
768 728
729 /* create the coroutine for the first time */
769 if (flags & TRANSFER_SAVE_CCTXT) 730 if (flags & TRANSFER_SAVE_CCTXT)
770 { 731 {
771 if (!prev->stack) 732 if (!prev->stack)
772 allocate_stack (prev, 0); 733 allocate_stack (prev, 0);
773 734
735 /* the new coroutine starts with start_env again */
736 PL_top_env = &PL_start_env;
737
774 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 738 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
775 { 739 {
776 PL_top_env = &next->start_env;
777
778 setup_coro (next); 740 setup_coro (next);
779 next->cursp = stacklevel; 741 next->cursp = stacklevel;
780 742
781 prev->stack->refcnt++; 743 prev->stack->refcnt++;
782 prev->stack->usecnt++; 744 prev->stack->usecnt++;
810 coro_mortal = 0; 772 coro_mortal = 0;
811 } 773 }
812 UNLOCK; 774 UNLOCK;
813} 775}
814 776
777/* use this function pointer to call the above function */
778/* this is done to increase chances of the compiler not inlining the call */
779void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
780
815#define SV_CORO(sv,func) \ 781#define SV_CORO(sv,func) \
816 do { \ 782 do { \
817 if (SvROK (sv)) \ 783 if (SvROK (sv)) \
818 sv = SvRV (sv); \ 784 sv = SvRV (sv); \
819 \ 785 \
831 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 797 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
832 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 798 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
833 \ 799 \
834 } while(0) 800 } while(0)
835 801
836#define SvSTATE(sv) (struct coro *)SvIV (sv) 802#define SvSTATE(sv) INT2PTR (struct coro *, SvIVX (sv))
837 803
838static void 804static void
839api_transfer(pTHX_ SV *prev, SV *next, int flags) 805api_transfer (pTHX_ SV *prev, SV *next, int flags)
840{ 806{
841 SV_CORO (prev, "Coro::transfer"); 807 SV_CORO (prev, "Coro::transfer");
842 SV_CORO (next, "Coro::transfer"); 808 SV_CORO (next, "Coro::transfer");
843 809
844 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 810 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
845} 811}
846 812
847/** Coro ********************************************************************/ 813/** Coro ********************************************************************/
848 814
849#define PRIO_MAX 3 815#define PRIO_MAX 3
853#define PRIO_IDLE -3 819#define PRIO_IDLE -3
854#define PRIO_MIN -4 820#define PRIO_MIN -4
855 821
856/* for Coro.pm */ 822/* for Coro.pm */
857static GV *coro_current, *coro_idle; 823static GV *coro_current, *coro_idle;
858static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 824static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
859static int coro_nready; 825static int coro_nready;
860 826
861static void 827static void
862coro_enq (pTHX_ SV *sv) 828coro_enq (pTHX_ SV *sv)
863{ 829{
830 SV **xprio;
831 int prio;
832
864 if (SvTYPE (sv) == SVt_PVHV) 833 if (SvTYPE (sv) != SVt_PVHV)
865 {
866 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
867 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
868
869 prio = prio > PRIO_MAX ? PRIO_MAX
870 : prio < PRIO_MIN ? PRIO_MIN
871 : prio;
872
873 av_push (coro_ready [prio - PRIO_MIN], sv);
874 coro_nready++;
875
876 return;
877 }
878
879 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 834 croak ("Coro::ready tried to enqueue something that is not a coroutine");
835
836 xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
837 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
838
839 prio = prio > PRIO_MAX ? PRIO_MAX
840 : prio < PRIO_MIN ? PRIO_MIN
841 : prio;
842
843 av_push (coro_ready [prio - PRIO_MIN], sv);
844 coro_nready++;
880} 845}
881 846
882static SV * 847static SV *
883coro_deq (pTHX_ int min_prio) 848coro_deq (pTHX_ int min_prio)
884{ 849{
887 min_prio -= PRIO_MIN; 852 min_prio -= PRIO_MIN;
888 if (min_prio < 0) 853 if (min_prio < 0)
889 min_prio = 0; 854 min_prio = 0;
890 855
891 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 856 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
892 if (av_len (coro_ready[prio]) >= 0) 857 if (AvFILLp (coro_ready [prio]) >= 0)
893 { 858 {
894 coro_nready--; 859 coro_nready--;
895 return av_shift (coro_ready[prio]); 860 return av_shift (coro_ready [prio]);
896 } 861 }
897 862
898 return 0; 863 return 0;
899} 864}
900 865
934 899
935 SV_CORO (next, "Coro::schedule"); 900 SV_CORO (next, "Coro::schedule");
936 901
937 UNLOCK; 902 UNLOCK;
938 903
939 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 904 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
940 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 905 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
941} 906}
942 907
943static void 908static void
944api_cede (void) 909api_cede (void)
945{ 910{
959BOOT: 924BOOT:
960{ /* {} necessary for stoopid perl-5.6.x */ 925{ /* {} necessary for stoopid perl-5.6.x */
961#ifdef USE_ITHREADS 926#ifdef USE_ITHREADS
962 MUTEX_INIT (&coro_mutex); 927 MUTEX_INIT (&coro_mutex);
963#endif 928#endif
929 BOOT_PAGESIZE;
964 930
965 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 931 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
966 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 932 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
967 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 933 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
968 934
990 Newz (0, coro, 1, struct coro); 956 Newz (0, coro, 1, struct coro);
991 957
992 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 958 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
993 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 959 /*coro->mainstack = 0; *//*actual work is done inside transfer */
994 /*coro->stack = 0;*/ 960 /*coro->stack = 0;*/
995
996 /* same as JMPENV_BOOTSTRAP */
997 /* we might be able to recycle start_env, but safe is safe */
998 /*Zero(&coro->start_env, 1, JMPENV);*/
999 coro->start_env.je_ret = -1;
1000 coro->start_env.je_mustcatch = TRUE;
1001 961
1002 RETVAL = coro; 962 RETVAL = coro;
1003 OUTPUT: 963 OUTPUT:
1004 RETVAL 964 RETVAL
1005 965
1011 PROTOTYPE: @ 971 PROTOTYPE: @
1012 CODE: 972 CODE:
1013 PUTBACK; 973 PUTBACK;
1014 SV_CORO (next, "Coro::transfer"); 974 SV_CORO (next, "Coro::transfer");
1015 SV_CORO (prev, "Coro::transfer"); 975 SV_CORO (prev, "Coro::transfer");
1016 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 976 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1017 SPAGAIN; 977 SPAGAIN;
1018 978
1019void 979void
1020DESTROY(coro) 980DESTROY(coro)
1021 Coro::State coro 981 Coro::State coro
1048 PROTOTYPE: $ 1008 PROTOTYPE: $
1049 CODE: 1009 CODE:
1050 _exit (code); 1010 _exit (code);
1051 1011
1052MODULE = Coro::State PACKAGE = Coro::Cont 1012MODULE = Coro::State PACKAGE = Coro::Cont
1053
1054# this is slightly dirty (should expose a c-level api)
1055 1013
1056void 1014void
1057yield(...) 1015yield(...)
1058 PROTOTYPE: @ 1016 PROTOTYPE: @
1059 CODE: 1017 CODE:
1073 av_fill (defav, items - 1); 1031 av_fill (defav, items - 1);
1074 while (items--) 1032 while (items--)
1075 av_store (defav, items, SvREFCNT_inc (ST(items))); 1033 av_store (defav, items, SvREFCNT_inc (ST(items)));
1076 1034
1077 sv = av_pop ((AV *)SvRV (yieldstack)); 1035 sv = av_pop ((AV *)SvRV (yieldstack));
1078 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1036 prev = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1079 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1037 next = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1080 SvREFCNT_dec (sv); 1038 SvREFCNT_dec (sv);
1081 1039
1082 transfer (aTHX_ prev, next, 0); 1040 coro_state_transfer (aTHX_ prev, next, 0);
1083 1041
1084MODULE = Coro::State PACKAGE = Coro 1042MODULE = Coro::State PACKAGE = Coro
1085
1086# this is slightly dirty (should expose a c-level api)
1087 1043
1088BOOT: 1044BOOT:
1089{ 1045{
1090 int i; 1046 int i;
1091 HV *stash = gv_stashpv ("Coro", TRUE); 1047 HV *stash = gv_stashpv ("Coro", TRUE);
1111 coroapi.ready = api_ready; 1067 coroapi.ready = api_ready;
1112 coroapi.nready = &coro_nready; 1068 coroapi.nready = &coro_nready;
1113 coroapi.current = coro_current; 1069 coroapi.current = coro_current;
1114 1070
1115 GCoroAPI = &coroapi; 1071 GCoroAPI = &coroapi;
1116 sv_setiv(sv, (IV)&coroapi); 1072 sv_setiv (sv, (IV)&coroapi);
1117 SvREADONLY_on(sv); 1073 SvREADONLY_on (sv);
1118 } 1074 }
1119} 1075}
1120 1076
1121#if !PERL_MICRO 1077#if !PERL_MICRO
1122 1078

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines