1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
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 | |
|
|
7 | #include <signal.h> |
6 | |
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> |
10 | # ifndef MAP_ANON |
12 | # ifndef MAP_ANON |
… | |
… | |
14 | # undef HAVE_MMAP |
16 | # undef HAVE_MMAP |
15 | # endif |
17 | # endif |
16 | # endif |
18 | # endif |
17 | #endif |
19 | #endif |
18 | |
20 | |
19 | #define MAY_FLUSH /* increases codesize */ |
21 | #define MAY_FLUSH /* increases codesize and is rarely used */ |
20 | |
|
|
21 | /* perl-related */ |
|
|
22 | #define TRANSFER_SAVE_DEFAV 0x00000001 |
|
|
23 | #define TRANSFER_SAVE_DEFSV 0x00000002 |
|
|
24 | #define TRANSFER_SAVE_ERRSV 0x00000004 |
|
|
25 | /* c-related */ |
|
|
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 |
|
|
32 | |
|
|
33 | #define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \ |
|
|
34 | |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT) |
|
|
35 | |
22 | |
36 | #define SUB_INIT "Coro::State::initialize" |
23 | #define SUB_INIT "Coro::State::initialize" |
37 | #define UCORO_STATE "_coro_state" |
24 | #define UCORO_STATE "_coro_state" |
38 | |
25 | |
39 | /* The next macro should delcare a variable stacklevel that contains and approximation |
26 | /* The next macro should declare a variable stacklevel that contains and approximation |
40 | * to the current C stack pointer. It's property is that it changes with each call |
27 | * to the current C stack pointer. Its property is that it changes with each call |
41 | * and should be unique. */ |
28 | * and should be unique. */ |
42 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
43 | |
30 | |
44 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
32 | |
|
|
33 | #include "CoroAPI.h" |
|
|
34 | |
|
|
35 | static struct CoroAPI coroapi; |
45 | |
36 | |
46 | /* this is actually not only the c stack but also c registers etc... */ |
37 | /* this is actually not only the c stack but also c registers etc... */ |
47 | typedef struct { |
38 | typedef struct { |
48 | int refcnt; /* pointer reference counter */ |
39 | int refcnt; /* pointer reference counter */ |
49 | int usecnt; /* shared by how many coroutines */ |
40 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
66 | SV *defsv; |
57 | SV *defsv; |
67 | SV *errsv; |
58 | SV *errsv; |
68 | |
59 | |
69 | /* saved global state not related to stacks */ |
60 | /* saved global state not related to stacks */ |
70 | U8 dowarn; |
61 | U8 dowarn; |
|
|
62 | I32 in_eval; |
71 | |
63 | |
72 | /* the stacks and related info (callchain etc..) */ |
64 | /* the stacks and related info (callchain etc..) */ |
73 | PERL_SI *curstackinfo; |
65 | PERL_SI *curstackinfo; |
74 | AV *curstack; |
66 | AV *curstack; |
75 | AV *mainstack; |
67 | AV *mainstack; |
… | |
… | |
93 | I32 savestack_max; |
85 | I32 savestack_max; |
94 | OP **retstack; |
86 | OP **retstack; |
95 | I32 retstack_ix; |
87 | I32 retstack_ix; |
96 | I32 retstack_max; |
88 | I32 retstack_max; |
97 | COP *curcop; |
89 | COP *curcop; |
98 | JMPENV start_env; |
|
|
99 | JMPENV *top_env; |
90 | JMPENV *top_env; |
100 | |
91 | |
101 | /* data associated with this coroutine (initial args) */ |
92 | /* data associated with this coroutine (initial args) */ |
102 | AV *args; |
93 | AV *args; |
103 | }; |
94 | }; |
… | |
… | |
204 | |
195 | |
205 | return newpadlist; |
196 | return newpadlist; |
206 | } |
197 | } |
207 | |
198 | |
208 | #ifdef MAY_FLUSH |
199 | #ifdef MAY_FLUSH |
209 | STATIC AV * |
200 | STATIC void |
210 | free_padlist (AV *padlist) |
201 | free_padlist (AV *padlist) |
211 | { |
202 | { |
212 | /* may be during global destruction */ |
203 | /* may be during global destruction */ |
213 | if (SvREFCNT(padlist)) |
204 | if (SvREFCNT(padlist)) |
214 | { |
205 | { |
… | |
… | |
279 | #endif |
270 | #endif |
280 | |
271 | |
281 | #define SB do { |
272 | #define SB do { |
282 | #define SE } while (0) |
273 | #define SE } while (0) |
283 | |
274 | |
284 | #define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE |
275 | #define LOAD(state) load_state(aTHX_ (state)); |
285 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE |
276 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
286 | |
277 | |
287 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
278 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
288 | |
279 | |
289 | static void |
280 | static void |
290 | load_state(pTHX_ Coro__State c) |
281 | load_state(pTHX_ Coro__State c) |
291 | { |
282 | { |
292 | PL_dowarn = c->dowarn; |
283 | PL_dowarn = c->dowarn; |
|
|
284 | PL_in_eval = c->in_eval; |
293 | |
285 | |
294 | PL_curstackinfo = c->curstackinfo; |
286 | PL_curstackinfo = c->curstackinfo; |
295 | PL_curstack = c->curstack; |
287 | PL_curstack = c->curstack; |
296 | PL_mainstack = c->mainstack; |
288 | PL_mainstack = c->mainstack; |
297 | PL_stack_sp = c->stack_sp; |
289 | PL_stack_sp = c->stack_sp; |
… | |
… | |
314 | PL_savestack_max = c->savestack_max; |
306 | PL_savestack_max = c->savestack_max; |
315 | PL_retstack = c->retstack; |
307 | PL_retstack = c->retstack; |
316 | PL_retstack_ix = c->retstack_ix; |
308 | PL_retstack_ix = c->retstack_ix; |
317 | PL_retstack_max = c->retstack_max; |
309 | PL_retstack_max = c->retstack_max; |
318 | PL_curcop = c->curcop; |
310 | PL_curcop = c->curcop; |
319 | PL_start_env = c->start_env; |
|
|
320 | PL_top_env = c->top_env; |
311 | PL_top_env = c->top_env; |
321 | |
312 | |
322 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
313 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
323 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
314 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
324 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
315 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
… | |
… | |
417 | |
408 | |
418 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
409 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
419 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
410 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
420 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
411 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
421 | |
412 | |
422 | /* I have not the slightest idea of why av_reify is necessary */ |
|
|
423 | /* but if it's missing the defav contents magically get replaced sometimes */ |
|
|
424 | if (c->defav) |
|
|
425 | av_reify (c->defav); |
|
|
426 | |
|
|
427 | c->dowarn = PL_dowarn; |
413 | c->dowarn = PL_dowarn; |
|
|
414 | c->in_eval = PL_in_eval; |
428 | |
415 | |
429 | c->curstackinfo = PL_curstackinfo; |
416 | c->curstackinfo = PL_curstackinfo; |
430 | c->curstack = PL_curstack; |
417 | c->curstack = PL_curstack; |
431 | c->mainstack = PL_mainstack; |
418 | c->mainstack = PL_mainstack; |
432 | c->stack_sp = PL_stack_sp; |
419 | c->stack_sp = PL_stack_sp; |
… | |
… | |
449 | c->savestack_max = PL_savestack_max; |
436 | c->savestack_max = PL_savestack_max; |
450 | c->retstack = PL_retstack; |
437 | c->retstack = PL_retstack; |
451 | c->retstack_ix = PL_retstack_ix; |
438 | c->retstack_ix = PL_retstack_ix; |
452 | c->retstack_max = PL_retstack_max; |
439 | c->retstack_max = PL_retstack_max; |
453 | c->curcop = PL_curcop; |
440 | c->curcop = PL_curcop; |
454 | c->start_env = PL_start_env; |
|
|
455 | c->top_env = PL_top_env; |
441 | c->top_env = PL_top_env; |
456 | } |
442 | } |
457 | |
443 | |
458 | /* |
444 | /* |
459 | * allocate various perl stacks. This is an exact copy |
445 | * allocate various perl stacks. This is an exact copy |
… | |
… | |
523 | dSP; |
509 | dSP; |
524 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
510 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
525 | PUTBACK; /* possibly superfluous */ |
511 | PUTBACK; /* possibly superfluous */ |
526 | } |
512 | } |
527 | |
513 | |
|
|
514 | if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ |
528 | dounwind(-1); |
515 | dounwind(-1); |
529 | |
516 | |
530 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
517 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
531 | Safefree(PL_curstackinfo->si_cxstack); |
518 | Safefree(PL_curstackinfo->si_cxstack); |
532 | Safefree(PL_curstackinfo); |
519 | Safefree(PL_curstackinfo); |
533 | PL_curstackinfo = p; |
520 | PL_curstackinfo = p; |
… | |
… | |
576 | |
563 | |
577 | ctx->stack = 0; |
564 | ctx->stack = 0; |
578 | |
565 | |
579 | if (stack) |
566 | if (stack) |
580 | { |
567 | { |
|
|
568 | printf ("deallocating stack %p %d\n", stack, stack->refcnt);/*D*/ |
581 | if (!--stack->refcnt) |
569 | if (!--stack->refcnt) |
582 | { |
570 | { |
583 | #ifdef HAVE_MMAP |
571 | #ifdef HAVE_MMAP |
584 | if (stack->ssize > 0 && stack->sptr) |
572 | if (stack->ssize > 0 && stack->sptr) |
585 | munmap (stack->sptr, stack->ssize); |
573 | munmap (stack->sptr, stack->ssize); |
… | |
… | |
603 | dSP; |
591 | dSP; |
604 | Coro__State ctx = (Coro__State)arg; |
592 | Coro__State ctx = (Coro__State)arg; |
605 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
593 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
606 | |
594 | |
607 | coro_init_stacks (aTHX); |
595 | coro_init_stacks (aTHX); |
608 | JMPENV_BOOTSTRAP; |
|
|
609 | SPAGAIN; |
|
|
610 | |
|
|
611 | /*PL_curcop = 0;*/ |
596 | /*PL_curcop = 0;*/ |
|
|
597 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
612 | SvREFCNT_dec (GvAV (PL_defgv)); |
598 | SvREFCNT_dec (GvAV (PL_defgv)); |
613 | GvAV (PL_defgv) = ctx->args; |
599 | GvAV (PL_defgv) = ctx->args; |
614 | |
600 | |
|
|
601 | SPAGAIN; |
|
|
602 | |
615 | if (ctx->stack) |
603 | if (ctx->stack) |
616 | { |
604 | { |
617 | ctx->cursp = 0; |
605 | ctx->cursp = 0; |
618 | |
606 | |
619 | PUSHMARK(SP); |
607 | PUSHMARK(SP); |
620 | PUTBACK; |
608 | PUTBACK; |
621 | (void) call_sv (sub_init, G_VOID|G_NOARGS); |
609 | (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); |
|
|
610 | |
|
|
611 | if (SvTRUE (ERRSV)) |
|
|
612 | croak (NULL); |
|
|
613 | else |
622 | croak ("FATAL: CCTXT coroutine returned!"); |
614 | croak ("FATAL: CCTXT coroutine returned!"); |
623 | } |
615 | } |
624 | else |
616 | else |
625 | { |
617 | { |
626 | UNOP myop; |
618 | UNOP myop; |
627 | |
619 | |
… | |
… | |
654 | /* |
646 | /* |
655 | * this is a _very_ stripped down perl interpreter ;) |
647 | * this is a _very_ stripped down perl interpreter ;) |
656 | */ |
648 | */ |
657 | Coro__State ctx = (Coro__State)arg; |
649 | Coro__State ctx = (Coro__State)arg; |
658 | |
650 | |
|
|
651 | /*FIXME*//* must set up top_env here */ |
659 | ctx->cursp = 0; |
652 | ctx->cursp = 0; |
660 | PL_op = PL_op->op_next; |
653 | PL_op = PL_op->op_next; |
661 | CALLRUNOPS(aTHX); |
654 | CALLRUNOPS(aTHX); |
662 | /*NORETURN*/ |
655 | |
663 | abort (); |
656 | abort (); |
664 | } |
657 | } |
665 | |
658 | |
666 | STATIC void |
659 | STATIC void |
667 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
660 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
668 | { |
661 | { |
669 | dSP; |
|
|
670 | dSTACKLEVEL; |
662 | dSTACKLEVEL; |
|
|
663 | static struct coro *xnext; |
671 | |
664 | |
|
|
665 | printf ("%p => %p\n", prev, next);/*D*/ |
672 | if (prev != next) |
666 | if (prev != next) |
673 | { |
667 | { |
|
|
668 | xnext = next; |
|
|
669 | |
674 | if (next->mainstack) |
670 | if (next->mainstack) |
675 | { |
671 | { |
676 | SAVE (prev, flags); |
672 | SAVE (prev, flags); |
677 | LOAD (next); |
673 | LOAD (next); |
678 | |
674 | |
… | |
… | |
701 | continue_coro, (void *)next, |
697 | continue_coro, (void *)next, |
702 | next->stack->sptr, labs (next->stack->ssize)); |
698 | next->stack->sptr, labs (next->stack->ssize)); |
703 | } |
699 | } |
704 | |
700 | |
705 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
701 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
702 | /* don't add any code here */ |
706 | } |
703 | } |
707 | |
704 | |
708 | } |
705 | } |
709 | else if (next->tmps_ix == -2) |
706 | else if (next->tmps_ix == -2) |
710 | croak ("tried to transfer to running coroutine"); |
707 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
731 | allocate_stack (next, 1); |
728 | allocate_stack (next, 1); |
732 | coro_create (&(next->stack->cctx), |
729 | coro_create (&(next->stack->cctx), |
733 | setup_coro, (void *)next, |
730 | setup_coro, (void *)next, |
734 | next->stack->sptr, labs (next->stack->ssize)); |
731 | next->stack->sptr, labs (next->stack->ssize)); |
735 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
732 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
733 | /* don't add any code here */ |
736 | } |
734 | } |
737 | } |
735 | } |
738 | else |
736 | else |
739 | setup_coro (next); |
737 | setup_coro (next); |
740 | } |
738 | } |
741 | } |
739 | } |
742 | |
740 | |
743 | next->cursp = stacklevel; |
741 | xnext->cursp = stacklevel; |
|
|
742 | } |
|
|
743 | |
|
|
744 | static struct coro * |
|
|
745 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
|
|
746 | { |
|
|
747 | if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) |
|
|
748 | { |
|
|
749 | HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); |
|
|
750 | |
|
|
751 | if (!he) |
|
|
752 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); |
|
|
753 | |
|
|
754 | arg = HeVAL(he); |
|
|
755 | } |
|
|
756 | |
|
|
757 | /* must also be changed inside Coro::Cont::yield */ |
|
|
758 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
|
|
759 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
|
|
760 | |
|
|
761 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
|
|
762 | /*NORETURN*/ |
|
|
763 | } |
|
|
764 | |
|
|
765 | static void |
|
|
766 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
|
|
767 | { |
|
|
768 | transfer(aTHX_ |
|
|
769 | sv_to_coro (prev, "Coro::transfer", "prev"), |
|
|
770 | sv_to_coro (next, "Coro::transfer", "next"), |
|
|
771 | flags); |
|
|
772 | } |
|
|
773 | |
|
|
774 | /** Coro ********************************************************************/ |
|
|
775 | |
|
|
776 | #define PRIO_MAX 3 |
|
|
777 | #define PRIO_HIGH 1 |
|
|
778 | #define PRIO_NORMAL 0 |
|
|
779 | #define PRIO_LOW -1 |
|
|
780 | #define PRIO_IDLE -3 |
|
|
781 | #define PRIO_MIN -4 |
|
|
782 | |
|
|
783 | /* for Coro.pm */ |
|
|
784 | static GV *coro_current, *coro_idle; |
|
|
785 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
|
|
786 | static int coro_nready; |
|
|
787 | |
|
|
788 | static void |
|
|
789 | coro_enq (SV *sv) |
|
|
790 | { |
|
|
791 | if (SvROK (sv)) |
|
|
792 | { |
|
|
793 | SV *hv = SvRV (sv); |
|
|
794 | if (SvTYPE (hv) == SVt_PVHV) |
|
|
795 | { |
|
|
796 | SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); |
|
|
797 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
798 | |
|
|
799 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
800 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
801 | : prio; |
|
|
802 | |
|
|
803 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
804 | coro_nready++; |
|
|
805 | |
|
|
806 | return; |
|
|
807 | } |
|
|
808 | } |
|
|
809 | |
|
|
810 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
811 | } |
|
|
812 | |
|
|
813 | static SV * |
|
|
814 | coro_deq (int min_prio) |
|
|
815 | { |
|
|
816 | int prio = PRIO_MAX - PRIO_MIN; |
|
|
817 | |
|
|
818 | min_prio -= PRIO_MIN; |
|
|
819 | if (min_prio < 0) |
|
|
820 | min_prio = 0; |
|
|
821 | |
|
|
822 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
|
|
823 | if (av_len (coro_ready[prio]) >= 0) |
|
|
824 | { |
|
|
825 | coro_nready--; |
|
|
826 | return av_shift (coro_ready[prio]); |
|
|
827 | } |
|
|
828 | |
|
|
829 | return 0; |
|
|
830 | } |
|
|
831 | |
|
|
832 | static void |
|
|
833 | api_ready (SV *coro) |
|
|
834 | { |
|
|
835 | coro_enq (SvREFCNT_inc (coro)); |
|
|
836 | } |
|
|
837 | |
|
|
838 | static void |
|
|
839 | api_schedule (int cede) |
|
|
840 | { |
|
|
841 | SV *prev, *next; |
|
|
842 | |
|
|
843 | prev = GvSV (coro_current); |
|
|
844 | |
|
|
845 | if (cede) |
|
|
846 | coro_enq (SvREFCNT_inc (prev)); |
|
|
847 | |
|
|
848 | next = coro_deq (PRIO_MIN); |
|
|
849 | |
|
|
850 | if (!next) |
|
|
851 | next = SvREFCNT_inc (GvSV (coro_idle)); |
|
|
852 | |
|
|
853 | GvSV (coro_current) = SvREFCNT_inc (next); |
|
|
854 | transfer (aTHX_ |
|
|
855 | sv_to_coro (prev, "Coro::schedule", "current coroutine"), |
|
|
856 | sv_to_coro (next, "Coro::schedule", "next coroutine"), |
|
|
857 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
858 | SvREFCNT_dec (next); |
|
|
859 | SvREFCNT_dec (prev); |
744 | } |
860 | } |
745 | |
861 | |
746 | MODULE = Coro::State PACKAGE = Coro::State |
862 | MODULE = Coro::State PACKAGE = Coro::State |
747 | |
863 | |
748 | PROTOTYPES: ENABLE |
864 | PROTOTYPES: ENABLE |
… | |
… | |
760 | |
876 | |
761 | if (!padlist_cache) |
877 | if (!padlist_cache) |
762 | padlist_cache = newHV (); |
878 | padlist_cache = newHV (); |
763 | |
879 | |
764 | main_mainstack = PL_mainstack; |
880 | main_mainstack = PL_mainstack; |
|
|
881 | |
|
|
882 | coroapi.ver = CORO_API_VERSION; |
|
|
883 | coroapi.transfer = api_transfer; |
765 | } |
884 | } |
766 | |
885 | |
767 | Coro::State |
886 | Coro::State |
768 | _newprocess(args) |
887 | _newprocess(args) |
769 | SV * args |
888 | SV * args |
… | |
… | |
783 | RETVAL = coro; |
902 | RETVAL = coro; |
784 | OUTPUT: |
903 | OUTPUT: |
785 | RETVAL |
904 | RETVAL |
786 | |
905 | |
787 | void |
906 | void |
788 | transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) |
907 | transfer(prev, next, flags) |
789 | Coro::State_or_hashref prev |
908 | Coro::State_or_hashref prev |
790 | Coro::State_or_hashref next |
909 | Coro::State_or_hashref next |
791 | int flags |
910 | int flags |
792 | PROTOTYPE: @ |
911 | PROTOTYPE: @ |
793 | CODE: |
912 | CODE: |
|
|
913 | PUTBACK; |
794 | transfer (aTHX_ prev, next, flags); |
914 | transfer (aTHX_ prev, next, flags); |
|
|
915 | SPAGAIN; |
795 | |
916 | |
796 | void |
917 | void |
797 | DESTROY(coro) |
918 | DESTROY(coro) |
798 | Coro::State coro |
919 | Coro::State coro |
799 | CODE: |
920 | CODE: |
800 | |
921 | |
801 | if (coro->mainstack && coro->mainstack != main_mainstack) |
922 | if (coro->mainstack && coro->mainstack != main_mainstack) |
802 | { |
923 | { |
803 | struct coro temp; |
924 | struct coro temp; |
804 | |
925 | |
|
|
926 | PUTBACK; |
805 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
927 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
806 | LOAD(aTHX_ coro); |
928 | LOAD(aTHX_ coro); |
|
|
929 | SPAGAIN; |
807 | |
930 | |
808 | destroy_stacks (aTHX); |
931 | destroy_stacks (aTHX); |
809 | |
932 | |
810 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
933 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
934 | SPAGAIN; |
811 | |
935 | |
812 | coro->mainstack = 0; |
936 | coro->mainstack = 0; |
813 | } |
937 | } |
814 | |
938 | |
815 | deallocate_stack (coro); |
939 | deallocate_stack (coro); |
… | |
… | |
821 | CODE: |
945 | CODE: |
822 | #ifdef MAY_FLUSH |
946 | #ifdef MAY_FLUSH |
823 | flush_padlist_cache (); |
947 | flush_padlist_cache (); |
824 | #endif |
948 | #endif |
825 | |
949 | |
|
|
950 | void |
|
|
951 | _exit(code) |
|
|
952 | int code |
|
|
953 | PROTOTYPE: $ |
|
|
954 | CODE: |
|
|
955 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
956 | _exit (code); |
|
|
957 | #else |
|
|
958 | signal (SIGTERM, SIG_DFL); |
|
|
959 | raise (SIGTERM); |
|
|
960 | exit (code); |
|
|
961 | #endif |
|
|
962 | |
826 | MODULE = Coro::State PACKAGE = Coro::Cont |
963 | MODULE = Coro::State PACKAGE = Coro::Cont |
827 | |
964 | |
828 | # this is slightly dirty |
965 | # this is slightly dirty (should expose a c-level api) |
829 | |
966 | |
830 | void |
967 | void |
831 | yield(...) |
968 | yield(...) |
832 | PROTOTYPE: @ |
969 | PROTOTYPE: @ |
833 | CODE: |
970 | CODE: |
… | |
… | |
851 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
988 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
852 | SvREFCNT_dec (sv); |
989 | SvREFCNT_dec (sv); |
853 | |
990 | |
854 | transfer(aTHX_ prev, next, 0); |
991 | transfer(aTHX_ prev, next, 0); |
855 | |
992 | |
|
|
993 | MODULE = Coro::State PACKAGE = Coro |
|
|
994 | |
|
|
995 | # this is slightly dirty (should expose a c-level api) |
|
|
996 | |
|
|
997 | BOOT: |
|
|
998 | { |
|
|
999 | int i; |
|
|
1000 | HV *stash = gv_stashpv ("Coro", TRUE); |
|
|
1001 | |
|
|
1002 | newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
|
|
1003 | newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
|
|
1004 | newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
|
|
1005 | newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
|
|
1006 | newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
|
|
1007 | newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
|
|
1008 | |
|
|
1009 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
|
|
1010 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
|
|
1011 | |
|
|
1012 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
1013 | coro_ready[i] = newAV (); |
|
|
1014 | |
|
|
1015 | { |
|
|
1016 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
1017 | |
|
|
1018 | coroapi.schedule = api_schedule; |
|
|
1019 | coroapi.ready = api_ready; |
|
|
1020 | coroapi.nready = &coro_nready; |
|
|
1021 | coroapi.current = coro_current; |
|
|
1022 | |
|
|
1023 | GCoroAPI = &coroapi; |
|
|
1024 | sv_setiv(sv, (IV)&coroapi); |
|
|
1025 | SvREADONLY_on(sv); |
|
|
1026 | } |
|
|
1027 | } |
|
|
1028 | |
|
|
1029 | void |
|
|
1030 | ready(self) |
|
|
1031 | SV * self |
|
|
1032 | CODE: |
|
|
1033 | api_ready (self); |
|
|
1034 | |
|
|
1035 | int |
|
|
1036 | nready(...) |
|
|
1037 | PROTOTYPE: |
|
|
1038 | CODE: |
|
|
1039 | RETVAL = coro_nready; |
|
|
1040 | OUTPUT: |
|
|
1041 | RETVAL |
|
|
1042 | |
|
|
1043 | void |
|
|
1044 | schedule(...) |
|
|
1045 | PROTOTYPE: |
|
|
1046 | ALIAS: |
|
|
1047 | cede = 1 |
|
|
1048 | CODE: |
|
|
1049 | api_schedule (ix); |
|
|
1050 | |