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.145 by root, Mon Mar 12 21:40:14 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
127}; 128};
128 129
129static size_t coro_stacksize = CORO_STACKSIZE; 130static size_t coro_stacksize = CORO_STACKSIZE;
130static struct CoroAPI coroapi; 131static struct CoroAPI coroapi;
131static 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;
132static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
133static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
134 136
135static struct coro_cctx *cctx_first; 137static struct coro_cctx *cctx_first;
136static int cctx_count, cctx_idle; 138static int cctx_count, cctx_idle;
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{
692 744
693 Safefree (cctx); 745 Safefree (cctx);
694} 746}
695 747
696static coro_cctx * 748static coro_cctx *
697cctx_get () 749cctx_get (pTHX)
698{ 750{
699 while (cctx_first) 751 while (cctx_first)
700 { 752 {
701 coro_cctx *cctx = cctx_first; 753 coro_cctx *cctx = cctx_first;
702 cctx_first = cctx->next; 754 cctx_first = cctx->next;
733 785
734/** coroutine switching *****************************************************/ 786/** coroutine switching *****************************************************/
735 787
736/* never call directly, always through the coro_state_transfer global variable */ 788/* never call directly, always through the coro_state_transfer global variable */
737static void NOINLINE 789static void NOINLINE
738transfer (struct coro *prev, struct coro *next) 790transfer (pTHX_ struct coro *prev, struct coro *next)
739{ 791{
740 dSTACKLEVEL; 792 dSTACKLEVEL;
741 793
742 /* sometimes transfer is only called to set idle_sp */ 794 /* sometimes transfer is only called to set idle_sp */
743 if (!next) 795 if (!next)
776 if (next->flags & CF_NEW) 828 if (next->flags & CF_NEW)
777 { 829 {
778 /* need to start coroutine */ 830 /* need to start coroutine */
779 next->flags &= ~CF_NEW; 831 next->flags &= ~CF_NEW;
780 /* first get rid of the old state */ 832 /* first get rid of the old state */
781 save_perl (prev); 833 save_perl (aTHX_ prev);
782 /* setup coroutine call */ 834 /* setup coroutine call */
783 setup_coro (next); 835 setup_coro (aTHX_ next);
784 /* need a new stack */ 836 /* need a new stack */
785 assert (!next->cctx); 837 assert (!next->cctx);
786 } 838 }
787 else 839 else
788 { 840 {
789 /* coroutine already started */ 841 /* coroutine already started */
790 save_perl (prev); 842 save_perl (aTHX_ prev);
791 load_perl (next); 843 load_perl (aTHX_ next);
792 } 844 }
793 845
794 prev__cctx = prev->cctx; 846 prev__cctx = prev->cctx;
795 847
796 /* possibly "free" the cctx */ 848 /* possibly "free" the cctx */
805 prev__cctx->inuse = 0; 857 prev__cctx->inuse = 0;
806 } 858 }
807 859
808 if (!next->cctx) 860 if (!next->cctx)
809 { 861 {
810 next->cctx = cctx_get (); 862 next->cctx = cctx_get (aTHX);
811 assert (!next->cctx->inuse); 863 assert (!next->cctx->inuse);
812 next->cctx->inuse = 1; 864 next->cctx->inuse = 1;
813 } 865 }
814 866
815 if (prev__cctx != next->cctx) 867 if (prev__cctx != next->cctx)
817 prev__cctx->top_env = PL_top_env; 869 prev__cctx->top_env = PL_top_env;
818 PL_top_env = next->cctx->top_env; 870 PL_top_env = next->cctx->top_env;
819 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 871 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
820 } 872 }
821 873
822 free_coro_mortal (); 874 free_coro_mortal (aTHX);
823 UNLOCK; 875 UNLOCK;
824 } 876 }
825} 877}
826 878
827struct transfer_args 879struct transfer_args
828{ 880{
829 struct coro *prev, *next; 881 struct coro *prev, *next;
830}; 882};
831 883
832#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 884#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
833 885
834/** high level stuff ********************************************************/ 886/** high level stuff ********************************************************/
835 887
836static int 888static int
837coro_state_destroy (struct coro *coro) 889coro_state_destroy (pTHX_ struct coro *coro)
838{ 890{
839 if (coro->flags & CF_DESTROYED) 891 if (coro->flags & CF_DESTROYED)
840 return 0; 892 return 0;
841 893
842 coro->flags |= CF_DESTROYED; 894 coro->flags |= CF_DESTROYED;
862 temp.save = CORO_SAVE_ALL; 914 temp.save = CORO_SAVE_ALL;
863 915
864 if (coro->flags & CF_RUNNING) 916 if (coro->flags & CF_RUNNING)
865 croak ("FATAL: tried to destroy currently running coroutine"); 917 croak ("FATAL: tried to destroy currently running coroutine");
866 918
867 save_perl (&temp); 919 save_perl (aTHX_ &temp);
868 load_perl (coro); 920 load_perl (aTHX_ coro);
869 921
870 coro_destroy_stacks (); 922 coro_destroy_stacks (aTHX);
871 923
872 load_perl (&temp); /* this will get rid of defsv etc.. */ 924 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
873 925
874 coro->mainstack = 0; 926 coro->mainstack = 0;
875 } 927 }
876 928
877 cctx_destroy (coro->cctx); 929 cctx_destroy (coro->cctx);
878 SvREFCNT_dec (coro->args); 930 SvREFCNT_dec (coro->args);
879 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
880 return 1; 936 return 1;
881} 937}
882 938
883static int 939static int
884coro_state_free (pTHX_ SV *sv, MAGIC *mg) 940coro_state_free (pTHX_ SV *sv, MAGIC *mg)
885{ 941{
886 struct coro *coro = (struct coro *)mg->mg_ptr; 942 struct coro *coro = (struct coro *)mg->mg_ptr;
887 mg->mg_ptr = 0; 943 mg->mg_ptr = 0;
888 944
945 coro->hv = 0;
946
889 if (--coro->refcnt < 0) 947 if (--coro->refcnt < 0)
890 { 948 {
891 coro_state_destroy (coro); 949 coro_state_destroy (aTHX_ coro);
892 Safefree (coro); 950 Safefree (coro);
893 } 951 }
894 952
895 return 0; 953 return 0;
896} 954}
915# define MGf_DUP 0 973# define MGf_DUP 0
916#endif 974#endif
917}; 975};
918 976
919static struct coro * 977static struct coro *
920SvSTATE (SV *coro) 978SvSTATE_ (pTHX_ SV *coro)
921{ 979{
922 HV *stash; 980 HV *stash;
923 MAGIC *mg; 981 MAGIC *mg;
924 982
925 if (SvROK (coro)) 983 if (SvROK (coro))
935 993
936 mg = CORO_MAGIC (coro); 994 mg = CORO_MAGIC (coro);
937 return (struct coro *)mg->mg_ptr; 995 return (struct coro *)mg->mg_ptr;
938} 996}
939 997
998#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
999
940static void 1000static void
941prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1001prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
942{ 1002{
943 ta->prev = SvSTATE (prev_sv); 1003 ta->prev = SvSTATE (prev_sv);
944 ta->next = SvSTATE (next_sv); 1004 ta->next = SvSTATE (next_sv);
945} 1005}
946 1006
947static void 1007static void
948api_transfer (SV *prev_sv, SV *next_sv) 1008api_transfer (SV *prev_sv, SV *next_sv)
949{ 1009{
1010 dTHX;
950 struct transfer_args ta; 1011 struct transfer_args ta;
951 1012
952 prepare_transfer (&ta, prev_sv, next_sv); 1013 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
953 TRANSFER (ta); 1014 TRANSFER (ta);
954} 1015}
955 1016
956static int 1017static int
957api_save (SV *coro_sv, int new_save) 1018api_save (SV *coro_sv, int new_save)
958{ 1019{
1020 dTHX;
959 struct coro *coro = SvSTATE (coro_sv); 1021 struct coro *coro = SvSTATE (coro_sv);
960 int old_save = coro->save; 1022 int old_save = coro->save;
961 1023
962 if (new_save >= 0) 1024 if (new_save >= 0)
963 coro->save = new_save; 1025 coro->save = new_save;
966} 1028}
967 1029
968/** Coro ********************************************************************/ 1030/** Coro ********************************************************************/
969 1031
970static void 1032static void
971coro_enq (SV *coro_sv) 1033coro_enq (pTHX_ SV *coro_sv)
972{ 1034{
973 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);
974} 1036}
975 1037
976static SV * 1038static SV *
977coro_deq (int min_prio) 1039coro_deq (pTHX_ int min_prio)
978{ 1040{
979 int prio = PRIO_MAX - PRIO_MIN; 1041 int prio = PRIO_MAX - PRIO_MIN;
980 1042
981 min_prio -= PRIO_MIN; 1043 min_prio -= PRIO_MIN;
982 if (min_prio < 0) 1044 if (min_prio < 0)
990} 1052}
991 1053
992static int 1054static int
993api_ready (SV *coro_sv) 1055api_ready (SV *coro_sv)
994{ 1056{
1057 dTHX;
995 struct coro *coro; 1058 struct coro *coro;
996 1059
997 if (SvROK (coro_sv)) 1060 if (SvROK (coro_sv))
998 coro_sv = SvRV (coro_sv); 1061 coro_sv = SvRV (coro_sv);
999 1062
1003 return 0; 1066 return 0;
1004 1067
1005 coro->flags |= CF_READY; 1068 coro->flags |= CF_READY;
1006 1069
1007 LOCK; 1070 LOCK;
1008 coro_enq (SvREFCNT_inc (coro_sv)); 1071 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1009 ++coro_nready; 1072 ++coro_nready;
1010 UNLOCK; 1073 UNLOCK;
1011 1074
1012 return 1; 1075 return 1;
1013} 1076}
1014 1077
1015static int 1078static int
1016api_is_ready (SV *coro_sv) 1079api_is_ready (SV *coro_sv)
1017{ 1080{
1081 dTHX;
1018 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1082 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1019} 1083}
1020 1084
1021static void 1085static void
1022prepare_schedule (struct transfer_args *ta) 1086prepare_schedule (pTHX_ struct transfer_args *ta)
1023{ 1087{
1024 SV *prev_sv, *next_sv; 1088 SV *prev_sv, *next_sv;
1025 1089
1026 for (;;) 1090 for (;;)
1027 { 1091 {
1028 LOCK; 1092 LOCK;
1029 next_sv = coro_deq (PRIO_MIN); 1093 next_sv = coro_deq (aTHX_ PRIO_MIN);
1030 1094
1031 /* nothing to schedule: call the idle handler */ 1095 /* nothing to schedule: call the idle handler */
1032 if (!next_sv) 1096 if (!next_sv)
1033 { 1097 {
1034 dSP; 1098 dSP;
1069 1133
1070 assert (ta->next->flags & CF_READY); 1134 assert (ta->next->flags & CF_READY);
1071 ta->next->flags &= ~CF_READY; 1135 ta->next->flags &= ~CF_READY;
1072 1136
1073 LOCK; 1137 LOCK;
1074 free_coro_mortal (); 1138 free_coro_mortal (aTHX);
1075 coro_mortal = prev_sv; 1139 coro_mortal = prev_sv;
1076 UNLOCK; 1140 UNLOCK;
1077} 1141}
1078 1142
1079static void 1143static void
1080prepare_cede (struct transfer_args *ta) 1144prepare_cede (pTHX_ struct transfer_args *ta)
1081{ 1145{
1082 api_ready (coro_current); 1146 api_ready (coro_current);
1083 prepare_schedule (ta); 1147 prepare_schedule (aTHX_ ta);
1084} 1148}
1085 1149
1086static int 1150static int
1087prepare_cede_notself (struct transfer_args *ta) 1151prepare_cede_notself (pTHX_ struct transfer_args *ta)
1088{ 1152{
1089 if (coro_nready) 1153 if (coro_nready)
1090 { 1154 {
1091 SV *prev = SvRV (coro_current); 1155 SV *prev = SvRV (coro_current);
1092 prepare_schedule (ta); 1156 prepare_schedule (aTHX_ ta);
1093 api_ready (prev); 1157 api_ready (prev);
1094 return 1; 1158 return 1;
1095 } 1159 }
1096 else 1160 else
1097 return 0; 1161 return 0;
1098} 1162}
1099 1163
1100static void 1164static void
1101api_schedule (void) 1165api_schedule (void)
1102{ 1166{
1167 dTHX;
1103 struct transfer_args ta; 1168 struct transfer_args ta;
1104 1169
1105 prepare_schedule (&ta); 1170 prepare_schedule (aTHX_ &ta);
1106 TRANSFER (ta); 1171 TRANSFER (ta);
1107} 1172}
1108 1173
1109static int 1174static int
1110api_cede (void) 1175api_cede (void)
1111{ 1176{
1177 dTHX;
1112 struct transfer_args ta; 1178 struct transfer_args ta;
1113 1179
1114 prepare_cede (&ta); 1180 prepare_cede (aTHX_ &ta);
1115 1181
1116 if (ta.prev != ta.next) 1182 if (ta.prev != ta.next)
1117 { 1183 {
1118 TRANSFER (ta); 1184 TRANSFER (ta);
1119 return 1; 1185 return 1;
1123} 1189}
1124 1190
1125static int 1191static int
1126api_cede_notself (void) 1192api_cede_notself (void)
1127{ 1193{
1194 dTHX;
1128 struct transfer_args ta; 1195 struct transfer_args ta;
1129 1196
1130 if (prepare_cede_notself (&ta)) 1197 if (prepare_cede_notself (aTHX_ &ta))
1131 { 1198 {
1132 TRANSFER (ta); 1199 TRANSFER (ta);
1133 return 1; 1200 return 1;
1134 } 1201 }
1135 else 1202 else
1151 1218
1152 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1219 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1153 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1220 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1154 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1221 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1155 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));
1156 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1225 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1157 1226
1158 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;
1159 1232
1160 coroapi.ver = CORO_API_VERSION; 1233 coroapi.ver = CORO_API_VERSION;
1161 coroapi.transfer = api_transfer; 1234 coroapi.transfer = api_transfer;
1162 1235
1163 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1236 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1171 HV *hv; 1244 HV *hv;
1172 int i; 1245 int i;
1173 1246
1174 Newz (0, coro, 1, struct coro); 1247 Newz (0, coro, 1, struct coro);
1175 coro->args = newAV (); 1248 coro->args = newAV ();
1176 coro->save = CORO_SAVE_ALL; 1249 coro->save = CORO_SAVE_DEF;
1177 coro->flags = CF_NEW; 1250 coro->flags = CF_NEW;
1178 1251
1252 if (coro_first) coro_first->prev = coro;
1253 coro->next = coro_first;
1254 coro_first = coro;
1255
1179 hv = newHV (); 1256 coro->hv = hv = newHV ();
1180 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;
1181 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1258 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1182 1259
1183 for (i = 1; i < items; i++) 1260 for (i = 1; i < items; i++)
1184 av_push (coro->args, newSVsv (ST (i))); 1261 av_push (coro->args, newSVsv (ST (i)));
1188 1265
1189int 1266int
1190save (SV *coro, int new_save = -1) 1267save (SV *coro, int new_save = -1)
1191 CODE: 1268 CODE:
1192 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}
1193 OUTPUT: 1281 OUTPUT:
1194 RETVAL 1282 RETVAL
1195 1283
1196void 1284void
1197_set_stacklevel (...) 1285_set_stacklevel (...)
1213 1301
1214 case 1: 1302 case 1:
1215 if (items != 2) 1303 if (items != 2)
1216 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);
1217 1305
1218 prepare_transfer (&ta, ST (0), ST (1)); 1306 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1219 break; 1307 break;
1220 1308
1221 case 2: 1309 case 2:
1222 prepare_schedule (&ta); 1310 prepare_schedule (aTHX_ &ta);
1223 break; 1311 break;
1224 1312
1225 case 3: 1313 case 3:
1226 prepare_cede (&ta); 1314 prepare_cede (aTHX_ &ta);
1227 break; 1315 break;
1228 1316
1229 case 4: 1317 case 4:
1230 if (!prepare_cede_notself (&ta)) 1318 if (!prepare_cede_notself (aTHX_ &ta))
1231 XSRETURN_EMPTY; 1319 XSRETURN_EMPTY;
1232 1320
1233 break; 1321 break;
1234 } 1322 }
1235 1323
1241} 1329}
1242 1330
1243bool 1331bool
1244_destroy (SV *coro_sv) 1332_destroy (SV *coro_sv)
1245 CODE: 1333 CODE:
1246 RETVAL = coro_state_destroy (SvSTATE (coro_sv)); 1334 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1247 OUTPUT: 1335 OUTPUT:
1248 RETVAL 1336 RETVAL
1249 1337
1250void 1338void
1251_exit (code) 1339_exit (code)
1275 CODE: 1363 CODE:
1276 RETVAL = cctx_idle; 1364 RETVAL = cctx_idle;
1277 OUTPUT: 1365 OUTPUT:
1278 RETVAL 1366 RETVAL
1279 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
1436
1280MODULE = Coro::State PACKAGE = Coro 1437MODULE = Coro::State PACKAGE = Coro
1281 1438
1282BOOT: 1439BOOT:
1283{ 1440{
1284 int i; 1441 int i;
1351 CODE: 1508 CODE:
1352 RETVAL = boolSV (api_ready (self)); 1509 RETVAL = boolSV (api_ready (self));
1353 OUTPUT: 1510 OUTPUT:
1354 RETVAL 1511 RETVAL
1355 1512
1356SV *
1357is_ready (SV *self)
1358 PROTOTYPE: $
1359 CODE:
1360 RETVAL = boolSV (api_is_ready (self));
1361 OUTPUT:
1362 RETVAL
1363
1364int 1513int
1365nready (...) 1514nready (...)
1366 PROTOTYPE: 1515 PROTOTYPE:
1367 CODE: 1516 CODE:
1368 RETVAL = coro_nready; 1517 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines