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.35 by root, Mon Sep 24 01:36:20 2001 UTC vs.
Revision 1.46 by pcg, Wed Nov 5 20:02:46 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
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
68 AV *curstack; 94 AV *curstack;
69 AV *mainstack; 95 AV *mainstack;
70 SV **stack_sp; 96 SV **stack_sp;
71 OP *op; 97 OP *op;
72 SV **curpad; 98 SV **curpad;
99 AV *comppad;
73 SV **stack_base; 100 SV **stack_base;
74 SV **stack_max; 101 SV **stack_max;
75 SV **tmps_stack; 102 SV **tmps_stack;
76 I32 tmps_floor; 103 I32 tmps_floor;
77 I32 tmps_ix; 104 I32 tmps_ix;
101static AV *main_mainstack; /* used to differentiate between $main and others */ 128static AV *main_mainstack; /* used to differentiate between $main and others */
102static HV *coro_state_stash; 129static HV *coro_state_stash;
103static SV *ucoro_state_sv; 130static SV *ucoro_state_sv;
104static U32 ucoro_state_hash; 131static U32 ucoro_state_hash;
105static HV *padlist_cache; 132static HV *padlist_cache;
133static SV *coro_mortal; /* will be freed after next transfer */
106 134
107/* mostly copied from op.c:cv_clone2 */ 135/* mostly copied from op.c:cv_clone2 */
108STATIC AV * 136STATIC AV *
109clone_padlist (AV *protopadlist) 137clone_padlist (AV *protopadlist)
110{ 138{
289 PL_curstack = c->curstack; 317 PL_curstack = c->curstack;
290 PL_mainstack = c->mainstack; 318 PL_mainstack = c->mainstack;
291 PL_stack_sp = c->stack_sp; 319 PL_stack_sp = c->stack_sp;
292 PL_op = c->op; 320 PL_op = c->op;
293 PL_curpad = c->curpad; 321 PL_curpad = c->curpad;
322 PL_comppad = c->comppad;
294 PL_stack_base = c->stack_base; 323 PL_stack_base = c->stack_base;
295 PL_stack_max = c->stack_max; 324 PL_stack_max = c->stack_max;
296 PL_tmps_stack = c->tmps_stack; 325 PL_tmps_stack = c->tmps_stack;
297 PL_tmps_floor = c->tmps_floor; 326 PL_tmps_floor = c->tmps_floor;
298 PL_tmps_ix = c->tmps_ix; 327 PL_tmps_ix = c->tmps_ix;
386 PUSHs ((SV *)cv); 415 PUSHs ((SV *)cv);
387 416
388 get_padlist (cv); /* this is a monster */ 417 get_padlist (cv); /* this is a monster */
389 } 418 }
390 } 419 }
420#ifdef CXt_FORMAT
391 else if (CxTYPE(cx) == CXt_FORMAT) 421 else if (CxTYPE(cx) == CXt_FORMAT)
392 { 422 {
393 /* 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? */
394 /* my bold guess is as a simple, plain sub... */ 424 /* my bold guess is as a simple, plain sub... */
395 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");
396 } 426 }
427#endif
397 } 428 }
398 429
399 if (top_si->si_type == PERLSI_MAIN) 430 if (top_si->si_type == PERLSI_MAIN)
400 break; 431 break;
401 432
418 c->curstack = PL_curstack; 449 c->curstack = PL_curstack;
419 c->mainstack = PL_mainstack; 450 c->mainstack = PL_mainstack;
420 c->stack_sp = PL_stack_sp; 451 c->stack_sp = PL_stack_sp;
421 c->op = PL_op; 452 c->op = PL_op;
422 c->curpad = PL_curpad; 453 c->curpad = PL_curpad;
454 c->comppad = PL_comppad;
423 c->stack_base = PL_stack_base; 455 c->stack_base = PL_stack_base;
424 c->stack_max = PL_stack_max; 456 c->stack_max = PL_stack_max;
425 c->tmps_stack = PL_tmps_stack; 457 c->tmps_stack = PL_tmps_stack;
426 c->tmps_floor = PL_tmps_floor; 458 c->tmps_floor = PL_tmps_floor;
427 c->tmps_ix = PL_tmps_ix; 459 c->tmps_ix = PL_tmps_ix;
467 499
468 New(54,PL_markstack,16,I32); 500 New(54,PL_markstack,16,I32);
469 PL_markstack_ptr = PL_markstack; 501 PL_markstack_ptr = PL_markstack;
470 PL_markstack_max = PL_markstack + 16; 502 PL_markstack_max = PL_markstack + 16;
471 503
504#ifdef SET_MARK_OFFSET
472 SET_MARK_OFFSET; 505 SET_MARK_OFFSET;
506#endif
473 507
474 New(54,PL_scopestack,16,I32); 508 New(54,PL_scopestack,16,I32);
475 PL_scopestack_ix = 0; 509 PL_scopestack_ix = 0;
476 PL_scopestack_max = 16; 510 PL_scopestack_max = 16;
477 511
543 stack->refcnt = 1; 577 stack->refcnt = 1;
544 stack->usecnt = 1; 578 stack->usecnt = 1;
545 stack->gencnt = ctx->gencnt = 0; 579 stack->gencnt = ctx->gencnt = 0;
546 if (alloc) 580 if (alloc)
547 { 581 {
548#ifdef HAVE_MMAP 582#if HAVE_MMAP
549 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 583 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
550 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 584 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
551 if (stack->sptr == (void *)-1) 585 if (stack->sptr == (void *)-1)
552#endif 586#endif
553 { 587 {
554 /*FIXME*//*D*//* reasonable stack size! */ 588 /*FIXME*//*D*//* reasonable stack size! */
555 stack->ssize = -4096 * sizeof (long); 589 stack->ssize = -4096 * sizeof (long);
636 * that doesn't matter, though, since it is only 670 * that doesn't matter, though, since it is only
637 * pp_nextstate and we never return... 671 * pp_nextstate and we never return...
638 * ah yes, and I don't care anyways ;) 672 * ah yes, and I don't care anyways ;)
639 */ 673 */
640 PUTBACK; 674 PUTBACK;
641 PL_op = pp_entersub(); 675 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
642 SPAGAIN; 676 SPAGAIN;
643 677
644 ENTER; /* necessary e.g. for dounwind */ 678 ENTER; /* necessary e.g. for dounwind */
645 } 679 }
646} 680}
652 * this is a _very_ stripped down perl interpreter ;) 686 * this is a _very_ stripped down perl interpreter ;)
653 */ 687 */
654 Coro__State ctx = (Coro__State)arg; 688 Coro__State ctx = (Coro__State)arg;
655 JMPENV coro_start_env; 689 JMPENV coro_start_env;
656 690
657 /* same as JMPENV_BOOTSTRAP */
658 Zero(&coro_start_env, 1, JMPENV);
659 coro_start_env.je_ret = -1;
660 coro_start_env.je_mustcatch = TRUE;
661 PL_top_env = &coro_start_env; 691 PL_top_env = &ctx->start_env;
662 692
663 ctx->cursp = 0; 693 ctx->cursp = 0;
664 PL_op = PL_op->op_next; 694 PL_op = PL_op->op_next;
665 CALLRUNOPS(aTHX); 695 CALLRUNOPS(aTHX);
666 696
724 if (!prev->stack) 754 if (!prev->stack)
725 allocate_stack (prev, 0); 755 allocate_stack (prev, 0);
726 756
727 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 757 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
728 { 758 {
759 PL_top_env = &next->start_env;
760
729 setup_coro (next); 761 setup_coro (next);
730 762
731 prev->stack->refcnt++; 763 prev->stack->refcnt++;
732 prev->stack->usecnt++; 764 prev->stack->usecnt++;
733 next->stack = prev->stack; 765 next->stack = prev->stack;
747 setup_coro (next); 779 setup_coro (next);
748 } 780 }
749 781
750 /* 782 /*
751 * xnext is now either prev or next, depending on wether 783 * xnext is now either prev or next, depending on wether
752 * we switched the c stack or not. that's why i use a global 784 * we switched the c stack or not. that's why I use a global
753 * variable, that should become thread-specific at one point. 785 * variable, that should become thread-specific at one point.
754 */ 786 */
755 xnext->cursp = stacklevel; 787 xnext->cursp = stacklevel;
756 } 788 }
757}
758 789
759static struct coro * 790 if (coro_mortal)
760sv_to_coro (SV *arg, const char *funcname, const char *varname)
761{
762 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
763 { 791 {
764 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 792 SvREFCNT_dec (coro_mortal);
765 793 coro_mortal = 0;
766 if (!he)
767 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
768
769 arg = HeVAL(he);
770 } 794 }
771 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 { \
804 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
805 \
806 if (!he) \
807 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
808 \
809 (sv) = SvRV (HeVAL(he)); \
810 } \
811 \
772 /* must also be changed inside Coro::Cont::yield */ 812 /* must also be changed inside Coro::Cont::yield */ \
773 if (SvROK(arg) && SvOBJECT(SvRV(arg)) 813 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \
774 && SvSTASH(SvRV(arg)) == coro_state_stash)
775 return (struct coro *) SvIV((SV*)SvRV(arg));
776
777 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); \
778 /*NORETURN*/ 815 \
779} 816 } while(0)
817
818#define SvSTATE(sv) (struct coro *)SvIV (sv)
780 819
781static void 820static void
782api_transfer(pTHX_ SV *prev, SV *next, int flags) 821api_transfer(pTHX_ SV *prev, SV *next, int flags)
783{ 822{
784 transfer(aTHX_ 823 SV_CORO (prev, "Coro::transfer");
785 sv_to_coro (prev, "Coro::transfer", "prev"), 824 SV_CORO (next, "Coro::transfer");
786 sv_to_coro (next, "Coro::transfer", "next"), 825
787 flags); 826 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
788} 827}
789 828
790/** Coro ********************************************************************/ 829/** Coro ********************************************************************/
791 830
792#define PRIO_MAX 3 831#define PRIO_MAX 3
802static int coro_nready; 841static int coro_nready;
803 842
804static void 843static void
805coro_enq (SV *sv) 844coro_enq (SV *sv)
806{ 845{
807 if (SvROK (sv))
808 {
809 SV *hv = SvRV (sv);
810 if (SvTYPE (hv) == SVt_PVHV) 846 if (SvTYPE (sv) == SVt_PVHV)
811 { 847 {
812 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); 848 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
813 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 849 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
814 850
815 prio = prio > PRIO_MAX ? PRIO_MAX 851 prio = prio > PRIO_MAX ? PRIO_MAX
816 : prio < PRIO_MIN ? PRIO_MIN 852 : prio < PRIO_MIN ? PRIO_MIN
817 : prio; 853 : prio;
818 854
819 av_push (coro_ready [prio - PRIO_MIN], sv); 855 av_push (coro_ready [prio - PRIO_MIN], sv);
820 coro_nready++; 856 coro_nready++;
821 857
822 return; 858 return;
823 }
824 } 859 }
825 860
826 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");
827} 862}
828 863
846} 881}
847 882
848static void 883static void
849api_ready (SV *coro) 884api_ready (SV *coro)
850{ 885{
886 if (SvROK (coro))
887 coro = SvRV (coro);
888
851 coro_enq (SvREFCNT_inc (coro)); 889 coro_enq (SvREFCNT_inc (coro));
852} 890}
853 891
854static void 892static void
855api_schedule (int cede) 893api_schedule (void)
856{ 894{
857 SV *prev, *next; 895 SV *prev, *next;
858 896
859 prev = GvSV (coro_current); 897 prev = SvRV (GvSV (coro_current));
860
861 if (cede)
862 coro_enq (SvREFCNT_inc (prev));
863
864 next = coro_deq (PRIO_MIN); 898 next = coro_deq (PRIO_MIN);
865 899
866 if (!next) 900 if (!next)
867 next = SvREFCNT_inc (GvSV (coro_idle)); 901 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
868 902
869 GvSV (coro_current) = SvREFCNT_inc (next); 903 /* free this only after the transfer */
870 transfer (aTHX_ 904 coro_mortal = prev;
871 sv_to_coro (prev, "Coro::schedule", "current coroutine"), 905 SV_CORO (prev, "Coro::schedule");
872 sv_to_coro (next, "Coro::schedule", "next coroutine"), 906
907 SvRV (GvSV (coro_current)) = next;
908
909 SV_CORO (next, "Coro::schedule");
910
911 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
873 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 912 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
874 SvREFCNT_dec (next); 913}
875 SvREFCNT_dec (prev); 914
915static void
916api_cede (void)
917{
918 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
919
920 api_schedule ();
876} 921}
877 922
878MODULE = Coro::State PACKAGE = Coro::State 923MODULE = Coro::State PACKAGE = Coro::State
879 924
880PROTOTYPES: ENABLE 925PROTOTYPES: ENABLE
913 958
914 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 959 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
915 coro->mainstack = 0; /* actual work is done inside transfer */ 960 coro->mainstack = 0; /* actual work is done inside transfer */
916 coro->stack = 0; 961 coro->stack = 0;
917 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
918 RETVAL = coro; 969 RETVAL = coro;
919 OUTPUT: 970 OUTPUT:
920 RETVAL 971 RETVAL
921 972
922void 973void
923transfer(prev, next, flags) 974transfer(prev, next, flags)
924 Coro::State_or_hashref prev 975 SV *prev
925 Coro::State_or_hashref next 976 SV *next
926 int flags 977 int flags
927 PROTOTYPE: @ 978 PROTOTYPE: @
928 CODE: 979 CODE:
929 PUTBACK; 980 PUTBACK;
981 SV_CORO (next, "Coro::transfer");
982 SV_CORO (prev, "Coro::transfer");
930 transfer (aTHX_ prev, next, flags); 983 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
931 SPAGAIN; 984 SPAGAIN;
932 985
933void 986void
934DESTROY(coro) 987DESTROY(coro)
935 Coro::State coro 988 Coro::State coro
988 SV *sv; 1041 SV *sv;
989 AV *defav = GvAV (PL_defgv); 1042 AV *defav = GvAV (PL_defgv);
990 struct coro *prev, *next; 1043 struct coro *prev, *next;
991 1044
992 if (!returnstk) 1045 if (!returnstk)
993 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 1046 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE));
994 1047
995 /* set up @_ -- ugly */ 1048 /* set up @_ -- ugly */
996 av_clear (defav); 1049 av_clear (defav);
997 av_fill (defav, items - 1); 1050 av_fill (defav, items - 1);
998 while (items--) 1051 while (items--)
1030 1083
1031 { 1084 {
1032 SV *sv = perl_get_sv("Coro::API", 1); 1085 SV *sv = perl_get_sv("Coro::API", 1);
1033 1086
1034 coroapi.schedule = api_schedule; 1087 coroapi.schedule = api_schedule;
1088 coroapi.cede = api_cede;
1035 coroapi.ready = api_ready; 1089 coroapi.ready = api_ready;
1036 coroapi.nready = &coro_nready; 1090 coroapi.nready = &coro_nready;
1037 coroapi.current = coro_current; 1091 coroapi.current = coro_current;
1038 1092
1039 GCoroAPI = &coroapi; 1093 GCoroAPI = &coroapi;
1058 RETVAL 1112 RETVAL
1059 1113
1060void 1114void
1061schedule(...) 1115schedule(...)
1062 PROTOTYPE: 1116 PROTOTYPE:
1063 ALIAS:
1064 cede = 1
1065 CODE: 1117 CODE:
1066 api_schedule (ix); 1118 api_schedule ();
1067 1119
1120void
1121cede(...)
1122 PROTOTYPE:
1123 CODE:
1124 api_cede ();
1125

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines