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 |
… | |
… | |
53 | |
55 | |
54 | void *sptr; |
56 | void *sptr; |
55 | long ssize; /* positive == mmap, otherwise malloc */ |
57 | long ssize; /* positive == mmap, otherwise malloc */ |
56 | } coro_stack; |
58 | } coro_stack; |
57 | |
59 | |
58 | static coro_stack main_stack = { 1, 0, 0 }; |
|
|
59 | |
|
|
60 | struct coro { |
60 | struct coro { |
61 | /* the optional C context */ |
61 | /* the optional C context */ |
62 | coro_stack *stack; |
62 | coro_stack *stack; |
63 | void *cursp; |
63 | void *cursp; |
64 | int gencnt; |
64 | int gencnt; |
… | |
… | |
68 | SV *defsv; |
68 | SV *defsv; |
69 | SV *errsv; |
69 | SV *errsv; |
70 | |
70 | |
71 | /* saved global state not related to stacks */ |
71 | /* saved global state not related to stacks */ |
72 | U8 dowarn; |
72 | U8 dowarn; |
|
|
73 | I32 in_eval; |
73 | |
74 | |
74 | /* the stacks and related info (callchain etc..) */ |
75 | /* the stacks and related info (callchain etc..) */ |
75 | PERL_SI *curstackinfo; |
76 | PERL_SI *curstackinfo; |
76 | AV *curstack; |
77 | AV *curstack; |
77 | AV *mainstack; |
78 | AV *mainstack; |
… | |
… | |
95 | I32 savestack_max; |
96 | I32 savestack_max; |
96 | OP **retstack; |
97 | OP **retstack; |
97 | I32 retstack_ix; |
98 | I32 retstack_ix; |
98 | I32 retstack_max; |
99 | I32 retstack_max; |
99 | COP *curcop; |
100 | COP *curcop; |
100 | JMPENV start_env; |
|
|
101 | JMPENV *top_env; |
101 | JMPENV *top_env; |
102 | |
102 | |
103 | /* data associated with this coroutine (initial args) */ |
103 | /* data associated with this coroutine (initial args) */ |
104 | AV *args; |
104 | AV *args; |
105 | }; |
105 | }; |
… | |
… | |
290 | |
290 | |
291 | static void |
291 | static void |
292 | load_state(pTHX_ Coro__State c) |
292 | load_state(pTHX_ Coro__State c) |
293 | { |
293 | { |
294 | PL_dowarn = c->dowarn; |
294 | PL_dowarn = c->dowarn; |
|
|
295 | PL_in_eval = c->in_eval; |
295 | |
296 | |
296 | PL_curstackinfo = c->curstackinfo; |
297 | PL_curstackinfo = c->curstackinfo; |
297 | PL_curstack = c->curstack; |
298 | PL_curstack = c->curstack; |
298 | PL_mainstack = c->mainstack; |
299 | PL_mainstack = c->mainstack; |
299 | PL_stack_sp = c->stack_sp; |
300 | PL_stack_sp = c->stack_sp; |
… | |
… | |
316 | PL_savestack_max = c->savestack_max; |
317 | PL_savestack_max = c->savestack_max; |
317 | PL_retstack = c->retstack; |
318 | PL_retstack = c->retstack; |
318 | PL_retstack_ix = c->retstack_ix; |
319 | PL_retstack_ix = c->retstack_ix; |
319 | PL_retstack_max = c->retstack_max; |
320 | PL_retstack_max = c->retstack_max; |
320 | PL_curcop = c->curcop; |
321 | PL_curcop = c->curcop; |
321 | PL_start_env = c->start_env; |
|
|
322 | PL_top_env = c->top_env; |
322 | PL_top_env = c->top_env; |
323 | |
323 | |
324 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
324 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
325 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
325 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
326 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
326 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
… | |
… | |
425 | /* but if it's missing the defav contents magically get replaced sometimes */ |
425 | /* but if it's missing the defav contents magically get replaced sometimes */ |
426 | if (c->defav) |
426 | if (c->defav) |
427 | av_reify (c->defav); |
427 | av_reify (c->defav); |
428 | |
428 | |
429 | c->dowarn = PL_dowarn; |
429 | c->dowarn = PL_dowarn; |
|
|
430 | c->in_eval = PL_in_eval; |
430 | |
431 | |
431 | c->curstackinfo = PL_curstackinfo; |
432 | c->curstackinfo = PL_curstackinfo; |
432 | c->curstack = PL_curstack; |
433 | c->curstack = PL_curstack; |
433 | c->mainstack = PL_mainstack; |
434 | c->mainstack = PL_mainstack; |
434 | c->stack_sp = PL_stack_sp; |
435 | c->stack_sp = PL_stack_sp; |
… | |
… | |
451 | c->savestack_max = PL_savestack_max; |
452 | c->savestack_max = PL_savestack_max; |
452 | c->retstack = PL_retstack; |
453 | c->retstack = PL_retstack; |
453 | c->retstack_ix = PL_retstack_ix; |
454 | c->retstack_ix = PL_retstack_ix; |
454 | c->retstack_max = PL_retstack_max; |
455 | c->retstack_max = PL_retstack_max; |
455 | c->curcop = PL_curcop; |
456 | c->curcop = PL_curcop; |
456 | c->start_env = PL_start_env; |
|
|
457 | c->top_env = PL_top_env; |
457 | c->top_env = PL_top_env; |
458 | } |
458 | } |
459 | |
459 | |
460 | /* |
460 | /* |
461 | * allocate various perl stacks. This is an exact copy |
461 | * allocate various perl stacks. This is an exact copy |
… | |
… | |
605 | dSP; |
605 | dSP; |
606 | Coro__State ctx = (Coro__State)arg; |
606 | Coro__State ctx = (Coro__State)arg; |
607 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
607 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
608 | |
608 | |
609 | coro_init_stacks (aTHX); |
609 | coro_init_stacks (aTHX); |
610 | JMPENV_BOOTSTRAP; |
|
|
611 | SPAGAIN; |
|
|
612 | |
|
|
613 | /*PL_curcop = 0;*/ |
610 | /*PL_curcop = 0;*/ |
|
|
611 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
614 | SvREFCNT_dec (GvAV (PL_defgv)); |
612 | SvREFCNT_dec (GvAV (PL_defgv)); |
615 | GvAV (PL_defgv) = ctx->args; |
613 | GvAV (PL_defgv) = ctx->args; |
616 | |
614 | |
|
|
615 | SPAGAIN; |
|
|
616 | |
617 | if (ctx->stack) |
617 | if (ctx->stack) |
618 | { |
618 | { |
619 | ctx->cursp = 0; |
619 | ctx->cursp = 0; |
620 | |
620 | |
621 | PUSHMARK(SP); |
621 | PUSHMARK(SP); |
622 | PUTBACK; |
622 | PUTBACK; |
623 | (void) call_sv (sub_init, G_VOID|G_NOARGS); |
623 | (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); |
|
|
624 | |
|
|
625 | if (SvTRUE (ERRSV)) |
|
|
626 | croak (NULL); |
|
|
627 | else |
624 | croak ("FATAL: CCTXT coroutine returned!"); |
628 | croak ("FATAL: CCTXT coroutine returned!"); |
625 | } |
629 | } |
626 | else |
630 | else |
627 | { |
631 | { |
628 | UNOP myop; |
632 | UNOP myop; |
629 | |
633 | |
… | |
… | |
656 | /* |
660 | /* |
657 | * this is a _very_ stripped down perl interpreter ;) |
661 | * this is a _very_ stripped down perl interpreter ;) |
658 | */ |
662 | */ |
659 | Coro__State ctx = (Coro__State)arg; |
663 | Coro__State ctx = (Coro__State)arg; |
660 | |
664 | |
|
|
665 | /*FIXME*//* must set up top_env here */ |
661 | ctx->cursp = 0; |
666 | ctx->cursp = 0; |
662 | PL_op = PL_op->op_next; |
667 | PL_op = PL_op->op_next; |
663 | CALLRUNOPS(aTHX); |
668 | CALLRUNOPS(aTHX); |
664 | /*NORETURN*/ |
669 | |
665 | abort (); |
670 | abort (); |
666 | } |
671 | } |
667 | |
672 | |
668 | STATIC void |
673 | STATIC void |
669 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
674 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
… | |
… | |
791 | Coro::State_or_hashref prev |
796 | Coro::State_or_hashref prev |
792 | Coro::State_or_hashref next |
797 | Coro::State_or_hashref next |
793 | int flags |
798 | int flags |
794 | PROTOTYPE: @ |
799 | PROTOTYPE: @ |
795 | CODE: |
800 | CODE: |
|
|
801 | PUTBACK; |
796 | transfer (aTHX_ prev, next, flags); |
802 | transfer (aTHX_ prev, next, flags); |
|
|
803 | SPAGAIN; |
797 | |
804 | |
798 | void |
805 | void |
799 | DESTROY(coro) |
806 | DESTROY(coro) |
800 | Coro::State coro |
807 | Coro::State coro |
801 | CODE: |
808 | CODE: |
… | |
… | |
821 | void |
828 | void |
822 | flush() |
829 | flush() |
823 | CODE: |
830 | CODE: |
824 | #ifdef MAY_FLUSH |
831 | #ifdef MAY_FLUSH |
825 | flush_padlist_cache (); |
832 | flush_padlist_cache (); |
|
|
833 | #endif |
|
|
834 | |
|
|
835 | void |
|
|
836 | _exit(code) |
|
|
837 | int code |
|
|
838 | PROTOTYPE: $ |
|
|
839 | CODE: |
|
|
840 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
841 | _exit (code); |
|
|
842 | #else |
|
|
843 | signal (SIGTERM, SIG_DFL); |
|
|
844 | raise (SIGTERM); |
|
|
845 | exit (code); |
826 | #endif |
846 | #endif |
827 | |
847 | |
828 | MODULE = Coro::State PACKAGE = Coro::Cont |
848 | MODULE = Coro::State PACKAGE = Coro::Cont |
829 | |
849 | |
830 | # this is slightly dirty |
850 | # this is slightly dirty |