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.18 by root, Thu Jul 26 03:46:15 2001 UTC vs.
Revision 1.27 by root, Mon Aug 20 16:58:19 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 */
66 SV *defsv; 57 SV *defsv;
67 SV *errsv; 58 SV *errsv;
68 59
69 /* saved global state not related to stacks */ 60 /* saved global state not related to stacks */
70 U8 dowarn; 61 U8 dowarn;
62 I32 in_eval;
71 63
72 /* the stacks and related info (callchain etc..) */ 64 /* the stacks and related info (callchain etc..) */
73 PERL_SI *curstackinfo; 65 PERL_SI *curstackinfo;
74 AV *curstack; 66 AV *curstack;
75 AV *mainstack; 67 AV *mainstack;
93 I32 savestack_max; 85 I32 savestack_max;
94 OP **retstack; 86 OP **retstack;
95 I32 retstack_ix; 87 I32 retstack_ix;
96 I32 retstack_max; 88 I32 retstack_max;
97 COP *curcop; 89 COP *curcop;
98 JMPENV start_env;
99 JMPENV *top_env; 90 JMPENV *top_env;
100 91
101 /* data associated with this coroutine (initial args) */ 92 /* data associated with this coroutine (initial args) */
102 AV *args; 93 AV *args;
103}; 94};
204 195
205 return newpadlist; 196 return newpadlist;
206} 197}
207 198
208#ifdef MAY_FLUSH 199#ifdef MAY_FLUSH
209STATIC AV * 200STATIC void
210free_padlist (AV *padlist) 201free_padlist (AV *padlist)
211{ 202{
212 /* may be during global destruction */ 203 /* may be during global destruction */
213 if (SvREFCNT(padlist)) 204 if (SvREFCNT(padlist))
214 { 205 {
288 279
289static void 280static void
290load_state(pTHX_ Coro__State c) 281load_state(pTHX_ Coro__State c)
291{ 282{
292 PL_dowarn = c->dowarn; 283 PL_dowarn = c->dowarn;
284 PL_in_eval = c->in_eval;
293 285
294 PL_curstackinfo = c->curstackinfo; 286 PL_curstackinfo = c->curstackinfo;
295 PL_curstack = c->curstack; 287 PL_curstack = c->curstack;
296 PL_mainstack = c->mainstack; 288 PL_mainstack = c->mainstack;
297 PL_stack_sp = c->stack_sp; 289 PL_stack_sp = c->stack_sp;
314 PL_savestack_max = c->savestack_max; 306 PL_savestack_max = c->savestack_max;
315 PL_retstack = c->retstack; 307 PL_retstack = c->retstack;
316 PL_retstack_ix = c->retstack_ix; 308 PL_retstack_ix = c->retstack_ix;
317 PL_retstack_max = c->retstack_max; 309 PL_retstack_max = c->retstack_max;
318 PL_curcop = c->curcop; 310 PL_curcop = c->curcop;
319 PL_start_env = c->start_env;
320 PL_top_env = c->top_env; 311 PL_top_env = c->top_env;
321 312
322 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 313 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
323 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 314 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
324 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 315 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
423 /* 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 */
424 if (c->defav) 415 if (c->defav)
425 av_reify (c->defav); 416 av_reify (c->defav);
426 417
427 c->dowarn = PL_dowarn; 418 c->dowarn = PL_dowarn;
419 c->in_eval = PL_in_eval;
428 420
429 c->curstackinfo = PL_curstackinfo; 421 c->curstackinfo = PL_curstackinfo;
430 c->curstack = PL_curstack; 422 c->curstack = PL_curstack;
431 c->mainstack = PL_mainstack; 423 c->mainstack = PL_mainstack;
432 c->stack_sp = PL_stack_sp; 424 c->stack_sp = PL_stack_sp;
449 c->savestack_max = PL_savestack_max; 441 c->savestack_max = PL_savestack_max;
450 c->retstack = PL_retstack; 442 c->retstack = PL_retstack;
451 c->retstack_ix = PL_retstack_ix; 443 c->retstack_ix = PL_retstack_ix;
452 c->retstack_max = PL_retstack_max; 444 c->retstack_max = PL_retstack_max;
453 c->curcop = PL_curcop; 445 c->curcop = PL_curcop;
454 c->start_env = PL_start_env;
455 c->top_env = PL_top_env; 446 c->top_env = PL_top_env;
456} 447}
457 448
458/* 449/*
459 * allocate various perl stacks. This is an exact copy 450 * allocate various perl stacks. This is an exact copy
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;
603 dSP; 595 dSP;
604 Coro__State ctx = (Coro__State)arg; 596 Coro__State ctx = (Coro__State)arg;
605 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 597 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
606 598
607 coro_init_stacks (aTHX); 599 coro_init_stacks (aTHX);
608 JMPENV_BOOTSTRAP;
609 SPAGAIN;
610
611 /*PL_curcop = 0;*/ 600 /*PL_curcop = 0;*/
601 /*PL_in_eval = PL_in_eval;*/ /* inherit */
612 SvREFCNT_dec (GvAV (PL_defgv)); 602 SvREFCNT_dec (GvAV (PL_defgv));
613 GvAV (PL_defgv) = ctx->args; 603 GvAV (PL_defgv) = ctx->args;
614 604
605 SPAGAIN;
606
615 if (ctx->stack) 607 if (ctx->stack)
616 { 608 {
617 ctx->cursp = 0; 609 ctx->cursp = 0;
618 610
619 PUSHMARK(SP); 611 PUSHMARK(SP);
620 PUTBACK; 612 PUTBACK;
621 (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
622 croak ("FATAL: CCTXT coroutine returned!"); 618 croak ("FATAL: CCTXT coroutine returned!");
623 } 619 }
624 else 620 else
625 { 621 {
626 UNOP myop; 622 UNOP myop;
627 623
654 /* 650 /*
655 * this is a _very_ stripped down perl interpreter ;) 651 * this is a _very_ stripped down perl interpreter ;)
656 */ 652 */
657 Coro__State ctx = (Coro__State)arg; 653 Coro__State ctx = (Coro__State)arg;
658 654
655 /*FIXME*//* must set up top_env here */
659 ctx->cursp = 0; 656 ctx->cursp = 0;
660 PL_op = PL_op->op_next; 657 PL_op = PL_op->op_next;
661 CALLRUNOPS(aTHX); 658 CALLRUNOPS(aTHX);
662 /*NORETURN*/ 659
663 abort (); 660 abort ();
664} 661}
665 662
666STATIC void 663STATIC void
667transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 664transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
741 } 738 }
742 739
743 next->cursp = stacklevel; 740 next->cursp = stacklevel;
744} 741}
745 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
760 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname);
761 /*NORETURN*/
762}
763
764static void
765api_transfer(pTHX_ SV *prev, SV *next, int flags)
766{
767 transfer(aTHX_
768 sv_to_coro (prev, "Coro::transfer", "prev"),
769 sv_to_coro (next, "Coro::transfer", "next"),
770 flags);
771}
772
773/** Coro ********************************************************************/
774
775#define PRIO_MAX 3
776#define PRIO_HIGH 1
777#define PRIO_NORMAL 0
778#define PRIO_LOW -1
779#define PRIO_IDLE -3
780#define PRIO_MIN -4
781
782/* for Coro.pm */
783static GV *coro_current, *coro_idle;
784static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
785static int coro_nready;
786
787static void
788coro_enq (SV *sv)
789{
790 if (SvROK (sv))
791 {
792 SV *hv = SvRV (sv);
793 if (SvTYPE (hv) == SVt_PVHV)
794 {
795 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0);
796 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
797
798 prio = prio > PRIO_MAX ? PRIO_MAX
799 : prio < PRIO_MIN ? PRIO_MIN
800 : prio;
801
802 av_push (coro_ready [prio - PRIO_MIN], sv);
803 coro_nready++;
804
805 return;
806 }
807 }
808
809 croak ("Coro::ready tried to enqueue something that is not a coroutine");
810}
811
812static SV *
813coro_deq (int min_prio)
814{
815 int prio = PRIO_MAX - PRIO_MIN;
816
817 min_prio -= PRIO_MIN;
818 if (min_prio < 0)
819 min_prio = 0;
820
821 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
822 if (av_len (coro_ready[prio]) >= 0)
823 {
824 coro_nready--;
825 return av_shift (coro_ready[prio]);
826 }
827
828 return 0;
829}
830
831static void
832api_ready (SV *coro)
833{
834 coro_enq (SvREFCNT_inc (coro));
835}
836
837static void
838api_schedule (int cede)
839{
840 SV *prev, *next;
841
842 prev = GvSV (coro_current);
843
844 if (cede)
845 coro_enq (SvREFCNT_inc (prev));
846
847 next = coro_deq (PRIO_MIN);
848
849 if (!next)
850 next = SvREFCNT_inc (GvSV (coro_idle));
851
852 GvSV (coro_current) = SvREFCNT_inc (next);
853 transfer (aTHX_
854 sv_to_coro (prev, "Coro::schedule", "current coroutine"),
855 sv_to_coro (next, "Coro::schedule", "next coroutine"),
856 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
857 SvREFCNT_dec (next);
858 SvREFCNT_dec (prev);
859}
860
746MODULE = Coro::State PACKAGE = Coro::State 861MODULE = Coro::State PACKAGE = Coro::State
747 862
748PROTOTYPES: ENABLE 863PROTOTYPES: ENABLE
749 864
750BOOT: 865BOOT:
760 875
761 if (!padlist_cache) 876 if (!padlist_cache)
762 padlist_cache = newHV (); 877 padlist_cache = newHV ();
763 878
764 main_mainstack = PL_mainstack; 879 main_mainstack = PL_mainstack;
880
881 coroapi.ver = CORO_API_VERSION;
882 coroapi.transfer = api_transfer;
765} 883}
766 884
767Coro::State 885Coro::State
768_newprocess(args) 886_newprocess(args)
769 SV * args 887 SV * args
783 RETVAL = coro; 901 RETVAL = coro;
784 OUTPUT: 902 OUTPUT:
785 RETVAL 903 RETVAL
786 904
787void 905void
788transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) 906transfer(prev, next, flags)
789 Coro::State_or_hashref prev 907 Coro::State_or_hashref prev
790 Coro::State_or_hashref next 908 Coro::State_or_hashref next
791 int flags 909 int flags
792 PROTOTYPE: @ 910 PROTOTYPE: @
793 CODE: 911 CODE:
912 PUTBACK;
794 transfer (aTHX_ prev, next, flags); 913 transfer (aTHX_ prev, next, flags);
914 SPAGAIN;
795 915
796void 916void
797DESTROY(coro) 917DESTROY(coro)
798 Coro::State coro 918 Coro::State coro
799 CODE: 919 CODE:
821 CODE: 941 CODE:
822#ifdef MAY_FLUSH 942#ifdef MAY_FLUSH
823 flush_padlist_cache (); 943 flush_padlist_cache ();
824#endif 944#endif
825 945
946void
947_exit(code)
948 int code
949 PROTOTYPE: $
950 CODE:
951#if defined(__GLIBC__) || _POSIX_C_SOURCE
952 _exit (code);
953#else
954 signal (SIGTERM, SIG_DFL);
955 raise (SIGTERM);
956 exit (code);
957#endif
958
826MODULE = Coro::State PACKAGE = Coro::Cont 959MODULE = Coro::State PACKAGE = Coro::Cont
827 960
828# this is slightly dirty 961# this is slightly dirty (should expose a c-level api)
829 962
830void 963void
831yield(...) 964yield(...)
832 PROTOTYPE: @ 965 PROTOTYPE: @
833 CODE: 966 CODE:
851 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 984 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
852 SvREFCNT_dec (sv); 985 SvREFCNT_dec (sv);
853 986
854 transfer(aTHX_ prev, next, 0); 987 transfer(aTHX_ prev, next, 0);
855 988
989MODULE = Coro::State PACKAGE = Coro
990
991# this is slightly dirty (should expose a c-level api)
992
993BOOT:
994{
995 int i;
996 HV *stash = gv_stashpv ("Coro", TRUE);
997
998 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
999 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1000 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1001 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
1002 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1003 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1004
1005 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1006 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1007
1008 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1009 coro_ready[i] = newAV ();
1010
1011 {
1012 SV *sv = perl_get_sv("Coro::API", 1);
1013
1014 coroapi.schedule = api_schedule;
1015 coroapi.ready = api_ready;
1016 coroapi.nready = &coro_nready;
1017 coroapi.current = coro_current;
1018
1019 GCoroAPI = &coroapi;
1020 sv_setiv(sv, (IV)&coroapi);
1021 SvREADONLY_on(sv);
1022 }
1023}
1024
1025void
1026ready(self)
1027 SV * self
1028 CODE:
1029 api_ready (self);
1030
1031int
1032nready(...)
1033 PROTOTYPE:
1034 CODE:
1035 RETVAL = coro_nready;
1036 OUTPUT:
1037 RETVAL
1038
1039void
1040schedule(...)
1041 PROTOTYPE:
1042 ALIAS:
1043 cede = 1
1044 CODE:
1045 api_schedule (ix);
1046

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines