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.24 by root, Tue Aug 14 14:56:22 2001 UTC

18# endif 18# endif
19#endif 19#endif
20 20
21#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize */
22 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
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 delcare 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;
529 dSP; 514 dSP;
530 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 515 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
531 PUTBACK; /* possibly superfluous */ 516 PUTBACK; /* possibly superfluous */
532 } 517 }
533 518
519 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */
534 dounwind(-1); 520 dounwind(-1);
535 521
536 SvREFCNT_dec(PL_curstackinfo->si_stack); 522 SvREFCNT_dec(PL_curstackinfo->si_stack);
537 Safefree(PL_curstackinfo->si_cxstack); 523 Safefree(PL_curstackinfo->si_cxstack);
538 Safefree(PL_curstackinfo); 524 Safefree(PL_curstackinfo);
539 PL_curstackinfo = p; 525 PL_curstackinfo = p;
755} 741}
756 742
757static struct coro * 743static struct coro *
758sv_to_coro (SV *arg, const char *funcname, const char *varname) 744sv_to_coro (SV *arg, const char *funcname, const char *varname)
759{ 745{
760 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) 746 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
761 { 747 {
762 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 748 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash);
763 749
764 if (!he) 750 if (!he)
765 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); 751 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
766 752
767 arg = HeVAL(he); 753 arg = HeVAL(he);
768 } 754 }
769 755
770 /* must also be changed inside Coro::Cont::yield */ 756 /* must also be changed inside Coro::Cont::yield */
771 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 757 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash)
772 return (struct coro *) SvIV((SV*)SvRV(arg)); 758 return (struct coro *) SvIV((SV*)SvRV(arg));
773 else 759 else
774 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 760 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname);
761}
762
763static void
764api_transfer(pTHX_ SV *prev, SV *next, int flags)
765{
766 transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"),
767 sv_to_coro (next, "Coro::transfer", "next"),
768 flags);
769}
770
771/** Coro ********************************************************************/
772
773#define PRIO_MAX 3
774#define PRIO_HIGH 1
775#define PRIO_NORMAL 0
776#define PRIO_LOW -1
777#define PRIO_IDLE -3
778#define PRIO_MIN -4
779
780/* for Coro.pm */
781static GV *coro_current, *coro_idle;
782static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
783
784static void
785coro_enq (SV *sv)
786{
787 if (SvROK (sv))
788 {
789 SV *hv = SvRV (sv);
790 if (SvTYPE (hv) == SVt_PVHV)
791 {
792 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0);
793 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
794
795 prio = prio > PRIO_MAX ? PRIO_MAX
796 : prio < PRIO_MIN ? PRIO_MIN
797 : prio;
798
799 av_push (coro_ready [prio - PRIO_MIN], sv);
800
801 return;
802 }
803 }
804
805 croak ("Coro::ready tried to enqueue something that is not a coroutine");
806}
807
808static SV *
809coro_deq (int min_prio)
810{
811 int prio = PRIO_MAX - PRIO_MIN;
812
813 min_prio -= PRIO_MIN;
814 if (min_prio < 0)
815 min_prio = 0;
816
817 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
818 if (av_len (coro_ready[prio]) >= 0)
819 return av_shift (coro_ready[prio]);
820
821 return 0;
822}
823
824static void
825api_ready (SV *coro)
826{
827 coro_enq (SvREFCNT_inc (coro));
828}
829
830static void
831api_schedule (int cede)
832{
833 SV *prev, *next;
834
835 prev = GvSV (coro_current);
836
837 if (cede)
838 coro_enq (SvREFCNT_inc (prev));
839
840 next = coro_deq (PRIO_MIN);
841
842 if (!next)
843 next = SvREFCNT_inc (GvSV (coro_idle));
844
845 GvSV (coro_current) = SvREFCNT_inc (next);
846 transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"),
847 sv_to_coro (next, "Coro::schedule", "next coroutine"),
848 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
849 SvREFCNT_dec (next);
850 SvREFCNT_dec (prev);
775} 851}
776 852
777MODULE = Coro::State PACKAGE = Coro::State 853MODULE = Coro::State PACKAGE = Coro::State
778 854
779PROTOTYPES: ENABLE 855PROTOTYPES: ENABLE
791 867
792 if (!padlist_cache) 868 if (!padlist_cache)
793 padlist_cache = newHV (); 869 padlist_cache = newHV ();
794 870
795 main_mainstack = PL_mainstack; 871 main_mainstack = PL_mainstack;
872
873 {
874 SV *sv = perl_get_sv("Coro::API", 1);
875
876 coroapi.ver = CORO_API_VERSION - 1;
877 coroapi.transfer = api_transfer;
878 coroapi.schedule = api_schedule;
879 coroapi.ready = api_ready;
880
881 GCoroAPI = &coroapi;
882 sv_setiv(sv, (IV)&coroapi);
883 SvREADONLY_on(sv);
884 }
796} 885}
797 886
798Coro::State 887Coro::State
799_newprocess(args) 888_newprocess(args)
800 SV * args 889 SV * args
903 992
904# this is slightly dirty (should expose a c-level api) 993# this is slightly dirty (should expose a c-level api)
905 994
906BOOT: 995BOOT:
907{ 996{
997 int i;
998 HV *stash = gv_stashpv ("Coro", TRUE);
999
1000 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
1001 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1002 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1003 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
1004 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1005 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1006
908 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1007 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
909 coro_ready = newAV ();
910 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1008 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1009
1010 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1011 coro_ready[i] = newAV ();
911} 1012}
912 1013
913void 1014void
914ready(self) 1015ready(self)
915 SV * self 1016 SV * self
916 CODE: 1017 CODE:
917 av_push (coro_ready, SvREFCNT_inc (self)); 1018 api_ready (self);
918 1019
919void 1020void
920schedule(...) 1021schedule(...)
921 ALIAS: 1022 ALIAS:
922 cede = 1 1023 cede = 1
923 CODE: 1024 CODE:
924 SV *prev = GvSV (coro_current); 1025 api_schedule (ix);
925 SV *next = av_shift (coro_ready);
926 1026
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