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.42 by root, Mon Dec 31 20:41:45 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
99static AV *main_mainstack; /* used to differentiate between $main and others */ 101static AV *main_mainstack; /* used to differentiate between $main and others */
100static HV *coro_state_stash; 102static HV *coro_state_stash;
101static SV *ucoro_state_sv; 103static SV *ucoro_state_sv;
102static U32 ucoro_state_hash; 104static U32 ucoro_state_hash;
103static HV *padlist_cache; 105static HV *padlist_cache;
106static SV *coro_mortal; /* will be freed after next transfer */
104 107
105/* mostly copied from op.c:cv_clone2 */ 108/* mostly copied from op.c:cv_clone2 */
106STATIC AV * 109STATIC AV *
107clone_padlist (AV *protopadlist) 110clone_padlist (AV *protopadlist)
108{ 111{
355 358
356 PUSHs (Nullsv); 359 PUSHs (Nullsv);
357 /* this loop was inspired by pp_caller */ 360 /* this loop was inspired by pp_caller */
358 for (;;) 361 for (;;)
359 { 362 {
360 do 363 while (cxix >= 0)
361 { 364 {
362 PERL_CONTEXT *cx = &ccstk[cxix--]; 365 PERL_CONTEXT *cx = &ccstk[cxix--];
363 366
364 if (CxTYPE(cx) == CXt_SUB) 367 if (CxTYPE(cx) == CXt_SUB)
365 { 368 {
391 /* I never used formats, so how should I know how these are implemented? */ 394 /* I never used formats, so how should I know how these are implemented? */
392 /* my bold guess is as a simple, plain sub... */ 395 /* my bold guess is as a simple, plain sub... */
393 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 396 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
394 } 397 }
395 } 398 }
396 while (cxix >= 0);
397 399
398 if (top_si->si_type == PERLSI_MAIN) 400 if (top_si->si_type == PERLSI_MAIN)
399 break; 401 break;
400 402
401 top_si = top_si->si_prev; 403 top_si = top_si->si_prev;
488 * still there is a memleak of 128 bytes... 490 * still there is a memleak of 128 bytes...
489 */ 491 */
490STATIC void 492STATIC void
491destroy_stacks(pTHX) 493destroy_stacks(pTHX)
492{ 494{
495 if (!IN_DESTRUCT)
496 {
493 /* is this ugly, I ask? */ 497 /* is this ugly, I ask? */
494 while (PL_scopestack_ix) 498 while (PL_scopestack_ix)
495 LEAVE; 499 LEAVE;
496 500
497 /* sure it is, but more important: is it correct?? :/ */ 501 /* sure it is, but more important: is it correct?? :/ */
498 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ 502 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
499 FREETMPS; 503 FREETMPS;
504 }
500 505
501 while (PL_curstackinfo->si_next) 506 while (PL_curstackinfo->si_next)
502 PL_curstackinfo = PL_curstackinfo->si_next; 507 PL_curstackinfo = PL_curstackinfo->si_next;
503 508
504 while (PL_curstackinfo) 509 while (PL_curstackinfo)
509 dSP; 514 dSP;
510 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 515 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
511 PUTBACK; /* possibly superfluous */ 516 PUTBACK; /* possibly superfluous */
512 } 517 }
513 518
514 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ 519 if (!IN_DESTRUCT)
520 {
515 dounwind(-1); 521 dounwind(-1);
516
517 SvREFCNT_dec(PL_curstackinfo->si_stack); 522 SvREFCNT_dec(PL_curstackinfo->si_stack);
523 }
524
518 Safefree(PL_curstackinfo->si_cxstack); 525 Safefree(PL_curstackinfo->si_cxstack);
519 Safefree(PL_curstackinfo); 526 Safefree(PL_curstackinfo);
520 PL_curstackinfo = p; 527 PL_curstackinfo = p;
521 } 528 }
522 529
537 stack->refcnt = 1; 544 stack->refcnt = 1;
538 stack->usecnt = 1; 545 stack->usecnt = 1;
539 stack->gencnt = ctx->gencnt = 0; 546 stack->gencnt = ctx->gencnt = 0;
540 if (alloc) 547 if (alloc)
541 { 548 {
542#ifdef HAVE_MMAP 549#if HAVE_MMAP
543 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 550 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); 551 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
545 if (stack->sptr == (void *)-1) 552 if (stack->sptr == (void *)-1)
546#endif 553#endif
547 { 554 {
548 /*FIXME*//*D*//* reasonable stack size! */ 555 /*FIXME*//*D*//* reasonable stack size! */
549 stack->ssize = -4096 * sizeof (long); 556 stack->ssize = -4096 * sizeof (long);
563 570
564 ctx->stack = 0; 571 ctx->stack = 0;
565 572
566 if (stack) 573 if (stack)
567 { 574 {
568 printf ("deallocating stack %p %d\n", stack, stack->refcnt);/*D*/
569 if (!--stack->refcnt) 575 if (!--stack->refcnt)
570 { 576 {
571#ifdef HAVE_MMAP 577#ifdef HAVE_MMAP
572 if (stack->ssize > 0 && stack->sptr) 578 if (stack->ssize > 0 && stack->sptr)
573 munmap (stack->sptr, stack->ssize); 579 munmap (stack->sptr, stack->ssize);
631 * that doesn't matter, though, since it is only 637 * that doesn't matter, though, since it is only
632 * pp_nextstate and we never return... 638 * pp_nextstate and we never return...
633 * ah yes, and I don't care anyways ;) 639 * ah yes, and I don't care anyways ;)
634 */ 640 */
635 PUTBACK; 641 PUTBACK;
636 PL_op = pp_entersub(); 642 PL_op = PL_ppaddr[OP_ENTERSUB]();
637 SPAGAIN; 643 SPAGAIN;
638 644
639 ENTER; /* necessary e.g. for dounwind */ 645 ENTER; /* necessary e.g. for dounwind */
640 } 646 }
641} 647}
645{ 651{
646 /* 652 /*
647 * this is a _very_ stripped down perl interpreter ;) 653 * this is a _very_ stripped down perl interpreter ;)
648 */ 654 */
649 Coro__State ctx = (Coro__State)arg; 655 Coro__State ctx = (Coro__State)arg;
656 JMPENV coro_start_env;
650 657
651 /*FIXME*//* must set up top_env here */ 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;
663
652 ctx->cursp = 0; 664 ctx->cursp = 0;
653 PL_op = PL_op->op_next; 665 PL_op = PL_op->op_next;
654 CALLRUNOPS(aTHX); 666 CALLRUNOPS(aTHX);
655 667
656 abort (); 668 abort ();
660transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 672transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
661{ 673{
662 dSTACKLEVEL; 674 dSTACKLEVEL;
663 static struct coro *xnext; 675 static struct coro *xnext;
664 676
665 printf ("%p => %p\n", prev, next);/*D*/
666 if (prev != next) 677 if (prev != next)
667 { 678 {
668 xnext = next; 679 xnext = next;
669 680
670 if (next->mainstack) 681 if (next->mainstack)
734 } 745 }
735 } 746 }
736 else 747 else
737 setup_coro (next); 748 setup_coro (next);
738 } 749 }
739 }
740 750
751 /*
752 * 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
754 * variable, that should become thread-specific at one point.
755 */
741 xnext->cursp = stacklevel; 756 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 { 757 }
758
759 if (coro_mortal)
760 {
761 SvREFCNT_dec (coro_mortal);
762 coro_mortal = 0;
763 }
764}
765
766#define SV_CORO(sv,func) \
767 do { \
768 if (SvROK (sv)) \
769 sv = SvRV (sv); \
770 \
771 if (SvTYPE(sv) == SVt_PVHV) \
772 { \
749 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 773 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
750 774 \
751 if (!he) 775 if (!he) \
752 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); 776 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
753 777 \
754 arg = HeVAL(he); 778 (sv) = SvRV (HeVAL(he)); \
755 } 779 } \
756 780 \
757 /* must also be changed inside Coro::Cont::yield */ 781 /* must also be changed inside Coro::Cont::yield */ \
758 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 782 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); 783 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
762 /*NORETURN*/ 784 \
763} 785 } while(0)
786
787#define SvSTATE(sv) (struct coro *)SvIV (sv)
764 788
765static void 789static void
766api_transfer(pTHX_ SV *prev, SV *next, int flags) 790api_transfer(pTHX_ SV *prev, SV *next, int flags)
767{ 791{
768 transfer(aTHX_ 792 SV_CORO (prev, "Coro::transfer");
769 sv_to_coro (prev, "Coro::transfer", "prev"), 793 SV_CORO (next, "Coro::transfer");
770 sv_to_coro (next, "Coro::transfer", "next"), 794
771 flags); 795 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
772} 796}
773 797
774/** Coro ********************************************************************/ 798/** Coro ********************************************************************/
775 799
776#define PRIO_MAX 3 800#define PRIO_MAX 3
786static int coro_nready; 810static int coro_nready;
787 811
788static void 812static void
789coro_enq (SV *sv) 813coro_enq (SV *sv)
790{ 814{
791 if (SvROK (sv))
792 {
793 SV *hv = SvRV (sv);
794 if (SvTYPE (hv) == SVt_PVHV) 815 if (SvTYPE (sv) == SVt_PVHV)
795 { 816 {
796 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); 817 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
797 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 818 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
798 819
799 prio = prio > PRIO_MAX ? PRIO_MAX 820 prio = prio > PRIO_MAX ? PRIO_MAX
800 : prio < PRIO_MIN ? PRIO_MIN 821 : prio < PRIO_MIN ? PRIO_MIN
801 : prio; 822 : prio;
802 823
803 av_push (coro_ready [prio - PRIO_MIN], sv); 824 av_push (coro_ready [prio - PRIO_MIN], sv);
804 coro_nready++; 825 coro_nready++;
805 826
806 return; 827 return;
807 }
808 } 828 }
809 829
810 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 830 croak ("Coro::ready tried to enqueue something that is not a coroutine");
811} 831}
812 832
830} 850}
831 851
832static void 852static void
833api_ready (SV *coro) 853api_ready (SV *coro)
834{ 854{
855 if (SvROK (coro))
856 coro = SvRV (coro);
857
835 coro_enq (SvREFCNT_inc (coro)); 858 coro_enq (SvREFCNT_inc (coro));
836} 859}
837 860
838static void 861static void
839api_schedule (int cede) 862api_schedule (void)
840{ 863{
841 SV *prev, *next; 864 SV *prev, *next;
842 865
843 prev = GvSV (coro_current); 866 prev = SvRV (GvSV (coro_current));
844
845 if (cede)
846 coro_enq (SvREFCNT_inc (prev));
847
848 next = coro_deq (PRIO_MIN); 867 next = coro_deq (PRIO_MIN);
849 868
850 if (!next) 869 if (!next)
851 next = SvREFCNT_inc (GvSV (coro_idle)); 870 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
852 871
853 GvSV (coro_current) = SvREFCNT_inc (next); 872 /* free this only after the transfer */
854 transfer (aTHX_ 873 coro_mortal = prev;
855 sv_to_coro (prev, "Coro::schedule", "current coroutine"), 874 SV_CORO (prev, "Coro::schedule");
856 sv_to_coro (next, "Coro::schedule", "next coroutine"), 875
876 SvRV (GvSV (coro_current)) = next;
877
878 SV_CORO (next, "Coro::schedule");
879
880 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
857 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 881 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
858 SvREFCNT_dec (next); 882}
859 SvREFCNT_dec (prev); 883
884static void
885api_cede (void)
886{
887 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
888
889 api_schedule ();
860} 890}
861 891
862MODULE = Coro::State PACKAGE = Coro::State 892MODULE = Coro::State PACKAGE = Coro::State
863 893
864PROTOTYPES: ENABLE 894PROTOTYPES: ENABLE
903 OUTPUT: 933 OUTPUT:
904 RETVAL 934 RETVAL
905 935
906void 936void
907transfer(prev, next, flags) 937transfer(prev, next, flags)
908 Coro::State_or_hashref prev 938 SV *prev
909 Coro::State_or_hashref next 939 SV *next
910 int flags 940 int flags
911 PROTOTYPE: @ 941 PROTOTYPE: @
912 CODE: 942 CODE:
913 PUTBACK; 943 PUTBACK;
944 SV_CORO (next, "Coro::transfer");
945 SV_CORO (prev, "Coro::transfer");
914 transfer (aTHX_ prev, next, flags); 946 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
915 SPAGAIN; 947 SPAGAIN;
916 948
917void 949void
918DESTROY(coro) 950DESTROY(coro)
919 Coro::State coro 951 Coro::State coro
1014 1046
1015 { 1047 {
1016 SV *sv = perl_get_sv("Coro::API", 1); 1048 SV *sv = perl_get_sv("Coro::API", 1);
1017 1049
1018 coroapi.schedule = api_schedule; 1050 coroapi.schedule = api_schedule;
1051 coroapi.cede = api_cede;
1019 coroapi.ready = api_ready; 1052 coroapi.ready = api_ready;
1020 coroapi.nready = &coro_nready; 1053 coroapi.nready = &coro_nready;
1021 coroapi.current = coro_current; 1054 coroapi.current = coro_current;
1022 1055
1023 GCoroAPI = &coroapi; 1056 GCoroAPI = &coroapi;
1027} 1060}
1028 1061
1029void 1062void
1030ready(self) 1063ready(self)
1031 SV * self 1064 SV * self
1065 PROTOTYPE: $
1032 CODE: 1066 CODE:
1033 api_ready (self); 1067 api_ready (self);
1034 1068
1035int 1069int
1036nready(...) 1070nready(...)
1041 RETVAL 1075 RETVAL
1042 1076
1043void 1077void
1044schedule(...) 1078schedule(...)
1045 PROTOTYPE: 1079 PROTOTYPE:
1046 ALIAS:
1047 cede = 1
1048 CODE: 1080 CODE:
1049 api_schedule (ix); 1081 api_schedule ();
1050 1082
1083void
1084cede(...)
1085 PROTOTYPE:
1086 CODE:
1087 api_cede ();
1088

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines