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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines