… | |
… | |
27 | # ifndef IS_PADCONST |
27 | # ifndef IS_PADCONST |
28 | # define IS_PADCONST(v) 0 |
28 | # define IS_PADCONST(v) 0 |
29 | # endif |
29 | # endif |
30 | #endif |
30 | #endif |
31 | |
31 | |
32 | #include <signal.h> |
32 | #include <errno.h> |
33 | |
33 | |
34 | #ifdef HAVE_MMAP |
34 | #ifdef HAVE_MMAP |
35 | # include <unistd.h> |
35 | # include <unistd.h> |
36 | # include <sys/mman.h> |
36 | # include <sys/mman.h> |
37 | # ifndef MAP_ANONYMOUS |
37 | # ifndef MAP_ANONYMOUS |
… | |
… | |
57 | |
57 | |
58 | #include "CoroAPI.h" |
58 | #include "CoroAPI.h" |
59 | |
59 | |
60 | #ifdef USE_ITHREADS |
60 | #ifdef USE_ITHREADS |
61 | static perl_mutex coro_mutex; |
61 | static perl_mutex coro_mutex; |
62 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
62 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
63 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
63 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
64 | #else |
64 | #else |
65 | # define LOCK 0 |
65 | # define LOCK (void)0 |
66 | # define UNLOCK 0 |
66 | # define UNLOCK (void)0 |
67 | #endif |
67 | #endif |
68 | |
68 | |
69 | static struct CoroAPI coroapi; |
69 | static struct CoroAPI coroapi; |
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
71 | static HV *coro_state_stash; |
71 | static HV *coro_state_stash; |
… | |
… | |
170 | av_store (newpadlist, 1, (SV *) newpad); |
170 | av_store (newpadlist, 1, (SV *) newpad); |
171 | |
171 | |
172 | av = newAV (); /* will be @_ */ |
172 | av = newAV (); /* will be @_ */ |
173 | av_extend (av, 0); |
173 | av_extend (av, 0); |
174 | av_store (newpad, 0, (SV *) av); |
174 | av_store (newpad, 0, (SV *) av); |
175 | AvFLAGS (av) = AVf_REIFY; |
175 | AvREIFY_on (av); |
176 | |
176 | |
177 | for (ix = fpad; ix > 0; ix--) |
177 | for (ix = fpad; ix > 0; ix--) |
178 | { |
178 | { |
179 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
179 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
180 | |
180 | |
… | |
… | |
274 | /* casting is fun. */ |
274 | /* casting is fun. */ |
275 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
275 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
276 | free_padlist (aTHX_ padlist); |
276 | free_padlist (aTHX_ padlist); |
277 | |
277 | |
278 | SvREFCNT_dec (av); |
278 | SvREFCNT_dec (av); |
|
|
279 | |
|
|
280 | return 0; |
279 | } |
281 | } |
280 | |
282 | |
281 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
283 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
282 | |
284 | |
283 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
285 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
… | |
… | |
345 | PL_scopestack_ix = c->scopestack_ix; |
347 | PL_scopestack_ix = c->scopestack_ix; |
346 | PL_scopestack_max = c->scopestack_max; |
348 | PL_scopestack_max = c->scopestack_max; |
347 | PL_savestack = c->savestack; |
349 | PL_savestack = c->savestack; |
348 | PL_savestack_ix = c->savestack_ix; |
350 | PL_savestack_ix = c->savestack_ix; |
349 | PL_savestack_max = c->savestack_max; |
351 | PL_savestack_max = c->savestack_max; |
|
|
352 | #if PERL_VERSION < 9 |
350 | PL_retstack = c->retstack; |
353 | PL_retstack = c->retstack; |
351 | PL_retstack_ix = c->retstack_ix; |
354 | PL_retstack_ix = c->retstack_ix; |
352 | PL_retstack_max = c->retstack_max; |
355 | PL_retstack_max = c->retstack_max; |
|
|
356 | #endif |
353 | PL_curpm = c->curpm; |
357 | PL_curpm = c->curpm; |
354 | PL_curcop = c->curcop; |
358 | PL_curcop = c->curcop; |
355 | PL_top_env = c->top_env; |
359 | PL_top_env = c->top_env; |
356 | |
360 | |
357 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
361 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
… | |
… | |
471 | c->scopestack_ix = PL_scopestack_ix; |
475 | c->scopestack_ix = PL_scopestack_ix; |
472 | c->scopestack_max = PL_scopestack_max; |
476 | c->scopestack_max = PL_scopestack_max; |
473 | c->savestack = PL_savestack; |
477 | c->savestack = PL_savestack; |
474 | c->savestack_ix = PL_savestack_ix; |
478 | c->savestack_ix = PL_savestack_ix; |
475 | c->savestack_max = PL_savestack_max; |
479 | c->savestack_max = PL_savestack_max; |
|
|
480 | #if PERL_VERSION < 9 |
476 | c->retstack = PL_retstack; |
481 | c->retstack = PL_retstack; |
477 | c->retstack_ix = PL_retstack_ix; |
482 | c->retstack_ix = PL_retstack_ix; |
478 | c->retstack_max = PL_retstack_max; |
483 | c->retstack_max = PL_retstack_max; |
|
|
484 | #endif |
479 | c->curpm = PL_curpm; |
485 | c->curpm = PL_curpm; |
480 | c->curcop = PL_curcop; |
486 | c->curcop = PL_curcop; |
481 | c->top_env = PL_top_env; |
487 | c->top_env = PL_top_env; |
482 | } |
488 | } |
483 | |
489 | |
… | |
… | |
520 | |
526 | |
521 | New(54,PL_savestack,96,ANY); |
527 | New(54,PL_savestack,96,ANY); |
522 | PL_savestack_ix = 0; |
528 | PL_savestack_ix = 0; |
523 | PL_savestack_max = 96; |
529 | PL_savestack_max = 96; |
524 | |
530 | |
|
|
531 | #if PERL_VERSION < 9 |
525 | New(54,PL_retstack,8,OP*); |
532 | New(54,PL_retstack,8,OP*); |
526 | PL_retstack_ix = 0; |
533 | PL_retstack_ix = 0; |
527 | PL_retstack_max = 8; |
534 | PL_retstack_max = 8; |
|
|
535 | #endif |
528 | |
536 | |
529 | UNLOCK; |
537 | UNLOCK; |
530 | } |
538 | } |
531 | |
539 | |
532 | /* |
540 | /* |
… | |
… | |
572 | |
580 | |
573 | Safefree (PL_tmps_stack); |
581 | Safefree (PL_tmps_stack); |
574 | Safefree (PL_markstack); |
582 | Safefree (PL_markstack); |
575 | Safefree (PL_scopestack); |
583 | Safefree (PL_scopestack); |
576 | Safefree (PL_savestack); |
584 | Safefree (PL_savestack); |
|
|
585 | #if PERL_VERSION < 9 |
577 | Safefree (PL_retstack); |
586 | Safefree (PL_retstack); |
|
|
587 | #endif |
578 | } |
588 | } |
579 | |
589 | |
580 | static void |
590 | static void |
581 | allocate_stack (Coro__State ctx, int alloc) |
591 | allocate_stack (Coro__State ctx, int alloc) |
582 | { |
592 | { |
… | |
… | |
589 | stack->gencnt = ctx->gencnt = 0; |
599 | stack->gencnt = ctx->gencnt = 0; |
590 | |
600 | |
591 | if (alloc) |
601 | if (alloc) |
592 | { |
602 | { |
593 | #if HAVE_MMAP |
603 | #if HAVE_MMAP |
594 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
604 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
595 | 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); |
596 | if (stack->sptr == (void *)-1) |
606 | if (stack->sptr == (void *)-1) |
597 | #endif |
607 | #endif |
598 | { |
608 | { |
599 | /*FIXME*//*D*//* reasonable stack size! */ |
|
|
600 | stack->ssize = - (8192 * sizeof (long)); |
609 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
601 | New (0, stack->sptr, 8192, long); |
610 | New (0, stack->sptr, STACKSIZE, long); |
602 | } |
611 | } |
603 | } |
612 | } |
604 | else |
613 | else |
605 | stack->sptr = 0; |
614 | stack->sptr = 0; |
606 | |
615 | |
… | |
… | |
697 | /* |
706 | /* |
698 | * this is a _very_ stripped down perl interpreter ;) |
707 | * this is a _very_ stripped down perl interpreter ;) |
699 | */ |
708 | */ |
700 | dTHX; |
709 | dTHX; |
701 | Coro__State ctx = (Coro__State)arg; |
710 | Coro__State ctx = (Coro__State)arg; |
702 | JMPENV coro_start_env; |
|
|
703 | |
711 | |
704 | PL_top_env = &ctx->start_env; |
712 | PL_top_env = &ctx->start_env; |
705 | |
713 | |
706 | ctx->cursp = 0; |
714 | ctx->cursp = 0; |
707 | PL_op = PL_op->op_next; |
715 | PL_op = PL_op->op_next; |
… | |
… | |
830 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
838 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
831 | 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); \ |
832 | \ |
840 | \ |
833 | } while(0) |
841 | } while(0) |
834 | |
842 | |
835 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
843 | #define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) |
836 | |
844 | |
837 | static void |
845 | static void |
838 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
846 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
839 | { |
847 | { |
840 | SV_CORO (prev, "Coro::transfer"); |
848 | SV_CORO (prev, "Coro::transfer"); |
… | |
… | |
858 | static int coro_nready; |
866 | static int coro_nready; |
859 | |
867 | |
860 | static void |
868 | static void |
861 | coro_enq (pTHX_ SV *sv) |
869 | coro_enq (pTHX_ SV *sv) |
862 | { |
870 | { |
|
|
871 | SV **xprio; |
|
|
872 | int prio; |
|
|
873 | |
863 | if (SvTYPE (sv) == SVt_PVHV) |
874 | if (SvTYPE (sv) != SVt_PVHV) |
864 | { |
|
|
865 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
866 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
867 | |
|
|
868 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
869 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
870 | : prio; |
|
|
871 | |
|
|
872 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
873 | coro_nready++; |
|
|
874 | |
|
|
875 | return; |
|
|
876 | } |
|
|
877 | |
|
|
878 | 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++; |
879 | } |
886 | } |
880 | |
887 | |
881 | static SV * |
888 | static SV * |
882 | coro_deq (pTHX_ int min_prio) |
889 | coro_deq (pTHX_ int min_prio) |
883 | { |
890 | { |
… | |
… | |
1072 | av_fill (defav, items - 1); |
1079 | av_fill (defav, items - 1); |
1073 | while (items--) |
1080 | while (items--) |
1074 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1081 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1075 | |
1082 | |
1076 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1083 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1077 | 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)))); |
1078 | 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)))); |
1079 | SvREFCNT_dec (sv); |
1086 | SvREFCNT_dec (sv); |
1080 | |
1087 | |
1081 | transfer (aTHX_ prev, next, 0); |
1088 | transfer (aTHX_ prev, next, 0); |
1082 | |
1089 | |
1083 | MODULE = Coro::State PACKAGE = Coro |
1090 | MODULE = Coro::State PACKAGE = Coro |
… | |
… | |
1146 | cede(...) |
1153 | cede(...) |
1147 | PROTOTYPE: |
1154 | PROTOTYPE: |
1148 | CODE: |
1155 | CODE: |
1149 | api_cede (); |
1156 | api_cede (); |
1150 | |
1157 | |
|
|
1158 | # and these are hacks |
|
|
1159 | SV * |
|
|
1160 | _aio_get_state () |
|
|
1161 | CODE: |
|
|
1162 | { |
|
|
1163 | struct { |
|
|
1164 | int errorno; |
|
|
1165 | int laststype; |
|
|
1166 | int laststatval; |
|
|
1167 | Stat_t statcache; |
|
|
1168 | } data; |
|
|
1169 | |
|
|
1170 | data.errorno = errno; |
|
|
1171 | data.laststype = PL_laststype; |
|
|
1172 | data.laststatval = PL_laststatval; |
|
|
1173 | data.statcache = PL_statcache; |
|
|
1174 | |
|
|
1175 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1176 | } |
|
|
1177 | OUTPUT: |
|
|
1178 | RETVAL |
|
|
1179 | |
|
|
1180 | void |
|
|
1181 | _aio_set_state (char *data_) |
|
|
1182 | PROTOTYPE: $ |
|
|
1183 | CODE: |
|
|
1184 | { |
|
|
1185 | struct { |
|
|
1186 | int errorno; |
|
|
1187 | int laststype; |
|
|
1188 | int laststatval; |
|
|
1189 | Stat_t statcache; |
|
|
1190 | } *data = (void *)data_; |
|
|
1191 | |
|
|
1192 | errno = data->errorno; |
|
|
1193 | PL_laststype = data->laststype; |
|
|
1194 | PL_laststatval = data->laststatval; |
|
|
1195 | PL_statcache = data->statcache; |
|
|
1196 | } |