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.114 by root, Thu Nov 30 21:22:45 2006 UTC vs.
Revision 1.123 by root, Mon Dec 4 21:56:00 2006 UTC

7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if USE_VALGRIND 9#if USE_VALGRIND
10# include <valgrind/valgrind.h> 10# include <valgrind/valgrind.h>
11#endif 11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
12 15
13#define PERL_VERSION_ATLEAST(a,b,c) \ 16#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \ 17 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \ 18 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \ 19 && (PERL_VERSION > (b) \
87 90
88#define NOINLINE attribute ((noinline)) 91#define NOINLINE attribute ((noinline))
89 92
90#include "CoroAPI.h" 93#include "CoroAPI.h"
91 94
92#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
93
94#ifdef USE_ITHREADS 95#ifdef USE_ITHREADS
95static perl_mutex coro_mutex; 96static perl_mutex coro_mutex;
96# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 97# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
97# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 98# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
98#else 99#else
99# define LOCK (void)0 100# define LOCK (void)0
100# define UNLOCK (void)0 101# define UNLOCK (void)0
101#endif 102#endif
102 103
104struct io_state
105{
106 int errorno;
107 I32 laststype;
108 int laststatval;
109 Stat_t statcache;
110};
111
103static struct CoroAPI coroapi; 112static struct CoroAPI coroapi;
104static AV *main_mainstack; /* used to differentiate between $main and others */ 113static AV *main_mainstack; /* used to differentiate between $main and others */
105static HV *coro_state_stash, *coro_stash; 114static HV *coro_state_stash, *coro_stash;
106static SV *coro_mortal; /* will be freed after next transfer */ 115static SV *coro_mortal; /* will be freed after next transfer */
107 116
127 int valgrind_id; 136 int valgrind_id;
128#endif 137#endif
129} coro_cctx; 138} coro_cctx;
130 139
131enum { 140enum {
132 CF_RUNNING, /* coroutine is running */ 141 CF_RUNNING = 0x0001, /* coroutine is running */
133 CF_READY, /* coroutine is ready */ 142 CF_READY = 0x0002, /* coroutine is ready */
143 CF_NEW = 0x0004, /* ahs never been switched to */
134}; 144};
135 145
136/* this is a structure representing a perl-level coroutine */ 146/* this is a structure representing a perl-level coroutine */
137struct coro { 147struct coro {
138 /* the c coroutine allocated to this perl coroutine, if any */ 148 /* the c coroutine allocated to this perl coroutine, if any */
139 coro_cctx *cctx; 149 coro_cctx *cctx;
140 150
141 /* data associated with this coroutine (initial args) */ 151 /* data associated with this coroutine (initial args) */
142 AV *args; 152 AV *args;
143 int refcnt; 153 int refcnt;
144 int flags; 154 int save; /* CORO_SAVE flags */
155 int flags; /* CF_ flags */
145 156
146 /* optionally saved, might be zero */ 157 /* optionally saved, might be zero */
147 AV *defav; 158 AV *defav; /* @_ */
148 SV *defsv; 159 SV *defsv; /* $_ */
149 SV *errsv; 160 SV *errsv; /* $@ */
161 SV *irssv; /* $/ */
162 SV *irssv_sv; /* real $/ cache */
150 163
151#define VAR(name,type) type name; 164#define VAR(name,type) type name;
152# include "state.h" 165# include "state.h"
153#undef VAR 166#undef VAR
154 167
277} 290}
278 291
279#define SB do { 292#define SB do {
280#define SE } while (0) 293#define SE } while (0)
281 294
282#define LOAD(state) load_state((state));
283#define SAVE(state,flags) save_state((state),(flags));
284
285#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 295#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
286 296
287static void 297static void
288load_state(Coro__State c) 298load_perl (Coro__State c)
289{ 299{
290#define VAR(name,type) PL_ ## name = c->name; 300#define VAR(name,type) PL_ ## name = c->name;
291# include "state.h" 301# include "state.h"
292#undef VAR 302#undef VAR
293 303
294 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 304 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
295 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 305 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
296 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 306 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
307 if (c->irssv)
308 {
309 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
310 SvREFCNT_dec (c->irssv);
311 else
312 {
313 REPLACE_SV (PL_rs, c->irssv);
314 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
315 sv_setsv (c->irssv_sv, PL_rs);
316 }
317 }
297 318
298 { 319 {
299 dSP; 320 dSP;
300 CV *cv; 321 CV *cv;
301 322
310 PUTBACK; 331 PUTBACK;
311 } 332 }
312} 333}
313 334
314static void 335static void
315save_state(Coro__State c, int flags) 336save_perl (Coro__State c)
316{ 337{
317 { 338 {
318 dSP; 339 dSP;
319 I32 cxix = cxstack_ix; 340 I32 cxix = cxstack_ix;
320 PERL_CONTEXT *ccstk = cxstack; 341 PERL_CONTEXT *ccstk = cxstack;
331 { 352 {
332 while (cxix >= 0) 353 while (cxix >= 0)
333 { 354 {
334 PERL_CONTEXT *cx = &ccstk[cxix--]; 355 PERL_CONTEXT *cx = &ccstk[cxix--];
335 356
336 if (CxTYPE(cx) == CXt_SUB) 357 if (CxTYPE (cx) == CXt_SUB)
337 { 358 {
338 CV *cv = cx->blk_sub.cv; 359 CV *cv = cx->blk_sub.cv;
339 360
340 if (CvDEPTH (cv)) 361 if (CvDEPTH (cv))
341 { 362 {
342 EXTEND (SP, 3); 363 EXTEND (SP, 3);
343 364
344 PUSHs ((SV *)CvPADLIST(cv)); 365 PUSHs ((SV *)CvPADLIST (cv));
345 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 366 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
346 PUSHs ((SV *)cv); 367 PUSHs ((SV *)cv);
347 368
348 CvDEPTH (cv) = 0; 369 CvDEPTH (cv) = 0;
349 get_padlist (cv); 370 get_padlist (cv);
350 } 371 }
351 } 372 }
352#ifdef CXt_FORMAT 373#ifdef CXt_FORMAT
353 else if (CxTYPE(cx) == CXt_FORMAT) 374 else if (CxTYPE (cx) == CXt_FORMAT)
354 { 375 {
355 /* I never used formats, so how should I know how these are implemented? */ 376 /* I never used formats, so how should I know how these are implemented? */
356 /* my bold guess is as a simple, plain sub... */ 377 /* my bold guess is as a simple, plain sub... */
357 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 378 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
358 } 379 }
368 } 389 }
369 390
370 PUTBACK; 391 PUTBACK;
371 } 392 }
372 393
373 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 394 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
374 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 395 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
375 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 396 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
397 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
376 398
377#define VAR(name,type)c->name = PL_ ## name; 399#define VAR(name,type)c->name = PL_ ## name;
378# include "state.h" 400# include "state.h"
379#undef VAR 401#undef VAR
380} 402}
386 * not usually need a lot of stackspace. 408 * not usually need a lot of stackspace.
387 */ 409 */
388static void 410static void
389coro_init_stacks () 411coro_init_stacks ()
390{ 412{
391 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 413 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
392 PL_curstackinfo->si_type = PERLSI_MAIN; 414 PL_curstackinfo->si_type = PERLSI_MAIN;
393 PL_curstack = PL_curstackinfo->si_stack; 415 PL_curstack = PL_curstackinfo->si_stack;
394 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 416 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
395 417
396 PL_stack_base = AvARRAY(PL_curstack); 418 PL_stack_base = AvARRAY(PL_curstack);
397 PL_stack_sp = PL_stack_base; 419 PL_stack_sp = PL_stack_base;
398 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 420 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
399 421
400 New(50,PL_tmps_stack,96,SV*); 422 New(50,PL_tmps_stack,128,SV*);
401 PL_tmps_floor = -1; 423 PL_tmps_floor = -1;
402 PL_tmps_ix = -1; 424 PL_tmps_ix = -1;
403 PL_tmps_max = 96; 425 PL_tmps_max = 128;
404 426
405 New(54,PL_markstack,16,I32); 427 New(54,PL_markstack,32,I32);
406 PL_markstack_ptr = PL_markstack; 428 PL_markstack_ptr = PL_markstack;
407 PL_markstack_max = PL_markstack + 16; 429 PL_markstack_max = PL_markstack + 32;
408 430
409#ifdef SET_MARK_OFFSET 431#ifdef SET_MARK_OFFSET
410 SET_MARK_OFFSET; 432 SET_MARK_OFFSET;
411#endif 433#endif
412 434
413 New(54,PL_scopestack,16,I32); 435 New(54,PL_scopestack,32,I32);
414 PL_scopestack_ix = 0; 436 PL_scopestack_ix = 0;
415 PL_scopestack_max = 16; 437 PL_scopestack_max = 32;
416 438
417 New(54,PL_savestack,96,ANY); 439 New(54,PL_savestack,64,ANY);
418 PL_savestack_ix = 0; 440 PL_savestack_ix = 0;
419 PL_savestack_max = 96; 441 PL_savestack_max = 64;
420 442
421#if !PERL_VERSION_ATLEAST (5,9,0) 443#if !PERL_VERSION_ATLEAST (5,9,0)
422 New(54,PL_retstack,8,OP*); 444 New(54,PL_retstack,16,OP*);
423 PL_retstack_ix = 0; 445 PL_retstack_ix = 0;
424 PL_retstack_max = 8; 446 PL_retstack_max = 16;
425#endif 447#endif
426} 448}
427 449
428/* 450/*
429 * destroy the stacks, the callchain etc... 451 * destroy the stacks, the callchain etc...
482 * emulate part of the perl startup here. 504 * emulate part of the perl startup here.
483 */ 505 */
484 506
485 coro_init_stacks (); 507 coro_init_stacks ();
486 508
487 PL_curcop = 0; 509 PL_curcop = &PL_compiling;
488 PL_in_eval = 0; 510 PL_in_eval = EVAL_NULL;
489 PL_curpm = 0; 511 PL_curpm = 0;
512 PL_localizing = 0;
513 PL_dirty = 0;
514 PL_restartop = 0;
490 515
491 { 516 {
492 dSP; 517 dSP;
493 LOGOP myop; 518 LOGOP myop;
494 519
495 /* I have no idea why this is needed, but it is */
496 PUSHMARK (SP);
497
498 SvREFCNT_dec (GvAV (PL_defgv)); 520 SvREFCNT_dec (GvAV (PL_defgv));
499 GvAV (PL_defgv) = coro->args; coro->args = 0; 521 GvAV (PL_defgv) = coro->args; coro->args = 0;
500 522
501 Zero (&myop, 1, LOGOP); 523 Zero (&myop, 1, LOGOP);
502 myop.op_next = Nullop; 524 myop.op_next = Nullop;
503 myop.op_flags = OPf_WANT_VOID; 525 myop.op_flags = OPf_WANT_VOID;
504 526
505 PL_op = (OP *)&myop;
506
507 PUSHMARK (SP); 527 PUSHMARK (SP);
508 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 528 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
509 PUTBACK; 529 PUTBACK;
530 PL_op = (OP *)&myop;
510 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 531 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
511 SPAGAIN; 532 SPAGAIN;
512
513 ENTER; /* necessary e.g. for dounwind */
514 } 533 }
534
535 ENTER; /* necessary e.g. for dounwind */
515} 536}
516 537
517static void 538static void
518free_coro_mortal () 539free_coro_mortal ()
519{ 540{
522 SvREFCNT_dec (coro_mortal); 543 SvREFCNT_dec (coro_mortal);
523 coro_mortal = 0; 544 coro_mortal = 0;
524 } 545 }
525} 546}
526 547
548/* inject a fake call to Coro::State::_cctx_init into the execution */
527static void NOINLINE 549static void NOINLINE
528prepare_cctx (coro_cctx *cctx) 550prepare_cctx (coro_cctx *cctx)
529{ 551{
530 dSP; 552 dSP;
531 LOGOP myop; 553 LOGOP myop;
532 554
533 Zero (&myop, 1, LOGOP); 555 Zero (&myop, 1, LOGOP);
534 myop.op_next = PL_op; 556 myop.op_next = PL_op;
535 myop.op_flags = OPf_WANT_VOID; 557 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
536
537 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
538 558
539 PUSHMARK (SP); 559 PUSHMARK (SP);
560 EXTEND (SP, 2);
561 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
540 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 562 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
541 PUTBACK; 563 PUTBACK;
564 PL_op = (OP *)&myop;
542 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 565 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
543 SPAGAIN; 566 SPAGAIN;
544} 567}
545 568
546static void 569static void
547coro_run (void *arg) 570coro_run (void *arg)
548{ 571{
549 /* coro_run is the alternative epilogue of transfer() */ 572 /* coro_run is the alternative tail of transfer(), so unlock here. */
550 UNLOCK; 573 UNLOCK;
551 574
552 /* 575 /*
553 * this is a _very_ stripped down perl interpreter ;) 576 * this is a _very_ stripped down perl interpreter ;)
554 */ 577 */
556 579
557 /* inject call to cctx_init */ 580 /* inject call to cctx_init */
558 prepare_cctx ((coro_cctx *)arg); 581 prepare_cctx ((coro_cctx *)arg);
559 582
560 /* somebody will hit me for both perl_run and PL_restartop */ 583 /* somebody will hit me for both perl_run and PL_restartop */
584 PL_restartop = PL_op;
561 perl_run (PL_curinterp); 585 perl_run (PL_curinterp);
562 586
563 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 587 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
564 abort (); 588 abort ();
565} 589}
574 New (0, cctx, 1, coro_cctx); 598 New (0, cctx, 1, coro_cctx);
575 599
576#if HAVE_MMAP 600#if HAVE_MMAP
577 601
578 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 602 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
579 /* mmap suppsedly does allocate-on-write for us */ 603 /* mmap supposedly does allocate-on-write for us */
580 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 604 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
581 605
582 if (cctx->sptr == (void *)-1) 606 if (cctx->sptr == (void *)-1)
583 { 607 {
584 perror ("FATAL: unable to mmap stack for coroutine"); 608 perror ("FATAL: unable to mmap stack for coroutine");
613 637
614 return cctx; 638 return cctx;
615} 639}
616 640
617static void 641static void
618cctx_free (coro_cctx *cctx) 642cctx_destroy (coro_cctx *cctx)
619{ 643{
620 if (!cctx) 644 if (!cctx)
621 return; 645 return;
622 646
623 --cctx_count; 647 --cctx_count;
640{ 664{
641 coro_cctx *cctx; 665 coro_cctx *cctx;
642 666
643 if (cctx_first) 667 if (cctx_first)
644 { 668 {
645 --cctx_idle;
646 cctx = cctx_first; 669 cctx = cctx_first;
647 cctx_first = cctx->next; 670 cctx_first = cctx->next;
671 --cctx_idle;
648 } 672 }
649 else 673 else
650 { 674 {
651 cctx = cctx_new (); 675 cctx = cctx_new ();
652 PL_op = PL_op->op_next; 676 PL_op = PL_op->op_next;
656} 680}
657 681
658static void 682static void
659cctx_put (coro_cctx *cctx) 683cctx_put (coro_cctx *cctx)
660{ 684{
685 /* free another cctx if overlimit */
686 if (cctx_idle >= MAX_IDLE_CCTX)
687 {
688 coro_cctx *first = cctx_first;
689 cctx_first = first->next;
690 --cctx_idle;
691
692 assert (!first->inuse);
693 cctx_destroy (first);
694 }
695
661 ++cctx_idle; 696 ++cctx_idle;
662 cctx->next = cctx_first; 697 cctx->next = cctx_first;
663 cctx_first = cctx; 698 cctx_first = cctx;
664} 699}
665 700
666/* never call directly, always through the coro_state_transfer global variable */ 701/* never call directly, always through the coro_state_transfer global variable */
667static void NOINLINE 702static void NOINLINE
668transfer (struct coro *prev, struct coro *next, int flags) 703transfer (struct coro *prev, struct coro *next)
669{ 704{
670 dSTACKLEVEL; 705 dSTACKLEVEL;
671 706
672 /* sometimes transfer is only called to set idle_sp */ 707 /* sometimes transfer is only called to set idle_sp */
673 if (flags == TRANSFER_SET_STACKLEVEL) 708 if (!next)
674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 709 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
675 else if (prev != next) 710 else if (prev != next)
676 { 711 {
677 coro_cctx *prev__cctx; 712 coro_cctx *prev__cctx;
678 713
679 if (!prev->cctx) 714 if (prev->flags & CF_NEW)
680 { 715 {
681 /* create a new empty context */ 716 /* create a new empty context */
682 Newz (0, prev->cctx, 1, coro_cctx); 717 Newz (0, prev->cctx, 1, coro_cctx);
683 prev->cctx->inuse = 1; 718 prev->cctx->inuse = 1;
719 prev->flags &= ~CF_NEW;
684 prev->flags |= CF_RUNNING; 720 prev->flags |= CF_RUNNING;
685 } 721 }
686 722
723 /*TODO: must not croak here */
687 if (!prev->flags & CF_RUNNING) 724 if (!prev->flags & CF_RUNNING)
688 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 725 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
689 726
690 if (next->flags & CF_RUNNING) 727 if (next->flags & CF_RUNNING)
691 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 728 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
693 prev->flags &= ~CF_RUNNING; 730 prev->flags &= ~CF_RUNNING;
694 next->flags |= CF_RUNNING; 731 next->flags |= CF_RUNNING;
695 732
696 LOCK; 733 LOCK;
697 734
698 if (next->mainstack) 735 if (next->flags & CF_NEW)
699 {
700 /* coroutine already started */
701 SAVE (prev, flags);
702 LOAD (next);
703 }
704 else
705 { 736 {
706 /* need to start coroutine */ 737 /* need to start coroutine */
738 next->flags &= ~CF_NEW;
707 /* first get rid of the old state */ 739 /* first get rid of the old state */
708 SAVE (prev, -1); 740 save_perl (prev);
709 /* setup coroutine call */ 741 /* setup coroutine call */
710 setup_coro (next); 742 setup_coro (next);
711 /* need a new stack */ 743 /* need a new stack */
712 assert (!next->stack); 744 assert (!next->stack);
713 } 745 }
746 else
747 {
748 /* coroutine already started */
749 save_perl (prev);
750 load_perl (next);
751 }
714 752
715 prev__cctx = prev->cctx; 753 prev__cctx = prev->cctx;
716 754
717 /* possibly "free" the cctx */ 755 /* possibly "free" the cctx */
718 if (prev__cctx->idle_sp == STACKLEVEL) 756 if (prev__cctx->idle_sp == STACKLEVEL)
719 { 757 {
720 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 758 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
721 assert (PL_top_env == prev__cctx->top_env); 759 assert (PL_top_env == prev__cctx->top_env);
722 760
761 prev->cctx = 0;
762
723 cctx_put (prev__cctx); 763 cctx_put (prev__cctx);
724 prev->cctx = 0; 764 prev__cctx->inuse = 0;
725 } 765 }
726 766
727 if (!next->cctx) 767 if (!next->cctx)
768 {
728 next->cctx = cctx_get (); 769 next->cctx = cctx_get ();
770 assert (!next->cctx->inuse);
771 next->cctx->inuse = 1;
772 }
729 773
730 if (prev__cctx != next->cctx) 774 if (prev__cctx != next->cctx)
731 { 775 {
732 assert ( prev__cctx->inuse);
733 assert (!next->cctx->inuse);
734
735 prev__cctx->inuse = 0;
736 next->cctx->inuse = 1;
737
738 prev__cctx->top_env = PL_top_env; 776 prev__cctx->top_env = PL_top_env;
739 PL_top_env = next->cctx->top_env; 777 PL_top_env = next->cctx->top_env;
740 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 778 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
741 } 779 }
742 780
747} 785}
748 786
749struct transfer_args 787struct transfer_args
750{ 788{
751 struct coro *prev, *next; 789 struct coro *prev, *next;
752 int flags;
753}; 790};
754 791
755#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 792#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
756 793
757static void 794static void
758coro_state_destroy (struct coro *coro) 795coro_state_destroy (struct coro *coro)
759{ 796{
760 if (coro->refcnt--) 797 if (coro->refcnt--)
761 return; 798 return;
762
763 if (coro->flags & CF_RUNNING)
764 croak ("FATAL: tried to destroy currently running coroutine");
765 799
766 if (coro->mainstack && coro->mainstack != main_mainstack) 800 if (coro->mainstack && coro->mainstack != main_mainstack)
767 { 801 {
768 struct coro temp; 802 struct coro temp;
803 Zero (&temp, 1, struct coro);
804 temp.save = CORO_SAVE_ALL;
769 805
770 SAVE ((&temp), TRANSFER_SAVE_ALL); 806 if (coro->flags & CF_RUNNING)
771 LOAD (coro); 807 croak ("FATAL: tried to destroy currently running coroutine");
808
809 save_perl (&temp);
810 load_perl (coro);
772 811
773 coro_destroy_stacks (); 812 coro_destroy_stacks ();
774 813
775 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 814 load_perl (&temp); /* this will get rid of defsv etc.. */
776 815
777 coro->mainstack = 0; 816 coro->mainstack = 0;
778 } 817 }
779 818
780 cctx_free (coro->cctx); 819 cctx_destroy (coro->cctx);
781 SvREFCNT_dec (coro->args); 820 SvREFCNT_dec (coro->args);
782 Safefree (coro); 821 Safefree (coro);
783} 822}
784 823
785static int 824static int
835 assert (mg->mg_type == PERL_MAGIC_ext); 874 assert (mg->mg_type == PERL_MAGIC_ext);
836 return (struct coro *)mg->mg_ptr; 875 return (struct coro *)mg->mg_ptr;
837} 876}
838 877
839static void 878static void
840prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 879prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
841{ 880{
842 ta->prev = SvSTATE (prev); 881 ta->prev = SvSTATE (prev_sv);
843 ta->next = SvSTATE (next); 882 ta->next = SvSTATE (next_sv);
844 ta->flags = flags;
845} 883}
846 884
847static void 885static void
848api_transfer (SV *prev, SV *next, int flags) 886api_transfer (SV *prev_sv, SV *next_sv)
849{ 887{
850 dTHX;
851 struct transfer_args ta; 888 struct transfer_args ta;
852 889
853 prepare_transfer (&ta, prev, next, flags); 890 prepare_transfer (&ta, prev_sv, next_sv);
854 TRANSFER (ta); 891 TRANSFER (ta);
892}
893
894static int
895api_save (SV *coro_sv, int new_save)
896{
897 struct coro *coro = SvSTATE (coro_sv);
898 int old_save = coro->save;
899
900 if (new_save >= 0)
901 coro->save = new_save;
902
903 return old_save;
855} 904}
856 905
857/** Coro ********************************************************************/ 906/** Coro ********************************************************************/
858 907
859#define PRIO_MAX 3 908#define PRIO_MAX 3
862#define PRIO_LOW -1 911#define PRIO_LOW -1
863#define PRIO_IDLE -3 912#define PRIO_IDLE -3
864#define PRIO_MIN -4 913#define PRIO_MIN -4
865 914
866/* for Coro.pm */ 915/* for Coro.pm */
867static GV *coro_current, *coro_idle; 916static SV *coro_current;
868static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 917static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
869static int coro_nready; 918static int coro_nready;
870 919
871static void 920static void
872coro_enq (SV *coro_sv) 921coro_enq (SV *coro_sv)
905 coro = SvSTATE (coro_sv); 954 coro = SvSTATE (coro_sv);
906 955
907 if (coro->flags & CF_READY) 956 if (coro->flags & CF_READY)
908 return 0; 957 return 0;
909 958
959#if 0 /* this is actually harmless */
910 if (coro->flags & CF_RUNNING) 960 if (coro->flags & CF_RUNNING)
911 croak ("Coro::ready called on currently running coroutine"); 961 croak ("Coro::ready called on currently running coroutine");
962#endif
912 963
913 coro->flags |= CF_READY; 964 coro->flags |= CF_READY;
914 965
915 LOCK; 966 LOCK;
916 coro_enq (SvREFCNT_inc (coro_sv)); 967 coro_enq (SvREFCNT_inc (coro_sv));
926} 977}
927 978
928static void 979static void
929prepare_schedule (struct transfer_args *ta) 980prepare_schedule (struct transfer_args *ta)
930{ 981{
931 SV *current, *prev, *next; 982 SV *prev, *next;
932
933 current = GvSV (coro_current);
934 983
935 for (;;) 984 for (;;)
936 { 985 {
937 LOCK; 986 LOCK;
938 next = coro_deq (PRIO_MIN); 987 next = coro_deq (PRIO_MIN);
947 ENTER; 996 ENTER;
948 SAVETMPS; 997 SAVETMPS;
949 998
950 PUSHMARK (SP); 999 PUSHMARK (SP);
951 PUTBACK; 1000 PUTBACK;
952 call_sv (GvSV (coro_idle), G_DISCARD); 1001 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
953 1002
954 FREETMPS; 1003 FREETMPS;
955 LEAVE; 1004 LEAVE;
956 } 1005 }
957 } 1006 }
958 1007
959 prev = SvRV (current); 1008 prev = SvRV (coro_current);
960 SvRV (current) = next; 1009 SvRV_set (coro_current, next);
961 1010
962 /* free this only after the transfer */ 1011 /* free this only after the transfer */
963 LOCK; 1012 LOCK;
964 free_coro_mortal (); 1013 free_coro_mortal ();
965 UNLOCK; 1014 UNLOCK;
966 coro_mortal = prev; 1015 coro_mortal = prev;
967 1016
1017 assert (!SvROK(prev));//D
1018 assert (!SvROK(next));//D
1019
968 ta->prev = SvSTATE (prev); 1020 ta->prev = SvSTATE (prev);
969 ta->next = SvSTATE (next); 1021 ta->next = SvSTATE (next);
970 ta->flags = TRANSFER_SAVE_ALL;
971 1022
1023 assert (ta->next->flags & CF_READY);
972 ta->next->flags &= ~CF_READY; 1024 ta->next->flags &= ~CF_READY;
973} 1025}
974 1026
975static void 1027static void
976prepare_cede (struct transfer_args *ta) 1028prepare_cede (struct transfer_args *ta)
977{ 1029{
978 api_ready (GvSV (coro_current)); 1030 api_ready (coro_current);
979 1031
980 prepare_schedule (ta); 1032 prepare_schedule (ta);
981} 1033}
982 1034
983static void 1035static void
984api_schedule (void) 1036api_schedule (void)
985{ 1037{
986 dTHX;
987 struct transfer_args ta; 1038 struct transfer_args ta;
988 1039
989 prepare_schedule (&ta); 1040 prepare_schedule (&ta);
990 TRANSFER (ta); 1041 TRANSFER (ta);
991} 1042}
992 1043
993static void 1044static void
994api_cede (void) 1045api_cede (void)
995{ 1046{
996 dTHX;
997 struct transfer_args ta; 1047 struct transfer_args ta;
998 1048
999 prepare_cede (&ta); 1049 prepare_cede (&ta);
1000 TRANSFER (ta); 1050 TRANSFER (ta);
1001} 1051}
1011#endif 1061#endif
1012 BOOT_PAGESIZE; 1062 BOOT_PAGESIZE;
1013 1063
1014 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1064 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1015 1065
1016 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1066 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1017 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1067 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1018 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1068 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1069 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1070 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1019 1071
1020 main_mainstack = PL_mainstack; 1072 main_mainstack = PL_mainstack;
1021 1073
1022 coroapi.ver = CORO_API_VERSION; 1074 coroapi.ver = CORO_API_VERSION;
1023 coroapi.transfer = api_transfer; 1075 coroapi.transfer = api_transfer;
1033 HV *hv; 1085 HV *hv;
1034 int i; 1086 int i;
1035 1087
1036 Newz (0, coro, 1, struct coro); 1088 Newz (0, coro, 1, struct coro);
1037 coro->args = newAV (); 1089 coro->args = newAV ();
1090 coro->save = CORO_SAVE_ALL;
1091 coro->flags = CF_NEW;
1038 1092
1039 hv = newHV (); 1093 hv = newHV ();
1040 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1094 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1041 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1095 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1042 1096
1043 for (i = 1; i < items; i++) 1097 for (i = 1; i < items; i++)
1044 av_push (coro->args, newSVsv (ST (i))); 1098 av_push (coro->args, newSVsv (ST (i)));
1045} 1099}
1046 OUTPUT: 1100 OUTPUT:
1101 RETVAL
1102
1103int
1104save (SV *coro, int new_save = -1)
1105 CODE:
1106 RETVAL = api_save (coro, new_save);
1107 OUTPUT:
1047 RETVAL 1108 RETVAL
1048 1109
1049void 1110void
1050_set_stacklevel (...) 1111_set_stacklevel (...)
1051 ALIAS: 1112 ALIAS:
1052 Coro::State::transfer = 1 1113 Coro::State::transfer = 1
1053 Coro::schedule = 2 1114 Coro::schedule = 2
1054 Coro::cede = 3 1115 Coro::cede = 3
1055 Coro::Cont::yield = 4
1056 CODE: 1116 CODE:
1057{ 1117{
1058 struct transfer_args ta; 1118 struct transfer_args ta;
1059 1119
1060 switch (ix) 1120 switch (ix)
1061 { 1121 {
1062 case 0: 1122 case 0:
1063 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1123 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1064 ta.next = 0; 1124 ta.next = 0;
1065 ta.flags = TRANSFER_SET_STACKLEVEL;
1066 break; 1125 break;
1067 1126
1068 case 1: 1127 case 1:
1069 if (items != 3) 1128 if (items != 2)
1070 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1129 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1071 1130
1072 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1131 prepare_transfer (&ta, ST (0), ST (1));
1073 break; 1132 break;
1074 1133
1075 case 2: 1134 case 2:
1076 prepare_schedule (&ta); 1135 prepare_schedule (&ta);
1077 break; 1136 break;
1078 1137
1079 case 3: 1138 case 3:
1080 prepare_cede (&ta); 1139 prepare_cede (&ta);
1081 break; 1140 break;
1082
1083 case 4:
1084 {
1085 SV *yieldstack;
1086 SV *sv;
1087 AV *defav = GvAV (PL_defgv);
1088
1089 yieldstack = *hv_fetch (
1090 (HV *)SvRV (GvSV (coro_current)),
1091 "yieldstack", sizeof ("yieldstack") - 1,
1092 0
1093 );
1094
1095 /* set up @_ -- ugly */
1096 av_clear (defav);
1097 av_fill (defav, items - 1);
1098 while (items--)
1099 av_store (defav, items, SvREFCNT_inc (ST(items)));
1100
1101 sv = av_pop ((AV *)SvRV (yieldstack));
1102 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1103 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1104 ta.flags = 0;
1105 SvREFCNT_dec (sv);
1106 }
1107 break;
1108
1109 } 1141 }
1110 1142
1111 TRANSFER (ta); 1143 TRANSFER (ta);
1112} 1144}
1113 1145
1157 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1189 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1158 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1190 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1159 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1191 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1160 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1192 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1161 1193
1162 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1194 coro_current = get_sv ("Coro::current", FALSE);
1163 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1195 SvREADONLY_on (coro_current);
1164 1196
1165 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1197 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1166 coro_ready[i] = newAV (); 1198 coro_ready[i] = newAV ();
1167 1199
1168 { 1200 {
1169 SV *sv = perl_get_sv("Coro::API", 1); 1201 SV *sv = perl_get_sv("Coro::API", 1);
1170 1202
1171 coroapi.schedule = api_schedule; 1203 coroapi.schedule = api_schedule;
1204 coroapi.save = api_save;
1172 coroapi.cede = api_cede; 1205 coroapi.cede = api_cede;
1173 coroapi.ready = api_ready; 1206 coroapi.ready = api_ready;
1174 coroapi.is_ready = api_is_ready; 1207 coroapi.is_ready = api_is_ready;
1175 coroapi.nready = &coro_nready; 1208 coroapi.nready = &coro_nready;
1176 coroapi.current = coro_current; 1209 coroapi.current = coro_current;
1178 GCoroAPI = &coroapi; 1211 GCoroAPI = &coroapi;
1179 sv_setiv (sv, (IV)&coroapi); 1212 sv_setiv (sv, (IV)&coroapi);
1180 SvREADONLY_on (sv); 1213 SvREADONLY_on (sv);
1181 } 1214 }
1182} 1215}
1216
1217void
1218_set_current (SV *current)
1219 PROTOTYPE: $
1220 CODE:
1221 SvREFCNT_dec (SvRV (coro_current));
1222 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1183 1223
1184int 1224int
1185prio (Coro::State coro, int newprio = 0) 1225prio (Coro::State coro, int newprio = 0)
1186 ALIAS: 1226 ALIAS:
1187 nice = 1 1227 nice = 1
1229 1269
1230SV * 1270SV *
1231_get_state () 1271_get_state ()
1232 CODE: 1272 CODE:
1233{ 1273{
1234 struct { 1274 struct io_state *data;
1235 int errorno;
1236 int laststype;
1237 int laststatval;
1238 Stat_t statcache;
1239 } data;
1240 1275
1276 RETVAL = newSV (sizeof (struct io_state));
1277 data = (struct io_state *)SvPVX (RETVAL);
1278 SvCUR_set (RETVAL, sizeof (struct io_state));
1279 SvPOK_only (RETVAL);
1280
1241 data.errorno = errno; 1281 data->errorno = errno;
1242 data.laststype = PL_laststype; 1282 data->laststype = PL_laststype;
1243 data.laststatval = PL_laststatval; 1283 data->laststatval = PL_laststatval;
1244 data.statcache = PL_statcache; 1284 data->statcache = PL_statcache;
1245
1246 RETVAL = newSVpvn ((char *)&data, sizeof data);
1247} 1285}
1248 OUTPUT: 1286 OUTPUT:
1249 RETVAL 1287 RETVAL
1250 1288
1251void 1289void
1252_set_state (char *data_) 1290_set_state (char *data_)
1253 PROTOTYPE: $ 1291 PROTOTYPE: $
1254 CODE: 1292 CODE:
1255{ 1293{
1256 struct { 1294 struct io_state *data = (void *)data_;
1257 int errorno;
1258 int laststype;
1259 int laststatval;
1260 Stat_t statcache;
1261 } *data = (void *)data_;
1262 1295
1263 errno = data->errorno; 1296 errno = data->errorno;
1264 PL_laststype = data->laststype; 1297 PL_laststype = data->laststype;
1265 PL_laststatval = data->laststatval; 1298 PL_laststatval = data->laststatval;
1266 PL_statcache = data->statcache; 1299 PL_statcache = data->statcache;
1267} 1300}
1301

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines