… | |
… | |
4 | #define PERL_EXT |
4 | #define PERL_EXT |
5 | |
5 | |
6 | #include "EXTERN.h" |
6 | #include "EXTERN.h" |
7 | #include "perl.h" |
7 | #include "perl.h" |
8 | #include "XSUB.h" |
8 | #include "XSUB.h" |
|
|
9 | #include "perliol.h" |
9 | |
10 | |
10 | #include "patchlevel.h" |
11 | #include "patchlevel.h" |
11 | |
12 | |
12 | #include <stdio.h> |
13 | #include <stdio.h> |
13 | #include <errno.h> |
14 | #include <errno.h> |
… | |
… | |
45 | # define BOOT_PAGESIZE (void)0 |
46 | # define BOOT_PAGESIZE (void)0 |
46 | #endif |
47 | #endif |
47 | |
48 | |
48 | #if CORO_USE_VALGRIND |
49 | #if CORO_USE_VALGRIND |
49 | # include <valgrind/valgrind.h> |
50 | # include <valgrind/valgrind.h> |
50 | # define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) |
|
|
51 | #else |
|
|
52 | # define REGISTER_STACK(cctx,start,end) |
|
|
53 | #endif |
51 | #endif |
54 | |
52 | |
55 | /* the maximum number of idle cctx that will be pooled */ |
53 | /* the maximum number of idle cctx that will be pooled */ |
56 | #define MAX_IDLE_CCTX 8 |
54 | static int cctx_max_idle = 4; |
57 | |
55 | |
58 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
59 | (PERL_REVISION > (a) \ |
57 | (PERL_REVISION > (a) \ |
60 | || (PERL_REVISION == (a) \ |
58 | || (PERL_REVISION == (a) \ |
61 | && (PERL_VERSION > (b) \ |
59 | && (PERL_VERSION > (b) \ |
… | |
… | |
80 | # ifndef IS_PADCONST |
78 | # ifndef IS_PADCONST |
81 | # define IS_PADCONST(v) 0 |
79 | # define IS_PADCONST(v) 0 |
82 | # endif |
80 | # endif |
83 | #endif |
81 | #endif |
84 | |
82 | |
|
|
83 | /* 5.11 */ |
|
|
84 | #ifndef CxHASARGS |
|
|
85 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | /* 5.10.0 */ |
|
|
89 | #ifndef SvREFCNT_inc_NN |
|
|
90 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
91 | #endif |
|
|
92 | |
85 | /* 5.8.8 */ |
93 | /* 5.8.8 */ |
86 | #ifndef GV_NOTQUAL |
94 | #ifndef GV_NOTQUAL |
87 | # define GV_NOTQUAL 0 |
95 | # define GV_NOTQUAL 0 |
88 | #endif |
96 | #endif |
89 | #ifndef newSV |
97 | #ifndef newSV |
90 | # define newSV(l) NEWSV(0,l) |
98 | # define newSV(l) NEWSV(0,l) |
91 | #endif |
99 | #endif |
92 | |
100 | |
93 | /* 5.11 */ |
|
|
94 | #ifndef CxHASARGS |
|
|
95 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
96 | #endif |
|
|
97 | |
|
|
98 | /* 5.8.7 */ |
101 | /* 5.8.7 */ |
99 | #ifndef SvRV_set |
102 | #ifndef SvRV_set |
100 | # define SvRV_set(s,v) SvRV(s) = (v) |
103 | # define SvRV_set(s,v) SvRV(s) = (v) |
101 | #endif |
104 | #endif |
102 | |
105 | |
… | |
… | |
113 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
116 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
114 | #endif |
117 | #endif |
115 | |
118 | |
116 | /* The next macros try to return the current stack pointer, in an as |
119 | /* The next macros try to return the current stack pointer, in an as |
117 | * portable way as possible. */ |
120 | * portable way as possible. */ |
118 | #define dSTACKLEVEL volatile char stacklevel |
121 | #if __GNUC__ >= 4 |
119 | #define STACKLEVEL ((void *)&stacklevel) |
122 | # define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) |
|
|
123 | #else |
|
|
124 | # define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel |
|
|
125 | #endif |
120 | |
126 | |
121 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
127 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
122 | |
128 | |
123 | #if __GNUC__ >= 3 |
129 | #if __GNUC__ >= 3 |
124 | # define attribute(x) __attribute__(x) |
130 | # define attribute(x) __attribute__(x) |
125 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
|
|
126 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
131 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
132 | # define INLINE static inline |
127 | #else |
133 | #else |
128 | # define attribute(x) |
134 | # define attribute(x) |
129 | # define BARRIER |
|
|
130 | # define expect(expr,value) (expr) |
135 | # define expect(expr,value) (expr) |
|
|
136 | # define INLINE static |
131 | #endif |
137 | #endif |
132 | |
138 | |
133 | #define expect_false(expr) expect ((expr) != 0, 0) |
139 | #define expect_false(expr) expect ((expr) != 0, 0) |
134 | #define expect_true(expr) expect ((expr) != 0, 1) |
140 | #define expect_true(expr) expect ((expr) != 0, 1) |
135 | |
141 | |
136 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
137 | |
143 | |
138 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
139 | |
145 | |
140 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
|
|
147 | |
141 | static perl_mutex coro_mutex; |
148 | static perl_mutex coro_lock; |
142 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
143 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
150 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
151 | # if CORO_PTHREAD |
|
|
152 | static void *coro_thx; |
|
|
153 | # endif |
|
|
154 | |
144 | #else |
155 | #else |
|
|
156 | |
145 | # define LOCK (void)0 |
157 | # define LOCK (void)0 |
146 | # define UNLOCK (void)0 |
158 | # define UNLOCK (void)0 |
|
|
159 | |
147 | #endif |
160 | #endif |
|
|
161 | |
|
|
162 | # undef LOCK |
|
|
163 | # define LOCK (void)0 |
|
|
164 | # undef UNLOCK |
|
|
165 | # define UNLOCK (void)0 |
148 | |
166 | |
149 | /* helper storage struct for Coro::AIO */ |
167 | /* helper storage struct for Coro::AIO */ |
150 | struct io_state |
168 | struct io_state |
151 | { |
169 | { |
|
|
170 | AV *res; |
152 | int errorno; |
171 | int errorno; |
153 | I32 laststype; |
172 | I32 laststype; /* U16 in 5.10.0 */ |
154 | int laststatval; |
173 | int laststatval; |
155 | Stat_t statcache; |
174 | Stat_t statcache; |
156 | }; |
175 | }; |
157 | |
176 | |
|
|
177 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
178 | |
|
|
179 | static U32 cctx_gen; |
158 | static size_t coro_stacksize = CORO_STACKSIZE; |
180 | static size_t cctx_stacksize = CORO_STACKSIZE; |
159 | static struct CoroAPI coroapi; |
181 | static struct CoroAPI coroapi; |
160 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
161 | static JMPENV *main_top_env; |
183 | static JMPENV *main_top_env; |
162 | static HV *coro_state_stash, *coro_stash; |
184 | static HV *coro_state_stash, *coro_stash; |
163 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
185 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
|
|
186 | static volatile struct coro *transfer_next; |
|
|
187 | |
|
|
188 | struct transfer_args |
|
|
189 | { |
|
|
190 | struct coro *prev, *next; |
|
|
191 | }; |
164 | |
192 | |
165 | static GV *irsgv; /* $/ */ |
193 | static GV *irsgv; /* $/ */ |
166 | static GV *stdoutgv; /* *STDOUT */ |
194 | static GV *stdoutgv; /* *STDOUT */ |
167 | static SV *rv_diehook; |
195 | static SV *rv_diehook; |
168 | static SV *rv_warnhook; |
196 | static SV *rv_warnhook; |
… | |
… | |
200 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
228 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
201 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
229 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
202 | JMPENV *top_env; |
230 | JMPENV *top_env; |
203 | coro_context cctx; |
231 | coro_context cctx; |
204 | |
232 | |
|
|
233 | U32 gen; |
205 | #if CORO_USE_VALGRIND |
234 | #if CORO_USE_VALGRIND |
206 | int valgrind_id; |
235 | int valgrind_id; |
207 | #endif |
236 | #endif |
208 | unsigned char flags; |
237 | unsigned char flags; |
209 | } coro_cctx; |
238 | } coro_cctx; |
… | |
… | |
269 | #define PRIO_MIN -4 |
298 | #define PRIO_MIN -4 |
270 | |
299 | |
271 | /* for Coro.pm */ |
300 | /* for Coro.pm */ |
272 | static SV *coro_current; |
301 | static SV *coro_current; |
273 | static SV *coro_readyhook; |
302 | static SV *coro_readyhook; |
274 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
303 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
275 | static int coro_nready; |
|
|
276 | static struct coro *coro_first; |
304 | static struct coro *coro_first; |
|
|
305 | #define coro_nready coroapi.nready |
277 | |
306 | |
278 | /** lowlevel stuff **********************************************************/ |
307 | /** lowlevel stuff **********************************************************/ |
279 | |
308 | |
280 | static SV * |
309 | static SV * |
281 | coro_get_sv (pTHX_ const char *name, int create) |
310 | coro_get_sv (pTHX_ const char *name, int create) |
… | |
… | |
321 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
350 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
322 | #endif |
351 | #endif |
323 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
352 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
324 | --AvFILLp (padlist); |
353 | --AvFILLp (padlist); |
325 | |
354 | |
326 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
355 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
327 | av_store (newpadlist, 1, (SV *)newpad); |
356 | av_store (newpadlist, 1, (SV *)newpad); |
328 | |
357 | |
329 | return newpadlist; |
358 | return newpadlist; |
330 | } |
359 | } |
331 | |
360 | |
… | |
… | |
361 | |
390 | |
362 | /* casting is fun. */ |
391 | /* casting is fun. */ |
363 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
392 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
364 | free_padlist (aTHX_ padlist); |
393 | free_padlist (aTHX_ padlist); |
365 | |
394 | |
|
|
395 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
|
|
396 | |
366 | return 0; |
397 | return 0; |
367 | } |
398 | } |
368 | |
399 | |
369 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
400 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
370 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
401 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
… | |
… | |
372 | static MGVTBL coro_cv_vtbl = { |
403 | static MGVTBL coro_cv_vtbl = { |
373 | 0, 0, 0, 0, |
404 | 0, 0, 0, 0, |
374 | coro_cv_free |
405 | coro_cv_free |
375 | }; |
406 | }; |
376 | |
407 | |
377 | #define CORO_MAGIC(sv,type) \ |
408 | #define CORO_MAGIC(sv, type) \ |
378 | SvMAGIC (sv) \ |
409 | SvMAGIC (sv) \ |
379 | ? SvMAGIC (sv)->mg_type == type \ |
410 | ? SvMAGIC (sv)->mg_type == type \ |
380 | ? SvMAGIC (sv) \ |
411 | ? SvMAGIC (sv) \ |
381 | : mg_find (sv, type) \ |
412 | : mg_find (sv, type) \ |
382 | : 0 |
413 | : 0 |
383 | |
414 | |
384 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
415 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
385 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
416 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
386 | |
417 | |
387 | static struct coro * |
418 | INLINE struct coro * |
388 | SvSTATE_ (pTHX_ SV *coro) |
419 | SvSTATE_ (pTHX_ SV *coro) |
389 | { |
420 | { |
390 | HV *stash; |
421 | HV *stash; |
391 | MAGIC *mg; |
422 | MAGIC *mg; |
392 | |
423 | |
… | |
… | |
420 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
451 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
421 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
452 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
422 | else |
453 | else |
423 | { |
454 | { |
424 | #if CORO_PREFER_PERL_FUNCTIONS |
455 | #if CORO_PREFER_PERL_FUNCTIONS |
425 | /* this is probably cleaner, but also slower? */ |
456 | /* this is probably cleaner? but also slower! */ |
|
|
457 | /* in practise, it seems to be less stable */ |
426 | CV *cp = Perl_cv_clone (cv); |
458 | CV *cp = Perl_cv_clone (cv); |
427 | CvPADLIST (cv) = CvPADLIST (cp); |
459 | CvPADLIST (cv) = CvPADLIST (cp); |
428 | CvPADLIST (cp) = 0; |
460 | CvPADLIST (cp) = 0; |
429 | SvREFCNT_dec (cp); |
461 | SvREFCNT_dec (cp); |
430 | #else |
462 | #else |
… | |
… | |
616 | |
648 | |
617 | /* |
649 | /* |
618 | * destroy the stacks, the callchain etc... |
650 | * destroy the stacks, the callchain etc... |
619 | */ |
651 | */ |
620 | static void |
652 | static void |
621 | coro_destroy_stacks (pTHX) |
653 | coro_destruct_stacks (pTHX) |
622 | { |
654 | { |
623 | while (PL_curstackinfo->si_next) |
655 | while (PL_curstackinfo->si_next) |
624 | PL_curstackinfo = PL_curstackinfo->si_next; |
656 | PL_curstackinfo = PL_curstackinfo->si_next; |
625 | |
657 | |
626 | while (PL_curstackinfo) |
658 | while (PL_curstackinfo) |
… | |
… | |
663 | #undef VAR |
695 | #undef VAR |
664 | } |
696 | } |
665 | else |
697 | else |
666 | slot = coro->slot; |
698 | slot = coro->slot; |
667 | |
699 | |
|
|
700 | if (slot) |
|
|
701 | { |
668 | rss += sizeof (slot->curstackinfo); |
702 | rss += sizeof (slot->curstackinfo); |
669 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
703 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
670 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
704 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
671 | rss += slot->tmps_max * sizeof (SV *); |
705 | rss += slot->tmps_max * sizeof (SV *); |
672 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
706 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
673 | rss += slot->scopestack_max * sizeof (I32); |
707 | rss += slot->scopestack_max * sizeof (I32); |
674 | rss += slot->savestack_max * sizeof (ANY); |
708 | rss += slot->savestack_max * sizeof (ANY); |
675 | |
709 | |
676 | #if !PERL_VERSION_ATLEAST (5,10,0) |
710 | #if !PERL_VERSION_ATLEAST (5,10,0) |
677 | rss += slot->retstack_max * sizeof (OP *); |
711 | rss += slot->retstack_max * sizeof (OP *); |
678 | #endif |
712 | #endif |
|
|
713 | } |
679 | } |
714 | } |
680 | |
715 | |
681 | return rss; |
716 | return rss; |
|
|
717 | } |
|
|
718 | |
|
|
719 | /** set stacklevel support **************************************************/ |
|
|
720 | |
|
|
721 | /* we sometimes need to create the effect of pp_slf calling us */ |
|
|
722 | #define SLF_HEAD (void)0 |
|
|
723 | /* we sometimes need to create the effect of leaving via pp_slf */ |
|
|
724 | #define SLF_TAIL slf_tail (aTHX) |
|
|
725 | |
|
|
726 | INLINE void |
|
|
727 | slf_tail (pTHX) |
|
|
728 | { |
|
|
729 | dSP; |
|
|
730 | SV **bot = SP; |
|
|
731 | |
|
|
732 | int gimme = GIMME_V; |
|
|
733 | |
|
|
734 | /* make sure we put something on the stack in scalar context */ |
|
|
735 | if (gimme == G_SCALAR) |
|
|
736 | { |
|
|
737 | if (sp == bot) |
|
|
738 | XPUSHs (&PL_sv_undef); |
|
|
739 | |
|
|
740 | SP = bot + 1; |
|
|
741 | } |
|
|
742 | |
|
|
743 | PUTBACK; |
682 | } |
744 | } |
683 | |
745 | |
684 | /** coroutine stack handling ************************************************/ |
746 | /** coroutine stack handling ************************************************/ |
685 | |
747 | |
686 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
748 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
687 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
749 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
750 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
|
|
751 | |
|
|
752 | /* apparently < 5.8.8 */ |
|
|
753 | #ifndef MgPV_nolen_const |
|
|
754 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
|
|
755 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
|
|
756 | (const char*)(mg)->mg_ptr) |
|
|
757 | #endif |
688 | |
758 | |
689 | /* |
759 | /* |
690 | * This overrides the default magic get method of %SIG elements. |
760 | * This overrides the default magic get method of %SIG elements. |
691 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
761 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
692 | * and instead of tryign to save and restore the hash elements, we just provide |
762 | * and instead of tryign to save and restore the hash elements, we just provide |
… | |
… | |
700 | { |
770 | { |
701 | const char *s = MgPV_nolen_const (mg); |
771 | const char *s = MgPV_nolen_const (mg); |
702 | |
772 | |
703 | if (*s == '_') |
773 | if (*s == '_') |
704 | { |
774 | { |
705 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
775 | SV **svp = 0; |
706 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
776 | |
|
|
777 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
778 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
779 | |
|
|
780 | if (svp) |
|
|
781 | { |
|
|
782 | sv_setsv (sv, *svp ? *svp : &PL_sv_undef); |
|
|
783 | return 0; |
|
|
784 | } |
707 | } |
785 | } |
708 | |
786 | |
709 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
787 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
|
|
788 | } |
|
|
789 | |
|
|
790 | static int |
|
|
791 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
|
|
792 | { |
|
|
793 | const char *s = MgPV_nolen_const (mg); |
|
|
794 | |
|
|
795 | if (*s == '_') |
|
|
796 | { |
|
|
797 | SV **svp = 0; |
|
|
798 | |
|
|
799 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
800 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
801 | |
|
|
802 | if (svp) |
|
|
803 | { |
|
|
804 | SV *old = *svp; |
|
|
805 | *svp = 0; |
|
|
806 | SvREFCNT_dec (old); |
|
|
807 | return 0; |
|
|
808 | } |
|
|
809 | } |
|
|
810 | |
|
|
811 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
710 | } |
812 | } |
711 | |
813 | |
712 | static int |
814 | static int |
713 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
815 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
714 | { |
816 | { |
… | |
… | |
761 | GvSV (PL_defgv) = newSV (0); |
863 | GvSV (PL_defgv) = newSV (0); |
762 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
864 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
763 | GvSV (PL_errgv) = newSV (0); |
865 | GvSV (PL_errgv) = newSV (0); |
764 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
866 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
765 | PL_rs = newSVsv (GvSV (irsgv)); |
867 | PL_rs = newSVsv (GvSV (irsgv)); |
766 | PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); |
868 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
767 | |
869 | |
768 | { |
870 | { |
769 | dSP; |
871 | dSP; |
770 | LOGOP myop; |
872 | UNOP myop; |
771 | |
873 | |
772 | Zero (&myop, 1, LOGOP); |
874 | Zero (&myop, 1, UNOP); |
773 | myop.op_next = Nullop; |
875 | myop.op_next = Nullop; |
774 | myop.op_flags = OPf_WANT_VOID; |
876 | myop.op_flags = OPf_WANT_VOID; |
775 | |
877 | |
776 | PUSHMARK (SP); |
878 | PUSHMARK (SP); |
777 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
879 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
… | |
… | |
780 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
882 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
781 | SPAGAIN; |
883 | SPAGAIN; |
782 | } |
884 | } |
783 | |
885 | |
784 | /* this newly created coroutine might be run on an existing cctx which most |
886 | /* this newly created coroutine might be run on an existing cctx which most |
785 | * likely was suspended in set_stacklevel, called from entersub. |
887 | * likely was suspended in set_stacklevel, called from pp_set_stacklevel, |
786 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
888 | * so we have to emulate entering pp_set_stacklevel here. |
787 | * so we ENTER here for symmetry |
|
|
788 | */ |
889 | */ |
789 | ENTER; |
890 | SLF_HEAD; |
790 | } |
891 | } |
791 | |
892 | |
792 | static void |
893 | static void |
793 | coro_destroy (pTHX_ struct coro *coro) |
894 | coro_destruct (pTHX_ struct coro *coro) |
794 | { |
895 | { |
795 | if (!IN_DESTRUCT) |
896 | if (!IN_DESTRUCT) |
796 | { |
897 | { |
797 | /* restore all saved variables and stuff */ |
898 | /* restore all saved variables and stuff */ |
798 | LEAVE_SCOPE (0); |
899 | LEAVE_SCOPE (0); |
… | |
… | |
820 | SvREFCNT_dec (PL_warnhook); |
921 | SvREFCNT_dec (PL_warnhook); |
821 | |
922 | |
822 | SvREFCNT_dec (coro->saved_deffh); |
923 | SvREFCNT_dec (coro->saved_deffh); |
823 | SvREFCNT_dec (coro->throw); |
924 | SvREFCNT_dec (coro->throw); |
824 | |
925 | |
825 | coro_destroy_stacks (aTHX); |
926 | coro_destruct_stacks (aTHX); |
826 | } |
927 | } |
827 | |
928 | |
828 | static void |
929 | INLINE void |
829 | free_coro_mortal (pTHX) |
930 | free_coro_mortal (pTHX) |
830 | { |
931 | { |
831 | if (expect_true (coro_mortal)) |
932 | if (expect_true (coro_mortal)) |
832 | { |
933 | { |
833 | SvREFCNT_dec (coro_mortal); |
934 | SvREFCNT_dec (coro_mortal); |
… | |
… | |
867 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
968 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
868 | : bot; |
969 | : bot; |
869 | |
970 | |
870 | av_extend (av, top - bot); |
971 | av_extend (av, top - bot); |
871 | while (bot < top) |
972 | while (bot < top) |
872 | av_push (av, SvREFCNT_inc (*bot++)); |
973 | av_push (av, SvREFCNT_inc_NN (*bot++)); |
873 | |
974 | |
874 | PL_runops = RUNOPS_DEFAULT; |
975 | PL_runops = RUNOPS_DEFAULT; |
875 | ENTER; |
976 | ENTER; |
876 | SAVETMPS; |
977 | SAVETMPS; |
877 | EXTEND (SP, 3); |
978 | EXTEND (SP, 3); |
… | |
… | |
957 | |
1058 | |
958 | TAINT_NOT; |
1059 | TAINT_NOT; |
959 | return 0; |
1060 | return 0; |
960 | } |
1061 | } |
961 | |
1062 | |
|
|
1063 | static void |
|
|
1064 | prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) |
|
|
1065 | { |
|
|
1066 | ta->prev = (struct coro *)cctx; |
|
|
1067 | ta->next = 0; |
|
|
1068 | } |
|
|
1069 | |
962 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1070 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
963 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1071 | /* _cctx_init should be careful, as it could be called at almost any time */ |
964 | /* during execution of a perl program */ |
1072 | /* during execution of a perl program */ |
|
|
1073 | /* also initialises PL_top_env */ |
965 | static void NOINLINE |
1074 | static void NOINLINE |
966 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1075 | cctx_prepare (pTHX_ coro_cctx *cctx) |
967 | { |
1076 | { |
968 | dSP; |
1077 | dSP; |
969 | LOGOP myop; |
1078 | UNOP myop; |
970 | |
1079 | |
971 | PL_top_env = &PL_start_env; |
1080 | PL_top_env = &PL_start_env; |
972 | |
1081 | |
973 | if (cctx->flags & CC_TRACE) |
1082 | if (cctx->flags & CC_TRACE) |
974 | PL_runops = runops_trace; |
1083 | PL_runops = runops_trace; |
975 | |
1084 | |
976 | Zero (&myop, 1, LOGOP); |
1085 | Zero (&myop, 1, UNOP); |
977 | myop.op_next = PL_op; |
1086 | myop.op_next = PL_op; |
978 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1087 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
979 | |
1088 | |
980 | PUSHMARK (SP); |
1089 | PUSHMARK (SP); |
981 | EXTEND (SP, 2); |
1090 | EXTEND (SP, 2); |
982 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
1091 | PUSHs (sv_2mortal (newSViv ((IV)cctx))); |
983 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1092 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
984 | PUTBACK; |
1093 | PUTBACK; |
985 | PL_op = (OP *)&myop; |
1094 | PL_op = (OP *)&myop; |
986 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
1095 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
987 | SPAGAIN; |
1096 | SPAGAIN; |
988 | } |
1097 | } |
989 | |
1098 | |
|
|
1099 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
|
|
1100 | INLINE void |
|
|
1101 | transfer_tail (pTHX) |
|
|
1102 | { |
|
|
1103 | struct coro *next = (struct coro *)transfer_next; |
|
|
1104 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
|
|
1105 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
|
|
1106 | |
|
|
1107 | free_coro_mortal (aTHX); |
|
|
1108 | UNLOCK; |
|
|
1109 | |
|
|
1110 | if (expect_false (next->throw)) |
|
|
1111 | { |
|
|
1112 | SV *exception = sv_2mortal (next->throw); |
|
|
1113 | |
|
|
1114 | next->throw = 0; |
|
|
1115 | sv_setsv (ERRSV, exception); |
|
|
1116 | croak (0); |
|
|
1117 | } |
|
|
1118 | } |
|
|
1119 | |
990 | /* |
1120 | /* |
991 | * this is a _very_ stripped down perl interpreter ;) |
1121 | * this is a _very_ stripped down perl interpreter ;) |
992 | */ |
1122 | */ |
993 | static void |
1123 | static void |
994 | cctx_run (void *arg) |
1124 | cctx_run (void *arg) |
995 | { |
1125 | { |
|
|
1126 | #ifdef USE_ITHREADS |
|
|
1127 | # if CORO_PTHREAD |
|
|
1128 | PERL_SET_CONTEXT (coro_thx); |
|
|
1129 | # endif |
|
|
1130 | #endif |
|
|
1131 | { |
996 | dTHX; |
1132 | dTHX; |
997 | |
1133 | |
998 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
1134 | /* we are the alternative tail to pp_set_stacklevel */ |
999 | UNLOCK; |
1135 | /* so do the same things here */ |
|
|
1136 | SLF_TAIL; |
1000 | |
1137 | |
1001 | /* we now skip the entersub that lead to transfer() */ |
1138 | /* we now skip the op that did lead to transfer() */ |
1002 | PL_op = PL_op->op_next; |
1139 | PL_op = PL_op->op_next; |
1003 | |
1140 | |
1004 | /* inject a fake subroutine call to cctx_init */ |
1141 | /* inject a fake subroutine call to cctx_init */ |
1005 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1142 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1006 | |
1143 | |
|
|
1144 | /* cctx_run is the alternative tail of transfer() */ |
|
|
1145 | transfer_tail (aTHX); |
|
|
1146 | |
1007 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1147 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1008 | PL_restartop = PL_op; |
1148 | PL_restartop = PL_op; |
1009 | perl_run (PL_curinterp); |
1149 | perl_run (PL_curinterp); |
1010 | |
1150 | |
1011 | /* |
1151 | /* |
1012 | * If perl-run returns we assume exit() was being called or the coro |
1152 | * If perl-run returns we assume exit() was being called or the coro |
1013 | * fell off the end, which seems to be the only valid (non-bug) |
1153 | * fell off the end, which seems to be the only valid (non-bug) |
1014 | * reason for perl_run to return. We try to exit by jumping to the |
1154 | * reason for perl_run to return. We try to exit by jumping to the |
1015 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1155 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1016 | * coroutine as Coro has no such concept |
1156 | * coroutine as Coro has no such concept |
1017 | */ |
1157 | */ |
1018 | PL_top_env = main_top_env; |
1158 | PL_top_env = main_top_env; |
1019 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1159 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1160 | } |
1020 | } |
1161 | } |
1021 | |
1162 | |
1022 | static coro_cctx * |
1163 | static coro_cctx * |
1023 | cctx_new () |
1164 | cctx_new () |
1024 | { |
1165 | { |
1025 | coro_cctx *cctx; |
1166 | coro_cctx *cctx; |
|
|
1167 | |
|
|
1168 | ++cctx_count; |
|
|
1169 | New (0, cctx, 1, coro_cctx); |
|
|
1170 | |
|
|
1171 | cctx->gen = cctx_gen; |
|
|
1172 | cctx->flags = 0; |
|
|
1173 | cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */ |
|
|
1174 | |
|
|
1175 | return cctx; |
|
|
1176 | } |
|
|
1177 | |
|
|
1178 | /* create a new cctx only suitable as source */ |
|
|
1179 | static coro_cctx * |
|
|
1180 | cctx_new_empty () |
|
|
1181 | { |
|
|
1182 | coro_cctx *cctx = cctx_new (); |
|
|
1183 | |
|
|
1184 | cctx->sptr = 0; |
|
|
1185 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
|
|
1186 | |
|
|
1187 | return cctx; |
|
|
1188 | } |
|
|
1189 | |
|
|
1190 | /* create a new cctx suitable as destination/running a perl interpreter */ |
|
|
1191 | static coro_cctx * |
|
|
1192 | cctx_new_run () |
|
|
1193 | { |
|
|
1194 | coro_cctx *cctx = cctx_new (); |
1026 | void *stack_start; |
1195 | void *stack_start; |
1027 | size_t stack_size; |
1196 | size_t stack_size; |
1028 | |
1197 | |
1029 | ++cctx_count; |
|
|
1030 | |
|
|
1031 | Newz (0, cctx, 1, coro_cctx); |
|
|
1032 | |
|
|
1033 | #if HAVE_MMAP |
1198 | #if HAVE_MMAP |
1034 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1199 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1035 | /* mmap supposedly does allocate-on-write for us */ |
1200 | /* mmap supposedly does allocate-on-write for us */ |
1036 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1201 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1037 | |
1202 | |
1038 | if (cctx->sptr != (void *)-1) |
1203 | if (cctx->sptr != (void *)-1) |
1039 | { |
1204 | { |
1040 | # if CORO_STACKGUARD |
1205 | #if CORO_STACKGUARD |
1041 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1206 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
1042 | # endif |
1207 | #endif |
1043 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
1208 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
1044 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1209 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
1045 | cctx->flags |= CC_MAPPED; |
1210 | cctx->flags |= CC_MAPPED; |
1046 | } |
1211 | } |
1047 | else |
1212 | else |
1048 | #endif |
1213 | #endif |
1049 | { |
1214 | { |
1050 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
1215 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
1051 | New (0, cctx->sptr, coro_stacksize, long); |
1216 | New (0, cctx->sptr, cctx_stacksize, long); |
1052 | |
1217 | |
1053 | if (!cctx->sptr) |
1218 | if (!cctx->sptr) |
1054 | { |
1219 | { |
1055 | perror ("FATAL: unable to allocate stack for coroutine"); |
1220 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1056 | _exit (EXIT_FAILURE); |
1221 | _exit (EXIT_FAILURE); |
1057 | } |
1222 | } |
1058 | |
1223 | |
1059 | stack_start = cctx->sptr; |
1224 | stack_start = cctx->sptr; |
1060 | stack_size = cctx->ssize; |
1225 | stack_size = cctx->ssize; |
1061 | } |
1226 | } |
1062 | |
1227 | |
1063 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1228 | #if CORO_USE_VALGRIND |
|
|
1229 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1230 | #endif |
|
|
1231 | |
1064 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1232 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1065 | |
1233 | |
1066 | return cctx; |
1234 | return cctx; |
1067 | } |
1235 | } |
1068 | |
1236 | |
… | |
… | |
1071 | { |
1239 | { |
1072 | if (!cctx) |
1240 | if (!cctx) |
1073 | return; |
1241 | return; |
1074 | |
1242 | |
1075 | --cctx_count; |
1243 | --cctx_count; |
|
|
1244 | coro_destroy (&cctx->cctx); |
1076 | |
1245 | |
|
|
1246 | /* coro_transfer creates new, empty cctx's */ |
|
|
1247 | if (cctx->sptr) |
|
|
1248 | { |
1077 | #if CORO_USE_VALGRIND |
1249 | #if CORO_USE_VALGRIND |
1078 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1250 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1079 | #endif |
1251 | #endif |
1080 | |
1252 | |
1081 | #if HAVE_MMAP |
1253 | #if HAVE_MMAP |
1082 | if (cctx->flags & CC_MAPPED) |
1254 | if (cctx->flags & CC_MAPPED) |
1083 | munmap (cctx->sptr, cctx->ssize); |
1255 | munmap (cctx->sptr, cctx->ssize); |
1084 | else |
1256 | else |
1085 | #endif |
1257 | #endif |
1086 | Safefree (cctx->sptr); |
1258 | Safefree (cctx->sptr); |
|
|
1259 | } |
1087 | |
1260 | |
1088 | Safefree (cctx); |
1261 | Safefree (cctx); |
1089 | } |
1262 | } |
1090 | |
1263 | |
1091 | /* wether this cctx should be destructed */ |
1264 | /* wether this cctx should be destructed */ |
1092 | #define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) |
1265 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1093 | |
1266 | |
1094 | static coro_cctx * |
1267 | static coro_cctx * |
1095 | cctx_get (pTHX) |
1268 | cctx_get (pTHX) |
1096 | { |
1269 | { |
1097 | while (expect_true (cctx_first)) |
1270 | while (expect_true (cctx_first)) |
… | |
… | |
1104 | return cctx; |
1277 | return cctx; |
1105 | |
1278 | |
1106 | cctx_destroy (cctx); |
1279 | cctx_destroy (cctx); |
1107 | } |
1280 | } |
1108 | |
1281 | |
1109 | return cctx_new (); |
1282 | return cctx_new_run (); |
1110 | } |
1283 | } |
1111 | |
1284 | |
1112 | static void |
1285 | static void |
1113 | cctx_put (coro_cctx *cctx) |
1286 | cctx_put (coro_cctx *cctx) |
1114 | { |
1287 | { |
|
|
1288 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
|
|
1289 | |
1115 | /* free another cctx if overlimit */ |
1290 | /* free another cctx if overlimit */ |
1116 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1291 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1117 | { |
1292 | { |
1118 | coro_cctx *first = cctx_first; |
1293 | coro_cctx *first = cctx_first; |
1119 | cctx_first = first->next; |
1294 | cctx_first = first->next; |
1120 | --cctx_idle; |
1295 | --cctx_idle; |
1121 | |
1296 | |
… | |
… | |
1153 | /* always use the TRANSFER macro */ |
1328 | /* always use the TRANSFER macro */ |
1154 | static void NOINLINE |
1329 | static void NOINLINE |
1155 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1330 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1156 | { |
1331 | { |
1157 | dSTACKLEVEL; |
1332 | dSTACKLEVEL; |
1158 | static volatile int has_throw; |
|
|
1159 | |
1333 | |
1160 | /* sometimes transfer is only called to set idle_sp */ |
1334 | /* sometimes transfer is only called to set idle_sp */ |
1161 | if (expect_false (!next)) |
1335 | if (expect_false (!next)) |
1162 | { |
1336 | { |
1163 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1337 | ((coro_cctx *)prev)->idle_sp = stacklevel; |
1164 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1338 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1165 | } |
1339 | } |
1166 | else if (expect_true (prev != next)) |
1340 | else if (expect_true (prev != next)) |
1167 | { |
1341 | { |
1168 | coro_cctx *prev__cctx; |
1342 | coro_cctx *prev__cctx; |
1169 | |
1343 | |
1170 | if (expect_false (prev->flags & CF_NEW)) |
1344 | if (expect_false (prev->flags & CF_NEW)) |
1171 | { |
1345 | { |
1172 | /* create a new empty context */ |
1346 | /* create a new empty/source context */ |
1173 | Newz (0, prev->cctx, 1, coro_cctx); |
1347 | prev->cctx = cctx_new_empty (); |
1174 | prev->flags &= ~CF_NEW; |
1348 | prev->flags &= ~CF_NEW; |
1175 | prev->flags |= CF_RUNNING; |
1349 | prev->flags |= CF_RUNNING; |
1176 | } |
1350 | } |
1177 | |
1351 | |
1178 | prev->flags &= ~CF_RUNNING; |
1352 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1193 | else |
1367 | else |
1194 | load_perl (aTHX_ next); |
1368 | load_perl (aTHX_ next); |
1195 | |
1369 | |
1196 | prev__cctx = prev->cctx; |
1370 | prev__cctx = prev->cctx; |
1197 | |
1371 | |
1198 | /* possibly "free" the cctx */ |
1372 | /* possibly untie and reuse the cctx */ |
1199 | if (expect_true ( |
1373 | if (expect_true ( |
1200 | prev__cctx->idle_sp == STACKLEVEL |
1374 | prev__cctx->idle_sp == stacklevel |
1201 | && !(prev__cctx->flags & CC_TRACE) |
1375 | && !(prev__cctx->flags & CC_TRACE) |
1202 | && !force_cctx |
1376 | && !force_cctx |
1203 | )) |
1377 | )) |
1204 | { |
1378 | { |
1205 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1379 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1206 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1380 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1207 | |
1381 | |
1208 | prev->cctx = 0; |
1382 | prev->cctx = 0; |
1209 | |
1383 | |
1210 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1384 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1211 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1385 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
… | |
… | |
1219 | ++next->usecount; |
1393 | ++next->usecount; |
1220 | |
1394 | |
1221 | if (expect_true (!next->cctx)) |
1395 | if (expect_true (!next->cctx)) |
1222 | next->cctx = cctx_get (aTHX); |
1396 | next->cctx = cctx_get (aTHX); |
1223 | |
1397 | |
1224 | has_throw = !!next->throw; |
1398 | assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); |
|
|
1399 | transfer_next = next; |
1225 | |
1400 | |
1226 | if (expect_false (prev__cctx != next->cctx)) |
1401 | if (expect_false (prev__cctx != next->cctx)) |
1227 | { |
1402 | { |
1228 | prev__cctx->top_env = PL_top_env; |
1403 | prev__cctx->top_env = PL_top_env; |
1229 | PL_top_env = next->cctx->top_env; |
1404 | PL_top_env = next->cctx->top_env; |
1230 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1405 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1231 | } |
1406 | } |
1232 | |
1407 | |
1233 | free_coro_mortal (aTHX); |
1408 | transfer_tail (aTHX); |
1234 | UNLOCK; |
|
|
1235 | |
|
|
1236 | if (expect_false (has_throw)) |
|
|
1237 | { |
|
|
1238 | struct coro *coro = SvSTATE (coro_current); |
|
|
1239 | |
|
|
1240 | if (coro->throw) |
|
|
1241 | { |
|
|
1242 | SV *exception = coro->throw; |
|
|
1243 | coro->throw = 0; |
|
|
1244 | sv_setsv (ERRSV, exception); |
|
|
1245 | croak (0); |
|
|
1246 | } |
|
|
1247 | } |
|
|
1248 | } |
1409 | } |
1249 | } |
1410 | } |
1250 | |
|
|
1251 | struct transfer_args |
|
|
1252 | { |
|
|
1253 | struct coro *prev, *next; |
|
|
1254 | }; |
|
|
1255 | |
1411 | |
1256 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1412 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1257 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1413 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1258 | |
1414 | |
1259 | /** high level stuff ********************************************************/ |
1415 | /** high level stuff ********************************************************/ |
… | |
… | |
1285 | croak ("FATAL: tried to destroy currently running coroutine"); |
1441 | croak ("FATAL: tried to destroy currently running coroutine"); |
1286 | |
1442 | |
1287 | save_perl (aTHX_ &temp); |
1443 | save_perl (aTHX_ &temp); |
1288 | load_perl (aTHX_ coro); |
1444 | load_perl (aTHX_ coro); |
1289 | |
1445 | |
1290 | coro_destroy (aTHX_ coro); |
1446 | coro_destruct (aTHX_ coro); |
1291 | |
1447 | |
1292 | load_perl (aTHX_ &temp); |
1448 | load_perl (aTHX_ &temp); |
1293 | |
1449 | |
1294 | coro->slot = 0; |
1450 | coro->slot = 0; |
1295 | } |
1451 | } |
… | |
… | |
1349 | ta->next = SvSTATE (next_sv); |
1505 | ta->next = SvSTATE (next_sv); |
1350 | TRANSFER_CHECK (*ta); |
1506 | TRANSFER_CHECK (*ta); |
1351 | } |
1507 | } |
1352 | |
1508 | |
1353 | static void |
1509 | static void |
1354 | api_transfer (SV *prev_sv, SV *next_sv) |
1510 | api_transfer (pTHX_ SV *prev_sv, SV *next_sv) |
1355 | { |
1511 | { |
1356 | dTHX; |
|
|
1357 | struct transfer_args ta; |
1512 | struct transfer_args ta; |
1358 | |
1513 | |
1359 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1514 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1360 | TRANSFER (ta, 1); |
1515 | TRANSFER (ta, 1); |
1361 | } |
1516 | } |
… | |
… | |
1379 | |
1534 | |
1380 | return 0; |
1535 | return 0; |
1381 | } |
1536 | } |
1382 | |
1537 | |
1383 | static int |
1538 | static int |
1384 | api_ready (SV *coro_sv) |
1539 | api_ready (pTHX_ SV *coro_sv) |
1385 | { |
1540 | { |
1386 | dTHX; |
|
|
1387 | struct coro *coro; |
1541 | struct coro *coro; |
1388 | SV *sv_hook; |
1542 | SV *sv_hook; |
1389 | void (*xs_hook)(void); |
1543 | void (*xs_hook)(void); |
1390 | |
1544 | |
1391 | if (SvROK (coro_sv)) |
1545 | if (SvROK (coro_sv)) |
… | |
… | |
1401 | LOCK; |
1555 | LOCK; |
1402 | |
1556 | |
1403 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1557 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1404 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1558 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1405 | |
1559 | |
1406 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1560 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1407 | ++coro_nready; |
1561 | ++coro_nready; |
1408 | |
1562 | |
1409 | UNLOCK; |
1563 | UNLOCK; |
1410 | |
1564 | |
1411 | if (sv_hook) |
1565 | if (sv_hook) |
… | |
… | |
1429 | |
1583 | |
1430 | return 1; |
1584 | return 1; |
1431 | } |
1585 | } |
1432 | |
1586 | |
1433 | static int |
1587 | static int |
1434 | api_is_ready (SV *coro_sv) |
1588 | api_is_ready (pTHX_ SV *coro_sv) |
1435 | { |
1589 | { |
1436 | dTHX; |
|
|
1437 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1590 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1438 | } |
1591 | } |
1439 | |
1592 | |
1440 | static void |
1593 | INLINE void |
1441 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1594 | prepare_schedule (pTHX_ struct transfer_args *ta) |
1442 | { |
1595 | { |
1443 | SV *prev_sv, *next_sv; |
1596 | SV *prev_sv, *next_sv; |
1444 | |
1597 | |
1445 | for (;;) |
1598 | for (;;) |
… | |
… | |
1471 | /* cannot transfer to destroyed coros, skip and look for next */ |
1624 | /* cannot transfer to destroyed coros, skip and look for next */ |
1472 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1625 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1473 | { |
1626 | { |
1474 | UNLOCK; |
1627 | UNLOCK; |
1475 | SvREFCNT_dec (next_sv); |
1628 | SvREFCNT_dec (next_sv); |
1476 | /* coro_nready is already taken care of by destroy */ |
1629 | /* coro_nready has already been taken care of by destroy */ |
1477 | continue; |
1630 | continue; |
1478 | } |
1631 | } |
1479 | |
1632 | |
1480 | --coro_nready; |
1633 | --coro_nready; |
1481 | UNLOCK; |
1634 | UNLOCK; |
… | |
… | |
1484 | |
1637 | |
1485 | /* free this only after the transfer */ |
1638 | /* free this only after the transfer */ |
1486 | prev_sv = SvRV (coro_current); |
1639 | prev_sv = SvRV (coro_current); |
1487 | ta->prev = SvSTATE (prev_sv); |
1640 | ta->prev = SvSTATE (prev_sv); |
1488 | TRANSFER_CHECK (*ta); |
1641 | TRANSFER_CHECK (*ta); |
1489 | assert (ta->next->flags & CF_READY); |
1642 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1490 | ta->next->flags &= ~CF_READY; |
1643 | ta->next->flags &= ~CF_READY; |
1491 | SvRV_set (coro_current, next_sv); |
1644 | SvRV_set (coro_current, next_sv); |
1492 | |
1645 | |
1493 | LOCK; |
1646 | LOCK; |
1494 | free_coro_mortal (aTHX); |
1647 | free_coro_mortal (aTHX); |
1495 | coro_mortal = prev_sv; |
1648 | coro_mortal = prev_sv; |
1496 | UNLOCK; |
1649 | UNLOCK; |
1497 | } |
1650 | } |
1498 | |
1651 | |
|
|
1652 | INLINE void |
|
|
1653 | prepare_cede (pTHX_ struct transfer_args *ta) |
|
|
1654 | { |
|
|
1655 | api_ready (aTHX_ coro_current); |
|
|
1656 | prepare_schedule (aTHX_ ta); |
|
|
1657 | } |
|
|
1658 | |
1499 | static void |
1659 | static void |
1500 | prepare_cede (pTHX_ struct transfer_args *ta) |
1660 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1501 | { |
1661 | { |
1502 | api_ready (coro_current); |
1662 | SV *prev = SvRV (coro_current); |
|
|
1663 | |
|
|
1664 | if (coro_nready) |
|
|
1665 | { |
|
|
1666 | prepare_schedule (aTHX_ ta); |
|
|
1667 | api_ready (aTHX_ prev); |
|
|
1668 | } |
|
|
1669 | else |
|
|
1670 | ta->prev = ta->next = SvSTATE (prev); |
|
|
1671 | } |
|
|
1672 | |
|
|
1673 | static void |
|
|
1674 | api_schedule (pTHX) |
|
|
1675 | { |
|
|
1676 | struct transfer_args ta; |
|
|
1677 | |
1503 | prepare_schedule (aTHX_ ta); |
1678 | prepare_schedule (aTHX_ &ta); |
|
|
1679 | TRANSFER (ta, 1); |
1504 | } |
1680 | } |
1505 | |
1681 | |
1506 | static int |
1682 | static int |
1507 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
1683 | api_cede (pTHX) |
1508 | { |
1684 | { |
1509 | if (coro_nready) |
1685 | struct transfer_args ta; |
1510 | { |
1686 | |
1511 | SV *prev = SvRV (coro_current); |
|
|
1512 | prepare_schedule (aTHX_ ta); |
1687 | prepare_cede (aTHX_ &ta); |
1513 | api_ready (prev); |
1688 | |
|
|
1689 | if (expect_true (ta.prev != ta.next)) |
|
|
1690 | { |
|
|
1691 | TRANSFER (ta, 1); |
1514 | return 1; |
1692 | return 1; |
1515 | } |
1693 | } |
1516 | else |
1694 | else |
1517 | return 0; |
1695 | return 0; |
1518 | } |
1696 | } |
1519 | |
1697 | |
1520 | static void |
|
|
1521 | api_schedule (void) |
|
|
1522 | { |
|
|
1523 | dTHX; |
|
|
1524 | struct transfer_args ta; |
|
|
1525 | |
|
|
1526 | prepare_schedule (aTHX_ &ta); |
|
|
1527 | TRANSFER (ta, 1); |
|
|
1528 | } |
|
|
1529 | |
|
|
1530 | static int |
1698 | static int |
1531 | api_cede (void) |
1699 | api_cede_notself (pTHX) |
1532 | { |
1700 | { |
1533 | dTHX; |
1701 | if (coro_nready) |
|
|
1702 | { |
1534 | struct transfer_args ta; |
1703 | struct transfer_args ta; |
1535 | |
1704 | |
1536 | prepare_cede (aTHX_ &ta); |
1705 | prepare_cede_notself (aTHX_ &ta); |
1537 | |
|
|
1538 | if (expect_true (ta.prev != ta.next)) |
|
|
1539 | { |
|
|
1540 | TRANSFER (ta, 1); |
1706 | TRANSFER (ta, 1); |
1541 | return 1; |
1707 | return 1; |
1542 | } |
1708 | } |
1543 | else |
1709 | else |
1544 | return 0; |
1710 | return 0; |
1545 | } |
1711 | } |
1546 | |
1712 | |
1547 | static int |
|
|
1548 | api_cede_notself (void) |
|
|
1549 | { |
|
|
1550 | dTHX; |
|
|
1551 | struct transfer_args ta; |
|
|
1552 | |
|
|
1553 | if (prepare_cede_notself (aTHX_ &ta)) |
|
|
1554 | { |
|
|
1555 | TRANSFER (ta, 1); |
|
|
1556 | return 1; |
|
|
1557 | } |
|
|
1558 | else |
|
|
1559 | return 0; |
|
|
1560 | } |
|
|
1561 | |
|
|
1562 | static void |
1713 | static void |
1563 | api_trace (SV *coro_sv, int flags) |
1714 | api_trace (pTHX_ SV *coro_sv, int flags) |
1564 | { |
1715 | { |
1565 | dTHX; |
|
|
1566 | struct coro *coro = SvSTATE (coro_sv); |
1716 | struct coro *coro = SvSTATE (coro_sv); |
1567 | |
1717 | |
1568 | if (flags & CC_TRACE) |
1718 | if (flags & CC_TRACE) |
1569 | { |
1719 | { |
1570 | if (!coro->cctx) |
1720 | if (!coro->cctx) |
1571 | coro->cctx = cctx_new (); |
1721 | coro->cctx = cctx_new_run (); |
1572 | else if (!(coro->cctx->flags & CC_TRACE)) |
1722 | else if (!(coro->cctx->flags & CC_TRACE)) |
1573 | croak ("cannot enable tracing on coroutine with custom stack"); |
1723 | croak ("cannot enable tracing on coroutine with custom stack"); |
1574 | |
1724 | |
1575 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1725 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1576 | } |
1726 | } |
… | |
… | |
1583 | else |
1733 | else |
1584 | coro->slot->runops = RUNOPS_DEFAULT; |
1734 | coro->slot->runops = RUNOPS_DEFAULT; |
1585 | } |
1735 | } |
1586 | } |
1736 | } |
1587 | |
1737 | |
|
|
1738 | #if 0 |
|
|
1739 | static int |
|
|
1740 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
1741 | { |
|
|
1742 | AV *padlist; |
|
|
1743 | AV *av = (AV *)mg->mg_obj; |
|
|
1744 | |
|
|
1745 | abort (); |
|
|
1746 | |
|
|
1747 | return 0; |
|
|
1748 | } |
|
|
1749 | |
|
|
1750 | static MGVTBL coro_gensub_vtbl = { |
|
|
1751 | 0, 0, 0, 0, |
|
|
1752 | coro_gensub_free |
|
|
1753 | }; |
|
|
1754 | #endif |
|
|
1755 | |
|
|
1756 | /*****************************************************************************/ |
|
|
1757 | /* PerlIO::cede */ |
|
|
1758 | |
|
|
1759 | typedef struct |
|
|
1760 | { |
|
|
1761 | PerlIOBuf base; |
|
|
1762 | NV next, every; |
|
|
1763 | } PerlIOCede; |
|
|
1764 | |
|
|
1765 | static IV |
|
|
1766 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
|
|
1767 | { |
|
|
1768 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1769 | |
|
|
1770 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
|
|
1771 | self->next = nvtime () + self->every; |
|
|
1772 | |
|
|
1773 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
|
|
1774 | } |
|
|
1775 | |
|
|
1776 | static SV * |
|
|
1777 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
|
|
1778 | { |
|
|
1779 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1780 | |
|
|
1781 | return newSVnv (self->every); |
|
|
1782 | } |
|
|
1783 | |
|
|
1784 | static IV |
|
|
1785 | PerlIOCede_flush (pTHX_ PerlIO *f) |
|
|
1786 | { |
|
|
1787 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1788 | double now = nvtime (); |
|
|
1789 | |
|
|
1790 | if (now >= self->next) |
|
|
1791 | { |
|
|
1792 | api_cede (aTHX); |
|
|
1793 | self->next = now + self->every; |
|
|
1794 | } |
|
|
1795 | |
|
|
1796 | return PerlIOBuf_flush (aTHX_ f); |
|
|
1797 | } |
|
|
1798 | |
|
|
1799 | static PerlIO_funcs PerlIO_cede = |
|
|
1800 | { |
|
|
1801 | sizeof(PerlIO_funcs), |
|
|
1802 | "cede", |
|
|
1803 | sizeof(PerlIOCede), |
|
|
1804 | PERLIO_K_DESTRUCT | PERLIO_K_RAW, |
|
|
1805 | PerlIOCede_pushed, |
|
|
1806 | PerlIOBuf_popped, |
|
|
1807 | PerlIOBuf_open, |
|
|
1808 | PerlIOBase_binmode, |
|
|
1809 | PerlIOCede_getarg, |
|
|
1810 | PerlIOBase_fileno, |
|
|
1811 | PerlIOBuf_dup, |
|
|
1812 | PerlIOBuf_read, |
|
|
1813 | PerlIOBuf_unread, |
|
|
1814 | PerlIOBuf_write, |
|
|
1815 | PerlIOBuf_seek, |
|
|
1816 | PerlIOBuf_tell, |
|
|
1817 | PerlIOBuf_close, |
|
|
1818 | PerlIOCede_flush, |
|
|
1819 | PerlIOBuf_fill, |
|
|
1820 | PerlIOBase_eof, |
|
|
1821 | PerlIOBase_error, |
|
|
1822 | PerlIOBase_clearerr, |
|
|
1823 | PerlIOBase_setlinebuf, |
|
|
1824 | PerlIOBuf_get_base, |
|
|
1825 | PerlIOBuf_bufsiz, |
|
|
1826 | PerlIOBuf_get_ptr, |
|
|
1827 | PerlIOBuf_get_cnt, |
|
|
1828 | PerlIOBuf_set_ptrcnt, |
|
|
1829 | }; |
|
|
1830 | |
|
|
1831 | /*****************************************************************************/ |
|
|
1832 | |
|
|
1833 | static const CV *slf_cv; /* for quick consistency check */ |
|
|
1834 | |
|
|
1835 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
|
|
1836 | static SV *slf_arg0; |
|
|
1837 | static SV *slf_arg1; |
|
|
1838 | |
|
|
1839 | /* this restores the stack in the case we patched the entersub, to */ |
|
|
1840 | /* recreate the stack frame as perl will on following calls */ |
|
|
1841 | /* since entersub cleared the stack */ |
|
|
1842 | static OP * |
|
|
1843 | pp_restore (pTHX) |
|
|
1844 | { |
|
|
1845 | dSP; |
|
|
1846 | |
|
|
1847 | PUSHMARK (SP); |
|
|
1848 | |
|
|
1849 | EXTEND (SP, 3); |
|
|
1850 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
|
|
1851 | if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); |
|
|
1852 | PUSHs ((SV *)CvGV (slf_cv)); |
|
|
1853 | |
|
|
1854 | RETURNOP (slf_restore.op_first); |
|
|
1855 | } |
|
|
1856 | |
|
|
1857 | #define OPpENTERSUB_SLF 15 /* the part of op_private entersub hopefully doesn't use */ |
|
|
1858 | |
|
|
1859 | enum { |
|
|
1860 | CORO_SLF_CUSTOM = 0, |
|
|
1861 | CORO_SLF_SET_STACKLEVEL = 1, |
|
|
1862 | CORO_SLF_TRANSFER = 2 |
|
|
1863 | }; |
|
|
1864 | |
|
|
1865 | /* declare prototype */ |
|
|
1866 | XS(XS_Coro__State__set_stacklevel); |
|
|
1867 | |
|
|
1868 | /* |
|
|
1869 | * these not obviously related functions are all rolled into one |
|
|
1870 | * function to increase chances that they all will call transfer with the same |
|
|
1871 | * stack offset |
|
|
1872 | * SLF stands for "schedule-like-function". |
|
|
1873 | */ |
|
|
1874 | static OP * |
|
|
1875 | pp_slf (pTHX) |
|
|
1876 | { |
|
|
1877 | dSP; |
|
|
1878 | struct transfer_args ta; |
|
|
1879 | SV **arg = PL_stack_base + TOPMARK + 1; |
|
|
1880 | int items = SP - arg; /* args without function object */ |
|
|
1881 | int ix = PL_op->op_private & OPpENTERSUB_SLF; |
|
|
1882 | struct CoroSLF *slf = 0; |
|
|
1883 | SV *gv = *sp; |
|
|
1884 | |
|
|
1885 | /* do a quick consistency check on the "function" object, and if it isn't */ |
|
|
1886 | /* for us, divert to the real entersub */ |
|
|
1887 | if (SvTYPE (gv) != SVt_PVGV || CvXSUB (GvCV (gv)) != XS_Coro__State__set_stacklevel) |
|
|
1888 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
1889 | |
|
|
1890 | /* pop args */ |
|
|
1891 | SP = PL_stack_base + POPMARK; |
|
|
1892 | |
|
|
1893 | if (!(PL_op->op_flags & OPf_STACKED)) |
|
|
1894 | { |
|
|
1895 | /* ampersand-form of call, use @_ instead of stack */ |
|
|
1896 | AV *av = GvAV (PL_defgv); |
|
|
1897 | arg = AvARRAY (av); |
|
|
1898 | items = AvFILLp (av) + 1; |
|
|
1899 | } |
|
|
1900 | |
|
|
1901 | PUTBACK; |
|
|
1902 | |
|
|
1903 | if (!ix) |
|
|
1904 | { |
|
|
1905 | slf = (struct CoroSLF *)CvXSUBANY (GvCV (gv)).any_ptr; |
|
|
1906 | ix = slf->prepare (aTHX_ arg, items); |
|
|
1907 | } |
|
|
1908 | |
|
|
1909 | switch (ix) |
|
|
1910 | { |
|
|
1911 | case CORO_SLF_SET_STACKLEVEL: |
|
|
1912 | prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0])); |
|
|
1913 | break; |
|
|
1914 | |
|
|
1915 | case CORO_SLF_TRANSFER: |
|
|
1916 | if (items != 2) |
|
|
1917 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items); |
|
|
1918 | |
|
|
1919 | prepare_transfer (aTHX_ &ta, arg [0], arg [1]); |
|
|
1920 | break; |
|
|
1921 | |
|
|
1922 | case CORO_SLF_SCHEDULE: |
|
|
1923 | prepare_schedule (aTHX_ &ta); |
|
|
1924 | break; |
|
|
1925 | |
|
|
1926 | case CORO_SLF_CEDE: |
|
|
1927 | prepare_cede (aTHX_ &ta); |
|
|
1928 | break; |
|
|
1929 | |
|
|
1930 | case CORO_SLF_CEDE_NOTSELF: |
|
|
1931 | prepare_cede_notself (aTHX_ &ta); |
|
|
1932 | break; |
|
|
1933 | |
|
|
1934 | default: |
|
|
1935 | abort (); |
|
|
1936 | } |
|
|
1937 | |
|
|
1938 | do |
|
|
1939 | TRANSFER (ta, 0); |
|
|
1940 | while (slf && slf->check (aTHX)); |
|
|
1941 | |
|
|
1942 | SPAGAIN; |
|
|
1943 | |
|
|
1944 | PUTBACK; |
|
|
1945 | SLF_TAIL; |
|
|
1946 | SPAGAIN; |
|
|
1947 | RETURN; |
|
|
1948 | } |
|
|
1949 | |
|
|
1950 | static void |
|
|
1951 | coro_slf_patch (pTHX_ CV *cv, int ix, SV **args, int items) |
|
|
1952 | { |
|
|
1953 | assert (("FATAL: SLF call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_slf)); |
|
|
1954 | |
|
|
1955 | assert (("FATAL: SLF call with illegal CV value", CvGV (cv))); |
|
|
1956 | slf_cv = cv; |
|
|
1957 | |
|
|
1958 | /* we patch the op, and then re-run the whole call */ |
|
|
1959 | /* we have to put the same argument on the stack for this to work */ |
|
|
1960 | /* and this will be done by pp_restore */ |
|
|
1961 | slf_restore.op_next = (OP *)&slf_restore; |
|
|
1962 | slf_restore.op_type = OP_NULL; |
|
|
1963 | slf_restore.op_ppaddr = pp_restore; |
|
|
1964 | slf_restore.op_first = PL_op; |
|
|
1965 | |
|
|
1966 | slf_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0; |
|
|
1967 | slf_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0; |
|
|
1968 | |
|
|
1969 | PL_op->op_ppaddr = pp_slf; |
|
|
1970 | PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SLF | ix; /* we potentially share our private flags with entersub */ |
|
|
1971 | |
|
|
1972 | PL_op = (OP *)&slf_restore; |
|
|
1973 | } |
|
|
1974 | |
|
|
1975 | static void |
|
|
1976 | api_execute_slf (pTHX_ CV *cv, const struct CoroSLF *slf, SV **arg, int items) |
|
|
1977 | { |
|
|
1978 | CvXSUBANY (cv).any_ptr = (void *)slf; |
|
|
1979 | coro_slf_patch (aTHX_ cv, CORO_SLF_CUSTOM, arg, items); |
|
|
1980 | } |
|
|
1981 | |
1588 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1982 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1589 | |
1983 | |
1590 | PROTOTYPES: DISABLE |
1984 | PROTOTYPES: DISABLE |
1591 | |
1985 | |
1592 | BOOT: |
1986 | BOOT: |
1593 | { |
1987 | { |
1594 | #ifdef USE_ITHREADS |
1988 | #ifdef USE_ITHREADS |
1595 | MUTEX_INIT (&coro_mutex); |
1989 | MUTEX_INIT (&coro_lock); |
|
|
1990 | # if CORO_PTHREAD |
|
|
1991 | coro_thx = PERL_GET_CONTEXT; |
|
|
1992 | # endif |
1596 | #endif |
1993 | #endif |
1597 | BOOT_PAGESIZE; |
1994 | BOOT_PAGESIZE; |
1598 | |
1995 | |
1599 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1996 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1600 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1997 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1601 | |
1998 | |
1602 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
1999 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
1603 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2000 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
1604 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; |
2001 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
1605 | PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
|
|
1606 | |
2002 | |
1607 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
2003 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1608 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
2004 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
1609 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
2005 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1610 | |
2006 | |
… | |
… | |
1619 | main_top_env = PL_top_env; |
2015 | main_top_env = PL_top_env; |
1620 | |
2016 | |
1621 | while (main_top_env->je_prev) |
2017 | while (main_top_env->je_prev) |
1622 | main_top_env = main_top_env->je_prev; |
2018 | main_top_env = main_top_env->je_prev; |
1623 | |
2019 | |
1624 | coroapi.ver = CORO_API_VERSION; |
2020 | coroapi.ver = CORO_API_VERSION; |
1625 | coroapi.rev = CORO_API_REVISION; |
2021 | coroapi.rev = CORO_API_REVISION; |
1626 | coroapi.transfer = api_transfer; |
2022 | coroapi.transfer = api_transfer; |
|
|
2023 | coroapi.execute_slf = api_execute_slf; |
|
|
2024 | |
|
|
2025 | { |
|
|
2026 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
2027 | |
|
|
2028 | if (!svp) croak ("Time::HiRes is required"); |
|
|
2029 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
2030 | |
|
|
2031 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
2032 | } |
1627 | |
2033 | |
1628 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2034 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1629 | } |
2035 | } |
1630 | |
2036 | |
1631 | SV * |
2037 | SV * |
… | |
… | |
1655 | av_push (coro->args, newSVsv (ST (i))); |
2061 | av_push (coro->args, newSVsv (ST (i))); |
1656 | } |
2062 | } |
1657 | OUTPUT: |
2063 | OUTPUT: |
1658 | RETVAL |
2064 | RETVAL |
1659 | |
2065 | |
1660 | # these not obviously related functions are all rolled into the same xs |
|
|
1661 | # function to increase chances that they all will call transfer with the same |
|
|
1662 | # stack offset |
|
|
1663 | void |
2066 | void |
1664 | _set_stacklevel (...) |
2067 | _set_stacklevel (...) |
1665 | ALIAS: |
2068 | ALIAS: |
|
|
2069 | _set_stacklevel = CORO_SLF_SET_STACKLEVEL |
1666 | Coro::State::transfer = 1 |
2070 | Coro::State::transfer = CORO_SLF_TRANSFER |
1667 | Coro::schedule = 2 |
2071 | Coro::schedule = CORO_SLF_SCHEDULE |
1668 | Coro::cede = 3 |
2072 | Coro::cede = CORO_SLF_CEDE |
1669 | Coro::cede_notself = 4 |
2073 | Coro::cede_notself = CORO_SLF_CEDE_NOTSELF |
1670 | CODE: |
2074 | CODE: |
1671 | { |
2075 | coro_slf_patch (aTHX_ cv, ix, &ST (0), items); |
1672 | struct transfer_args ta; |
|
|
1673 | |
|
|
1674 | PUTBACK; |
|
|
1675 | switch (ix) |
|
|
1676 | { |
|
|
1677 | case 0: |
|
|
1678 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
|
|
1679 | ta.next = 0; |
|
|
1680 | break; |
|
|
1681 | |
|
|
1682 | case 1: |
|
|
1683 | if (items != 2) |
|
|
1684 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
|
|
1685 | |
|
|
1686 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
|
|
1687 | break; |
|
|
1688 | |
|
|
1689 | case 2: |
|
|
1690 | prepare_schedule (aTHX_ &ta); |
|
|
1691 | break; |
|
|
1692 | |
|
|
1693 | case 3: |
|
|
1694 | prepare_cede (aTHX_ &ta); |
|
|
1695 | break; |
|
|
1696 | |
|
|
1697 | case 4: |
|
|
1698 | if (!prepare_cede_notself (aTHX_ &ta)) |
|
|
1699 | XSRETURN_EMPTY; |
|
|
1700 | |
|
|
1701 | break; |
|
|
1702 | } |
|
|
1703 | SPAGAIN; |
|
|
1704 | |
|
|
1705 | BARRIER; |
|
|
1706 | PUTBACK; |
|
|
1707 | TRANSFER (ta, 0); |
|
|
1708 | SPAGAIN; /* might be the sp of a different coroutine now */ |
|
|
1709 | /* be extra careful not to ever do anything after TRANSFER */ |
|
|
1710 | } |
|
|
1711 | |
2076 | |
1712 | bool |
2077 | bool |
1713 | _destroy (SV *coro_sv) |
2078 | _destroy (SV *coro_sv) |
1714 | CODE: |
2079 | CODE: |
1715 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2080 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
1723 | _exit (code); |
2088 | _exit (code); |
1724 | |
2089 | |
1725 | int |
2090 | int |
1726 | cctx_stacksize (int new_stacksize = 0) |
2091 | cctx_stacksize (int new_stacksize = 0) |
1727 | CODE: |
2092 | CODE: |
1728 | RETVAL = coro_stacksize; |
2093 | RETVAL = cctx_stacksize; |
1729 | if (new_stacksize) |
2094 | if (new_stacksize) |
|
|
2095 | { |
1730 | coro_stacksize = new_stacksize; |
2096 | cctx_stacksize = new_stacksize; |
|
|
2097 | ++cctx_gen; |
|
|
2098 | } |
|
|
2099 | OUTPUT: |
|
|
2100 | RETVAL |
|
|
2101 | |
|
|
2102 | int |
|
|
2103 | cctx_max_idle (int max_idle = 0) |
|
|
2104 | CODE: |
|
|
2105 | RETVAL = cctx_max_idle; |
|
|
2106 | if (max_idle > 1) |
|
|
2107 | cctx_max_idle = max_idle; |
1731 | OUTPUT: |
2108 | OUTPUT: |
1732 | RETVAL |
2109 | RETVAL |
1733 | |
2110 | |
1734 | int |
2111 | int |
1735 | cctx_count () |
2112 | cctx_count () |
… | |
… | |
1759 | call (Coro::State coro, SV *coderef) |
2136 | call (Coro::State coro, SV *coderef) |
1760 | ALIAS: |
2137 | ALIAS: |
1761 | eval = 1 |
2138 | eval = 1 |
1762 | CODE: |
2139 | CODE: |
1763 | { |
2140 | { |
1764 | if (coro->mainstack) |
2141 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
1765 | { |
2142 | { |
1766 | struct coro temp; |
2143 | struct coro temp; |
1767 | |
2144 | |
1768 | if (!(coro->flags & CF_RUNNING)) |
2145 | if (!(coro->flags & CF_RUNNING)) |
1769 | { |
2146 | { |
… | |
… | |
1813 | RETVAL = boolSV (coro->flags & ix); |
2190 | RETVAL = boolSV (coro->flags & ix); |
1814 | OUTPUT: |
2191 | OUTPUT: |
1815 | RETVAL |
2192 | RETVAL |
1816 | |
2193 | |
1817 | void |
2194 | void |
|
|
2195 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2196 | PROTOTYPE: $;$ |
|
|
2197 | CODE: |
|
|
2198 | SvREFCNT_dec (self->throw); |
|
|
2199 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2200 | |
|
|
2201 | void |
1818 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2202 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
2203 | C_ARGS: aTHX_ coro, flags |
1819 | |
2204 | |
1820 | SV * |
2205 | SV * |
1821 | has_cctx (Coro::State coro) |
2206 | has_cctx (Coro::State coro) |
1822 | PROTOTYPE: $ |
2207 | PROTOTYPE: $ |
1823 | CODE: |
2208 | CODE: |
… | |
… | |
1831 | CODE: |
2216 | CODE: |
1832 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
2217 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1833 | OUTPUT: |
2218 | OUTPUT: |
1834 | RETVAL |
2219 | RETVAL |
1835 | |
2220 | |
1836 | IV |
2221 | UV |
1837 | rss (Coro::State coro) |
2222 | rss (Coro::State coro) |
1838 | PROTOTYPE: $ |
2223 | PROTOTYPE: $ |
1839 | ALIAS: |
2224 | ALIAS: |
1840 | usecount = 1 |
2225 | usecount = 1 |
1841 | CODE: |
2226 | CODE: |
… | |
… | |
1851 | force_cctx () |
2236 | force_cctx () |
1852 | CODE: |
2237 | CODE: |
1853 | struct coro *coro = SvSTATE (coro_current); |
2238 | struct coro *coro = SvSTATE (coro_current); |
1854 | coro->cctx->idle_sp = 0; |
2239 | coro->cctx->idle_sp = 0; |
1855 | |
2240 | |
1856 | MODULE = Coro::State PACKAGE = Coro |
|
|
1857 | |
|
|
1858 | BOOT: |
|
|
1859 | { |
|
|
1860 | int i; |
|
|
1861 | |
|
|
1862 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
1863 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
|
|
1864 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
|
|
1865 | |
|
|
1866 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
|
|
1867 | SvREADONLY_on (coro_current); |
|
|
1868 | |
|
|
1869 | coro_stash = gv_stashpv ("Coro", TRUE); |
|
|
1870 | |
|
|
1871 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
|
|
1872 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
|
|
1873 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
|
|
1874 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
|
|
1875 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
|
|
1876 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
|
|
1877 | |
|
|
1878 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
1879 | coro_ready[i] = newAV (); |
|
|
1880 | |
|
|
1881 | { |
|
|
1882 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
|
|
1883 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
|
|
1884 | |
|
|
1885 | coroapi.schedule = api_schedule; |
|
|
1886 | coroapi.cede = api_cede; |
|
|
1887 | coroapi.cede_notself = api_cede_notself; |
|
|
1888 | coroapi.ready = api_ready; |
|
|
1889 | coroapi.is_ready = api_is_ready; |
|
|
1890 | coroapi.nready = &coro_nready; |
|
|
1891 | coroapi.current = coro_current; |
|
|
1892 | |
|
|
1893 | GCoroAPI = &coroapi; |
|
|
1894 | sv_setiv (sv, (IV)&coroapi); |
|
|
1895 | SvREADONLY_on (sv); |
|
|
1896 | } |
|
|
1897 | } |
|
|
1898 | |
|
|
1899 | void |
|
|
1900 | _set_current (SV *current) |
|
|
1901 | PROTOTYPE: $ |
|
|
1902 | CODE: |
|
|
1903 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1904 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
|
|
1905 | |
|
|
1906 | void |
|
|
1907 | _set_readyhook (SV *hook) |
|
|
1908 | PROTOTYPE: $ |
|
|
1909 | CODE: |
|
|
1910 | LOCK; |
|
|
1911 | SvREFCNT_dec (coro_readyhook); |
|
|
1912 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
|
|
1913 | UNLOCK; |
|
|
1914 | |
|
|
1915 | int |
|
|
1916 | prio (Coro::State coro, int newprio = 0) |
|
|
1917 | ALIAS: |
|
|
1918 | nice = 1 |
|
|
1919 | CODE: |
|
|
1920 | { |
|
|
1921 | RETVAL = coro->prio; |
|
|
1922 | |
|
|
1923 | if (items > 1) |
|
|
1924 | { |
|
|
1925 | if (ix) |
|
|
1926 | newprio = coro->prio - newprio; |
|
|
1927 | |
|
|
1928 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
1929 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
1930 | |
|
|
1931 | coro->prio = newprio; |
|
|
1932 | } |
|
|
1933 | } |
|
|
1934 | OUTPUT: |
|
|
1935 | RETVAL |
|
|
1936 | |
|
|
1937 | SV * |
|
|
1938 | ready (SV *self) |
|
|
1939 | PROTOTYPE: $ |
|
|
1940 | CODE: |
|
|
1941 | RETVAL = boolSV (api_ready (self)); |
|
|
1942 | OUTPUT: |
|
|
1943 | RETVAL |
|
|
1944 | |
|
|
1945 | int |
|
|
1946 | nready (...) |
|
|
1947 | PROTOTYPE: |
|
|
1948 | CODE: |
|
|
1949 | RETVAL = coro_nready; |
|
|
1950 | OUTPUT: |
|
|
1951 | RETVAL |
|
|
1952 | |
|
|
1953 | void |
|
|
1954 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
1955 | PROTOTYPE: $;$ |
|
|
1956 | CODE: |
|
|
1957 | SvREFCNT_dec (self->throw); |
|
|
1958 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
1959 | |
|
|
1960 | void |
2241 | void |
1961 | swap_defsv (Coro::State self) |
2242 | swap_defsv (Coro::State self) |
1962 | PROTOTYPE: $ |
2243 | PROTOTYPE: $ |
1963 | ALIAS: |
2244 | ALIAS: |
1964 | swap_defav = 1 |
2245 | swap_defav = 1 |
… | |
… | |
1971 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2252 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
1972 | |
2253 | |
1973 | SV *tmp = *src; *src = *dst; *dst = tmp; |
2254 | SV *tmp = *src; *src = *dst; *dst = tmp; |
1974 | } |
2255 | } |
1975 | |
2256 | |
|
|
2257 | MODULE = Coro::State PACKAGE = Coro |
|
|
2258 | |
|
|
2259 | BOOT: |
|
|
2260 | { |
|
|
2261 | int i; |
|
|
2262 | |
|
|
2263 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2264 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
|
|
2265 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
|
|
2266 | |
|
|
2267 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
|
|
2268 | SvREADONLY_on (coro_current); |
|
|
2269 | |
|
|
2270 | coro_stash = gv_stashpv ("Coro", TRUE); |
|
|
2271 | |
|
|
2272 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
|
|
2273 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
|
|
2274 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
|
|
2275 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
|
|
2276 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
|
|
2277 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
|
|
2278 | |
|
|
2279 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
2280 | coro_ready[i] = newAV (); |
|
|
2281 | |
|
|
2282 | { |
|
|
2283 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
|
|
2284 | |
|
|
2285 | coroapi.schedule = api_schedule; |
|
|
2286 | coroapi.cede = api_cede; |
|
|
2287 | coroapi.cede_notself = api_cede_notself; |
|
|
2288 | coroapi.ready = api_ready; |
|
|
2289 | coroapi.is_ready = api_is_ready; |
|
|
2290 | coroapi.nready = coro_nready; |
|
|
2291 | coroapi.current = coro_current; |
|
|
2292 | |
|
|
2293 | GCoroAPI = &coroapi; |
|
|
2294 | sv_setiv (sv, (IV)&coroapi); |
|
|
2295 | SvREADONLY_on (sv); |
|
|
2296 | } |
|
|
2297 | } |
|
|
2298 | |
|
|
2299 | void |
|
|
2300 | _set_current (SV *current) |
|
|
2301 | PROTOTYPE: $ |
|
|
2302 | CODE: |
|
|
2303 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
2304 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
|
|
2305 | |
|
|
2306 | void |
|
|
2307 | _set_readyhook (SV *hook) |
|
|
2308 | PROTOTYPE: $ |
|
|
2309 | CODE: |
|
|
2310 | LOCK; |
|
|
2311 | SvREFCNT_dec (coro_readyhook); |
|
|
2312 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
|
|
2313 | UNLOCK; |
|
|
2314 | |
|
|
2315 | int |
|
|
2316 | prio (Coro::State coro, int newprio = 0) |
|
|
2317 | ALIAS: |
|
|
2318 | nice = 1 |
|
|
2319 | CODE: |
|
|
2320 | { |
|
|
2321 | RETVAL = coro->prio; |
|
|
2322 | |
|
|
2323 | if (items > 1) |
|
|
2324 | { |
|
|
2325 | if (ix) |
|
|
2326 | newprio = coro->prio - newprio; |
|
|
2327 | |
|
|
2328 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
2329 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
2330 | |
|
|
2331 | coro->prio = newprio; |
|
|
2332 | } |
|
|
2333 | } |
|
|
2334 | OUTPUT: |
|
|
2335 | RETVAL |
|
|
2336 | |
|
|
2337 | SV * |
|
|
2338 | ready (SV *self) |
|
|
2339 | PROTOTYPE: $ |
|
|
2340 | CODE: |
|
|
2341 | RETVAL = boolSV (api_ready (aTHX_ self)); |
|
|
2342 | OUTPUT: |
|
|
2343 | RETVAL |
|
|
2344 | |
|
|
2345 | int |
|
|
2346 | nready (...) |
|
|
2347 | PROTOTYPE: |
|
|
2348 | CODE: |
|
|
2349 | RETVAL = coro_nready; |
|
|
2350 | OUTPUT: |
|
|
2351 | RETVAL |
|
|
2352 | |
1976 | # for async_pool speedup |
2353 | # for async_pool speedup |
1977 | void |
2354 | void |
1978 | _pool_1 (SV *cb) |
2355 | _pool_1 (SV *cb) |
1979 | CODE: |
2356 | CODE: |
1980 | { |
2357 | { |
… | |
… | |
1985 | AV *invoke_av; |
2362 | AV *invoke_av; |
1986 | int i, len; |
2363 | int i, len; |
1987 | |
2364 | |
1988 | if (!invoke) |
2365 | if (!invoke) |
1989 | { |
2366 | { |
1990 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
2367 | SV *old = PL_diehook; |
|
|
2368 | PL_diehook = 0; |
|
|
2369 | SvREFCNT_dec (old); |
1991 | croak ("\3async_pool terminate\2\n"); |
2370 | croak ("\3async_pool terminate\2\n"); |
1992 | } |
2371 | } |
1993 | |
2372 | |
1994 | SvREFCNT_dec (coro->saved_deffh); |
2373 | SvREFCNT_dec (coro->saved_deffh); |
1995 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
2374 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
1996 | |
2375 | |
1997 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2376 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1998 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2377 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1999 | |
2378 | |
2000 | invoke_av = (AV *)SvRV (invoke); |
2379 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
2004 | |
2383 | |
2005 | if (len > 0) |
2384 | if (len > 0) |
2006 | { |
2385 | { |
2007 | av_fill (defav, len - 1); |
2386 | av_fill (defav, len - 1); |
2008 | for (i = 0; i < len; ++i) |
2387 | for (i = 0; i < len; ++i) |
2009 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
2388 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
2010 | } |
2389 | } |
2011 | |
2390 | |
2012 | SvREFCNT_dec (invoke); |
2391 | SvREFCNT_dec (invoke); |
2013 | } |
2392 | } |
2014 | |
2393 | |
… | |
… | |
2021 | sv_setsv (cb, &PL_sv_undef); |
2400 | sv_setsv (cb, &PL_sv_undef); |
2022 | |
2401 | |
2023 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2402 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2024 | coro->saved_deffh = 0; |
2403 | coro->saved_deffh = 0; |
2025 | |
2404 | |
2026 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2405 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2027 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2406 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2028 | { |
2407 | { |
2029 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
2408 | SV *old = PL_diehook; |
|
|
2409 | PL_diehook = 0; |
|
|
2410 | SvREFCNT_dec (old); |
2030 | croak ("\3async_pool terminate\2\n"); |
2411 | croak ("\3async_pool terminate\2\n"); |
2031 | } |
2412 | } |
2032 | |
2413 | |
2033 | av_clear (GvAV (PL_defgv)); |
2414 | av_clear (GvAV (PL_defgv)); |
2034 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
2415 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
2035 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2416 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2036 | |
2417 | |
2037 | coro->prio = 0; |
2418 | coro->prio = 0; |
2038 | |
2419 | |
2039 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2420 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2040 | api_trace (coro_current, 0); |
2421 | api_trace (aTHX_ coro_current, 0); |
2041 | |
2422 | |
2042 | av_push (av_async_pool, newSVsv (coro_current)); |
2423 | av_push (av_async_pool, newSVsv (coro_current)); |
2043 | } |
2424 | } |
2044 | |
2425 | |
|
|
2426 | #if 0 |
|
|
2427 | |
|
|
2428 | void |
|
|
2429 | _generator_call (...) |
|
|
2430 | PROTOTYPE: @ |
|
|
2431 | PPCODE: |
|
|
2432 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2433 | xxxx |
|
|
2434 | abort (); |
|
|
2435 | |
|
|
2436 | SV * |
|
|
2437 | gensub (SV *sub, ...) |
|
|
2438 | PROTOTYPE: &;@ |
|
|
2439 | CODE: |
|
|
2440 | { |
|
|
2441 | struct coro *coro; |
|
|
2442 | MAGIC *mg; |
|
|
2443 | CV *xcv; |
|
|
2444 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2445 | int i; |
|
|
2446 | |
|
|
2447 | CvGV (ncv) = CvGV (cv); |
|
|
2448 | CvFILE (ncv) = CvFILE (cv); |
|
|
2449 | |
|
|
2450 | Newz (0, coro, 1, struct coro); |
|
|
2451 | coro->args = newAV (); |
|
|
2452 | coro->flags = CF_NEW; |
|
|
2453 | |
|
|
2454 | av_extend (coro->args, items - 1); |
|
|
2455 | for (i = 1; i < items; i++) |
|
|
2456 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2457 | |
|
|
2458 | CvISXSUB_on (ncv); |
|
|
2459 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2460 | |
|
|
2461 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2462 | |
|
|
2463 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2464 | CvANON_on (ncv); |
|
|
2465 | |
|
|
2466 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2467 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2468 | } |
|
|
2469 | OUTPUT: |
|
|
2470 | RETVAL |
|
|
2471 | |
|
|
2472 | #endif |
|
|
2473 | |
2045 | |
2474 | |
2046 | MODULE = Coro::State PACKAGE = Coro::AIO |
2475 | MODULE = Coro::State PACKAGE = Coro::AIO |
2047 | |
2476 | |
2048 | SV * |
2477 | void |
2049 | _get_state () |
2478 | _get_state (SV *self) |
2050 | CODE: |
2479 | PPCODE: |
2051 | { |
2480 | { |
2052 | struct io_state *data; |
2481 | AV *defav = GvAV (PL_defgv); |
2053 | |
2482 | AV *av = newAV (); |
|
|
2483 | int i; |
2054 | RETVAL = newSV (sizeof (struct io_state)); |
2484 | SV *data_sv = newSV (sizeof (struct io_state)); |
2055 | data = (struct io_state *)SvPVX (RETVAL); |
2485 | struct io_state *data = (struct io_state *)SvPVX (data_sv); |
2056 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
2486 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2057 | SvPOK_only (RETVAL); |
2487 | SvPOK_only (data_sv); |
2058 | |
2488 | |
2059 | data->errorno = errno; |
2489 | data->errorno = errno; |
2060 | data->laststype = PL_laststype; |
2490 | data->laststype = PL_laststype; |
2061 | data->laststatval = PL_laststatval; |
2491 | data->laststatval = PL_laststatval; |
2062 | data->statcache = PL_statcache; |
2492 | data->statcache = PL_statcache; |
|
|
2493 | |
|
|
2494 | av_extend (av, AvFILLp (defav) + 1 + 1); |
|
|
2495 | |
|
|
2496 | for (i = 0; i <= AvFILLp (defav); ++i) |
|
|
2497 | av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i])); |
|
|
2498 | |
|
|
2499 | av_push (av, data_sv); |
|
|
2500 | |
|
|
2501 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
2502 | |
|
|
2503 | api_ready (aTHX_ self); |
2063 | } |
2504 | } |
2064 | OUTPUT: |
|
|
2065 | RETVAL |
|
|
2066 | |
2505 | |
2067 | void |
2506 | void |
2068 | _set_state (char *data_) |
2507 | _set_state (SV *state) |
2069 | PROTOTYPE: $ |
2508 | PROTOTYPE: $ |
2070 | CODE: |
2509 | PPCODE: |
2071 | { |
2510 | { |
2072 | struct io_state *data = (void *)data_; |
2511 | AV *av = (AV *)SvRV (state); |
|
|
2512 | struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]); |
|
|
2513 | int i; |
2073 | |
2514 | |
2074 | errno = data->errorno; |
2515 | errno = data->errorno; |
2075 | PL_laststype = data->laststype; |
2516 | PL_laststype = data->laststype; |
2076 | PL_laststatval = data->laststatval; |
2517 | PL_laststatval = data->laststatval; |
2077 | PL_statcache = data->statcache; |
2518 | PL_statcache = data->statcache; |
|
|
2519 | |
|
|
2520 | EXTEND (SP, AvFILLp (av)); |
|
|
2521 | for (i = 0; i < AvFILLp (av); ++i) |
|
|
2522 | PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i]))); |
2078 | } |
2523 | } |
2079 | |
2524 | |
2080 | |
2525 | |
2081 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2526 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2082 | |
2527 | |
… | |
… | |
2088 | PROTOTYPE: @ |
2533 | PROTOTYPE: @ |
2089 | CODE: |
2534 | CODE: |
2090 | { |
2535 | { |
2091 | static int incede; |
2536 | static int incede; |
2092 | |
2537 | |
2093 | api_cede_notself (); |
2538 | api_cede_notself (aTHX); |
2094 | |
2539 | |
2095 | ++incede; |
2540 | ++incede; |
2096 | while (coro_nready >= incede && api_cede ()) |
2541 | while (coro_nready >= incede && api_cede (aTHX)) |
2097 | ; |
2542 | ; |
2098 | |
2543 | |
2099 | sv_setsv (sv_activity, &PL_sv_undef); |
2544 | sv_setsv (sv_activity, &PL_sv_undef); |
2100 | if (coro_nready >= incede) |
2545 | if (coro_nready >= incede) |
2101 | { |
2546 | { |
… | |
… | |
2106 | } |
2551 | } |
2107 | |
2552 | |
2108 | --incede; |
2553 | --incede; |
2109 | } |
2554 | } |
2110 | |
2555 | |
|
|
2556 | |
|
|
2557 | MODULE = Coro::State PACKAGE = PerlIO::cede |
|
|
2558 | |
|
|
2559 | BOOT: |
|
|
2560 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
|
|
2561 | |