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.16 by root, Wed Jul 25 04:26:55 2001 UTC vs.
Revision 1.25 by root, Wed Aug 15 03:24:07 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 */
52 coro_context cctx; 43 coro_context cctx;
53 44
54 void *sptr; 45 void *sptr;
55 long ssize; /* positive == mmap, otherwise malloc */ 46 long ssize; /* positive == mmap, otherwise malloc */
56} coro_stack; 47} coro_stack;
57
58static coro_stack main_stack = { 1, 0, 0 };
59 48
60struct coro { 49struct coro {
61 /* the optional C context */ 50 /* the optional C context */
62 coro_stack *stack; 51 coro_stack *stack;
63 void *cursp; 52 void *cursp;
68 SV *defsv; 57 SV *defsv;
69 SV *errsv; 58 SV *errsv;
70 59
71 /* saved global state not related to stacks */ 60 /* saved global state not related to stacks */
72 U8 dowarn; 61 U8 dowarn;
62 I32 in_eval;
73 63
74 /* the stacks and related info (callchain etc..) */ 64 /* the stacks and related info (callchain etc..) */
75 PERL_SI *curstackinfo; 65 PERL_SI *curstackinfo;
76 AV *curstack; 66 AV *curstack;
77 AV *mainstack; 67 AV *mainstack;
95 I32 savestack_max; 85 I32 savestack_max;
96 OP **retstack; 86 OP **retstack;
97 I32 retstack_ix; 87 I32 retstack_ix;
98 I32 retstack_max; 88 I32 retstack_max;
99 COP *curcop; 89 COP *curcop;
100 JMPENV start_env;
101 JMPENV *top_env; 90 JMPENV *top_env;
102 91
103 /* data associated with this coroutine (initial args) */ 92 /* data associated with this coroutine (initial args) */
104 AV *args; 93 AV *args;
105}; 94};
290 279
291static void 280static void
292load_state(pTHX_ Coro__State c) 281load_state(pTHX_ Coro__State c)
293{ 282{
294 PL_dowarn = c->dowarn; 283 PL_dowarn = c->dowarn;
284 PL_in_eval = c->in_eval;
295 285
296 PL_curstackinfo = c->curstackinfo; 286 PL_curstackinfo = c->curstackinfo;
297 PL_curstack = c->curstack; 287 PL_curstack = c->curstack;
298 PL_mainstack = c->mainstack; 288 PL_mainstack = c->mainstack;
299 PL_stack_sp = c->stack_sp; 289 PL_stack_sp = c->stack_sp;
316 PL_savestack_max = c->savestack_max; 306 PL_savestack_max = c->savestack_max;
317 PL_retstack = c->retstack; 307 PL_retstack = c->retstack;
318 PL_retstack_ix = c->retstack_ix; 308 PL_retstack_ix = c->retstack_ix;
319 PL_retstack_max = c->retstack_max; 309 PL_retstack_max = c->retstack_max;
320 PL_curcop = c->curcop; 310 PL_curcop = c->curcop;
321 PL_start_env = c->start_env;
322 PL_top_env = c->top_env; 311 PL_top_env = c->top_env;
323 312
324 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 313 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
325 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 314 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
326 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 315 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
425 /* 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 */
426 if (c->defav) 415 if (c->defav)
427 av_reify (c->defav); 416 av_reify (c->defav);
428 417
429 c->dowarn = PL_dowarn; 418 c->dowarn = PL_dowarn;
419 c->in_eval = PL_in_eval;
430 420
431 c->curstackinfo = PL_curstackinfo; 421 c->curstackinfo = PL_curstackinfo;
432 c->curstack = PL_curstack; 422 c->curstack = PL_curstack;
433 c->mainstack = PL_mainstack; 423 c->mainstack = PL_mainstack;
434 c->stack_sp = PL_stack_sp; 424 c->stack_sp = PL_stack_sp;
451 c->savestack_max = PL_savestack_max; 441 c->savestack_max = PL_savestack_max;
452 c->retstack = PL_retstack; 442 c->retstack = PL_retstack;
453 c->retstack_ix = PL_retstack_ix; 443 c->retstack_ix = PL_retstack_ix;
454 c->retstack_max = PL_retstack_max; 444 c->retstack_max = PL_retstack_max;
455 c->curcop = PL_curcop; 445 c->curcop = PL_curcop;
456 c->start_env = PL_start_env;
457 c->top_env = PL_top_env; 446 c->top_env = PL_top_env;
458} 447}
459 448
460/* 449/*
461 * allocate various perl stacks. This is an exact copy 450 * allocate various perl stacks. This is an exact copy
525 dSP; 514 dSP;
526 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 515 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
527 PUTBACK; /* possibly superfluous */ 516 PUTBACK; /* possibly superfluous */
528 } 517 }
529 518
519 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */
530 dounwind(-1); 520 dounwind(-1);
531 521
532 SvREFCNT_dec(PL_curstackinfo->si_stack); 522 SvREFCNT_dec(PL_curstackinfo->si_stack);
533 Safefree(PL_curstackinfo->si_cxstack); 523 Safefree(PL_curstackinfo->si_cxstack);
534 Safefree(PL_curstackinfo); 524 Safefree(PL_curstackinfo);
535 PL_curstackinfo = p; 525 PL_curstackinfo = p;
553 stack->usecnt = 1; 543 stack->usecnt = 1;
554 stack->gencnt = ctx->gencnt = 0; 544 stack->gencnt = ctx->gencnt = 0;
555 if (alloc) 545 if (alloc)
556 { 546 {
557#ifdef HAVE_MMAP 547#ifdef HAVE_MMAP
558 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write */ 548 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
559 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 549 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
560 if (stack->sptr == (void *)-1) 550 if (stack->sptr == (void *)-1)
561#endif 551#endif
562 { 552 {
563 /*FIXME*//*D*//* reasonable stack size! */ 553 /*FIXME*//*D*//* reasonable stack size! */
605 dSP; 595 dSP;
606 Coro__State ctx = (Coro__State)arg; 596 Coro__State ctx = (Coro__State)arg;
607 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 597 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
608 598
609 coro_init_stacks (aTHX); 599 coro_init_stacks (aTHX);
610 JMPENV_BOOTSTRAP;
611 SPAGAIN;
612
613 /*PL_curcop = 0;*/ 600 /*PL_curcop = 0;*/
601 /*PL_in_eval = PL_in_eval;*/ /* inherit */
614 SvREFCNT_dec (GvAV (PL_defgv)); 602 SvREFCNT_dec (GvAV (PL_defgv));
615 GvAV (PL_defgv) = ctx->args; 603 GvAV (PL_defgv) = ctx->args;
616 604
605 SPAGAIN;
606
617 if (ctx->stack) 607 if (ctx->stack)
618 { 608 {
619 ctx->cursp = 0; 609 ctx->cursp = 0;
620 610
621 PUSHMARK(SP); 611 PUSHMARK(SP);
622 PUTBACK; 612 PUTBACK;
623 (void) call_sv (sub_init, G_VOID|G_NOARGS); 613 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
614
615 if (SvTRUE (ERRSV))
616 croak (NULL);
617 else
624 croak ("FATAL: CCTXT coroutine returned!"); 618 croak ("FATAL: CCTXT coroutine returned!");
625 } 619 }
626 else 620 else
627 { 621 {
628 UNOP myop; 622 UNOP myop;
629 623
656 /* 650 /*
657 * this is a _very_ stripped down perl interpreter ;) 651 * this is a _very_ stripped down perl interpreter ;)
658 */ 652 */
659 Coro__State ctx = (Coro__State)arg; 653 Coro__State ctx = (Coro__State)arg;
660 654
655 /*FIXME*//* must set up top_env here */
661 ctx->cursp = 0; 656 ctx->cursp = 0;
662 PL_op = PL_op->op_next; 657 PL_op = PL_op->op_next;
663 CALLRUNOPS(aTHX); 658 CALLRUNOPS(aTHX);
664 /*NORETURN*/ 659
665 abort (); 660 abort ();
666} 661}
667 662
668STATIC void 663STATIC void
669transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 664transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
743 } 738 }
744 739
745 next->cursp = stacklevel; 740 next->cursp = stacklevel;
746} 741}
747 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
748MODULE = Coro::State PACKAGE = Coro::State 858MODULE = Coro::State PACKAGE = Coro::State
749 859
750PROTOTYPES: ENABLE 860PROTOTYPES: ENABLE
751 861
752BOOT: 862BOOT:
762 872
763 if (!padlist_cache) 873 if (!padlist_cache)
764 padlist_cache = newHV (); 874 padlist_cache = newHV ();
765 875
766 main_mainstack = PL_mainstack; 876 main_mainstack = PL_mainstack;
877
878 {
879 SV *sv = perl_get_sv("Coro::API", 1);
880
881 coroapi.ver = CORO_API_VERSION - 1;
882 coroapi.transfer = api_transfer;
883 coroapi.schedule = api_schedule;
884 coroapi.ready = api_ready;
885 coroapi.nready = &coro_nready;
886
887 GCoroAPI = &coroapi;
888 sv_setiv(sv, (IV)&coroapi);
889 SvREADONLY_on(sv);
890 }
767} 891}
768 892
769Coro::State 893Coro::State
770_newprocess(args) 894_newprocess(args)
771 SV * args 895 SV * args
785 RETVAL = coro; 909 RETVAL = coro;
786 OUTPUT: 910 OUTPUT:
787 RETVAL 911 RETVAL
788 912
789void 913void
790transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) 914transfer(prev, next, flags)
791 Coro::State_or_hashref prev 915 Coro::State_or_hashref prev
792 Coro::State_or_hashref next 916 Coro::State_or_hashref next
793 int flags 917 int flags
794 PROTOTYPE: @ 918 PROTOTYPE: @
795 CODE: 919 CODE:
920 PUTBACK;
796 transfer (aTHX_ prev, next, flags); 921 transfer (aTHX_ prev, next, flags);
922 SPAGAIN;
797 923
798void 924void
799DESTROY(coro) 925DESTROY(coro)
800 Coro::State coro 926 Coro::State coro
801 CODE: 927 CODE:
823 CODE: 949 CODE:
824#ifdef MAY_FLUSH 950#ifdef MAY_FLUSH
825 flush_padlist_cache (); 951 flush_padlist_cache ();
826#endif 952#endif
827 953
954void
955_exit(code)
956 int code
957 PROTOTYPE: $
958 CODE:
959#if defined(__GLIBC__) || _POSIX_C_SOURCE
960 _exit (code);
961#else
962 signal (SIGTERM, SIG_DFL);
963 raise (SIGTERM);
964 exit (code);
965#endif
966
828MODULE = Coro::State PACKAGE = Coro::Cont 967MODULE = Coro::State PACKAGE = Coro::Cont
829 968
830# this is slightly dirty 969# this is slightly dirty (should expose a c-level api)
831 970
832void 971void
833yield(...) 972yield(...)
834 PROTOTYPE: @ 973 PROTOTYPE: @
835 CODE: 974 CODE:
853 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 992 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
854 SvREFCNT_dec (sv); 993 SvREFCNT_dec (sv);
855 994
856 transfer(aTHX_ prev, next, 0); 995 transfer(aTHX_ prev, next, 0);
857 996
997MODULE = Coro::State PACKAGE = Coro
998
999# this is slightly dirty (should expose a c-level api)
1000
1001BOOT:
1002{
1003 int i;
1004 HV *stash = gv_stashpv ("Coro", TRUE);
1005
1006 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
1007 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1008 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1009 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
1010 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1011 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1012
1013 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1014 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1015
1016 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1017 coro_ready[i] = newAV ();
1018}
1019
1020void
1021ready(self)
1022 SV * self
1023 CODE:
1024 api_ready (self);
1025
1026int
1027nready(...)
1028 PROTOTYPE:
1029 CODE:
1030 RETVAL = coro_nready;
1031 OUTPUT:
1032 RETVAL
1033
1034void
1035schedule(...)
1036 PROTOTYPE:
1037 ALIAS:
1038 cede = 1
1039 CODE:
1040 api_schedule (ix);
1041

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines