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 |
… | |
… | |
66 | SV *defsv; |
68 | SV *defsv; |
67 | SV *errsv; |
69 | SV *errsv; |
68 | |
70 | |
69 | /* saved global state not related to stacks */ |
71 | /* saved global state not related to stacks */ |
70 | U8 dowarn; |
72 | U8 dowarn; |
|
|
73 | I32 in_eval; |
71 | |
74 | |
72 | /* the stacks and related info (callchain etc..) */ |
75 | /* the stacks and related info (callchain etc..) */ |
73 | PERL_SI *curstackinfo; |
76 | PERL_SI *curstackinfo; |
74 | AV *curstack; |
77 | AV *curstack; |
75 | AV *mainstack; |
78 | AV *mainstack; |
… | |
… | |
93 | I32 savestack_max; |
96 | I32 savestack_max; |
94 | OP **retstack; |
97 | OP **retstack; |
95 | I32 retstack_ix; |
98 | I32 retstack_ix; |
96 | I32 retstack_max; |
99 | I32 retstack_max; |
97 | COP *curcop; |
100 | COP *curcop; |
98 | JMPENV start_env; |
|
|
99 | JMPENV *top_env; |
101 | JMPENV *top_env; |
100 | |
102 | |
101 | /* data associated with this coroutine (initial args) */ |
103 | /* data associated with this coroutine (initial args) */ |
102 | AV *args; |
104 | AV *args; |
103 | }; |
105 | }; |
… | |
… | |
288 | |
290 | |
289 | static void |
291 | static void |
290 | load_state(pTHX_ Coro__State c) |
292 | load_state(pTHX_ Coro__State c) |
291 | { |
293 | { |
292 | PL_dowarn = c->dowarn; |
294 | PL_dowarn = c->dowarn; |
|
|
295 | PL_in_eval = c->in_eval; |
293 | |
296 | |
294 | PL_curstackinfo = c->curstackinfo; |
297 | PL_curstackinfo = c->curstackinfo; |
295 | PL_curstack = c->curstack; |
298 | PL_curstack = c->curstack; |
296 | PL_mainstack = c->mainstack; |
299 | PL_mainstack = c->mainstack; |
297 | PL_stack_sp = c->stack_sp; |
300 | PL_stack_sp = c->stack_sp; |
… | |
… | |
314 | PL_savestack_max = c->savestack_max; |
317 | PL_savestack_max = c->savestack_max; |
315 | PL_retstack = c->retstack; |
318 | PL_retstack = c->retstack; |
316 | PL_retstack_ix = c->retstack_ix; |
319 | PL_retstack_ix = c->retstack_ix; |
317 | PL_retstack_max = c->retstack_max; |
320 | PL_retstack_max = c->retstack_max; |
318 | PL_curcop = c->curcop; |
321 | PL_curcop = c->curcop; |
319 | PL_start_env = c->start_env; |
|
|
320 | PL_top_env = c->top_env; |
322 | PL_top_env = c->top_env; |
321 | |
323 | |
322 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
324 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
323 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
325 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
324 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
326 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
… | |
… | |
423 | /* 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 */ |
424 | if (c->defav) |
426 | if (c->defav) |
425 | av_reify (c->defav); |
427 | av_reify (c->defav); |
426 | |
428 | |
427 | c->dowarn = PL_dowarn; |
429 | c->dowarn = PL_dowarn; |
|
|
430 | c->in_eval = PL_in_eval; |
428 | |
431 | |
429 | c->curstackinfo = PL_curstackinfo; |
432 | c->curstackinfo = PL_curstackinfo; |
430 | c->curstack = PL_curstack; |
433 | c->curstack = PL_curstack; |
431 | c->mainstack = PL_mainstack; |
434 | c->mainstack = PL_mainstack; |
432 | c->stack_sp = PL_stack_sp; |
435 | c->stack_sp = PL_stack_sp; |
… | |
… | |
449 | c->savestack_max = PL_savestack_max; |
452 | c->savestack_max = PL_savestack_max; |
450 | c->retstack = PL_retstack; |
453 | c->retstack = PL_retstack; |
451 | c->retstack_ix = PL_retstack_ix; |
454 | c->retstack_ix = PL_retstack_ix; |
452 | c->retstack_max = PL_retstack_max; |
455 | c->retstack_max = PL_retstack_max; |
453 | c->curcop = PL_curcop; |
456 | c->curcop = PL_curcop; |
454 | c->start_env = PL_start_env; |
|
|
455 | c->top_env = PL_top_env; |
457 | c->top_env = PL_top_env; |
456 | } |
458 | } |
457 | |
459 | |
458 | /* |
460 | /* |
459 | * allocate various perl stacks. This is an exact copy |
461 | * allocate various perl stacks. This is an exact copy |
… | |
… | |
603 | dSP; |
605 | dSP; |
604 | Coro__State ctx = (Coro__State)arg; |
606 | Coro__State ctx = (Coro__State)arg; |
605 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
607 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
606 | |
608 | |
607 | coro_init_stacks (aTHX); |
609 | coro_init_stacks (aTHX); |
608 | JMPENV_BOOTSTRAP; |
|
|
609 | SPAGAIN; |
|
|
610 | |
|
|
611 | /*PL_curcop = 0;*/ |
610 | /*PL_curcop = 0;*/ |
|
|
611 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
612 | SvREFCNT_dec (GvAV (PL_defgv)); |
612 | SvREFCNT_dec (GvAV (PL_defgv)); |
613 | GvAV (PL_defgv) = ctx->args; |
613 | GvAV (PL_defgv) = ctx->args; |
614 | |
614 | |
|
|
615 | SPAGAIN; |
|
|
616 | |
615 | if (ctx->stack) |
617 | if (ctx->stack) |
616 | { |
618 | { |
617 | ctx->cursp = 0; |
619 | ctx->cursp = 0; |
618 | |
620 | |
619 | PUSHMARK(SP); |
621 | PUSHMARK(SP); |
620 | PUTBACK; |
622 | PUTBACK; |
621 | (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 |
622 | croak ("FATAL: CCTXT coroutine returned!"); |
628 | croak ("FATAL: CCTXT coroutine returned!"); |
623 | } |
629 | } |
624 | else |
630 | else |
625 | { |
631 | { |
626 | UNOP myop; |
632 | UNOP myop; |
627 | |
633 | |
… | |
… | |
654 | /* |
660 | /* |
655 | * this is a _very_ stripped down perl interpreter ;) |
661 | * this is a _very_ stripped down perl interpreter ;) |
656 | */ |
662 | */ |
657 | Coro__State ctx = (Coro__State)arg; |
663 | Coro__State ctx = (Coro__State)arg; |
658 | |
664 | |
|
|
665 | /*FIXME*//* must set up top_env here */ |
659 | ctx->cursp = 0; |
666 | ctx->cursp = 0; |
660 | PL_op = PL_op->op_next; |
667 | PL_op = PL_op->op_next; |
661 | CALLRUNOPS(aTHX); |
668 | CALLRUNOPS(aTHX); |
662 | /*NORETURN*/ |
669 | |
663 | abort (); |
670 | abort (); |
664 | } |
671 | } |
665 | |
672 | |
666 | STATIC void |
673 | STATIC void |
667 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
674 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
… | |
… | |
789 | Coro::State_or_hashref prev |
796 | Coro::State_or_hashref prev |
790 | Coro::State_or_hashref next |
797 | Coro::State_or_hashref next |
791 | int flags |
798 | int flags |
792 | PROTOTYPE: @ |
799 | PROTOTYPE: @ |
793 | CODE: |
800 | CODE: |
|
|
801 | PUTBACK; |
794 | transfer (aTHX_ prev, next, flags); |
802 | transfer (aTHX_ prev, next, flags); |
|
|
803 | SPAGAIN; |
795 | |
804 | |
796 | void |
805 | void |
797 | DESTROY(coro) |
806 | DESTROY(coro) |
798 | Coro::State coro |
807 | Coro::State coro |
799 | CODE: |
808 | CODE: |
… | |
… | |
819 | void |
828 | void |
820 | flush() |
829 | flush() |
821 | CODE: |
830 | CODE: |
822 | #ifdef MAY_FLUSH |
831 | #ifdef MAY_FLUSH |
823 | 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); |
824 | #endif |
846 | #endif |
825 | |
847 | |
826 | MODULE = Coro::State PACKAGE = Coro::Cont |
848 | MODULE = Coro::State PACKAGE = Coro::Cont |
827 | |
849 | |
828 | # this is slightly dirty |
850 | # this is slightly dirty |