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 macro should declare a variable stacklevel that contains and approximation |
89 | * to the current C stack pointer. Its property is that it changes with each call |
94 | * to the current C stack pointer. Its property is that it changes with each call |
90 | * and should be unique. */ |
95 | * and should be unique. */ |
… | |
… | |
121 | I32 laststype; |
126 | I32 laststype; |
122 | int laststatval; |
127 | int laststatval; |
123 | Stat_t statcache; |
128 | Stat_t statcache; |
124 | }; |
129 | }; |
125 | |
130 | |
|
|
131 | static size_t coro_stacksize = CORO_STACKSIZE; |
126 | static struct CoroAPI coroapi; |
132 | static struct CoroAPI coroapi; |
127 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
133 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
134 | static JMPENV *main_top_env; |
128 | static HV *coro_state_stash, *coro_stash; |
135 | static HV *coro_state_stash, *coro_stash; |
129 | static SV *coro_mortal; /* will be freed after next transfer */ |
136 | static SV *coro_mortal; /* will be freed after next transfer */ |
130 | |
137 | |
131 | static struct coro_cctx *cctx_first; |
138 | static struct coro_cctx *cctx_first; |
132 | static int cctx_count, cctx_idle; |
139 | static int cctx_count, cctx_idle; |
… | |
… | |
135 | typedef struct coro_cctx { |
142 | typedef struct coro_cctx { |
136 | struct coro_cctx *next; |
143 | struct coro_cctx *next; |
137 | |
144 | |
138 | /* the stack */ |
145 | /* the stack */ |
139 | void *sptr; |
146 | void *sptr; |
140 | long ssize; /* positive == mmap, otherwise malloc */ |
147 | size_t ssize; |
141 | |
148 | |
142 | /* cpu state */ |
149 | /* cpu state */ |
143 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
150 | 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 */ |
151 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
145 | JMPENV *top_env; |
152 | JMPENV *top_env; |
146 | coro_context cctx; |
153 | coro_context cctx; |
147 | |
154 | |
148 | int inuse; |
|
|
149 | |
|
|
150 | #if USE_VALGRIND |
155 | #if CORO_USE_VALGRIND |
151 | int valgrind_id; |
156 | int valgrind_id; |
152 | #endif |
157 | #endif |
|
|
158 | char inuse, mapped; |
153 | } coro_cctx; |
159 | } coro_cctx; |
154 | |
160 | |
155 | enum { |
161 | enum { |
156 | CF_RUNNING = 0x0001, /* coroutine is running */ |
162 | CF_RUNNING = 0x0001, /* coroutine is running */ |
157 | CF_READY = 0x0002, /* coroutine is ready */ |
163 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
172 | |
178 | |
173 | /* optionally saved, might be zero */ |
179 | /* optionally saved, might be zero */ |
174 | AV *defav; /* @_ */ |
180 | AV *defav; /* @_ */ |
175 | SV *defsv; /* $_ */ |
181 | SV *defsv; /* $_ */ |
176 | SV *errsv; /* $@ */ |
182 | SV *errsv; /* $@ */ |
|
|
183 | GV *deffh; /* default filehandle */ |
177 | SV *irssv; /* $/ */ |
184 | SV *irssv; /* $/ */ |
178 | SV *irssv_sv; /* real $/ cache */ |
185 | SV *irssv_sv; /* real $/ cache */ |
179 | |
186 | |
180 | #define VAR(name,type) type name; |
187 | #define VAR(name,type) type name; |
181 | # include "state.h" |
188 | # include "state.h" |
182 | #undef VAR |
189 | #undef VAR |
183 | |
190 | |
184 | /* coro process data */ |
191 | /* coro process data */ |
185 | int prio; |
192 | int prio; |
|
|
193 | |
|
|
194 | /* linked list */ |
|
|
195 | struct coro *next, *prev; |
|
|
196 | HV *hv; /* the perl hash associated with this coro, if any */ |
186 | }; |
197 | }; |
187 | |
198 | |
188 | typedef struct coro *Coro__State; |
199 | typedef struct coro *Coro__State; |
189 | typedef struct coro *Coro__State_or_hashref; |
200 | typedef struct coro *Coro__State_or_hashref; |
190 | |
201 | |
… | |
… | |
199 | |
210 | |
200 | /* for Coro.pm */ |
211 | /* for Coro.pm */ |
201 | static SV *coro_current; |
212 | static SV *coro_current; |
202 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
213 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
203 | static int coro_nready; |
214 | static int coro_nready; |
|
|
215 | static struct coro *first; |
204 | |
216 | |
205 | /** lowlevel stuff **********************************************************/ |
217 | /** lowlevel stuff **********************************************************/ |
206 | |
218 | |
207 | static AV * |
219 | static AV * |
208 | coro_clone_padlist (CV *cv) |
220 | coro_clone_padlist (pTHX_ CV *cv) |
209 | { |
221 | { |
210 | AV *padlist = CvPADLIST (cv); |
222 | AV *padlist = CvPADLIST (cv); |
211 | AV *newpadlist, *newpad; |
223 | AV *newpadlist, *newpad; |
212 | |
224 | |
213 | newpadlist = newAV (); |
225 | newpadlist = newAV (); |
… | |
… | |
225 | |
237 | |
226 | return newpadlist; |
238 | return newpadlist; |
227 | } |
239 | } |
228 | |
240 | |
229 | static void |
241 | static void |
230 | free_padlist (AV *padlist) |
242 | free_padlist (pTHX_ AV *padlist) |
231 | { |
243 | { |
232 | /* may be during global destruction */ |
244 | /* may be during global destruction */ |
233 | if (SvREFCNT (padlist)) |
245 | if (SvREFCNT (padlist)) |
234 | { |
246 | { |
235 | I32 i = AvFILLp (padlist); |
247 | I32 i = AvFILLp (padlist); |
… | |
… | |
256 | AV *padlist; |
268 | AV *padlist; |
257 | AV *av = (AV *)mg->mg_obj; |
269 | AV *av = (AV *)mg->mg_obj; |
258 | |
270 | |
259 | /* casting is fun. */ |
271 | /* casting is fun. */ |
260 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
272 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
261 | free_padlist (padlist); |
273 | free_padlist (aTHX_ padlist); |
262 | |
274 | |
263 | SvREFCNT_dec (av); |
275 | SvREFCNT_dec (av); |
264 | |
276 | |
265 | return 0; |
277 | return 0; |
266 | } |
278 | } |
… | |
… | |
276 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
288 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
277 | : 0 |
289 | : 0 |
278 | |
290 | |
279 | /* the next two functions merely cache the padlists */ |
291 | /* the next two functions merely cache the padlists */ |
280 | static void |
292 | static void |
281 | get_padlist (CV *cv) |
293 | get_padlist (pTHX_ CV *cv) |
282 | { |
294 | { |
283 | MAGIC *mg = CORO_MAGIC (cv); |
295 | MAGIC *mg = CORO_MAGIC (cv); |
284 | AV *av; |
296 | AV *av; |
285 | |
297 | |
286 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
298 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
287 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
299 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
288 | else |
300 | else |
289 | { |
301 | { |
290 | #if PREFER_PERL_FUNCTIONS |
302 | #if CORO_PREFER_PERL_FUNCTIONS |
291 | /* this is probably cleaner, but also slower? */ |
303 | /* this is probably cleaner, but also slower? */ |
292 | CV *cp = Perl_cv_clone (cv); |
304 | CV *cp = Perl_cv_clone (cv); |
293 | CvPADLIST (cv) = CvPADLIST (cp); |
305 | CvPADLIST (cv) = CvPADLIST (cp); |
294 | CvPADLIST (cp) = 0; |
306 | CvPADLIST (cp) = 0; |
295 | SvREFCNT_dec (cp); |
307 | SvREFCNT_dec (cp); |
296 | #else |
308 | #else |
297 | CvPADLIST (cv) = coro_clone_padlist (cv); |
309 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
298 | #endif |
310 | #endif |
299 | } |
311 | } |
300 | } |
312 | } |
301 | |
313 | |
302 | static void |
314 | static void |
303 | put_padlist (CV *cv) |
315 | put_padlist (pTHX_ CV *cv) |
304 | { |
316 | { |
305 | MAGIC *mg = CORO_MAGIC (cv); |
317 | MAGIC *mg = CORO_MAGIC (cv); |
306 | AV *av; |
318 | AV *av; |
307 | |
319 | |
308 | if (!mg) |
320 | if (!mg) |
… | |
… | |
327 | #define SE } while (0) |
339 | #define SE } while (0) |
328 | |
340 | |
329 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
341 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
330 | |
342 | |
331 | static void |
343 | static void |
332 | load_perl (Coro__State c) |
344 | load_perl (pTHX_ Coro__State c) |
333 | { |
345 | { |
334 | #define VAR(name,type) PL_ ## name = c->name; |
346 | #define VAR(name,type) PL_ ## name = c->name; |
335 | # include "state.h" |
347 | # include "state.h" |
336 | #undef VAR |
348 | #undef VAR |
337 | |
349 | |
338 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
350 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
339 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
351 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
340 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
352 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
353 | if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); |
|
|
354 | |
341 | if (c->irssv) |
355 | if (c->irssv) |
342 | { |
356 | { |
343 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
357 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
358 | { |
344 | SvREFCNT_dec (c->irssv); |
359 | SvREFCNT_dec (c->irssv); |
|
|
360 | c->irssv = 0; |
|
|
361 | } |
345 | else |
362 | else |
346 | { |
363 | { |
347 | REPLACE_SV (PL_rs, c->irssv); |
364 | REPLACE_SV (PL_rs, c->irssv); |
348 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
365 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
349 | sv_setsv (c->irssv_sv, PL_rs); |
366 | sv_setsv (c->irssv_sv, PL_rs); |
… | |
… | |
355 | CV *cv; |
372 | CV *cv; |
356 | |
373 | |
357 | /* now do the ugly restore mess */ |
374 | /* now do the ugly restore mess */ |
358 | while ((cv = (CV *)POPs)) |
375 | while ((cv = (CV *)POPs)) |
359 | { |
376 | { |
360 | put_padlist (cv); /* mark this padlist as available */ |
377 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
361 | CvDEPTH (cv) = PTR2IV (POPs); |
378 | CvDEPTH (cv) = PTR2IV (POPs); |
362 | CvPADLIST (cv) = (AV *)POPs; |
379 | CvPADLIST (cv) = (AV *)POPs; |
363 | } |
380 | } |
364 | |
381 | |
365 | PUTBACK; |
382 | PUTBACK; |
366 | } |
383 | } |
367 | } |
384 | } |
368 | |
385 | |
369 | static void |
386 | static void |
370 | save_perl (Coro__State c) |
387 | save_perl (pTHX_ Coro__State c) |
371 | { |
388 | { |
372 | { |
389 | { |
373 | dSP; |
390 | dSP; |
374 | I32 cxix = cxstack_ix; |
391 | I32 cxix = cxstack_ix; |
375 | PERL_CONTEXT *ccstk = cxstack; |
392 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
387 | { |
404 | { |
388 | while (cxix >= 0) |
405 | while (cxix >= 0) |
389 | { |
406 | { |
390 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
407 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
391 | |
408 | |
392 | if (CxTYPE (cx) == CXt_SUB) |
409 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
393 | { |
410 | { |
394 | CV *cv = cx->blk_sub.cv; |
411 | CV *cv = cx->blk_sub.cv; |
395 | |
412 | |
396 | if (CvDEPTH (cv)) |
413 | if (CvDEPTH (cv)) |
397 | { |
414 | { |
… | |
… | |
399 | PUSHs ((SV *)CvPADLIST (cv)); |
416 | PUSHs ((SV *)CvPADLIST (cv)); |
400 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
417 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
401 | PUSHs ((SV *)cv); |
418 | PUSHs ((SV *)cv); |
402 | |
419 | |
403 | CvDEPTH (cv) = 0; |
420 | CvDEPTH (cv) = 0; |
404 | get_padlist (cv); |
421 | get_padlist (aTHX_ cv); |
405 | } |
422 | } |
406 | } |
423 | } |
407 | } |
424 | } |
408 | |
425 | |
409 | if (top_si->si_type == PERLSI_MAIN) |
426 | if (top_si->si_type == PERLSI_MAIN) |
… | |
… | |
418 | } |
435 | } |
419 | |
436 | |
420 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
437 | 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; |
438 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
422 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
439 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
440 | 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; |
441 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
424 | |
442 | |
425 | #define VAR(name,type)c->name = PL_ ## name; |
443 | #define VAR(name,type)c->name = PL_ ## name; |
426 | # include "state.h" |
444 | # include "state.h" |
427 | #undef VAR |
445 | #undef VAR |
… | |
… | |
431 | * allocate various perl stacks. This is an exact copy |
449 | * allocate various perl stacks. This is an exact copy |
432 | * of perl.c:init_stacks, except that it uses less memory |
450 | * of perl.c:init_stacks, except that it uses less memory |
433 | * on the (sometimes correct) assumption that coroutines do |
451 | * on the (sometimes correct) assumption that coroutines do |
434 | * not usually need a lot of stackspace. |
452 | * not usually need a lot of stackspace. |
435 | */ |
453 | */ |
436 | #if PREFER_PERL_FUNCTIONS |
454 | #if CORO_PREFER_PERL_FUNCTIONS |
437 | # define coro_init_stacks init_stacks |
455 | # define coro_init_stacks init_stacks |
438 | #else |
456 | #else |
439 | static void |
457 | static void |
440 | coro_init_stacks () |
458 | coro_init_stacks (pTHX) |
441 | { |
459 | { |
442 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
460 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
443 | PL_curstackinfo->si_type = PERLSI_MAIN; |
461 | PL_curstackinfo->si_type = PERLSI_MAIN; |
444 | PL_curstack = PL_curstackinfo->si_stack; |
462 | PL_curstack = PL_curstackinfo->si_stack; |
445 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
463 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
… | |
… | |
479 | |
497 | |
480 | /* |
498 | /* |
481 | * destroy the stacks, the callchain etc... |
499 | * destroy the stacks, the callchain etc... |
482 | */ |
500 | */ |
483 | static void |
501 | static void |
484 | coro_destroy_stacks () |
502 | coro_destroy_stacks (pTHX) |
485 | { |
503 | { |
486 | if (!IN_DESTRUCT) |
504 | if (!IN_DESTRUCT) |
487 | { |
505 | { |
488 | /* restore all saved variables and stuff */ |
506 | /* restore all saved variables and stuff */ |
489 | LEAVE_SCOPE (0); |
507 | LEAVE_SCOPE (0); |
… | |
… | |
522 | #if !PERL_VERSION_ATLEAST (5,9,0) |
540 | #if !PERL_VERSION_ATLEAST (5,9,0) |
523 | Safefree (PL_retstack); |
541 | Safefree (PL_retstack); |
524 | #endif |
542 | #endif |
525 | } |
543 | } |
526 | |
544 | |
|
|
545 | static size_t |
|
|
546 | coro_rss (struct coro *coro) |
|
|
547 | { |
|
|
548 | size_t rss = sizeof (coro); |
|
|
549 | |
|
|
550 | if (coro->mainstack) |
|
|
551 | { |
|
|
552 | rss += sizeof (coro->curstackinfo); |
|
|
553 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
554 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
555 | |
|
|
556 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
557 | |
|
|
558 | rss += coro->tmps_max * sizeof (SV *); |
|
|
559 | |
|
|
560 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
561 | |
|
|
562 | rss += coro->scopestack_max * sizeof (I32); |
|
|
563 | |
|
|
564 | rss += coro->savestack_max * sizeof (ANY); |
|
|
565 | |
|
|
566 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
567 | rss += coro->retstack_max * sizeof (OP *); |
|
|
568 | #endif |
|
|
569 | } |
|
|
570 | |
|
|
571 | return rss; |
|
|
572 | } |
|
|
573 | |
527 | /** coroutine stack handling ************************************************/ |
574 | /** coroutine stack handling ************************************************/ |
528 | |
575 | |
529 | static void |
576 | static void |
530 | setup_coro (struct coro *coro) |
577 | setup_coro (pTHX_ struct coro *coro) |
531 | { |
578 | { |
532 | /* |
579 | /* |
533 | * emulate part of the perl startup here. |
580 | * emulate part of the perl startup here. |
534 | */ |
581 | */ |
535 | |
|
|
536 | coro_init_stacks (); |
582 | coro_init_stacks (aTHX); |
537 | |
583 | |
538 | PL_curcop = &PL_compiling; |
584 | PL_curcop = &PL_compiling; |
539 | PL_in_eval = EVAL_NULL; |
585 | PL_in_eval = EVAL_NULL; |
|
|
586 | PL_comppad = 0; |
540 | PL_curpm = 0; |
587 | PL_curpm = 0; |
541 | PL_localizing = 0; |
588 | PL_localizing = 0; |
542 | PL_dirty = 0; |
589 | PL_dirty = 0; |
543 | PL_restartop = 0; |
590 | PL_restartop = 0; |
544 | |
591 | |
… | |
… | |
552 | Zero (&myop, 1, LOGOP); |
599 | Zero (&myop, 1, LOGOP); |
553 | myop.op_next = Nullop; |
600 | myop.op_next = Nullop; |
554 | myop.op_flags = OPf_WANT_VOID; |
601 | myop.op_flags = OPf_WANT_VOID; |
555 | |
602 | |
556 | PUSHMARK (SP); |
603 | PUSHMARK (SP); |
557 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
604 | XPUSHs (av_shift (GvAV (PL_defgv))); |
558 | PUTBACK; |
605 | PUTBACK; |
559 | PL_op = (OP *)&myop; |
606 | PL_op = (OP *)&myop; |
560 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
607 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
561 | SPAGAIN; |
608 | SPAGAIN; |
562 | } |
609 | } |
563 | |
610 | |
564 | ENTER; /* necessary e.g. for dounwind */ |
611 | ENTER; /* necessary e.g. for dounwind */ |
565 | } |
612 | } |
566 | |
613 | |
567 | static void |
614 | static void |
568 | free_coro_mortal () |
615 | free_coro_mortal (pTHX) |
569 | { |
616 | { |
570 | if (coro_mortal) |
617 | if (coro_mortal) |
571 | { |
618 | { |
572 | SvREFCNT_dec (coro_mortal); |
619 | SvREFCNT_dec (coro_mortal); |
573 | coro_mortal = 0; |
620 | coro_mortal = 0; |
574 | } |
621 | } |
575 | } |
622 | } |
576 | |
623 | |
577 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
624 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
|
|
625 | /* _cctx_init should be careful, as it could be called at almost any time */ |
|
|
626 | /* during execution of a perl program */ |
578 | static void NOINLINE |
627 | static void NOINLINE |
579 | prepare_cctx (coro_cctx *cctx) |
628 | prepare_cctx (pTHX_ coro_cctx *cctx) |
580 | { |
629 | { |
581 | dSP; |
630 | dSP; |
582 | LOGOP myop; |
631 | LOGOP myop; |
583 | |
632 | |
584 | Zero (&myop, 1, LOGOP); |
633 | Zero (&myop, 1, LOGOP); |
… | |
… | |
593 | PL_op = (OP *)&myop; |
642 | PL_op = (OP *)&myop; |
594 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
643 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
595 | SPAGAIN; |
644 | SPAGAIN; |
596 | } |
645 | } |
597 | |
646 | |
|
|
647 | /* |
|
|
648 | * this is a _very_ stripped down perl interpreter ;) |
|
|
649 | */ |
598 | static void |
650 | static void |
599 | coro_run (void *arg) |
651 | coro_run (void *arg) |
600 | { |
652 | { |
|
|
653 | dTHX; |
|
|
654 | |
601 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
655 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
602 | UNLOCK; |
656 | UNLOCK; |
603 | |
657 | |
604 | /* |
|
|
605 | * this is a _very_ stripped down perl interpreter ;) |
|
|
606 | */ |
|
|
607 | PL_top_env = &PL_start_env; |
658 | PL_top_env = &PL_start_env; |
608 | |
659 | |
609 | /* inject call to cctx_init */ |
660 | /* inject a fake subroutine call to cctx_init */ |
610 | prepare_cctx ((coro_cctx *)arg); |
661 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
611 | |
662 | |
612 | /* somebody will hit me for both perl_run and PL_restartop */ |
663 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
613 | PL_restartop = PL_op; |
664 | PL_restartop = PL_op; |
614 | perl_run (PL_curinterp); |
665 | perl_run (PL_curinterp); |
615 | |
666 | |
616 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
667 | /* |
617 | abort (); |
668 | * If perl-run returns we assume exit() was being called or the coro |
|
|
669 | * fell off the end, which seems to be the only valid (non-bug) |
|
|
670 | * reason for perl_run to return. We try to exit by jumping to the |
|
|
671 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
|
|
672 | * coroutine as Coro has no such concept |
|
|
673 | */ |
|
|
674 | PL_top_env = main_top_env; |
|
|
675 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
618 | } |
676 | } |
619 | |
677 | |
620 | static coro_cctx * |
678 | static coro_cctx * |
621 | cctx_new () |
679 | cctx_new () |
622 | { |
680 | { |
623 | coro_cctx *cctx; |
681 | coro_cctx *cctx; |
|
|
682 | void *stack_start; |
|
|
683 | size_t stack_size; |
624 | |
684 | |
625 | ++cctx_count; |
685 | ++cctx_count; |
626 | |
686 | |
627 | Newz (0, cctx, 1, coro_cctx); |
687 | Newz (0, cctx, 1, coro_cctx); |
628 | |
688 | |
629 | #if HAVE_MMAP |
689 | #if HAVE_MMAP |
630 | |
690 | |
631 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
691 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
632 | /* mmap supposedly does allocate-on-write for us */ |
692 | /* 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); |
693 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
634 | |
694 | |
635 | if (cctx->sptr == (void *)-1) |
695 | if (cctx->sptr != (void *)-1) |
636 | { |
|
|
637 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
638 | _exit (EXIT_FAILURE); |
|
|
639 | } |
696 | { |
640 | |
|
|
641 | # if STACKGUARD |
697 | # if CORO_STACKGUARD |
642 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
698 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
643 | # endif |
699 | # endif |
|
|
700 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
|
|
701 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
702 | cctx->mapped = 1; |
|
|
703 | } |
|
|
704 | else |
|
|
705 | #endif |
|
|
706 | { |
|
|
707 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
|
|
708 | New (0, cctx->sptr, coro_stacksize, long); |
644 | |
709 | |
645 | #else |
|
|
646 | |
|
|
647 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
648 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
649 | |
|
|
650 | if (!cctx->sptr) |
710 | if (!cctx->sptr) |
651 | { |
711 | { |
652 | perror ("FATAL: unable to malloc stack for coroutine"); |
712 | perror ("FATAL: unable to allocate stack for coroutine"); |
653 | _exit (EXIT_FAILURE); |
713 | _exit (EXIT_FAILURE); |
654 | } |
714 | } |
655 | |
715 | |
656 | #endif |
716 | stack_start = cctx->sptr; |
|
|
717 | stack_size = cctx->ssize; |
|
|
718 | } |
657 | |
719 | |
658 | #if USE_VALGRIND |
720 | 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); |
721 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
666 | |
722 | |
667 | return cctx; |
723 | return cctx; |
668 | } |
724 | } |
669 | |
725 | |
670 | static void |
726 | static void |
… | |
… | |
673 | if (!cctx) |
729 | if (!cctx) |
674 | return; |
730 | return; |
675 | |
731 | |
676 | --cctx_count; |
732 | --cctx_count; |
677 | |
733 | |
678 | #if USE_VALGRIND |
734 | #if CORO_USE_VALGRIND |
679 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
735 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
680 | #endif |
736 | #endif |
681 | |
737 | |
682 | #if HAVE_MMAP |
738 | #if HAVE_MMAP |
|
|
739 | if (cctx->mapped) |
683 | munmap (cctx->sptr, cctx->ssize); |
740 | munmap (cctx->sptr, cctx->ssize); |
684 | #else |
741 | else |
|
|
742 | #endif |
685 | Safefree (cctx->sptr); |
743 | Safefree (cctx->sptr); |
686 | #endif |
|
|
687 | |
744 | |
688 | Safefree (cctx); |
745 | Safefree (cctx); |
689 | } |
746 | } |
690 | |
747 | |
691 | static coro_cctx * |
748 | static coro_cctx * |
692 | cctx_get () |
749 | cctx_get (pTHX) |
693 | { |
750 | { |
694 | coro_cctx *cctx; |
|
|
695 | |
|
|
696 | if (cctx_first) |
751 | while (cctx_first) |
697 | { |
752 | { |
698 | cctx = cctx_first; |
753 | coro_cctx *cctx = cctx_first; |
699 | cctx_first = cctx->next; |
754 | cctx_first = cctx->next; |
700 | --cctx_idle; |
755 | --cctx_idle; |
|
|
756 | |
|
|
757 | if (cctx->ssize >= coro_stacksize) |
|
|
758 | return cctx; |
|
|
759 | |
|
|
760 | cctx_destroy (cctx); |
701 | } |
761 | } |
702 | else |
762 | |
703 | { |
|
|
704 | cctx = cctx_new (); |
|
|
705 | PL_op = PL_op->op_next; |
763 | PL_op = PL_op->op_next; |
706 | } |
|
|
707 | |
|
|
708 | return cctx; |
764 | return cctx_new (); |
709 | } |
765 | } |
710 | |
766 | |
711 | static void |
767 | static void |
712 | cctx_put (coro_cctx *cctx) |
768 | cctx_put (coro_cctx *cctx) |
713 | { |
769 | { |
… | |
… | |
729 | |
785 | |
730 | /** coroutine switching *****************************************************/ |
786 | /** coroutine switching *****************************************************/ |
731 | |
787 | |
732 | /* never call directly, always through the coro_state_transfer global variable */ |
788 | /* never call directly, always through the coro_state_transfer global variable */ |
733 | static void NOINLINE |
789 | static void NOINLINE |
734 | transfer (struct coro *prev, struct coro *next) |
790 | transfer (pTHX_ struct coro *prev, struct coro *next) |
735 | { |
791 | { |
736 | dSTACKLEVEL; |
792 | dSTACKLEVEL; |
737 | |
793 | |
738 | /* sometimes transfer is only called to set idle_sp */ |
794 | /* sometimes transfer is only called to set idle_sp */ |
739 | if (!next) |
795 | if (!next) |
… | |
… | |
772 | if (next->flags & CF_NEW) |
828 | if (next->flags & CF_NEW) |
773 | { |
829 | { |
774 | /* need to start coroutine */ |
830 | /* need to start coroutine */ |
775 | next->flags &= ~CF_NEW; |
831 | next->flags &= ~CF_NEW; |
776 | /* first get rid of the old state */ |
832 | /* first get rid of the old state */ |
777 | save_perl (prev); |
833 | save_perl (aTHX_ prev); |
778 | /* setup coroutine call */ |
834 | /* setup coroutine call */ |
779 | setup_coro (next); |
835 | setup_coro (aTHX_ next); |
780 | /* need a new stack */ |
836 | /* need a new stack */ |
781 | assert (!next->cctx); |
837 | assert (!next->cctx); |
782 | } |
838 | } |
783 | else |
839 | else |
784 | { |
840 | { |
785 | /* coroutine already started */ |
841 | /* coroutine already started */ |
786 | save_perl (prev); |
842 | save_perl (aTHX_ prev); |
787 | load_perl (next); |
843 | load_perl (aTHX_ next); |
788 | } |
844 | } |
789 | |
845 | |
790 | prev__cctx = prev->cctx; |
846 | prev__cctx = prev->cctx; |
791 | |
847 | |
792 | /* possibly "free" the cctx */ |
848 | /* possibly "free" the cctx */ |
… | |
… | |
801 | prev__cctx->inuse = 0; |
857 | prev__cctx->inuse = 0; |
802 | } |
858 | } |
803 | |
859 | |
804 | if (!next->cctx) |
860 | if (!next->cctx) |
805 | { |
861 | { |
806 | next->cctx = cctx_get (); |
862 | next->cctx = cctx_get (aTHX); |
807 | assert (!next->cctx->inuse); |
863 | assert (!next->cctx->inuse); |
808 | next->cctx->inuse = 1; |
864 | next->cctx->inuse = 1; |
809 | } |
865 | } |
810 | |
866 | |
811 | if (prev__cctx != next->cctx) |
867 | if (prev__cctx != next->cctx) |
… | |
… | |
813 | prev__cctx->top_env = PL_top_env; |
869 | prev__cctx->top_env = PL_top_env; |
814 | PL_top_env = next->cctx->top_env; |
870 | PL_top_env = next->cctx->top_env; |
815 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
871 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
816 | } |
872 | } |
817 | |
873 | |
818 | free_coro_mortal (); |
874 | free_coro_mortal (aTHX); |
819 | UNLOCK; |
875 | UNLOCK; |
820 | } |
876 | } |
821 | } |
877 | } |
822 | |
878 | |
823 | struct transfer_args |
879 | struct transfer_args |
824 | { |
880 | { |
825 | struct coro *prev, *next; |
881 | struct coro *prev, *next; |
826 | }; |
882 | }; |
827 | |
883 | |
828 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
884 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
829 | |
885 | |
830 | /** high level stuff ********************************************************/ |
886 | /** high level stuff ********************************************************/ |
831 | |
887 | |
832 | static int |
888 | static int |
833 | coro_state_destroy (struct coro *coro) |
889 | coro_state_destroy (pTHX_ struct coro *coro) |
834 | { |
890 | { |
835 | if (coro->flags & CF_DESTROYED) |
891 | if (coro->flags & CF_DESTROYED) |
836 | return 0; |
892 | return 0; |
837 | |
893 | |
838 | coro->flags |= CF_DESTROYED; |
894 | coro->flags |= CF_DESTROYED; |
… | |
… | |
858 | temp.save = CORO_SAVE_ALL; |
914 | temp.save = CORO_SAVE_ALL; |
859 | |
915 | |
860 | if (coro->flags & CF_RUNNING) |
916 | if (coro->flags & CF_RUNNING) |
861 | croak ("FATAL: tried to destroy currently running coroutine"); |
917 | croak ("FATAL: tried to destroy currently running coroutine"); |
862 | |
918 | |
863 | save_perl (&temp); |
919 | save_perl (aTHX_ &temp); |
864 | load_perl (coro); |
920 | load_perl (aTHX_ coro); |
865 | |
921 | |
866 | coro_destroy_stacks (); |
922 | coro_destroy_stacks (aTHX); |
867 | |
923 | |
868 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
924 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
869 | |
925 | |
870 | coro->mainstack = 0; |
926 | coro->mainstack = 0; |
871 | } |
927 | } |
872 | |
928 | |
873 | cctx_destroy (coro->cctx); |
929 | cctx_destroy (coro->cctx); |
874 | SvREFCNT_dec (coro->args); |
930 | SvREFCNT_dec (coro->args); |
875 | |
931 | |
|
|
932 | if (coro->next) coro->next->prev = coro->prev; |
|
|
933 | if (coro->prev) coro->prev->next = coro->next; |
|
|
934 | if (coro == first) first = coro->next; |
|
|
935 | |
876 | return 1; |
936 | return 1; |
877 | } |
937 | } |
878 | |
938 | |
879 | static int |
939 | static int |
880 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
940 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
881 | { |
941 | { |
882 | struct coro *coro = (struct coro *)mg->mg_ptr; |
942 | struct coro *coro = (struct coro *)mg->mg_ptr; |
883 | mg->mg_ptr = 0; |
943 | mg->mg_ptr = 0; |
884 | |
944 | |
|
|
945 | coro->hv = 0; |
|
|
946 | |
885 | if (--coro->refcnt < 0) |
947 | if (--coro->refcnt < 0) |
886 | { |
948 | { |
887 | coro_state_destroy (coro); |
949 | coro_state_destroy (aTHX_ coro); |
888 | Safefree (coro); |
950 | Safefree (coro); |
889 | } |
951 | } |
890 | |
952 | |
891 | return 0; |
953 | return 0; |
892 | } |
954 | } |
… | |
… | |
911 | # define MGf_DUP 0 |
973 | # define MGf_DUP 0 |
912 | #endif |
974 | #endif |
913 | }; |
975 | }; |
914 | |
976 | |
915 | static struct coro * |
977 | static struct coro * |
916 | SvSTATE (SV *coro) |
978 | SvSTATE_ (pTHX_ SV *coro) |
917 | { |
979 | { |
918 | HV *stash; |
980 | HV *stash; |
919 | MAGIC *mg; |
981 | MAGIC *mg; |
920 | |
982 | |
921 | if (SvROK (coro)) |
983 | if (SvROK (coro)) |
… | |
… | |
927 | /* very slow, but rare, check */ |
989 | /* very slow, but rare, check */ |
928 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
990 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
929 | croak ("Coro::State object required"); |
991 | croak ("Coro::State object required"); |
930 | } |
992 | } |
931 | |
993 | |
932 | mg = SvMAGIC (coro); |
994 | mg = CORO_MAGIC (coro); |
933 | assert (mg->mg_type == PERL_MAGIC_ext); |
|
|
934 | return (struct coro *)mg->mg_ptr; |
995 | return (struct coro *)mg->mg_ptr; |
935 | } |
996 | } |
936 | |
997 | |
|
|
998 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
999 | |
937 | static void |
1000 | static void |
938 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1001 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
939 | { |
1002 | { |
940 | ta->prev = SvSTATE (prev_sv); |
1003 | ta->prev = SvSTATE (prev_sv); |
941 | ta->next = SvSTATE (next_sv); |
1004 | ta->next = SvSTATE (next_sv); |
942 | } |
1005 | } |
943 | |
1006 | |
944 | static void |
1007 | static void |
945 | api_transfer (SV *prev_sv, SV *next_sv) |
1008 | api_transfer (SV *prev_sv, SV *next_sv) |
946 | { |
1009 | { |
|
|
1010 | dTHX; |
947 | struct transfer_args ta; |
1011 | struct transfer_args ta; |
948 | |
1012 | |
949 | prepare_transfer (&ta, prev_sv, next_sv); |
1013 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
950 | TRANSFER (ta); |
1014 | TRANSFER (ta); |
951 | } |
1015 | } |
952 | |
1016 | |
953 | static int |
1017 | static int |
954 | api_save (SV *coro_sv, int new_save) |
1018 | api_save (SV *coro_sv, int new_save) |
955 | { |
1019 | { |
|
|
1020 | dTHX; |
956 | struct coro *coro = SvSTATE (coro_sv); |
1021 | struct coro *coro = SvSTATE (coro_sv); |
957 | int old_save = coro->save; |
1022 | int old_save = coro->save; |
958 | |
1023 | |
959 | if (new_save >= 0) |
1024 | if (new_save >= 0) |
960 | coro->save = new_save; |
1025 | coro->save = new_save; |
… | |
… | |
963 | } |
1028 | } |
964 | |
1029 | |
965 | /** Coro ********************************************************************/ |
1030 | /** Coro ********************************************************************/ |
966 | |
1031 | |
967 | static void |
1032 | static void |
968 | coro_enq (SV *coro_sv) |
1033 | coro_enq (pTHX_ SV *coro_sv) |
969 | { |
1034 | { |
970 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1035 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
971 | } |
1036 | } |
972 | |
1037 | |
973 | static SV * |
1038 | static SV * |
974 | coro_deq (int min_prio) |
1039 | coro_deq (pTHX_ int min_prio) |
975 | { |
1040 | { |
976 | int prio = PRIO_MAX - PRIO_MIN; |
1041 | int prio = PRIO_MAX - PRIO_MIN; |
977 | |
1042 | |
978 | min_prio -= PRIO_MIN; |
1043 | min_prio -= PRIO_MIN; |
979 | if (min_prio < 0) |
1044 | if (min_prio < 0) |
… | |
… | |
987 | } |
1052 | } |
988 | |
1053 | |
989 | static int |
1054 | static int |
990 | api_ready (SV *coro_sv) |
1055 | api_ready (SV *coro_sv) |
991 | { |
1056 | { |
|
|
1057 | dTHX; |
992 | struct coro *coro; |
1058 | struct coro *coro; |
993 | |
1059 | |
994 | if (SvROK (coro_sv)) |
1060 | if (SvROK (coro_sv)) |
995 | coro_sv = SvRV (coro_sv); |
1061 | coro_sv = SvRV (coro_sv); |
996 | |
1062 | |
… | |
… | |
1000 | return 0; |
1066 | return 0; |
1001 | |
1067 | |
1002 | coro->flags |= CF_READY; |
1068 | coro->flags |= CF_READY; |
1003 | |
1069 | |
1004 | LOCK; |
1070 | LOCK; |
1005 | coro_enq (SvREFCNT_inc (coro_sv)); |
1071 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1006 | ++coro_nready; |
1072 | ++coro_nready; |
1007 | UNLOCK; |
1073 | UNLOCK; |
1008 | |
1074 | |
1009 | return 1; |
1075 | return 1; |
1010 | } |
1076 | } |
1011 | |
1077 | |
1012 | static int |
1078 | static int |
1013 | api_is_ready (SV *coro_sv) |
1079 | api_is_ready (SV *coro_sv) |
1014 | { |
1080 | { |
|
|
1081 | dTHX; |
1015 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1082 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1016 | } |
1083 | } |
1017 | |
1084 | |
1018 | static void |
1085 | static void |
1019 | prepare_schedule (struct transfer_args *ta) |
1086 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1020 | { |
1087 | { |
1021 | SV *prev_sv, *next_sv; |
1088 | SV *prev_sv, *next_sv; |
1022 | |
1089 | |
1023 | for (;;) |
1090 | for (;;) |
1024 | { |
1091 | { |
1025 | LOCK; |
1092 | LOCK; |
1026 | next_sv = coro_deq (PRIO_MIN); |
1093 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1027 | |
1094 | |
1028 | /* nothing to schedule: call the idle handler */ |
1095 | /* nothing to schedule: call the idle handler */ |
1029 | if (!next_sv) |
1096 | if (!next_sv) |
1030 | { |
1097 | { |
1031 | dSP; |
1098 | dSP; |
… | |
… | |
1066 | |
1133 | |
1067 | assert (ta->next->flags & CF_READY); |
1134 | assert (ta->next->flags & CF_READY); |
1068 | ta->next->flags &= ~CF_READY; |
1135 | ta->next->flags &= ~CF_READY; |
1069 | |
1136 | |
1070 | LOCK; |
1137 | LOCK; |
1071 | free_coro_mortal (); |
1138 | free_coro_mortal (aTHX); |
1072 | coro_mortal = prev_sv; |
1139 | coro_mortal = prev_sv; |
1073 | UNLOCK; |
1140 | UNLOCK; |
1074 | } |
1141 | } |
1075 | |
1142 | |
1076 | static void |
1143 | static void |
1077 | prepare_cede (struct transfer_args *ta) |
1144 | prepare_cede (pTHX_ struct transfer_args *ta) |
1078 | { |
1145 | { |
1079 | api_ready (coro_current); |
1146 | api_ready (coro_current); |
1080 | prepare_schedule (ta); |
1147 | prepare_schedule (aTHX_ ta); |
1081 | } |
1148 | } |
1082 | |
1149 | |
1083 | static int |
1150 | static int |
1084 | prepare_cede_notself (struct transfer_args *ta) |
1151 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1085 | { |
1152 | { |
1086 | if (coro_nready) |
1153 | if (coro_nready) |
1087 | { |
1154 | { |
1088 | SV *prev = SvRV (coro_current); |
1155 | SV *prev = SvRV (coro_current); |
1089 | prepare_schedule (ta); |
1156 | prepare_schedule (aTHX_ ta); |
1090 | api_ready (prev); |
1157 | api_ready (prev); |
1091 | return 1; |
1158 | return 1; |
1092 | } |
1159 | } |
1093 | else |
1160 | else |
1094 | return 0; |
1161 | return 0; |
1095 | } |
1162 | } |
1096 | |
1163 | |
1097 | static void |
1164 | static void |
1098 | api_schedule (void) |
1165 | api_schedule (void) |
1099 | { |
1166 | { |
|
|
1167 | dTHX; |
1100 | struct transfer_args ta; |
1168 | struct transfer_args ta; |
1101 | |
1169 | |
1102 | prepare_schedule (&ta); |
1170 | prepare_schedule (aTHX_ &ta); |
1103 | TRANSFER (ta); |
1171 | TRANSFER (ta); |
1104 | } |
1172 | } |
1105 | |
1173 | |
1106 | static int |
1174 | static int |
1107 | api_cede (void) |
1175 | api_cede (void) |
1108 | { |
1176 | { |
|
|
1177 | dTHX; |
1109 | struct transfer_args ta; |
1178 | struct transfer_args ta; |
1110 | |
1179 | |
1111 | prepare_cede (&ta); |
1180 | prepare_cede (aTHX_ &ta); |
1112 | |
1181 | |
1113 | if (ta.prev != ta.next) |
1182 | if (ta.prev != ta.next) |
1114 | { |
1183 | { |
1115 | TRANSFER (ta); |
1184 | TRANSFER (ta); |
1116 | return 1; |
1185 | return 1; |
… | |
… | |
1120 | } |
1189 | } |
1121 | |
1190 | |
1122 | static int |
1191 | static int |
1123 | api_cede_notself (void) |
1192 | api_cede_notself (void) |
1124 | { |
1193 | { |
|
|
1194 | dTHX; |
1125 | struct transfer_args ta; |
1195 | struct transfer_args ta; |
1126 | |
1196 | |
1127 | if (prepare_cede_notself (&ta)) |
1197 | if (prepare_cede_notself (aTHX_ &ta)) |
1128 | { |
1198 | { |
1129 | TRANSFER (ta); |
1199 | TRANSFER (ta); |
1130 | return 1; |
1200 | return 1; |
1131 | } |
1201 | } |
1132 | else |
1202 | else |
… | |
… | |
1148 | |
1218 | |
1149 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1219 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1150 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1220 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1151 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1221 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1152 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1222 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1223 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
|
|
1224 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1153 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1225 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1154 | |
1226 | |
1155 | main_mainstack = PL_mainstack; |
1227 | main_mainstack = PL_mainstack; |
|
|
1228 | main_top_env = PL_top_env; |
|
|
1229 | |
|
|
1230 | while (main_top_env->je_prev) |
|
|
1231 | main_top_env = main_top_env->je_prev; |
1156 | |
1232 | |
1157 | coroapi.ver = CORO_API_VERSION; |
1233 | coroapi.ver = CORO_API_VERSION; |
1158 | coroapi.transfer = api_transfer; |
1234 | coroapi.transfer = api_transfer; |
1159 | |
1235 | |
1160 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1236 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
… | |
… | |
1168 | HV *hv; |
1244 | HV *hv; |
1169 | int i; |
1245 | int i; |
1170 | |
1246 | |
1171 | Newz (0, coro, 1, struct coro); |
1247 | Newz (0, coro, 1, struct coro); |
1172 | coro->args = newAV (); |
1248 | coro->args = newAV (); |
1173 | coro->save = CORO_SAVE_ALL; |
1249 | coro->save = CORO_SAVE_DEF; |
1174 | coro->flags = CF_NEW; |
1250 | coro->flags = CF_NEW; |
1175 | |
1251 | |
|
|
1252 | if (first) first->prev = coro; |
|
|
1253 | coro->next = first; |
|
|
1254 | first = coro; |
|
|
1255 | |
1176 | hv = newHV (); |
1256 | coro->hv = hv = newHV (); |
1177 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1257 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1178 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1258 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1179 | |
1259 | |
1180 | for (i = 1; i < items; i++) |
1260 | for (i = 1; i < items; i++) |
1181 | av_push (coro->args, newSVsv (ST (i))); |
1261 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1185 | |
1265 | |
1186 | int |
1266 | int |
1187 | save (SV *coro, int new_save = -1) |
1267 | save (SV *coro, int new_save = -1) |
1188 | CODE: |
1268 | CODE: |
1189 | RETVAL = api_save (coro, new_save); |
1269 | RETVAL = api_save (coro, new_save); |
|
|
1270 | OUTPUT: |
|
|
1271 | RETVAL |
|
|
1272 | |
|
|
1273 | int |
|
|
1274 | save_also (SV *coro_sv, int save_also) |
|
|
1275 | CODE: |
|
|
1276 | { |
|
|
1277 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1278 | RETVAL = coro->save; |
|
|
1279 | coro->save |= save_also; |
|
|
1280 | } |
1190 | OUTPUT: |
1281 | OUTPUT: |
1191 | RETVAL |
1282 | RETVAL |
1192 | |
1283 | |
1193 | void |
1284 | void |
1194 | _set_stacklevel (...) |
1285 | _set_stacklevel (...) |
… | |
… | |
1210 | |
1301 | |
1211 | case 1: |
1302 | case 1: |
1212 | if (items != 2) |
1303 | if (items != 2) |
1213 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1304 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1214 | |
1305 | |
1215 | prepare_transfer (&ta, ST (0), ST (1)); |
1306 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1216 | break; |
1307 | break; |
1217 | |
1308 | |
1218 | case 2: |
1309 | case 2: |
1219 | prepare_schedule (&ta); |
1310 | prepare_schedule (aTHX_ &ta); |
1220 | break; |
1311 | break; |
1221 | |
1312 | |
1222 | case 3: |
1313 | case 3: |
1223 | prepare_cede (&ta); |
1314 | prepare_cede (aTHX_ &ta); |
1224 | break; |
1315 | break; |
1225 | |
1316 | |
1226 | case 4: |
1317 | case 4: |
1227 | if (!prepare_cede_notself (&ta)) |
1318 | if (!prepare_cede_notself (aTHX_ &ta)) |
1228 | XSRETURN_EMPTY; |
1319 | XSRETURN_EMPTY; |
1229 | |
1320 | |
1230 | break; |
1321 | break; |
1231 | } |
1322 | } |
1232 | |
1323 | |
… | |
… | |
1238 | } |
1329 | } |
1239 | |
1330 | |
1240 | bool |
1331 | bool |
1241 | _destroy (SV *coro_sv) |
1332 | _destroy (SV *coro_sv) |
1242 | CODE: |
1333 | CODE: |
1243 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1334 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1244 | OUTPUT: |
1335 | OUTPUT: |
1245 | RETVAL |
1336 | RETVAL |
1246 | |
1337 | |
1247 | void |
1338 | void |
1248 | _exit (code) |
1339 | _exit (code) |
… | |
… | |
1250 | PROTOTYPE: $ |
1341 | PROTOTYPE: $ |
1251 | CODE: |
1342 | CODE: |
1252 | _exit (code); |
1343 | _exit (code); |
1253 | |
1344 | |
1254 | int |
1345 | int |
|
|
1346 | cctx_stacksize (int new_stacksize = 0) |
|
|
1347 | CODE: |
|
|
1348 | RETVAL = coro_stacksize; |
|
|
1349 | if (new_stacksize) |
|
|
1350 | coro_stacksize = new_stacksize; |
|
|
1351 | OUTPUT: |
|
|
1352 | RETVAL |
|
|
1353 | |
|
|
1354 | int |
1255 | cctx_count () |
1355 | cctx_count () |
1256 | CODE: |
1356 | CODE: |
1257 | RETVAL = cctx_count; |
1357 | RETVAL = cctx_count; |
1258 | OUTPUT: |
1358 | OUTPUT: |
1259 | RETVAL |
1359 | RETVAL |
… | |
… | |
1262 | cctx_idle () |
1362 | cctx_idle () |
1263 | CODE: |
1363 | CODE: |
1264 | RETVAL = cctx_idle; |
1364 | RETVAL = cctx_idle; |
1265 | OUTPUT: |
1365 | OUTPUT: |
1266 | RETVAL |
1366 | RETVAL |
|
|
1367 | |
|
|
1368 | void |
|
|
1369 | list () |
|
|
1370 | PPCODE: |
|
|
1371 | { |
|
|
1372 | struct coro *coro; |
|
|
1373 | for (coro = first; coro; coro = coro->next) |
|
|
1374 | if (coro->hv) |
|
|
1375 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
|
|
1376 | } |
|
|
1377 | |
|
|
1378 | void |
|
|
1379 | _eval (Coro::State coro, SV *coderef) |
|
|
1380 | CODE: |
|
|
1381 | { |
|
|
1382 | if (coro->mainstack) |
|
|
1383 | { |
|
|
1384 | struct coro temp; |
|
|
1385 | Zero (&temp, 1, struct coro); |
|
|
1386 | temp.save = CORO_SAVE_ALL; |
|
|
1387 | |
|
|
1388 | if (!(coro->flags & CF_RUNNING)) |
|
|
1389 | { |
|
|
1390 | save_perl (aTHX_ &temp); |
|
|
1391 | load_perl (aTHX_ coro); |
|
|
1392 | } |
|
|
1393 | |
|
|
1394 | { |
|
|
1395 | dSP; |
|
|
1396 | ENTER; |
|
|
1397 | SAVETMPS; |
|
|
1398 | PUSHMARK (SP); |
|
|
1399 | PUTBACK; |
|
|
1400 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1401 | SPAGAIN; |
|
|
1402 | FREETMPS; |
|
|
1403 | LEAVE; |
|
|
1404 | PUTBACK; |
|
|
1405 | } |
|
|
1406 | |
|
|
1407 | if (!(coro->flags & CF_RUNNING)) |
|
|
1408 | { |
|
|
1409 | save_perl (aTHX_ coro); |
|
|
1410 | load_perl (aTHX_ &temp); |
|
|
1411 | } |
|
|
1412 | } |
|
|
1413 | } |
|
|
1414 | |
|
|
1415 | SV * |
|
|
1416 | is_ready (Coro::State coro) |
|
|
1417 | PROTOTYPE: $ |
|
|
1418 | ALIAS: |
|
|
1419 | is_ready = CF_READY |
|
|
1420 | is_running = CF_RUNNING |
|
|
1421 | is_new = CF_NEW |
|
|
1422 | is_destroyed = CF_DESTROYED |
|
|
1423 | CODE: |
|
|
1424 | RETVAL = boolSV (coro->flags & ix); |
|
|
1425 | OUTPUT: |
|
|
1426 | RETVAL |
|
|
1427 | |
|
|
1428 | IV |
|
|
1429 | rss (Coro::State coro) |
|
|
1430 | PROTOTYPE: $ |
|
|
1431 | CODE: |
|
|
1432 | RETVAL = coro_rss (coro); |
|
|
1433 | OUTPUT: |
|
|
1434 | RETVAL |
|
|
1435 | |
1267 | |
1436 | |
1268 | MODULE = Coro::State PACKAGE = Coro |
1437 | MODULE = Coro::State PACKAGE = Coro |
1269 | |
1438 | |
1270 | BOOT: |
1439 | BOOT: |
1271 | { |
1440 | { |
… | |
… | |
1339 | CODE: |
1508 | CODE: |
1340 | RETVAL = boolSV (api_ready (self)); |
1509 | RETVAL = boolSV (api_ready (self)); |
1341 | OUTPUT: |
1510 | OUTPUT: |
1342 | RETVAL |
1511 | RETVAL |
1343 | |
1512 | |
1344 | SV * |
|
|
1345 | is_ready (SV *self) |
|
|
1346 | PROTOTYPE: $ |
|
|
1347 | CODE: |
|
|
1348 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1349 | OUTPUT: |
|
|
1350 | RETVAL |
|
|
1351 | |
|
|
1352 | int |
1513 | int |
1353 | nready (...) |
1514 | nready (...) |
1354 | PROTOTYPE: |
1515 | PROTOTYPE: |
1355 | CODE: |
1516 | CODE: |
1356 | RETVAL = coro_nready; |
1517 | RETVAL = coro_nready; |