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.104 by root, Mon Nov 27 01:51:02 2006 UTC vs.
Revision 1.108 by root, Mon Nov 27 18:15:47 2006 UTC

62# ifndef PAGESIZE 62# ifndef PAGESIZE
63# define PAGESIZE pagesize 63# define PAGESIZE pagesize
64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
65static long pagesize; 65static long pagesize;
66# else 66# else
67# define BOOT_PAGESIZE 67# define BOOT_PAGESIZE (void)0
68# endif 68# endif
69#else
70# define PAGESIZE 0
71# define BOOT_PAGESIZE (void)0
69#endif 72#endif
70 73
71/* The next macro should declare a variable stacklevel that contains and approximation 74/* The next macro should declare a variable stacklevel that contains and approximation
72 * to the current C stack pointer. Its property is that it changes with each call 75 * to the current C stack pointer. Its property is that it changes with each call
73 * and should be unique. */ 76 * and should be unique. */
100static struct CoroAPI coroapi; 103static struct CoroAPI coroapi;
101static AV *main_mainstack; /* used to differentiate between $main and others */ 104static AV *main_mainstack; /* used to differentiate between $main and others */
102static HV *coro_state_stash, *coro_stash; 105static HV *coro_state_stash, *coro_stash;
103static SV *coro_mortal; /* will be freed after next transfer */ 106static SV *coro_mortal; /* will be freed after next transfer */
104 107
108static struct coro_cctx *cctx_first;
109static int cctx_count, cctx_idle;
110
105/* this is a structure representing a c-level coroutine */ 111/* this is a structure representing a c-level coroutine */
106typedef struct coro_stack { 112typedef struct coro_cctx {
107 struct coro_stack *next; 113 struct coro_cctx *next;
108 114
109 /* the stack */ 115 /* the stack */
110 void *sptr; 116 void *sptr;
111 long ssize; /* positive == mmap, otherwise malloc */ 117 long ssize; /* positive == mmap, otherwise malloc */
112 118
116 coro_context cctx; 122 coro_context cctx;
117 123
118#if USE_VALGRIND 124#if USE_VALGRIND
119 int valgrind_id; 125 int valgrind_id;
120#endif 126#endif
121} coro_stack; 127} coro_cctx;
122 128
123/* this is a structure representing a perl-level coroutine */ 129/* this is a structure representing a perl-level coroutine */
124struct coro { 130struct coro {
125 /* the c coroutine allocated to this perl coroutine, if any */ 131 /* the c coroutine allocated to this perl coroutine, if any */
126 coro_stack *stack; 132 coro_cctx *cctx;
127 133
128 /* data associated with this coroutine (initial args) */ 134 /* data associated with this coroutine (initial args) */
129 AV *args; 135 AV *args;
130 int refcnt; 136 int refcnt;
131 137
132 /* optionally saved, might be zero */ 138 /* optionally saved, might be zero */
133 AV *defav; 139 AV *defav;
134 SV *defsv; 140 SV *defsv;
135 SV *errsv; 141 SV *errsv;
136 142
137 /* saved global state not related to stacks */ 143#define VAR(name,type) type name;
138 U8 dowarn; 144# include "state.h"
139 I32 in_eval; 145#undef VAR
140
141 /* the stacks and related info (callchain etc..) */
142 PERL_SI *curstackinfo;
143 AV *curstack;
144 AV *mainstack;
145 SV **stack_sp;
146 OP *op;
147 SV **curpad;
148 AV *comppad;
149 CV *compcv;
150 SV **stack_base;
151 SV **stack_max;
152 SV **tmps_stack;
153 I32 tmps_floor;
154 I32 tmps_ix;
155 I32 tmps_max;
156 I32 *markstack;
157 I32 *markstack_ptr;
158 I32 *markstack_max;
159 I32 *scopestack;
160 I32 scopestack_ix;
161 I32 scopestack_max;
162 ANY *savestack;
163 I32 savestack_ix;
164 I32 savestack_max;
165 OP **retstack;
166 I32 retstack_ix;
167 I32 retstack_max;
168 PMOP *curpm;
169 COP *curcop;
170 146
171 /* coro process data */ 147 /* coro process data */
172 int prio; 148 int prio;
173}; 149};
174 150
287#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 263#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
288 264
289static void 265static void
290load_state(Coro__State c) 266load_state(Coro__State c)
291{ 267{
292 PL_dowarn = c->dowarn; 268#define VAR(name,type) PL_ ## name = c->name;
293 PL_in_eval = c->in_eval; 269# include "state.h"
294 270#undef VAR
295 PL_curstackinfo = c->curstackinfo;
296 PL_curstack = c->curstack;
297 PL_mainstack = c->mainstack;
298 PL_stack_sp = c->stack_sp;
299 PL_op = c->op;
300 PL_curpad = c->curpad;
301 PL_comppad = c->comppad;
302 PL_compcv = c->compcv;
303 PL_stack_base = c->stack_base;
304 PL_stack_max = c->stack_max;
305 PL_tmps_stack = c->tmps_stack;
306 PL_tmps_floor = c->tmps_floor;
307 PL_tmps_ix = c->tmps_ix;
308 PL_tmps_max = c->tmps_max;
309 PL_markstack = c->markstack;
310 PL_markstack_ptr = c->markstack_ptr;
311 PL_markstack_max = c->markstack_max;
312 PL_scopestack = c->scopestack;
313 PL_scopestack_ix = c->scopestack_ix;
314 PL_scopestack_max = c->scopestack_max;
315 PL_savestack = c->savestack;
316 PL_savestack_ix = c->savestack_ix;
317 PL_savestack_max = c->savestack_max;
318#if !PERL_VERSION_ATLEAST (5,9,0)
319 PL_retstack = c->retstack;
320 PL_retstack_ix = c->retstack_ix;
321 PL_retstack_max = c->retstack_max;
322#endif
323 PL_curpm = c->curpm;
324 PL_curcop = c->curcop;
325 271
326 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 272 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
327 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 273 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
328 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 274 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
329 275
414 360
415 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 361 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
416 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 362 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
417 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 363 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
418 364
419 c->dowarn = PL_dowarn; 365#define VAR(name,type)c->name = PL_ ## name;
420 c->in_eval = PL_in_eval; 366# include "state.h"
421 367#undef VAR
422 c->curstackinfo = PL_curstackinfo;
423 c->curstack = PL_curstack;
424 c->mainstack = PL_mainstack;
425 c->stack_sp = PL_stack_sp;
426 c->op = PL_op;
427 c->curpad = PL_curpad;
428 c->comppad = PL_comppad;
429 c->compcv = PL_compcv;
430 c->stack_base = PL_stack_base;
431 c->stack_max = PL_stack_max;
432 c->tmps_stack = PL_tmps_stack;
433 c->tmps_floor = PL_tmps_floor;
434 c->tmps_ix = PL_tmps_ix;
435 c->tmps_max = PL_tmps_max;
436 c->markstack = PL_markstack;
437 c->markstack_ptr = PL_markstack_ptr;
438 c->markstack_max = PL_markstack_max;
439 c->scopestack = PL_scopestack;
440 c->scopestack_ix = PL_scopestack_ix;
441 c->scopestack_max = PL_scopestack_max;
442 c->savestack = PL_savestack;
443 c->savestack_ix = PL_savestack_ix;
444 c->savestack_max = PL_savestack_max;
445#if !PERL_VERSION_ATLEAST (5,9,0)
446 c->retstack = PL_retstack;
447 c->retstack_ix = PL_retstack_ix;
448 c->retstack_max = PL_retstack_max;
449#endif
450 c->curpm = PL_curpm;
451 c->curcop = PL_curcop;
452} 368}
453 369
454/* 370/*
455 * allocate various perl stacks. This is an exact copy 371 * allocate various perl stacks. This is an exact copy
456 * of perl.c:init_stacks, except that it uses less memory 372 * of perl.c:init_stacks, except that it uses less memory
499 415
500/* 416/*
501 * destroy the stacks, the callchain etc... 417 * destroy the stacks, the callchain etc...
502 */ 418 */
503static void 419static void
504coro_destroy_stacks() 420coro_destroy_stacks ()
505{ 421{
506 if (!IN_DESTRUCT) 422 if (!IN_DESTRUCT)
507 { 423 {
508 /* is this ugly, I ask? */ 424 /* is this ugly, I ask? */
509 LEAVE_SCOPE (0); 425 LEAVE_SCOPE (0);
575 myop.op_flags = OPf_WANT_VOID; 491 myop.op_flags = OPf_WANT_VOID;
576 492
577 PL_op = (OP *)&myop; 493 PL_op = (OP *)&myop;
578 494
579 PUSHMARK (SP); 495 PUSHMARK (SP);
580 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); 496 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
581 PUTBACK; 497 PUTBACK;
582 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 498 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
583 SPAGAIN; 499 SPAGAIN;
584 500
585 ENTER; /* necessary e.g. for dounwind */ 501 ENTER; /* necessary e.g. for dounwind */
595 coro_mortal = 0; 511 coro_mortal = 0;
596 } 512 }
597} 513}
598 514
599static void NOINLINE 515static void NOINLINE
600prepare_cctx (coro_stack *cctx) 516prepare_cctx (coro_cctx *cctx)
601{ 517{
602 dSP; 518 dSP;
603 LOGOP myop; 519 LOGOP myop;
604 520
605 Zero (&myop, 1, LOGOP); 521 Zero (&myop, 1, LOGOP);
606 myop.op_next = PL_op; 522 myop.op_next = PL_op;
607 myop.op_flags = OPf_WANT_VOID; 523 myop.op_flags = OPf_WANT_VOID;
608 524
609 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 525 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
610 526
611 PUSHMARK (SP); 527 PUSHMARK (SP);
612 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 528 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
613 PUTBACK; 529 PUTBACK;
614 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 530 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
615 SPAGAIN; 531 SPAGAIN;
616} 532}
617 533
623 539
624 /* 540 /*
625 * this is a _very_ stripped down perl interpreter ;) 541 * this is a _very_ stripped down perl interpreter ;)
626 */ 542 */
627 PL_top_env = &PL_start_env; 543 PL_top_env = &PL_start_env;
544
628 /* inject call to cctx_init */ 545 /* inject call to cctx_init */
629 prepare_cctx ((coro_stack *)arg); 546 prepare_cctx ((coro_cctx *)arg);
630 547
631 /* somebody will hit me for both perl_run and PL_restartop */ 548 /* somebody will hit me for both perl_run and PL_restartop */
632 perl_run (PL_curinterp); 549 perl_run (PL_curinterp);
633 550
634 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 551 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
635 abort (); 552 abort ();
636} 553}
637 554
638static coro_stack * 555static coro_cctx *
639stack_new () 556cctx_new ()
640{ 557{
641 coro_stack *stack; 558 coro_cctx *cctx;
642 559
560 ++cctx_count;
561
643 New (0, stack, 1, coro_stack); 562 New (0, cctx, 1, coro_cctx);
644 563
645#if HAVE_MMAP 564#if HAVE_MMAP
646 565
647 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 566 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
648 /* mmap suppsedly does allocate-on-write for us */ 567 /* mmap suppsedly does allocate-on-write for us */
649 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 568 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
650 569
651 if (stack->sptr == (void *)-1) 570 if (cctx->sptr == (void *)-1)
652 { 571 {
653 perror ("FATAL: unable to mmap stack for coroutine"); 572 perror ("FATAL: unable to mmap stack for coroutine");
654 _exit (EXIT_FAILURE); 573 _exit (EXIT_FAILURE);
655 } 574 }
656 575
657# if STACKGUARD 576# if STACKGUARD
658 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 577 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
659# endif 578# endif
660 579
661#else 580#else
662 581
663 stack->ssize = STACKSIZE * (long)sizeof (long); 582 cctx->ssize = STACKSIZE * (long)sizeof (long);
664 New (0, stack->sptr, STACKSIZE, long); 583 New (0, cctx->sptr, STACKSIZE, long);
665 584
666 if (!stack->sptr) 585 if (!cctx->sptr)
667 { 586 {
668 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 587 perror ("FATAL: unable to malloc stack for coroutine");
669 _exit (EXIT_FAILURE); 588 _exit (EXIT_FAILURE);
670 } 589 }
671 590
672#endif 591#endif
673 592
674#if USE_VALGRIND 593#if USE_VALGRIND
675 stack->valgrind_id = VALGRIND_STACK_REGISTER ( 594 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
676 STACKGUARD * PAGESIZE + (char *)stack->sptr, 595 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
677 stack->ssize + (char *)stack->sptr 596 cctx->ssize + (char *)cctx->sptr
678 ); 597 );
679#endif 598#endif
680 599
681 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 600 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
682 601
683 return stack; 602 return cctx;
684} 603}
685 604
686static void 605static void
687stack_free (coro_stack *stack) 606cctx_free (coro_cctx *cctx)
688{ 607{
689 if (!stack) 608 if (!cctx)
690 return; 609 return;
691 610
611 --cctx_count;
612
692#if USE_VALGRIND 613#if USE_VALGRIND
693 VALGRIND_STACK_DEREGISTER (stack->valgrind_id); 614 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
694#endif 615#endif
695 616
696#if HAVE_MMAP 617#if HAVE_MMAP
697 munmap (stack->sptr, stack->ssize); 618 munmap (cctx->sptr, cctx->ssize);
698#else 619#else
699 Safefree (stack->sptr); 620 Safefree (cctx->sptr);
700#endif 621#endif
701 622
702 Safefree (stack); 623 Safefree (cctx);
703} 624}
704 625
705static coro_stack *stack_first;
706static int cctx_count, cctx_idle;
707
708static coro_stack * 626static coro_cctx *
709stack_get () 627cctx_get ()
710{ 628{
711 coro_stack *stack; 629 coro_cctx *cctx;
712 630
713 if (stack_first) 631 if (cctx_first)
714 { 632 {
715 --cctx_idle; 633 --cctx_idle;
716 stack = stack_first; 634 cctx = cctx_first;
717 stack_first = stack->next; 635 cctx_first = cctx->next;
718 } 636 }
719 else 637 else
720 { 638 {
721 ++cctx_count; 639 cctx = cctx_new ();
722 stack = stack_new ();
723 PL_op = PL_op->op_next; 640 PL_op = PL_op->op_next;
724 } 641 }
725 642
726 return stack; 643 return cctx;
727} 644}
728 645
729static void 646static void
730stack_put (coro_stack *stack) 647cctx_put (coro_cctx *cctx)
731{ 648{
732 ++cctx_idle; 649 ++cctx_idle;
733 stack->next = stack_first; 650 cctx->next = cctx_first;
734 stack_first = stack; 651 cctx_first = cctx;
735} 652}
736 653
737/* never call directly, always through the coro_state_transfer global variable */ 654/* never call directly, always through the coro_state_transfer global variable */
738static void NOINLINE 655static void NOINLINE
739transfer (struct coro *prev, struct coro *next, int flags) 656transfer (struct coro *prev, struct coro *next, int flags)
740{ 657{
741 dSTACKLEVEL; 658 dSTACKLEVEL;
742 659
743 /* sometimes transfer is only called to set idle_sp */ 660 /* sometimes transfer is only called to set idle_sp */
744 if (flags == TRANSFER_SET_STACKLEVEL) 661 if (flags == TRANSFER_SET_STACKLEVEL)
745 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 662 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
746 else if (prev != next) 663 else if (prev != next)
747 { 664 {
748 coro_stack *prev__stack; 665 coro_cctx *prev__cctx;
749 666
750 LOCK; 667 LOCK;
751 668
752 if (next->mainstack) 669 if (next->mainstack)
753 { 670 {
761 /* first get rid of the old state */ 678 /* first get rid of the old state */
762 SAVE (prev, -1); 679 SAVE (prev, -1);
763 /* setup coroutine call */ 680 /* setup coroutine call */
764 setup_coro (next); 681 setup_coro (next);
765 /* need a stack */ 682 /* need a stack */
766 next->stack = 0; 683 next->cctx = 0;
767 } 684 }
768 685
769 if (!prev->stack) 686 if (!prev->cctx)
770 /* create a new empty context */ 687 /* create a new empty context */
771 Newz (0, prev->stack, 1, coro_stack); 688 Newz (0, prev->cctx, 1, coro_cctx);
772 689
773 prev__stack = prev->stack; 690 prev__cctx = prev->cctx;
774 691
775 /* possibly "free" the stack */ 692 /* possibly "free" the cctx */
776 if (prev__stack->idle_sp == STACKLEVEL) 693 if (prev__cctx->idle_sp == STACKLEVEL)
777 { 694 {
778 stack_put (prev__stack); 695 cctx_put (prev__cctx);
779 prev->stack = 0; 696 prev->cctx = 0;
780 } 697 }
781 698
782 if (!next->stack) 699 if (!next->cctx)
783 next->stack = stack_get (); 700 next->cctx = cctx_get ();
784 701
785 if (prev__stack != next->stack) 702 if (prev__cctx != next->cctx)
786 { 703 {
787 prev__stack->top_env = PL_top_env; 704 prev__cctx->top_env = PL_top_env;
788 PL_top_env = next->stack->top_env; 705 PL_top_env = next->cctx->top_env;
789 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 706 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
790 } 707 }
791 708
792 free_coro_mortal (); 709 free_coro_mortal ();
793 710
794 UNLOCK; 711 UNLOCK;
821 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 738 LOAD ((&temp)); /* this will get rid of defsv etc.. */
822 739
823 coro->mainstack = 0; 740 coro->mainstack = 0;
824 } 741 }
825 742
826 stack_free (coro->stack); 743 cctx_free (coro->cctx);
827 SvREFCNT_dec (coro->args); 744 SvREFCNT_dec (coro->args);
828 Safefree (coro); 745 Safefree (coro);
829} 746}
830 747
831static int 748static int
1075 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 992 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1076 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 993 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1077 994
1078 for (i = 1; i < items; i++) 995 for (i = 1; i < items; i++)
1079 av_push (coro->args, newSVsv (ST (i))); 996 av_push (coro->args, newSVsv (ST (i)));
1080
1081 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1082 /*coro->stack = 0;*/
1083} 997}
1084 OUTPUT: 998 OUTPUT:
1085 RETVAL 999 RETVAL
1086 1000
1087void 1001void
1096 struct transfer_args ta; 1010 struct transfer_args ta;
1097 1011
1098 switch (ix) 1012 switch (ix)
1099 { 1013 {
1100 case 0: 1014 case 0:
1101 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1015 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1102 ta.next = 0; 1016 ta.next = 0;
1103 ta.flags = TRANSFER_SET_STACKLEVEL; 1017 ta.flags = TRANSFER_SET_STACKLEVEL;
1104 break; 1018 break;
1105 1019
1106 case 1: 1020 case 1:
1166 int code 1080 int code
1167 PROTOTYPE: $ 1081 PROTOTYPE: $
1168 CODE: 1082 CODE:
1169 _exit (code); 1083 _exit (code);
1170 1084
1085int
1086cctx_count ()
1087 CODE:
1088 RETVAL = cctx_count;
1089 OUTPUT:
1090 RETVAL
1091
1092int
1093cctx_idle ()
1094 CODE:
1095 RETVAL = cctx_idle;
1096 OUTPUT:
1097 RETVAL
1098
1171MODULE = Coro::State PACKAGE = Coro 1099MODULE = Coro::State PACKAGE = Coro
1172 1100
1173BOOT: 1101BOOT:
1174{ 1102{
1175 int i; 1103 int i;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines