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.21 by root, Sat Aug 11 19:59:19 2001 UTC vs.
Revision 1.28 by root, Sat Aug 25 21:32:05 2001 UTC

16# undef HAVE_MMAP 16# undef HAVE_MMAP
17# endif 17# endif
18# endif 18# endif
19#endif 19#endif
20 20
21#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize and is rarely used */
22
23/* perl-related */
24#define TRANSFER_SAVE_DEFAV 0x00000001
25#define TRANSFER_SAVE_DEFSV 0x00000002
26#define TRANSFER_SAVE_ERRSV 0x00000004
27/* c-related */
28#define TRANSFER_SAVE_CCTXT 0x00000008
29#ifdef CORO_LAZY_STACK
30# define TRANSFER_LAZY_STACK 0x00000010
31#else
32# define TRANSFER_LAZY_STACK 0x00000000
33#endif
34
35#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
36 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
37 22
38#define SUB_INIT "Coro::State::initialize" 23#define SUB_INIT "Coro::State::initialize"
39#define UCORO_STATE "_coro_state" 24#define UCORO_STATE "_coro_state"
40 25
41/* The next macro should delcare a variable stacklevel that contains and approximation 26/* The next macro should declare a variable stacklevel that contains and approximation
42 * to the current C stack pointer. It's property is that it changes with each call 27 * to the current C stack pointer. Its property is that it changes with each call
43 * and should be unique. */ 28 * and should be unique. */
44#define dSTACKLEVEL void *stacklevel = &stacklevel 29#define dSTACKLEVEL void *stacklevel = &stacklevel
45 30
46#define labs(l) ((l) >= 0 ? (l) : -(l)) 31#define labs(l) ((l) >= 0 ? (l) : -(l))
32
33#include "CoroAPI.h"
34
35static struct CoroAPI coroapi;
47 36
48/* this is actually not only the c stack but also c registers etc... */ 37/* this is actually not only the c stack but also c registers etc... */
49typedef struct { 38typedef struct {
50 int refcnt; /* pointer reference counter */ 39 int refcnt; /* pointer reference counter */
51 int usecnt; /* shared by how many coroutines */ 40 int usecnt; /* shared by how many coroutines */
111static HV *coro_state_stash; 100static HV *coro_state_stash;
112static SV *ucoro_state_sv; 101static SV *ucoro_state_sv;
113static U32 ucoro_state_hash; 102static U32 ucoro_state_hash;
114static HV *padlist_cache; 103static HV *padlist_cache;
115 104
116/* for Coro.pm */
117static GV *coro_current, *coro_idle;
118static AV *coro_ready;
119
120/* mostly copied from op.c:cv_clone2 */ 105/* mostly copied from op.c:cv_clone2 */
121STATIC AV * 106STATIC AV *
122clone_padlist (AV *protopadlist) 107clone_padlist (AV *protopadlist)
123{ 108{
124 AV *av; 109 AV *av;
210 195
211 return newpadlist; 196 return newpadlist;
212} 197}
213 198
214#ifdef MAY_FLUSH 199#ifdef MAY_FLUSH
215STATIC AV * 200STATIC void
216free_padlist (AV *padlist) 201free_padlist (AV *padlist)
217{ 202{
218 /* may be during global destruction */ 203 /* may be during global destruction */
219 if (SvREFCNT(padlist)) 204 if (SvREFCNT(padlist))
220 { 205 {
285#endif 270#endif
286 271
287#define SB do { 272#define SB do {
288#define SE } while (0) 273#define SE } while (0)
289 274
290#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 275#define LOAD(state) load_state(aTHX_ (state));
291#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 276#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
292 277
293#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 278#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE
294 279
295static void 280static void
296load_state(pTHX_ Coro__State c) 281load_state(pTHX_ Coro__State c)
423 408
424 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 409 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
425 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 410 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
426 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 411 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
427 412
428 /* I have not the slightest idea of why av_reify is necessary */
429 /* but if it's missing the defav contents magically get replaced sometimes */
430 if (c->defav)
431 av_reify (c->defav);
432
433 c->dowarn = PL_dowarn; 413 c->dowarn = PL_dowarn;
434 c->in_eval = PL_in_eval; 414 c->in_eval = PL_in_eval;
435 415
436 c->curstackinfo = PL_curstackinfo; 416 c->curstackinfo = PL_curstackinfo;
437 c->curstack = PL_curstack; 417 c->curstack = PL_curstack;
529 dSP; 509 dSP;
530 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 510 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
531 PUTBACK; /* possibly superfluous */ 511 PUTBACK; /* possibly superfluous */
532 } 512 }
533 513
514 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */
534 dounwind(-1); 515 dounwind(-1);
535 516
536 SvREFCNT_dec(PL_curstackinfo->si_stack); 517 SvREFCNT_dec(PL_curstackinfo->si_stack);
537 Safefree(PL_curstackinfo->si_cxstack); 518 Safefree(PL_curstackinfo->si_cxstack);
538 Safefree(PL_curstackinfo); 519 Safefree(PL_curstackinfo);
539 PL_curstackinfo = p; 520 PL_curstackinfo = p;
582 563
583 ctx->stack = 0; 564 ctx->stack = 0;
584 565
585 if (stack) 566 if (stack)
586 { 567 {
568 printf ("deallocating stack %p %d\n", stack, stack->refcnt);/*D*/
587 if (!--stack->refcnt) 569 if (!--stack->refcnt)
588 { 570 {
589#ifdef HAVE_MMAP 571#ifdef HAVE_MMAP
590 if (stack->ssize > 0 && stack->sptr) 572 if (stack->ssize > 0 && stack->sptr)
591 munmap (stack->sptr, stack->ssize); 573 munmap (stack->sptr, stack->ssize);
675} 657}
676 658
677STATIC void 659STATIC void
678transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 660transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
679{ 661{
680 dSP;
681 dSTACKLEVEL; 662 dSTACKLEVEL;
663 static struct coro *xnext;
682 664
665 printf ("%p => %p\n", prev, next);/*D*/
683 if (prev != next) 666 if (prev != next)
684 { 667 {
668 xnext = next;
669
685 if (next->mainstack) 670 if (next->mainstack)
686 { 671 {
687 SAVE (prev, flags); 672 SAVE (prev, flags);
688 LOAD (next); 673 LOAD (next);
689 674
712 continue_coro, (void *)next, 697 continue_coro, (void *)next,
713 next->stack->sptr, labs (next->stack->ssize)); 698 next->stack->sptr, labs (next->stack->ssize));
714 } 699 }
715 700
716 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 701 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
702 /* don't add any code here */
717 } 703 }
718 704
719 } 705 }
720 else if (next->tmps_ix == -2) 706 else if (next->tmps_ix == -2)
721 croak ("tried to transfer to running coroutine"); 707 croak ("tried to transfer to running coroutine");
742 allocate_stack (next, 1); 728 allocate_stack (next, 1);
743 coro_create (&(next->stack->cctx), 729 coro_create (&(next->stack->cctx),
744 setup_coro, (void *)next, 730 setup_coro, (void *)next,
745 next->stack->sptr, labs (next->stack->ssize)); 731 next->stack->sptr, labs (next->stack->ssize));
746 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 732 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
733 /* don't add any code here */
747 } 734 }
748 } 735 }
749 else 736 else
750 setup_coro (next); 737 setup_coro (next);
751 } 738 }
752 } 739 }
753 740
754 next->cursp = stacklevel; 741 xnext->cursp = stacklevel;
755} 742}
756 743
757static struct coro * 744static struct coro *
758sv_to_coro (SV *arg, const char *funcname, const char *varname) 745sv_to_coro (SV *arg, const char *funcname, const char *varname)
759{ 746{
760 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) 747 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
761 { 748 {
762 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 749 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash);
763 750
764 if (!he) 751 if (!he)
765 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); 752 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
766 753
767 arg = HeVAL(he); 754 arg = HeVAL(he);
768 } 755 }
769 756
770 /* must also be changed inside Coro::Cont::yield */ 757 /* must also be changed inside Coro::Cont::yield */
771 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 758 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash)
772 return (struct coro *) SvIV((SV*)SvRV(arg)); 759 return (struct coro *) SvIV((SV*)SvRV(arg));
773 else 760
774 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 761 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname);
762 /*NORETURN*/
763}
764
765static void
766api_transfer(pTHX_ SV *prev, SV *next, int flags)
767{
768 transfer(aTHX_
769 sv_to_coro (prev, "Coro::transfer", "prev"),
770 sv_to_coro (next, "Coro::transfer", "next"),
771 flags);
772}
773
774/** Coro ********************************************************************/
775
776#define PRIO_MAX 3
777#define PRIO_HIGH 1
778#define PRIO_NORMAL 0
779#define PRIO_LOW -1
780#define PRIO_IDLE -3
781#define PRIO_MIN -4
782
783/* for Coro.pm */
784static GV *coro_current, *coro_idle;
785static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
786static int coro_nready;
787
788static void
789coro_enq (SV *sv)
790{
791 if (SvROK (sv))
792 {
793 SV *hv = SvRV (sv);
794 if (SvTYPE (hv) == SVt_PVHV)
795 {
796 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0);
797 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
798
799 prio = prio > PRIO_MAX ? PRIO_MAX
800 : prio < PRIO_MIN ? PRIO_MIN
801 : prio;
802
803 av_push (coro_ready [prio - PRIO_MIN], sv);
804 coro_nready++;
805
806 return;
807 }
808 }
809
810 croak ("Coro::ready tried to enqueue something that is not a coroutine");
811}
812
813static SV *
814coro_deq (int min_prio)
815{
816 int prio = PRIO_MAX - PRIO_MIN;
817
818 min_prio -= PRIO_MIN;
819 if (min_prio < 0)
820 min_prio = 0;
821
822 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
823 if (av_len (coro_ready[prio]) >= 0)
824 {
825 coro_nready--;
826 return av_shift (coro_ready[prio]);
827 }
828
829 return 0;
830}
831
832static void
833api_ready (SV *coro)
834{
835 coro_enq (SvREFCNT_inc (coro));
836}
837
838static void
839api_schedule (int cede)
840{
841 SV *prev, *next;
842
843 prev = GvSV (coro_current);
844
845 if (cede)
846 coro_enq (SvREFCNT_inc (prev));
847
848 next = coro_deq (PRIO_MIN);
849
850 if (!next)
851 next = SvREFCNT_inc (GvSV (coro_idle));
852
853 GvSV (coro_current) = SvREFCNT_inc (next);
854 transfer (aTHX_
855 sv_to_coro (prev, "Coro::schedule", "current coroutine"),
856 sv_to_coro (next, "Coro::schedule", "next coroutine"),
857 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
858 SvREFCNT_dec (next);
859 SvREFCNT_dec (prev);
775} 860}
776 861
777MODULE = Coro::State PACKAGE = Coro::State 862MODULE = Coro::State PACKAGE = Coro::State
778 863
779PROTOTYPES: ENABLE 864PROTOTYPES: ENABLE
791 876
792 if (!padlist_cache) 877 if (!padlist_cache)
793 padlist_cache = newHV (); 878 padlist_cache = newHV ();
794 879
795 main_mainstack = PL_mainstack; 880 main_mainstack = PL_mainstack;
881
882 coroapi.ver = CORO_API_VERSION;
883 coroapi.transfer = api_transfer;
796} 884}
797 885
798Coro::State 886Coro::State
799_newprocess(args) 887_newprocess(args)
800 SV * args 888 SV * args
833 921
834 if (coro->mainstack && coro->mainstack != main_mainstack) 922 if (coro->mainstack && coro->mainstack != main_mainstack)
835 { 923 {
836 struct coro temp; 924 struct coro temp;
837 925
926 PUTBACK;
838 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 927 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
839 LOAD(aTHX_ coro); 928 LOAD(aTHX_ coro);
929 SPAGAIN;
840 930
841 destroy_stacks (aTHX); 931 destroy_stacks (aTHX);
842 932
843 LOAD((&temp)); /* this will get rid of defsv etc.. */ 933 LOAD((&temp)); /* this will get rid of defsv etc.. */
934 SPAGAIN;
844 935
845 coro->mainstack = 0; 936 coro->mainstack = 0;
846 } 937 }
847 938
848 deallocate_stack (coro); 939 deallocate_stack (coro);
903 994
904# this is slightly dirty (should expose a c-level api) 995# this is slightly dirty (should expose a c-level api)
905 996
906BOOT: 997BOOT:
907{ 998{
999 int i;
1000 HV *stash = gv_stashpv ("Coro", TRUE);
1001
1002 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
1003 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1004 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1005 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
1006 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1007 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1008
908 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1009 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
909 coro_ready = newAV ();
910 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1010 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1011
1012 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1013 coro_ready[i] = newAV ();
1014
1015 {
1016 SV *sv = perl_get_sv("Coro::API", 1);
1017
1018 coroapi.schedule = api_schedule;
1019 coroapi.ready = api_ready;
1020 coroapi.nready = &coro_nready;
1021 coroapi.current = coro_current;
1022
1023 GCoroAPI = &coroapi;
1024 sv_setiv(sv, (IV)&coroapi);
1025 SvREADONLY_on(sv);
1026 }
911} 1027}
912 1028
913void 1029void
914ready(self) 1030ready(self)
915 SV * self 1031 SV * self
916 CODE: 1032 CODE:
917 av_push (coro_ready, SvREFCNT_inc (self)); 1033 api_ready (self);
1034
1035int
1036nready(...)
1037 PROTOTYPE:
1038 CODE:
1039 RETVAL = coro_nready;
1040 OUTPUT:
1041 RETVAL
918 1042
919void 1043void
920schedule(...) 1044schedule(...)
1045 PROTOTYPE:
921 ALIAS: 1046 ALIAS:
922 cede = 1 1047 cede = 1
923 CODE: 1048 CODE:
924 SV *prev = GvSV (coro_current); 1049 api_schedule (ix);
925 SV *next = av_shift (coro_ready);
926 1050
927 if (next == &PL_sv_undef)
928 next = SvREFCNT_inc (GvSV (coro_idle));
929
930 if (ix)
931 av_push (coro_ready, SvREFCNT_inc (prev));
932
933 GvSV (coro_current) = SvREFCNT_inc (next);
934 transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"),
935 sv_to_coro (next, "Coro::schedule", "next coroutine"),
936 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
937 SvREFCNT_dec (next);
938 SvREFCNT_dec (prev);
939

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines