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.81 by root, Fri Nov 3 00:56:03 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 672static void
718transfer (pTHX_ struct coro *prev, struct coro *next, int flags) 673transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
719{ 674{
720 dSTACKLEVEL; 675 dSTACKLEVEL;
721 676
722 if (prev != next) 677 if (prev != next)
723 { 678 {
679 /* has this coro been created yet? */
724 if (next->mainstack) 680 if (next->mainstack)
725 { 681 {
726 LOCK; 682 LOCK;
727 SAVE (prev, flags); 683 SAVE (prev, flags);
728 LOAD (next); 684 LOAD (next);
767 { 723 {
768 LOCK; 724 LOCK;
769 SAVE (prev, -1); /* first get rid of the old state */ 725 SAVE (prev, -1); /* first get rid of the old state */
770 UNLOCK; 726 UNLOCK;
771 727
728 /* create the coroutine for the first time */
772 if (flags & TRANSFER_SAVE_CCTXT) 729 if (flags & TRANSFER_SAVE_CCTXT)
773 { 730 {
774 if (!prev->stack) 731 if (!prev->stack)
775 allocate_stack (prev, 0); 732 allocate_stack (prev, 0);
776 733
734 /* the new coroutine starts with start_env again */
735 PL_top_env = &PL_start_env;
736
777 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 737 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
778 { 738 {
779 PL_top_env = &next->start_env;
780
781 setup_coro (next); 739 setup_coro (next);
782 next->cursp = stacklevel; 740 next->cursp = stacklevel;
783 741
784 prev->stack->refcnt++; 742 prev->stack->refcnt++;
785 prev->stack->usecnt++; 743 prev->stack->usecnt++;
834 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 792 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
835 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); \
836 \ 794 \
837 } while(0) 795 } while(0)
838 796
839#define SvSTATE(sv) (struct coro *)SvIV (sv) 797#define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv))
840 798
841static void 799static void
842api_transfer(pTHX_ SV *prev, SV *next, int flags) 800api_transfer(pTHX_ SV *prev, SV *next, int flags)
843{ 801{
844 SV_CORO (prev, "Coro::transfer"); 802 SV_CORO (prev, "Coro::transfer");
862static int coro_nready; 820static int coro_nready;
863 821
864static void 822static void
865coro_enq (pTHX_ SV *sv) 823coro_enq (pTHX_ SV *sv)
866{ 824{
825 SV **xprio;
826 int prio;
827
867 if (SvTYPE (sv) == SVt_PVHV) 828 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"); 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++;
883} 840}
884 841
885static SV * 842static SV *
886coro_deq (pTHX_ int min_prio) 843coro_deq (pTHX_ int min_prio)
887{ 844{
962BOOT: 919BOOT:
963{ /* {} necessary for stoopid perl-5.6.x */ 920{ /* {} necessary for stoopid perl-5.6.x */
964#ifdef USE_ITHREADS 921#ifdef USE_ITHREADS
965 MUTEX_INIT (&coro_mutex); 922 MUTEX_INIT (&coro_mutex);
966#endif 923#endif
924 BOOT_PAGESIZE;
967 925
968 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 926 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
969 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 927 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
970 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 928 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
971 929
993 Newz (0, coro, 1, struct coro); 951 Newz (0, coro, 1, struct coro);
994 952
995 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 953 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
996 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 954 /*coro->mainstack = 0; *//*actual work is done inside transfer */
997 /*coro->stack = 0;*/ 955 /*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 956
1005 RETVAL = coro; 957 RETVAL = coro;
1006 OUTPUT: 958 OUTPUT:
1007 RETVAL 959 RETVAL
1008 960
1052 CODE: 1004 CODE:
1053 _exit (code); 1005 _exit (code);
1054 1006
1055MODULE = Coro::State PACKAGE = Coro::Cont 1007MODULE = Coro::State PACKAGE = Coro::Cont
1056 1008
1057# this is slightly dirty (should expose a c-level api)
1058
1059void 1009void
1060yield(...) 1010yield(...)
1061 PROTOTYPE: @ 1011 PROTOTYPE: @
1062 CODE: 1012 CODE:
1063 SV *yieldstack; 1013 SV *yieldstack;
1076 av_fill (defav, items - 1); 1026 av_fill (defav, items - 1);
1077 while (items--) 1027 while (items--)
1078 av_store (defav, items, SvREFCNT_inc (ST(items))); 1028 av_store (defav, items, SvREFCNT_inc (ST(items)));
1079 1029
1080 sv = av_pop ((AV *)SvRV (yieldstack)); 1030 sv = av_pop ((AV *)SvRV (yieldstack));
1081 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1031 prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))));
1082 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1032 next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))));
1083 SvREFCNT_dec (sv); 1033 SvREFCNT_dec (sv);
1084 1034
1085 transfer (aTHX_ prev, next, 0); 1035 transfer (aTHX_ prev, next, 0);
1086 1036
1087MODULE = Coro::State PACKAGE = Coro 1037MODULE = Coro::State PACKAGE = Coro
1088
1089# this is slightly dirty (should expose a c-level api)
1090 1038
1091BOOT: 1039BOOT:
1092{ 1040{
1093 int i; 1041 int i;
1094 HV *stash = gv_stashpv ("Coro", TRUE); 1042 HV *stash = gv_stashpv ("Coro", TRUE);
1114 coroapi.ready = api_ready; 1062 coroapi.ready = api_ready;
1115 coroapi.nready = &coro_nready; 1063 coroapi.nready = &coro_nready;
1116 coroapi.current = coro_current; 1064 coroapi.current = coro_current;
1117 1065
1118 GCoroAPI = &coroapi; 1066 GCoroAPI = &coroapi;
1119 sv_setiv(sv, (IV)&coroapi); 1067 sv_setiv (sv, (IV)&coroapi);
1120 SvREADONLY_on(sv); 1068 SvREADONLY_on (sv);
1121 } 1069 }
1122} 1070}
1123 1071
1124#if !PERL_MICRO 1072#if !PERL_MICRO
1125 1073
1150cede(...) 1098cede(...)
1151 PROTOTYPE: 1099 PROTOTYPE:
1152 CODE: 1100 CODE:
1153 api_cede (); 1101 api_cede ();
1154 1102
1103# and these are hacks
1104SV *
1105_aio_get_state ()
1106 CODE:
1107{
1108 struct {
1109 int errorno;
1110 int laststype;
1111 int laststatval;
1112 Stat_t statcache;
1113 } data;
1114
1115 data.errorno = errno;
1116 data.laststype = PL_laststype;
1117 data.laststatval = PL_laststatval;
1118 data.statcache = PL_statcache;
1119
1120 RETVAL = newSVpvn ((char *)&data, sizeof data);
1121}
1122 OUTPUT:
1123 RETVAL
1124
1125void
1126_aio_set_state (char *data_)
1127 PROTOTYPE: $
1128 CODE:
1129{
1130 struct {
1131 int errorno;
1132 int laststype;
1133 int laststatval;
1134 Stat_t statcache;
1135 } *data = (void *)data_;
1136
1137 errno = data->errorno;
1138 PL_laststype = data->laststype;
1139 PL_laststatval = data->laststatval;
1140 PL_statcache = data->statcache;
1141}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines