… | |
… | |
31 | #else |
31 | #else |
32 | # define PAGESIZE 0 |
32 | # define PAGESIZE 0 |
33 | # define BOOT_PAGESIZE (void)0 |
33 | # define BOOT_PAGESIZE (void)0 |
34 | #endif |
34 | #endif |
35 | |
35 | |
36 | #if USE_VALGRIND |
36 | #if CORO_USE_VALGRIND |
37 | # include <valgrind/valgrind.h> |
37 | # include <valgrind/valgrind.h> |
|
|
38 | # define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) |
|
|
39 | #else |
|
|
40 | # define REGISTER_STACK(cctx,start,end) |
38 | #endif |
41 | #endif |
39 | |
42 | |
40 | /* the maximum number of idle cctx that will be pooled */ |
43 | /* the maximum number of idle cctx that will be pooled */ |
41 | #define MAX_IDLE_CCTX 8 |
44 | #define MAX_IDLE_CCTX 8 |
42 | |
45 | |
… | |
… | |
71 | #ifndef SvRV_set |
74 | #ifndef SvRV_set |
72 | # define SvRV_set(s,v) SvRV(s) = (v) |
75 | # define SvRV_set(s,v) SvRV(s) = (v) |
73 | #endif |
76 | #endif |
74 | |
77 | |
75 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
78 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
76 | # undef STACKGUARD |
79 | # undef CORO_STACKGUARD |
77 | #endif |
80 | #endif |
78 | |
81 | |
79 | #ifndef STACKGUARD |
82 | #ifndef CORO_STACKGUARD |
80 | # define STACKGUARD 0 |
83 | # define CORO_STACKGUARD 0 |
81 | #endif |
84 | #endif |
82 | |
85 | |
83 | /* prefer perl internal functions over our own? */ |
86 | /* prefer perl internal functions over our own? */ |
84 | #ifndef PREFER_PERL_FUNCTIONS |
87 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
85 | # define PREFER_PERL_FUNCTIONS 0 |
88 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
86 | #endif |
89 | #endif |
87 | |
90 | |
88 | /* The next macro should declare a variable stacklevel that contains and approximation |
91 | /* 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 |
92 | * to the current C stack pointer. Its property is that it changes with each call |
90 | * and should be unique. */ |
93 | * and should be unique. */ |
… | |
… | |
112 | #else |
115 | #else |
113 | # define LOCK (void)0 |
116 | # define LOCK (void)0 |
114 | # define UNLOCK (void)0 |
117 | # define UNLOCK (void)0 |
115 | #endif |
118 | #endif |
116 | |
119 | |
|
|
120 | /* helper storage struct for Coro::AIO */ |
117 | struct io_state |
121 | struct io_state |
118 | { |
122 | { |
119 | int errorno; |
123 | int errorno; |
120 | I32 laststype; |
124 | I32 laststype; |
121 | int laststatval; |
125 | int laststatval; |
122 | Stat_t statcache; |
126 | Stat_t statcache; |
123 | }; |
127 | }; |
124 | |
128 | |
|
|
129 | static size_t coro_stacksize = CORO_STACKSIZE; |
125 | static struct CoroAPI coroapi; |
130 | static struct CoroAPI coroapi; |
126 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
131 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
127 | static HV *coro_state_stash, *coro_stash; |
132 | static HV *coro_state_stash, *coro_stash; |
128 | static SV *coro_mortal; /* will be freed after next transfer */ |
133 | static SV *coro_mortal; /* will be freed after next transfer */ |
129 | |
134 | |
… | |
… | |
134 | typedef struct coro_cctx { |
139 | typedef struct coro_cctx { |
135 | struct coro_cctx *next; |
140 | struct coro_cctx *next; |
136 | |
141 | |
137 | /* the stack */ |
142 | /* the stack */ |
138 | void *sptr; |
143 | void *sptr; |
139 | long ssize; /* positive == mmap, otherwise malloc */ |
144 | size_t ssize; |
140 | |
145 | |
141 | /* cpu state */ |
146 | /* cpu state */ |
142 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
147 | 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 */ |
148 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
144 | JMPENV *top_env; |
149 | JMPENV *top_env; |
145 | coro_context cctx; |
150 | coro_context cctx; |
146 | |
151 | |
147 | int inuse; |
|
|
148 | |
|
|
149 | #if USE_VALGRIND |
152 | #if CORO_USE_VALGRIND |
150 | int valgrind_id; |
153 | int valgrind_id; |
151 | #endif |
154 | #endif |
|
|
155 | char inuse, mapped; |
152 | } coro_cctx; |
156 | } coro_cctx; |
153 | |
157 | |
154 | enum { |
158 | enum { |
155 | CF_RUNNING = 0x0001, /* coroutine is running */ |
159 | CF_RUNNING = 0x0001, /* coroutine is running */ |
156 | CF_READY = 0x0002, /* coroutine is ready */ |
160 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
185 | }; |
189 | }; |
186 | |
190 | |
187 | typedef struct coro *Coro__State; |
191 | typedef struct coro *Coro__State; |
188 | typedef struct coro *Coro__State_or_hashref; |
192 | typedef struct coro *Coro__State_or_hashref; |
189 | |
193 | |
|
|
194 | /** Coro ********************************************************************/ |
|
|
195 | |
|
|
196 | #define PRIO_MAX 3 |
|
|
197 | #define PRIO_HIGH 1 |
|
|
198 | #define PRIO_NORMAL 0 |
|
|
199 | #define PRIO_LOW -1 |
|
|
200 | #define PRIO_IDLE -3 |
|
|
201 | #define PRIO_MIN -4 |
|
|
202 | |
|
|
203 | /* for Coro.pm */ |
|
|
204 | static SV *coro_current; |
|
|
205 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
206 | static int coro_nready; |
|
|
207 | |
|
|
208 | /** lowlevel stuff **********************************************************/ |
|
|
209 | |
190 | static AV * |
210 | static AV * |
191 | coro_clone_padlist (CV *cv) |
211 | coro_clone_padlist (CV *cv) |
192 | { |
212 | { |
193 | AV *padlist = CvPADLIST (cv); |
213 | AV *padlist = CvPADLIST (cv); |
194 | AV *newpadlist, *newpad; |
214 | AV *newpadlist, *newpad; |
… | |
… | |
268 | |
288 | |
269 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
289 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
270 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
290 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
271 | else |
291 | else |
272 | { |
292 | { |
273 | #if PREFER_PERL_FUNCTIONS |
293 | #if CORO_PREFER_PERL_FUNCTIONS |
274 | /* this is probably cleaner, but also slower? */ |
294 | /* this is probably cleaner, but also slower? */ |
275 | CV *cp = Perl_cv_clone (cv); |
295 | CV *cp = Perl_cv_clone (cv); |
276 | CvPADLIST (cv) = CvPADLIST (cp); |
296 | CvPADLIST (cv) = CvPADLIST (cp); |
277 | CvPADLIST (cp) = 0; |
297 | CvPADLIST (cp) = 0; |
278 | SvREFCNT_dec (cp); |
298 | SvREFCNT_dec (cp); |
… | |
… | |
301 | if (AvFILLp (av) >= AvMAX (av)) |
321 | if (AvFILLp (av) >= AvMAX (av)) |
302 | av_extend (av, AvMAX (av) + 1); |
322 | av_extend (av, AvMAX (av) + 1); |
303 | |
323 | |
304 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
324 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
305 | } |
325 | } |
|
|
326 | |
|
|
327 | /** load & save, init *******************************************************/ |
306 | |
328 | |
307 | #define SB do { |
329 | #define SB do { |
308 | #define SE } while (0) |
330 | #define SE } while (0) |
309 | |
331 | |
310 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
332 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
… | |
… | |
343 | CvPADLIST (cv) = (AV *)POPs; |
365 | CvPADLIST (cv) = (AV *)POPs; |
344 | } |
366 | } |
345 | |
367 | |
346 | PUTBACK; |
368 | PUTBACK; |
347 | } |
369 | } |
|
|
370 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
348 | } |
371 | } |
349 | |
372 | |
350 | static void |
373 | static void |
351 | save_perl (Coro__State c) |
374 | save_perl (Coro__State c) |
352 | { |
375 | { |
|
|
376 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
353 | { |
377 | { |
354 | dSP; |
378 | dSP; |
355 | I32 cxix = cxstack_ix; |
379 | I32 cxix = cxstack_ix; |
356 | PERL_CONTEXT *ccstk = cxstack; |
380 | PERL_CONTEXT *ccstk = cxstack; |
357 | PERL_SI *top_si = PL_curstackinfo; |
381 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
412 | * allocate various perl stacks. This is an exact copy |
436 | * allocate various perl stacks. This is an exact copy |
413 | * of perl.c:init_stacks, except that it uses less memory |
437 | * of perl.c:init_stacks, except that it uses less memory |
414 | * on the (sometimes correct) assumption that coroutines do |
438 | * on the (sometimes correct) assumption that coroutines do |
415 | * not usually need a lot of stackspace. |
439 | * not usually need a lot of stackspace. |
416 | */ |
440 | */ |
417 | #if PREFER_PERL_FUNCTIONS |
441 | #if CORO_PREFER_PERL_FUNCTIONS |
418 | # define coro_init_stacks init_stacks |
442 | # define coro_init_stacks init_stacks |
419 | #else |
443 | #else |
420 | static void |
444 | static void |
421 | coro_init_stacks () |
445 | coro_init_stacks () |
422 | { |
446 | { |
… | |
… | |
503 | #if !PERL_VERSION_ATLEAST (5,9,0) |
527 | #if !PERL_VERSION_ATLEAST (5,9,0) |
504 | Safefree (PL_retstack); |
528 | Safefree (PL_retstack); |
505 | #endif |
529 | #endif |
506 | } |
530 | } |
507 | |
531 | |
|
|
532 | /** coroutine stack handling ************************************************/ |
|
|
533 | |
508 | static void |
534 | static void |
509 | setup_coro (struct coro *coro) |
535 | setup_coro (struct coro *coro) |
510 | { |
536 | { |
511 | /* |
537 | /* |
512 | * emulate part of the perl startup here. |
538 | * emulate part of the perl startup here. |
… | |
… | |
514 | |
540 | |
515 | coro_init_stacks (); |
541 | coro_init_stacks (); |
516 | |
542 | |
517 | PL_curcop = &PL_compiling; |
543 | PL_curcop = &PL_compiling; |
518 | PL_in_eval = EVAL_NULL; |
544 | PL_in_eval = EVAL_NULL; |
|
|
545 | PL_comppad = 0; |
519 | PL_curpm = 0; |
546 | PL_curpm = 0; |
520 | PL_localizing = 0; |
547 | PL_localizing = 0; |
521 | PL_dirty = 0; |
548 | PL_dirty = 0; |
522 | PL_restartop = 0; |
549 | PL_restartop = 0; |
523 | |
550 | |
… | |
… | |
598 | |
625 | |
599 | static coro_cctx * |
626 | static coro_cctx * |
600 | cctx_new () |
627 | cctx_new () |
601 | { |
628 | { |
602 | coro_cctx *cctx; |
629 | coro_cctx *cctx; |
|
|
630 | void *stack_start; |
|
|
631 | size_t stack_size; |
603 | |
632 | |
604 | ++cctx_count; |
633 | ++cctx_count; |
605 | |
634 | |
606 | Newz (0, cctx, 1, coro_cctx); |
635 | Newz (0, cctx, 1, coro_cctx); |
607 | |
636 | |
608 | #if HAVE_MMAP |
637 | #if HAVE_MMAP |
609 | |
638 | |
610 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
639 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
611 | /* mmap supposedly does allocate-on-write for us */ |
640 | /* 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); |
641 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
613 | |
642 | |
614 | if (cctx->sptr == (void *)-1) |
643 | if (cctx->sptr != (void *)-1) |
615 | { |
|
|
616 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
617 | _exit (EXIT_FAILURE); |
|
|
618 | } |
644 | { |
619 | |
|
|
620 | # if STACKGUARD |
645 | # if CORO_STACKGUARD |
621 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
646 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
622 | # endif |
647 | # endif |
|
|
648 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
|
|
649 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
650 | cctx->mapped = 1; |
|
|
651 | } |
|
|
652 | else |
|
|
653 | #endif |
|
|
654 | { |
|
|
655 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
|
|
656 | New (0, cctx->sptr, coro_stacksize, long); |
623 | |
657 | |
624 | #else |
|
|
625 | |
|
|
626 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
627 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
628 | |
|
|
629 | if (!cctx->sptr) |
658 | if (!cctx->sptr) |
630 | { |
659 | { |
631 | perror ("FATAL: unable to malloc stack for coroutine"); |
660 | perror ("FATAL: unable to allocate stack for coroutine"); |
632 | _exit (EXIT_FAILURE); |
661 | _exit (EXIT_FAILURE); |
633 | } |
662 | } |
634 | |
663 | |
635 | #endif |
664 | stack_start = cctx->sptr; |
|
|
665 | stack_size = cctx->ssize; |
|
|
666 | } |
636 | |
667 | |
637 | #if USE_VALGRIND |
668 | 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); |
669 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
645 | |
670 | |
646 | return cctx; |
671 | return cctx; |
647 | } |
672 | } |
648 | |
673 | |
649 | static void |
674 | static void |
… | |
… | |
652 | if (!cctx) |
677 | if (!cctx) |
653 | return; |
678 | return; |
654 | |
679 | |
655 | --cctx_count; |
680 | --cctx_count; |
656 | |
681 | |
657 | #if USE_VALGRIND |
682 | #if CORO_USE_VALGRIND |
658 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
683 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
659 | #endif |
684 | #endif |
660 | |
685 | |
661 | #if HAVE_MMAP |
686 | #if HAVE_MMAP |
|
|
687 | if (cctx->mapped) |
662 | munmap (cctx->sptr, cctx->ssize); |
688 | munmap (cctx->sptr, cctx->ssize); |
663 | #else |
689 | else |
|
|
690 | #endif |
664 | Safefree (cctx->sptr); |
691 | Safefree (cctx->sptr); |
665 | #endif |
|
|
666 | |
692 | |
667 | Safefree (cctx); |
693 | Safefree (cctx); |
668 | } |
694 | } |
669 | |
695 | |
670 | static coro_cctx * |
696 | static coro_cctx * |
671 | cctx_get () |
697 | cctx_get () |
672 | { |
698 | { |
673 | coro_cctx *cctx; |
|
|
674 | |
|
|
675 | if (cctx_first) |
699 | while (cctx_first) |
676 | { |
700 | { |
677 | cctx = cctx_first; |
701 | coro_cctx *cctx = cctx_first; |
678 | cctx_first = cctx->next; |
702 | cctx_first = cctx->next; |
679 | --cctx_idle; |
703 | --cctx_idle; |
|
|
704 | |
|
|
705 | if (cctx->ssize >= coro_stacksize) |
|
|
706 | return cctx; |
|
|
707 | |
|
|
708 | cctx_destroy (cctx); |
680 | } |
709 | } |
681 | else |
710 | |
682 | { |
|
|
683 | cctx = cctx_new (); |
|
|
684 | PL_op = PL_op->op_next; |
711 | PL_op = PL_op->op_next; |
685 | } |
|
|
686 | |
|
|
687 | return cctx; |
712 | return cctx_new (); |
688 | } |
713 | } |
689 | |
714 | |
690 | static void |
715 | static void |
691 | cctx_put (coro_cctx *cctx) |
716 | cctx_put (coro_cctx *cctx) |
692 | { |
717 | { |
… | |
… | |
703 | |
728 | |
704 | ++cctx_idle; |
729 | ++cctx_idle; |
705 | cctx->next = cctx_first; |
730 | cctx->next = cctx_first; |
706 | cctx_first = cctx; |
731 | cctx_first = cctx; |
707 | } |
732 | } |
|
|
733 | |
|
|
734 | /** coroutine switching *****************************************************/ |
708 | |
735 | |
709 | /* never call directly, always through the coro_state_transfer global variable */ |
736 | /* never call directly, always through the coro_state_transfer global variable */ |
710 | static void NOINLINE |
737 | static void NOINLINE |
711 | transfer (struct coro *prev, struct coro *next) |
738 | transfer (struct coro *prev, struct coro *next) |
712 | { |
739 | { |
… | |
… | |
802 | struct coro *prev, *next; |
829 | struct coro *prev, *next; |
803 | }; |
830 | }; |
804 | |
831 | |
805 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
832 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
806 | |
833 | |
|
|
834 | /** high level stuff ********************************************************/ |
|
|
835 | |
807 | static int |
836 | static int |
808 | coro_state_destroy (struct coro *coro) |
837 | coro_state_destroy (struct coro *coro) |
809 | { |
838 | { |
810 | if (coro->flags & CF_DESTROYED) |
839 | if (coro->flags & CF_DESTROYED) |
811 | return 0; |
840 | return 0; |
812 | |
841 | |
813 | coro->flags |= CF_DESTROYED; |
842 | coro->flags |= CF_DESTROYED; |
|
|
843 | |
|
|
844 | if (coro->flags & CF_READY) |
|
|
845 | { |
|
|
846 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
|
|
847 | /* alternative: look through all ready queues and remove the coro */ |
|
|
848 | LOCK; |
|
|
849 | --coro_nready; |
|
|
850 | UNLOCK; |
|
|
851 | } |
|
|
852 | else |
|
|
853 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
814 | |
854 | |
815 | if (coro->mainstack && coro->mainstack != main_mainstack) |
855 | if (coro->mainstack && coro->mainstack != main_mainstack) |
816 | { |
856 | { |
|
|
857 | struct coro temp; |
|
|
858 | |
817 | assert (!(coro->flags & CF_RUNNING)); |
859 | assert (!(coro->flags & CF_RUNNING)); |
818 | |
860 | |
819 | struct coro temp; |
|
|
820 | Zero (&temp, 1, struct coro); |
861 | Zero (&temp, 1, struct coro); |
821 | temp.save = CORO_SAVE_ALL; |
862 | temp.save = CORO_SAVE_ALL; |
822 | |
863 | |
823 | if (coro->flags & CF_RUNNING) |
864 | if (coro->flags & CF_RUNNING) |
824 | croak ("FATAL: tried to destroy currently running coroutine"); |
865 | croak ("FATAL: tried to destroy currently running coroutine"); |
… | |
… | |
890 | /* very slow, but rare, check */ |
931 | /* very slow, but rare, check */ |
891 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
932 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
892 | croak ("Coro::State object required"); |
933 | croak ("Coro::State object required"); |
893 | } |
934 | } |
894 | |
935 | |
895 | mg = SvMAGIC (coro); |
936 | mg = CORO_MAGIC (coro); |
896 | assert (mg->mg_type == PERL_MAGIC_ext); |
|
|
897 | return (struct coro *)mg->mg_ptr; |
937 | return (struct coro *)mg->mg_ptr; |
898 | } |
938 | } |
899 | |
939 | |
900 | static void |
940 | static void |
901 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
941 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
… | |
… | |
925 | return old_save; |
965 | return old_save; |
926 | } |
966 | } |
927 | |
967 | |
928 | /** Coro ********************************************************************/ |
968 | /** Coro ********************************************************************/ |
929 | |
969 | |
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 |
970 | static void |
943 | coro_enq (SV *coro_sv) |
971 | coro_enq (SV *coro_sv) |
944 | { |
972 | { |
945 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
973 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
946 | coro_nready++; |
|
|
947 | } |
974 | } |
948 | |
975 | |
949 | static SV * |
976 | static SV * |
950 | coro_deq (int min_prio) |
977 | coro_deq (int min_prio) |
951 | { |
978 | { |
… | |
… | |
955 | if (min_prio < 0) |
982 | if (min_prio < 0) |
956 | min_prio = 0; |
983 | min_prio = 0; |
957 | |
984 | |
958 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
985 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
959 | if (AvFILLp (coro_ready [prio]) >= 0) |
986 | if (AvFILLp (coro_ready [prio]) >= 0) |
960 | { |
|
|
961 | coro_nready--; |
|
|
962 | return av_shift (coro_ready [prio]); |
987 | return av_shift (coro_ready [prio]); |
963 | } |
|
|
964 | |
988 | |
965 | return 0; |
989 | return 0; |
966 | } |
990 | } |
967 | |
991 | |
968 | static int |
992 | static int |
… | |
… | |
980 | |
1004 | |
981 | coro->flags |= CF_READY; |
1005 | coro->flags |= CF_READY; |
982 | |
1006 | |
983 | LOCK; |
1007 | LOCK; |
984 | coro_enq (SvREFCNT_inc (coro_sv)); |
1008 | coro_enq (SvREFCNT_inc (coro_sv)); |
|
|
1009 | ++coro_nready; |
985 | UNLOCK; |
1010 | UNLOCK; |
986 | |
1011 | |
987 | return 1; |
1012 | return 1; |
988 | } |
1013 | } |
989 | |
1014 | |
… | |
… | |
1000 | |
1025 | |
1001 | for (;;) |
1026 | for (;;) |
1002 | { |
1027 | { |
1003 | LOCK; |
1028 | LOCK; |
1004 | next_sv = coro_deq (PRIO_MIN); |
1029 | next_sv = coro_deq (PRIO_MIN); |
1005 | UNLOCK; |
|
|
1006 | |
1030 | |
1007 | /* nothing to schedule: call the idle handler */ |
1031 | /* nothing to schedule: call the idle handler */ |
1008 | if (!next_sv) |
1032 | if (!next_sv) |
1009 | { |
1033 | { |
1010 | dSP; |
1034 | dSP; |
|
|
1035 | UNLOCK; |
1011 | |
1036 | |
1012 | ENTER; |
1037 | ENTER; |
1013 | SAVETMPS; |
1038 | SAVETMPS; |
1014 | |
1039 | |
1015 | PUSHMARK (SP); |
1040 | PUSHMARK (SP); |
… | |
… | |
1024 | ta->next = SvSTATE (next_sv); |
1049 | ta->next = SvSTATE (next_sv); |
1025 | |
1050 | |
1026 | /* cannot transfer to destroyed coros, skip and look for next */ |
1051 | /* cannot transfer to destroyed coros, skip and look for next */ |
1027 | if (ta->next->flags & CF_DESTROYED) |
1052 | if (ta->next->flags & CF_DESTROYED) |
1028 | { |
1053 | { |
|
|
1054 | UNLOCK; |
1029 | SvREFCNT_dec (next_sv); |
1055 | SvREFCNT_dec (next_sv); |
|
|
1056 | /* coro_nready is already taken care of by destroy */ |
1030 | continue; |
1057 | continue; |
1031 | } |
1058 | } |
1032 | |
1059 | |
|
|
1060 | --coro_nready; |
|
|
1061 | UNLOCK; |
1033 | break; |
1062 | break; |
1034 | } |
1063 | } |
1035 | |
1064 | |
1036 | /* free this only after the transfer */ |
1065 | /* free this only after the transfer */ |
1037 | prev_sv = SvRV (coro_current); |
1066 | prev_sv = SvRV (coro_current); |
… | |
… | |
1204 | break; |
1233 | break; |
1205 | } |
1234 | } |
1206 | |
1235 | |
1207 | BARRIER; |
1236 | BARRIER; |
1208 | TRANSFER (ta); |
1237 | TRANSFER (ta); |
|
|
1238 | |
|
|
1239 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
|
|
1240 | XSRETURN_YES; |
1209 | } |
1241 | } |
1210 | |
1242 | |
1211 | bool |
1243 | bool |
1212 | _destroy (SV *coro_sv) |
1244 | _destroy (SV *coro_sv) |
1213 | CODE: |
1245 | CODE: |
… | |
… | |
1219 | _exit (code) |
1251 | _exit (code) |
1220 | int code |
1252 | int code |
1221 | PROTOTYPE: $ |
1253 | PROTOTYPE: $ |
1222 | CODE: |
1254 | CODE: |
1223 | _exit (code); |
1255 | _exit (code); |
|
|
1256 | |
|
|
1257 | int |
|
|
1258 | cctx_stacksize (int new_stacksize = 0) |
|
|
1259 | CODE: |
|
|
1260 | RETVAL = coro_stacksize; |
|
|
1261 | if (new_stacksize) |
|
|
1262 | coro_stacksize = new_stacksize; |
|
|
1263 | OUTPUT: |
|
|
1264 | RETVAL |
1224 | |
1265 | |
1225 | int |
1266 | int |
1226 | cctx_count () |
1267 | cctx_count () |
1227 | CODE: |
1268 | CODE: |
1228 | RETVAL = cctx_count; |
1269 | RETVAL = cctx_count; |