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.38 by root, Wed Nov 7 00:21:06 2001 UTC vs.
Revision 1.47 by pcg, Sat Nov 15 03:53:10 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
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
4 27
5#include "libcoro/coro.c" 28#include "libcoro/coro.c"
6 29
7#include <signal.h> 30#include <signal.h>
8 31
47 void *sptr; 70 void *sptr;
48 long ssize; /* positive == mmap, otherwise malloc */ 71 long ssize; /* positive == mmap, otherwise malloc */
49} coro_stack; 72} coro_stack;
50 73
51struct coro { 74struct coro {
75 /* the top-level JMPENV for each coroutine, needed to catch dies. */
76 JMPENV start_env;
77
52 /* the optional C context */ 78 /* the optional C context */
53 coro_stack *stack; 79 coro_stack *stack;
54 void *cursp; 80 void *cursp;
55 int gencnt; 81 int gencnt;
56 82
57 /* optionally saved, might be zero */ 83 /* optionally saved, might be zero */
58 AV *defav; 84 AV *defav;
59 SV *defsv; 85 SV *defsv;
60 SV *errsv; 86 SV *errsv;
61 /* TODO: PL_pmop might be nice && effortless to save. */
62 87
63 /* saved global state not related to stacks */ 88 /* saved global state not related to stacks */
64 U8 dowarn; 89 U8 dowarn;
65 I32 in_eval; 90 I32 in_eval;
66 91
69 AV *curstack; 94 AV *curstack;
70 AV *mainstack; 95 AV *mainstack;
71 SV **stack_sp; 96 SV **stack_sp;
72 OP *op; 97 OP *op;
73 SV **curpad; 98 SV **curpad;
99 AV *comppad;
74 SV **stack_base; 100 SV **stack_base;
75 SV **stack_max; 101 SV **stack_max;
76 SV **tmps_stack; 102 SV **tmps_stack;
77 I32 tmps_floor; 103 I32 tmps_floor;
78 I32 tmps_ix; 104 I32 tmps_ix;
102static AV *main_mainstack; /* used to differentiate between $main and others */ 128static AV *main_mainstack; /* used to differentiate between $main and others */
103static HV *coro_state_stash; 129static HV *coro_state_stash;
104static SV *ucoro_state_sv; 130static SV *ucoro_state_sv;
105static U32 ucoro_state_hash; 131static U32 ucoro_state_hash;
106static HV *padlist_cache; 132static HV *padlist_cache;
133static SV *coro_mortal; /* will be freed after next transfer */
107 134
108/* mostly copied from op.c:cv_clone2 */ 135/* mostly copied from op.c:cv_clone2 */
109STATIC AV * 136STATIC AV *
110clone_padlist (AV *protopadlist) 137clone_padlist (AV *protopadlist)
111{ 138{
276#define SE } while (0) 303#define SE } while (0)
277 304
278#define LOAD(state) load_state(aTHX_ (state)); 305#define LOAD(state) load_state(aTHX_ (state));
279#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 306#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
280 307
281#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
282 309
283static void 310static void
284load_state(pTHX_ Coro__State c) 311load_state(pTHX_ Coro__State c)
285{ 312{
286 PL_dowarn = c->dowarn; 313 PL_dowarn = c->dowarn;
290 PL_curstack = c->curstack; 317 PL_curstack = c->curstack;
291 PL_mainstack = c->mainstack; 318 PL_mainstack = c->mainstack;
292 PL_stack_sp = c->stack_sp; 319 PL_stack_sp = c->stack_sp;
293 PL_op = c->op; 320 PL_op = c->op;
294 PL_curpad = c->curpad; 321 PL_curpad = c->curpad;
322 PL_comppad = c->comppad;
295 PL_stack_base = c->stack_base; 323 PL_stack_base = c->stack_base;
296 PL_stack_max = c->stack_max; 324 PL_stack_max = c->stack_max;
297 PL_tmps_stack = c->tmps_stack; 325 PL_tmps_stack = c->tmps_stack;
298 PL_tmps_floor = c->tmps_floor; 326 PL_tmps_floor = c->tmps_floor;
299 PL_tmps_ix = c->tmps_ix; 327 PL_tmps_ix = c->tmps_ix;
387 PUSHs ((SV *)cv); 415 PUSHs ((SV *)cv);
388 416
389 get_padlist (cv); /* this is a monster */ 417 get_padlist (cv); /* this is a monster */
390 } 418 }
391 } 419 }
420#ifdef CXt_FORMAT
392 else if (CxTYPE(cx) == CXt_FORMAT) 421 else if (CxTYPE(cx) == CXt_FORMAT)
393 { 422 {
394 /* 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? */
395 /* my bold guess is as a simple, plain sub... */ 424 /* my bold guess is as a simple, plain sub... */
396 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");
397 } 426 }
427#endif
398 } 428 }
399 429
400 if (top_si->si_type == PERLSI_MAIN) 430 if (top_si->si_type == PERLSI_MAIN)
401 break; 431 break;
402 432
419 c->curstack = PL_curstack; 449 c->curstack = PL_curstack;
420 c->mainstack = PL_mainstack; 450 c->mainstack = PL_mainstack;
421 c->stack_sp = PL_stack_sp; 451 c->stack_sp = PL_stack_sp;
422 c->op = PL_op; 452 c->op = PL_op;
423 c->curpad = PL_curpad; 453 c->curpad = PL_curpad;
454 c->comppad = PL_comppad;
424 c->stack_base = PL_stack_base; 455 c->stack_base = PL_stack_base;
425 c->stack_max = PL_stack_max; 456 c->stack_max = PL_stack_max;
426 c->tmps_stack = PL_tmps_stack; 457 c->tmps_stack = PL_tmps_stack;
427 c->tmps_floor = PL_tmps_floor; 458 c->tmps_floor = PL_tmps_floor;
428 c->tmps_ix = PL_tmps_ix; 459 c->tmps_ix = PL_tmps_ix;
468 499
469 New(54,PL_markstack,16,I32); 500 New(54,PL_markstack,16,I32);
470 PL_markstack_ptr = PL_markstack; 501 PL_markstack_ptr = PL_markstack;
471 PL_markstack_max = PL_markstack + 16; 502 PL_markstack_max = PL_markstack + 16;
472 503
504#ifdef SET_MARK_OFFSET
473 SET_MARK_OFFSET; 505 SET_MARK_OFFSET;
506#endif
474 507
475 New(54,PL_scopestack,16,I32); 508 New(54,PL_scopestack,16,I32);
476 PL_scopestack_ix = 0; 509 PL_scopestack_ix = 0;
477 PL_scopestack_max = 16; 510 PL_scopestack_max = 16;
478 511
576 { 609 {
577#ifdef HAVE_MMAP 610#ifdef HAVE_MMAP
578 if (stack->ssize > 0 && stack->sptr) 611 if (stack->ssize > 0 && stack->sptr)
579 munmap (stack->sptr, stack->ssize); 612 munmap (stack->sptr, stack->ssize);
580 else 613 else
581#else 614#endif
582 Safefree (stack->sptr); 615 Safefree (stack->sptr);
583#endif 616
584 Safefree (stack); 617 Safefree (stack);
585 } 618 }
586 else if (ctx->gencnt == stack->gencnt) 619 else if (ctx->gencnt == stack->gencnt)
587 --stack->usecnt; 620 --stack->usecnt;
588 } 621 }
600 633
601 coro_init_stacks (aTHX); 634 coro_init_stacks (aTHX);
602 /*PL_curcop = 0;*/ 635 /*PL_curcop = 0;*/
603 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 636 /*PL_in_eval = PL_in_eval;*/ /* inherit */
604 SvREFCNT_dec (GvAV (PL_defgv)); 637 SvREFCNT_dec (GvAV (PL_defgv));
605 GvAV (PL_defgv) = ctx->args; 638 GvAV (PL_defgv) = ctx->args; ctx->args = 0;
606 639
607 SPAGAIN; 640 SPAGAIN;
608 641
609 if (ctx->stack) 642 if (ctx->stack)
610 { 643 {
637 * that doesn't matter, though, since it is only 670 * that doesn't matter, though, since it is only
638 * pp_nextstate and we never return... 671 * pp_nextstate and we never return...
639 * ah yes, and I don't care anyways ;) 672 * ah yes, and I don't care anyways ;)
640 */ 673 */
641 PUTBACK; 674 PUTBACK;
642 PL_op = pp_entersub(); 675 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
643 SPAGAIN; 676 SPAGAIN;
644 677
645 ENTER; /* necessary e.g. for dounwind */ 678 ENTER; /* necessary e.g. for dounwind */
646 } 679 }
647} 680}
653 * this is a _very_ stripped down perl interpreter ;) 686 * this is a _very_ stripped down perl interpreter ;)
654 */ 687 */
655 Coro__State ctx = (Coro__State)arg; 688 Coro__State ctx = (Coro__State)arg;
656 JMPENV coro_start_env; 689 JMPENV coro_start_env;
657 690
658 /* same as JMPENV_BOOTSTRAP */
659 Zero(&coro_start_env, 1, JMPENV);
660 coro_start_env.je_ret = -1;
661 coro_start_env.je_mustcatch = TRUE;
662 PL_top_env = &coro_start_env; 691 PL_top_env = &ctx->start_env;
663 692
664 ctx->cursp = 0; 693 ctx->cursp = 0;
665 PL_op = PL_op->op_next; 694 PL_op = PL_op->op_next;
666 CALLRUNOPS(aTHX); 695 CALLRUNOPS(aTHX);
667 696
725 if (!prev->stack) 754 if (!prev->stack)
726 allocate_stack (prev, 0); 755 allocate_stack (prev, 0);
727 756
728 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 757 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
729 { 758 {
759 PL_top_env = &next->start_env;
760
730 setup_coro (next); 761 setup_coro (next);
731 762
732 prev->stack->refcnt++; 763 prev->stack->refcnt++;
733 prev->stack->usecnt++; 764 prev->stack->usecnt++;
734 next->stack = prev->stack; 765 next->stack = prev->stack;
735 next->gencnt = prev->gencnt; 766 next->gencnt = prev->gencnt;
736 } 767 }
737 else 768 else
738 { 769 {
770 assert (!next->stack);
739 allocate_stack (next, 1); 771 allocate_stack (next, 1);
740 coro_create (&(next->stack->cctx), 772 coro_create (&(next->stack->cctx),
741 setup_coro, (void *)next, 773 setup_coro, (void *)next,
742 next->stack->sptr, labs (next->stack->ssize)); 774 next->stack->sptr, labs (next->stack->ssize));
743 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 775 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
748 setup_coro (next); 780 setup_coro (next);
749 } 781 }
750 782
751 /* 783 /*
752 * xnext is now either prev or next, depending on wether 784 * xnext is now either prev or next, depending on wether
753 * we switched the c stack or not. that's why i use a global 785 * we switched the c stack or not. that's why I use a global
754 * variable, that should become thread-specific at one point. 786 * variable, that should become thread-specific at one point.
755 */ 787 */
756 xnext->cursp = stacklevel; 788 xnext->cursp = stacklevel;
757 } 789 }
758}
759 790
760static struct coro * 791 if (coro_mortal)
761sv_to_coro (SV *arg, const char *funcname, const char *varname)
762{
763 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
764 { 792 {
765 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 793 SvREFCNT_dec (coro_mortal);
766 794 coro_mortal = 0;
767 if (!he)
768 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
769
770 arg = HeVAL(he);
771 } 795 }
772 796}
797
798#define SV_CORO(sv,func) \
799 do { \
800 if (SvROK (sv)) \
801 sv = SvRV (sv); \
802 \
803 if (SvTYPE(sv) == SVt_PVHV) \
804 { \
805 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
806 \
807 if (!he) \
808 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
809 \
810 (sv) = SvRV (HeVAL(he)); \
811 } \
812 \
773 /* must also be changed inside Coro::Cont::yield */ 813 /* must also be changed inside Coro::Cont::yield */ \
774 if (SvROK(arg) && SvOBJECT(SvRV(arg)) 814 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \
775 && SvSTASH(SvRV(arg)) == coro_state_stash)
776 return (struct coro *) SvIV((SV*)SvRV(arg));
777
778 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 815 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
779 /*NORETURN*/ 816 \
780} 817 } while(0)
818
819#define SvSTATE(sv) (struct coro *)SvIV (sv)
781 820
782static void 821static void
783api_transfer(pTHX_ SV *prev, SV *next, int flags) 822api_transfer(pTHX_ SV *prev, SV *next, int flags)
784{ 823{
785 transfer(aTHX_ 824 SV_CORO (prev, "Coro::transfer");
786 sv_to_coro (prev, "Coro::transfer", "prev"), 825 SV_CORO (next, "Coro::transfer");
787 sv_to_coro (next, "Coro::transfer", "next"), 826
788 flags); 827 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
789} 828}
790 829
791/** Coro ********************************************************************/ 830/** Coro ********************************************************************/
792 831
793#define PRIO_MAX 3 832#define PRIO_MAX 3
803static int coro_nready; 842static int coro_nready;
804 843
805static void 844static void
806coro_enq (SV *sv) 845coro_enq (SV *sv)
807{ 846{
808 if (SvROK (sv))
809 {
810 SV *hv = SvRV (sv);
811 if (SvTYPE (hv) == SVt_PVHV) 847 if (SvTYPE (sv) == SVt_PVHV)
812 { 848 {
813 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); 849 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
814 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 850 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
815 851
816 prio = prio > PRIO_MAX ? PRIO_MAX 852 prio = prio > PRIO_MAX ? PRIO_MAX
817 : prio < PRIO_MIN ? PRIO_MIN 853 : prio < PRIO_MIN ? PRIO_MIN
818 : prio; 854 : prio;
819 855
820 av_push (coro_ready [prio - PRIO_MIN], sv); 856 av_push (coro_ready [prio - PRIO_MIN], sv);
821 coro_nready++; 857 coro_nready++;
822 858
823 return; 859 return;
824 }
825 } 860 }
826 861
827 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 862 croak ("Coro::ready tried to enqueue something that is not a coroutine");
828} 863}
829 864
847} 882}
848 883
849static void 884static void
850api_ready (SV *coro) 885api_ready (SV *coro)
851{ 886{
887 if (SvROK (coro))
888 coro = SvRV (coro);
889
852 coro_enq (SvREFCNT_inc (coro)); 890 coro_enq (SvREFCNT_inc (coro));
853} 891}
854 892
855static void 893static void
856api_schedule (int cede) 894api_schedule (void)
857{ 895{
858 SV *prev, *next; 896 SV *prev, *next;
859 897
860 prev = GvSV (coro_current); 898 prev = SvRV (GvSV (coro_current));
861
862 if (cede)
863 coro_enq (SvREFCNT_inc (prev));
864
865 next = coro_deq (PRIO_MIN); 899 next = coro_deq (PRIO_MIN);
866 900
867 if (!next) 901 if (!next)
868 next = SvREFCNT_inc (GvSV (coro_idle)); 902 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
869 903
870 GvSV (coro_current) = SvREFCNT_inc (next); 904 /* free this only after the transfer */
871 transfer (aTHX_ 905 coro_mortal = prev;
872 sv_to_coro (prev, "Coro::schedule", "current coroutine"), 906 SV_CORO (prev, "Coro::schedule");
873 sv_to_coro (next, "Coro::schedule", "next coroutine"), 907
908 SvRV (GvSV (coro_current)) = next;
909
910 SV_CORO (next, "Coro::schedule");
911
912 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
874 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 913 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
875 SvREFCNT_dec (next); 914}
876 SvREFCNT_dec (prev); 915
916static void
917api_cede (void)
918{
919 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
920
921 api_schedule ();
877} 922}
878 923
879MODULE = Coro::State PACKAGE = Coro::State 924MODULE = Coro::State PACKAGE = Coro::State
880 925
881PROTOTYPES: ENABLE 926PROTOTYPES: ENABLE
908 Coro__State coro; 953 Coro__State coro;
909 954
910 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 955 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
911 croak ("Coro::State::_newprocess expects an arrayref"); 956 croak ("Coro::State::_newprocess expects an arrayref");
912 957
913 New (0, coro, 1, struct coro); 958 Newz (0, coro, 1, struct coro);
914 959
915 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 960 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
916 coro->mainstack = 0; /* actual work is done inside transfer */ 961 coro->mainstack = 0; /* actual work is done inside transfer */
917 coro->stack = 0; 962 coro->stack = 0;
918 963
964 /* same as JMPENV_BOOTSTRAP */
965 /* we might be able to recycle start_env, but safe is safe */
966 //Zero(&coro->start_env, 1, JMPENV);
967 coro->start_env.je_ret = -1;
968 coro->start_env.je_mustcatch = TRUE;
969
919 RETVAL = coro; 970 RETVAL = coro;
920 OUTPUT: 971 OUTPUT:
921 RETVAL 972 RETVAL
922 973
923void 974void
924transfer(prev, next, flags) 975transfer(prev, next, flags)
925 Coro::State_or_hashref prev 976 SV *prev
926 Coro::State_or_hashref next 977 SV *next
927 int flags 978 int flags
928 PROTOTYPE: @ 979 PROTOTYPE: @
929 CODE: 980 CODE:
930 PUTBACK; 981 PUTBACK;
982 SV_CORO (next, "Coro::transfer");
983 SV_CORO (prev, "Coro::transfer");
931 transfer (aTHX_ prev, next, flags); 984 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
932 SPAGAIN; 985 SPAGAIN;
933 986
934void 987void
935DESTROY(coro) 988DESTROY(coro)
936 Coro::State coro 989 Coro::State coro
952 1005
953 coro->mainstack = 0; 1006 coro->mainstack = 0;
954 } 1007 }
955 1008
956 deallocate_stack (coro); 1009 deallocate_stack (coro);
957 1010 SvREFCNT_dec (coro->args);
958 Safefree (coro); 1011 Safefree (coro);
959 1012
960void 1013void
961flush() 1014flush()
962 CODE: 1015 CODE:
989 SV *sv; 1042 SV *sv;
990 AV *defav = GvAV (PL_defgv); 1043 AV *defav = GvAV (PL_defgv);
991 struct coro *prev, *next; 1044 struct coro *prev, *next;
992 1045
993 if (!returnstk) 1046 if (!returnstk)
994 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 1047 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE));
995 1048
996 /* set up @_ -- ugly */ 1049 /* set up @_ -- ugly */
997 av_clear (defav); 1050 av_clear (defav);
998 av_fill (defav, items - 1); 1051 av_fill (defav, items - 1);
999 while (items--) 1052 while (items--)
1031 1084
1032 { 1085 {
1033 SV *sv = perl_get_sv("Coro::API", 1); 1086 SV *sv = perl_get_sv("Coro::API", 1);
1034 1087
1035 coroapi.schedule = api_schedule; 1088 coroapi.schedule = api_schedule;
1089 coroapi.cede = api_cede;
1036 coroapi.ready = api_ready; 1090 coroapi.ready = api_ready;
1037 coroapi.nready = &coro_nready; 1091 coroapi.nready = &coro_nready;
1038 coroapi.current = coro_current; 1092 coroapi.current = coro_current;
1039 1093
1040 GCoroAPI = &coroapi; 1094 GCoroAPI = &coroapi;
1059 RETVAL 1113 RETVAL
1060 1114
1061void 1115void
1062schedule(...) 1116schedule(...)
1063 PROTOTYPE: 1117 PROTOTYPE:
1064 ALIAS:
1065 cede = 1
1066 CODE: 1118 CODE:
1067 api_schedule (ix); 1119 api_schedule ();
1068 1120
1121void
1122cede(...)
1123 PROTOTYPE:
1124 CODE:
1125 api_cede ();
1126

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines