… | |
… | |
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> |
14 | #include <assert.h> |
15 | #include <assert.h> |
|
|
16 | |
|
|
17 | #ifdef WIN32 |
|
|
18 | # undef setjmp |
|
|
19 | # undef longjmp |
|
|
20 | # undef _exit |
|
|
21 | # define setjmp _setjmp // deep magic, don't ask |
|
|
22 | #else |
15 | #include <inttypes.h> /* portable stdint.h */ |
23 | # include <inttypes.h> /* most portable stdint.h */ |
|
|
24 | #endif |
16 | |
25 | |
17 | #ifdef HAVE_MMAP |
26 | #ifdef HAVE_MMAP |
18 | # include <unistd.h> |
27 | # include <unistd.h> |
19 | # include <sys/mman.h> |
28 | # include <sys/mman.h> |
20 | # ifndef MAP_ANONYMOUS |
29 | # ifndef MAP_ANONYMOUS |
… | |
… | |
72 | # ifndef IS_PADCONST |
81 | # ifndef IS_PADCONST |
73 | # define IS_PADCONST(v) 0 |
82 | # define IS_PADCONST(v) 0 |
74 | # endif |
83 | # endif |
75 | #endif |
84 | #endif |
76 | |
85 | |
|
|
86 | /* 5.11 */ |
|
|
87 | #ifndef CxHASARGS |
|
|
88 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | /* 5.10.0 */ |
|
|
92 | #ifndef SvREFCNT_inc_NN |
|
|
93 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
94 | #endif |
|
|
95 | |
77 | /* 5.8.8 */ |
96 | /* 5.8.8 */ |
78 | #ifndef GV_NOTQUAL |
97 | #ifndef GV_NOTQUAL |
79 | # define GV_NOTQUAL 0 |
98 | # define GV_NOTQUAL 0 |
80 | #endif |
99 | #endif |
81 | #ifndef newSV |
100 | #ifndef newSV |
82 | # define newSV(l) NEWSV(0,l) |
101 | # define newSV(l) NEWSV(0,l) |
83 | #endif |
|
|
84 | |
|
|
85 | /* 5.11 */ |
|
|
86 | #ifndef CxHASARGS |
|
|
87 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
88 | #endif |
102 | #endif |
89 | |
103 | |
90 | /* 5.8.7 */ |
104 | /* 5.8.7 */ |
91 | #ifndef SvRV_set |
105 | #ifndef SvRV_set |
92 | # define SvRV_set(s,v) SvRV(s) = (v) |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
… | |
… | |
128 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
129 | |
143 | |
130 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
131 | |
145 | |
132 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
133 | static perl_mutex coro_mutex; |
147 | static perl_mutex coro_lock; |
134 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
148 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
135 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
149 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
136 | #else |
150 | #else |
137 | # define LOCK (void)0 |
151 | # define LOCK (void)0 |
138 | # define UNLOCK (void)0 |
152 | # define UNLOCK (void)0 |
139 | #endif |
153 | #endif |
140 | |
154 | |
141 | /* helper storage struct for Coro::AIO */ |
155 | /* helper storage struct for Coro::AIO */ |
142 | struct io_state |
156 | struct io_state |
143 | { |
157 | { |
|
|
158 | AV *res; |
144 | int errorno; |
159 | int errorno; |
145 | I32 laststype; |
160 | I32 laststype; |
146 | int laststatval; |
161 | int laststatval; |
147 | Stat_t statcache; |
162 | Stat_t statcache; |
148 | }; |
163 | }; |
149 | |
164 | |
|
|
165 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
166 | |
150 | static size_t coro_stacksize = CORO_STACKSIZE; |
167 | static size_t coro_stacksize = CORO_STACKSIZE; |
151 | static struct CoroAPI coroapi; |
168 | static struct CoroAPI coroapi; |
152 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
169 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
153 | static JMPENV *main_top_env; |
170 | static JMPENV *main_top_env; |
154 | static HV *coro_state_stash, *coro_stash; |
171 | static HV *coro_state_stash, *coro_stash; |
155 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
172 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
156 | |
173 | |
157 | static GV *irsgv; /* $/ */ |
174 | static GV *irsgv; /* $/ */ |
158 | static GV *stdoutgv; /* *STDOUT */ |
175 | static GV *stdoutgv; /* *STDOUT */ |
159 | static SV *rv_diehook; |
176 | static SV *rv_diehook; |
160 | static SV *rv_warnhook; |
177 | static SV *rv_warnhook; |
… | |
… | |
162 | |
179 | |
163 | /* async_pool helper stuff */ |
180 | /* async_pool helper stuff */ |
164 | static SV *sv_pool_rss; |
181 | static SV *sv_pool_rss; |
165 | static SV *sv_pool_size; |
182 | static SV *sv_pool_size; |
166 | static AV *av_async_pool; |
183 | static AV *av_async_pool; |
|
|
184 | |
|
|
185 | /* Coro::AnyEvent */ |
|
|
186 | static SV *sv_activity; |
167 | |
187 | |
168 | static struct coro_cctx *cctx_first; |
188 | static struct coro_cctx *cctx_first; |
169 | static int cctx_count, cctx_idle; |
189 | static int cctx_count, cctx_idle; |
170 | |
190 | |
171 | enum { |
191 | enum { |
… | |
… | |
257 | #define PRIO_IDLE -3 |
277 | #define PRIO_IDLE -3 |
258 | #define PRIO_MIN -4 |
278 | #define PRIO_MIN -4 |
259 | |
279 | |
260 | /* for Coro.pm */ |
280 | /* for Coro.pm */ |
261 | static SV *coro_current; |
281 | static SV *coro_current; |
|
|
282 | static SV *coro_readyhook; |
262 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
283 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
263 | static int coro_nready; |
284 | static int coro_nready; |
264 | static struct coro *coro_first; |
285 | static struct coro *coro_first; |
265 | |
286 | |
266 | /** lowlevel stuff **********************************************************/ |
287 | /** lowlevel stuff **********************************************************/ |
… | |
… | |
309 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
330 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
310 | #endif |
331 | #endif |
311 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
332 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
312 | --AvFILLp (padlist); |
333 | --AvFILLp (padlist); |
313 | |
334 | |
314 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
335 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
315 | av_store (newpadlist, 1, (SV *)newpad); |
336 | av_store (newpadlist, 1, (SV *)newpad); |
316 | |
337 | |
317 | return newpadlist; |
338 | return newpadlist; |
318 | } |
339 | } |
319 | |
340 | |
… | |
… | |
349 | |
370 | |
350 | /* casting is fun. */ |
371 | /* casting is fun. */ |
351 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
372 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
352 | free_padlist (aTHX_ padlist); |
373 | free_padlist (aTHX_ padlist); |
353 | |
374 | |
|
|
375 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
|
|
376 | |
354 | return 0; |
377 | return 0; |
355 | } |
378 | } |
356 | |
379 | |
357 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
380 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
358 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
381 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
… | |
… | |
360 | static MGVTBL coro_cv_vtbl = { |
383 | static MGVTBL coro_cv_vtbl = { |
361 | 0, 0, 0, 0, |
384 | 0, 0, 0, 0, |
362 | coro_cv_free |
385 | coro_cv_free |
363 | }; |
386 | }; |
364 | |
387 | |
365 | #define CORO_MAGIC(sv,type) \ |
388 | #define CORO_MAGIC(sv, type) \ |
366 | SvMAGIC (sv) \ |
389 | SvMAGIC (sv) \ |
367 | ? SvMAGIC (sv)->mg_type == type \ |
390 | ? SvMAGIC (sv)->mg_type == type \ |
368 | ? SvMAGIC (sv) \ |
391 | ? SvMAGIC (sv) \ |
369 | : mg_find (sv, type) \ |
392 | : mg_find (sv, type) \ |
370 | : 0 |
393 | : 0 |
371 | |
394 | |
372 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
395 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
373 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
396 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
374 | |
397 | |
375 | static struct coro * |
398 | static struct coro * |
… | |
… | |
408 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
431 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
409 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
432 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
410 | else |
433 | else |
411 | { |
434 | { |
412 | #if CORO_PREFER_PERL_FUNCTIONS |
435 | #if CORO_PREFER_PERL_FUNCTIONS |
413 | /* this is probably cleaner, but also slower? */ |
436 | /* this is probably cleaner? but also slower! */ |
|
|
437 | /* in practise, it seems to be less stable */ |
414 | CV *cp = Perl_cv_clone (cv); |
438 | CV *cp = Perl_cv_clone (cv); |
415 | CvPADLIST (cv) = CvPADLIST (cp); |
439 | CvPADLIST (cv) = CvPADLIST (cp); |
416 | CvPADLIST (cp) = 0; |
440 | CvPADLIST (cp) = 0; |
417 | SvREFCNT_dec (cp); |
441 | SvREFCNT_dec (cp); |
418 | #else |
442 | #else |
… | |
… | |
502 | |
526 | |
503 | if (expect_true (CvDEPTH (cv))) |
527 | if (expect_true (CvDEPTH (cv))) |
504 | { |
528 | { |
505 | EXTEND (SP, 3); |
529 | EXTEND (SP, 3); |
506 | PUSHs ((SV *)CvPADLIST (cv)); |
530 | PUSHs ((SV *)CvPADLIST (cv)); |
507 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
531 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
508 | PUSHs ((SV *)cv); |
532 | PUSHs ((SV *)cv); |
509 | |
533 | |
510 | CvDEPTH (cv) = 0; |
534 | CvDEPTH (cv) = 0; |
511 | get_padlist (aTHX_ cv); |
535 | get_padlist (aTHX_ cv); |
512 | } |
536 | } |
… | |
… | |
604 | |
628 | |
605 | /* |
629 | /* |
606 | * destroy the stacks, the callchain etc... |
630 | * destroy the stacks, the callchain etc... |
607 | */ |
631 | */ |
608 | static void |
632 | static void |
609 | coro_destroy_stacks (pTHX) |
633 | coro_destruct_stacks (pTHX) |
610 | { |
634 | { |
611 | while (PL_curstackinfo->si_next) |
635 | while (PL_curstackinfo->si_next) |
612 | PL_curstackinfo = PL_curstackinfo->si_next; |
636 | PL_curstackinfo = PL_curstackinfo->si_next; |
613 | |
637 | |
614 | while (PL_curstackinfo) |
638 | while (PL_curstackinfo) |
… | |
… | |
651 | #undef VAR |
675 | #undef VAR |
652 | } |
676 | } |
653 | else |
677 | else |
654 | slot = coro->slot; |
678 | slot = coro->slot; |
655 | |
679 | |
|
|
680 | if (slot) |
|
|
681 | { |
656 | rss += sizeof (slot->curstackinfo); |
682 | rss += sizeof (slot->curstackinfo); |
657 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
683 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
658 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
684 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
659 | rss += slot->tmps_max * sizeof (SV *); |
685 | rss += slot->tmps_max * sizeof (SV *); |
660 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
686 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
661 | rss += slot->scopestack_max * sizeof (I32); |
687 | rss += slot->scopestack_max * sizeof (I32); |
662 | rss += slot->savestack_max * sizeof (ANY); |
688 | rss += slot->savestack_max * sizeof (ANY); |
663 | |
689 | |
664 | #if !PERL_VERSION_ATLEAST (5,10,0) |
690 | #if !PERL_VERSION_ATLEAST (5,10,0) |
665 | rss += slot->retstack_max * sizeof (OP *); |
691 | rss += slot->retstack_max * sizeof (OP *); |
666 | #endif |
692 | #endif |
|
|
693 | } |
667 | } |
694 | } |
668 | |
695 | |
669 | return rss; |
696 | return rss; |
670 | } |
697 | } |
671 | |
698 | |
672 | /** coroutine stack handling ************************************************/ |
699 | /** coroutine stack handling ************************************************/ |
673 | |
700 | |
674 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
701 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
675 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
702 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
703 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
|
|
704 | |
|
|
705 | /* apparently < 5.8.8 */ |
|
|
706 | #ifndef MgPV_nolen_const |
|
|
707 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
|
|
708 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
|
|
709 | (const char*)(mg)->mg_ptr) |
|
|
710 | #endif |
676 | |
711 | |
677 | /* |
712 | /* |
678 | * This overrides the default magic get method of %SIG elements. |
713 | * This overrides the default magic get method of %SIG elements. |
679 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
714 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
680 | * and instead of tryign to save and restore the hash elements, we just provide |
715 | * and instead of tryign to save and restore the hash elements, we just provide |
… | |
… | |
688 | { |
723 | { |
689 | const char *s = MgPV_nolen_const (mg); |
724 | const char *s = MgPV_nolen_const (mg); |
690 | |
725 | |
691 | if (*s == '_') |
726 | if (*s == '_') |
692 | { |
727 | { |
693 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
728 | SV **svp = 0; |
694 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
729 | |
|
|
730 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
731 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
732 | |
|
|
733 | if (svp) |
|
|
734 | { |
|
|
735 | sv_setsv (sv, *svp ? *svp : &PL_sv_undef); |
|
|
736 | return 0; |
|
|
737 | } |
695 | } |
738 | } |
696 | |
739 | |
697 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
740 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
|
|
741 | } |
|
|
742 | |
|
|
743 | static int |
|
|
744 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
|
|
745 | { |
|
|
746 | const char *s = MgPV_nolen_const (mg); |
|
|
747 | |
|
|
748 | if (*s == '_') |
|
|
749 | { |
|
|
750 | SV **svp = 0; |
|
|
751 | |
|
|
752 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
753 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
754 | |
|
|
755 | if (svp) |
|
|
756 | { |
|
|
757 | SV *old = *svp; |
|
|
758 | *svp = 0; |
|
|
759 | SvREFCNT_dec (old); |
|
|
760 | return 0; |
|
|
761 | } |
|
|
762 | } |
|
|
763 | |
|
|
764 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
698 | } |
765 | } |
699 | |
766 | |
700 | static int |
767 | static int |
701 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
768 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
702 | { |
769 | { |
… | |
… | |
712 | if (svp) |
779 | if (svp) |
713 | { |
780 | { |
714 | SV *old = *svp; |
781 | SV *old = *svp; |
715 | *svp = newSVsv (sv); |
782 | *svp = newSVsv (sv); |
716 | SvREFCNT_dec (old); |
783 | SvREFCNT_dec (old); |
717 | return; |
784 | return 0; |
718 | } |
785 | } |
719 | } |
786 | } |
720 | |
787 | |
721 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
788 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
722 | } |
789 | } |
… | |
… | |
749 | GvSV (PL_defgv) = newSV (0); |
816 | GvSV (PL_defgv) = newSV (0); |
750 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
817 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
751 | GvSV (PL_errgv) = newSV (0); |
818 | GvSV (PL_errgv) = newSV (0); |
752 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
819 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
753 | PL_rs = newSVsv (GvSV (irsgv)); |
820 | PL_rs = newSVsv (GvSV (irsgv)); |
754 | PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); |
821 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
755 | |
822 | |
756 | { |
823 | { |
757 | dSP; |
824 | dSP; |
758 | LOGOP myop; |
825 | LOGOP myop; |
759 | |
826 | |
… | |
… | |
776 | */ |
843 | */ |
777 | ENTER; |
844 | ENTER; |
778 | } |
845 | } |
779 | |
846 | |
780 | static void |
847 | static void |
781 | coro_destroy (pTHX_ struct coro *coro) |
848 | coro_destruct (pTHX_ struct coro *coro) |
782 | { |
849 | { |
783 | if (!IN_DESTRUCT) |
850 | if (!IN_DESTRUCT) |
784 | { |
851 | { |
785 | /* restore all saved variables and stuff */ |
852 | /* restore all saved variables and stuff */ |
786 | LEAVE_SCOPE (0); |
853 | LEAVE_SCOPE (0); |
… | |
… | |
808 | SvREFCNT_dec (PL_warnhook); |
875 | SvREFCNT_dec (PL_warnhook); |
809 | |
876 | |
810 | SvREFCNT_dec (coro->saved_deffh); |
877 | SvREFCNT_dec (coro->saved_deffh); |
811 | SvREFCNT_dec (coro->throw); |
878 | SvREFCNT_dec (coro->throw); |
812 | |
879 | |
813 | coro_destroy_stacks (aTHX); |
880 | coro_destruct_stacks (aTHX); |
814 | } |
881 | } |
815 | |
882 | |
816 | static void |
883 | static void |
817 | free_coro_mortal (pTHX) |
884 | free_coro_mortal (pTHX) |
818 | { |
885 | { |
… | |
… | |
855 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
922 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
856 | : bot; |
923 | : bot; |
857 | |
924 | |
858 | av_extend (av, top - bot); |
925 | av_extend (av, top - bot); |
859 | while (bot < top) |
926 | while (bot < top) |
860 | av_push (av, SvREFCNT_inc (*bot++)); |
927 | av_push (av, SvREFCNT_inc_NN (*bot++)); |
861 | |
928 | |
862 | PL_runops = RUNOPS_DEFAULT; |
929 | PL_runops = RUNOPS_DEFAULT; |
863 | ENTER; |
930 | ENTER; |
864 | SAVETMPS; |
931 | SAVETMPS; |
865 | EXTEND (SP, 3); |
932 | EXTEND (SP, 3); |
… | |
… | |
1013 | coro_cctx *cctx; |
1080 | coro_cctx *cctx; |
1014 | void *stack_start; |
1081 | void *stack_start; |
1015 | size_t stack_size; |
1082 | size_t stack_size; |
1016 | |
1083 | |
1017 | ++cctx_count; |
1084 | ++cctx_count; |
1018 | |
|
|
1019 | Newz (0, cctx, 1, coro_cctx); |
1085 | Newz (0, cctx, 1, coro_cctx); |
1020 | |
1086 | |
1021 | #if HAVE_MMAP |
1087 | #if HAVE_MMAP |
1022 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1088 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1023 | /* mmap supposedly does allocate-on-write for us */ |
1089 | /* mmap supposedly does allocate-on-write for us */ |
… | |
… | |
1059 | { |
1125 | { |
1060 | if (!cctx) |
1126 | if (!cctx) |
1061 | return; |
1127 | return; |
1062 | |
1128 | |
1063 | --cctx_count; |
1129 | --cctx_count; |
|
|
1130 | coro_destroy (&cctx->cctx); |
1064 | |
1131 | |
|
|
1132 | /* coro_transfer creates new, empty cctx's */ |
|
|
1133 | if (cctx->sptr) |
|
|
1134 | { |
1065 | #if CORO_USE_VALGRIND |
1135 | #if CORO_USE_VALGRIND |
1066 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1136 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
1067 | #endif |
1137 | #endif |
1068 | |
1138 | |
1069 | #if HAVE_MMAP |
1139 | #if HAVE_MMAP |
1070 | if (cctx->flags & CC_MAPPED) |
1140 | if (cctx->flags & CC_MAPPED) |
1071 | munmap (cctx->sptr, cctx->ssize); |
1141 | munmap (cctx->sptr, cctx->ssize); |
1072 | else |
1142 | else |
1073 | #endif |
1143 | #endif |
1074 | Safefree (cctx->sptr); |
1144 | Safefree (cctx->sptr); |
|
|
1145 | } |
1075 | |
1146 | |
1076 | Safefree (cctx); |
1147 | Safefree (cctx); |
1077 | } |
1148 | } |
1078 | |
1149 | |
1079 | /* wether this cctx should be destructed */ |
1150 | /* wether this cctx should be destructed */ |
… | |
… | |
1098 | } |
1169 | } |
1099 | |
1170 | |
1100 | static void |
1171 | static void |
1101 | cctx_put (coro_cctx *cctx) |
1172 | cctx_put (coro_cctx *cctx) |
1102 | { |
1173 | { |
|
|
1174 | assert (("cctx_put called on non-initialised cctx", cctx->sptr)); |
|
|
1175 | |
1103 | /* free another cctx if overlimit */ |
1176 | /* free another cctx if overlimit */ |
1104 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1177 | if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) |
1105 | { |
1178 | { |
1106 | coro_cctx *first = cctx_first; |
1179 | coro_cctx *first = cctx_first; |
1107 | cctx_first = first->next; |
1180 | cctx_first = first->next; |
… | |
… | |
1130 | |
1203 | |
1131 | if (expect_false (next->flags & CF_DESTROYED)) |
1204 | if (expect_false (next->flags & CF_DESTROYED)) |
1132 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1205 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1133 | |
1206 | |
1134 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1207 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1135 | if (expect_false (PL_lex_state != LEX_NOTPARSING) |
1208 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1136 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version"); |
1209 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version"); |
1137 | #endif |
1210 | #endif |
1138 | } |
1211 | } |
1139 | } |
1212 | } |
1140 | |
1213 | |
1141 | /* always use the TRANSFER macro */ |
1214 | /* always use the TRANSFER macro */ |
1142 | static void NOINLINE |
1215 | static void NOINLINE |
1143 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1216 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1144 | { |
1217 | { |
1145 | dSTACKLEVEL; |
1218 | dSTACKLEVEL; |
1146 | static volatile int has_throw; |
|
|
1147 | |
1219 | |
1148 | /* sometimes transfer is only called to set idle_sp */ |
1220 | /* sometimes transfer is only called to set idle_sp */ |
1149 | if (expect_false (!next)) |
1221 | if (expect_false (!next)) |
1150 | { |
1222 | { |
1151 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1223 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1152 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1224 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1153 | } |
1225 | } |
1154 | else if (expect_true (prev != next)) |
1226 | else if (expect_true (prev != next)) |
1155 | { |
1227 | { |
|
|
1228 | static volatile int has_throw; |
1156 | coro_cctx *prev__cctx; |
1229 | coro_cctx *prev__cctx; |
1157 | |
1230 | |
1158 | if (expect_false (prev->flags & CF_NEW)) |
1231 | if (expect_false (prev->flags & CF_NEW)) |
1159 | { |
1232 | { |
1160 | /* create a new empty context */ |
1233 | /* create a new empty/source context */ |
|
|
1234 | ++cctx_count; |
1161 | Newz (0, prev->cctx, 1, coro_cctx); |
1235 | New (0, prev->cctx, 1, coro_cctx); |
|
|
1236 | prev->cctx->sptr = 0; |
|
|
1237 | coro_create (&prev->cctx->cctx, 0, 0, 0, 0); |
|
|
1238 | |
1162 | prev->flags &= ~CF_NEW; |
1239 | prev->flags &= ~CF_NEW; |
1163 | prev->flags |= CF_RUNNING; |
1240 | prev->flags |= CF_RUNNING; |
1164 | } |
1241 | } |
1165 | |
1242 | |
1166 | prev->flags &= ~CF_RUNNING; |
1243 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1273 | croak ("FATAL: tried to destroy currently running coroutine"); |
1350 | croak ("FATAL: tried to destroy currently running coroutine"); |
1274 | |
1351 | |
1275 | save_perl (aTHX_ &temp); |
1352 | save_perl (aTHX_ &temp); |
1276 | load_perl (aTHX_ coro); |
1353 | load_perl (aTHX_ coro); |
1277 | |
1354 | |
1278 | coro_destroy (aTHX_ coro); |
1355 | coro_destruct (aTHX_ coro); |
1279 | |
1356 | |
1280 | load_perl (aTHX_ &temp); |
1357 | load_perl (aTHX_ &temp); |
1281 | |
1358 | |
1282 | coro->slot = 0; |
1359 | coro->slot = 0; |
1283 | } |
1360 | } |
… | |
… | |
1371 | static int |
1448 | static int |
1372 | api_ready (SV *coro_sv) |
1449 | api_ready (SV *coro_sv) |
1373 | { |
1450 | { |
1374 | dTHX; |
1451 | dTHX; |
1375 | struct coro *coro; |
1452 | struct coro *coro; |
|
|
1453 | SV *sv_hook; |
|
|
1454 | void (*xs_hook)(void); |
1376 | |
1455 | |
1377 | if (SvROK (coro_sv)) |
1456 | if (SvROK (coro_sv)) |
1378 | coro_sv = SvRV (coro_sv); |
1457 | coro_sv = SvRV (coro_sv); |
1379 | |
1458 | |
1380 | coro = SvSTATE (coro_sv); |
1459 | coro = SvSTATE (coro_sv); |
… | |
… | |
1383 | return 0; |
1462 | return 0; |
1384 | |
1463 | |
1385 | coro->flags |= CF_READY; |
1464 | coro->flags |= CF_READY; |
1386 | |
1465 | |
1387 | LOCK; |
1466 | LOCK; |
|
|
1467 | |
|
|
1468 | sv_hook = coro_nready ? 0 : coro_readyhook; |
|
|
1469 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
|
|
1470 | |
1388 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1471 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1389 | ++coro_nready; |
1472 | ++coro_nready; |
|
|
1473 | |
1390 | UNLOCK; |
1474 | UNLOCK; |
|
|
1475 | |
|
|
1476 | if (sv_hook) |
|
|
1477 | { |
|
|
1478 | dSP; |
|
|
1479 | |
|
|
1480 | ENTER; |
|
|
1481 | SAVETMPS; |
|
|
1482 | |
|
|
1483 | PUSHMARK (SP); |
|
|
1484 | PUTBACK; |
|
|
1485 | call_sv (sv_hook, G_DISCARD); |
|
|
1486 | SPAGAIN; |
|
|
1487 | |
|
|
1488 | FREETMPS; |
|
|
1489 | LEAVE; |
|
|
1490 | } |
|
|
1491 | |
|
|
1492 | if (xs_hook) |
|
|
1493 | xs_hook (); |
1391 | |
1494 | |
1392 | return 1; |
1495 | return 1; |
1393 | } |
1496 | } |
1394 | |
1497 | |
1395 | static int |
1498 | static int |
… | |
… | |
1545 | else |
1648 | else |
1546 | coro->slot->runops = RUNOPS_DEFAULT; |
1649 | coro->slot->runops = RUNOPS_DEFAULT; |
1547 | } |
1650 | } |
1548 | } |
1651 | } |
1549 | |
1652 | |
|
|
1653 | static int |
|
|
1654 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
1655 | { |
|
|
1656 | AV *padlist; |
|
|
1657 | AV *av = (AV *)mg->mg_obj; |
|
|
1658 | |
|
|
1659 | abort (); |
|
|
1660 | |
|
|
1661 | return 0; |
|
|
1662 | } |
|
|
1663 | |
|
|
1664 | static MGVTBL coro_gensub_vtbl = { |
|
|
1665 | 0, 0, 0, 0, |
|
|
1666 | coro_gensub_free |
|
|
1667 | }; |
|
|
1668 | |
|
|
1669 | /*****************************************************************************/ |
|
|
1670 | /* PerlIO::cede */ |
|
|
1671 | |
|
|
1672 | typedef struct |
|
|
1673 | { |
|
|
1674 | PerlIOBuf base; |
|
|
1675 | NV next, every; |
|
|
1676 | } PerlIOCede; |
|
|
1677 | |
|
|
1678 | static IV |
|
|
1679 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
|
|
1680 | { |
|
|
1681 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1682 | |
|
|
1683 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
|
|
1684 | self->next = nvtime () + self->every; |
|
|
1685 | |
|
|
1686 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
|
|
1687 | } |
|
|
1688 | |
|
|
1689 | static SV * |
|
|
1690 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
|
|
1691 | { |
|
|
1692 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1693 | |
|
|
1694 | return newSVnv (self->every); |
|
|
1695 | } |
|
|
1696 | |
|
|
1697 | static IV |
|
|
1698 | PerlIOCede_flush (pTHX_ PerlIO *f) |
|
|
1699 | { |
|
|
1700 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1701 | double now = nvtime (); |
|
|
1702 | |
|
|
1703 | if (now >= self->next) |
|
|
1704 | { |
|
|
1705 | api_cede (); |
|
|
1706 | self->next = now + self->every; |
|
|
1707 | } |
|
|
1708 | |
|
|
1709 | return PerlIOBuf_flush (aTHX_ f); |
|
|
1710 | } |
|
|
1711 | |
|
|
1712 | static PerlIO_funcs PerlIO_cede = |
|
|
1713 | { |
|
|
1714 | sizeof(PerlIO_funcs), |
|
|
1715 | "cede", |
|
|
1716 | sizeof(PerlIOCede), |
|
|
1717 | PERLIO_K_DESTRUCT | PERLIO_K_RAW, |
|
|
1718 | PerlIOCede_pushed, |
|
|
1719 | PerlIOBuf_popped, |
|
|
1720 | PerlIOBuf_open, |
|
|
1721 | PerlIOBase_binmode, |
|
|
1722 | PerlIOCede_getarg, |
|
|
1723 | PerlIOBase_fileno, |
|
|
1724 | PerlIOBuf_dup, |
|
|
1725 | PerlIOBuf_read, |
|
|
1726 | PerlIOBuf_unread, |
|
|
1727 | PerlIOBuf_write, |
|
|
1728 | PerlIOBuf_seek, |
|
|
1729 | PerlIOBuf_tell, |
|
|
1730 | PerlIOBuf_close, |
|
|
1731 | PerlIOCede_flush, |
|
|
1732 | PerlIOBuf_fill, |
|
|
1733 | PerlIOBase_eof, |
|
|
1734 | PerlIOBase_error, |
|
|
1735 | PerlIOBase_clearerr, |
|
|
1736 | PerlIOBase_setlinebuf, |
|
|
1737 | PerlIOBuf_get_base, |
|
|
1738 | PerlIOBuf_bufsiz, |
|
|
1739 | PerlIOBuf_get_ptr, |
|
|
1740 | PerlIOBuf_get_cnt, |
|
|
1741 | PerlIOBuf_set_ptrcnt, |
|
|
1742 | }; |
|
|
1743 | |
|
|
1744 | |
1550 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1745 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1551 | |
1746 | |
1552 | PROTOTYPES: DISABLE |
1747 | PROTOTYPES: DISABLE |
1553 | |
1748 | |
1554 | BOOT: |
1749 | BOOT: |
1555 | { |
1750 | { |
1556 | #ifdef USE_ITHREADS |
1751 | #ifdef USE_ITHREADS |
1557 | MUTEX_INIT (&coro_mutex); |
1752 | MUTEX_INIT (&coro_lock); |
1558 | #endif |
1753 | #endif |
1559 | BOOT_PAGESIZE; |
1754 | BOOT_PAGESIZE; |
1560 | |
1755 | |
1561 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1756 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1562 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1757 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1563 | |
1758 | |
1564 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
1759 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
1565 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
1760 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
1566 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; |
1761 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
1567 | PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
|
|
1568 | |
1762 | |
1569 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1763 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1570 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
1764 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
1571 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1765 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1572 | |
1766 | |
… | |
… | |
1581 | main_top_env = PL_top_env; |
1775 | main_top_env = PL_top_env; |
1582 | |
1776 | |
1583 | while (main_top_env->je_prev) |
1777 | while (main_top_env->je_prev) |
1584 | main_top_env = main_top_env->je_prev; |
1778 | main_top_env = main_top_env->je_prev; |
1585 | |
1779 | |
1586 | coroapi.ver = CORO_API_VERSION; |
1780 | coroapi.ver = CORO_API_VERSION; |
1587 | coroapi.rev = CORO_API_REVISION; |
1781 | coroapi.rev = CORO_API_REVISION; |
1588 | coroapi.transfer = api_transfer; |
1782 | coroapi.transfer = api_transfer; |
|
|
1783 | |
|
|
1784 | { |
|
|
1785 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
1786 | |
|
|
1787 | if (!svp) croak ("Time::HiRes is required"); |
|
|
1788 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
1789 | |
|
|
1790 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
1791 | } |
1589 | |
1792 | |
1590 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1793 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1591 | } |
1794 | } |
1592 | |
1795 | |
1593 | SV * |
1796 | SV * |
… | |
… | |
1641 | ta.next = 0; |
1844 | ta.next = 0; |
1642 | break; |
1845 | break; |
1643 | |
1846 | |
1644 | case 1: |
1847 | case 1: |
1645 | if (items != 2) |
1848 | if (items != 2) |
1646 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1849 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); |
1647 | |
1850 | |
1648 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1851 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1649 | break; |
1852 | break; |
1650 | |
1853 | |
1651 | case 2: |
1854 | case 2: |
… | |
… | |
1677 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1880 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1678 | OUTPUT: |
1881 | OUTPUT: |
1679 | RETVAL |
1882 | RETVAL |
1680 | |
1883 | |
1681 | void |
1884 | void |
1682 | _exit (code) |
1885 | _exit (int code) |
1683 | int code |
|
|
1684 | PROTOTYPE: $ |
1886 | PROTOTYPE: $ |
1685 | CODE: |
1887 | CODE: |
1686 | _exit (code); |
1888 | _exit (code); |
1687 | |
1889 | |
1688 | int |
1890 | int |
… | |
… | |
1722 | call (Coro::State coro, SV *coderef) |
1924 | call (Coro::State coro, SV *coderef) |
1723 | ALIAS: |
1925 | ALIAS: |
1724 | eval = 1 |
1926 | eval = 1 |
1725 | CODE: |
1927 | CODE: |
1726 | { |
1928 | { |
1727 | if (coro->mainstack) |
1929 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
1728 | { |
1930 | { |
1729 | struct coro temp; |
1931 | struct coro temp; |
1730 | |
1932 | |
1731 | if (!(coro->flags & CF_RUNNING)) |
1933 | if (!(coro->flags & CF_RUNNING)) |
1732 | { |
1934 | { |
… | |
… | |
1779 | |
1981 | |
1780 | void |
1982 | void |
1781 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1983 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1782 | |
1984 | |
1783 | SV * |
1985 | SV * |
1784 | has_stack (Coro::State coro) |
1986 | has_cctx (Coro::State coro) |
1785 | PROTOTYPE: $ |
1987 | PROTOTYPE: $ |
1786 | CODE: |
1988 | CODE: |
1787 | RETVAL = boolSV (!!coro->cctx); |
1989 | RETVAL = boolSV (!!coro->cctx); |
1788 | OUTPUT: |
1990 | OUTPUT: |
1789 | RETVAL |
1991 | RETVAL |
… | |
… | |
1814 | force_cctx () |
2016 | force_cctx () |
1815 | CODE: |
2017 | CODE: |
1816 | struct coro *coro = SvSTATE (coro_current); |
2018 | struct coro *coro = SvSTATE (coro_current); |
1817 | coro->cctx->idle_sp = 0; |
2019 | coro->cctx->idle_sp = 0; |
1818 | |
2020 | |
1819 | MODULE = Coro::State PACKAGE = Coro |
|
|
1820 | |
|
|
1821 | BOOT: |
|
|
1822 | { |
|
|
1823 | int i; |
|
|
1824 | |
|
|
1825 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
|
|
1826 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
|
|
1827 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
1828 | |
|
|
1829 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
|
|
1830 | SvREADONLY_on (coro_current); |
|
|
1831 | |
|
|
1832 | coro_stash = gv_stashpv ("Coro", TRUE); |
|
|
1833 | |
|
|
1834 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
|
|
1835 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
|
|
1836 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
|
|
1837 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
|
|
1838 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
|
|
1839 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
|
|
1840 | |
|
|
1841 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
1842 | coro_ready[i] = newAV (); |
|
|
1843 | |
|
|
1844 | { |
|
|
1845 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
|
|
1846 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
|
|
1847 | |
|
|
1848 | coroapi.schedule = api_schedule; |
|
|
1849 | coroapi.cede = api_cede; |
|
|
1850 | coroapi.cede_notself = api_cede_notself; |
|
|
1851 | coroapi.ready = api_ready; |
|
|
1852 | coroapi.is_ready = api_is_ready; |
|
|
1853 | coroapi.nready = &coro_nready; |
|
|
1854 | coroapi.current = coro_current; |
|
|
1855 | |
|
|
1856 | GCoroAPI = &coroapi; |
|
|
1857 | sv_setiv (sv, (IV)&coroapi); |
|
|
1858 | SvREADONLY_on (sv); |
|
|
1859 | } |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | void |
|
|
1863 | _set_current (SV *current) |
|
|
1864 | PROTOTYPE: $ |
|
|
1865 | CODE: |
|
|
1866 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1867 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
|
|
1868 | |
|
|
1869 | int |
|
|
1870 | prio (Coro::State coro, int newprio = 0) |
|
|
1871 | ALIAS: |
|
|
1872 | nice = 1 |
|
|
1873 | CODE: |
|
|
1874 | { |
|
|
1875 | RETVAL = coro->prio; |
|
|
1876 | |
|
|
1877 | if (items > 1) |
|
|
1878 | { |
|
|
1879 | if (ix) |
|
|
1880 | newprio = coro->prio - newprio; |
|
|
1881 | |
|
|
1882 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
1883 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
1884 | |
|
|
1885 | coro->prio = newprio; |
|
|
1886 | } |
|
|
1887 | } |
|
|
1888 | OUTPUT: |
|
|
1889 | RETVAL |
|
|
1890 | |
|
|
1891 | SV * |
|
|
1892 | ready (SV *self) |
|
|
1893 | PROTOTYPE: $ |
|
|
1894 | CODE: |
|
|
1895 | RETVAL = boolSV (api_ready (self)); |
|
|
1896 | OUTPUT: |
|
|
1897 | RETVAL |
|
|
1898 | |
|
|
1899 | int |
|
|
1900 | nready (...) |
|
|
1901 | PROTOTYPE: |
|
|
1902 | CODE: |
|
|
1903 | RETVAL = coro_nready; |
|
|
1904 | OUTPUT: |
|
|
1905 | RETVAL |
|
|
1906 | |
|
|
1907 | void |
|
|
1908 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
1909 | PROTOTYPE: $;$ |
|
|
1910 | CODE: |
|
|
1911 | SvREFCNT_dec (self->throw); |
|
|
1912 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
1913 | |
|
|
1914 | void |
2021 | void |
1915 | swap_defsv (Coro::State self) |
2022 | swap_defsv (Coro::State self) |
1916 | PROTOTYPE: $ |
2023 | PROTOTYPE: $ |
1917 | ALIAS: |
2024 | ALIAS: |
1918 | swap_defav = 1 |
2025 | swap_defav = 1 |
… | |
… | |
1925 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
2032 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
1926 | |
2033 | |
1927 | SV *tmp = *src; *src = *dst; *dst = tmp; |
2034 | SV *tmp = *src; *src = *dst; *dst = tmp; |
1928 | } |
2035 | } |
1929 | |
2036 | |
|
|
2037 | MODULE = Coro::State PACKAGE = Coro |
|
|
2038 | |
|
|
2039 | BOOT: |
|
|
2040 | { |
|
|
2041 | int i; |
|
|
2042 | |
|
|
2043 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2044 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
|
|
2045 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
|
|
2046 | |
|
|
2047 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
|
|
2048 | SvREADONLY_on (coro_current); |
|
|
2049 | |
|
|
2050 | coro_stash = gv_stashpv ("Coro", TRUE); |
|
|
2051 | |
|
|
2052 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
|
|
2053 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
|
|
2054 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
|
|
2055 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
|
|
2056 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
|
|
2057 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
|
|
2058 | |
|
|
2059 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
2060 | coro_ready[i] = newAV (); |
|
|
2061 | |
|
|
2062 | { |
|
|
2063 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
|
|
2064 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
|
|
2065 | |
|
|
2066 | coroapi.schedule = api_schedule; |
|
|
2067 | coroapi.cede = api_cede; |
|
|
2068 | coroapi.cede_notself = api_cede_notself; |
|
|
2069 | coroapi.ready = api_ready; |
|
|
2070 | coroapi.is_ready = api_is_ready; |
|
|
2071 | coroapi.nready = &coro_nready; |
|
|
2072 | coroapi.current = coro_current; |
|
|
2073 | |
|
|
2074 | GCoroAPI = &coroapi; |
|
|
2075 | sv_setiv (sv, (IV)&coroapi); |
|
|
2076 | SvREADONLY_on (sv); |
|
|
2077 | } |
|
|
2078 | } |
|
|
2079 | |
|
|
2080 | void |
|
|
2081 | _set_current (SV *current) |
|
|
2082 | PROTOTYPE: $ |
|
|
2083 | CODE: |
|
|
2084 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
2085 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
|
|
2086 | |
|
|
2087 | void |
|
|
2088 | _set_readyhook (SV *hook) |
|
|
2089 | PROTOTYPE: $ |
|
|
2090 | CODE: |
|
|
2091 | LOCK; |
|
|
2092 | SvREFCNT_dec (coro_readyhook); |
|
|
2093 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
|
|
2094 | UNLOCK; |
|
|
2095 | |
|
|
2096 | int |
|
|
2097 | prio (Coro::State coro, int newprio = 0) |
|
|
2098 | ALIAS: |
|
|
2099 | nice = 1 |
|
|
2100 | CODE: |
|
|
2101 | { |
|
|
2102 | RETVAL = coro->prio; |
|
|
2103 | |
|
|
2104 | if (items > 1) |
|
|
2105 | { |
|
|
2106 | if (ix) |
|
|
2107 | newprio = coro->prio - newprio; |
|
|
2108 | |
|
|
2109 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
2110 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
2111 | |
|
|
2112 | coro->prio = newprio; |
|
|
2113 | } |
|
|
2114 | } |
|
|
2115 | OUTPUT: |
|
|
2116 | RETVAL |
|
|
2117 | |
|
|
2118 | SV * |
|
|
2119 | ready (SV *self) |
|
|
2120 | PROTOTYPE: $ |
|
|
2121 | CODE: |
|
|
2122 | RETVAL = boolSV (api_ready (self)); |
|
|
2123 | OUTPUT: |
|
|
2124 | RETVAL |
|
|
2125 | |
|
|
2126 | int |
|
|
2127 | nready (...) |
|
|
2128 | PROTOTYPE: |
|
|
2129 | CODE: |
|
|
2130 | RETVAL = coro_nready; |
|
|
2131 | OUTPUT: |
|
|
2132 | RETVAL |
|
|
2133 | |
|
|
2134 | void |
|
|
2135 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2136 | PROTOTYPE: $;$ |
|
|
2137 | CODE: |
|
|
2138 | SvREFCNT_dec (self->throw); |
|
|
2139 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2140 | |
1930 | # for async_pool speedup |
2141 | # for async_pool speedup |
1931 | void |
2142 | void |
1932 | _pool_1 (SV *cb) |
2143 | _pool_1 (SV *cb) |
1933 | CODE: |
2144 | CODE: |
1934 | { |
2145 | { |
… | |
… | |
1939 | AV *invoke_av; |
2150 | AV *invoke_av; |
1940 | int i, len; |
2151 | int i, len; |
1941 | |
2152 | |
1942 | if (!invoke) |
2153 | if (!invoke) |
1943 | { |
2154 | { |
1944 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
2155 | SV *old = PL_diehook; |
|
|
2156 | PL_diehook = 0; |
|
|
2157 | SvREFCNT_dec (old); |
1945 | croak ("\3async_pool terminate\2\n"); |
2158 | croak ("\3async_pool terminate\2\n"); |
1946 | } |
2159 | } |
1947 | |
2160 | |
1948 | SvREFCNT_dec (coro->saved_deffh); |
2161 | SvREFCNT_dec (coro->saved_deffh); |
1949 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
2162 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
1950 | |
2163 | |
1951 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2164 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1952 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2165 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1953 | |
2166 | |
1954 | invoke_av = (AV *)SvRV (invoke); |
2167 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
1958 | |
2171 | |
1959 | if (len > 0) |
2172 | if (len > 0) |
1960 | { |
2173 | { |
1961 | av_fill (defav, len - 1); |
2174 | av_fill (defav, len - 1); |
1962 | for (i = 0; i < len; ++i) |
2175 | for (i = 0; i < len; ++i) |
1963 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
2176 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
1964 | } |
2177 | } |
1965 | |
2178 | |
1966 | SvREFCNT_dec (invoke); |
2179 | SvREFCNT_dec (invoke); |
1967 | } |
2180 | } |
1968 | |
2181 | |
… | |
… | |
1978 | coro->saved_deffh = 0; |
2191 | coro->saved_deffh = 0; |
1979 | |
2192 | |
1980 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2193 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1981 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2194 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1982 | { |
2195 | { |
1983 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
2196 | SV *old = PL_diehook; |
|
|
2197 | PL_diehook = 0; |
|
|
2198 | SvREFCNT_dec (old); |
1984 | croak ("\3async_pool terminate\2\n"); |
2199 | croak ("\3async_pool terminate\2\n"); |
1985 | } |
2200 | } |
1986 | |
2201 | |
1987 | av_clear (GvAV (PL_defgv)); |
2202 | av_clear (GvAV (PL_defgv)); |
1988 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
2203 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
… | |
… | |
1994 | api_trace (coro_current, 0); |
2209 | api_trace (coro_current, 0); |
1995 | |
2210 | |
1996 | av_push (av_async_pool, newSVsv (coro_current)); |
2211 | av_push (av_async_pool, newSVsv (coro_current)); |
1997 | } |
2212 | } |
1998 | |
2213 | |
|
|
2214 | #if 0 |
|
|
2215 | |
|
|
2216 | void |
|
|
2217 | _generator_call (...) |
|
|
2218 | PROTOTYPE: @ |
|
|
2219 | PPCODE: |
|
|
2220 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2221 | xxxx |
|
|
2222 | abort (); |
|
|
2223 | |
|
|
2224 | SV * |
|
|
2225 | gensub (SV *sub, ...) |
|
|
2226 | PROTOTYPE: &;@ |
|
|
2227 | CODE: |
|
|
2228 | { |
|
|
2229 | struct coro *coro; |
|
|
2230 | MAGIC *mg; |
|
|
2231 | CV *xcv; |
|
|
2232 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2233 | int i; |
|
|
2234 | |
|
|
2235 | CvGV (ncv) = CvGV (cv); |
|
|
2236 | CvFILE (ncv) = CvFILE (cv); |
|
|
2237 | |
|
|
2238 | Newz (0, coro, 1, struct coro); |
|
|
2239 | coro->args = newAV (); |
|
|
2240 | coro->flags = CF_NEW; |
|
|
2241 | |
|
|
2242 | av_extend (coro->args, items - 1); |
|
|
2243 | for (i = 1; i < items; i++) |
|
|
2244 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2245 | |
|
|
2246 | CvISXSUB_on (ncv); |
|
|
2247 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2248 | |
|
|
2249 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2250 | |
|
|
2251 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2252 | CvANON_on (ncv); |
|
|
2253 | |
|
|
2254 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2255 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2256 | } |
|
|
2257 | OUTPUT: |
|
|
2258 | RETVAL |
|
|
2259 | |
|
|
2260 | #endif |
|
|
2261 | |
1999 | |
2262 | |
2000 | MODULE = Coro::State PACKAGE = Coro::AIO |
2263 | MODULE = Coro::State PACKAGE = Coro::AIO |
2001 | |
2264 | |
2002 | SV * |
2265 | void |
2003 | _get_state () |
2266 | _get_state (SV *self) |
2004 | CODE: |
2267 | PPCODE: |
2005 | { |
2268 | { |
2006 | struct io_state *data; |
2269 | AV *defav = GvAV (PL_defgv); |
2007 | |
2270 | AV *av = newAV (); |
|
|
2271 | int i; |
2008 | RETVAL = newSV (sizeof (struct io_state)); |
2272 | SV *data_sv = newSV (sizeof (struct io_state)); |
2009 | data = (struct io_state *)SvPVX (RETVAL); |
2273 | struct io_state *data = (struct io_state *)SvPVX (data_sv); |
2010 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
2274 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2011 | SvPOK_only (RETVAL); |
2275 | SvPOK_only (data_sv); |
2012 | |
2276 | |
2013 | data->errorno = errno; |
2277 | data->errorno = errno; |
2014 | data->laststype = PL_laststype; |
2278 | data->laststype = PL_laststype; |
2015 | data->laststatval = PL_laststatval; |
2279 | data->laststatval = PL_laststatval; |
2016 | data->statcache = PL_statcache; |
2280 | data->statcache = PL_statcache; |
|
|
2281 | |
|
|
2282 | av_extend (av, AvFILLp (defav) + 1 + 1); |
|
|
2283 | |
|
|
2284 | for (i = 0; i <= AvFILLp (defav); ++i) |
|
|
2285 | av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i])); |
|
|
2286 | |
|
|
2287 | av_push (av, data_sv); |
|
|
2288 | |
|
|
2289 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
2290 | |
|
|
2291 | api_ready (self); |
2017 | } |
2292 | } |
2018 | OUTPUT: |
|
|
2019 | RETVAL |
|
|
2020 | |
2293 | |
2021 | void |
2294 | void |
2022 | _set_state (char *data_) |
2295 | _set_state (SV *state) |
2023 | PROTOTYPE: $ |
2296 | PROTOTYPE: $ |
2024 | CODE: |
2297 | PPCODE: |
2025 | { |
2298 | { |
2026 | struct io_state *data = (void *)data_; |
2299 | AV *av = (AV *)SvRV (state); |
|
|
2300 | struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]); |
|
|
2301 | int i; |
2027 | |
2302 | |
2028 | errno = data->errorno; |
2303 | errno = data->errorno; |
2029 | PL_laststype = data->laststype; |
2304 | PL_laststype = data->laststype; |
2030 | PL_laststatval = data->laststatval; |
2305 | PL_laststatval = data->laststatval; |
2031 | PL_statcache = data->statcache; |
2306 | PL_statcache = data->statcache; |
2032 | } |
|
|
2033 | |
2307 | |
|
|
2308 | EXTEND (SP, AvFILLp (av)); |
|
|
2309 | for (i = 0; i < AvFILLp (av); ++i) |
|
|
2310 | PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i]))); |
|
|
2311 | } |
|
|
2312 | |
|
|
2313 | |
|
|
2314 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
|
|
2315 | |
|
|
2316 | BOOT: |
|
|
2317 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
|
|
2318 | |
|
|
2319 | SV * |
|
|
2320 | _schedule (...) |
|
|
2321 | PROTOTYPE: @ |
|
|
2322 | CODE: |
|
|
2323 | { |
|
|
2324 | static int incede; |
|
|
2325 | |
|
|
2326 | api_cede_notself (); |
|
|
2327 | |
|
|
2328 | ++incede; |
|
|
2329 | while (coro_nready >= incede && api_cede ()) |
|
|
2330 | ; |
|
|
2331 | |
|
|
2332 | sv_setsv (sv_activity, &PL_sv_undef); |
|
|
2333 | if (coro_nready >= incede) |
|
|
2334 | { |
|
|
2335 | PUSHMARK (SP); |
|
|
2336 | PUTBACK; |
|
|
2337 | call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); |
|
|
2338 | SPAGAIN; |
|
|
2339 | } |
|
|
2340 | |
|
|
2341 | --incede; |
|
|
2342 | } |
|
|
2343 | |
|
|
2344 | |
|
|
2345 | MODULE = Coro::State PACKAGE = PerlIO::cede |
|
|
2346 | |
|
|
2347 | BOOT: |
|
|
2348 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |