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.16 by root, Wed Jul 25 04:26:55 2001 UTC vs.
Revision 1.70 by root, Mon Dec 26 19:26:52 2005 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 "libcoro/coro.c" 9#include "patchlevel.h"
10
11#if PERL_VERSION < 6
12# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr
14# endif
15# ifndef call_sv
16# define call_sv perl_call_sv
17# endif
18# ifndef get_sv
19# define get_sv perl_get_sv
20# endif
21# ifndef get_cv
22# define get_cv perl_get_cv
23# endif
24# ifndef IS_PADGV
25# define IS_PADGV(v) 0
26# endif
27# ifndef IS_PADCONST
28# define IS_PADCONST(v) 0
29# endif
30#endif
31
32#include <errno.h>
33#include <signal.h>
6 34
7#ifdef HAVE_MMAP 35#ifdef HAVE_MMAP
8# include <unistd.h> 36# include <unistd.h>
9# include <sys/mman.h> 37# include <sys/mman.h>
10# ifndef MAP_ANON
11# ifdef MAP_ANONYMOUS 38# ifndef MAP_ANONYMOUS
12# define MAP_ANON MAP_ANONYMOUS 39# ifdef MAP_ANON
40# define MAP_ANONYMOUS MAP_ANON
13# else 41# else
14# undef HAVE_MMAP 42# undef HAVE_MMAP
15# endif 43# endif
16# endif 44# endif
17#endif 45#endif
18 46
19#define MAY_FLUSH /* increases codesize */
20
21/* perl-related */
22#define TRANSFER_SAVE_DEFAV 0x00000001
23#define TRANSFER_SAVE_DEFSV 0x00000002
24#define TRANSFER_SAVE_ERRSV 0x00000004
25/* c-related */
26#define TRANSFER_SAVE_CCTXT 0x00000008
27#ifdef CORO_LAZY_STACK
28# define TRANSFER_LAZY_STACK 0x00000010
29#else
30# define TRANSFER_LAZY_STACK 0x00000000
31#endif
32
33#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
34 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
35
36#define SUB_INIT "Coro::State::initialize" 47#define SUB_INIT "Coro::State::initialize"
37#define UCORO_STATE "_coro_state" 48#define UCORO_STATE "_coro_state"
38 49
39/* The next macro should delcare a variable stacklevel that contains and approximation 50/* The next macro should declare a variable stacklevel that contains and approximation
40 * to the current C stack pointer. It's property is that it changes with each call 51 * to the current C stack pointer. Its property is that it changes with each call
41 * and should be unique. */ 52 * and should be unique. */
42#define dSTACKLEVEL void *stacklevel = &stacklevel 53#define dSTACKLEVEL void *stacklevel = &stacklevel
43 54
55#define IN_DESTRUCT (PL_main_cv == Nullcv)
56
44#define labs(l) ((l) >= 0 ? (l) : -(l)) 57#define labs(l) ((l) >= 0 ? (l) : -(l))
58
59#include "CoroAPI.h"
60
61#ifdef USE_ITHREADS
62static perl_mutex coro_mutex;
63# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
64# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
65#else
66# define LOCK (void)0
67# define UNLOCK (void)0
68#endif
69
70static struct CoroAPI coroapi;
71static AV *main_mainstack; /* used to differentiate between $main and others */
72static HV *coro_state_stash;
73static SV *ucoro_state_sv;
74static U32 ucoro_state_hash;
75static SV *coro_mortal; /* will be freed after next transfer */
45 76
46/* this is actually not only the c stack but also c registers etc... */ 77/* this is actually not only the c stack but also c registers etc... */
47typedef struct { 78typedef struct {
48 int refcnt; /* pointer reference counter */ 79 int refcnt; /* pointer reference counter */
49 int usecnt; /* shared by how many coroutines */ 80 int usecnt; /* shared by how many coroutines */
53 84
54 void *sptr; 85 void *sptr;
55 long ssize; /* positive == mmap, otherwise malloc */ 86 long ssize; /* positive == mmap, otherwise malloc */
56} coro_stack; 87} coro_stack;
57 88
58static coro_stack main_stack = { 1, 0, 0 };
59
60struct coro { 89struct coro {
90 /* the top-level JMPENV for each coroutine, needed to catch dies. */
91 JMPENV start_env;
92
61 /* the optional C context */ 93 /* the optional C context */
62 coro_stack *stack; 94 coro_stack *stack;
63 void *cursp; 95 void *cursp;
64 int gencnt; 96 int gencnt;
65 97
68 SV *defsv; 100 SV *defsv;
69 SV *errsv; 101 SV *errsv;
70 102
71 /* saved global state not related to stacks */ 103 /* saved global state not related to stacks */
72 U8 dowarn; 104 U8 dowarn;
105 I32 in_eval;
73 106
74 /* the stacks and related info (callchain etc..) */ 107 /* the stacks and related info (callchain etc..) */
75 PERL_SI *curstackinfo; 108 PERL_SI *curstackinfo;
76 AV *curstack; 109 AV *curstack;
77 AV *mainstack; 110 AV *mainstack;
78 SV **stack_sp; 111 SV **stack_sp;
79 OP *op; 112 OP *op;
80 SV **curpad; 113 SV **curpad;
114 AV *comppad;
115 CV *compcv;
81 SV **stack_base; 116 SV **stack_base;
82 SV **stack_max; 117 SV **stack_max;
83 SV **tmps_stack; 118 SV **tmps_stack;
84 I32 tmps_floor; 119 I32 tmps_floor;
85 I32 tmps_ix; 120 I32 tmps_ix;
94 I32 savestack_ix; 129 I32 savestack_ix;
95 I32 savestack_max; 130 I32 savestack_max;
96 OP **retstack; 131 OP **retstack;
97 I32 retstack_ix; 132 I32 retstack_ix;
98 I32 retstack_max; 133 I32 retstack_max;
134 PMOP *curpm;
99 COP *curcop; 135 COP *curcop;
100 JMPENV start_env;
101 JMPENV *top_env; 136 JMPENV *top_env;
102 137
103 /* data associated with this coroutine (initial args) */ 138 /* data associated with this coroutine (initial args) */
104 AV *args; 139 AV *args;
105}; 140};
106 141
107typedef struct coro *Coro__State; 142typedef struct coro *Coro__State;
108typedef struct coro *Coro__State_or_hashref; 143typedef struct coro *Coro__State_or_hashref;
109 144
110static AV *main_mainstack; /* used to differentiate between $main and others */
111static HV *coro_state_stash;
112static SV *ucoro_state_sv;
113static U32 ucoro_state_hash;
114static HV *padlist_cache;
115
116/* mostly copied from op.c:cv_clone2 */ 145/* mostly copied from op.c:cv_clone2 */
117STATIC AV * 146STATIC AV *
118clone_padlist (AV *protopadlist) 147clone_padlist (pTHX_ AV *protopadlist)
119{ 148{
120 AV *av; 149 AV *av;
121 I32 ix; 150 I32 ix;
122 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); 151 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
123 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); 152 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
147 AvFLAGS (av) = AVf_REIFY; 176 AvFLAGS (av) = AVf_REIFY;
148 177
149 for (ix = fpad; ix > 0; ix--) 178 for (ix = fpad; ix > 0; ix--)
150 { 179 {
151 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 180 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
181
152 if (namesv && namesv != &PL_sv_undef) 182 if (namesv && namesv != &PL_sv_undef)
153 { 183 {
154 char *name = SvPVX (namesv); /* XXX */ 184 char *name = SvPVX (namesv); /* XXX */
185
155 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') 186 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
156 { /* lexical from outside? */ 187 { /* lexical from outside? */
157 npad[ix] = SvREFCNT_inc (ppad[ix]); 188 npad[ix] = SvREFCNT_inc (ppad[ix]);
158 } 189 }
159 else 190 else
165 sv = (SV *) newAV (); 196 sv = (SV *) newAV ();
166 else if (*name == '%') 197 else if (*name == '%')
167 sv = (SV *) newHV (); 198 sv = (SV *) newHV ();
168 else 199 else
169 sv = NEWSV (0, 0); 200 sv = NEWSV (0, 0);
201
202#ifdef SvPADBUSY
170 if (!SvPADBUSY (sv)) 203 if (!SvPADBUSY (sv))
204#endif
171 SvPADMY_on (sv); 205 SvPADMY_on (sv);
206
172 npad[ix] = sv; 207 npad[ix] = sv;
173 } 208 }
174 } 209 }
175 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 210 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
176 { 211 {
205#endif 240#endif
206 241
207 return newpadlist; 242 return newpadlist;
208} 243}
209 244
210#ifdef MAY_FLUSH 245STATIC void
211STATIC AV *
212free_padlist (AV *padlist) 246free_padlist (pTHX_ AV *padlist)
213{ 247{
214 /* may be during global destruction */ 248 /* may be during global destruction */
215 if (SvREFCNT(padlist)) 249 if (SvREFCNT (padlist))
216 { 250 {
217 I32 i = AvFILLp(padlist); 251 I32 i = AvFILLp (padlist);
218 while (i >= 0) 252 while (i >= 0)
219 { 253 {
220 SV **svp = av_fetch(padlist, i--, FALSE); 254 SV **svp = av_fetch (padlist, i--, FALSE);
221 SV *sv = svp ? *svp : Nullsv;
222 if (sv) 255 if (svp)
256 {
257 SV *sv;
258 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
223 SvREFCNT_dec(sv); 259 SvREFCNT_dec (sv);
260
261 SvREFCNT_dec (*svp);
262 }
224 } 263 }
225 264
226 SvREFCNT_dec((SV*)padlist); 265 SvREFCNT_dec ((SV*)padlist);
227 } 266 }
228} 267}
229#endif 268
269STATIC int
270coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
271{
272 AV *padlist;
273 AV *av = (AV *)mg->mg_obj;
274
275 /* casting is fun. */
276 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
277 free_padlist (aTHX_ padlist);
278
279 SvREFCNT_dec (av);
280}
281
282#define PERL_MAGIC_coro PERL_MAGIC_ext
283
284static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
230 285
231/* the next two functions merely cache the padlists */ 286/* the next two functions merely cache the padlists */
232STATIC void 287STATIC void
233get_padlist (CV *cv) 288get_padlist (pTHX_ CV *cv)
234{ 289{
235 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); 290 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
236 291
237 if (he && AvFILLp ((AV *)*he) >= 0) 292 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
238 CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); 293 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
239 else 294 else
240 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 295 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv));
241} 296}
242 297
243STATIC void 298STATIC void
244put_padlist (CV *cv) 299put_padlist (pTHX_ CV *cv)
245{ 300{
246 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); 301 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
247 302
248 if (SvTYPE (*he) != SVt_PVAV) 303 if (!mg)
249 { 304 {
250 SvREFCNT_dec (*he); 305 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
306 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
307 mg->mg_virtual = &vtbl_coro;
251 *he = (SV *)newAV (); 308 mg->mg_obj = (SV *)newAV ();
252 } 309 }
253 310
254 av_push ((AV *)*he, (SV *)CvPADLIST (cv)); 311 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv));
255} 312}
256
257#ifdef MAY_FLUSH
258STATIC void
259flush_padlist_cache ()
260{
261 HV *hv = padlist_cache;
262 padlist_cache = newHV ();
263
264 if (hv_iterinit (hv))
265 {
266 HE *he;
267 AV *padlist;
268
269 while (!!(he = hv_iternext (hv)))
270 {
271 AV *av = (AV *)HeVAL(he);
272
273 /* casting is fun. */
274 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
275 free_padlist (padlist);
276 }
277 }
278
279 SvREFCNT_dec (hv);
280}
281#endif
282 313
283#define SB do { 314#define SB do {
284#define SE } while (0) 315#define SE } while (0)
285 316
286#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 317#define LOAD(state) load_state(aTHX_ (state));
287#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 318#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
288 319
289#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 320#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
290 321
291static void 322static void
292load_state(pTHX_ Coro__State c) 323load_state(pTHX_ Coro__State c)
293{ 324{
294 PL_dowarn = c->dowarn; 325 PL_dowarn = c->dowarn;
326 PL_in_eval = c->in_eval;
295 327
296 PL_curstackinfo = c->curstackinfo; 328 PL_curstackinfo = c->curstackinfo;
297 PL_curstack = c->curstack; 329 PL_curstack = c->curstack;
298 PL_mainstack = c->mainstack; 330 PL_mainstack = c->mainstack;
299 PL_stack_sp = c->stack_sp; 331 PL_stack_sp = c->stack_sp;
300 PL_op = c->op; 332 PL_op = c->op;
301 PL_curpad = c->curpad; 333 PL_curpad = c->curpad;
334 PL_comppad = c->comppad;
335 PL_compcv = c->compcv;
302 PL_stack_base = c->stack_base; 336 PL_stack_base = c->stack_base;
303 PL_stack_max = c->stack_max; 337 PL_stack_max = c->stack_max;
304 PL_tmps_stack = c->tmps_stack; 338 PL_tmps_stack = c->tmps_stack;
305 PL_tmps_floor = c->tmps_floor; 339 PL_tmps_floor = c->tmps_floor;
306 PL_tmps_ix = c->tmps_ix; 340 PL_tmps_ix = c->tmps_ix;
315 PL_savestack_ix = c->savestack_ix; 349 PL_savestack_ix = c->savestack_ix;
316 PL_savestack_max = c->savestack_max; 350 PL_savestack_max = c->savestack_max;
317 PL_retstack = c->retstack; 351 PL_retstack = c->retstack;
318 PL_retstack_ix = c->retstack_ix; 352 PL_retstack_ix = c->retstack_ix;
319 PL_retstack_max = c->retstack_max; 353 PL_retstack_max = c->retstack_max;
354 PL_curpm = c->curpm;
320 PL_curcop = c->curcop; 355 PL_curcop = c->curcop;
321 PL_start_env = c->start_env;
322 PL_top_env = c->top_env; 356 PL_top_env = c->top_env;
323 357
324 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 358 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
325 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 359 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
326 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 360 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
334 { 368 {
335 AV *padlist = (AV *)POPs; 369 AV *padlist = (AV *)POPs;
336 370
337 if (padlist) 371 if (padlist)
338 { 372 {
339 put_padlist (cv); /* mark this padlist as available */ 373 put_padlist (aTHX_ cv); /* mark this padlist as available */
340 CvPADLIST(cv) = padlist; 374 CvPADLIST(cv) = padlist;
341#ifdef USE_THREADS
342 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
343#endif
344 } 375 }
345 376
346 ++CvDEPTH(cv); 377 ++CvDEPTH(cv);
347 } 378 }
348 379
366 397
367 PUSHs (Nullsv); 398 PUSHs (Nullsv);
368 /* this loop was inspired by pp_caller */ 399 /* this loop was inspired by pp_caller */
369 for (;;) 400 for (;;)
370 { 401 {
371 do 402 while (cxix >= 0)
372 { 403 {
373 PERL_CONTEXT *cx = &ccstk[cxix--]; 404 PERL_CONTEXT *cx = &ccstk[cxix--];
374 405
375 if (CxTYPE(cx) == CXt_SUB) 406 if (CxTYPE(cx) == CXt_SUB)
376 { 407 {
377 CV *cv = cx->blk_sub.cv; 408 CV *cv = cx->blk_sub.cv;
378 if (CvDEPTH(cv)) 409 if (CvDEPTH(cv))
379 { 410 {
380#ifdef USE_THREADS
381 /*XPUSHs ((SV *)CvOWNER(cv));*/
382 /*CvOWNER(cv) = 0;*/
383 /*error must unlock this cv etc.. etc...*/
384#endif
385 EXTEND (SP, CvDEPTH(cv)*2); 411 EXTEND (SP, CvDEPTH(cv)*2);
386 412
387 while (--CvDEPTH(cv)) 413 while (--CvDEPTH(cv))
388 { 414 {
389 /* this tells the restore code to increment CvDEPTH */ 415 /* this tells the restore code to increment CvDEPTH */
392 } 418 }
393 419
394 PUSHs ((SV *)CvPADLIST(cv)); 420 PUSHs ((SV *)CvPADLIST(cv));
395 PUSHs ((SV *)cv); 421 PUSHs ((SV *)cv);
396 422
397 get_padlist (cv); /* this is a monster */ 423 get_padlist (aTHX_ cv); /* this is a monster */
398 } 424 }
399 } 425 }
426#ifdef CXt_FORMAT
400 else if (CxTYPE(cx) == CXt_FORMAT) 427 else if (CxTYPE(cx) == CXt_FORMAT)
401 { 428 {
402 /* I never used formats, so how should I know how these are implemented? */ 429 /* I never used formats, so how should I know how these are implemented? */
403 /* my bold guess is as a simple, plain sub... */ 430 /* my bold guess is as a simple, plain sub... */
404 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 431 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
405 } 432 }
433#endif
406 } 434 }
407 while (cxix >= 0);
408 435
409 if (top_si->si_type == PERLSI_MAIN) 436 if (top_si->si_type == PERLSI_MAIN)
410 break; 437 break;
411 438
412 top_si = top_si->si_prev; 439 top_si = top_si->si_prev;
419 446
420 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 447 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
421 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 448 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
422 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 449 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
423 450
424 /* I have not the slightest idea of why av_reify is necessary */
425 /* but if it's missing the defav contents magically get replaced sometimes */
426 if (c->defav)
427 av_reify (c->defav);
428
429 c->dowarn = PL_dowarn; 451 c->dowarn = PL_dowarn;
452 c->in_eval = PL_in_eval;
430 453
431 c->curstackinfo = PL_curstackinfo; 454 c->curstackinfo = PL_curstackinfo;
432 c->curstack = PL_curstack; 455 c->curstack = PL_curstack;
433 c->mainstack = PL_mainstack; 456 c->mainstack = PL_mainstack;
434 c->stack_sp = PL_stack_sp; 457 c->stack_sp = PL_stack_sp;
435 c->op = PL_op; 458 c->op = PL_op;
436 c->curpad = PL_curpad; 459 c->curpad = PL_curpad;
460 c->comppad = PL_comppad;
461 c->compcv = PL_compcv;
437 c->stack_base = PL_stack_base; 462 c->stack_base = PL_stack_base;
438 c->stack_max = PL_stack_max; 463 c->stack_max = PL_stack_max;
439 c->tmps_stack = PL_tmps_stack; 464 c->tmps_stack = PL_tmps_stack;
440 c->tmps_floor = PL_tmps_floor; 465 c->tmps_floor = PL_tmps_floor;
441 c->tmps_ix = PL_tmps_ix; 466 c->tmps_ix = PL_tmps_ix;
450 c->savestack_ix = PL_savestack_ix; 475 c->savestack_ix = PL_savestack_ix;
451 c->savestack_max = PL_savestack_max; 476 c->savestack_max = PL_savestack_max;
452 c->retstack = PL_retstack; 477 c->retstack = PL_retstack;
453 c->retstack_ix = PL_retstack_ix; 478 c->retstack_ix = PL_retstack_ix;
454 c->retstack_max = PL_retstack_max; 479 c->retstack_max = PL_retstack_max;
480 c->curpm = PL_curpm;
455 c->curcop = PL_curcop; 481 c->curcop = PL_curcop;
456 c->start_env = PL_start_env;
457 c->top_env = PL_top_env; 482 c->top_env = PL_top_env;
458} 483}
459 484
460/* 485/*
461 * allocate various perl stacks. This is an exact copy 486 * allocate various perl stacks. This is an exact copy
462 * of perl.c:init_stacks, except that it uses less memory 487 * of perl.c:init_stacks, except that it uses less memory
463 * on the assumption that coroutines do not usually need 488 * on the (sometimes correct) assumption that coroutines do
464 * a lot of stackspace. 489 * not usually need a lot of stackspace.
465 */ 490 */
466STATIC void 491STATIC void
467coro_init_stacks (pTHX) 492coro_init_stacks (pTHX)
468{ 493{
494 LOCK;
495
469 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 496 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
470 PL_curstackinfo->si_type = PERLSI_MAIN; 497 PL_curstackinfo->si_type = PERLSI_MAIN;
471 PL_curstack = PL_curstackinfo->si_stack; 498 PL_curstack = PL_curstackinfo->si_stack;
472 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 499 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
473 500
482 509
483 New(54,PL_markstack,16,I32); 510 New(54,PL_markstack,16,I32);
484 PL_markstack_ptr = PL_markstack; 511 PL_markstack_ptr = PL_markstack;
485 PL_markstack_max = PL_markstack + 16; 512 PL_markstack_max = PL_markstack + 16;
486 513
514#ifdef SET_MARK_OFFSET
487 SET_MARK_OFFSET; 515 SET_MARK_OFFSET;
516#endif
488 517
489 New(54,PL_scopestack,16,I32); 518 New(54,PL_scopestack,16,I32);
490 PL_scopestack_ix = 0; 519 PL_scopestack_ix = 0;
491 PL_scopestack_max = 16; 520 PL_scopestack_max = 16;
492 521
495 PL_savestack_max = 96; 524 PL_savestack_max = 96;
496 525
497 New(54,PL_retstack,8,OP*); 526 New(54,PL_retstack,8,OP*);
498 PL_retstack_ix = 0; 527 PL_retstack_ix = 0;
499 PL_retstack_max = 8; 528 PL_retstack_max = 8;
529
530 UNLOCK;
500} 531}
501 532
502/* 533/*
503 * destroy the stacks, the callchain etc... 534 * destroy the stacks, the callchain etc...
504 * still there is a memleak of 128 bytes...
505 */ 535 */
506STATIC void 536STATIC void
507destroy_stacks(pTHX) 537destroy_stacks(pTHX)
508{ 538{
539 if (!IN_DESTRUCT)
540 {
509 /* is this ugly, I ask? */ 541 /* is this ugly, I ask? */
510 while (PL_scopestack_ix) 542 LEAVE_SCOPE (0);
511 LEAVE;
512 543
513 /* sure it is, but more important: is it correct?? :/ */ 544 /* sure it is, but more important: is it correct?? :/ */
514 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
515 FREETMPS; 545 FREETMPS;
546
547 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
548 }
516 549
517 while (PL_curstackinfo->si_next) 550 while (PL_curstackinfo->si_next)
518 PL_curstackinfo = PL_curstackinfo->si_next; 551 PL_curstackinfo = PL_curstackinfo->si_next;
519 552
520 while (PL_curstackinfo) 553 while (PL_curstackinfo)
521 { 554 {
522 PERL_SI *p = PL_curstackinfo->si_prev; 555 PERL_SI *p = PL_curstackinfo->si_prev;
523 556
524 { 557 { /*D*//*remove*/
525 dSP; 558 dSP;
526 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 559 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
527 PUTBACK; /* possibly superfluous */ 560 PUTBACK; /* possibly superfluous */
528 } 561 }
529 562
530 dounwind(-1); 563 if (!IN_DESTRUCT)
531 564 {
565 dounwind (-1);/*D*//*remove*/
532 SvREFCNT_dec(PL_curstackinfo->si_stack); 566 SvREFCNT_dec (PL_curstackinfo->si_stack);
567 }
568
533 Safefree(PL_curstackinfo->si_cxstack); 569 Safefree (PL_curstackinfo->si_cxstack);
534 Safefree(PL_curstackinfo); 570 Safefree (PL_curstackinfo);
535 PL_curstackinfo = p; 571 PL_curstackinfo = p;
536 } 572 }
537 573
538 Safefree(PL_tmps_stack); 574 Safefree (PL_tmps_stack);
539 Safefree(PL_markstack); 575 Safefree (PL_markstack);
540 Safefree(PL_scopestack); 576 Safefree (PL_scopestack);
541 Safefree(PL_savestack); 577 Safefree (PL_savestack);
542 Safefree(PL_retstack); 578 Safefree (PL_retstack);
543} 579}
544 580
545static void 581static void
546allocate_stack (Coro__State ctx, int alloc) 582allocate_stack (Coro__State ctx, int alloc)
547{ 583{
550 New (0, stack, 1, coro_stack); 586 New (0, stack, 1, coro_stack);
551 587
552 stack->refcnt = 1; 588 stack->refcnt = 1;
553 stack->usecnt = 1; 589 stack->usecnt = 1;
554 stack->gencnt = ctx->gencnt = 0; 590 stack->gencnt = ctx->gencnt = 0;
591
555 if (alloc) 592 if (alloc)
556 { 593 {
557#ifdef HAVE_MMAP 594#if HAVE_MMAP
558 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write */ 595 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */
559 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 596 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
560 if (stack->sptr == (void *)-1) 597 if (stack->sptr == (void *)-1)
561#endif 598#endif
562 { 599 {
563 /*FIXME*//*D*//* reasonable stack size! */ 600 /*FIXME*//*D*//* reasonable stack size! */
564 stack->ssize = -4096 * sizeof (long); 601 stack->ssize = - (STACKSIZE * sizeof (long));
565 New (0, stack->sptr, 4096, long); 602 New (0, stack->sptr, STACKSIZE, long);
566 } 603 }
567 } 604 }
568 else 605 else
569 stack->sptr = 0; 606 stack->sptr = 0;
570 607
584 { 621 {
585#ifdef HAVE_MMAP 622#ifdef HAVE_MMAP
586 if (stack->ssize > 0 && stack->sptr) 623 if (stack->ssize > 0 && stack->sptr)
587 munmap (stack->sptr, stack->ssize); 624 munmap (stack->sptr, stack->ssize);
588 else 625 else
589#else 626#endif
590 Safefree (stack->sptr); 627 Safefree (stack->sptr);
591#endif 628
592 Safefree (stack); 629 Safefree (stack);
593 } 630 }
594 else if (ctx->gencnt == stack->gencnt) 631 else if (ctx->gencnt == stack->gencnt)
595 --stack->usecnt; 632 --stack->usecnt;
596 } 633 }
600setup_coro (void *arg) 637setup_coro (void *arg)
601{ 638{
602 /* 639 /*
603 * emulate part of the perl startup here. 640 * emulate part of the perl startup here.
604 */ 641 */
642 dTHX;
605 dSP; 643 dSP;
606 Coro__State ctx = (Coro__State)arg; 644 Coro__State ctx = (Coro__State)arg;
607 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 645 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
608 646
609 coro_init_stacks (aTHX); 647 coro_init_stacks (aTHX);
610 JMPENV_BOOTSTRAP; 648 /*PL_curcop = 0;*/
649 /*PL_in_eval = PL_in_eval;*/ /* inherit */
650 SvREFCNT_dec (GvAV (PL_defgv));
651 GvAV (PL_defgv) = ctx->args; ctx->args = 0;
652
611 SPAGAIN; 653 SPAGAIN;
612
613 /*PL_curcop = 0;*/
614 SvREFCNT_dec (GvAV (PL_defgv));
615 GvAV (PL_defgv) = ctx->args;
616 654
617 if (ctx->stack) 655 if (ctx->stack)
618 { 656 {
619 ctx->cursp = 0; 657 ctx->cursp = 0;
620 658
621 PUSHMARK(SP); 659 PUSHMARK(SP);
622 PUTBACK; 660 PUTBACK;
623 (void) call_sv (sub_init, G_VOID|G_NOARGS); 661 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
662
663 if (SvTRUE (ERRSV))
664 croak (NULL);
665 else
624 croak ("FATAL: CCTXT coroutine returned!"); 666 croak ("FATAL: CCTXT coroutine returned!");
625 } 667 }
626 else 668 else
627 { 669 {
628 UNOP myop; 670 UNOP myop;
629 671
641 * that doesn't matter, though, since it is only 683 * that doesn't matter, though, since it is only
642 * pp_nextstate and we never return... 684 * pp_nextstate and we never return...
643 * ah yes, and I don't care anyways ;) 685 * ah yes, and I don't care anyways ;)
644 */ 686 */
645 PUTBACK; 687 PUTBACK;
646 PL_op = pp_entersub(); 688 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
647 SPAGAIN; 689 SPAGAIN;
648 690
649 ENTER; /* necessary e.g. for dounwind */ 691 ENTER; /* necessary e.g. for dounwind */
650 } 692 }
651} 693}
654continue_coro (void *arg) 696continue_coro (void *arg)
655{ 697{
656 /* 698 /*
657 * this is a _very_ stripped down perl interpreter ;) 699 * this is a _very_ stripped down perl interpreter ;)
658 */ 700 */
701 dTHX;
659 Coro__State ctx = (Coro__State)arg; 702 Coro__State ctx = (Coro__State)arg;
703 JMPENV coro_start_env;
704
705 PL_top_env = &ctx->start_env;
660 706
661 ctx->cursp = 0; 707 ctx->cursp = 0;
662 PL_op = PL_op->op_next; 708 PL_op = PL_op->op_next;
663 CALLRUNOPS(aTHX); 709 CALLRUNOPS(aTHX);
664 /*NORETURN*/ 710
665 abort (); 711 abort ();
666} 712}
667 713
668STATIC void 714STATIC void
669transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 715transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
670{ 716{
671 dSP;
672 dSTACKLEVEL; 717 dSTACKLEVEL;
673 718
674 if (prev != next) 719 if (prev != next)
675 { 720 {
676 if (next->mainstack) 721 if (next->mainstack)
677 { 722 {
723 LOCK;
678 SAVE (prev, flags); 724 SAVE (prev, flags);
679 LOAD (next); 725 LOAD (next);
726 UNLOCK;
680 727
681 /* mark this state as in-use */ 728 /* mark this state as in-use */
682 next->mainstack = 0; 729 next->mainstack = 0;
683 next->tmps_ix = -2; 730 next->tmps_ix = -2;
684 731
703 continue_coro, (void *)next, 750 continue_coro, (void *)next,
704 next->stack->sptr, labs (next->stack->ssize)); 751 next->stack->sptr, labs (next->stack->ssize));
705 } 752 }
706 753
707 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 754 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
755 prev->cursp = stacklevel;
756 /* don't add any code here */
708 } 757 }
709 758 else
759 next->cursp = stacklevel;
710 } 760 }
711 else if (next->tmps_ix == -2) 761 else if (next->tmps_ix == -2)
712 croak ("tried to transfer to running coroutine"); 762 croak ("tried to transfer to running coroutine");
713 else 763 else
714 { 764 {
765 LOCK;
715 SAVE (prev, -1); /* first get rid of the old state */ 766 SAVE (prev, -1); /* first get rid of the old state */
767 UNLOCK;
716 768
717 if (flags & TRANSFER_SAVE_CCTXT) 769 if (flags & TRANSFER_SAVE_CCTXT)
718 { 770 {
719 if (!prev->stack) 771 if (!prev->stack)
720 allocate_stack (prev, 0); 772 allocate_stack (prev, 0);
721 773
722 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 774 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
723 { 775 {
776 PL_top_env = &next->start_env;
777
724 setup_coro (next); 778 setup_coro (next);
779 next->cursp = stacklevel;
725 780
726 prev->stack->refcnt++; 781 prev->stack->refcnt++;
727 prev->stack->usecnt++; 782 prev->stack->usecnt++;
728 next->stack = prev->stack; 783 next->stack = prev->stack;
729 next->gencnt = prev->gencnt; 784 next->gencnt = prev->gencnt;
730 } 785 }
731 else 786 else
732 { 787 {
788 assert (!next->stack);
733 allocate_stack (next, 1); 789 allocate_stack (next, 1);
734 coro_create (&(next->stack->cctx), 790 coro_create (&(next->stack->cctx),
735 setup_coro, (void *)next, 791 setup_coro, (void *)next,
736 next->stack->sptr, labs (next->stack->ssize)); 792 next->stack->sptr, labs (next->stack->ssize));
737 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 793 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
794 prev->cursp = stacklevel;
795 /* don't add any code here */
738 } 796 }
739 } 797 }
740 else 798 else
799 {
741 setup_coro (next); 800 setup_coro (next);
801 next->cursp = stacklevel;
802 }
742 } 803 }
743 } 804 }
744 805
745 next->cursp = stacklevel; 806 LOCK;
807 if (coro_mortal)
808 {
809 SvREFCNT_dec (coro_mortal);
810 coro_mortal = 0;
811 }
812 UNLOCK;
813}
814
815#define SV_CORO(sv,func) \
816 do { \
817 if (SvROK (sv)) \
818 sv = SvRV (sv); \
819 \
820 if (SvTYPE (sv) == SVt_PVHV) \
821 { \
822 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
823 \
824 if (!he) \
825 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
826 \
827 (sv) = SvRV (HeVAL(he)); \
828 } \
829 \
830 /* must also be changed inside Coro::Cont::yield */ \
831 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
832 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
833 \
834 } while(0)
835
836#define SvSTATE(sv) (struct coro *)SvIV (sv)
837
838static void
839api_transfer(pTHX_ SV *prev, SV *next, int flags)
840{
841 SV_CORO (prev, "Coro::transfer");
842 SV_CORO (next, "Coro::transfer");
843
844 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
845}
846
847/** Coro ********************************************************************/
848
849#define PRIO_MAX 3
850#define PRIO_HIGH 1
851#define PRIO_NORMAL 0
852#define PRIO_LOW -1
853#define PRIO_IDLE -3
854#define PRIO_MIN -4
855
856/* for Coro.pm */
857static GV *coro_current, *coro_idle;
858static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
859static int coro_nready;
860
861static void
862coro_enq (pTHX_ SV *sv)
863{
864 if (SvTYPE (sv) == SVt_PVHV)
865 {
866 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
867 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
868
869 prio = prio > PRIO_MAX ? PRIO_MAX
870 : prio < PRIO_MIN ? PRIO_MIN
871 : prio;
872
873 av_push (coro_ready [prio - PRIO_MIN], sv);
874 coro_nready++;
875
876 return;
877 }
878
879 croak ("Coro::ready tried to enqueue something that is not a coroutine");
880}
881
882static SV *
883coro_deq (pTHX_ int min_prio)
884{
885 int prio = PRIO_MAX - PRIO_MIN;
886
887 min_prio -= PRIO_MIN;
888 if (min_prio < 0)
889 min_prio = 0;
890
891 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
892 if (av_len (coro_ready[prio]) >= 0)
893 {
894 coro_nready--;
895 return av_shift (coro_ready[prio]);
896 }
897
898 return 0;
899}
900
901static void
902api_ready (SV *coro)
903{
904 dTHX;
905
906 if (SvROK (coro))
907 coro = SvRV (coro);
908
909 LOCK;
910 coro_enq (aTHX_ SvREFCNT_inc (coro));
911 UNLOCK;
912}
913
914static void
915api_schedule (void)
916{
917 dTHX;
918
919 SV *prev, *next;
920
921 LOCK;
922
923 prev = SvRV (GvSV (coro_current));
924 next = coro_deq (aTHX_ PRIO_MIN);
925
926 if (!next)
927 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
928
929 /* free this only after the transfer */
930 coro_mortal = prev;
931 SV_CORO (prev, "Coro::schedule");
932
933 SvRV (GvSV (coro_current)) = next;
934
935 SV_CORO (next, "Coro::schedule");
936
937 UNLOCK;
938
939 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
940 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
941}
942
943static void
944api_cede (void)
945{
946 dTHX;
947
948 LOCK;
949 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
950 UNLOCK;
951
952 api_schedule ();
746} 953}
747 954
748MODULE = Coro::State PACKAGE = Coro::State 955MODULE = Coro::State PACKAGE = Coro::State
749 956
750PROTOTYPES: ENABLE 957PROTOTYPES: ENABLE
751 958
752BOOT: 959BOOT:
753{ /* {} necessary for stoopid perl-5.6.x */ 960{ /* {} necessary for stoopid perl-5.6.x */
961#ifdef USE_ITHREADS
962 MUTEX_INIT (&coro_mutex);
963#endif
964
754 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 965 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
755 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 966 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
756 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 967 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
757 968
758 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 969 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
759 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 970 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
760 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 971 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
761 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 972 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
762 973
763 if (!padlist_cache)
764 padlist_cache = newHV ();
765
766 main_mainstack = PL_mainstack; 974 main_mainstack = PL_mainstack;
975
976 coroapi.ver = CORO_API_VERSION;
977 coroapi.transfer = api_transfer;
767} 978}
768 979
769Coro::State 980Coro::State
770_newprocess(args) 981_newprocess(args)
771 SV * args 982 SV * args
774 Coro__State coro; 985 Coro__State coro;
775 986
776 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 987 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
777 croak ("Coro::State::_newprocess expects an arrayref"); 988 croak ("Coro::State::_newprocess expects an arrayref");
778 989
779 New (0, coro, 1, struct coro); 990 Newz (0, coro, 1, struct coro);
780 991
781 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 992 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
782 coro->mainstack = 0; /* actual work is done inside transfer */ 993 /*coro->mainstack = 0; *//*actual work is done inside transfer */
783 coro->stack = 0; 994 /*coro->stack = 0;*/
995
996 /* same as JMPENV_BOOTSTRAP */
997 /* we might be able to recycle start_env, but safe is safe */
998 /*Zero(&coro->start_env, 1, JMPENV);*/
999 coro->start_env.je_ret = -1;
1000 coro->start_env.je_mustcatch = TRUE;
784 1001
785 RETVAL = coro; 1002 RETVAL = coro;
786 OUTPUT: 1003 OUTPUT:
787 RETVAL 1004 RETVAL
788 1005
789void 1006void
790transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) 1007transfer(prev, next, flags)
791 Coro::State_or_hashref prev 1008 SV *prev
792 Coro::State_or_hashref next 1009 SV *next
793 int flags 1010 int flags
794 PROTOTYPE: @ 1011 PROTOTYPE: @
795 CODE: 1012 CODE:
1013 PUTBACK;
1014 SV_CORO (next, "Coro::transfer");
1015 SV_CORO (prev, "Coro::transfer");
796 transfer (aTHX_ prev, next, flags); 1016 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1017 SPAGAIN;
797 1018
798void 1019void
799DESTROY(coro) 1020DESTROY(coro)
800 Coro::State coro 1021 Coro::State coro
801 CODE: 1022 CODE:
802 1023
803 if (coro->mainstack && coro->mainstack != main_mainstack) 1024 if (coro->mainstack && coro->mainstack != main_mainstack)
804 { 1025 {
805 struct coro temp; 1026 struct coro temp;
806 1027
1028 PUTBACK;
807 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1029 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
808 LOAD(aTHX_ coro); 1030 LOAD (aTHX_ coro);
1031 SPAGAIN;
809 1032
810 destroy_stacks (aTHX); 1033 destroy_stacks (aTHX);
811 1034
812 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1035 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1036 SPAGAIN;
813 1037
814 coro->mainstack = 0; 1038 coro->mainstack = 0;
815 } 1039 }
816 1040
817 deallocate_stack (coro); 1041 deallocate_stack (coro);
818 1042 SvREFCNT_dec (coro->args);
819 Safefree (coro); 1043 Safefree (coro);
820 1044
821void 1045void
822flush() 1046_exit(code)
1047 int code
1048 PROTOTYPE: $
823 CODE: 1049 CODE:
824#ifdef MAY_FLUSH 1050 _exit (code);
825 flush_padlist_cache ();
826#endif
827 1051
828MODULE = Coro::State PACKAGE = Coro::Cont 1052MODULE = Coro::State PACKAGE = Coro::Cont
829 1053
830# this is slightly dirty 1054# this is slightly dirty (should expose a c-level api)
831 1055
832void 1056void
833yield(...) 1057yield(...)
834 PROTOTYPE: @ 1058 PROTOTYPE: @
835 CODE: 1059 CODE:
836 static SV *returnstk; 1060 SV *yieldstack;
837 SV *sv; 1061 SV *sv;
838 AV *defav = GvAV (PL_defgv); 1062 AV *defav = GvAV (PL_defgv);
839 struct coro *prev, *next; 1063 struct coro *prev, *next;
840 1064
841 if (!returnstk) 1065 yieldstack = *hv_fetch (
842 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 1066 (HV *)SvRV (GvSV (coro_current)),
1067 "yieldstack", sizeof ("yieldstack") - 1,
1068 0
1069 );
843 1070
844 /* set up @_ -- ugly */ 1071 /* set up @_ -- ugly */
845 av_clear (defav); 1072 av_clear (defav);
846 av_fill (defav, items - 1); 1073 av_fill (defav, items - 1);
847 while (items--) 1074 while (items--)
848 av_store (defav, items, SvREFCNT_inc (ST(items))); 1075 av_store (defav, items, SvREFCNT_inc (ST(items)));
849 1076
850 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
851 sv = av_pop ((AV *)SvRV (returnstk)); 1077 sv = av_pop ((AV *)SvRV (yieldstack));
852 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1078 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
853 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1079 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
854 SvREFCNT_dec (sv); 1080 SvREFCNT_dec (sv);
855 1081
856 transfer(aTHX_ prev, next, 0); 1082 transfer (aTHX_ prev, next, 0);
857 1083
1084MODULE = Coro::State PACKAGE = Coro
1085
1086# this is slightly dirty (should expose a c-level api)
1087
1088BOOT:
1089{
1090 int i;
1091 HV *stash = gv_stashpv ("Coro", TRUE);
1092
1093 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
1094 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1095 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1096 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
1097 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1098 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1099
1100 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1101 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1102
1103 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1104 coro_ready[i] = newAV ();
1105
1106 {
1107 SV *sv = perl_get_sv("Coro::API", 1);
1108
1109 coroapi.schedule = api_schedule;
1110 coroapi.cede = api_cede;
1111 coroapi.ready = api_ready;
1112 coroapi.nready = &coro_nready;
1113 coroapi.current = coro_current;
1114
1115 GCoroAPI = &coroapi;
1116 sv_setiv(sv, (IV)&coroapi);
1117 SvREADONLY_on(sv);
1118 }
1119}
1120
1121#if !PERL_MICRO
1122
1123void
1124ready(self)
1125 SV * self
1126 PROTOTYPE: $
1127 CODE:
1128 api_ready (self);
1129
1130#endif
1131
1132int
1133nready(...)
1134 PROTOTYPE:
1135 CODE:
1136 RETVAL = coro_nready;
1137 OUTPUT:
1138 RETVAL
1139
1140void
1141schedule(...)
1142 PROTOTYPE:
1143 CODE:
1144 api_schedule ();
1145
1146void
1147cede(...)
1148 PROTOTYPE:
1149 CODE:
1150 api_cede ();
1151
1152# and these are hacks
1153SV *
1154_aio_get_state ()
1155 CODE:
1156{
1157 struct {
1158 int errorno;
1159 int laststype;
1160 int laststatval;
1161 Stat_t statcache;
1162 } data;
1163
1164 data.errorno = errno;
1165 data.laststype = PL_laststype;
1166 data.laststatval = PL_laststatval;
1167 data.statcache = PL_statcache;
1168
1169 RETVAL = newSVpvn ((char *)&data, sizeof data);
1170}
1171 OUTPUT:
1172 RETVAL
1173
1174void
1175_aio_set_state (char *data_)
1176 PROTOTYPE: $
1177 CODE:
1178{
1179 struct {
1180 int errorno;
1181 int laststype;
1182 int laststatval;
1183 Stat_t statcache;
1184 } *data = (void *)data_;
1185
1186 errno = data->errorno;
1187 PL_laststype = data->laststype;
1188 PL_laststatval = data->laststatval;
1189 PL_statcache = data->statcache;
1190}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines