|
|
1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
|
|
4 | |
1 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
2 | #include "perl.h" |
6 | #include "perl.h" |
3 | #include "XSUB.h" |
7 | #include "XSUB.h" |
4 | |
8 | |
5 | #include "patchlevel.h" |
9 | #include "patchlevel.h" |
6 | |
10 | |
7 | #if PATCHLEVEL < 6 |
11 | #if PERL_VERSION < 6 |
8 | # ifndef PL_ppaddr |
12 | # ifndef PL_ppaddr |
9 | # define PL_ppaddr ppaddr |
13 | # define PL_ppaddr ppaddr |
10 | # endif |
14 | # endif |
11 | # ifndef call_sv |
15 | # ifndef call_sv |
12 | # define call_sv perl_call_sv |
16 | # define call_sv perl_call_sv |
… | |
… | |
23 | # ifndef IS_PADCONST |
27 | # ifndef IS_PADCONST |
24 | # define IS_PADCONST(v) 0 |
28 | # define IS_PADCONST(v) 0 |
25 | # endif |
29 | # endif |
26 | #endif |
30 | #endif |
27 | |
31 | |
28 | #include "libcoro/coro.c" |
|
|
29 | |
|
|
30 | #include <signal.h> |
32 | #include <errno.h> |
|
|
33 | |
|
|
34 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
35 | # undef STACKGUARD |
|
|
36 | #endif |
|
|
37 | |
|
|
38 | #ifndef STACKGUARD |
|
|
39 | # define STACKGUARD 0 |
|
|
40 | #endif |
31 | |
41 | |
32 | #ifdef HAVE_MMAP |
42 | #ifdef HAVE_MMAP |
33 | # include <unistd.h> |
43 | # include <unistd.h> |
34 | # include <sys/mman.h> |
44 | # include <sys/mman.h> |
35 | # ifndef MAP_ANONYMOUS |
45 | # ifndef MAP_ANONYMOUS |
… | |
… | |
37 | # define MAP_ANONYMOUS MAP_ANON |
47 | # define MAP_ANONYMOUS MAP_ANON |
38 | # else |
48 | # else |
39 | # undef HAVE_MMAP |
49 | # undef HAVE_MMAP |
40 | # endif |
50 | # endif |
41 | # endif |
51 | # endif |
|
|
52 | # include <limits.h> |
|
|
53 | # ifndef PAGESIZE |
|
|
54 | # define PAGESIZE pagesize |
|
|
55 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
|
|
56 | static long pagesize; |
|
|
57 | # else |
|
|
58 | # define BOOT_PAGESIZE |
|
|
59 | # endif |
42 | #endif |
60 | #endif |
43 | |
61 | |
44 | #define SUB_INIT "Coro::State::initialize" |
62 | #define SUB_INIT "Coro::State::initialize" |
45 | #define UCORO_STATE "_coro_state" |
63 | #define UCORO_STATE "_coro_state" |
46 | |
64 | |
… | |
… | |
55 | |
73 | |
56 | #include "CoroAPI.h" |
74 | #include "CoroAPI.h" |
57 | |
75 | |
58 | #ifdef USE_ITHREADS |
76 | #ifdef USE_ITHREADS |
59 | static perl_mutex coro_mutex; |
77 | static perl_mutex coro_mutex; |
60 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
78 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
61 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
79 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
62 | #else |
80 | #else |
63 | # define LOCK 0 |
81 | # define LOCK (void)0 |
64 | # define UNLOCK 0 |
82 | # define UNLOCK (void)0 |
65 | #endif |
83 | #endif |
66 | |
84 | |
67 | static struct CoroAPI coroapi; |
85 | static struct CoroAPI coroapi; |
|
|
86 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
87 | static HV *coro_state_stash; |
|
|
88 | static SV *ucoro_state_sv; |
|
|
89 | static U32 ucoro_state_hash; |
|
|
90 | static SV *coro_mortal; /* will be freed after next transfer */ |
68 | |
91 | |
69 | /* this is actually not only the c stack but also c registers etc... */ |
92 | /* this is actually not only the c stack but also c registers etc... */ |
70 | typedef struct { |
93 | typedef struct { |
71 | int refcnt; /* pointer reference counter */ |
94 | int refcnt; /* pointer reference counter */ |
72 | int usecnt; /* shared by how many coroutines */ |
95 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
102 | AV *mainstack; |
125 | AV *mainstack; |
103 | SV **stack_sp; |
126 | SV **stack_sp; |
104 | OP *op; |
127 | OP *op; |
105 | SV **curpad; |
128 | SV **curpad; |
106 | AV *comppad; |
129 | AV *comppad; |
|
|
130 | CV *compcv; |
107 | SV **stack_base; |
131 | SV **stack_base; |
108 | SV **stack_max; |
132 | SV **stack_max; |
109 | SV **tmps_stack; |
133 | SV **tmps_stack; |
110 | I32 tmps_floor; |
134 | I32 tmps_floor; |
111 | I32 tmps_ix; |
135 | I32 tmps_ix; |
… | |
… | |
120 | I32 savestack_ix; |
144 | I32 savestack_ix; |
121 | I32 savestack_max; |
145 | I32 savestack_max; |
122 | OP **retstack; |
146 | OP **retstack; |
123 | I32 retstack_ix; |
147 | I32 retstack_ix; |
124 | I32 retstack_max; |
148 | I32 retstack_max; |
|
|
149 | PMOP *curpm; |
125 | COP *curcop; |
150 | COP *curcop; |
126 | JMPENV *top_env; |
151 | JMPENV *top_env; |
127 | |
152 | |
128 | /* data associated with this coroutine (initial args) */ |
153 | /* data associated with this coroutine (initial args) */ |
129 | AV *args; |
154 | AV *args; |
130 | }; |
155 | }; |
131 | |
156 | |
132 | typedef struct coro *Coro__State; |
157 | typedef struct coro *Coro__State; |
133 | typedef struct coro *Coro__State_or_hashref; |
158 | typedef struct coro *Coro__State_or_hashref; |
134 | |
159 | |
135 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
160 | static AV * |
136 | static HV *coro_state_stash; |
161 | coro_clone_padlist (pTHX_ CV *cv) |
137 | static SV *ucoro_state_sv; |
|
|
138 | static U32 ucoro_state_hash; |
|
|
139 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
140 | |
|
|
141 | /* mostly copied from op.c:cv_clone2 */ |
|
|
142 | STATIC AV * |
|
|
143 | clone_padlist (AV *protopadlist) |
|
|
144 | { |
162 | { |
145 | AV *av; |
163 | AV *padlist = CvPADLIST (cv); |
146 | I32 ix; |
|
|
147 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
|
|
148 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
|
|
149 | SV **pname = AvARRAY (protopad_name); |
|
|
150 | SV **ppad = AvARRAY (protopad); |
|
|
151 | I32 fname = AvFILLp (protopad_name); |
|
|
152 | I32 fpad = AvFILLp (protopad); |
|
|
153 | AV *newpadlist, *newpad_name, *newpad; |
164 | AV *newpadlist, *newpad; |
154 | SV **npad; |
|
|
155 | |
|
|
156 | newpad_name = newAV (); |
|
|
157 | for (ix = fname; ix >= 0; ix--) |
|
|
158 | av_store (newpad_name, ix, SvREFCNT_inc (pname[ix])); |
|
|
159 | |
|
|
160 | newpad = newAV (); |
|
|
161 | av_fill (newpad, AvFILLp (protopad)); |
|
|
162 | npad = AvARRAY (newpad); |
|
|
163 | |
165 | |
164 | newpadlist = newAV (); |
166 | newpadlist = newAV (); |
165 | AvREAL_off (newpadlist); |
167 | AvREAL_off (newpadlist); |
166 | av_store (newpadlist, 0, (SV *) newpad_name); |
168 | #if PERL_VERSION < 9 |
|
|
169 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
|
|
170 | #else |
|
|
171 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
172 | #endif |
|
|
173 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
|
|
174 | --AvFILLp (padlist); |
|
|
175 | |
|
|
176 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
167 | av_store (newpadlist, 1, (SV *) newpad); |
177 | av_store (newpadlist, 1, (SV *)newpad); |
168 | |
|
|
169 | av = newAV (); /* will be @_ */ |
|
|
170 | av_extend (av, 0); |
|
|
171 | av_store (newpad, 0, (SV *) av); |
|
|
172 | AvFLAGS (av) = AVf_REIFY; |
|
|
173 | |
|
|
174 | for (ix = fpad; ix > 0; ix--) |
|
|
175 | { |
|
|
176 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
177 | |
|
|
178 | if (namesv && namesv != &PL_sv_undef) |
|
|
179 | { |
|
|
180 | char *name = SvPVX (namesv); /* XXX */ |
|
|
181 | |
|
|
182 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
|
|
183 | { /* lexical from outside? */ |
|
|
184 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
|
|
185 | } |
|
|
186 | else |
|
|
187 | { /* our own lexical */ |
|
|
188 | SV *sv; |
|
|
189 | if (*name == '&') |
|
|
190 | sv = SvREFCNT_inc (ppad[ix]); |
|
|
191 | else if (*name == '@') |
|
|
192 | sv = (SV *) newAV (); |
|
|
193 | else if (*name == '%') |
|
|
194 | sv = (SV *) newHV (); |
|
|
195 | else |
|
|
196 | sv = NEWSV (0, 0); |
|
|
197 | |
|
|
198 | #ifdef SvPADBUSY |
|
|
199 | if (!SvPADBUSY (sv)) |
|
|
200 | #endif |
|
|
201 | SvPADMY_on (sv); |
|
|
202 | |
|
|
203 | npad[ix] = sv; |
|
|
204 | } |
|
|
205 | } |
|
|
206 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
|
|
207 | { |
|
|
208 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
|
|
209 | } |
|
|
210 | else |
|
|
211 | { |
|
|
212 | SV *sv = NEWSV (0, 0); |
|
|
213 | SvPADTMP_on (sv); |
|
|
214 | npad[ix] = sv; |
|
|
215 | } |
|
|
216 | } |
|
|
217 | |
|
|
218 | #if 0 /* return -ENOTUNDERSTOOD */ |
|
|
219 | /* Now that vars are all in place, clone nested closures. */ |
|
|
220 | |
|
|
221 | for (ix = fpad; ix > 0; ix--) { |
|
|
222 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
223 | if (namesv |
|
|
224 | && namesv != &PL_sv_undef |
|
|
225 | && !(SvFLAGS(namesv) & SVf_FAKE) |
|
|
226 | && *SvPVX(namesv) == '&' |
|
|
227 | && CvCLONE(ppad[ix])) |
|
|
228 | { |
|
|
229 | CV *kid = cv_clone((CV*)ppad[ix]); |
|
|
230 | SvREFCNT_dec(ppad[ix]); |
|
|
231 | CvCLONE_on(kid); |
|
|
232 | SvPADMY_on(kid); |
|
|
233 | npad[ix] = (SV*)kid; |
|
|
234 | } |
|
|
235 | } |
|
|
236 | #endif |
|
|
237 | |
178 | |
238 | return newpadlist; |
179 | return newpadlist; |
239 | } |
180 | } |
240 | |
181 | |
241 | STATIC void |
182 | static void |
242 | free_padlist (AV *padlist) |
183 | free_padlist (pTHX_ AV *padlist) |
243 | { |
184 | { |
244 | /* may be during global destruction */ |
185 | /* may be during global destruction */ |
245 | if (SvREFCNT (padlist)) |
186 | if (SvREFCNT (padlist)) |
246 | { |
187 | { |
247 | I32 i = AvFILLp (padlist); |
188 | I32 i = AvFILLp (padlist); |
… | |
… | |
260 | |
201 | |
261 | SvREFCNT_dec ((SV*)padlist); |
202 | SvREFCNT_dec ((SV*)padlist); |
262 | } |
203 | } |
263 | } |
204 | } |
264 | |
205 | |
265 | STATIC int |
206 | static int |
266 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
207 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
267 | { |
208 | { |
268 | AV *padlist; |
209 | AV *padlist; |
269 | AV *av = (AV *)mg->mg_obj; |
210 | AV *av = (AV *)mg->mg_obj; |
270 | |
211 | |
271 | /* casting is fun. */ |
212 | /* casting is fun. */ |
272 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
213 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
273 | free_padlist (padlist); |
214 | free_padlist (aTHX_ padlist); |
|
|
215 | |
|
|
216 | SvREFCNT_dec (av); |
|
|
217 | |
|
|
218 | return 0; |
274 | } |
219 | } |
275 | |
220 | |
276 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
221 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
277 | |
222 | |
278 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
223 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
279 | |
224 | |
280 | /* the next two functions merely cache the padlists */ |
225 | /* the next two functions merely cache the padlists */ |
281 | STATIC void |
226 | static void |
282 | get_padlist (CV *cv) |
227 | get_padlist (pTHX_ CV *cv) |
283 | { |
228 | { |
284 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
229 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
285 | |
230 | |
286 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
231 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
287 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
232 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
288 | else |
233 | else |
|
|
234 | { |
|
|
235 | #if 0 |
|
|
236 | /* this should work - but it doesn't :( */ |
|
|
237 | CV *cp = Perl_cv_clone (aTHX_ cv); |
|
|
238 | CvPADLIST (cv) = CvPADLIST (cp); |
|
|
239 | CvPADLIST (cp) = 0; |
|
|
240 | SvREFCNT_dec (cp); |
|
|
241 | #else |
289 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
242 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
|
|
243 | #endif |
|
|
244 | } |
290 | } |
245 | } |
291 | |
246 | |
292 | STATIC void |
247 | static void |
293 | put_padlist (CV *cv) |
248 | put_padlist (pTHX_ CV *cv) |
294 | { |
249 | { |
295 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
250 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
296 | |
251 | |
297 | if (!mg) |
252 | if (!mg) |
298 | { |
253 | { |
… | |
… | |
324 | PL_mainstack = c->mainstack; |
279 | PL_mainstack = c->mainstack; |
325 | PL_stack_sp = c->stack_sp; |
280 | PL_stack_sp = c->stack_sp; |
326 | PL_op = c->op; |
281 | PL_op = c->op; |
327 | PL_curpad = c->curpad; |
282 | PL_curpad = c->curpad; |
328 | PL_comppad = c->comppad; |
283 | PL_comppad = c->comppad; |
|
|
284 | PL_compcv = c->compcv; |
329 | PL_stack_base = c->stack_base; |
285 | PL_stack_base = c->stack_base; |
330 | PL_stack_max = c->stack_max; |
286 | PL_stack_max = c->stack_max; |
331 | PL_tmps_stack = c->tmps_stack; |
287 | PL_tmps_stack = c->tmps_stack; |
332 | PL_tmps_floor = c->tmps_floor; |
288 | PL_tmps_floor = c->tmps_floor; |
333 | PL_tmps_ix = c->tmps_ix; |
289 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
339 | PL_scopestack_ix = c->scopestack_ix; |
295 | PL_scopestack_ix = c->scopestack_ix; |
340 | PL_scopestack_max = c->scopestack_max; |
296 | PL_scopestack_max = c->scopestack_max; |
341 | PL_savestack = c->savestack; |
297 | PL_savestack = c->savestack; |
342 | PL_savestack_ix = c->savestack_ix; |
298 | PL_savestack_ix = c->savestack_ix; |
343 | PL_savestack_max = c->savestack_max; |
299 | PL_savestack_max = c->savestack_max; |
|
|
300 | #if PERL_VERSION < 9 |
344 | PL_retstack = c->retstack; |
301 | PL_retstack = c->retstack; |
345 | PL_retstack_ix = c->retstack_ix; |
302 | PL_retstack_ix = c->retstack_ix; |
346 | PL_retstack_max = c->retstack_max; |
303 | PL_retstack_max = c->retstack_max; |
|
|
304 | #endif |
|
|
305 | PL_curpm = c->curpm; |
347 | PL_curcop = c->curcop; |
306 | PL_curcop = c->curcop; |
348 | PL_top_env = c->top_env; |
307 | PL_top_env = c->top_env; |
349 | |
308 | |
350 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
309 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
351 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
310 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
… | |
… | |
360 | { |
319 | { |
361 | AV *padlist = (AV *)POPs; |
320 | AV *padlist = (AV *)POPs; |
362 | |
321 | |
363 | if (padlist) |
322 | if (padlist) |
364 | { |
323 | { |
365 | put_padlist (cv); /* mark this padlist as available */ |
324 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
366 | CvPADLIST(cv) = padlist; |
325 | CvPADLIST(cv) = padlist; |
367 | } |
326 | } |
368 | |
327 | |
369 | ++CvDEPTH(cv); |
328 | ++CvDEPTH(cv); |
370 | } |
329 | } |
… | |
… | |
410 | } |
369 | } |
411 | |
370 | |
412 | PUSHs ((SV *)CvPADLIST(cv)); |
371 | PUSHs ((SV *)CvPADLIST(cv)); |
413 | PUSHs ((SV *)cv); |
372 | PUSHs ((SV *)cv); |
414 | |
373 | |
415 | get_padlist (cv); /* this is a monster */ |
374 | get_padlist (aTHX_ cv); |
416 | } |
375 | } |
417 | } |
376 | } |
418 | #ifdef CXt_FORMAT |
377 | #ifdef CXt_FORMAT |
419 | else if (CxTYPE(cx) == CXt_FORMAT) |
378 | else if (CxTYPE(cx) == CXt_FORMAT) |
420 | { |
379 | { |
… | |
… | |
448 | c->mainstack = PL_mainstack; |
407 | c->mainstack = PL_mainstack; |
449 | c->stack_sp = PL_stack_sp; |
408 | c->stack_sp = PL_stack_sp; |
450 | c->op = PL_op; |
409 | c->op = PL_op; |
451 | c->curpad = PL_curpad; |
410 | c->curpad = PL_curpad; |
452 | c->comppad = PL_comppad; |
411 | c->comppad = PL_comppad; |
|
|
412 | c->compcv = PL_compcv; |
453 | c->stack_base = PL_stack_base; |
413 | c->stack_base = PL_stack_base; |
454 | c->stack_max = PL_stack_max; |
414 | c->stack_max = PL_stack_max; |
455 | c->tmps_stack = PL_tmps_stack; |
415 | c->tmps_stack = PL_tmps_stack; |
456 | c->tmps_floor = PL_tmps_floor; |
416 | c->tmps_floor = PL_tmps_floor; |
457 | c->tmps_ix = PL_tmps_ix; |
417 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
463 | c->scopestack_ix = PL_scopestack_ix; |
423 | c->scopestack_ix = PL_scopestack_ix; |
464 | c->scopestack_max = PL_scopestack_max; |
424 | c->scopestack_max = PL_scopestack_max; |
465 | c->savestack = PL_savestack; |
425 | c->savestack = PL_savestack; |
466 | c->savestack_ix = PL_savestack_ix; |
426 | c->savestack_ix = PL_savestack_ix; |
467 | c->savestack_max = PL_savestack_max; |
427 | c->savestack_max = PL_savestack_max; |
|
|
428 | #if PERL_VERSION < 9 |
468 | c->retstack = PL_retstack; |
429 | c->retstack = PL_retstack; |
469 | c->retstack_ix = PL_retstack_ix; |
430 | c->retstack_ix = PL_retstack_ix; |
470 | c->retstack_max = PL_retstack_max; |
431 | c->retstack_max = PL_retstack_max; |
|
|
432 | #endif |
|
|
433 | c->curpm = PL_curpm; |
471 | c->curcop = PL_curcop; |
434 | c->curcop = PL_curcop; |
472 | c->top_env = PL_top_env; |
435 | c->top_env = PL_top_env; |
473 | } |
436 | } |
474 | |
437 | |
475 | /* |
438 | /* |
476 | * allocate various perl stacks. This is an exact copy |
439 | * allocate various perl stacks. This is an exact copy |
477 | * of perl.c:init_stacks, except that it uses less memory |
440 | * of perl.c:init_stacks, except that it uses less memory |
478 | * on the (sometimes correct) assumption that coroutines do |
441 | * on the (sometimes correct) assumption that coroutines do |
479 | * not usually need a lot of stackspace. |
442 | * not usually need a lot of stackspace. |
480 | */ |
443 | */ |
481 | STATIC void |
444 | static void |
482 | coro_init_stacks (pTHX) |
445 | coro_init_stacks (pTHX) |
483 | { |
446 | { |
484 | LOCK; |
447 | LOCK; |
485 | |
448 | |
486 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
449 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
… | |
… | |
511 | |
474 | |
512 | New(54,PL_savestack,96,ANY); |
475 | New(54,PL_savestack,96,ANY); |
513 | PL_savestack_ix = 0; |
476 | PL_savestack_ix = 0; |
514 | PL_savestack_max = 96; |
477 | PL_savestack_max = 96; |
515 | |
478 | |
|
|
479 | #if PERL_VERSION < 9 |
516 | New(54,PL_retstack,8,OP*); |
480 | New(54,PL_retstack,8,OP*); |
517 | PL_retstack_ix = 0; |
481 | PL_retstack_ix = 0; |
518 | PL_retstack_max = 8; |
482 | PL_retstack_max = 8; |
|
|
483 | #endif |
519 | |
484 | |
520 | UNLOCK; |
485 | UNLOCK; |
521 | } |
486 | } |
522 | |
487 | |
523 | /* |
488 | /* |
524 | * destroy the stacks, the callchain etc... |
489 | * destroy the stacks, the callchain etc... |
525 | */ |
490 | */ |
526 | STATIC void |
491 | static void |
527 | destroy_stacks(pTHX) |
492 | destroy_stacks(pTHX) |
528 | { |
493 | { |
529 | if (!IN_DESTRUCT) |
494 | if (!IN_DESTRUCT) |
530 | { |
495 | { |
531 | /* is this ugly, I ask? */ |
496 | /* is this ugly, I ask? */ |
532 | LEAVE_SCOPE (0); |
497 | LEAVE_SCOPE (0); |
533 | |
498 | |
534 | /* sure it is, but more important: is it correct?? :/ */ |
499 | /* sure it is, but more important: is it correct?? :/ */ |
535 | FREETMPS; |
500 | FREETMPS; |
|
|
501 | |
|
|
502 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
536 | } |
503 | } |
537 | |
504 | |
538 | while (PL_curstackinfo->si_next) |
505 | while (PL_curstackinfo->si_next) |
539 | PL_curstackinfo = PL_curstackinfo->si_next; |
506 | PL_curstackinfo = PL_curstackinfo->si_next; |
540 | |
507 | |
541 | while (PL_curstackinfo) |
508 | while (PL_curstackinfo) |
542 | { |
509 | { |
543 | PERL_SI *p = PL_curstackinfo->si_prev; |
510 | PERL_SI *p = PL_curstackinfo->si_prev; |
544 | |
511 | |
545 | { |
512 | { /*D*//*remove*/ |
546 | dSP; |
513 | dSP; |
547 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
514 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
548 | PUTBACK; /* possibly superfluous */ |
515 | PUTBACK; /* possibly superfluous */ |
549 | } |
516 | } |
550 | |
517 | |
551 | if (!IN_DESTRUCT) |
518 | if (!IN_DESTRUCT) |
552 | { |
519 | { |
553 | dounwind(-1); |
520 | dounwind (-1);/*D*//*remove*/ |
554 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
521 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
555 | } |
522 | } |
556 | |
523 | |
557 | Safefree(PL_curstackinfo->si_cxstack); |
524 | Safefree (PL_curstackinfo->si_cxstack); |
558 | Safefree(PL_curstackinfo); |
525 | Safefree (PL_curstackinfo); |
559 | PL_curstackinfo = p; |
526 | PL_curstackinfo = p; |
560 | } |
527 | } |
561 | |
528 | |
562 | Safefree(PL_tmps_stack); |
529 | Safefree (PL_tmps_stack); |
563 | Safefree(PL_markstack); |
530 | Safefree (PL_markstack); |
564 | Safefree(PL_scopestack); |
531 | Safefree (PL_scopestack); |
565 | Safefree(PL_savestack); |
532 | Safefree (PL_savestack); |
|
|
533 | #if PERL_VERSION < 9 |
566 | Safefree(PL_retstack); |
534 | Safefree (PL_retstack); |
|
|
535 | #endif |
567 | } |
536 | } |
568 | |
537 | |
569 | static void |
538 | static void |
570 | allocate_stack (Coro__State ctx, int alloc) |
539 | allocate_stack (Coro__State ctx, int alloc) |
571 | { |
540 | { |
… | |
… | |
578 | stack->gencnt = ctx->gencnt = 0; |
547 | stack->gencnt = ctx->gencnt = 0; |
579 | |
548 | |
580 | if (alloc) |
549 | if (alloc) |
581 | { |
550 | { |
582 | #if HAVE_MMAP |
551 | #if HAVE_MMAP |
583 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
552 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
584 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
553 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
585 | if (stack->sptr == (void *)-1) |
554 | if (stack->sptr != (void *)-1) |
586 | #endif |
|
|
587 | { |
555 | { |
588 | /*FIXME*//*D*//* reasonable stack size! */ |
556 | # if STACKGUARD |
|
|
557 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
558 | # endif |
|
|
559 | } |
|
|
560 | else |
|
|
561 | #endif |
|
|
562 | { |
589 | stack->ssize = - (8192 * sizeof (long)); |
563 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
590 | New (0, stack->sptr, 8192, long); |
564 | New (0, stack->sptr, STACKSIZE, long); |
591 | } |
565 | } |
592 | } |
566 | } |
593 | else |
567 | else |
594 | stack->sptr = 0; |
568 | stack->sptr = 0; |
595 | |
569 | |
… | |
… | |
625 | setup_coro (void *arg) |
599 | setup_coro (void *arg) |
626 | { |
600 | { |
627 | /* |
601 | /* |
628 | * emulate part of the perl startup here. |
602 | * emulate part of the perl startup here. |
629 | */ |
603 | */ |
|
|
604 | dTHX; |
630 | dSP; |
605 | dSP; |
631 | Coro__State ctx = (Coro__State)arg; |
606 | Coro__State ctx = (Coro__State)arg; |
632 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
607 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
633 | |
608 | |
634 | coro_init_stacks (aTHX); |
609 | coro_init_stacks (aTHX); |
… | |
… | |
683 | continue_coro (void *arg) |
658 | continue_coro (void *arg) |
684 | { |
659 | { |
685 | /* |
660 | /* |
686 | * this is a _very_ stripped down perl interpreter ;) |
661 | * this is a _very_ stripped down perl interpreter ;) |
687 | */ |
662 | */ |
|
|
663 | dTHX; |
688 | Coro__State ctx = (Coro__State)arg; |
664 | Coro__State ctx = (Coro__State)arg; |
689 | JMPENV coro_start_env; |
|
|
690 | |
665 | |
691 | PL_top_env = &ctx->start_env; |
666 | PL_top_env = &ctx->start_env; |
692 | |
667 | |
693 | ctx->cursp = 0; |
668 | ctx->cursp = 0; |
694 | PL_op = PL_op->op_next; |
669 | PL_op = PL_op->op_next; |
695 | CALLRUNOPS(aTHX); |
670 | CALLRUNOPS(aTHX); |
696 | |
671 | |
697 | abort (); |
672 | abort (); |
698 | } |
673 | } |
699 | |
674 | |
700 | STATIC void |
675 | static void |
701 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
676 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
702 | { |
677 | { |
703 | dSTACKLEVEL; |
678 | dSTACKLEVEL; |
704 | |
679 | |
705 | if (prev != next) |
680 | if (prev != next) |
… | |
… | |
817 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
792 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
818 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
793 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
819 | \ |
794 | \ |
820 | } while(0) |
795 | } while(0) |
821 | |
796 | |
822 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
797 | #define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) |
823 | |
798 | |
824 | static void |
799 | static void |
825 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
800 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
826 | { |
801 | { |
827 | SV_CORO (prev, "Coro::transfer"); |
802 | SV_CORO (prev, "Coro::transfer"); |
828 | SV_CORO (next, "Coro::transfer"); |
803 | SV_CORO (next, "Coro::transfer"); |
829 | |
804 | |
830 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
805 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
831 | } |
806 | } |
832 | |
807 | |
833 | /** Coro ********************************************************************/ |
808 | /** Coro ********************************************************************/ |
834 | |
809 | |
835 | #define PRIO_MAX 3 |
810 | #define PRIO_MAX 3 |
… | |
… | |
843 | static GV *coro_current, *coro_idle; |
818 | static GV *coro_current, *coro_idle; |
844 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
819 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
845 | static int coro_nready; |
820 | static int coro_nready; |
846 | |
821 | |
847 | static void |
822 | static void |
848 | coro_enq (SV *sv) |
823 | coro_enq (pTHX_ SV *sv) |
849 | { |
824 | { |
|
|
825 | SV **xprio; |
|
|
826 | int prio; |
|
|
827 | |
850 | if (SvTYPE (sv) == SVt_PVHV) |
828 | if (SvTYPE (sv) != SVt_PVHV) |
851 | { |
|
|
852 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
853 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
854 | |
|
|
855 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
856 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
857 | : prio; |
|
|
858 | |
|
|
859 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
860 | coro_nready++; |
|
|
861 | |
|
|
862 | return; |
|
|
863 | } |
|
|
864 | |
|
|
865 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
829 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
830 | |
|
|
831 | xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
832 | prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
833 | |
|
|
834 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
835 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
836 | : prio; |
|
|
837 | |
|
|
838 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
839 | coro_nready++; |
866 | } |
840 | } |
867 | |
841 | |
868 | static SV * |
842 | static SV * |
869 | coro_deq (int min_prio) |
843 | coro_deq (pTHX_ int min_prio) |
870 | { |
844 | { |
871 | int prio = PRIO_MAX - PRIO_MIN; |
845 | int prio = PRIO_MAX - PRIO_MIN; |
872 | |
846 | |
873 | min_prio -= PRIO_MIN; |
847 | min_prio -= PRIO_MIN; |
874 | if (min_prio < 0) |
848 | if (min_prio < 0) |
… | |
… | |
885 | } |
859 | } |
886 | |
860 | |
887 | static void |
861 | static void |
888 | api_ready (SV *coro) |
862 | api_ready (SV *coro) |
889 | { |
863 | { |
|
|
864 | dTHX; |
|
|
865 | |
890 | if (SvROK (coro)) |
866 | if (SvROK (coro)) |
891 | coro = SvRV (coro); |
867 | coro = SvRV (coro); |
892 | |
868 | |
893 | LOCK; |
869 | LOCK; |
894 | coro_enq (SvREFCNT_inc (coro)); |
870 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
895 | UNLOCK; |
871 | UNLOCK; |
896 | } |
872 | } |
897 | |
873 | |
898 | static void |
874 | static void |
899 | api_schedule (void) |
875 | api_schedule (void) |
900 | { |
876 | { |
|
|
877 | dTHX; |
|
|
878 | |
901 | SV *prev, *next; |
879 | SV *prev, *next; |
902 | |
880 | |
903 | LOCK; |
881 | LOCK; |
904 | |
882 | |
905 | prev = SvRV (GvSV (coro_current)); |
883 | prev = SvRV (GvSV (coro_current)); |
906 | next = coro_deq (PRIO_MIN); |
884 | next = coro_deq (aTHX_ PRIO_MIN); |
907 | |
885 | |
908 | if (!next) |
886 | if (!next) |
909 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
887 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
910 | |
888 | |
911 | /* free this only after the transfer */ |
889 | /* free this only after the transfer */ |
… | |
… | |
923 | } |
901 | } |
924 | |
902 | |
925 | static void |
903 | static void |
926 | api_cede (void) |
904 | api_cede (void) |
927 | { |
905 | { |
|
|
906 | dTHX; |
|
|
907 | |
928 | LOCK; |
908 | LOCK; |
929 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
909 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
930 | UNLOCK; |
910 | UNLOCK; |
931 | |
911 | |
932 | api_schedule (); |
912 | api_schedule (); |
933 | } |
913 | } |
934 | |
914 | |
… | |
… | |
939 | BOOT: |
919 | BOOT: |
940 | { /* {} necessary for stoopid perl-5.6.x */ |
920 | { /* {} necessary for stoopid perl-5.6.x */ |
941 | #ifdef USE_ITHREADS |
921 | #ifdef USE_ITHREADS |
942 | MUTEX_INIT (&coro_mutex); |
922 | MUTEX_INIT (&coro_mutex); |
943 | #endif |
923 | #endif |
|
|
924 | BOOT_PAGESIZE; |
944 | |
925 | |
945 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
926 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
946 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
927 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
947 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
928 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
948 | |
929 | |
… | |
… | |
968 | croak ("Coro::State::_newprocess expects an arrayref"); |
949 | croak ("Coro::State::_newprocess expects an arrayref"); |
969 | |
950 | |
970 | Newz (0, coro, 1, struct coro); |
951 | Newz (0, coro, 1, struct coro); |
971 | |
952 | |
972 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
953 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
973 | coro->mainstack = 0; /* actual work is done inside transfer */ |
954 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
974 | coro->stack = 0; |
955 | /*coro->stack = 0;*/ |
975 | |
956 | |
976 | /* same as JMPENV_BOOTSTRAP */ |
957 | /* same as JMPENV_BOOTSTRAP */ |
977 | /* we might be able to recycle start_env, but safe is safe */ |
958 | /* we might be able to recycle start_env, but safe is safe */ |
978 | //Zero(&coro->start_env, 1, JMPENV); |
959 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
979 | coro->start_env.je_ret = -1; |
960 | coro->start_env.je_ret = -1; |
980 | coro->start_env.je_mustcatch = TRUE; |
961 | coro->start_env.je_mustcatch = TRUE; |
981 | |
962 | |
982 | RETVAL = coro; |
963 | RETVAL = coro; |
983 | OUTPUT: |
964 | OUTPUT: |
… | |
… | |
1004 | if (coro->mainstack && coro->mainstack != main_mainstack) |
985 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1005 | { |
986 | { |
1006 | struct coro temp; |
987 | struct coro temp; |
1007 | |
988 | |
1008 | PUTBACK; |
989 | PUTBACK; |
1009 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
990 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1010 | LOAD(aTHX_ coro); |
991 | LOAD (aTHX_ coro); |
1011 | SPAGAIN; |
992 | SPAGAIN; |
1012 | |
993 | |
1013 | destroy_stacks (aTHX); |
994 | destroy_stacks (aTHX); |
1014 | |
995 | |
1015 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
996 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
1016 | SPAGAIN; |
997 | SPAGAIN; |
1017 | |
998 | |
1018 | coro->mainstack = 0; |
999 | coro->mainstack = 0; |
1019 | } |
1000 | } |
1020 | |
1001 | |
… | |
… | |
1035 | |
1016 | |
1036 | void |
1017 | void |
1037 | yield(...) |
1018 | yield(...) |
1038 | PROTOTYPE: @ |
1019 | PROTOTYPE: @ |
1039 | CODE: |
1020 | CODE: |
1040 | static SV *returnstk; |
1021 | SV *yieldstack; |
1041 | SV *sv; |
1022 | SV *sv; |
1042 | AV *defav = GvAV (PL_defgv); |
1023 | AV *defav = GvAV (PL_defgv); |
1043 | struct coro *prev, *next; |
1024 | struct coro *prev, *next; |
1044 | |
1025 | |
1045 | if (!returnstk) |
1026 | yieldstack = *hv_fetch ( |
1046 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
1027 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1028 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1029 | 0 |
|
|
1030 | ); |
1047 | |
1031 | |
1048 | /* set up @_ -- ugly */ |
1032 | /* set up @_ -- ugly */ |
1049 | av_clear (defav); |
1033 | av_clear (defav); |
1050 | av_fill (defav, items - 1); |
1034 | av_fill (defav, items - 1); |
1051 | while (items--) |
1035 | while (items--) |
1052 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1036 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1053 | |
1037 | |
1054 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
|
|
1055 | sv = av_pop ((AV *)SvRV (returnstk)); |
1038 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1056 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1039 | prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)))); |
1057 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1040 | next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)))); |
1058 | SvREFCNT_dec (sv); |
1041 | SvREFCNT_dec (sv); |
1059 | |
1042 | |
1060 | transfer(aTHX_ prev, next, 0); |
1043 | transfer (aTHX_ prev, next, 0); |
1061 | |
1044 | |
1062 | MODULE = Coro::State PACKAGE = Coro |
1045 | MODULE = Coro::State PACKAGE = Coro |
1063 | |
1046 | |
1064 | # this is slightly dirty (should expose a c-level api) |
1047 | # this is slightly dirty (should expose a c-level api) |
1065 | |
1048 | |
… | |
… | |
1125 | cede(...) |
1108 | cede(...) |
1126 | PROTOTYPE: |
1109 | PROTOTYPE: |
1127 | CODE: |
1110 | CODE: |
1128 | api_cede (); |
1111 | api_cede (); |
1129 | |
1112 | |
|
|
1113 | # and these are hacks |
|
|
1114 | SV * |
|
|
1115 | _aio_get_state () |
|
|
1116 | CODE: |
|
|
1117 | { |
|
|
1118 | struct { |
|
|
1119 | int errorno; |
|
|
1120 | int laststype; |
|
|
1121 | int laststatval; |
|
|
1122 | Stat_t statcache; |
|
|
1123 | } data; |
|
|
1124 | |
|
|
1125 | data.errorno = errno; |
|
|
1126 | data.laststype = PL_laststype; |
|
|
1127 | data.laststatval = PL_laststatval; |
|
|
1128 | data.statcache = PL_statcache; |
|
|
1129 | |
|
|
1130 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1131 | } |
|
|
1132 | OUTPUT: |
|
|
1133 | RETVAL |
|
|
1134 | |
|
|
1135 | void |
|
|
1136 | _aio_set_state (char *data_) |
|
|
1137 | PROTOTYPE: $ |
|
|
1138 | CODE: |
|
|
1139 | { |
|
|
1140 | struct { |
|
|
1141 | int errorno; |
|
|
1142 | int laststype; |
|
|
1143 | int laststatval; |
|
|
1144 | Stat_t statcache; |
|
|
1145 | } *data = (void *)data_; |
|
|
1146 | |
|
|
1147 | errno = data->errorno; |
|
|
1148 | PL_laststype = data->laststype; |
|
|
1149 | PL_laststatval = data->laststatval; |
|
|
1150 | PL_statcache = data->statcache; |
|
|
1151 | } |