… | |
… | |
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... */ |
|
|
47 | typedef 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 | |
24 | struct coro { |
58 | struct 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; |
… | |
… | |
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 | |
509 | static void |
543 | static void |
510 | allocate_stack (Coro__State ctx) |
544 | allocate_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 | |
524 | static void |
572 | static void |
525 | deallocate_stack (Coro__State ctx) |
573 | deallocate_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 */ |
|
|
535 | static void |
597 | static void |
536 | setup_coro (void *arg) |
598 | setup_coro (void *arg) |
537 | { |
599 | { |
538 | /* |
600 | /* |
539 | * emulate part of the perl startup here. |
601 | * emulate part of the perl startup here. |
… | |
… | |
548 | |
610 | |
549 | /*PL_curcop = 0;*/ |
611 | /*PL_curcop = 0;*/ |
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 | |
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); |
558 | croak ("FATAL: CCTXT coroutine returned!"); |
622 | croak ("FATAL: CCTXT coroutine returned!"); |
559 | } |
623 | } |
… | |
… | |
582 | |
646 | |
583 | ENTER; /* necessary e.g. for dounwind */ |
647 | ENTER; /* necessary e.g. for dounwind */ |
584 | } |
648 | } |
585 | } |
649 | } |
586 | |
650 | |
|
|
651 | static void |
|
|
652 | continue_coro (void *arg) |
|
|
653 | { |
|
|
654 | /* |
|
|
655 | * this is a _very_ stripped down perl interpreter ;) |
|
|
656 | */ |
|
|
657 | Coro__State ctx = (Coro__State)arg; |
|
|
658 | |
|
|
659 | ctx->cursp = 0; |
|
|
660 | PL_op = PL_op->op_next; |
|
|
661 | CALLRUNOPS(aTHX); |
|
|
662 | /*NORETURN*/ |
|
|
663 | abort (); |
|
|
664 | } |
|
|
665 | |
587 | STATIC void |
666 | STATIC void |
588 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
667 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
589 | { |
668 | { |
590 | dSP; |
669 | dSP; |
|
|
670 | dSTACKLEVEL; |
591 | |
671 | |
592 | if (prev != next) |
672 | if (prev != next) |
593 | { |
673 | { |
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) |
674 | if (next->mainstack) |
601 | { |
675 | { |
602 | SAVE (prev, flags); |
676 | SAVE (prev, flags); |
603 | LOAD (next); |
677 | LOAD (next); |
604 | |
678 | |
605 | /* mark this state as in-use */ |
679 | /* mark this state as in-use */ |
606 | next->mainstack = 0; |
680 | next->mainstack = 0; |
607 | next->tmps_ix = -2; |
681 | next->tmps_ix = -2; |
608 | |
682 | |
|
|
683 | /* stacklevel changed? if yes, grab the stack for us! */ |
609 | if (flags & TRANSFER_SAVE_CCTXT) |
684 | if (flags & TRANSFER_SAVE_CCTXT) |
610 | { |
685 | { |
611 | if (!next->ssize) |
|
|
612 | croak ("destination coroutine has no CCTXT (%p, %d)", next->sptr, next->ssize); |
|
|
613 | |
|
|
614 | if (!prev->ssize) |
686 | if (!prev->stack) |
615 | prev->ssize = 1; /* mark cctx as valid ;) */ |
687 | allocate_stack (prev, 0); |
|
|
688 | else if (prev->cursp != stacklevel |
|
|
689 | && prev->stack->usecnt > 1) |
|
|
690 | { |
|
|
691 | prev->gencnt = ++prev->stack->gencnt; |
|
|
692 | prev->stack->usecnt = 1; |
|
|
693 | } |
616 | |
694 | |
|
|
695 | /* has our stack been invalidated? */ |
|
|
696 | if (next->stack && next->stack->gencnt != next->gencnt) |
|
|
697 | { |
|
|
698 | deallocate_stack (next); |
|
|
699 | allocate_stack (next, 1); |
|
|
700 | coro_create (&(next->stack->cctx), |
|
|
701 | continue_coro, (void *)next, |
|
|
702 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
703 | } |
|
|
704 | |
617 | coro_transfer (&(prev->cctx), &(next->cctx)); |
705 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
618 | } |
706 | } |
619 | |
707 | |
620 | } |
708 | } |
621 | else if (next->tmps_ix == -2) |
709 | else if (next->tmps_ix == -2) |
622 | croak ("tried to transfer to running coroutine"); |
710 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
624 | { |
712 | { |
625 | SAVE (prev, -1); /* first get rid of the old state */ |
713 | SAVE (prev, -1); /* first get rid of the old state */ |
626 | |
714 | |
627 | if (flags & TRANSFER_SAVE_CCTXT) |
715 | if (flags & TRANSFER_SAVE_CCTXT) |
628 | { |
716 | { |
629 | if (!next->ssize) |
717 | if (!prev->stack) |
|
|
718 | allocate_stack (prev, 0); |
|
|
719 | |
|
|
720 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
630 | { |
721 | { |
|
|
722 | setup_coro (next); |
|
|
723 | |
|
|
724 | prev->stack->refcnt++; |
|
|
725 | prev->stack->usecnt++; |
|
|
726 | next->stack = prev->stack; |
|
|
727 | next->gencnt = prev->gencnt; |
|
|
728 | } |
|
|
729 | else |
|
|
730 | { |
631 | allocate_stack (next); |
731 | allocate_stack (next, 1); |
632 | coro_create (&(next->cctx), |
732 | coro_create (&(next->stack->cctx), |
633 | setup_coro, (void *)next, |
733 | setup_coro, (void *)next, |
634 | next->sptr, next->ssize); |
734 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
735 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
635 | } |
736 | } |
636 | |
|
|
637 | if (!prev->ssize) |
|
|
638 | prev->ssize = 1; /* mark cctx as valid ;) */ |
|
|
639 | |
|
|
640 | coro_transfer (&(prev->cctx), &(next->cctx)); |
|
|
641 | } |
737 | } |
642 | else |
738 | else |
643 | setup_coro (next); |
739 | setup_coro (next); |
644 | } |
740 | } |
645 | } |
741 | } |
|
|
742 | |
|
|
743 | next->cursp = stacklevel; |
646 | } |
744 | } |
647 | |
745 | |
648 | MODULE = Coro::State PACKAGE = Coro::State |
746 | MODULE = Coro::State PACKAGE = Coro::State |
649 | |
747 | |
650 | PROTOTYPES: ENABLE |
748 | PROTOTYPES: ENABLE |
… | |
… | |
678 | |
776 | |
679 | New (0, coro, 1, struct coro); |
777 | New (0, coro, 1, struct coro); |
680 | |
778 | |
681 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
779 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
682 | coro->mainstack = 0; /* actual work is done inside transfer */ |
780 | coro->mainstack = 0; /* actual work is done inside transfer */ |
683 | coro->sptr = 0; |
781 | coro->stack = 0; |
684 | coro->ssize = 0; |
|
|
685 | |
782 | |
686 | RETVAL = coro; |
783 | RETVAL = coro; |
687 | OUTPUT: |
784 | OUTPUT: |
688 | RETVAL |
785 | RETVAL |
689 | |
786 | |
690 | void |
787 | void |
691 | transfer(prev, next, flags = TRANSFER_SAVE_ALL) |
788 | transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) |
692 | Coro::State_or_hashref prev |
789 | Coro::State_or_hashref prev |
693 | Coro::State_or_hashref next |
790 | Coro::State_or_hashref next |
694 | int flags |
791 | int flags |
695 | PROTOTYPE: @ |
792 | PROTOTYPE: @ |
696 | CODE: |
793 | CODE: |
… | |
… | |
713 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
810 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
714 | |
811 | |
715 | coro->mainstack = 0; |
812 | coro->mainstack = 0; |
716 | } |
813 | } |
717 | |
814 | |
718 | if (coro->sptr) |
|
|
719 | { |
|
|
720 | deallocate_stack (coro); |
815 | deallocate_stack (coro); |
721 | coro->sptr = 0; |
|
|
722 | } |
|
|
723 | |
816 | |
724 | Safefree (coro); |
817 | Safefree (coro); |
725 | |
818 | |
726 | void |
819 | void |
727 | flush() |
820 | flush() |