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. */ |
… | |
… | |
112 | #else |
117 | #else |
113 | # define LOCK (void)0 |
118 | # define LOCK (void)0 |
114 | # define UNLOCK (void)0 |
119 | # define UNLOCK (void)0 |
115 | #endif |
120 | #endif |
116 | |
121 | |
|
|
122 | /* helper storage struct for Coro::AIO */ |
117 | struct io_state |
123 | struct io_state |
118 | { |
124 | { |
119 | int errorno; |
125 | int errorno; |
120 | I32 laststype; |
126 | I32 laststype; |
121 | int laststatval; |
127 | int laststatval; |
122 | Stat_t statcache; |
128 | Stat_t statcache; |
123 | }; |
129 | }; |
124 | |
130 | |
|
|
131 | static size_t coro_stacksize = CORO_STACKSIZE; |
125 | static struct CoroAPI coroapi; |
132 | static struct CoroAPI coroapi; |
126 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
133 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
127 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
128 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
129 | |
136 | |
… | |
… | |
134 | typedef struct coro_cctx { |
141 | typedef struct coro_cctx { |
135 | struct coro_cctx *next; |
142 | struct coro_cctx *next; |
136 | |
143 | |
137 | /* the stack */ |
144 | /* the stack */ |
138 | void *sptr; |
145 | void *sptr; |
139 | long ssize; /* positive == mmap, otherwise malloc */ |
146 | size_t ssize; |
140 | |
147 | |
141 | /* cpu state */ |
148 | /* cpu state */ |
142 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
149 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
150 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
143 | JMPENV *top_env; |
151 | JMPENV *top_env; |
144 | coro_context cctx; |
152 | coro_context cctx; |
145 | |
153 | |
146 | int inuse; |
|
|
147 | |
|
|
148 | #if USE_VALGRIND |
154 | #if CORO_USE_VALGRIND |
149 | int valgrind_id; |
155 | int valgrind_id; |
150 | #endif |
156 | #endif |
|
|
157 | char inuse, mapped; |
151 | } coro_cctx; |
158 | } coro_cctx; |
152 | |
159 | |
153 | enum { |
160 | enum { |
154 | CF_RUNNING = 0x0001, /* coroutine is running */ |
161 | CF_RUNNING = 0x0001, /* coroutine is running */ |
155 | CF_READY = 0x0002, /* coroutine is ready */ |
162 | CF_READY = 0x0002, /* coroutine is ready */ |
156 | CF_NEW = 0x0004, /* ahs never been switched to */ |
163 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
164 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
157 | }; |
165 | }; |
158 | |
166 | |
159 | /* this is a structure representing a perl-level coroutine */ |
167 | /* this is a structure representing a perl-level coroutine */ |
160 | struct coro { |
168 | struct coro { |
161 | /* the c coroutine allocated to this perl coroutine, if any */ |
169 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
183 | }; |
191 | }; |
184 | |
192 | |
185 | typedef struct coro *Coro__State; |
193 | typedef struct coro *Coro__State; |
186 | typedef struct coro *Coro__State_or_hashref; |
194 | typedef struct coro *Coro__State_or_hashref; |
187 | |
195 | |
|
|
196 | /** Coro ********************************************************************/ |
|
|
197 | |
|
|
198 | #define PRIO_MAX 3 |
|
|
199 | #define PRIO_HIGH 1 |
|
|
200 | #define PRIO_NORMAL 0 |
|
|
201 | #define PRIO_LOW -1 |
|
|
202 | #define PRIO_IDLE -3 |
|
|
203 | #define PRIO_MIN -4 |
|
|
204 | |
|
|
205 | /* for Coro.pm */ |
|
|
206 | static SV *coro_current; |
|
|
207 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
208 | static int coro_nready; |
|
|
209 | |
|
|
210 | /** lowlevel stuff **********************************************************/ |
|
|
211 | |
188 | static AV * |
212 | static AV * |
189 | coro_clone_padlist (CV *cv) |
213 | coro_clone_padlist (pTHX_ CV *cv) |
190 | { |
214 | { |
191 | AV *padlist = CvPADLIST (cv); |
215 | AV *padlist = CvPADLIST (cv); |
192 | AV *newpadlist, *newpad; |
216 | AV *newpadlist, *newpad; |
193 | |
217 | |
194 | newpadlist = newAV (); |
218 | newpadlist = newAV (); |
… | |
… | |
206 | |
230 | |
207 | return newpadlist; |
231 | return newpadlist; |
208 | } |
232 | } |
209 | |
233 | |
210 | static void |
234 | static void |
211 | free_padlist (AV *padlist) |
235 | free_padlist (pTHX_ AV *padlist) |
212 | { |
236 | { |
213 | /* may be during global destruction */ |
237 | /* may be during global destruction */ |
214 | if (SvREFCNT (padlist)) |
238 | if (SvREFCNT (padlist)) |
215 | { |
239 | { |
216 | I32 i = AvFILLp (padlist); |
240 | I32 i = AvFILLp (padlist); |
… | |
… | |
237 | AV *padlist; |
261 | AV *padlist; |
238 | AV *av = (AV *)mg->mg_obj; |
262 | AV *av = (AV *)mg->mg_obj; |
239 | |
263 | |
240 | /* casting is fun. */ |
264 | /* casting is fun. */ |
241 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
265 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
242 | free_padlist (padlist); |
266 | free_padlist (aTHX_ padlist); |
243 | |
267 | |
244 | SvREFCNT_dec (av); |
268 | SvREFCNT_dec (av); |
245 | |
269 | |
246 | return 0; |
270 | return 0; |
247 | } |
271 | } |
… | |
… | |
257 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
281 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
258 | : 0 |
282 | : 0 |
259 | |
283 | |
260 | /* the next two functions merely cache the padlists */ |
284 | /* the next two functions merely cache the padlists */ |
261 | static void |
285 | static void |
262 | get_padlist (CV *cv) |
286 | get_padlist (pTHX_ CV *cv) |
263 | { |
287 | { |
264 | MAGIC *mg = CORO_MAGIC (cv); |
288 | MAGIC *mg = CORO_MAGIC (cv); |
265 | AV *av; |
289 | AV *av; |
266 | |
290 | |
267 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
291 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
268 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
292 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
269 | else |
293 | else |
270 | { |
294 | { |
271 | #if PREFER_PERL_FUNCTIONS |
295 | #if CORO_PREFER_PERL_FUNCTIONS |
272 | /* this is probably cleaner, but also slower? */ |
296 | /* this is probably cleaner, but also slower? */ |
273 | CV *cp = Perl_cv_clone (cv); |
297 | CV *cp = Perl_cv_clone (cv); |
274 | CvPADLIST (cv) = CvPADLIST (cp); |
298 | CvPADLIST (cv) = CvPADLIST (cp); |
275 | CvPADLIST (cp) = 0; |
299 | CvPADLIST (cp) = 0; |
276 | SvREFCNT_dec (cp); |
300 | SvREFCNT_dec (cp); |
277 | #else |
301 | #else |
278 | CvPADLIST (cv) = coro_clone_padlist (cv); |
302 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
279 | #endif |
303 | #endif |
280 | } |
304 | } |
281 | } |
305 | } |
282 | |
306 | |
283 | static void |
307 | static void |
284 | put_padlist (CV *cv) |
308 | put_padlist (pTHX_ CV *cv) |
285 | { |
309 | { |
286 | MAGIC *mg = CORO_MAGIC (cv); |
310 | MAGIC *mg = CORO_MAGIC (cv); |
287 | AV *av; |
311 | AV *av; |
288 | |
312 | |
289 | if (!mg) |
313 | if (!mg) |
… | |
… | |
300 | av_extend (av, AvMAX (av) + 1); |
324 | av_extend (av, AvMAX (av) + 1); |
301 | |
325 | |
302 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
326 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
303 | } |
327 | } |
304 | |
328 | |
|
|
329 | /** load & save, init *******************************************************/ |
|
|
330 | |
305 | #define SB do { |
331 | #define SB do { |
306 | #define SE } while (0) |
332 | #define SE } while (0) |
307 | |
333 | |
308 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
334 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
309 | |
335 | |
310 | static void |
336 | static void |
311 | load_perl (Coro__State c) |
337 | load_perl (pTHX_ Coro__State c) |
312 | { |
338 | { |
313 | #define VAR(name,type) PL_ ## name = c->name; |
339 | #define VAR(name,type) PL_ ## name = c->name; |
314 | # include "state.h" |
340 | # include "state.h" |
315 | #undef VAR |
341 | #undef VAR |
316 | |
342 | |
… | |
… | |
334 | CV *cv; |
360 | CV *cv; |
335 | |
361 | |
336 | /* now do the ugly restore mess */ |
362 | /* now do the ugly restore mess */ |
337 | while ((cv = (CV *)POPs)) |
363 | while ((cv = (CV *)POPs)) |
338 | { |
364 | { |
339 | put_padlist (cv); /* mark this padlist as available */ |
365 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
340 | CvDEPTH (cv) = PTR2IV (POPs); |
366 | CvDEPTH (cv) = PTR2IV (POPs); |
341 | CvPADLIST (cv) = (AV *)POPs; |
367 | CvPADLIST (cv) = (AV *)POPs; |
342 | } |
368 | } |
343 | |
369 | |
344 | PUTBACK; |
370 | PUTBACK; |
345 | } |
371 | } |
|
|
372 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
346 | } |
373 | } |
347 | |
374 | |
348 | static void |
375 | static void |
349 | save_perl (Coro__State c) |
376 | save_perl (pTHX_ Coro__State c) |
350 | { |
377 | { |
|
|
378 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
351 | { |
379 | { |
352 | dSP; |
380 | dSP; |
353 | I32 cxix = cxstack_ix; |
381 | I32 cxix = cxstack_ix; |
354 | PERL_CONTEXT *ccstk = cxstack; |
382 | PERL_CONTEXT *ccstk = cxstack; |
355 | PERL_SI *top_si = PL_curstackinfo; |
383 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
378 | PUSHs ((SV *)CvPADLIST (cv)); |
406 | PUSHs ((SV *)CvPADLIST (cv)); |
379 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
407 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
380 | PUSHs ((SV *)cv); |
408 | PUSHs ((SV *)cv); |
381 | |
409 | |
382 | CvDEPTH (cv) = 0; |
410 | CvDEPTH (cv) = 0; |
383 | get_padlist (cv); |
411 | get_padlist (aTHX_ cv); |
384 | } |
412 | } |
385 | } |
413 | } |
386 | } |
414 | } |
387 | |
415 | |
388 | if (top_si->si_type == PERLSI_MAIN) |
416 | if (top_si->si_type == PERLSI_MAIN) |
… | |
… | |
410 | * allocate various perl stacks. This is an exact copy |
438 | * allocate various perl stacks. This is an exact copy |
411 | * of perl.c:init_stacks, except that it uses less memory |
439 | * of perl.c:init_stacks, except that it uses less memory |
412 | * on the (sometimes correct) assumption that coroutines do |
440 | * on the (sometimes correct) assumption that coroutines do |
413 | * not usually need a lot of stackspace. |
441 | * not usually need a lot of stackspace. |
414 | */ |
442 | */ |
415 | #if PREFER_PERL_FUNCTIONS |
443 | #if CORO_PREFER_PERL_FUNCTIONS |
416 | # define coro_init_stacks init_stacks |
444 | # define coro_init_stacks init_stacks |
417 | #else |
445 | #else |
418 | static void |
446 | static void |
419 | coro_init_stacks () |
447 | coro_init_stacks (pTHX) |
420 | { |
448 | { |
421 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
449 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
422 | PL_curstackinfo->si_type = PERLSI_MAIN; |
450 | PL_curstackinfo->si_type = PERLSI_MAIN; |
423 | PL_curstack = PL_curstackinfo->si_stack; |
451 | PL_curstack = PL_curstackinfo->si_stack; |
424 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
452 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
… | |
… | |
458 | |
486 | |
459 | /* |
487 | /* |
460 | * destroy the stacks, the callchain etc... |
488 | * destroy the stacks, the callchain etc... |
461 | */ |
489 | */ |
462 | static void |
490 | static void |
463 | coro_destroy_stacks () |
491 | coro_destroy_stacks (pTHX) |
464 | { |
492 | { |
465 | if (!IN_DESTRUCT) |
493 | if (!IN_DESTRUCT) |
466 | { |
494 | { |
467 | /* restore all saved variables and stuff */ |
495 | /* restore all saved variables and stuff */ |
468 | LEAVE_SCOPE (0); |
496 | LEAVE_SCOPE (0); |
… | |
… | |
470 | |
498 | |
471 | /* free all temporaries */ |
499 | /* free all temporaries */ |
472 | FREETMPS; |
500 | FREETMPS; |
473 | assert (PL_tmps_ix == -1); |
501 | assert (PL_tmps_ix == -1); |
474 | |
502 | |
|
|
503 | /* unwind all extra stacks */ |
475 | POPSTACK_TO (PL_mainstack); |
504 | POPSTACK_TO (PL_mainstack); |
|
|
505 | |
|
|
506 | /* unwind main stack */ |
|
|
507 | dounwind (-1); |
476 | } |
508 | } |
477 | |
509 | |
478 | while (PL_curstackinfo->si_next) |
510 | while (PL_curstackinfo->si_next) |
479 | PL_curstackinfo = PL_curstackinfo->si_next; |
511 | PL_curstackinfo = PL_curstackinfo->si_next; |
480 | |
512 | |
… | |
… | |
497 | #if !PERL_VERSION_ATLEAST (5,9,0) |
529 | #if !PERL_VERSION_ATLEAST (5,9,0) |
498 | Safefree (PL_retstack); |
530 | Safefree (PL_retstack); |
499 | #endif |
531 | #endif |
500 | } |
532 | } |
501 | |
533 | |
|
|
534 | /** coroutine stack handling ************************************************/ |
|
|
535 | |
502 | static void |
536 | static void |
503 | setup_coro (struct coro *coro) |
537 | setup_coro (pTHX_ struct coro *coro) |
504 | { |
538 | { |
505 | /* |
539 | /* |
506 | * emulate part of the perl startup here. |
540 | * emulate part of the perl startup here. |
507 | */ |
541 | */ |
508 | |
|
|
509 | coro_init_stacks (); |
542 | coro_init_stacks (aTHX); |
510 | |
543 | |
511 | PL_curcop = &PL_compiling; |
544 | PL_curcop = &PL_compiling; |
512 | PL_in_eval = EVAL_NULL; |
545 | PL_in_eval = EVAL_NULL; |
|
|
546 | PL_comppad = 0; |
513 | PL_curpm = 0; |
547 | PL_curpm = 0; |
514 | PL_localizing = 0; |
548 | PL_localizing = 0; |
515 | PL_dirty = 0; |
549 | PL_dirty = 0; |
516 | PL_restartop = 0; |
550 | PL_restartop = 0; |
517 | |
551 | |
… | |
… | |
536 | |
570 | |
537 | ENTER; /* necessary e.g. for dounwind */ |
571 | ENTER; /* necessary e.g. for dounwind */ |
538 | } |
572 | } |
539 | |
573 | |
540 | static void |
574 | static void |
541 | free_coro_mortal () |
575 | free_coro_mortal (pTHX) |
542 | { |
576 | { |
543 | if (coro_mortal) |
577 | if (coro_mortal) |
544 | { |
578 | { |
545 | SvREFCNT_dec (coro_mortal); |
579 | SvREFCNT_dec (coro_mortal); |
546 | coro_mortal = 0; |
580 | coro_mortal = 0; |
547 | } |
581 | } |
548 | } |
582 | } |
549 | |
583 | |
550 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
584 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
551 | static void NOINLINE |
585 | static void NOINLINE |
552 | prepare_cctx (coro_cctx *cctx) |
586 | prepare_cctx (pTHX_ coro_cctx *cctx) |
553 | { |
587 | { |
554 | dSP; |
588 | dSP; |
555 | LOGOP myop; |
589 | LOGOP myop; |
556 | |
590 | |
557 | Zero (&myop, 1, LOGOP); |
591 | Zero (&myop, 1, LOGOP); |
… | |
… | |
569 | } |
603 | } |
570 | |
604 | |
571 | static void |
605 | static void |
572 | coro_run (void *arg) |
606 | coro_run (void *arg) |
573 | { |
607 | { |
|
|
608 | dTHX; |
|
|
609 | |
574 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
610 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
575 | UNLOCK; |
611 | UNLOCK; |
576 | |
612 | |
577 | /* |
613 | /* |
578 | * this is a _very_ stripped down perl interpreter ;) |
614 | * this is a _very_ stripped down perl interpreter ;) |
579 | */ |
615 | */ |
580 | PL_top_env = &PL_start_env; |
616 | PL_top_env = &PL_start_env; |
581 | |
617 | |
582 | /* inject call to cctx_init */ |
618 | /* inject call to cctx_init */ |
583 | prepare_cctx ((coro_cctx *)arg); |
619 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
584 | |
620 | |
585 | /* somebody will hit me for both perl_run and PL_restartop */ |
621 | /* somebody will hit me for both perl_run and PL_restartop */ |
586 | PL_restartop = PL_op; |
622 | PL_restartop = PL_op; |
587 | perl_run (PL_curinterp); |
623 | perl_run (PL_curinterp); |
588 | |
624 | |
… | |
… | |
592 | |
628 | |
593 | static coro_cctx * |
629 | static coro_cctx * |
594 | cctx_new () |
630 | cctx_new () |
595 | { |
631 | { |
596 | coro_cctx *cctx; |
632 | coro_cctx *cctx; |
|
|
633 | void *stack_start; |
|
|
634 | size_t stack_size; |
597 | |
635 | |
598 | ++cctx_count; |
636 | ++cctx_count; |
599 | |
637 | |
600 | Newz (0, cctx, 1, coro_cctx); |
638 | Newz (0, cctx, 1, coro_cctx); |
601 | |
639 | |
602 | #if HAVE_MMAP |
640 | #if HAVE_MMAP |
603 | |
641 | |
604 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
642 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
605 | /* mmap supposedly does allocate-on-write for us */ |
643 | /* mmap supposedly does allocate-on-write for us */ |
606 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
644 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
607 | |
645 | |
608 | if (cctx->sptr == (void *)-1) |
646 | if (cctx->sptr != (void *)-1) |
609 | { |
|
|
610 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
611 | _exit (EXIT_FAILURE); |
|
|
612 | } |
647 | { |
613 | |
|
|
614 | # if STACKGUARD |
648 | # if CORO_STACKGUARD |
615 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
649 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
616 | # endif |
650 | # endif |
|
|
651 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
|
|
652 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
653 | cctx->mapped = 1; |
|
|
654 | } |
|
|
655 | else |
|
|
656 | #endif |
|
|
657 | { |
|
|
658 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
|
|
659 | New (0, cctx->sptr, coro_stacksize, long); |
617 | |
660 | |
618 | #else |
|
|
619 | |
|
|
620 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
621 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
622 | |
|
|
623 | if (!cctx->sptr) |
661 | if (!cctx->sptr) |
624 | { |
662 | { |
625 | perror ("FATAL: unable to malloc stack for coroutine"); |
663 | perror ("FATAL: unable to allocate stack for coroutine"); |
626 | _exit (EXIT_FAILURE); |
664 | _exit (EXIT_FAILURE); |
627 | } |
665 | } |
628 | |
666 | |
629 | #endif |
667 | stack_start = cctx->sptr; |
|
|
668 | stack_size = cctx->ssize; |
|
|
669 | } |
630 | |
670 | |
631 | #if USE_VALGRIND |
671 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
632 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
633 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
634 | cctx->ssize + (char *)cctx->sptr |
|
|
635 | ); |
|
|
636 | #endif |
|
|
637 | |
|
|
638 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
672 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
639 | |
673 | |
640 | return cctx; |
674 | return cctx; |
641 | } |
675 | } |
642 | |
676 | |
643 | static void |
677 | static void |
… | |
… | |
646 | if (!cctx) |
680 | if (!cctx) |
647 | return; |
681 | return; |
648 | |
682 | |
649 | --cctx_count; |
683 | --cctx_count; |
650 | |
684 | |
651 | #if USE_VALGRIND |
685 | #if CORO_USE_VALGRIND |
652 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
686 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
653 | #endif |
687 | #endif |
654 | |
688 | |
655 | #if HAVE_MMAP |
689 | #if HAVE_MMAP |
|
|
690 | if (cctx->mapped) |
656 | munmap (cctx->sptr, cctx->ssize); |
691 | munmap (cctx->sptr, cctx->ssize); |
657 | #else |
692 | else |
|
|
693 | #endif |
658 | Safefree (cctx->sptr); |
694 | Safefree (cctx->sptr); |
659 | #endif |
|
|
660 | |
695 | |
661 | Safefree (cctx); |
696 | Safefree (cctx); |
662 | } |
697 | } |
663 | |
698 | |
664 | static coro_cctx * |
699 | static coro_cctx * |
665 | cctx_get () |
700 | cctx_get (pTHX) |
666 | { |
701 | { |
667 | coro_cctx *cctx; |
|
|
668 | |
|
|
669 | if (cctx_first) |
702 | while (cctx_first) |
670 | { |
703 | { |
671 | cctx = cctx_first; |
704 | coro_cctx *cctx = cctx_first; |
672 | cctx_first = cctx->next; |
705 | cctx_first = cctx->next; |
673 | --cctx_idle; |
706 | --cctx_idle; |
|
|
707 | |
|
|
708 | if (cctx->ssize >= coro_stacksize) |
|
|
709 | return cctx; |
|
|
710 | |
|
|
711 | cctx_destroy (cctx); |
674 | } |
712 | } |
675 | else |
713 | |
676 | { |
|
|
677 | cctx = cctx_new (); |
|
|
678 | PL_op = PL_op->op_next; |
714 | PL_op = PL_op->op_next; |
679 | } |
|
|
680 | |
|
|
681 | return cctx; |
715 | return cctx_new (); |
682 | } |
716 | } |
683 | |
717 | |
684 | static void |
718 | static void |
685 | cctx_put (coro_cctx *cctx) |
719 | cctx_put (coro_cctx *cctx) |
686 | { |
720 | { |
… | |
… | |
698 | ++cctx_idle; |
732 | ++cctx_idle; |
699 | cctx->next = cctx_first; |
733 | cctx->next = cctx_first; |
700 | cctx_first = cctx; |
734 | cctx_first = cctx; |
701 | } |
735 | } |
702 | |
736 | |
|
|
737 | /** coroutine switching *****************************************************/ |
|
|
738 | |
703 | /* never call directly, always through the coro_state_transfer global variable */ |
739 | /* never call directly, always through the coro_state_transfer global variable */ |
704 | static void NOINLINE |
740 | static void NOINLINE |
705 | transfer (struct coro *prev, struct coro *next) |
741 | transfer (pTHX_ struct coro *prev, struct coro *next) |
706 | { |
742 | { |
707 | dSTACKLEVEL; |
743 | dSTACKLEVEL; |
708 | |
744 | |
709 | /* sometimes transfer is only called to set idle_sp */ |
745 | /* sometimes transfer is only called to set idle_sp */ |
710 | if (!next) |
746 | if (!next) |
711 | { |
747 | { |
712 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
748 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
713 | assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ |
749 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
714 | } |
750 | } |
715 | else if (prev != next) |
751 | else if (prev != next) |
716 | { |
752 | { |
717 | coro_cctx *prev__cctx; |
753 | coro_cctx *prev__cctx; |
718 | |
754 | |
… | |
… | |
730 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
766 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
731 | |
767 | |
732 | if (next->flags & CF_RUNNING) |
768 | if (next->flags & CF_RUNNING) |
733 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
769 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
734 | |
770 | |
|
|
771 | if (next->flags & CF_DESTROYED) |
|
|
772 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
773 | |
735 | prev->flags &= ~CF_RUNNING; |
774 | prev->flags &= ~CF_RUNNING; |
736 | next->flags |= CF_RUNNING; |
775 | next->flags |= CF_RUNNING; |
737 | |
776 | |
738 | LOCK; |
777 | LOCK; |
739 | |
778 | |
740 | if (next->flags & CF_NEW) |
779 | if (next->flags & CF_NEW) |
741 | { |
780 | { |
742 | /* need to start coroutine */ |
781 | /* need to start coroutine */ |
743 | next->flags &= ~CF_NEW; |
782 | next->flags &= ~CF_NEW; |
744 | /* first get rid of the old state */ |
783 | /* first get rid of the old state */ |
745 | save_perl (prev); |
784 | save_perl (aTHX_ prev); |
746 | /* setup coroutine call */ |
785 | /* setup coroutine call */ |
747 | setup_coro (next); |
786 | setup_coro (aTHX_ next); |
748 | /* need a new stack */ |
787 | /* need a new stack */ |
749 | assert (!next->cctx); |
788 | assert (!next->cctx); |
750 | } |
789 | } |
751 | else |
790 | else |
752 | { |
791 | { |
753 | /* coroutine already started */ |
792 | /* coroutine already started */ |
754 | save_perl (prev); |
793 | save_perl (aTHX_ prev); |
755 | load_perl (next); |
794 | load_perl (aTHX_ next); |
756 | } |
795 | } |
757 | |
796 | |
758 | prev__cctx = prev->cctx; |
797 | prev__cctx = prev->cctx; |
759 | |
798 | |
760 | /* possibly "free" the cctx */ |
799 | /* possibly "free" the cctx */ |
761 | if (prev__cctx->idle_sp == STACKLEVEL) |
800 | if (prev__cctx->idle_sp == STACKLEVEL) |
762 | { |
801 | { |
763 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
802 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
764 | assert (PL_top_env == prev__cctx->top_env); |
803 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
765 | |
804 | |
766 | prev->cctx = 0; |
805 | prev->cctx = 0; |
767 | |
806 | |
768 | cctx_put (prev__cctx); |
807 | cctx_put (prev__cctx); |
769 | prev__cctx->inuse = 0; |
808 | prev__cctx->inuse = 0; |
770 | } |
809 | } |
771 | |
810 | |
772 | if (!next->cctx) |
811 | if (!next->cctx) |
773 | { |
812 | { |
774 | next->cctx = cctx_get (); |
813 | next->cctx = cctx_get (aTHX); |
775 | assert (!next->cctx->inuse); |
814 | assert (!next->cctx->inuse); |
776 | next->cctx->inuse = 1; |
815 | next->cctx->inuse = 1; |
777 | } |
816 | } |
778 | |
817 | |
779 | if (prev__cctx != next->cctx) |
818 | if (prev__cctx != next->cctx) |
… | |
… | |
781 | prev__cctx->top_env = PL_top_env; |
820 | prev__cctx->top_env = PL_top_env; |
782 | PL_top_env = next->cctx->top_env; |
821 | PL_top_env = next->cctx->top_env; |
783 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
822 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
784 | } |
823 | } |
785 | |
824 | |
786 | free_coro_mortal (); |
825 | free_coro_mortal (aTHX); |
787 | |
|
|
788 | UNLOCK; |
826 | UNLOCK; |
789 | } |
827 | } |
790 | } |
828 | } |
791 | |
829 | |
792 | struct transfer_args |
830 | struct transfer_args |
793 | { |
831 | { |
794 | struct coro *prev, *next; |
832 | struct coro *prev, *next; |
795 | }; |
833 | }; |
796 | |
834 | |
797 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
835 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
798 | |
836 | |
|
|
837 | /** high level stuff ********************************************************/ |
|
|
838 | |
799 | static void |
839 | static int |
800 | coro_state_destroy (struct coro *coro) |
840 | coro_state_destroy (pTHX_ struct coro *coro) |
801 | { |
841 | { |
802 | if (coro->refcnt--) |
842 | if (coro->flags & CF_DESTROYED) |
803 | return; |
843 | return 0; |
|
|
844 | |
|
|
845 | coro->flags |= CF_DESTROYED; |
|
|
846 | |
|
|
847 | if (coro->flags & CF_READY) |
|
|
848 | { |
|
|
849 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
|
|
850 | /* alternative: look through all ready queues and remove the coro */ |
|
|
851 | LOCK; |
|
|
852 | --coro_nready; |
|
|
853 | UNLOCK; |
|
|
854 | } |
|
|
855 | else |
|
|
856 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
804 | |
857 | |
805 | if (coro->mainstack && coro->mainstack != main_mainstack) |
858 | if (coro->mainstack && coro->mainstack != main_mainstack) |
806 | { |
859 | { |
807 | struct coro temp; |
860 | struct coro temp; |
|
|
861 | |
|
|
862 | assert (!(coro->flags & CF_RUNNING)); |
|
|
863 | |
808 | Zero (&temp, 1, struct coro); |
864 | Zero (&temp, 1, struct coro); |
809 | temp.save = CORO_SAVE_ALL; |
865 | temp.save = CORO_SAVE_ALL; |
810 | |
866 | |
811 | if (coro->flags & CF_RUNNING) |
867 | if (coro->flags & CF_RUNNING) |
812 | croak ("FATAL: tried to destroy currently running coroutine"); |
868 | croak ("FATAL: tried to destroy currently running coroutine"); |
813 | |
869 | |
814 | save_perl (&temp); |
870 | save_perl (aTHX_ &temp); |
815 | load_perl (coro); |
871 | load_perl (aTHX_ coro); |
816 | |
872 | |
817 | coro_destroy_stacks (); |
873 | coro_destroy_stacks (aTHX); |
818 | |
874 | |
819 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
875 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
820 | |
876 | |
821 | coro->mainstack = 0; |
877 | coro->mainstack = 0; |
822 | } |
878 | } |
823 | |
879 | |
824 | cctx_destroy (coro->cctx); |
880 | cctx_destroy (coro->cctx); |
825 | SvREFCNT_dec (coro->args); |
881 | SvREFCNT_dec (coro->args); |
826 | Safefree (coro); |
882 | |
|
|
883 | return 1; |
827 | } |
884 | } |
828 | |
885 | |
829 | static int |
886 | static int |
830 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
887 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
831 | { |
888 | { |
832 | struct coro *coro = (struct coro *)mg->mg_ptr; |
889 | struct coro *coro = (struct coro *)mg->mg_ptr; |
833 | mg->mg_ptr = 0; |
890 | mg->mg_ptr = 0; |
834 | |
891 | |
|
|
892 | if (--coro->refcnt < 0) |
|
|
893 | { |
835 | coro_state_destroy (coro); |
894 | coro_state_destroy (aTHX_ coro); |
|
|
895 | Safefree (coro); |
|
|
896 | } |
836 | |
897 | |
837 | return 0; |
898 | return 0; |
838 | } |
899 | } |
839 | |
900 | |
840 | static int |
901 | static int |
… | |
… | |
847 | return 0; |
908 | return 0; |
848 | } |
909 | } |
849 | |
910 | |
850 | static MGVTBL coro_state_vtbl = { |
911 | static MGVTBL coro_state_vtbl = { |
851 | 0, 0, 0, 0, |
912 | 0, 0, 0, 0, |
852 | coro_state_clear, |
913 | coro_state_free, |
853 | 0, |
914 | 0, |
854 | #ifdef MGf_DUP |
915 | #ifdef MGf_DUP |
855 | coro_state_dup, |
916 | coro_state_dup, |
856 | #else |
917 | #else |
857 | # define MGf_DUP 0 |
918 | # define MGf_DUP 0 |
858 | #endif |
919 | #endif |
859 | }; |
920 | }; |
860 | |
921 | |
861 | static struct coro * |
922 | static struct coro * |
862 | SvSTATE (SV *coro) |
923 | SvSTATE_ (pTHX_ SV *coro) |
863 | { |
924 | { |
864 | HV *stash; |
925 | HV *stash; |
865 | MAGIC *mg; |
926 | MAGIC *mg; |
866 | |
927 | |
867 | if (SvROK (coro)) |
928 | if (SvROK (coro)) |
… | |
… | |
873 | /* very slow, but rare, check */ |
934 | /* very slow, but rare, check */ |
874 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
935 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
875 | croak ("Coro::State object required"); |
936 | croak ("Coro::State object required"); |
876 | } |
937 | } |
877 | |
938 | |
878 | mg = SvMAGIC (coro); |
939 | mg = CORO_MAGIC (coro); |
879 | assert (mg->mg_type == PERL_MAGIC_ext); |
|
|
880 | return (struct coro *)mg->mg_ptr; |
940 | return (struct coro *)mg->mg_ptr; |
881 | } |
941 | } |
882 | |
942 | |
|
|
943 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
944 | |
883 | static void |
945 | static void |
884 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
946 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
885 | { |
947 | { |
886 | ta->prev = SvSTATE (prev_sv); |
948 | ta->prev = SvSTATE (prev_sv); |
887 | ta->next = SvSTATE (next_sv); |
949 | ta->next = SvSTATE (next_sv); |
888 | } |
950 | } |
889 | |
951 | |
890 | static void |
952 | static void |
891 | api_transfer (SV *prev_sv, SV *next_sv) |
953 | api_transfer (SV *prev_sv, SV *next_sv) |
892 | { |
954 | { |
|
|
955 | dTHX; |
893 | struct transfer_args ta; |
956 | struct transfer_args ta; |
894 | |
957 | |
895 | prepare_transfer (&ta, prev_sv, next_sv); |
958 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
896 | TRANSFER (ta); |
959 | TRANSFER (ta); |
897 | } |
960 | } |
898 | |
961 | |
899 | static int |
962 | static int |
900 | api_save (SV *coro_sv, int new_save) |
963 | api_save (SV *coro_sv, int new_save) |
901 | { |
964 | { |
|
|
965 | dTHX; |
902 | struct coro *coro = SvSTATE (coro_sv); |
966 | struct coro *coro = SvSTATE (coro_sv); |
903 | int old_save = coro->save; |
967 | int old_save = coro->save; |
904 | |
968 | |
905 | if (new_save >= 0) |
969 | if (new_save >= 0) |
906 | coro->save = new_save; |
970 | coro->save = new_save; |
… | |
… | |
908 | return old_save; |
972 | return old_save; |
909 | } |
973 | } |
910 | |
974 | |
911 | /** Coro ********************************************************************/ |
975 | /** Coro ********************************************************************/ |
912 | |
976 | |
913 | #define PRIO_MAX 3 |
|
|
914 | #define PRIO_HIGH 1 |
|
|
915 | #define PRIO_NORMAL 0 |
|
|
916 | #define PRIO_LOW -1 |
|
|
917 | #define PRIO_IDLE -3 |
|
|
918 | #define PRIO_MIN -4 |
|
|
919 | |
|
|
920 | /* for Coro.pm */ |
|
|
921 | static SV *coro_current; |
|
|
922 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
923 | static int coro_nready; |
|
|
924 | |
|
|
925 | static void |
977 | static void |
926 | coro_enq (SV *coro_sv) |
978 | coro_enq (pTHX_ SV *coro_sv) |
927 | { |
979 | { |
928 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
980 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
929 | coro_nready++; |
|
|
930 | } |
981 | } |
931 | |
982 | |
932 | static SV * |
983 | static SV * |
933 | coro_deq (int min_prio) |
984 | coro_deq (pTHX_ int min_prio) |
934 | { |
985 | { |
935 | int prio = PRIO_MAX - PRIO_MIN; |
986 | int prio = PRIO_MAX - PRIO_MIN; |
936 | |
987 | |
937 | min_prio -= PRIO_MIN; |
988 | min_prio -= PRIO_MIN; |
938 | if (min_prio < 0) |
989 | if (min_prio < 0) |
939 | min_prio = 0; |
990 | min_prio = 0; |
940 | |
991 | |
941 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
992 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
942 | if (AvFILLp (coro_ready [prio]) >= 0) |
993 | if (AvFILLp (coro_ready [prio]) >= 0) |
943 | { |
|
|
944 | coro_nready--; |
|
|
945 | return av_shift (coro_ready [prio]); |
994 | return av_shift (coro_ready [prio]); |
946 | } |
|
|
947 | |
995 | |
948 | return 0; |
996 | return 0; |
949 | } |
997 | } |
950 | |
998 | |
951 | static int |
999 | static int |
952 | api_ready (SV *coro_sv) |
1000 | api_ready (SV *coro_sv) |
953 | { |
1001 | { |
|
|
1002 | dTHX; |
954 | struct coro *coro; |
1003 | struct coro *coro; |
955 | |
1004 | |
956 | if (SvROK (coro_sv)) |
1005 | if (SvROK (coro_sv)) |
957 | coro_sv = SvRV (coro_sv); |
1006 | coro_sv = SvRV (coro_sv); |
958 | |
1007 | |
… | |
… | |
962 | return 0; |
1011 | return 0; |
963 | |
1012 | |
964 | coro->flags |= CF_READY; |
1013 | coro->flags |= CF_READY; |
965 | |
1014 | |
966 | LOCK; |
1015 | LOCK; |
967 | coro_enq (SvREFCNT_inc (coro_sv)); |
1016 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
|
|
1017 | ++coro_nready; |
968 | UNLOCK; |
1018 | UNLOCK; |
969 | |
1019 | |
970 | return 1; |
1020 | return 1; |
971 | } |
1021 | } |
972 | |
1022 | |
973 | static int |
1023 | static int |
974 | api_is_ready (SV *coro_sv) |
1024 | api_is_ready (SV *coro_sv) |
975 | { |
1025 | { |
|
|
1026 | dTHX; |
976 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1027 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
977 | } |
1028 | } |
978 | |
1029 | |
979 | static void |
1030 | static void |
980 | prepare_schedule (struct transfer_args *ta) |
1031 | prepare_schedule (pTHX_ struct transfer_args *ta) |
981 | { |
1032 | { |
982 | SV *prev, *next; |
1033 | SV *prev_sv, *next_sv; |
983 | |
1034 | |
984 | for (;;) |
1035 | for (;;) |
985 | { |
1036 | { |
986 | LOCK; |
1037 | LOCK; |
987 | next = coro_deq (PRIO_MIN); |
1038 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
|
|
1039 | |
|
|
1040 | /* nothing to schedule: call the idle handler */ |
|
|
1041 | if (!next_sv) |
|
|
1042 | { |
|
|
1043 | dSP; |
|
|
1044 | UNLOCK; |
|
|
1045 | |
|
|
1046 | ENTER; |
|
|
1047 | SAVETMPS; |
|
|
1048 | |
|
|
1049 | PUSHMARK (SP); |
|
|
1050 | PUTBACK; |
|
|
1051 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1052 | |
|
|
1053 | FREETMPS; |
|
|
1054 | LEAVE; |
|
|
1055 | continue; |
|
|
1056 | } |
|
|
1057 | |
|
|
1058 | ta->next = SvSTATE (next_sv); |
|
|
1059 | |
|
|
1060 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1061 | if (ta->next->flags & CF_DESTROYED) |
|
|
1062 | { |
|
|
1063 | UNLOCK; |
|
|
1064 | SvREFCNT_dec (next_sv); |
|
|
1065 | /* coro_nready is already taken care of by destroy */ |
|
|
1066 | continue; |
|
|
1067 | } |
|
|
1068 | |
|
|
1069 | --coro_nready; |
988 | UNLOCK; |
1070 | UNLOCK; |
989 | |
|
|
990 | if (next) |
|
|
991 | break; |
1071 | break; |
992 | |
|
|
993 | { |
|
|
994 | dSP; |
|
|
995 | |
|
|
996 | ENTER; |
|
|
997 | SAVETMPS; |
|
|
998 | |
|
|
999 | PUSHMARK (SP); |
|
|
1000 | PUTBACK; |
|
|
1001 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1002 | |
|
|
1003 | FREETMPS; |
|
|
1004 | LEAVE; |
|
|
1005 | } |
1072 | } |
1006 | } |
|
|
1007 | |
|
|
1008 | prev = SvRV (coro_current); |
|
|
1009 | SvRV_set (coro_current, next); |
|
|
1010 | |
1073 | |
1011 | /* free this only after the transfer */ |
1074 | /* free this only after the transfer */ |
1012 | LOCK; |
1075 | prev_sv = SvRV (coro_current); |
1013 | free_coro_mortal (); |
1076 | SvRV_set (coro_current, next_sv); |
1014 | UNLOCK; |
|
|
1015 | coro_mortal = prev; |
|
|
1016 | |
|
|
1017 | assert (!SvROK(prev));//D |
|
|
1018 | assert (!SvROK(next));//D |
|
|
1019 | |
|
|
1020 | ta->prev = SvSTATE (prev); |
1077 | ta->prev = SvSTATE (prev_sv); |
1021 | ta->next = SvSTATE (next); |
|
|
1022 | |
1078 | |
1023 | assert (ta->next->flags & CF_READY); |
1079 | assert (ta->next->flags & CF_READY); |
1024 | ta->next->flags &= ~CF_READY; |
1080 | ta->next->flags &= ~CF_READY; |
1025 | } |
|
|
1026 | |
1081 | |
|
|
1082 | LOCK; |
|
|
1083 | free_coro_mortal (aTHX); |
|
|
1084 | coro_mortal = prev_sv; |
|
|
1085 | UNLOCK; |
|
|
1086 | } |
|
|
1087 | |
1027 | static void |
1088 | static void |
1028 | prepare_cede (struct transfer_args *ta) |
1089 | prepare_cede (pTHX_ struct transfer_args *ta) |
1029 | { |
1090 | { |
1030 | api_ready (coro_current); |
1091 | api_ready (coro_current); |
1031 | prepare_schedule (ta); |
1092 | prepare_schedule (aTHX_ ta); |
1032 | } |
1093 | } |
1033 | |
1094 | |
1034 | static int |
1095 | static int |
1035 | prepare_cede_notself (struct transfer_args *ta) |
1096 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1036 | { |
1097 | { |
1037 | if (coro_nready) |
1098 | if (coro_nready) |
1038 | { |
1099 | { |
1039 | SV *prev = SvRV (coro_current); |
1100 | SV *prev = SvRV (coro_current); |
1040 | prepare_schedule (ta); |
1101 | prepare_schedule (aTHX_ ta); |
1041 | api_ready (prev); |
1102 | api_ready (prev); |
1042 | return 1; |
1103 | return 1; |
1043 | } |
1104 | } |
1044 | else |
1105 | else |
1045 | return 0; |
1106 | return 0; |
1046 | } |
1107 | } |
1047 | |
1108 | |
1048 | static void |
1109 | static void |
1049 | api_schedule (void) |
1110 | api_schedule (void) |
1050 | { |
1111 | { |
|
|
1112 | dTHX; |
1051 | struct transfer_args ta; |
1113 | struct transfer_args ta; |
1052 | |
1114 | |
1053 | prepare_schedule (&ta); |
1115 | prepare_schedule (aTHX_ &ta); |
1054 | TRANSFER (ta); |
1116 | TRANSFER (ta); |
1055 | } |
1117 | } |
1056 | |
1118 | |
1057 | static int |
1119 | static int |
1058 | api_cede (void) |
1120 | api_cede (void) |
1059 | { |
1121 | { |
|
|
1122 | dTHX; |
1060 | struct transfer_args ta; |
1123 | struct transfer_args ta; |
1061 | |
1124 | |
1062 | prepare_cede (&ta); |
1125 | prepare_cede (aTHX_ &ta); |
1063 | |
1126 | |
1064 | if (ta.prev != ta.next) |
1127 | if (ta.prev != ta.next) |
1065 | { |
1128 | { |
1066 | TRANSFER (ta); |
1129 | TRANSFER (ta); |
1067 | return 1; |
1130 | return 1; |
… | |
… | |
1071 | } |
1134 | } |
1072 | |
1135 | |
1073 | static int |
1136 | static int |
1074 | api_cede_notself (void) |
1137 | api_cede_notself (void) |
1075 | { |
1138 | { |
|
|
1139 | dTHX; |
1076 | struct transfer_args ta; |
1140 | struct transfer_args ta; |
1077 | |
1141 | |
1078 | if (prepare_cede_notself (&ta)) |
1142 | if (prepare_cede_notself (aTHX_ &ta)) |
1079 | { |
1143 | { |
1080 | TRANSFER (ta); |
1144 | TRANSFER (ta); |
1081 | return 1; |
1145 | return 1; |
1082 | } |
1146 | } |
1083 | else |
1147 | else |
… | |
… | |
1161 | |
1225 | |
1162 | case 1: |
1226 | case 1: |
1163 | if (items != 2) |
1227 | if (items != 2) |
1164 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1228 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1165 | |
1229 | |
1166 | prepare_transfer (&ta, ST (0), ST (1)); |
1230 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1167 | break; |
1231 | break; |
1168 | |
1232 | |
1169 | case 2: |
1233 | case 2: |
1170 | prepare_schedule (&ta); |
1234 | prepare_schedule (aTHX_ &ta); |
1171 | break; |
1235 | break; |
1172 | |
1236 | |
1173 | case 3: |
1237 | case 3: |
1174 | prepare_cede (&ta); |
1238 | prepare_cede (aTHX_ &ta); |
1175 | break; |
1239 | break; |
1176 | |
1240 | |
1177 | case 4: |
1241 | case 4: |
1178 | if (!prepare_cede_notself (&ta)) |
1242 | if (!prepare_cede_notself (aTHX_ &ta)) |
1179 | XSRETURN_EMPTY; |
1243 | XSRETURN_EMPTY; |
1180 | |
1244 | |
1181 | break; |
1245 | break; |
1182 | } |
1246 | } |
1183 | |
1247 | |
1184 | BARRIER; |
1248 | BARRIER; |
1185 | TRANSFER (ta); |
1249 | TRANSFER (ta); |
1186 | } |
|
|
1187 | |
1250 | |
1188 | void |
1251 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1189 | _clone_state_from (SV *dst, SV *src) |
1252 | XSRETURN_YES; |
|
|
1253 | } |
|
|
1254 | |
|
|
1255 | bool |
|
|
1256 | _destroy (SV *coro_sv) |
1190 | CODE: |
1257 | CODE: |
1191 | { |
1258 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1192 | struct coro *coro_src = SvSTATE (src); |
1259 | OUTPUT: |
1193 | |
1260 | RETVAL |
1194 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1195 | |
|
|
1196 | ++coro_src->refcnt; |
|
|
1197 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1198 | } |
|
|
1199 | |
1261 | |
1200 | void |
1262 | void |
1201 | _exit (code) |
1263 | _exit (code) |
1202 | int code |
1264 | int code |
1203 | PROTOTYPE: $ |
1265 | PROTOTYPE: $ |
1204 | CODE: |
1266 | CODE: |
1205 | _exit (code); |
1267 | _exit (code); |
|
|
1268 | |
|
|
1269 | int |
|
|
1270 | cctx_stacksize (int new_stacksize = 0) |
|
|
1271 | CODE: |
|
|
1272 | RETVAL = coro_stacksize; |
|
|
1273 | if (new_stacksize) |
|
|
1274 | coro_stacksize = new_stacksize; |
|
|
1275 | OUTPUT: |
|
|
1276 | RETVAL |
1206 | |
1277 | |
1207 | int |
1278 | int |
1208 | cctx_count () |
1279 | cctx_count () |
1209 | CODE: |
1280 | CODE: |
1210 | RETVAL = cctx_count; |
1281 | RETVAL = cctx_count; |