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