… | |
… | |
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 | |
… | |
… | |
355 | |
358 | |
356 | PUSHs (Nullsv); |
359 | PUSHs (Nullsv); |
357 | /* this loop was inspired by pp_caller */ |
360 | /* this loop was inspired by pp_caller */ |
358 | for (;;) |
361 | for (;;) |
359 | { |
362 | { |
360 | do |
363 | while (cxix >= 0) |
361 | { |
364 | { |
362 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
365 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
363 | |
366 | |
364 | if (CxTYPE(cx) == CXt_SUB) |
367 | if (CxTYPE(cx) == CXt_SUB) |
365 | { |
368 | { |
… | |
… | |
391 | /* I never used formats, so how should I know how these are implemented? */ |
394 | /* I never used formats, so how should I know how these are implemented? */ |
392 | /* my bold guess is as a simple, plain sub... */ |
395 | /* my bold guess is as a simple, plain sub... */ |
393 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
396 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
394 | } |
397 | } |
395 | } |
398 | } |
396 | while (cxix >= 0); |
|
|
397 | |
399 | |
398 | if (top_si->si_type == PERLSI_MAIN) |
400 | if (top_si->si_type == PERLSI_MAIN) |
399 | break; |
401 | break; |
400 | |
402 | |
401 | top_si = top_si->si_prev; |
403 | top_si = top_si->si_prev; |
… | |
… | |
488 | * still there is a memleak of 128 bytes... |
490 | * still there is a memleak of 128 bytes... |
489 | */ |
491 | */ |
490 | STATIC void |
492 | STATIC void |
491 | destroy_stacks(pTHX) |
493 | destroy_stacks(pTHX) |
492 | { |
494 | { |
|
|
495 | if (!IN_DESTRUCT) |
|
|
496 | { |
493 | /* is this ugly, I ask? */ |
497 | /* is this ugly, I ask? */ |
494 | while (PL_scopestack_ix) |
498 | while (PL_scopestack_ix) |
495 | LEAVE; |
499 | LEAVE; |
496 | |
500 | |
497 | /* sure it is, but more important: is it correct?? :/ */ |
501 | /* sure it is, but more important: is it correct?? :/ */ |
498 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
502 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
499 | FREETMPS; |
503 | FREETMPS; |
|
|
504 | } |
500 | |
505 | |
501 | while (PL_curstackinfo->si_next) |
506 | while (PL_curstackinfo->si_next) |
502 | PL_curstackinfo = PL_curstackinfo->si_next; |
507 | PL_curstackinfo = PL_curstackinfo->si_next; |
503 | |
508 | |
504 | while (PL_curstackinfo) |
509 | while (PL_curstackinfo) |
… | |
… | |
509 | dSP; |
514 | dSP; |
510 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
515 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
511 | PUTBACK; /* possibly superfluous */ |
516 | PUTBACK; /* possibly superfluous */ |
512 | } |
517 | } |
513 | |
518 | |
514 | if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ |
519 | if (!IN_DESTRUCT) |
|
|
520 | { |
515 | dounwind(-1); |
521 | dounwind(-1); |
516 | |
|
|
517 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
522 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
523 | } |
|
|
524 | |
518 | Safefree(PL_curstackinfo->si_cxstack); |
525 | Safefree(PL_curstackinfo->si_cxstack); |
519 | Safefree(PL_curstackinfo); |
526 | Safefree(PL_curstackinfo); |
520 | PL_curstackinfo = p; |
527 | PL_curstackinfo = p; |
521 | } |
528 | } |
522 | |
529 | |
… | |
… | |
537 | stack->refcnt = 1; |
544 | stack->refcnt = 1; |
538 | stack->usecnt = 1; |
545 | stack->usecnt = 1; |
539 | stack->gencnt = ctx->gencnt = 0; |
546 | stack->gencnt = ctx->gencnt = 0; |
540 | if (alloc) |
547 | if (alloc) |
541 | { |
548 | { |
542 | #ifdef HAVE_MMAP |
549 | #if HAVE_MMAP |
543 | 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 */ |
544 | 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); |
545 | if (stack->sptr == (void *)-1) |
552 | if (stack->sptr == (void *)-1) |
546 | #endif |
553 | #endif |
547 | { |
554 | { |
548 | /*FIXME*//*D*//* reasonable stack size! */ |
555 | /*FIXME*//*D*//* reasonable stack size! */ |
549 | stack->ssize = -4096 * sizeof (long); |
556 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
563 | |
570 | |
564 | ctx->stack = 0; |
571 | ctx->stack = 0; |
565 | |
572 | |
566 | if (stack) |
573 | if (stack) |
567 | { |
574 | { |
568 | printf ("deallocating stack %p %d\n", stack, stack->refcnt);/*D*/ |
|
|
569 | if (!--stack->refcnt) |
575 | if (!--stack->refcnt) |
570 | { |
576 | { |
571 | #ifdef HAVE_MMAP |
577 | #ifdef HAVE_MMAP |
572 | if (stack->ssize > 0 && stack->sptr) |
578 | if (stack->ssize > 0 && stack->sptr) |
573 | munmap (stack->sptr, stack->ssize); |
579 | munmap (stack->sptr, stack->ssize); |
… | |
… | |
645 | { |
651 | { |
646 | /* |
652 | /* |
647 | * this is a _very_ stripped down perl interpreter ;) |
653 | * this is a _very_ stripped down perl interpreter ;) |
648 | */ |
654 | */ |
649 | Coro__State ctx = (Coro__State)arg; |
655 | Coro__State ctx = (Coro__State)arg; |
|
|
656 | JMPENV coro_start_env; |
650 | |
657 | |
651 | /*FIXME*//* must set up top_env here */ |
658 | /* same as JMPENV_BOOTSTRAP */ |
|
|
659 | Zero(&coro_start_env, 1, JMPENV); |
|
|
660 | coro_start_env.je_ret = -1; |
|
|
661 | coro_start_env.je_mustcatch = TRUE; |
|
|
662 | PL_top_env = &coro_start_env; |
|
|
663 | |
652 | ctx->cursp = 0; |
664 | ctx->cursp = 0; |
653 | PL_op = PL_op->op_next; |
665 | PL_op = PL_op->op_next; |
654 | CALLRUNOPS(aTHX); |
666 | CALLRUNOPS(aTHX); |
655 | |
667 | |
656 | abort (); |
668 | abort (); |
… | |
… | |
660 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
672 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
661 | { |
673 | { |
662 | dSTACKLEVEL; |
674 | dSTACKLEVEL; |
663 | static struct coro *xnext; |
675 | static struct coro *xnext; |
664 | |
676 | |
665 | printf ("%p => %p\n", prev, next);/*D*/ |
|
|
666 | if (prev != next) |
677 | if (prev != next) |
667 | { |
678 | { |
668 | xnext = next; |
679 | xnext = next; |
669 | |
680 | |
670 | if (next->mainstack) |
681 | if (next->mainstack) |
… | |
… | |
734 | } |
745 | } |
735 | } |
746 | } |
736 | else |
747 | else |
737 | setup_coro (next); |
748 | setup_coro (next); |
738 | } |
749 | } |
739 | } |
|
|
740 | |
750 | |
|
|
751 | /* |
|
|
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 |
|
|
754 | * variable, that should become thread-specific at one point. |
|
|
755 | */ |
741 | xnext->cursp = stacklevel; |
756 | xnext->cursp = stacklevel; |
|
|
757 | } |
742 | } |
758 | } |
743 | |
759 | |
744 | static struct coro * |
760 | static struct coro * |
745 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
761 | sv_to_coro (SV *arg, const char *funcname, const char *varname) |
746 | { |
762 | { |
… | |
… | |
753 | |
769 | |
754 | arg = HeVAL(he); |
770 | arg = HeVAL(he); |
755 | } |
771 | } |
756 | |
772 | |
757 | /* must also be changed inside Coro::Cont::yield */ |
773 | /* must also be changed inside Coro::Cont::yield */ |
|
|
774 | if (SvROK(arg) && SvOBJECT(SvRV(arg)) |
758 | if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) |
775 | && SvSTASH(SvRV(arg)) == coro_state_stash) |
759 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
776 | return (struct coro *) SvIV((SV*)SvRV(arg)); |
760 | |
777 | |
761 | 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); |
762 | /*NORETURN*/ |
779 | /*NORETURN*/ |
763 | } |
780 | } |
… | |
… | |
1027 | } |
1044 | } |
1028 | |
1045 | |
1029 | void |
1046 | void |
1030 | ready(self) |
1047 | ready(self) |
1031 | SV * self |
1048 | SV * self |
|
|
1049 | PROTOTYPE: $ |
1032 | CODE: |
1050 | CODE: |
1033 | api_ready (self); |
1051 | api_ready (self); |
1034 | |
1052 | |
1035 | int |
1053 | int |
1036 | nready(...) |
1054 | nready(...) |