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.15 by root, Wed Jul 25 04:14:38 2001 UTC vs.
Revision 1.23 by root, Tue Aug 14 04:33:58 2001 UTC

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 6
7#include <signal.h>
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>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS
15# else
16# undef HAVE_MMAP
17# endif
18# endif
10#endif 19#endif
11 20
12#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize */
13
14/* perl-related */
15#define TRANSFER_SAVE_DEFAV 0x00000001
16#define TRANSFER_SAVE_DEFSV 0x00000002
17#define TRANSFER_SAVE_ERRSV 0x00000004
18/* c-related */
19#define TRANSFER_SAVE_CCTXT 0x00000008
20#ifdef CORO_LAZY_STACK
21# define TRANSFER_LAZY_STACK 0x00000010
22#else
23# define TRANSFER_LAZY_STACK 0x00000000
24#endif
25
26#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
27 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
28 22
29#define SUB_INIT "Coro::State::initialize" 23#define SUB_INIT "Coro::State::initialize"
30#define UCORO_STATE "_coro_state" 24#define UCORO_STATE "_coro_state"
31 25
32/* 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
33 * 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
34 * and should be unique. */ 28 * and should be unique. */
35#define dSTACKLEVEL void *stacklevel = &stacklevel 29#define dSTACKLEVEL void *stacklevel = &stacklevel
36 30
37#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;
38 36
39/* 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... */
40typedef struct { 38typedef struct {
41 int refcnt; /* pointer reference counter */ 39 int refcnt; /* pointer reference counter */
42 int usecnt; /* shared by how many coroutines */ 40 int usecnt; /* shared by how many coroutines */
45 coro_context cctx; 43 coro_context cctx;
46 44
47 void *sptr; 45 void *sptr;
48 long ssize; /* positive == mmap, otherwise malloc */ 46 long ssize; /* positive == mmap, otherwise malloc */
49} coro_stack; 47} coro_stack;
50
51static coro_stack main_stack = { 1, 0, 0 };
52 48
53struct coro { 49struct coro {
54 /* the optional C context */ 50 /* the optional C context */
55 coro_stack *stack; 51 coro_stack *stack;
56 void *cursp; 52 void *cursp;
61 SV *defsv; 57 SV *defsv;
62 SV *errsv; 58 SV *errsv;
63 59
64 /* saved global state not related to stacks */ 60 /* saved global state not related to stacks */
65 U8 dowarn; 61 U8 dowarn;
62 I32 in_eval;
66 63
67 /* the stacks and related info (callchain etc..) */ 64 /* the stacks and related info (callchain etc..) */
68 PERL_SI *curstackinfo; 65 PERL_SI *curstackinfo;
69 AV *curstack; 66 AV *curstack;
70 AV *mainstack; 67 AV *mainstack;
88 I32 savestack_max; 85 I32 savestack_max;
89 OP **retstack; 86 OP **retstack;
90 I32 retstack_ix; 87 I32 retstack_ix;
91 I32 retstack_max; 88 I32 retstack_max;
92 COP *curcop; 89 COP *curcop;
93 JMPENV start_env;
94 JMPENV *top_env; 90 JMPENV *top_env;
95 91
96 /* data associated with this coroutine (initial args) */ 92 /* data associated with this coroutine (initial args) */
97 AV *args; 93 AV *args;
98}; 94};
283 279
284static void 280static void
285load_state(pTHX_ Coro__State c) 281load_state(pTHX_ Coro__State c)
286{ 282{
287 PL_dowarn = c->dowarn; 283 PL_dowarn = c->dowarn;
284 PL_in_eval = c->in_eval;
288 285
289 PL_curstackinfo = c->curstackinfo; 286 PL_curstackinfo = c->curstackinfo;
290 PL_curstack = c->curstack; 287 PL_curstack = c->curstack;
291 PL_mainstack = c->mainstack; 288 PL_mainstack = c->mainstack;
292 PL_stack_sp = c->stack_sp; 289 PL_stack_sp = c->stack_sp;
309 PL_savestack_max = c->savestack_max; 306 PL_savestack_max = c->savestack_max;
310 PL_retstack = c->retstack; 307 PL_retstack = c->retstack;
311 PL_retstack_ix = c->retstack_ix; 308 PL_retstack_ix = c->retstack_ix;
312 PL_retstack_max = c->retstack_max; 309 PL_retstack_max = c->retstack_max;
313 PL_curcop = c->curcop; 310 PL_curcop = c->curcop;
314 PL_start_env = c->start_env;
315 PL_top_env = c->top_env; 311 PL_top_env = c->top_env;
316 312
317 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 313 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
318 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 314 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
319 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 315 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
418 /* but if it's missing the defav contents magically get replaced sometimes */ 414 /* but if it's missing the defav contents magically get replaced sometimes */
419 if (c->defav) 415 if (c->defav)
420 av_reify (c->defav); 416 av_reify (c->defav);
421 417
422 c->dowarn = PL_dowarn; 418 c->dowarn = PL_dowarn;
419 c->in_eval = PL_in_eval;
423 420
424 c->curstackinfo = PL_curstackinfo; 421 c->curstackinfo = PL_curstackinfo;
425 c->curstack = PL_curstack; 422 c->curstack = PL_curstack;
426 c->mainstack = PL_mainstack; 423 c->mainstack = PL_mainstack;
427 c->stack_sp = PL_stack_sp; 424 c->stack_sp = PL_stack_sp;
444 c->savestack_max = PL_savestack_max; 441 c->savestack_max = PL_savestack_max;
445 c->retstack = PL_retstack; 442 c->retstack = PL_retstack;
446 c->retstack_ix = PL_retstack_ix; 443 c->retstack_ix = PL_retstack_ix;
447 c->retstack_max = PL_retstack_max; 444 c->retstack_max = PL_retstack_max;
448 c->curcop = PL_curcop; 445 c->curcop = PL_curcop;
449 c->start_env = PL_start_env;
450 c->top_env = PL_top_env; 446 c->top_env = PL_top_env;
451} 447}
452 448
453/* 449/*
454 * allocate various perl stacks. This is an exact copy 450 * allocate various perl stacks. This is an exact copy
545 stack->refcnt = 1; 541 stack->refcnt = 1;
546 stack->usecnt = 1; 542 stack->usecnt = 1;
547 stack->gencnt = ctx->gencnt = 0; 543 stack->gencnt = ctx->gencnt = 0;
548 if (alloc) 544 if (alloc)
549 { 545 {
550#ifdef HAVE_MMAP 546#ifdef HAVE_MMAP
551 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-use */ 547 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
552 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 548 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
553 if (stack->sptr == (void *)-1) 549 if (stack->sptr == (void *)-1)
554#endif 550#endif
555 { 551 {
556 /*FIXME*//*D*//* reasonable stack size! */ 552 /*FIXME*//*D*//* reasonable stack size! */
598 dSP; 594 dSP;
599 Coro__State ctx = (Coro__State)arg; 595 Coro__State ctx = (Coro__State)arg;
600 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 596 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
601 597
602 coro_init_stacks (aTHX); 598 coro_init_stacks (aTHX);
603 JMPENV_BOOTSTRAP;
604 SPAGAIN;
605
606 /*PL_curcop = 0;*/ 599 /*PL_curcop = 0;*/
600 /*PL_in_eval = PL_in_eval;*/ /* inherit */
607 SvREFCNT_dec (GvAV (PL_defgv)); 601 SvREFCNT_dec (GvAV (PL_defgv));
608 GvAV (PL_defgv) = ctx->args; 602 GvAV (PL_defgv) = ctx->args;
609 603
604 SPAGAIN;
605
610 if (ctx->stack) 606 if (ctx->stack)
611 { 607 {
612 ctx->cursp = 0; 608 ctx->cursp = 0;
613 609
614 PUSHMARK(SP); 610 PUSHMARK(SP);
615 PUTBACK; 611 PUTBACK;
616 (void) call_sv (sub_init, G_VOID|G_NOARGS); 612 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
613
614 if (SvTRUE (ERRSV))
615 croak (NULL);
616 else
617 croak ("FATAL: CCTXT coroutine returned!"); 617 croak ("FATAL: CCTXT coroutine returned!");
618 } 618 }
619 else 619 else
620 { 620 {
621 UNOP myop; 621 UNOP myop;
622 622
649 /* 649 /*
650 * this is a _very_ stripped down perl interpreter ;) 650 * this is a _very_ stripped down perl interpreter ;)
651 */ 651 */
652 Coro__State ctx = (Coro__State)arg; 652 Coro__State ctx = (Coro__State)arg;
653 653
654 /*FIXME*//* must set up top_env here */
654 ctx->cursp = 0; 655 ctx->cursp = 0;
655 PL_op = PL_op->op_next; 656 PL_op = PL_op->op_next;
656 CALLRUNOPS(aTHX); 657 CALLRUNOPS(aTHX);
657 /*NORETURN*/ 658
658 abort (); 659 abort ();
659} 660}
660 661
661STATIC void 662STATIC void
662transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 663transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
736 } 737 }
737 738
738 next->cursp = stacklevel; 739 next->cursp = stacklevel;
739} 740}
740 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
741MODULE = Coro::State PACKAGE = Coro::State 852MODULE = Coro::State PACKAGE = Coro::State
742 853
743PROTOTYPES: ENABLE 854PROTOTYPES: ENABLE
744 855
745BOOT: 856BOOT:
755 866
756 if (!padlist_cache) 867 if (!padlist_cache)
757 padlist_cache = newHV (); 868 padlist_cache = newHV ();
758 869
759 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 }
760} 884}
761 885
762Coro::State 886Coro::State
763_newprocess(args) 887_newprocess(args)
764 SV * args 888 SV * args
778 RETVAL = coro; 902 RETVAL = coro;
779 OUTPUT: 903 OUTPUT:
780 RETVAL 904 RETVAL
781 905
782void 906void
783transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) 907transfer(prev, next, flags)
784 Coro::State_or_hashref prev 908 Coro::State_or_hashref prev
785 Coro::State_or_hashref next 909 Coro::State_or_hashref next
786 int flags 910 int flags
787 PROTOTYPE: @ 911 PROTOTYPE: @
788 CODE: 912 CODE:
913 PUTBACK;
789 transfer (aTHX_ prev, next, flags); 914 transfer (aTHX_ prev, next, flags);
915 SPAGAIN;
790 916
791void 917void
792DESTROY(coro) 918DESTROY(coro)
793 Coro::State coro 919 Coro::State coro
794 CODE: 920 CODE:
816 CODE: 942 CODE:
817#ifdef MAY_FLUSH 943#ifdef MAY_FLUSH
818 flush_padlist_cache (); 944 flush_padlist_cache ();
819#endif 945#endif
820 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
821MODULE = Coro::State PACKAGE = Coro::Cont 960MODULE = Coro::State PACKAGE = Coro::Cont
822 961
823# this is slightly dirty 962# this is slightly dirty (should expose a c-level api)
824 963
825void 964void
826yield(...) 965yield(...)
827 PROTOTYPE: @ 966 PROTOTYPE: @
828 CODE: 967 CODE:
846 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)));
847 SvREFCNT_dec (sv); 986 SvREFCNT_dec (sv);
848 987
849 transfer(aTHX_ prev, next, 0); 988 transfer(aTHX_ prev, next, 0);
850 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