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.14 by root, Mon Jul 23 23:48:05 2001 UTC vs.
Revision 1.22 by root, Sat Aug 11 23:10:56 2001 UTC

2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "libcoro/coro.c" 5#include "libcoro/coro.c"
6 6
7#include <signal.h>
8
7#ifdef HAVE_MMAP 9#ifdef HAVE_MMAP
8# include <unistd.h> 10# include <unistd.h>
9# include <sys/mman.h> 11# include <sys/mman.h>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS
15# else
16# undef HAVE_MMAP
17# endif
18# endif
10#endif 19#endif
11 20
12#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize */
13 22
23/* perl-related */
14#define TRANSFER_SAVE_DEFAV 0x00000001 24#define TRANSFER_SAVE_DEFAV 0x00000001
15#define TRANSFER_SAVE_DEFSV 0x00000002 25#define TRANSFER_SAVE_DEFSV 0x00000002
16#define TRANSFER_SAVE_ERRSV 0x00000004 26#define TRANSFER_SAVE_ERRSV 0x00000004
27/* c-related */
17#define TRANSFER_SAVE_CCTXT 0x00000008 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
18 34
19#define TRANSFER_SAVE_ALL -1 35#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
36 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
20 37
21#define SUB_INIT "Coro::State::initialize" 38#define SUB_INIT "Coro::State::initialize"
22#define UCORO_STATE "_coro_state" 39#define UCORO_STATE "_coro_state"
23 40
41/* The next macro should delcare a variable stacklevel that contains and approximation
42 * to the current C stack pointer. It's property is that it changes with each call
43 * and should be unique. */
44#define dSTACKLEVEL void *stacklevel = &stacklevel
45
46#define labs(l) ((l) >= 0 ? (l) : -(l))
47
48/* this is actually not only the c stack but also c registers etc... */
49typedef struct {
50 int refcnt; /* pointer reference counter */
51 int usecnt; /* shared by how many coroutines */
52 int gencnt; /* generation counter */
53
54 coro_context cctx;
55
56 void *sptr;
57 long ssize; /* positive == mmap, otherwise malloc */
58} coro_stack;
59
24struct coro { 60struct coro {
25 /* the optional C context */ 61 /* the optional C context */
26 coro_context cctx; 62 coro_stack *stack;
27 void *sptr; 63 void *cursp;
28 long ssize; 64 int gencnt;
29 65
30 /* optionally saved, might be zero */ 66 /* optionally saved, might be zero */
31 AV *defav; 67 AV *defav;
32 SV *defsv; 68 SV *defsv;
33 SV *errsv; 69 SV *errsv;
34 70
35 /* saved global state not related to stacks */ 71 /* saved global state not related to stacks */
36 U8 dowarn; 72 U8 dowarn;
73 I32 in_eval;
37 74
38 /* the stacks and related info (callchain etc..) */ 75 /* the stacks and related info (callchain etc..) */
39 PERL_SI *curstackinfo; 76 PERL_SI *curstackinfo;
40 AV *curstack; 77 AV *curstack;
41 AV *mainstack; 78 AV *mainstack;
59 I32 savestack_max; 96 I32 savestack_max;
60 OP **retstack; 97 OP **retstack;
61 I32 retstack_ix; 98 I32 retstack_ix;
62 I32 retstack_max; 99 I32 retstack_max;
63 COP *curcop; 100 COP *curcop;
64 JMPENV start_env;
65 JMPENV *top_env; 101 JMPENV *top_env;
66 102
67 /* data associated with this coroutine (initial args) */ 103 /* data associated with this coroutine (initial args) */
68 AV *args; 104 AV *args;
69}; 105};
254 290
255static void 291static void
256load_state(pTHX_ Coro__State c) 292load_state(pTHX_ Coro__State c)
257{ 293{
258 PL_dowarn = c->dowarn; 294 PL_dowarn = c->dowarn;
295 PL_in_eval = c->in_eval;
259 296
260 PL_curstackinfo = c->curstackinfo; 297 PL_curstackinfo = c->curstackinfo;
261 PL_curstack = c->curstack; 298 PL_curstack = c->curstack;
262 PL_mainstack = c->mainstack; 299 PL_mainstack = c->mainstack;
263 PL_stack_sp = c->stack_sp; 300 PL_stack_sp = c->stack_sp;
280 PL_savestack_max = c->savestack_max; 317 PL_savestack_max = c->savestack_max;
281 PL_retstack = c->retstack; 318 PL_retstack = c->retstack;
282 PL_retstack_ix = c->retstack_ix; 319 PL_retstack_ix = c->retstack_ix;
283 PL_retstack_max = c->retstack_max; 320 PL_retstack_max = c->retstack_max;
284 PL_curcop = c->curcop; 321 PL_curcop = c->curcop;
285 PL_start_env = c->start_env;
286 PL_top_env = c->top_env; 322 PL_top_env = c->top_env;
287 323
288 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 324 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
289 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 325 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
290 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 326 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
389 /* but if it's missing the defav contents magically get replaced sometimes */ 425 /* but if it's missing the defav contents magically get replaced sometimes */
390 if (c->defav) 426 if (c->defav)
391 av_reify (c->defav); 427 av_reify (c->defav);
392 428
393 c->dowarn = PL_dowarn; 429 c->dowarn = PL_dowarn;
430 c->in_eval = PL_in_eval;
394 431
395 c->curstackinfo = PL_curstackinfo; 432 c->curstackinfo = PL_curstackinfo;
396 c->curstack = PL_curstack; 433 c->curstack = PL_curstack;
397 c->mainstack = PL_mainstack; 434 c->mainstack = PL_mainstack;
398 c->stack_sp = PL_stack_sp; 435 c->stack_sp = PL_stack_sp;
415 c->savestack_max = PL_savestack_max; 452 c->savestack_max = PL_savestack_max;
416 c->retstack = PL_retstack; 453 c->retstack = PL_retstack;
417 c->retstack_ix = PL_retstack_ix; 454 c->retstack_ix = PL_retstack_ix;
418 c->retstack_max = PL_retstack_max; 455 c->retstack_max = PL_retstack_max;
419 c->curcop = PL_curcop; 456 c->curcop = PL_curcop;
420 c->start_env = PL_start_env;
421 c->top_env = PL_top_env; 457 c->top_env = PL_top_env;
422} 458}
423 459
424/* 460/*
425 * allocate various perl stacks. This is an exact copy 461 * allocate various perl stacks. This is an exact copy
437 473
438 PL_stack_base = AvARRAY(PL_curstack); 474 PL_stack_base = AvARRAY(PL_curstack);
439 PL_stack_sp = PL_stack_base; 475 PL_stack_sp = PL_stack_base;
440 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 476 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
441 477
442 New(50,PL_tmps_stack,64,SV*); 478 New(50,PL_tmps_stack,96,SV*);
443 PL_tmps_floor = -1; 479 PL_tmps_floor = -1;
444 PL_tmps_ix = -1; 480 PL_tmps_ix = -1;
445 PL_tmps_max = 64; 481 PL_tmps_max = 96;
446 482
447 New(54,PL_markstack,12,I32); 483 New(54,PL_markstack,16,I32);
448 PL_markstack_ptr = PL_markstack; 484 PL_markstack_ptr = PL_markstack;
449 PL_markstack_max = PL_markstack + 12; 485 PL_markstack_max = PL_markstack + 16;
450 486
451 SET_MARK_OFFSET; 487 SET_MARK_OFFSET;
452 488
453 New(54,PL_scopestack,12,I32); 489 New(54,PL_scopestack,16,I32);
454 PL_scopestack_ix = 0; 490 PL_scopestack_ix = 0;
455 PL_scopestack_max = 12; 491 PL_scopestack_max = 16;
456 492
457 New(54,PL_savestack,64,ANY); 493 New(54,PL_savestack,96,ANY);
458 PL_savestack_ix = 0; 494 PL_savestack_ix = 0;
459 PL_savestack_max = 64; 495 PL_savestack_max = 96;
460 496
461 New(54,PL_retstack,8,OP*); 497 New(54,PL_retstack,8,OP*);
462 PL_retstack_ix = 0; 498 PL_retstack_ix = 0;
463 PL_retstack_max = 8; 499 PL_retstack_max = 8;
464} 500}
505 Safefree(PL_savestack); 541 Safefree(PL_savestack);
506 Safefree(PL_retstack); 542 Safefree(PL_retstack);
507} 543}
508 544
509static void 545static void
510allocate_stack (Coro__State ctx) 546allocate_stack (Coro__State ctx, int alloc)
511{ 547{
548 coro_stack *stack;
549
550 New (0, stack, 1, coro_stack);
551
552 stack->refcnt = 1;
553 stack->usecnt = 1;
554 stack->gencnt = ctx->gencnt = 0;
555 if (alloc)
556 {
512#ifdef HAVE_MMAP 557#ifdef HAVE_MMAP
513 ctx->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-use */ 558 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
514 ctx->sptr = mmap (0, ctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 559 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
515 if (ctx->sptr == (void *)-1) 560 if (stack->sptr == (void *)-1)
516#endif 561#endif
517 { 562 {
518 /*FIXME*//*D*//* reasonable stack size! */ 563 /*FIXME*//*D*//* reasonable stack size! */
519 ctx->ssize = 4096 * sizeof (long); 564 stack->ssize = -4096 * sizeof (long);
520 New (0, ctx->sptr, 4096, long); 565 New (0, stack->sptr, 4096, long);
566 }
521 } 567 }
568 else
569 stack->sptr = 0;
570
571 ctx->stack = stack;
522} 572}
523 573
524static void 574static void
525deallocate_stack (Coro__State ctx) 575deallocate_stack (Coro__State ctx)
526{ 576{
577 coro_stack *stack = ctx->stack;
578
579 ctx->stack = 0;
580
581 if (stack)
582 {
583 if (!--stack->refcnt)
584 {
527#ifdef HAVE_MMAP 585#ifdef HAVE_MMAP
528 munmap (ctx->sptr, ctx->ssize); 586 if (stack->ssize > 0 && stack->sptr)
587 munmap (stack->sptr, stack->ssize);
588 else
529#else 589#else
530 Safefree (ctx->sptr); 590 Safefree (stack->sptr);
531#endif 591#endif
592 Safefree (stack);
593 }
594 else if (ctx->gencnt == stack->gencnt)
595 --stack->usecnt;
596 }
532} 597}
533 598
534/* might go away together with optional SAVE_CCTXT */
535static void 599static void
536setup_coro (void *arg) 600setup_coro (void *arg)
537{ 601{
538 /* 602 /*
539 * emulate part of the perl startup here. 603 * emulate part of the perl startup here.
541 dSP; 605 dSP;
542 Coro__State ctx = (Coro__State)arg; 606 Coro__State ctx = (Coro__State)arg;
543 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 607 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
544 608
545 coro_init_stacks (aTHX); 609 coro_init_stacks (aTHX);
546 JMPENV_BOOTSTRAP;
547 SPAGAIN;
548
549 /*PL_curcop = 0;*/ 610 /*PL_curcop = 0;*/
611 /*PL_in_eval = PL_in_eval;*/ /* inherit */
550 SvREFCNT_dec (GvAV (PL_defgv)); 612 SvREFCNT_dec (GvAV (PL_defgv));
551 GvAV (PL_defgv) = ctx->args; 613 GvAV (PL_defgv) = ctx->args;
552 614
615 SPAGAIN;
616
553 if (ctx->sptr) 617 if (ctx->stack)
554 { 618 {
619 ctx->cursp = 0;
620
555 PUSHMARK(SP); 621 PUSHMARK(SP);
556 PUTBACK; 622 PUTBACK;
557 (void) call_sv (sub_init, G_VOID|G_NOARGS); 623 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
624
625 if (SvTRUE (ERRSV))
626 croak (NULL);
627 else
558 croak ("FATAL: CCTXT coroutine returned!"); 628 croak ("FATAL: CCTXT coroutine returned!");
559 } 629 }
560 else 630 else
561 { 631 {
562 UNOP myop; 632 UNOP myop;
563 633
582 652
583 ENTER; /* necessary e.g. for dounwind */ 653 ENTER; /* necessary e.g. for dounwind */
584 } 654 }
585} 655}
586 656
657static void
658continue_coro (void *arg)
659{
660 /*
661 * this is a _very_ stripped down perl interpreter ;)
662 */
663 Coro__State ctx = (Coro__State)arg;
664
665 /*FIXME*//* must set up top_env here */
666 ctx->cursp = 0;
667 PL_op = PL_op->op_next;
668 CALLRUNOPS(aTHX);
669
670 abort ();
671}
672
587STATIC void 673STATIC void
588transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 674transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
589{ 675{
590 dSP; 676 dSP;
677 dSTACKLEVEL;
591 678
592 if (prev != next) 679 if (prev != next)
593 { 680 {
594 /*
595 * this could be done in newprocess which would lead to
596 * extremely elegant and fast (basically just SAVE/LOAD)
597 * code here, but lazy allocation of stacks has also
598 * some virtues and the overhead of the if() is nil.
599 */
600 if (next->mainstack) 681 if (next->mainstack)
601 { 682 {
602 SAVE (prev, flags); 683 SAVE (prev, flags);
603 LOAD (next); 684 LOAD (next);
604 685
605 /* mark this state as in-use */ 686 /* mark this state as in-use */
606 next->mainstack = 0; 687 next->mainstack = 0;
607 next->tmps_ix = -2; 688 next->tmps_ix = -2;
608 689
690 /* stacklevel changed? if yes, grab the stack for us! */
609 if (flags & TRANSFER_SAVE_CCTXT) 691 if (flags & TRANSFER_SAVE_CCTXT)
610 { 692 {
611 if (!next->ssize)
612 croak ("destination coroutine has no CCTXT (%p, %d)", next->sptr, next->ssize);
613
614 if (!prev->ssize) 693 if (!prev->stack)
615 prev->ssize = 1; /* mark cctx as valid ;) */ 694 allocate_stack (prev, 0);
695 else if (prev->cursp != stacklevel
696 && prev->stack->usecnt > 1)
697 {
698 prev->gencnt = ++prev->stack->gencnt;
699 prev->stack->usecnt = 1;
700 }
616 701
702 /* has our stack been invalidated? */
703 if (next->stack && next->stack->gencnt != next->gencnt)
704 {
705 deallocate_stack (next);
706 allocate_stack (next, 1);
707 coro_create (&(next->stack->cctx),
708 continue_coro, (void *)next,
709 next->stack->sptr, labs (next->stack->ssize));
710 }
711
617 coro_transfer (&(prev->cctx), &(next->cctx)); 712 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
618 } 713 }
619 714
620 } 715 }
621 else if (next->tmps_ix == -2) 716 else if (next->tmps_ix == -2)
622 croak ("tried to transfer to running coroutine"); 717 croak ("tried to transfer to running coroutine");
624 { 719 {
625 SAVE (prev, -1); /* first get rid of the old state */ 720 SAVE (prev, -1); /* first get rid of the old state */
626 721
627 if (flags & TRANSFER_SAVE_CCTXT) 722 if (flags & TRANSFER_SAVE_CCTXT)
628 { 723 {
629 if (!next->ssize) 724 if (!prev->stack)
725 allocate_stack (prev, 0);
726
727 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
630 { 728 {
729 setup_coro (next);
730
731 prev->stack->refcnt++;
732 prev->stack->usecnt++;
733 next->stack = prev->stack;
734 next->gencnt = prev->gencnt;
735 }
736 else
737 {
631 allocate_stack (next); 738 allocate_stack (next, 1);
632 coro_create (&(next->cctx), 739 coro_create (&(next->stack->cctx),
633 setup_coro, (void *)next, 740 setup_coro, (void *)next,
634 next->sptr, next->ssize); 741 next->stack->sptr, labs (next->stack->ssize));
742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
635 } 743 }
636
637 if (!prev->ssize)
638 prev->ssize = 1; /* mark cctx as valid ;) */
639
640 coro_transfer (&(prev->cctx), &(next->cctx));
641 } 744 }
642 else 745 else
643 setup_coro (next); 746 setup_coro (next);
644 } 747 }
645 } 748 }
749
750 next->cursp = stacklevel;
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 {
758 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash);
759
760 if (!he)
761 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
762
763 arg = HeVAL(he);
764 }
765
766 /* must also be changed inside Coro::Cont::yield */
767 if (SvROK(arg) && SvSTASH(SvRV(arg)) == 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);
771}
772
773/** Coro ********************************************************************/
774
775#define PRIO_MAX 3
776#define PRIO_HIGH 1
777#define PRIO_NORMAL 0
778#define PRIO_LOW -1
779#define PRIO_IDLE -3
780#define PRIO_MIN -4
781
782/* for Coro.pm */
783static GV *coro_current, *coro_idle;
784static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
785
786static void
787coro_enq (SV *sv)
788{
789 if (SvROK (sv))
790 {
791 SV *hv = SvRV (sv);
792 if (SvTYPE (hv) == SVt_PVHV)
793 {
794 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0);
795 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
796
797 prio = prio > PRIO_MAX ? PRIO_MAX
798 : prio < PRIO_MIN ? PRIO_MIN
799 : prio;
800
801 av_push (coro_ready [prio - PRIO_MIN], sv);
802
803 return;
804 }
805 }
806
807 croak ("Coro::ready tried to enqueue something that is not a coroutine");
808}
809
810static SV *
811coro_deq (int min_prio)
812{
813 int prio = PRIO_MAX - PRIO_MIN;
814
815 min_prio -= PRIO_MIN;
816 if (min_prio < 0)
817 min_prio = 0;
818
819 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
820 if (av_len (coro_ready[prio]) >= 0)
821 return av_shift (coro_ready[prio]);
822
823 return 0;
646} 824}
647 825
648MODULE = Coro::State PACKAGE = Coro::State 826MODULE = Coro::State PACKAGE = Coro::State
649 827
650PROTOTYPES: ENABLE 828PROTOTYPES: ENABLE
678 856
679 New (0, coro, 1, struct coro); 857 New (0, coro, 1, struct coro);
680 858
681 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 859 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
682 coro->mainstack = 0; /* actual work is done inside transfer */ 860 coro->mainstack = 0; /* actual work is done inside transfer */
683 coro->sptr = 0; 861 coro->stack = 0;
684 coro->ssize = 0;
685 862
686 RETVAL = coro; 863 RETVAL = coro;
687 OUTPUT: 864 OUTPUT:
688 RETVAL 865 RETVAL
689 866
690void 867void
691transfer(prev, next, flags = TRANSFER_SAVE_ALL) 868transfer(prev, next, flags)
692 Coro::State_or_hashref prev 869 Coro::State_or_hashref prev
693 Coro::State_or_hashref next 870 Coro::State_or_hashref next
694 int flags 871 int flags
695 PROTOTYPE: @ 872 PROTOTYPE: @
696 CODE: 873 CODE:
874 PUTBACK;
697 transfer (aTHX_ prev, next, flags); 875 transfer (aTHX_ prev, next, flags);
876 SPAGAIN;
698 877
699void 878void
700DESTROY(coro) 879DESTROY(coro)
701 Coro::State coro 880 Coro::State coro
702 CODE: 881 CODE:
713 LOAD((&temp)); /* this will get rid of defsv etc.. */ 892 LOAD((&temp)); /* this will get rid of defsv etc.. */
714 893
715 coro->mainstack = 0; 894 coro->mainstack = 0;
716 } 895 }
717 896
718 if (coro->sptr)
719 {
720 deallocate_stack (coro); 897 deallocate_stack (coro);
721 coro->sptr = 0;
722 }
723 898
724 Safefree (coro); 899 Safefree (coro);
725 900
726void 901void
727flush() 902flush()
728 CODE: 903 CODE:
729#ifdef MAY_FLUSH 904#ifdef MAY_FLUSH
730 flush_padlist_cache (); 905 flush_padlist_cache ();
731#endif 906#endif
732 907
908void
909_exit(code)
910 int code
911 PROTOTYPE: $
912 CODE:
913#if defined(__GLIBC__) || _POSIX_C_SOURCE
914 _exit (code);
915#else
916 signal (SIGTERM, SIG_DFL);
917 raise (SIGTERM);
918 exit (code);
919#endif
920
733MODULE = Coro::State PACKAGE = Coro::Cont 921MODULE = Coro::State PACKAGE = Coro::Cont
734 922
735# this is slightly dirty 923# this is slightly dirty (should expose a c-level api)
736 924
737void 925void
738yield(...) 926yield(...)
739 PROTOTYPE: @ 927 PROTOTYPE: @
740 CODE: 928 CODE:
758 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 946 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
759 SvREFCNT_dec (sv); 947 SvREFCNT_dec (sv);
760 948
761 transfer(aTHX_ prev, next, 0); 949 transfer(aTHX_ prev, next, 0);
762 950
951MODULE = Coro::State PACKAGE = Coro
952
953# this is slightly dirty (should expose a c-level api)
954
955BOOT:
956{
957 int i;
958 HV *stash = gv_stashpv ("Coro", TRUE);
959
960 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
961 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
962 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
963 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
964 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
965 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
966
967 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
968 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
969
970 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
971 coro_ready[i] = newAV ();
972}
973
974void
975ready(self)
976 SV * self
977 CODE:
978 coro_enq (SvREFCNT_inc (self));
979
980void
981schedule(...)
982 ALIAS:
983 cede = 1
984 CODE:
985 SV *prev, *next;
986
987 prev = GvSV (coro_current);
988
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