… | |
… | |
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; |
… | |
… | |
134 | typedef struct coro_cctx { |
138 | typedef struct coro_cctx { |
135 | struct coro_cctx *next; |
139 | struct coro_cctx *next; |
136 | |
140 | |
137 | /* the stack */ |
141 | /* the stack */ |
138 | void *sptr; |
142 | void *sptr; |
139 | long ssize; /* positive == mmap, otherwise malloc */ |
143 | ssize_t ssize; /* positive == mmap, otherwise malloc */ |
140 | |
144 | |
141 | /* cpu state */ |
145 | /* cpu state */ |
142 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
146 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
147 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
143 | JMPENV *top_env; |
148 | JMPENV *top_env; |
144 | coro_context cctx; |
149 | coro_context cctx; |
145 | |
150 | |
146 | int inuse; |
151 | int inuse; |
147 | |
152 | |
148 | #if USE_VALGRIND |
153 | #if CORO_USE_VALGRIND |
149 | int valgrind_id; |
154 | int valgrind_id; |
150 | #endif |
155 | #endif |
151 | } coro_cctx; |
156 | } coro_cctx; |
152 | |
157 | |
153 | enum { |
158 | enum { |
154 | CF_RUNNING = 0x0001, /* coroutine is running */ |
159 | CF_RUNNING = 0x0001, /* coroutine is running */ |
155 | CF_READY = 0x0002, /* coroutine is ready */ |
160 | CF_READY = 0x0002, /* coroutine is ready */ |
156 | CF_NEW = 0x0004, /* ahs never been switched to */ |
161 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
162 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
157 | }; |
163 | }; |
158 | |
164 | |
159 | /* this is a structure representing a perl-level coroutine */ |
165 | /* this is a structure representing a perl-level coroutine */ |
160 | struct coro { |
166 | struct coro { |
161 | /* the c coroutine allocated to this perl coroutine, if any */ |
167 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
183 | }; |
189 | }; |
184 | |
190 | |
185 | typedef struct coro *Coro__State; |
191 | typedef struct coro *Coro__State; |
186 | typedef struct coro *Coro__State_or_hashref; |
192 | typedef struct coro *Coro__State_or_hashref; |
187 | |
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 | |
188 | static AV * |
210 | static AV * |
189 | coro_clone_padlist (CV *cv) |
211 | coro_clone_padlist (CV *cv) |
190 | { |
212 | { |
191 | AV *padlist = CvPADLIST (cv); |
213 | AV *padlist = CvPADLIST (cv); |
192 | AV *newpadlist, *newpad; |
214 | AV *newpadlist, *newpad; |
… | |
… | |
266 | |
288 | |
267 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
289 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
268 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
290 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
269 | else |
291 | else |
270 | { |
292 | { |
271 | #if PREFER_PERL_FUNCTIONS |
293 | #if CORO_PREFER_PERL_FUNCTIONS |
272 | /* this is probably cleaner, but also slower? */ |
294 | /* this is probably cleaner, but also slower? */ |
273 | CV *cp = Perl_cv_clone (cv); |
295 | CV *cp = Perl_cv_clone (cv); |
274 | CvPADLIST (cv) = CvPADLIST (cp); |
296 | CvPADLIST (cv) = CvPADLIST (cp); |
275 | CvPADLIST (cp) = 0; |
297 | CvPADLIST (cp) = 0; |
276 | SvREFCNT_dec (cp); |
298 | SvREFCNT_dec (cp); |
… | |
… | |
299 | if (AvFILLp (av) >= AvMAX (av)) |
321 | if (AvFILLp (av) >= AvMAX (av)) |
300 | av_extend (av, AvMAX (av) + 1); |
322 | av_extend (av, AvMAX (av) + 1); |
301 | |
323 | |
302 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
324 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
303 | } |
325 | } |
|
|
326 | |
|
|
327 | /** load & save, init *******************************************************/ |
304 | |
328 | |
305 | #define SB do { |
329 | #define SB do { |
306 | #define SE } while (0) |
330 | #define SE } while (0) |
307 | |
331 | |
308 | #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 |
… | |
… | |
341 | CvPADLIST (cv) = (AV *)POPs; |
365 | CvPADLIST (cv) = (AV *)POPs; |
342 | } |
366 | } |
343 | |
367 | |
344 | PUTBACK; |
368 | PUTBACK; |
345 | } |
369 | } |
|
|
370 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
346 | } |
371 | } |
347 | |
372 | |
348 | static void |
373 | static void |
349 | save_perl (Coro__State c) |
374 | save_perl (Coro__State c) |
350 | { |
375 | { |
|
|
376 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
351 | { |
377 | { |
352 | dSP; |
378 | dSP; |
353 | I32 cxix = cxstack_ix; |
379 | I32 cxix = cxstack_ix; |
354 | PERL_CONTEXT *ccstk = cxstack; |
380 | PERL_CONTEXT *ccstk = cxstack; |
355 | PERL_SI *top_si = PL_curstackinfo; |
381 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
410 | * allocate various perl stacks. This is an exact copy |
436 | * allocate various perl stacks. This is an exact copy |
411 | * of perl.c:init_stacks, except that it uses less memory |
437 | * of perl.c:init_stacks, except that it uses less memory |
412 | * on the (sometimes correct) assumption that coroutines do |
438 | * on the (sometimes correct) assumption that coroutines do |
413 | * not usually need a lot of stackspace. |
439 | * not usually need a lot of stackspace. |
414 | */ |
440 | */ |
415 | #if PREFER_PERL_FUNCTIONS |
441 | #if CORO_PREFER_PERL_FUNCTIONS |
416 | # define coro_init_stacks init_stacks |
442 | # define coro_init_stacks init_stacks |
417 | #else |
443 | #else |
418 | static void |
444 | static void |
419 | coro_init_stacks () |
445 | coro_init_stacks () |
420 | { |
446 | { |
… | |
… | |
470 | |
496 | |
471 | /* free all temporaries */ |
497 | /* free all temporaries */ |
472 | FREETMPS; |
498 | FREETMPS; |
473 | assert (PL_tmps_ix == -1); |
499 | assert (PL_tmps_ix == -1); |
474 | |
500 | |
|
|
501 | /* unwind all extra stacks */ |
475 | POPSTACK_TO (PL_mainstack); |
502 | POPSTACK_TO (PL_mainstack); |
|
|
503 | |
|
|
504 | /* unwind main stack */ |
|
|
505 | dounwind (-1); |
476 | } |
506 | } |
477 | |
507 | |
478 | while (PL_curstackinfo->si_next) |
508 | while (PL_curstackinfo->si_next) |
479 | PL_curstackinfo = PL_curstackinfo->si_next; |
509 | PL_curstackinfo = PL_curstackinfo->si_next; |
480 | |
510 | |
… | |
… | |
497 | #if !PERL_VERSION_ATLEAST (5,9,0) |
527 | #if !PERL_VERSION_ATLEAST (5,9,0) |
498 | Safefree (PL_retstack); |
528 | Safefree (PL_retstack); |
499 | #endif |
529 | #endif |
500 | } |
530 | } |
501 | |
531 | |
|
|
532 | /** coroutine stack handling ************************************************/ |
|
|
533 | |
502 | static void |
534 | static void |
503 | setup_coro (struct coro *coro) |
535 | setup_coro (struct coro *coro) |
504 | { |
536 | { |
505 | /* |
537 | /* |
506 | * emulate part of the perl startup here. |
538 | * emulate part of the perl startup here. |
… | |
… | |
508 | |
540 | |
509 | coro_init_stacks (); |
541 | coro_init_stacks (); |
510 | |
542 | |
511 | PL_curcop = &PL_compiling; |
543 | PL_curcop = &PL_compiling; |
512 | PL_in_eval = EVAL_NULL; |
544 | PL_in_eval = EVAL_NULL; |
|
|
545 | PL_comppad = 0; |
513 | PL_curpm = 0; |
546 | PL_curpm = 0; |
514 | PL_localizing = 0; |
547 | PL_localizing = 0; |
515 | PL_dirty = 0; |
548 | PL_dirty = 0; |
516 | PL_restartop = 0; |
549 | PL_restartop = 0; |
517 | |
550 | |
… | |
… | |
599 | |
632 | |
600 | Newz (0, cctx, 1, coro_cctx); |
633 | Newz (0, cctx, 1, coro_cctx); |
601 | |
634 | |
602 | #if HAVE_MMAP |
635 | #if HAVE_MMAP |
603 | |
636 | |
604 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
637 | cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
605 | /* mmap supposedly does allocate-on-write for us */ |
638 | /* 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); |
639 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
607 | |
640 | |
608 | if (cctx->sptr == (void *)-1) |
641 | if (cctx->sptr != (void *)-1) |
609 | { |
|
|
610 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
611 | _exit (EXIT_FAILURE); |
|
|
612 | } |
642 | { |
613 | |
|
|
614 | # if STACKGUARD |
643 | # if CORO_STACKGUARD |
615 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
644 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
616 | # endif |
645 | # endif |
617 | |
646 | REGISTER_STACK ( |
618 | #else |
647 | cctx, |
619 | |
|
|
620 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
621 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
622 | |
|
|
623 | if (!cctx->sptr) |
|
|
624 | { |
|
|
625 | perror ("FATAL: unable to malloc stack for coroutine"); |
|
|
626 | _exit (EXIT_FAILURE); |
|
|
627 | } |
|
|
628 | |
|
|
629 | #endif |
|
|
630 | |
|
|
631 | #if USE_VALGRIND |
|
|
632 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
633 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
648 | CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
634 | cctx->ssize + (char *)cctx->sptr |
649 | cctx->ssize + (char *)cctx->sptr |
635 | ); |
650 | ); |
636 | #endif |
|
|
637 | |
651 | |
638 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
652 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
|
|
653 | } |
|
|
654 | else |
|
|
655 | #endif |
|
|
656 | { |
|
|
657 | cctx->ssize = -CORO_STACKSIZE * (long)sizeof (long); |
|
|
658 | New (0, cctx->sptr, CORO_STACKSIZE, long); |
|
|
659 | |
|
|
660 | if (!cctx->sptr) |
|
|
661 | { |
|
|
662 | perror ("FATAL: unable to allocate stack for coroutine"); |
|
|
663 | _exit (EXIT_FAILURE); |
|
|
664 | } |
|
|
665 | |
|
|
666 | REGISTER_STACK ( |
|
|
667 | cctx, |
|
|
668 | (char *)cctx->sptr, |
|
|
669 | (char *)cctx->sptr - cctx->ssize |
|
|
670 | ); |
|
|
671 | |
|
|
672 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize); |
|
|
673 | } |
639 | |
674 | |
640 | return cctx; |
675 | return cctx; |
641 | } |
676 | } |
642 | |
677 | |
643 | static void |
678 | static void |
… | |
… | |
646 | if (!cctx) |
681 | if (!cctx) |
647 | return; |
682 | return; |
648 | |
683 | |
649 | --cctx_count; |
684 | --cctx_count; |
650 | |
685 | |
651 | #if USE_VALGRIND |
686 | #if CORO_USE_VALGRIND |
652 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
687 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
653 | #endif |
688 | #endif |
654 | |
689 | |
655 | #if HAVE_MMAP |
690 | #if HAVE_MMAP |
|
|
691 | if (cctx->ssize > 0) |
656 | munmap (cctx->sptr, cctx->ssize); |
692 | munmap (cctx->sptr, cctx->ssize); |
657 | #else |
693 | else |
|
|
694 | #endif |
658 | Safefree (cctx->sptr); |
695 | Safefree (cctx->sptr); |
659 | #endif |
|
|
660 | |
696 | |
661 | Safefree (cctx); |
697 | Safefree (cctx); |
662 | } |
698 | } |
663 | |
699 | |
664 | static coro_cctx * |
700 | static coro_cctx * |
… | |
… | |
698 | ++cctx_idle; |
734 | ++cctx_idle; |
699 | cctx->next = cctx_first; |
735 | cctx->next = cctx_first; |
700 | cctx_first = cctx; |
736 | cctx_first = cctx; |
701 | } |
737 | } |
702 | |
738 | |
|
|
739 | /** coroutine switching *****************************************************/ |
|
|
740 | |
703 | /* never call directly, always through the coro_state_transfer global variable */ |
741 | /* never call directly, always through the coro_state_transfer global variable */ |
704 | static void NOINLINE |
742 | static void NOINLINE |
705 | transfer (struct coro *prev, struct coro *next) |
743 | transfer (struct coro *prev, struct coro *next) |
706 | { |
744 | { |
707 | dSTACKLEVEL; |
745 | dSTACKLEVEL; |
708 | |
746 | |
709 | /* sometimes transfer is only called to set idle_sp */ |
747 | /* sometimes transfer is only called to set idle_sp */ |
710 | if (!next) |
748 | if (!next) |
711 | { |
749 | { |
712 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
750 | ((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 */ |
751 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
714 | } |
752 | } |
715 | else if (prev != next) |
753 | else if (prev != next) |
716 | { |
754 | { |
717 | coro_cctx *prev__cctx; |
755 | coro_cctx *prev__cctx; |
718 | |
756 | |
… | |
… | |
729 | if (!prev->flags & CF_RUNNING) |
767 | if (!prev->flags & CF_RUNNING) |
730 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
768 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
731 | |
769 | |
732 | if (next->flags & CF_RUNNING) |
770 | if (next->flags & CF_RUNNING) |
733 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
771 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
772 | |
|
|
773 | if (next->flags & CF_DESTROYED) |
|
|
774 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
734 | |
775 | |
735 | prev->flags &= ~CF_RUNNING; |
776 | prev->flags &= ~CF_RUNNING; |
736 | next->flags |= CF_RUNNING; |
777 | next->flags |= CF_RUNNING; |
737 | |
778 | |
738 | LOCK; |
779 | LOCK; |
… | |
… | |
759 | |
800 | |
760 | /* possibly "free" the cctx */ |
801 | /* possibly "free" the cctx */ |
761 | if (prev__cctx->idle_sp == STACKLEVEL) |
802 | if (prev__cctx->idle_sp == STACKLEVEL) |
762 | { |
803 | { |
763 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
804 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
764 | assert (PL_top_env == prev__cctx->top_env); |
805 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
765 | |
806 | |
766 | prev->cctx = 0; |
807 | prev->cctx = 0; |
767 | |
808 | |
768 | cctx_put (prev__cctx); |
809 | cctx_put (prev__cctx); |
769 | prev__cctx->inuse = 0; |
810 | prev__cctx->inuse = 0; |
… | |
… | |
782 | PL_top_env = next->cctx->top_env; |
823 | PL_top_env = next->cctx->top_env; |
783 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
824 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
784 | } |
825 | } |
785 | |
826 | |
786 | free_coro_mortal (); |
827 | free_coro_mortal (); |
787 | |
|
|
788 | UNLOCK; |
828 | UNLOCK; |
789 | } |
829 | } |
790 | } |
830 | } |
791 | |
831 | |
792 | struct transfer_args |
832 | struct transfer_args |
… | |
… | |
794 | struct coro *prev, *next; |
834 | struct coro *prev, *next; |
795 | }; |
835 | }; |
796 | |
836 | |
797 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
837 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
798 | |
838 | |
|
|
839 | /** high level stuff ********************************************************/ |
|
|
840 | |
799 | static void |
841 | static int |
800 | coro_state_destroy (struct coro *coro) |
842 | coro_state_destroy (struct coro *coro) |
801 | { |
843 | { |
802 | if (coro->refcnt--) |
844 | if (coro->flags & CF_DESTROYED) |
803 | return; |
845 | return 0; |
|
|
846 | |
|
|
847 | coro->flags |= CF_DESTROYED; |
|
|
848 | |
|
|
849 | if (coro->flags & CF_READY) |
|
|
850 | { |
|
|
851 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
|
|
852 | /* alternative: look through all ready queues and remove the coro */ |
|
|
853 | LOCK; |
|
|
854 | --coro_nready; |
|
|
855 | UNLOCK; |
|
|
856 | } |
|
|
857 | else |
|
|
858 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
804 | |
859 | |
805 | if (coro->mainstack && coro->mainstack != main_mainstack) |
860 | if (coro->mainstack && coro->mainstack != main_mainstack) |
806 | { |
861 | { |
807 | struct coro temp; |
862 | struct coro temp; |
|
|
863 | |
|
|
864 | assert (!(coro->flags & CF_RUNNING)); |
|
|
865 | |
808 | Zero (&temp, 1, struct coro); |
866 | Zero (&temp, 1, struct coro); |
809 | temp.save = CORO_SAVE_ALL; |
867 | temp.save = CORO_SAVE_ALL; |
810 | |
868 | |
811 | if (coro->flags & CF_RUNNING) |
869 | if (coro->flags & CF_RUNNING) |
812 | croak ("FATAL: tried to destroy currently running coroutine"); |
870 | croak ("FATAL: tried to destroy currently running coroutine"); |
… | |
… | |
821 | coro->mainstack = 0; |
879 | coro->mainstack = 0; |
822 | } |
880 | } |
823 | |
881 | |
824 | cctx_destroy (coro->cctx); |
882 | cctx_destroy (coro->cctx); |
825 | SvREFCNT_dec (coro->args); |
883 | SvREFCNT_dec (coro->args); |
826 | Safefree (coro); |
884 | |
|
|
885 | return 1; |
827 | } |
886 | } |
828 | |
887 | |
829 | static int |
888 | static int |
830 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
889 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
831 | { |
890 | { |
832 | struct coro *coro = (struct coro *)mg->mg_ptr; |
891 | struct coro *coro = (struct coro *)mg->mg_ptr; |
833 | mg->mg_ptr = 0; |
892 | mg->mg_ptr = 0; |
834 | |
893 | |
|
|
894 | if (--coro->refcnt < 0) |
|
|
895 | { |
835 | coro_state_destroy (coro); |
896 | coro_state_destroy (coro); |
|
|
897 | Safefree (coro); |
|
|
898 | } |
836 | |
899 | |
837 | return 0; |
900 | return 0; |
838 | } |
901 | } |
839 | |
902 | |
840 | static int |
903 | static int |
… | |
… | |
847 | return 0; |
910 | return 0; |
848 | } |
911 | } |
849 | |
912 | |
850 | static MGVTBL coro_state_vtbl = { |
913 | static MGVTBL coro_state_vtbl = { |
851 | 0, 0, 0, 0, |
914 | 0, 0, 0, 0, |
852 | coro_state_clear, |
915 | coro_state_free, |
853 | 0, |
916 | 0, |
854 | #ifdef MGf_DUP |
917 | #ifdef MGf_DUP |
855 | coro_state_dup, |
918 | coro_state_dup, |
856 | #else |
919 | #else |
857 | # define MGf_DUP 0 |
920 | # define MGf_DUP 0 |
… | |
… | |
908 | return old_save; |
971 | return old_save; |
909 | } |
972 | } |
910 | |
973 | |
911 | /** Coro ********************************************************************/ |
974 | /** Coro ********************************************************************/ |
912 | |
975 | |
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 |
976 | static void |
926 | coro_enq (SV *coro_sv) |
977 | coro_enq (SV *coro_sv) |
927 | { |
978 | { |
928 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
979 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
929 | coro_nready++; |
|
|
930 | } |
980 | } |
931 | |
981 | |
932 | static SV * |
982 | static SV * |
933 | coro_deq (int min_prio) |
983 | coro_deq (int min_prio) |
934 | { |
984 | { |
… | |
… | |
938 | if (min_prio < 0) |
988 | if (min_prio < 0) |
939 | min_prio = 0; |
989 | min_prio = 0; |
940 | |
990 | |
941 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
991 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
942 | if (AvFILLp (coro_ready [prio]) >= 0) |
992 | if (AvFILLp (coro_ready [prio]) >= 0) |
943 | { |
|
|
944 | coro_nready--; |
|
|
945 | return av_shift (coro_ready [prio]); |
993 | return av_shift (coro_ready [prio]); |
946 | } |
|
|
947 | |
994 | |
948 | return 0; |
995 | return 0; |
949 | } |
996 | } |
950 | |
997 | |
951 | static int |
998 | static int |
… | |
… | |
963 | |
1010 | |
964 | coro->flags |= CF_READY; |
1011 | coro->flags |= CF_READY; |
965 | |
1012 | |
966 | LOCK; |
1013 | LOCK; |
967 | coro_enq (SvREFCNT_inc (coro_sv)); |
1014 | coro_enq (SvREFCNT_inc (coro_sv)); |
|
|
1015 | ++coro_nready; |
968 | UNLOCK; |
1016 | UNLOCK; |
969 | |
1017 | |
970 | return 1; |
1018 | return 1; |
971 | } |
1019 | } |
972 | |
1020 | |
… | |
… | |
977 | } |
1025 | } |
978 | |
1026 | |
979 | static void |
1027 | static void |
980 | prepare_schedule (struct transfer_args *ta) |
1028 | prepare_schedule (struct transfer_args *ta) |
981 | { |
1029 | { |
982 | SV *prev, *next; |
1030 | SV *prev_sv, *next_sv; |
983 | |
1031 | |
984 | for (;;) |
1032 | for (;;) |
985 | { |
1033 | { |
986 | LOCK; |
1034 | LOCK; |
987 | next = coro_deq (PRIO_MIN); |
1035 | next_sv = coro_deq (PRIO_MIN); |
|
|
1036 | |
|
|
1037 | /* nothing to schedule: call the idle handler */ |
|
|
1038 | if (!next_sv) |
|
|
1039 | { |
|
|
1040 | dSP; |
|
|
1041 | UNLOCK; |
|
|
1042 | |
|
|
1043 | ENTER; |
|
|
1044 | SAVETMPS; |
|
|
1045 | |
|
|
1046 | PUSHMARK (SP); |
|
|
1047 | PUTBACK; |
|
|
1048 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1049 | |
|
|
1050 | FREETMPS; |
|
|
1051 | LEAVE; |
|
|
1052 | continue; |
|
|
1053 | } |
|
|
1054 | |
|
|
1055 | ta->next = SvSTATE (next_sv); |
|
|
1056 | |
|
|
1057 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1058 | if (ta->next->flags & CF_DESTROYED) |
|
|
1059 | { |
|
|
1060 | UNLOCK; |
|
|
1061 | SvREFCNT_dec (next_sv); |
|
|
1062 | /* coro_nready is already taken care of by destroy */ |
|
|
1063 | continue; |
|
|
1064 | } |
|
|
1065 | |
|
|
1066 | --coro_nready; |
988 | UNLOCK; |
1067 | UNLOCK; |
989 | |
|
|
990 | if (next) |
|
|
991 | break; |
1068 | 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 | } |
1069 | } |
1006 | } |
|
|
1007 | |
|
|
1008 | prev = SvRV (coro_current); |
|
|
1009 | SvRV_set (coro_current, next); |
|
|
1010 | |
1070 | |
1011 | /* free this only after the transfer */ |
1071 | /* free this only after the transfer */ |
|
|
1072 | prev_sv = SvRV (coro_current); |
|
|
1073 | SvRV_set (coro_current, next_sv); |
|
|
1074 | ta->prev = SvSTATE (prev_sv); |
|
|
1075 | |
|
|
1076 | assert (ta->next->flags & CF_READY); |
|
|
1077 | ta->next->flags &= ~CF_READY; |
|
|
1078 | |
1012 | LOCK; |
1079 | LOCK; |
1013 | free_coro_mortal (); |
1080 | free_coro_mortal (); |
|
|
1081 | coro_mortal = prev_sv; |
1014 | UNLOCK; |
1082 | UNLOCK; |
1015 | coro_mortal = prev; |
|
|
1016 | |
|
|
1017 | assert (!SvROK(prev));//D |
|
|
1018 | assert (!SvROK(next));//D |
|
|
1019 | |
|
|
1020 | ta->prev = SvSTATE (prev); |
|
|
1021 | ta->next = SvSTATE (next); |
|
|
1022 | |
|
|
1023 | assert (ta->next->flags & CF_READY); |
|
|
1024 | ta->next->flags &= ~CF_READY; |
|
|
1025 | } |
1083 | } |
1026 | |
1084 | |
1027 | static void |
1085 | static void |
1028 | prepare_cede (struct transfer_args *ta) |
1086 | prepare_cede (struct transfer_args *ta) |
1029 | { |
1087 | { |
… | |
… | |
1181 | break; |
1239 | break; |
1182 | } |
1240 | } |
1183 | |
1241 | |
1184 | BARRIER; |
1242 | BARRIER; |
1185 | TRANSFER (ta); |
1243 | TRANSFER (ta); |
1186 | } |
|
|
1187 | |
1244 | |
1188 | void |
1245 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1189 | _clone_state_from (SV *dst, SV *src) |
1246 | XSRETURN_YES; |
|
|
1247 | } |
|
|
1248 | |
|
|
1249 | bool |
|
|
1250 | _destroy (SV *coro_sv) |
1190 | CODE: |
1251 | CODE: |
1191 | { |
1252 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1192 | struct coro *coro_src = SvSTATE (src); |
1253 | OUTPUT: |
1193 | |
1254 | 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 | |
1255 | |
1200 | void |
1256 | void |
1201 | _exit (code) |
1257 | _exit (code) |
1202 | int code |
1258 | int code |
1203 | PROTOTYPE: $ |
1259 | PROTOTYPE: $ |