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.133 by root, Thu Jan 4 20:14:19 2007 UTC vs.
Revision 1.143 by root, Wed Feb 28 11:43:03 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{
472 496
473 /* free all temporaries */ 497 /* free all temporaries */
474 FREETMPS; 498 FREETMPS;
475 assert (PL_tmps_ix == -1); 499 assert (PL_tmps_ix == -1);
476 500
501 /* unwind all extra stacks */
477 POPSTACK_TO (PL_mainstack); 502 POPSTACK_TO (PL_mainstack);
503
504 /* unwind main stack */
505 dounwind (-1);
478 } 506 }
479 507
480 while (PL_curstackinfo->si_next) 508 while (PL_curstackinfo->si_next)
481 PL_curstackinfo = PL_curstackinfo->si_next; 509 PL_curstackinfo = PL_curstackinfo->si_next;
482 510
499#if !PERL_VERSION_ATLEAST (5,9,0) 527#if !PERL_VERSION_ATLEAST (5,9,0)
500 Safefree (PL_retstack); 528 Safefree (PL_retstack);
501#endif 529#endif
502} 530}
503 531
532/** coroutine stack handling ************************************************/
533
504static void 534static void
505setup_coro (struct coro *coro) 535setup_coro (struct coro *coro)
506{ 536{
507 /* 537 /*
508 * emulate part of the perl startup here. 538 * emulate part of the perl startup here.
510 540
511 coro_init_stacks (); 541 coro_init_stacks ();
512 542
513 PL_curcop = &PL_compiling; 543 PL_curcop = &PL_compiling;
514 PL_in_eval = EVAL_NULL; 544 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0;
515 PL_curpm = 0; 546 PL_curpm = 0;
516 PL_localizing = 0; 547 PL_localizing = 0;
517 PL_dirty = 0; 548 PL_dirty = 0;
518 PL_restartop = 0; 549 PL_restartop = 0;
519 550
601 632
602 Newz (0, cctx, 1, coro_cctx); 633 Newz (0, cctx, 1, coro_cctx);
603 634
604#if HAVE_MMAP 635#if HAVE_MMAP
605 636
606 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 637 cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
607 /* mmap supposedly does allocate-on-write for us */ 638 /* mmap supposedly does allocate-on-write for us */
608 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);
609 640
610 if (cctx->sptr == (void *)-1) 641 if (cctx->sptr != (void *)-1)
611 {
612 perror ("FATAL: unable to mmap stack for coroutine");
613 _exit (EXIT_FAILURE);
614 } 642 {
615
616# if STACKGUARD 643# if CORO_STACKGUARD
617 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 644 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
618# endif 645# endif
619 646 REGISTER_STACK (
620#else 647 cctx,
621
622 cctx->ssize = STACKSIZE * (long)sizeof (long);
623 New (0, cctx->sptr, STACKSIZE, long);
624
625 if (!cctx->sptr)
626 {
627 perror ("FATAL: unable to malloc stack for coroutine");
628 _exit (EXIT_FAILURE);
629 }
630
631#endif
632
633#if USE_VALGRIND
634 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
635 STACKGUARD * PAGESIZE + (char *)cctx->sptr, 648 CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr,
636 cctx->ssize + (char *)cctx->sptr 649 cctx->ssize + (char *)cctx->sptr
637 ); 650 );
638#endif
639 651
640 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 }
641 674
642 return cctx; 675 return cctx;
643} 676}
644 677
645static void 678static void
648 if (!cctx) 681 if (!cctx)
649 return; 682 return;
650 683
651 --cctx_count; 684 --cctx_count;
652 685
653#if USE_VALGRIND 686#if CORO_USE_VALGRIND
654 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 687 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
655#endif 688#endif
656 689
657#if HAVE_MMAP 690#if HAVE_MMAP
691 if (cctx->ssize > 0)
658 munmap (cctx->sptr, cctx->ssize); 692 munmap (cctx->sptr, cctx->ssize);
659#else 693 else
694#endif
660 Safefree (cctx->sptr); 695 Safefree (cctx->sptr);
661#endif
662 696
663 Safefree (cctx); 697 Safefree (cctx);
664} 698}
665 699
666static coro_cctx * 700static coro_cctx *
699 733
700 ++cctx_idle; 734 ++cctx_idle;
701 cctx->next = cctx_first; 735 cctx->next = cctx_first;
702 cctx_first = cctx; 736 cctx_first = cctx;
703} 737}
738
739/** coroutine switching *****************************************************/
704 740
705/* never call directly, always through the coro_state_transfer global variable */ 741/* never call directly, always through the coro_state_transfer global variable */
706static void NOINLINE 742static void NOINLINE
707transfer (struct coro *prev, struct coro *next) 743transfer (struct coro *prev, struct coro *next)
708{ 744{
798 struct coro *prev, *next; 834 struct coro *prev, *next;
799}; 835};
800 836
801#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 837#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
802 838
839/** high level stuff ********************************************************/
840
803static int 841static int
804coro_state_destroy (struct coro *coro) 842coro_state_destroy (struct coro *coro)
805{ 843{
806 if (coro->refcnt--)
807 return 0;
808
809 if (coro->flags & CF_DESTROYED) 844 if (coro->flags & CF_DESTROYED)
810 return 0; 845 return 0;
811 846
812 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 */
813 859
814 if (coro->mainstack && coro->mainstack != main_mainstack) 860 if (coro->mainstack && coro->mainstack != main_mainstack)
815 { 861 {
862 struct coro temp;
863
816 assert (!(coro->flags & CF_RUNNING)); 864 assert (!(coro->flags & CF_RUNNING));
817 865
818 struct coro temp;
819 Zero (&temp, 1, struct coro); 866 Zero (&temp, 1, struct coro);
820 temp.save = CORO_SAVE_ALL; 867 temp.save = CORO_SAVE_ALL;
821 868
822 if (coro->flags & CF_RUNNING) 869 if (coro->flags & CF_RUNNING)
823 croak ("FATAL: tried to destroy currently running coroutine"); 870 croak ("FATAL: tried to destroy currently running coroutine");
837 884
838 return 1; 885 return 1;
839} 886}
840 887
841static int 888static int
842coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 889coro_state_free (pTHX_ SV *sv, MAGIC *mg)
843{ 890{
844 struct coro *coro = (struct coro *)mg->mg_ptr; 891 struct coro *coro = (struct coro *)mg->mg_ptr;
845 mg->mg_ptr = 0; 892 mg->mg_ptr = 0;
846 893
894 if (--coro->refcnt < 0)
895 {
847 coro_state_destroy (coro); 896 coro_state_destroy (coro);
848
849 if (!coro->refcnt)
850 Safefree (coro); 897 Safefree (coro);
898 }
851 899
852 return 0; 900 return 0;
853} 901}
854 902
855static int 903static int
862 return 0; 910 return 0;
863} 911}
864 912
865static MGVTBL coro_state_vtbl = { 913static MGVTBL coro_state_vtbl = {
866 0, 0, 0, 0, 914 0, 0, 0, 0,
867 coro_state_clear, 915 coro_state_free,
868 0, 916 0,
869#ifdef MGf_DUP 917#ifdef MGf_DUP
870 coro_state_dup, 918 coro_state_dup,
871#else 919#else
872# define MGf_DUP 0 920# define MGf_DUP 0
923 return old_save; 971 return old_save;
924} 972}
925 973
926/** Coro ********************************************************************/ 974/** Coro ********************************************************************/
927 975
928#define PRIO_MAX 3
929#define PRIO_HIGH 1
930#define PRIO_NORMAL 0
931#define PRIO_LOW -1
932#define PRIO_IDLE -3
933#define PRIO_MIN -4
934
935/* for Coro.pm */
936static SV *coro_current;
937static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
938static int coro_nready;
939
940static void 976static void
941coro_enq (SV *coro_sv) 977coro_enq (SV *coro_sv)
942{ 978{
943 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 979 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
944 coro_nready++;
945} 980}
946 981
947static SV * 982static SV *
948coro_deq (int min_prio) 983coro_deq (int min_prio)
949{ 984{
953 if (min_prio < 0) 988 if (min_prio < 0)
954 min_prio = 0; 989 min_prio = 0;
955 990
956 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 991 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
957 if (AvFILLp (coro_ready [prio]) >= 0) 992 if (AvFILLp (coro_ready [prio]) >= 0)
958 {
959 coro_nready--;
960 return av_shift (coro_ready [prio]); 993 return av_shift (coro_ready [prio]);
961 }
962 994
963 return 0; 995 return 0;
964} 996}
965 997
966static int 998static int
978 1010
979 coro->flags |= CF_READY; 1011 coro->flags |= CF_READY;
980 1012
981 LOCK; 1013 LOCK;
982 coro_enq (SvREFCNT_inc (coro_sv)); 1014 coro_enq (SvREFCNT_inc (coro_sv));
1015 ++coro_nready;
983 UNLOCK; 1016 UNLOCK;
984 1017
985 return 1; 1018 return 1;
986} 1019}
987 1020
998 1031
999 for (;;) 1032 for (;;)
1000 { 1033 {
1001 LOCK; 1034 LOCK;
1002 next_sv = coro_deq (PRIO_MIN); 1035 next_sv = coro_deq (PRIO_MIN);
1003 UNLOCK;
1004 1036
1005 /* nothing to schedule: call the idle handler */ 1037 /* nothing to schedule: call the idle handler */
1006 if (!next_sv) 1038 if (!next_sv)
1007 { 1039 {
1008 dSP; 1040 dSP;
1041 UNLOCK;
1009 1042
1010 ENTER; 1043 ENTER;
1011 SAVETMPS; 1044 SAVETMPS;
1012 1045
1013 PUSHMARK (SP); 1046 PUSHMARK (SP);
1022 ta->next = SvSTATE (next_sv); 1055 ta->next = SvSTATE (next_sv);
1023 1056
1024 /* cannot transfer to destroyed coros, skip and look for next */ 1057 /* cannot transfer to destroyed coros, skip and look for next */
1025 if (ta->next->flags & CF_DESTROYED) 1058 if (ta->next->flags & CF_DESTROYED)
1026 { 1059 {
1060 UNLOCK;
1027 SvREFCNT_dec (next_sv); 1061 SvREFCNT_dec (next_sv);
1062 /* coro_nready is already taken care of by destroy */
1028 continue; 1063 continue;
1029 } 1064 }
1030 1065
1066 --coro_nready;
1067 UNLOCK;
1031 break; 1068 break;
1032 } 1069 }
1033 1070
1034 /* free this only after the transfer */ 1071 /* free this only after the transfer */
1035 prev_sv = SvRV (coro_current); 1072 prev_sv = SvRV (coro_current);
1202 break; 1239 break;
1203 } 1240 }
1204 1241
1205 BARRIER; 1242 BARRIER;
1206 TRANSFER (ta); 1243 TRANSFER (ta);
1244
1245 if (GIMME_V != G_VOID && ta.next != ta.prev)
1246 XSRETURN_YES;
1207} 1247}
1208 1248
1209bool 1249bool
1210_destroy (SV *coro_sv) 1250_destroy (SV *coro_sv)
1211 CODE: 1251 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines