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.112 by root, Thu Nov 30 20:52:52 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 {
758 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
720 assert (PL_top_env == prev__cctx->top_env); 759 assert (PL_top_env == prev__cctx->top_env);
721 760
761 prev->cctx = 0;
762
722 cctx_put (prev__cctx); 763 cctx_put (prev__cctx);
723 prev->cctx = 0; 764 prev__cctx->inuse = 0;
724 } 765 }
725 766
726 if (!next->cctx) 767 if (!next->cctx)
768 {
727 next->cctx = cctx_get (); 769 next->cctx = cctx_get ();
770 assert (!next->cctx->inuse);
771 next->cctx->inuse = 1;
772 }
728 773
729 if (prev__cctx != next->cctx) 774 if (prev__cctx != next->cctx)
730 { 775 {
731 assert ( prev__cctx->inuse);
732 assert (!next->cctx->inuse);
733
734 prev__cctx->inuse = 0;
735 next->cctx->inuse = 1;
736
737 prev__cctx->top_env = PL_top_env; 776 prev__cctx->top_env = PL_top_env;
738 PL_top_env = next->cctx->top_env; 777 PL_top_env = next->cctx->top_env;
739 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 778 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
740 } 779 }
741 780
746} 785}
747 786
748struct transfer_args 787struct transfer_args
749{ 788{
750 struct coro *prev, *next; 789 struct coro *prev, *next;
751 int flags;
752}; 790};
753 791
754#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 792#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
755 793
756static void 794static void
757coro_state_destroy (struct coro *coro) 795coro_state_destroy (struct coro *coro)
758{ 796{
759 if (coro->refcnt--) 797 if (coro->refcnt--)
760 return; 798 return;
761
762 if (coro->flags & CF_RUNNING)
763 croak ("FATAL: tried to destroy currently running coroutine");
764 799
765 if (coro->mainstack && coro->mainstack != main_mainstack) 800 if (coro->mainstack && coro->mainstack != main_mainstack)
766 { 801 {
767 struct coro temp; 802 struct coro temp;
803 Zero (&temp, 1, struct coro);
804 temp.save = CORO_SAVE_ALL;
768 805
769 SAVE ((&temp), TRANSFER_SAVE_ALL); 806 if (coro->flags & CF_RUNNING)
770 LOAD (coro); 807 croak ("FATAL: tried to destroy currently running coroutine");
808
809 save_perl (&temp);
810 load_perl (coro);
771 811
772 coro_destroy_stacks (); 812 coro_destroy_stacks ();
773 813
774 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 814 load_perl (&temp); /* this will get rid of defsv etc.. */
775 815
776 coro->mainstack = 0; 816 coro->mainstack = 0;
777 } 817 }
778 818
779 cctx_free (coro->cctx); 819 cctx_destroy (coro->cctx);
780 SvREFCNT_dec (coro->args); 820 SvREFCNT_dec (coro->args);
781 Safefree (coro); 821 Safefree (coro);
782} 822}
783 823
784static int 824static int
834 assert (mg->mg_type == PERL_MAGIC_ext); 874 assert (mg->mg_type == PERL_MAGIC_ext);
835 return (struct coro *)mg->mg_ptr; 875 return (struct coro *)mg->mg_ptr;
836} 876}
837 877
838static void 878static void
839prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 879prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
840{ 880{
841 ta->prev = SvSTATE (prev); 881 ta->prev = SvSTATE (prev_sv);
842 ta->next = SvSTATE (next); 882 ta->next = SvSTATE (next_sv);
843 ta->flags = flags;
844} 883}
845 884
846static void 885static void
847api_transfer (SV *prev, SV *next, int flags) 886api_transfer (SV *prev_sv, SV *next_sv)
848{ 887{
849 dTHX;
850 struct transfer_args ta; 888 struct transfer_args ta;
851 889
852 prepare_transfer (&ta, prev, next, flags); 890 prepare_transfer (&ta, prev_sv, next_sv);
853 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;
854} 904}
855 905
856/** Coro ********************************************************************/ 906/** Coro ********************************************************************/
857 907
858#define PRIO_MAX 3 908#define PRIO_MAX 3
861#define PRIO_LOW -1 911#define PRIO_LOW -1
862#define PRIO_IDLE -3 912#define PRIO_IDLE -3
863#define PRIO_MIN -4 913#define PRIO_MIN -4
864 914
865/* for Coro.pm */ 915/* for Coro.pm */
866static GV *coro_current, *coro_idle; 916static SV *coro_current;
867static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 917static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
868static int coro_nready; 918static int coro_nready;
869 919
870static void 920static void
871coro_enq (SV *coro_sv) 921coro_enq (SV *coro_sv)
904 coro = SvSTATE (coro_sv); 954 coro = SvSTATE (coro_sv);
905 955
906 if (coro->flags & CF_READY) 956 if (coro->flags & CF_READY)
907 return 0; 957 return 0;
908 958
959#if 0 /* this is actually harmless */
909 if (coro->flags & CF_RUNNING) 960 if (coro->flags & CF_RUNNING)
910 croak ("Coro::ready called on currently running coroutine"); 961 croak ("Coro::ready called on currently running coroutine");
962#endif
911 963
912 coro->flags |= CF_READY; 964 coro->flags |= CF_READY;
913 965
914 LOCK; 966 LOCK;
915 coro_enq (SvREFCNT_inc (coro_sv)); 967 coro_enq (SvREFCNT_inc (coro_sv));
925} 977}
926 978
927static void 979static void
928prepare_schedule (struct transfer_args *ta) 980prepare_schedule (struct transfer_args *ta)
929{ 981{
930 SV *current, *prev, *next; 982 SV *prev, *next;
931
932 current = GvSV (coro_current);
933 983
934 for (;;) 984 for (;;)
935 { 985 {
936 LOCK; 986 LOCK;
937 next = coro_deq (PRIO_MIN); 987 next = coro_deq (PRIO_MIN);
946 ENTER; 996 ENTER;
947 SAVETMPS; 997 SAVETMPS;
948 998
949 PUSHMARK (SP); 999 PUSHMARK (SP);
950 PUTBACK; 1000 PUTBACK;
951 call_sv (GvSV (coro_idle), G_DISCARD); 1001 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
952 1002
953 FREETMPS; 1003 FREETMPS;
954 LEAVE; 1004 LEAVE;
955 } 1005 }
956 } 1006 }
957 1007
958 prev = SvRV (current); 1008 prev = SvRV (coro_current);
959 SvRV (current) = next; 1009 SvRV_set (coro_current, next);
960 1010
961 /* free this only after the transfer */ 1011 /* free this only after the transfer */
962 LOCK; 1012 LOCK;
963 free_coro_mortal (); 1013 free_coro_mortal ();
964 UNLOCK; 1014 UNLOCK;
965 coro_mortal = prev; 1015 coro_mortal = prev;
966 1016
1017 assert (!SvROK(prev));//D
1018 assert (!SvROK(next));//D
1019
967 ta->prev = SvSTATE (prev); 1020 ta->prev = SvSTATE (prev);
968 ta->next = SvSTATE (next); 1021 ta->next = SvSTATE (next);
969 ta->flags = TRANSFER_SAVE_ALL;
970 1022
1023 assert (ta->next->flags & CF_READY);
971 ta->next->flags &= ~CF_READY; 1024 ta->next->flags &= ~CF_READY;
972} 1025}
973 1026
974static void 1027static void
975prepare_cede (struct transfer_args *ta) 1028prepare_cede (struct transfer_args *ta)
976{ 1029{
977 api_ready (GvSV (coro_current)); 1030 api_ready (coro_current);
978 1031
979 prepare_schedule (ta); 1032 prepare_schedule (ta);
980} 1033}
981 1034
982static void 1035static void
983api_schedule (void) 1036api_schedule (void)
984{ 1037{
985 dTHX;
986 struct transfer_args ta; 1038 struct transfer_args ta;
987 1039
988 prepare_schedule (&ta); 1040 prepare_schedule (&ta);
989 TRANSFER (ta); 1041 TRANSFER (ta);
990} 1042}
991 1043
992static void 1044static void
993api_cede (void) 1045api_cede (void)
994{ 1046{
995 dTHX;
996 struct transfer_args ta; 1047 struct transfer_args ta;
997 1048
998 prepare_cede (&ta); 1049 prepare_cede (&ta);
999 TRANSFER (ta); 1050 TRANSFER (ta);
1000} 1051}
1010#endif 1061#endif
1011 BOOT_PAGESIZE; 1062 BOOT_PAGESIZE;
1012 1063
1013 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1064 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1014 1065
1015 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1066 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1016 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1067 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1017 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));
1018 1071
1019 main_mainstack = PL_mainstack; 1072 main_mainstack = PL_mainstack;
1020 1073
1021 coroapi.ver = CORO_API_VERSION; 1074 coroapi.ver = CORO_API_VERSION;
1022 coroapi.transfer = api_transfer; 1075 coroapi.transfer = api_transfer;
1032 HV *hv; 1085 HV *hv;
1033 int i; 1086 int i;
1034 1087
1035 Newz (0, coro, 1, struct coro); 1088 Newz (0, coro, 1, struct coro);
1036 coro->args = newAV (); 1089 coro->args = newAV ();
1090 coro->save = CORO_SAVE_ALL;
1091 coro->flags = CF_NEW;
1037 1092
1038 hv = newHV (); 1093 hv = newHV ();
1039 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;
1040 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1095 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1041 1096
1042 for (i = 1; i < items; i++) 1097 for (i = 1; i < items; i++)
1043 av_push (coro->args, newSVsv (ST (i))); 1098 av_push (coro->args, newSVsv (ST (i)));
1044} 1099}
1045 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:
1046 RETVAL 1108 RETVAL
1047 1109
1048void 1110void
1049_set_stacklevel (...) 1111_set_stacklevel (...)
1050 ALIAS: 1112 ALIAS:
1051 Coro::State::transfer = 1 1113 Coro::State::transfer = 1
1052 Coro::schedule = 2 1114 Coro::schedule = 2
1053 Coro::cede = 3 1115 Coro::cede = 3
1054 Coro::Cont::yield = 4
1055 CODE: 1116 CODE:
1056{ 1117{
1057 struct transfer_args ta; 1118 struct transfer_args ta;
1058 1119
1059 switch (ix) 1120 switch (ix)
1060 { 1121 {
1061 case 0: 1122 case 0:
1062 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1123 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1063 ta.next = 0; 1124 ta.next = 0;
1064 ta.flags = TRANSFER_SET_STACKLEVEL;
1065 break; 1125 break;
1066 1126
1067 case 1: 1127 case 1:
1068 if (items != 3) 1128 if (items != 2)
1069 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);
1070 1130
1071 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1131 prepare_transfer (&ta, ST (0), ST (1));
1072 break; 1132 break;
1073 1133
1074 case 2: 1134 case 2:
1075 prepare_schedule (&ta); 1135 prepare_schedule (&ta);
1076 break; 1136 break;
1077 1137
1078 case 3: 1138 case 3:
1079 prepare_cede (&ta); 1139 prepare_cede (&ta);
1080 break; 1140 break;
1081
1082 case 4:
1083 {
1084 SV *yieldstack;
1085 SV *sv;
1086 AV *defav = GvAV (PL_defgv);
1087
1088 yieldstack = *hv_fetch (
1089 (HV *)SvRV (GvSV (coro_current)),
1090 "yieldstack", sizeof ("yieldstack") - 1,
1091 0
1092 );
1093
1094 /* set up @_ -- ugly */
1095 av_clear (defav);
1096 av_fill (defav, items - 1);
1097 while (items--)
1098 av_store (defav, items, SvREFCNT_inc (ST(items)));
1099
1100 sv = av_pop ((AV *)SvRV (yieldstack));
1101 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1102 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1103 ta.flags = 0;
1104 SvREFCNT_dec (sv);
1105 }
1106 break;
1107
1108 } 1141 }
1109 1142
1110 TRANSFER (ta); 1143 TRANSFER (ta);
1111} 1144}
1112 1145
1156 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1189 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1157 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1190 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1158 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1191 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1159 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1192 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1160 1193
1161 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1194 coro_current = get_sv ("Coro::current", FALSE);
1162 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1195 SvREADONLY_on (coro_current);
1163 1196
1164 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1197 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1165 coro_ready[i] = newAV (); 1198 coro_ready[i] = newAV ();
1166 1199
1167 { 1200 {
1168 SV *sv = perl_get_sv("Coro::API", 1); 1201 SV *sv = perl_get_sv("Coro::API", 1);
1169 1202
1170 coroapi.schedule = api_schedule; 1203 coroapi.schedule = api_schedule;
1204 coroapi.save = api_save;
1171 coroapi.cede = api_cede; 1205 coroapi.cede = api_cede;
1172 coroapi.ready = api_ready; 1206 coroapi.ready = api_ready;
1173 coroapi.is_ready = api_is_ready; 1207 coroapi.is_ready = api_is_ready;
1174 coroapi.nready = &coro_nready; 1208 coroapi.nready = &coro_nready;
1175 coroapi.current = coro_current; 1209 coroapi.current = coro_current;
1177 GCoroAPI = &coroapi; 1211 GCoroAPI = &coroapi;
1178 sv_setiv (sv, (IV)&coroapi); 1212 sv_setiv (sv, (IV)&coroapi);
1179 SvREADONLY_on (sv); 1213 SvREADONLY_on (sv);
1180 } 1214 }
1181} 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)));
1182 1223
1183int 1224int
1184prio (Coro::State coro, int newprio = 0) 1225prio (Coro::State coro, int newprio = 0)
1185 ALIAS: 1226 ALIAS:
1186 nice = 1 1227 nice = 1
1228 1269
1229SV * 1270SV *
1230_get_state () 1271_get_state ()
1231 CODE: 1272 CODE:
1232{ 1273{
1233 struct { 1274 struct io_state *data;
1234 int errorno;
1235 int laststype;
1236 int laststatval;
1237 Stat_t statcache;
1238 } data;
1239 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
1240 data.errorno = errno; 1281 data->errorno = errno;
1241 data.laststype = PL_laststype; 1282 data->laststype = PL_laststype;
1242 data.laststatval = PL_laststatval; 1283 data->laststatval = PL_laststatval;
1243 data.statcache = PL_statcache; 1284 data->statcache = PL_statcache;
1244
1245 RETVAL = newSVpvn ((char *)&data, sizeof data);
1246} 1285}
1247 OUTPUT: 1286 OUTPUT:
1248 RETVAL 1287 RETVAL
1249 1288
1250void 1289void
1251_set_state (char *data_) 1290_set_state (char *data_)
1252 PROTOTYPE: $ 1291 PROTOTYPE: $
1253 CODE: 1292 CODE:
1254{ 1293{
1255 struct { 1294 struct io_state *data = (void *)data_;
1256 int errorno;
1257 int laststype;
1258 int laststatval;
1259 Stat_t statcache;
1260 } *data = (void *)data_;
1261 1295
1262 errno = data->errorno; 1296 errno = data->errorno;
1263 PL_laststype = data->laststype; 1297 PL_laststype = data->laststype;
1264 PL_laststatval = data->laststatval; 1298 PL_laststatval = data->laststatval;
1265 PL_statcache = data->statcache; 1299 PL_statcache = data->statcache;
1266} 1300}
1301

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines