… | |
… | |
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 | #define UCORO_STATE "_coro_state" |
|
|
64 | |
|
|
65 | /* 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 |
66 | * 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 |
67 | * and should be unique. */ |
65 | * and should be unique. */ |
|
|
66 | #define dSTACKLEVEL int stacklevel |
68 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
67 | #define STACKLEVEL ((void *)&stacklevel) |
69 | |
68 | |
70 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
71 | |
70 | |
72 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
73 | |
|
|
74 | #include "CoroAPI.h" |
71 | #include "CoroAPI.h" |
|
|
72 | |
|
|
73 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
75 | |
74 | |
76 | #ifdef USE_ITHREADS |
75 | #ifdef USE_ITHREADS |
77 | static perl_mutex coro_mutex; |
76 | static perl_mutex coro_mutex; |
78 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
77 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
79 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
78 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
… | |
… | |
82 | # define UNLOCK (void)0 |
81 | # define UNLOCK (void)0 |
83 | #endif |
82 | #endif |
84 | |
83 | |
85 | static struct CoroAPI coroapi; |
84 | static struct CoroAPI coroapi; |
86 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
87 | static HV *coro_state_stash; |
86 | static HV *coro_state_stash, *coro_stash; |
88 | static SV *ucoro_state_sv; |
|
|
89 | static U32 ucoro_state_hash; |
|
|
90 | static SV *coro_mortal; /* will be freed after next transfer */ |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
91 | |
88 | |
92 | /* this is actually not only the c stack but also c registers etc... */ |
89 | /* this is a structure representing a c-level coroutine */ |
93 | typedef struct { |
90 | typedef struct coro_stack { |
94 | int refcnt; /* pointer reference counter */ |
91 | struct coro_stack *next; |
95 | int usecnt; /* shared by how many coroutines */ |
|
|
96 | int gencnt; /* generation counter */ |
|
|
97 | |
92 | |
98 | coro_context cctx; |
93 | /* the stack */ |
99 | |
|
|
100 | void *sptr; |
94 | void *sptr; |
101 | 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; |
102 | } coro_stack; |
101 | } coro_stack; |
103 | |
102 | |
|
|
103 | /* this is a structure representing a perl-level coroutine */ |
104 | struct coro { |
104 | struct coro { |
105 | /* the optional C context */ |
105 | /* the c coroutine allocated to this perl coroutine, if any */ |
106 | coro_stack *stack; |
106 | coro_stack *stack; |
107 | void *cursp; |
107 | |
|
|
108 | /* data associated with this coroutine (initial args) */ |
|
|
109 | AV *args; |
108 | int gencnt; |
110 | int refcnt; |
109 | |
111 | |
110 | /* optionally saved, might be zero */ |
112 | /* optionally saved, might be zero */ |
111 | AV *defav; |
113 | AV *defav; |
112 | SV *defsv; |
114 | SV *defsv; |
113 | SV *errsv; |
115 | SV *errsv; |
… | |
… | |
143 | OP **retstack; |
145 | OP **retstack; |
144 | I32 retstack_ix; |
146 | I32 retstack_ix; |
145 | I32 retstack_max; |
147 | I32 retstack_max; |
146 | PMOP *curpm; |
148 | PMOP *curpm; |
147 | COP *curcop; |
149 | COP *curcop; |
148 | JMPENV *top_env; |
|
|
149 | |
150 | |
150 | /* coro process data */ |
151 | /* coro process data */ |
151 | int prio; |
152 | int prio; |
152 | |
|
|
153 | /* data associated with this coroutine (initial args) */ |
|
|
154 | AV *args; |
|
|
155 | int refcnt; |
|
|
156 | }; |
153 | }; |
157 | |
154 | |
158 | typedef struct coro *Coro__State; |
155 | typedef struct coro *Coro__State; |
159 | typedef struct coro *Coro__State_or_hashref; |
156 | typedef struct coro *Coro__State_or_hashref; |
160 | |
157 | |
… | |
… | |
232 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
229 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
233 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
230 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
234 | else |
231 | else |
235 | { |
232 | { |
236 | #if 0 |
233 | #if 0 |
237 | /* this should work - but it doesn't :( */ |
234 | /* this is probably cleaner, but also slower? */ |
238 | CV *cp = Perl_cv_clone (aTHX_ cv); |
235 | CV *cp = Perl_cv_clone (aTHX_ cv); |
239 | CvPADLIST (cv) = CvPADLIST (cp); |
236 | CvPADLIST (cv) = CvPADLIST (cp); |
240 | CvPADLIST (cp) = 0; |
237 | CvPADLIST (cp) = 0; |
241 | SvREFCNT_dec (cp); |
238 | SvREFCNT_dec (cp); |
242 | #else |
239 | #else |
… | |
… | |
303 | PL_retstack_ix = c->retstack_ix; |
300 | PL_retstack_ix = c->retstack_ix; |
304 | PL_retstack_max = c->retstack_max; |
301 | PL_retstack_max = c->retstack_max; |
305 | #endif |
302 | #endif |
306 | PL_curpm = c->curpm; |
303 | PL_curpm = c->curpm; |
307 | PL_curcop = c->curcop; |
304 | PL_curcop = c->curcop; |
308 | PL_top_env = c->top_env; |
|
|
309 | |
305 | |
310 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
306 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
311 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
307 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
312 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
308 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
313 | |
309 | |
… | |
… | |
431 | c->retstack_ix = PL_retstack_ix; |
427 | c->retstack_ix = PL_retstack_ix; |
432 | c->retstack_max = PL_retstack_max; |
428 | c->retstack_max = PL_retstack_max; |
433 | #endif |
429 | #endif |
434 | c->curpm = PL_curpm; |
430 | c->curpm = PL_curpm; |
435 | c->curcop = PL_curcop; |
431 | c->curcop = PL_curcop; |
436 | c->top_env = PL_top_env; |
|
|
437 | } |
432 | } |
438 | |
433 | |
439 | /* |
434 | /* |
440 | * allocate various perl stacks. This is an exact copy |
435 | * allocate various perl stacks. This is an exact copy |
441 | * of perl.c:init_stacks, except that it uses less memory |
436 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
535 | Safefree (PL_retstack); |
530 | Safefree (PL_retstack); |
536 | #endif |
531 | #endif |
537 | } |
532 | } |
538 | |
533 | |
539 | static void |
534 | static void |
540 | allocate_stack (Coro__State ctx, int alloc) |
535 | setup_coro (struct coro *coro) |
541 | { |
|
|
542 | coro_stack *stack; |
|
|
543 | |
|
|
544 | New (0, stack, 1, coro_stack); |
|
|
545 | |
|
|
546 | stack->refcnt = 1; |
|
|
547 | stack->usecnt = 1; |
|
|
548 | stack->gencnt = ctx->gencnt = 0; |
|
|
549 | |
|
|
550 | if (alloc) |
|
|
551 | { |
|
|
552 | #if HAVE_MMAP |
|
|
553 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
|
|
554 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
|
|
555 | if (stack->sptr != (void *)-1) |
|
|
556 | { |
|
|
557 | # if STACKGUARD |
|
|
558 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
559 | # endif |
|
|
560 | } |
|
|
561 | else |
|
|
562 | #endif |
|
|
563 | { |
|
|
564 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
|
|
565 | New (0, stack->sptr, STACKSIZE, long); |
|
|
566 | } |
|
|
567 | } |
|
|
568 | else |
|
|
569 | stack->sptr = 0; |
|
|
570 | |
|
|
571 | ctx->stack = stack; |
|
|
572 | } |
|
|
573 | |
|
|
574 | static void |
|
|
575 | deallocate_stack (Coro__State ctx) |
|
|
576 | { |
|
|
577 | coro_stack *stack = ctx->stack; |
|
|
578 | |
|
|
579 | ctx->stack = 0; |
|
|
580 | |
|
|
581 | if (stack) |
|
|
582 | { |
|
|
583 | if (!--stack->refcnt) |
|
|
584 | { |
|
|
585 | #ifdef HAVE_MMAP |
|
|
586 | if (stack->ssize > 0 && stack->sptr) |
|
|
587 | munmap (stack->sptr, stack->ssize); |
|
|
588 | else |
|
|
589 | #endif |
|
|
590 | Safefree (stack->sptr); |
|
|
591 | |
|
|
592 | Safefree (stack); |
|
|
593 | } |
|
|
594 | else if (ctx->gencnt == stack->gencnt) |
|
|
595 | --stack->usecnt; |
|
|
596 | } |
|
|
597 | } |
|
|
598 | |
|
|
599 | static void |
|
|
600 | setup_coro (void *arg) |
|
|
601 | { |
536 | { |
602 | /* |
537 | /* |
603 | * emulate part of the perl startup here. |
538 | * emulate part of the perl startup here. |
604 | */ |
539 | */ |
605 | dTHX; |
540 | dTHX; |
606 | dSP; |
541 | dSP; |
607 | Coro__State ctx = (Coro__State)arg; |
542 | UNOP myop; |
608 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
543 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
609 | |
544 | |
610 | coro_init_stacks (aTHX); |
545 | coro_init_stacks (aTHX); |
611 | /*PL_curcop = 0;*/ |
546 | /*PL_curcop = 0;*/ |
612 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
547 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
613 | SvREFCNT_dec (GvAV (PL_defgv)); |
548 | SvREFCNT_dec (GvAV (PL_defgv)); |
614 | GvAV (PL_defgv) = ctx->args; ctx->args = 0; |
549 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
615 | |
550 | |
616 | SPAGAIN; |
551 | SPAGAIN; |
617 | |
552 | |
618 | 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) |
619 | { |
572 | { |
620 | ctx->cursp = 0; |
573 | dTHX; |
621 | |
574 | |
622 | PUSHMARK(SP); |
575 | SvREFCNT_dec (coro_mortal); |
623 | PUTBACK; |
576 | coro_mortal = 0; |
624 | (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); |
|
|
625 | |
|
|
626 | if (SvTRUE (ERRSV)) |
|
|
627 | croak (NULL); |
|
|
628 | else |
|
|
629 | croak ("FATAL: CCTXT coroutine returned!"); |
|
|
630 | } |
577 | } |
631 | else |
|
|
632 | { |
|
|
633 | UNOP myop; |
|
|
634 | |
|
|
635 | Zero(&myop, 1, UNOP); |
|
|
636 | myop.op_next = Nullop; |
|
|
637 | myop.op_flags = OPf_WANT_VOID; |
|
|
638 | |
|
|
639 | PL_op = (OP *)&myop; |
|
|
640 | |
|
|
641 | PUSHMARK(SP); |
|
|
642 | XPUSHs (sub_init); |
|
|
643 | /* |
|
|
644 | * the next line is slightly wrong, as PL_op->op_next |
|
|
645 | * is actually being executed so we skip the first op. |
|
|
646 | * that doesn't matter, though, since it is only |
|
|
647 | * pp_nextstate and we never return... |
|
|
648 | * ah yes, and I don't care anyways ;) |
|
|
649 | */ |
|
|
650 | PUTBACK; |
|
|
651 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
652 | SPAGAIN; |
|
|
653 | |
|
|
654 | ENTER; /* necessary e.g. for dounwind */ |
|
|
655 | } |
|
|
656 | } |
578 | } |
657 | |
579 | |
658 | static void |
580 | static void |
659 | continue_coro (void *arg) |
581 | coro_run (void *arg) |
660 | { |
582 | { |
661 | /* |
583 | /* |
662 | * this is a _very_ stripped down perl interpreter ;) |
584 | * this is a _very_ stripped down perl interpreter ;) |
663 | */ |
585 | */ |
664 | dTHX; |
586 | dTHX; |
665 | Coro__State ctx = (Coro__State)arg; |
587 | int ret; |
|
|
588 | |
|
|
589 | UNLOCK; |
666 | |
590 | |
667 | PL_top_env = &PL_start_env; |
591 | PL_top_env = &PL_start_env; |
668 | |
592 | |
669 | ctx->cursp = 0; |
593 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); |
670 | PL_op = PL_op->op_next; |
594 | sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); |
671 | CALLRUNOPS(aTHX); |
|
|
672 | |
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); |
673 | abort (); |
604 | abort (); |
674 | } |
605 | } |
675 | |
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 | |
676 | /* never call directly, always through the coro_state_transfer global variable */ |
691 | /* never call directly, always through the coro_state_transfer global variable */ |
677 | static void |
692 | static void |
678 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
693 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
679 | { |
694 | { |
680 | dSTACKLEVEL; |
695 | dSTACKLEVEL; |
681 | |
696 | |
|
|
697 | /* sometimes transfer is only called to set idle_sp */ |
|
|
698 | if (flags == TRANSFER_SET_STACKLEVEL) |
|
|
699 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
682 | if (prev != next) |
700 | else if (prev != next) |
683 | { |
701 | { |
684 | /* has this coro been created yet? */ |
702 | coro_stack *prev__stack; |
|
|
703 | |
|
|
704 | LOCK; |
|
|
705 | |
685 | if (next->mainstack) |
706 | if (next->mainstack) |
686 | { |
707 | { |
687 | LOCK; |
708 | /* coroutine already started */ |
688 | SAVE (prev, flags); |
709 | SAVE (prev, flags); |
689 | LOAD (next); |
710 | LOAD (next); |
690 | UNLOCK; |
|
|
691 | |
|
|
692 | /* mark this state as in-use */ |
|
|
693 | next->mainstack = 0; |
|
|
694 | next->tmps_ix = -2; |
|
|
695 | |
|
|
696 | /* stacklevel changed? if yes, grab the stack for us! */ |
|
|
697 | if (flags & TRANSFER_SAVE_CCTXT) |
|
|
698 | { |
|
|
699 | if (!prev->stack) |
|
|
700 | allocate_stack (prev, 0); |
|
|
701 | else if (prev->cursp != stacklevel |
|
|
702 | && prev->stack->usecnt > 1) |
|
|
703 | { |
|
|
704 | prev->gencnt = ++prev->stack->gencnt; |
|
|
705 | prev->stack->usecnt = 1; |
|
|
706 | } |
|
|
707 | |
|
|
708 | /* has our stack been invalidated? */ |
|
|
709 | if (next->stack && next->stack->gencnt != next->gencnt) |
|
|
710 | { |
|
|
711 | deallocate_stack (next); |
|
|
712 | allocate_stack (next, 1); |
|
|
713 | coro_create (&(next->stack->cctx), |
|
|
714 | continue_coro, (void *)next, |
|
|
715 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
716 | } |
|
|
717 | |
|
|
718 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
719 | prev->cursp = stacklevel; |
|
|
720 | /* don't add any code here */ |
|
|
721 | } |
|
|
722 | else |
|
|
723 | next->cursp = stacklevel; |
|
|
724 | } |
711 | } |
725 | else if (next->tmps_ix == -2) |
|
|
726 | croak ("tried to transfer to running coroutine"); |
|
|
727 | else |
712 | else |
728 | { |
713 | { |
729 | LOCK; |
714 | /* need to start coroutine */ |
730 | SAVE (prev, -1); /* first get rid of the old state */ |
715 | /* first get rid of the old state */ |
731 | UNLOCK; |
716 | SAVE (prev, -1); |
732 | |
717 | /* setup coroutine call */ |
733 | /* create the coroutine for the first time */ |
|
|
734 | if (flags & TRANSFER_SAVE_CCTXT) |
|
|
735 | { |
|
|
736 | if (!prev->stack) |
|
|
737 | allocate_stack (prev, 0); |
|
|
738 | |
|
|
739 | /* the new coroutine starts with start_env again */ |
|
|
740 | PL_top_env = &PL_start_env; |
|
|
741 | |
|
|
742 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
|
|
743 | { |
|
|
744 | setup_coro (next); |
|
|
745 | next->cursp = stacklevel; |
|
|
746 | |
|
|
747 | prev->stack->refcnt++; |
|
|
748 | prev->stack->usecnt++; |
|
|
749 | next->stack = prev->stack; |
|
|
750 | next->gencnt = prev->gencnt; |
|
|
751 | } |
|
|
752 | else |
|
|
753 | { |
|
|
754 | assert (!next->stack); |
|
|
755 | allocate_stack (next, 1); |
|
|
756 | coro_create (&(next->stack->cctx), |
|
|
757 | setup_coro, (void *)next, |
|
|
758 | next->stack->sptr, labs (next->stack->ssize)); |
|
|
759 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
760 | prev->cursp = stacklevel; |
|
|
761 | /* don't add any code here */ |
|
|
762 | } |
|
|
763 | } |
|
|
764 | else |
|
|
765 | { |
|
|
766 | setup_coro (next); |
718 | setup_coro (next); |
767 | next->cursp = stacklevel; |
719 | /* need a stack */ |
768 | } |
720 | next->stack = 0; |
769 | } |
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; |
770 | } |
749 | } |
771 | |
|
|
772 | LOCK; |
|
|
773 | if (coro_mortal) |
|
|
774 | { |
|
|
775 | SvREFCNT_dec (coro_mortal); |
|
|
776 | coro_mortal = 0; |
|
|
777 | } |
|
|
778 | UNLOCK; |
|
|
779 | } |
750 | } |
780 | |
751 | |
781 | /* use this function pointer to call the above function */ |
752 | /* use this function pointer to call the above function */ |
782 | /* 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 */ |
783 | 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; |
784 | |
756 | |
785 | #define SV_CORO(sv,func) \ |
757 | struct transfer_args |
786 | do { \ |
758 | { |
787 | if (SvROK (sv)) \ |
759 | struct coro *prev, *next; |
788 | sv = SvRV (sv); \ |
760 | int flags; |
789 | \ |
761 | }; |
790 | if (SvTYPE (sv) == SVt_PVHV && SvSTASH (sv) != coro_state_stash) \ |
762 | |
791 | { \ |
763 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
792 | HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
|
|
793 | \ |
|
|
794 | if (!he) \ |
|
|
795 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
|
|
796 | \ |
|
|
797 | (sv) = SvRV (HeVAL(he)); \ |
|
|
798 | } \ |
|
|
799 | \ |
|
|
800 | /* must also be changed inside Coro::Cont::yield */ \ |
|
|
801 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
|
|
802 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
|
|
803 | \ |
|
|
804 | } while(0) |
|
|
805 | |
764 | |
806 | static void |
765 | static void |
807 | coro_state_destroy (struct coro *coro) |
766 | coro_state_destroy (struct coro *coro) |
808 | { |
767 | { |
809 | if (coro->refcnt--) |
768 | if (coro->refcnt--) |
… | |
… | |
821 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
780 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
822 | |
781 | |
823 | coro->mainstack = 0; |
782 | coro->mainstack = 0; |
824 | } |
783 | } |
825 | |
784 | |
826 | deallocate_stack (coro); |
785 | stack_free (coro->stack); |
827 | SvREFCNT_dec (coro->args); |
786 | SvREFCNT_dec (coro->args); |
828 | Safefree (coro); |
787 | Safefree (coro); |
829 | } |
788 | } |
830 | |
789 | |
831 | static int |
790 | static int |
… | |
… | |
852 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
811 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
853 | |
812 | |
854 | static struct coro * |
813 | static struct coro * |
855 | SvSTATE (SV *coro) |
814 | SvSTATE (SV *coro) |
856 | { |
815 | { |
857 | MAGIC *mg = SvMAGIC (SvROK (coro) ? SvRV (coro) : coro); |
816 | HV *stash; |
|
|
817 | MAGIC *mg; |
|
|
818 | |
|
|
819 | if (SvROK (coro)) |
|
|
820 | coro = SvRV (coro); |
|
|
821 | |
|
|
822 | stash = SvSTASH (coro); |
|
|
823 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
824 | { |
|
|
825 | /* very slow, but rare, check */ |
|
|
826 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
827 | croak ("Coro::State object required"); |
|
|
828 | } |
|
|
829 | |
|
|
830 | mg = SvMAGIC (coro); |
858 | assert (mg->mg_type == PERL_MAGIC_ext); |
831 | assert (mg->mg_type == PERL_MAGIC_ext); |
859 | return (struct coro *)mg->mg_ptr; |
832 | return (struct coro *)mg->mg_ptr; |
860 | } |
833 | } |
861 | |
834 | |
862 | 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 |
863 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
844 | api_transfer (SV *prev, SV *next, int flags) |
864 | { |
845 | { |
865 | SV_CORO (prev, "Coro::transfer"); |
846 | dTHX; |
866 | SV_CORO (next, "Coro::transfer"); |
847 | struct transfer_args ta; |
867 | |
848 | |
868 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
849 | prepare_transfer (aTHX_ &ta, prev, next, flags); |
|
|
850 | TRANSFER (ta); |
869 | } |
851 | } |
870 | |
852 | |
871 | /** Coro ********************************************************************/ |
853 | /** Coro ********************************************************************/ |
872 | |
854 | |
873 | #define PRIO_MAX 3 |
855 | #define PRIO_MAX 3 |
… | |
… | |
888 | int prio; |
870 | int prio; |
889 | |
871 | |
890 | if (SvTYPE (sv) != SVt_PVHV) |
872 | if (SvTYPE (sv) != SVt_PVHV) |
891 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
873 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
892 | |
874 | |
893 | { |
|
|
894 | SV *coro = sv; |
|
|
895 | SV_CORO (coro, "omg"); |
|
|
896 | prio = SvSTATE (coro)->prio; |
875 | prio = SvSTATE (sv)->prio; |
897 | } |
|
|
898 | |
876 | |
899 | av_push (coro_ready [prio - PRIO_MIN], sv); |
877 | av_push (coro_ready [prio - PRIO_MIN], sv); |
900 | coro_nready++; |
878 | coro_nready++; |
901 | } |
879 | } |
902 | |
880 | |
… | |
… | |
931 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
909 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
932 | UNLOCK; |
910 | UNLOCK; |
933 | } |
911 | } |
934 | |
912 | |
935 | static void |
913 | static void |
936 | api_schedule (void) |
914 | prepare_schedule (aTHX_ struct transfer_args *ta) |
937 | { |
915 | { |
938 | dTHX; |
|
|
939 | |
|
|
940 | SV *prev, *next; |
916 | SV *current, *prev, *next; |
941 | |
917 | |
942 | LOCK; |
918 | LOCK; |
943 | |
919 | |
944 | prev = SvRV (GvSV (coro_current)); |
920 | current = GvSV (coro_current); |
|
|
921 | |
|
|
922 | for (;;) |
|
|
923 | { |
|
|
924 | LOCK; |
|
|
925 | |
945 | next = coro_deq (aTHX_ PRIO_MIN); |
926 | next = coro_deq (aTHX_ PRIO_MIN); |
946 | |
927 | |
947 | if (!next) |
928 | if (next) |
948 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
929 | break; |
|
|
930 | |
|
|
931 | UNLOCK; |
|
|
932 | |
|
|
933 | { |
|
|
934 | dSP; |
|
|
935 | |
|
|
936 | ENTER; |
|
|
937 | SAVETMPS; |
|
|
938 | |
|
|
939 | PUSHMARK (SP); |
|
|
940 | PUTBACK; |
|
|
941 | call_sv (GvSV (coro_idle), G_DISCARD); |
|
|
942 | |
|
|
943 | FREETMPS; |
|
|
944 | LEAVE; |
|
|
945 | } |
|
|
946 | } |
|
|
947 | |
|
|
948 | prev = SvRV (current); |
|
|
949 | SvRV (current) = next; |
949 | |
950 | |
950 | /* free this only after the transfer */ |
951 | /* free this only after the transfer */ |
|
|
952 | free_coro_mortal (); |
951 | coro_mortal = prev; |
953 | coro_mortal = prev; |
952 | SV_CORO (prev, "Coro::schedule"); |
|
|
953 | |
954 | |
954 | SvRV (GvSV (coro_current)) = next; |
955 | ta->prev = SvSTATE (prev); |
955 | |
956 | ta->next = SvSTATE (next); |
956 | SV_CORO (next, "Coro::schedule"); |
957 | ta->flags = TRANSFER_SAVE_ALL; |
957 | |
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 |
979 | |
995 | |
980 | BOOT: |
996 | BOOT: |
981 | { /* {} necessary for stoopid perl-5.6.x */ |
997 | { |
982 | #ifdef USE_ITHREADS |
998 | #ifdef USE_ITHREADS |
983 | MUTEX_INIT (&coro_mutex); |
999 | MUTEX_INIT (&coro_mutex); |
984 | #endif |
1000 | #endif |
985 | BOOT_PAGESIZE; |
1001 | BOOT_PAGESIZE; |
986 | |
1002 | |
987 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
|
|
988 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
|
|
989 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1003 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
990 | |
1004 | |
991 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1005 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
992 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1006 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
993 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1007 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
994 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
|
|
995 | |
1008 | |
996 | main_mainstack = PL_mainstack; |
1009 | main_mainstack = PL_mainstack; |
997 | |
1010 | |
998 | coroapi.ver = CORO_API_VERSION; |
1011 | coroapi.ver = CORO_API_VERSION; |
999 | coroapi.transfer = api_transfer; |
1012 | coroapi.transfer = api_transfer; |
… | |
… | |
1010 | int i; |
1023 | int i; |
1011 | |
1024 | |
1012 | Newz (0, coro, 1, struct coro); |
1025 | Newz (0, coro, 1, struct coro); |
1013 | coro->args = newAV (); |
1026 | coro->args = newAV (); |
1014 | |
1027 | |
1015 | for (i = 1; i < items; i++) |
|
|
1016 | av_push (coro->args, newSVsv (ST (i))); |
|
|
1017 | |
|
|
1018 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1019 | /*coro->stack = 0;*/ |
|
|
1020 | |
|
|
1021 | hv = newHV (); |
1028 | hv = newHV (); |
1022 | 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; |
1023 | 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;*/ |
1024 | } |
1037 | } |
1025 | OUTPUT: |
1038 | OUTPUT: |
1026 | RETVAL |
1039 | RETVAL |
1027 | |
1040 | |
1028 | void |
1041 | void |
1029 | transfer(prev, next, flags) |
1042 | _set_stacklevel (...) |
1030 | SV *prev |
1043 | ALIAS: |
1031 | SV *next |
1044 | Coro::State::transfer = 1 |
1032 | int flags |
1045 | Coro::schedule = 2 |
|
|
1046 | Coro::cede = 3 |
|
|
1047 | Coro::Cont::yield = 4 |
1033 | CODE: |
1048 | CODE: |
1034 | PUTBACK; |
|
|
1035 | SV_CORO (next, "Coro::transfer"); |
|
|
1036 | SV_CORO (prev, "Coro::transfer"); |
|
|
1037 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
|
|
1038 | SPAGAIN; |
|
|
1039 | |
|
|
1040 | int |
|
|
1041 | prio (Coro::State coro, int newprio = 0) |
|
|
1042 | ALIAS: |
|
|
1043 | nice = 1 |
|
|
1044 | CODE: |
|
|
1045 | { |
1049 | { |
1046 | RETVAL = coro->prio; |
1050 | struct transfer_args ta; |
1047 | |
1051 | |
1048 | if (items > 1) |
1052 | switch (ix) |
1049 | { |
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: |
1050 | if (ix) |
1061 | if (items != 3) |
1051 | newprio += coro->prio; |
1062 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1052 | |
1063 | |
1053 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1064 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1054 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1065 | break; |
1055 | |
1066 | |
1056 | 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 | |
1057 | } |
1101 | } |
|
|
1102 | |
|
|
1103 | TRANSFER (ta); |
1058 | } |
1104 | } |
1059 | |
1105 | |
1060 | void |
1106 | void |
1061 | _clone_state_from (SV *dst, SV *src) |
1107 | _clone_state_from (SV *dst, SV *src) |
1062 | CODE: |
1108 | CODE: |
… | |
… | |
1066 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1112 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1067 | |
1113 | |
1068 | ++coro_src->refcnt; |
1114 | ++coro_src->refcnt; |
1069 | 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; |
1070 | } |
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); |
1071 | |
1123 | |
1072 | void |
1124 | void |
1073 | _exit (code) |
1125 | _exit (code) |
1074 | int code |
1126 | int code |
1075 | PROTOTYPE: $ |
1127 | PROTOTYPE: $ |
1076 | CODE: |
1128 | CODE: |
1077 | _exit (code); |
1129 | _exit (code); |
1078 | |
1130 | |
1079 | MODULE = Coro::State PACKAGE = Coro::Cont |
|
|
1080 | |
|
|
1081 | void |
|
|
1082 | yield (...) |
|
|
1083 | PROTOTYPE: @ |
|
|
1084 | CODE: |
|
|
1085 | SV *yieldstack; |
|
|
1086 | SV *sv; |
|
|
1087 | AV *defav = GvAV (PL_defgv); |
|
|
1088 | struct coro *prev, *next; |
|
|
1089 | |
|
|
1090 | yieldstack = *hv_fetch ( |
|
|
1091 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1092 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1093 | 0 |
|
|
1094 | ); |
|
|
1095 | |
|
|
1096 | /* set up @_ -- ugly */ |
|
|
1097 | av_clear (defav); |
|
|
1098 | av_fill (defav, items - 1); |
|
|
1099 | while (items--) |
|
|
1100 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1101 | |
|
|
1102 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1103 | prev = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
|
|
1104 | next = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
|
|
1105 | SvREFCNT_dec (sv); |
|
|
1106 | |
|
|
1107 | coro_state_transfer (aTHX_ prev, next, 0); |
|
|
1108 | |
|
|
1109 | MODULE = Coro::State PACKAGE = Coro |
1131 | MODULE = Coro::State PACKAGE = Coro |
1110 | |
1132 | |
1111 | BOOT: |
1133 | BOOT: |
1112 | { |
1134 | { |
1113 | int i; |
1135 | int i; |
|
|
1136 | |
1114 | HV *stash = gv_stashpv ("Coro", TRUE); |
1137 | coro_stash = gv_stashpv ("Coro", TRUE); |
1115 | |
1138 | |
1116 | newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1139 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1117 | newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1140 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1118 | newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1141 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1119 | newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1142 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1120 | newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1143 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1121 | newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1144 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1122 | |
1145 | |
1123 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1146 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1124 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1147 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1125 | |
1148 | |
1126 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1149 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
… | |
… | |
1139 | sv_setiv (sv, (IV)&coroapi); |
1162 | sv_setiv (sv, (IV)&coroapi); |
1140 | SvREADONLY_on (sv); |
1163 | SvREADONLY_on (sv); |
1141 | } |
1164 | } |
1142 | } |
1165 | } |
1143 | |
1166 | |
1144 | #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 | } |
1145 | |
1186 | |
1146 | void |
1187 | void |
1147 | ready (self) |
1188 | ready (SV *self) |
1148 | SV * self |
|
|
1149 | PROTOTYPE: $ |
1189 | PROTOTYPE: $ |
1150 | CODE: |
1190 | CODE: |
1151 | api_ready (self); |
1191 | api_ready (self); |
1152 | |
|
|
1153 | #endif |
|
|
1154 | |
1192 | |
1155 | int |
1193 | int |
1156 | nready (...) |
1194 | nready (...) |
1157 | PROTOTYPE: |
1195 | PROTOTYPE: |
1158 | CODE: |
1196 | CODE: |
1159 | RETVAL = coro_nready; |
1197 | RETVAL = coro_nready; |
1160 | OUTPUT: |
1198 | OUTPUT: |
1161 | RETVAL |
1199 | RETVAL |
1162 | |
1200 | |
1163 | void |
1201 | MODULE = Coro::State PACKAGE = Coro::AIO |
1164 | schedule (...) |
|
|
1165 | PROTOTYPE: |
|
|
1166 | CODE: |
|
|
1167 | api_schedule (); |
|
|
1168 | |
1202 | |
1169 | void |
|
|
1170 | cede (...) |
|
|
1171 | PROTOTYPE: |
|
|
1172 | CODE: |
|
|
1173 | api_cede (); |
|
|
1174 | |
|
|
1175 | # and these are hacks |
|
|
1176 | SV * |
1203 | SV * |
1177 | _aio_get_state () |
1204 | _get_state () |
1178 | CODE: |
1205 | CODE: |
1179 | { |
1206 | { |
1180 | struct { |
1207 | struct { |
1181 | int errorno; |
1208 | int errorno; |
1182 | int laststype; |
1209 | int laststype; |
… | |
… | |
1193 | } |
1220 | } |
1194 | OUTPUT: |
1221 | OUTPUT: |
1195 | RETVAL |
1222 | RETVAL |
1196 | |
1223 | |
1197 | void |
1224 | void |
1198 | _aio_set_state (char *data_) |
1225 | _set_state (char *data_) |
1199 | PROTOTYPE: $ |
1226 | PROTOTYPE: $ |
1200 | CODE: |
1227 | CODE: |
1201 | { |
1228 | { |
1202 | struct { |
1229 | struct { |
1203 | int errorno; |
1230 | int errorno; |