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.20 by root, Fri Aug 3 12:51:56 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines