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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines