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.116 by root, Fri Dec 1 14:01:43 2006 UTC vs.
Revision 1.126 by root, Tue Dec 12 04:19:56 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines