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.134 by root, Thu Jan 4 23:49:27 2007 UTC vs.
Revision 1.144 by root, Sun Mar 4 11:45:23 2007 UTC

31#else 31#else
32# define PAGESIZE 0 32# define PAGESIZE 0
33# define BOOT_PAGESIZE (void)0 33# define BOOT_PAGESIZE (void)0
34#endif 34#endif
35 35
36#if USE_VALGRIND 36#if CORO_USE_VALGRIND
37# include <valgrind/valgrind.h> 37# include <valgrind/valgrind.h>
38# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
39#else
40# define REGISTER_STACK(cctx,start,end)
38#endif 41#endif
39 42
40/* the maximum number of idle cctx that will be pooled */ 43/* the maximum number of idle cctx that will be pooled */
41#define MAX_IDLE_CCTX 8 44#define MAX_IDLE_CCTX 8
42 45
71#ifndef SvRV_set 74#ifndef SvRV_set
72# define SvRV_set(s,v) SvRV(s) = (v) 75# define SvRV_set(s,v) SvRV(s) = (v)
73#endif 76#endif
74 77
75#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 78#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
76# undef STACKGUARD 79# undef CORO_STACKGUARD
77#endif 80#endif
78 81
79#ifndef STACKGUARD 82#ifndef CORO_STACKGUARD
80# define STACKGUARD 0 83# define CORO_STACKGUARD 0
81#endif 84#endif
82 85
83/* prefer perl internal functions over our own? */ 86/* prefer perl internal functions over our own? */
84#ifndef PREFER_PERL_FUNCTIONS 87#ifndef CORO_PREFER_PERL_FUNCTIONS
85# define PREFER_PERL_FUNCTIONS 0 88# define CORO_PREFER_PERL_FUNCTIONS 0
86#endif 89#endif
87 90
88/* The next macro should declare a variable stacklevel that contains and approximation 91/* The next macro should declare a variable stacklevel that contains and approximation
89 * to the current C stack pointer. Its property is that it changes with each call 92 * to the current C stack pointer. Its property is that it changes with each call
90 * and should be unique. */ 93 * and should be unique. */
112#else 115#else
113# define LOCK (void)0 116# define LOCK (void)0
114# define UNLOCK (void)0 117# define UNLOCK (void)0
115#endif 118#endif
116 119
120/* helper storage struct for Coro::AIO */
117struct io_state 121struct io_state
118{ 122{
119 int errorno; 123 int errorno;
120 I32 laststype; 124 I32 laststype;
121 int laststatval; 125 int laststatval;
134typedef struct coro_cctx { 138typedef struct coro_cctx {
135 struct coro_cctx *next; 139 struct coro_cctx *next;
136 140
137 /* the stack */ 141 /* the stack */
138 void *sptr; 142 void *sptr;
139 long ssize; /* positive == mmap, otherwise malloc */ 143 ssize_t ssize; /* positive == mmap, otherwise malloc */
140 144
141 /* cpu state */ 145 /* cpu state */
142 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 146 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
143 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 147 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
144 JMPENV *top_env; 148 JMPENV *top_env;
145 coro_context cctx; 149 coro_context cctx;
146 150
147 int inuse; 151 int inuse;
148 152
149#if USE_VALGRIND 153#if CORO_USE_VALGRIND
150 int valgrind_id; 154 int valgrind_id;
151#endif 155#endif
152} coro_cctx; 156} coro_cctx;
153 157
154enum { 158enum {
185}; 189};
186 190
187typedef struct coro *Coro__State; 191typedef struct coro *Coro__State;
188typedef struct coro *Coro__State_or_hashref; 192typedef struct coro *Coro__State_or_hashref;
189 193
194/** Coro ********************************************************************/
195
196#define PRIO_MAX 3
197#define PRIO_HIGH 1
198#define PRIO_NORMAL 0
199#define PRIO_LOW -1
200#define PRIO_IDLE -3
201#define PRIO_MIN -4
202
203/* for Coro.pm */
204static SV *coro_current;
205static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
206static int coro_nready;
207
208/** lowlevel stuff **********************************************************/
209
190static AV * 210static AV *
191coro_clone_padlist (CV *cv) 211coro_clone_padlist (CV *cv)
192{ 212{
193 AV *padlist = CvPADLIST (cv); 213 AV *padlist = CvPADLIST (cv);
194 AV *newpadlist, *newpad; 214 AV *newpadlist, *newpad;
268 288
269 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
270 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 290 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
271 else 291 else
272 { 292 {
273#if PREFER_PERL_FUNCTIONS 293#if CORO_PREFER_PERL_FUNCTIONS
274 /* this is probably cleaner, but also slower? */ 294 /* this is probably cleaner, but also slower? */
275 CV *cp = Perl_cv_clone (cv); 295 CV *cp = Perl_cv_clone (cv);
276 CvPADLIST (cv) = CvPADLIST (cp); 296 CvPADLIST (cv) = CvPADLIST (cp);
277 CvPADLIST (cp) = 0; 297 CvPADLIST (cp) = 0;
278 SvREFCNT_dec (cp); 298 SvREFCNT_dec (cp);
301 if (AvFILLp (av) >= AvMAX (av)) 321 if (AvFILLp (av) >= AvMAX (av))
302 av_extend (av, AvMAX (av) + 1); 322 av_extend (av, AvMAX (av) + 1);
303 323
304 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 324 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
305} 325}
326
327/** load & save, init *******************************************************/
306 328
307#define SB do { 329#define SB do {
308#define SE } while (0) 330#define SE } while (0)
309 331
310#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
343 CvPADLIST (cv) = (AV *)POPs; 365 CvPADLIST (cv) = (AV *)POPs;
344 } 366 }
345 367
346 PUTBACK; 368 PUTBACK;
347 } 369 }
370 assert (!PL_comppad || AvARRAY (PL_comppad));//D
348} 371}
349 372
350static void 373static void
351save_perl (Coro__State c) 374save_perl (Coro__State c)
352{ 375{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D
353 { 377 {
354 dSP; 378 dSP;
355 I32 cxix = cxstack_ix; 379 I32 cxix = cxstack_ix;
356 PERL_CONTEXT *ccstk = cxstack; 380 PERL_CONTEXT *ccstk = cxstack;
357 PERL_SI *top_si = PL_curstackinfo; 381 PERL_SI *top_si = PL_curstackinfo;
412 * allocate various perl stacks. This is an exact copy 436 * allocate various perl stacks. This is an exact copy
413 * of perl.c:init_stacks, except that it uses less memory 437 * of perl.c:init_stacks, except that it uses less memory
414 * on the (sometimes correct) assumption that coroutines do 438 * on the (sometimes correct) assumption that coroutines do
415 * not usually need a lot of stackspace. 439 * not usually need a lot of stackspace.
416 */ 440 */
417#if PREFER_PERL_FUNCTIONS 441#if CORO_PREFER_PERL_FUNCTIONS
418# define coro_init_stacks init_stacks 442# define coro_init_stacks init_stacks
419#else 443#else
420static void 444static void
421coro_init_stacks () 445coro_init_stacks ()
422{ 446{
503#if !PERL_VERSION_ATLEAST (5,9,0) 527#if !PERL_VERSION_ATLEAST (5,9,0)
504 Safefree (PL_retstack); 528 Safefree (PL_retstack);
505#endif 529#endif
506} 530}
507 531
532/** coroutine stack handling ************************************************/
533
508static void 534static void
509setup_coro (struct coro *coro) 535setup_coro (struct coro *coro)
510{ 536{
511 /* 537 /*
512 * emulate part of the perl startup here. 538 * emulate part of the perl startup here.
514 540
515 coro_init_stacks (); 541 coro_init_stacks ();
516 542
517 PL_curcop = &PL_compiling; 543 PL_curcop = &PL_compiling;
518 PL_in_eval = EVAL_NULL; 544 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0;
519 PL_curpm = 0; 546 PL_curpm = 0;
520 PL_localizing = 0; 547 PL_localizing = 0;
521 PL_dirty = 0; 548 PL_dirty = 0;
522 PL_restartop = 0; 549 PL_restartop = 0;
523 550
605 632
606 Newz (0, cctx, 1, coro_cctx); 633 Newz (0, cctx, 1, coro_cctx);
607 634
608#if HAVE_MMAP 635#if HAVE_MMAP
609 636
610 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 637 cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
611 /* mmap supposedly does allocate-on-write for us */ 638 /* mmap supposedly does allocate-on-write for us */
612 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 639 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
613 640
614 if (cctx->sptr == (void *)-1) 641 if (cctx->sptr != (void *)-1)
615 {
616 perror ("FATAL: unable to mmap stack for coroutine");
617 _exit (EXIT_FAILURE);
618 } 642 {
619
620# if STACKGUARD 643# if CORO_STACKGUARD
621 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 644 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
622# endif 645# endif
623 646 REGISTER_STACK (
624#else 647 cctx,
625
626 cctx->ssize = STACKSIZE * (long)sizeof (long);
627 New (0, cctx->sptr, STACKSIZE, long);
628
629 if (!cctx->sptr)
630 {
631 perror ("FATAL: unable to malloc stack for coroutine");
632 _exit (EXIT_FAILURE);
633 }
634
635#endif
636
637#if USE_VALGRIND
638 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
639 STACKGUARD * PAGESIZE + (char *)cctx->sptr, 648 CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr,
640 cctx->ssize + (char *)cctx->sptr 649 cctx->ssize + (char *)cctx->sptr
641 ); 650 );
642#endif
643 651
644 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); 652 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
653 }
654 else
655#endif
656 {
657 cctx->ssize = -CORO_STACKSIZE * (long)sizeof (long);
658 New (0, cctx->sptr, CORO_STACKSIZE, long);
659
660 if (!cctx->sptr)
661 {
662 perror ("FATAL: unable to allocate stack for coroutine");
663 _exit (EXIT_FAILURE);
664 }
665
666 REGISTER_STACK (
667 cctx,
668 (char *)cctx->sptr,
669 (char *)cctx->sptr - cctx->ssize
670 );
671
672 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize);
673 }
645 674
646 return cctx; 675 return cctx;
647} 676}
648 677
649static void 678static void
652 if (!cctx) 681 if (!cctx)
653 return; 682 return;
654 683
655 --cctx_count; 684 --cctx_count;
656 685
657#if USE_VALGRIND 686#if CORO_USE_VALGRIND
658 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 687 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
659#endif 688#endif
660 689
661#if HAVE_MMAP 690#if HAVE_MMAP
691 if (cctx->ssize > 0)
662 munmap (cctx->sptr, cctx->ssize); 692 munmap (cctx->sptr, cctx->ssize);
663#else 693 else
694#endif
664 Safefree (cctx->sptr); 695 Safefree (cctx->sptr);
665#endif
666 696
667 Safefree (cctx); 697 Safefree (cctx);
668} 698}
669 699
670static coro_cctx * 700static coro_cctx *
703 733
704 ++cctx_idle; 734 ++cctx_idle;
705 cctx->next = cctx_first; 735 cctx->next = cctx_first;
706 cctx_first = cctx; 736 cctx_first = cctx;
707} 737}
738
739/** coroutine switching *****************************************************/
708 740
709/* never call directly, always through the coro_state_transfer global variable */ 741/* never call directly, always through the coro_state_transfer global variable */
710static void NOINLINE 742static void NOINLINE
711transfer (struct coro *prev, struct coro *next) 743transfer (struct coro *prev, struct coro *next)
712{ 744{
802 struct coro *prev, *next; 834 struct coro *prev, *next;
803}; 835};
804 836
805#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 837#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
806 838
839/** high level stuff ********************************************************/
840
807static int 841static int
808coro_state_destroy (struct coro *coro) 842coro_state_destroy (struct coro *coro)
809{ 843{
810 if (coro->flags & CF_DESTROYED) 844 if (coro->flags & CF_DESTROYED)
811 return 0; 845 return 0;
812 846
813 coro->flags |= CF_DESTROYED; 847 coro->flags |= CF_DESTROYED;
848
849 if (coro->flags & CF_READY)
850 {
851 /* reduce nready, as destroying a ready coro effectively unreadies it */
852 /* alternative: look through all ready queues and remove the coro */
853 LOCK;
854 --coro_nready;
855 UNLOCK;
856 }
857 else
858 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
814 859
815 if (coro->mainstack && coro->mainstack != main_mainstack) 860 if (coro->mainstack && coro->mainstack != main_mainstack)
816 { 861 {
862 struct coro temp;
863
817 assert (!(coro->flags & CF_RUNNING)); 864 assert (!(coro->flags & CF_RUNNING));
818 865
819 struct coro temp;
820 Zero (&temp, 1, struct coro); 866 Zero (&temp, 1, struct coro);
821 temp.save = CORO_SAVE_ALL; 867 temp.save = CORO_SAVE_ALL;
822 868
823 if (coro->flags & CF_RUNNING) 869 if (coro->flags & CF_RUNNING)
824 croak ("FATAL: tried to destroy currently running coroutine"); 870 croak ("FATAL: tried to destroy currently running coroutine");
890 /* very slow, but rare, check */ 936 /* very slow, but rare, check */
891 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 937 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
892 croak ("Coro::State object required"); 938 croak ("Coro::State object required");
893 } 939 }
894 940
895 mg = SvMAGIC (coro); 941 mg = CORO_MAGIC (coro);
896 assert (mg->mg_type == PERL_MAGIC_ext);
897 return (struct coro *)mg->mg_ptr; 942 return (struct coro *)mg->mg_ptr;
898} 943}
899 944
900static void 945static void
901prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 946prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
925 return old_save; 970 return old_save;
926} 971}
927 972
928/** Coro ********************************************************************/ 973/** Coro ********************************************************************/
929 974
930#define PRIO_MAX 3
931#define PRIO_HIGH 1
932#define PRIO_NORMAL 0
933#define PRIO_LOW -1
934#define PRIO_IDLE -3
935#define PRIO_MIN -4
936
937/* for Coro.pm */
938static SV *coro_current;
939static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
940static int coro_nready;
941
942static void 975static void
943coro_enq (SV *coro_sv) 976coro_enq (SV *coro_sv)
944{ 977{
945 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 978 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
946 coro_nready++;
947} 979}
948 980
949static SV * 981static SV *
950coro_deq (int min_prio) 982coro_deq (int min_prio)
951{ 983{
955 if (min_prio < 0) 987 if (min_prio < 0)
956 min_prio = 0; 988 min_prio = 0;
957 989
958 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 990 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
959 if (AvFILLp (coro_ready [prio]) >= 0) 991 if (AvFILLp (coro_ready [prio]) >= 0)
960 {
961 coro_nready--;
962 return av_shift (coro_ready [prio]); 992 return av_shift (coro_ready [prio]);
963 }
964 993
965 return 0; 994 return 0;
966} 995}
967 996
968static int 997static int
980 1009
981 coro->flags |= CF_READY; 1010 coro->flags |= CF_READY;
982 1011
983 LOCK; 1012 LOCK;
984 coro_enq (SvREFCNT_inc (coro_sv)); 1013 coro_enq (SvREFCNT_inc (coro_sv));
1014 ++coro_nready;
985 UNLOCK; 1015 UNLOCK;
986 1016
987 return 1; 1017 return 1;
988} 1018}
989 1019
1000 1030
1001 for (;;) 1031 for (;;)
1002 { 1032 {
1003 LOCK; 1033 LOCK;
1004 next_sv = coro_deq (PRIO_MIN); 1034 next_sv = coro_deq (PRIO_MIN);
1005 UNLOCK;
1006 1035
1007 /* nothing to schedule: call the idle handler */ 1036 /* nothing to schedule: call the idle handler */
1008 if (!next_sv) 1037 if (!next_sv)
1009 { 1038 {
1010 dSP; 1039 dSP;
1040 UNLOCK;
1011 1041
1012 ENTER; 1042 ENTER;
1013 SAVETMPS; 1043 SAVETMPS;
1014 1044
1015 PUSHMARK (SP); 1045 PUSHMARK (SP);
1024 ta->next = SvSTATE (next_sv); 1054 ta->next = SvSTATE (next_sv);
1025 1055
1026 /* cannot transfer to destroyed coros, skip and look for next */ 1056 /* cannot transfer to destroyed coros, skip and look for next */
1027 if (ta->next->flags & CF_DESTROYED) 1057 if (ta->next->flags & CF_DESTROYED)
1028 { 1058 {
1059 UNLOCK;
1029 SvREFCNT_dec (next_sv); 1060 SvREFCNT_dec (next_sv);
1061 /* coro_nready is already taken care of by destroy */
1030 continue; 1062 continue;
1031 } 1063 }
1032 1064
1065 --coro_nready;
1066 UNLOCK;
1033 break; 1067 break;
1034 } 1068 }
1035 1069
1036 /* free this only after the transfer */ 1070 /* free this only after the transfer */
1037 prev_sv = SvRV (coro_current); 1071 prev_sv = SvRV (coro_current);
1204 break; 1238 break;
1205 } 1239 }
1206 1240
1207 BARRIER; 1241 BARRIER;
1208 TRANSFER (ta); 1242 TRANSFER (ta);
1243
1244 if (GIMME_V != G_VOID && ta.next != ta.prev)
1245 XSRETURN_YES;
1209} 1246}
1210 1247
1211bool 1248bool
1212_destroy (SV *coro_sv) 1249_destroy (SV *coro_sv)
1213 CODE: 1250 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines