… | |
… | |
7 | #include <signal.h> |
7 | #include <signal.h> |
8 | |
8 | |
9 | #ifdef HAVE_MMAP |
9 | #ifdef HAVE_MMAP |
10 | # include <unistd.h> |
10 | # include <unistd.h> |
11 | # include <sys/mman.h> |
11 | # include <sys/mman.h> |
12 | # ifndef MAP_ANON |
|
|
13 | # ifdef MAP_ANONYMOUS |
12 | # ifndef MAP_ANONYMOUS |
14 | # define MAP_ANON MAP_ANONYMOUS |
13 | # ifdef MAP_ANON |
|
|
14 | # define MAP_ANONYMOUS MAP_ANON |
15 | # else |
15 | # else |
16 | # undef HAVE_MMAP |
16 | # undef HAVE_MMAP |
17 | # endif |
17 | # endif |
18 | # endif |
18 | # endif |
19 | #endif |
19 | #endif |
… | |
… | |
26 | /* The next macro should declare a variable stacklevel that contains and approximation |
26 | /* The next macro should declare a variable stacklevel that contains and approximation |
27 | * to the current C stack pointer. Its property is that it changes with each call |
27 | * to the current C stack pointer. Its property is that it changes with each call |
28 | * and should be unique. */ |
28 | * and should be unique. */ |
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
29 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
30 | |
30 | |
|
|
31 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
|
|
32 | |
31 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
33 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
32 | |
34 | |
33 | #include "CoroAPI.h" |
35 | #include "CoroAPI.h" |
34 | |
36 | |
35 | static struct CoroAPI coroapi; |
37 | static struct CoroAPI coroapi; |
… | |
… | |
54 | |
56 | |
55 | /* optionally saved, might be zero */ |
57 | /* optionally saved, might be zero */ |
56 | AV *defav; |
58 | AV *defav; |
57 | SV *defsv; |
59 | SV *defsv; |
58 | SV *errsv; |
60 | SV *errsv; |
|
|
61 | /* TODO: PL_pmop might be nice && effortless to save. */ |
59 | |
62 | |
60 | /* saved global state not related to stacks */ |
63 | /* saved global state not related to stacks */ |
61 | U8 dowarn; |
64 | U8 dowarn; |
62 | I32 in_eval; |
65 | I32 in_eval; |
63 | |
66 | |
… | |
… | |
487 | * still there is a memleak of 128 bytes... |
490 | * still there is a memleak of 128 bytes... |
488 | */ |
491 | */ |
489 | STATIC void |
492 | STATIC void |
490 | destroy_stacks(pTHX) |
493 | destroy_stacks(pTHX) |
491 | { |
494 | { |
492 | int destruct = PL_main_cv != Nullcv; |
495 | if (!IN_DESTRUCT) |
493 | |
|
|
494 | if (destruct) |
|
|
495 | { |
496 | { |
496 | /* is this ugly, I ask? */ |
497 | /* is this ugly, I ask? */ |
497 | while (PL_scopestack_ix) |
498 | while (PL_scopestack_ix) |
498 | LEAVE; |
499 | LEAVE; |
499 | |
500 | |
… | |
… | |
513 | dSP; |
514 | dSP; |
514 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
515 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
515 | PUTBACK; /* possibly superfluous */ |
516 | PUTBACK; /* possibly superfluous */ |
516 | } |
517 | } |
517 | |
518 | |
518 | if (destruct) |
519 | if (!IN_DESTRUCT) |
519 | { |
520 | { |
520 | dounwind(-1); |
521 | dounwind(-1); |
521 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
522 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
522 | } |
523 | } |
523 | |
524 | |
… | |
… | |
543 | stack->refcnt = 1; |
544 | stack->refcnt = 1; |
544 | stack->usecnt = 1; |
545 | stack->usecnt = 1; |
545 | stack->gencnt = ctx->gencnt = 0; |
546 | stack->gencnt = ctx->gencnt = 0; |
546 | if (alloc) |
547 | if (alloc) |
547 | { |
548 | { |
548 | #ifdef HAVE_MMAP |
549 | #if HAVE_MMAP |
549 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
550 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
550 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
551 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
551 | if (stack->sptr == (void *)-1) |
552 | if (stack->sptr == (void *)-1) |
552 | #endif |
553 | #endif |
553 | { |
554 | { |
554 | /*FIXME*//*D*//* reasonable stack size! */ |
555 | /*FIXME*//*D*//* reasonable stack size! */ |
555 | stack->ssize = -4096 * sizeof (long); |
556 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
744 | } |
745 | } |
745 | } |
746 | } |
746 | else |
747 | else |
747 | setup_coro (next); |
748 | setup_coro (next); |
748 | } |
749 | } |
749 | } |
|
|
750 | |
750 | |
751 | /* |
751 | /* |
752 | * xnext is now either prev or next, depending on wether |
752 | * xnext is now either prev or next, depending on wether |
753 | * we switched the c stack or not. that's why i use a global |
753 | * we switched the c stack or not. that's why i use a global |
754 | * variable, that should become thread-specific at one point. |
754 | * variable, that should become thread-specific at one point. |
755 | */ |
755 | */ |
756 | xnext->cursp = stacklevel; |
756 | xnext->cursp = stacklevel; |
|
|
757 | } |
757 | } |
758 | } |
758 | |
759 | |
759 | static struct coro * |
760 | static struct coro * |
760 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
761 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
761 | { |
762 | { |
… | |
… | |
768 | |
769 | |
769 | arg = HeVAL(he); |
770 | arg = HeVAL(he); |
770 | } |
771 | } |
771 | |
772 | |
772 | /* must also be changed inside Coro::Cont::yield */ |
773 | /* must also be changed inside Coro::Cont::yield */ |
|
|
774 | if (SvROK(arg) && SvOBJECT(SvRV(arg)) |
773 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
775 | && SvSTASH(SvRV(arg)) == coro_state_stash) |
774 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
776 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
775 | |
777 | |
776 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
778 | croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); |
777 | /*NORETURN*/ |
779 | /*NORETURN*/ |
778 | } |
780 | } |
… | |
… | |
1042 | } |
1044 | } |
1043 | |
1045 | |
1044 | void |
1046 | void |
1045 | ready(self) |
1047 | ready(self) |
1046 | SV * self |
1048 | SV * self |
|
|
1049 | PROTOTYPE: $ |
1047 | CODE: |
1050 | CODE: |
1048 | api_ready (self); |
1051 | api_ready (self); |
1049 | |
1052 | |
1050 | int |
1053 | int |
1051 | nready(...) |
1054 | nready(...) |