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.141 by root, Mon Jan 22 18:45:17 2007 UTC vs.
Revision 1.158 by root, Fri Sep 21 02:23:48 2007 UTC

1#include "libcoro/coro.c" 1#include "libcoro/coro.c"
2
3#define PERL_NO_GET_CONTEXT
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
31#else 33#else
32# define PAGESIZE 0 34# define PAGESIZE 0
33# define BOOT_PAGESIZE (void)0 35# define BOOT_PAGESIZE (void)0
34#endif 36#endif
35 37
36#if USE_VALGRIND 38#if CORO_USE_VALGRIND
37# include <valgrind/valgrind.h> 39# include <valgrind/valgrind.h>
38# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) 40# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
39#else 41#else
40# define REGISTER_STACK(cctx,start,end) 42# define REGISTER_STACK(cctx,start,end)
41#endif 43#endif
74#ifndef SvRV_set 76#ifndef SvRV_set
75# define SvRV_set(s,v) SvRV(s) = (v) 77# define SvRV_set(s,v) SvRV(s) = (v)
76#endif 78#endif
77 79
78#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 80#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
79# undef STACKGUARD 81# undef CORO_STACKGUARD
80#endif 82#endif
81 83
82#ifndef STACKGUARD 84#ifndef CORO_STACKGUARD
83# define STACKGUARD 0 85# define CORO_STACKGUARD 0
84#endif 86#endif
85 87
86/* prefer perl internal functions over our own? */ 88/* prefer perl internal functions over our own? */
87#ifndef PREFER_PERL_FUNCTIONS 89#ifndef CORO_PREFER_PERL_FUNCTIONS
88# define PREFER_PERL_FUNCTIONS 0 90# define CORO_PREFER_PERL_FUNCTIONS 0
89#endif 91#endif
90 92
91/* The next macro should declare a variable stacklevel that contains and approximation 93/* The next macros try to return the current stack pointer, in an as
92 * to the current C stack pointer. Its property is that it changes with each call 94 * portable way as possible. */
93 * and should be unique. */
94#define dSTACKLEVEL int stacklevel 95#define dSTACKLEVEL volatile char stacklevel
95#define STACKLEVEL ((void *)&stacklevel) 96#define STACKLEVEL ((void *)&stacklevel)
96 97
97#define IN_DESTRUCT (PL_main_cv == Nullcv) 98#define IN_DESTRUCT (PL_main_cv == Nullcv)
98 99
99#if __GNUC__ >= 3 100#if __GNUC__ >= 3
124 I32 laststype; 125 I32 laststype;
125 int laststatval; 126 int laststatval;
126 Stat_t statcache; 127 Stat_t statcache;
127}; 128};
128 129
130static size_t coro_stacksize = CORO_STACKSIZE;
129static struct CoroAPI coroapi; 131static struct CoroAPI coroapi;
130static AV *main_mainstack; /* used to differentiate between $main and others */ 132static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env;
131static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
132static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
133 136
134static struct coro_cctx *cctx_first; 137static struct coro_cctx *cctx_first;
135static int cctx_count, cctx_idle; 138static int cctx_count, cctx_idle;
138typedef struct coro_cctx { 141typedef struct coro_cctx {
139 struct coro_cctx *next; 142 struct coro_cctx *next;
140 143
141 /* the stack */ 144 /* the stack */
142 void *sptr; 145 void *sptr;
143 ssize_t ssize; /* positive == mmap, otherwise malloc */ 146 size_t ssize;
144 147
145 /* cpu state */ 148 /* cpu state */
146 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 149 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
147 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 150 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
148 JMPENV *top_env; 151 JMPENV *top_env;
149 coro_context cctx; 152 coro_context cctx;
150 153
151 int inuse;
152
153#if USE_VALGRIND 154#if CORO_USE_VALGRIND
154 int valgrind_id; 155 int valgrind_id;
155#endif 156#endif
157 char inuse, mapped;
156} coro_cctx; 158} coro_cctx;
157 159
158enum { 160enum {
159 CF_RUNNING = 0x0001, /* coroutine is running */ 161 CF_RUNNING = 0x0001, /* coroutine is running */
160 CF_READY = 0x0002, /* coroutine is ready */ 162 CF_READY = 0x0002, /* coroutine is ready */
175 177
176 /* optionally saved, might be zero */ 178 /* optionally saved, might be zero */
177 AV *defav; /* @_ */ 179 AV *defav; /* @_ */
178 SV *defsv; /* $_ */ 180 SV *defsv; /* $_ */
179 SV *errsv; /* $@ */ 181 SV *errsv; /* $@ */
182 GV *deffh; /* default filehandle */
180 SV *irssv; /* $/ */ 183 SV *irssv; /* $/ */
181 SV *irssv_sv; /* real $/ cache */ 184 SV *irssv_sv; /* real $/ cache */
182 185
183#define VAR(name,type) type name; 186#define VAR(name,type) type name;
184# include "state.h" 187# include "state.h"
185#undef VAR 188#undef VAR
186 189
187 /* coro process data */ 190 /* coro process data */
188 int prio; 191 int prio;
192
193 /* linked list */
194 struct coro *next, *prev;
195 HV *hv; /* the perl hash associated with this coro, if any */
189}; 196};
190 197
191typedef struct coro *Coro__State; 198typedef struct coro *Coro__State;
192typedef struct coro *Coro__State_or_hashref; 199typedef struct coro *Coro__State_or_hashref;
193 200
202 209
203/* for Coro.pm */ 210/* for Coro.pm */
204static SV *coro_current; 211static SV *coro_current;
205static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 212static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
206static int coro_nready; 213static int coro_nready;
214static struct coro *coro_first;
207 215
208/** lowlevel stuff **********************************************************/ 216/** lowlevel stuff **********************************************************/
209 217
210static AV * 218static AV *
211coro_clone_padlist (CV *cv) 219coro_clone_padlist (pTHX_ CV *cv)
212{ 220{
213 AV *padlist = CvPADLIST (cv); 221 AV *padlist = CvPADLIST (cv);
214 AV *newpadlist, *newpad; 222 AV *newpadlist, *newpad;
215 223
216 newpadlist = newAV (); 224 newpadlist = newAV ();
228 236
229 return newpadlist; 237 return newpadlist;
230} 238}
231 239
232static void 240static void
233free_padlist (AV *padlist) 241free_padlist (pTHX_ AV *padlist)
234{ 242{
235 /* may be during global destruction */ 243 /* may be during global destruction */
236 if (SvREFCNT (padlist)) 244 if (SvREFCNT (padlist))
237 { 245 {
238 I32 i = AvFILLp (padlist); 246 I32 i = AvFILLp (padlist);
259 AV *padlist; 267 AV *padlist;
260 AV *av = (AV *)mg->mg_obj; 268 AV *av = (AV *)mg->mg_obj;
261 269
262 /* casting is fun. */ 270 /* casting is fun. */
263 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 271 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
264 free_padlist (padlist); 272 free_padlist (aTHX_ padlist);
265 273
266 SvREFCNT_dec (av); 274 SvREFCNT_dec (av);
267 275
268 return 0; 276 return 0;
269} 277}
279 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 287 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
280 : 0 288 : 0
281 289
282/* the next two functions merely cache the padlists */ 290/* the next two functions merely cache the padlists */
283static void 291static void
284get_padlist (CV *cv) 292get_padlist (pTHX_ CV *cv)
285{ 293{
286 MAGIC *mg = CORO_MAGIC (cv); 294 MAGIC *mg = CORO_MAGIC (cv);
287 AV *av; 295 AV *av;
288 296
289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 297 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
290 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 298 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
291 else 299 else
292 { 300 {
293#if PREFER_PERL_FUNCTIONS 301#if CORO_PREFER_PERL_FUNCTIONS
294 /* this is probably cleaner, but also slower? */ 302 /* this is probably cleaner, but also slower? */
295 CV *cp = Perl_cv_clone (cv); 303 CV *cp = Perl_cv_clone (cv);
296 CvPADLIST (cv) = CvPADLIST (cp); 304 CvPADLIST (cv) = CvPADLIST (cp);
297 CvPADLIST (cp) = 0; 305 CvPADLIST (cp) = 0;
298 SvREFCNT_dec (cp); 306 SvREFCNT_dec (cp);
299#else 307#else
300 CvPADLIST (cv) = coro_clone_padlist (cv); 308 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
301#endif 309#endif
302 } 310 }
303} 311}
304 312
305static void 313static void
306put_padlist (CV *cv) 314put_padlist (pTHX_ CV *cv)
307{ 315{
308 MAGIC *mg = CORO_MAGIC (cv); 316 MAGIC *mg = CORO_MAGIC (cv);
309 AV *av; 317 AV *av;
310 318
311 if (!mg) 319 if (!mg)
330#define SE } while (0) 338#define SE } while (0)
331 339
332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 340#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
333 341
334static void 342static void
335load_perl (Coro__State c) 343load_perl (pTHX_ Coro__State c)
336{ 344{
337#define VAR(name,type) PL_ ## name = c->name; 345#define VAR(name,type) PL_ ## name = c->name;
338# include "state.h" 346# include "state.h"
339#undef VAR 347#undef VAR
340 348
341 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 349 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
342 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 350 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
343 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 351 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
352 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh);
353
344 if (c->irssv) 354 if (c->irssv)
345 { 355 {
346 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) 356 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
357 {
347 SvREFCNT_dec (c->irssv); 358 SvREFCNT_dec (c->irssv);
359 c->irssv = 0;
360 }
348 else 361 else
349 { 362 {
350 REPLACE_SV (PL_rs, c->irssv); 363 REPLACE_SV (PL_rs, c->irssv);
351 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); 364 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
352 sv_setsv (c->irssv_sv, PL_rs); 365 sv_setsv (c->irssv_sv, PL_rs);
358 CV *cv; 371 CV *cv;
359 372
360 /* now do the ugly restore mess */ 373 /* now do the ugly restore mess */
361 while ((cv = (CV *)POPs)) 374 while ((cv = (CV *)POPs))
362 { 375 {
363 put_padlist (cv); /* mark this padlist as available */ 376 put_padlist (aTHX_ cv); /* mark this padlist as available */
364 CvDEPTH (cv) = PTR2IV (POPs); 377 CvDEPTH (cv) = PTR2IV (POPs);
365 CvPADLIST (cv) = (AV *)POPs; 378 CvPADLIST (cv) = (AV *)POPs;
366 } 379 }
367 380
368 PUTBACK; 381 PUTBACK;
369 } 382 }
370} 383}
371 384
372static void 385static void
373save_perl (Coro__State c) 386save_perl (pTHX_ Coro__State c)
374{ 387{
375 { 388 {
376 dSP; 389 dSP;
377 I32 cxix = cxstack_ix; 390 I32 cxix = cxstack_ix;
378 PERL_CONTEXT *ccstk = cxstack; 391 PERL_CONTEXT *ccstk = cxstack;
381 /* 394 /*
382 * the worst thing you can imagine happens first - we have to save 395 * the worst thing you can imagine happens first - we have to save
383 * (and reinitialize) all cv's in the whole callchain :( 396 * (and reinitialize) all cv's in the whole callchain :(
384 */ 397 */
385 398
386 EXTEND (SP, 3 + 1);
387 PUSHs (Nullsv); 399 XPUSHs (Nullsv);
388 /* this loop was inspired by pp_caller */ 400 /* this loop was inspired by pp_caller */
389 for (;;) 401 for (;;)
390 { 402 {
391 while (cxix >= 0) 403 while (cxix >= 0)
392 { 404 {
393 PERL_CONTEXT *cx = &ccstk[cxix--]; 405 PERL_CONTEXT *cx = &ccstk[cxix--];
394 406
395 if (CxTYPE (cx) == CXt_SUB) 407 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
396 { 408 {
397 CV *cv = cx->blk_sub.cv; 409 CV *cv = cx->blk_sub.cv;
398 410
399 if (CvDEPTH (cv)) 411 if (CvDEPTH (cv))
400 { 412 {
402 PUSHs ((SV *)CvPADLIST (cv)); 414 PUSHs ((SV *)CvPADLIST (cv));
403 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 415 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
404 PUSHs ((SV *)cv); 416 PUSHs ((SV *)cv);
405 417
406 CvDEPTH (cv) = 0; 418 CvDEPTH (cv) = 0;
407 get_padlist (cv); 419 get_padlist (aTHX_ cv);
408 } 420 }
409 } 421 }
410 } 422 }
411 423
412 if (top_si->si_type == PERLSI_MAIN) 424 if (top_si->si_type == PERLSI_MAIN)
413 break; 425 break;
414 426
415 top_si = top_si->si_prev; 427 top_si = top_si->si_prev;
416 ccstk = top_si->si_cxstack; 428 ccstk = top_si->si_cxstack;
417 cxix = top_si->si_cxix; 429 cxix = top_si->si_cxix;
418 } 430 }
419 431
420 PUTBACK; 432 PUTBACK;
421 } 433 }
422 434
423 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 435 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
424 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 436 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
425 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 437 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
438 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0;
426 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 439 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
427 440
428#define VAR(name,type)c->name = PL_ ## name; 441#define VAR(name,type)c->name = PL_ ## name;
429# include "state.h" 442# include "state.h"
430#undef VAR 443#undef VAR
434 * allocate various perl stacks. This is an exact copy 447 * allocate various perl stacks. This is an exact copy
435 * of perl.c:init_stacks, except that it uses less memory 448 * of perl.c:init_stacks, except that it uses less memory
436 * on the (sometimes correct) assumption that coroutines do 449 * on the (sometimes correct) assumption that coroutines do
437 * not usually need a lot of stackspace. 450 * not usually need a lot of stackspace.
438 */ 451 */
439#if PREFER_PERL_FUNCTIONS 452#if CORO_PREFER_PERL_FUNCTIONS
440# define coro_init_stacks init_stacks 453# define coro_init_stacks init_stacks
441#else 454#else
442static void 455static void
443coro_init_stacks () 456coro_init_stacks (pTHX)
444{ 457{
445 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 458 PL_curstackinfo = new_stackinfo(64, 6);
446 PL_curstackinfo->si_type = PERLSI_MAIN; 459 PL_curstackinfo->si_type = PERLSI_MAIN;
447 PL_curstack = PL_curstackinfo->si_stack; 460 PL_curstack = PL_curstackinfo->si_stack;
448 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 461 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
449 462
450 PL_stack_base = AvARRAY(PL_curstack); 463 PL_stack_base = AvARRAY(PL_curstack);
451 PL_stack_sp = PL_stack_base; 464 PL_stack_sp = PL_stack_base;
452 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 465 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
453 466
454 New(50,PL_tmps_stack,128,SV*); 467 New(50,PL_tmps_stack,64,SV*);
455 PL_tmps_floor = -1; 468 PL_tmps_floor = -1;
456 PL_tmps_ix = -1; 469 PL_tmps_ix = -1;
457 PL_tmps_max = 128; 470 PL_tmps_max = 64;
458 471
459 New(54,PL_markstack,32,I32); 472 New(54,PL_markstack,16,I32);
460 PL_markstack_ptr = PL_markstack; 473 PL_markstack_ptr = PL_markstack;
461 PL_markstack_max = PL_markstack + 32; 474 PL_markstack_max = PL_markstack + 16;
462 475
463#ifdef SET_MARK_OFFSET 476#ifdef SET_MARK_OFFSET
464 SET_MARK_OFFSET; 477 SET_MARK_OFFSET;
465#endif 478#endif
466 479
467 New(54,PL_scopestack,32,I32); 480 New(54,PL_scopestack,16,I32);
468 PL_scopestack_ix = 0; 481 PL_scopestack_ix = 0;
469 PL_scopestack_max = 32; 482 PL_scopestack_max = 16;
470 483
471 New(54,PL_savestack,64,ANY); 484 New(54,PL_savestack,64,ANY);
472 PL_savestack_ix = 0; 485 PL_savestack_ix = 0;
473 PL_savestack_max = 64; 486 PL_savestack_max = 64;
474 487
475#if !PERL_VERSION_ATLEAST (5,9,0) 488#if !PERL_VERSION_ATLEAST (5,9,0)
476 New(54,PL_retstack,16,OP*); 489 New(54,PL_retstack,4,OP*);
477 PL_retstack_ix = 0; 490 PL_retstack_ix = 0;
478 PL_retstack_max = 16; 491 PL_retstack_max = 4;
479#endif 492#endif
480} 493}
481#endif 494#endif
482 495
483/* 496/*
484 * destroy the stacks, the callchain etc... 497 * destroy the stacks, the callchain etc...
485 */ 498 */
486static void 499static void
487coro_destroy_stacks () 500coro_destroy_stacks (pTHX)
488{ 501{
489 if (!IN_DESTRUCT) 502 if (!IN_DESTRUCT)
490 { 503 {
491 /* restore all saved variables and stuff */ 504 /* restore all saved variables and stuff */
492 LEAVE_SCOPE (0); 505 LEAVE_SCOPE (0);
525#if !PERL_VERSION_ATLEAST (5,9,0) 538#if !PERL_VERSION_ATLEAST (5,9,0)
526 Safefree (PL_retstack); 539 Safefree (PL_retstack);
527#endif 540#endif
528} 541}
529 542
543static size_t
544coro_rss (struct coro *coro)
545{
546 size_t rss = sizeof (coro);
547
548 if (coro->mainstack)
549 {
550 if (coro->flags & CF_RUNNING)
551 {
552 #define VAR(name,type)coro->name = PL_ ## name;
553 # include "state.h"
554 #undef VAR
555 }
556
557 rss += sizeof (coro->curstackinfo);
558 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
559 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
560 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
561 rss += coro->tmps_max * sizeof (SV *);
562 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
563 rss += coro->scopestack_max * sizeof (I32);
564 rss += coro->savestack_max * sizeof (ANY);
565
566#if !PERL_VERSION_ATLEAST (5,9,0)
567 rss += coro->retstack_max * sizeof (OP *);
568#endif
569 }
570
571 return rss;
572}
573
530/** coroutine stack handling ************************************************/ 574/** coroutine stack handling ************************************************/
531 575
532static void 576static void
533setup_coro (struct coro *coro) 577setup_coro (pTHX_ struct coro *coro)
534{ 578{
535 /* 579 /*
536 * emulate part of the perl startup here. 580 * emulate part of the perl startup here.
537 */ 581 */
538
539 coro_init_stacks (); 582 coro_init_stacks (aTHX);
540 583
541 PL_curcop = &PL_compiling; 584 PL_curcop = &PL_compiling;
542 PL_in_eval = EVAL_NULL; 585 PL_in_eval = EVAL_NULL;
586 PL_comppad = 0;
543 PL_curpm = 0; 587 PL_curpm = 0;
544 PL_localizing = 0; 588 PL_localizing = 0;
545 PL_dirty = 0; 589 PL_dirty = 0;
546 PL_restartop = 0; 590 PL_restartop = 0;
547 591
555 Zero (&myop, 1, LOGOP); 599 Zero (&myop, 1, LOGOP);
556 myop.op_next = Nullop; 600 myop.op_next = Nullop;
557 myop.op_flags = OPf_WANT_VOID; 601 myop.op_flags = OPf_WANT_VOID;
558 602
559 PUSHMARK (SP); 603 PUSHMARK (SP);
560 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 604 XPUSHs (av_shift (GvAV (PL_defgv)));
561 PUTBACK; 605 PUTBACK;
562 PL_op = (OP *)&myop; 606 PL_op = (OP *)&myop;
563 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 607 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
564 SPAGAIN; 608 SPAGAIN;
565 } 609 }
566 610
567 ENTER; /* necessary e.g. for dounwind */ 611 ENTER; /* necessary e.g. for dounwind */
568} 612}
569 613
570static void 614static void
571free_coro_mortal () 615free_coro_mortal (pTHX)
572{ 616{
573 if (coro_mortal) 617 if (coro_mortal)
574 { 618 {
575 SvREFCNT_dec (coro_mortal); 619 SvREFCNT_dec (coro_mortal);
576 coro_mortal = 0; 620 coro_mortal = 0;
577 } 621 }
578} 622}
579 623
580/* inject a fake call to Coro::State::_cctx_init into the execution */ 624/* inject a fake call to Coro::State::_cctx_init into the execution */
625/* _cctx_init should be careful, as it could be called at almost any time */
626/* during execution of a perl program */
581static void NOINLINE 627static void NOINLINE
582prepare_cctx (coro_cctx *cctx) 628prepare_cctx (pTHX_ coro_cctx *cctx)
583{ 629{
584 dSP; 630 dSP;
585 LOGOP myop; 631 LOGOP myop;
586 632
587 Zero (&myop, 1, LOGOP); 633 Zero (&myop, 1, LOGOP);
596 PL_op = (OP *)&myop; 642 PL_op = (OP *)&myop;
597 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 643 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
598 SPAGAIN; 644 SPAGAIN;
599} 645}
600 646
647/*
648 * this is a _very_ stripped down perl interpreter ;)
649 */
601static void 650static void
602coro_run (void *arg) 651coro_run (void *arg)
603{ 652{
653 dTHX;
654
604 /* coro_run is the alternative tail of transfer(), so unlock here. */ 655 /* coro_run is the alternative tail of transfer(), so unlock here. */
605 UNLOCK; 656 UNLOCK;
606 657
607 /*
608 * this is a _very_ stripped down perl interpreter ;)
609 */
610 PL_top_env = &PL_start_env; 658 PL_top_env = &PL_start_env;
611 659
612 /* inject call to cctx_init */ 660 /* inject a fake subroutine call to cctx_init */
613 prepare_cctx ((coro_cctx *)arg); 661 prepare_cctx (aTHX_ (coro_cctx *)arg);
614 662
615 /* somebody will hit me for both perl_run and PL_restartop */ 663 /* somebody or something will hit me for both perl_run and PL_restartop */
616 PL_restartop = PL_op; 664 PL_restartop = PL_op;
617 perl_run (PL_curinterp); 665 perl_run (PL_curinterp);
618 666
619 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 667 /*
620 abort (); 668 * If perl-run returns we assume exit() was being called or the coro
669 * fell off the end, which seems to be the only valid (non-bug)
670 * reason for perl_run to return. We try to exit by jumping to the
671 * bootstrap-time "top" top_env, as we cannot restore the "main"
672 * coroutine as Coro has no such concept
673 */
674 PL_top_env = main_top_env;
675 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
621} 676}
622 677
623static coro_cctx * 678static coro_cctx *
624cctx_new () 679cctx_new ()
625{ 680{
626 coro_cctx *cctx; 681 coro_cctx *cctx;
682 void *stack_start;
683 size_t stack_size;
627 684
628 ++cctx_count; 685 ++cctx_count;
629 686
630 Newz (0, cctx, 1, coro_cctx); 687 Newz (0, cctx, 1, coro_cctx);
631 688
632#if HAVE_MMAP 689#if HAVE_MMAP
633 690
634 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 691 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
635 /* mmap supposedly does allocate-on-write for us */ 692 /* mmap supposedly does allocate-on-write for us */
636 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 693 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
637 694
638 if (cctx->sptr != (void *)-1) 695 if (cctx->sptr != (void *)-1)
639 { 696 {
640# if STACKGUARD 697# if CORO_STACKGUARD
641 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 698 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
642# endif 699# endif
643 REGISTER_STACK (
644 cctx,
645 STACKGUARD * PAGESIZE + (char *)cctx->sptr, 700 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
646 cctx->ssize + (char *)cctx->sptr 701 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
647 ); 702 cctx->mapped = 1;
648
649 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
650 } 703 }
651 else 704 else
652#endif 705#endif
653 { 706 {
654 cctx->ssize = -STACKSIZE * (long)sizeof (long); 707 cctx->ssize = coro_stacksize * (long)sizeof (long);
655 New (0, cctx->sptr, STACKSIZE, long); 708 New (0, cctx->sptr, coro_stacksize, long);
656 709
657 if (!cctx->sptr) 710 if (!cctx->sptr)
658 { 711 {
659 perror ("FATAL: unable to allocate stack for coroutine"); 712 perror ("FATAL: unable to allocate stack for coroutine");
660 _exit (EXIT_FAILURE); 713 _exit (EXIT_FAILURE);
661 } 714 }
662 715
663 REGISTER_STACK ( 716 stack_start = cctx->sptr;
664 cctx, 717 stack_size = cctx->ssize;
665 (char *)cctx->sptr, 718 }
666 (char *)cctx->sptr - cctx->ssize
667 );
668 719
720 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
669 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize); 721 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size);
670 }
671 722
672 return cctx; 723 return cctx;
673} 724}
674 725
675static void 726static void
678 if (!cctx) 729 if (!cctx)
679 return; 730 return;
680 731
681 --cctx_count; 732 --cctx_count;
682 733
683#if USE_VALGRIND 734#if CORO_USE_VALGRIND
684 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 735 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
685#endif 736#endif
686 737
687#if HAVE_MMAP 738#if HAVE_MMAP
688 if (cctx->ssize > 0) 739 if (cctx->mapped)
689 munmap (cctx->sptr, cctx->ssize); 740 munmap (cctx->sptr, cctx->ssize);
690 else 741 else
691#endif 742#endif
692 Safefree (cctx->sptr); 743 Safefree (cctx->sptr);
693 744
694 Safefree (cctx); 745 Safefree (cctx);
695} 746}
696 747
697static coro_cctx * 748static coro_cctx *
698cctx_get () 749cctx_get (pTHX)
699{ 750{
700 coro_cctx *cctx;
701
702 if (cctx_first) 751 while (cctx_first)
703 { 752 {
704 cctx = cctx_first; 753 coro_cctx *cctx = cctx_first;
705 cctx_first = cctx->next; 754 cctx_first = cctx->next;
706 --cctx_idle; 755 --cctx_idle;
756
757 if (cctx->ssize >= coro_stacksize)
758 return cctx;
759
760 cctx_destroy (cctx);
707 } 761 }
708 else 762
709 {
710 cctx = cctx_new ();
711 PL_op = PL_op->op_next; 763 PL_op = PL_op->op_next;
712 }
713
714 return cctx; 764 return cctx_new ();
715} 765}
716 766
717static void 767static void
718cctx_put (coro_cctx *cctx) 768cctx_put (coro_cctx *cctx)
719{ 769{
735 785
736/** coroutine switching *****************************************************/ 786/** coroutine switching *****************************************************/
737 787
738/* never call directly, always through the coro_state_transfer global variable */ 788/* never call directly, always through the coro_state_transfer global variable */
739static void NOINLINE 789static void NOINLINE
740transfer (struct coro *prev, struct coro *next) 790transfer (pTHX_ struct coro *prev, struct coro *next)
741{ 791{
742 dSTACKLEVEL; 792 dSTACKLEVEL;
743 793
744 /* sometimes transfer is only called to set idle_sp */ 794 /* sometimes transfer is only called to set idle_sp */
745 if (!next) 795 if (!next)
778 if (next->flags & CF_NEW) 828 if (next->flags & CF_NEW)
779 { 829 {
780 /* need to start coroutine */ 830 /* need to start coroutine */
781 next->flags &= ~CF_NEW; 831 next->flags &= ~CF_NEW;
782 /* first get rid of the old state */ 832 /* first get rid of the old state */
783 save_perl (prev); 833 save_perl (aTHX_ prev);
784 /* setup coroutine call */ 834 /* setup coroutine call */
785 setup_coro (next); 835 setup_coro (aTHX_ next);
786 /* need a new stack */ 836 /* need a new stack */
787 assert (!next->cctx); 837 assert (!next->cctx);
788 } 838 }
789 else 839 else
790 { 840 {
791 /* coroutine already started */ 841 /* coroutine already started */
792 save_perl (prev); 842 save_perl (aTHX_ prev);
793 load_perl (next); 843 load_perl (aTHX_ next);
794 } 844 }
795 845
796 prev__cctx = prev->cctx; 846 prev__cctx = prev->cctx;
797 847
798 /* possibly "free" the cctx */ 848 /* possibly "free" the cctx */
807 prev__cctx->inuse = 0; 857 prev__cctx->inuse = 0;
808 } 858 }
809 859
810 if (!next->cctx) 860 if (!next->cctx)
811 { 861 {
812 next->cctx = cctx_get (); 862 next->cctx = cctx_get (aTHX);
813 assert (!next->cctx->inuse); 863 assert (!next->cctx->inuse);
814 next->cctx->inuse = 1; 864 next->cctx->inuse = 1;
815 } 865 }
816 866
817 if (prev__cctx != next->cctx) 867 if (prev__cctx != next->cctx)
819 prev__cctx->top_env = PL_top_env; 869 prev__cctx->top_env = PL_top_env;
820 PL_top_env = next->cctx->top_env; 870 PL_top_env = next->cctx->top_env;
821 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 871 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
822 } 872 }
823 873
824 free_coro_mortal (); 874 free_coro_mortal (aTHX);
825 UNLOCK; 875 UNLOCK;
826 } 876 }
827} 877}
828 878
829struct transfer_args 879struct transfer_args
830{ 880{
831 struct coro *prev, *next; 881 struct coro *prev, *next;
832}; 882};
833 883
834#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 884#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
835 885
836/** high level stuff ********************************************************/ 886/** high level stuff ********************************************************/
837 887
838static int 888static int
839coro_state_destroy (struct coro *coro) 889coro_state_destroy (pTHX_ struct coro *coro)
840{ 890{
841 if (coro->flags & CF_DESTROYED) 891 if (coro->flags & CF_DESTROYED)
842 return 0; 892 return 0;
843 893
844 coro->flags |= CF_DESTROYED; 894 coro->flags |= CF_DESTROYED;
864 temp.save = CORO_SAVE_ALL; 914 temp.save = CORO_SAVE_ALL;
865 915
866 if (coro->flags & CF_RUNNING) 916 if (coro->flags & CF_RUNNING)
867 croak ("FATAL: tried to destroy currently running coroutine"); 917 croak ("FATAL: tried to destroy currently running coroutine");
868 918
869 save_perl (&temp); 919 save_perl (aTHX_ &temp);
870 load_perl (coro); 920 load_perl (aTHX_ coro);
871 921
872 coro_destroy_stacks (); 922 coro_destroy_stacks (aTHX);
873 923
874 load_perl (&temp); /* this will get rid of defsv etc.. */ 924 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
875 925
876 coro->mainstack = 0; 926 coro->mainstack = 0;
877 } 927 }
878 928
879 cctx_destroy (coro->cctx); 929 cctx_destroy (coro->cctx);
880 SvREFCNT_dec (coro->args); 930 SvREFCNT_dec (coro->args);
881 931
932 if (coro->next) coro->next->prev = coro->prev;
933 if (coro->prev) coro->prev->next = coro->next;
934 if (coro == coro_first) coro_first = coro->next;
935
882 return 1; 936 return 1;
883} 937}
884 938
885static int 939static int
886coro_state_free (pTHX_ SV *sv, MAGIC *mg) 940coro_state_free (pTHX_ SV *sv, MAGIC *mg)
887{ 941{
888 struct coro *coro = (struct coro *)mg->mg_ptr; 942 struct coro *coro = (struct coro *)mg->mg_ptr;
889 mg->mg_ptr = 0; 943 mg->mg_ptr = 0;
890 944
945 coro->hv = 0;
946
891 if (--coro->refcnt < 0) 947 if (--coro->refcnt < 0)
892 { 948 {
893 coro_state_destroy (coro); 949 coro_state_destroy (aTHX_ coro);
894 Safefree (coro); 950 Safefree (coro);
895 } 951 }
896 952
897 return 0; 953 return 0;
898} 954}
917# define MGf_DUP 0 973# define MGf_DUP 0
918#endif 974#endif
919}; 975};
920 976
921static struct coro * 977static struct coro *
922SvSTATE (SV *coro) 978SvSTATE_ (pTHX_ SV *coro)
923{ 979{
924 HV *stash; 980 HV *stash;
925 MAGIC *mg; 981 MAGIC *mg;
926 982
927 if (SvROK (coro)) 983 if (SvROK (coro))
933 /* very slow, but rare, check */ 989 /* very slow, but rare, check */
934 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 990 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
935 croak ("Coro::State object required"); 991 croak ("Coro::State object required");
936 } 992 }
937 993
938 mg = SvMAGIC (coro); 994 mg = CORO_MAGIC (coro);
939 assert (mg->mg_type == PERL_MAGIC_ext);
940 return (struct coro *)mg->mg_ptr; 995 return (struct coro *)mg->mg_ptr;
941} 996}
942 997
998#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
999
943static void 1000static void
944prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1001prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
945{ 1002{
946 ta->prev = SvSTATE (prev_sv); 1003 ta->prev = SvSTATE (prev_sv);
947 ta->next = SvSTATE (next_sv); 1004 ta->next = SvSTATE (next_sv);
948} 1005}
949 1006
950static void 1007static void
951api_transfer (SV *prev_sv, SV *next_sv) 1008api_transfer (SV *prev_sv, SV *next_sv)
952{ 1009{
1010 dTHX;
953 struct transfer_args ta; 1011 struct transfer_args ta;
954 1012
955 prepare_transfer (&ta, prev_sv, next_sv); 1013 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
956 TRANSFER (ta); 1014 TRANSFER (ta);
957} 1015}
958 1016
959static int 1017static int
960api_save (SV *coro_sv, int new_save) 1018api_save (SV *coro_sv, int new_save)
961{ 1019{
1020 dTHX;
962 struct coro *coro = SvSTATE (coro_sv); 1021 struct coro *coro = SvSTATE (coro_sv);
963 int old_save = coro->save; 1022 int old_save = coro->save;
964 1023
965 if (new_save >= 0) 1024 if (new_save >= 0)
966 coro->save = new_save; 1025 coro->save = new_save;
969} 1028}
970 1029
971/** Coro ********************************************************************/ 1030/** Coro ********************************************************************/
972 1031
973static void 1032static void
974coro_enq (SV *coro_sv) 1033coro_enq (pTHX_ SV *coro_sv)
975{ 1034{
976 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1035 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
977} 1036}
978 1037
979static SV * 1038static SV *
980coro_deq (int min_prio) 1039coro_deq (pTHX_ int min_prio)
981{ 1040{
982 int prio = PRIO_MAX - PRIO_MIN; 1041 int prio = PRIO_MAX - PRIO_MIN;
983 1042
984 min_prio -= PRIO_MIN; 1043 min_prio -= PRIO_MIN;
985 if (min_prio < 0) 1044 if (min_prio < 0)
993} 1052}
994 1053
995static int 1054static int
996api_ready (SV *coro_sv) 1055api_ready (SV *coro_sv)
997{ 1056{
1057 dTHX;
998 struct coro *coro; 1058 struct coro *coro;
999 1059
1000 if (SvROK (coro_sv)) 1060 if (SvROK (coro_sv))
1001 coro_sv = SvRV (coro_sv); 1061 coro_sv = SvRV (coro_sv);
1002 1062
1006 return 0; 1066 return 0;
1007 1067
1008 coro->flags |= CF_READY; 1068 coro->flags |= CF_READY;
1009 1069
1010 LOCK; 1070 LOCK;
1011 coro_enq (SvREFCNT_inc (coro_sv)); 1071 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1012 ++coro_nready; 1072 ++coro_nready;
1013 UNLOCK; 1073 UNLOCK;
1014 1074
1015 return 1; 1075 return 1;
1016} 1076}
1017 1077
1018static int 1078static int
1019api_is_ready (SV *coro_sv) 1079api_is_ready (SV *coro_sv)
1020{ 1080{
1081 dTHX;
1021 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1082 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1022} 1083}
1023 1084
1024static void 1085static void
1025prepare_schedule (struct transfer_args *ta) 1086prepare_schedule (pTHX_ struct transfer_args *ta)
1026{ 1087{
1027 SV *prev_sv, *next_sv; 1088 SV *prev_sv, *next_sv;
1028 1089
1029 for (;;) 1090 for (;;)
1030 { 1091 {
1031 LOCK; 1092 LOCK;
1032 next_sv = coro_deq (PRIO_MIN); 1093 next_sv = coro_deq (aTHX_ PRIO_MIN);
1033 1094
1034 /* nothing to schedule: call the idle handler */ 1095 /* nothing to schedule: call the idle handler */
1035 if (!next_sv) 1096 if (!next_sv)
1036 { 1097 {
1037 dSP; 1098 dSP;
1072 1133
1073 assert (ta->next->flags & CF_READY); 1134 assert (ta->next->flags & CF_READY);
1074 ta->next->flags &= ~CF_READY; 1135 ta->next->flags &= ~CF_READY;
1075 1136
1076 LOCK; 1137 LOCK;
1077 free_coro_mortal (); 1138 free_coro_mortal (aTHX);
1078 coro_mortal = prev_sv; 1139 coro_mortal = prev_sv;
1079 UNLOCK; 1140 UNLOCK;
1080} 1141}
1081 1142
1082static void 1143static void
1083prepare_cede (struct transfer_args *ta) 1144prepare_cede (pTHX_ struct transfer_args *ta)
1084{ 1145{
1085 api_ready (coro_current); 1146 api_ready (coro_current);
1086 prepare_schedule (ta); 1147 prepare_schedule (aTHX_ ta);
1087} 1148}
1088 1149
1089static int 1150static int
1090prepare_cede_notself (struct transfer_args *ta) 1151prepare_cede_notself (pTHX_ struct transfer_args *ta)
1091{ 1152{
1092 if (coro_nready) 1153 if (coro_nready)
1093 { 1154 {
1094 SV *prev = SvRV (coro_current); 1155 SV *prev = SvRV (coro_current);
1095 prepare_schedule (ta); 1156 prepare_schedule (aTHX_ ta);
1096 api_ready (prev); 1157 api_ready (prev);
1097 return 1; 1158 return 1;
1098 } 1159 }
1099 else 1160 else
1100 return 0; 1161 return 0;
1101} 1162}
1102 1163
1103static void 1164static void
1104api_schedule (void) 1165api_schedule (void)
1105{ 1166{
1167 dTHX;
1106 struct transfer_args ta; 1168 struct transfer_args ta;
1107 1169
1108 prepare_schedule (&ta); 1170 prepare_schedule (aTHX_ &ta);
1109 TRANSFER (ta); 1171 TRANSFER (ta);
1110} 1172}
1111 1173
1112static int 1174static int
1113api_cede (void) 1175api_cede (void)
1114{ 1176{
1177 dTHX;
1115 struct transfer_args ta; 1178 struct transfer_args ta;
1116 1179
1117 prepare_cede (&ta); 1180 prepare_cede (aTHX_ &ta);
1118 1181
1119 if (ta.prev != ta.next) 1182 if (ta.prev != ta.next)
1120 { 1183 {
1121 TRANSFER (ta); 1184 TRANSFER (ta);
1122 return 1; 1185 return 1;
1126} 1189}
1127 1190
1128static int 1191static int
1129api_cede_notself (void) 1192api_cede_notself (void)
1130{ 1193{
1194 dTHX;
1131 struct transfer_args ta; 1195 struct transfer_args ta;
1132 1196
1133 if (prepare_cede_notself (&ta)) 1197 if (prepare_cede_notself (aTHX_ &ta))
1134 { 1198 {
1135 TRANSFER (ta); 1199 TRANSFER (ta);
1136 return 1; 1200 return 1;
1137 } 1201 }
1138 else 1202 else
1154 1218
1155 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1219 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1156 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1220 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1157 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1221 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1158 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1222 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1223 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1224 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1159 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1225 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1160 1226
1161 main_mainstack = PL_mainstack; 1227 main_mainstack = PL_mainstack;
1228 main_top_env = PL_top_env;
1229
1230 while (main_top_env->je_prev)
1231 main_top_env = main_top_env->je_prev;
1162 1232
1163 coroapi.ver = CORO_API_VERSION; 1233 coroapi.ver = CORO_API_VERSION;
1164 coroapi.transfer = api_transfer; 1234 coroapi.transfer = api_transfer;
1165 1235
1166 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1236 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1174 HV *hv; 1244 HV *hv;
1175 int i; 1245 int i;
1176 1246
1177 Newz (0, coro, 1, struct coro); 1247 Newz (0, coro, 1, struct coro);
1178 coro->args = newAV (); 1248 coro->args = newAV ();
1179 coro->save = CORO_SAVE_ALL; 1249 coro->save = CORO_SAVE_DEF;
1180 coro->flags = CF_NEW; 1250 coro->flags = CF_NEW;
1181 1251
1252 if (coro_first) coro_first->prev = coro;
1253 coro->next = coro_first;
1254 coro_first = coro;
1255
1182 hv = newHV (); 1256 coro->hv = hv = newHV ();
1183 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1257 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1184 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1258 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1185 1259
1186 for (i = 1; i < items; i++) 1260 for (i = 1; i < items; i++)
1187 av_push (coro->args, newSVsv (ST (i))); 1261 av_push (coro->args, newSVsv (ST (i)));
1191 1265
1192int 1266int
1193save (SV *coro, int new_save = -1) 1267save (SV *coro, int new_save = -1)
1194 CODE: 1268 CODE:
1195 RETVAL = api_save (coro, new_save); 1269 RETVAL = api_save (coro, new_save);
1270 OUTPUT:
1271 RETVAL
1272
1273int
1274save_also (SV *coro_sv, int save_also)
1275 CODE:
1276{
1277 struct coro *coro = SvSTATE (coro_sv);
1278 RETVAL = coro->save;
1279 coro->save |= save_also;
1280}
1196 OUTPUT: 1281 OUTPUT:
1197 RETVAL 1282 RETVAL
1198 1283
1199void 1284void
1200_set_stacklevel (...) 1285_set_stacklevel (...)
1216 1301
1217 case 1: 1302 case 1:
1218 if (items != 2) 1303 if (items != 2)
1219 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1304 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1220 1305
1221 prepare_transfer (&ta, ST (0), ST (1)); 1306 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1222 break; 1307 break;
1223 1308
1224 case 2: 1309 case 2:
1225 prepare_schedule (&ta); 1310 prepare_schedule (aTHX_ &ta);
1226 break; 1311 break;
1227 1312
1228 case 3: 1313 case 3:
1229 prepare_cede (&ta); 1314 prepare_cede (aTHX_ &ta);
1230 break; 1315 break;
1231 1316
1232 case 4: 1317 case 4:
1233 if (!prepare_cede_notself (&ta)) 1318 if (!prepare_cede_notself (aTHX_ &ta))
1234 XSRETURN_EMPTY; 1319 XSRETURN_EMPTY;
1235 1320
1236 break; 1321 break;
1237 } 1322 }
1238 1323
1244} 1329}
1245 1330
1246bool 1331bool
1247_destroy (SV *coro_sv) 1332_destroy (SV *coro_sv)
1248 CODE: 1333 CODE:
1249 RETVAL = coro_state_destroy (SvSTATE (coro_sv)); 1334 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1250 OUTPUT: 1335 OUTPUT:
1251 RETVAL 1336 RETVAL
1252 1337
1253void 1338void
1254_exit (code) 1339_exit (code)
1256 PROTOTYPE: $ 1341 PROTOTYPE: $
1257 CODE: 1342 CODE:
1258 _exit (code); 1343 _exit (code);
1259 1344
1260int 1345int
1346cctx_stacksize (int new_stacksize = 0)
1347 CODE:
1348 RETVAL = coro_stacksize;
1349 if (new_stacksize)
1350 coro_stacksize = new_stacksize;
1351 OUTPUT:
1352 RETVAL
1353
1354int
1261cctx_count () 1355cctx_count ()
1262 CODE: 1356 CODE:
1263 RETVAL = cctx_count; 1357 RETVAL = cctx_count;
1264 OUTPUT: 1358 OUTPUT:
1265 RETVAL 1359 RETVAL
1268cctx_idle () 1362cctx_idle ()
1269 CODE: 1363 CODE:
1270 RETVAL = cctx_idle; 1364 RETVAL = cctx_idle;
1271 OUTPUT: 1365 OUTPUT:
1272 RETVAL 1366 RETVAL
1367
1368void
1369list ()
1370 PPCODE:
1371{
1372 struct coro *coro;
1373 for (coro = coro_first; coro; coro = coro->next)
1374 if (coro->hv)
1375 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1376}
1377
1378void
1379_eval (Coro::State coro, SV *coderef)
1380 CODE:
1381{
1382 if (coro->mainstack)
1383 {
1384 struct coro temp;
1385 Zero (&temp, 1, struct coro);
1386 temp.save = CORO_SAVE_ALL;
1387
1388 if (!(coro->flags & CF_RUNNING))
1389 {
1390 save_perl (aTHX_ &temp);
1391 load_perl (aTHX_ coro);
1392 }
1393
1394 {
1395 dSP;
1396 ENTER;
1397 SAVETMPS;
1398 PUSHMARK (SP);
1399 PUTBACK;
1400 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1401 SPAGAIN;
1402 FREETMPS;
1403 LEAVE;
1404 PUTBACK;
1405 }
1406
1407 if (!(coro->flags & CF_RUNNING))
1408 {
1409 save_perl (aTHX_ coro);
1410 load_perl (aTHX_ &temp);
1411 }
1412 }
1413}
1414
1415SV *
1416is_ready (Coro::State coro)
1417 PROTOTYPE: $
1418 ALIAS:
1419 is_ready = CF_READY
1420 is_running = CF_RUNNING
1421 is_new = CF_NEW
1422 is_destroyed = CF_DESTROYED
1423 CODE:
1424 RETVAL = boolSV (coro->flags & ix);
1425 OUTPUT:
1426 RETVAL
1427
1428IV
1429rss (Coro::State coro)
1430 PROTOTYPE: $
1431 CODE:
1432 RETVAL = coro_rss (coro);
1433 OUTPUT:
1434 RETVAL
1435
1273 1436
1274MODULE = Coro::State PACKAGE = Coro 1437MODULE = Coro::State PACKAGE = Coro
1275 1438
1276BOOT: 1439BOOT:
1277{ 1440{
1345 CODE: 1508 CODE:
1346 RETVAL = boolSV (api_ready (self)); 1509 RETVAL = boolSV (api_ready (self));
1347 OUTPUT: 1510 OUTPUT:
1348 RETVAL 1511 RETVAL
1349 1512
1350SV *
1351is_ready (SV *self)
1352 PROTOTYPE: $
1353 CODE:
1354 RETVAL = boolSV (api_is_ready (self));
1355 OUTPUT:
1356 RETVAL
1357
1358int 1513int
1359nready (...) 1514nready (...)
1360 PROTOTYPE: 1515 PROTOTYPE:
1361 CODE: 1516 CODE:
1362 RETVAL = coro_nready; 1517 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines