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.19 by root, Sun Jul 29 01:11:41 2001 UTC vs.
Revision 1.23 by root, Tue Aug 14 04:33:58 2001 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 "libcoro/coro.c" 5#include "libcoro/coro.c"
6
7#include <signal.h>
6 8
7#ifdef HAVE_MMAP 9#ifdef HAVE_MMAP
8# include <unistd.h> 10# include <unistd.h>
9# include <sys/mman.h> 11# include <sys/mman.h>
10# ifndef MAP_ANON 12# ifndef MAP_ANON
16# endif 18# endif
17#endif 19#endif
18 20
19#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize */
20 22
21/* perl-related */
22#define TRANSFER_SAVE_DEFAV 0x00000001
23#define TRANSFER_SAVE_DEFSV 0x00000002
24#define TRANSFER_SAVE_ERRSV 0x00000004
25/* c-related */
26#define TRANSFER_SAVE_CCTXT 0x00000008
27#ifdef CORO_LAZY_STACK
28# define TRANSFER_LAZY_STACK 0x00000010
29#else
30# define TRANSFER_LAZY_STACK 0x00000000
31#endif
32
33#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
34 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
35
36#define SUB_INIT "Coro::State::initialize" 23#define SUB_INIT "Coro::State::initialize"
37#define UCORO_STATE "_coro_state" 24#define UCORO_STATE "_coro_state"
38 25
39/* 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
40 * 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
41 * and should be unique. */ 28 * and should be unique. */
42#define dSTACKLEVEL void *stacklevel = &stacklevel 29#define dSTACKLEVEL void *stacklevel = &stacklevel
43 30
44#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;
45 36
46/* 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... */
47typedef struct { 38typedef struct {
48 int refcnt; /* pointer reference counter */ 39 int refcnt; /* pointer reference counter */
49 int usecnt; /* shared by how many coroutines */ 40 int usecnt; /* shared by how many coroutines */
746 } 737 }
747 738
748 next->cursp = stacklevel; 739 next->cursp = stacklevel;
749} 740}
750 741
742static struct coro *
743sv_to_coro (SV *arg, const char *funcname, const char *varname)
744{
745 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
746 {
747 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash);
748
749 if (!he)
750 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
751
752 arg = HeVAL(he);
753 }
754
755 /* must also be changed inside Coro::Cont::yield */
756 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash)
757 return (struct coro *) SvIV((SV*)SvRV(arg));
758 else
759 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname);
760}
761
762static void
763api_transfer(pTHX_ SV *prev, SV *next, int flags)
764{
765 transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"),
766 sv_to_coro (next, "Coro::transfer", "next"),
767 flags);
768}
769
770/** Coro ********************************************************************/
771
772#define PRIO_MAX 3
773#define PRIO_HIGH 1
774#define PRIO_NORMAL 0
775#define PRIO_LOW -1
776#define PRIO_IDLE -3
777#define PRIO_MIN -4
778
779/* for Coro.pm */
780static GV *coro_current, *coro_idle;
781static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
782
783static void
784coro_enq (SV *sv)
785{
786 if (SvROK (sv))
787 {
788 SV *hv = SvRV (sv);
789 if (SvTYPE (hv) == SVt_PVHV)
790 {
791 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0);
792 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
793
794 prio = prio > PRIO_MAX ? PRIO_MAX
795 : prio < PRIO_MIN ? PRIO_MIN
796 : prio;
797
798 av_push (coro_ready [prio - PRIO_MIN], sv);
799
800 return;
801 }
802 }
803
804 croak ("Coro::ready tried to enqueue something that is not a coroutine");
805}
806
807static SV *
808coro_deq (int min_prio)
809{
810 int prio = PRIO_MAX - PRIO_MIN;
811
812 min_prio -= PRIO_MIN;
813 if (min_prio < 0)
814 min_prio = 0;
815
816 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
817 if (av_len (coro_ready[prio]) >= 0)
818 return av_shift (coro_ready[prio]);
819
820 return 0;
821}
822
823static void
824api_ready (SV *coro)
825{
826 coro_enq (SvREFCNT_inc (coro));
827}
828
829static void
830api_schedule (int cede)
831{
832 SV *prev, *next;
833
834 prev = GvSV (coro_current);
835
836 if (cede)
837 coro_enq (SvREFCNT_inc (prev));
838
839 next = coro_deq (PRIO_MIN);
840
841 if (!next)
842 next = SvREFCNT_inc (GvSV (coro_idle));
843
844 GvSV (coro_current) = SvREFCNT_inc (next);
845 transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"),
846 sv_to_coro (next, "Coro::schedule", "next coroutine"),
847 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
848 SvREFCNT_dec (next);
849 SvREFCNT_dec (prev);
850}
851
751MODULE = Coro::State PACKAGE = Coro::State 852MODULE = Coro::State PACKAGE = Coro::State
752 853
753PROTOTYPES: ENABLE 854PROTOTYPES: ENABLE
754 855
755BOOT: 856BOOT:
765 866
766 if (!padlist_cache) 867 if (!padlist_cache)
767 padlist_cache = newHV (); 868 padlist_cache = newHV ();
768 869
769 main_mainstack = PL_mainstack; 870 main_mainstack = PL_mainstack;
871
872 {
873 SV *sv = perl_get_sv("Coro::API", 1);
874
875 coroapi.ver = CORO_API_VERSION - 1;
876 coroapi.transfer = api_transfer;
877 coroapi.schedule = api_schedule;
878 coroapi.ready = api_ready;
879
880 GCoroAPI = &coroapi;
881 sv_setiv(sv, (IV)&coroapi);
882 SvREADONLY_on(sv);
883 }
770} 884}
771 885
772Coro::State 886Coro::State
773_newprocess(args) 887_newprocess(args)
774 SV * args 888 SV * args
788 RETVAL = coro; 902 RETVAL = coro;
789 OUTPUT: 903 OUTPUT:
790 RETVAL 904 RETVAL
791 905
792void 906void
793transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) 907transfer(prev, next, flags)
794 Coro::State_or_hashref prev 908 Coro::State_or_hashref prev
795 Coro::State_or_hashref next 909 Coro::State_or_hashref next
796 int flags 910 int flags
797 PROTOTYPE: @ 911 PROTOTYPE: @
798 CODE: 912 CODE:
913 PUTBACK;
799 transfer (aTHX_ prev, next, flags); 914 transfer (aTHX_ prev, next, flags);
915 SPAGAIN;
800 916
801void 917void
802DESTROY(coro) 918DESTROY(coro)
803 Coro::State coro 919 Coro::State coro
804 CODE: 920 CODE:
826 CODE: 942 CODE:
827#ifdef MAY_FLUSH 943#ifdef MAY_FLUSH
828 flush_padlist_cache (); 944 flush_padlist_cache ();
829#endif 945#endif
830 946
947void
948_exit(code)
949 int code
950 PROTOTYPE: $
951 CODE:
952#if defined(__GLIBC__) || _POSIX_C_SOURCE
953 _exit (code);
954#else
955 signal (SIGTERM, SIG_DFL);
956 raise (SIGTERM);
957 exit (code);
958#endif
959
831MODULE = Coro::State PACKAGE = Coro::Cont 960MODULE = Coro::State PACKAGE = Coro::Cont
832 961
833# this is slightly dirty 962# this is slightly dirty (should expose a c-level api)
834 963
835void 964void
836yield(...) 965yield(...)
837 PROTOTYPE: @ 966 PROTOTYPE: @
838 CODE: 967 CODE:
856 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 985 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
857 SvREFCNT_dec (sv); 986 SvREFCNT_dec (sv);
858 987
859 transfer(aTHX_ prev, next, 0); 988 transfer(aTHX_ prev, next, 0);
860 989
990MODULE = Coro::State PACKAGE = Coro
991
992# this is slightly dirty (should expose a c-level api)
993
994BOOT:
995{
996 int i;
997 HV *stash = gv_stashpv ("Coro", TRUE);
998
999 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
1000 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1001 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1002 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
1003 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1004 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1005
1006 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1007 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1008
1009 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1010 coro_ready[i] = newAV ();
1011}
1012
1013void
1014ready(self)
1015 SV * self
1016 CODE:
1017 api_ready (self);
1018
1019void
1020schedule(...)
1021 ALIAS:
1022 cede = 1
1023 CODE:
1024 api_schedule (ix);
1025

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines