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.22 by root, Sat Aug 11 23:10:56 2001 UTC vs.
Revision 1.48 by pcg, Sat Nov 15 03:53:37 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
23/* perl-related */
24#define TRANSFER_SAVE_DEFAV 0x00000001
25#define TRANSFER_SAVE_DEFSV 0x00000002
26#define TRANSFER_SAVE_ERRSV 0x00000004
27/* c-related */
28#define TRANSFER_SAVE_CCTXT 0x00000008
29#ifdef CORO_LAZY_STACK
30# define TRANSFER_LAZY_STACK 0x00000010
31#else
32# define TRANSFER_LAZY_STACK 0x00000000
33#endif
34
35#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
36 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
37 45
38#define SUB_INIT "Coro::State::initialize" 46#define SUB_INIT "Coro::State::initialize"
39#define UCORO_STATE "_coro_state" 47#define UCORO_STATE "_coro_state"
40 48
41/* 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
42 * to the current C stack pointer. It's property is that it changes with each call 50 * to the current C stack pointer. Its property is that it changes with each call
43 * and should be unique. */ 51 * and should be unique. */
44#define dSTACKLEVEL void *stacklevel = &stacklevel 52#define dSTACKLEVEL void *stacklevel = &stacklevel
45 53
54#define IN_DESTRUCT (PL_main_cv == Nullcv)
55
46#define labs(l) ((l) >= 0 ? (l) : -(l)) 56#define labs(l) ((l) >= 0 ? (l) : -(l))
57
58#include "CoroAPI.h"
59
60static struct CoroAPI coroapi;
47 61
48/* this is actually not only the c stack but also c registers etc... */ 62/* this is actually not only the c stack but also c registers etc... */
49typedef struct { 63typedef struct {
50 int refcnt; /* pointer reference counter */ 64 int refcnt; /* pointer reference counter */
51 int usecnt; /* shared by how many coroutines */ 65 int usecnt; /* shared by how many coroutines */
56 void *sptr; 70 void *sptr;
57 long ssize; /* positive == mmap, otherwise malloc */ 71 long ssize; /* positive == mmap, otherwise malloc */
58} coro_stack; 72} coro_stack;
59 73
60struct coro { 74struct coro {
75 /* the top-level JMPENV for each coroutine, needed to catch dies. */
76 JMPENV start_env;
77
61 /* the optional C context */ 78 /* the optional C context */
62 coro_stack *stack; 79 coro_stack *stack;
63 void *cursp; 80 void *cursp;
64 int gencnt; 81 int gencnt;
65 82
77 AV *curstack; 94 AV *curstack;
78 AV *mainstack; 95 AV *mainstack;
79 SV **stack_sp; 96 SV **stack_sp;
80 OP *op; 97 OP *op;
81 SV **curpad; 98 SV **curpad;
99 AV *comppad;
82 SV **stack_base; 100 SV **stack_base;
83 SV **stack_max; 101 SV **stack_max;
84 SV **tmps_stack; 102 SV **tmps_stack;
85 I32 tmps_floor; 103 I32 tmps_floor;
86 I32 tmps_ix; 104 I32 tmps_ix;
110static AV *main_mainstack; /* used to differentiate between $main and others */ 128static AV *main_mainstack; /* used to differentiate between $main and others */
111static HV *coro_state_stash; 129static HV *coro_state_stash;
112static SV *ucoro_state_sv; 130static SV *ucoro_state_sv;
113static U32 ucoro_state_hash; 131static U32 ucoro_state_hash;
114static HV *padlist_cache; 132static HV *padlist_cache;
133static SV *coro_mortal; /* will be freed after next transfer */
115 134
116/* mostly copied from op.c:cv_clone2 */ 135/* mostly copied from op.c:cv_clone2 */
117STATIC AV * 136STATIC AV *
118clone_padlist (AV *protopadlist) 137clone_padlist (AV *protopadlist)
119{ 138{
206 225
207 return newpadlist; 226 return newpadlist;
208} 227}
209 228
210#ifdef MAY_FLUSH 229#ifdef MAY_FLUSH
211STATIC AV * 230STATIC void
212free_padlist (AV *padlist) 231free_padlist (AV *padlist)
213{ 232{
214 /* may be during global destruction */ 233 /* may be during global destruction */
215 if (SvREFCNT(padlist)) 234 if (SvREFCNT(padlist))
216 { 235 {
281#endif 300#endif
282 301
283#define SB do { 302#define SB do {
284#define SE } while (0) 303#define SE } while (0)
285 304
286#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 305#define LOAD(state) load_state(aTHX_ (state));
287#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 306#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
288 307
289#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 308#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
290 309
291static void 310static void
292load_state(pTHX_ Coro__State c) 311load_state(pTHX_ Coro__State c)
293{ 312{
294 PL_dowarn = c->dowarn; 313 PL_dowarn = c->dowarn;
298 PL_curstack = c->curstack; 317 PL_curstack = c->curstack;
299 PL_mainstack = c->mainstack; 318 PL_mainstack = c->mainstack;
300 PL_stack_sp = c->stack_sp; 319 PL_stack_sp = c->stack_sp;
301 PL_op = c->op; 320 PL_op = c->op;
302 PL_curpad = c->curpad; 321 PL_curpad = c->curpad;
322 PL_comppad = c->comppad;
303 PL_stack_base = c->stack_base; 323 PL_stack_base = c->stack_base;
304 PL_stack_max = c->stack_max; 324 PL_stack_max = c->stack_max;
305 PL_tmps_stack = c->tmps_stack; 325 PL_tmps_stack = c->tmps_stack;
306 PL_tmps_floor = c->tmps_floor; 326 PL_tmps_floor = c->tmps_floor;
307 PL_tmps_ix = c->tmps_ix; 327 PL_tmps_ix = c->tmps_ix;
366 386
367 PUSHs (Nullsv); 387 PUSHs (Nullsv);
368 /* this loop was inspired by pp_caller */ 388 /* this loop was inspired by pp_caller */
369 for (;;) 389 for (;;)
370 { 390 {
371 do 391 while (cxix >= 0)
372 { 392 {
373 PERL_CONTEXT *cx = &ccstk[cxix--]; 393 PERL_CONTEXT *cx = &ccstk[cxix--];
374 394
375 if (CxTYPE(cx) == CXt_SUB) 395 if (CxTYPE(cx) == CXt_SUB)
376 { 396 {
395 PUSHs ((SV *)cv); 415 PUSHs ((SV *)cv);
396 416
397 get_padlist (cv); /* this is a monster */ 417 get_padlist (cv); /* this is a monster */
398 } 418 }
399 } 419 }
420#ifdef CXt_FORMAT
400 else if (CxTYPE(cx) == CXt_FORMAT) 421 else if (CxTYPE(cx) == CXt_FORMAT)
401 { 422 {
402 /* I never used formats, so how should I know how these are implemented? */ 423 /* I never used formats, so how should I know how these are implemented? */
403 /* my bold guess is as a simple, plain sub... */ 424 /* my bold guess is as a simple, plain sub... */
404 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 425 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
405 } 426 }
427#endif
406 } 428 }
407 while (cxix >= 0);
408 429
409 if (top_si->si_type == PERLSI_MAIN) 430 if (top_si->si_type == PERLSI_MAIN)
410 break; 431 break;
411 432
412 top_si = top_si->si_prev; 433 top_si = top_si->si_prev;
418 } 439 }
419 440
420 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 441 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
421 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 442 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
422 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 443 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
423
424 /* I have not the slightest idea of why av_reify is necessary */
425 /* but if it's missing the defav contents magically get replaced sometimes */
426 if (c->defav)
427 av_reify (c->defav);
428 444
429 c->dowarn = PL_dowarn; 445 c->dowarn = PL_dowarn;
430 c->in_eval = PL_in_eval; 446 c->in_eval = PL_in_eval;
431 447
432 c->curstackinfo = PL_curstackinfo; 448 c->curstackinfo = PL_curstackinfo;
433 c->curstack = PL_curstack; 449 c->curstack = PL_curstack;
434 c->mainstack = PL_mainstack; 450 c->mainstack = PL_mainstack;
435 c->stack_sp = PL_stack_sp; 451 c->stack_sp = PL_stack_sp;
436 c->op = PL_op; 452 c->op = PL_op;
437 c->curpad = PL_curpad; 453 c->curpad = PL_curpad;
454 c->comppad = PL_comppad;
438 c->stack_base = PL_stack_base; 455 c->stack_base = PL_stack_base;
439 c->stack_max = PL_stack_max; 456 c->stack_max = PL_stack_max;
440 c->tmps_stack = PL_tmps_stack; 457 c->tmps_stack = PL_tmps_stack;
441 c->tmps_floor = PL_tmps_floor; 458 c->tmps_floor = PL_tmps_floor;
442 c->tmps_ix = PL_tmps_ix; 459 c->tmps_ix = PL_tmps_ix;
482 499
483 New(54,PL_markstack,16,I32); 500 New(54,PL_markstack,16,I32);
484 PL_markstack_ptr = PL_markstack; 501 PL_markstack_ptr = PL_markstack;
485 PL_markstack_max = PL_markstack + 16; 502 PL_markstack_max = PL_markstack + 16;
486 503
504#ifdef SET_MARK_OFFSET
487 SET_MARK_OFFSET; 505 SET_MARK_OFFSET;
506#endif
488 507
489 New(54,PL_scopestack,16,I32); 508 New(54,PL_scopestack,16,I32);
490 PL_scopestack_ix = 0; 509 PL_scopestack_ix = 0;
491 PL_scopestack_max = 16; 510 PL_scopestack_max = 16;
492 511
499 PL_retstack_max = 8; 518 PL_retstack_max = 8;
500} 519}
501 520
502/* 521/*
503 * destroy the stacks, the callchain etc... 522 * destroy the stacks, the callchain etc...
504 * still there is a memleak of 128 bytes...
505 */ 523 */
506STATIC void 524STATIC void
507destroy_stacks(pTHX) 525destroy_stacks(pTHX)
508{ 526{
527 if (!IN_DESTRUCT)
528 {
509 /* is this ugly, I ask? */ 529 /* is this ugly, I ask? */
510 while (PL_scopestack_ix) 530 while (PL_scopestack_ix)
511 LEAVE; 531 LEAVE;
512 532
513 /* sure it is, but more important: is it correct?? :/ */ 533 /* sure it is, but more important: is it correct?? :/ */
514 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ 534 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
515 FREETMPS; 535 FREETMPS;
536 }
516 537
517 while (PL_curstackinfo->si_next) 538 while (PL_curstackinfo->si_next)
518 PL_curstackinfo = PL_curstackinfo->si_next; 539 PL_curstackinfo = PL_curstackinfo->si_next;
519 540
520 while (PL_curstackinfo) 541 while (PL_curstackinfo)
525 dSP; 546 dSP;
526 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 547 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
527 PUTBACK; /* possibly superfluous */ 548 PUTBACK; /* possibly superfluous */
528 } 549 }
529 550
551 if (!IN_DESTRUCT)
552 {
530 dounwind(-1); 553 dounwind(-1);
531
532 SvREFCNT_dec(PL_curstackinfo->si_stack); 554 SvREFCNT_dec(PL_curstackinfo->si_stack);
555 }
556
533 Safefree(PL_curstackinfo->si_cxstack); 557 Safefree(PL_curstackinfo->si_cxstack);
534 Safefree(PL_curstackinfo); 558 Safefree(PL_curstackinfo);
535 PL_curstackinfo = p; 559 PL_curstackinfo = p;
536 } 560 }
537 561
552 stack->refcnt = 1; 576 stack->refcnt = 1;
553 stack->usecnt = 1; 577 stack->usecnt = 1;
554 stack->gencnt = ctx->gencnt = 0; 578 stack->gencnt = ctx->gencnt = 0;
555 if (alloc) 579 if (alloc)
556 { 580 {
557#ifdef HAVE_MMAP 581#if HAVE_MMAP
558 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 582 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
559 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 583 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
560 if (stack->sptr == (void *)-1) 584 if (stack->sptr == (void *)-1)
561#endif 585#endif
562 { 586 {
563 /*FIXME*//*D*//* reasonable stack size! */ 587 /*FIXME*//*D*//* reasonable stack size! */
564 stack->ssize = -4096 * sizeof (long); 588 stack->ssize = -4096 * sizeof (long);
584 { 608 {
585#ifdef HAVE_MMAP 609#ifdef HAVE_MMAP
586 if (stack->ssize > 0 && stack->sptr) 610 if (stack->ssize > 0 && stack->sptr)
587 munmap (stack->sptr, stack->ssize); 611 munmap (stack->sptr, stack->ssize);
588 else 612 else
589#else 613#endif
590 Safefree (stack->sptr); 614 Safefree (stack->sptr);
591#endif 615
592 Safefree (stack); 616 Safefree (stack);
593 } 617 }
594 else if (ctx->gencnt == stack->gencnt) 618 else if (ctx->gencnt == stack->gencnt)
595 --stack->usecnt; 619 --stack->usecnt;
596 } 620 }
608 632
609 coro_init_stacks (aTHX); 633 coro_init_stacks (aTHX);
610 /*PL_curcop = 0;*/ 634 /*PL_curcop = 0;*/
611 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 635 /*PL_in_eval = PL_in_eval;*/ /* inherit */
612 SvREFCNT_dec (GvAV (PL_defgv)); 636 SvREFCNT_dec (GvAV (PL_defgv));
613 GvAV (PL_defgv) = ctx->args; 637 GvAV (PL_defgv) = ctx->args; ctx->args = 0;
614 638
615 SPAGAIN; 639 SPAGAIN;
616 640
617 if (ctx->stack) 641 if (ctx->stack)
618 { 642 {
645 * that doesn't matter, though, since it is only 669 * that doesn't matter, though, since it is only
646 * pp_nextstate and we never return... 670 * pp_nextstate and we never return...
647 * ah yes, and I don't care anyways ;) 671 * ah yes, and I don't care anyways ;)
648 */ 672 */
649 PUTBACK; 673 PUTBACK;
650 PL_op = pp_entersub(); 674 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
651 SPAGAIN; 675 SPAGAIN;
652 676
653 ENTER; /* necessary e.g. for dounwind */ 677 ENTER; /* necessary e.g. for dounwind */
654 } 678 }
655} 679}
659{ 683{
660 /* 684 /*
661 * this is a _very_ stripped down perl interpreter ;) 685 * this is a _very_ stripped down perl interpreter ;)
662 */ 686 */
663 Coro__State ctx = (Coro__State)arg; 687 Coro__State ctx = (Coro__State)arg;
688 JMPENV coro_start_env;
664 689
665 /*FIXME*//* must set up top_env here */ 690 PL_top_env = &ctx->start_env;
691
666 ctx->cursp = 0; 692 ctx->cursp = 0;
667 PL_op = PL_op->op_next; 693 PL_op = PL_op->op_next;
668 CALLRUNOPS(aTHX); 694 CALLRUNOPS(aTHX);
669 695
670 abort (); 696 abort ();
671} 697}
672 698
673STATIC void 699STATIC void
674transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 700transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
675{ 701{
676 dSP;
677 dSTACKLEVEL; 702 dSTACKLEVEL;
703 static struct coro *xnext;
678 704
679 if (prev != next) 705 if (prev != next)
680 { 706 {
707 xnext = next;
708
681 if (next->mainstack) 709 if (next->mainstack)
682 { 710 {
683 SAVE (prev, flags); 711 SAVE (prev, flags);
684 LOAD (next); 712 LOAD (next);
685 713
708 continue_coro, (void *)next, 736 continue_coro, (void *)next,
709 next->stack->sptr, labs (next->stack->ssize)); 737 next->stack->sptr, labs (next->stack->ssize));
710 } 738 }
711 739
712 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 740 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
741 /* don't add any code here */
713 } 742 }
714 743
715 } 744 }
716 else if (next->tmps_ix == -2) 745 else if (next->tmps_ix == -2)
717 croak ("tried to transfer to running coroutine"); 746 croak ("tried to transfer to running coroutine");
724 if (!prev->stack) 753 if (!prev->stack)
725 allocate_stack (prev, 0); 754 allocate_stack (prev, 0);
726 755
727 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 756 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
728 { 757 {
758 PL_top_env = &next->start_env;
759
729 setup_coro (next); 760 setup_coro (next);
730 761
731 prev->stack->refcnt++; 762 prev->stack->refcnt++;
732 prev->stack->usecnt++; 763 prev->stack->usecnt++;
733 next->stack = prev->stack; 764 next->stack = prev->stack;
734 next->gencnt = prev->gencnt; 765 next->gencnt = prev->gencnt;
735 } 766 }
736 else 767 else
737 { 768 {
769 assert (!next->stack);
738 allocate_stack (next, 1); 770 allocate_stack (next, 1);
739 coro_create (&(next->stack->cctx), 771 coro_create (&(next->stack->cctx),
740 setup_coro, (void *)next, 772 setup_coro, (void *)next,
741 next->stack->sptr, labs (next->stack->ssize)); 773 next->stack->sptr, labs (next->stack->ssize));
742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 774 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
775 /* don't add any code here */
743 } 776 }
744 } 777 }
745 else 778 else
746 setup_coro (next); 779 setup_coro (next);
747 } 780 }
781
782 /*
783 * xnext is now either prev or next, depending on wether
784 * we switched the c stack or not. that's why I use a global
785 * variable, that should become thread-specific at one point.
786 */
787 xnext->cursp = stacklevel;
748 } 788 }
749 789
750 next->cursp = stacklevel; 790 if (coro_mortal)
751}
752
753static struct coro *
754sv_to_coro (SV *arg, const char *funcname, const char *varname)
755{
756 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
757 { 791 {
792 SvREFCNT_dec (coro_mortal);
793 coro_mortal = 0;
794 }
795}
796
797#define SV_CORO(sv,func) \
798 do { \
799 if (SvROK (sv)) \
800 sv = SvRV (sv); \
801 \
802 if (SvTYPE(sv) == SVt_PVHV) \
803 { \
758 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 804 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
759 805 \
760 if (!he) 806 if (!he) \
761 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); 807 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
762 808 \
763 arg = HeVAL(he); 809 (sv) = SvRV (HeVAL(he)); \
764 } 810 } \
765 811 \
766 /* must also be changed inside Coro::Cont::yield */ 812 /* must also be changed inside Coro::Cont::yield */ \
767 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 813 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \
768 return (struct coro *) SvIV((SV*)SvRV(arg));
769 else
770 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 814 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
815 \
816 } while(0)
817
818#define SvSTATE(sv) (struct coro *)SvIV (sv)
819
820static void
821api_transfer(pTHX_ SV *prev, SV *next, int flags)
822{
823 SV_CORO (prev, "Coro::transfer");
824 SV_CORO (next, "Coro::transfer");
825
826 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
771} 827}
772 828
773/** Coro ********************************************************************/ 829/** Coro ********************************************************************/
774 830
775#define PRIO_MAX 3 831#define PRIO_MAX 3
780#define PRIO_MIN -4 836#define PRIO_MIN -4
781 837
782/* for Coro.pm */ 838/* for Coro.pm */
783static GV *coro_current, *coro_idle; 839static GV *coro_current, *coro_idle;
784static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 840static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
841static int coro_nready;
785 842
786static void 843static void
787coro_enq (SV *sv) 844coro_enq (SV *sv)
788{ 845{
789 if (SvROK (sv))
790 {
791 SV *hv = SvRV (sv);
792 if (SvTYPE (hv) == SVt_PVHV) 846 if (SvTYPE (sv) == SVt_PVHV)
793 { 847 {
794 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); 848 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
795 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 849 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
796 850
797 prio = prio > PRIO_MAX ? PRIO_MAX 851 prio = prio > PRIO_MAX ? PRIO_MAX
798 : prio < PRIO_MIN ? PRIO_MIN 852 : prio < PRIO_MIN ? PRIO_MIN
799 : prio; 853 : prio;
800 854
801 av_push (coro_ready [prio - PRIO_MIN], sv); 855 av_push (coro_ready [prio - PRIO_MIN], sv);
856 coro_nready++;
802 857
803 return; 858 return;
804 }
805 } 859 }
806 860
807 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 861 croak ("Coro::ready tried to enqueue something that is not a coroutine");
808} 862}
809 863
816 if (min_prio < 0) 870 if (min_prio < 0)
817 min_prio = 0; 871 min_prio = 0;
818 872
819 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 873 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
820 if (av_len (coro_ready[prio]) >= 0) 874 if (av_len (coro_ready[prio]) >= 0)
875 {
876 coro_nready--;
821 return av_shift (coro_ready[prio]); 877 return av_shift (coro_ready[prio]);
878 }
822 879
823 return 0; 880 return 0;
881}
882
883static void
884api_ready (SV *coro)
885{
886 if (SvROK (coro))
887 coro = SvRV (coro);
888
889 coro_enq (SvREFCNT_inc (coro));
890}
891
892static void
893api_schedule (void)
894{
895 SV *prev, *next;
896
897 prev = SvRV (GvSV (coro_current));
898 next = coro_deq (PRIO_MIN);
899
900 if (!next)
901 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
902
903 /* free this only after the transfer */
904 coro_mortal = prev;
905 SV_CORO (prev, "Coro::schedule");
906
907 SvRV (GvSV (coro_current)) = next;
908
909 SV_CORO (next, "Coro::schedule");
910
911 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
912 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
913}
914
915static void
916api_cede (void)
917{
918 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
919
920 api_schedule ();
824} 921}
825 922
826MODULE = Coro::State PACKAGE = Coro::State 923MODULE = Coro::State PACKAGE = Coro::State
827 924
828PROTOTYPES: ENABLE 925PROTOTYPES: ENABLE
840 937
841 if (!padlist_cache) 938 if (!padlist_cache)
842 padlist_cache = newHV (); 939 padlist_cache = newHV ();
843 940
844 main_mainstack = PL_mainstack; 941 main_mainstack = PL_mainstack;
942
943 coroapi.ver = CORO_API_VERSION;
944 coroapi.transfer = api_transfer;
845} 945}
846 946
847Coro::State 947Coro::State
848_newprocess(args) 948_newprocess(args)
849 SV * args 949 SV * args
852 Coro__State coro; 952 Coro__State coro;
853 953
854 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 954 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
855 croak ("Coro::State::_newprocess expects an arrayref"); 955 croak ("Coro::State::_newprocess expects an arrayref");
856 956
857 New (0, coro, 1, struct coro); 957 Newz (0, coro, 1, struct coro);
858 958
859 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 959 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
860 coro->mainstack = 0; /* actual work is done inside transfer */ 960 coro->mainstack = 0; /* actual work is done inside transfer */
861 coro->stack = 0; 961 coro->stack = 0;
862 962
963 /* same as JMPENV_BOOTSTRAP */
964 /* we might be able to recycle start_env, but safe is safe */
965 //Zero(&coro->start_env, 1, JMPENV);
966 coro->start_env.je_ret = -1;
967 coro->start_env.je_mustcatch = TRUE;
968
863 RETVAL = coro; 969 RETVAL = coro;
864 OUTPUT: 970 OUTPUT:
865 RETVAL 971 RETVAL
866 972
867void 973void
868transfer(prev, next, flags) 974transfer(prev, next, flags)
869 Coro::State_or_hashref prev 975 SV *prev
870 Coro::State_or_hashref next 976 SV *next
871 int flags 977 int flags
872 PROTOTYPE: @ 978 PROTOTYPE: @
873 CODE: 979 CODE:
874 PUTBACK; 980 PUTBACK;
981 SV_CORO (next, "Coro::transfer");
982 SV_CORO (prev, "Coro::transfer");
875 transfer (aTHX_ prev, next, flags); 983 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
876 SPAGAIN; 984 SPAGAIN;
877 985
878void 986void
879DESTROY(coro) 987DESTROY(coro)
880 Coro::State coro 988 Coro::State coro
882 990
883 if (coro->mainstack && coro->mainstack != main_mainstack) 991 if (coro->mainstack && coro->mainstack != main_mainstack)
884 { 992 {
885 struct coro temp; 993 struct coro temp;
886 994
995 PUTBACK;
887 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 996 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
888 LOAD(aTHX_ coro); 997 LOAD(aTHX_ coro);
998 SPAGAIN;
889 999
890 destroy_stacks (aTHX); 1000 destroy_stacks (aTHX);
891 1001
892 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1002 LOAD((&temp)); /* this will get rid of defsv etc.. */
1003 SPAGAIN;
893 1004
894 coro->mainstack = 0; 1005 coro->mainstack = 0;
895 } 1006 }
896 1007
897 deallocate_stack (coro); 1008 deallocate_stack (coro);
898 1009 SvREFCNT_dec (coro->args);
899 Safefree (coro); 1010 Safefree (coro);
900 1011
901void 1012void
902flush() 1013flush()
903 CODE: 1014 CODE:
930 SV *sv; 1041 SV *sv;
931 AV *defav = GvAV (PL_defgv); 1042 AV *defav = GvAV (PL_defgv);
932 struct coro *prev, *next; 1043 struct coro *prev, *next;
933 1044
934 if (!returnstk) 1045 if (!returnstk)
935 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 1046 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE));
936 1047
937 /* set up @_ -- ugly */ 1048 /* set up @_ -- ugly */
938 av_clear (defav); 1049 av_clear (defav);
939 av_fill (defav, items - 1); 1050 av_fill (defav, items - 1);
940 while (items--) 1051 while (items--)
967 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1078 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
968 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1079 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
969 1080
970 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1081 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
971 coro_ready[i] = newAV (); 1082 coro_ready[i] = newAV ();
1083
1084 {
1085 SV *sv = perl_get_sv("Coro::API", 1);
1086
1087 coroapi.schedule = api_schedule;
1088 coroapi.cede = api_cede;
1089 coroapi.ready = api_ready;
1090 coroapi.nready = &coro_nready;
1091 coroapi.current = coro_current;
1092
1093 GCoroAPI = &coroapi;
1094 sv_setiv(sv, (IV)&coroapi);
1095 SvREADONLY_on(sv);
1096 }
972} 1097}
973 1098
974void 1099void
975ready(self) 1100ready(self)
976 SV * self 1101 SV * self
1102 PROTOTYPE: $
977 CODE: 1103 CODE:
978 coro_enq (SvREFCNT_inc (self)); 1104 api_ready (self);
1105
1106int
1107nready(...)
1108 PROTOTYPE:
1109 CODE:
1110 RETVAL = coro_nready;
1111 OUTPUT:
1112 RETVAL
979 1113
980void 1114void
981schedule(...) 1115schedule(...)
982 ALIAS: 1116 PROTOTYPE:
983 cede = 1
984 CODE: 1117 CODE:
985 SV *prev, *next; 1118 api_schedule ();
986 1119
987 prev = GvSV (coro_current); 1120void
1121cede(...)
1122 PROTOTYPE:
1123 CODE:
1124 api_cede ();
988 1125
989 if (ix)
990 coro_enq (SvREFCNT_inc (prev));
991
992 next = coro_deq (PRIO_MIN);
993
994 if (!next)
995 next = SvREFCNT_inc (GvSV (coro_idle));
996
997 GvSV (coro_current) = SvREFCNT_inc (next);
998 transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"),
999 sv_to_coro (next, "Coro::schedule", "next coroutine"),
1000 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
1001 SvREFCNT_dec (next);
1002 SvREFCNT_dec (prev);
1003

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines