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.144 by root, Sun Mar 4 11:45:23 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
86/* prefer perl internal functions over our own? */ 88/* prefer perl internal functions over our own? */
87#ifndef CORO_PREFER_PERL_FUNCTIONS 89#ifndef CORO_PREFER_PERL_FUNCTIONS
88# define CORO_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 CORO_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)
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 assert (!PL_comppad || AvARRAY (PL_comppad));//D
371} 383}
372 384
373static void 385static void
374save_perl (Coro__State c) 386save_perl (pTHX_ Coro__State c)
375{ 387{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D
377 { 388 {
378 dSP; 389 dSP;
379 I32 cxix = cxstack_ix; 390 I32 cxix = cxstack_ix;
380 PERL_CONTEXT *ccstk = cxstack; 391 PERL_CONTEXT *ccstk = cxstack;
381 PERL_SI *top_si = PL_curstackinfo; 392 PERL_SI *top_si = PL_curstackinfo;
383 /* 394 /*
384 * 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
385 * (and reinitialize) all cv's in the whole callchain :( 396 * (and reinitialize) all cv's in the whole callchain :(
386 */ 397 */
387 398
388 EXTEND (SP, 3 + 1);
389 PUSHs (Nullsv); 399 XPUSHs (Nullsv);
390 /* this loop was inspired by pp_caller */ 400 /* this loop was inspired by pp_caller */
391 for (;;) 401 for (;;)
392 { 402 {
393 while (cxix >= 0) 403 while (cxix >= 0)
394 { 404 {
395 PERL_CONTEXT *cx = &ccstk[cxix--]; 405 PERL_CONTEXT *cx = &ccstk[cxix--];
396 406
397 if (CxTYPE (cx) == CXt_SUB) 407 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
398 { 408 {
399 CV *cv = cx->blk_sub.cv; 409 CV *cv = cx->blk_sub.cv;
400 410
401 if (CvDEPTH (cv)) 411 if (CvDEPTH (cv))
402 { 412 {
404 PUSHs ((SV *)CvPADLIST (cv)); 414 PUSHs ((SV *)CvPADLIST (cv));
405 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 415 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
406 PUSHs ((SV *)cv); 416 PUSHs ((SV *)cv);
407 417
408 CvDEPTH (cv) = 0; 418 CvDEPTH (cv) = 0;
409 get_padlist (cv); 419 get_padlist (aTHX_ cv);
410 } 420 }
411 } 421 }
412 } 422 }
413 423
414 if (top_si->si_type == PERLSI_MAIN) 424 if (top_si->si_type == PERLSI_MAIN)
415 break; 425 break;
416 426
417 top_si = top_si->si_prev; 427 top_si = top_si->si_prev;
418 ccstk = top_si->si_cxstack; 428 ccstk = top_si->si_cxstack;
419 cxix = top_si->si_cxix; 429 cxix = top_si->si_cxix;
420 } 430 }
421 431
422 PUTBACK; 432 PUTBACK;
423 } 433 }
424 434
425 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;
426 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 436 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
427 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;
428 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;
429 440
430#define VAR(name,type)c->name = PL_ ## name; 441#define VAR(name,type)c->name = PL_ ## name;
431# include "state.h" 442# include "state.h"
432#undef VAR 443#undef VAR
440 */ 451 */
441#if CORO_PREFER_PERL_FUNCTIONS 452#if CORO_PREFER_PERL_FUNCTIONS
442# define coro_init_stacks init_stacks 453# define coro_init_stacks init_stacks
443#else 454#else
444static void 455static void
445coro_init_stacks () 456coro_init_stacks (pTHX)
446{ 457{
447 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 458 PL_curstackinfo = new_stackinfo(64, 6);
448 PL_curstackinfo->si_type = PERLSI_MAIN; 459 PL_curstackinfo->si_type = PERLSI_MAIN;
449 PL_curstack = PL_curstackinfo->si_stack; 460 PL_curstack = PL_curstackinfo->si_stack;
450 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 461 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
451 462
452 PL_stack_base = AvARRAY(PL_curstack); 463 PL_stack_base = AvARRAY(PL_curstack);
453 PL_stack_sp = PL_stack_base; 464 PL_stack_sp = PL_stack_base;
454 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 465 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
455 466
456 New(50,PL_tmps_stack,128,SV*); 467 New(50,PL_tmps_stack,64,SV*);
457 PL_tmps_floor = -1; 468 PL_tmps_floor = -1;
458 PL_tmps_ix = -1; 469 PL_tmps_ix = -1;
459 PL_tmps_max = 128; 470 PL_tmps_max = 64;
460 471
461 New(54,PL_markstack,32,I32); 472 New(54,PL_markstack,16,I32);
462 PL_markstack_ptr = PL_markstack; 473 PL_markstack_ptr = PL_markstack;
463 PL_markstack_max = PL_markstack + 32; 474 PL_markstack_max = PL_markstack + 16;
464 475
465#ifdef SET_MARK_OFFSET 476#ifdef SET_MARK_OFFSET
466 SET_MARK_OFFSET; 477 SET_MARK_OFFSET;
467#endif 478#endif
468 479
469 New(54,PL_scopestack,32,I32); 480 New(54,PL_scopestack,16,I32);
470 PL_scopestack_ix = 0; 481 PL_scopestack_ix = 0;
471 PL_scopestack_max = 32; 482 PL_scopestack_max = 16;
472 483
473 New(54,PL_savestack,64,ANY); 484 New(54,PL_savestack,64,ANY);
474 PL_savestack_ix = 0; 485 PL_savestack_ix = 0;
475 PL_savestack_max = 64; 486 PL_savestack_max = 64;
476 487
477#if !PERL_VERSION_ATLEAST (5,9,0) 488#if !PERL_VERSION_ATLEAST (5,9,0)
478 New(54,PL_retstack,16,OP*); 489 New(54,PL_retstack,4,OP*);
479 PL_retstack_ix = 0; 490 PL_retstack_ix = 0;
480 PL_retstack_max = 16; 491 PL_retstack_max = 4;
481#endif 492#endif
482} 493}
483#endif 494#endif
484 495
485/* 496/*
486 * destroy the stacks, the callchain etc... 497 * destroy the stacks, the callchain etc...
487 */ 498 */
488static void 499static void
489coro_destroy_stacks () 500coro_destroy_stacks (pTHX)
490{ 501{
491 if (!IN_DESTRUCT) 502 if (!IN_DESTRUCT)
492 { 503 {
493 /* restore all saved variables and stuff */ 504 /* restore all saved variables and stuff */
494 LEAVE_SCOPE (0); 505 LEAVE_SCOPE (0);
527#if !PERL_VERSION_ATLEAST (5,9,0) 538#if !PERL_VERSION_ATLEAST (5,9,0)
528 Safefree (PL_retstack); 539 Safefree (PL_retstack);
529#endif 540#endif
530} 541}
531 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
532/** coroutine stack handling ************************************************/ 574/** coroutine stack handling ************************************************/
533 575
534static void 576static void
535setup_coro (struct coro *coro) 577setup_coro (pTHX_ struct coro *coro)
536{ 578{
537 /* 579 /*
538 * emulate part of the perl startup here. 580 * emulate part of the perl startup here.
539 */ 581 */
540
541 coro_init_stacks (); 582 coro_init_stacks (aTHX);
542 583
543 PL_curcop = &PL_compiling; 584 PL_curcop = &PL_compiling;
544 PL_in_eval = EVAL_NULL; 585 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0; 586 PL_comppad = 0;
546 PL_curpm = 0; 587 PL_curpm = 0;
558 Zero (&myop, 1, LOGOP); 599 Zero (&myop, 1, LOGOP);
559 myop.op_next = Nullop; 600 myop.op_next = Nullop;
560 myop.op_flags = OPf_WANT_VOID; 601 myop.op_flags = OPf_WANT_VOID;
561 602
562 PUSHMARK (SP); 603 PUSHMARK (SP);
563 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 604 XPUSHs (av_shift (GvAV (PL_defgv)));
564 PUTBACK; 605 PUTBACK;
565 PL_op = (OP *)&myop; 606 PL_op = (OP *)&myop;
566 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 607 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
567 SPAGAIN; 608 SPAGAIN;
568 } 609 }
569 610
570 ENTER; /* necessary e.g. for dounwind */ 611 ENTER; /* necessary e.g. for dounwind */
571} 612}
572 613
573static void 614static void
574free_coro_mortal () 615free_coro_mortal (pTHX)
575{ 616{
576 if (coro_mortal) 617 if (coro_mortal)
577 { 618 {
578 SvREFCNT_dec (coro_mortal); 619 SvREFCNT_dec (coro_mortal);
579 coro_mortal = 0; 620 coro_mortal = 0;
580 } 621 }
581} 622}
582 623
583/* 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 */
584static void NOINLINE 627static void NOINLINE
585prepare_cctx (coro_cctx *cctx) 628prepare_cctx (pTHX_ coro_cctx *cctx)
586{ 629{
587 dSP; 630 dSP;
588 LOGOP myop; 631 LOGOP myop;
589 632
590 Zero (&myop, 1, LOGOP); 633 Zero (&myop, 1, LOGOP);
599 PL_op = (OP *)&myop; 642 PL_op = (OP *)&myop;
600 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 643 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
601 SPAGAIN; 644 SPAGAIN;
602} 645}
603 646
647/*
648 * this is a _very_ stripped down perl interpreter ;)
649 */
604static void 650static void
605coro_run (void *arg) 651coro_run (void *arg)
606{ 652{
653 dTHX;
654
607 /* coro_run is the alternative tail of transfer(), so unlock here. */ 655 /* coro_run is the alternative tail of transfer(), so unlock here. */
608 UNLOCK; 656 UNLOCK;
609 657
610 /*
611 * this is a _very_ stripped down perl interpreter ;)
612 */
613 PL_top_env = &PL_start_env; 658 PL_top_env = &PL_start_env;
614 659
615 /* inject call to cctx_init */ 660 /* inject a fake subroutine call to cctx_init */
616 prepare_cctx ((coro_cctx *)arg); 661 prepare_cctx (aTHX_ (coro_cctx *)arg);
617 662
618 /* 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 */
619 PL_restartop = PL_op; 664 PL_restartop = PL_op;
620 perl_run (PL_curinterp); 665 perl_run (PL_curinterp);
621 666
622 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 667 /*
623 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 */
624} 676}
625 677
626static coro_cctx * 678static coro_cctx *
627cctx_new () 679cctx_new ()
628{ 680{
629 coro_cctx *cctx; 681 coro_cctx *cctx;
682 void *stack_start;
683 size_t stack_size;
630 684
631 ++cctx_count; 685 ++cctx_count;
632 686
633 Newz (0, cctx, 1, coro_cctx); 687 Newz (0, cctx, 1, coro_cctx);
634 688
635#if HAVE_MMAP 689#if HAVE_MMAP
636 690
637 cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 691 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
638 /* mmap supposedly does allocate-on-write for us */ 692 /* mmap supposedly does allocate-on-write for us */
639 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);
640 694
641 if (cctx->sptr != (void *)-1) 695 if (cctx->sptr != (void *)-1)
642 { 696 {
643# if CORO_STACKGUARD 697# if CORO_STACKGUARD
644 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 698 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
645# endif 699# endif
646 REGISTER_STACK (
647 cctx,
648 CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr, 700 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
649 cctx->ssize + (char *)cctx->sptr 701 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
650 ); 702 cctx->mapped = 1;
651
652 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
653 } 703 }
654 else 704 else
655#endif 705#endif
656 { 706 {
657 cctx->ssize = -CORO_STACKSIZE * (long)sizeof (long); 707 cctx->ssize = coro_stacksize * (long)sizeof (long);
658 New (0, cctx->sptr, CORO_STACKSIZE, long); 708 New (0, cctx->sptr, coro_stacksize, long);
659 709
660 if (!cctx->sptr) 710 if (!cctx->sptr)
661 { 711 {
662 perror ("FATAL: unable to allocate stack for coroutine"); 712 perror ("FATAL: unable to allocate stack for coroutine");
663 _exit (EXIT_FAILURE); 713 _exit (EXIT_FAILURE);
664 } 714 }
665 715
666 REGISTER_STACK ( 716 stack_start = cctx->sptr;
667 cctx, 717 stack_size = cctx->ssize;
668 (char *)cctx->sptr, 718 }
669 (char *)cctx->sptr - cctx->ssize
670 );
671 719
720 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
672 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);
673 }
674 722
675 return cctx; 723 return cctx;
676} 724}
677 725
678static void 726static void
686#if CORO_USE_VALGRIND 734#if CORO_USE_VALGRIND
687 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 735 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
688#endif 736#endif
689 737
690#if HAVE_MMAP 738#if HAVE_MMAP
691 if (cctx->ssize > 0) 739 if (cctx->mapped)
692 munmap (cctx->sptr, cctx->ssize); 740 munmap (cctx->sptr, cctx->ssize);
693 else 741 else
694#endif 742#endif
695 Safefree (cctx->sptr); 743 Safefree (cctx->sptr);
696 744
697 Safefree (cctx); 745 Safefree (cctx);
698} 746}
699 747
700static coro_cctx * 748static coro_cctx *
701cctx_get () 749cctx_get (pTHX)
702{ 750{
703 coro_cctx *cctx;
704
705 if (cctx_first) 751 while (cctx_first)
706 { 752 {
707 cctx = cctx_first; 753 coro_cctx *cctx = cctx_first;
708 cctx_first = cctx->next; 754 cctx_first = cctx->next;
709 --cctx_idle; 755 --cctx_idle;
756
757 if (cctx->ssize >= coro_stacksize)
758 return cctx;
759
760 cctx_destroy (cctx);
710 } 761 }
711 else 762
712 {
713 cctx = cctx_new ();
714 PL_op = PL_op->op_next; 763 PL_op = PL_op->op_next;
715 }
716
717 return cctx; 764 return cctx_new ();
718} 765}
719 766
720static void 767static void
721cctx_put (coro_cctx *cctx) 768cctx_put (coro_cctx *cctx)
722{ 769{
738 785
739/** coroutine switching *****************************************************/ 786/** coroutine switching *****************************************************/
740 787
741/* never call directly, always through the coro_state_transfer global variable */ 788/* never call directly, always through the coro_state_transfer global variable */
742static void NOINLINE 789static void NOINLINE
743transfer (struct coro *prev, struct coro *next) 790transfer (pTHX_ struct coro *prev, struct coro *next)
744{ 791{
745 dSTACKLEVEL; 792 dSTACKLEVEL;
746 793
747 /* sometimes transfer is only called to set idle_sp */ 794 /* sometimes transfer is only called to set idle_sp */
748 if (!next) 795 if (!next)
781 if (next->flags & CF_NEW) 828 if (next->flags & CF_NEW)
782 { 829 {
783 /* need to start coroutine */ 830 /* need to start coroutine */
784 next->flags &= ~CF_NEW; 831 next->flags &= ~CF_NEW;
785 /* first get rid of the old state */ 832 /* first get rid of the old state */
786 save_perl (prev); 833 save_perl (aTHX_ prev);
787 /* setup coroutine call */ 834 /* setup coroutine call */
788 setup_coro (next); 835 setup_coro (aTHX_ next);
789 /* need a new stack */ 836 /* need a new stack */
790 assert (!next->cctx); 837 assert (!next->cctx);
791 } 838 }
792 else 839 else
793 { 840 {
794 /* coroutine already started */ 841 /* coroutine already started */
795 save_perl (prev); 842 save_perl (aTHX_ prev);
796 load_perl (next); 843 load_perl (aTHX_ next);
797 } 844 }
798 845
799 prev__cctx = prev->cctx; 846 prev__cctx = prev->cctx;
800 847
801 /* possibly "free" the cctx */ 848 /* possibly "free" the cctx */
810 prev__cctx->inuse = 0; 857 prev__cctx->inuse = 0;
811 } 858 }
812 859
813 if (!next->cctx) 860 if (!next->cctx)
814 { 861 {
815 next->cctx = cctx_get (); 862 next->cctx = cctx_get (aTHX);
816 assert (!next->cctx->inuse); 863 assert (!next->cctx->inuse);
817 next->cctx->inuse = 1; 864 next->cctx->inuse = 1;
818 } 865 }
819 866
820 if (prev__cctx != next->cctx) 867 if (prev__cctx != next->cctx)
822 prev__cctx->top_env = PL_top_env; 869 prev__cctx->top_env = PL_top_env;
823 PL_top_env = next->cctx->top_env; 870 PL_top_env = next->cctx->top_env;
824 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 871 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
825 } 872 }
826 873
827 free_coro_mortal (); 874 free_coro_mortal (aTHX);
828 UNLOCK; 875 UNLOCK;
829 } 876 }
830} 877}
831 878
832struct transfer_args 879struct transfer_args
833{ 880{
834 struct coro *prev, *next; 881 struct coro *prev, *next;
835}; 882};
836 883
837#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 884#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
838 885
839/** high level stuff ********************************************************/ 886/** high level stuff ********************************************************/
840 887
841static int 888static int
842coro_state_destroy (struct coro *coro) 889coro_state_destroy (pTHX_ struct coro *coro)
843{ 890{
844 if (coro->flags & CF_DESTROYED) 891 if (coro->flags & CF_DESTROYED)
845 return 0; 892 return 0;
846 893
847 coro->flags |= CF_DESTROYED; 894 coro->flags |= CF_DESTROYED;
867 temp.save = CORO_SAVE_ALL; 914 temp.save = CORO_SAVE_ALL;
868 915
869 if (coro->flags & CF_RUNNING) 916 if (coro->flags & CF_RUNNING)
870 croak ("FATAL: tried to destroy currently running coroutine"); 917 croak ("FATAL: tried to destroy currently running coroutine");
871 918
872 save_perl (&temp); 919 save_perl (aTHX_ &temp);
873 load_perl (coro); 920 load_perl (aTHX_ coro);
874 921
875 coro_destroy_stacks (); 922 coro_destroy_stacks (aTHX);
876 923
877 load_perl (&temp); /* this will get rid of defsv etc.. */ 924 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
878 925
879 coro->mainstack = 0; 926 coro->mainstack = 0;
880 } 927 }
881 928
882 cctx_destroy (coro->cctx); 929 cctx_destroy (coro->cctx);
883 SvREFCNT_dec (coro->args); 930 SvREFCNT_dec (coro->args);
884 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
885 return 1; 936 return 1;
886} 937}
887 938
888static int 939static int
889coro_state_free (pTHX_ SV *sv, MAGIC *mg) 940coro_state_free (pTHX_ SV *sv, MAGIC *mg)
890{ 941{
891 struct coro *coro = (struct coro *)mg->mg_ptr; 942 struct coro *coro = (struct coro *)mg->mg_ptr;
892 mg->mg_ptr = 0; 943 mg->mg_ptr = 0;
893 944
945 coro->hv = 0;
946
894 if (--coro->refcnt < 0) 947 if (--coro->refcnt < 0)
895 { 948 {
896 coro_state_destroy (coro); 949 coro_state_destroy (aTHX_ coro);
897 Safefree (coro); 950 Safefree (coro);
898 } 951 }
899 952
900 return 0; 953 return 0;
901} 954}
920# define MGf_DUP 0 973# define MGf_DUP 0
921#endif 974#endif
922}; 975};
923 976
924static struct coro * 977static struct coro *
925SvSTATE (SV *coro) 978SvSTATE_ (pTHX_ SV *coro)
926{ 979{
927 HV *stash; 980 HV *stash;
928 MAGIC *mg; 981 MAGIC *mg;
929 982
930 if (SvROK (coro)) 983 if (SvROK (coro))
940 993
941 mg = CORO_MAGIC (coro); 994 mg = CORO_MAGIC (coro);
942 return (struct coro *)mg->mg_ptr; 995 return (struct coro *)mg->mg_ptr;
943} 996}
944 997
998#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
999
945static void 1000static void
946prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1001prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
947{ 1002{
948 ta->prev = SvSTATE (prev_sv); 1003 ta->prev = SvSTATE (prev_sv);
949 ta->next = SvSTATE (next_sv); 1004 ta->next = SvSTATE (next_sv);
950} 1005}
951 1006
952static void 1007static void
953api_transfer (SV *prev_sv, SV *next_sv) 1008api_transfer (SV *prev_sv, SV *next_sv)
954{ 1009{
1010 dTHX;
955 struct transfer_args ta; 1011 struct transfer_args ta;
956 1012
957 prepare_transfer (&ta, prev_sv, next_sv); 1013 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
958 TRANSFER (ta); 1014 TRANSFER (ta);
959} 1015}
960 1016
961static int 1017static int
962api_save (SV *coro_sv, int new_save) 1018api_save (SV *coro_sv, int new_save)
963{ 1019{
1020 dTHX;
964 struct coro *coro = SvSTATE (coro_sv); 1021 struct coro *coro = SvSTATE (coro_sv);
965 int old_save = coro->save; 1022 int old_save = coro->save;
966 1023
967 if (new_save >= 0) 1024 if (new_save >= 0)
968 coro->save = new_save; 1025 coro->save = new_save;
971} 1028}
972 1029
973/** Coro ********************************************************************/ 1030/** Coro ********************************************************************/
974 1031
975static void 1032static void
976coro_enq (SV *coro_sv) 1033coro_enq (pTHX_ SV *coro_sv)
977{ 1034{
978 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);
979} 1036}
980 1037
981static SV * 1038static SV *
982coro_deq (int min_prio) 1039coro_deq (pTHX_ int min_prio)
983{ 1040{
984 int prio = PRIO_MAX - PRIO_MIN; 1041 int prio = PRIO_MAX - PRIO_MIN;
985 1042
986 min_prio -= PRIO_MIN; 1043 min_prio -= PRIO_MIN;
987 if (min_prio < 0) 1044 if (min_prio < 0)
995} 1052}
996 1053
997static int 1054static int
998api_ready (SV *coro_sv) 1055api_ready (SV *coro_sv)
999{ 1056{
1057 dTHX;
1000 struct coro *coro; 1058 struct coro *coro;
1001 1059
1002 if (SvROK (coro_sv)) 1060 if (SvROK (coro_sv))
1003 coro_sv = SvRV (coro_sv); 1061 coro_sv = SvRV (coro_sv);
1004 1062
1008 return 0; 1066 return 0;
1009 1067
1010 coro->flags |= CF_READY; 1068 coro->flags |= CF_READY;
1011 1069
1012 LOCK; 1070 LOCK;
1013 coro_enq (SvREFCNT_inc (coro_sv)); 1071 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1014 ++coro_nready; 1072 ++coro_nready;
1015 UNLOCK; 1073 UNLOCK;
1016 1074
1017 return 1; 1075 return 1;
1018} 1076}
1019 1077
1020static int 1078static int
1021api_is_ready (SV *coro_sv) 1079api_is_ready (SV *coro_sv)
1022{ 1080{
1081 dTHX;
1023 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1082 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1024} 1083}
1025 1084
1026static void 1085static void
1027prepare_schedule (struct transfer_args *ta) 1086prepare_schedule (pTHX_ struct transfer_args *ta)
1028{ 1087{
1029 SV *prev_sv, *next_sv; 1088 SV *prev_sv, *next_sv;
1030 1089
1031 for (;;) 1090 for (;;)
1032 { 1091 {
1033 LOCK; 1092 LOCK;
1034 next_sv = coro_deq (PRIO_MIN); 1093 next_sv = coro_deq (aTHX_ PRIO_MIN);
1035 1094
1036 /* nothing to schedule: call the idle handler */ 1095 /* nothing to schedule: call the idle handler */
1037 if (!next_sv) 1096 if (!next_sv)
1038 { 1097 {
1039 dSP; 1098 dSP;
1074 1133
1075 assert (ta->next->flags & CF_READY); 1134 assert (ta->next->flags & CF_READY);
1076 ta->next->flags &= ~CF_READY; 1135 ta->next->flags &= ~CF_READY;
1077 1136
1078 LOCK; 1137 LOCK;
1079 free_coro_mortal (); 1138 free_coro_mortal (aTHX);
1080 coro_mortal = prev_sv; 1139 coro_mortal = prev_sv;
1081 UNLOCK; 1140 UNLOCK;
1082} 1141}
1083 1142
1084static void 1143static void
1085prepare_cede (struct transfer_args *ta) 1144prepare_cede (pTHX_ struct transfer_args *ta)
1086{ 1145{
1087 api_ready (coro_current); 1146 api_ready (coro_current);
1088 prepare_schedule (ta); 1147 prepare_schedule (aTHX_ ta);
1089} 1148}
1090 1149
1091static int 1150static int
1092prepare_cede_notself (struct transfer_args *ta) 1151prepare_cede_notself (pTHX_ struct transfer_args *ta)
1093{ 1152{
1094 if (coro_nready) 1153 if (coro_nready)
1095 { 1154 {
1096 SV *prev = SvRV (coro_current); 1155 SV *prev = SvRV (coro_current);
1097 prepare_schedule (ta); 1156 prepare_schedule (aTHX_ ta);
1098 api_ready (prev); 1157 api_ready (prev);
1099 return 1; 1158 return 1;
1100 } 1159 }
1101 else 1160 else
1102 return 0; 1161 return 0;
1103} 1162}
1104 1163
1105static void 1164static void
1106api_schedule (void) 1165api_schedule (void)
1107{ 1166{
1167 dTHX;
1108 struct transfer_args ta; 1168 struct transfer_args ta;
1109 1169
1110 prepare_schedule (&ta); 1170 prepare_schedule (aTHX_ &ta);
1111 TRANSFER (ta); 1171 TRANSFER (ta);
1112} 1172}
1113 1173
1114static int 1174static int
1115api_cede (void) 1175api_cede (void)
1116{ 1176{
1177 dTHX;
1117 struct transfer_args ta; 1178 struct transfer_args ta;
1118 1179
1119 prepare_cede (&ta); 1180 prepare_cede (aTHX_ &ta);
1120 1181
1121 if (ta.prev != ta.next) 1182 if (ta.prev != ta.next)
1122 { 1183 {
1123 TRANSFER (ta); 1184 TRANSFER (ta);
1124 return 1; 1185 return 1;
1128} 1189}
1129 1190
1130static int 1191static int
1131api_cede_notself (void) 1192api_cede_notself (void)
1132{ 1193{
1194 dTHX;
1133 struct transfer_args ta; 1195 struct transfer_args ta;
1134 1196
1135 if (prepare_cede_notself (&ta)) 1197 if (prepare_cede_notself (aTHX_ &ta))
1136 { 1198 {
1137 TRANSFER (ta); 1199 TRANSFER (ta);
1138 return 1; 1200 return 1;
1139 } 1201 }
1140 else 1202 else
1156 1218
1157 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1219 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1158 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1220 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1159 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1221 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1160 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));
1161 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1225 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1162 1226
1163 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;
1164 1232
1165 coroapi.ver = CORO_API_VERSION; 1233 coroapi.ver = CORO_API_VERSION;
1166 coroapi.transfer = api_transfer; 1234 coroapi.transfer = api_transfer;
1167 1235
1168 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1236 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1176 HV *hv; 1244 HV *hv;
1177 int i; 1245 int i;
1178 1246
1179 Newz (0, coro, 1, struct coro); 1247 Newz (0, coro, 1, struct coro);
1180 coro->args = newAV (); 1248 coro->args = newAV ();
1181 coro->save = CORO_SAVE_ALL; 1249 coro->save = CORO_SAVE_DEF;
1182 coro->flags = CF_NEW; 1250 coro->flags = CF_NEW;
1183 1251
1252 if (coro_first) coro_first->prev = coro;
1253 coro->next = coro_first;
1254 coro_first = coro;
1255
1184 hv = newHV (); 1256 coro->hv = hv = newHV ();
1185 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;
1186 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1258 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1187 1259
1188 for (i = 1; i < items; i++) 1260 for (i = 1; i < items; i++)
1189 av_push (coro->args, newSVsv (ST (i))); 1261 av_push (coro->args, newSVsv (ST (i)));
1193 1265
1194int 1266int
1195save (SV *coro, int new_save = -1) 1267save (SV *coro, int new_save = -1)
1196 CODE: 1268 CODE:
1197 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}
1198 OUTPUT: 1281 OUTPUT:
1199 RETVAL 1282 RETVAL
1200 1283
1201void 1284void
1202_set_stacklevel (...) 1285_set_stacklevel (...)
1218 1301
1219 case 1: 1302 case 1:
1220 if (items != 2) 1303 if (items != 2)
1221 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);
1222 1305
1223 prepare_transfer (&ta, ST (0), ST (1)); 1306 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1224 break; 1307 break;
1225 1308
1226 case 2: 1309 case 2:
1227 prepare_schedule (&ta); 1310 prepare_schedule (aTHX_ &ta);
1228 break; 1311 break;
1229 1312
1230 case 3: 1313 case 3:
1231 prepare_cede (&ta); 1314 prepare_cede (aTHX_ &ta);
1232 break; 1315 break;
1233 1316
1234 case 4: 1317 case 4:
1235 if (!prepare_cede_notself (&ta)) 1318 if (!prepare_cede_notself (aTHX_ &ta))
1236 XSRETURN_EMPTY; 1319 XSRETURN_EMPTY;
1237 1320
1238 break; 1321 break;
1239 } 1322 }
1240 1323
1246} 1329}
1247 1330
1248bool 1331bool
1249_destroy (SV *coro_sv) 1332_destroy (SV *coro_sv)
1250 CODE: 1333 CODE:
1251 RETVAL = coro_state_destroy (SvSTATE (coro_sv)); 1334 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1252 OUTPUT: 1335 OUTPUT:
1253 RETVAL 1336 RETVAL
1254 1337
1255void 1338void
1256_exit (code) 1339_exit (code)
1258 PROTOTYPE: $ 1341 PROTOTYPE: $
1259 CODE: 1342 CODE:
1260 _exit (code); 1343 _exit (code);
1261 1344
1262int 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
1263cctx_count () 1355cctx_count ()
1264 CODE: 1356 CODE:
1265 RETVAL = cctx_count; 1357 RETVAL = cctx_count;
1266 OUTPUT: 1358 OUTPUT:
1267 RETVAL 1359 RETVAL
1270cctx_idle () 1362cctx_idle ()
1271 CODE: 1363 CODE:
1272 RETVAL = cctx_idle; 1364 RETVAL = cctx_idle;
1273 OUTPUT: 1365 OUTPUT:
1274 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
1275 1436
1276MODULE = Coro::State PACKAGE = Coro 1437MODULE = Coro::State PACKAGE = Coro
1277 1438
1278BOOT: 1439BOOT:
1279{ 1440{
1347 CODE: 1508 CODE:
1348 RETVAL = boolSV (api_ready (self)); 1509 RETVAL = boolSV (api_ready (self));
1349 OUTPUT: 1510 OUTPUT:
1350 RETVAL 1511 RETVAL
1351 1512
1352SV *
1353is_ready (SV *self)
1354 PROTOTYPE: $
1355 CODE:
1356 RETVAL = boolSV (api_is_ready (self));
1357 OUTPUT:
1358 RETVAL
1359
1360int 1513int
1361nready (...) 1514nready (...)
1362 PROTOTYPE: 1515 PROTOTYPE:
1363 CODE: 1516 CODE:
1364 RETVAL = coro_nready; 1517 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines