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.26 by root, Fri Aug 17 01:45:39 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 */
523 dSP; 514 dSP;
524 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 515 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
525 PUTBACK; /* possibly superfluous */ 516 PUTBACK; /* possibly superfluous */
526 } 517 }
527 518
519 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */
528 dounwind(-1); 520 dounwind(-1);
529 521
530 SvREFCNT_dec(PL_curstackinfo->si_stack); 522 SvREFCNT_dec(PL_curstackinfo->si_stack);
531 Safefree(PL_curstackinfo->si_cxstack); 523 Safefree(PL_curstackinfo->si_cxstack);
532 Safefree(PL_curstackinfo); 524 Safefree(PL_curstackinfo);
533 PL_curstackinfo = p; 525 PL_curstackinfo = p;
746 } 738 }
747 739
748 next->cursp = stacklevel; 740 next->cursp = stacklevel;
749} 741}
750 742
743static struct coro *
744sv_to_coro (SV *arg, const char *funcname, const char *varname)
745{
746 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
747 {
748 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash);
749
750 if (!he)
751 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
752
753 arg = HeVAL(he);
754 }
755
756 /* must also be changed inside Coro::Cont::yield */
757 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash)
758 return (struct coro *) SvIV((SV*)SvRV(arg));
759 else
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];
783static int coro_nready;
784
785static void
786coro_enq (SV *sv)
787{
788 if (SvROK (sv))
789 {
790 SV *hv = SvRV (sv);
791 if (SvTYPE (hv) == SVt_PVHV)
792 {
793 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0);
794 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
795
796 prio = prio > PRIO_MAX ? PRIO_MAX
797 : prio < PRIO_MIN ? PRIO_MIN
798 : prio;
799
800 av_push (coro_ready [prio - PRIO_MIN], sv);
801 coro_nready++;
802
803 return;
804 }
805 }
806
807 croak ("Coro::ready tried to enqueue something that is not a coroutine");
808}
809
810static SV *
811coro_deq (int min_prio)
812{
813 int prio = PRIO_MAX - PRIO_MIN;
814
815 min_prio -= PRIO_MIN;
816 if (min_prio < 0)
817 min_prio = 0;
818
819 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
820 if (av_len (coro_ready[prio]) >= 0)
821 {
822 coro_nready--;
823 return av_shift (coro_ready[prio]);
824 }
825
826 return 0;
827}
828
829static void
830api_ready (SV *coro)
831{
832 coro_enq (SvREFCNT_inc (coro));
833}
834
835static void
836api_schedule (int cede)
837{
838 SV *prev, *next;
839
840 prev = GvSV (coro_current);
841
842 if (cede)
843 coro_enq (SvREFCNT_inc (prev));
844
845 next = coro_deq (PRIO_MIN);
846
847 if (!next)
848 next = SvREFCNT_inc (GvSV (coro_idle));
849
850 GvSV (coro_current) = SvREFCNT_inc (next);
851 transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"),
852 sv_to_coro (next, "Coro::schedule", "next coroutine"),
853 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
854 SvREFCNT_dec (next);
855 SvREFCNT_dec (prev);
856}
857
751MODULE = Coro::State PACKAGE = Coro::State 858MODULE = Coro::State PACKAGE = Coro::State
752 859
753PROTOTYPES: ENABLE 860PROTOTYPES: ENABLE
754 861
755BOOT: 862BOOT:
765 872
766 if (!padlist_cache) 873 if (!padlist_cache)
767 padlist_cache = newHV (); 874 padlist_cache = newHV ();
768 875
769 main_mainstack = PL_mainstack; 876 main_mainstack = PL_mainstack;
877
878 coroapi.ver = CORO_API_VERSION;
879 coroapi.transfer = api_transfer;
770} 880}
771 881
772Coro::State 882Coro::State
773_newprocess(args) 883_newprocess(args)
774 SV * args 884 SV * args
788 RETVAL = coro; 898 RETVAL = coro;
789 OUTPUT: 899 OUTPUT:
790 RETVAL 900 RETVAL
791 901
792void 902void
793transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) 903transfer(prev, next, flags)
794 Coro::State_or_hashref prev 904 Coro::State_or_hashref prev
795 Coro::State_or_hashref next 905 Coro::State_or_hashref next
796 int flags 906 int flags
797 PROTOTYPE: @ 907 PROTOTYPE: @
798 CODE: 908 CODE:
909 PUTBACK;
799 transfer (aTHX_ prev, next, flags); 910 transfer (aTHX_ prev, next, flags);
911 SPAGAIN;
800 912
801void 913void
802DESTROY(coro) 914DESTROY(coro)
803 Coro::State coro 915 Coro::State coro
804 CODE: 916 CODE:
826 CODE: 938 CODE:
827#ifdef MAY_FLUSH 939#ifdef MAY_FLUSH
828 flush_padlist_cache (); 940 flush_padlist_cache ();
829#endif 941#endif
830 942
943void
944_exit(code)
945 int code
946 PROTOTYPE: $
947 CODE:
948#if defined(__GLIBC__) || _POSIX_C_SOURCE
949 _exit (code);
950#else
951 signal (SIGTERM, SIG_DFL);
952 raise (SIGTERM);
953 exit (code);
954#endif
955
831MODULE = Coro::State PACKAGE = Coro::Cont 956MODULE = Coro::State PACKAGE = Coro::Cont
832 957
833# this is slightly dirty 958# this is slightly dirty (should expose a c-level api)
834 959
835void 960void
836yield(...) 961yield(...)
837 PROTOTYPE: @ 962 PROTOTYPE: @
838 CODE: 963 CODE:
856 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 981 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
857 SvREFCNT_dec (sv); 982 SvREFCNT_dec (sv);
858 983
859 transfer(aTHX_ prev, next, 0); 984 transfer(aTHX_ prev, next, 0);
860 985
986MODULE = Coro::State PACKAGE = Coro
987
988# this is slightly dirty (should expose a c-level api)
989
990BOOT:
991{
992 int i;
993 HV *stash = gv_stashpv ("Coro", TRUE);
994
995 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
996 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
997 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
998 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
999 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1000 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1001
1002 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1003 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1004
1005 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1006 coro_ready[i] = newAV ();
1007
1008 {
1009 SV *sv = perl_get_sv("Coro::API", 1);
1010
1011 coroapi.schedule = api_schedule;
1012 coroapi.ready = api_ready;
1013 coroapi.nready = &coro_nready;
1014 coroapi.current = coro_current;
1015
1016 GCoroAPI = &coroapi;
1017 sv_setiv(sv, (IV)&coroapi);
1018 SvREADONLY_on(sv);
1019 }
1020}
1021
1022void
1023ready(self)
1024 SV * self
1025 CODE:
1026 api_ready (self);
1027
1028int
1029nready(...)
1030 PROTOTYPE:
1031 CODE:
1032 RETVAL = coro_nready;
1033 OUTPUT:
1034 RETVAL
1035
1036void
1037schedule(...)
1038 PROTOTYPE:
1039 ALIAS:
1040 cede = 1
1041 CODE:
1042 api_schedule (ix);
1043

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines