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.111 by root, Thu Nov 30 18:21:14 2006 UTC vs.
Revision 1.118 by root, Fri Dec 1 19:58:53 2006 UTC

7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if USE_VALGRIND 9#if USE_VALGRIND
10# include <valgrind/valgrind.h> 10# include <valgrind/valgrind.h>
11#endif 11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
12 15
13#define PERL_VERSION_ATLEAST(a,b,c) \ 16#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \ 17 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \ 18 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \ 19 && (PERL_VERSION > (b) \
127 int valgrind_id; 130 int valgrind_id;
128#endif 131#endif
129} coro_cctx; 132} coro_cctx;
130 133
131enum { 134enum {
132 CF_RUNNING, /* coroutine is running */ 135 CF_RUNNING = 0x0001, /* coroutine is running */
133 CF_READY, /* coroutine is ready */ 136 CF_READY = 0x0002, /* coroutine is ready */
137 CF_NEW = 0x0004, /* ahs never been switched to */
134}; 138};
135 139
136/* this is a structure representing a perl-level coroutine */ 140/* this is a structure representing a perl-level coroutine */
137struct coro { 141struct coro {
138 /* the c coroutine allocated to this perl coroutine, if any */ 142 /* the c coroutine allocated to this perl coroutine, if any */
277} 281}
278 282
279#define SB do { 283#define SB do {
280#define SE } while (0) 284#define SE } while (0)
281 285
282#define LOAD(state) load_state((state)); 286#define LOAD(state) load_state ((state))
283#define SAVE(state,flags) save_state((state),(flags)); 287#define SAVE(state,flags) save_state ((state),(flags))
284 288
285#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 289#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
286 290
287static void 291static void
288load_state(Coro__State c) 292load_state (Coro__State c)
289{ 293{
290#define VAR(name,type) PL_ ## name = c->name; 294#define VAR(name,type) PL_ ## name = c->name;
291# include "state.h" 295# include "state.h"
292#undef VAR 296#undef VAR
293 297
310 PUTBACK; 314 PUTBACK;
311 } 315 }
312} 316}
313 317
314static void 318static void
315save_state(Coro__State c, int flags) 319save_state (Coro__State c, int flags)
316{ 320{
317 { 321 {
318 dSP; 322 dSP;
319 I32 cxix = cxstack_ix; 323 I32 cxix = cxstack_ix;
320 PERL_CONTEXT *ccstk = cxstack; 324 PERL_CONTEXT *ccstk = cxstack;
331 { 335 {
332 while (cxix >= 0) 336 while (cxix >= 0)
333 { 337 {
334 PERL_CONTEXT *cx = &ccstk[cxix--]; 338 PERL_CONTEXT *cx = &ccstk[cxix--];
335 339
336 if (CxTYPE(cx) == CXt_SUB) 340 if (CxTYPE (cx) == CXt_SUB)
337 { 341 {
338 CV *cv = cx->blk_sub.cv; 342 CV *cv = cx->blk_sub.cv;
339 343
340 if (CvDEPTH (cv)) 344 if (CvDEPTH (cv))
341 { 345 {
342 EXTEND (SP, 3); 346 EXTEND (SP, 3);
343 347
344 PUSHs ((SV *)CvPADLIST(cv)); 348 PUSHs ((SV *)CvPADLIST (cv));
345 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 349 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
346 PUSHs ((SV *)cv); 350 PUSHs ((SV *)cv);
347 351
348 CvDEPTH (cv) = 0; 352 CvDEPTH (cv) = 0;
349 get_padlist (cv); 353 get_padlist (cv);
350 } 354 }
351 } 355 }
352#ifdef CXt_FORMAT 356#ifdef CXt_FORMAT
353 else if (CxTYPE(cx) == CXt_FORMAT) 357 else if (CxTYPE (cx) == CXt_FORMAT)
354 { 358 {
355 /* I never used formats, so how should I know how these are implemented? */ 359 /* I never used formats, so how should I know how these are implemented? */
356 /* my bold guess is as a simple, plain sub... */ 360 /* my bold guess is as a simple, plain sub... */
357 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 361 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
358 } 362 }
386 * not usually need a lot of stackspace. 390 * not usually need a lot of stackspace.
387 */ 391 */
388static void 392static void
389coro_init_stacks () 393coro_init_stacks ()
390{ 394{
391 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 395 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
392 PL_curstackinfo->si_type = PERLSI_MAIN; 396 PL_curstackinfo->si_type = PERLSI_MAIN;
393 PL_curstack = PL_curstackinfo->si_stack; 397 PL_curstack = PL_curstackinfo->si_stack;
394 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 398 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
395 399
396 PL_stack_base = AvARRAY(PL_curstack); 400 PL_stack_base = AvARRAY(PL_curstack);
397 PL_stack_sp = PL_stack_base; 401 PL_stack_sp = PL_stack_base;
398 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 402 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
399 403
400 New(50,PL_tmps_stack,96,SV*); 404 New(50,PL_tmps_stack,128,SV*);
401 PL_tmps_floor = -1; 405 PL_tmps_floor = -1;
402 PL_tmps_ix = -1; 406 PL_tmps_ix = -1;
403 PL_tmps_max = 96; 407 PL_tmps_max = 128;
404 408
405 New(54,PL_markstack,16,I32); 409 New(54,PL_markstack,32,I32);
406 PL_markstack_ptr = PL_markstack; 410 PL_markstack_ptr = PL_markstack;
407 PL_markstack_max = PL_markstack + 16; 411 PL_markstack_max = PL_markstack + 32;
408 412
409#ifdef SET_MARK_OFFSET 413#ifdef SET_MARK_OFFSET
410 SET_MARK_OFFSET; 414 SET_MARK_OFFSET;
411#endif 415#endif
412 416
413 New(54,PL_scopestack,16,I32); 417 New(54,PL_scopestack,32,I32);
414 PL_scopestack_ix = 0; 418 PL_scopestack_ix = 0;
415 PL_scopestack_max = 16; 419 PL_scopestack_max = 32;
416 420
417 New(54,PL_savestack,96,ANY); 421 New(54,PL_savestack,64,ANY);
418 PL_savestack_ix = 0; 422 PL_savestack_ix = 0;
419 PL_savestack_max = 96; 423 PL_savestack_max = 64;
420 424
421#if !PERL_VERSION_ATLEAST (5,9,0) 425#if !PERL_VERSION_ATLEAST (5,9,0)
422 New(54,PL_retstack,8,OP*); 426 New(54,PL_retstack,16,OP*);
423 PL_retstack_ix = 0; 427 PL_retstack_ix = 0;
424 PL_retstack_max = 8; 428 PL_retstack_max = 16;
425#endif 429#endif
426} 430}
427 431
428/* 432/*
429 * destroy the stacks, the callchain etc... 433 * destroy the stacks, the callchain etc...
482 * emulate part of the perl startup here. 486 * emulate part of the perl startup here.
483 */ 487 */
484 488
485 coro_init_stacks (); 489 coro_init_stacks ();
486 490
487 PL_curcop = 0; 491 PL_curcop = &PL_compiling;
488 PL_in_eval = 0; 492 PL_in_eval = EVAL_NULL;
489 PL_curpm = 0; 493 PL_curpm = 0;
494 PL_localizing = 0;
495 PL_dirty = 0;
496 PL_restartop = 0;
490 497
491 { 498 {
492 dSP; 499 dSP;
493 LOGOP myop; 500 LOGOP myop;
494 501
507 PUSHMARK (SP); 514 PUSHMARK (SP);
508 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 515 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
509 PUTBACK; 516 PUTBACK;
510 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 517 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
511 SPAGAIN; 518 SPAGAIN;
512
513 ENTER; /* necessary e.g. for dounwind */
514 } 519 }
520
521 ENTER; /* necessary e.g. for dounwind */
515} 522}
516 523
517static void 524static void
518free_coro_mortal () 525free_coro_mortal ()
519{ 526{
613 620
614 return cctx; 621 return cctx;
615} 622}
616 623
617static void 624static void
618cctx_free (coro_cctx *cctx) 625cctx_destroy (coro_cctx *cctx)
619{ 626{
620 if (!cctx) 627 if (!cctx)
621 return; 628 return;
622 629
623 --cctx_count; 630 --cctx_count;
640{ 647{
641 coro_cctx *cctx; 648 coro_cctx *cctx;
642 649
643 if (cctx_first) 650 if (cctx_first)
644 { 651 {
645 --cctx_idle;
646 cctx = cctx_first; 652 cctx = cctx_first;
647 cctx_first = cctx->next; 653 cctx_first = cctx->next;
654 --cctx_idle;
648 } 655 }
649 else 656 else
650 { 657 {
651 cctx = cctx_new (); 658 cctx = cctx_new ();
652 PL_op = PL_op->op_next; 659 PL_op = PL_op->op_next;
656} 663}
657 664
658static void 665static void
659cctx_put (coro_cctx *cctx) 666cctx_put (coro_cctx *cctx)
660{ 667{
668 /* free another cctx if overlimit */
669 if (cctx_idle >= MAX_IDLE_CCTX)
670 {
671 coro_cctx *first = cctx_first;
672 cctx_first = first->next;
673 --cctx_idle;
674
675 assert (!first->inuse);
676 cctx_destroy (first);
677 }
678
661 ++cctx_idle; 679 ++cctx_idle;
662 cctx->next = cctx_first; 680 cctx->next = cctx_first;
663 cctx_first = cctx; 681 cctx_first = cctx;
664} 682}
665 683
674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 692 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
675 else if (prev != next) 693 else if (prev != next)
676 { 694 {
677 coro_cctx *prev__cctx; 695 coro_cctx *prev__cctx;
678 696
679 if (!prev->cctx) 697 if (prev->flags & CF_NEW)
680 { 698 {
681 /* create a new empty context */ 699 /* create a new empty context */
682 Newz (0, prev->cctx, 1, coro_cctx); 700 Newz (0, prev->cctx, 1, coro_cctx);
683 prev->cctx->inuse = 1; 701 prev->cctx->inuse = 1;
702 prev->flags &= ~CF_NEW;
684 prev->flags |= CF_RUNNING; 703 prev->flags |= CF_RUNNING;
685 } 704 }
686 705
706 /*TODO: must not croak here */
687 assert ( prev->flags & CF_RUNNING); 707 if (!prev->flags & CF_RUNNING)
708 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
709
688 assert (!next->flags & CF_RUNNING); 710 if (next->flags & CF_RUNNING)
711 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
689 712
690 prev->flags &= ~CF_RUNNING; 713 prev->flags &= ~CF_RUNNING;
691 next->flags |= CF_RUNNING; 714 next->flags |= CF_RUNNING;
692 715
693 LOCK; 716 LOCK;
694 717
695 if (next->mainstack) 718 if (next->flags & CF_NEW)
719 {
720 /* need to start coroutine */
721 next->flags &= ~CF_NEW;
722 /* first get rid of the old state */
723 SAVE (prev, -1);
724 /* setup coroutine call */
725 setup_coro (next);
726 /* need a new stack */
727 assert (!next->stack);
728 }
729 else
696 { 730 {
697 /* coroutine already started */ 731 /* coroutine already started */
698 SAVE (prev, flags); 732 SAVE (prev, flags);
699 LOAD (next); 733 LOAD (next);
700 } 734 }
701 else 735
736 prev__cctx = prev->cctx;
737
738 /* possibly "free" the cctx */
739 if (prev__cctx->idle_sp == STACKLEVEL && 0)
702 { 740 {
703 /* need to start coroutine */ 741 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
704 /* first get rid of the old state */ 742 assert (PL_top_env == prev__cctx->top_env);
705 SAVE (prev, -1); 743
706 /* setup coroutine call */
707 setup_coro (next);
708 /* need a stack */
709 assert (!next->stack);
710 next->cctx = 0; 744 prev->cctx = 0;
745
746 cctx_put (prev__cctx);
747 prev__cctx->inuse = 0;
711 } 748 }
712 749
713 prev__cctx = prev->cctx; 750 if (!next->cctx)
714
715 /* possibly "free" the cctx */
716 if (prev__cctx->idle_sp == STACKLEVEL)
717 { 751 {
718 assert (PL_top_env == prev__cctx->top_env);//D 752 next->cctx = cctx_get ();
719 cctx_put (prev__cctx); 753 assert (!next->cctx->inuse);
720 prev->cctx = 0; 754 next->cctx->inuse = 1;
721 } 755 }
722
723 if (!next->cctx)
724 next->cctx = cctx_get ();
725 756
726 if (prev__cctx != next->cctx) 757 if (prev__cctx != next->cctx)
727 { 758 {
728 assert ( prev__cctx->inuse);
729 assert (!next->cctx->inuse);
730
731 prev__cctx->inuse = 0;
732 next->cctx->inuse = 1;
733
734 prev__cctx->top_env = PL_top_env; 759 prev__cctx->top_env = PL_top_env;
735 PL_top_env = next->cctx->top_env; 760 PL_top_env = next->cctx->top_env;
736 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 761 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
737 } 762 }
738 763
758 783
759 if (coro->mainstack && coro->mainstack != main_mainstack) 784 if (coro->mainstack && coro->mainstack != main_mainstack)
760 { 785 {
761 struct coro temp; 786 struct coro temp;
762 787
788 if (coro->flags & CF_RUNNING)
789 croak ("FATAL: tried to destroy currently running coroutine");
790
763 SAVE ((&temp), TRANSFER_SAVE_ALL); 791 SAVE ((&temp), TRANSFER_SAVE_ALL);
764 LOAD (coro); 792 LOAD (coro);
765 793
766 coro_destroy_stacks (); 794 coro_destroy_stacks ();
767 795
768 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 796 LOAD ((&temp)); /* this will get rid of defsv etc.. */
769 797
770 coro->mainstack = 0; 798 coro->mainstack = 0;
771 } 799 }
772 800
773 cctx_free (coro->cctx); 801 cctx_destroy (coro->cctx);
774 SvREFCNT_dec (coro->args); 802 SvREFCNT_dec (coro->args);
775 Safefree (coro); 803 Safefree (coro);
776} 804}
777 805
778static int 806static int
838} 866}
839 867
840static void 868static void
841api_transfer (SV *prev, SV *next, int flags) 869api_transfer (SV *prev, SV *next, int flags)
842{ 870{
843 dTHX;
844 struct transfer_args ta; 871 struct transfer_args ta;
845 872
846 prepare_transfer (&ta, prev, next, flags); 873 prepare_transfer (&ta, prev, next, flags);
847 TRANSFER (ta); 874 TRANSFER (ta);
848} 875}
855#define PRIO_LOW -1 882#define PRIO_LOW -1
856#define PRIO_IDLE -3 883#define PRIO_IDLE -3
857#define PRIO_MIN -4 884#define PRIO_MIN -4
858 885
859/* for Coro.pm */ 886/* for Coro.pm */
860static GV *coro_current, *coro_idle; 887static SV *coro_current;
861static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 888static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
862static int coro_nready; 889static int coro_nready;
863 890
864static void 891static void
865coro_enq (SV *sv) 892coro_enq (SV *coro_sv)
866{ 893{
867 int prio;
868
869 if (SvTYPE (sv) != SVt_PVHV)
870 croak ("Coro::ready tried to enqueue something that is not a coroutine");
871
872 prio = SvSTATE (sv)->prio;
873
874 av_push (coro_ready [prio - PRIO_MIN], sv); 894 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
875 coro_nready++; 895 coro_nready++;
876} 896}
877 897
878static SV * 898static SV *
879coro_deq (int min_prio) 899coro_deq (int min_prio)
901 921
902 if (SvROK (coro_sv)) 922 if (SvROK (coro_sv))
903 coro_sv = SvRV (coro_sv); 923 coro_sv = SvRV (coro_sv);
904 924
905 coro = SvSTATE (coro_sv); 925 coro = SvSTATE (coro_sv);
926
906 if (coro->flags & CF_READY) 927 if (coro->flags & CF_READY)
907 return 0; 928 return 0;
929
930#if 0 /* this is actually harmless */
931 if (coro->flags & CF_RUNNING)
932 croak ("Coro::ready called on currently running coroutine");
933#endif
908 934
909 coro->flags |= CF_READY; 935 coro->flags |= CF_READY;
910 936
911 LOCK; 937 LOCK;
912 coro_enq (SvREFCNT_inc (coro_sv)); 938 coro_enq (SvREFCNT_inc (coro_sv));
916} 942}
917 943
918static int 944static int
919api_is_ready (SV *coro_sv) 945api_is_ready (SV *coro_sv)
920{ 946{
921 struct coro *coro;
922
923 return !!(SvSTATE (coro_sv)->flags & CF_READY); 947 return !!SvSTATE (coro_sv)->flags & CF_READY;
924} 948}
925 949
926static void 950static void
927prepare_schedule (struct transfer_args *ta) 951prepare_schedule (struct transfer_args *ta)
928{ 952{
929 SV *current, *prev, *next; 953 SV *prev, *next;
930
931 current = GvSV (coro_current);
932 954
933 for (;;) 955 for (;;)
934 { 956 {
935 LOCK; 957 LOCK;
936 next = coro_deq (PRIO_MIN); 958 next = coro_deq (PRIO_MIN);
945 ENTER; 967 ENTER;
946 SAVETMPS; 968 SAVETMPS;
947 969
948 PUSHMARK (SP); 970 PUSHMARK (SP);
949 PUTBACK; 971 PUTBACK;
950 call_sv (GvSV (coro_idle), G_DISCARD); 972 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
951 973
952 FREETMPS; 974 FREETMPS;
953 LEAVE; 975 LEAVE;
954 } 976 }
955 } 977 }
956 978
957 prev = SvRV (current); 979 prev = SvRV (coro_current);
958 SvRV (current) = next; 980 SvRV_set (coro_current, next);
959 981
960 /* free this only after the transfer */ 982 /* free this only after the transfer */
961 LOCK; 983 LOCK;
962 free_coro_mortal (); 984 free_coro_mortal ();
963 UNLOCK; 985 UNLOCK;
964 coro_mortal = prev; 986 coro_mortal = prev;
965 987
988 assert (!SvROK(prev));//D
989 assert (!SvROK(next));//D
990
966 ta->prev = SvSTATE (prev); 991 ta->prev = SvSTATE (prev);
967 ta->next = SvSTATE (next); 992 ta->next = SvSTATE (next);
968 ta->flags = TRANSFER_SAVE_ALL; 993 ta->flags = TRANSFER_SAVE_ALL;
969 994
995 assert (ta->flags & CF_READY);
970 ta->next->flags &= ~CF_READY; 996 ta->next->flags &= ~CF_READY;
971} 997}
972 998
973static void 999static void
974prepare_cede (struct transfer_args *ta) 1000prepare_cede (struct transfer_args *ta)
975{ 1001{
976 api_ready (GvSV (coro_current)); 1002 api_ready (coro_current);
977 1003
978 prepare_schedule (ta); 1004 prepare_schedule (ta);
979} 1005}
980 1006
981static void 1007static void
982api_schedule (void) 1008api_schedule (void)
983{ 1009{
984 dTHX;
985 struct transfer_args ta; 1010 struct transfer_args ta;
986 1011
987 prepare_schedule (&ta); 1012 prepare_schedule (&ta);
988 TRANSFER (ta); 1013 TRANSFER (ta);
989} 1014}
990 1015
991static void 1016static void
992api_cede (void) 1017api_cede (void)
993{ 1018{
994 dTHX;
995 struct transfer_args ta; 1019 struct transfer_args ta;
996 1020
997 prepare_cede (&ta); 1021 prepare_cede (&ta);
998 TRANSFER (ta); 1022 TRANSFER (ta);
999} 1023}
1031 HV *hv; 1055 HV *hv;
1032 int i; 1056 int i;
1033 1057
1034 Newz (0, coro, 1, struct coro); 1058 Newz (0, coro, 1, struct coro);
1035 coro->args = newAV (); 1059 coro->args = newAV ();
1060 coro->flags = CF_NEW;
1036 1061
1037 hv = newHV (); 1062 hv = newHV ();
1038 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1063 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1039 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1064 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1040 1065
1048_set_stacklevel (...) 1073_set_stacklevel (...)
1049 ALIAS: 1074 ALIAS:
1050 Coro::State::transfer = 1 1075 Coro::State::transfer = 1
1051 Coro::schedule = 2 1076 Coro::schedule = 2
1052 Coro::cede = 3 1077 Coro::cede = 3
1053 Coro::Cont::yield = 4
1054 CODE: 1078 CODE:
1055{ 1079{
1056 struct transfer_args ta; 1080 struct transfer_args ta;
1057 1081
1058 switch (ix) 1082 switch (ix)
1063 ta.flags = TRANSFER_SET_STACKLEVEL; 1087 ta.flags = TRANSFER_SET_STACKLEVEL;
1064 break; 1088 break;
1065 1089
1066 case 1: 1090 case 1:
1067 if (items != 3) 1091 if (items != 3)
1068 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1092 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items);
1069 1093
1070 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1094 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1071 break; 1095 break;
1072 1096
1073 case 2: 1097 case 2:
1075 break; 1099 break;
1076 1100
1077 case 3: 1101 case 3:
1078 prepare_cede (&ta); 1102 prepare_cede (&ta);
1079 break; 1103 break;
1080
1081 case 4:
1082 {
1083 SV *yieldstack;
1084 SV *sv;
1085 AV *defav = GvAV (PL_defgv);
1086
1087 yieldstack = *hv_fetch (
1088 (HV *)SvRV (GvSV (coro_current)),
1089 "yieldstack", sizeof ("yieldstack") - 1,
1090 0
1091 );
1092
1093 /* set up @_ -- ugly */
1094 av_clear (defav);
1095 av_fill (defav, items - 1);
1096 while (items--)
1097 av_store (defav, items, SvREFCNT_inc (ST(items)));
1098
1099 sv = av_pop ((AV *)SvRV (yieldstack));
1100 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1101 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1102 ta.flags = 0;
1103 SvREFCNT_dec (sv);
1104 }
1105 break;
1106
1107 } 1104 }
1108 1105
1109 TRANSFER (ta); 1106 TRANSFER (ta);
1110} 1107}
1111 1108
1155 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1152 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1156 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1153 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1157 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1154 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1158 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1155 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1159 1156
1160 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1157 coro_current = get_sv ("Coro::current", FALSE);
1161 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1158 SvREADONLY_on (coro_current);
1162 1159
1163 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1160 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1164 coro_ready[i] = newAV (); 1161 coro_ready[i] = newAV ();
1165 1162
1166 { 1163 {
1221 CODE: 1218 CODE:
1222 RETVAL = coro_nready; 1219 RETVAL = coro_nready;
1223 OUTPUT: 1220 OUTPUT:
1224 RETVAL 1221 RETVAL
1225 1222
1223void
1224_set_current (SV *current)
1225 PROTOTYPE: $
1226 CODE:
1227 SvREFCNT_dec (SvRV (coro_current));
1228 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1229
1226MODULE = Coro::State PACKAGE = Coro::AIO 1230MODULE = Coro::State PACKAGE = Coro::AIO
1227 1231
1228SV * 1232SV *
1229_get_state () 1233_get_state ()
1230 CODE: 1234 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines