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.85 by root, Fri Nov 24 00:32:04 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;
84 void *sptr; 100 void *sptr;
85 long ssize; /* positive == mmap, otherwise malloc */ 101 long ssize; /* positive == mmap, otherwise malloc */
86} coro_stack; 102} coro_stack;
87 103
88struct coro { 104struct coro {
89 /* the top-level JMPENV for each coroutine, needed to catch dies. */
90 JMPENV start_env;
91
92 /* the optional C context */ 105 /* the optional C context */
93 coro_stack *stack; 106 coro_stack *stack;
94 void *cursp; 107 void *cursp;
95 int gencnt; 108 int gencnt;
96 109
109 AV *mainstack; 122 AV *mainstack;
110 SV **stack_sp; 123 SV **stack_sp;
111 OP *op; 124 OP *op;
112 SV **curpad; 125 SV **curpad;
113 AV *comppad; 126 AV *comppad;
127 CV *compcv;
114 SV **stack_base; 128 SV **stack_base;
115 SV **stack_max; 129 SV **stack_max;
116 SV **tmps_stack; 130 SV **tmps_stack;
117 I32 tmps_floor; 131 I32 tmps_floor;
118 I32 tmps_ix; 132 I32 tmps_ix;
127 I32 savestack_ix; 141 I32 savestack_ix;
128 I32 savestack_max; 142 I32 savestack_max;
129 OP **retstack; 143 OP **retstack;
130 I32 retstack_ix; 144 I32 retstack_ix;
131 I32 retstack_max; 145 I32 retstack_max;
146 PMOP *curpm;
132 COP *curcop; 147 COP *curcop;
133 JMPENV *top_env; 148 JMPENV *top_env;
134 149
135 /* data associated with this coroutine (initial args) */ 150 /* data associated with this coroutine (initial args) */
136 AV *args; 151 AV *args;
137}; 152};
138 153
139typedef struct coro *Coro__State; 154typedef struct coro *Coro__State;
140typedef struct coro *Coro__State_or_hashref; 155typedef struct coro *Coro__State_or_hashref;
141 156
142/* mostly copied from op.c:cv_clone2 */ 157static AV *
143STATIC AV * 158coro_clone_padlist (pTHX_ CV *cv)
144clone_padlist (pTHX_ AV *protopadlist)
145{ 159{
146 AV *av; 160 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; 161 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 162
165 newpadlist = newAV (); 163 newpadlist = newAV ();
166 AvREAL_off (newpadlist); 164 AvREAL_off (newpadlist);
167 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)));
168 av_store (newpadlist, 1, (SV *) newpad); 174 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 175
239 return newpadlist; 176 return newpadlist;
240} 177}
241 178
242STATIC void 179static void
243free_padlist (pTHX_ AV *padlist) 180free_padlist (pTHX_ AV *padlist)
244{ 181{
245 /* may be during global destruction */ 182 /* may be during global destruction */
246 if (SvREFCNT (padlist)) 183 if (SvREFCNT (padlist))
247 { 184 {
261 198
262 SvREFCNT_dec ((SV*)padlist); 199 SvREFCNT_dec ((SV*)padlist);
263 } 200 }
264} 201}
265 202
266STATIC int 203static int
267coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 204coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
268{ 205{
269 AV *padlist; 206 AV *padlist;
270 AV *av = (AV *)mg->mg_obj; 207 AV *av = (AV *)mg->mg_obj;
271 208
272 /* casting is fun. */ 209 /* casting is fun. */
273 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 210 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
274 free_padlist (aTHX_ padlist); 211 free_padlist (aTHX_ padlist);
212
213 SvREFCNT_dec (av);
214
215 return 0;
275} 216}
276 217
277#define PERL_MAGIC_coro PERL_MAGIC_ext 218#define PERL_MAGIC_coro PERL_MAGIC_ext
278 219
279static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 220static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
280 221
281/* the next two functions merely cache the padlists */ 222/* the next two functions merely cache the padlists */
282STATIC void 223static void
283get_padlist (pTHX_ CV *cv) 224get_padlist (pTHX_ CV *cv)
284{ 225{
285 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 226 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
286 227
287 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 228 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
288 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 229 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
289 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
290 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); 239 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
240#endif
241 }
291} 242}
292 243
293STATIC void 244static void
294put_padlist (pTHX_ CV *cv) 245put_padlist (pTHX_ CV *cv)
295{ 246{
296 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 247 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
297 248
298 if (!mg) 249 if (!mg)
325 PL_mainstack = c->mainstack; 276 PL_mainstack = c->mainstack;
326 PL_stack_sp = c->stack_sp; 277 PL_stack_sp = c->stack_sp;
327 PL_op = c->op; 278 PL_op = c->op;
328 PL_curpad = c->curpad; 279 PL_curpad = c->curpad;
329 PL_comppad = c->comppad; 280 PL_comppad = c->comppad;
281 PL_compcv = c->compcv;
330 PL_stack_base = c->stack_base; 282 PL_stack_base = c->stack_base;
331 PL_stack_max = c->stack_max; 283 PL_stack_max = c->stack_max;
332 PL_tmps_stack = c->tmps_stack; 284 PL_tmps_stack = c->tmps_stack;
333 PL_tmps_floor = c->tmps_floor; 285 PL_tmps_floor = c->tmps_floor;
334 PL_tmps_ix = c->tmps_ix; 286 PL_tmps_ix = c->tmps_ix;
340 PL_scopestack_ix = c->scopestack_ix; 292 PL_scopestack_ix = c->scopestack_ix;
341 PL_scopestack_max = c->scopestack_max; 293 PL_scopestack_max = c->scopestack_max;
342 PL_savestack = c->savestack; 294 PL_savestack = c->savestack;
343 PL_savestack_ix = c->savestack_ix; 295 PL_savestack_ix = c->savestack_ix;
344 PL_savestack_max = c->savestack_max; 296 PL_savestack_max = c->savestack_max;
297#if PERL_VERSION < 9
345 PL_retstack = c->retstack; 298 PL_retstack = c->retstack;
346 PL_retstack_ix = c->retstack_ix; 299 PL_retstack_ix = c->retstack_ix;
347 PL_retstack_max = c->retstack_max; 300 PL_retstack_max = c->retstack_max;
301#endif
302 PL_curpm = c->curpm;
348 PL_curcop = c->curcop; 303 PL_curcop = c->curcop;
349 PL_top_env = c->top_env; 304 PL_top_env = c->top_env;
350 305
351 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
352 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
411 } 366 }
412 367
413 PUSHs ((SV *)CvPADLIST(cv)); 368 PUSHs ((SV *)CvPADLIST(cv));
414 PUSHs ((SV *)cv); 369 PUSHs ((SV *)cv);
415 370
416 get_padlist (aTHX_ cv); /* this is a monster */ 371 get_padlist (aTHX_ cv);
417 } 372 }
418 } 373 }
419#ifdef CXt_FORMAT 374#ifdef CXt_FORMAT
420 else if (CxTYPE(cx) == CXt_FORMAT) 375 else if (CxTYPE(cx) == CXt_FORMAT)
421 { 376 {
449 c->mainstack = PL_mainstack; 404 c->mainstack = PL_mainstack;
450 c->stack_sp = PL_stack_sp; 405 c->stack_sp = PL_stack_sp;
451 c->op = PL_op; 406 c->op = PL_op;
452 c->curpad = PL_curpad; 407 c->curpad = PL_curpad;
453 c->comppad = PL_comppad; 408 c->comppad = PL_comppad;
409 c->compcv = PL_compcv;
454 c->stack_base = PL_stack_base; 410 c->stack_base = PL_stack_base;
455 c->stack_max = PL_stack_max; 411 c->stack_max = PL_stack_max;
456 c->tmps_stack = PL_tmps_stack; 412 c->tmps_stack = PL_tmps_stack;
457 c->tmps_floor = PL_tmps_floor; 413 c->tmps_floor = PL_tmps_floor;
458 c->tmps_ix = PL_tmps_ix; 414 c->tmps_ix = PL_tmps_ix;
464 c->scopestack_ix = PL_scopestack_ix; 420 c->scopestack_ix = PL_scopestack_ix;
465 c->scopestack_max = PL_scopestack_max; 421 c->scopestack_max = PL_scopestack_max;
466 c->savestack = PL_savestack; 422 c->savestack = PL_savestack;
467 c->savestack_ix = PL_savestack_ix; 423 c->savestack_ix = PL_savestack_ix;
468 c->savestack_max = PL_savestack_max; 424 c->savestack_max = PL_savestack_max;
425#if PERL_VERSION < 9
469 c->retstack = PL_retstack; 426 c->retstack = PL_retstack;
470 c->retstack_ix = PL_retstack_ix; 427 c->retstack_ix = PL_retstack_ix;
471 c->retstack_max = PL_retstack_max; 428 c->retstack_max = PL_retstack_max;
429#endif
430 c->curpm = PL_curpm;
472 c->curcop = PL_curcop; 431 c->curcop = PL_curcop;
473 c->top_env = PL_top_env; 432 c->top_env = PL_top_env;
474} 433}
475 434
476/* 435/*
477 * allocate various perl stacks. This is an exact copy 436 * allocate various perl stacks. This is an exact copy
478 * of perl.c:init_stacks, except that it uses less memory 437 * of perl.c:init_stacks, except that it uses less memory
479 * on the (sometimes correct) assumption that coroutines do 438 * on the (sometimes correct) assumption that coroutines do
480 * not usually need a lot of stackspace. 439 * not usually need a lot of stackspace.
481 */ 440 */
482STATIC void 441static void
483coro_init_stacks (pTHX) 442coro_init_stacks (pTHX)
484{ 443{
485 LOCK; 444 LOCK;
486 445
487 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 446 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
512 471
513 New(54,PL_savestack,96,ANY); 472 New(54,PL_savestack,96,ANY);
514 PL_savestack_ix = 0; 473 PL_savestack_ix = 0;
515 PL_savestack_max = 96; 474 PL_savestack_max = 96;
516 475
476#if PERL_VERSION < 9
517 New(54,PL_retstack,8,OP*); 477 New(54,PL_retstack,8,OP*);
518 PL_retstack_ix = 0; 478 PL_retstack_ix = 0;
519 PL_retstack_max = 8; 479 PL_retstack_max = 8;
480#endif
520 481
521 UNLOCK; 482 UNLOCK;
522} 483}
523 484
524/* 485/*
525 * destroy the stacks, the callchain etc... 486 * destroy the stacks, the callchain etc...
526 */ 487 */
527STATIC void 488static void
528destroy_stacks(pTHX) 489destroy_stacks(pTHX)
529{ 490{
530 if (!IN_DESTRUCT) 491 if (!IN_DESTRUCT)
531 { 492 {
532 /* is this ugly, I ask? */ 493 /* is this ugly, I ask? */
564 525
565 Safefree (PL_tmps_stack); 526 Safefree (PL_tmps_stack);
566 Safefree (PL_markstack); 527 Safefree (PL_markstack);
567 Safefree (PL_scopestack); 528 Safefree (PL_scopestack);
568 Safefree (PL_savestack); 529 Safefree (PL_savestack);
530#if PERL_VERSION < 9
569 Safefree (PL_retstack); 531 Safefree (PL_retstack);
532#endif
570} 533}
571 534
572static void 535static void
573allocate_stack (Coro__State ctx, int alloc) 536allocate_stack (Coro__State ctx, int alloc)
574{ 537{
581 stack->gencnt = ctx->gencnt = 0; 544 stack->gencnt = ctx->gencnt = 0;
582 545
583 if (alloc) 546 if (alloc)
584 { 547 {
585#if HAVE_MMAP 548#if HAVE_MMAP
586 stack->ssize = 16384 * 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 */
587 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);
588 if (stack->sptr == (void *)-1) 551 if (stack->sptr != (void *)-1)
589#endif
590 { 552 {
591 /*FIXME*//*D*//* reasonable stack size! */ 553# if STACKGUARD
554 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
555# endif
556 }
557 else
558#endif
559 {
592 stack->ssize = - (8192 * sizeof (long)); 560 stack->ssize = - (STACKSIZE * (long)sizeof (long));
593 New (0, stack->sptr, 8192, long); 561 New (0, stack->sptr, STACKSIZE, long);
594 } 562 }
595 } 563 }
596 else 564 else
597 stack->sptr = 0; 565 stack->sptr = 0;
598 566
658 } 626 }
659 else 627 else
660 { 628 {
661 UNOP myop; 629 UNOP myop;
662 630
663 PL_op = (OP *)&myop;
664
665 Zero(&myop, 1, UNOP); 631 Zero(&myop, 1, UNOP);
666 myop.op_next = Nullop; 632 myop.op_next = Nullop;
667 myop.op_flags = OPf_WANT_VOID; 633 myop.op_flags = OPf_WANT_VOID;
634
635 PL_op = (OP *)&myop;
668 636
669 PUSHMARK(SP); 637 PUSHMARK(SP);
670 XPUSHs (sub_init); 638 XPUSHs (sub_init);
671 /* 639 /*
672 * the next line is slightly wrong, as PL_op->op_next 640 * the next line is slightly wrong, as PL_op->op_next
689 /* 657 /*
690 * this is a _very_ stripped down perl interpreter ;) 658 * this is a _very_ stripped down perl interpreter ;)
691 */ 659 */
692 dTHX; 660 dTHX;
693 Coro__State ctx = (Coro__State)arg; 661 Coro__State ctx = (Coro__State)arg;
694 JMPENV coro_start_env;
695 662
696 PL_top_env = &ctx->start_env; 663 PL_top_env = &PL_start_env;
697 664
698 ctx->cursp = 0; 665 ctx->cursp = 0;
699 PL_op = PL_op->op_next; 666 PL_op = PL_op->op_next;
700 CALLRUNOPS(aTHX); 667 CALLRUNOPS(aTHX);
701 668
702 abort (); 669 abort ();
703} 670}
704 671
705STATIC void 672/* never call directly, always through the coro_state_transfer global variable */
673static void
706transfer (pTHX_ struct coro *prev, struct coro *next, int flags) 674transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
707{ 675{
708 dSTACKLEVEL; 676 dSTACKLEVEL;
709 677
710 if (prev != next) 678 if (prev != next)
711 { 679 {
680 /* has this coro been created yet? */
712 if (next->mainstack) 681 if (next->mainstack)
713 { 682 {
714 LOCK; 683 LOCK;
715 SAVE (prev, flags); 684 SAVE (prev, flags);
716 LOAD (next); 685 LOAD (next);
755 { 724 {
756 LOCK; 725 LOCK;
757 SAVE (prev, -1); /* first get rid of the old state */ 726 SAVE (prev, -1); /* first get rid of the old state */
758 UNLOCK; 727 UNLOCK;
759 728
729 /* create the coroutine for the first time */
760 if (flags & TRANSFER_SAVE_CCTXT) 730 if (flags & TRANSFER_SAVE_CCTXT)
761 { 731 {
762 if (!prev->stack) 732 if (!prev->stack)
763 allocate_stack (prev, 0); 733 allocate_stack (prev, 0);
764 734
735 /* the new coroutine starts with start_env again */
736 PL_top_env = &PL_start_env;
737
765 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 738 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
766 { 739 {
767 PL_top_env = &next->start_env;
768
769 setup_coro (next); 740 setup_coro (next);
770 next->cursp = stacklevel; 741 next->cursp = stacklevel;
771 742
772 prev->stack->refcnt++; 743 prev->stack->refcnt++;
773 prev->stack->usecnt++; 744 prev->stack->usecnt++;
801 coro_mortal = 0; 772 coro_mortal = 0;
802 } 773 }
803 UNLOCK; 774 UNLOCK;
804} 775}
805 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
806#define SV_CORO(sv,func) \ 781#define SV_CORO(sv,func) \
807 do { \ 782 do { \
808 if (SvROK (sv)) \ 783 if (SvROK (sv)) \
809 sv = SvRV (sv); \ 784 sv = SvRV (sv); \
810 \ 785 \
822 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 797 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
823 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); \
824 \ 799 \
825 } while(0) 800 } while(0)
826 801
827#define SvSTATE(sv) (struct coro *)SvIV (sv) 802#define SvSTATE(sv) INT2PTR (struct coro *, SvIVX (sv))
828 803
829static void 804static void
830api_transfer(pTHX_ SV *prev, SV *next, int flags) 805api_transfer (pTHX_ SV *prev, SV *next, int flags)
831{ 806{
832 SV_CORO (prev, "Coro::transfer"); 807 SV_CORO (prev, "Coro::transfer");
833 SV_CORO (next, "Coro::transfer"); 808 SV_CORO (next, "Coro::transfer");
834 809
835 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 810 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
836} 811}
837 812
838/** Coro ********************************************************************/ 813/** Coro ********************************************************************/
839 814
840#define PRIO_MAX 3 815#define PRIO_MAX 3
844#define PRIO_IDLE -3 819#define PRIO_IDLE -3
845#define PRIO_MIN -4 820#define PRIO_MIN -4
846 821
847/* for Coro.pm */ 822/* for Coro.pm */
848static GV *coro_current, *coro_idle; 823static GV *coro_current, *coro_idle;
849static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 824static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
850static int coro_nready; 825static int coro_nready;
851 826
852static void 827static void
853coro_enq (pTHX_ SV *sv) 828coro_enq (pTHX_ SV *sv)
854{ 829{
830 SV **xprio;
831 int prio;
832
855 if (SvTYPE (sv) == SVt_PVHV) 833 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"); 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++;
871} 845}
872 846
873static SV * 847static SV *
874coro_deq (pTHX_ int min_prio) 848coro_deq (pTHX_ int min_prio)
875{ 849{
878 min_prio -= PRIO_MIN; 852 min_prio -= PRIO_MIN;
879 if (min_prio < 0) 853 if (min_prio < 0)
880 min_prio = 0; 854 min_prio = 0;
881 855
882 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 856 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
883 if (av_len (coro_ready[prio]) >= 0) 857 if (AvFILLp (coro_ready [prio]) >= 0)
884 { 858 {
885 coro_nready--; 859 coro_nready--;
886 return av_shift (coro_ready[prio]); 860 return av_shift (coro_ready [prio]);
887 } 861 }
888 862
889 return 0; 863 return 0;
890} 864}
891 865
925 899
926 SV_CORO (next, "Coro::schedule"); 900 SV_CORO (next, "Coro::schedule");
927 901
928 UNLOCK; 902 UNLOCK;
929 903
930 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 904 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
931 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 905 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
932} 906}
933 907
934static void 908static void
935api_cede (void) 909api_cede (void)
936{ 910{
950BOOT: 924BOOT:
951{ /* {} necessary for stoopid perl-5.6.x */ 925{ /* {} necessary for stoopid perl-5.6.x */
952#ifdef USE_ITHREADS 926#ifdef USE_ITHREADS
953 MUTEX_INIT (&coro_mutex); 927 MUTEX_INIT (&coro_mutex);
954#endif 928#endif
929 BOOT_PAGESIZE;
955 930
956 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 931 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
957 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 932 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
958 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 933 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
959 934
979 croak ("Coro::State::_newprocess expects an arrayref"); 954 croak ("Coro::State::_newprocess expects an arrayref");
980 955
981 Newz (0, coro, 1, struct coro); 956 Newz (0, coro, 1, struct coro);
982 957
983 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 958 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
984 coro->mainstack = 0; /* actual work is done inside transfer */ 959 /*coro->mainstack = 0; *//*actual work is done inside transfer */
985 coro->stack = 0; 960 /*coro->stack = 0;*/
986
987 /* same as JMPENV_BOOTSTRAP */
988 /* we might be able to recycle start_env, but safe is safe */
989 //Zero(&coro->start_env, 1, JMPENV);
990 coro->start_env.je_ret = -1;
991 coro->start_env.je_mustcatch = TRUE;
992 961
993 RETVAL = coro; 962 RETVAL = coro;
994 OUTPUT: 963 OUTPUT:
995 RETVAL 964 RETVAL
996 965
1002 PROTOTYPE: @ 971 PROTOTYPE: @
1003 CODE: 972 CODE:
1004 PUTBACK; 973 PUTBACK;
1005 SV_CORO (next, "Coro::transfer"); 974 SV_CORO (next, "Coro::transfer");
1006 SV_CORO (prev, "Coro::transfer"); 975 SV_CORO (prev, "Coro::transfer");
1007 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 976 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1008 SPAGAIN; 977 SPAGAIN;
1009 978
1010void 979void
1011DESTROY(coro) 980DESTROY(coro)
1012 Coro::State coro 981 Coro::State coro
1015 if (coro->mainstack && coro->mainstack != main_mainstack) 984 if (coro->mainstack && coro->mainstack != main_mainstack)
1016 { 985 {
1017 struct coro temp; 986 struct coro temp;
1018 987
1019 PUTBACK; 988 PUTBACK;
1020 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 989 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
1021 LOAD(aTHX_ coro); 990 LOAD (aTHX_ coro);
1022 SPAGAIN; 991 SPAGAIN;
1023 992
1024 destroy_stacks (aTHX); 993 destroy_stacks (aTHX);
1025 994
1026 LOAD((&temp)); /* this will get rid of defsv etc.. */ 995 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1027 SPAGAIN; 996 SPAGAIN;
1028 997
1029 coro->mainstack = 0; 998 coro->mainstack = 0;
1030 } 999 }
1031 1000
1040 CODE: 1009 CODE:
1041 _exit (code); 1010 _exit (code);
1042 1011
1043MODULE = Coro::State PACKAGE = Coro::Cont 1012MODULE = Coro::State PACKAGE = Coro::Cont
1044 1013
1045# this is slightly dirty (should expose a c-level api)
1046
1047void 1014void
1048yield(...) 1015yield(...)
1049 PROTOTYPE: @ 1016 PROTOTYPE: @
1050 CODE: 1017 CODE:
1051 static SV *returnstk; 1018 SV *yieldstack;
1052 SV *sv; 1019 SV *sv;
1053 AV *defav = GvAV (PL_defgv); 1020 AV *defav = GvAV (PL_defgv);
1054 struct coro *prev, *next; 1021 struct coro *prev, *next;
1055 1022
1056 if (!returnstk) 1023 yieldstack = *hv_fetch (
1057 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1024 (HV *)SvRV (GvSV (coro_current)),
1025 "yieldstack", sizeof ("yieldstack") - 1,
1026 0
1027 );
1058 1028
1059 /* set up @_ -- ugly */ 1029 /* set up @_ -- ugly */
1060 av_clear (defav); 1030 av_clear (defav);
1061 av_fill (defav, items - 1); 1031 av_fill (defav, items - 1);
1062 while (items--) 1032 while (items--)
1063 av_store (defav, items, SvREFCNT_inc (ST(items))); 1033 av_store (defav, items, SvREFCNT_inc (ST(items)));
1064 1034
1065 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1066 sv = av_pop ((AV *)SvRV (returnstk)); 1035 sv = av_pop ((AV *)SvRV (yieldstack));
1067 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)));
1068 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)));
1069 SvREFCNT_dec (sv); 1038 SvREFCNT_dec (sv);
1070 1039
1071 transfer(aTHX_ prev, next, 0); 1040 coro_state_transfer (aTHX_ prev, next, 0);
1072 1041
1073MODULE = Coro::State PACKAGE = Coro 1042MODULE = Coro::State PACKAGE = Coro
1074
1075# this is slightly dirty (should expose a c-level api)
1076 1043
1077BOOT: 1044BOOT:
1078{ 1045{
1079 int i; 1046 int i;
1080 HV *stash = gv_stashpv ("Coro", TRUE); 1047 HV *stash = gv_stashpv ("Coro", TRUE);
1100 coroapi.ready = api_ready; 1067 coroapi.ready = api_ready;
1101 coroapi.nready = &coro_nready; 1068 coroapi.nready = &coro_nready;
1102 coroapi.current = coro_current; 1069 coroapi.current = coro_current;
1103 1070
1104 GCoroAPI = &coroapi; 1071 GCoroAPI = &coroapi;
1105 sv_setiv(sv, (IV)&coroapi); 1072 sv_setiv (sv, (IV)&coroapi);
1106 SvREADONLY_on(sv); 1073 SvREADONLY_on (sv);
1107 } 1074 }
1108} 1075}
1109 1076
1110#if !PERL_MICRO 1077#if !PERL_MICRO
1111 1078
1136cede(...) 1103cede(...)
1137 PROTOTYPE: 1104 PROTOTYPE:
1138 CODE: 1105 CODE:
1139 api_cede (); 1106 api_cede ();
1140 1107
1108# and these are hacks
1109SV *
1110_aio_get_state ()
1111 CODE:
1112{
1113 struct {
1114 int errorno;
1115 int laststype;
1116 int laststatval;
1117 Stat_t statcache;
1118 } data;
1119
1120 data.errorno = errno;
1121 data.laststype = PL_laststype;
1122 data.laststatval = PL_laststatval;
1123 data.statcache = PL_statcache;
1124
1125 RETVAL = newSVpvn ((char *)&data, sizeof data);
1126}
1127 OUTPUT:
1128 RETVAL
1129
1130void
1131_aio_set_state (char *data_)
1132 PROTOTYPE: $
1133 CODE:
1134{
1135 struct {
1136 int errorno;
1137 int laststype;
1138 int laststatval;
1139 Stat_t statcache;
1140 } *data = (void *)data_;
1141
1142 errno = data->errorno;
1143 PL_laststype = data->laststype;
1144 PL_laststatval = data->laststatval;
1145 PL_statcache = data->statcache;
1146}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines