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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines