1 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
|
|
2 | |
|
|
3 | #define PERL_NO_GET_CONTEXT |
2 | |
4 | |
3 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
4 | #include "perl.h" |
6 | #include "perl.h" |
5 | #include "XSUB.h" |
7 | #include "XSUB.h" |
6 | |
8 | |
… | |
… | |
31 | #else |
33 | #else |
32 | # define PAGESIZE 0 |
34 | # define PAGESIZE 0 |
33 | # define BOOT_PAGESIZE (void)0 |
35 | # define BOOT_PAGESIZE (void)0 |
34 | #endif |
36 | #endif |
35 | |
37 | |
36 | #if USE_VALGRIND |
38 | #if CORO_USE_VALGRIND |
37 | # include <valgrind/valgrind.h> |
39 | # include <valgrind/valgrind.h> |
|
|
40 | # define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) |
|
|
41 | #else |
|
|
42 | # define REGISTER_STACK(cctx,start,end) |
38 | #endif |
43 | #endif |
39 | |
44 | |
40 | /* the maximum number of idle cctx that will be pooled */ |
45 | /* the maximum number of idle cctx that will be pooled */ |
41 | #define MAX_IDLE_CCTX 8 |
46 | #define MAX_IDLE_CCTX 8 |
42 | |
47 | |
… | |
… | |
71 | #ifndef SvRV_set |
76 | #ifndef SvRV_set |
72 | # define SvRV_set(s,v) SvRV(s) = (v) |
77 | # define SvRV_set(s,v) SvRV(s) = (v) |
73 | #endif |
78 | #endif |
74 | |
79 | |
75 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
80 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
76 | # undef STACKGUARD |
81 | # undef CORO_STACKGUARD |
77 | #endif |
82 | #endif |
78 | |
83 | |
79 | #ifndef STACKGUARD |
84 | #ifndef CORO_STACKGUARD |
80 | # define STACKGUARD 0 |
85 | # define CORO_STACKGUARD 0 |
81 | #endif |
86 | #endif |
82 | |
87 | |
83 | /* prefer perl internal functions over our own? */ |
88 | /* prefer perl internal functions over our own? */ |
84 | #ifndef PREFER_PERL_FUNCTIONS |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
85 | # define PREFER_PERL_FUNCTIONS 0 |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
86 | #endif |
91 | #endif |
87 | |
92 | |
88 | /* The next macro should declare a variable stacklevel that contains and approximation |
93 | /* The next macros try to return the current stack pointer, in an as |
89 | * to the current C stack pointer. Its property is that it changes with each call |
94 | * portable way as possible. */ |
90 | * and should be unique. */ |
|
|
91 | #define dSTACKLEVEL int stacklevel |
95 | #define dSTACKLEVEL volatile char stacklevel |
92 | #define STACKLEVEL ((void *)&stacklevel) |
96 | #define STACKLEVEL ((void *)&stacklevel) |
93 | |
97 | |
94 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
98 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
95 | |
99 | |
96 | #if __GNUC__ >= 3 |
100 | #if __GNUC__ >= 3 |
… | |
… | |
121 | I32 laststype; |
125 | I32 laststype; |
122 | int laststatval; |
126 | int laststatval; |
123 | Stat_t statcache; |
127 | Stat_t statcache; |
124 | }; |
128 | }; |
125 | |
129 | |
|
|
130 | static size_t coro_stacksize = CORO_STACKSIZE; |
126 | static struct CoroAPI coroapi; |
131 | static struct CoroAPI coroapi; |
127 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
132 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
133 | static JMPENV *main_top_env; |
128 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
129 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
130 | |
136 | |
|
|
137 | /* async_pool helper stuff */ |
|
|
138 | static SV *sv_pool_rss; |
|
|
139 | static SV *sv_pool_size; |
|
|
140 | static AV *av_async_pool; |
|
|
141 | |
131 | static struct coro_cctx *cctx_first; |
142 | static struct coro_cctx *cctx_first; |
132 | static int cctx_count, cctx_idle; |
143 | static int cctx_count, cctx_idle; |
|
|
144 | |
|
|
145 | enum { |
|
|
146 | CC_MAPPED = 0x01, |
|
|
147 | CC_TRACE = 0x02, |
|
|
148 | CC_NOREUSE = 0x04, /* throw this away after tracing */ |
|
|
149 | }; |
133 | |
150 | |
134 | /* this is a structure representing a c-level coroutine */ |
151 | /* this is a structure representing a c-level coroutine */ |
135 | typedef struct coro_cctx { |
152 | typedef struct coro_cctx { |
136 | struct coro_cctx *next; |
153 | struct coro_cctx *next; |
137 | |
154 | |
138 | /* the stack */ |
155 | /* the stack */ |
139 | void *sptr; |
156 | void *sptr; |
140 | long ssize; /* positive == mmap, otherwise malloc */ |
157 | size_t ssize; |
141 | |
158 | |
142 | /* cpu state */ |
159 | /* cpu state */ |
143 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
160 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
144 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
161 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
145 | JMPENV *top_env; |
162 | JMPENV *top_env; |
146 | coro_context cctx; |
163 | coro_context cctx; |
147 | |
164 | |
148 | int inuse; |
|
|
149 | |
|
|
150 | #if USE_VALGRIND |
165 | #if CORO_USE_VALGRIND |
151 | int valgrind_id; |
166 | int valgrind_id; |
152 | #endif |
167 | #endif |
|
|
168 | unsigned char flags; |
153 | } coro_cctx; |
169 | } coro_cctx; |
154 | |
170 | |
155 | enum { |
171 | enum { |
156 | CF_RUNNING = 0x0001, /* coroutine is running */ |
172 | CF_RUNNING = 0x0001, /* coroutine is running */ |
157 | CF_READY = 0x0002, /* coroutine is ready */ |
173 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
172 | |
188 | |
173 | /* optionally saved, might be zero */ |
189 | /* optionally saved, might be zero */ |
174 | AV *defav; /* @_ */ |
190 | AV *defav; /* @_ */ |
175 | SV *defsv; /* $_ */ |
191 | SV *defsv; /* $_ */ |
176 | SV *errsv; /* $@ */ |
192 | SV *errsv; /* $@ */ |
|
|
193 | GV *deffh; /* default filehandle */ |
177 | SV *irssv; /* $/ */ |
194 | SV *irssv; /* $/ */ |
178 | SV *irssv_sv; /* real $/ cache */ |
195 | SV *irssv_sv; /* real $/ cache */ |
179 | |
196 | |
180 | #define VAR(name,type) type name; |
197 | #define VAR(name,type) type name; |
181 | # include "state.h" |
198 | # include "state.h" |
182 | #undef VAR |
199 | #undef VAR |
183 | |
200 | |
184 | /* coro process data */ |
201 | /* coro process data */ |
185 | int prio; |
202 | int prio; |
|
|
203 | |
|
|
204 | /* linked list */ |
|
|
205 | struct coro *next, *prev; |
|
|
206 | HV *hv; /* the perl hash associated with this coro, if any */ |
186 | }; |
207 | }; |
187 | |
208 | |
188 | typedef struct coro *Coro__State; |
209 | typedef struct coro *Coro__State; |
189 | typedef struct coro *Coro__State_or_hashref; |
210 | typedef struct coro *Coro__State_or_hashref; |
190 | |
211 | |
… | |
… | |
199 | |
220 | |
200 | /* for Coro.pm */ |
221 | /* for Coro.pm */ |
201 | static SV *coro_current; |
222 | static SV *coro_current; |
202 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
223 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
203 | static int coro_nready; |
224 | static int coro_nready; |
|
|
225 | static struct coro *coro_first; |
204 | |
226 | |
205 | /** lowlevel stuff **********************************************************/ |
227 | /** lowlevel stuff **********************************************************/ |
206 | |
228 | |
207 | static AV * |
229 | static AV * |
208 | coro_clone_padlist (CV *cv) |
230 | coro_clone_padlist (pTHX_ CV *cv) |
209 | { |
231 | { |
210 | AV *padlist = CvPADLIST (cv); |
232 | AV *padlist = CvPADLIST (cv); |
211 | AV *newpadlist, *newpad; |
233 | AV *newpadlist, *newpad; |
212 | |
234 | |
213 | newpadlist = newAV (); |
235 | newpadlist = newAV (); |
… | |
… | |
225 | |
247 | |
226 | return newpadlist; |
248 | return newpadlist; |
227 | } |
249 | } |
228 | |
250 | |
229 | static void |
251 | static void |
230 | free_padlist (AV *padlist) |
252 | free_padlist (pTHX_ AV *padlist) |
231 | { |
253 | { |
232 | /* may be during global destruction */ |
254 | /* may be during global destruction */ |
233 | if (SvREFCNT (padlist)) |
255 | if (SvREFCNT (padlist)) |
234 | { |
256 | { |
235 | I32 i = AvFILLp (padlist); |
257 | I32 i = AvFILLp (padlist); |
… | |
… | |
256 | AV *padlist; |
278 | AV *padlist; |
257 | AV *av = (AV *)mg->mg_obj; |
279 | AV *av = (AV *)mg->mg_obj; |
258 | |
280 | |
259 | /* casting is fun. */ |
281 | /* casting is fun. */ |
260 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
282 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
261 | free_padlist (padlist); |
283 | free_padlist (aTHX_ padlist); |
262 | |
284 | |
263 | SvREFCNT_dec (av); |
285 | SvREFCNT_dec (av); |
264 | |
286 | |
265 | return 0; |
287 | return 0; |
266 | } |
288 | } |
… | |
… | |
276 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
298 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
277 | : 0 |
299 | : 0 |
278 | |
300 | |
279 | /* the next two functions merely cache the padlists */ |
301 | /* the next two functions merely cache the padlists */ |
280 | static void |
302 | static void |
281 | get_padlist (CV *cv) |
303 | get_padlist (pTHX_ CV *cv) |
282 | { |
304 | { |
283 | MAGIC *mg = CORO_MAGIC (cv); |
305 | MAGIC *mg = CORO_MAGIC (cv); |
284 | AV *av; |
306 | AV *av; |
285 | |
307 | |
286 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
308 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
287 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
309 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
288 | else |
310 | else |
289 | { |
311 | { |
290 | #if PREFER_PERL_FUNCTIONS |
312 | #if CORO_PREFER_PERL_FUNCTIONS |
291 | /* this is probably cleaner, but also slower? */ |
313 | /* this is probably cleaner, but also slower? */ |
292 | CV *cp = Perl_cv_clone (cv); |
314 | CV *cp = Perl_cv_clone (cv); |
293 | CvPADLIST (cv) = CvPADLIST (cp); |
315 | CvPADLIST (cv) = CvPADLIST (cp); |
294 | CvPADLIST (cp) = 0; |
316 | CvPADLIST (cp) = 0; |
295 | SvREFCNT_dec (cp); |
317 | SvREFCNT_dec (cp); |
296 | #else |
318 | #else |
297 | CvPADLIST (cv) = coro_clone_padlist (cv); |
319 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
298 | #endif |
320 | #endif |
299 | } |
321 | } |
300 | } |
322 | } |
301 | |
323 | |
302 | static void |
324 | static void |
303 | put_padlist (CV *cv) |
325 | put_padlist (pTHX_ CV *cv) |
304 | { |
326 | { |
305 | MAGIC *mg = CORO_MAGIC (cv); |
327 | MAGIC *mg = CORO_MAGIC (cv); |
306 | AV *av; |
328 | AV *av; |
307 | |
329 | |
308 | if (!mg) |
330 | if (!mg) |
… | |
… | |
327 | #define SE } while (0) |
349 | #define SE } while (0) |
328 | |
350 | |
329 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
351 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
330 | |
352 | |
331 | static void |
353 | static void |
332 | load_perl (Coro__State c) |
354 | load_perl (pTHX_ Coro__State c) |
333 | { |
355 | { |
334 | #define VAR(name,type) PL_ ## name = c->name; |
356 | #define VAR(name,type) PL_ ## name = c->name; |
335 | # include "state.h" |
357 | # include "state.h" |
336 | #undef VAR |
358 | #undef VAR |
337 | |
359 | |
338 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
360 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
339 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
361 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
340 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
362 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
363 | if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); |
|
|
364 | |
341 | if (c->irssv) |
365 | if (c->irssv) |
342 | { |
366 | { |
343 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
367 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
368 | { |
344 | SvREFCNT_dec (c->irssv); |
369 | SvREFCNT_dec (c->irssv); |
|
|
370 | c->irssv = 0; |
|
|
371 | } |
345 | else |
372 | else |
346 | { |
373 | { |
347 | REPLACE_SV (PL_rs, c->irssv); |
374 | REPLACE_SV (PL_rs, c->irssv); |
348 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
375 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
349 | sv_setsv (c->irssv_sv, PL_rs); |
376 | sv_setsv (c->irssv_sv, PL_rs); |
… | |
… | |
355 | CV *cv; |
382 | CV *cv; |
356 | |
383 | |
357 | /* now do the ugly restore mess */ |
384 | /* now do the ugly restore mess */ |
358 | while ((cv = (CV *)POPs)) |
385 | while ((cv = (CV *)POPs)) |
359 | { |
386 | { |
360 | put_padlist (cv); /* mark this padlist as available */ |
387 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
361 | CvDEPTH (cv) = PTR2IV (POPs); |
388 | CvDEPTH (cv) = PTR2IV (POPs); |
362 | CvPADLIST (cv) = (AV *)POPs; |
389 | CvPADLIST (cv) = (AV *)POPs; |
363 | } |
390 | } |
364 | |
391 | |
365 | PUTBACK; |
392 | PUTBACK; |
366 | } |
393 | } |
367 | } |
394 | } |
368 | |
395 | |
369 | static void |
396 | static void |
370 | save_perl (Coro__State c) |
397 | save_perl (pTHX_ Coro__State c) |
371 | { |
398 | { |
372 | { |
399 | { |
373 | dSP; |
400 | dSP; |
374 | I32 cxix = cxstack_ix; |
401 | I32 cxix = cxstack_ix; |
375 | PERL_CONTEXT *ccstk = cxstack; |
402 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
378 | /* |
405 | /* |
379 | * the worst thing you can imagine happens first - we have to save |
406 | * the worst thing you can imagine happens first - we have to save |
380 | * (and reinitialize) all cv's in the whole callchain :( |
407 | * (and reinitialize) all cv's in the whole callchain :( |
381 | */ |
408 | */ |
382 | |
409 | |
383 | EXTEND (SP, 3 + 1); |
|
|
384 | PUSHs (Nullsv); |
410 | XPUSHs (Nullsv); |
385 | /* this loop was inspired by pp_caller */ |
411 | /* this loop was inspired by pp_caller */ |
386 | for (;;) |
412 | for (;;) |
387 | { |
413 | { |
388 | while (cxix >= 0) |
414 | while (cxix >= 0) |
389 | { |
415 | { |
390 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
416 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
391 | |
417 | |
392 | if (CxTYPE (cx) == CXt_SUB) |
418 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
393 | { |
419 | { |
394 | CV *cv = cx->blk_sub.cv; |
420 | CV *cv = cx->blk_sub.cv; |
395 | |
421 | |
396 | if (CvDEPTH (cv)) |
422 | if (CvDEPTH (cv)) |
397 | { |
423 | { |
… | |
… | |
399 | PUSHs ((SV *)CvPADLIST (cv)); |
425 | PUSHs ((SV *)CvPADLIST (cv)); |
400 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
426 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
401 | PUSHs ((SV *)cv); |
427 | PUSHs ((SV *)cv); |
402 | |
428 | |
403 | CvDEPTH (cv) = 0; |
429 | CvDEPTH (cv) = 0; |
404 | get_padlist (cv); |
430 | get_padlist (aTHX_ cv); |
405 | } |
431 | } |
406 | } |
432 | } |
407 | } |
433 | } |
408 | |
434 | |
409 | if (top_si->si_type == PERLSI_MAIN) |
435 | if (top_si->si_type == PERLSI_MAIN) |
410 | break; |
436 | break; |
411 | |
437 | |
412 | top_si = top_si->si_prev; |
438 | top_si = top_si->si_prev; |
413 | ccstk = top_si->si_cxstack; |
439 | ccstk = top_si->si_cxstack; |
414 | cxix = top_si->si_cxix; |
440 | cxix = top_si->si_cxix; |
415 | } |
441 | } |
416 | |
442 | |
417 | PUTBACK; |
443 | PUTBACK; |
418 | } |
444 | } |
419 | |
445 | |
420 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
446 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
421 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
447 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
422 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
448 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
449 | c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; |
423 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
450 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
424 | |
451 | |
425 | #define VAR(name,type)c->name = PL_ ## name; |
452 | #define VAR(name,type)c->name = PL_ ## name; |
426 | # include "state.h" |
453 | # include "state.h" |
427 | #undef VAR |
454 | #undef VAR |
… | |
… | |
431 | * allocate various perl stacks. This is an exact copy |
458 | * allocate various perl stacks. This is an exact copy |
432 | * of perl.c:init_stacks, except that it uses less memory |
459 | * of perl.c:init_stacks, except that it uses less memory |
433 | * on the (sometimes correct) assumption that coroutines do |
460 | * on the (sometimes correct) assumption that coroutines do |
434 | * not usually need a lot of stackspace. |
461 | * not usually need a lot of stackspace. |
435 | */ |
462 | */ |
436 | #if PREFER_PERL_FUNCTIONS |
463 | #if CORO_PREFER_PERL_FUNCTIONS |
437 | # define coro_init_stacks init_stacks |
464 | # define coro_init_stacks init_stacks |
438 | #else |
465 | #else |
439 | static void |
466 | static void |
440 | coro_init_stacks () |
467 | coro_init_stacks (pTHX) |
441 | { |
468 | { |
442 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
469 | PL_curstackinfo = new_stackinfo(64, 6); |
443 | PL_curstackinfo->si_type = PERLSI_MAIN; |
470 | PL_curstackinfo->si_type = PERLSI_MAIN; |
444 | PL_curstack = PL_curstackinfo->si_stack; |
471 | PL_curstack = PL_curstackinfo->si_stack; |
445 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
472 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
446 | |
473 | |
447 | PL_stack_base = AvARRAY(PL_curstack); |
474 | PL_stack_base = AvARRAY(PL_curstack); |
448 | PL_stack_sp = PL_stack_base; |
475 | PL_stack_sp = PL_stack_base; |
449 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
476 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
450 | |
477 | |
451 | New(50,PL_tmps_stack,128,SV*); |
478 | New(50,PL_tmps_stack,64,SV*); |
452 | PL_tmps_floor = -1; |
479 | PL_tmps_floor = -1; |
453 | PL_tmps_ix = -1; |
480 | PL_tmps_ix = -1; |
454 | PL_tmps_max = 128; |
481 | PL_tmps_max = 64; |
455 | |
482 | |
456 | New(54,PL_markstack,32,I32); |
483 | New(54,PL_markstack,16,I32); |
457 | PL_markstack_ptr = PL_markstack; |
484 | PL_markstack_ptr = PL_markstack; |
458 | PL_markstack_max = PL_markstack + 32; |
485 | PL_markstack_max = PL_markstack + 16; |
459 | |
486 | |
460 | #ifdef SET_MARK_OFFSET |
487 | #ifdef SET_MARK_OFFSET |
461 | SET_MARK_OFFSET; |
488 | SET_MARK_OFFSET; |
462 | #endif |
489 | #endif |
463 | |
490 | |
464 | New(54,PL_scopestack,32,I32); |
491 | New(54,PL_scopestack,16,I32); |
465 | PL_scopestack_ix = 0; |
492 | PL_scopestack_ix = 0; |
466 | PL_scopestack_max = 32; |
493 | PL_scopestack_max = 16; |
467 | |
494 | |
468 | New(54,PL_savestack,64,ANY); |
495 | New(54,PL_savestack,64,ANY); |
469 | PL_savestack_ix = 0; |
496 | PL_savestack_ix = 0; |
470 | PL_savestack_max = 64; |
497 | PL_savestack_max = 64; |
471 | |
498 | |
472 | #if !PERL_VERSION_ATLEAST (5,9,0) |
499 | #if !PERL_VERSION_ATLEAST (5,9,0) |
473 | New(54,PL_retstack,16,OP*); |
500 | New(54,PL_retstack,4,OP*); |
474 | PL_retstack_ix = 0; |
501 | PL_retstack_ix = 0; |
475 | PL_retstack_max = 16; |
502 | PL_retstack_max = 4; |
476 | #endif |
503 | #endif |
477 | } |
504 | } |
478 | #endif |
505 | #endif |
479 | |
506 | |
480 | /* |
507 | /* |
481 | * destroy the stacks, the callchain etc... |
508 | * destroy the stacks, the callchain etc... |
482 | */ |
509 | */ |
483 | static void |
510 | static void |
484 | coro_destroy_stacks () |
511 | coro_destroy_stacks (pTHX) |
485 | { |
512 | { |
486 | if (!IN_DESTRUCT) |
513 | if (!IN_DESTRUCT) |
487 | { |
514 | { |
488 | /* restore all saved variables and stuff */ |
515 | /* restore all saved variables and stuff */ |
489 | LEAVE_SCOPE (0); |
516 | LEAVE_SCOPE (0); |
… | |
… | |
522 | #if !PERL_VERSION_ATLEAST (5,9,0) |
549 | #if !PERL_VERSION_ATLEAST (5,9,0) |
523 | Safefree (PL_retstack); |
550 | Safefree (PL_retstack); |
524 | #endif |
551 | #endif |
525 | } |
552 | } |
526 | |
553 | |
|
|
554 | static size_t |
|
|
555 | coro_rss (struct coro *coro) |
|
|
556 | { |
|
|
557 | size_t rss = sizeof (coro); |
|
|
558 | |
|
|
559 | if (coro->mainstack) |
|
|
560 | { |
|
|
561 | if (coro->flags & CF_RUNNING) |
|
|
562 | { |
|
|
563 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
564 | # include "state.h" |
|
|
565 | #undef VAR |
|
|
566 | } |
|
|
567 | |
|
|
568 | rss += sizeof (coro->curstackinfo); |
|
|
569 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
570 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
571 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
572 | rss += coro->tmps_max * sizeof (SV *); |
|
|
573 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
574 | rss += coro->scopestack_max * sizeof (I32); |
|
|
575 | rss += coro->savestack_max * sizeof (ANY); |
|
|
576 | |
|
|
577 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
578 | rss += coro->retstack_max * sizeof (OP *); |
|
|
579 | #endif |
|
|
580 | } |
|
|
581 | |
|
|
582 | return rss; |
|
|
583 | } |
|
|
584 | |
527 | /** coroutine stack handling ************************************************/ |
585 | /** coroutine stack handling ************************************************/ |
528 | |
586 | |
529 | static void |
587 | static void |
530 | setup_coro (struct coro *coro) |
588 | setup_coro (pTHX_ struct coro *coro) |
531 | { |
589 | { |
532 | /* |
590 | /* |
533 | * emulate part of the perl startup here. |
591 | * emulate part of the perl startup here. |
534 | */ |
592 | */ |
535 | |
|
|
536 | coro_init_stacks (); |
593 | coro_init_stacks (aTHX); |
537 | |
594 | |
538 | PL_curcop = &PL_compiling; |
595 | PL_curcop = &PL_compiling; |
539 | PL_in_eval = EVAL_NULL; |
596 | PL_in_eval = EVAL_NULL; |
|
|
597 | PL_comppad = 0; |
540 | PL_curpm = 0; |
598 | PL_curpm = 0; |
541 | PL_localizing = 0; |
599 | PL_localizing = 0; |
542 | PL_dirty = 0; |
600 | PL_dirty = 0; |
543 | PL_restartop = 0; |
601 | PL_restartop = 0; |
544 | |
602 | |
… | |
… | |
552 | Zero (&myop, 1, LOGOP); |
610 | Zero (&myop, 1, LOGOP); |
553 | myop.op_next = Nullop; |
611 | myop.op_next = Nullop; |
554 | myop.op_flags = OPf_WANT_VOID; |
612 | myop.op_flags = OPf_WANT_VOID; |
555 | |
613 | |
556 | PUSHMARK (SP); |
614 | PUSHMARK (SP); |
557 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
615 | XPUSHs (av_shift (GvAV (PL_defgv))); |
558 | PUTBACK; |
616 | PUTBACK; |
559 | PL_op = (OP *)&myop; |
617 | PL_op = (OP *)&myop; |
560 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
618 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
561 | SPAGAIN; |
619 | SPAGAIN; |
562 | } |
620 | } |
563 | |
621 | |
564 | ENTER; /* necessary e.g. for dounwind */ |
622 | ENTER; /* necessary e.g. for dounwind */ |
565 | } |
623 | } |
566 | |
624 | |
567 | static void |
625 | static void |
568 | free_coro_mortal () |
626 | free_coro_mortal (pTHX) |
569 | { |
627 | { |
570 | if (coro_mortal) |
628 | if (coro_mortal) |
571 | { |
629 | { |
572 | SvREFCNT_dec (coro_mortal); |
630 | SvREFCNT_dec (coro_mortal); |
573 | coro_mortal = 0; |
631 | coro_mortal = 0; |
574 | } |
632 | } |
575 | } |
633 | } |
576 | |
634 | |
|
|
635 | static void |
|
|
636 | do_trace (pTHX) |
|
|
637 | { |
|
|
638 | if (PL_curcop != &PL_compiling) |
|
|
639 | { |
|
|
640 | runops_proc_t old_runops = PL_runops; |
|
|
641 | dSP; |
|
|
642 | ENTER; |
|
|
643 | SAVETMPS; |
|
|
644 | PUSHMARK (SP); |
|
|
645 | PUTBACK; |
|
|
646 | PL_runops = RUNOPS_DEFAULT; |
|
|
647 | call_pv ("Coro::_do_trace", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD | G_NOARGS); |
|
|
648 | PL_runops = old_runops; |
|
|
649 | SPAGAIN; |
|
|
650 | FREETMPS; |
|
|
651 | LEAVE; |
|
|
652 | PUTBACK; |
|
|
653 | } |
|
|
654 | } |
|
|
655 | |
|
|
656 | static int |
|
|
657 | runops_coro (pTHX) |
|
|
658 | { |
|
|
659 | COP *oldcop = 0; |
|
|
660 | |
|
|
661 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
662 | { |
|
|
663 | PERL_ASYNC_CHECK (); |
|
|
664 | |
|
|
665 | if (oldcop != PL_curcop) |
|
|
666 | { |
|
|
667 | oldcop = PL_curcop; |
|
|
668 | do_trace (aTHX); |
|
|
669 | } |
|
|
670 | } |
|
|
671 | |
|
|
672 | TAINT_NOT; |
|
|
673 | return 0; |
|
|
674 | } |
|
|
675 | |
577 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
676 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
|
|
677 | /* _cctx_init should be careful, as it could be called at almost any time */ |
|
|
678 | /* during execution of a perl program */ |
578 | static void NOINLINE |
679 | static void NOINLINE |
579 | prepare_cctx (coro_cctx *cctx) |
680 | prepare_cctx (pTHX_ coro_cctx *cctx) |
580 | { |
681 | { |
581 | dSP; |
682 | dSP; |
582 | LOGOP myop; |
683 | LOGOP myop; |
|
|
684 | |
|
|
685 | PL_top_env = &PL_start_env; |
|
|
686 | |
|
|
687 | if (cctx->flags & CC_TRACE) |
|
|
688 | PL_runops = runops_coro; |
583 | |
689 | |
584 | Zero (&myop, 1, LOGOP); |
690 | Zero (&myop, 1, LOGOP); |
585 | myop.op_next = PL_op; |
691 | myop.op_next = PL_op; |
586 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
692 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
587 | |
693 | |
… | |
… | |
593 | PL_op = (OP *)&myop; |
699 | PL_op = (OP *)&myop; |
594 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
700 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
595 | SPAGAIN; |
701 | SPAGAIN; |
596 | } |
702 | } |
597 | |
703 | |
|
|
704 | /* |
|
|
705 | * this is a _very_ stripped down perl interpreter ;) |
|
|
706 | */ |
598 | static void |
707 | static void |
599 | coro_run (void *arg) |
708 | coro_run (void *arg) |
600 | { |
709 | { |
|
|
710 | dTHX; |
|
|
711 | |
601 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
712 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
602 | UNLOCK; |
713 | UNLOCK; |
603 | |
714 | |
604 | /* |
715 | /* we now skip the entersub that lead to transfer() */ |
605 | * this is a _very_ stripped down perl interpreter ;) |
716 | PL_op = PL_op->op_next; |
606 | */ |
|
|
607 | PL_top_env = &PL_start_env; |
|
|
608 | |
717 | |
609 | /* inject call to cctx_init */ |
718 | /* inject a fake subroutine call to cctx_init */ |
610 | prepare_cctx ((coro_cctx *)arg); |
719 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
611 | |
720 | |
612 | /* somebody will hit me for both perl_run and PL_restartop */ |
721 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
613 | PL_restartop = PL_op; |
722 | PL_restartop = PL_op; |
614 | perl_run (PL_curinterp); |
723 | perl_run (PL_curinterp); |
615 | |
724 | |
616 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
725 | /* |
617 | abort (); |
726 | * If perl-run returns we assume exit() was being called or the coro |
|
|
727 | * fell off the end, which seems to be the only valid (non-bug) |
|
|
728 | * reason for perl_run to return. We try to exit by jumping to the |
|
|
729 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
|
|
730 | * coroutine as Coro has no such concept |
|
|
731 | */ |
|
|
732 | PL_top_env = main_top_env; |
|
|
733 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
618 | } |
734 | } |
619 | |
735 | |
620 | static coro_cctx * |
736 | static coro_cctx * |
621 | cctx_new () |
737 | cctx_new () |
622 | { |
738 | { |
623 | coro_cctx *cctx; |
739 | coro_cctx *cctx; |
|
|
740 | void *stack_start; |
|
|
741 | size_t stack_size; |
624 | |
742 | |
625 | ++cctx_count; |
743 | ++cctx_count; |
626 | |
744 | |
627 | Newz (0, cctx, 1, coro_cctx); |
745 | Newz (0, cctx, 1, coro_cctx); |
628 | |
746 | |
629 | #if HAVE_MMAP |
747 | #if HAVE_MMAP |
630 | |
748 | |
631 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
749 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
632 | /* mmap supposedly does allocate-on-write for us */ |
750 | /* mmap supposedly does allocate-on-write for us */ |
633 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
751 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
634 | |
752 | |
635 | if (cctx->sptr == (void *)-1) |
753 | if (cctx->sptr != (void *)-1) |
636 | { |
|
|
637 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
638 | _exit (EXIT_FAILURE); |
|
|
639 | } |
754 | { |
640 | |
|
|
641 | # if STACKGUARD |
755 | # if CORO_STACKGUARD |
642 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
756 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
643 | # endif |
757 | # endif |
|
|
758 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
|
|
759 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
760 | cctx->flags |= CC_MAPPED; |
|
|
761 | } |
|
|
762 | else |
|
|
763 | #endif |
|
|
764 | { |
|
|
765 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
|
|
766 | New (0, cctx->sptr, coro_stacksize, long); |
644 | |
767 | |
645 | #else |
|
|
646 | |
|
|
647 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
648 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
649 | |
|
|
650 | if (!cctx->sptr) |
768 | if (!cctx->sptr) |
651 | { |
769 | { |
652 | perror ("FATAL: unable to malloc stack for coroutine"); |
770 | perror ("FATAL: unable to allocate stack for coroutine"); |
653 | _exit (EXIT_FAILURE); |
771 | _exit (EXIT_FAILURE); |
654 | } |
772 | } |
655 | |
773 | |
656 | #endif |
774 | stack_start = cctx->sptr; |
|
|
775 | stack_size = cctx->ssize; |
|
|
776 | } |
657 | |
777 | |
658 | #if USE_VALGRIND |
778 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
659 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
660 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
661 | cctx->ssize + (char *)cctx->sptr |
|
|
662 | ); |
|
|
663 | #endif |
|
|
664 | |
|
|
665 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
779 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
666 | |
780 | |
667 | return cctx; |
781 | return cctx; |
668 | } |
782 | } |
669 | |
783 | |
670 | static void |
784 | static void |
… | |
… | |
673 | if (!cctx) |
787 | if (!cctx) |
674 | return; |
788 | return; |
675 | |
789 | |
676 | --cctx_count; |
790 | --cctx_count; |
677 | |
791 | |
678 | #if USE_VALGRIND |
792 | #if CORO_USE_VALGRIND |
679 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
793 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
680 | #endif |
794 | #endif |
681 | |
795 | |
682 | #if HAVE_MMAP |
796 | #if HAVE_MMAP |
|
|
797 | if (cctx->flags & CC_MAPPED) |
683 | munmap (cctx->sptr, cctx->ssize); |
798 | munmap (cctx->sptr, cctx->ssize); |
684 | #else |
799 | else |
|
|
800 | #endif |
685 | Safefree (cctx->sptr); |
801 | Safefree (cctx->sptr); |
686 | #endif |
|
|
687 | |
802 | |
688 | Safefree (cctx); |
803 | Safefree (cctx); |
689 | } |
804 | } |
690 | |
805 | |
691 | static coro_cctx * |
806 | static coro_cctx * |
692 | cctx_get () |
807 | cctx_get (pTHX) |
693 | { |
808 | { |
694 | coro_cctx *cctx; |
|
|
695 | |
|
|
696 | if (cctx_first) |
809 | while (cctx_first) |
697 | { |
810 | { |
698 | cctx = cctx_first; |
811 | coro_cctx *cctx = cctx_first; |
699 | cctx_first = cctx->next; |
812 | cctx_first = cctx->next; |
700 | --cctx_idle; |
813 | --cctx_idle; |
701 | } |
814 | |
702 | else |
815 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
703 | { |
816 | return cctx; |
704 | cctx = cctx_new (); |
817 | |
705 | PL_op = PL_op->op_next; |
818 | cctx_destroy (cctx); |
706 | } |
819 | } |
707 | |
820 | |
708 | return cctx; |
821 | return cctx_new (); |
709 | } |
822 | } |
710 | |
823 | |
711 | static void |
824 | static void |
712 | cctx_put (coro_cctx *cctx) |
825 | cctx_put (coro_cctx *cctx) |
713 | { |
826 | { |
… | |
… | |
716 | { |
829 | { |
717 | coro_cctx *first = cctx_first; |
830 | coro_cctx *first = cctx_first; |
718 | cctx_first = first->next; |
831 | cctx_first = first->next; |
719 | --cctx_idle; |
832 | --cctx_idle; |
720 | |
833 | |
721 | assert (!first->inuse); |
|
|
722 | cctx_destroy (first); |
834 | cctx_destroy (first); |
723 | } |
835 | } |
724 | |
836 | |
725 | ++cctx_idle; |
837 | ++cctx_idle; |
726 | cctx->next = cctx_first; |
838 | cctx->next = cctx_first; |
… | |
… | |
729 | |
841 | |
730 | /** coroutine switching *****************************************************/ |
842 | /** coroutine switching *****************************************************/ |
731 | |
843 | |
732 | /* never call directly, always through the coro_state_transfer global variable */ |
844 | /* never call directly, always through the coro_state_transfer global variable */ |
733 | static void NOINLINE |
845 | static void NOINLINE |
734 | transfer (struct coro *prev, struct coro *next) |
846 | transfer (pTHX_ struct coro *prev, struct coro *next) |
735 | { |
847 | { |
736 | dSTACKLEVEL; |
848 | dSTACKLEVEL; |
737 | |
849 | |
738 | /* sometimes transfer is only called to set idle_sp */ |
850 | /* sometimes transfer is only called to set idle_sp */ |
739 | if (!next) |
851 | if (!next) |
… | |
… | |
747 | |
859 | |
748 | if (prev->flags & CF_NEW) |
860 | if (prev->flags & CF_NEW) |
749 | { |
861 | { |
750 | /* create a new empty context */ |
862 | /* create a new empty context */ |
751 | Newz (0, prev->cctx, 1, coro_cctx); |
863 | Newz (0, prev->cctx, 1, coro_cctx); |
752 | prev->cctx->inuse = 1; |
|
|
753 | prev->flags &= ~CF_NEW; |
864 | prev->flags &= ~CF_NEW; |
754 | prev->flags |= CF_RUNNING; |
865 | prev->flags |= CF_RUNNING; |
755 | } |
866 | } |
756 | |
867 | |
757 | /*TODO: must not croak here */ |
868 | /*TODO: must not croak here */ |
… | |
… | |
772 | if (next->flags & CF_NEW) |
883 | if (next->flags & CF_NEW) |
773 | { |
884 | { |
774 | /* need to start coroutine */ |
885 | /* need to start coroutine */ |
775 | next->flags &= ~CF_NEW; |
886 | next->flags &= ~CF_NEW; |
776 | /* first get rid of the old state */ |
887 | /* first get rid of the old state */ |
777 | save_perl (prev); |
888 | save_perl (aTHX_ prev); |
778 | /* setup coroutine call */ |
889 | /* setup coroutine call */ |
779 | setup_coro (next); |
890 | setup_coro (aTHX_ next); |
780 | /* need a new stack */ |
|
|
781 | assert (!next->cctx); |
|
|
782 | } |
891 | } |
783 | else |
892 | else |
784 | { |
893 | { |
785 | /* coroutine already started */ |
894 | /* coroutine already started */ |
786 | save_perl (prev); |
895 | save_perl (aTHX_ prev); |
787 | load_perl (next); |
896 | load_perl (aTHX_ next); |
788 | } |
897 | } |
789 | |
898 | |
790 | prev__cctx = prev->cctx; |
899 | prev__cctx = prev->cctx; |
791 | |
900 | |
792 | /* possibly "free" the cctx */ |
901 | /* possibly "free" the cctx */ |
… | |
… | |
796 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
905 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
797 | |
906 | |
798 | prev->cctx = 0; |
907 | prev->cctx = 0; |
799 | |
908 | |
800 | cctx_put (prev__cctx); |
909 | cctx_put (prev__cctx); |
801 | prev__cctx->inuse = 0; |
|
|
802 | } |
910 | } |
803 | |
911 | |
804 | if (!next->cctx) |
912 | if (!next->cctx) |
805 | { |
|
|
806 | next->cctx = cctx_get (); |
913 | next->cctx = cctx_get (aTHX); |
807 | assert (!next->cctx->inuse); |
|
|
808 | next->cctx->inuse = 1; |
|
|
809 | } |
|
|
810 | |
914 | |
811 | if (prev__cctx != next->cctx) |
915 | if (prev__cctx != next->cctx) |
812 | { |
916 | { |
813 | prev__cctx->top_env = PL_top_env; |
917 | prev__cctx->top_env = PL_top_env; |
814 | PL_top_env = next->cctx->top_env; |
918 | PL_top_env = next->cctx->top_env; |
815 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
919 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
816 | } |
920 | } |
817 | |
921 | |
818 | free_coro_mortal (); |
922 | free_coro_mortal (aTHX); |
819 | UNLOCK; |
923 | UNLOCK; |
820 | } |
924 | } |
821 | } |
925 | } |
822 | |
926 | |
823 | struct transfer_args |
927 | struct transfer_args |
824 | { |
928 | { |
825 | struct coro *prev, *next; |
929 | struct coro *prev, *next; |
826 | }; |
930 | }; |
827 | |
931 | |
828 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
932 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
829 | |
933 | |
830 | /** high level stuff ********************************************************/ |
934 | /** high level stuff ********************************************************/ |
831 | |
935 | |
832 | static int |
936 | static int |
833 | coro_state_destroy (struct coro *coro) |
937 | coro_state_destroy (pTHX_ struct coro *coro) |
834 | { |
938 | { |
835 | if (coro->flags & CF_DESTROYED) |
939 | if (coro->flags & CF_DESTROYED) |
836 | return 0; |
940 | return 0; |
837 | |
941 | |
838 | coro->flags |= CF_DESTROYED; |
942 | coro->flags |= CF_DESTROYED; |
… | |
… | |
848 | else |
952 | else |
849 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
953 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
850 | |
954 | |
851 | if (coro->mainstack && coro->mainstack != main_mainstack) |
955 | if (coro->mainstack && coro->mainstack != main_mainstack) |
852 | { |
956 | { |
|
|
957 | struct coro temp; |
|
|
958 | |
853 | assert (!(coro->flags & CF_RUNNING)); |
959 | assert (!(coro->flags & CF_RUNNING)); |
854 | |
960 | |
855 | struct coro temp; |
|
|
856 | Zero (&temp, 1, struct coro); |
961 | Zero (&temp, 1, struct coro); |
857 | temp.save = CORO_SAVE_ALL; |
962 | temp.save = CORO_SAVE_ALL; |
858 | |
963 | |
859 | if (coro->flags & CF_RUNNING) |
964 | if (coro->flags & CF_RUNNING) |
860 | croak ("FATAL: tried to destroy currently running coroutine"); |
965 | croak ("FATAL: tried to destroy currently running coroutine"); |
861 | |
966 | |
862 | save_perl (&temp); |
967 | save_perl (aTHX_ &temp); |
863 | load_perl (coro); |
968 | load_perl (aTHX_ coro); |
864 | |
969 | |
865 | coro_destroy_stacks (); |
970 | coro_destroy_stacks (aTHX); |
866 | |
971 | |
867 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
972 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
868 | |
973 | |
869 | coro->mainstack = 0; |
974 | coro->mainstack = 0; |
870 | } |
975 | } |
871 | |
976 | |
872 | cctx_destroy (coro->cctx); |
977 | cctx_destroy (coro->cctx); |
873 | SvREFCNT_dec (coro->args); |
978 | SvREFCNT_dec (coro->args); |
874 | |
979 | |
|
|
980 | if (coro->next) coro->next->prev = coro->prev; |
|
|
981 | if (coro->prev) coro->prev->next = coro->next; |
|
|
982 | if (coro == coro_first) coro_first = coro->next; |
|
|
983 | |
875 | return 1; |
984 | return 1; |
876 | } |
985 | } |
877 | |
986 | |
878 | static int |
987 | static int |
879 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
988 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
880 | { |
989 | { |
881 | struct coro *coro = (struct coro *)mg->mg_ptr; |
990 | struct coro *coro = (struct coro *)mg->mg_ptr; |
882 | mg->mg_ptr = 0; |
991 | mg->mg_ptr = 0; |
883 | |
992 | |
|
|
993 | coro->hv = 0; |
|
|
994 | |
884 | if (--coro->refcnt < 0) |
995 | if (--coro->refcnt < 0) |
885 | { |
996 | { |
886 | coro_state_destroy (coro); |
997 | coro_state_destroy (aTHX_ coro); |
887 | Safefree (coro); |
998 | Safefree (coro); |
888 | } |
999 | } |
889 | |
1000 | |
890 | return 0; |
1001 | return 0; |
891 | } |
1002 | } |
… | |
… | |
910 | # define MGf_DUP 0 |
1021 | # define MGf_DUP 0 |
911 | #endif |
1022 | #endif |
912 | }; |
1023 | }; |
913 | |
1024 | |
914 | static struct coro * |
1025 | static struct coro * |
915 | SvSTATE (SV *coro) |
1026 | SvSTATE_ (pTHX_ SV *coro) |
916 | { |
1027 | { |
917 | HV *stash; |
1028 | HV *stash; |
918 | MAGIC *mg; |
1029 | MAGIC *mg; |
919 | |
1030 | |
920 | if (SvROK (coro)) |
1031 | if (SvROK (coro)) |
… | |
… | |
926 | /* very slow, but rare, check */ |
1037 | /* very slow, but rare, check */ |
927 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
1038 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
928 | croak ("Coro::State object required"); |
1039 | croak ("Coro::State object required"); |
929 | } |
1040 | } |
930 | |
1041 | |
931 | mg = SvMAGIC (coro); |
1042 | mg = CORO_MAGIC (coro); |
932 | assert (mg->mg_type == PERL_MAGIC_ext); |
|
|
933 | return (struct coro *)mg->mg_ptr; |
1043 | return (struct coro *)mg->mg_ptr; |
934 | } |
1044 | } |
935 | |
1045 | |
|
|
1046 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
1047 | |
936 | static void |
1048 | static void |
937 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1049 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
938 | { |
1050 | { |
939 | ta->prev = SvSTATE (prev_sv); |
1051 | ta->prev = SvSTATE (prev_sv); |
940 | ta->next = SvSTATE (next_sv); |
1052 | ta->next = SvSTATE (next_sv); |
941 | } |
1053 | } |
942 | |
1054 | |
943 | static void |
1055 | static void |
944 | api_transfer (SV *prev_sv, SV *next_sv) |
1056 | api_transfer (SV *prev_sv, SV *next_sv) |
945 | { |
1057 | { |
|
|
1058 | dTHX; |
946 | struct transfer_args ta; |
1059 | struct transfer_args ta; |
947 | |
1060 | |
948 | prepare_transfer (&ta, prev_sv, next_sv); |
1061 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
949 | TRANSFER (ta); |
1062 | TRANSFER (ta); |
950 | } |
1063 | } |
951 | |
1064 | |
952 | static int |
1065 | static int |
953 | api_save (SV *coro_sv, int new_save) |
1066 | api_save (SV *coro_sv, int new_save) |
954 | { |
1067 | { |
|
|
1068 | dTHX; |
955 | struct coro *coro = SvSTATE (coro_sv); |
1069 | struct coro *coro = SvSTATE (coro_sv); |
956 | int old_save = coro->save; |
1070 | int old_save = coro->save; |
957 | |
1071 | |
958 | if (new_save >= 0) |
1072 | if (new_save >= 0) |
959 | coro->save = new_save; |
1073 | coro->save = new_save; |
… | |
… | |
962 | } |
1076 | } |
963 | |
1077 | |
964 | /** Coro ********************************************************************/ |
1078 | /** Coro ********************************************************************/ |
965 | |
1079 | |
966 | static void |
1080 | static void |
967 | coro_enq (SV *coro_sv) |
1081 | coro_enq (pTHX_ SV *coro_sv) |
968 | { |
1082 | { |
969 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1083 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
970 | } |
1084 | } |
971 | |
1085 | |
972 | static SV * |
1086 | static SV * |
973 | coro_deq (int min_prio) |
1087 | coro_deq (pTHX_ int min_prio) |
974 | { |
1088 | { |
975 | int prio = PRIO_MAX - PRIO_MIN; |
1089 | int prio = PRIO_MAX - PRIO_MIN; |
976 | |
1090 | |
977 | min_prio -= PRIO_MIN; |
1091 | min_prio -= PRIO_MIN; |
978 | if (min_prio < 0) |
1092 | if (min_prio < 0) |
… | |
… | |
986 | } |
1100 | } |
987 | |
1101 | |
988 | static int |
1102 | static int |
989 | api_ready (SV *coro_sv) |
1103 | api_ready (SV *coro_sv) |
990 | { |
1104 | { |
|
|
1105 | dTHX; |
991 | struct coro *coro; |
1106 | struct coro *coro; |
992 | |
1107 | |
993 | if (SvROK (coro_sv)) |
1108 | if (SvROK (coro_sv)) |
994 | coro_sv = SvRV (coro_sv); |
1109 | coro_sv = SvRV (coro_sv); |
995 | |
1110 | |
… | |
… | |
999 | return 0; |
1114 | return 0; |
1000 | |
1115 | |
1001 | coro->flags |= CF_READY; |
1116 | coro->flags |= CF_READY; |
1002 | |
1117 | |
1003 | LOCK; |
1118 | LOCK; |
1004 | coro_enq (SvREFCNT_inc (coro_sv)); |
1119 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1005 | ++coro_nready; |
1120 | ++coro_nready; |
1006 | UNLOCK; |
1121 | UNLOCK; |
1007 | |
1122 | |
1008 | return 1; |
1123 | return 1; |
1009 | } |
1124 | } |
1010 | |
1125 | |
1011 | static int |
1126 | static int |
1012 | api_is_ready (SV *coro_sv) |
1127 | api_is_ready (SV *coro_sv) |
1013 | { |
1128 | { |
|
|
1129 | dTHX; |
1014 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1130 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1015 | } |
1131 | } |
1016 | |
1132 | |
1017 | static void |
1133 | static void |
1018 | prepare_schedule (struct transfer_args *ta) |
1134 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1019 | { |
1135 | { |
1020 | SV *prev_sv, *next_sv; |
1136 | SV *prev_sv, *next_sv; |
1021 | |
1137 | |
1022 | for (;;) |
1138 | for (;;) |
1023 | { |
1139 | { |
1024 | LOCK; |
1140 | LOCK; |
1025 | next_sv = coro_deq (PRIO_MIN); |
1141 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1026 | |
1142 | |
1027 | /* nothing to schedule: call the idle handler */ |
1143 | /* nothing to schedule: call the idle handler */ |
1028 | if (!next_sv) |
1144 | if (!next_sv) |
1029 | { |
1145 | { |
|
|
1146 | dSP; |
1030 | UNLOCK; |
1147 | UNLOCK; |
1031 | dSP; |
|
|
1032 | |
1148 | |
1033 | ENTER; |
1149 | ENTER; |
1034 | SAVETMPS; |
1150 | SAVETMPS; |
1035 | |
1151 | |
1036 | PUSHMARK (SP); |
1152 | PUSHMARK (SP); |
… | |
… | |
1065 | |
1181 | |
1066 | assert (ta->next->flags & CF_READY); |
1182 | assert (ta->next->flags & CF_READY); |
1067 | ta->next->flags &= ~CF_READY; |
1183 | ta->next->flags &= ~CF_READY; |
1068 | |
1184 | |
1069 | LOCK; |
1185 | LOCK; |
1070 | free_coro_mortal (); |
1186 | free_coro_mortal (aTHX); |
1071 | coro_mortal = prev_sv; |
1187 | coro_mortal = prev_sv; |
1072 | UNLOCK; |
1188 | UNLOCK; |
1073 | } |
1189 | } |
1074 | |
1190 | |
1075 | static void |
1191 | static void |
1076 | prepare_cede (struct transfer_args *ta) |
1192 | prepare_cede (pTHX_ struct transfer_args *ta) |
1077 | { |
1193 | { |
1078 | api_ready (coro_current); |
1194 | api_ready (coro_current); |
1079 | prepare_schedule (ta); |
1195 | prepare_schedule (aTHX_ ta); |
1080 | } |
1196 | } |
1081 | |
1197 | |
1082 | static int |
1198 | static int |
1083 | prepare_cede_notself (struct transfer_args *ta) |
1199 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1084 | { |
1200 | { |
1085 | if (coro_nready) |
1201 | if (coro_nready) |
1086 | { |
1202 | { |
1087 | SV *prev = SvRV (coro_current); |
1203 | SV *prev = SvRV (coro_current); |
1088 | prepare_schedule (ta); |
1204 | prepare_schedule (aTHX_ ta); |
1089 | api_ready (prev); |
1205 | api_ready (prev); |
1090 | return 1; |
1206 | return 1; |
1091 | } |
1207 | } |
1092 | else |
1208 | else |
1093 | return 0; |
1209 | return 0; |
1094 | } |
1210 | } |
1095 | |
1211 | |
1096 | static void |
1212 | static void |
1097 | api_schedule (void) |
1213 | api_schedule (void) |
1098 | { |
1214 | { |
|
|
1215 | dTHX; |
1099 | struct transfer_args ta; |
1216 | struct transfer_args ta; |
1100 | |
1217 | |
1101 | prepare_schedule (&ta); |
1218 | prepare_schedule (aTHX_ &ta); |
1102 | TRANSFER (ta); |
1219 | TRANSFER (ta); |
1103 | } |
1220 | } |
1104 | |
1221 | |
1105 | static int |
1222 | static int |
1106 | api_cede (void) |
1223 | api_cede (void) |
1107 | { |
1224 | { |
|
|
1225 | dTHX; |
1108 | struct transfer_args ta; |
1226 | struct transfer_args ta; |
1109 | |
1227 | |
1110 | prepare_cede (&ta); |
1228 | prepare_cede (aTHX_ &ta); |
1111 | |
1229 | |
1112 | if (ta.prev != ta.next) |
1230 | if (ta.prev != ta.next) |
1113 | { |
1231 | { |
1114 | TRANSFER (ta); |
1232 | TRANSFER (ta); |
1115 | return 1; |
1233 | return 1; |
… | |
… | |
1119 | } |
1237 | } |
1120 | |
1238 | |
1121 | static int |
1239 | static int |
1122 | api_cede_notself (void) |
1240 | api_cede_notself (void) |
1123 | { |
1241 | { |
|
|
1242 | dTHX; |
1124 | struct transfer_args ta; |
1243 | struct transfer_args ta; |
1125 | |
1244 | |
1126 | if (prepare_cede_notself (&ta)) |
1245 | if (prepare_cede_notself (aTHX_ &ta)) |
1127 | { |
1246 | { |
1128 | TRANSFER (ta); |
1247 | TRANSFER (ta); |
1129 | return 1; |
1248 | return 1; |
1130 | } |
1249 | } |
1131 | else |
1250 | else |
… | |
… | |
1147 | |
1266 | |
1148 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1267 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1149 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1268 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1150 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1269 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1151 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1270 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1271 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
|
|
1272 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1152 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1273 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1153 | |
1274 | |
1154 | main_mainstack = PL_mainstack; |
1275 | main_mainstack = PL_mainstack; |
|
|
1276 | main_top_env = PL_top_env; |
|
|
1277 | |
|
|
1278 | while (main_top_env->je_prev) |
|
|
1279 | main_top_env = main_top_env->je_prev; |
1155 | |
1280 | |
1156 | coroapi.ver = CORO_API_VERSION; |
1281 | coroapi.ver = CORO_API_VERSION; |
1157 | coroapi.transfer = api_transfer; |
1282 | coroapi.transfer = api_transfer; |
1158 | |
1283 | |
1159 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1284 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
… | |
… | |
1167 | HV *hv; |
1292 | HV *hv; |
1168 | int i; |
1293 | int i; |
1169 | |
1294 | |
1170 | Newz (0, coro, 1, struct coro); |
1295 | Newz (0, coro, 1, struct coro); |
1171 | coro->args = newAV (); |
1296 | coro->args = newAV (); |
1172 | coro->save = CORO_SAVE_ALL; |
1297 | coro->save = CORO_SAVE_DEF; |
1173 | coro->flags = CF_NEW; |
1298 | coro->flags = CF_NEW; |
1174 | |
1299 | |
|
|
1300 | if (coro_first) coro_first->prev = coro; |
|
|
1301 | coro->next = coro_first; |
|
|
1302 | coro_first = coro; |
|
|
1303 | |
1175 | hv = newHV (); |
1304 | coro->hv = hv = newHV (); |
1176 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1305 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1177 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1306 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1178 | |
1307 | |
1179 | for (i = 1; i < items; i++) |
1308 | for (i = 1; i < items; i++) |
1180 | av_push (coro->args, newSVsv (ST (i))); |
1309 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1184 | |
1313 | |
1185 | int |
1314 | int |
1186 | save (SV *coro, int new_save = -1) |
1315 | save (SV *coro, int new_save = -1) |
1187 | CODE: |
1316 | CODE: |
1188 | RETVAL = api_save (coro, new_save); |
1317 | RETVAL = api_save (coro, new_save); |
|
|
1318 | OUTPUT: |
|
|
1319 | RETVAL |
|
|
1320 | |
|
|
1321 | int |
|
|
1322 | save_also (SV *coro_sv, int save_also) |
|
|
1323 | CODE: |
|
|
1324 | { |
|
|
1325 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1326 | RETVAL = coro->save; |
|
|
1327 | coro->save |= save_also; |
|
|
1328 | } |
1189 | OUTPUT: |
1329 | OUTPUT: |
1190 | RETVAL |
1330 | RETVAL |
1191 | |
1331 | |
1192 | void |
1332 | void |
1193 | _set_stacklevel (...) |
1333 | _set_stacklevel (...) |
… | |
… | |
1209 | |
1349 | |
1210 | case 1: |
1350 | case 1: |
1211 | if (items != 2) |
1351 | if (items != 2) |
1212 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1352 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1213 | |
1353 | |
1214 | prepare_transfer (&ta, ST (0), ST (1)); |
1354 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1215 | break; |
1355 | break; |
1216 | |
1356 | |
1217 | case 2: |
1357 | case 2: |
1218 | prepare_schedule (&ta); |
1358 | prepare_schedule (aTHX_ &ta); |
1219 | break; |
1359 | break; |
1220 | |
1360 | |
1221 | case 3: |
1361 | case 3: |
1222 | prepare_cede (&ta); |
1362 | prepare_cede (aTHX_ &ta); |
1223 | break; |
1363 | break; |
1224 | |
1364 | |
1225 | case 4: |
1365 | case 4: |
1226 | if (!prepare_cede_notself (&ta)) |
1366 | if (!prepare_cede_notself (aTHX_ &ta)) |
1227 | XSRETURN_EMPTY; |
1367 | XSRETURN_EMPTY; |
1228 | |
1368 | |
1229 | break; |
1369 | break; |
1230 | } |
1370 | } |
1231 | |
1371 | |
… | |
… | |
1237 | } |
1377 | } |
1238 | |
1378 | |
1239 | bool |
1379 | bool |
1240 | _destroy (SV *coro_sv) |
1380 | _destroy (SV *coro_sv) |
1241 | CODE: |
1381 | CODE: |
1242 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1382 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1243 | OUTPUT: |
1383 | OUTPUT: |
1244 | RETVAL |
1384 | RETVAL |
1245 | |
1385 | |
1246 | void |
1386 | void |
1247 | _exit (code) |
1387 | _exit (code) |
… | |
… | |
1249 | PROTOTYPE: $ |
1389 | PROTOTYPE: $ |
1250 | CODE: |
1390 | CODE: |
1251 | _exit (code); |
1391 | _exit (code); |
1252 | |
1392 | |
1253 | int |
1393 | int |
|
|
1394 | cctx_stacksize (int new_stacksize = 0) |
|
|
1395 | CODE: |
|
|
1396 | RETVAL = coro_stacksize; |
|
|
1397 | if (new_stacksize) |
|
|
1398 | coro_stacksize = new_stacksize; |
|
|
1399 | OUTPUT: |
|
|
1400 | RETVAL |
|
|
1401 | |
|
|
1402 | int |
1254 | cctx_count () |
1403 | cctx_count () |
1255 | CODE: |
1404 | CODE: |
1256 | RETVAL = cctx_count; |
1405 | RETVAL = cctx_count; |
1257 | OUTPUT: |
1406 | OUTPUT: |
1258 | RETVAL |
1407 | RETVAL |
… | |
… | |
1262 | CODE: |
1411 | CODE: |
1263 | RETVAL = cctx_idle; |
1412 | RETVAL = cctx_idle; |
1264 | OUTPUT: |
1413 | OUTPUT: |
1265 | RETVAL |
1414 | RETVAL |
1266 | |
1415 | |
|
|
1416 | void |
|
|
1417 | list () |
|
|
1418 | PPCODE: |
|
|
1419 | { |
|
|
1420 | struct coro *coro; |
|
|
1421 | for (coro = coro_first; coro; coro = coro->next) |
|
|
1422 | if (coro->hv) |
|
|
1423 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
|
|
1424 | } |
|
|
1425 | |
|
|
1426 | void |
|
|
1427 | call (Coro::State coro, SV *coderef) |
|
|
1428 | ALIAS: |
|
|
1429 | eval = 1 |
|
|
1430 | CODE: |
|
|
1431 | { |
|
|
1432 | if (coro->mainstack) |
|
|
1433 | { |
|
|
1434 | struct coro temp; |
|
|
1435 | Zero (&temp, 1, struct coro); |
|
|
1436 | temp.save = CORO_SAVE_ALL; |
|
|
1437 | |
|
|
1438 | if (!(coro->flags & CF_RUNNING)) |
|
|
1439 | { |
|
|
1440 | save_perl (aTHX_ &temp); |
|
|
1441 | load_perl (aTHX_ coro); |
|
|
1442 | } |
|
|
1443 | |
|
|
1444 | { |
|
|
1445 | dSP; |
|
|
1446 | ENTER; |
|
|
1447 | SAVETMPS; |
|
|
1448 | PUSHMARK (SP); |
|
|
1449 | PUTBACK; |
|
|
1450 | if (ix) |
|
|
1451 | eval_sv (coderef, 0); |
|
|
1452 | else |
|
|
1453 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1454 | SPAGAIN; |
|
|
1455 | FREETMPS; |
|
|
1456 | LEAVE; |
|
|
1457 | PUTBACK; |
|
|
1458 | } |
|
|
1459 | |
|
|
1460 | if (!(coro->flags & CF_RUNNING)) |
|
|
1461 | { |
|
|
1462 | save_perl (aTHX_ coro); |
|
|
1463 | load_perl (aTHX_ &temp); |
|
|
1464 | } |
|
|
1465 | } |
|
|
1466 | } |
|
|
1467 | |
|
|
1468 | SV * |
|
|
1469 | is_ready (Coro::State coro) |
|
|
1470 | PROTOTYPE: $ |
|
|
1471 | ALIAS: |
|
|
1472 | is_ready = CF_READY |
|
|
1473 | is_running = CF_RUNNING |
|
|
1474 | is_new = CF_NEW |
|
|
1475 | is_destroyed = CF_DESTROYED |
|
|
1476 | CODE: |
|
|
1477 | RETVAL = boolSV (coro->flags & ix); |
|
|
1478 | OUTPUT: |
|
|
1479 | RETVAL |
|
|
1480 | |
|
|
1481 | void |
|
|
1482 | trace (Coro::State coro, int enable = 0) |
|
|
1483 | CODE: |
|
|
1484 | if (enable) |
|
|
1485 | { |
|
|
1486 | if (coro->flags & CF_RUNNING) |
|
|
1487 | croak ("cannot enable tracing on running coroutine"); |
|
|
1488 | |
|
|
1489 | if (coro->cctx) |
|
|
1490 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1491 | |
|
|
1492 | coro->cctx = cctx_new (); |
|
|
1493 | coro->cctx->flags |= CC_TRACE; |
|
|
1494 | } |
|
|
1495 | else |
|
|
1496 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1497 | { |
|
|
1498 | coro->cctx->flags &= ~CC_TRACE; |
|
|
1499 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1500 | } |
|
|
1501 | |
|
|
1502 | SV * |
|
|
1503 | has_stack (Coro::State coro) |
|
|
1504 | PROTOTYPE: $ |
|
|
1505 | CODE: |
|
|
1506 | RETVAL = boolSV (!!coro->cctx); |
|
|
1507 | OUTPUT: |
|
|
1508 | RETVAL |
|
|
1509 | |
|
|
1510 | IV |
|
|
1511 | rss (Coro::State coro) |
|
|
1512 | PROTOTYPE: $ |
|
|
1513 | CODE: |
|
|
1514 | RETVAL = coro_rss (coro); |
|
|
1515 | OUTPUT: |
|
|
1516 | RETVAL |
|
|
1517 | |
|
|
1518 | |
1267 | MODULE = Coro::State PACKAGE = Coro |
1519 | MODULE = Coro::State PACKAGE = Coro |
1268 | |
1520 | |
1269 | BOOT: |
1521 | BOOT: |
1270 | { |
1522 | { |
1271 | int i; |
1523 | int i; |
|
|
1524 | |
|
|
1525 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1526 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1527 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1528 | |
|
|
1529 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1530 | SvREADONLY_on (coro_current); |
1272 | |
1531 | |
1273 | coro_stash = gv_stashpv ("Coro", TRUE); |
1532 | coro_stash = gv_stashpv ("Coro", TRUE); |
1274 | |
1533 | |
1275 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1534 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1276 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1535 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1277 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1536 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1278 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1537 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1279 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1538 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1280 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1539 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1281 | |
|
|
1282 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1283 | SvREADONLY_on (coro_current); |
|
|
1284 | |
1540 | |
1285 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1541 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1286 | coro_ready[i] = newAV (); |
1542 | coro_ready[i] = newAV (); |
1287 | |
1543 | |
1288 | { |
1544 | { |
… | |
… | |
1338 | CODE: |
1594 | CODE: |
1339 | RETVAL = boolSV (api_ready (self)); |
1595 | RETVAL = boolSV (api_ready (self)); |
1340 | OUTPUT: |
1596 | OUTPUT: |
1341 | RETVAL |
1597 | RETVAL |
1342 | |
1598 | |
1343 | SV * |
|
|
1344 | is_ready (SV *self) |
|
|
1345 | PROTOTYPE: $ |
|
|
1346 | CODE: |
|
|
1347 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1348 | OUTPUT: |
|
|
1349 | RETVAL |
|
|
1350 | |
|
|
1351 | int |
1599 | int |
1352 | nready (...) |
1600 | nready (...) |
1353 | PROTOTYPE: |
1601 | PROTOTYPE: |
1354 | CODE: |
1602 | CODE: |
1355 | RETVAL = coro_nready; |
1603 | RETVAL = coro_nready; |
1356 | OUTPUT: |
1604 | OUTPUT: |
1357 | RETVAL |
1605 | RETVAL |
1358 | |
1606 | |
|
|
1607 | # for async_pool speedup |
|
|
1608 | void |
|
|
1609 | _pool_1 (SV *cb) |
|
|
1610 | CODE: |
|
|
1611 | { |
|
|
1612 | int i, len; |
|
|
1613 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1614 | AV *defav = GvAV (PL_defgv); |
|
|
1615 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1616 | AV *invoke_av; |
|
|
1617 | |
|
|
1618 | if (!invoke) |
|
|
1619 | croak ("\3terminate\2\n"); |
|
|
1620 | |
|
|
1621 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1622 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1623 | |
|
|
1624 | invoke_av = (AV *)SvRV (invoke); |
|
|
1625 | len = av_len (invoke_av); |
|
|
1626 | |
|
|
1627 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1628 | |
|
|
1629 | if (len > 0) |
|
|
1630 | { |
|
|
1631 | av_fill (defav, len - 1); |
|
|
1632 | for (i = 0; i < len; ++i) |
|
|
1633 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1634 | } |
|
|
1635 | |
|
|
1636 | SvREFCNT_dec (invoke); |
|
|
1637 | } |
|
|
1638 | |
|
|
1639 | void |
|
|
1640 | _pool_2 (SV *cb) |
|
|
1641 | CODE: |
|
|
1642 | { |
|
|
1643 | struct coro *coro = SvSTATE (coro_current); |
|
|
1644 | |
|
|
1645 | sv_setsv (cb, &PL_sv_undef); |
|
|
1646 | |
|
|
1647 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
|
|
1648 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1649 | croak ("\3terminate\2\n"); |
|
|
1650 | |
|
|
1651 | av_clear (GvAV (PL_defgv)); |
|
|
1652 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1653 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1654 | coro->save = CORO_SAVE_DEF; |
|
|
1655 | coro->prio = 0; |
|
|
1656 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1657 | } |
|
|
1658 | |
|
|
1659 | |
1359 | MODULE = Coro::State PACKAGE = Coro::AIO |
1660 | MODULE = Coro::State PACKAGE = Coro::AIO |
1360 | |
1661 | |
1361 | SV * |
1662 | SV * |
1362 | _get_state () |
1663 | _get_state () |
1363 | CODE: |
1664 | CODE: |