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.55 by pcg, Thu Apr 1 02:29:05 2004 UTC vs.
Revision 1.85 by root, Fri Nov 24 00:32:04 2006 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c"
4
1#include "EXTERN.h" 5#include "EXTERN.h"
2#include "perl.h" 6#include "perl.h"
3#include "XSUB.h" 7#include "XSUB.h"
4 8
5#include "patchlevel.h" 9#include "patchlevel.h"
6 10
7#if PATCHLEVEL < 6 11#if PERL_VERSION < 6
8# ifndef PL_ppaddr 12# ifndef PL_ppaddr
9# define PL_ppaddr ppaddr 13# define PL_ppaddr ppaddr
10# endif 14# endif
11# ifndef call_sv 15# ifndef call_sv
12# define call_sv perl_call_sv 16# define call_sv perl_call_sv
23# ifndef IS_PADCONST 27# ifndef IS_PADCONST
24# define IS_PADCONST(v) 0 28# define IS_PADCONST(v) 0
25# endif 29# endif
26#endif 30#endif
27 31
28#include "libcoro/coro.c"
29
30#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
31 41
32#ifdef HAVE_MMAP 42#ifdef HAVE_MMAP
33# include <unistd.h> 43# include <unistd.h>
34# include <sys/mman.h> 44# include <sys/mman.h>
35# ifndef MAP_ANONYMOUS 45# ifndef MAP_ANONYMOUS
37# define MAP_ANONYMOUS MAP_ANON 47# define MAP_ANONYMOUS MAP_ANON
38# else 48# else
39# undef HAVE_MMAP 49# undef HAVE_MMAP
40# endif 50# endif
41# 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
42#endif 60#endif
43 61
44#define SUB_INIT "Coro::State::initialize" 62#define SUB_INIT "Coro::State::initialize"
45#define UCORO_STATE "_coro_state" 63#define UCORO_STATE "_coro_state"
46 64
55 73
56#include "CoroAPI.h" 74#include "CoroAPI.h"
57 75
58#ifdef USE_ITHREADS 76#ifdef USE_ITHREADS
59static perl_mutex coro_mutex; 77static perl_mutex coro_mutex;
60# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 78# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
61# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 79# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
62#else 80#else
63# define LOCK 0 81# define LOCK (void)0
64# define UNLOCK 0 82# define UNLOCK (void)0
65#endif 83#endif
66 84
67static struct CoroAPI coroapi; 85static struct CoroAPI coroapi;
86static AV *main_mainstack; /* used to differentiate between $main and others */
87static HV *coro_state_stash;
88static SV *ucoro_state_sv;
89static U32 ucoro_state_hash;
90static SV *coro_mortal; /* will be freed after next transfer */
68 91
69/* this is actually not only the c stack but also c registers etc... */ 92/* this is actually not only the c stack but also c registers etc... */
70typedef struct { 93typedef struct {
71 int refcnt; /* pointer reference counter */ 94 int refcnt; /* pointer reference counter */
72 int usecnt; /* shared by how many coroutines */ 95 int usecnt; /* shared by how many coroutines */
77 void *sptr; 100 void *sptr;
78 long ssize; /* positive == mmap, otherwise malloc */ 101 long ssize; /* positive == mmap, otherwise malloc */
79} coro_stack; 102} coro_stack;
80 103
81struct coro { 104struct coro {
82 /* the top-level JMPENV for each coroutine, needed to catch dies. */
83 JMPENV start_env;
84
85 /* the optional C context */ 105 /* the optional C context */
86 coro_stack *stack; 106 coro_stack *stack;
87 void *cursp; 107 void *cursp;
88 int gencnt; 108 int gencnt;
89 109
102 AV *mainstack; 122 AV *mainstack;
103 SV **stack_sp; 123 SV **stack_sp;
104 OP *op; 124 OP *op;
105 SV **curpad; 125 SV **curpad;
106 AV *comppad; 126 AV *comppad;
127 CV *compcv;
107 SV **stack_base; 128 SV **stack_base;
108 SV **stack_max; 129 SV **stack_max;
109 SV **tmps_stack; 130 SV **tmps_stack;
110 I32 tmps_floor; 131 I32 tmps_floor;
111 I32 tmps_ix; 132 I32 tmps_ix;
120 I32 savestack_ix; 141 I32 savestack_ix;
121 I32 savestack_max; 142 I32 savestack_max;
122 OP **retstack; 143 OP **retstack;
123 I32 retstack_ix; 144 I32 retstack_ix;
124 I32 retstack_max; 145 I32 retstack_max;
146 PMOP *curpm;
125 COP *curcop; 147 COP *curcop;
126 JMPENV *top_env; 148 JMPENV *top_env;
127 149
128 /* data associated with this coroutine (initial args) */ 150 /* data associated with this coroutine (initial args) */
129 AV *args; 151 AV *args;
130}; 152};
131 153
132typedef struct coro *Coro__State; 154typedef struct coro *Coro__State;
133typedef struct coro *Coro__State_or_hashref; 155typedef struct coro *Coro__State_or_hashref;
134 156
135static AV *main_mainstack; /* used to differentiate between $main and others */ 157static AV *
136static HV *coro_state_stash; 158coro_clone_padlist (pTHX_ CV *cv)
137static SV *ucoro_state_sv;
138static U32 ucoro_state_hash;
139static SV *coro_mortal; /* will be freed after next transfer */
140
141/* mostly copied from op.c:cv_clone2 */
142STATIC AV *
143clone_padlist (AV *protopadlist)
144{ 159{
145 AV *av; 160 AV *padlist = CvPADLIST (cv);
146 I32 ix;
147 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
148 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
149 SV **pname = AvARRAY (protopad_name);
150 SV **ppad = AvARRAY (protopad);
151 I32 fname = AvFILLp (protopad_name);
152 I32 fpad = AvFILLp (protopad);
153 AV *newpadlist, *newpad_name, *newpad; 161 AV *newpadlist, *newpad;
154 SV **npad;
155
156 newpad_name = newAV ();
157 for (ix = fname; ix >= 0; ix--)
158 av_store (newpad_name, ix, SvREFCNT_inc (pname[ix]));
159
160 newpad = newAV ();
161 av_fill (newpad, AvFILLp (protopad));
162 npad = AvARRAY (newpad);
163 162
164 newpadlist = newAV (); 163 newpadlist = newAV ();
165 AvREAL_off (newpadlist); 164 AvREAL_off (newpadlist);
166 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)));
167 av_store (newpadlist, 1, (SV *) newpad); 174 av_store (newpadlist, 1, (SV *)newpad);
168
169 av = newAV (); /* will be @_ */
170 av_extend (av, 0);
171 av_store (newpad, 0, (SV *) av);
172 AvFLAGS (av) = AVf_REIFY;
173
174 for (ix = fpad; ix > 0; ix--)
175 {
176 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
177
178 if (namesv && namesv != &PL_sv_undef)
179 {
180 char *name = SvPVX (namesv); /* XXX */
181
182 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
183 { /* lexical from outside? */
184 npad[ix] = SvREFCNT_inc (ppad[ix]);
185 }
186 else
187 { /* our own lexical */
188 SV *sv;
189 if (*name == '&')
190 sv = SvREFCNT_inc (ppad[ix]);
191 else if (*name == '@')
192 sv = (SV *) newAV ();
193 else if (*name == '%')
194 sv = (SV *) newHV ();
195 else
196 sv = NEWSV (0, 0);
197
198#ifdef SvPADBUSY
199 if (!SvPADBUSY (sv))
200#endif
201 SvPADMY_on (sv);
202
203 npad[ix] = sv;
204 }
205 }
206 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
207 {
208 npad[ix] = SvREFCNT_inc (ppad[ix]);
209 }
210 else
211 {
212 SV *sv = NEWSV (0, 0);
213 SvPADTMP_on (sv);
214 npad[ix] = sv;
215 }
216 }
217
218#if 0 /* return -ENOTUNDERSTOOD */
219 /* Now that vars are all in place, clone nested closures. */
220
221 for (ix = fpad; ix > 0; ix--) {
222 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
223 if (namesv
224 && namesv != &PL_sv_undef
225 && !(SvFLAGS(namesv) & SVf_FAKE)
226 && *SvPVX(namesv) == '&'
227 && CvCLONE(ppad[ix]))
228 {
229 CV *kid = cv_clone((CV*)ppad[ix]);
230 SvREFCNT_dec(ppad[ix]);
231 CvCLONE_on(kid);
232 SvPADMY_on(kid);
233 npad[ix] = (SV*)kid;
234 }
235 }
236#endif
237 175
238 return newpadlist; 176 return newpadlist;
239} 177}
240 178
241STATIC void 179static void
242free_padlist (AV *padlist) 180free_padlist (pTHX_ AV *padlist)
243{ 181{
244 /* may be during global destruction */ 182 /* may be during global destruction */
245 if (SvREFCNT (padlist)) 183 if (SvREFCNT (padlist))
246 { 184 {
247 I32 i = AvFILLp (padlist); 185 I32 i = AvFILLp (padlist);
260 198
261 SvREFCNT_dec ((SV*)padlist); 199 SvREFCNT_dec ((SV*)padlist);
262 } 200 }
263} 201}
264 202
265STATIC int 203static int
266coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 204coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
267{ 205{
268 AV *padlist; 206 AV *padlist;
269 AV *av = (AV *)mg->mg_obj; 207 AV *av = (AV *)mg->mg_obj;
270 208
271 /* casting is fun. */ 209 /* casting is fun. */
272 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 210 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
273 free_padlist (padlist); 211 free_padlist (aTHX_ padlist);
212
213 SvREFCNT_dec (av);
214
215 return 0;
274} 216}
275 217
276#define PERL_MAGIC_coro PERL_MAGIC_ext 218#define PERL_MAGIC_coro PERL_MAGIC_ext
277 219
278static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 220static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
279 221
280/* the next two functions merely cache the padlists */ 222/* the next two functions merely cache the padlists */
281STATIC void 223static void
282get_padlist (CV *cv) 224get_padlist (pTHX_ CV *cv)
283{ 225{
284 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 226 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
285 227
286 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 228 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
287 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 229 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
288 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
289 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 239 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
240#endif
241 }
290} 242}
291 243
292STATIC void 244static void
293put_padlist (CV *cv) 245put_padlist (pTHX_ CV *cv)
294{ 246{
295 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 247 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
296 248
297 if (!mg) 249 if (!mg)
298 { 250 {
324 PL_mainstack = c->mainstack; 276 PL_mainstack = c->mainstack;
325 PL_stack_sp = c->stack_sp; 277 PL_stack_sp = c->stack_sp;
326 PL_op = c->op; 278 PL_op = c->op;
327 PL_curpad = c->curpad; 279 PL_curpad = c->curpad;
328 PL_comppad = c->comppad; 280 PL_comppad = c->comppad;
281 PL_compcv = c->compcv;
329 PL_stack_base = c->stack_base; 282 PL_stack_base = c->stack_base;
330 PL_stack_max = c->stack_max; 283 PL_stack_max = c->stack_max;
331 PL_tmps_stack = c->tmps_stack; 284 PL_tmps_stack = c->tmps_stack;
332 PL_tmps_floor = c->tmps_floor; 285 PL_tmps_floor = c->tmps_floor;
333 PL_tmps_ix = c->tmps_ix; 286 PL_tmps_ix = c->tmps_ix;
339 PL_scopestack_ix = c->scopestack_ix; 292 PL_scopestack_ix = c->scopestack_ix;
340 PL_scopestack_max = c->scopestack_max; 293 PL_scopestack_max = c->scopestack_max;
341 PL_savestack = c->savestack; 294 PL_savestack = c->savestack;
342 PL_savestack_ix = c->savestack_ix; 295 PL_savestack_ix = c->savestack_ix;
343 PL_savestack_max = c->savestack_max; 296 PL_savestack_max = c->savestack_max;
297#if PERL_VERSION < 9
344 PL_retstack = c->retstack; 298 PL_retstack = c->retstack;
345 PL_retstack_ix = c->retstack_ix; 299 PL_retstack_ix = c->retstack_ix;
346 PL_retstack_max = c->retstack_max; 300 PL_retstack_max = c->retstack_max;
301#endif
302 PL_curpm = c->curpm;
347 PL_curcop = c->curcop; 303 PL_curcop = c->curcop;
348 PL_top_env = c->top_env; 304 PL_top_env = c->top_env;
349 305
350 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
351 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
360 { 316 {
361 AV *padlist = (AV *)POPs; 317 AV *padlist = (AV *)POPs;
362 318
363 if (padlist) 319 if (padlist)
364 { 320 {
365 put_padlist (cv); /* mark this padlist as available */ 321 put_padlist (aTHX_ cv); /* mark this padlist as available */
366 CvPADLIST(cv) = padlist; 322 CvPADLIST(cv) = padlist;
367 } 323 }
368 324
369 ++CvDEPTH(cv); 325 ++CvDEPTH(cv);
370 } 326 }
410 } 366 }
411 367
412 PUSHs ((SV *)CvPADLIST(cv)); 368 PUSHs ((SV *)CvPADLIST(cv));
413 PUSHs ((SV *)cv); 369 PUSHs ((SV *)cv);
414 370
415 get_padlist (cv); /* this is a monster */ 371 get_padlist (aTHX_ cv);
416 } 372 }
417 } 373 }
418#ifdef CXt_FORMAT 374#ifdef CXt_FORMAT
419 else if (CxTYPE(cx) == CXt_FORMAT) 375 else if (CxTYPE(cx) == CXt_FORMAT)
420 { 376 {
448 c->mainstack = PL_mainstack; 404 c->mainstack = PL_mainstack;
449 c->stack_sp = PL_stack_sp; 405 c->stack_sp = PL_stack_sp;
450 c->op = PL_op; 406 c->op = PL_op;
451 c->curpad = PL_curpad; 407 c->curpad = PL_curpad;
452 c->comppad = PL_comppad; 408 c->comppad = PL_comppad;
409 c->compcv = PL_compcv;
453 c->stack_base = PL_stack_base; 410 c->stack_base = PL_stack_base;
454 c->stack_max = PL_stack_max; 411 c->stack_max = PL_stack_max;
455 c->tmps_stack = PL_tmps_stack; 412 c->tmps_stack = PL_tmps_stack;
456 c->tmps_floor = PL_tmps_floor; 413 c->tmps_floor = PL_tmps_floor;
457 c->tmps_ix = PL_tmps_ix; 414 c->tmps_ix = PL_tmps_ix;
463 c->scopestack_ix = PL_scopestack_ix; 420 c->scopestack_ix = PL_scopestack_ix;
464 c->scopestack_max = PL_scopestack_max; 421 c->scopestack_max = PL_scopestack_max;
465 c->savestack = PL_savestack; 422 c->savestack = PL_savestack;
466 c->savestack_ix = PL_savestack_ix; 423 c->savestack_ix = PL_savestack_ix;
467 c->savestack_max = PL_savestack_max; 424 c->savestack_max = PL_savestack_max;
425#if PERL_VERSION < 9
468 c->retstack = PL_retstack; 426 c->retstack = PL_retstack;
469 c->retstack_ix = PL_retstack_ix; 427 c->retstack_ix = PL_retstack_ix;
470 c->retstack_max = PL_retstack_max; 428 c->retstack_max = PL_retstack_max;
429#endif
430 c->curpm = PL_curpm;
471 c->curcop = PL_curcop; 431 c->curcop = PL_curcop;
472 c->top_env = PL_top_env; 432 c->top_env = PL_top_env;
473} 433}
474 434
475/* 435/*
476 * allocate various perl stacks. This is an exact copy 436 * allocate various perl stacks. This is an exact copy
477 * of perl.c:init_stacks, except that it uses less memory 437 * of perl.c:init_stacks, except that it uses less memory
478 * on the (sometimes correct) assumption that coroutines do 438 * on the (sometimes correct) assumption that coroutines do
479 * not usually need a lot of stackspace. 439 * not usually need a lot of stackspace.
480 */ 440 */
481STATIC void 441static void
482coro_init_stacks (pTHX) 442coro_init_stacks (pTHX)
483{ 443{
484 LOCK; 444 LOCK;
485 445
486 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 446 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
511 471
512 New(54,PL_savestack,96,ANY); 472 New(54,PL_savestack,96,ANY);
513 PL_savestack_ix = 0; 473 PL_savestack_ix = 0;
514 PL_savestack_max = 96; 474 PL_savestack_max = 96;
515 475
476#if PERL_VERSION < 9
516 New(54,PL_retstack,8,OP*); 477 New(54,PL_retstack,8,OP*);
517 PL_retstack_ix = 0; 478 PL_retstack_ix = 0;
518 PL_retstack_max = 8; 479 PL_retstack_max = 8;
480#endif
519 481
520 UNLOCK; 482 UNLOCK;
521} 483}
522 484
523/* 485/*
524 * destroy the stacks, the callchain etc... 486 * destroy the stacks, the callchain etc...
525 */ 487 */
526STATIC void 488static void
527destroy_stacks(pTHX) 489destroy_stacks(pTHX)
528{ 490{
529 if (!IN_DESTRUCT) 491 if (!IN_DESTRUCT)
530 { 492 {
531 /* is this ugly, I ask? */ 493 /* is this ugly, I ask? */
532 LEAVE_SCOPE (0); 494 LEAVE_SCOPE (0);
533 495
534 /* sure it is, but more important: is it correct?? :/ */ 496 /* sure it is, but more important: is it correct?? :/ */
535 FREETMPS; 497 FREETMPS;
498
499 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
536 } 500 }
537 501
538 while (PL_curstackinfo->si_next) 502 while (PL_curstackinfo->si_next)
539 PL_curstackinfo = PL_curstackinfo->si_next; 503 PL_curstackinfo = PL_curstackinfo->si_next;
540 504
541 while (PL_curstackinfo) 505 while (PL_curstackinfo)
542 { 506 {
543 PERL_SI *p = PL_curstackinfo->si_prev; 507 PERL_SI *p = PL_curstackinfo->si_prev;
544 508
545 { 509 { /*D*//*remove*/
546 dSP; 510 dSP;
547 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 511 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
548 PUTBACK; /* possibly superfluous */ 512 PUTBACK; /* possibly superfluous */
549 } 513 }
550 514
551 if (!IN_DESTRUCT) 515 if (!IN_DESTRUCT)
552 { 516 {
553 dounwind(-1); 517 dounwind (-1);/*D*//*remove*/
554 SvREFCNT_dec(PL_curstackinfo->si_stack); 518 SvREFCNT_dec (PL_curstackinfo->si_stack);
555 } 519 }
556 520
557 Safefree(PL_curstackinfo->si_cxstack); 521 Safefree (PL_curstackinfo->si_cxstack);
558 Safefree(PL_curstackinfo); 522 Safefree (PL_curstackinfo);
559 PL_curstackinfo = p; 523 PL_curstackinfo = p;
560 } 524 }
561 525
562 Safefree(PL_tmps_stack); 526 Safefree (PL_tmps_stack);
563 Safefree(PL_markstack); 527 Safefree (PL_markstack);
564 Safefree(PL_scopestack); 528 Safefree (PL_scopestack);
565 Safefree(PL_savestack); 529 Safefree (PL_savestack);
530#if PERL_VERSION < 9
566 Safefree(PL_retstack); 531 Safefree (PL_retstack);
532#endif
567} 533}
568 534
569static void 535static void
570allocate_stack (Coro__State ctx, int alloc) 536allocate_stack (Coro__State ctx, int alloc)
571{ 537{
578 stack->gencnt = ctx->gencnt = 0; 544 stack->gencnt = ctx->gencnt = 0;
579 545
580 if (alloc) 546 if (alloc)
581 { 547 {
582#if HAVE_MMAP 548#if HAVE_MMAP
583 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 */
584 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);
585 if (stack->sptr == (void *)-1) 551 if (stack->sptr != (void *)-1)
586#endif
587 { 552 {
588 /*FIXME*//*D*//* reasonable stack size! */ 553# if STACKGUARD
554 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
555# endif
556 }
557 else
558#endif
559 {
589 stack->ssize = - (8192 * sizeof (long)); 560 stack->ssize = - (STACKSIZE * (long)sizeof (long));
590 New (0, stack->sptr, 8192, long); 561 New (0, stack->sptr, STACKSIZE, long);
591 } 562 }
592 } 563 }
593 else 564 else
594 stack->sptr = 0; 565 stack->sptr = 0;
595 566
625setup_coro (void *arg) 596setup_coro (void *arg)
626{ 597{
627 /* 598 /*
628 * emulate part of the perl startup here. 599 * emulate part of the perl startup here.
629 */ 600 */
601 dTHX;
630 dSP; 602 dSP;
631 Coro__State ctx = (Coro__State)arg; 603 Coro__State ctx = (Coro__State)arg;
632 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 604 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
633 605
634 coro_init_stacks (aTHX); 606 coro_init_stacks (aTHX);
654 } 626 }
655 else 627 else
656 { 628 {
657 UNOP myop; 629 UNOP myop;
658 630
659 PL_op = (OP *)&myop;
660
661 Zero(&myop, 1, UNOP); 631 Zero(&myop, 1, UNOP);
662 myop.op_next = Nullop; 632 myop.op_next = Nullop;
663 myop.op_flags = OPf_WANT_VOID; 633 myop.op_flags = OPf_WANT_VOID;
634
635 PL_op = (OP *)&myop;
664 636
665 PUSHMARK(SP); 637 PUSHMARK(SP);
666 XPUSHs (sub_init); 638 XPUSHs (sub_init);
667 /* 639 /*
668 * the next line is slightly wrong, as PL_op->op_next 640 * the next line is slightly wrong, as PL_op->op_next
683continue_coro (void *arg) 655continue_coro (void *arg)
684{ 656{
685 /* 657 /*
686 * this is a _very_ stripped down perl interpreter ;) 658 * this is a _very_ stripped down perl interpreter ;)
687 */ 659 */
660 dTHX;
688 Coro__State ctx = (Coro__State)arg; 661 Coro__State ctx = (Coro__State)arg;
689 JMPENV coro_start_env;
690 662
691 PL_top_env = &ctx->start_env; 663 PL_top_env = &PL_start_env;
692 664
693 ctx->cursp = 0; 665 ctx->cursp = 0;
694 PL_op = PL_op->op_next; 666 PL_op = PL_op->op_next;
695 CALLRUNOPS(aTHX); 667 CALLRUNOPS(aTHX);
696 668
697 abort (); 669 abort ();
698} 670}
699 671
700STATIC void 672/* never call directly, always through the coro_state_transfer global variable */
673static void
701transfer (pTHX_ struct coro *prev, struct coro *next, int flags) 674transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
702{ 675{
703 dSTACKLEVEL; 676 dSTACKLEVEL;
704 677
705 if (prev != next) 678 if (prev != next)
706 { 679 {
680 /* has this coro been created yet? */
707 if (next->mainstack) 681 if (next->mainstack)
708 { 682 {
709 LOCK; 683 LOCK;
710 SAVE (prev, flags); 684 SAVE (prev, flags);
711 LOAD (next); 685 LOAD (next);
750 { 724 {
751 LOCK; 725 LOCK;
752 SAVE (prev, -1); /* first get rid of the old state */ 726 SAVE (prev, -1); /* first get rid of the old state */
753 UNLOCK; 727 UNLOCK;
754 728
729 /* create the coroutine for the first time */
755 if (flags & TRANSFER_SAVE_CCTXT) 730 if (flags & TRANSFER_SAVE_CCTXT)
756 { 731 {
757 if (!prev->stack) 732 if (!prev->stack)
758 allocate_stack (prev, 0); 733 allocate_stack (prev, 0);
759 734
735 /* the new coroutine starts with start_env again */
736 PL_top_env = &PL_start_env;
737
760 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 738 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
761 { 739 {
762 PL_top_env = &next->start_env;
763
764 setup_coro (next); 740 setup_coro (next);
765 next->cursp = stacklevel; 741 next->cursp = stacklevel;
766 742
767 prev->stack->refcnt++; 743 prev->stack->refcnt++;
768 prev->stack->usecnt++; 744 prev->stack->usecnt++;
796 coro_mortal = 0; 772 coro_mortal = 0;
797 } 773 }
798 UNLOCK; 774 UNLOCK;
799} 775}
800 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
801#define SV_CORO(sv,func) \ 781#define SV_CORO(sv,func) \
802 do { \ 782 do { \
803 if (SvROK (sv)) \ 783 if (SvROK (sv)) \
804 sv = SvRV (sv); \ 784 sv = SvRV (sv); \
805 \ 785 \
817 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 797 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
818 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); \
819 \ 799 \
820 } while(0) 800 } while(0)
821 801
822#define SvSTATE(sv) (struct coro *)SvIV (sv) 802#define SvSTATE(sv) INT2PTR (struct coro *, SvIVX (sv))
823 803
824static void 804static void
825api_transfer(pTHX_ SV *prev, SV *next, int flags) 805api_transfer (pTHX_ SV *prev, SV *next, int flags)
826{ 806{
827 SV_CORO (prev, "Coro::transfer"); 807 SV_CORO (prev, "Coro::transfer");
828 SV_CORO (next, "Coro::transfer"); 808 SV_CORO (next, "Coro::transfer");
829 809
830 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); 810 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
831} 811}
832 812
833/** Coro ********************************************************************/ 813/** Coro ********************************************************************/
834 814
835#define PRIO_MAX 3 815#define PRIO_MAX 3
839#define PRIO_IDLE -3 819#define PRIO_IDLE -3
840#define PRIO_MIN -4 820#define PRIO_MIN -4
841 821
842/* for Coro.pm */ 822/* for Coro.pm */
843static GV *coro_current, *coro_idle; 823static GV *coro_current, *coro_idle;
844static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 824static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
845static int coro_nready; 825static int coro_nready;
846 826
847static void 827static void
848coro_enq (SV *sv) 828coro_enq (pTHX_ SV *sv)
849{ 829{
830 SV **xprio;
831 int prio;
832
850 if (SvTYPE (sv) == SVt_PVHV) 833 if (SvTYPE (sv) != SVt_PVHV)
851 {
852 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
853 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
854
855 prio = prio > PRIO_MAX ? PRIO_MAX
856 : prio < PRIO_MIN ? PRIO_MIN
857 : prio;
858
859 av_push (coro_ready [prio - PRIO_MIN], sv);
860 coro_nready++;
861
862 return;
863 }
864
865 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++;
866} 845}
867 846
868static SV * 847static SV *
869coro_deq (int min_prio) 848coro_deq (pTHX_ int min_prio)
870{ 849{
871 int prio = PRIO_MAX - PRIO_MIN; 850 int prio = PRIO_MAX - PRIO_MIN;
872 851
873 min_prio -= PRIO_MIN; 852 min_prio -= PRIO_MIN;
874 if (min_prio < 0) 853 if (min_prio < 0)
875 min_prio = 0; 854 min_prio = 0;
876 855
877 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 856 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
878 if (av_len (coro_ready[prio]) >= 0) 857 if (AvFILLp (coro_ready [prio]) >= 0)
879 { 858 {
880 coro_nready--; 859 coro_nready--;
881 return av_shift (coro_ready[prio]); 860 return av_shift (coro_ready [prio]);
882 } 861 }
883 862
884 return 0; 863 return 0;
885} 864}
886 865
887static void 866static void
888api_ready (SV *coro) 867api_ready (SV *coro)
889{ 868{
869 dTHX;
870
890 if (SvROK (coro)) 871 if (SvROK (coro))
891 coro = SvRV (coro); 872 coro = SvRV (coro);
892 873
893 LOCK; 874 LOCK;
894 coro_enq (SvREFCNT_inc (coro)); 875 coro_enq (aTHX_ SvREFCNT_inc (coro));
895 UNLOCK; 876 UNLOCK;
896} 877}
897 878
898static void 879static void
899api_schedule (void) 880api_schedule (void)
900{ 881{
882 dTHX;
883
901 SV *prev, *next; 884 SV *prev, *next;
902 885
903 LOCK; 886 LOCK;
904 887
905 prev = SvRV (GvSV (coro_current)); 888 prev = SvRV (GvSV (coro_current));
906 next = coro_deq (PRIO_MIN); 889 next = coro_deq (aTHX_ PRIO_MIN);
907 890
908 if (!next) 891 if (!next)
909 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 892 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
910 893
911 /* free this only after the transfer */ 894 /* free this only after the transfer */
916 899
917 SV_CORO (next, "Coro::schedule"); 900 SV_CORO (next, "Coro::schedule");
918 901
919 UNLOCK; 902 UNLOCK;
920 903
921 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 904 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
922 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 905 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
923} 906}
924 907
925static void 908static void
926api_cede (void) 909api_cede (void)
927{ 910{
911 dTHX;
912
928 LOCK; 913 LOCK;
929 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); 914 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
930 UNLOCK; 915 UNLOCK;
931 916
932 api_schedule (); 917 api_schedule ();
933} 918}
934 919
939BOOT: 924BOOT:
940{ /* {} necessary for stoopid perl-5.6.x */ 925{ /* {} necessary for stoopid perl-5.6.x */
941#ifdef USE_ITHREADS 926#ifdef USE_ITHREADS
942 MUTEX_INIT (&coro_mutex); 927 MUTEX_INIT (&coro_mutex);
943#endif 928#endif
929 BOOT_PAGESIZE;
944 930
945 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 931 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
946 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 932 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
947 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 933 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
948 934
968 croak ("Coro::State::_newprocess expects an arrayref"); 954 croak ("Coro::State::_newprocess expects an arrayref");
969 955
970 Newz (0, coro, 1, struct coro); 956 Newz (0, coro, 1, struct coro);
971 957
972 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 958 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
973 coro->mainstack = 0; /* actual work is done inside transfer */ 959 /*coro->mainstack = 0; *//*actual work is done inside transfer */
974 coro->stack = 0; 960 /*coro->stack = 0;*/
975
976 /* same as JMPENV_BOOTSTRAP */
977 /* we might be able to recycle start_env, but safe is safe */
978 //Zero(&coro->start_env, 1, JMPENV);
979 coro->start_env.je_ret = -1;
980 coro->start_env.je_mustcatch = TRUE;
981 961
982 RETVAL = coro; 962 RETVAL = coro;
983 OUTPUT: 963 OUTPUT:
984 RETVAL 964 RETVAL
985 965
991 PROTOTYPE: @ 971 PROTOTYPE: @
992 CODE: 972 CODE:
993 PUTBACK; 973 PUTBACK;
994 SV_CORO (next, "Coro::transfer"); 974 SV_CORO (next, "Coro::transfer");
995 SV_CORO (prev, "Coro::transfer"); 975 SV_CORO (prev, "Coro::transfer");
996 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 976 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
997 SPAGAIN; 977 SPAGAIN;
998 978
999void 979void
1000DESTROY(coro) 980DESTROY(coro)
1001 Coro::State coro 981 Coro::State coro
1004 if (coro->mainstack && coro->mainstack != main_mainstack) 984 if (coro->mainstack && coro->mainstack != main_mainstack)
1005 { 985 {
1006 struct coro temp; 986 struct coro temp;
1007 987
1008 PUTBACK; 988 PUTBACK;
1009 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 989 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
1010 LOAD(aTHX_ coro); 990 LOAD (aTHX_ coro);
1011 SPAGAIN; 991 SPAGAIN;
1012 992
1013 destroy_stacks (aTHX); 993 destroy_stacks (aTHX);
1014 994
1015 LOAD((&temp)); /* this will get rid of defsv etc.. */ 995 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1016 SPAGAIN; 996 SPAGAIN;
1017 997
1018 coro->mainstack = 0; 998 coro->mainstack = 0;
1019 } 999 }
1020 1000
1029 CODE: 1009 CODE:
1030 _exit (code); 1010 _exit (code);
1031 1011
1032MODULE = Coro::State PACKAGE = Coro::Cont 1012MODULE = Coro::State PACKAGE = Coro::Cont
1033 1013
1034# this is slightly dirty (should expose a c-level api)
1035
1036void 1014void
1037yield(...) 1015yield(...)
1038 PROTOTYPE: @ 1016 PROTOTYPE: @
1039 CODE: 1017 CODE:
1040 static SV *returnstk; 1018 SV *yieldstack;
1041 SV *sv; 1019 SV *sv;
1042 AV *defav = GvAV (PL_defgv); 1020 AV *defav = GvAV (PL_defgv);
1043 struct coro *prev, *next; 1021 struct coro *prev, *next;
1044 1022
1045 if (!returnstk) 1023 yieldstack = *hv_fetch (
1046 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1024 (HV *)SvRV (GvSV (coro_current)),
1025 "yieldstack", sizeof ("yieldstack") - 1,
1026 0
1027 );
1047 1028
1048 /* set up @_ -- ugly */ 1029 /* set up @_ -- ugly */
1049 av_clear (defav); 1030 av_clear (defav);
1050 av_fill (defav, items - 1); 1031 av_fill (defav, items - 1);
1051 while (items--) 1032 while (items--)
1052 av_store (defav, items, SvREFCNT_inc (ST(items))); 1033 av_store (defav, items, SvREFCNT_inc (ST(items)));
1053 1034
1054 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1055 sv = av_pop ((AV *)SvRV (returnstk)); 1035 sv = av_pop ((AV *)SvRV (yieldstack));
1056 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)));
1057 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)));
1058 SvREFCNT_dec (sv); 1038 SvREFCNT_dec (sv);
1059 1039
1060 transfer(aTHX_ prev, next, 0); 1040 coro_state_transfer (aTHX_ prev, next, 0);
1061 1041
1062MODULE = Coro::State PACKAGE = Coro 1042MODULE = Coro::State PACKAGE = Coro
1063
1064# this is slightly dirty (should expose a c-level api)
1065 1043
1066BOOT: 1044BOOT:
1067{ 1045{
1068 int i; 1046 int i;
1069 HV *stash = gv_stashpv ("Coro", TRUE); 1047 HV *stash = gv_stashpv ("Coro", TRUE);
1089 coroapi.ready = api_ready; 1067 coroapi.ready = api_ready;
1090 coroapi.nready = &coro_nready; 1068 coroapi.nready = &coro_nready;
1091 coroapi.current = coro_current; 1069 coroapi.current = coro_current;
1092 1070
1093 GCoroAPI = &coroapi; 1071 GCoroAPI = &coroapi;
1094 sv_setiv(sv, (IV)&coroapi); 1072 sv_setiv (sv, (IV)&coroapi);
1095 SvREADONLY_on(sv); 1073 SvREADONLY_on (sv);
1096 } 1074 }
1097} 1075}
1098 1076
1099#if !PERL_MICRO 1077#if !PERL_MICRO
1100 1078
1125cede(...) 1103cede(...)
1126 PROTOTYPE: 1104 PROTOTYPE:
1127 CODE: 1105 CODE:
1128 api_cede (); 1106 api_cede ();
1129 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