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 | |
… | |
… | |
277 | #define SE } while (0) |
303 | #define SE } while (0) |
278 | |
304 | |
279 | #define LOAD(state) load_state(aTHX_ (state)); |
305 | #define LOAD(state) load_state(aTHX_ (state)); |
280 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
306 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
281 | |
307 | |
282 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
308 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
283 | |
309 | |
284 | static void |
310 | static void |
285 | load_state(pTHX_ Coro__State c) |
311 | load_state(pTHX_ Coro__State c) |
286 | { |
312 | { |
287 | PL_dowarn = c->dowarn; |
313 | PL_dowarn = c->dowarn; |
… | |
… | |
389 | PUSHs ((SV *)cv); |
415 | PUSHs ((SV *)cv); |
390 | |
416 | |
391 | get_padlist (cv); /* this is a monster */ |
417 | get_padlist (cv); /* this is a monster */ |
392 | } |
418 | } |
393 | } |
419 | } |
|
|
420 | #ifdef CXt_FORMAT |
394 | else if (CxTYPE(cx) == CXt_FORMAT) |
421 | else if (CxTYPE(cx) == CXt_FORMAT) |
395 | { |
422 | { |
396 | /* 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? */ |
397 | /* my bold guess is as a simple, plain sub... */ |
424 | /* my bold guess is as a simple, plain sub... */ |
398 | 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"); |
399 | } |
426 | } |
|
|
427 | #endif |
400 | } |
428 | } |
401 | |
429 | |
402 | if (top_si->si_type == PERLSI_MAIN) |
430 | if (top_si->si_type == PERLSI_MAIN) |
403 | break; |
431 | break; |
404 | |
432 | |
… | |
… | |
471 | |
499 | |
472 | New(54,PL_markstack,16,I32); |
500 | New(54,PL_markstack,16,I32); |
473 | PL_markstack_ptr = PL_markstack; |
501 | PL_markstack_ptr = PL_markstack; |
474 | PL_markstack_max = PL_markstack + 16; |
502 | PL_markstack_max = PL_markstack + 16; |
475 | |
503 | |
|
|
504 | #ifdef SET_MARK_OFFSET |
476 | SET_MARK_OFFSET; |
505 | SET_MARK_OFFSET; |
|
|
506 | #endif |
477 | |
507 | |
478 | New(54,PL_scopestack,16,I32); |
508 | New(54,PL_scopestack,16,I32); |
479 | PL_scopestack_ix = 0; |
509 | PL_scopestack_ix = 0; |
480 | PL_scopestack_max = 16; |
510 | PL_scopestack_max = 16; |
481 | |
511 | |
… | |
… | |
488 | PL_retstack_max = 8; |
518 | PL_retstack_max = 8; |
489 | } |
519 | } |
490 | |
520 | |
491 | /* |
521 | /* |
492 | * destroy the stacks, the callchain etc... |
522 | * destroy the stacks, the callchain etc... |
493 | * still there is a memleak of 128 bytes... |
|
|
494 | */ |
523 | */ |
495 | STATIC void |
524 | STATIC void |
496 | destroy_stacks(pTHX) |
525 | destroy_stacks(pTHX) |
497 | { |
526 | { |
498 | if (!IN_DESTRUCT) |
527 | if (!IN_DESTRUCT) |
… | |
… | |
579 | { |
608 | { |
580 | #ifdef HAVE_MMAP |
609 | #ifdef HAVE_MMAP |
581 | if (stack->ssize > 0 && stack->sptr) |
610 | if (stack->ssize > 0 && stack->sptr) |
582 | munmap (stack->sptr, stack->ssize); |
611 | munmap (stack->sptr, stack->ssize); |
583 | else |
612 | else |
584 | #else |
613 | #endif |
585 | Safefree (stack->sptr); |
614 | Safefree (stack->sptr); |
586 | #endif |
615 | |
587 | Safefree (stack); |
616 | Safefree (stack); |
588 | } |
617 | } |
589 | else if (ctx->gencnt == stack->gencnt) |
618 | else if (ctx->gencnt == stack->gencnt) |
590 | --stack->usecnt; |
619 | --stack->usecnt; |
591 | } |
620 | } |
… | |
… | |
603 | |
632 | |
604 | coro_init_stacks (aTHX); |
633 | coro_init_stacks (aTHX); |
605 | /*PL_curcop = 0;*/ |
634 | /*PL_curcop = 0;*/ |
606 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
635 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
607 | SvREFCNT_dec (GvAV (PL_defgv)); |
636 | SvREFCNT_dec (GvAV (PL_defgv)); |
608 | GvAV (PL_defgv) = ctx->args; |
637 | GvAV (PL_defgv) = ctx->args; ctx->args = 0; |
609 | |
638 | |
610 | SPAGAIN; |
639 | SPAGAIN; |
611 | |
640 | |
612 | if (ctx->stack) |
641 | if (ctx->stack) |
613 | { |
642 | { |
… | |
… | |
656 | * this is a _very_ stripped down perl interpreter ;) |
685 | * this is a _very_ stripped down perl interpreter ;) |
657 | */ |
686 | */ |
658 | Coro__State ctx = (Coro__State)arg; |
687 | Coro__State ctx = (Coro__State)arg; |
659 | JMPENV coro_start_env; |
688 | JMPENV coro_start_env; |
660 | |
689 | |
661 | /* same as JMPENV_BOOTSTRAP */ |
|
|
662 | Zero(&coro_start_env, 1, JMPENV); |
|
|
663 | coro_start_env.je_ret = -1; |
|
|
664 | coro_start_env.je_mustcatch = TRUE; |
|
|
665 | PL_top_env = &coro_start_env; |
690 | PL_top_env = &ctx->start_env; |
666 | |
691 | |
667 | ctx->cursp = 0; |
692 | ctx->cursp = 0; |
668 | PL_op = PL_op->op_next; |
693 | PL_op = PL_op->op_next; |
669 | CALLRUNOPS(aTHX); |
694 | CALLRUNOPS(aTHX); |
670 | |
695 | |
… | |
… | |
728 | if (!prev->stack) |
753 | if (!prev->stack) |
729 | allocate_stack (prev, 0); |
754 | allocate_stack (prev, 0); |
730 | |
755 | |
731 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
756 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
732 | { |
757 | { |
|
|
758 | PL_top_env = &next->start_env; |
|
|
759 | |
733 | setup_coro (next); |
760 | setup_coro (next); |
734 | |
761 | |
735 | prev->stack->refcnt++; |
762 | prev->stack->refcnt++; |
736 | prev->stack->usecnt++; |
763 | prev->stack->usecnt++; |
737 | next->stack = prev->stack; |
764 | next->stack = prev->stack; |
738 | next->gencnt = prev->gencnt; |
765 | next->gencnt = prev->gencnt; |
739 | } |
766 | } |
740 | else |
767 | else |
741 | { |
768 | { |
|
|
769 | assert (!next->stack); |
742 | allocate_stack (next, 1); |
770 | allocate_stack (next, 1); |
743 | coro_create (&(next->stack->cctx), |
771 | coro_create (&(next->stack->cctx), |
744 | setup_coro, (void *)next, |
772 | setup_coro, (void *)next, |
745 | next->stack->sptr, labs (next->stack->ssize)); |
773 | next->stack->sptr, labs (next->stack->ssize)); |
746 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
774 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
… | |
… | |
924 | Coro__State coro; |
952 | Coro__State coro; |
925 | |
953 | |
926 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
954 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
927 | croak ("Coro::State::_newprocess expects an arrayref"); |
955 | croak ("Coro::State::_newprocess expects an arrayref"); |
928 | |
956 | |
929 | New (0, coro, 1, struct coro); |
957 | Newz (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 | |
… | |
… | |
970 | |
1004 | |
971 | coro->mainstack = 0; |
1005 | coro->mainstack = 0; |
972 | } |
1006 | } |
973 | |
1007 | |
974 | deallocate_stack (coro); |
1008 | deallocate_stack (coro); |
975 | |
1009 | SvREFCNT_dec (coro->args); |
976 | Safefree (coro); |
1010 | Safefree (coro); |
977 | |
1011 | |
978 | void |
1012 | void |
979 | flush() |
1013 | flush() |
980 | CODE: |
1014 | CODE: |
… | |
… | |
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--) |