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.23 by root, Tue Aug 14 04:33:58 2001 UTC vs.
Revision 1.36 by root, Fri Oct 5 14:20:10 2001 UTC

7#include <signal.h> 7#include <signal.h>
8 8
9#ifdef HAVE_MMAP 9#ifdef HAVE_MMAP
10# include <unistd.h> 10# include <unistd.h>
11# include <sys/mman.h> 11# include <sys/mman.h>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS 12# ifndef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS 13# ifdef MAP_ANON
14# define MAP_ANONYMOUS MAP_ANON
15# else 15# else
16# undef HAVE_MMAP 16# undef HAVE_MMAP
17# endif 17# endif
18# endif 18# endif
19#endif 19#endif
20 20
21#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize and is rarely used */
22 22
23#define SUB_INIT "Coro::State::initialize" 23#define SUB_INIT "Coro::State::initialize"
24#define UCORO_STATE "_coro_state" 24#define UCORO_STATE "_coro_state"
25 25
26/* 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
27 * to the current C stack pointer. Its property is that it changes with each call 27 * to the current C stack pointer. Its property is that it changes with each call
28 * and should be unique. */ 28 * and should be unique. */
29#define dSTACKLEVEL void *stacklevel = &stacklevel 29#define dSTACKLEVEL void *stacklevel = &stacklevel
30
31#define IN_DESTRUCT (PL_main_cv == Nullcv)
30 32
31#define labs(l) ((l) >= 0 ? (l) : -(l)) 33#define labs(l) ((l) >= 0 ? (l) : -(l))
32 34
33#include "CoroAPI.h" 35#include "CoroAPI.h"
34 36
195 197
196 return newpadlist; 198 return newpadlist;
197} 199}
198 200
199#ifdef MAY_FLUSH 201#ifdef MAY_FLUSH
200STATIC AV * 202STATIC void
201free_padlist (AV *padlist) 203free_padlist (AV *padlist)
202{ 204{
203 /* may be during global destruction */ 205 /* may be during global destruction */
204 if (SvREFCNT(padlist)) 206 if (SvREFCNT(padlist))
205 { 207 {
270#endif 272#endif
271 273
272#define SB do { 274#define SB do {
273#define SE } while (0) 275#define SE } while (0)
274 276
275#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 277#define LOAD(state) load_state(aTHX_ (state));
276#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 278#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
277 279
278#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 280#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE
279 281
280static void 282static void
281load_state(pTHX_ Coro__State c) 283load_state(pTHX_ Coro__State c)
355 357
356 PUSHs (Nullsv); 358 PUSHs (Nullsv);
357 /* this loop was inspired by pp_caller */ 359 /* this loop was inspired by pp_caller */
358 for (;;) 360 for (;;)
359 { 361 {
360 do 362 while (cxix >= 0)
361 { 363 {
362 PERL_CONTEXT *cx = &ccstk[cxix--]; 364 PERL_CONTEXT *cx = &ccstk[cxix--];
363 365
364 if (CxTYPE(cx) == CXt_SUB) 366 if (CxTYPE(cx) == CXt_SUB)
365 { 367 {
391 /* I never used formats, so how should I know how these are implemented? */ 393 /* I never used formats, so how should I know how these are implemented? */
392 /* my bold guess is as a simple, plain sub... */ 394 /* my bold guess is as a simple, plain sub... */
393 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 395 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
394 } 396 }
395 } 397 }
396 while (cxix >= 0);
397 398
398 if (top_si->si_type == PERLSI_MAIN) 399 if (top_si->si_type == PERLSI_MAIN)
399 break; 400 break;
400 401
401 top_si = top_si->si_prev; 402 top_si = top_si->si_prev;
407 } 408 }
408 409
409 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 410 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
410 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 411 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
411 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 412 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
412
413 /* I have not the slightest idea of why av_reify is necessary */
414 /* but if it's missing the defav contents magically get replaced sometimes */
415 if (c->defav)
416 av_reify (c->defav);
417 413
418 c->dowarn = PL_dowarn; 414 c->dowarn = PL_dowarn;
419 c->in_eval = PL_in_eval; 415 c->in_eval = PL_in_eval;
420 416
421 c->curstackinfo = PL_curstackinfo; 417 c->curstackinfo = PL_curstackinfo;
493 * still there is a memleak of 128 bytes... 489 * still there is a memleak of 128 bytes...
494 */ 490 */
495STATIC void 491STATIC void
496destroy_stacks(pTHX) 492destroy_stacks(pTHX)
497{ 493{
494 if (!IN_DESTRUCT)
495 {
498 /* is this ugly, I ask? */ 496 /* is this ugly, I ask? */
499 while (PL_scopestack_ix) 497 while (PL_scopestack_ix)
500 LEAVE; 498 LEAVE;
501 499
502 /* sure it is, but more important: is it correct?? :/ */ 500 /* sure it is, but more important: is it correct?? :/ */
503 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ 501 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
504 FREETMPS; 502 FREETMPS;
503 }
505 504
506 while (PL_curstackinfo->si_next) 505 while (PL_curstackinfo->si_next)
507 PL_curstackinfo = PL_curstackinfo->si_next; 506 PL_curstackinfo = PL_curstackinfo->si_next;
508 507
509 while (PL_curstackinfo) 508 while (PL_curstackinfo)
514 dSP; 513 dSP;
515 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 514 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
516 PUTBACK; /* possibly superfluous */ 515 PUTBACK; /* possibly superfluous */
517 } 516 }
518 517
518 if (!IN_DESTRUCT)
519 {
519 dounwind(-1); 520 dounwind(-1);
520
521 SvREFCNT_dec(PL_curstackinfo->si_stack); 521 SvREFCNT_dec(PL_curstackinfo->si_stack);
522 }
523
522 Safefree(PL_curstackinfo->si_cxstack); 524 Safefree(PL_curstackinfo->si_cxstack);
523 Safefree(PL_curstackinfo); 525 Safefree(PL_curstackinfo);
524 PL_curstackinfo = p; 526 PL_curstackinfo = p;
525 } 527 }
526 528
543 stack->gencnt = ctx->gencnt = 0; 545 stack->gencnt = ctx->gencnt = 0;
544 if (alloc) 546 if (alloc)
545 { 547 {
546#ifdef HAVE_MMAP 548#ifdef HAVE_MMAP
547 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 549 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
548 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 550 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
549 if (stack->sptr == (void *)-1) 551 if (stack->sptr == (void *)-1)
550#endif 552#endif
551 { 553 {
552 /*FIXME*//*D*//* reasonable stack size! */ 554 /*FIXME*//*D*//* reasonable stack size! */
553 stack->ssize = -4096 * sizeof (long); 555 stack->ssize = -4096 * sizeof (long);
648{ 650{
649 /* 651 /*
650 * this is a _very_ stripped down perl interpreter ;) 652 * this is a _very_ stripped down perl interpreter ;)
651 */ 653 */
652 Coro__State ctx = (Coro__State)arg; 654 Coro__State ctx = (Coro__State)arg;
655 JMPENV coro_start_env;
653 656
654 /*FIXME*//* must set up top_env here */ 657 /* same as JMPENV_BOOTSTRAP */
658 Zero(&coro_start_env, 1, JMPENV);
659 coro_start_env.je_ret = -1;
660 coro_start_env.je_mustcatch = TRUE;
661 PL_top_env = &coro_start_env;
662
655 ctx->cursp = 0; 663 ctx->cursp = 0;
656 PL_op = PL_op->op_next; 664 PL_op = PL_op->op_next;
657 CALLRUNOPS(aTHX); 665 CALLRUNOPS(aTHX);
658 666
659 abort (); 667 abort ();
660} 668}
661 669
662STATIC void 670STATIC void
663transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 671transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
664{ 672{
665 dSP;
666 dSTACKLEVEL; 673 dSTACKLEVEL;
674 static struct coro *xnext;
667 675
668 if (prev != next) 676 if (prev != next)
669 { 677 {
678 xnext = next;
679
670 if (next->mainstack) 680 if (next->mainstack)
671 { 681 {
672 SAVE (prev, flags); 682 SAVE (prev, flags);
673 LOAD (next); 683 LOAD (next);
674 684
697 continue_coro, (void *)next, 707 continue_coro, (void *)next,
698 next->stack->sptr, labs (next->stack->ssize)); 708 next->stack->sptr, labs (next->stack->ssize));
699 } 709 }
700 710
701 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 711 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
712 /* don't add any code here */
702 } 713 }
703 714
704 } 715 }
705 else if (next->tmps_ix == -2) 716 else if (next->tmps_ix == -2)
706 croak ("tried to transfer to running coroutine"); 717 croak ("tried to transfer to running coroutine");
727 allocate_stack (next, 1); 738 allocate_stack (next, 1);
728 coro_create (&(next->stack->cctx), 739 coro_create (&(next->stack->cctx),
729 setup_coro, (void *)next, 740 setup_coro, (void *)next,
730 next->stack->sptr, labs (next->stack->ssize)); 741 next->stack->sptr, labs (next->stack->ssize));
731 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
743 /* don't add any code here */
732 } 744 }
733 } 745 }
734 else 746 else
735 setup_coro (next); 747 setup_coro (next);
736 } 748 }
737 }
738 749
750 /*
751 * xnext is now either prev or next, depending on wether
752 * we switched the c stack or not. that's why i use a global
753 * variable, that should become thread-specific at one point.
754 */
739 next->cursp = stacklevel; 755 xnext->cursp = stacklevel;
756 }
740} 757}
741 758
742static struct coro * 759static struct coro *
743sv_to_coro (SV *arg, const char *funcname, const char *varname) 760sv_to_coro (SV *arg, const char *funcname, const char *varname)
744{ 761{
751 768
752 arg = HeVAL(he); 769 arg = HeVAL(he);
753 } 770 }
754 771
755 /* must also be changed inside Coro::Cont::yield */ 772 /* must also be changed inside Coro::Cont::yield */
773 if (SvROK(arg) && SvOBJECT(SvRV(arg))
756 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 774 && SvSTASH(SvRV(arg)) == coro_state_stash)
757 return (struct coro *) SvIV((SV*)SvRV(arg)); 775 return (struct coro *) SvIV((SV*)SvRV(arg));
758 else 776
759 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 777 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname);
778 /*NORETURN*/
760} 779}
761 780
762static void 781static void
763api_transfer(pTHX_ SV *prev, SV *next, int flags) 782api_transfer(pTHX_ SV *prev, SV *next, int flags)
764{ 783{
784 transfer(aTHX_
765 transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"), 785 sv_to_coro (prev, "Coro::transfer", "prev"),
766 sv_to_coro (next, "Coro::transfer", "next"), 786 sv_to_coro (next, "Coro::transfer", "next"),
767 flags); 787 flags);
768} 788}
769 789
770/** Coro ********************************************************************/ 790/** Coro ********************************************************************/
771 791
772#define PRIO_MAX 3 792#define PRIO_MAX 3
777#define PRIO_MIN -4 797#define PRIO_MIN -4
778 798
779/* for Coro.pm */ 799/* for Coro.pm */
780static GV *coro_current, *coro_idle; 800static GV *coro_current, *coro_idle;
781static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 801static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
802static int coro_nready;
782 803
783static void 804static void
784coro_enq (SV *sv) 805coro_enq (SV *sv)
785{ 806{
786 if (SvROK (sv)) 807 if (SvROK (sv))
794 prio = prio > PRIO_MAX ? PRIO_MAX 815 prio = prio > PRIO_MAX ? PRIO_MAX
795 : prio < PRIO_MIN ? PRIO_MIN 816 : prio < PRIO_MIN ? PRIO_MIN
796 : prio; 817 : prio;
797 818
798 av_push (coro_ready [prio - PRIO_MIN], sv); 819 av_push (coro_ready [prio - PRIO_MIN], sv);
820 coro_nready++;
799 821
800 return; 822 return;
801 } 823 }
802 } 824 }
803 825
813 if (min_prio < 0) 835 if (min_prio < 0)
814 min_prio = 0; 836 min_prio = 0;
815 837
816 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 838 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
817 if (av_len (coro_ready[prio]) >= 0) 839 if (av_len (coro_ready[prio]) >= 0)
840 {
841 coro_nready--;
818 return av_shift (coro_ready[prio]); 842 return av_shift (coro_ready[prio]);
843 }
819 844
820 return 0; 845 return 0;
821} 846}
822 847
823static void 848static void
840 865
841 if (!next) 866 if (!next)
842 next = SvREFCNT_inc (GvSV (coro_idle)); 867 next = SvREFCNT_inc (GvSV (coro_idle));
843 868
844 GvSV (coro_current) = SvREFCNT_inc (next); 869 GvSV (coro_current) = SvREFCNT_inc (next);
870 transfer (aTHX_
845 transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), 871 sv_to_coro (prev, "Coro::schedule", "current coroutine"),
846 sv_to_coro (next, "Coro::schedule", "next coroutine"), 872 sv_to_coro (next, "Coro::schedule", "next coroutine"),
847 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 873 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
848 SvREFCNT_dec (next); 874 SvREFCNT_dec (next);
849 SvREFCNT_dec (prev); 875 SvREFCNT_dec (prev);
850} 876}
867 if (!padlist_cache) 893 if (!padlist_cache)
868 padlist_cache = newHV (); 894 padlist_cache = newHV ();
869 895
870 main_mainstack = PL_mainstack; 896 main_mainstack = PL_mainstack;
871 897
872 {
873 SV *sv = perl_get_sv("Coro::API", 1);
874
875 coroapi.ver = CORO_API_VERSION - 1; 898 coroapi.ver = CORO_API_VERSION;
876 coroapi.transfer = api_transfer; 899 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 }
884} 900}
885 901
886Coro::State 902Coro::State
887_newprocess(args) 903_newprocess(args)
888 SV * args 904 SV * args
921 937
922 if (coro->mainstack && coro->mainstack != main_mainstack) 938 if (coro->mainstack && coro->mainstack != main_mainstack)
923 { 939 {
924 struct coro temp; 940 struct coro temp;
925 941
942 PUTBACK;
926 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 943 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
927 LOAD(aTHX_ coro); 944 LOAD(aTHX_ coro);
945 SPAGAIN;
928 946
929 destroy_stacks (aTHX); 947 destroy_stacks (aTHX);
930 948
931 LOAD((&temp)); /* this will get rid of defsv etc.. */ 949 LOAD((&temp)); /* this will get rid of defsv etc.. */
950 SPAGAIN;
932 951
933 coro->mainstack = 0; 952 coro->mainstack = 0;
934 } 953 }
935 954
936 deallocate_stack (coro); 955 deallocate_stack (coro);
1006 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1025 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1007 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1026 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1008 1027
1009 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1028 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1010 coro_ready[i] = newAV (); 1029 coro_ready[i] = newAV ();
1030
1031 {
1032 SV *sv = perl_get_sv("Coro::API", 1);
1033
1034 coroapi.schedule = api_schedule;
1035 coroapi.ready = api_ready;
1036 coroapi.nready = &coro_nready;
1037 coroapi.current = coro_current;
1038
1039 GCoroAPI = &coroapi;
1040 sv_setiv(sv, (IV)&coroapi);
1041 SvREADONLY_on(sv);
1042 }
1011} 1043}
1012 1044
1013void 1045void
1014ready(self) 1046ready(self)
1015 SV * self 1047 SV * self
1048 PROTOTYPE: $
1016 CODE: 1049 CODE:
1017 api_ready (self); 1050 api_ready (self);
1018 1051
1052int
1053nready(...)
1054 PROTOTYPE:
1055 CODE:
1056 RETVAL = coro_nready;
1057 OUTPUT:
1058 RETVAL
1059
1019void 1060void
1020schedule(...) 1061schedule(...)
1062 PROTOTYPE:
1021 ALIAS: 1063 ALIAS:
1022 cede = 1 1064 cede = 1
1023 CODE: 1065 CODE:
1024 api_schedule (ix); 1066 api_schedule (ix);
1025 1067

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines