ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.221 by root, Sun Jan 13 09:47:22 2008 UTC vs.
Revision 1.250 by root, Wed Oct 22 16:34:07 2008 UTC

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.8.8 */
87#ifndef GV_NOTQUAL
88# define GV_NOTQUAL 0
89#endif
90#ifndef newSV
91# define newSV(l) NEWSV(0,l)
92#endif
93#ifndef SvREFCNT_inc_NN
94# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
95#endif
96
97/* 5.11 */
98#ifndef CxHASARGS
99# define CxHASARGS(cx) (cx)->blk_sub.hasargs
100#endif
101
77/* 5.8.7 */ 102/* 5.8.7 */
78#ifndef SvRV_set 103#ifndef SvRV_set
79# define SvRV_set(s,v) SvRV(s) = (v) 104# define SvRV_set(s,v) SvRV(s) = (v)
80#endif
81
82/* 5.8.8 */
83#ifndef GV_NOTQUAL
84# define GV_NOTQUAL 0
85#endif
86#ifndef newSV
87# define newSV(l) NEWSV(0,l)
88#endif 105#endif
89 106
90#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 107#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
91# undef CORO_STACKGUARD 108# undef CORO_STACKGUARD
92#endif 109#endif
134#endif 151#endif
135 152
136/* helper storage struct for Coro::AIO */ 153/* helper storage struct for Coro::AIO */
137struct io_state 154struct io_state
138{ 155{
156 AV *res;
139 int errorno; 157 int errorno;
140 I32 laststype; 158 I32 laststype;
141 int laststatval; 159 int laststatval;
142 Stat_t statcache; 160 Stat_t statcache;
143}; 161};
162
163static double (*nvtime)(); /* so why doesn't it take void? */
144 164
145static size_t coro_stacksize = CORO_STACKSIZE; 165static size_t coro_stacksize = CORO_STACKSIZE;
146static struct CoroAPI coroapi; 166static struct CoroAPI coroapi;
147static AV *main_mainstack; /* used to differentiate between $main and others */ 167static AV *main_mainstack; /* used to differentiate between $main and others */
148static JMPENV *main_top_env; 168static JMPENV *main_top_env;
157 177
158/* async_pool helper stuff */ 178/* async_pool helper stuff */
159static SV *sv_pool_rss; 179static SV *sv_pool_rss;
160static SV *sv_pool_size; 180static SV *sv_pool_size;
161static AV *av_async_pool; 181static AV *av_async_pool;
182
183/* Coro::AnyEvent */
184static SV *sv_activity;
162 185
163static struct coro_cctx *cctx_first; 186static struct coro_cctx *cctx_first;
164static int cctx_count, cctx_idle; 187static int cctx_count, cctx_idle;
165 188
166enum { 189enum {
252#define PRIO_IDLE -3 275#define PRIO_IDLE -3
253#define PRIO_MIN -4 276#define PRIO_MIN -4
254 277
255/* for Coro.pm */ 278/* for Coro.pm */
256static SV *coro_current; 279static SV *coro_current;
280static SV *coro_readyhook;
257static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 281static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
258static int coro_nready; 282static int coro_nready;
259static struct coro *coro_first; 283static struct coro *coro_first;
260 284
261/** lowlevel stuff **********************************************************/ 285/** lowlevel stuff **********************************************************/
262 286
263static SV * 287static SV *
264coro_get_sv (pTHX_ const char *name, int create) 288coro_get_sv (pTHX_ const char *name, int create)
265{ 289{
266#if PERL_VERSION_ATLEAST (5,9,0) 290#if PERL_VERSION_ATLEAST (5,10,0)
267 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 291 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
268 get_sv (name, create); 292 get_sv (name, create);
269#endif 293#endif
270 return get_sv (name, create); 294 return get_sv (name, create);
271} 295}
272 296
273static AV * 297static AV *
274coro_get_av (pTHX_ const char *name, int create) 298coro_get_av (pTHX_ const char *name, int create)
275{ 299{
276#if PERL_VERSION_ATLEAST (5,9,0) 300#if PERL_VERSION_ATLEAST (5,10,0)
277 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 301 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
278 get_av (name, create); 302 get_av (name, create);
279#endif 303#endif
280 return get_av (name, create); 304 return get_av (name, create);
281} 305}
282 306
283static HV * 307static HV *
284coro_get_hv (pTHX_ const char *name, int create) 308coro_get_hv (pTHX_ const char *name, int create)
285{ 309{
286#if PERL_VERSION_ATLEAST (5,9,0) 310#if PERL_VERSION_ATLEAST (5,10,0)
287 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 311 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
288 get_hv (name, create); 312 get_hv (name, create);
289#endif 313#endif
290 return get_hv (name, create); 314 return get_hv (name, create);
291} 315}
296 AV *padlist = CvPADLIST (cv); 320 AV *padlist = CvPADLIST (cv);
297 AV *newpadlist, *newpad; 321 AV *newpadlist, *newpad;
298 322
299 newpadlist = newAV (); 323 newpadlist = newAV ();
300 AvREAL_off (newpadlist); 324 AvREAL_off (newpadlist);
301#if PERL_VERSION_ATLEAST (5,9,0) 325#if PERL_VERSION_ATLEAST (5,10,0)
302 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 326 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
303#else 327#else
304 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 328 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
305#endif 329#endif
306 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 330 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
307 --AvFILLp (padlist); 331 --AvFILLp (padlist);
308 332
309 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 333 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
310 av_store (newpadlist, 1, (SV *)newpad); 334 av_store (newpadlist, 1, (SV *)newpad);
311 335
312 return newpadlist; 336 return newpadlist;
313} 337}
314 338
344 368
345 /* casting is fun. */ 369 /* casting is fun. */
346 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 370 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
347 free_padlist (aTHX_ padlist); 371 free_padlist (aTHX_ padlist);
348 372
373 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
374
349 return 0; 375 return 0;
350} 376}
351 377
352#define CORO_MAGIC_type_cv PERL_MAGIC_ext 378#define CORO_MAGIC_type_cv PERL_MAGIC_ext
353#define CORO_MAGIC_type_state PERL_MAGIC_ext 379#define CORO_MAGIC_type_state PERL_MAGIC_ext
403 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 429 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
404 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 430 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
405 else 431 else
406 { 432 {
407#if CORO_PREFER_PERL_FUNCTIONS 433#if CORO_PREFER_PERL_FUNCTIONS
408 /* this is probably cleaner, but also slower? */ 434 /* this is probably cleaner? but also slower! */
435 /* in practise, it seems to be less stable */
409 CV *cp = Perl_cv_clone (cv); 436 CV *cp = Perl_cv_clone (cv);
410 CvPADLIST (cv) = CvPADLIST (cp); 437 CvPADLIST (cv) = CvPADLIST (cp);
411 CvPADLIST (cp) = 0; 438 CvPADLIST (cp) = 0;
412 SvREFCNT_dec (cp); 439 SvREFCNT_dec (cp);
413#else 440#else
497 524
498 if (expect_true (CvDEPTH (cv))) 525 if (expect_true (CvDEPTH (cv)))
499 { 526 {
500 EXTEND (SP, 3); 527 EXTEND (SP, 3);
501 PUSHs ((SV *)CvPADLIST (cv)); 528 PUSHs ((SV *)CvPADLIST (cv));
502 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 529 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
503 PUSHs ((SV *)cv); 530 PUSHs ((SV *)cv);
504 531
505 CvDEPTH (cv) = 0; 532 CvDEPTH (cv) = 0;
506 get_padlist (aTHX_ cv); 533 get_padlist (aTHX_ cv);
507 } 534 }
587 614
588 New(54,PL_savestack,24,ANY); 615 New(54,PL_savestack,24,ANY);
589 PL_savestack_ix = 0; 616 PL_savestack_ix = 0;
590 PL_savestack_max = 24; 617 PL_savestack_max = 24;
591 618
592#if !PERL_VERSION_ATLEAST (5,9,0) 619#if !PERL_VERSION_ATLEAST (5,10,0)
593 New(54,PL_retstack,4,OP*); 620 New(54,PL_retstack,4,OP*);
594 PL_retstack_ix = 0; 621 PL_retstack_ix = 0;
595 PL_retstack_max = 4; 622 PL_retstack_max = 4;
596#endif 623#endif
597} 624}
620 647
621 Safefree (PL_tmps_stack); 648 Safefree (PL_tmps_stack);
622 Safefree (PL_markstack); 649 Safefree (PL_markstack);
623 Safefree (PL_scopestack); 650 Safefree (PL_scopestack);
624 Safefree (PL_savestack); 651 Safefree (PL_savestack);
625#if !PERL_VERSION_ATLEAST (5,9,0) 652#if !PERL_VERSION_ATLEAST (5,10,0)
626 Safefree (PL_retstack); 653 Safefree (PL_retstack);
627#endif 654#endif
628} 655}
629 656
630static size_t 657static size_t
646 #undef VAR 673 #undef VAR
647 } 674 }
648 else 675 else
649 slot = coro->slot; 676 slot = coro->slot;
650 677
678 if (slot)
679 {
651 rss += sizeof (slot->curstackinfo); 680 rss += sizeof (slot->curstackinfo);
652 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 681 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
653 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 682 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
654 rss += slot->tmps_max * sizeof (SV *); 683 rss += slot->tmps_max * sizeof (SV *);
655 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 684 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
656 rss += slot->scopestack_max * sizeof (I32); 685 rss += slot->scopestack_max * sizeof (I32);
657 rss += slot->savestack_max * sizeof (ANY); 686 rss += slot->savestack_max * sizeof (ANY);
658 687
659#if !PERL_VERSION_ATLEAST (5,9,0) 688#if !PERL_VERSION_ATLEAST (5,10,0)
660 rss += slot->retstack_max * sizeof (OP *); 689 rss += slot->retstack_max * sizeof (OP *);
661#endif 690#endif
691 }
662 } 692 }
663 693
664 return rss; 694 return rss;
665} 695}
666 696
667/** coroutine stack handling ************************************************/ 697/** coroutine stack handling ************************************************/
668 698
669static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 699static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
670static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 700static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
701static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
702
703/* apparently < 5.8.8 */
704#ifndef MgPV_nolen_const
705#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
706 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
707 (const char*)(mg)->mg_ptr)
708#endif
671 709
672/* 710/*
673 * This overrides the default magic get method of %SIG elements. 711 * This overrides the default magic get method of %SIG elements.
674 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 712 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
675 * and instead of tryign to save and restore the hash elements, we just provide 713 * and instead of tryign to save and restore the hash elements, we just provide
683{ 721{
684 const char *s = MgPV_nolen_const (mg); 722 const char *s = MgPV_nolen_const (mg);
685 723
686 if (*s == '_') 724 if (*s == '_')
687 { 725 {
688 if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; 726 SV **svp = 0;
689 if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; 727
728 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
729 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
730
731 if (svp)
732 {
733 sv_setsv (sv, *svp ? *svp : &PL_sv_undef);
734 return 0;
735 }
690 } 736 }
691 737
692 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 738 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
739}
740
741static int
742coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
743{
744 const char *s = MgPV_nolen_const (mg);
745
746 if (*s == '_')
747 {
748 SV **svp = 0;
749
750 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
751 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
752
753 if (svp)
754 {
755 SV *old = *svp;
756 *svp = 0;
757 SvREFCNT_dec (old);
758 return 0;
759 }
760 }
761
762 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
693} 763}
694 764
695static int 765static int
696coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 766coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
697{ 767{
707 if (svp) 777 if (svp)
708 { 778 {
709 SV *old = *svp; 779 SV *old = *svp;
710 *svp = newSVsv (sv); 780 *svp = newSVsv (sv);
711 SvREFCNT_dec (old); 781 SvREFCNT_dec (old);
712 return; 782 return 0;
713 } 783 }
714 } 784 }
715 785
716 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 786 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
717} 787}
731 PL_curpm = 0; 801 PL_curpm = 0;
732 PL_curpad = 0; 802 PL_curpad = 0;
733 PL_localizing = 0; 803 PL_localizing = 0;
734 PL_dirty = 0; 804 PL_dirty = 0;
735 PL_restartop = 0; 805 PL_restartop = 0;
806#if PERL_VERSION_ATLEAST (5,10,0)
807 PL_parser = 0;
808#endif
736 809
737 /* recreate the die/warn hooks */ 810 /* recreate the die/warn hooks */
738 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 811 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
739 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 812 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
740 813
741 GvSV (PL_defgv) = newSV (0); 814 GvSV (PL_defgv) = newSV (0);
742 GvAV (PL_defgv) = coro->args; coro->args = 0; 815 GvAV (PL_defgv) = coro->args; coro->args = 0;
743 GvSV (PL_errgv) = newSV (0); 816 GvSV (PL_errgv) = newSV (0);
744 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 817 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
745 PL_rs = newSVsv (GvSV (irsgv)); 818 PL_rs = newSVsv (GvSV (irsgv));
746 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 819 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
747 820
748 { 821 {
749 dSP; 822 dSP;
750 LOGOP myop; 823 LOGOP myop;
751 824
847 : cx->blk_gimme == G_SCALAR ? bot + 1 920 : cx->blk_gimme == G_SCALAR ? bot + 1
848 : bot; 921 : bot;
849 922
850 av_extend (av, top - bot); 923 av_extend (av, top - bot);
851 while (bot < top) 924 while (bot < top)
852 av_push (av, SvREFCNT_inc (*bot++)); 925 av_push (av, SvREFCNT_inc_NN (*bot++));
853 926
854 PL_runops = RUNOPS_DEFAULT; 927 PL_runops = RUNOPS_DEFAULT;
855 ENTER; 928 ENTER;
856 SAVETMPS; 929 SAVETMPS;
857 EXTEND (SP, 3); 930 EXTEND (SP, 3);
895 SAVETMPS; 968 SAVETMPS;
896 EXTEND (SP, 3); 969 EXTEND (SP, 3);
897 PUSHMARK (SP); 970 PUSHMARK (SP);
898 PUSHs (&PL_sv_yes); 971 PUSHs (&PL_sv_yes);
899 PUSHs (fullname); 972 PUSHs (fullname);
900 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 973 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
901 PUTBACK; 974 PUTBACK;
902 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 975 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
903 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 976 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
904 SPAGAIN; 977 SPAGAIN;
905 FREETMPS; 978 FREETMPS;
1121 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1194 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
1122 1195
1123 if (expect_false (next->flags & CF_DESTROYED)) 1196 if (expect_false (next->flags & CF_DESTROYED))
1124 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 1197 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
1125 1198
1126 if (
1127#if PERL_VERSION_ATLEAST (5,9,0) 1199#if !PERL_VERSION_ATLEAST (5,10,0)
1128 expect_false (PL_parser)
1129#else
1130 expect_false (PL_lex_state != LEX_NOTPARSING) 1200 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1131#endif
1132 )
1133 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1201 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version");
1202#endif
1134 } 1203 }
1135} 1204}
1136 1205
1137/* always use the TRANSFER macro */ 1206/* always use the TRANSFER macro */
1138static void NOINLINE 1207static void NOINLINE
1139transfer (pTHX_ struct coro *prev, struct coro *next) 1208transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1140{ 1209{
1141 dSTACKLEVEL; 1210 dSTACKLEVEL;
1142 static volatile int has_throw; 1211 static volatile int has_throw;
1143 1212
1144 /* sometimes transfer is only called to set idle_sp */ 1213 /* sometimes transfer is only called to set idle_sp */
1178 load_perl (aTHX_ next); 1247 load_perl (aTHX_ next);
1179 1248
1180 prev__cctx = prev->cctx; 1249 prev__cctx = prev->cctx;
1181 1250
1182 /* possibly "free" the cctx */ 1251 /* possibly "free" the cctx */
1183 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) 1252 if (expect_true (
1253 prev__cctx->idle_sp == STACKLEVEL
1254 && !(prev__cctx->flags & CC_TRACE)
1255 && !force_cctx
1256 ))
1184 { 1257 {
1185 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1258 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1186 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1259 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
1187 1260
1188 prev->cctx = 0; 1261 prev->cctx = 0;
1231struct transfer_args 1304struct transfer_args
1232{ 1305{
1233 struct coro *prev, *next; 1306 struct coro *prev, *next;
1234}; 1307};
1235 1308
1236#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1309#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1237#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1310#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1238 1311
1239/** high level stuff ********************************************************/ 1312/** high level stuff ********************************************************/
1240 1313
1241static int 1314static int
1335{ 1408{
1336 dTHX; 1409 dTHX;
1337 struct transfer_args ta; 1410 struct transfer_args ta;
1338 1411
1339 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1412 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1340 TRANSFER (ta); 1413 TRANSFER (ta, 1);
1341} 1414}
1342 1415
1343/** Coro ********************************************************************/ 1416/** Coro ********************************************************************/
1344 1417
1345static void 1418static void
1363static int 1436static int
1364api_ready (SV *coro_sv) 1437api_ready (SV *coro_sv)
1365{ 1438{
1366 dTHX; 1439 dTHX;
1367 struct coro *coro; 1440 struct coro *coro;
1441 SV *sv_hook;
1442 void (*xs_hook)(void);
1368 1443
1369 if (SvROK (coro_sv)) 1444 if (SvROK (coro_sv))
1370 coro_sv = SvRV (coro_sv); 1445 coro_sv = SvRV (coro_sv);
1371 1446
1372 coro = SvSTATE (coro_sv); 1447 coro = SvSTATE (coro_sv);
1375 return 0; 1450 return 0;
1376 1451
1377 coro->flags |= CF_READY; 1452 coro->flags |= CF_READY;
1378 1453
1379 LOCK; 1454 LOCK;
1455
1456 sv_hook = coro_nready ? 0 : coro_readyhook;
1457 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1458
1380 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1459 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1381 ++coro_nready; 1460 ++coro_nready;
1461
1382 UNLOCK; 1462 UNLOCK;
1463
1464 if (sv_hook)
1465 {
1466 dSP;
1467
1468 ENTER;
1469 SAVETMPS;
1470
1471 PUSHMARK (SP);
1472 PUTBACK;
1473 call_sv (sv_hook, G_DISCARD);
1474 SPAGAIN;
1475
1476 FREETMPS;
1477 LEAVE;
1478 }
1479
1480 if (xs_hook)
1481 xs_hook ();
1383 1482
1384 return 1; 1483 return 1;
1385} 1484}
1386 1485
1387static int 1486static int
1476{ 1575{
1477 dTHX; 1576 dTHX;
1478 struct transfer_args ta; 1577 struct transfer_args ta;
1479 1578
1480 prepare_schedule (aTHX_ &ta); 1579 prepare_schedule (aTHX_ &ta);
1481 TRANSFER (ta); 1580 TRANSFER (ta, 1);
1482} 1581}
1483 1582
1484static int 1583static int
1485api_cede (void) 1584api_cede (void)
1486{ 1585{
1489 1588
1490 prepare_cede (aTHX_ &ta); 1589 prepare_cede (aTHX_ &ta);
1491 1590
1492 if (expect_true (ta.prev != ta.next)) 1591 if (expect_true (ta.prev != ta.next))
1493 { 1592 {
1494 TRANSFER (ta); 1593 TRANSFER (ta, 1);
1495 return 1; 1594 return 1;
1496 } 1595 }
1497 else 1596 else
1498 return 0; 1597 return 0;
1499} 1598}
1504 dTHX; 1603 dTHX;
1505 struct transfer_args ta; 1604 struct transfer_args ta;
1506 1605
1507 if (prepare_cede_notself (aTHX_ &ta)) 1606 if (prepare_cede_notself (aTHX_ &ta))
1508 { 1607 {
1509 TRANSFER (ta); 1608 TRANSFER (ta, 1);
1510 return 1; 1609 return 1;
1511 } 1610 }
1512 else 1611 else
1513 return 0; 1612 return 0;
1514} 1613}
1537 else 1636 else
1538 coro->slot->runops = RUNOPS_DEFAULT; 1637 coro->slot->runops = RUNOPS_DEFAULT;
1539 } 1638 }
1540} 1639}
1541 1640
1641static int
1642coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1643{
1644 AV *padlist;
1645 AV *av = (AV *)mg->mg_obj;
1646
1647 abort ();
1648
1649 return 0;
1650}
1651
1652static MGVTBL coro_gensub_vtbl = {
1653 0, 0, 0, 0,
1654 coro_gensub_free
1655};
1656
1657/*****************************************************************************/
1658/* PerlIO::cede */
1659
1660typedef struct
1661{
1662 PerlIOBuf base;
1663 NV next, every;
1664} PerlIOCede;
1665
1666static IV
1667PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1668{
1669 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1670
1671 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1672 self->next = nvtime () + self->every;
1673
1674 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1675}
1676
1677static SV *
1678PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1679{
1680 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1681
1682 return newSVnv (self->every);
1683}
1684
1685static IV
1686PerlIOCede_flush (pTHX_ PerlIO *f)
1687{
1688 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1689 double now = nvtime ();
1690
1691 if (now >= self->next)
1692 {
1693 api_cede ();
1694 self->next = now + self->every;
1695 }
1696
1697 return PerlIOBuf_flush (aTHX_ f);
1698}
1699
1700static PerlIO_funcs PerlIO_cede =
1701{
1702 sizeof(PerlIO_funcs),
1703 "cede",
1704 sizeof(PerlIOCede),
1705 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1706 PerlIOCede_pushed,
1707 PerlIOBuf_popped,
1708 PerlIOBuf_open,
1709 PerlIOBase_binmode,
1710 PerlIOCede_getarg,
1711 PerlIOBase_fileno,
1712 PerlIOBuf_dup,
1713 PerlIOBuf_read,
1714 PerlIOBuf_unread,
1715 PerlIOBuf_write,
1716 PerlIOBuf_seek,
1717 PerlIOBuf_tell,
1718 PerlIOBuf_close,
1719 PerlIOCede_flush,
1720 PerlIOBuf_fill,
1721 PerlIOBase_eof,
1722 PerlIOBase_error,
1723 PerlIOBase_clearerr,
1724 PerlIOBase_setlinebuf,
1725 PerlIOBuf_get_base,
1726 PerlIOBuf_bufsiz,
1727 PerlIOBuf_get_ptr,
1728 PerlIOBuf_get_cnt,
1729 PerlIOBuf_set_ptrcnt,
1730};
1731
1732
1542MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1733MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1543 1734
1544PROTOTYPES: DISABLE 1735PROTOTYPES: DISABLE
1545 1736
1546BOOT: 1737BOOT:
1551 BOOT_PAGESIZE; 1742 BOOT_PAGESIZE;
1552 1743
1553 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1744 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1554 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1745 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1555 1746
1556 orig_sigelem_get = PL_vtbl_sigelem.svt_get; 1747 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1557 PL_vtbl_sigelem.svt_get = coro_sigelem_get; 1748 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1558 orig_sigelem_set = PL_vtbl_sigelem.svt_set; 1749 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1559 PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1560 1750
1561 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); 1751 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1562 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 1752 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1563 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); 1753 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1564 1754
1573 main_top_env = PL_top_env; 1763 main_top_env = PL_top_env;
1574 1764
1575 while (main_top_env->je_prev) 1765 while (main_top_env->je_prev)
1576 main_top_env = main_top_env->je_prev; 1766 main_top_env = main_top_env->je_prev;
1577 1767
1578 coroapi.ver = CORO_API_VERSION; 1768 coroapi.ver = CORO_API_VERSION;
1579 coroapi.rev = CORO_API_REVISION; 1769 coroapi.rev = CORO_API_REVISION;
1580 coroapi.transfer = api_transfer; 1770 coroapi.transfer = api_transfer;
1771
1772 {
1773 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1774
1775 if (!svp) croak ("Time::HiRes is required");
1776 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1777
1778 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1779 }
1581 1780
1582 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1781 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1583} 1782}
1584 1783
1585SV * 1784SV *
1656 } 1855 }
1657 SPAGAIN; 1856 SPAGAIN;
1658 1857
1659 BARRIER; 1858 BARRIER;
1660 PUTBACK; 1859 PUTBACK;
1661 TRANSFER (ta); 1860 TRANSFER (ta, 0);
1662 SPAGAIN; /* might be the sp of a different coroutine now */ 1861 SPAGAIN; /* might be the sp of a different coroutine now */
1663 /* be extra careful not to ever do anything after TRANSFER */ 1862 /* be extra careful not to ever do anything after TRANSFER */
1664} 1863}
1665 1864
1666bool 1865bool
1669 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1868 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1670 OUTPUT: 1869 OUTPUT:
1671 RETVAL 1870 RETVAL
1672 1871
1673void 1872void
1674_exit (code) 1873_exit (int code)
1675 int code
1676 PROTOTYPE: $ 1874 PROTOTYPE: $
1677 CODE: 1875 CODE:
1678 _exit (code); 1876 _exit (code);
1679 1877
1680int 1878int
1714call (Coro::State coro, SV *coderef) 1912call (Coro::State coro, SV *coderef)
1715 ALIAS: 1913 ALIAS:
1716 eval = 1 1914 eval = 1
1717 CODE: 1915 CODE:
1718{ 1916{
1719 if (coro->mainstack) 1917 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1720 { 1918 {
1721 struct coro temp; 1919 struct coro temp;
1722 1920
1723 if (!(coro->flags & CF_RUNNING)) 1921 if (!(coro->flags & CF_RUNNING))
1724 { 1922 {
1771 1969
1772void 1970void
1773api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1971api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1774 1972
1775SV * 1973SV *
1776has_stack (Coro::State coro) 1974has_cctx (Coro::State coro)
1777 PROTOTYPE: $ 1975 PROTOTYPE: $
1778 CODE: 1976 CODE:
1779 RETVAL = boolSV (!!coro->cctx); 1977 RETVAL = boolSV (!!coro->cctx);
1780 OUTPUT: 1978 OUTPUT:
1781 RETVAL 1979 RETVAL
1800 case 1: RETVAL = coro->usecount; break; 1998 case 1: RETVAL = coro->usecount; break;
1801 } 1999 }
1802 OUTPUT: 2000 OUTPUT:
1803 RETVAL 2001 RETVAL
1804 2002
2003void
2004force_cctx ()
2005 CODE:
2006 struct coro *coro = SvSTATE (coro_current);
2007 coro->cctx->idle_sp = 0;
2008
2009void
2010throw (Coro::State self, SV *throw = &PL_sv_undef)
2011 PROTOTYPE: $;$
2012 CODE:
2013 SvREFCNT_dec (self->throw);
2014 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2015
2016void
2017swap_defsv (Coro::State self)
2018 PROTOTYPE: $
2019 ALIAS:
2020 swap_defav = 1
2021 CODE:
2022 if (!self->slot)
2023 croak ("cannot swap state with coroutine that has no saved state");
2024 else
2025 {
2026 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv);
2027 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2028
2029 SV *tmp = *src; *src = *dst; *dst = tmp;
2030 }
1805 2031
1806MODULE = Coro::State PACKAGE = Coro 2032MODULE = Coro::State PACKAGE = Coro
1807 2033
1808BOOT: 2034BOOT:
1809{ 2035{
1810 int i; 2036 int i;
1811 2037
2038 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1812 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 2039 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1813 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 2040 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1814 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1815 2041
1816 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 2042 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1817 SvREADONLY_on (coro_current); 2043 SvREADONLY_on (coro_current);
1818 2044
1819 coro_stash = gv_stashpv ("Coro", TRUE); 2045 coro_stash = gv_stashpv ("Coro", TRUE);
1849void 2075void
1850_set_current (SV *current) 2076_set_current (SV *current)
1851 PROTOTYPE: $ 2077 PROTOTYPE: $
1852 CODE: 2078 CODE:
1853 SvREFCNT_dec (SvRV (coro_current)); 2079 SvREFCNT_dec (SvRV (coro_current));
1854 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2080 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2081
2082void
2083_set_readyhook (SV *hook)
2084 PROTOTYPE: $
2085 CODE:
2086 LOCK;
2087 SvREFCNT_dec (coro_readyhook);
2088 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2089 UNLOCK;
1855 2090
1856int 2091int
1857prio (Coro::State coro, int newprio = 0) 2092prio (Coro::State coro, int newprio = 0)
1858 ALIAS: 2093 ALIAS:
1859 nice = 1 2094 nice = 1
1889 CODE: 2124 CODE:
1890 RETVAL = coro_nready; 2125 RETVAL = coro_nready;
1891 OUTPUT: 2126 OUTPUT:
1892 RETVAL 2127 RETVAL
1893 2128
1894void
1895throw (Coro::State self, SV *throw = &PL_sv_undef)
1896 PROTOTYPE: $;$
1897 CODE:
1898 SvREFCNT_dec (self->throw);
1899 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1900
1901# for async_pool speedup 2129# for async_pool speedup
1902void 2130void
1903_pool_1 (SV *cb) 2131_pool_1 (SV *cb)
1904 CODE: 2132 CODE:
1905{ 2133{
1910 AV *invoke_av; 2138 AV *invoke_av;
1911 int i, len; 2139 int i, len;
1912 2140
1913 if (!invoke) 2141 if (!invoke)
1914 { 2142 {
1915 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2143 SV *old = PL_diehook;
2144 PL_diehook = 0;
2145 SvREFCNT_dec (old);
1916 croak ("\3async_pool terminate\2\n"); 2146 croak ("\3async_pool terminate\2\n");
1917 } 2147 }
1918 2148
1919 SvREFCNT_dec (coro->saved_deffh); 2149 SvREFCNT_dec (coro->saved_deffh);
1920 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2150 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1921 2151
1922 hv_store (hv, "desc", sizeof ("desc") - 1, 2152 hv_store (hv, "desc", sizeof ("desc") - 1,
1923 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2153 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1924 2154
1925 invoke_av = (AV *)SvRV (invoke); 2155 invoke_av = (AV *)SvRV (invoke);
1929 2159
1930 if (len > 0) 2160 if (len > 0)
1931 { 2161 {
1932 av_fill (defav, len - 1); 2162 av_fill (defav, len - 1);
1933 for (i = 0; i < len; ++i) 2163 for (i = 0; i < len; ++i)
1934 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2164 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1935 } 2165 }
1936 2166
1937 SvREFCNT_dec (invoke); 2167 SvREFCNT_dec (invoke);
1938} 2168}
1939 2169
1949 coro->saved_deffh = 0; 2179 coro->saved_deffh = 0;
1950 2180
1951 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2181 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1952 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2182 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1953 { 2183 {
1954 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2184 SV *old = PL_diehook;
2185 PL_diehook = 0;
2186 SvREFCNT_dec (old);
1955 croak ("\3async_pool terminate\2\n"); 2187 croak ("\3async_pool terminate\2\n");
1956 } 2188 }
1957 2189
1958 av_clear (GvAV (PL_defgv)); 2190 av_clear (GvAV (PL_defgv));
1959 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 2191 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1965 api_trace (coro_current, 0); 2197 api_trace (coro_current, 0);
1966 2198
1967 av_push (av_async_pool, newSVsv (coro_current)); 2199 av_push (av_async_pool, newSVsv (coro_current));
1968} 2200}
1969 2201
2202#if 0
2203
2204void
2205_generator_call (...)
2206 PROTOTYPE: @
2207 PPCODE:
2208 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2209 xxxx
2210 abort ();
2211
2212SV *
2213gensub (SV *sub, ...)
2214 PROTOTYPE: &;@
2215 CODE:
2216{
2217 struct coro *coro;
2218 MAGIC *mg;
2219 CV *xcv;
2220 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2221 int i;
2222
2223 CvGV (ncv) = CvGV (cv);
2224 CvFILE (ncv) = CvFILE (cv);
2225
2226 Newz (0, coro, 1, struct coro);
2227 coro->args = newAV ();
2228 coro->flags = CF_NEW;
2229
2230 av_extend (coro->args, items - 1);
2231 for (i = 1; i < items; i++)
2232 av_push (coro->args, newSVsv (ST (i)));
2233
2234 CvISXSUB_on (ncv);
2235 CvXSUBANY (ncv).any_ptr = (void *)coro;
2236
2237 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2238
2239 CvXSUB (ncv) = CvXSUB (xcv);
2240 CvANON_on (ncv);
2241
2242 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2243 RETVAL = newRV_noinc ((SV *)ncv);
2244}
2245 OUTPUT:
2246 RETVAL
2247
2248#endif
2249
1970 2250
1971MODULE = Coro::State PACKAGE = Coro::AIO 2251MODULE = Coro::State PACKAGE = Coro::AIO
1972 2252
1973SV * 2253void
1974_get_state () 2254_get_state (SV *self)
1975 CODE: 2255 PPCODE:
1976{ 2256{
1977 struct io_state *data; 2257 AV *defav = GvAV (PL_defgv);
1978 2258 AV *av = newAV ();
2259 int i;
1979 RETVAL = newSV (sizeof (struct io_state)); 2260 SV *data_sv = newSV (sizeof (struct io_state));
1980 data = (struct io_state *)SvPVX (RETVAL); 2261 struct io_state *data = (struct io_state *)SvPVX (data_sv);
1981 SvCUR_set (RETVAL, sizeof (struct io_state)); 2262 SvCUR_set (data_sv, sizeof (struct io_state));
1982 SvPOK_only (RETVAL); 2263 SvPOK_only (data_sv);
1983 2264
1984 data->errorno = errno; 2265 data->errorno = errno;
1985 data->laststype = PL_laststype; 2266 data->laststype = PL_laststype;
1986 data->laststatval = PL_laststatval; 2267 data->laststatval = PL_laststatval;
1987 data->statcache = PL_statcache; 2268 data->statcache = PL_statcache;
2269
2270 av_extend (av, AvFILLp (defav) + 1 + 1);
2271
2272 for (i = 0; i <= AvFILLp (defav); ++i)
2273 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2274
2275 av_push (av, data_sv);
2276
2277 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2278
2279 api_ready (self);
1988} 2280}
1989 OUTPUT:
1990 RETVAL
1991 2281
1992void 2282void
1993_set_state (char *data_) 2283_set_state (SV *state)
1994 PROTOTYPE: $ 2284 PROTOTYPE: $
1995 CODE: 2285 PPCODE:
1996{ 2286{
1997 struct io_state *data = (void *)data_; 2287 AV *av = (AV *)SvRV (state);
2288 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2289 int i;
1998 2290
1999 errno = data->errorno; 2291 errno = data->errorno;
2000 PL_laststype = data->laststype; 2292 PL_laststype = data->laststype;
2001 PL_laststatval = data->laststatval; 2293 PL_laststatval = data->laststatval;
2002 PL_statcache = data->statcache; 2294 PL_statcache = data->statcache;
2003}
2004 2295
2296 EXTEND (SP, AvFILLp (av));
2297 for (i = 0; i < AvFILLp (av); ++i)
2298 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2299}
2300
2301
2302MODULE = Coro::State PACKAGE = Coro::AnyEvent
2303
2304BOOT:
2305 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2306
2307SV *
2308_schedule (...)
2309 PROTOTYPE: @
2310 CODE:
2311{
2312 static int incede;
2313
2314 api_cede_notself ();
2315
2316 ++incede;
2317 while (coro_nready >= incede && api_cede ())
2318 ;
2319
2320 sv_setsv (sv_activity, &PL_sv_undef);
2321 if (coro_nready >= incede)
2322 {
2323 PUSHMARK (SP);
2324 PUTBACK;
2325 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2326 SPAGAIN;
2327 }
2328
2329 --incede;
2330}
2331
2332
2333MODULE = Coro::State PACKAGE = PerlIO::cede
2334
2335BOOT:
2336 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines