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.24 by root, Tue Aug 14 14:56:22 2001 UTC vs.
Revision 1.49 by pcg, Sun Nov 23 03:34:13 2003 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "patchlevel.h"
6
7#if PATCHLEVEL < 6
8# ifndef PL_ppaddr
9# define PL_ppaddr ppaddr
10# endif
11# ifndef call_sv
12# define call_sv perl_call_sv
13# endif
14# ifndef get_sv
15# define get_sv perl_get_sv
16# endif
17# ifndef get_cv
18# define get_cv perl_get_cv
19# endif
20# ifndef IS_PADGV
21# define IS_PADGV(v) 0
22# endif
23# ifndef IS_PADCONST
24# define IS_PADCONST(v) 0
25# endif
26#endif
27
5#include "libcoro/coro.c" 28#include "libcoro/coro.c"
6 29
7#include <signal.h> 30#include <signal.h>
8 31
9#ifdef HAVE_MMAP 32#ifdef HAVE_MMAP
10# include <unistd.h> 33# include <unistd.h>
11# include <sys/mman.h> 34# include <sys/mman.h>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS 35# ifndef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS 36# ifdef MAP_ANON
37# define MAP_ANONYMOUS MAP_ANON
15# else 38# else
16# undef HAVE_MMAP 39# undef HAVE_MMAP
17# endif 40# endif
18# endif 41# endif
19#endif 42#endif
20 43
21#define MAY_FLUSH /* increases codesize */ 44#define MAY_FLUSH /* increases codesize and is rarely used */
22 45
23#define SUB_INIT "Coro::State::initialize" 46#define SUB_INIT "Coro::State::initialize"
24#define UCORO_STATE "_coro_state" 47#define UCORO_STATE "_coro_state"
25 48
26/* The next macro should delcare a variable stacklevel that contains and approximation 49/* The next macro should declare a variable stacklevel that contains and approximation
27 * to the current C stack pointer. Its property is that it changes with each call 50 * to the current C stack pointer. Its property is that it changes with each call
28 * and should be unique. */ 51 * and should be unique. */
29#define dSTACKLEVEL void *stacklevel = &stacklevel 52#define dSTACKLEVEL void *stacklevel = &stacklevel
53
54#define IN_DESTRUCT (PL_main_cv == Nullcv)
30 55
31#define labs(l) ((l) >= 0 ? (l) : -(l)) 56#define labs(l) ((l) >= 0 ? (l) : -(l))
32 57
33#include "CoroAPI.h" 58#include "CoroAPI.h"
34 59
45 void *sptr; 70 void *sptr;
46 long ssize; /* positive == mmap, otherwise malloc */ 71 long ssize; /* positive == mmap, otherwise malloc */
47} coro_stack; 72} coro_stack;
48 73
49struct coro { 74struct coro {
75 /* the top-level JMPENV for each coroutine, needed to catch dies. */
76 JMPENV start_env;
77
50 /* the optional C context */ 78 /* the optional C context */
51 coro_stack *stack; 79 coro_stack *stack;
52 void *cursp; 80 void *cursp;
53 int gencnt; 81 int gencnt;
54 82
66 AV *curstack; 94 AV *curstack;
67 AV *mainstack; 95 AV *mainstack;
68 SV **stack_sp; 96 SV **stack_sp;
69 OP *op; 97 OP *op;
70 SV **curpad; 98 SV **curpad;
99 AV *comppad;
71 SV **stack_base; 100 SV **stack_base;
72 SV **stack_max; 101 SV **stack_max;
73 SV **tmps_stack; 102 SV **tmps_stack;
74 I32 tmps_floor; 103 I32 tmps_floor;
75 I32 tmps_ix; 104 I32 tmps_ix;
99static AV *main_mainstack; /* used to differentiate between $main and others */ 128static AV *main_mainstack; /* used to differentiate between $main and others */
100static HV *coro_state_stash; 129static HV *coro_state_stash;
101static SV *ucoro_state_sv; 130static SV *ucoro_state_sv;
102static U32 ucoro_state_hash; 131static U32 ucoro_state_hash;
103static HV *padlist_cache; 132static HV *padlist_cache;
133static SV *coro_mortal; /* will be freed after next transfer */
104 134
105/* mostly copied from op.c:cv_clone2 */ 135/* mostly copied from op.c:cv_clone2 */
106STATIC AV * 136STATIC AV *
107clone_padlist (AV *protopadlist) 137clone_padlist (AV *protopadlist)
108{ 138{
154 sv = (SV *) newAV (); 184 sv = (SV *) newAV ();
155 else if (*name == '%') 185 else if (*name == '%')
156 sv = (SV *) newHV (); 186 sv = (SV *) newHV ();
157 else 187 else
158 sv = NEWSV (0, 0); 188 sv = NEWSV (0, 0);
189#ifdef SvPADBUSY
159 if (!SvPADBUSY (sv)) 190 if (!SvPADBUSY (sv))
191#endif
160 SvPADMY_on (sv); 192 SvPADMY_on (sv);
161 npad[ix] = sv; 193 npad[ix] = sv;
162 } 194 }
163 } 195 }
164 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 196 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
195 227
196 return newpadlist; 228 return newpadlist;
197} 229}
198 230
199#ifdef MAY_FLUSH 231#ifdef MAY_FLUSH
200STATIC AV * 232STATIC void
201free_padlist (AV *padlist) 233free_padlist (AV *padlist)
202{ 234{
203 /* may be during global destruction */ 235 /* may be during global destruction */
204 if (SvREFCNT(padlist)) 236 if (SvREFCNT(padlist))
205 { 237 {
270#endif 302#endif
271 303
272#define SB do { 304#define SB do {
273#define SE } while (0) 305#define SE } while (0)
274 306
275#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 307#define LOAD(state) load_state(aTHX_ (state));
276#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 308#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
277 309
278#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 310#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
279 311
280static void 312static void
281load_state(pTHX_ Coro__State c) 313load_state(pTHX_ Coro__State c)
282{ 314{
283 PL_dowarn = c->dowarn; 315 PL_dowarn = c->dowarn;
287 PL_curstack = c->curstack; 319 PL_curstack = c->curstack;
288 PL_mainstack = c->mainstack; 320 PL_mainstack = c->mainstack;
289 PL_stack_sp = c->stack_sp; 321 PL_stack_sp = c->stack_sp;
290 PL_op = c->op; 322 PL_op = c->op;
291 PL_curpad = c->curpad; 323 PL_curpad = c->curpad;
324 PL_comppad = c->comppad;
292 PL_stack_base = c->stack_base; 325 PL_stack_base = c->stack_base;
293 PL_stack_max = c->stack_max; 326 PL_stack_max = c->stack_max;
294 PL_tmps_stack = c->tmps_stack; 327 PL_tmps_stack = c->tmps_stack;
295 PL_tmps_floor = c->tmps_floor; 328 PL_tmps_floor = c->tmps_floor;
296 PL_tmps_ix = c->tmps_ix; 329 PL_tmps_ix = c->tmps_ix;
355 388
356 PUSHs (Nullsv); 389 PUSHs (Nullsv);
357 /* this loop was inspired by pp_caller */ 390 /* this loop was inspired by pp_caller */
358 for (;;) 391 for (;;)
359 { 392 {
360 do 393 while (cxix >= 0)
361 { 394 {
362 PERL_CONTEXT *cx = &ccstk[cxix--]; 395 PERL_CONTEXT *cx = &ccstk[cxix--];
363 396
364 if (CxTYPE(cx) == CXt_SUB) 397 if (CxTYPE(cx) == CXt_SUB)
365 { 398 {
384 PUSHs ((SV *)cv); 417 PUSHs ((SV *)cv);
385 418
386 get_padlist (cv); /* this is a monster */ 419 get_padlist (cv); /* this is a monster */
387 } 420 }
388 } 421 }
422#ifdef CXt_FORMAT
389 else if (CxTYPE(cx) == CXt_FORMAT) 423 else if (CxTYPE(cx) == CXt_FORMAT)
390 { 424 {
391 /* I never used formats, so how should I know how these are implemented? */ 425 /* I never used formats, so how should I know how these are implemented? */
392 /* my bold guess is as a simple, plain sub... */ 426 /* my bold guess is as a simple, plain sub... */
393 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 427 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
394 } 428 }
429#endif
395 } 430 }
396 while (cxix >= 0);
397 431
398 if (top_si->si_type == PERLSI_MAIN) 432 if (top_si->si_type == PERLSI_MAIN)
399 break; 433 break;
400 434
401 top_si = top_si->si_prev; 435 top_si = top_si->si_prev;
407 } 441 }
408 442
409 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 443 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
410 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 444 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
411 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 445 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
412
413 /* I have not the slightest idea of why av_reify is necessary */
414 /* but if it's missing the defav contents magically get replaced sometimes */
415 if (c->defav)
416 av_reify (c->defav);
417 446
418 c->dowarn = PL_dowarn; 447 c->dowarn = PL_dowarn;
419 c->in_eval = PL_in_eval; 448 c->in_eval = PL_in_eval;
420 449
421 c->curstackinfo = PL_curstackinfo; 450 c->curstackinfo = PL_curstackinfo;
422 c->curstack = PL_curstack; 451 c->curstack = PL_curstack;
423 c->mainstack = PL_mainstack; 452 c->mainstack = PL_mainstack;
424 c->stack_sp = PL_stack_sp; 453 c->stack_sp = PL_stack_sp;
425 c->op = PL_op; 454 c->op = PL_op;
426 c->curpad = PL_curpad; 455 c->curpad = PL_curpad;
456 c->comppad = PL_comppad;
427 c->stack_base = PL_stack_base; 457 c->stack_base = PL_stack_base;
428 c->stack_max = PL_stack_max; 458 c->stack_max = PL_stack_max;
429 c->tmps_stack = PL_tmps_stack; 459 c->tmps_stack = PL_tmps_stack;
430 c->tmps_floor = PL_tmps_floor; 460 c->tmps_floor = PL_tmps_floor;
431 c->tmps_ix = PL_tmps_ix; 461 c->tmps_ix = PL_tmps_ix;
471 501
472 New(54,PL_markstack,16,I32); 502 New(54,PL_markstack,16,I32);
473 PL_markstack_ptr = PL_markstack; 503 PL_markstack_ptr = PL_markstack;
474 PL_markstack_max = PL_markstack + 16; 504 PL_markstack_max = PL_markstack + 16;
475 505
506#ifdef SET_MARK_OFFSET
476 SET_MARK_OFFSET; 507 SET_MARK_OFFSET;
508#endif
477 509
478 New(54,PL_scopestack,16,I32); 510 New(54,PL_scopestack,16,I32);
479 PL_scopestack_ix = 0; 511 PL_scopestack_ix = 0;
480 PL_scopestack_max = 16; 512 PL_scopestack_max = 16;
481 513
488 PL_retstack_max = 8; 520 PL_retstack_max = 8;
489} 521}
490 522
491/* 523/*
492 * destroy the stacks, the callchain etc... 524 * destroy the stacks, the callchain etc...
493 * still there is a memleak of 128 bytes...
494 */ 525 */
495STATIC void 526STATIC void
496destroy_stacks(pTHX) 527destroy_stacks(pTHX)
497{ 528{
529 if (!IN_DESTRUCT)
530 {
498 /* is this ugly, I ask? */ 531 /* is this ugly, I ask? */
499 while (PL_scopestack_ix) 532 while (PL_scopestack_ix)
500 LEAVE; 533 LEAVE;
501 534
502 /* sure it is, but more important: is it correct?? :/ */ 535 /* sure it is, but more important: is it correct?? :/ */
503 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ 536 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
504 FREETMPS; 537 FREETMPS;
538 }
505 539
506 while (PL_curstackinfo->si_next) 540 while (PL_curstackinfo->si_next)
507 PL_curstackinfo = PL_curstackinfo->si_next; 541 PL_curstackinfo = PL_curstackinfo->si_next;
508 542
509 while (PL_curstackinfo) 543 while (PL_curstackinfo)
514 dSP; 548 dSP;
515 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 549 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
516 PUTBACK; /* possibly superfluous */ 550 PUTBACK; /* possibly superfluous */
517 } 551 }
518 552
519 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ 553 if (!IN_DESTRUCT)
554 {
520 dounwind(-1); 555 dounwind(-1);
521
522 SvREFCNT_dec(PL_curstackinfo->si_stack); 556 SvREFCNT_dec(PL_curstackinfo->si_stack);
557 }
558
523 Safefree(PL_curstackinfo->si_cxstack); 559 Safefree(PL_curstackinfo->si_cxstack);
524 Safefree(PL_curstackinfo); 560 Safefree(PL_curstackinfo);
525 PL_curstackinfo = p; 561 PL_curstackinfo = p;
526 } 562 }
527 563
542 stack->refcnt = 1; 578 stack->refcnt = 1;
543 stack->usecnt = 1; 579 stack->usecnt = 1;
544 stack->gencnt = ctx->gencnt = 0; 580 stack->gencnt = ctx->gencnt = 0;
545 if (alloc) 581 if (alloc)
546 { 582 {
547#ifdef HAVE_MMAP 583#if HAVE_MMAP
548 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 584 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
549 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 585 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
550 if (stack->sptr == (void *)-1) 586 if (stack->sptr == (void *)-1)
551#endif 587#endif
552 { 588 {
553 /*FIXME*//*D*//* reasonable stack size! */ 589 /*FIXME*//*D*//* reasonable stack size! */
554 stack->ssize = -4096 * sizeof (long); 590 stack->ssize = -4096 * sizeof (long);
574 { 610 {
575#ifdef HAVE_MMAP 611#ifdef HAVE_MMAP
576 if (stack->ssize > 0 && stack->sptr) 612 if (stack->ssize > 0 && stack->sptr)
577 munmap (stack->sptr, stack->ssize); 613 munmap (stack->sptr, stack->ssize);
578 else 614 else
579#else 615#endif
580 Safefree (stack->sptr); 616 Safefree (stack->sptr);
581#endif 617
582 Safefree (stack); 618 Safefree (stack);
583 } 619 }
584 else if (ctx->gencnt == stack->gencnt) 620 else if (ctx->gencnt == stack->gencnt)
585 --stack->usecnt; 621 --stack->usecnt;
586 } 622 }
598 634
599 coro_init_stacks (aTHX); 635 coro_init_stacks (aTHX);
600 /*PL_curcop = 0;*/ 636 /*PL_curcop = 0;*/
601 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 637 /*PL_in_eval = PL_in_eval;*/ /* inherit */
602 SvREFCNT_dec (GvAV (PL_defgv)); 638 SvREFCNT_dec (GvAV (PL_defgv));
603 GvAV (PL_defgv) = ctx->args; 639 GvAV (PL_defgv) = ctx->args; ctx->args = 0;
604 640
605 SPAGAIN; 641 SPAGAIN;
606 642
607 if (ctx->stack) 643 if (ctx->stack)
608 { 644 {
635 * that doesn't matter, though, since it is only 671 * that doesn't matter, though, since it is only
636 * pp_nextstate and we never return... 672 * pp_nextstate and we never return...
637 * ah yes, and I don't care anyways ;) 673 * ah yes, and I don't care anyways ;)
638 */ 674 */
639 PUTBACK; 675 PUTBACK;
640 PL_op = pp_entersub(); 676 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
641 SPAGAIN; 677 SPAGAIN;
642 678
643 ENTER; /* necessary e.g. for dounwind */ 679 ENTER; /* necessary e.g. for dounwind */
644 } 680 }
645} 681}
649{ 685{
650 /* 686 /*
651 * this is a _very_ stripped down perl interpreter ;) 687 * this is a _very_ stripped down perl interpreter ;)
652 */ 688 */
653 Coro__State ctx = (Coro__State)arg; 689 Coro__State ctx = (Coro__State)arg;
690 JMPENV coro_start_env;
654 691
655 /*FIXME*//* must set up top_env here */ 692 PL_top_env = &ctx->start_env;
693
656 ctx->cursp = 0; 694 ctx->cursp = 0;
657 PL_op = PL_op->op_next; 695 PL_op = PL_op->op_next;
658 CALLRUNOPS(aTHX); 696 CALLRUNOPS(aTHX);
659 697
660 abort (); 698 abort ();
661} 699}
662 700
663STATIC void 701STATIC void
664transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 702transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
665{ 703{
666 dSP;
667 dSTACKLEVEL; 704 dSTACKLEVEL;
705 static struct coro *xnext;
668 706
669 if (prev != next) 707 if (prev != next)
670 { 708 {
709 xnext = next;
710
671 if (next->mainstack) 711 if (next->mainstack)
672 { 712 {
673 SAVE (prev, flags); 713 SAVE (prev, flags);
674 LOAD (next); 714 LOAD (next);
675 715
698 continue_coro, (void *)next, 738 continue_coro, (void *)next,
699 next->stack->sptr, labs (next->stack->ssize)); 739 next->stack->sptr, labs (next->stack->ssize));
700 } 740 }
701 741
702 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
743 /* don't add any code here */
703 } 744 }
704 745
705 } 746 }
706 else if (next->tmps_ix == -2) 747 else if (next->tmps_ix == -2)
707 croak ("tried to transfer to running coroutine"); 748 croak ("tried to transfer to running coroutine");
714 if (!prev->stack) 755 if (!prev->stack)
715 allocate_stack (prev, 0); 756 allocate_stack (prev, 0);
716 757
717 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 758 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
718 { 759 {
760 PL_top_env = &next->start_env;
761
719 setup_coro (next); 762 setup_coro (next);
720 763
721 prev->stack->refcnt++; 764 prev->stack->refcnt++;
722 prev->stack->usecnt++; 765 prev->stack->usecnt++;
723 next->stack = prev->stack; 766 next->stack = prev->stack;
724 next->gencnt = prev->gencnt; 767 next->gencnt = prev->gencnt;
725 } 768 }
726 else 769 else
727 { 770 {
771 assert (!next->stack);
728 allocate_stack (next, 1); 772 allocate_stack (next, 1);
729 coro_create (&(next->stack->cctx), 773 coro_create (&(next->stack->cctx),
730 setup_coro, (void *)next, 774 setup_coro, (void *)next,
731 next->stack->sptr, labs (next->stack->ssize)); 775 next->stack->sptr, labs (next->stack->ssize));
732 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 776 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
777 /* don't add any code here */
733 } 778 }
734 } 779 }
735 else 780 else
736 setup_coro (next); 781 setup_coro (next);
737 } 782 }
783
784 /*
785 * xnext is now either prev or next, depending on wether
786 * we switched the c stack or not. that's why I use a global
787 * variable, that should become thread-specific at one point.
788 */
789 xnext->cursp = stacklevel;
738 } 790 }
739 791
740 next->cursp = stacklevel; 792 if (coro_mortal)
741}
742
743static struct coro *
744sv_to_coro (SV *arg, const char *funcname, const char *varname)
745{
746 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
747 { 793 {
748 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 794 SvREFCNT_dec (coro_mortal);
749 795 coro_mortal = 0;
750 if (!he)
751 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
752
753 arg = HeVAL(he);
754 } 796 }
755 797}
798
799#define SV_CORO(sv,func) \
800 do { \
801 if (SvROK (sv)) \
802 sv = SvRV (sv); \
803 \
804 if (SvTYPE(sv) == SVt_PVHV) \
805 { \
806 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
807 \
808 if (!he) \
809 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
810 \
811 (sv) = SvRV (HeVAL(he)); \
812 } \
813 \
756 /* must also be changed inside Coro::Cont::yield */ 814 /* must also be changed inside Coro::Cont::yield */ \
757 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 815 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \
758 return (struct coro *) SvIV((SV*)SvRV(arg));
759 else
760 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 816 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
761} 817 \
818 } while(0)
819
820#define SvSTATE(sv) (struct coro *)SvIV (sv)
762 821
763static void 822static void
764api_transfer(pTHX_ SV *prev, SV *next, int flags) 823api_transfer(pTHX_ SV *prev, SV *next, int flags)
765{ 824{
766 transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"), 825 SV_CORO (prev, "Coro::transfer");
767 sv_to_coro (next, "Coro::transfer", "next"), 826 SV_CORO (next, "Coro::transfer");
768 flags); 827
828 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
769} 829}
770 830
771/** Coro ********************************************************************/ 831/** Coro ********************************************************************/
772 832
773#define PRIO_MAX 3 833#define PRIO_MAX 3
778#define PRIO_MIN -4 838#define PRIO_MIN -4
779 839
780/* for Coro.pm */ 840/* for Coro.pm */
781static GV *coro_current, *coro_idle; 841static GV *coro_current, *coro_idle;
782static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 842static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
843static int coro_nready;
783 844
784static void 845static void
785coro_enq (SV *sv) 846coro_enq (SV *sv)
786{ 847{
787 if (SvROK (sv))
788 {
789 SV *hv = SvRV (sv);
790 if (SvTYPE (hv) == SVt_PVHV) 848 if (SvTYPE (sv) == SVt_PVHV)
791 { 849 {
792 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); 850 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
793 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 851 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
794 852
795 prio = prio > PRIO_MAX ? PRIO_MAX 853 prio = prio > PRIO_MAX ? PRIO_MAX
796 : prio < PRIO_MIN ? PRIO_MIN 854 : prio < PRIO_MIN ? PRIO_MIN
797 : prio; 855 : prio;
798 856
799 av_push (coro_ready [prio - PRIO_MIN], sv); 857 av_push (coro_ready [prio - PRIO_MIN], sv);
858 coro_nready++;
800 859
801 return; 860 return;
802 }
803 } 861 }
804 862
805 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 863 croak ("Coro::ready tried to enqueue something that is not a coroutine");
806} 864}
807 865
814 if (min_prio < 0) 872 if (min_prio < 0)
815 min_prio = 0; 873 min_prio = 0;
816 874
817 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 875 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
818 if (av_len (coro_ready[prio]) >= 0) 876 if (av_len (coro_ready[prio]) >= 0)
877 {
878 coro_nready--;
819 return av_shift (coro_ready[prio]); 879 return av_shift (coro_ready[prio]);
880 }
820 881
821 return 0; 882 return 0;
822} 883}
823 884
824static void 885static void
825api_ready (SV *coro) 886api_ready (SV *coro)
826{ 887{
888 if (SvROK (coro))
889 coro = SvRV (coro);
890
827 coro_enq (SvREFCNT_inc (coro)); 891 coro_enq (SvREFCNT_inc (coro));
828} 892}
829 893
830static void 894static void
831api_schedule (int cede) 895api_schedule (void)
832{ 896{
833 SV *prev, *next; 897 SV *prev, *next;
834 898
835 prev = GvSV (coro_current); 899 prev = SvRV (GvSV (coro_current));
836
837 if (cede)
838 coro_enq (SvREFCNT_inc (prev));
839
840 next = coro_deq (PRIO_MIN); 900 next = coro_deq (PRIO_MIN);
841 901
842 if (!next) 902 if (!next)
843 next = SvREFCNT_inc (GvSV (coro_idle)); 903 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
844 904
845 GvSV (coro_current) = SvREFCNT_inc (next); 905 /* free this only after the transfer */
846 transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), 906 coro_mortal = prev;
847 sv_to_coro (next, "Coro::schedule", "next coroutine"), 907 SV_CORO (prev, "Coro::schedule");
908
909 SvRV (GvSV (coro_current)) = next;
910
911 SV_CORO (next, "Coro::schedule");
912
913 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
848 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 914 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
849 SvREFCNT_dec (next); 915}
850 SvREFCNT_dec (prev); 916
917static void
918api_cede (void)
919{
920 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
921
922 api_schedule ();
851} 923}
852 924
853MODULE = Coro::State PACKAGE = Coro::State 925MODULE = Coro::State PACKAGE = Coro::State
854 926
855PROTOTYPES: ENABLE 927PROTOTYPES: ENABLE
868 if (!padlist_cache) 940 if (!padlist_cache)
869 padlist_cache = newHV (); 941 padlist_cache = newHV ();
870 942
871 main_mainstack = PL_mainstack; 943 main_mainstack = PL_mainstack;
872 944
873 {
874 SV *sv = perl_get_sv("Coro::API", 1);
875
876 coroapi.ver = CORO_API_VERSION - 1; 945 coroapi.ver = CORO_API_VERSION;
877 coroapi.transfer = api_transfer; 946 coroapi.transfer = api_transfer;
878 coroapi.schedule = api_schedule;
879 coroapi.ready = api_ready;
880
881 GCoroAPI = &coroapi;
882 sv_setiv(sv, (IV)&coroapi);
883 SvREADONLY_on(sv);
884 }
885} 947}
886 948
887Coro::State 949Coro::State
888_newprocess(args) 950_newprocess(args)
889 SV * args 951 SV * args
892 Coro__State coro; 954 Coro__State coro;
893 955
894 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 956 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
895 croak ("Coro::State::_newprocess expects an arrayref"); 957 croak ("Coro::State::_newprocess expects an arrayref");
896 958
897 New (0, coro, 1, struct coro); 959 Newz (0, coro, 1, struct coro);
898 960
899 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 961 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
900 coro->mainstack = 0; /* actual work is done inside transfer */ 962 coro->mainstack = 0; /* actual work is done inside transfer */
901 coro->stack = 0; 963 coro->stack = 0;
902 964
965 /* same as JMPENV_BOOTSTRAP */
966 /* we might be able to recycle start_env, but safe is safe */
967 //Zero(&coro->start_env, 1, JMPENV);
968 coro->start_env.je_ret = -1;
969 coro->start_env.je_mustcatch = TRUE;
970
903 RETVAL = coro; 971 RETVAL = coro;
904 OUTPUT: 972 OUTPUT:
905 RETVAL 973 RETVAL
906 974
907void 975void
908transfer(prev, next, flags) 976transfer(prev, next, flags)
909 Coro::State_or_hashref prev 977 SV *prev
910 Coro::State_or_hashref next 978 SV *next
911 int flags 979 int flags
912 PROTOTYPE: @ 980 PROTOTYPE: @
913 CODE: 981 CODE:
914 PUTBACK; 982 PUTBACK;
983 SV_CORO (next, "Coro::transfer");
984 SV_CORO (prev, "Coro::transfer");
915 transfer (aTHX_ prev, next, flags); 985 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
916 SPAGAIN; 986 SPAGAIN;
917 987
918void 988void
919DESTROY(coro) 989DESTROY(coro)
920 Coro::State coro 990 Coro::State coro
922 992
923 if (coro->mainstack && coro->mainstack != main_mainstack) 993 if (coro->mainstack && coro->mainstack != main_mainstack)
924 { 994 {
925 struct coro temp; 995 struct coro temp;
926 996
997 PUTBACK;
927 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 998 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
928 LOAD(aTHX_ coro); 999 LOAD(aTHX_ coro);
1000 SPAGAIN;
929 1001
930 destroy_stacks (aTHX); 1002 destroy_stacks (aTHX);
931 1003
932 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1004 LOAD((&temp)); /* this will get rid of defsv etc.. */
1005 SPAGAIN;
933 1006
934 coro->mainstack = 0; 1007 coro->mainstack = 0;
935 } 1008 }
936 1009
937 deallocate_stack (coro); 1010 deallocate_stack (coro);
938 1011 SvREFCNT_dec (coro->args);
939 Safefree (coro); 1012 Safefree (coro);
940 1013
941void 1014void
942flush() 1015flush()
943 CODE: 1016 CODE:
970 SV *sv; 1043 SV *sv;
971 AV *defav = GvAV (PL_defgv); 1044 AV *defav = GvAV (PL_defgv);
972 struct coro *prev, *next; 1045 struct coro *prev, *next;
973 1046
974 if (!returnstk) 1047 if (!returnstk)
975 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 1048 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE));
976 1049
977 /* set up @_ -- ugly */ 1050 /* set up @_ -- ugly */
978 av_clear (defav); 1051 av_clear (defav);
979 av_fill (defav, items - 1); 1052 av_fill (defav, items - 1);
980 while (items--) 1053 while (items--)
1007 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1080 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1008 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1081 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1009 1082
1010 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1083 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1011 coro_ready[i] = newAV (); 1084 coro_ready[i] = newAV ();
1085
1086 {
1087 SV *sv = perl_get_sv("Coro::API", 1);
1088
1089 coroapi.schedule = api_schedule;
1090 coroapi.cede = api_cede;
1091 coroapi.ready = api_ready;
1092 coroapi.nready = &coro_nready;
1093 coroapi.current = coro_current;
1094
1095 GCoroAPI = &coroapi;
1096 sv_setiv(sv, (IV)&coroapi);
1097 SvREADONLY_on(sv);
1098 }
1012} 1099}
1013 1100
1014void 1101void
1015ready(self) 1102ready(self)
1016 SV * self 1103 SV * self
1104 PROTOTYPE: $
1017 CODE: 1105 CODE:
1018 api_ready (self); 1106 api_ready (self);
1019 1107
1108int
1109nready(...)
1110 PROTOTYPE:
1111 CODE:
1112 RETVAL = coro_nready;
1113 OUTPUT:
1114 RETVAL
1115
1020void 1116void
1021schedule(...) 1117schedule(...)
1022 ALIAS: 1118 PROTOTYPE:
1023 cede = 1
1024 CODE: 1119 CODE:
1025 api_schedule (ix); 1120 api_schedule ();
1026 1121
1122void
1123cede(...)
1124 PROTOTYPE:
1125 CODE:
1126 api_cede ();
1127

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines