… | |
… | |
4 | #include "perl.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
5 | #include "XSUB.h" |
6 | |
6 | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
|
|
12 | |
|
|
13 | /* the maximum number of idle cctx that will be pooled */ |
|
|
14 | #define MAX_IDLE_CCTX 8 |
|
|
15 | |
|
|
16 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
17 | (PERL_REVISION > (a) \ |
|
|
18 | || (PERL_REVISION == (a) \ |
|
|
19 | && (PERL_VERSION > (b) \ |
|
|
20 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
21 | |
|
|
22 | #if !PERL_VERSION_ATLEAST (5,6,0) |
|
|
23 | # ifndef PL_ppaddr |
|
|
24 | # define PL_ppaddr ppaddr |
|
|
25 | # endif |
|
|
26 | # ifndef call_sv |
|
|
27 | # define call_sv perl_call_sv |
|
|
28 | # endif |
|
|
29 | # ifndef get_sv |
|
|
30 | # define get_sv perl_get_sv |
|
|
31 | # endif |
|
|
32 | # ifndef get_cv |
|
|
33 | # define get_cv perl_get_cv |
|
|
34 | # endif |
|
|
35 | # ifndef IS_PADGV |
|
|
36 | # define IS_PADGV(v) 0 |
|
|
37 | # endif |
|
|
38 | # ifndef IS_PADCONST |
|
|
39 | # define IS_PADCONST(v) 0 |
|
|
40 | # endif |
|
|
41 | #endif |
|
|
42 | |
|
|
43 | /* 5.8.7 */ |
|
|
44 | #ifndef SvRV_set |
|
|
45 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
46 | #endif |
|
|
47 | |
|
|
48 | #include <stdio.h> |
9 | #include <stdio.h> |
49 | #include <errno.h> |
10 | #include <errno.h> |
50 | #include <assert.h> |
11 | #include <assert.h> |
51 | |
|
|
52 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
53 | # undef STACKGUARD |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | #ifndef STACKGUARD |
|
|
57 | # define STACKGUARD 0 |
|
|
58 | #endif |
|
|
59 | |
12 | |
60 | #ifdef HAVE_MMAP |
13 | #ifdef HAVE_MMAP |
61 | # include <unistd.h> |
14 | # include <unistd.h> |
62 | # include <sys/mman.h> |
15 | # include <sys/mman.h> |
63 | # ifndef MAP_ANONYMOUS |
16 | # ifndef MAP_ANONYMOUS |
… | |
… | |
78 | #else |
31 | #else |
79 | # define PAGESIZE 0 |
32 | # define PAGESIZE 0 |
80 | # define BOOT_PAGESIZE (void)0 |
33 | # define BOOT_PAGESIZE (void)0 |
81 | #endif |
34 | #endif |
82 | |
35 | |
|
|
36 | #if USE_VALGRIND |
|
|
37 | # include <valgrind/valgrind.h> |
|
|
38 | #endif |
|
|
39 | |
|
|
40 | /* the maximum number of idle cctx that will be pooled */ |
|
|
41 | #define MAX_IDLE_CCTX 8 |
|
|
42 | |
|
|
43 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
44 | (PERL_REVISION > (a) \ |
|
|
45 | || (PERL_REVISION == (a) \ |
|
|
46 | && (PERL_VERSION > (b) \ |
|
|
47 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
48 | |
|
|
49 | #if !PERL_VERSION_ATLEAST (5,6,0) |
|
|
50 | # ifndef PL_ppaddr |
|
|
51 | # define PL_ppaddr ppaddr |
|
|
52 | # endif |
|
|
53 | # ifndef call_sv |
|
|
54 | # define call_sv perl_call_sv |
|
|
55 | # endif |
|
|
56 | # ifndef get_sv |
|
|
57 | # define get_sv perl_get_sv |
|
|
58 | # endif |
|
|
59 | # ifndef get_cv |
|
|
60 | # define get_cv perl_get_cv |
|
|
61 | # endif |
|
|
62 | # ifndef IS_PADGV |
|
|
63 | # define IS_PADGV(v) 0 |
|
|
64 | # endif |
|
|
65 | # ifndef IS_PADCONST |
|
|
66 | # define IS_PADCONST(v) 0 |
|
|
67 | # endif |
|
|
68 | #endif |
|
|
69 | |
|
|
70 | /* 5.8.7 */ |
|
|
71 | #ifndef SvRV_set |
|
|
72 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
73 | #endif |
|
|
74 | |
|
|
75 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
76 | # undef STACKGUARD |
|
|
77 | #endif |
|
|
78 | |
|
|
79 | #ifndef STACKGUARD |
|
|
80 | # define STACKGUARD 0 |
|
|
81 | #endif |
|
|
82 | |
|
|
83 | /* prefer perl internal functions over our own? */ |
|
|
84 | #ifndef PREFER_PERL_FUNCTIONS |
|
|
85 | # define PREFER_PERL_FUNCTIONS 0 |
|
|
86 | #endif |
|
|
87 | |
83 | /* The next macro should declare a variable stacklevel that contains and approximation |
88 | /* The next macro should declare a variable stacklevel that contains and approximation |
84 | * to the current C stack pointer. Its property is that it changes with each call |
89 | * to the current C stack pointer. Its property is that it changes with each call |
85 | * and should be unique. */ |
90 | * and should be unique. */ |
86 | #define dSTACKLEVEL int stacklevel |
91 | #define dSTACKLEVEL int stacklevel |
87 | #define STACKLEVEL ((void *)&stacklevel) |
92 | #define STACKLEVEL ((void *)&stacklevel) |
88 | |
93 | |
89 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
94 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
90 | |
95 | |
91 | #if __GNUC__ >= 3 |
96 | #if __GNUC__ >= 3 |
92 | # define attribute(x) __attribute__(x) |
97 | # define attribute(x) __attribute__(x) |
|
|
98 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
93 | #else |
99 | #else |
94 | # define attribute(x) |
100 | # define attribute(x) |
|
|
101 | # define BARRIER |
95 | #endif |
102 | #endif |
96 | |
103 | |
97 | #define NOINLINE attribute ((noinline)) |
104 | #define NOINLINE attribute ((noinline)) |
98 | |
105 | |
99 | #include "CoroAPI.h" |
106 | #include "CoroAPI.h" |
… | |
… | |
105 | #else |
112 | #else |
106 | # define LOCK (void)0 |
113 | # define LOCK (void)0 |
107 | # define UNLOCK (void)0 |
114 | # define UNLOCK (void)0 |
108 | #endif |
115 | #endif |
109 | |
116 | |
|
|
117 | /* helper storage struct for Coro::AIO */ |
110 | struct io_state |
118 | struct io_state |
111 | { |
119 | { |
112 | int errorno; |
120 | int errorno; |
113 | I32 laststype; |
121 | I32 laststype; |
114 | int laststatval; |
122 | int laststatval; |
… | |
… | |
130 | /* the stack */ |
138 | /* the stack */ |
131 | void *sptr; |
139 | void *sptr; |
132 | long ssize; /* positive == mmap, otherwise malloc */ |
140 | long ssize; /* positive == mmap, otherwise malloc */ |
133 | |
141 | |
134 | /* cpu state */ |
142 | /* cpu state */ |
135 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
143 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
144 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
136 | JMPENV *top_env; |
145 | JMPENV *top_env; |
137 | coro_context cctx; |
146 | coro_context cctx; |
138 | |
147 | |
139 | int inuse; |
148 | int inuse; |
140 | |
149 | |
… | |
… | |
142 | int valgrind_id; |
151 | int valgrind_id; |
143 | #endif |
152 | #endif |
144 | } coro_cctx; |
153 | } coro_cctx; |
145 | |
154 | |
146 | enum { |
155 | enum { |
147 | CF_RUNNING = 0x0001, /* coroutine is running */ |
156 | CF_RUNNING = 0x0001, /* coroutine is running */ |
148 | CF_READY = 0x0002, /* coroutine is ready */ |
157 | CF_READY = 0x0002, /* coroutine is ready */ |
149 | CF_NEW = 0x0004, /* ahs never been switched to */ |
158 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
159 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
150 | }; |
160 | }; |
151 | |
161 | |
152 | /* this is a structure representing a perl-level coroutine */ |
162 | /* this is a structure representing a perl-level coroutine */ |
153 | struct coro { |
163 | struct coro { |
154 | /* the c coroutine allocated to this perl coroutine, if any */ |
164 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
259 | |
269 | |
260 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
270 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
261 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
271 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
262 | else |
272 | else |
263 | { |
273 | { |
264 | #if 0 |
274 | #if PREFER_PERL_FUNCTIONS |
265 | /* this is probably cleaner, but also slower? */ |
275 | /* this is probably cleaner, but also slower? */ |
266 | CV *cp = Perl_cv_clone (cv); |
276 | CV *cp = Perl_cv_clone (cv); |
267 | CvPADLIST (cv) = CvPADLIST (cp); |
277 | CvPADLIST (cv) = CvPADLIST (cp); |
268 | CvPADLIST (cp) = 0; |
278 | CvPADLIST (cp) = 0; |
269 | SvREFCNT_dec (cp); |
279 | SvREFCNT_dec (cp); |
… | |
… | |
403 | * allocate various perl stacks. This is an exact copy |
413 | * allocate various perl stacks. This is an exact copy |
404 | * of perl.c:init_stacks, except that it uses less memory |
414 | * of perl.c:init_stacks, except that it uses less memory |
405 | * on the (sometimes correct) assumption that coroutines do |
415 | * on the (sometimes correct) assumption that coroutines do |
406 | * not usually need a lot of stackspace. |
416 | * not usually need a lot of stackspace. |
407 | */ |
417 | */ |
408 | #if USE_PERL_INIT_STACKS |
418 | #if PREFER_PERL_FUNCTIONS |
409 | # define coro_init_stacks init_stacks |
419 | # define coro_init_stacks init_stacks |
410 | #else |
420 | #else |
411 | |
|
|
412 | static void |
421 | static void |
413 | coro_init_stacks () |
422 | coro_init_stacks () |
414 | { |
423 | { |
415 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
424 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
416 | PL_curstackinfo->si_type = PERLSI_MAIN; |
425 | PL_curstackinfo->si_type = PERLSI_MAIN; |
… | |
… | |
446 | New(54,PL_retstack,16,OP*); |
455 | New(54,PL_retstack,16,OP*); |
447 | PL_retstack_ix = 0; |
456 | PL_retstack_ix = 0; |
448 | PL_retstack_max = 16; |
457 | PL_retstack_max = 16; |
449 | #endif |
458 | #endif |
450 | } |
459 | } |
|
|
460 | #endif |
451 | |
461 | |
452 | /* |
462 | /* |
453 | * destroy the stacks, the callchain etc... |
463 | * destroy the stacks, the callchain etc... |
454 | */ |
464 | */ |
455 | static void |
465 | static void |
… | |
… | |
463 | |
473 | |
464 | /* free all temporaries */ |
474 | /* free all temporaries */ |
465 | FREETMPS; |
475 | FREETMPS; |
466 | assert (PL_tmps_ix == -1); |
476 | assert (PL_tmps_ix == -1); |
467 | |
477 | |
|
|
478 | /* unwind all extra stacks */ |
468 | POPSTACK_TO (PL_mainstack); |
479 | POPSTACK_TO (PL_mainstack); |
|
|
480 | |
|
|
481 | /* unwind main stack */ |
|
|
482 | dounwind (-1); |
469 | } |
483 | } |
470 | |
484 | |
471 | while (PL_curstackinfo->si_next) |
485 | while (PL_curstackinfo->si_next) |
472 | PL_curstackinfo = PL_curstackinfo->si_next; |
486 | PL_curstackinfo = PL_curstackinfo->si_next; |
473 | |
487 | |
… | |
… | |
489 | Safefree (PL_savestack); |
503 | Safefree (PL_savestack); |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
504 | #if !PERL_VERSION_ATLEAST (5,9,0) |
491 | Safefree (PL_retstack); |
505 | Safefree (PL_retstack); |
492 | #endif |
506 | #endif |
493 | } |
507 | } |
494 | #endif |
|
|
495 | |
508 | |
496 | static void |
509 | static void |
497 | setup_coro (struct coro *coro) |
510 | setup_coro (struct coro *coro) |
498 | { |
511 | { |
499 | /* |
512 | /* |
… | |
… | |
702 | |
715 | |
703 | /* sometimes transfer is only called to set idle_sp */ |
716 | /* sometimes transfer is only called to set idle_sp */ |
704 | if (!next) |
717 | if (!next) |
705 | { |
718 | { |
706 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
719 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
707 | assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ |
720 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
708 | } |
721 | } |
709 | else if (prev != next) |
722 | else if (prev != next) |
710 | { |
723 | { |
711 | coro_cctx *prev__cctx; |
724 | coro_cctx *prev__cctx; |
712 | |
725 | |
… | |
… | |
723 | if (!prev->flags & CF_RUNNING) |
736 | if (!prev->flags & CF_RUNNING) |
724 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
737 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
725 | |
738 | |
726 | if (next->flags & CF_RUNNING) |
739 | if (next->flags & CF_RUNNING) |
727 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
740 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
741 | |
|
|
742 | if (next->flags & CF_DESTROYED) |
|
|
743 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
728 | |
744 | |
729 | prev->flags &= ~CF_RUNNING; |
745 | prev->flags &= ~CF_RUNNING; |
730 | next->flags |= CF_RUNNING; |
746 | next->flags |= CF_RUNNING; |
731 | |
747 | |
732 | LOCK; |
748 | LOCK; |
… | |
… | |
753 | |
769 | |
754 | /* possibly "free" the cctx */ |
770 | /* possibly "free" the cctx */ |
755 | if (prev__cctx->idle_sp == STACKLEVEL) |
771 | if (prev__cctx->idle_sp == STACKLEVEL) |
756 | { |
772 | { |
757 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
773 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
758 | assert (PL_top_env == prev__cctx->top_env); |
774 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
759 | |
775 | |
760 | prev->cctx = 0; |
776 | prev->cctx = 0; |
761 | |
777 | |
762 | cctx_put (prev__cctx); |
778 | cctx_put (prev__cctx); |
763 | prev__cctx->inuse = 0; |
779 | prev__cctx->inuse = 0; |
… | |
… | |
776 | PL_top_env = next->cctx->top_env; |
792 | PL_top_env = next->cctx->top_env; |
777 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
793 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
778 | } |
794 | } |
779 | |
795 | |
780 | free_coro_mortal (); |
796 | free_coro_mortal (); |
781 | |
|
|
782 | UNLOCK; |
797 | UNLOCK; |
783 | } |
798 | } |
784 | } |
799 | } |
785 | |
800 | |
786 | struct transfer_args |
801 | struct transfer_args |
… | |
… | |
788 | struct coro *prev, *next; |
803 | struct coro *prev, *next; |
789 | }; |
804 | }; |
790 | |
805 | |
791 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
806 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
792 | |
807 | |
793 | static void |
808 | static int |
794 | coro_state_destroy (struct coro *coro) |
809 | coro_state_destroy (struct coro *coro) |
795 | { |
810 | { |
796 | if (coro->refcnt--) |
811 | if (coro->flags & CF_DESTROYED) |
797 | return; |
812 | return 0; |
|
|
813 | |
|
|
814 | coro->flags |= CF_DESTROYED; |
798 | |
815 | |
799 | if (coro->mainstack && coro->mainstack != main_mainstack) |
816 | if (coro->mainstack && coro->mainstack != main_mainstack) |
800 | { |
817 | { |
|
|
818 | assert (!(coro->flags & CF_RUNNING)); |
|
|
819 | |
801 | struct coro temp; |
820 | struct coro temp; |
802 | Zero (&temp, 1, struct coro); |
821 | Zero (&temp, 1, struct coro); |
803 | temp.save = CORO_SAVE_ALL; |
822 | temp.save = CORO_SAVE_ALL; |
804 | |
823 | |
805 | if (coro->flags & CF_RUNNING) |
824 | if (coro->flags & CF_RUNNING) |
… | |
… | |
815 | coro->mainstack = 0; |
834 | coro->mainstack = 0; |
816 | } |
835 | } |
817 | |
836 | |
818 | cctx_destroy (coro->cctx); |
837 | cctx_destroy (coro->cctx); |
819 | SvREFCNT_dec (coro->args); |
838 | SvREFCNT_dec (coro->args); |
820 | Safefree (coro); |
839 | |
|
|
840 | return 1; |
821 | } |
841 | } |
822 | |
842 | |
823 | static int |
843 | static int |
824 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
844 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
825 | { |
845 | { |
826 | struct coro *coro = (struct coro *)mg->mg_ptr; |
846 | struct coro *coro = (struct coro *)mg->mg_ptr; |
827 | mg->mg_ptr = 0; |
847 | mg->mg_ptr = 0; |
828 | |
848 | |
|
|
849 | if (--coro->refcnt < 0) |
|
|
850 | { |
829 | coro_state_destroy (coro); |
851 | coro_state_destroy (coro); |
|
|
852 | Safefree (coro); |
|
|
853 | } |
830 | |
854 | |
831 | return 0; |
855 | return 0; |
832 | } |
856 | } |
833 | |
857 | |
834 | static int |
858 | static int |
… | |
… | |
841 | return 0; |
865 | return 0; |
842 | } |
866 | } |
843 | |
867 | |
844 | static MGVTBL coro_state_vtbl = { |
868 | static MGVTBL coro_state_vtbl = { |
845 | 0, 0, 0, 0, |
869 | 0, 0, 0, 0, |
846 | coro_state_clear, |
870 | coro_state_free, |
847 | 0, |
871 | 0, |
848 | #ifdef MGf_DUP |
872 | #ifdef MGf_DUP |
849 | coro_state_dup, |
873 | coro_state_dup, |
850 | #else |
874 | #else |
851 | # define MGf_DUP 0 |
875 | # define MGf_DUP 0 |
… | |
… | |
953 | coro = SvSTATE (coro_sv); |
977 | coro = SvSTATE (coro_sv); |
954 | |
978 | |
955 | if (coro->flags & CF_READY) |
979 | if (coro->flags & CF_READY) |
956 | return 0; |
980 | return 0; |
957 | |
981 | |
958 | #if 0 /* this is actually harmless */ |
|
|
959 | if (coro->flags & CF_RUNNING) |
|
|
960 | croak ("Coro::ready called on currently running coroutine"); |
|
|
961 | #endif |
|
|
962 | |
|
|
963 | coro->flags |= CF_READY; |
982 | coro->flags |= CF_READY; |
964 | |
983 | |
965 | LOCK; |
984 | LOCK; |
966 | coro_enq (SvREFCNT_inc (coro_sv)); |
985 | coro_enq (SvREFCNT_inc (coro_sv)); |
967 | UNLOCK; |
986 | UNLOCK; |
… | |
… | |
970 | } |
989 | } |
971 | |
990 | |
972 | static int |
991 | static int |
973 | api_is_ready (SV *coro_sv) |
992 | api_is_ready (SV *coro_sv) |
974 | { |
993 | { |
975 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
994 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
976 | } |
995 | } |
977 | |
996 | |
978 | static void |
997 | static void |
979 | prepare_schedule (struct transfer_args *ta) |
998 | prepare_schedule (struct transfer_args *ta) |
980 | { |
999 | { |
981 | SV *prev, *next; |
1000 | SV *prev_sv, *next_sv; |
982 | |
1001 | |
983 | for (;;) |
1002 | for (;;) |
984 | { |
1003 | { |
985 | LOCK; |
1004 | LOCK; |
986 | next = coro_deq (PRIO_MIN); |
1005 | next_sv = coro_deq (PRIO_MIN); |
987 | UNLOCK; |
1006 | UNLOCK; |
988 | |
1007 | |
|
|
1008 | /* nothing to schedule: call the idle handler */ |
989 | if (next) |
1009 | if (!next_sv) |
990 | break; |
|
|
991 | |
|
|
992 | { |
1010 | { |
993 | dSP; |
1011 | dSP; |
994 | |
1012 | |
995 | ENTER; |
1013 | ENTER; |
996 | SAVETMPS; |
1014 | SAVETMPS; |
997 | |
1015 | |
998 | PUSHMARK (SP); |
1016 | PUSHMARK (SP); |
999 | PUTBACK; |
1017 | PUTBACK; |
1000 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1018 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1001 | |
1019 | |
1002 | FREETMPS; |
1020 | FREETMPS; |
1003 | LEAVE; |
1021 | LEAVE; |
|
|
1022 | continue; |
1004 | } |
1023 | } |
1005 | } |
|
|
1006 | |
1024 | |
1007 | prev = SvRV (coro_current); |
1025 | ta->next = SvSTATE (next_sv); |
1008 | SvRV_set (coro_current, next); |
1026 | |
|
|
1027 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1028 | if (ta->next->flags & CF_DESTROYED) |
|
|
1029 | { |
|
|
1030 | SvREFCNT_dec (next_sv); |
|
|
1031 | continue; |
|
|
1032 | } |
|
|
1033 | |
|
|
1034 | break; |
|
|
1035 | } |
1009 | |
1036 | |
1010 | /* free this only after the transfer */ |
1037 | /* free this only after the transfer */ |
|
|
1038 | prev_sv = SvRV (coro_current); |
|
|
1039 | SvRV_set (coro_current, next_sv); |
|
|
1040 | ta->prev = SvSTATE (prev_sv); |
|
|
1041 | |
|
|
1042 | assert (ta->next->flags & CF_READY); |
|
|
1043 | ta->next->flags &= ~CF_READY; |
|
|
1044 | |
1011 | LOCK; |
1045 | LOCK; |
1012 | free_coro_mortal (); |
1046 | free_coro_mortal (); |
|
|
1047 | coro_mortal = prev_sv; |
1013 | UNLOCK; |
1048 | UNLOCK; |
1014 | coro_mortal = prev; |
|
|
1015 | |
|
|
1016 | assert (!SvROK(prev));//D |
|
|
1017 | assert (!SvROK(next));//D |
|
|
1018 | |
|
|
1019 | ta->prev = SvSTATE (prev); |
|
|
1020 | ta->next = SvSTATE (next); |
|
|
1021 | |
|
|
1022 | assert (ta->next->flags & CF_READY); |
|
|
1023 | ta->next->flags &= ~CF_READY; |
|
|
1024 | } |
1049 | } |
1025 | |
1050 | |
1026 | static void |
1051 | static void |
1027 | prepare_cede (struct transfer_args *ta) |
1052 | prepare_cede (struct transfer_args *ta) |
1028 | { |
1053 | { |
1029 | api_ready (coro_current); |
1054 | api_ready (coro_current); |
1030 | |
|
|
1031 | prepare_schedule (ta); |
1055 | prepare_schedule (ta); |
|
|
1056 | } |
|
|
1057 | |
|
|
1058 | static int |
|
|
1059 | prepare_cede_notself (struct transfer_args *ta) |
|
|
1060 | { |
|
|
1061 | if (coro_nready) |
|
|
1062 | { |
|
|
1063 | SV *prev = SvRV (coro_current); |
|
|
1064 | prepare_schedule (ta); |
|
|
1065 | api_ready (prev); |
|
|
1066 | return 1; |
|
|
1067 | } |
|
|
1068 | else |
|
|
1069 | return 0; |
1032 | } |
1070 | } |
1033 | |
1071 | |
1034 | static void |
1072 | static void |
1035 | api_schedule (void) |
1073 | api_schedule (void) |
1036 | { |
1074 | { |
… | |
… | |
1038 | |
1076 | |
1039 | prepare_schedule (&ta); |
1077 | prepare_schedule (&ta); |
1040 | TRANSFER (ta); |
1078 | TRANSFER (ta); |
1041 | } |
1079 | } |
1042 | |
1080 | |
1043 | static void |
1081 | static int |
1044 | api_cede (void) |
1082 | api_cede (void) |
1045 | { |
1083 | { |
1046 | struct transfer_args ta; |
1084 | struct transfer_args ta; |
1047 | |
1085 | |
1048 | prepare_cede (&ta); |
1086 | prepare_cede (&ta); |
|
|
1087 | |
|
|
1088 | if (ta.prev != ta.next) |
|
|
1089 | { |
1049 | TRANSFER (ta); |
1090 | TRANSFER (ta); |
|
|
1091 | return 1; |
|
|
1092 | } |
|
|
1093 | else |
|
|
1094 | return 0; |
|
|
1095 | } |
|
|
1096 | |
|
|
1097 | static int |
|
|
1098 | api_cede_notself (void) |
|
|
1099 | { |
|
|
1100 | struct transfer_args ta; |
|
|
1101 | |
|
|
1102 | if (prepare_cede_notself (&ta)) |
|
|
1103 | { |
|
|
1104 | TRANSFER (ta); |
|
|
1105 | return 1; |
|
|
1106 | } |
|
|
1107 | else |
|
|
1108 | return 0; |
1050 | } |
1109 | } |
1051 | |
1110 | |
1052 | MODULE = Coro::State PACKAGE = Coro::State |
1111 | MODULE = Coro::State PACKAGE = Coro::State |
1053 | |
1112 | |
1054 | PROTOTYPES: DISABLE |
1113 | PROTOTYPES: DISABLE |
… | |
… | |
1110 | _set_stacklevel (...) |
1169 | _set_stacklevel (...) |
1111 | ALIAS: |
1170 | ALIAS: |
1112 | Coro::State::transfer = 1 |
1171 | Coro::State::transfer = 1 |
1113 | Coro::schedule = 2 |
1172 | Coro::schedule = 2 |
1114 | Coro::cede = 3 |
1173 | Coro::cede = 3 |
|
|
1174 | Coro::cede_notself = 4 |
1115 | CODE: |
1175 | CODE: |
1116 | { |
1176 | { |
1117 | struct transfer_args ta; |
1177 | struct transfer_args ta; |
1118 | |
1178 | |
1119 | switch (ix) |
1179 | switch (ix) |
… | |
… | |
1135 | break; |
1195 | break; |
1136 | |
1196 | |
1137 | case 3: |
1197 | case 3: |
1138 | prepare_cede (&ta); |
1198 | prepare_cede (&ta); |
1139 | break; |
1199 | break; |
|
|
1200 | |
|
|
1201 | case 4: |
|
|
1202 | if (!prepare_cede_notself (&ta)) |
|
|
1203 | XSRETURN_EMPTY; |
|
|
1204 | |
|
|
1205 | break; |
1140 | } |
1206 | } |
1141 | |
1207 | |
|
|
1208 | BARRIER; |
1142 | TRANSFER (ta); |
1209 | TRANSFER (ta); |
1143 | } |
|
|
1144 | |
1210 | |
1145 | void |
1211 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1146 | _clone_state_from (SV *dst, SV *src) |
1212 | XSRETURN_YES; |
|
|
1213 | } |
|
|
1214 | |
|
|
1215 | bool |
|
|
1216 | _destroy (SV *coro_sv) |
1147 | CODE: |
1217 | CODE: |
1148 | { |
1218 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1149 | struct coro *coro_src = SvSTATE (src); |
1219 | OUTPUT: |
1150 | |
1220 | RETVAL |
1151 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1152 | |
|
|
1153 | ++coro_src->refcnt; |
|
|
1154 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1155 | } |
|
|
1156 | |
1221 | |
1157 | void |
1222 | void |
1158 | _exit (code) |
1223 | _exit (code) |
1159 | int code |
1224 | int code |
1160 | PROTOTYPE: $ |
1225 | PROTOTYPE: $ |
… | |
… | |
1197 | coro_ready[i] = newAV (); |
1262 | coro_ready[i] = newAV (); |
1198 | |
1263 | |
1199 | { |
1264 | { |
1200 | SV *sv = perl_get_sv("Coro::API", 1); |
1265 | SV *sv = perl_get_sv("Coro::API", 1); |
1201 | |
1266 | |
1202 | coroapi.schedule = api_schedule; |
1267 | coroapi.schedule = api_schedule; |
1203 | coroapi.save = api_save; |
1268 | coroapi.save = api_save; |
1204 | coroapi.cede = api_cede; |
1269 | coroapi.cede = api_cede; |
|
|
1270 | coroapi.cede_notself = api_cede_notself; |
1205 | coroapi.ready = api_ready; |
1271 | coroapi.ready = api_ready; |
1206 | coroapi.is_ready = api_is_ready; |
1272 | coroapi.is_ready = api_is_ready; |
1207 | coroapi.nready = &coro_nready; |
1273 | coroapi.nready = &coro_nready; |
1208 | coroapi.current = coro_current; |
1274 | coroapi.current = coro_current; |
1209 | |
1275 | |
1210 | GCoroAPI = &coroapi; |
1276 | GCoroAPI = &coroapi; |
1211 | sv_setiv (sv, (IV)&coroapi); |
1277 | sv_setiv (sv, (IV)&coroapi); |
1212 | SvREADONLY_on (sv); |
1278 | SvREADONLY_on (sv); |
1213 | } |
1279 | } |
… | |
… | |
1229 | RETVAL = coro->prio; |
1295 | RETVAL = coro->prio; |
1230 | |
1296 | |
1231 | if (items > 1) |
1297 | if (items > 1) |
1232 | { |
1298 | { |
1233 | if (ix) |
1299 | if (ix) |
1234 | newprio += coro->prio; |
1300 | newprio = coro->prio - newprio; |
1235 | |
1301 | |
1236 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1302 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1237 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1303 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1238 | |
1304 | |
1239 | coro->prio = newprio; |
1305 | coro->prio = newprio; |
1240 | } |
1306 | } |
1241 | } |
1307 | } |
|
|
1308 | OUTPUT: |
|
|
1309 | RETVAL |
1242 | |
1310 | |
1243 | SV * |
1311 | SV * |
1244 | ready (SV *self) |
1312 | ready (SV *self) |
1245 | PROTOTYPE: $ |
1313 | PROTOTYPE: $ |
1246 | CODE: |
1314 | CODE: |