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.58 by pcg, Fri May 14 13:25:09 2004 UTC vs.
Revision 1.78 by root, Wed Nov 1 01:21:21 2006 UTC

1#define PERL_NO_GET_CONTEXT 1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c"
2 4
3#include "EXTERN.h" 5#include "EXTERN.h"
4#include "perl.h" 6#include "perl.h"
5#include "XSUB.h" 7#include "XSUB.h"
6 8
7#include "patchlevel.h" 9#include "patchlevel.h"
8 10
9#if PATCHLEVEL < 6 11#if PERL_VERSION < 6
10# ifndef PL_ppaddr 12# ifndef PL_ppaddr
11# define PL_ppaddr ppaddr 13# define PL_ppaddr ppaddr
12# endif 14# endif
13# ifndef call_sv 15# ifndef call_sv
14# define call_sv perl_call_sv 16# define call_sv perl_call_sv
25# ifndef IS_PADCONST 27# ifndef IS_PADCONST
26# define IS_PADCONST(v) 0 28# define IS_PADCONST(v) 0
27# endif 29# endif
28#endif 30#endif
29 31
30#include "libcoro/coro.c"
31
32#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
33 41
34#ifdef HAVE_MMAP 42#ifdef HAVE_MMAP
35# include <unistd.h> 43# include <unistd.h>
36# include <sys/mman.h> 44# include <sys/mman.h>
37# ifndef MAP_ANONYMOUS 45# ifndef MAP_ANONYMOUS
39# define MAP_ANONYMOUS MAP_ANON 47# define MAP_ANONYMOUS MAP_ANON
40# else 48# else
41# undef HAVE_MMAP 49# undef HAVE_MMAP
42# endif 50# endif
43# 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
44#endif 60#endif
45 61
46#define SUB_INIT "Coro::State::initialize" 62#define SUB_INIT "Coro::State::initialize"
47#define UCORO_STATE "_coro_state" 63#define UCORO_STATE "_coro_state"
48 64
57 73
58#include "CoroAPI.h" 74#include "CoroAPI.h"
59 75
60#ifdef USE_ITHREADS 76#ifdef USE_ITHREADS
61static perl_mutex coro_mutex; 77static perl_mutex coro_mutex;
62# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 78# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
63# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 79# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
64#else 80#else
65# define LOCK 0 81# define LOCK (void)0
66# define UNLOCK 0 82# define UNLOCK (void)0
67#endif 83#endif
68 84
69static struct CoroAPI coroapi; 85static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */ 86static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash; 87static HV *coro_state_stash;
109 AV *mainstack; 125 AV *mainstack;
110 SV **stack_sp; 126 SV **stack_sp;
111 OP *op; 127 OP *op;
112 SV **curpad; 128 SV **curpad;
113 AV *comppad; 129 AV *comppad;
130 CV *compcv;
114 SV **stack_base; 131 SV **stack_base;
115 SV **stack_max; 132 SV **stack_max;
116 SV **tmps_stack; 133 SV **tmps_stack;
117 I32 tmps_floor; 134 I32 tmps_floor;
118 I32 tmps_ix; 135 I32 tmps_ix;
127 I32 savestack_ix; 144 I32 savestack_ix;
128 I32 savestack_max; 145 I32 savestack_max;
129 OP **retstack; 146 OP **retstack;
130 I32 retstack_ix; 147 I32 retstack_ix;
131 I32 retstack_max; 148 I32 retstack_max;
149 PMOP *curpm;
132 COP *curcop; 150 COP *curcop;
133 JMPENV *top_env; 151 JMPENV *top_env;
134 152
135 /* data associated with this coroutine (initial args) */ 153 /* data associated with this coroutine (initial args) */
136 AV *args; 154 AV *args;
137}; 155};
138 156
139typedef struct coro *Coro__State; 157typedef struct coro *Coro__State;
140typedef struct coro *Coro__State_or_hashref; 158typedef struct coro *Coro__State_or_hashref;
141 159
142/* mostly copied from op.c:cv_clone2 */ 160static AV *
143STATIC AV * 161coro_clone_padlist (pTHX_ CV *cv)
144clone_padlist (pTHX_ AV *protopadlist)
145{ 162{
146 AV *av; 163 AV *padlist = CvPADLIST (cv);
147 I32 ix;
148 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
149 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
150 SV **pname = AvARRAY (protopad_name);
151 SV **ppad = AvARRAY (protopad);
152 I32 fname = AvFILLp (protopad_name);
153 I32 fpad = AvFILLp (protopad);
154 AV *newpadlist, *newpad_name, *newpad; 164 AV *newpadlist, *newpad;
155 SV **npad;
156
157 newpad_name = newAV ();
158 for (ix = fname; ix >= 0; ix--)
159 av_store (newpad_name, ix, SvREFCNT_inc (pname[ix]));
160
161 newpad = newAV ();
162 av_fill (newpad, AvFILLp (protopad));
163 npad = AvARRAY (newpad);
164 165
165 newpadlist = newAV (); 166 newpadlist = newAV ();
166 AvREAL_off (newpadlist); 167 AvREAL_off (newpadlist);
167 av_store (newpadlist, 0, (SV *) newpad_name); 168 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
169 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
170 --AvFILLp (padlist);
171
172 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
168 av_store (newpadlist, 1, (SV *) newpad); 173 av_store (newpadlist, 1, (SV *)newpad);
169
170 av = newAV (); /* will be @_ */
171 av_extend (av, 0);
172 av_store (newpad, 0, (SV *) av);
173 AvFLAGS (av) = AVf_REIFY;
174
175 for (ix = fpad; ix > 0; ix--)
176 {
177 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
178
179 if (namesv && namesv != &PL_sv_undef)
180 {
181 char *name = SvPVX (namesv); /* XXX */
182
183 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
184 { /* lexical from outside? */
185 npad[ix] = SvREFCNT_inc (ppad[ix]);
186 }
187 else
188 { /* our own lexical */
189 SV *sv;
190 if (*name == '&')
191 sv = SvREFCNT_inc (ppad[ix]);
192 else if (*name == '@')
193 sv = (SV *) newAV ();
194 else if (*name == '%')
195 sv = (SV *) newHV ();
196 else
197 sv = NEWSV (0, 0);
198
199#ifdef SvPADBUSY
200 if (!SvPADBUSY (sv))
201#endif
202 SvPADMY_on (sv);
203
204 npad[ix] = sv;
205 }
206 }
207 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
208 {
209 npad[ix] = SvREFCNT_inc (ppad[ix]);
210 }
211 else
212 {
213 SV *sv = NEWSV (0, 0);
214 SvPADTMP_on (sv);
215 npad[ix] = sv;
216 }
217 }
218
219#if 0 /* return -ENOTUNDERSTOOD */
220 /* Now that vars are all in place, clone nested closures. */
221
222 for (ix = fpad; ix > 0; ix--) {
223 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
224 if (namesv
225 && namesv != &PL_sv_undef
226 && !(SvFLAGS(namesv) & SVf_FAKE)
227 && *SvPVX(namesv) == '&'
228 && CvCLONE(ppad[ix]))
229 {
230 CV *kid = cv_clone((CV*)ppad[ix]);
231 SvREFCNT_dec(ppad[ix]);
232 CvCLONE_on(kid);
233 SvPADMY_on(kid);
234 npad[ix] = (SV*)kid;
235 }
236 }
237#endif
238 174
239 return newpadlist; 175 return newpadlist;
240} 176}
241 177
242STATIC void 178static void
243free_padlist (pTHX_ AV *padlist) 179free_padlist (pTHX_ AV *padlist)
244{ 180{
245 /* may be during global destruction */ 181 /* may be during global destruction */
246 if (SvREFCNT (padlist)) 182 if (SvREFCNT (padlist))
247 { 183 {
261 197
262 SvREFCNT_dec ((SV*)padlist); 198 SvREFCNT_dec ((SV*)padlist);
263 } 199 }
264} 200}
265 201
266STATIC int 202static int
267coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 203coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
268{ 204{
269 AV *padlist; 205 AV *padlist;
270 AV *av = (AV *)mg->mg_obj; 206 AV *av = (AV *)mg->mg_obj;
271 207
272 /* casting is fun. */ 208 /* casting is fun. */
273 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 209 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
274 free_padlist (aTHX_ padlist); 210 free_padlist (aTHX_ padlist);
211
212 SvREFCNT_dec (av);
213
214 return 0;
275} 215}
276 216
277#define PERL_MAGIC_coro PERL_MAGIC_ext 217#define PERL_MAGIC_coro PERL_MAGIC_ext
278 218
279static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 219static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
280 220
281/* the next two functions merely cache the padlists */ 221/* the next two functions merely cache the padlists */
282STATIC void 222static void
283get_padlist (pTHX_ CV *cv) 223get_padlist (pTHX_ CV *cv)
284{ 224{
285 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 225 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
286 226
287 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 227 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
288 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 228 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
289 else 229 else
230 {
231#if 0
232 /* this should work - but it doesn't :( */
233 CV *cp = Perl_cv_clone (aTHX_ cv);
234 CvPADLIST (cv) = CvPADLIST (cp);
235 CvPADLIST (cp) = 0;
236 SvREFCNT_dec (cp);
237#else
290 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); 238 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
239#endif
240 }
291} 241}
292 242
293STATIC void 243static void
294put_padlist (pTHX_ CV *cv) 244put_padlist (pTHX_ CV *cv)
295{ 245{
296 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 246 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
297 247
298 if (!mg) 248 if (!mg)
325 PL_mainstack = c->mainstack; 275 PL_mainstack = c->mainstack;
326 PL_stack_sp = c->stack_sp; 276 PL_stack_sp = c->stack_sp;
327 PL_op = c->op; 277 PL_op = c->op;
328 PL_curpad = c->curpad; 278 PL_curpad = c->curpad;
329 PL_comppad = c->comppad; 279 PL_comppad = c->comppad;
280 PL_compcv = c->compcv;
330 PL_stack_base = c->stack_base; 281 PL_stack_base = c->stack_base;
331 PL_stack_max = c->stack_max; 282 PL_stack_max = c->stack_max;
332 PL_tmps_stack = c->tmps_stack; 283 PL_tmps_stack = c->tmps_stack;
333 PL_tmps_floor = c->tmps_floor; 284 PL_tmps_floor = c->tmps_floor;
334 PL_tmps_ix = c->tmps_ix; 285 PL_tmps_ix = c->tmps_ix;
340 PL_scopestack_ix = c->scopestack_ix; 291 PL_scopestack_ix = c->scopestack_ix;
341 PL_scopestack_max = c->scopestack_max; 292 PL_scopestack_max = c->scopestack_max;
342 PL_savestack = c->savestack; 293 PL_savestack = c->savestack;
343 PL_savestack_ix = c->savestack_ix; 294 PL_savestack_ix = c->savestack_ix;
344 PL_savestack_max = c->savestack_max; 295 PL_savestack_max = c->savestack_max;
296#if PERL_VERSION < 9
345 PL_retstack = c->retstack; 297 PL_retstack = c->retstack;
346 PL_retstack_ix = c->retstack_ix; 298 PL_retstack_ix = c->retstack_ix;
347 PL_retstack_max = c->retstack_max; 299 PL_retstack_max = c->retstack_max;
300#endif
301 PL_curpm = c->curpm;
348 PL_curcop = c->curcop; 302 PL_curcop = c->curcop;
349 PL_top_env = c->top_env; 303 PL_top_env = c->top_env;
350 304
351 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 305 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
352 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 306 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
411 } 365 }
412 366
413 PUSHs ((SV *)CvPADLIST(cv)); 367 PUSHs ((SV *)CvPADLIST(cv));
414 PUSHs ((SV *)cv); 368 PUSHs ((SV *)cv);
415 369
416 get_padlist (aTHX_ cv); /* this is a monster */ 370 get_padlist (aTHX_ cv);
417 } 371 }
418 } 372 }
419#ifdef CXt_FORMAT 373#ifdef CXt_FORMAT
420 else if (CxTYPE(cx) == CXt_FORMAT) 374 else if (CxTYPE(cx) == CXt_FORMAT)
421 { 375 {
449 c->mainstack = PL_mainstack; 403 c->mainstack = PL_mainstack;
450 c->stack_sp = PL_stack_sp; 404 c->stack_sp = PL_stack_sp;
451 c->op = PL_op; 405 c->op = PL_op;
452 c->curpad = PL_curpad; 406 c->curpad = PL_curpad;
453 c->comppad = PL_comppad; 407 c->comppad = PL_comppad;
408 c->compcv = PL_compcv;
454 c->stack_base = PL_stack_base; 409 c->stack_base = PL_stack_base;
455 c->stack_max = PL_stack_max; 410 c->stack_max = PL_stack_max;
456 c->tmps_stack = PL_tmps_stack; 411 c->tmps_stack = PL_tmps_stack;
457 c->tmps_floor = PL_tmps_floor; 412 c->tmps_floor = PL_tmps_floor;
458 c->tmps_ix = PL_tmps_ix; 413 c->tmps_ix = PL_tmps_ix;
464 c->scopestack_ix = PL_scopestack_ix; 419 c->scopestack_ix = PL_scopestack_ix;
465 c->scopestack_max = PL_scopestack_max; 420 c->scopestack_max = PL_scopestack_max;
466 c->savestack = PL_savestack; 421 c->savestack = PL_savestack;
467 c->savestack_ix = PL_savestack_ix; 422 c->savestack_ix = PL_savestack_ix;
468 c->savestack_max = PL_savestack_max; 423 c->savestack_max = PL_savestack_max;
424#if PERL_VERSION < 9
469 c->retstack = PL_retstack; 425 c->retstack = PL_retstack;
470 c->retstack_ix = PL_retstack_ix; 426 c->retstack_ix = PL_retstack_ix;
471 c->retstack_max = PL_retstack_max; 427 c->retstack_max = PL_retstack_max;
428#endif
429 c->curpm = PL_curpm;
472 c->curcop = PL_curcop; 430 c->curcop = PL_curcop;
473 c->top_env = PL_top_env; 431 c->top_env = PL_top_env;
474} 432}
475 433
476/* 434/*
477 * allocate various perl stacks. This is an exact copy 435 * allocate various perl stacks. This is an exact copy
478 * of perl.c:init_stacks, except that it uses less memory 436 * of perl.c:init_stacks, except that it uses less memory
479 * on the (sometimes correct) assumption that coroutines do 437 * on the (sometimes correct) assumption that coroutines do
480 * not usually need a lot of stackspace. 438 * not usually need a lot of stackspace.
481 */ 439 */
482STATIC void 440static void
483coro_init_stacks (pTHX) 441coro_init_stacks (pTHX)
484{ 442{
485 LOCK; 443 LOCK;
486 444
487 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 445 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
512 470
513 New(54,PL_savestack,96,ANY); 471 New(54,PL_savestack,96,ANY);
514 PL_savestack_ix = 0; 472 PL_savestack_ix = 0;
515 PL_savestack_max = 96; 473 PL_savestack_max = 96;
516 474
475#if PERL_VERSION < 9
517 New(54,PL_retstack,8,OP*); 476 New(54,PL_retstack,8,OP*);
518 PL_retstack_ix = 0; 477 PL_retstack_ix = 0;
519 PL_retstack_max = 8; 478 PL_retstack_max = 8;
479#endif
520 480
521 UNLOCK; 481 UNLOCK;
522} 482}
523 483
524/* 484/*
525 * destroy the stacks, the callchain etc... 485 * destroy the stacks, the callchain etc...
526 */ 486 */
527STATIC void 487static void
528destroy_stacks(pTHX) 488destroy_stacks(pTHX)
529{ 489{
530 if (!IN_DESTRUCT) 490 if (!IN_DESTRUCT)
531 { 491 {
532 /* is this ugly, I ask? */ 492 /* is this ugly, I ask? */
564 524
565 Safefree (PL_tmps_stack); 525 Safefree (PL_tmps_stack);
566 Safefree (PL_markstack); 526 Safefree (PL_markstack);
567 Safefree (PL_scopestack); 527 Safefree (PL_scopestack);
568 Safefree (PL_savestack); 528 Safefree (PL_savestack);
529#if PERL_VERSION < 9
569 Safefree (PL_retstack); 530 Safefree (PL_retstack);
531#endif
570} 532}
571 533
572static void 534static void
573allocate_stack (Coro__State ctx, int alloc) 535allocate_stack (Coro__State ctx, int alloc)
574{ 536{
581 stack->gencnt = ctx->gencnt = 0; 543 stack->gencnt = ctx->gencnt = 0;
582 544
583 if (alloc) 545 if (alloc)
584 { 546 {
585#if HAVE_MMAP 547#if HAVE_MMAP
586 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ 548 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
587 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 549 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
588 if (stack->sptr == (void *)-1) 550 if (stack->sptr != (void *)-1)
589#endif
590 { 551 {
591 /*FIXME*//*D*//* reasonable stack size! */ 552# if STACKGUARD
553 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
554# endif
555 }
556 else
557#endif
558 {
592 stack->ssize = - (8192 * sizeof (long)); 559 stack->ssize = - (STACKSIZE * (long)sizeof (long));
593 New (0, stack->sptr, 8192, long); 560 New (0, stack->sptr, STACKSIZE, long);
594 } 561 }
595 } 562 }
596 else 563 else
597 stack->sptr = 0; 564 stack->sptr = 0;
598 565
689 /* 656 /*
690 * this is a _very_ stripped down perl interpreter ;) 657 * this is a _very_ stripped down perl interpreter ;)
691 */ 658 */
692 dTHX; 659 dTHX;
693 Coro__State ctx = (Coro__State)arg; 660 Coro__State ctx = (Coro__State)arg;
694 JMPENV coro_start_env;
695 661
696 PL_top_env = &ctx->start_env; 662 PL_top_env = &ctx->start_env;
697 663
698 ctx->cursp = 0; 664 ctx->cursp = 0;
699 PL_op = PL_op->op_next; 665 PL_op = PL_op->op_next;
700 CALLRUNOPS(aTHX); 666 CALLRUNOPS(aTHX);
701 667
702 abort (); 668 abort ();
703} 669}
704 670
705STATIC void 671static void
706transfer (pTHX_ struct coro *prev, struct coro *next, int flags) 672transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
707{ 673{
708 dSTACKLEVEL; 674 dSTACKLEVEL;
709 675
710 if (prev != next) 676 if (prev != next)
822 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 788 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
823 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 789 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
824 \ 790 \
825 } while(0) 791 } while(0)
826 792
827#define SvSTATE(sv) (struct coro *)SvIV (sv) 793#define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv))
828 794
829static void 795static void
830api_transfer(pTHX_ SV *prev, SV *next, int flags) 796api_transfer(pTHX_ SV *prev, SV *next, int flags)
831{ 797{
832 SV_CORO (prev, "Coro::transfer"); 798 SV_CORO (prev, "Coro::transfer");
850static int coro_nready; 816static int coro_nready;
851 817
852static void 818static void
853coro_enq (pTHX_ SV *sv) 819coro_enq (pTHX_ SV *sv)
854{ 820{
821 SV **xprio;
822 int prio;
823
855 if (SvTYPE (sv) == SVt_PVHV) 824 if (SvTYPE (sv) != SVt_PVHV)
856 {
857 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
858 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
859
860 prio = prio > PRIO_MAX ? PRIO_MAX
861 : prio < PRIO_MIN ? PRIO_MIN
862 : prio;
863
864 av_push (coro_ready [prio - PRIO_MIN], sv);
865 coro_nready++;
866
867 return;
868 }
869
870 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 825 croak ("Coro::ready tried to enqueue something that is not a coroutine");
826
827 xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
828 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
829
830 prio = prio > PRIO_MAX ? PRIO_MAX
831 : prio < PRIO_MIN ? PRIO_MIN
832 : prio;
833
834 av_push (coro_ready [prio - PRIO_MIN], sv);
835 coro_nready++;
871} 836}
872 837
873static SV * 838static SV *
874coro_deq (pTHX_ int min_prio) 839coro_deq (pTHX_ int min_prio)
875{ 840{
950BOOT: 915BOOT:
951{ /* {} necessary for stoopid perl-5.6.x */ 916{ /* {} necessary for stoopid perl-5.6.x */
952#ifdef USE_ITHREADS 917#ifdef USE_ITHREADS
953 MUTEX_INIT (&coro_mutex); 918 MUTEX_INIT (&coro_mutex);
954#endif 919#endif
920 BOOT_PAGESIZE;
955 921
956 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 922 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
957 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 923 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
958 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 924 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
959 925
979 croak ("Coro::State::_newprocess expects an arrayref"); 945 croak ("Coro::State::_newprocess expects an arrayref");
980 946
981 Newz (0, coro, 1, struct coro); 947 Newz (0, coro, 1, struct coro);
982 948
983 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 949 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
984 coro->mainstack = 0; /* actual work is done inside transfer */ 950 /*coro->mainstack = 0; *//*actual work is done inside transfer */
985 coro->stack = 0; 951 /*coro->stack = 0;*/
986 952
987 /* same as JMPENV_BOOTSTRAP */ 953 /* same as JMPENV_BOOTSTRAP */
988 /* we might be able to recycle start_env, but safe is safe */ 954 /* we might be able to recycle start_env, but safe is safe */
989 //Zero(&coro->start_env, 1, JMPENV); 955 /*Zero(&coro->start_env, 1, JMPENV);*/
990 coro->start_env.je_ret = -1; 956 coro->start_env.je_ret = -1;
991 coro->start_env.je_mustcatch = TRUE; 957 coro->start_env.je_mustcatch = TRUE;
992 958
993 RETVAL = coro; 959 RETVAL = coro;
994 OUTPUT: 960 OUTPUT:
1015 if (coro->mainstack && coro->mainstack != main_mainstack) 981 if (coro->mainstack && coro->mainstack != main_mainstack)
1016 { 982 {
1017 struct coro temp; 983 struct coro temp;
1018 984
1019 PUTBACK; 985 PUTBACK;
1020 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 986 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
1021 LOAD(aTHX_ coro); 987 LOAD (aTHX_ coro);
1022 SPAGAIN; 988 SPAGAIN;
1023 989
1024 destroy_stacks (aTHX); 990 destroy_stacks (aTHX);
1025 991
1026 LOAD((&temp)); /* this will get rid of defsv etc.. */ 992 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1027 SPAGAIN; 993 SPAGAIN;
1028 994
1029 coro->mainstack = 0; 995 coro->mainstack = 0;
1030 } 996 }
1031 997
1046 1012
1047void 1013void
1048yield(...) 1014yield(...)
1049 PROTOTYPE: @ 1015 PROTOTYPE: @
1050 CODE: 1016 CODE:
1051 static SV *returnstk; 1017 SV *yieldstack;
1052 SV *sv; 1018 SV *sv;
1053 AV *defav = GvAV (PL_defgv); 1019 AV *defav = GvAV (PL_defgv);
1054 struct coro *prev, *next; 1020 struct coro *prev, *next;
1055 1021
1056 if (!returnstk) 1022 yieldstack = *hv_fetch (
1057 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1023 (HV *)SvRV (GvSV (coro_current)),
1024 "yieldstack", sizeof ("yieldstack") - 1,
1025 0
1026 );
1058 1027
1059 /* set up @_ -- ugly */ 1028 /* set up @_ -- ugly */
1060 av_clear (defav); 1029 av_clear (defav);
1061 av_fill (defav, items - 1); 1030 av_fill (defav, items - 1);
1062 while (items--) 1031 while (items--)
1063 av_store (defav, items, SvREFCNT_inc (ST(items))); 1032 av_store (defav, items, SvREFCNT_inc (ST(items)));
1064 1033
1065 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1066 sv = av_pop ((AV *)SvRV (returnstk)); 1034 sv = av_pop ((AV *)SvRV (yieldstack));
1067 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1035 prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))));
1068 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1036 next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))));
1069 SvREFCNT_dec (sv); 1037 SvREFCNT_dec (sv);
1070 1038
1071 transfer(aTHX_ prev, next, 0); 1039 transfer (aTHX_ prev, next, 0);
1072 1040
1073MODULE = Coro::State PACKAGE = Coro 1041MODULE = Coro::State PACKAGE = Coro
1074 1042
1075# this is slightly dirty (should expose a c-level api) 1043# this is slightly dirty (should expose a c-level api)
1076 1044
1136cede(...) 1104cede(...)
1137 PROTOTYPE: 1105 PROTOTYPE:
1138 CODE: 1106 CODE:
1139 api_cede (); 1107 api_cede ();
1140 1108
1109# and these are hacks
1110SV *
1111_aio_get_state ()
1112 CODE:
1113{
1114 struct {
1115 int errorno;
1116 int laststype;
1117 int laststatval;
1118 Stat_t statcache;
1119 } data;
1120
1121 data.errorno = errno;
1122 data.laststype = PL_laststype;
1123 data.laststatval = PL_laststatval;
1124 data.statcache = PL_statcache;
1125
1126 RETVAL = newSVpvn ((char *)&data, sizeof data);
1127}
1128 OUTPUT:
1129 RETVAL
1130
1131void
1132_aio_set_state (char *data_)
1133 PROTOTYPE: $
1134 CODE:
1135{
1136 struct {
1137 int errorno;
1138 int laststype;
1139 int laststatval;
1140 Stat_t statcache;
1141 } *data = (void *)data_;
1142
1143 errno = data->errorno;
1144 PL_laststype = data->laststype;
1145 PL_laststatval = data->laststatval;
1146 PL_statcache = data->statcache;
1147}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines