… | |
… | |
6 | #include "perl.h" |
6 | #include "perl.h" |
7 | #include "XSUB.h" |
7 | #include "XSUB.h" |
8 | |
8 | |
9 | #include "patchlevel.h" |
9 | #include "patchlevel.h" |
10 | |
10 | |
11 | #if PATCHLEVEL < 6 |
11 | #if PERL_VERSION < 6 |
12 | # ifndef PL_ppaddr |
12 | # ifndef PL_ppaddr |
13 | # define PL_ppaddr ppaddr |
13 | # define PL_ppaddr ppaddr |
14 | # endif |
14 | # endif |
15 | # ifndef call_sv |
15 | # ifndef call_sv |
16 | # define call_sv perl_call_sv |
16 | # define call_sv perl_call_sv |
… | |
… | |
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 <errno.h> |
32 | #include <signal.h> |
33 | #include <signal.h> |
33 | |
34 | |
34 | #ifdef HAVE_MMAP |
35 | #ifdef HAVE_MMAP |
35 | # include <unistd.h> |
36 | # include <unistd.h> |
36 | # include <sys/mman.h> |
37 | # include <sys/mman.h> |
… | |
… | |
57 | |
58 | |
58 | #include "CoroAPI.h" |
59 | #include "CoroAPI.h" |
59 | |
60 | |
60 | #ifdef USE_ITHREADS |
61 | #ifdef USE_ITHREADS |
61 | static perl_mutex coro_mutex; |
62 | static perl_mutex coro_mutex; |
62 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
63 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
63 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
64 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
64 | #else |
65 | #else |
65 | # define LOCK 0 |
66 | # define LOCK (void)0 |
66 | # define UNLOCK 0 |
67 | # define UNLOCK (void)0 |
67 | #endif |
68 | #endif |
68 | |
69 | |
69 | static struct CoroAPI coroapi; |
70 | static struct CoroAPI coroapi; |
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
71 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
71 | static HV *coro_state_stash; |
72 | static HV *coro_state_stash; |
… | |
… | |
128 | I32 savestack_ix; |
129 | I32 savestack_ix; |
129 | I32 savestack_max; |
130 | I32 savestack_max; |
130 | OP **retstack; |
131 | OP **retstack; |
131 | I32 retstack_ix; |
132 | I32 retstack_ix; |
132 | I32 retstack_max; |
133 | I32 retstack_max; |
|
|
134 | PMOP *curpm; |
133 | COP *curcop; |
135 | COP *curcop; |
134 | JMPENV *top_env; |
136 | JMPENV *top_env; |
135 | |
137 | |
136 | /* data associated with this coroutine (initial args) */ |
138 | /* data associated with this coroutine (initial args) */ |
137 | AV *args; |
139 | AV *args; |
… | |
… | |
169 | av_store (newpadlist, 1, (SV *) newpad); |
171 | av_store (newpadlist, 1, (SV *) newpad); |
170 | |
172 | |
171 | av = newAV (); /* will be @_ */ |
173 | av = newAV (); /* will be @_ */ |
172 | av_extend (av, 0); |
174 | av_extend (av, 0); |
173 | av_store (newpad, 0, (SV *) av); |
175 | av_store (newpad, 0, (SV *) av); |
174 | AvFLAGS (av) = AVf_REIFY; |
176 | AvREIFY_on (av); |
175 | |
177 | |
176 | for (ix = fpad; ix > 0; ix--) |
178 | for (ix = fpad; ix > 0; ix--) |
177 | { |
179 | { |
178 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
180 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
179 | |
181 | |
… | |
… | |
344 | PL_scopestack_ix = c->scopestack_ix; |
346 | PL_scopestack_ix = c->scopestack_ix; |
345 | PL_scopestack_max = c->scopestack_max; |
347 | PL_scopestack_max = c->scopestack_max; |
346 | PL_savestack = c->savestack; |
348 | PL_savestack = c->savestack; |
347 | PL_savestack_ix = c->savestack_ix; |
349 | PL_savestack_ix = c->savestack_ix; |
348 | PL_savestack_max = c->savestack_max; |
350 | PL_savestack_max = c->savestack_max; |
|
|
351 | #if PERL_VERSION < 9 |
349 | PL_retstack = c->retstack; |
352 | PL_retstack = c->retstack; |
350 | PL_retstack_ix = c->retstack_ix; |
353 | PL_retstack_ix = c->retstack_ix; |
351 | PL_retstack_max = c->retstack_max; |
354 | PL_retstack_max = c->retstack_max; |
|
|
355 | #endif |
|
|
356 | PL_curpm = c->curpm; |
352 | PL_curcop = c->curcop; |
357 | PL_curcop = c->curcop; |
353 | PL_top_env = c->top_env; |
358 | PL_top_env = c->top_env; |
354 | |
359 | |
355 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
360 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
356 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
361 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
… | |
… | |
469 | c->scopestack_ix = PL_scopestack_ix; |
474 | c->scopestack_ix = PL_scopestack_ix; |
470 | c->scopestack_max = PL_scopestack_max; |
475 | c->scopestack_max = PL_scopestack_max; |
471 | c->savestack = PL_savestack; |
476 | c->savestack = PL_savestack; |
472 | c->savestack_ix = PL_savestack_ix; |
477 | c->savestack_ix = PL_savestack_ix; |
473 | c->savestack_max = PL_savestack_max; |
478 | c->savestack_max = PL_savestack_max; |
|
|
479 | #if PERL_VERSION < 9 |
474 | c->retstack = PL_retstack; |
480 | c->retstack = PL_retstack; |
475 | c->retstack_ix = PL_retstack_ix; |
481 | c->retstack_ix = PL_retstack_ix; |
476 | c->retstack_max = PL_retstack_max; |
482 | c->retstack_max = PL_retstack_max; |
|
|
483 | #endif |
|
|
484 | c->curpm = PL_curpm; |
477 | c->curcop = PL_curcop; |
485 | c->curcop = PL_curcop; |
478 | c->top_env = PL_top_env; |
486 | c->top_env = PL_top_env; |
479 | } |
487 | } |
480 | |
488 | |
481 | /* |
489 | /* |
… | |
… | |
517 | |
525 | |
518 | New(54,PL_savestack,96,ANY); |
526 | New(54,PL_savestack,96,ANY); |
519 | PL_savestack_ix = 0; |
527 | PL_savestack_ix = 0; |
520 | PL_savestack_max = 96; |
528 | PL_savestack_max = 96; |
521 | |
529 | |
|
|
530 | #if PERL_VERSION < 9 |
522 | New(54,PL_retstack,8,OP*); |
531 | New(54,PL_retstack,8,OP*); |
523 | PL_retstack_ix = 0; |
532 | PL_retstack_ix = 0; |
524 | PL_retstack_max = 8; |
533 | PL_retstack_max = 8; |
|
|
534 | #endif |
525 | |
535 | |
526 | UNLOCK; |
536 | UNLOCK; |
527 | } |
537 | } |
528 | |
538 | |
529 | /* |
539 | /* |
… | |
… | |
569 | |
579 | |
570 | Safefree (PL_tmps_stack); |
580 | Safefree (PL_tmps_stack); |
571 | Safefree (PL_markstack); |
581 | Safefree (PL_markstack); |
572 | Safefree (PL_scopestack); |
582 | Safefree (PL_scopestack); |
573 | Safefree (PL_savestack); |
583 | Safefree (PL_savestack); |
|
|
584 | #if PERL_VERSION < 9 |
574 | Safefree (PL_retstack); |
585 | Safefree (PL_retstack); |
|
|
586 | #endif |
575 | } |
587 | } |
576 | |
588 | |
577 | static void |
589 | static void |
578 | allocate_stack (Coro__State ctx, int alloc) |
590 | allocate_stack (Coro__State ctx, int alloc) |
579 | { |
591 | { |
… | |
… | |
586 | stack->gencnt = ctx->gencnt = 0; |
598 | stack->gencnt = ctx->gencnt = 0; |
587 | |
599 | |
588 | if (alloc) |
600 | if (alloc) |
589 | { |
601 | { |
590 | #if HAVE_MMAP |
602 | #if HAVE_MMAP |
591 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
603 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
592 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
604 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
593 | if (stack->sptr == (void *)-1) |
605 | if (stack->sptr == (void *)-1) |
594 | #endif |
606 | #endif |
595 | { |
607 | { |
596 | /*FIXME*//*D*//* reasonable stack size! */ |
608 | /*FIXME*//*D*//* reasonable stack size! */ |
597 | stack->ssize = - (8192 * sizeof (long)); |
609 | stack->ssize = - (STACKSIZE * sizeof (long)); |
598 | New (0, stack->sptr, 8192, long); |
610 | New (0, stack->sptr, STACKSIZE, long); |
599 | } |
611 | } |
600 | } |
612 | } |
601 | else |
613 | else |
602 | stack->sptr = 0; |
614 | stack->sptr = 0; |
603 | |
615 | |
… | |
… | |
855 | static int coro_nready; |
867 | static int coro_nready; |
856 | |
868 | |
857 | static void |
869 | static void |
858 | coro_enq (pTHX_ SV *sv) |
870 | coro_enq (pTHX_ SV *sv) |
859 | { |
871 | { |
|
|
872 | SV **xprio; |
|
|
873 | int prio; |
|
|
874 | |
860 | if (SvTYPE (sv) == SVt_PVHV) |
875 | if (SvTYPE (sv) != SVt_PVHV) |
861 | { |
|
|
862 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
863 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
864 | |
|
|
865 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
866 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
867 | : prio; |
|
|
868 | |
|
|
869 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
870 | coro_nready++; |
|
|
871 | |
|
|
872 | return; |
|
|
873 | } |
|
|
874 | |
|
|
875 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
876 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
877 | |
|
|
878 | xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
879 | prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
880 | |
|
|
881 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
882 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
883 | : prio; |
|
|
884 | |
|
|
885 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
886 | coro_nready++; |
876 | } |
887 | } |
877 | |
888 | |
878 | static SV * |
889 | static SV * |
879 | coro_deq (pTHX_ int min_prio) |
890 | coro_deq (pTHX_ int min_prio) |
880 | { |
891 | { |
… | |
… | |
1143 | cede(...) |
1154 | cede(...) |
1144 | PROTOTYPE: |
1155 | PROTOTYPE: |
1145 | CODE: |
1156 | CODE: |
1146 | api_cede (); |
1157 | api_cede (); |
1147 | |
1158 | |
|
|
1159 | # and these are hacks |
|
|
1160 | SV * |
|
|
1161 | _aio_get_state () |
|
|
1162 | CODE: |
|
|
1163 | { |
|
|
1164 | struct { |
|
|
1165 | int errorno; |
|
|
1166 | int laststype; |
|
|
1167 | int laststatval; |
|
|
1168 | Stat_t statcache; |
|
|
1169 | } data; |
|
|
1170 | |
|
|
1171 | data.errorno = errno; |
|
|
1172 | data.laststype = PL_laststype; |
|
|
1173 | data.laststatval = PL_laststatval; |
|
|
1174 | data.statcache = PL_statcache; |
|
|
1175 | |
|
|
1176 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1177 | } |
|
|
1178 | OUTPUT: |
|
|
1179 | RETVAL |
|
|
1180 | |
|
|
1181 | void |
|
|
1182 | _aio_set_state (char *data_) |
|
|
1183 | PROTOTYPE: $ |
|
|
1184 | CODE: |
|
|
1185 | { |
|
|
1186 | struct { |
|
|
1187 | int errorno; |
|
|
1188 | int laststype; |
|
|
1189 | int laststatval; |
|
|
1190 | Stat_t statcache; |
|
|
1191 | } *data = (void *)data_; |
|
|
1192 | |
|
|
1193 | errno = data->errorno; |
|
|
1194 | PL_laststype = data->laststype; |
|
|
1195 | PL_laststatval = data->laststatval; |
|
|
1196 | PL_statcache = data->statcache; |
|
|
1197 | } |