… | |
… | |
4 | #include "perl.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
5 | #include "XSUB.h" |
6 | |
6 | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
|
|
12 | |
|
|
13 | /* the maximum number of idle cctx that will be pooled */ |
|
|
14 | #define MAX_IDLE_CCTX 8 |
|
|
15 | |
|
|
16 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
17 | (PERL_REVISION > (a) \ |
|
|
18 | || (PERL_REVISION == (a) \ |
|
|
19 | && (PERL_VERSION > (b) \ |
|
|
20 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
21 | |
|
|
22 | #if !PERL_VERSION_ATLEAST (5,6,0) |
|
|
23 | # ifndef PL_ppaddr |
|
|
24 | # define PL_ppaddr ppaddr |
|
|
25 | # endif |
|
|
26 | # ifndef call_sv |
|
|
27 | # define call_sv perl_call_sv |
|
|
28 | # endif |
|
|
29 | # ifndef get_sv |
|
|
30 | # define get_sv perl_get_sv |
|
|
31 | # endif |
|
|
32 | # ifndef get_cv |
|
|
33 | # define get_cv perl_get_cv |
|
|
34 | # endif |
|
|
35 | # ifndef IS_PADGV |
|
|
36 | # define IS_PADGV(v) 0 |
|
|
37 | # endif |
|
|
38 | # ifndef IS_PADCONST |
|
|
39 | # define IS_PADCONST(v) 0 |
|
|
40 | # endif |
|
|
41 | #endif |
|
|
42 | |
|
|
43 | /* 5.8.7 */ |
|
|
44 | #ifndef SvRV_set |
|
|
45 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
46 | #endif |
|
|
47 | |
|
|
48 | #include <stdio.h> |
9 | #include <stdio.h> |
49 | #include <errno.h> |
10 | #include <errno.h> |
50 | #include <assert.h> |
11 | #include <assert.h> |
51 | |
|
|
52 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
53 | # undef STACKGUARD |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | #ifndef STACKGUARD |
|
|
57 | # define STACKGUARD 0 |
|
|
58 | #endif |
|
|
59 | |
12 | |
60 | #ifdef HAVE_MMAP |
13 | #ifdef HAVE_MMAP |
61 | # include <unistd.h> |
14 | # include <unistd.h> |
62 | # include <sys/mman.h> |
15 | # include <sys/mman.h> |
63 | # ifndef MAP_ANONYMOUS |
16 | # ifndef MAP_ANONYMOUS |
… | |
… | |
78 | #else |
31 | #else |
79 | # define PAGESIZE 0 |
32 | # define PAGESIZE 0 |
80 | # define BOOT_PAGESIZE (void)0 |
33 | # define BOOT_PAGESIZE (void)0 |
81 | #endif |
34 | #endif |
82 | |
35 | |
|
|
36 | #if USE_VALGRIND |
|
|
37 | # include <valgrind/valgrind.h> |
|
|
38 | #endif |
|
|
39 | |
|
|
40 | /* the maximum number of idle cctx that will be pooled */ |
|
|
41 | #define MAX_IDLE_CCTX 8 |
|
|
42 | |
|
|
43 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
44 | (PERL_REVISION > (a) \ |
|
|
45 | || (PERL_REVISION == (a) \ |
|
|
46 | && (PERL_VERSION > (b) \ |
|
|
47 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
48 | |
|
|
49 | #if !PERL_VERSION_ATLEAST (5,6,0) |
|
|
50 | # ifndef PL_ppaddr |
|
|
51 | # define PL_ppaddr ppaddr |
|
|
52 | # endif |
|
|
53 | # ifndef call_sv |
|
|
54 | # define call_sv perl_call_sv |
|
|
55 | # endif |
|
|
56 | # ifndef get_sv |
|
|
57 | # define get_sv perl_get_sv |
|
|
58 | # endif |
|
|
59 | # ifndef get_cv |
|
|
60 | # define get_cv perl_get_cv |
|
|
61 | # endif |
|
|
62 | # ifndef IS_PADGV |
|
|
63 | # define IS_PADGV(v) 0 |
|
|
64 | # endif |
|
|
65 | # ifndef IS_PADCONST |
|
|
66 | # define IS_PADCONST(v) 0 |
|
|
67 | # endif |
|
|
68 | #endif |
|
|
69 | |
|
|
70 | /* 5.8.7 */ |
|
|
71 | #ifndef SvRV_set |
|
|
72 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
73 | #endif |
|
|
74 | |
|
|
75 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
76 | # undef STACKGUARD |
|
|
77 | #endif |
|
|
78 | |
|
|
79 | #ifndef STACKGUARD |
|
|
80 | # define STACKGUARD 0 |
|
|
81 | #endif |
|
|
82 | |
|
|
83 | /* prefer perl internal functions over our own? */ |
|
|
84 | #ifndef PREFER_PERL_FUNCTIONS |
|
|
85 | # define PREFER_PERL_FUNCTIONS 0 |
|
|
86 | #endif |
|
|
87 | |
83 | /* The next macro should declare a variable stacklevel that contains and approximation |
88 | /* The next macro should declare a variable stacklevel that contains and approximation |
84 | * to the current C stack pointer. Its property is that it changes with each call |
89 | * to the current C stack pointer. Its property is that it changes with each call |
85 | * and should be unique. */ |
90 | * and should be unique. */ |
86 | #define dSTACKLEVEL int stacklevel |
91 | #define dSTACKLEVEL int stacklevel |
87 | #define STACKLEVEL ((void *)&stacklevel) |
92 | #define STACKLEVEL ((void *)&stacklevel) |
88 | |
93 | |
89 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
94 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
90 | |
95 | |
91 | #if __GNUC__ >= 3 |
96 | #if __GNUC__ >= 3 |
92 | # define attribute(x) __attribute__(x) |
97 | # define attribute(x) __attribute__(x) |
|
|
98 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
93 | #else |
99 | #else |
94 | # define attribute(x) |
100 | # define attribute(x) |
|
|
101 | # define BARRIER |
95 | #endif |
102 | #endif |
96 | |
103 | |
97 | #define NOINLINE attribute ((noinline)) |
104 | #define NOINLINE attribute ((noinline)) |
98 | |
105 | |
99 | #include "CoroAPI.h" |
106 | #include "CoroAPI.h" |
… | |
… | |
259 | |
266 | |
260 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
267 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
261 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
268 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
262 | else |
269 | else |
263 | { |
270 | { |
264 | #if 0 |
271 | #if PREFER_PERL_FUNCTIONS |
265 | /* this is probably cleaner, but also slower? */ |
272 | /* this is probably cleaner, but also slower? */ |
266 | CV *cp = Perl_cv_clone (cv); |
273 | CV *cp = Perl_cv_clone (cv); |
267 | CvPADLIST (cv) = CvPADLIST (cp); |
274 | CvPADLIST (cv) = CvPADLIST (cp); |
268 | CvPADLIST (cp) = 0; |
275 | CvPADLIST (cp) = 0; |
269 | SvREFCNT_dec (cp); |
276 | SvREFCNT_dec (cp); |
… | |
… | |
403 | * allocate various perl stacks. This is an exact copy |
410 | * allocate various perl stacks. This is an exact copy |
404 | * of perl.c:init_stacks, except that it uses less memory |
411 | * of perl.c:init_stacks, except that it uses less memory |
405 | * on the (sometimes correct) assumption that coroutines do |
412 | * on the (sometimes correct) assumption that coroutines do |
406 | * not usually need a lot of stackspace. |
413 | * not usually need a lot of stackspace. |
407 | */ |
414 | */ |
408 | #if USE_PERL_INIT_STACKS |
415 | #if PREFER_PERL_FUNCTIONS |
409 | # define coro_init_stacks init_stacks |
416 | # define coro_init_stacks init_stacks |
410 | #else |
417 | #else |
411 | |
|
|
412 | static void |
418 | static void |
413 | coro_init_stacks () |
419 | coro_init_stacks () |
414 | { |
420 | { |
415 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
421 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
416 | PL_curstackinfo->si_type = PERLSI_MAIN; |
422 | PL_curstackinfo->si_type = PERLSI_MAIN; |
… | |
… | |
446 | New(54,PL_retstack,16,OP*); |
452 | New(54,PL_retstack,16,OP*); |
447 | PL_retstack_ix = 0; |
453 | PL_retstack_ix = 0; |
448 | PL_retstack_max = 16; |
454 | PL_retstack_max = 16; |
449 | #endif |
455 | #endif |
450 | } |
456 | } |
|
|
457 | #endif |
451 | |
458 | |
452 | /* |
459 | /* |
453 | * destroy the stacks, the callchain etc... |
460 | * destroy the stacks, the callchain etc... |
454 | */ |
461 | */ |
455 | static void |
462 | static void |
… | |
… | |
489 | Safefree (PL_savestack); |
496 | Safefree (PL_savestack); |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
497 | #if !PERL_VERSION_ATLEAST (5,9,0) |
491 | Safefree (PL_retstack); |
498 | Safefree (PL_retstack); |
492 | #endif |
499 | #endif |
493 | } |
500 | } |
494 | #endif |
|
|
495 | |
501 | |
496 | static void |
502 | static void |
497 | setup_coro (struct coro *coro) |
503 | setup_coro (struct coro *coro) |
498 | { |
504 | { |
499 | /* |
505 | /* |
… | |
… | |
953 | coro = SvSTATE (coro_sv); |
959 | coro = SvSTATE (coro_sv); |
954 | |
960 | |
955 | if (coro->flags & CF_READY) |
961 | if (coro->flags & CF_READY) |
956 | return 0; |
962 | return 0; |
957 | |
963 | |
958 | #if 0 /* this is actually harmless */ |
|
|
959 | if (coro->flags & CF_RUNNING) |
|
|
960 | croak ("Coro::ready called on currently running coroutine"); |
|
|
961 | #endif |
|
|
962 | |
|
|
963 | coro->flags |= CF_READY; |
964 | coro->flags |= CF_READY; |
964 | |
965 | |
965 | LOCK; |
966 | LOCK; |
966 | coro_enq (SvREFCNT_inc (coro_sv)); |
967 | coro_enq (SvREFCNT_inc (coro_sv)); |
967 | UNLOCK; |
968 | UNLOCK; |
… | |
… | |
970 | } |
971 | } |
971 | |
972 | |
972 | static int |
973 | static int |
973 | api_is_ready (SV *coro_sv) |
974 | api_is_ready (SV *coro_sv) |
974 | { |
975 | { |
975 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
976 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
976 | } |
977 | } |
977 | |
978 | |
978 | static void |
979 | static void |
979 | prepare_schedule (struct transfer_args *ta) |
980 | prepare_schedule (struct transfer_args *ta) |
980 | { |
981 | { |
… | |
… | |
1025 | |
1026 | |
1026 | static void |
1027 | static void |
1027 | prepare_cede (struct transfer_args *ta) |
1028 | prepare_cede (struct transfer_args *ta) |
1028 | { |
1029 | { |
1029 | api_ready (coro_current); |
1030 | api_ready (coro_current); |
1030 | |
|
|
1031 | prepare_schedule (ta); |
1031 | prepare_schedule (ta); |
|
|
1032 | } |
|
|
1033 | |
|
|
1034 | static int |
|
|
1035 | prepare_cede_notself (struct transfer_args *ta) |
|
|
1036 | { |
|
|
1037 | if (coro_nready) |
|
|
1038 | { |
|
|
1039 | SV *prev = SvRV (coro_current); |
|
|
1040 | prepare_schedule (ta); |
|
|
1041 | api_ready (prev); |
|
|
1042 | return 1; |
|
|
1043 | } |
|
|
1044 | else |
|
|
1045 | return 0; |
1032 | } |
1046 | } |
1033 | |
1047 | |
1034 | static void |
1048 | static void |
1035 | api_schedule (void) |
1049 | api_schedule (void) |
1036 | { |
1050 | { |
… | |
… | |
1038 | |
1052 | |
1039 | prepare_schedule (&ta); |
1053 | prepare_schedule (&ta); |
1040 | TRANSFER (ta); |
1054 | TRANSFER (ta); |
1041 | } |
1055 | } |
1042 | |
1056 | |
1043 | static void |
1057 | static int |
1044 | api_cede (void) |
1058 | api_cede (void) |
1045 | { |
1059 | { |
1046 | struct transfer_args ta; |
1060 | struct transfer_args ta; |
1047 | |
1061 | |
1048 | prepare_cede (&ta); |
1062 | prepare_cede (&ta); |
|
|
1063 | |
|
|
1064 | if (ta.prev != ta.next) |
|
|
1065 | { |
1049 | TRANSFER (ta); |
1066 | TRANSFER (ta); |
|
|
1067 | return 1; |
|
|
1068 | } |
|
|
1069 | else |
|
|
1070 | return 0; |
|
|
1071 | } |
|
|
1072 | |
|
|
1073 | static int |
|
|
1074 | api_cede_notself (void) |
|
|
1075 | { |
|
|
1076 | struct transfer_args ta; |
|
|
1077 | |
|
|
1078 | if (prepare_cede_notself (&ta)) |
|
|
1079 | { |
|
|
1080 | TRANSFER (ta); |
|
|
1081 | return 1; |
|
|
1082 | } |
|
|
1083 | else |
|
|
1084 | return 0; |
1050 | } |
1085 | } |
1051 | |
1086 | |
1052 | MODULE = Coro::State PACKAGE = Coro::State |
1087 | MODULE = Coro::State PACKAGE = Coro::State |
1053 | |
1088 | |
1054 | PROTOTYPES: DISABLE |
1089 | PROTOTYPES: DISABLE |
… | |
… | |
1110 | _set_stacklevel (...) |
1145 | _set_stacklevel (...) |
1111 | ALIAS: |
1146 | ALIAS: |
1112 | Coro::State::transfer = 1 |
1147 | Coro::State::transfer = 1 |
1113 | Coro::schedule = 2 |
1148 | Coro::schedule = 2 |
1114 | Coro::cede = 3 |
1149 | Coro::cede = 3 |
|
|
1150 | Coro::cede_notself = 4 |
1115 | CODE: |
1151 | CODE: |
1116 | { |
1152 | { |
1117 | struct transfer_args ta; |
1153 | struct transfer_args ta; |
1118 | |
1154 | |
1119 | switch (ix) |
1155 | switch (ix) |
… | |
… | |
1135 | break; |
1171 | break; |
1136 | |
1172 | |
1137 | case 3: |
1173 | case 3: |
1138 | prepare_cede (&ta); |
1174 | prepare_cede (&ta); |
1139 | break; |
1175 | break; |
|
|
1176 | |
|
|
1177 | case 4: |
|
|
1178 | if (!prepare_cede_notself (&ta)) |
|
|
1179 | XSRETURN_EMPTY; |
|
|
1180 | |
|
|
1181 | break; |
1140 | } |
1182 | } |
1141 | |
1183 | |
|
|
1184 | BARRIER; |
1142 | TRANSFER (ta); |
1185 | TRANSFER (ta); |
1143 | } |
1186 | } |
1144 | |
1187 | |
1145 | void |
1188 | void |
1146 | _clone_state_from (SV *dst, SV *src) |
1189 | _clone_state_from (SV *dst, SV *src) |
… | |
… | |
1197 | coro_ready[i] = newAV (); |
1240 | coro_ready[i] = newAV (); |
1198 | |
1241 | |
1199 | { |
1242 | { |
1200 | SV *sv = perl_get_sv("Coro::API", 1); |
1243 | SV *sv = perl_get_sv("Coro::API", 1); |
1201 | |
1244 | |
1202 | coroapi.schedule = api_schedule; |
1245 | coroapi.schedule = api_schedule; |
1203 | coroapi.save = api_save; |
1246 | coroapi.save = api_save; |
1204 | coroapi.cede = api_cede; |
1247 | coroapi.cede = api_cede; |
|
|
1248 | coroapi.cede_notself = api_cede_notself; |
1205 | coroapi.ready = api_ready; |
1249 | coroapi.ready = api_ready; |
1206 | coroapi.is_ready = api_is_ready; |
1250 | coroapi.is_ready = api_is_ready; |
1207 | coroapi.nready = &coro_nready; |
1251 | coroapi.nready = &coro_nready; |
1208 | coroapi.current = coro_current; |
1252 | coroapi.current = coro_current; |
1209 | |
1253 | |
1210 | GCoroAPI = &coroapi; |
1254 | GCoroAPI = &coroapi; |
1211 | sv_setiv (sv, (IV)&coroapi); |
1255 | sv_setiv (sv, (IV)&coroapi); |
1212 | SvREADONLY_on (sv); |
1256 | SvREADONLY_on (sv); |
1213 | } |
1257 | } |
… | |
… | |
1229 | RETVAL = coro->prio; |
1273 | RETVAL = coro->prio; |
1230 | |
1274 | |
1231 | if (items > 1) |
1275 | if (items > 1) |
1232 | { |
1276 | { |
1233 | if (ix) |
1277 | if (ix) |
1234 | newprio += coro->prio; |
1278 | newprio = coro->prio - newprio; |
1235 | |
1279 | |
1236 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1280 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1237 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1281 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1238 | |
1282 | |
1239 | coro->prio = newprio; |
1283 | coro->prio = newprio; |
1240 | } |
1284 | } |
1241 | } |
1285 | } |
|
|
1286 | OUTPUT: |
|
|
1287 | RETVAL |
1242 | |
1288 | |
1243 | SV * |
1289 | SV * |
1244 | ready (SV *self) |
1290 | ready (SV *self) |
1245 | PROTOTYPE: $ |
1291 | PROTOTYPE: $ |
1246 | CODE: |
1292 | CODE: |