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.83 by root, Mon Nov 20 22:32:44 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 672static void
715transfer (pTHX_ struct coro *prev, struct coro *next, int flags) 673transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
716{ 674{
717 dSTACKLEVEL; 675 dSTACKLEVEL;
718 676
719 if (prev != next) 677 if (prev != next)
720 { 678 {
679 /* has this coro been created yet? */
721 if (next->mainstack) 680 if (next->mainstack)
722 { 681 {
723 LOCK; 682 LOCK;
724 SAVE (prev, flags); 683 SAVE (prev, flags);
725 LOAD (next); 684 LOAD (next);
764 { 723 {
765 LOCK; 724 LOCK;
766 SAVE (prev, -1); /* first get rid of the old state */ 725 SAVE (prev, -1); /* first get rid of the old state */
767 UNLOCK; 726 UNLOCK;
768 727
728 /* create the coroutine for the first time */
769 if (flags & TRANSFER_SAVE_CCTXT) 729 if (flags & TRANSFER_SAVE_CCTXT)
770 { 730 {
771 if (!prev->stack) 731 if (!prev->stack)
772 allocate_stack (prev, 0); 732 allocate_stack (prev, 0);
773 733
734 /* the new coroutine starts with start_env again */
735 PL_top_env = &PL_start_env;
736
774 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 737 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
775 { 738 {
776 PL_top_env = &next->start_env;
777
778 setup_coro (next); 739 setup_coro (next);
779 next->cursp = stacklevel; 740 next->cursp = stacklevel;
780 741
781 prev->stack->refcnt++; 742 prev->stack->refcnt++;
782 prev->stack->usecnt++; 743 prev->stack->usecnt++;
831 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 792 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
832 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 793 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
833 \ 794 \
834 } while(0) 795 } while(0)
835 796
836#define SvSTATE(sv) (struct coro *)SvIV (sv) 797#define SvSTATE(sv) INT2PTR (struct coro *, SvIVX (sv))
837 798
838static void 799static void
839api_transfer(pTHX_ SV *prev, SV *next, int flags) 800api_transfer(pTHX_ SV *prev, SV *next, int flags)
840{ 801{
841 SV_CORO (prev, "Coro::transfer"); 802 SV_CORO (prev, "Coro::transfer");
853#define PRIO_IDLE -3 814#define PRIO_IDLE -3
854#define PRIO_MIN -4 815#define PRIO_MIN -4
855 816
856/* for Coro.pm */ 817/* for Coro.pm */
857static GV *coro_current, *coro_idle; 818static GV *coro_current, *coro_idle;
858static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 819static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
859static int coro_nready; 820static int coro_nready;
860 821
861static void 822static void
862coro_enq (pTHX_ SV *sv) 823coro_enq (pTHX_ SV *sv)
863{ 824{
825 SV **xprio;
826 int prio;
827
864 if (SvTYPE (sv) == SVt_PVHV) 828 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"); 829 croak ("Coro::ready tried to enqueue something that is not a coroutine");
830
831 xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
832 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
833
834 prio = prio > PRIO_MAX ? PRIO_MAX
835 : prio < PRIO_MIN ? PRIO_MIN
836 : prio;
837
838 av_push (coro_ready [prio - PRIO_MIN], sv);
839 coro_nready++;
880} 840}
881 841
882static SV * 842static SV *
883coro_deq (pTHX_ int min_prio) 843coro_deq (pTHX_ int min_prio)
884{ 844{
887 min_prio -= PRIO_MIN; 847 min_prio -= PRIO_MIN;
888 if (min_prio < 0) 848 if (min_prio < 0)
889 min_prio = 0; 849 min_prio = 0;
890 850
891 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 851 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
892 if (av_len (coro_ready[prio]) >= 0) 852 if (AvFILLp (coro_ready [prio]) >= 0)
893 { 853 {
894 coro_nready--; 854 coro_nready--;
895 return av_shift (coro_ready[prio]); 855 return av_shift (coro_ready [prio]);
896 } 856 }
897 857
898 return 0; 858 return 0;
899} 859}
900 860
959BOOT: 919BOOT:
960{ /* {} necessary for stoopid perl-5.6.x */ 920{ /* {} necessary for stoopid perl-5.6.x */
961#ifdef USE_ITHREADS 921#ifdef USE_ITHREADS
962 MUTEX_INIT (&coro_mutex); 922 MUTEX_INIT (&coro_mutex);
963#endif 923#endif
924 BOOT_PAGESIZE;
964 925
965 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 926 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
966 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 927 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
967 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 928 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
968 929
990 Newz (0, coro, 1, struct coro); 951 Newz (0, coro, 1, struct coro);
991 952
992 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 953 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
993 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 954 /*coro->mainstack = 0; *//*actual work is done inside transfer */
994 /*coro->stack = 0;*/ 955 /*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 956
1002 RETVAL = coro; 957 RETVAL = coro;
1003 OUTPUT: 958 OUTPUT:
1004 RETVAL 959 RETVAL
1005 960
1049 CODE: 1004 CODE:
1050 _exit (code); 1005 _exit (code);
1051 1006
1052MODULE = Coro::State PACKAGE = Coro::Cont 1007MODULE = Coro::State PACKAGE = Coro::Cont
1053 1008
1054# this is slightly dirty (should expose a c-level api)
1055
1056void 1009void
1057yield(...) 1010yield(...)
1058 PROTOTYPE: @ 1011 PROTOTYPE: @
1059 CODE: 1012 CODE:
1060 SV *yieldstack; 1013 SV *yieldstack;
1073 av_fill (defav, items - 1); 1026 av_fill (defav, items - 1);
1074 while (items--) 1027 while (items--)
1075 av_store (defav, items, SvREFCNT_inc (ST(items))); 1028 av_store (defav, items, SvREFCNT_inc (ST(items)));
1076 1029
1077 sv = av_pop ((AV *)SvRV (yieldstack)); 1030 sv = av_pop ((AV *)SvRV (yieldstack));
1078 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1031 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))); 1032 next = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1080 SvREFCNT_dec (sv); 1033 SvREFCNT_dec (sv);
1081 1034
1082 transfer (aTHX_ prev, next, 0); 1035 transfer (aTHX_ prev, next, 0);
1083 1036
1084MODULE = Coro::State PACKAGE = Coro 1037MODULE = Coro::State PACKAGE = Coro
1085
1086# this is slightly dirty (should expose a c-level api)
1087 1038
1088BOOT: 1039BOOT:
1089{ 1040{
1090 int i; 1041 int i;
1091 HV *stash = gv_stashpv ("Coro", TRUE); 1042 HV *stash = gv_stashpv ("Coro", TRUE);
1111 coroapi.ready = api_ready; 1062 coroapi.ready = api_ready;
1112 coroapi.nready = &coro_nready; 1063 coroapi.nready = &coro_nready;
1113 coroapi.current = coro_current; 1064 coroapi.current = coro_current;
1114 1065
1115 GCoroAPI = &coroapi; 1066 GCoroAPI = &coroapi;
1116 sv_setiv(sv, (IV)&coroapi); 1067 sv_setiv (sv, (IV)&coroapi);
1117 SvREADONLY_on(sv); 1068 SvREADONLY_on (sv);
1118 } 1069 }
1119} 1070}
1120 1071
1121#if !PERL_MICRO 1072#if !PERL_MICRO
1122 1073

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines