… | |
… | |
28 | # define IS_PADCONST(v) 0 |
28 | # define IS_PADCONST(v) 0 |
29 | # endif |
29 | # endif |
30 | #endif |
30 | #endif |
31 | |
31 | |
32 | #include <errno.h> |
32 | #include <errno.h> |
33 | #include <signal.h> |
|
|
34 | |
33 | |
35 | #ifdef HAVE_MMAP |
34 | #ifdef HAVE_MMAP |
36 | # include <unistd.h> |
35 | # include <unistd.h> |
37 | # include <sys/mman.h> |
36 | # include <sys/mman.h> |
38 | # ifndef MAP_ANONYMOUS |
37 | # ifndef MAP_ANONYMOUS |
… | |
… | |
275 | /* casting is fun. */ |
274 | /* casting is fun. */ |
276 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
275 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
277 | free_padlist (aTHX_ padlist); |
276 | free_padlist (aTHX_ padlist); |
278 | |
277 | |
279 | SvREFCNT_dec (av); |
278 | SvREFCNT_dec (av); |
|
|
279 | |
|
|
280 | return 0; |
280 | } |
281 | } |
281 | |
282 | |
282 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
283 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
283 | |
284 | |
284 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
285 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
… | |
… | |
346 | PL_scopestack_ix = c->scopestack_ix; |
347 | PL_scopestack_ix = c->scopestack_ix; |
347 | PL_scopestack_max = c->scopestack_max; |
348 | PL_scopestack_max = c->scopestack_max; |
348 | PL_savestack = c->savestack; |
349 | PL_savestack = c->savestack; |
349 | PL_savestack_ix = c->savestack_ix; |
350 | PL_savestack_ix = c->savestack_ix; |
350 | PL_savestack_max = c->savestack_max; |
351 | PL_savestack_max = c->savestack_max; |
351 | #ifdef PL_retstack |
352 | #if PERL_VERSION < 9 |
352 | PL_retstack = c->retstack; |
353 | PL_retstack = c->retstack; |
353 | PL_retstack_ix = c->retstack_ix; |
354 | PL_retstack_ix = c->retstack_ix; |
354 | PL_retstack_max = c->retstack_max; |
355 | PL_retstack_max = c->retstack_max; |
355 | #endif |
356 | #endif |
356 | PL_curpm = c->curpm; |
357 | PL_curpm = c->curpm; |
… | |
… | |
474 | c->scopestack_ix = PL_scopestack_ix; |
475 | c->scopestack_ix = PL_scopestack_ix; |
475 | c->scopestack_max = PL_scopestack_max; |
476 | c->scopestack_max = PL_scopestack_max; |
476 | c->savestack = PL_savestack; |
477 | c->savestack = PL_savestack; |
477 | c->savestack_ix = PL_savestack_ix; |
478 | c->savestack_ix = PL_savestack_ix; |
478 | c->savestack_max = PL_savestack_max; |
479 | c->savestack_max = PL_savestack_max; |
479 | #ifdef PL_retstack |
480 | #if PERL_VERSION < 9 |
480 | c->retstack = PL_retstack; |
481 | c->retstack = PL_retstack; |
481 | c->retstack_ix = PL_retstack_ix; |
482 | c->retstack_ix = PL_retstack_ix; |
482 | c->retstack_max = PL_retstack_max; |
483 | c->retstack_max = PL_retstack_max; |
483 | #endif |
484 | #endif |
484 | c->curpm = PL_curpm; |
485 | c->curpm = PL_curpm; |
… | |
… | |
525 | |
526 | |
526 | New(54,PL_savestack,96,ANY); |
527 | New(54,PL_savestack,96,ANY); |
527 | PL_savestack_ix = 0; |
528 | PL_savestack_ix = 0; |
528 | PL_savestack_max = 96; |
529 | PL_savestack_max = 96; |
529 | |
530 | |
530 | #ifdef PL_retstack |
531 | #if PERL_VERSION < 9 |
531 | New(54,PL_retstack,8,OP*); |
532 | New(54,PL_retstack,8,OP*); |
532 | PL_retstack_ix = 0; |
533 | PL_retstack_ix = 0; |
533 | PL_retstack_max = 8; |
534 | PL_retstack_max = 8; |
534 | #endif |
535 | #endif |
535 | |
536 | |
… | |
… | |
579 | |
580 | |
580 | Safefree (PL_tmps_stack); |
581 | Safefree (PL_tmps_stack); |
581 | Safefree (PL_markstack); |
582 | Safefree (PL_markstack); |
582 | Safefree (PL_scopestack); |
583 | Safefree (PL_scopestack); |
583 | Safefree (PL_savestack); |
584 | Safefree (PL_savestack); |
584 | #ifdef PL_retstack |
585 | #if PERL_VERSION < 9 |
585 | Safefree (PL_retstack); |
586 | Safefree (PL_retstack); |
586 | #endif |
587 | #endif |
587 | } |
588 | } |
588 | |
589 | |
589 | static void |
590 | static void |
… | |
… | |
603 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
604 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
604 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
605 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
605 | if (stack->sptr == (void *)-1) |
606 | if (stack->sptr == (void *)-1) |
606 | #endif |
607 | #endif |
607 | { |
608 | { |
608 | /*FIXME*//*D*//* reasonable stack size! */ |
|
|
609 | stack->ssize = - (STACKSIZE * sizeof (long)); |
609 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
610 | New (0, stack->sptr, STACKSIZE, long); |
610 | New (0, stack->sptr, STACKSIZE, long); |
611 | } |
611 | } |
612 | } |
612 | } |
613 | else |
613 | else |
614 | stack->sptr = 0; |
614 | stack->sptr = 0; |
… | |
… | |
706 | /* |
706 | /* |
707 | * this is a _very_ stripped down perl interpreter ;) |
707 | * this is a _very_ stripped down perl interpreter ;) |
708 | */ |
708 | */ |
709 | dTHX; |
709 | dTHX; |
710 | Coro__State ctx = (Coro__State)arg; |
710 | Coro__State ctx = (Coro__State)arg; |
711 | JMPENV coro_start_env; |
|
|
712 | |
711 | |
713 | PL_top_env = &ctx->start_env; |
712 | PL_top_env = &ctx->start_env; |
714 | |
713 | |
715 | ctx->cursp = 0; |
714 | ctx->cursp = 0; |
716 | PL_op = PL_op->op_next; |
715 | PL_op = PL_op->op_next; |
… | |
… | |
839 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
838 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
840 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
839 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
841 | \ |
840 | \ |
842 | } while(0) |
841 | } while(0) |
843 | |
842 | |
844 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
843 | #define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) |
845 | |
844 | |
846 | static void |
845 | static void |
847 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
846 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
848 | { |
847 | { |
849 | SV_CORO (prev, "Coro::transfer"); |
848 | SV_CORO (prev, "Coro::transfer"); |
… | |
… | |
867 | static int coro_nready; |
866 | static int coro_nready; |
868 | |
867 | |
869 | static void |
868 | static void |
870 | coro_enq (pTHX_ SV *sv) |
869 | coro_enq (pTHX_ SV *sv) |
871 | { |
870 | { |
|
|
871 | SV **xprio; |
|
|
872 | int prio; |
|
|
873 | |
872 | if (SvTYPE (sv) == SVt_PVHV) |
874 | if (SvTYPE (sv) != SVt_PVHV) |
873 | { |
|
|
874 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
875 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
876 | |
|
|
877 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
878 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
879 | : prio; |
|
|
880 | |
|
|
881 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
882 | coro_nready++; |
|
|
883 | |
|
|
884 | return; |
|
|
885 | } |
|
|
886 | |
|
|
887 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
875 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
876 | |
|
|
877 | xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
878 | prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
879 | |
|
|
880 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
881 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
882 | : prio; |
|
|
883 | |
|
|
884 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
885 | coro_nready++; |
888 | } |
886 | } |
889 | |
887 | |
890 | static SV * |
888 | static SV * |
891 | coro_deq (pTHX_ int min_prio) |
889 | coro_deq (pTHX_ int min_prio) |
892 | { |
890 | { |
… | |
… | |
1081 | av_fill (defav, items - 1); |
1079 | av_fill (defav, items - 1); |
1082 | while (items--) |
1080 | while (items--) |
1083 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1081 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1084 | |
1082 | |
1085 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1083 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1086 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1084 | prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)))); |
1087 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1085 | next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)))); |
1088 | SvREFCNT_dec (sv); |
1086 | SvREFCNT_dec (sv); |
1089 | |
1087 | |
1090 | transfer (aTHX_ prev, next, 0); |
1088 | transfer (aTHX_ prev, next, 0); |
1091 | |
1089 | |
1092 | MODULE = Coro::State PACKAGE = Coro |
1090 | MODULE = Coro::State PACKAGE = Coro |