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.14 by root, Mon Jul 23 23:48:05 2001 UTC vs.
Revision 1.19 by root, Sun Jul 29 01:11:41 2001 UTC

5#include "libcoro/coro.c" 5#include "libcoro/coro.c"
6 6
7#ifdef HAVE_MMAP 7#ifdef HAVE_MMAP
8# include <unistd.h> 8# include <unistd.h>
9# include <sys/mman.h> 9# include <sys/mman.h>
10# ifndef MAP_ANON
11# ifdef MAP_ANONYMOUS
12# define MAP_ANON MAP_ANONYMOUS
13# else
14# undef HAVE_MMAP
15# endif
16# endif
10#endif 17#endif
11 18
12#define MAY_FLUSH /* increases codesize */ 19#define MAY_FLUSH /* increases codesize */
13 20
21/* perl-related */
14#define TRANSFER_SAVE_DEFAV 0x00000001 22#define TRANSFER_SAVE_DEFAV 0x00000001
15#define TRANSFER_SAVE_DEFSV 0x00000002 23#define TRANSFER_SAVE_DEFSV 0x00000002
16#define TRANSFER_SAVE_ERRSV 0x00000004 24#define TRANSFER_SAVE_ERRSV 0x00000004
25/* c-related */
17#define TRANSFER_SAVE_CCTXT 0x00000008 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
18 32
19#define TRANSFER_SAVE_ALL -1 33#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
34 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
20 35
21#define SUB_INIT "Coro::State::initialize" 36#define SUB_INIT "Coro::State::initialize"
22#define UCORO_STATE "_coro_state" 37#define UCORO_STATE "_coro_state"
23 38
39/* The next macro should delcare a variable stacklevel that contains and approximation
40 * to the current C stack pointer. It's property is that it changes with each call
41 * and should be unique. */
42#define dSTACKLEVEL void *stacklevel = &stacklevel
43
44#define labs(l) ((l) >= 0 ? (l) : -(l))
45
46/* this is actually not only the c stack but also c registers etc... */
47typedef struct {
48 int refcnt; /* pointer reference counter */
49 int usecnt; /* shared by how many coroutines */
50 int gencnt; /* generation counter */
51
52 coro_context cctx;
53
54 void *sptr;
55 long ssize; /* positive == mmap, otherwise malloc */
56} coro_stack;
57
24struct coro { 58struct coro {
25 /* the optional C context */ 59 /* the optional C context */
26 coro_context cctx; 60 coro_stack *stack;
27 void *sptr; 61 void *cursp;
28 long ssize; 62 int gencnt;
29 63
30 /* optionally saved, might be zero */ 64 /* optionally saved, might be zero */
31 AV *defav; 65 AV *defav;
32 SV *defsv; 66 SV *defsv;
33 SV *errsv; 67 SV *errsv;
34 68
35 /* saved global state not related to stacks */ 69 /* saved global state not related to stacks */
36 U8 dowarn; 70 U8 dowarn;
71 I32 in_eval;
37 72
38 /* the stacks and related info (callchain etc..) */ 73 /* the stacks and related info (callchain etc..) */
39 PERL_SI *curstackinfo; 74 PERL_SI *curstackinfo;
40 AV *curstack; 75 AV *curstack;
41 AV *mainstack; 76 AV *mainstack;
59 I32 savestack_max; 94 I32 savestack_max;
60 OP **retstack; 95 OP **retstack;
61 I32 retstack_ix; 96 I32 retstack_ix;
62 I32 retstack_max; 97 I32 retstack_max;
63 COP *curcop; 98 COP *curcop;
64 JMPENV start_env;
65 JMPENV *top_env; 99 JMPENV *top_env;
66 100
67 /* data associated with this coroutine (initial args) */ 101 /* data associated with this coroutine (initial args) */
68 AV *args; 102 AV *args;
69}; 103};
254 288
255static void 289static void
256load_state(pTHX_ Coro__State c) 290load_state(pTHX_ Coro__State c)
257{ 291{
258 PL_dowarn = c->dowarn; 292 PL_dowarn = c->dowarn;
293 PL_in_eval = c->in_eval;
259 294
260 PL_curstackinfo = c->curstackinfo; 295 PL_curstackinfo = c->curstackinfo;
261 PL_curstack = c->curstack; 296 PL_curstack = c->curstack;
262 PL_mainstack = c->mainstack; 297 PL_mainstack = c->mainstack;
263 PL_stack_sp = c->stack_sp; 298 PL_stack_sp = c->stack_sp;
280 PL_savestack_max = c->savestack_max; 315 PL_savestack_max = c->savestack_max;
281 PL_retstack = c->retstack; 316 PL_retstack = c->retstack;
282 PL_retstack_ix = c->retstack_ix; 317 PL_retstack_ix = c->retstack_ix;
283 PL_retstack_max = c->retstack_max; 318 PL_retstack_max = c->retstack_max;
284 PL_curcop = c->curcop; 319 PL_curcop = c->curcop;
285 PL_start_env = c->start_env;
286 PL_top_env = c->top_env; 320 PL_top_env = c->top_env;
287 321
288 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 322 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
289 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 323 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
290 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 324 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
389 /* but if it's missing the defav contents magically get replaced sometimes */ 423 /* but if it's missing the defav contents magically get replaced sometimes */
390 if (c->defav) 424 if (c->defav)
391 av_reify (c->defav); 425 av_reify (c->defav);
392 426
393 c->dowarn = PL_dowarn; 427 c->dowarn = PL_dowarn;
428 c->in_eval = PL_in_eval;
394 429
395 c->curstackinfo = PL_curstackinfo; 430 c->curstackinfo = PL_curstackinfo;
396 c->curstack = PL_curstack; 431 c->curstack = PL_curstack;
397 c->mainstack = PL_mainstack; 432 c->mainstack = PL_mainstack;
398 c->stack_sp = PL_stack_sp; 433 c->stack_sp = PL_stack_sp;
415 c->savestack_max = PL_savestack_max; 450 c->savestack_max = PL_savestack_max;
416 c->retstack = PL_retstack; 451 c->retstack = PL_retstack;
417 c->retstack_ix = PL_retstack_ix; 452 c->retstack_ix = PL_retstack_ix;
418 c->retstack_max = PL_retstack_max; 453 c->retstack_max = PL_retstack_max;
419 c->curcop = PL_curcop; 454 c->curcop = PL_curcop;
420 c->start_env = PL_start_env;
421 c->top_env = PL_top_env; 455 c->top_env = PL_top_env;
422} 456}
423 457
424/* 458/*
425 * allocate various perl stacks. This is an exact copy 459 * allocate various perl stacks. This is an exact copy
437 471
438 PL_stack_base = AvARRAY(PL_curstack); 472 PL_stack_base = AvARRAY(PL_curstack);
439 PL_stack_sp = PL_stack_base; 473 PL_stack_sp = PL_stack_base;
440 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 474 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
441 475
442 New(50,PL_tmps_stack,64,SV*); 476 New(50,PL_tmps_stack,96,SV*);
443 PL_tmps_floor = -1; 477 PL_tmps_floor = -1;
444 PL_tmps_ix = -1; 478 PL_tmps_ix = -1;
445 PL_tmps_max = 64; 479 PL_tmps_max = 96;
446 480
447 New(54,PL_markstack,12,I32); 481 New(54,PL_markstack,16,I32);
448 PL_markstack_ptr = PL_markstack; 482 PL_markstack_ptr = PL_markstack;
449 PL_markstack_max = PL_markstack + 12; 483 PL_markstack_max = PL_markstack + 16;
450 484
451 SET_MARK_OFFSET; 485 SET_MARK_OFFSET;
452 486
453 New(54,PL_scopestack,12,I32); 487 New(54,PL_scopestack,16,I32);
454 PL_scopestack_ix = 0; 488 PL_scopestack_ix = 0;
455 PL_scopestack_max = 12; 489 PL_scopestack_max = 16;
456 490
457 New(54,PL_savestack,64,ANY); 491 New(54,PL_savestack,96,ANY);
458 PL_savestack_ix = 0; 492 PL_savestack_ix = 0;
459 PL_savestack_max = 64; 493 PL_savestack_max = 96;
460 494
461 New(54,PL_retstack,8,OP*); 495 New(54,PL_retstack,8,OP*);
462 PL_retstack_ix = 0; 496 PL_retstack_ix = 0;
463 PL_retstack_max = 8; 497 PL_retstack_max = 8;
464} 498}
505 Safefree(PL_savestack); 539 Safefree(PL_savestack);
506 Safefree(PL_retstack); 540 Safefree(PL_retstack);
507} 541}
508 542
509static void 543static void
510allocate_stack (Coro__State ctx) 544allocate_stack (Coro__State ctx, int alloc)
511{ 545{
546 coro_stack *stack;
547
548 New (0, stack, 1, coro_stack);
549
550 stack->refcnt = 1;
551 stack->usecnt = 1;
552 stack->gencnt = ctx->gencnt = 0;
553 if (alloc)
554 {
512#ifdef HAVE_MMAP 555#ifdef HAVE_MMAP
513 ctx->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-use */ 556 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
514 ctx->sptr = mmap (0, ctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 557 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
515 if (ctx->sptr == (void *)-1) 558 if (stack->sptr == (void *)-1)
516#endif 559#endif
517 { 560 {
518 /*FIXME*//*D*//* reasonable stack size! */ 561 /*FIXME*//*D*//* reasonable stack size! */
519 ctx->ssize = 4096 * sizeof (long); 562 stack->ssize = -4096 * sizeof (long);
520 New (0, ctx->sptr, 4096, long); 563 New (0, stack->sptr, 4096, long);
564 }
521 } 565 }
566 else
567 stack->sptr = 0;
568
569 ctx->stack = stack;
522} 570}
523 571
524static void 572static void
525deallocate_stack (Coro__State ctx) 573deallocate_stack (Coro__State ctx)
526{ 574{
575 coro_stack *stack = ctx->stack;
576
577 ctx->stack = 0;
578
579 if (stack)
580 {
581 if (!--stack->refcnt)
582 {
527#ifdef HAVE_MMAP 583#ifdef HAVE_MMAP
528 munmap (ctx->sptr, ctx->ssize); 584 if (stack->ssize > 0 && stack->sptr)
585 munmap (stack->sptr, stack->ssize);
586 else
529#else 587#else
530 Safefree (ctx->sptr); 588 Safefree (stack->sptr);
531#endif 589#endif
590 Safefree (stack);
591 }
592 else if (ctx->gencnt == stack->gencnt)
593 --stack->usecnt;
594 }
532} 595}
533 596
534/* might go away together with optional SAVE_CCTXT */
535static void 597static void
536setup_coro (void *arg) 598setup_coro (void *arg)
537{ 599{
538 /* 600 /*
539 * emulate part of the perl startup here. 601 * emulate part of the perl startup here.
541 dSP; 603 dSP;
542 Coro__State ctx = (Coro__State)arg; 604 Coro__State ctx = (Coro__State)arg;
543 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 605 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
544 606
545 coro_init_stacks (aTHX); 607 coro_init_stacks (aTHX);
546 JMPENV_BOOTSTRAP;
547 SPAGAIN;
548
549 /*PL_curcop = 0;*/ 608 /*PL_curcop = 0;*/
609 /*PL_in_eval = PL_in_eval;*/ /* inherit */
550 SvREFCNT_dec (GvAV (PL_defgv)); 610 SvREFCNT_dec (GvAV (PL_defgv));
551 GvAV (PL_defgv) = ctx->args; 611 GvAV (PL_defgv) = ctx->args;
552 612
613 SPAGAIN;
614
553 if (ctx->sptr) 615 if (ctx->stack)
554 { 616 {
617 ctx->cursp = 0;
618
555 PUSHMARK(SP); 619 PUSHMARK(SP);
556 PUTBACK; 620 PUTBACK;
557 (void) call_sv (sub_init, G_VOID|G_NOARGS); 621 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
622
623 if (SvTRUE (ERRSV))
624 croak (NULL);
625 else
558 croak ("FATAL: CCTXT coroutine returned!"); 626 croak ("FATAL: CCTXT coroutine returned!");
559 } 627 }
560 else 628 else
561 { 629 {
562 UNOP myop; 630 UNOP myop;
563 631
582 650
583 ENTER; /* necessary e.g. for dounwind */ 651 ENTER; /* necessary e.g. for dounwind */
584 } 652 }
585} 653}
586 654
655static void
656continue_coro (void *arg)
657{
658 /*
659 * this is a _very_ stripped down perl interpreter ;)
660 */
661 Coro__State ctx = (Coro__State)arg;
662
663 /*FIXME*//* must set up top_env here */
664 ctx->cursp = 0;
665 PL_op = PL_op->op_next;
666 CALLRUNOPS(aTHX);
667
668 abort ();
669}
670
587STATIC void 671STATIC void
588transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 672transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
589{ 673{
590 dSP; 674 dSP;
675 dSTACKLEVEL;
591 676
592 if (prev != next) 677 if (prev != next)
593 { 678 {
594 /*
595 * this could be done in newprocess which would lead to
596 * extremely elegant and fast (basically just SAVE/LOAD)
597 * code here, but lazy allocation of stacks has also
598 * some virtues and the overhead of the if() is nil.
599 */
600 if (next->mainstack) 679 if (next->mainstack)
601 { 680 {
602 SAVE (prev, flags); 681 SAVE (prev, flags);
603 LOAD (next); 682 LOAD (next);
604 683
605 /* mark this state as in-use */ 684 /* mark this state as in-use */
606 next->mainstack = 0; 685 next->mainstack = 0;
607 next->tmps_ix = -2; 686 next->tmps_ix = -2;
608 687
688 /* stacklevel changed? if yes, grab the stack for us! */
609 if (flags & TRANSFER_SAVE_CCTXT) 689 if (flags & TRANSFER_SAVE_CCTXT)
610 { 690 {
611 if (!next->ssize)
612 croak ("destination coroutine has no CCTXT (%p, %d)", next->sptr, next->ssize);
613
614 if (!prev->ssize) 691 if (!prev->stack)
615 prev->ssize = 1; /* mark cctx as valid ;) */ 692 allocate_stack (prev, 0);
693 else if (prev->cursp != stacklevel
694 && prev->stack->usecnt > 1)
695 {
696 prev->gencnt = ++prev->stack->gencnt;
697 prev->stack->usecnt = 1;
698 }
616 699
700 /* has our stack been invalidated? */
701 if (next->stack && next->stack->gencnt != next->gencnt)
702 {
703 deallocate_stack (next);
704 allocate_stack (next, 1);
705 coro_create (&(next->stack->cctx),
706 continue_coro, (void *)next,
707 next->stack->sptr, labs (next->stack->ssize));
708 }
709
617 coro_transfer (&(prev->cctx), &(next->cctx)); 710 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
618 } 711 }
619 712
620 } 713 }
621 else if (next->tmps_ix == -2) 714 else if (next->tmps_ix == -2)
622 croak ("tried to transfer to running coroutine"); 715 croak ("tried to transfer to running coroutine");
624 { 717 {
625 SAVE (prev, -1); /* first get rid of the old state */ 718 SAVE (prev, -1); /* first get rid of the old state */
626 719
627 if (flags & TRANSFER_SAVE_CCTXT) 720 if (flags & TRANSFER_SAVE_CCTXT)
628 { 721 {
629 if (!next->ssize) 722 if (!prev->stack)
723 allocate_stack (prev, 0);
724
725 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
630 { 726 {
727 setup_coro (next);
728
729 prev->stack->refcnt++;
730 prev->stack->usecnt++;
731 next->stack = prev->stack;
732 next->gencnt = prev->gencnt;
733 }
734 else
735 {
631 allocate_stack (next); 736 allocate_stack (next, 1);
632 coro_create (&(next->cctx), 737 coro_create (&(next->stack->cctx),
633 setup_coro, (void *)next, 738 setup_coro, (void *)next,
634 next->sptr, next->ssize); 739 next->stack->sptr, labs (next->stack->ssize));
740 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
635 } 741 }
636
637 if (!prev->ssize)
638 prev->ssize = 1; /* mark cctx as valid ;) */
639
640 coro_transfer (&(prev->cctx), &(next->cctx));
641 } 742 }
642 else 743 else
643 setup_coro (next); 744 setup_coro (next);
644 } 745 }
645 } 746 }
747
748 next->cursp = stacklevel;
646} 749}
647 750
648MODULE = Coro::State PACKAGE = Coro::State 751MODULE = Coro::State PACKAGE = Coro::State
649 752
650PROTOTYPES: ENABLE 753PROTOTYPES: ENABLE
678 781
679 New (0, coro, 1, struct coro); 782 New (0, coro, 1, struct coro);
680 783
681 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 784 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
682 coro->mainstack = 0; /* actual work is done inside transfer */ 785 coro->mainstack = 0; /* actual work is done inside transfer */
683 coro->sptr = 0; 786 coro->stack = 0;
684 coro->ssize = 0;
685 787
686 RETVAL = coro; 788 RETVAL = coro;
687 OUTPUT: 789 OUTPUT:
688 RETVAL 790 RETVAL
689 791
690void 792void
691transfer(prev, next, flags = TRANSFER_SAVE_ALL) 793transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK)
692 Coro::State_or_hashref prev 794 Coro::State_or_hashref prev
693 Coro::State_or_hashref next 795 Coro::State_or_hashref next
694 int flags 796 int flags
695 PROTOTYPE: @ 797 PROTOTYPE: @
696 CODE: 798 CODE:
713 LOAD((&temp)); /* this will get rid of defsv etc.. */ 815 LOAD((&temp)); /* this will get rid of defsv etc.. */
714 816
715 coro->mainstack = 0; 817 coro->mainstack = 0;
716 } 818 }
717 819
718 if (coro->sptr)
719 {
720 deallocate_stack (coro); 820 deallocate_stack (coro);
721 coro->sptr = 0;
722 }
723 821
724 Safefree (coro); 822 Safefree (coro);
725 823
726void 824void
727flush() 825flush()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines