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.30 by root, Sat Aug 25 22:19:05 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
14# undef HAVE_MMAP 16# undef HAVE_MMAP
15# endif 17# endif
16# endif 18# endif
17#endif 19#endif
18 20
19#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize and is rarely used */
20
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 22
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 declare 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 */
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 {
279#endif 270#endif
280 271
281#define SB do { 272#define SB do {
282#define SE } while (0) 273#define SE } while (0)
283 274
284#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 275#define LOAD(state) load_state(aTHX_ (state));
285#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 276#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
286 277
287#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 278#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE
288 279
289static void 280static void
290load_state(pTHX_ Coro__State c) 281load_state(pTHX_ Coro__State c)
364 355
365 PUSHs (Nullsv); 356 PUSHs (Nullsv);
366 /* this loop was inspired by pp_caller */ 357 /* this loop was inspired by pp_caller */
367 for (;;) 358 for (;;)
368 { 359 {
369 do 360 while (cxix >= 0)
370 { 361 {
371 PERL_CONTEXT *cx = &ccstk[cxix--]; 362 PERL_CONTEXT *cx = &ccstk[cxix--];
372 363
373 if (CxTYPE(cx) == CXt_SUB) 364 if (CxTYPE(cx) == CXt_SUB)
374 { 365 {
400 /* I never used formats, so how should I know how these are implemented? */ 391 /* I never used formats, so how should I know how these are implemented? */
401 /* my bold guess is as a simple, plain sub... */ 392 /* my bold guess is as a simple, plain sub... */
402 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 393 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
403 } 394 }
404 } 395 }
405 while (cxix >= 0);
406 396
407 if (top_si->si_type == PERLSI_MAIN) 397 if (top_si->si_type == PERLSI_MAIN)
408 break; 398 break;
409 399
410 top_si = top_si->si_prev; 400 top_si = top_si->si_prev;
416 } 406 }
417 407
418 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 408 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
419 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 409 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
420 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 410 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
421
422 /* I have not the slightest idea of why av_reify is necessary */
423 /* but if it's missing the defav contents magically get replaced sometimes */
424 if (c->defav)
425 av_reify (c->defav);
426 411
427 c->dowarn = PL_dowarn; 412 c->dowarn = PL_dowarn;
428 c->in_eval = PL_in_eval; 413 c->in_eval = PL_in_eval;
429 414
430 c->curstackinfo = PL_curstackinfo; 415 c->curstackinfo = PL_curstackinfo;
523 dSP; 508 dSP;
524 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 509 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
525 PUTBACK; /* possibly superfluous */ 510 PUTBACK; /* possibly superfluous */
526 } 511 }
527 512
513 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */
528 dounwind(-1); 514 dounwind(-1);
529 515
530 SvREFCNT_dec(PL_curstackinfo->si_stack); 516 SvREFCNT_dec(PL_curstackinfo->si_stack);
531 Safefree(PL_curstackinfo->si_cxstack); 517 Safefree(PL_curstackinfo->si_cxstack);
532 Safefree(PL_curstackinfo); 518 Safefree(PL_curstackinfo);
533 PL_curstackinfo = p; 519 PL_curstackinfo = p;
669} 655}
670 656
671STATIC void 657STATIC void
672transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 658transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
673{ 659{
674 dSP;
675 dSTACKLEVEL; 660 dSTACKLEVEL;
661 static struct coro *xnext;
676 662
677 if (prev != next) 663 if (prev != next)
678 { 664 {
665 xnext = next;
666
679 if (next->mainstack) 667 if (next->mainstack)
680 { 668 {
681 SAVE (prev, flags); 669 SAVE (prev, flags);
682 LOAD (next); 670 LOAD (next);
683 671
706 continue_coro, (void *)next, 694 continue_coro, (void *)next,
707 next->stack->sptr, labs (next->stack->ssize)); 695 next->stack->sptr, labs (next->stack->ssize));
708 } 696 }
709 697
710 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 698 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
699 /* don't add any code here */
711 } 700 }
712 701
713 } 702 }
714 else if (next->tmps_ix == -2) 703 else if (next->tmps_ix == -2)
715 croak ("tried to transfer to running coroutine"); 704 croak ("tried to transfer to running coroutine");
736 allocate_stack (next, 1); 725 allocate_stack (next, 1);
737 coro_create (&(next->stack->cctx), 726 coro_create (&(next->stack->cctx),
738 setup_coro, (void *)next, 727 setup_coro, (void *)next,
739 next->stack->sptr, labs (next->stack->ssize)); 728 next->stack->sptr, labs (next->stack->ssize));
740 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 729 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
730 /* don't add any code here */
741 } 731 }
742 } 732 }
743 else 733 else
744 setup_coro (next); 734 setup_coro (next);
745 } 735 }
746 } 736 }
747 737
738 /*
739 * xnext is now either prev or next, depending on wether
740 * we switched the c stack or not. that's why i use a global
741 * variable, that should become thread-specific at one point.
742 */
748 next->cursp = stacklevel; 743 xnext->cursp = stacklevel;
744}
745
746static struct coro *
747sv_to_coro (SV *arg, const char *funcname, const char *varname)
748{
749 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
750 {
751 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash);
752
753 if (!he)
754 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname);
755
756 arg = HeVAL(he);
757 }
758
759 /* must also be changed inside Coro::Cont::yield */
760 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash)
761 return (struct coro *) SvIV((SV*)SvRV(arg));
762
763 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname);
764 /*NORETURN*/
765}
766
767static void
768api_transfer(pTHX_ SV *prev, SV *next, int flags)
769{
770 transfer(aTHX_
771 sv_to_coro (prev, "Coro::transfer", "prev"),
772 sv_to_coro (next, "Coro::transfer", "next"),
773 flags);
774}
775
776/** Coro ********************************************************************/
777
778#define PRIO_MAX 3
779#define PRIO_HIGH 1
780#define PRIO_NORMAL 0
781#define PRIO_LOW -1
782#define PRIO_IDLE -3
783#define PRIO_MIN -4
784
785/* for Coro.pm */
786static GV *coro_current, *coro_idle;
787static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
788static int coro_nready;
789
790static void
791coro_enq (SV *sv)
792{
793 if (SvROK (sv))
794 {
795 SV *hv = SvRV (sv);
796 if (SvTYPE (hv) == SVt_PVHV)
797 {
798 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0);
799 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
800
801 prio = prio > PRIO_MAX ? PRIO_MAX
802 : prio < PRIO_MIN ? PRIO_MIN
803 : prio;
804
805 av_push (coro_ready [prio - PRIO_MIN], sv);
806 coro_nready++;
807
808 return;
809 }
810 }
811
812 croak ("Coro::ready tried to enqueue something that is not a coroutine");
813}
814
815static SV *
816coro_deq (int min_prio)
817{
818 int prio = PRIO_MAX - PRIO_MIN;
819
820 min_prio -= PRIO_MIN;
821 if (min_prio < 0)
822 min_prio = 0;
823
824 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
825 if (av_len (coro_ready[prio]) >= 0)
826 {
827 coro_nready--;
828 return av_shift (coro_ready[prio]);
829 }
830
831 return 0;
832}
833
834static void
835api_ready (SV *coro)
836{
837 coro_enq (SvREFCNT_inc (coro));
838}
839
840static void
841api_schedule (int cede)
842{
843 SV *prev, *next;
844
845 prev = GvSV (coro_current);
846
847 if (cede)
848 coro_enq (SvREFCNT_inc (prev));
849
850 next = coro_deq (PRIO_MIN);
851
852 if (!next)
853 next = SvREFCNT_inc (GvSV (coro_idle));
854
855 GvSV (coro_current) = SvREFCNT_inc (next);
856 transfer (aTHX_
857 sv_to_coro (prev, "Coro::schedule", "current coroutine"),
858 sv_to_coro (next, "Coro::schedule", "next coroutine"),
859 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
860 SvREFCNT_dec (next);
861 SvREFCNT_dec (prev);
749} 862}
750 863
751MODULE = Coro::State PACKAGE = Coro::State 864MODULE = Coro::State PACKAGE = Coro::State
752 865
753PROTOTYPES: ENABLE 866PROTOTYPES: ENABLE
765 878
766 if (!padlist_cache) 879 if (!padlist_cache)
767 padlist_cache = newHV (); 880 padlist_cache = newHV ();
768 881
769 main_mainstack = PL_mainstack; 882 main_mainstack = PL_mainstack;
883
884 coroapi.ver = CORO_API_VERSION;
885 coroapi.transfer = api_transfer;
770} 886}
771 887
772Coro::State 888Coro::State
773_newprocess(args) 889_newprocess(args)
774 SV * args 890 SV * args
788 RETVAL = coro; 904 RETVAL = coro;
789 OUTPUT: 905 OUTPUT:
790 RETVAL 906 RETVAL
791 907
792void 908void
793transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) 909transfer(prev, next, flags)
794 Coro::State_or_hashref prev 910 Coro::State_or_hashref prev
795 Coro::State_or_hashref next 911 Coro::State_or_hashref next
796 int flags 912 int flags
797 PROTOTYPE: @ 913 PROTOTYPE: @
798 CODE: 914 CODE:
915 PUTBACK;
799 transfer (aTHX_ prev, next, flags); 916 transfer (aTHX_ prev, next, flags);
917 SPAGAIN;
800 918
801void 919void
802DESTROY(coro) 920DESTROY(coro)
803 Coro::State coro 921 Coro::State coro
804 CODE: 922 CODE:
805 923
806 if (coro->mainstack && coro->mainstack != main_mainstack) 924 if (coro->mainstack && coro->mainstack != main_mainstack)
807 { 925 {
808 struct coro temp; 926 struct coro temp;
809 927
928 PUTBACK;
810 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 929 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
811 LOAD(aTHX_ coro); 930 LOAD(aTHX_ coro);
931 SPAGAIN;
812 932
813 destroy_stacks (aTHX); 933 destroy_stacks (aTHX);
814 934
815 LOAD((&temp)); /* this will get rid of defsv etc.. */ 935 LOAD((&temp)); /* this will get rid of defsv etc.. */
936 SPAGAIN;
816 937
817 coro->mainstack = 0; 938 coro->mainstack = 0;
818 } 939 }
819 940
820 deallocate_stack (coro); 941 deallocate_stack (coro);
826 CODE: 947 CODE:
827#ifdef MAY_FLUSH 948#ifdef MAY_FLUSH
828 flush_padlist_cache (); 949 flush_padlist_cache ();
829#endif 950#endif
830 951
952void
953_exit(code)
954 int code
955 PROTOTYPE: $
956 CODE:
957#if defined(__GLIBC__) || _POSIX_C_SOURCE
958 _exit (code);
959#else
960 signal (SIGTERM, SIG_DFL);
961 raise (SIGTERM);
962 exit (code);
963#endif
964
831MODULE = Coro::State PACKAGE = Coro::Cont 965MODULE = Coro::State PACKAGE = Coro::Cont
832 966
833# this is slightly dirty 967# this is slightly dirty (should expose a c-level api)
834 968
835void 969void
836yield(...) 970yield(...)
837 PROTOTYPE: @ 971 PROTOTYPE: @
838 CODE: 972 CODE:
856 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 990 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
857 SvREFCNT_dec (sv); 991 SvREFCNT_dec (sv);
858 992
859 transfer(aTHX_ prev, next, 0); 993 transfer(aTHX_ prev, next, 0);
860 994
995MODULE = Coro::State PACKAGE = Coro
996
997# this is slightly dirty (should expose a c-level api)
998
999BOOT:
1000{
1001 int i;
1002 HV *stash = gv_stashpv ("Coro", TRUE);
1003
1004 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
1005 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1006 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1007 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
1008 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1009 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1010
1011 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1012 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1013
1014 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1015 coro_ready[i] = newAV ();
1016
1017 {
1018 SV *sv = perl_get_sv("Coro::API", 1);
1019
1020 coroapi.schedule = api_schedule;
1021 coroapi.ready = api_ready;
1022 coroapi.nready = &coro_nready;
1023 coroapi.current = coro_current;
1024
1025 GCoroAPI = &coroapi;
1026 sv_setiv(sv, (IV)&coroapi);
1027 SvREADONLY_on(sv);
1028 }
1029}
1030
1031void
1032ready(self)
1033 SV * self
1034 CODE:
1035 api_ready (self);
1036
1037int
1038nready(...)
1039 PROTOTYPE:
1040 CODE:
1041 RETVAL = coro_nready;
1042 OUTPUT:
1043 RETVAL
1044
1045void
1046schedule(...)
1047 PROTOTYPE:
1048 ALIAS:
1049 cede = 1
1050 CODE:
1051 api_schedule (ix);
1052

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines