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.67 by root, Mon Dec 12 20:07:44 2005 UTC vs.
Revision 1.84 by root, Fri Nov 24 00:31:21 2006 UTC

5#include "EXTERN.h" 5#include "EXTERN.h"
6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8 8
9#include "patchlevel.h" 9#include "patchlevel.h"
10
11// threaded perls might need much more, 65536 or more,
12// which is 0.5 to 1MB
13#define STACKSIZE 16384
14 10
15#if PERL_VERSION < 6 11#if PERL_VERSION < 6
16# ifndef PL_ppaddr 12# ifndef PL_ppaddr
17# define PL_ppaddr ppaddr 13# define PL_ppaddr ppaddr
18# endif 14# endif
31# ifndef IS_PADCONST 27# ifndef IS_PADCONST
32# define IS_PADCONST(v) 0 28# define IS_PADCONST(v) 0
33# endif 29# endif
34#endif 30#endif
35 31
36#include <signal.h> 32#include <errno.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
37 41
38#ifdef HAVE_MMAP 42#ifdef HAVE_MMAP
39# include <unistd.h> 43# include <unistd.h>
40# include <sys/mman.h> 44# include <sys/mman.h>
41# ifndef MAP_ANONYMOUS 45# ifndef MAP_ANONYMOUS
43# define MAP_ANONYMOUS MAP_ANON 47# define MAP_ANONYMOUS MAP_ANON
44# else 48# else
45# undef HAVE_MMAP 49# undef HAVE_MMAP
46# endif 50# endif
47# 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
48#endif 60#endif
49 61
50#define SUB_INIT "Coro::State::initialize" 62#define SUB_INIT "Coro::State::initialize"
51#define UCORO_STATE "_coro_state" 63#define UCORO_STATE "_coro_state"
52 64
61 73
62#include "CoroAPI.h" 74#include "CoroAPI.h"
63 75
64#ifdef USE_ITHREADS 76#ifdef USE_ITHREADS
65static perl_mutex coro_mutex; 77static perl_mutex coro_mutex;
66# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 78# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
67# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 79# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
68#else 80#else
69# define LOCK 0 81# define LOCK (void)0
70# define UNLOCK 0 82# define UNLOCK (void)0
71#endif 83#endif
72 84
73static struct CoroAPI coroapi; 85static struct CoroAPI coroapi;
74static AV *main_mainstack; /* used to differentiate between $main and others */ 86static AV *main_mainstack; /* used to differentiate between $main and others */
75static HV *coro_state_stash; 87static HV *coro_state_stash;
88 void *sptr; 100 void *sptr;
89 long ssize; /* positive == mmap, otherwise malloc */ 101 long ssize; /* positive == mmap, otherwise malloc */
90} coro_stack; 102} coro_stack;
91 103
92struct coro { 104struct coro {
93 /* the top-level JMPENV for each coroutine, needed to catch dies. */
94 JMPENV start_env;
95
96 /* the optional C context */ 105 /* the optional C context */
97 coro_stack *stack; 106 coro_stack *stack;
98 void *cursp; 107 void *cursp;
99 int gencnt; 108 int gencnt;
100 109
143}; 152};
144 153
145typedef struct coro *Coro__State; 154typedef struct coro *Coro__State;
146typedef struct coro *Coro__State_or_hashref; 155typedef struct coro *Coro__State_or_hashref;
147 156
148/* mostly copied from op.c:cv_clone2 */ 157static AV *
149STATIC AV * 158coro_clone_padlist (pTHX_ CV *cv)
150clone_padlist (pTHX_ AV *protopadlist)
151{ 159{
152 AV *av; 160 AV *padlist = CvPADLIST (cv);
153 I32 ix;
154 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
155 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
156 SV **pname = AvARRAY (protopad_name);
157 SV **ppad = AvARRAY (protopad);
158 I32 fname = AvFILLp (protopad_name);
159 I32 fpad = AvFILLp (protopad);
160 AV *newpadlist, *newpad_name, *newpad; 161 AV *newpadlist, *newpad;
161 SV **npad;
162
163 newpad_name = newAV ();
164 for (ix = fname; ix >= 0; ix--)
165 av_store (newpad_name, ix, SvREFCNT_inc (pname[ix]));
166
167 newpad = newAV ();
168 av_fill (newpad, AvFILLp (protopad));
169 npad = AvARRAY (newpad);
170 162
171 newpadlist = newAV (); 163 newpadlist = newAV ();
172 AvREAL_off (newpadlist); 164 AvREAL_off (newpadlist);
173 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)));
174 av_store (newpadlist, 1, (SV *) newpad); 174 av_store (newpadlist, 1, (SV *)newpad);
175
176 av = newAV (); /* will be @_ */
177 av_extend (av, 0);
178 av_store (newpad, 0, (SV *) av);
179 AvFLAGS (av) = AVf_REIFY;
180
181 for (ix = fpad; ix > 0; ix--)
182 {
183 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
184
185 if (namesv && namesv != &PL_sv_undef)
186 {
187 char *name = SvPVX (namesv); /* XXX */
188
189 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
190 { /* lexical from outside? */
191 npad[ix] = SvREFCNT_inc (ppad[ix]);
192 }
193 else
194 { /* our own lexical */
195 SV *sv;
196 if (*name == '&')
197 sv = SvREFCNT_inc (ppad[ix]);
198 else if (*name == '@')
199 sv = (SV *) newAV ();
200 else if (*name == '%')
201 sv = (SV *) newHV ();
202 else
203 sv = NEWSV (0, 0);
204
205#ifdef SvPADBUSY
206 if (!SvPADBUSY (sv))
207#endif
208 SvPADMY_on (sv);
209
210 npad[ix] = sv;
211 }
212 }
213 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
214 {
215 npad[ix] = SvREFCNT_inc (ppad[ix]);
216 }
217 else
218 {
219 SV *sv = NEWSV (0, 0);
220 SvPADTMP_on (sv);
221 npad[ix] = sv;
222 }
223 }
224
225#if 0 /* return -ENOTUNDERSTOOD */
226 /* Now that vars are all in place, clone nested closures. */
227
228 for (ix = fpad; ix > 0; ix--) {
229 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
230 if (namesv
231 && namesv != &PL_sv_undef
232 && !(SvFLAGS(namesv) & SVf_FAKE)
233 && *SvPVX(namesv) == '&'
234 && CvCLONE(ppad[ix]))
235 {
236 CV *kid = cv_clone((CV*)ppad[ix]);
237 SvREFCNT_dec(ppad[ix]);
238 CvCLONE_on(kid);
239 SvPADMY_on(kid);
240 npad[ix] = (SV*)kid;
241 }
242 }
243#endif
244 175
245 return newpadlist; 176 return newpadlist;
246} 177}
247 178
248STATIC void 179static void
249free_padlist (pTHX_ AV *padlist) 180free_padlist (pTHX_ AV *padlist)
250{ 181{
251 /* may be during global destruction */ 182 /* may be during global destruction */
252 if (SvREFCNT (padlist)) 183 if (SvREFCNT (padlist))
253 { 184 {
267 198
268 SvREFCNT_dec ((SV*)padlist); 199 SvREFCNT_dec ((SV*)padlist);
269 } 200 }
270} 201}
271 202
272STATIC int 203static int
273coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 204coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
274{ 205{
275 AV *padlist; 206 AV *padlist;
276 AV *av = (AV *)mg->mg_obj; 207 AV *av = (AV *)mg->mg_obj;
277 208
278 /* casting is fun. */ 209 /* casting is fun. */
279 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 210 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
280 free_padlist (aTHX_ padlist); 211 free_padlist (aTHX_ padlist);
281 212
282 SvREFCNT_dec (av); 213 SvREFCNT_dec (av);
214
215 return 0;
283} 216}
284 217
285#define PERL_MAGIC_coro PERL_MAGIC_ext 218#define PERL_MAGIC_coro PERL_MAGIC_ext
286 219
287static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 220static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
288 221
289/* the next two functions merely cache the padlists */ 222/* the next two functions merely cache the padlists */
290STATIC void 223static void
291get_padlist (pTHX_ CV *cv) 224get_padlist (pTHX_ CV *cv)
292{ 225{
293 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 226 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
294 227
295 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 228 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
296 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 229 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
297 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
298 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); 239 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
240#endif
241 }
299} 242}
300 243
301STATIC void 244static void
302put_padlist (pTHX_ CV *cv) 245put_padlist (pTHX_ CV *cv)
303{ 246{
304 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 247 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
305 248
306 if (!mg) 249 if (!mg)
349 PL_scopestack_ix = c->scopestack_ix; 292 PL_scopestack_ix = c->scopestack_ix;
350 PL_scopestack_max = c->scopestack_max; 293 PL_scopestack_max = c->scopestack_max;
351 PL_savestack = c->savestack; 294 PL_savestack = c->savestack;
352 PL_savestack_ix = c->savestack_ix; 295 PL_savestack_ix = c->savestack_ix;
353 PL_savestack_max = c->savestack_max; 296 PL_savestack_max = c->savestack_max;
297#if PERL_VERSION < 9
354 PL_retstack = c->retstack; 298 PL_retstack = c->retstack;
355 PL_retstack_ix = c->retstack_ix; 299 PL_retstack_ix = c->retstack_ix;
356 PL_retstack_max = c->retstack_max; 300 PL_retstack_max = c->retstack_max;
301#endif
357 PL_curpm = c->curpm; 302 PL_curpm = c->curpm;
358 PL_curcop = c->curcop; 303 PL_curcop = c->curcop;
359 PL_top_env = c->top_env; 304 PL_top_env = c->top_env;
360 305
361 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
421 } 366 }
422 367
423 PUSHs ((SV *)CvPADLIST(cv)); 368 PUSHs ((SV *)CvPADLIST(cv));
424 PUSHs ((SV *)cv); 369 PUSHs ((SV *)cv);
425 370
426 get_padlist (aTHX_ cv); /* this is a monster */ 371 get_padlist (aTHX_ cv);
427 } 372 }
428 } 373 }
429#ifdef CXt_FORMAT 374#ifdef CXt_FORMAT
430 else if (CxTYPE(cx) == CXt_FORMAT) 375 else if (CxTYPE(cx) == CXt_FORMAT)
431 { 376 {
475 c->scopestack_ix = PL_scopestack_ix; 420 c->scopestack_ix = PL_scopestack_ix;
476 c->scopestack_max = PL_scopestack_max; 421 c->scopestack_max = PL_scopestack_max;
477 c->savestack = PL_savestack; 422 c->savestack = PL_savestack;
478 c->savestack_ix = PL_savestack_ix; 423 c->savestack_ix = PL_savestack_ix;
479 c->savestack_max = PL_savestack_max; 424 c->savestack_max = PL_savestack_max;
425#if PERL_VERSION < 9
480 c->retstack = PL_retstack; 426 c->retstack = PL_retstack;
481 c->retstack_ix = PL_retstack_ix; 427 c->retstack_ix = PL_retstack_ix;
482 c->retstack_max = PL_retstack_max; 428 c->retstack_max = PL_retstack_max;
429#endif
483 c->curpm = PL_curpm; 430 c->curpm = PL_curpm;
484 c->curcop = PL_curcop; 431 c->curcop = PL_curcop;
485 c->top_env = PL_top_env; 432 c->top_env = PL_top_env;
486} 433}
487 434
489 * allocate various perl stacks. This is an exact copy 436 * allocate various perl stacks. This is an exact copy
490 * of perl.c:init_stacks, except that it uses less memory 437 * of perl.c:init_stacks, except that it uses less memory
491 * on the (sometimes correct) assumption that coroutines do 438 * on the (sometimes correct) assumption that coroutines do
492 * not usually need a lot of stackspace. 439 * not usually need a lot of stackspace.
493 */ 440 */
494STATIC void 441static void
495coro_init_stacks (pTHX) 442coro_init_stacks (pTHX)
496{ 443{
497 LOCK; 444 LOCK;
498 445
499 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 446 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
524 471
525 New(54,PL_savestack,96,ANY); 472 New(54,PL_savestack,96,ANY);
526 PL_savestack_ix = 0; 473 PL_savestack_ix = 0;
527 PL_savestack_max = 96; 474 PL_savestack_max = 96;
528 475
476#if PERL_VERSION < 9
529 New(54,PL_retstack,8,OP*); 477 New(54,PL_retstack,8,OP*);
530 PL_retstack_ix = 0; 478 PL_retstack_ix = 0;
531 PL_retstack_max = 8; 479 PL_retstack_max = 8;
480#endif
532 481
533 UNLOCK; 482 UNLOCK;
534} 483}
535 484
536/* 485/*
537 * destroy the stacks, the callchain etc... 486 * destroy the stacks, the callchain etc...
538 */ 487 */
539STATIC void 488static void
540destroy_stacks(pTHX) 489destroy_stacks(pTHX)
541{ 490{
542 if (!IN_DESTRUCT) 491 if (!IN_DESTRUCT)
543 { 492 {
544 /* is this ugly, I ask? */ 493 /* is this ugly, I ask? */
576 525
577 Safefree (PL_tmps_stack); 526 Safefree (PL_tmps_stack);
578 Safefree (PL_markstack); 527 Safefree (PL_markstack);
579 Safefree (PL_scopestack); 528 Safefree (PL_scopestack);
580 Safefree (PL_savestack); 529 Safefree (PL_savestack);
530#if PERL_VERSION < 9
581 Safefree (PL_retstack); 531 Safefree (PL_retstack);
532#endif
582} 533}
583 534
584static void 535static void
585allocate_stack (Coro__State ctx, int alloc) 536allocate_stack (Coro__State ctx, int alloc)
586{ 537{
593 stack->gencnt = ctx->gencnt = 0; 544 stack->gencnt = ctx->gencnt = 0;
594 545
595 if (alloc) 546 if (alloc)
596 { 547 {
597#if HAVE_MMAP 548#if HAVE_MMAP
598 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 */
599 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);
600 if (stack->sptr == (void *)-1) 551 if (stack->sptr != (void *)-1)
601#endif
602 { 552 {
603 /*FIXME*//*D*//* reasonable stack size! */ 553# if STACKGUARD
554 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
555# endif
556 }
557 else
558#endif
559 {
604 stack->ssize = - (STACKSIZE * sizeof (long)); 560 stack->ssize = - (STACKSIZE * (long)sizeof (long));
605 New (0, stack->sptr, STACKSIZE, long); 561 New (0, stack->sptr, STACKSIZE, long);
606 } 562 }
607 } 563 }
608 else 564 else
609 stack->sptr = 0; 565 stack->sptr = 0;
670 } 626 }
671 else 627 else
672 { 628 {
673 UNOP myop; 629 UNOP myop;
674 630
675 PL_op = (OP *)&myop;
676
677 Zero(&myop, 1, UNOP); 631 Zero(&myop, 1, UNOP);
678 myop.op_next = Nullop; 632 myop.op_next = Nullop;
679 myop.op_flags = OPf_WANT_VOID; 633 myop.op_flags = OPf_WANT_VOID;
634
635 PL_op = (OP *)&myop;
680 636
681 PUSHMARK(SP); 637 PUSHMARK(SP);
682 XPUSHs (sub_init); 638 XPUSHs (sub_init);
683 /* 639 /*
684 * the next line is slightly wrong, as PL_op->op_next 640 * the next line is slightly wrong, as PL_op->op_next
701 /* 657 /*
702 * this is a _very_ stripped down perl interpreter ;) 658 * this is a _very_ stripped down perl interpreter ;)
703 */ 659 */
704 dTHX; 660 dTHX;
705 Coro__State ctx = (Coro__State)arg; 661 Coro__State ctx = (Coro__State)arg;
706 JMPENV coro_start_env;
707 662
708 PL_top_env = &ctx->start_env; 663 PL_top_env = &PL_start_env;
709 664
710 ctx->cursp = 0; 665 ctx->cursp = 0;
711 PL_op = PL_op->op_next; 666 PL_op = PL_op->op_next;
712 CALLRUNOPS(aTHX); 667 CALLRUNOPS(aTHX);
713 668
714 abort (); 669 abort ();
715} 670}
716 671
717STATIC void 672/* never call directly, always through the coro_state_transfer global variable */
673static void
718transfer (pTHX_ struct coro *prev, struct coro *next, int flags) 674transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
719{ 675{
720 dSTACKLEVEL; 676 dSTACKLEVEL;
721 677
722 if (prev != next) 678 if (prev != next)
723 { 679 {
680 /* has this coro been created yet? */
724 if (next->mainstack) 681 if (next->mainstack)
725 { 682 {
726 LOCK; 683 LOCK;
727 SAVE (prev, flags); 684 SAVE (prev, flags);
728 LOAD (next); 685 LOAD (next);
767 { 724 {
768 LOCK; 725 LOCK;
769 SAVE (prev, -1); /* first get rid of the old state */ 726 SAVE (prev, -1); /* first get rid of the old state */
770 UNLOCK; 727 UNLOCK;
771 728
729 /* create the coroutine for the first time */
772 if (flags & TRANSFER_SAVE_CCTXT) 730 if (flags & TRANSFER_SAVE_CCTXT)
773 { 731 {
774 if (!prev->stack) 732 if (!prev->stack)
775 allocate_stack (prev, 0); 733 allocate_stack (prev, 0);
776 734
735 /* the new coroutine starts with start_env again */
736 PL_top_env = &PL_start_env;
737
777 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 738 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
778 { 739 {
779 PL_top_env = &next->start_env;
780
781 setup_coro (next); 740 setup_coro (next);
782 next->cursp = stacklevel; 741 next->cursp = stacklevel;
783 742
784 prev->stack->refcnt++; 743 prev->stack->refcnt++;
785 prev->stack->usecnt++; 744 prev->stack->usecnt++;
813 coro_mortal = 0; 772 coro_mortal = 0;
814 } 773 }
815 UNLOCK; 774 UNLOCK;
816} 775}
817 776
777void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
778
818#define SV_CORO(sv,func) \ 779#define SV_CORO(sv,func) \
819 do { \ 780 do { \
820 if (SvROK (sv)) \ 781 if (SvROK (sv)) \
821 sv = SvRV (sv); \ 782 sv = SvRV (sv); \
822 \ 783 \
834 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 795 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
835 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 796 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
836 \ 797 \
837 } while(0) 798 } while(0)
838 799
839#define SvSTATE(sv) (struct coro *)SvIV (sv) 800#define SvSTATE(sv) INT2PTR (struct coro *, SvIVX (sv))
840 801
841static void 802static void
842api_transfer(pTHX_ SV *prev, SV *next, int flags) 803api_transfer (pTHX_ SV *prev, SV *next, int flags)
843{ 804{
844 SV_CORO (prev, "Coro::transfer"); 805 SV_CORO (prev, "Coro::transfer");
845 SV_CORO (next, "Coro::transfer"); 806 SV_CORO (next, "Coro::transfer");
846 807
847 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 808 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
848} 809}
849 810
850/** Coro ********************************************************************/ 811/** Coro ********************************************************************/
851 812
852#define PRIO_MAX 3 813#define PRIO_MAX 3
856#define PRIO_IDLE -3 817#define PRIO_IDLE -3
857#define PRIO_MIN -4 818#define PRIO_MIN -4
858 819
859/* for Coro.pm */ 820/* for Coro.pm */
860static GV *coro_current, *coro_idle; 821static GV *coro_current, *coro_idle;
861static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 822static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
862static int coro_nready; 823static int coro_nready;
863 824
864static void 825static void
865coro_enq (pTHX_ SV *sv) 826coro_enq (pTHX_ SV *sv)
866{ 827{
828 SV **xprio;
829 int prio;
830
867 if (SvTYPE (sv) == SVt_PVHV) 831 if (SvTYPE (sv) != SVt_PVHV)
868 {
869 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
870 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
871
872 prio = prio > PRIO_MAX ? PRIO_MAX
873 : prio < PRIO_MIN ? PRIO_MIN
874 : prio;
875
876 av_push (coro_ready [prio - PRIO_MIN], sv);
877 coro_nready++;
878
879 return;
880 }
881
882 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 832 croak ("Coro::ready tried to enqueue something that is not a coroutine");
833
834 xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
835 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
836
837 prio = prio > PRIO_MAX ? PRIO_MAX
838 : prio < PRIO_MIN ? PRIO_MIN
839 : prio;
840
841 av_push (coro_ready [prio - PRIO_MIN], sv);
842 coro_nready++;
883} 843}
884 844
885static SV * 845static SV *
886coro_deq (pTHX_ int min_prio) 846coro_deq (pTHX_ int min_prio)
887{ 847{
890 min_prio -= PRIO_MIN; 850 min_prio -= PRIO_MIN;
891 if (min_prio < 0) 851 if (min_prio < 0)
892 min_prio = 0; 852 min_prio = 0;
893 853
894 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 854 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
895 if (av_len (coro_ready[prio]) >= 0) 855 if (AvFILLp (coro_ready [prio]) >= 0)
896 { 856 {
897 coro_nready--; 857 coro_nready--;
898 return av_shift (coro_ready[prio]); 858 return av_shift (coro_ready [prio]);
899 } 859 }
900 860
901 return 0; 861 return 0;
902} 862}
903 863
937 897
938 SV_CORO (next, "Coro::schedule"); 898 SV_CORO (next, "Coro::schedule");
939 899
940 UNLOCK; 900 UNLOCK;
941 901
942 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 902 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
943 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 903 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
944} 904}
945 905
946static void 906static void
947api_cede (void) 907api_cede (void)
948{ 908{
962BOOT: 922BOOT:
963{ /* {} necessary for stoopid perl-5.6.x */ 923{ /* {} necessary for stoopid perl-5.6.x */
964#ifdef USE_ITHREADS 924#ifdef USE_ITHREADS
965 MUTEX_INIT (&coro_mutex); 925 MUTEX_INIT (&coro_mutex);
966#endif 926#endif
927 BOOT_PAGESIZE;
967 928
968 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 929 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
969 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 930 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
970 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 931 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
971 932
993 Newz (0, coro, 1, struct coro); 954 Newz (0, coro, 1, struct coro);
994 955
995 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 956 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
996 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 957 /*coro->mainstack = 0; *//*actual work is done inside transfer */
997 /*coro->stack = 0;*/ 958 /*coro->stack = 0;*/
998
999 /* same as JMPENV_BOOTSTRAP */
1000 /* we might be able to recycle start_env, but safe is safe */
1001 /*Zero(&coro->start_env, 1, JMPENV);*/
1002 coro->start_env.je_ret = -1;
1003 coro->start_env.je_mustcatch = TRUE;
1004 959
1005 RETVAL = coro; 960 RETVAL = coro;
1006 OUTPUT: 961 OUTPUT:
1007 RETVAL 962 RETVAL
1008 963
1014 PROTOTYPE: @ 969 PROTOTYPE: @
1015 CODE: 970 CODE:
1016 PUTBACK; 971 PUTBACK;
1017 SV_CORO (next, "Coro::transfer"); 972 SV_CORO (next, "Coro::transfer");
1018 SV_CORO (prev, "Coro::transfer"); 973 SV_CORO (prev, "Coro::transfer");
1019 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 974 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1020 SPAGAIN; 975 SPAGAIN;
1021 976
1022void 977void
1023DESTROY(coro) 978DESTROY(coro)
1024 Coro::State coro 979 Coro::State coro
1051 PROTOTYPE: $ 1006 PROTOTYPE: $
1052 CODE: 1007 CODE:
1053 _exit (code); 1008 _exit (code);
1054 1009
1055MODULE = Coro::State PACKAGE = Coro::Cont 1010MODULE = Coro::State PACKAGE = Coro::Cont
1056
1057# this is slightly dirty (should expose a c-level api)
1058 1011
1059void 1012void
1060yield(...) 1013yield(...)
1061 PROTOTYPE: @ 1014 PROTOTYPE: @
1062 CODE: 1015 CODE:
1076 av_fill (defav, items - 1); 1029 av_fill (defav, items - 1);
1077 while (items--) 1030 while (items--)
1078 av_store (defav, items, SvREFCNT_inc (ST(items))); 1031 av_store (defav, items, SvREFCNT_inc (ST(items)));
1079 1032
1080 sv = av_pop ((AV *)SvRV (yieldstack)); 1033 sv = av_pop ((AV *)SvRV (yieldstack));
1081 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1034 prev = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1082 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1035 next = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1083 SvREFCNT_dec (sv); 1036 SvREFCNT_dec (sv);
1084 1037
1085 transfer (aTHX_ prev, next, 0); 1038 coro_state_transfer (aTHX_ prev, next, 0);
1086 1039
1087MODULE = Coro::State PACKAGE = Coro 1040MODULE = Coro::State PACKAGE = Coro
1088
1089# this is slightly dirty (should expose a c-level api)
1090 1041
1091BOOT: 1042BOOT:
1092{ 1043{
1093 int i; 1044 int i;
1094 HV *stash = gv_stashpv ("Coro", TRUE); 1045 HV *stash = gv_stashpv ("Coro", TRUE);
1114 coroapi.ready = api_ready; 1065 coroapi.ready = api_ready;
1115 coroapi.nready = &coro_nready; 1066 coroapi.nready = &coro_nready;
1116 coroapi.current = coro_current; 1067 coroapi.current = coro_current;
1117 1068
1118 GCoroAPI = &coroapi; 1069 GCoroAPI = &coroapi;
1119 sv_setiv(sv, (IV)&coroapi); 1070 sv_setiv (sv, (IV)&coroapi);
1120 SvREADONLY_on(sv); 1071 SvREADONLY_on (sv);
1121 } 1072 }
1122} 1073}
1123 1074
1124#if !PERL_MICRO 1075#if !PERL_MICRO
1125 1076
1150cede(...) 1101cede(...)
1151 PROTOTYPE: 1102 PROTOTYPE:
1152 CODE: 1103 CODE:
1153 api_cede (); 1104 api_cede ();
1154 1105
1106# and these are hacks
1107SV *
1108_aio_get_state ()
1109 CODE:
1110{
1111 struct {
1112 int errorno;
1113 int laststype;
1114 int laststatval;
1115 Stat_t statcache;
1116 } data;
1117
1118 data.errorno = errno;
1119 data.laststype = PL_laststype;
1120 data.laststatval = PL_laststatval;
1121 data.statcache = PL_statcache;
1122
1123 RETVAL = newSVpvn ((char *)&data, sizeof data);
1124}
1125 OUTPUT:
1126 RETVAL
1127
1128void
1129_aio_set_state (char *data_)
1130 PROTOTYPE: $
1131 CODE:
1132{
1133 struct {
1134 int errorno;
1135 int laststype;
1136 int laststatval;
1137 Stat_t statcache;
1138 } *data = (void *)data_;
1139
1140 errno = data->errorno;
1141 PL_laststype = data->laststype;
1142 PL_laststatval = data->laststatval;
1143 PL_statcache = data->statcache;
1144}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines