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.115 by root, Fri Dec 1 13:36:08 2006 UTC vs.
Revision 1.124 by root, Tue Dec 5 12:50:04 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines