… | |
… | |
27 | # ifndef IS_PADCONST |
27 | # ifndef IS_PADCONST |
28 | # define IS_PADCONST(v) 0 |
28 | # define IS_PADCONST(v) 0 |
29 | # endif |
29 | # endif |
30 | #endif |
30 | #endif |
31 | |
31 | |
|
|
32 | #include <stdio.h> |
32 | #include <errno.h> |
33 | #include <errno.h> |
33 | |
34 | |
34 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
35 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
35 | # undef STACKGUARD |
36 | # undef STACKGUARD |
36 | #endif |
37 | #endif |
… | |
… | |
57 | # else |
58 | # else |
58 | # define BOOT_PAGESIZE |
59 | # define BOOT_PAGESIZE |
59 | # endif |
60 | # endif |
60 | #endif |
61 | #endif |
61 | |
62 | |
62 | #define SUB_INIT "Coro::State::initialize" |
|
|
63 | |
|
|
64 | /* The next macro should declare a variable stacklevel that contains and approximation |
63 | /* The next macro should declare a variable stacklevel that contains and approximation |
65 | * to the current C stack pointer. Its property is that it changes with each call |
64 | * to the current C stack pointer. Its property is that it changes with each call |
66 | * and should be unique. */ |
65 | * and should be unique. */ |
|
|
66 | #define dSTACKLEVEL int stacklevel |
67 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
67 | #define STACKLEVEL ((void *)&stacklevel) |
68 | |
68 | |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
70 | |
70 | |
71 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
72 | |
|
|
73 | #include "CoroAPI.h" |
71 | #include "CoroAPI.h" |
|
|
72 | |
|
|
73 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
74 | |
74 | |
75 | #ifdef USE_ITHREADS |
75 | #ifdef USE_ITHREADS |
76 | static perl_mutex coro_mutex; |
76 | static perl_mutex coro_mutex; |
77 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
77 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
78 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
78 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
… | |
… | |
84 | static struct CoroAPI coroapi; |
84 | static struct CoroAPI coroapi; |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
86 | static HV *coro_state_stash, *coro_stash; |
86 | static HV *coro_state_stash, *coro_stash; |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
88 | |
88 | |
89 | /* this is actually not only the c stack but also c registers etc... */ |
89 | /* this is a structure representing a c-level coroutine */ |
90 | typedef struct { |
90 | typedef struct coro_stack { |
91 | int refcnt; /* pointer reference counter */ |
91 | struct coro_stack *next; |
92 | int usecnt; /* shared by how many coroutines */ |
|
|
93 | int gencnt; /* generation counter */ |
|
|
94 | |
92 | |
95 | coro_context cctx; |
93 | /* the stack */ |
96 | |
|
|
97 | void *sptr; |
94 | void *sptr; |
98 | long ssize; /* positive == mmap, otherwise malloc */ |
95 | long ssize; /* positive == mmap, otherwise malloc */ |
|
|
96 | |
|
|
97 | /* cpu state */ |
|
|
98 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
99 | JMPENV *top_env; |
|
|
100 | coro_context cctx; |
99 | } coro_stack; |
101 | } coro_stack; |
100 | |
102 | |
|
|
103 | /* this is a structure representing a perl-level coroutine */ |
101 | struct coro { |
104 | struct coro { |
102 | /* the optional C context */ |
105 | /* the c coroutine allocated to this perl coroutine, if any */ |
103 | coro_stack *stack; |
106 | coro_stack *stack; |
104 | void *cursp; |
107 | |
|
|
108 | /* data associated with this coroutine (initial args) */ |
|
|
109 | AV *args; |
105 | int gencnt; |
110 | int refcnt; |
106 | |
111 | |
107 | /* optionally saved, might be zero */ |
112 | /* optionally saved, might be zero */ |
108 | AV *defav; |
113 | AV *defav; |
109 | SV *defsv; |
114 | SV *defsv; |
110 | SV *errsv; |
115 | SV *errsv; |
… | |
… | |
140 | OP **retstack; |
145 | OP **retstack; |
141 | I32 retstack_ix; |
146 | I32 retstack_ix; |
142 | I32 retstack_max; |
147 | I32 retstack_max; |
143 | PMOP *curpm; |
148 | PMOP *curpm; |
144 | COP *curcop; |
149 | COP *curcop; |
145 | JMPENV *top_env; |
|
|
146 | |
150 | |
147 | /* coro process data */ |
151 | /* coro process data */ |
148 | int prio; |
152 | int prio; |
149 | |
|
|
150 | /* data associated with this coroutine (initial args) */ |
|
|
151 | AV *args; |
|
|
152 | int refcnt; |
|
|
153 | }; |
153 | }; |
154 | |
154 | |
155 | typedef struct coro *Coro__State; |
155 | typedef struct coro *Coro__State; |
156 | typedef struct coro *Coro__State_or_hashref; |
156 | typedef struct coro *Coro__State_or_hashref; |
157 | |
157 | |
… | |
… | |
229 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
229 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
230 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
230 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
231 | else |
231 | else |
232 | { |
232 | { |
233 | #if 0 |
233 | #if 0 |
234 | /* this should work - but it doesn't :( */ |
234 | /* this is probably cleaner, but also slower? */ |
235 | CV *cp = Perl_cv_clone (aTHX_ cv); |
235 | CV *cp = Perl_cv_clone (aTHX_ cv); |
236 | CvPADLIST (cv) = CvPADLIST (cp); |
236 | CvPADLIST (cv) = CvPADLIST (cp); |
237 | CvPADLIST (cp) = 0; |
237 | CvPADLIST (cp) = 0; |
238 | SvREFCNT_dec (cp); |
238 | SvREFCNT_dec (cp); |
239 | #else |
239 | #else |
… | |
… | |
300 | PL_retstack_ix = c->retstack_ix; |
300 | PL_retstack_ix = c->retstack_ix; |
301 | PL_retstack_max = c->retstack_max; |
301 | PL_retstack_max = c->retstack_max; |
302 | #endif |
302 | #endif |
303 | PL_curpm = c->curpm; |
303 | PL_curpm = c->curpm; |
304 | PL_curcop = c->curcop; |
304 | PL_curcop = c->curcop; |
305 | PL_top_env = c->top_env; |
|
|
306 | |
305 | |
307 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
306 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
308 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
307 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
309 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
308 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
310 | |
309 | |
… | |
… | |
428 | c->retstack_ix = PL_retstack_ix; |
427 | c->retstack_ix = PL_retstack_ix; |
429 | c->retstack_max = PL_retstack_max; |
428 | c->retstack_max = PL_retstack_max; |
430 | #endif |
429 | #endif |
431 | c->curpm = PL_curpm; |
430 | c->curpm = PL_curpm; |
432 | c->curcop = PL_curcop; |
431 | c->curcop = PL_curcop; |
433 | c->top_env = PL_top_env; |
|
|
434 | } |
432 | } |
435 | |
433 | |
436 | /* |
434 | /* |
437 | * allocate various perl stacks. This is an exact copy |
435 | * allocate various perl stacks. This is an exact copy |
438 | * of perl.c:init_stacks, except that it uses less memory |
436 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
532 | Safefree (PL_retstack); |
530 | Safefree (PL_retstack); |
533 | #endif |
531 | #endif |
534 | } |
532 | } |
535 | |
533 | |
536 | static void |
534 | static void |
537 | allocate_stack (Coro__State ctx, int alloc) |
535 | setup_coro (struct coro *coro) |
538 | { |
|
|
539 | coro_stack *stack; |
|
|
540 | |
|
|
541 | New (0, stack, 1, coro_stack); |
|
|
542 | |
|
|
543 | stack->refcnt = 1; |
|
|
544 | stack->usecnt = 1; |
|
|
545 | stack->gencnt = ctx->gencnt = 0; |
|
|
546 | |
|
|
547 | if (alloc) |
|
|
548 | { |
|
|
549 | #if HAVE_MMAP |
|
|
550 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
|
|
551 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
|
|
552 | if (stack->sptr != (void *)-1) |
|
|
553 | { |
|
|
554 | # if STACKGUARD |
|
|
555 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
556 | # endif |
|
|
557 | } |
|
|
558 | else |
|
|
559 | #endif |
|
|
560 | { |
|
|
561 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
|
|
562 | New (0, stack->sptr, STACKSIZE, long); |
|
|
563 | } |
|
|
564 | } |
|
|
565 | else |
|
|
566 | stack->sptr = 0; |
|
|
567 | |
|
|
568 | ctx->stack = stack; |
|
|
569 | } |
|
|
570 | |
|
|
571 | static void |
|
|
572 | deallocate_stack (Coro__State ctx) |
|
|
573 | { |
|
|
574 | coro_stack *stack = ctx->stack; |
|
|
575 | |
|
|
576 | ctx->stack = 0; |
|
|
577 | |
|
|
578 | if (stack) |
|
|
579 | { |
|
|
580 | if (!--stack->refcnt) |
|
|
581 | { |
|
|
582 | #ifdef HAVE_MMAP |
|
|
583 | if (stack->ssize > 0 && stack->sptr) |
|
|
584 | munmap (stack->sptr, stack->ssize); |
|
|
585 | else |
|
|
586 | #endif |
|
|
587 | Safefree (stack->sptr); |
|
|
588 | |
|
|
589 | Safefree (stack); |
|
|
590 | } |
|
|
591 | else if (ctx->gencnt == stack->gencnt) |
|
|
592 | --stack->usecnt; |
|
|
593 | } |
|
|
594 | } |
|
|
595 | |
|
|
596 | static void |
|
|
597 | setup_coro (void *arg) |
|
|
598 | { |
536 | { |
599 | /* |
537 | /* |
600 | * emulate part of the perl startup here. |
538 | * emulate part of the perl startup here. |
601 | */ |
539 | */ |
602 | dTHX; |
540 | dTHX; |
603 | dSP; |
541 | dSP; |
604 | Coro__State ctx = (Coro__State)arg; |
542 | UNOP myop; |
605 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
543 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
606 | |
544 | |
607 | coro_init_stacks (aTHX); |
545 | coro_init_stacks (aTHX); |
608 | /*PL_curcop = 0;*/ |
546 | /*PL_curcop = 0;*/ |
609 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
547 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
610 | SvREFCNT_dec (GvAV (PL_defgv)); |
548 | SvREFCNT_dec (GvAV (PL_defgv)); |
611 | GvAV (PL_defgv) = ctx->args; ctx->args = 0; |
549 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
612 | |
550 | |
613 | SPAGAIN; |
551 | SPAGAIN; |
614 | |
552 | |
615 | if (ctx->stack) |
553 | Zero (&myop, 1, UNOP); |
|
|
554 | myop.op_next = Nullop; |
|
|
555 | myop.op_flags = OPf_WANT_VOID; |
|
|
556 | |
|
|
557 | PL_op = (OP *)&myop; |
|
|
558 | |
|
|
559 | PUSHMARK(SP); |
|
|
560 | XPUSHs (sub_init); |
|
|
561 | PUTBACK; |
|
|
562 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
563 | SPAGAIN; |
|
|
564 | |
|
|
565 | ENTER; /* necessary e.g. for dounwind */ |
|
|
566 | } |
|
|
567 | |
|
|
568 | static void |
|
|
569 | free_coro_mortal () |
|
|
570 | { |
|
|
571 | if (coro_mortal) |
616 | { |
572 | { |
617 | ctx->cursp = 0; |
573 | dTHX; |
618 | |
574 | |
619 | PUSHMARK(SP); |
575 | SvREFCNT_dec (coro_mortal); |
620 | PUTBACK; |
576 | coro_mortal = 0; |
621 | (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); |
|
|
622 | |
|
|
623 | if (SvTRUE (ERRSV)) |
|
|
624 | croak (NULL); |
|
|
625 | else |
|
|
626 | croak ("FATAL: CCTXT coroutine returned!"); |
|
|
627 | } |
577 | } |
628 | else |
|
|
629 | { |
|
|
630 | UNOP myop; |
|
|
631 | |
|
|
632 | Zero(&myop, 1, UNOP); |
|
|
633 | myop.op_next = Nullop; |
|
|
634 | myop.op_flags = OPf_WANT_VOID; |
|
|
635 | |
|
|
636 | PL_op = (OP *)&myop; |
|
|
637 | |
|
|
638 | PUSHMARK(SP); |
|
|
639 | XPUSHs (sub_init); |
|
|
640 | /* |
|
|
641 | * the next line is slightly wrong, as PL_op->op_next |
|
|
642 | * is actually being executed so we skip the first op. |
|
|
643 | * that doesn't matter, though, since it is only |
|
|
644 | * pp_nextstate and we never return... |
|
|
645 | * ah yes, and I don't care anyways ;) |
|
|
646 | */ |
|
|
647 | PUTBACK; |
|
|
648 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
649 | SPAGAIN; |
|
|
650 | |
|
|
651 | ENTER; /* necessary e.g. for dounwind */ |
|
|
652 | } |
|
|
653 | } |
578 | } |
654 | |
579 | |
655 | static void |
580 | static void |
656 | continue_coro (void *arg) |
581 | coro_run (void *arg) |
657 | { |
582 | { |
658 | /* |
583 | /* |
659 | * this is a _very_ stripped down perl interpreter ;) |
584 | * this is a _very_ stripped down perl interpreter ;) |
660 | */ |
585 | */ |
661 | dTHX; |
586 | dTHX; |
662 | Coro__State ctx = (Coro__State)arg; |
587 | int ret; |
|
|
588 | |
|
|
589 | UNLOCK; |
663 | |
590 | |
664 | PL_top_env = &PL_start_env; |
591 | PL_top_env = &PL_start_env; |
665 | |
592 | |
666 | ctx->cursp = 0; |
593 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); |
667 | PL_op = PL_op->op_next; |
594 | sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); |
668 | CALLRUNOPS(aTHX); |
|
|
669 | |
595 | |
|
|
596 | /* continue at cctx_init, without entersub */ |
|
|
597 | PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
598 | |
|
|
599 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
600 | ret = perl_run (aTHX_ PERL_GET_CONTEXT); |
|
|
601 | printf ("ret %d\n", ret);//D |
|
|
602 | |
|
|
603 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
670 | abort (); |
604 | abort (); |
671 | } |
605 | } |
672 | |
606 | |
|
|
607 | static coro_stack * |
|
|
608 | stack_new () |
|
|
609 | { |
|
|
610 | coro_stack *stack; |
|
|
611 | |
|
|
612 | New (0, stack, 1, coro_stack); |
|
|
613 | |
|
|
614 | #if HAVE_MMAP |
|
|
615 | |
|
|
616 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
|
|
617 | /* mmap suppsedly does allocate-on-write for us */ |
|
|
618 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
|
|
619 | |
|
|
620 | if (stack->sptr == (void *)-1) |
|
|
621 | { |
|
|
622 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
623 | _exit (EXIT_FAILURE); |
|
|
624 | } |
|
|
625 | |
|
|
626 | # if STACKGUARD |
|
|
627 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
628 | # endif |
|
|
629 | |
|
|
630 | #else |
|
|
631 | |
|
|
632 | stack->ssize = STACKSIZE * (long)sizeof (long); |
|
|
633 | New (0, stack->sptr, STACKSIZE, long); |
|
|
634 | |
|
|
635 | if (!stack->sptr) |
|
|
636 | { |
|
|
637 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
|
|
638 | _exit (EXIT_FAILURE); |
|
|
639 | } |
|
|
640 | |
|
|
641 | #endif |
|
|
642 | |
|
|
643 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
|
|
644 | |
|
|
645 | return stack; |
|
|
646 | } |
|
|
647 | |
|
|
648 | static void |
|
|
649 | stack_free (coro_stack *stack) |
|
|
650 | { |
|
|
651 | if (!stack) |
|
|
652 | return; |
|
|
653 | |
|
|
654 | #if HAVE_MMAP |
|
|
655 | munmap (stack->sptr, stack->ssize); |
|
|
656 | #else |
|
|
657 | Safefree (stack->sptr); |
|
|
658 | #endif |
|
|
659 | |
|
|
660 | Safefree (stack); |
|
|
661 | } |
|
|
662 | |
|
|
663 | static coro_stack *stack_first; |
|
|
664 | |
|
|
665 | static coro_stack * |
|
|
666 | stack_get () |
|
|
667 | { |
|
|
668 | coro_stack *stack; |
|
|
669 | |
|
|
670 | if (stack_first) |
|
|
671 | { |
|
|
672 | stack = stack_first; |
|
|
673 | stack_first = stack->next; |
|
|
674 | } |
|
|
675 | else |
|
|
676 | { |
|
|
677 | stack = stack_new (); |
|
|
678 | PL_op = PL_op->op_next; |
|
|
679 | } |
|
|
680 | |
|
|
681 | return stack; |
|
|
682 | } |
|
|
683 | |
|
|
684 | static void |
|
|
685 | stack_put (coro_stack *stack) |
|
|
686 | { |
|
|
687 | stack->next = stack_first; |
|
|
688 | stack_first = stack; |
|
|
689 | } |
|
|
690 | |
673 | /* never call directly, always through the coro_state_transfer global variable */ |
691 | /* never call directly, always through the coro_state_transfer global variable */ |
674 | static void |
692 | static void |
675 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
693 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
676 | { |
694 | { |
677 | dSTACKLEVEL; |
695 | dSTACKLEVEL; |
678 | |
696 | |
|
|
697 | /* sometimes transfer is only called to set idle_sp */ |
|
|
698 | if (flags == TRANSFER_SET_STACKLEVEL) |
|
|
699 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
679 | if (prev != next) |
700 | else if (prev != next) |
680 | { |
701 | { |
681 | /* has this coro been created yet? */ |
702 | coro_stack *prev__stack; |
|
|
703 | |
|
|
704 | LOCK; |
|
|
705 | |
682 | if (next->mainstack) |
706 | if (next->mainstack) |
683 | { |
707 | { |
684 | LOCK; |
708 | /* coroutine already started */ |
685 | SAVE (prev, flags); |
709 | SAVE (prev, flags); |
686 | LOAD (next); |
710 | LOAD (next); |
687 | UNLOCK; |
|
|
688 | |
|
|
689 | /* mark this state as in-use */ |
|
|
690 | next->mainstack = 0; |
|
|
691 | next->tmps_ix = -2; |
|
|
692 | |
|
|
693 | /* stacklevel changed? if yes, grab the stack for us! */ |
|
|
694 | if (flags & TRANSFER_SAVE_CCTXT) |
|
|
695 | { |
|
|
696 | if (!prev->stack) |
|
|
697 | allocate_stack (prev, 0); |
|
|
698 | else if (prev->cursp != stacklevel |
|
|
699 | && prev->stack->usecnt > 1) |
|
|
700 | { |
|
|
701 | prev->gencnt = ++prev->stack->gencnt; |
|
|
702 | prev->stack->usecnt = 1; |
|
|
703 | } |
|
|
704 | |
|
|
705 | /* has our stack been invalidated? */ |
|
|
706 | if (next->stack && next->stack->gencnt != next->gencnt) |
|
|
707 | { |
|
|
708 | deallocate_stack (next); |
|
|
709 | allocate_stack (next, 1); |
|
|
710 | coro_create (&(next->stack->cctx), |
|
|
711 | continue_coro, (void *)next, |
|
|
712 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
713 | } |
|
|
714 | |
|
|
715 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
716 | prev->cursp = stacklevel; |
|
|
717 | /* don't add any code here */ |
|
|
718 | } |
|
|
719 | else |
|
|
720 | next->cursp = stacklevel; |
|
|
721 | } |
711 | } |
722 | else if (next->tmps_ix == -2) |
|
|
723 | croak ("tried to transfer to running coroutine"); |
|
|
724 | else |
712 | else |
725 | { |
713 | { |
726 | LOCK; |
714 | /* need to start coroutine */ |
727 | SAVE (prev, -1); /* first get rid of the old state */ |
715 | /* first get rid of the old state */ |
728 | UNLOCK; |
716 | SAVE (prev, -1); |
729 | |
717 | /* setup coroutine call */ |
730 | /* create the coroutine for the first time */ |
|
|
731 | if (flags & TRANSFER_SAVE_CCTXT) |
|
|
732 | { |
|
|
733 | if (!prev->stack) |
|
|
734 | allocate_stack (prev, 0); |
|
|
735 | |
|
|
736 | /* the new coroutine starts with start_env again */ |
|
|
737 | PL_top_env = &PL_start_env; |
|
|
738 | |
|
|
739 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
|
|
740 | { |
|
|
741 | setup_coro (next); |
|
|
742 | next->cursp = stacklevel; |
|
|
743 | |
|
|
744 | prev->stack->refcnt++; |
|
|
745 | prev->stack->usecnt++; |
|
|
746 | next->stack = prev->stack; |
|
|
747 | next->gencnt = prev->gencnt; |
|
|
748 | } |
|
|
749 | else |
|
|
750 | { |
|
|
751 | assert (!next->stack); |
|
|
752 | allocate_stack (next, 1); |
|
|
753 | coro_create (&(next->stack->cctx), |
|
|
754 | setup_coro, (void *)next, |
|
|
755 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
756 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
757 | prev->cursp = stacklevel; |
|
|
758 | /* don't add any code here */ |
|
|
759 | } |
|
|
760 | } |
|
|
761 | else |
|
|
762 | { |
|
|
763 | setup_coro (next); |
718 | setup_coro (next); |
764 | next->cursp = stacklevel; |
719 | /* need a stack */ |
765 | } |
720 | next->stack = 0; |
766 | } |
721 | } |
|
|
722 | |
|
|
723 | if (!prev->stack) |
|
|
724 | /* create a new empty context */ |
|
|
725 | Newz (0, prev->stack, 1, coro_stack); |
|
|
726 | |
|
|
727 | prev__stack = prev->stack; |
|
|
728 | |
|
|
729 | /* possibly "free" the stack */ |
|
|
730 | if (prev__stack->idle_sp == STACKLEVEL) |
|
|
731 | { |
|
|
732 | stack_put (prev__stack); |
|
|
733 | prev->stack = 0; |
|
|
734 | } |
|
|
735 | |
|
|
736 | if (!next->stack) |
|
|
737 | next->stack = stack_get (); |
|
|
738 | |
|
|
739 | if (prev__stack != next->stack) |
|
|
740 | { |
|
|
741 | prev__stack->top_env = PL_top_env; |
|
|
742 | PL_top_env = next->stack->top_env; |
|
|
743 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
|
|
744 | } |
|
|
745 | |
|
|
746 | free_coro_mortal (); |
|
|
747 | |
|
|
748 | UNLOCK; |
767 | } |
749 | } |
768 | |
|
|
769 | LOCK; |
|
|
770 | if (coro_mortal) |
|
|
771 | { |
|
|
772 | SvREFCNT_dec (coro_mortal); |
|
|
773 | coro_mortal = 0; |
|
|
774 | } |
|
|
775 | UNLOCK; |
|
|
776 | } |
750 | } |
777 | |
751 | |
778 | /* use this function pointer to call the above function */ |
752 | /* use this function pointer to call the above function */ |
779 | /* this is done to increase chances of the compiler not inlining the call */ |
753 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
754 | /* not static to make it even harder for the compiler (and theoretically impossible in most cases */ |
780 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
755 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
756 | |
|
|
757 | struct transfer_args |
|
|
758 | { |
|
|
759 | struct coro *prev, *next; |
|
|
760 | int flags; |
|
|
761 | }; |
|
|
762 | |
|
|
763 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
781 | |
764 | |
782 | static void |
765 | static void |
783 | coro_state_destroy (struct coro *coro) |
766 | coro_state_destroy (struct coro *coro) |
784 | { |
767 | { |
785 | if (coro->refcnt--) |
768 | if (coro->refcnt--) |
… | |
… | |
797 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
780 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
798 | |
781 | |
799 | coro->mainstack = 0; |
782 | coro->mainstack = 0; |
800 | } |
783 | } |
801 | |
784 | |
802 | deallocate_stack (coro); |
785 | stack_free (coro->stack); |
803 | SvREFCNT_dec (coro->args); |
786 | SvREFCNT_dec (coro->args); |
804 | Safefree (coro); |
787 | Safefree (coro); |
805 | } |
788 | } |
806 | |
789 | |
807 | static int |
790 | static int |
… | |
… | |
848 | assert (mg->mg_type == PERL_MAGIC_ext); |
831 | assert (mg->mg_type == PERL_MAGIC_ext); |
849 | return (struct coro *)mg->mg_ptr; |
832 | return (struct coro *)mg->mg_ptr; |
850 | } |
833 | } |
851 | |
834 | |
852 | static void |
835 | static void |
|
|
836 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) |
|
|
837 | { |
|
|
838 | ta->prev = SvSTATE (prev); |
|
|
839 | ta->next = SvSTATE (next); |
|
|
840 | ta->flags = flags; |
|
|
841 | } |
|
|
842 | |
|
|
843 | static void |
853 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
844 | api_transfer (SV *prev, SV *next, int flags) |
854 | { |
845 | { |
855 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
846 | dTHX; |
|
|
847 | struct transfer_args ta; |
|
|
848 | |
|
|
849 | prepare_transfer (aTHX_ &ta, prev, next, flags); |
|
|
850 | TRANSFER (ta); |
856 | } |
851 | } |
857 | |
852 | |
858 | /** Coro ********************************************************************/ |
853 | /** Coro ********************************************************************/ |
859 | |
854 | |
860 | #define PRIO_MAX 3 |
855 | #define PRIO_MAX 3 |
… | |
… | |
914 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
909 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
915 | UNLOCK; |
910 | UNLOCK; |
916 | } |
911 | } |
917 | |
912 | |
918 | static void |
913 | static void |
919 | api_schedule (void) |
914 | prepare_schedule (aTHX_ struct transfer_args *ta) |
920 | { |
915 | { |
921 | dTHX; |
|
|
922 | |
|
|
923 | SV *prev, *next; |
916 | SV *current, *prev, *next; |
|
|
917 | |
|
|
918 | LOCK; |
|
|
919 | |
924 | SV *current = GvSV (coro_current); |
920 | current = GvSV (coro_current); |
925 | |
921 | |
926 | for (;;) |
922 | for (;;) |
927 | { |
923 | { |
928 | LOCK; |
924 | LOCK; |
929 | |
925 | |
… | |
… | |
951 | |
947 | |
952 | prev = SvRV (current); |
948 | prev = SvRV (current); |
953 | SvRV (current) = next; |
949 | SvRV (current) = next; |
954 | |
950 | |
955 | /* free this only after the transfer */ |
951 | /* free this only after the transfer */ |
|
|
952 | free_coro_mortal (); |
956 | coro_mortal = prev; |
953 | coro_mortal = prev; |
957 | |
954 | |
|
|
955 | ta->prev = SvSTATE (prev); |
|
|
956 | ta->next = SvSTATE (next); |
|
|
957 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
958 | |
958 | UNLOCK; |
959 | UNLOCK; |
959 | |
|
|
960 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
|
|
961 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
962 | } |
960 | } |
963 | |
961 | |
964 | static void |
962 | static void |
965 | api_cede (void) |
963 | prepare_cede (aTHX_ struct transfer_args *ta) |
966 | { |
964 | { |
967 | dTHX; |
|
|
968 | |
|
|
969 | LOCK; |
965 | LOCK; |
970 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
966 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
971 | UNLOCK; |
967 | UNLOCK; |
972 | |
968 | |
|
|
969 | prepare_schedule (ta); |
|
|
970 | } |
|
|
971 | |
|
|
972 | static void |
973 | api_schedule (); |
973 | api_schedule (void) |
|
|
974 | { |
|
|
975 | dTHX; |
|
|
976 | struct transfer_args ta; |
|
|
977 | |
|
|
978 | prepare_schedule (&ta); |
|
|
979 | TRANSFER (ta); |
|
|
980 | } |
|
|
981 | |
|
|
982 | static void |
|
|
983 | api_cede (void) |
|
|
984 | { |
|
|
985 | dTHX; |
|
|
986 | struct transfer_args ta; |
|
|
987 | |
|
|
988 | prepare_cede (&ta); |
|
|
989 | TRANSFER (ta); |
974 | } |
990 | } |
975 | |
991 | |
976 | MODULE = Coro::State PACKAGE = Coro::State |
992 | MODULE = Coro::State PACKAGE = Coro::State |
977 | |
993 | |
978 | PROTOTYPES: DISABLE |
994 | PROTOTYPES: DISABLE |
… | |
… | |
987 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1003 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
988 | |
1004 | |
989 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1005 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
990 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1006 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
991 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1007 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
992 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
|
|
993 | |
1008 | |
994 | main_mainstack = PL_mainstack; |
1009 | main_mainstack = PL_mainstack; |
995 | |
1010 | |
996 | coroapi.ver = CORO_API_VERSION; |
1011 | coroapi.ver = CORO_API_VERSION; |
997 | coroapi.transfer = api_transfer; |
1012 | coroapi.transfer = api_transfer; |
… | |
… | |
1008 | int i; |
1023 | int i; |
1009 | |
1024 | |
1010 | Newz (0, coro, 1, struct coro); |
1025 | Newz (0, coro, 1, struct coro); |
1011 | coro->args = newAV (); |
1026 | coro->args = newAV (); |
1012 | |
1027 | |
1013 | for (i = 1; i < items; i++) |
|
|
1014 | av_push (coro->args, newSVsv (ST (i))); |
|
|
1015 | |
|
|
1016 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1017 | /*coro->stack = 0;*/ |
|
|
1018 | |
|
|
1019 | hv = newHV (); |
1028 | hv = newHV (); |
1020 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1029 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1021 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1030 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
1031 | |
|
|
1032 | for (i = 1; i < items; i++) |
|
|
1033 | av_push (coro->args, newSVsv (ST (i))); |
|
|
1034 | |
|
|
1035 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1036 | /*coro->stack = 0;*/ |
1022 | } |
1037 | } |
1023 | OUTPUT: |
1038 | OUTPUT: |
1024 | RETVAL |
1039 | RETVAL |
1025 | |
1040 | |
1026 | void |
1041 | void |
1027 | transfer(prev, next, flags) |
1042 | _set_stacklevel (...) |
1028 | SV *prev |
1043 | ALIAS: |
1029 | SV *next |
1044 | Coro::State::transfer = 1 |
1030 | int flags |
1045 | Coro::schedule = 2 |
|
|
1046 | Coro::cede = 3 |
|
|
1047 | Coro::Cont::yield = 4 |
1031 | CODE: |
1048 | CODE: |
1032 | PUTBACK; |
|
|
1033 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
|
|
1034 | SPAGAIN; |
|
|
1035 | |
|
|
1036 | int |
|
|
1037 | prio (Coro::State coro, int newprio = 0) |
|
|
1038 | ALIAS: |
|
|
1039 | nice = 1 |
|
|
1040 | CODE: |
|
|
1041 | { |
1049 | { |
1042 | RETVAL = coro->prio; |
1050 | struct transfer_args ta; |
1043 | |
1051 | |
1044 | if (items > 1) |
1052 | switch (ix) |
1045 | { |
1053 | { |
|
|
1054 | case 0: |
|
|
1055 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
|
|
1056 | ta.next = 0; |
|
|
1057 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1058 | break; |
|
|
1059 | |
|
|
1060 | case 1: |
1046 | if (ix) |
1061 | if (items != 3) |
1047 | newprio += coro->prio; |
1062 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1048 | |
1063 | |
1049 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1064 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1050 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1065 | break; |
1051 | |
1066 | |
1052 | coro->prio = newprio; |
1067 | case 2: |
|
|
1068 | prepare_schedule (&ta); |
|
|
1069 | break; |
|
|
1070 | |
|
|
1071 | case 3: |
|
|
1072 | prepare_cede (&ta); |
|
|
1073 | break; |
|
|
1074 | |
|
|
1075 | case 4: |
|
|
1076 | { |
|
|
1077 | SV *yieldstack; |
|
|
1078 | SV *sv; |
|
|
1079 | AV *defav = GvAV (PL_defgv); |
|
|
1080 | |
|
|
1081 | yieldstack = *hv_fetch ( |
|
|
1082 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1083 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1084 | 0 |
|
|
1085 | ); |
|
|
1086 | |
|
|
1087 | /* set up @_ -- ugly */ |
|
|
1088 | av_clear (defav); |
|
|
1089 | av_fill (defav, items - 1); |
|
|
1090 | while (items--) |
|
|
1091 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1092 | |
|
|
1093 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1094 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1095 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1096 | ta.flags = 0; |
|
|
1097 | SvREFCNT_dec (sv); |
|
|
1098 | } |
|
|
1099 | break; |
|
|
1100 | |
1053 | } |
1101 | } |
|
|
1102 | |
|
|
1103 | TRANSFER (ta); |
1054 | } |
1104 | } |
1055 | |
1105 | |
1056 | void |
1106 | void |
1057 | _clone_state_from (SV *dst, SV *src) |
1107 | _clone_state_from (SV *dst, SV *src) |
1058 | CODE: |
1108 | CODE: |
… | |
… | |
1062 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1112 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1063 | |
1113 | |
1064 | ++coro_src->refcnt; |
1114 | ++coro_src->refcnt; |
1065 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1115 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1066 | } |
1116 | } |
|
|
1117 | |
|
|
1118 | void |
|
|
1119 | _nonlocal_goto (IV nextop) |
|
|
1120 | CODE: |
|
|
1121 | /* uuh, somebody will kill me again for this */ |
|
|
1122 | PL_op->op_next = INT2PTR (OP *, nextop); |
1067 | |
1123 | |
1068 | void |
1124 | void |
1069 | _exit (code) |
1125 | _exit (code) |
1070 | int code |
1126 | int code |
1071 | PROTOTYPE: $ |
1127 | PROTOTYPE: $ |
1072 | CODE: |
1128 | CODE: |
1073 | _exit (code); |
1129 | _exit (code); |
1074 | |
1130 | |
1075 | MODULE = Coro::State PACKAGE = Coro::Cont |
|
|
1076 | |
|
|
1077 | void |
|
|
1078 | yield (...) |
|
|
1079 | PROTOTYPE: @ |
|
|
1080 | CODE: |
|
|
1081 | { |
|
|
1082 | SV *yieldstack; |
|
|
1083 | SV *sv; |
|
|
1084 | AV *defav = GvAV (PL_defgv); |
|
|
1085 | struct coro *prev, *next; |
|
|
1086 | |
|
|
1087 | yieldstack = *hv_fetch ( |
|
|
1088 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1089 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1090 | 0 |
|
|
1091 | ); |
|
|
1092 | |
|
|
1093 | /* set up @_ -- ugly */ |
|
|
1094 | av_clear (defav); |
|
|
1095 | av_fill (defav, items - 1); |
|
|
1096 | while (items--) |
|
|
1097 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1098 | |
|
|
1099 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1100 | prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1101 | next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1102 | SvREFCNT_dec (sv); |
|
|
1103 | |
|
|
1104 | coro_state_transfer (aTHX_ prev, next, 0); |
|
|
1105 | } |
|
|
1106 | |
|
|
1107 | MODULE = Coro::State PACKAGE = Coro |
1131 | MODULE = Coro::State PACKAGE = Coro |
1108 | |
1132 | |
1109 | BOOT: |
1133 | BOOT: |
1110 | { |
1134 | { |
1111 | int i; |
1135 | int i; |
… | |
… | |
1138 | sv_setiv (sv, (IV)&coroapi); |
1162 | sv_setiv (sv, (IV)&coroapi); |
1139 | SvREADONLY_on (sv); |
1163 | SvREADONLY_on (sv); |
1140 | } |
1164 | } |
1141 | } |
1165 | } |
1142 | |
1166 | |
1143 | #if !PERL_MICRO |
1167 | int |
|
|
1168 | prio (Coro::State coro, int newprio = 0) |
|
|
1169 | ALIAS: |
|
|
1170 | nice = 1 |
|
|
1171 | CODE: |
|
|
1172 | { |
|
|
1173 | RETVAL = coro->prio; |
|
|
1174 | |
|
|
1175 | if (items > 1) |
|
|
1176 | { |
|
|
1177 | if (ix) |
|
|
1178 | newprio += coro->prio; |
|
|
1179 | |
|
|
1180 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
1181 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
1182 | |
|
|
1183 | coro->prio = newprio; |
|
|
1184 | } |
|
|
1185 | } |
1144 | |
1186 | |
1145 | void |
1187 | void |
1146 | ready (self) |
1188 | ready (SV *self) |
1147 | SV * self |
|
|
1148 | PROTOTYPE: $ |
1189 | PROTOTYPE: $ |
1149 | CODE: |
1190 | CODE: |
1150 | api_ready (self); |
1191 | api_ready (self); |
1151 | |
|
|
1152 | #endif |
|
|
1153 | |
1192 | |
1154 | int |
1193 | int |
1155 | nready (...) |
1194 | nready (...) |
1156 | PROTOTYPE: |
1195 | PROTOTYPE: |
1157 | CODE: |
1196 | CODE: |
1158 | RETVAL = coro_nready; |
1197 | RETVAL = coro_nready; |
1159 | OUTPUT: |
1198 | OUTPUT: |
1160 | RETVAL |
1199 | RETVAL |
1161 | |
1200 | |
1162 | void |
1201 | MODULE = Coro::State PACKAGE = Coro::AIO |
1163 | schedule (...) |
|
|
1164 | PROTOTYPE: |
|
|
1165 | CODE: |
|
|
1166 | api_schedule (); |
|
|
1167 | |
1202 | |
1168 | void |
|
|
1169 | cede (...) |
|
|
1170 | PROTOTYPE: |
|
|
1171 | CODE: |
|
|
1172 | api_cede (); |
|
|
1173 | |
|
|
1174 | # and these are hacks |
|
|
1175 | SV * |
1203 | SV * |
1176 | _aio_get_state () |
1204 | _get_state () |
1177 | CODE: |
1205 | CODE: |
1178 | { |
1206 | { |
1179 | struct { |
1207 | struct { |
1180 | int errorno; |
1208 | int errorno; |
1181 | int laststype; |
1209 | int laststype; |
… | |
… | |
1192 | } |
1220 | } |
1193 | OUTPUT: |
1221 | OUTPUT: |
1194 | RETVAL |
1222 | RETVAL |
1195 | |
1223 | |
1196 | void |
1224 | void |
1197 | _aio_set_state (char *data_) |
1225 | _set_state (char *data_) |
1198 | PROTOTYPE: $ |
1226 | PROTOTYPE: $ |
1199 | CODE: |
1227 | CODE: |
1200 | { |
1228 | { |
1201 | struct { |
1229 | struct { |
1202 | int errorno; |
1230 | int errorno; |