|
|
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> |
31 | |
33 | |
32 | #ifdef HAVE_MMAP |
34 | #ifdef HAVE_MMAP |
33 | # include <unistd.h> |
35 | # include <unistd.h> |
34 | # include <sys/mman.h> |
36 | # include <sys/mman.h> |
35 | # ifndef MAP_ANONYMOUS |
37 | # ifndef MAP_ANONYMOUS |
… | |
… | |
39 | # undef HAVE_MMAP |
41 | # undef HAVE_MMAP |
40 | # endif |
42 | # endif |
41 | # endif |
43 | # endif |
42 | #endif |
44 | #endif |
43 | |
45 | |
44 | #define MAY_FLUSH /* increases codesize and is rarely used */ |
|
|
45 | |
|
|
46 | #define SUB_INIT "Coro::State::initialize" |
46 | #define SUB_INIT "Coro::State::initialize" |
47 | #define UCORO_STATE "_coro_state" |
47 | #define UCORO_STATE "_coro_state" |
48 | |
48 | |
49 | /* The next macro should declare a variable stacklevel that contains and approximation |
49 | /* The next macro should declare a variable stacklevel that contains and approximation |
50 | * to the current C stack pointer. Its property is that it changes with each call |
50 | * to the current C stack pointer. Its property is that it changes with each call |
… | |
… | |
55 | |
55 | |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
57 | |
57 | |
58 | #include "CoroAPI.h" |
58 | #include "CoroAPI.h" |
59 | |
59 | |
|
|
60 | #ifdef USE_ITHREADS |
|
|
61 | static perl_mutex coro_mutex; |
|
|
62 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
|
|
63 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
|
|
64 | #else |
|
|
65 | # define LOCK (void)0 |
|
|
66 | # define UNLOCK (void)0 |
|
|
67 | #endif |
|
|
68 | |
60 | static struct CoroAPI coroapi; |
69 | static struct CoroAPI coroapi; |
|
|
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
71 | static HV *coro_state_stash; |
|
|
72 | static SV *ucoro_state_sv; |
|
|
73 | static U32 ucoro_state_hash; |
|
|
74 | static SV *coro_mortal; /* will be freed after next transfer */ |
61 | |
75 | |
62 | /* this is actually not only the c stack but also c registers etc... */ |
76 | /* this is actually not only the c stack but also c registers etc... */ |
63 | typedef struct { |
77 | typedef struct { |
64 | int refcnt; /* pointer reference counter */ |
78 | int refcnt; /* pointer reference counter */ |
65 | int usecnt; /* shared by how many coroutines */ |
79 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
95 | AV *mainstack; |
109 | AV *mainstack; |
96 | SV **stack_sp; |
110 | SV **stack_sp; |
97 | OP *op; |
111 | OP *op; |
98 | SV **curpad; |
112 | SV **curpad; |
99 | AV *comppad; |
113 | AV *comppad; |
|
|
114 | CV *compcv; |
100 | SV **stack_base; |
115 | SV **stack_base; |
101 | SV **stack_max; |
116 | SV **stack_max; |
102 | SV **tmps_stack; |
117 | SV **tmps_stack; |
103 | I32 tmps_floor; |
118 | I32 tmps_floor; |
104 | I32 tmps_ix; |
119 | I32 tmps_ix; |
… | |
… | |
113 | I32 savestack_ix; |
128 | I32 savestack_ix; |
114 | I32 savestack_max; |
129 | I32 savestack_max; |
115 | OP **retstack; |
130 | OP **retstack; |
116 | I32 retstack_ix; |
131 | I32 retstack_ix; |
117 | I32 retstack_max; |
132 | I32 retstack_max; |
|
|
133 | PMOP *curpm; |
118 | COP *curcop; |
134 | COP *curcop; |
119 | JMPENV *top_env; |
135 | JMPENV *top_env; |
120 | |
136 | |
121 | /* data associated with this coroutine (initial args) */ |
137 | /* data associated with this coroutine (initial args) */ |
122 | AV *args; |
138 | AV *args; |
123 | }; |
139 | }; |
124 | |
140 | |
125 | typedef struct coro *Coro__State; |
141 | typedef struct coro *Coro__State; |
126 | typedef struct coro *Coro__State_or_hashref; |
142 | typedef struct coro *Coro__State_or_hashref; |
127 | |
143 | |
128 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
144 | static AV * |
129 | static HV *coro_state_stash; |
145 | coro_clone_padlist (pTHX_ CV *cv) |
130 | static SV *ucoro_state_sv; |
|
|
131 | static U32 ucoro_state_hash; |
|
|
132 | static HV *padlist_cache; |
|
|
133 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
134 | |
|
|
135 | /* mostly copied from op.c:cv_clone2 */ |
|
|
136 | STATIC AV * |
|
|
137 | clone_padlist (AV *protopadlist) |
|
|
138 | { |
146 | { |
139 | AV *av; |
147 | AV *padlist = CvPADLIST (cv); |
140 | I32 ix; |
|
|
141 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
|
|
142 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
|
|
143 | SV **pname = AvARRAY (protopad_name); |
|
|
144 | SV **ppad = AvARRAY (protopad); |
|
|
145 | I32 fname = AvFILLp (protopad_name); |
|
|
146 | I32 fpad = AvFILLp (protopad); |
|
|
147 | AV *newpadlist, *newpad_name, *newpad; |
148 | AV *newpadlist, *newpad; |
148 | SV **npad; |
|
|
149 | |
|
|
150 | newpad_name = newAV (); |
|
|
151 | for (ix = fname; ix >= 0; ix--) |
|
|
152 | av_store (newpad_name, ix, SvREFCNT_inc (pname[ix])); |
|
|
153 | |
|
|
154 | newpad = newAV (); |
|
|
155 | av_fill (newpad, AvFILLp (protopad)); |
|
|
156 | npad = AvARRAY (newpad); |
|
|
157 | |
149 | |
158 | newpadlist = newAV (); |
150 | newpadlist = newAV (); |
159 | AvREAL_off (newpadlist); |
151 | AvREAL_off (newpadlist); |
160 | av_store (newpadlist, 0, (SV *) newpad_name); |
152 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
|
|
153 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
|
|
154 | --AvFILLp (padlist); |
|
|
155 | |
|
|
156 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
161 | av_store (newpadlist, 1, (SV *) newpad); |
157 | av_store (newpadlist, 1, (SV *)newpad); |
162 | |
158 | |
163 | av = newAV (); /* will be @_ */ |
159 | return newpadlist; |
164 | av_extend (av, 0); |
160 | } |
165 | av_store (newpad, 0, (SV *) av); |
|
|
166 | AvFLAGS (av) = AVf_REIFY; |
|
|
167 | |
161 | |
168 | for (ix = fpad; ix > 0; ix--) |
162 | static void |
|
|
163 | free_padlist (pTHX_ AV *padlist) |
|
|
164 | { |
|
|
165 | /* may be during global destruction */ |
|
|
166 | if (SvREFCNT (padlist)) |
169 | { |
167 | { |
170 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
168 | I32 i = AvFILLp (padlist); |
171 | if (namesv && namesv != &PL_sv_undef) |
169 | while (i >= 0) |
172 | { |
170 | { |
173 | char *name = SvPVX (namesv); /* XXX */ |
171 | SV **svp = av_fetch (padlist, i--, FALSE); |
174 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
172 | if (svp) |
175 | { /* lexical from outside? */ |
|
|
176 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
|
|
177 | } |
173 | { |
178 | else |
|
|
179 | { /* our own lexical */ |
|
|
180 | SV *sv; |
174 | SV *sv; |
181 | if (*name == '&') |
175 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
182 | sv = SvREFCNT_inc (ppad[ix]); |
176 | SvREFCNT_dec (sv); |
183 | else if (*name == '@') |
177 | |
184 | sv = (SV *) newAV (); |
178 | SvREFCNT_dec (*svp); |
185 | else if (*name == '%') |
|
|
186 | sv = (SV *) newHV (); |
|
|
187 | else |
|
|
188 | sv = NEWSV (0, 0); |
|
|
189 | #ifdef SvPADBUSY |
|
|
190 | if (!SvPADBUSY (sv)) |
|
|
191 | #endif |
|
|
192 | SvPADMY_on (sv); |
|
|
193 | npad[ix] = sv; |
|
|
194 | } |
179 | } |
195 | } |
180 | } |
196 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
181 | |
197 | { |
182 | SvREFCNT_dec ((SV*)padlist); |
198 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
|
|
199 | } |
|
|
200 | else |
|
|
201 | { |
|
|
202 | SV *sv = NEWSV (0, 0); |
|
|
203 | SvPADTMP_on (sv); |
|
|
204 | npad[ix] = sv; |
|
|
205 | } |
|
|
206 | } |
183 | } |
|
|
184 | } |
207 | |
185 | |
208 | #if 0 /* return -ENOTUNDERSTOOD */ |
186 | static int |
209 | /* Now that vars are all in place, clone nested closures. */ |
187 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
188 | { |
|
|
189 | AV *padlist; |
|
|
190 | AV *av = (AV *)mg->mg_obj; |
210 | |
191 | |
211 | for (ix = fpad; ix > 0; ix--) { |
192 | /* casting is fun. */ |
212 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; |
193 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
213 | if (namesv |
194 | free_padlist (aTHX_ padlist); |
214 | && namesv != &PL_sv_undef |
195 | |
215 | && !(SvFLAGS(namesv) & SVf_FAKE) |
196 | SvREFCNT_dec (av); |
216 | && *SvPVX(namesv) == '&' |
197 | |
217 | && CvCLONE(ppad[ix])) |
198 | return 0; |
218 | { |
199 | } |
219 | CV *kid = cv_clone((CV*)ppad[ix]); |
200 | |
220 | SvREFCNT_dec(ppad[ix]); |
201 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
221 | CvCLONE_on(kid); |
202 | |
222 | SvPADMY_on(kid); |
203 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
223 | npad[ix] = (SV*)kid; |
204 | |
224 | } |
205 | /* the next two functions merely cache the padlists */ |
|
|
206 | static void |
|
|
207 | get_padlist (pTHX_ CV *cv) |
|
|
208 | { |
|
|
209 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
210 | |
|
|
211 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
|
|
212 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
|
|
213 | else |
|
|
214 | { |
|
|
215 | #if 0 |
|
|
216 | /* this should work - but it doesn't :( */ |
|
|
217 | CV *cp = Perl_cv_clone (aTHX_ cv); |
|
|
218 | CvPADLIST (cv) = CvPADLIST (cp); |
|
|
219 | CvPADLIST (cp) = 0; |
|
|
220 | SvREFCNT_dec (cp); |
|
|
221 | #else |
|
|
222 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
|
|
223 | #endif |
|
|
224 | } |
|
|
225 | } |
|
|
226 | |
|
|
227 | static void |
|
|
228 | put_padlist (pTHX_ CV *cv) |
|
|
229 | { |
|
|
230 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
231 | |
|
|
232 | if (!mg) |
|
|
233 | { |
|
|
234 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
235 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
236 | mg->mg_virtual = &vtbl_coro; |
|
|
237 | mg->mg_obj = (SV *)newAV (); |
225 | } |
238 | } |
226 | #endif |
|
|
227 | |
239 | |
228 | return newpadlist; |
|
|
229 | } |
|
|
230 | |
|
|
231 | #ifdef MAY_FLUSH |
|
|
232 | STATIC void |
|
|
233 | free_padlist (AV *padlist) |
|
|
234 | { |
|
|
235 | /* may be during global destruction */ |
|
|
236 | if (SvREFCNT(padlist)) |
|
|
237 | { |
|
|
238 | I32 i = AvFILLp(padlist); |
|
|
239 | while (i >= 0) |
|
|
240 | { |
|
|
241 | SV **svp = av_fetch(padlist, i--, FALSE); |
|
|
242 | SV *sv = svp ? *svp : Nullsv; |
|
|
243 | if (sv) |
|
|
244 | SvREFCNT_dec(sv); |
|
|
245 | } |
|
|
246 | |
|
|
247 | SvREFCNT_dec((SV*)padlist); |
|
|
248 | } |
|
|
249 | } |
|
|
250 | #endif |
|
|
251 | |
|
|
252 | /* the next two functions merely cache the padlists */ |
|
|
253 | STATIC void |
|
|
254 | get_padlist (CV *cv) |
|
|
255 | { |
|
|
256 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); |
|
|
257 | |
|
|
258 | if (he && AvFILLp ((AV *)*he) >= 0) |
|
|
259 | CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); |
|
|
260 | else |
|
|
261 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
|
|
262 | } |
|
|
263 | |
|
|
264 | STATIC void |
|
|
265 | put_padlist (CV *cv) |
|
|
266 | { |
|
|
267 | SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); |
|
|
268 | |
|
|
269 | if (SvTYPE (*he) != SVt_PVAV) |
|
|
270 | { |
|
|
271 | SvREFCNT_dec (*he); |
|
|
272 | *he = (SV *)newAV (); |
|
|
273 | } |
|
|
274 | |
|
|
275 | av_push ((AV *)*he, (SV *)CvPADLIST (cv)); |
240 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
276 | } |
241 | } |
277 | |
|
|
278 | #ifdef MAY_FLUSH |
|
|
279 | STATIC void |
|
|
280 | flush_padlist_cache () |
|
|
281 | { |
|
|
282 | HV *hv = padlist_cache; |
|
|
283 | padlist_cache = newHV (); |
|
|
284 | |
|
|
285 | if (hv_iterinit (hv)) |
|
|
286 | { |
|
|
287 | HE *he; |
|
|
288 | AV *padlist; |
|
|
289 | |
|
|
290 | while (!!(he = hv_iternext (hv))) |
|
|
291 | { |
|
|
292 | AV *av = (AV *)HeVAL(he); |
|
|
293 | |
|
|
294 | /* casting is fun. */ |
|
|
295 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
296 | free_padlist (padlist); |
|
|
297 | } |
|
|
298 | } |
|
|
299 | |
|
|
300 | SvREFCNT_dec (hv); |
|
|
301 | } |
|
|
302 | #endif |
|
|
303 | |
242 | |
304 | #define SB do { |
243 | #define SB do { |
305 | #define SE } while (0) |
244 | #define SE } while (0) |
306 | |
245 | |
307 | #define LOAD(state) load_state(aTHX_ (state)); |
246 | #define LOAD(state) load_state(aTHX_ (state)); |
… | |
… | |
320 | PL_mainstack = c->mainstack; |
259 | PL_mainstack = c->mainstack; |
321 | PL_stack_sp = c->stack_sp; |
260 | PL_stack_sp = c->stack_sp; |
322 | PL_op = c->op; |
261 | PL_op = c->op; |
323 | PL_curpad = c->curpad; |
262 | PL_curpad = c->curpad; |
324 | PL_comppad = c->comppad; |
263 | PL_comppad = c->comppad; |
|
|
264 | PL_compcv = c->compcv; |
325 | PL_stack_base = c->stack_base; |
265 | PL_stack_base = c->stack_base; |
326 | PL_stack_max = c->stack_max; |
266 | PL_stack_max = c->stack_max; |
327 | PL_tmps_stack = c->tmps_stack; |
267 | PL_tmps_stack = c->tmps_stack; |
328 | PL_tmps_floor = c->tmps_floor; |
268 | PL_tmps_floor = c->tmps_floor; |
329 | PL_tmps_ix = c->tmps_ix; |
269 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
335 | PL_scopestack_ix = c->scopestack_ix; |
275 | PL_scopestack_ix = c->scopestack_ix; |
336 | PL_scopestack_max = c->scopestack_max; |
276 | PL_scopestack_max = c->scopestack_max; |
337 | PL_savestack = c->savestack; |
277 | PL_savestack = c->savestack; |
338 | PL_savestack_ix = c->savestack_ix; |
278 | PL_savestack_ix = c->savestack_ix; |
339 | PL_savestack_max = c->savestack_max; |
279 | PL_savestack_max = c->savestack_max; |
|
|
280 | #if PERL_VERSION < 9 |
340 | PL_retstack = c->retstack; |
281 | PL_retstack = c->retstack; |
341 | PL_retstack_ix = c->retstack_ix; |
282 | PL_retstack_ix = c->retstack_ix; |
342 | PL_retstack_max = c->retstack_max; |
283 | PL_retstack_max = c->retstack_max; |
|
|
284 | #endif |
|
|
285 | PL_curpm = c->curpm; |
343 | PL_curcop = c->curcop; |
286 | PL_curcop = c->curcop; |
344 | PL_top_env = c->top_env; |
287 | PL_top_env = c->top_env; |
345 | |
288 | |
346 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
289 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
347 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
290 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
… | |
… | |
356 | { |
299 | { |
357 | AV *padlist = (AV *)POPs; |
300 | AV *padlist = (AV *)POPs; |
358 | |
301 | |
359 | if (padlist) |
302 | if (padlist) |
360 | { |
303 | { |
361 | put_padlist (cv); /* mark this padlist as available */ |
304 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
362 | CvPADLIST(cv) = padlist; |
305 | CvPADLIST(cv) = padlist; |
363 | #ifdef USE_THREADS |
|
|
364 | /*CvOWNER(cv) = (struct perl_thread *)POPs;*/ |
|
|
365 | #endif |
|
|
366 | } |
306 | } |
367 | |
307 | |
368 | ++CvDEPTH(cv); |
308 | ++CvDEPTH(cv); |
369 | } |
309 | } |
370 | |
310 | |
… | |
… | |
397 | if (CxTYPE(cx) == CXt_SUB) |
337 | if (CxTYPE(cx) == CXt_SUB) |
398 | { |
338 | { |
399 | CV *cv = cx->blk_sub.cv; |
339 | CV *cv = cx->blk_sub.cv; |
400 | if (CvDEPTH(cv)) |
340 | if (CvDEPTH(cv)) |
401 | { |
341 | { |
402 | #ifdef USE_THREADS |
|
|
403 | /*XPUSHs ((SV *)CvOWNER(cv));*/ |
|
|
404 | /*CvOWNER(cv) = 0;*/ |
|
|
405 | /*error must unlock this cv etc.. etc...*/ |
|
|
406 | #endif |
|
|
407 | EXTEND (SP, CvDEPTH(cv)*2); |
342 | EXTEND (SP, CvDEPTH(cv)*2); |
408 | |
343 | |
409 | while (--CvDEPTH(cv)) |
344 | while (--CvDEPTH(cv)) |
410 | { |
345 | { |
411 | /* this tells the restore code to increment CvDEPTH */ |
346 | /* this tells the restore code to increment CvDEPTH */ |
… | |
… | |
414 | } |
349 | } |
415 | |
350 | |
416 | PUSHs ((SV *)CvPADLIST(cv)); |
351 | PUSHs ((SV *)CvPADLIST(cv)); |
417 | PUSHs ((SV *)cv); |
352 | PUSHs ((SV *)cv); |
418 | |
353 | |
419 | get_padlist (cv); /* this is a monster */ |
354 | get_padlist (aTHX_ cv); |
420 | } |
355 | } |
421 | } |
356 | } |
422 | #ifdef CXt_FORMAT |
357 | #ifdef CXt_FORMAT |
423 | else if (CxTYPE(cx) == CXt_FORMAT) |
358 | else if (CxTYPE(cx) == CXt_FORMAT) |
424 | { |
359 | { |
… | |
… | |
452 | c->mainstack = PL_mainstack; |
387 | c->mainstack = PL_mainstack; |
453 | c->stack_sp = PL_stack_sp; |
388 | c->stack_sp = PL_stack_sp; |
454 | c->op = PL_op; |
389 | c->op = PL_op; |
455 | c->curpad = PL_curpad; |
390 | c->curpad = PL_curpad; |
456 | c->comppad = PL_comppad; |
391 | c->comppad = PL_comppad; |
|
|
392 | c->compcv = PL_compcv; |
457 | c->stack_base = PL_stack_base; |
393 | c->stack_base = PL_stack_base; |
458 | c->stack_max = PL_stack_max; |
394 | c->stack_max = PL_stack_max; |
459 | c->tmps_stack = PL_tmps_stack; |
395 | c->tmps_stack = PL_tmps_stack; |
460 | c->tmps_floor = PL_tmps_floor; |
396 | c->tmps_floor = PL_tmps_floor; |
461 | c->tmps_ix = PL_tmps_ix; |
397 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
467 | c->scopestack_ix = PL_scopestack_ix; |
403 | c->scopestack_ix = PL_scopestack_ix; |
468 | c->scopestack_max = PL_scopestack_max; |
404 | c->scopestack_max = PL_scopestack_max; |
469 | c->savestack = PL_savestack; |
405 | c->savestack = PL_savestack; |
470 | c->savestack_ix = PL_savestack_ix; |
406 | c->savestack_ix = PL_savestack_ix; |
471 | c->savestack_max = PL_savestack_max; |
407 | c->savestack_max = PL_savestack_max; |
|
|
408 | #if PERL_VERSION < 9 |
472 | c->retstack = PL_retstack; |
409 | c->retstack = PL_retstack; |
473 | c->retstack_ix = PL_retstack_ix; |
410 | c->retstack_ix = PL_retstack_ix; |
474 | c->retstack_max = PL_retstack_max; |
411 | c->retstack_max = PL_retstack_max; |
|
|
412 | #endif |
|
|
413 | c->curpm = PL_curpm; |
475 | c->curcop = PL_curcop; |
414 | c->curcop = PL_curcop; |
476 | c->top_env = PL_top_env; |
415 | c->top_env = PL_top_env; |
477 | } |
416 | } |
478 | |
417 | |
479 | /* |
418 | /* |
480 | * allocate various perl stacks. This is an exact copy |
419 | * allocate various perl stacks. This is an exact copy |
481 | * of perl.c:init_stacks, except that it uses less memory |
420 | * of perl.c:init_stacks, except that it uses less memory |
482 | * on the assumption that coroutines do not usually need |
421 | * on the (sometimes correct) assumption that coroutines do |
483 | * a lot of stackspace. |
422 | * not usually need a lot of stackspace. |
484 | */ |
423 | */ |
485 | STATIC void |
424 | static void |
486 | coro_init_stacks (pTHX) |
425 | coro_init_stacks (pTHX) |
487 | { |
426 | { |
|
|
427 | LOCK; |
|
|
428 | |
488 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
429 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
489 | PL_curstackinfo->si_type = PERLSI_MAIN; |
430 | PL_curstackinfo->si_type = PERLSI_MAIN; |
490 | PL_curstack = PL_curstackinfo->si_stack; |
431 | PL_curstack = PL_curstackinfo->si_stack; |
491 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
432 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
492 | |
433 | |
… | |
… | |
513 | |
454 | |
514 | New(54,PL_savestack,96,ANY); |
455 | New(54,PL_savestack,96,ANY); |
515 | PL_savestack_ix = 0; |
456 | PL_savestack_ix = 0; |
516 | PL_savestack_max = 96; |
457 | PL_savestack_max = 96; |
517 | |
458 | |
|
|
459 | #if PERL_VERSION < 9 |
518 | New(54,PL_retstack,8,OP*); |
460 | New(54,PL_retstack,8,OP*); |
519 | PL_retstack_ix = 0; |
461 | PL_retstack_ix = 0; |
520 | PL_retstack_max = 8; |
462 | PL_retstack_max = 8; |
|
|
463 | #endif |
|
|
464 | |
|
|
465 | UNLOCK; |
521 | } |
466 | } |
522 | |
467 | |
523 | /* |
468 | /* |
524 | * destroy the stacks, the callchain etc... |
469 | * destroy the stacks, the callchain etc... |
525 | */ |
470 | */ |
526 | STATIC void |
471 | static void |
527 | destroy_stacks(pTHX) |
472 | destroy_stacks(pTHX) |
528 | { |
473 | { |
529 | if (!IN_DESTRUCT) |
474 | if (!IN_DESTRUCT) |
530 | { |
475 | { |
531 | /* is this ugly, I ask? */ |
476 | /* is this ugly, I ask? */ |
532 | while (PL_scopestack_ix) |
477 | LEAVE_SCOPE (0); |
533 | LEAVE; |
|
|
534 | |
478 | |
535 | /* sure it is, but more important: is it correct?? :/ */ |
479 | /* sure it is, but more important: is it correct?? :/ */ |
536 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
|
|
537 | FREETMPS; |
480 | FREETMPS; |
|
|
481 | |
|
|
482 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
538 | } |
483 | } |
539 | |
484 | |
540 | while (PL_curstackinfo->si_next) |
485 | while (PL_curstackinfo->si_next) |
541 | PL_curstackinfo = PL_curstackinfo->si_next; |
486 | PL_curstackinfo = PL_curstackinfo->si_next; |
542 | |
487 | |
543 | while (PL_curstackinfo) |
488 | while (PL_curstackinfo) |
544 | { |
489 | { |
545 | PERL_SI *p = PL_curstackinfo->si_prev; |
490 | PERL_SI *p = PL_curstackinfo->si_prev; |
546 | |
491 | |
547 | { |
492 | { /*D*//*remove*/ |
548 | dSP; |
493 | dSP; |
549 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
494 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
550 | PUTBACK; /* possibly superfluous */ |
495 | PUTBACK; /* possibly superfluous */ |
551 | } |
496 | } |
552 | |
497 | |
553 | if (!IN_DESTRUCT) |
498 | if (!IN_DESTRUCT) |
554 | { |
499 | { |
555 | dounwind(-1); |
500 | dounwind (-1);/*D*//*remove*/ |
556 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
501 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
557 | } |
502 | } |
558 | |
503 | |
559 | Safefree(PL_curstackinfo->si_cxstack); |
504 | Safefree (PL_curstackinfo->si_cxstack); |
560 | Safefree(PL_curstackinfo); |
505 | Safefree (PL_curstackinfo); |
561 | PL_curstackinfo = p; |
506 | PL_curstackinfo = p; |
562 | } |
507 | } |
563 | |
508 | |
564 | Safefree(PL_tmps_stack); |
509 | Safefree (PL_tmps_stack); |
565 | Safefree(PL_markstack); |
510 | Safefree (PL_markstack); |
566 | Safefree(PL_scopestack); |
511 | Safefree (PL_scopestack); |
567 | Safefree(PL_savestack); |
512 | Safefree (PL_savestack); |
|
|
513 | #if PERL_VERSION < 9 |
568 | Safefree(PL_retstack); |
514 | Safefree (PL_retstack); |
|
|
515 | #endif |
569 | } |
516 | } |
570 | |
517 | |
571 | static void |
518 | static void |
572 | allocate_stack (Coro__State ctx, int alloc) |
519 | allocate_stack (Coro__State ctx, int alloc) |
573 | { |
520 | { |
… | |
… | |
576 | New (0, stack, 1, coro_stack); |
523 | New (0, stack, 1, coro_stack); |
577 | |
524 | |
578 | stack->refcnt = 1; |
525 | stack->refcnt = 1; |
579 | stack->usecnt = 1; |
526 | stack->usecnt = 1; |
580 | stack->gencnt = ctx->gencnt = 0; |
527 | stack->gencnt = ctx->gencnt = 0; |
|
|
528 | |
581 | if (alloc) |
529 | if (alloc) |
582 | { |
530 | { |
583 | #if HAVE_MMAP |
531 | #if HAVE_MMAP |
584 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
532 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
585 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
533 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
586 | if (stack->sptr == (void *)-1) |
534 | if (stack->sptr == (void *)-1) |
587 | #endif |
535 | #endif |
588 | { |
536 | { |
589 | /*FIXME*//*D*//* reasonable stack size! */ |
|
|
590 | stack->ssize = -4096 * sizeof (long); |
537 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
591 | New (0, stack->sptr, 4096, long); |
538 | New (0, stack->sptr, STACKSIZE, long); |
592 | } |
539 | } |
593 | } |
540 | } |
594 | else |
541 | else |
595 | stack->sptr = 0; |
542 | stack->sptr = 0; |
596 | |
543 | |
… | |
… | |
626 | setup_coro (void *arg) |
573 | setup_coro (void *arg) |
627 | { |
574 | { |
628 | /* |
575 | /* |
629 | * emulate part of the perl startup here. |
576 | * emulate part of the perl startup here. |
630 | */ |
577 | */ |
|
|
578 | dTHX; |
631 | dSP; |
579 | dSP; |
632 | Coro__State ctx = (Coro__State)arg; |
580 | Coro__State ctx = (Coro__State)arg; |
633 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
581 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
634 | |
582 | |
635 | coro_init_stacks (aTHX); |
583 | coro_init_stacks (aTHX); |
636 | /*PL_curcop = 0;*/ |
584 | /*PL_curcop = 0;*/ |
637 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
585 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
638 | SvREFCNT_dec (GvAV (PL_defgv)); |
586 | SvREFCNT_dec (GvAV (PL_defgv)); |
… | |
… | |
684 | continue_coro (void *arg) |
632 | continue_coro (void *arg) |
685 | { |
633 | { |
686 | /* |
634 | /* |
687 | * this is a _very_ stripped down perl interpreter ;) |
635 | * this is a _very_ stripped down perl interpreter ;) |
688 | */ |
636 | */ |
|
|
637 | dTHX; |
689 | Coro__State ctx = (Coro__State)arg; |
638 | Coro__State ctx = (Coro__State)arg; |
690 | JMPENV coro_start_env; |
|
|
691 | |
639 | |
692 | PL_top_env = &ctx->start_env; |
640 | PL_top_env = &ctx->start_env; |
693 | |
641 | |
694 | ctx->cursp = 0; |
642 | ctx->cursp = 0; |
695 | PL_op = PL_op->op_next; |
643 | PL_op = PL_op->op_next; |
696 | CALLRUNOPS(aTHX); |
644 | CALLRUNOPS(aTHX); |
697 | |
645 | |
698 | abort (); |
646 | abort (); |
699 | } |
647 | } |
700 | |
648 | |
701 | STATIC void |
649 | static void |
702 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
650 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
703 | { |
651 | { |
704 | dSTACKLEVEL; |
652 | dSTACKLEVEL; |
705 | static struct coro *xnext; |
|
|
706 | |
653 | |
707 | if (prev != next) |
654 | if (prev != next) |
708 | { |
655 | { |
709 | xnext = next; |
|
|
710 | |
|
|
711 | if (next->mainstack) |
656 | if (next->mainstack) |
712 | { |
657 | { |
|
|
658 | LOCK; |
713 | SAVE (prev, flags); |
659 | SAVE (prev, flags); |
714 | LOAD (next); |
660 | LOAD (next); |
|
|
661 | UNLOCK; |
715 | |
662 | |
716 | /* mark this state as in-use */ |
663 | /* mark this state as in-use */ |
717 | next->mainstack = 0; |
664 | next->mainstack = 0; |
718 | next->tmps_ix = -2; |
665 | next->tmps_ix = -2; |
719 | |
666 | |
… | |
… | |
738 | continue_coro, (void *)next, |
685 | continue_coro, (void *)next, |
739 | next->stack->sptr, labs (next->stack->ssize)); |
686 | next->stack->sptr, labs (next->stack->ssize)); |
740 | } |
687 | } |
741 | |
688 | |
742 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
689 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
690 | prev->cursp = stacklevel; |
743 | /* don't add any code here */ |
691 | /* don't add any code here */ |
744 | } |
692 | } |
745 | |
693 | else |
|
|
694 | next->cursp = stacklevel; |
746 | } |
695 | } |
747 | else if (next->tmps_ix == -2) |
696 | else if (next->tmps_ix == -2) |
748 | croak ("tried to transfer to running coroutine"); |
697 | croak ("tried to transfer to running coroutine"); |
749 | else |
698 | else |
750 | { |
699 | { |
|
|
700 | LOCK; |
751 | SAVE (prev, -1); /* first get rid of the old state */ |
701 | SAVE (prev, -1); /* first get rid of the old state */ |
|
|
702 | UNLOCK; |
752 | |
703 | |
753 | if (flags & TRANSFER_SAVE_CCTXT) |
704 | if (flags & TRANSFER_SAVE_CCTXT) |
754 | { |
705 | { |
755 | if (!prev->stack) |
706 | if (!prev->stack) |
756 | allocate_stack (prev, 0); |
707 | allocate_stack (prev, 0); |
… | |
… | |
758 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
709 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
759 | { |
710 | { |
760 | PL_top_env = &next->start_env; |
711 | PL_top_env = &next->start_env; |
761 | |
712 | |
762 | setup_coro (next); |
713 | setup_coro (next); |
|
|
714 | next->cursp = stacklevel; |
763 | |
715 | |
764 | prev->stack->refcnt++; |
716 | prev->stack->refcnt++; |
765 | prev->stack->usecnt++; |
717 | prev->stack->usecnt++; |
766 | next->stack = prev->stack; |
718 | next->stack = prev->stack; |
767 | next->gencnt = prev->gencnt; |
719 | next->gencnt = prev->gencnt; |
… | |
… | |
772 | allocate_stack (next, 1); |
724 | allocate_stack (next, 1); |
773 | coro_create (&(next->stack->cctx), |
725 | coro_create (&(next->stack->cctx), |
774 | setup_coro, (void *)next, |
726 | setup_coro, (void *)next, |
775 | next->stack->sptr, labs (next->stack->ssize)); |
727 | next->stack->sptr, labs (next->stack->ssize)); |
776 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
728 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
729 | prev->cursp = stacklevel; |
777 | /* don't add any code here */ |
730 | /* don't add any code here */ |
778 | } |
731 | } |
779 | } |
732 | } |
780 | else |
733 | else |
|
|
734 | { |
781 | setup_coro (next); |
735 | setup_coro (next); |
|
|
736 | next->cursp = stacklevel; |
|
|
737 | } |
782 | } |
738 | } |
783 | |
|
|
784 | /* |
|
|
785 | * xnext is now either prev or next, depending on wether |
|
|
786 | * we switched the c stack or not. that's why I use a global |
|
|
787 | * variable, that should become thread-specific at one point. |
|
|
788 | */ |
|
|
789 | xnext->cursp = stacklevel; |
|
|
790 | } |
739 | } |
791 | |
740 | |
|
|
741 | LOCK; |
792 | if (coro_mortal) |
742 | if (coro_mortal) |
793 | { |
743 | { |
794 | SvREFCNT_dec (coro_mortal); |
744 | SvREFCNT_dec (coro_mortal); |
795 | coro_mortal = 0; |
745 | coro_mortal = 0; |
796 | } |
746 | } |
|
|
747 | UNLOCK; |
797 | } |
748 | } |
798 | |
749 | |
799 | #define SV_CORO(sv,func) \ |
750 | #define SV_CORO(sv,func) \ |
800 | do { \ |
751 | do { \ |
801 | if (SvROK (sv)) \ |
752 | if (SvROK (sv)) \ |
802 | sv = SvRV (sv); \ |
753 | sv = SvRV (sv); \ |
803 | \ |
754 | \ |
804 | if (SvTYPE(sv) == SVt_PVHV) \ |
755 | if (SvTYPE (sv) == SVt_PVHV) \ |
805 | { \ |
756 | { \ |
806 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
757 | HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
807 | \ |
758 | \ |
808 | if (!he) \ |
759 | if (!he) \ |
809 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
760 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
810 | \ |
761 | \ |
811 | (sv) = SvRV (HeVAL(he)); \ |
762 | (sv) = SvRV (HeVAL(he)); \ |
812 | } \ |
763 | } \ |
813 | \ |
764 | \ |
814 | /* must also be changed inside Coro::Cont::yield */ \ |
765 | /* must also be changed inside Coro::Cont::yield */ \ |
815 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
766 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
816 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
767 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
817 | \ |
768 | \ |
818 | } while(0) |
769 | } while(0) |
819 | |
770 | |
820 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
771 | #define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) |
821 | |
772 | |
822 | static void |
773 | static void |
823 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
774 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
824 | { |
775 | { |
825 | SV_CORO (prev, "Coro::transfer"); |
776 | SV_CORO (prev, "Coro::transfer"); |
826 | SV_CORO (next, "Coro::transfer"); |
777 | SV_CORO (next, "Coro::transfer"); |
827 | |
778 | |
828 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
779 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
829 | } |
780 | } |
830 | |
781 | |
831 | /** Coro ********************************************************************/ |
782 | /** Coro ********************************************************************/ |
832 | |
783 | |
833 | #define PRIO_MAX 3 |
784 | #define PRIO_MAX 3 |
… | |
… | |
841 | static GV *coro_current, *coro_idle; |
792 | static GV *coro_current, *coro_idle; |
842 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
793 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
843 | static int coro_nready; |
794 | static int coro_nready; |
844 | |
795 | |
845 | static void |
796 | static void |
846 | coro_enq (SV *sv) |
797 | coro_enq (pTHX_ SV *sv) |
847 | { |
798 | { |
|
|
799 | SV **xprio; |
|
|
800 | int prio; |
|
|
801 | |
848 | if (SvTYPE (sv) == SVt_PVHV) |
802 | if (SvTYPE (sv) != SVt_PVHV) |
849 | { |
|
|
850 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
851 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
852 | |
|
|
853 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
854 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
855 | : prio; |
|
|
856 | |
|
|
857 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
858 | coro_nready++; |
|
|
859 | |
|
|
860 | return; |
|
|
861 | } |
|
|
862 | |
|
|
863 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
803 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
804 | |
|
|
805 | xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
806 | prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
807 | |
|
|
808 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
809 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
810 | : prio; |
|
|
811 | |
|
|
812 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
813 | coro_nready++; |
864 | } |
814 | } |
865 | |
815 | |
866 | static SV * |
816 | static SV * |
867 | coro_deq (int min_prio) |
817 | coro_deq (pTHX_ int min_prio) |
868 | { |
818 | { |
869 | int prio = PRIO_MAX - PRIO_MIN; |
819 | int prio = PRIO_MAX - PRIO_MIN; |
870 | |
820 | |
871 | min_prio -= PRIO_MIN; |
821 | min_prio -= PRIO_MIN; |
872 | if (min_prio < 0) |
822 | if (min_prio < 0) |
… | |
… | |
883 | } |
833 | } |
884 | |
834 | |
885 | static void |
835 | static void |
886 | api_ready (SV *coro) |
836 | api_ready (SV *coro) |
887 | { |
837 | { |
|
|
838 | dTHX; |
|
|
839 | |
888 | if (SvROK (coro)) |
840 | if (SvROK (coro)) |
889 | coro = SvRV (coro); |
841 | coro = SvRV (coro); |
890 | |
842 | |
|
|
843 | LOCK; |
891 | coro_enq (SvREFCNT_inc (coro)); |
844 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
|
|
845 | UNLOCK; |
892 | } |
846 | } |
893 | |
847 | |
894 | static void |
848 | static void |
895 | api_schedule (void) |
849 | api_schedule (void) |
896 | { |
850 | { |
|
|
851 | dTHX; |
|
|
852 | |
897 | SV *prev, *next; |
853 | SV *prev, *next; |
898 | |
854 | |
|
|
855 | LOCK; |
|
|
856 | |
899 | prev = SvRV (GvSV (coro_current)); |
857 | prev = SvRV (GvSV (coro_current)); |
900 | next = coro_deq (PRIO_MIN); |
858 | next = coro_deq (aTHX_ PRIO_MIN); |
901 | |
859 | |
902 | if (!next) |
860 | if (!next) |
903 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
861 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
904 | |
862 | |
905 | /* free this only after the transfer */ |
863 | /* free this only after the transfer */ |
… | |
… | |
908 | |
866 | |
909 | SvRV (GvSV (coro_current)) = next; |
867 | SvRV (GvSV (coro_current)) = next; |
910 | |
868 | |
911 | SV_CORO (next, "Coro::schedule"); |
869 | SV_CORO (next, "Coro::schedule"); |
912 | |
870 | |
|
|
871 | UNLOCK; |
|
|
872 | |
913 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
873 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
914 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
874 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
915 | } |
875 | } |
916 | |
876 | |
917 | static void |
877 | static void |
918 | api_cede (void) |
878 | api_cede (void) |
919 | { |
879 | { |
|
|
880 | dTHX; |
|
|
881 | |
|
|
882 | LOCK; |
920 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
883 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
884 | UNLOCK; |
921 | |
885 | |
922 | api_schedule (); |
886 | api_schedule (); |
923 | } |
887 | } |
924 | |
888 | |
925 | MODULE = Coro::State PACKAGE = Coro::State |
889 | MODULE = Coro::State PACKAGE = Coro::State |
926 | |
890 | |
927 | PROTOTYPES: ENABLE |
891 | PROTOTYPES: ENABLE |
928 | |
892 | |
929 | BOOT: |
893 | BOOT: |
930 | { /* {} necessary for stoopid perl-5.6.x */ |
894 | { /* {} necessary for stoopid perl-5.6.x */ |
|
|
895 | #ifdef USE_ITHREADS |
|
|
896 | MUTEX_INIT (&coro_mutex); |
|
|
897 | #endif |
|
|
898 | |
931 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
899 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
932 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
900 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
933 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
901 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
934 | |
902 | |
935 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
903 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
936 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
904 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
937 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
905 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
938 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
906 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
939 | |
|
|
940 | if (!padlist_cache) |
|
|
941 | padlist_cache = newHV (); |
|
|
942 | |
907 | |
943 | main_mainstack = PL_mainstack; |
908 | main_mainstack = PL_mainstack; |
944 | |
909 | |
945 | coroapi.ver = CORO_API_VERSION; |
910 | coroapi.ver = CORO_API_VERSION; |
946 | coroapi.transfer = api_transfer; |
911 | coroapi.transfer = api_transfer; |
… | |
… | |
957 | croak ("Coro::State::_newprocess expects an arrayref"); |
922 | croak ("Coro::State::_newprocess expects an arrayref"); |
958 | |
923 | |
959 | Newz (0, coro, 1, struct coro); |
924 | Newz (0, coro, 1, struct coro); |
960 | |
925 | |
961 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
926 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
962 | coro->mainstack = 0; /* actual work is done inside transfer */ |
927 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
963 | coro->stack = 0; |
928 | /*coro->stack = 0;*/ |
964 | |
929 | |
965 | /* same as JMPENV_BOOTSTRAP */ |
930 | /* same as JMPENV_BOOTSTRAP */ |
966 | /* we might be able to recycle start_env, but safe is safe */ |
931 | /* we might be able to recycle start_env, but safe is safe */ |
967 | //Zero(&coro->start_env, 1, JMPENV); |
932 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
968 | coro->start_env.je_ret = -1; |
933 | coro->start_env.je_ret = -1; |
969 | coro->start_env.je_mustcatch = TRUE; |
934 | coro->start_env.je_mustcatch = TRUE; |
970 | |
935 | |
971 | RETVAL = coro; |
936 | RETVAL = coro; |
972 | OUTPUT: |
937 | OUTPUT: |
… | |
… | |
993 | if (coro->mainstack && coro->mainstack != main_mainstack) |
958 | if (coro->mainstack && coro->mainstack != main_mainstack) |
994 | { |
959 | { |
995 | struct coro temp; |
960 | struct coro temp; |
996 | |
961 | |
997 | PUTBACK; |
962 | PUTBACK; |
998 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
963 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
999 | LOAD(aTHX_ coro); |
964 | LOAD (aTHX_ coro); |
1000 | SPAGAIN; |
965 | SPAGAIN; |
1001 | |
966 | |
1002 | destroy_stacks (aTHX); |
967 | destroy_stacks (aTHX); |
1003 | |
968 | |
1004 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
969 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
1005 | SPAGAIN; |
970 | SPAGAIN; |
1006 | |
971 | |
1007 | coro->mainstack = 0; |
972 | coro->mainstack = 0; |
1008 | } |
973 | } |
1009 | |
974 | |
1010 | deallocate_stack (coro); |
975 | deallocate_stack (coro); |
1011 | SvREFCNT_dec (coro->args); |
976 | SvREFCNT_dec (coro->args); |
1012 | Safefree (coro); |
977 | Safefree (coro); |
1013 | |
|
|
1014 | void |
|
|
1015 | flush() |
|
|
1016 | CODE: |
|
|
1017 | #ifdef MAY_FLUSH |
|
|
1018 | flush_padlist_cache (); |
|
|
1019 | #endif |
|
|
1020 | |
978 | |
1021 | void |
979 | void |
1022 | _exit(code) |
980 | _exit(code) |
1023 | int code |
981 | int code |
1024 | PROTOTYPE: $ |
982 | PROTOTYPE: $ |
1025 | CODE: |
983 | CODE: |
1026 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
1027 | _exit (code); |
984 | _exit (code); |
1028 | #else |
|
|
1029 | signal (SIGTERM, SIG_DFL); |
|
|
1030 | raise (SIGTERM); |
|
|
1031 | exit (code); |
|
|
1032 | #endif |
|
|
1033 | |
985 | |
1034 | MODULE = Coro::State PACKAGE = Coro::Cont |
986 | MODULE = Coro::State PACKAGE = Coro::Cont |
1035 | |
987 | |
1036 | # this is slightly dirty (should expose a c-level api) |
988 | # this is slightly dirty (should expose a c-level api) |
1037 | |
989 | |
1038 | void |
990 | void |
1039 | yield(...) |
991 | yield(...) |
1040 | PROTOTYPE: @ |
992 | PROTOTYPE: @ |
1041 | CODE: |
993 | CODE: |
1042 | static SV *returnstk; |
994 | SV *yieldstack; |
1043 | SV *sv; |
995 | SV *sv; |
1044 | AV *defav = GvAV (PL_defgv); |
996 | AV *defav = GvAV (PL_defgv); |
1045 | struct coro *prev, *next; |
997 | struct coro *prev, *next; |
1046 | |
998 | |
1047 | if (!returnstk) |
999 | yieldstack = *hv_fetch ( |
1048 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
1000 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1001 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1002 | 0 |
|
|
1003 | ); |
1049 | |
1004 | |
1050 | /* set up @_ -- ugly */ |
1005 | /* set up @_ -- ugly */ |
1051 | av_clear (defav); |
1006 | av_clear (defav); |
1052 | av_fill (defav, items - 1); |
1007 | av_fill (defav, items - 1); |
1053 | while (items--) |
1008 | while (items--) |
1054 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1009 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1055 | |
1010 | |
1056 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
|
|
1057 | sv = av_pop ((AV *)SvRV (returnstk)); |
1011 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1058 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1012 | prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)))); |
1059 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1013 | next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)))); |
1060 | SvREFCNT_dec (sv); |
1014 | SvREFCNT_dec (sv); |
1061 | |
1015 | |
1062 | transfer(aTHX_ prev, next, 0); |
1016 | transfer (aTHX_ prev, next, 0); |
1063 | |
1017 | |
1064 | MODULE = Coro::State PACKAGE = Coro |
1018 | MODULE = Coro::State PACKAGE = Coro |
1065 | |
1019 | |
1066 | # this is slightly dirty (should expose a c-level api) |
1020 | # this is slightly dirty (should expose a c-level api) |
1067 | |
1021 | |
… | |
… | |
1096 | sv_setiv(sv, (IV)&coroapi); |
1050 | sv_setiv(sv, (IV)&coroapi); |
1097 | SvREADONLY_on(sv); |
1051 | SvREADONLY_on(sv); |
1098 | } |
1052 | } |
1099 | } |
1053 | } |
1100 | |
1054 | |
|
|
1055 | #if !PERL_MICRO |
|
|
1056 | |
1101 | void |
1057 | void |
1102 | ready(self) |
1058 | ready(self) |
1103 | SV * self |
1059 | SV * self |
1104 | PROTOTYPE: $ |
1060 | PROTOTYPE: $ |
1105 | CODE: |
1061 | CODE: |
1106 | api_ready (self); |
1062 | api_ready (self); |
|
|
1063 | |
|
|
1064 | #endif |
1107 | |
1065 | |
1108 | int |
1066 | int |
1109 | nready(...) |
1067 | nready(...) |
1110 | PROTOTYPE: |
1068 | PROTOTYPE: |
1111 | CODE: |
1069 | CODE: |
… | |
… | |
1123 | cede(...) |
1081 | cede(...) |
1124 | PROTOTYPE: |
1082 | PROTOTYPE: |
1125 | CODE: |
1083 | CODE: |
1126 | api_cede (); |
1084 | api_cede (); |
1127 | |
1085 | |
|
|
1086 | # and these are hacks |
|
|
1087 | SV * |
|
|
1088 | _aio_get_state () |
|
|
1089 | CODE: |
|
|
1090 | { |
|
|
1091 | struct { |
|
|
1092 | int errorno; |
|
|
1093 | int laststype; |
|
|
1094 | int laststatval; |
|
|
1095 | Stat_t statcache; |
|
|
1096 | } data; |
|
|
1097 | |
|
|
1098 | data.errorno = errno; |
|
|
1099 | data.laststype = PL_laststype; |
|
|
1100 | data.laststatval = PL_laststatval; |
|
|
1101 | data.statcache = PL_statcache; |
|
|
1102 | |
|
|
1103 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1104 | } |
|
|
1105 | OUTPUT: |
|
|
1106 | RETVAL |
|
|
1107 | |
|
|
1108 | void |
|
|
1109 | _aio_set_state (char *data_) |
|
|
1110 | PROTOTYPE: $ |
|
|
1111 | CODE: |
|
|
1112 | { |
|
|
1113 | struct { |
|
|
1114 | int errorno; |
|
|
1115 | int laststype; |
|
|
1116 | int laststatval; |
|
|
1117 | Stat_t statcache; |
|
|
1118 | } *data = (void *)data_; |
|
|
1119 | |
|
|
1120 | errno = data->errorno; |
|
|
1121 | PL_laststype = data->laststype; |
|
|
1122 | PL_laststatval = data->laststatval; |
|
|
1123 | PL_statcache = data->statcache; |
|
|
1124 | } |