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 | |
|
|
5 | #include "patchlevel.h" |
|
|
6 | |
|
|
7 | #if PATCHLEVEL < 6 |
|
|
8 | # ifndef PL_ppaddr |
|
|
9 | # define PL_ppaddr ppaddr |
|
|
10 | # endif |
|
|
11 | # ifndef call_sv |
|
|
12 | # define call_sv perl_call_sv |
|
|
13 | # endif |
|
|
14 | # ifndef get_sv |
|
|
15 | # define get_sv perl_get_sv |
|
|
16 | # endif |
|
|
17 | # ifndef get_cv |
|
|
18 | # define get_cv perl_get_cv |
|
|
19 | # endif |
|
|
20 | # ifndef IS_PADGV |
|
|
21 | # define IS_PADGV(v) 0 |
|
|
22 | # endif |
|
|
23 | # ifndef IS_PADCONST |
|
|
24 | # define IS_PADCONST(v) 0 |
|
|
25 | # endif |
|
|
26 | #endif |
4 | |
27 | |
5 | #include "libcoro/coro.c" |
28 | #include "libcoro/coro.c" |
6 | |
29 | |
7 | #include <signal.h> |
30 | #include <signal.h> |
8 | |
31 | |
… | |
… | |
47 | void *sptr; |
70 | void *sptr; |
48 | long ssize; /* positive == mmap, otherwise malloc */ |
71 | long ssize; /* positive == mmap, otherwise malloc */ |
49 | } coro_stack; |
72 | } coro_stack; |
50 | |
73 | |
51 | struct coro { |
74 | struct coro { |
|
|
75 | /* the top-level JMPENV for each coroutine, needed to catch dies. */ |
|
|
76 | JMPENV start_env; |
|
|
77 | |
52 | /* the optional C context */ |
78 | /* the optional C context */ |
53 | coro_stack *stack; |
79 | coro_stack *stack; |
54 | void *cursp; |
80 | void *cursp; |
55 | int gencnt; |
81 | int gencnt; |
56 | |
82 | |
… | |
… | |
68 | AV *curstack; |
94 | AV *curstack; |
69 | AV *mainstack; |
95 | AV *mainstack; |
70 | SV **stack_sp; |
96 | SV **stack_sp; |
71 | OP *op; |
97 | OP *op; |
72 | SV **curpad; |
98 | SV **curpad; |
|
|
99 | AV *comppad; |
73 | SV **stack_base; |
100 | SV **stack_base; |
74 | SV **stack_max; |
101 | SV **stack_max; |
75 | SV **tmps_stack; |
102 | SV **tmps_stack; |
76 | I32 tmps_floor; |
103 | I32 tmps_floor; |
77 | I32 tmps_ix; |
104 | I32 tmps_ix; |
… | |
… | |
87 | I32 savestack_max; |
114 | I32 savestack_max; |
88 | OP **retstack; |
115 | OP **retstack; |
89 | I32 retstack_ix; |
116 | I32 retstack_ix; |
90 | I32 retstack_max; |
117 | I32 retstack_max; |
91 | COP *curcop; |
118 | COP *curcop; |
92 | PMOP *curpm; |
|
|
93 | JMPENV *top_env; |
119 | JMPENV *top_env; |
94 | |
120 | |
95 | /* data associated with this coroutine (initial args) */ |
121 | /* data associated with this coroutine (initial args) */ |
96 | AV *args; |
122 | AV *args; |
97 | }; |
123 | }; |
… | |
… | |
291 | PL_curstack = c->curstack; |
317 | PL_curstack = c->curstack; |
292 | PL_mainstack = c->mainstack; |
318 | PL_mainstack = c->mainstack; |
293 | PL_stack_sp = c->stack_sp; |
319 | PL_stack_sp = c->stack_sp; |
294 | PL_op = c->op; |
320 | PL_op = c->op; |
295 | PL_curpad = c->curpad; |
321 | PL_curpad = c->curpad; |
|
|
322 | PL_comppad = c->comppad; |
296 | PL_stack_base = c->stack_base; |
323 | PL_stack_base = c->stack_base; |
297 | PL_stack_max = c->stack_max; |
324 | PL_stack_max = c->stack_max; |
298 | PL_tmps_stack = c->tmps_stack; |
325 | PL_tmps_stack = c->tmps_stack; |
299 | PL_tmps_floor = c->tmps_floor; |
326 | PL_tmps_floor = c->tmps_floor; |
300 | PL_tmps_ix = c->tmps_ix; |
327 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
388 | PUSHs ((SV *)cv); |
415 | PUSHs ((SV *)cv); |
389 | |
416 | |
390 | get_padlist (cv); /* this is a monster */ |
417 | get_padlist (cv); /* this is a monster */ |
391 | } |
418 | } |
392 | } |
419 | } |
|
|
420 | #ifdef CXt_FORMAT |
393 | else if (CxTYPE(cx) == CXt_FORMAT) |
421 | else if (CxTYPE(cx) == CXt_FORMAT) |
394 | { |
422 | { |
395 | /* I never used formats, so how should I know how these are implemented? */ |
423 | /* I never used formats, so how should I know how these are implemented? */ |
396 | /* my bold guess is as a simple, plain sub... */ |
424 | /* my bold guess is as a simple, plain sub... */ |
397 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
425 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
398 | } |
426 | } |
|
|
427 | #endif |
399 | } |
428 | } |
400 | |
429 | |
401 | if (top_si->si_type == PERLSI_MAIN) |
430 | if (top_si->si_type == PERLSI_MAIN) |
402 | break; |
431 | break; |
403 | |
432 | |
… | |
… | |
420 | c->curstack = PL_curstack; |
449 | c->curstack = PL_curstack; |
421 | c->mainstack = PL_mainstack; |
450 | c->mainstack = PL_mainstack; |
422 | c->stack_sp = PL_stack_sp; |
451 | c->stack_sp = PL_stack_sp; |
423 | c->op = PL_op; |
452 | c->op = PL_op; |
424 | c->curpad = PL_curpad; |
453 | c->curpad = PL_curpad; |
|
|
454 | c->comppad = PL_comppad; |
425 | c->stack_base = PL_stack_base; |
455 | c->stack_base = PL_stack_base; |
426 | c->stack_max = PL_stack_max; |
456 | c->stack_max = PL_stack_max; |
427 | c->tmps_stack = PL_tmps_stack; |
457 | c->tmps_stack = PL_tmps_stack; |
428 | c->tmps_floor = PL_tmps_floor; |
458 | c->tmps_floor = PL_tmps_floor; |
429 | c->tmps_ix = PL_tmps_ix; |
459 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
469 | |
499 | |
470 | New(54,PL_markstack,16,I32); |
500 | New(54,PL_markstack,16,I32); |
471 | PL_markstack_ptr = PL_markstack; |
501 | PL_markstack_ptr = PL_markstack; |
472 | PL_markstack_max = PL_markstack + 16; |
502 | PL_markstack_max = PL_markstack + 16; |
473 | |
503 | |
|
|
504 | #ifdef SET_MARK_OFFSET |
474 | SET_MARK_OFFSET; |
505 | SET_MARK_OFFSET; |
|
|
506 | #endif |
475 | |
507 | |
476 | New(54,PL_scopestack,16,I32); |
508 | New(54,PL_scopestack,16,I32); |
477 | PL_scopestack_ix = 0; |
509 | PL_scopestack_ix = 0; |
478 | PL_scopestack_max = 16; |
510 | PL_scopestack_max = 16; |
479 | |
511 | |
… | |
… | |
598 | dSP; |
630 | dSP; |
599 | Coro__State ctx = (Coro__State)arg; |
631 | Coro__State ctx = (Coro__State)arg; |
600 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
632 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
601 | |
633 | |
602 | coro_init_stacks (aTHX); |
634 | coro_init_stacks (aTHX); |
603 | PL_curpm = 0; /* segfault on first access */ |
|
|
604 | /*PL_curcop = 0;*/ |
635 | /*PL_curcop = 0;*/ |
605 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
636 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
606 | SvREFCNT_dec (GvAV (PL_defgv)); |
637 | SvREFCNT_dec (GvAV (PL_defgv)); |
607 | GvAV (PL_defgv) = ctx->args; |
638 | GvAV (PL_defgv) = ctx->args; |
608 | |
639 | |
… | |
… | |
639 | * that doesn't matter, though, since it is only |
670 | * that doesn't matter, though, since it is only |
640 | * pp_nextstate and we never return... |
671 | * pp_nextstate and we never return... |
641 | * ah yes, and I don't care anyways ;) |
672 | * ah yes, and I don't care anyways ;) |
642 | */ |
673 | */ |
643 | PUTBACK; |
674 | PUTBACK; |
644 | PL_op = pp_entersub(); |
675 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
645 | SPAGAIN; |
676 | SPAGAIN; |
646 | |
677 | |
647 | ENTER; /* necessary e.g. for dounwind */ |
678 | ENTER; /* necessary e.g. for dounwind */ |
648 | } |
679 | } |
649 | } |
680 | } |
… | |
… | |
655 | * this is a _very_ stripped down perl interpreter ;) |
686 | * this is a _very_ stripped down perl interpreter ;) |
656 | */ |
687 | */ |
657 | Coro__State ctx = (Coro__State)arg; |
688 | Coro__State ctx = (Coro__State)arg; |
658 | JMPENV coro_start_env; |
689 | JMPENV coro_start_env; |
659 | |
690 | |
660 | /* same as JMPENV_BOOTSTRAP */ |
|
|
661 | Zero(&coro_start_env, 1, JMPENV); |
|
|
662 | coro_start_env.je_ret = -1; |
|
|
663 | coro_start_env.je_mustcatch = TRUE; |
|
|
664 | PL_top_env = &coro_start_env; |
691 | PL_top_env = &ctx->start_env; |
665 | |
692 | |
666 | ctx->cursp = 0; |
693 | ctx->cursp = 0; |
667 | PL_op = PL_op->op_next; |
694 | PL_op = PL_op->op_next; |
668 | CALLRUNOPS(aTHX); |
695 | CALLRUNOPS(aTHX); |
669 | |
696 | |
… | |
… | |
727 | if (!prev->stack) |
754 | if (!prev->stack) |
728 | allocate_stack (prev, 0); |
755 | allocate_stack (prev, 0); |
729 | |
756 | |
730 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
757 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
731 | { |
758 | { |
|
|
759 | PL_top_env = &next->start_env; |
|
|
760 | |
732 | setup_coro (next); |
761 | setup_coro (next); |
733 | |
762 | |
734 | prev->stack->refcnt++; |
763 | prev->stack->refcnt++; |
735 | prev->stack->usecnt++; |
764 | prev->stack->usecnt++; |
736 | next->stack = prev->stack; |
765 | next->stack = prev->stack; |
… | |
… | |
750 | setup_coro (next); |
779 | setup_coro (next); |
751 | } |
780 | } |
752 | |
781 | |
753 | /* |
782 | /* |
754 | * xnext is now either prev or next, depending on wether |
783 | * xnext is now either prev or next, depending on wether |
755 | * we switched the c stack or not. that's why i use a global |
784 | * we switched the c stack or not. that's why I use a global |
756 | * variable, that should become thread-specific at one point. |
785 | * variable, that should become thread-specific at one point. |
757 | */ |
786 | */ |
758 | xnext->cursp = stacklevel; |
787 | xnext->cursp = stacklevel; |
759 | } |
788 | } |
760 | |
789 | |
… | |
… | |
902 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
931 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
903 | |
932 | |
904 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
933 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
905 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
934 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
906 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
935 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
907 | newCONSTSUB (coro_state_stash, "SAVE_CURPM", newSViv (TRANSFER_SAVE_CURPM)); |
|
|
908 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
936 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
909 | |
937 | |
910 | if (!padlist_cache) |
938 | if (!padlist_cache) |
911 | padlist_cache = newHV (); |
939 | padlist_cache = newHV (); |
912 | |
940 | |
… | |
… | |
929 | New (0, coro, 1, struct coro); |
957 | New (0, coro, 1, struct coro); |
930 | |
958 | |
931 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
959 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
932 | coro->mainstack = 0; /* actual work is done inside transfer */ |
960 | coro->mainstack = 0; /* actual work is done inside transfer */ |
933 | coro->stack = 0; |
961 | coro->stack = 0; |
|
|
962 | |
|
|
963 | /* same as JMPENV_BOOTSTRAP */ |
|
|
964 | /* we might be able to recycle start_env, but safe is safe */ |
|
|
965 | Zero(&coro->start_env, 1, JMPENV); |
|
|
966 | coro->start_env.je_ret = -1; |
|
|
967 | coro->start_env.je_mustcatch = TRUE; |
934 | |
968 | |
935 | RETVAL = coro; |
969 | RETVAL = coro; |
936 | OUTPUT: |
970 | OUTPUT: |
937 | RETVAL |
971 | RETVAL |
938 | |
972 | |
… | |
… | |
1007 | SV *sv; |
1041 | SV *sv; |
1008 | AV *defav = GvAV (PL_defgv); |
1042 | AV *defav = GvAV (PL_defgv); |
1009 | struct coro *prev, *next; |
1043 | struct coro *prev, *next; |
1010 | |
1044 | |
1011 | if (!returnstk) |
1045 | if (!returnstk) |
1012 | returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); |
1046 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
1013 | |
1047 | |
1014 | /* set up @_ -- ugly */ |
1048 | /* set up @_ -- ugly */ |
1015 | av_clear (defav); |
1049 | av_clear (defav); |
1016 | av_fill (defav, items - 1); |
1050 | av_fill (defav, items - 1); |
1017 | while (items--) |
1051 | while (items--) |