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.28 by root, Sat Aug 25 21:32:05 2001 UTC vs.
Revision 1.40 by root, Tue Nov 27 01:41:41 2001 UTC

7#include <signal.h> 7#include <signal.h>
8 8
9#ifdef HAVE_MMAP 9#ifdef HAVE_MMAP
10# include <unistd.h> 10# include <unistd.h>
11# include <sys/mman.h> 11# include <sys/mman.h>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS 12# ifndef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS 13# ifdef MAP_ANON
14# define MAP_ANONYMOUS MAP_ANON
15# else 15# else
16# undef HAVE_MMAP 16# undef HAVE_MMAP
17# endif 17# endif
18# endif 18# endif
19#endif 19#endif
25 25
26/* The next macro should declare a variable stacklevel that contains and approximation 26/* The next macro should declare a variable stacklevel that contains and approximation
27 * to the current C stack pointer. Its property is that it changes with each call 27 * to the current C stack pointer. Its property is that it changes with each call
28 * and should be unique. */ 28 * and should be unique. */
29#define dSTACKLEVEL void *stacklevel = &stacklevel 29#define dSTACKLEVEL void *stacklevel = &stacklevel
30
31#define IN_DESTRUCT (PL_main_cv == Nullcv)
30 32
31#define labs(l) ((l) >= 0 ? (l) : -(l)) 33#define labs(l) ((l) >= 0 ? (l) : -(l))
32 34
33#include "CoroAPI.h" 35#include "CoroAPI.h"
34 36
85 I32 savestack_max; 87 I32 savestack_max;
86 OP **retstack; 88 OP **retstack;
87 I32 retstack_ix; 89 I32 retstack_ix;
88 I32 retstack_max; 90 I32 retstack_max;
89 COP *curcop; 91 COP *curcop;
92 PMOP *curpm;
90 JMPENV *top_env; 93 JMPENV *top_env;
91 94
92 /* data associated with this coroutine (initial args) */ 95 /* data associated with this coroutine (initial args) */
93 AV *args; 96 AV *args;
94}; 97};
99static AV *main_mainstack; /* used to differentiate between $main and others */ 102static AV *main_mainstack; /* used to differentiate between $main and others */
100static HV *coro_state_stash; 103static HV *coro_state_stash;
101static SV *ucoro_state_sv; 104static SV *ucoro_state_sv;
102static U32 ucoro_state_hash; 105static U32 ucoro_state_hash;
103static HV *padlist_cache; 106static HV *padlist_cache;
107static SV *coro_mortal; /* will be freed after next transfer */
104 108
105/* mostly copied from op.c:cv_clone2 */ 109/* mostly copied from op.c:cv_clone2 */
106STATIC AV * 110STATIC AV *
107clone_padlist (AV *protopadlist) 111clone_padlist (AV *protopadlist)
108{ 112{
355 359
356 PUSHs (Nullsv); 360 PUSHs (Nullsv);
357 /* this loop was inspired by pp_caller */ 361 /* this loop was inspired by pp_caller */
358 for (;;) 362 for (;;)
359 { 363 {
360 do 364 while (cxix >= 0)
361 { 365 {
362 PERL_CONTEXT *cx = &ccstk[cxix--]; 366 PERL_CONTEXT *cx = &ccstk[cxix--];
363 367
364 if (CxTYPE(cx) == CXt_SUB) 368 if (CxTYPE(cx) == CXt_SUB)
365 { 369 {
391 /* I never used formats, so how should I know how these are implemented? */ 395 /* I never used formats, so how should I know how these are implemented? */
392 /* my bold guess is as a simple, plain sub... */ 396 /* my bold guess is as a simple, plain sub... */
393 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 397 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
394 } 398 }
395 } 399 }
396 while (cxix >= 0);
397 400
398 if (top_si->si_type == PERLSI_MAIN) 401 if (top_si->si_type == PERLSI_MAIN)
399 break; 402 break;
400 403
401 top_si = top_si->si_prev; 404 top_si = top_si->si_prev;
488 * still there is a memleak of 128 bytes... 491 * still there is a memleak of 128 bytes...
489 */ 492 */
490STATIC void 493STATIC void
491destroy_stacks(pTHX) 494destroy_stacks(pTHX)
492{ 495{
496 if (!IN_DESTRUCT)
497 {
493 /* is this ugly, I ask? */ 498 /* is this ugly, I ask? */
494 while (PL_scopestack_ix) 499 while (PL_scopestack_ix)
495 LEAVE; 500 LEAVE;
496 501
497 /* sure it is, but more important: is it correct?? :/ */ 502 /* sure it is, but more important: is it correct?? :/ */
498 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ 503 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
499 FREETMPS; 504 FREETMPS;
505 }
500 506
501 while (PL_curstackinfo->si_next) 507 while (PL_curstackinfo->si_next)
502 PL_curstackinfo = PL_curstackinfo->si_next; 508 PL_curstackinfo = PL_curstackinfo->si_next;
503 509
504 while (PL_curstackinfo) 510 while (PL_curstackinfo)
509 dSP; 515 dSP;
510 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 516 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
511 PUTBACK; /* possibly superfluous */ 517 PUTBACK; /* possibly superfluous */
512 } 518 }
513 519
514 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ 520 if (!IN_DESTRUCT)
521 {
515 dounwind(-1); 522 dounwind(-1);
516
517 SvREFCNT_dec(PL_curstackinfo->si_stack); 523 SvREFCNT_dec(PL_curstackinfo->si_stack);
524 }
525
518 Safefree(PL_curstackinfo->si_cxstack); 526 Safefree(PL_curstackinfo->si_cxstack);
519 Safefree(PL_curstackinfo); 527 Safefree(PL_curstackinfo);
520 PL_curstackinfo = p; 528 PL_curstackinfo = p;
521 } 529 }
522 530
537 stack->refcnt = 1; 545 stack->refcnt = 1;
538 stack->usecnt = 1; 546 stack->usecnt = 1;
539 stack->gencnt = ctx->gencnt = 0; 547 stack->gencnt = ctx->gencnt = 0;
540 if (alloc) 548 if (alloc)
541 { 549 {
542#ifdef HAVE_MMAP 550#if HAVE_MMAP
543 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 551 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
544 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 552 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
545 if (stack->sptr == (void *)-1) 553 if (stack->sptr == (void *)-1)
546#endif 554#endif
547 { 555 {
548 /*FIXME*//*D*//* reasonable stack size! */ 556 /*FIXME*//*D*//* reasonable stack size! */
549 stack->ssize = -4096 * sizeof (long); 557 stack->ssize = -4096 * sizeof (long);
563 571
564 ctx->stack = 0; 572 ctx->stack = 0;
565 573
566 if (stack) 574 if (stack)
567 { 575 {
568 printf ("deallocating stack %p %d\n", stack, stack->refcnt);/*D*/
569 if (!--stack->refcnt) 576 if (!--stack->refcnt)
570 { 577 {
571#ifdef HAVE_MMAP 578#ifdef HAVE_MMAP
572 if (stack->ssize > 0 && stack->sptr) 579 if (stack->ssize > 0 && stack->sptr)
573 munmap (stack->sptr, stack->ssize); 580 munmap (stack->sptr, stack->ssize);
591 dSP; 598 dSP;
592 Coro__State ctx = (Coro__State)arg; 599 Coro__State ctx = (Coro__State)arg;
593 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 600 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
594 601
595 coro_init_stacks (aTHX); 602 coro_init_stacks (aTHX);
603 PL_curpm = 0; /* segfault on first access */
596 /*PL_curcop = 0;*/ 604 /*PL_curcop = 0;*/
597 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 605 /*PL_in_eval = PL_in_eval;*/ /* inherit */
598 SvREFCNT_dec (GvAV (PL_defgv)); 606 SvREFCNT_dec (GvAV (PL_defgv));
599 GvAV (PL_defgv) = ctx->args; 607 GvAV (PL_defgv) = ctx->args;
600 608
645{ 653{
646 /* 654 /*
647 * this is a _very_ stripped down perl interpreter ;) 655 * this is a _very_ stripped down perl interpreter ;)
648 */ 656 */
649 Coro__State ctx = (Coro__State)arg; 657 Coro__State ctx = (Coro__State)arg;
658 JMPENV coro_start_env;
650 659
651 /*FIXME*//* must set up top_env here */ 660 /* same as JMPENV_BOOTSTRAP */
661 Zero(&coro_start_env, 1, JMPENV);
662 coro_start_env.je_ret = -1;
663 coro_start_env.je_mustcatch = TRUE;
664 PL_top_env = &coro_start_env;
665
652 ctx->cursp = 0; 666 ctx->cursp = 0;
653 PL_op = PL_op->op_next; 667 PL_op = PL_op->op_next;
654 CALLRUNOPS(aTHX); 668 CALLRUNOPS(aTHX);
655 669
656 abort (); 670 abort ();
660transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 674transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
661{ 675{
662 dSTACKLEVEL; 676 dSTACKLEVEL;
663 static struct coro *xnext; 677 static struct coro *xnext;
664 678
665 printf ("%p => %p\n", prev, next);/*D*/
666 if (prev != next) 679 if (prev != next)
667 { 680 {
668 xnext = next; 681 xnext = next;
669 682
670 if (next->mainstack) 683 if (next->mainstack)
734 } 747 }
735 } 748 }
736 else 749 else
737 setup_coro (next); 750 setup_coro (next);
738 } 751 }
739 }
740 752
753 /*
754 * xnext is now either prev or next, depending on wether
755 * we switched the c stack or not. that's why i use a global
756 * variable, that should become thread-specific at one point.
757 */
741 xnext->cursp = stacklevel; 758 xnext->cursp = stacklevel;
742}
743
744static struct coro *
745sv_to_coro (SV *arg, const char *funcname, const char *varname)
746{
747 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
748 { 759 }
760
761 if (coro_mortal)
762 {
763 SvREFCNT_dec (coro_mortal);
764 coro_mortal = 0;
765 }
766}
767
768#define SV_CORO(sv,func) \
769 do { \
770 if (SvROK (sv)) \
771 sv = SvRV (sv); \
772 \
773 if (SvTYPE(sv) == SVt_PVHV) \
774 { \
749 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 775 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
750 776 \
751 if (!he) 777 if (!he) \
752 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); 778 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
753 779 \
754 arg = HeVAL(he); 780 (sv) = SvRV (HeVAL(he)); \
755 } 781 } \
756 782 \
757 /* must also be changed inside Coro::Cont::yield */ 783 /* must also be changed inside Coro::Cont::yield */ \
758 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 784 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \
759 return (struct coro *) SvIV((SV*)SvRV(arg));
760
761 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 785 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
762 /*NORETURN*/ 786 \
763} 787 } while(0)
788
789#define SvSTATE(sv) (struct coro *)SvIV (sv)
764 790
765static void 791static void
766api_transfer(pTHX_ SV *prev, SV *next, int flags) 792api_transfer(pTHX_ SV *prev, SV *next, int flags)
767{ 793{
768 transfer(aTHX_ 794 SV_CORO (prev, "Coro::transfer");
769 sv_to_coro (prev, "Coro::transfer", "prev"), 795 SV_CORO (next, "Coro::transfer");
770 sv_to_coro (next, "Coro::transfer", "next"), 796
771 flags); 797 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
772} 798}
773 799
774/** Coro ********************************************************************/ 800/** Coro ********************************************************************/
775 801
776#define PRIO_MAX 3 802#define PRIO_MAX 3
786static int coro_nready; 812static int coro_nready;
787 813
788static void 814static void
789coro_enq (SV *sv) 815coro_enq (SV *sv)
790{ 816{
791 if (SvROK (sv))
792 {
793 SV *hv = SvRV (sv);
794 if (SvTYPE (hv) == SVt_PVHV) 817 if (SvTYPE (sv) == SVt_PVHV)
795 { 818 {
796 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); 819 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
797 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 820 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
798 821
799 prio = prio > PRIO_MAX ? PRIO_MAX 822 prio = prio > PRIO_MAX ? PRIO_MAX
800 : prio < PRIO_MIN ? PRIO_MIN 823 : prio < PRIO_MIN ? PRIO_MIN
801 : prio; 824 : prio;
802 825
803 av_push (coro_ready [prio - PRIO_MIN], sv); 826 av_push (coro_ready [prio - PRIO_MIN], sv);
804 coro_nready++; 827 coro_nready++;
805 828
806 return; 829 return;
807 }
808 } 830 }
809 831
810 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 832 croak ("Coro::ready tried to enqueue something that is not a coroutine");
811} 833}
812 834
830} 852}
831 853
832static void 854static void
833api_ready (SV *coro) 855api_ready (SV *coro)
834{ 856{
857 if (SvROK (coro))
858 coro = SvRV (coro);
859
835 coro_enq (SvREFCNT_inc (coro)); 860 coro_enq (SvREFCNT_inc (coro));
836} 861}
837 862
838static void 863static void
839api_schedule (int cede) 864api_schedule (void)
840{ 865{
841 SV *prev, *next; 866 SV *prev, *next;
842 867
843 prev = GvSV (coro_current); 868 prev = SvRV (GvSV (coro_current));
844
845 if (cede)
846 coro_enq (SvREFCNT_inc (prev));
847
848 next = coro_deq (PRIO_MIN); 869 next = coro_deq (PRIO_MIN);
849 870
850 if (!next) 871 if (!next)
851 next = SvREFCNT_inc (GvSV (coro_idle)); 872 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
852 873
853 GvSV (coro_current) = SvREFCNT_inc (next); 874 /* free this only after the transfer */
854 transfer (aTHX_ 875 coro_mortal = prev;
855 sv_to_coro (prev, "Coro::schedule", "current coroutine"), 876 SV_CORO (prev, "Coro::schedule");
856 sv_to_coro (next, "Coro::schedule", "next coroutine"), 877
878 SvRV (GvSV (coro_current)) = next;
879
880 SV_CORO (next, "Coro::schedule");
881
882 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
857 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 883 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
858 SvREFCNT_dec (next); 884}
859 SvREFCNT_dec (prev); 885
886static void
887api_cede (void)
888{
889 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
890
891 api_schedule ();
860} 892}
861 893
862MODULE = Coro::State PACKAGE = Coro::State 894MODULE = Coro::State PACKAGE = Coro::State
863 895
864PROTOTYPES: ENABLE 896PROTOTYPES: ENABLE
870 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 902 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
871 903
872 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 904 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
873 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 905 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
874 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 906 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
907 newCONSTSUB (coro_state_stash, "SAVE_CURPM", newSViv (TRANSFER_SAVE_CURPM));
875 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 908 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
876 909
877 if (!padlist_cache) 910 if (!padlist_cache)
878 padlist_cache = newHV (); 911 padlist_cache = newHV ();
879 912
903 OUTPUT: 936 OUTPUT:
904 RETVAL 937 RETVAL
905 938
906void 939void
907transfer(prev, next, flags) 940transfer(prev, next, flags)
908 Coro::State_or_hashref prev 941 SV *prev
909 Coro::State_or_hashref next 942 SV *next
910 int flags 943 int flags
911 PROTOTYPE: @ 944 PROTOTYPE: @
912 CODE: 945 CODE:
913 PUTBACK; 946 PUTBACK;
947 SV_CORO (next, "Coro::transfer");
948 SV_CORO (prev, "Coro::transfer");
914 transfer (aTHX_ prev, next, flags); 949 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
915 SPAGAIN; 950 SPAGAIN;
916 951
917void 952void
918DESTROY(coro) 953DESTROY(coro)
919 Coro::State coro 954 Coro::State coro
1014 1049
1015 { 1050 {
1016 SV *sv = perl_get_sv("Coro::API", 1); 1051 SV *sv = perl_get_sv("Coro::API", 1);
1017 1052
1018 coroapi.schedule = api_schedule; 1053 coroapi.schedule = api_schedule;
1054 coroapi.cede = api_cede;
1019 coroapi.ready = api_ready; 1055 coroapi.ready = api_ready;
1020 coroapi.nready = &coro_nready; 1056 coroapi.nready = &coro_nready;
1021 coroapi.current = coro_current; 1057 coroapi.current = coro_current;
1022 1058
1023 GCoroAPI = &coroapi; 1059 GCoroAPI = &coroapi;
1027} 1063}
1028 1064
1029void 1065void
1030ready(self) 1066ready(self)
1031 SV * self 1067 SV * self
1068 PROTOTYPE: $
1032 CODE: 1069 CODE:
1033 api_ready (self); 1070 api_ready (self);
1034 1071
1035int 1072int
1036nready(...) 1073nready(...)
1041 RETVAL 1078 RETVAL
1042 1079
1043void 1080void
1044schedule(...) 1081schedule(...)
1045 PROTOTYPE: 1082 PROTOTYPE:
1046 ALIAS:
1047 cede = 1
1048 CODE: 1083 CODE:
1049 api_schedule (ix); 1084 api_schedule ();
1050 1085
1086void
1087cede(...)
1088 PROTOTYPE:
1089 CODE:
1090 api_cede ();
1091

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines