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.225 by root, Wed Mar 12 14:13:11 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
79# define GV_NOTQUAL 0 88# define GV_NOTQUAL 0
80#endif 89#endif
81#ifndef newSV 90#ifndef newSV
82# define newSV(l) NEWSV(0,l) 91# define newSV(l) NEWSV(0,l)
83#endif 92#endif
93#ifndef SvREFCNT_inc_NN
94# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
95#endif
84 96
85/* 5.11 */ 97/* 5.11 */
86#ifndef CxHASARGS 98#ifndef CxHASARGS
87# define CxHASARGS(cx) (cx)->blk_sub.hasargs 99# define CxHASARGS(cx) (cx)->blk_sub.hasargs
88#endif 100#endif
139#endif 151#endif
140 152
141/* helper storage struct for Coro::AIO */ 153/* helper storage struct for Coro::AIO */
142struct io_state 154struct io_state
143{ 155{
156 AV *res;
144 int errorno; 157 int errorno;
145 I32 laststype; 158 I32 laststype;
146 int laststatval; 159 int laststatval;
147 Stat_t statcache; 160 Stat_t statcache;
148}; 161};
162
163static double (*nvtime)(); /* so why doesn't it take void? */
149 164
150static size_t coro_stacksize = CORO_STACKSIZE; 165static size_t coro_stacksize = CORO_STACKSIZE;
151static struct CoroAPI coroapi; 166static struct CoroAPI coroapi;
152static AV *main_mainstack; /* used to differentiate between $main and others */ 167static AV *main_mainstack; /* used to differentiate between $main and others */
153static JMPENV *main_top_env; 168static JMPENV *main_top_env;
162 177
163/* async_pool helper stuff */ 178/* async_pool helper stuff */
164static SV *sv_pool_rss; 179static SV *sv_pool_rss;
165static SV *sv_pool_size; 180static SV *sv_pool_size;
166static AV *av_async_pool; 181static AV *av_async_pool;
182
183/* Coro::AnyEvent */
184static SV *sv_activity;
167 185
168static struct coro_cctx *cctx_first; 186static struct coro_cctx *cctx_first;
169static int cctx_count, cctx_idle; 187static int cctx_count, cctx_idle;
170 188
171enum { 189enum {
257#define PRIO_IDLE -3 275#define PRIO_IDLE -3
258#define PRIO_MIN -4 276#define PRIO_MIN -4
259 277
260/* for Coro.pm */ 278/* for Coro.pm */
261static SV *coro_current; 279static SV *coro_current;
280static SV *coro_readyhook;
262static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 281static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
263static int coro_nready; 282static int coro_nready;
264static struct coro *coro_first; 283static struct coro *coro_first;
265 284
266/** lowlevel stuff **********************************************************/ 285/** lowlevel stuff **********************************************************/
267 286
268static SV * 287static SV *
269coro_get_sv (pTHX_ const char *name, int create) 288coro_get_sv (pTHX_ const char *name, int create)
270{ 289{
271#if PERL_VERSION_ATLEAST (5,9,0) 290#if PERL_VERSION_ATLEAST (5,10,0)
272 /* 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 */
273 get_sv (name, create); 292 get_sv (name, create);
274#endif 293#endif
275 return get_sv (name, create); 294 return get_sv (name, create);
276} 295}
277 296
278static AV * 297static AV *
279coro_get_av (pTHX_ const char *name, int create) 298coro_get_av (pTHX_ const char *name, int create)
280{ 299{
281#if PERL_VERSION_ATLEAST (5,9,0) 300#if PERL_VERSION_ATLEAST (5,10,0)
282 /* 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 */
283 get_av (name, create); 302 get_av (name, create);
284#endif 303#endif
285 return get_av (name, create); 304 return get_av (name, create);
286} 305}
287 306
288static HV * 307static HV *
289coro_get_hv (pTHX_ const char *name, int create) 308coro_get_hv (pTHX_ const char *name, int create)
290{ 309{
291#if PERL_VERSION_ATLEAST (5,9,0) 310#if PERL_VERSION_ATLEAST (5,10,0)
292 /* 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 */
293 get_hv (name, create); 312 get_hv (name, create);
294#endif 313#endif
295 return get_hv (name, create); 314 return get_hv (name, create);
296} 315}
301 AV *padlist = CvPADLIST (cv); 320 AV *padlist = CvPADLIST (cv);
302 AV *newpadlist, *newpad; 321 AV *newpadlist, *newpad;
303 322
304 newpadlist = newAV (); 323 newpadlist = newAV ();
305 AvREAL_off (newpadlist); 324 AvREAL_off (newpadlist);
306#if PERL_VERSION_ATLEAST (5,9,0) 325#if PERL_VERSION_ATLEAST (5,10,0)
307 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 326 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
308#else 327#else
309 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 328 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
310#endif 329#endif
311 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 330 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
312 --AvFILLp (padlist); 331 --AvFILLp (padlist);
313 332
314 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 333 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
315 av_store (newpadlist, 1, (SV *)newpad); 334 av_store (newpadlist, 1, (SV *)newpad);
316 335
317 return newpadlist; 336 return newpadlist;
318} 337}
319 338
349 368
350 /* casting is fun. */ 369 /* casting is fun. */
351 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 370 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
352 free_padlist (aTHX_ padlist); 371 free_padlist (aTHX_ padlist);
353 372
373 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
374
354 return 0; 375 return 0;
355} 376}
356 377
357#define CORO_MAGIC_type_cv PERL_MAGIC_ext 378#define CORO_MAGIC_type_cv PERL_MAGIC_ext
358#define CORO_MAGIC_type_state PERL_MAGIC_ext 379#define CORO_MAGIC_type_state PERL_MAGIC_ext
408 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 429 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
409 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 430 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
410 else 431 else
411 { 432 {
412#if CORO_PREFER_PERL_FUNCTIONS 433#if CORO_PREFER_PERL_FUNCTIONS
413 /* this is probably cleaner, but also slower? */ 434 /* this is probably cleaner? but also slower! */
435 /* in practise, it seems to be less stable */
414 CV *cp = Perl_cv_clone (cv); 436 CV *cp = Perl_cv_clone (cv);
415 CvPADLIST (cv) = CvPADLIST (cp); 437 CvPADLIST (cv) = CvPADLIST (cp);
416 CvPADLIST (cp) = 0; 438 CvPADLIST (cp) = 0;
417 SvREFCNT_dec (cp); 439 SvREFCNT_dec (cp);
418#else 440#else
502 524
503 if (expect_true (CvDEPTH (cv))) 525 if (expect_true (CvDEPTH (cv)))
504 { 526 {
505 EXTEND (SP, 3); 527 EXTEND (SP, 3);
506 PUSHs ((SV *)CvPADLIST (cv)); 528 PUSHs ((SV *)CvPADLIST (cv));
507 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 529 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
508 PUSHs ((SV *)cv); 530 PUSHs ((SV *)cv);
509 531
510 CvDEPTH (cv) = 0; 532 CvDEPTH (cv) = 0;
511 get_padlist (aTHX_ cv); 533 get_padlist (aTHX_ cv);
512 } 534 }
592 614
593 New(54,PL_savestack,24,ANY); 615 New(54,PL_savestack,24,ANY);
594 PL_savestack_ix = 0; 616 PL_savestack_ix = 0;
595 PL_savestack_max = 24; 617 PL_savestack_max = 24;
596 618
597#if !PERL_VERSION_ATLEAST (5,9,0) 619#if !PERL_VERSION_ATLEAST (5,10,0)
598 New(54,PL_retstack,4,OP*); 620 New(54,PL_retstack,4,OP*);
599 PL_retstack_ix = 0; 621 PL_retstack_ix = 0;
600 PL_retstack_max = 4; 622 PL_retstack_max = 4;
601#endif 623#endif
602} 624}
625 647
626 Safefree (PL_tmps_stack); 648 Safefree (PL_tmps_stack);
627 Safefree (PL_markstack); 649 Safefree (PL_markstack);
628 Safefree (PL_scopestack); 650 Safefree (PL_scopestack);
629 Safefree (PL_savestack); 651 Safefree (PL_savestack);
630#if !PERL_VERSION_ATLEAST (5,9,0) 652#if !PERL_VERSION_ATLEAST (5,10,0)
631 Safefree (PL_retstack); 653 Safefree (PL_retstack);
632#endif 654#endif
633} 655}
634 656
635static size_t 657static size_t
651 #undef VAR 673 #undef VAR
652 } 674 }
653 else 675 else
654 slot = coro->slot; 676 slot = coro->slot;
655 677
678 if (slot)
679 {
656 rss += sizeof (slot->curstackinfo); 680 rss += sizeof (slot->curstackinfo);
657 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 681 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
658 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 *);
659 rss += slot->tmps_max * sizeof (SV *); 683 rss += slot->tmps_max * sizeof (SV *);
660 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 684 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
661 rss += slot->scopestack_max * sizeof (I32); 685 rss += slot->scopestack_max * sizeof (I32);
662 rss += slot->savestack_max * sizeof (ANY); 686 rss += slot->savestack_max * sizeof (ANY);
663 687
664#if !PERL_VERSION_ATLEAST (5,9,0) 688#if !PERL_VERSION_ATLEAST (5,10,0)
665 rss += slot->retstack_max * sizeof (OP *); 689 rss += slot->retstack_max * sizeof (OP *);
666#endif 690#endif
691 }
667 } 692 }
668 693
669 return rss; 694 return rss;
670} 695}
671 696
672/** coroutine stack handling ************************************************/ 697/** coroutine stack handling ************************************************/
673 698
674static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 699static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
675static 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
676 709
677/* 710/*
678 * This overrides the default magic get method of %SIG elements. 711 * 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 712 * 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 713 * and instead of tryign to save and restore the hash elements, we just provide
688{ 721{
689 const char *s = MgPV_nolen_const (mg); 722 const char *s = MgPV_nolen_const (mg);
690 723
691 if (*s == '_') 724 if (*s == '_')
692 { 725 {
693 if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; 726 SV **svp = 0;
694 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 }
695 } 736 }
696 737
697 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;
698} 763}
699 764
700static int 765static int
701coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 766coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
702{ 767{
712 if (svp) 777 if (svp)
713 { 778 {
714 SV *old = *svp; 779 SV *old = *svp;
715 *svp = newSVsv (sv); 780 *svp = newSVsv (sv);
716 SvREFCNT_dec (old); 781 SvREFCNT_dec (old);
717 return; 782 return 0;
718 } 783 }
719 } 784 }
720 785
721 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 786 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
722} 787}
736 PL_curpm = 0; 801 PL_curpm = 0;
737 PL_curpad = 0; 802 PL_curpad = 0;
738 PL_localizing = 0; 803 PL_localizing = 0;
739 PL_dirty = 0; 804 PL_dirty = 0;
740 PL_restartop = 0; 805 PL_restartop = 0;
806#if PERL_VERSION_ATLEAST (5,10,0)
807 PL_parser = 0;
808#endif
741 809
742 /* recreate the die/warn hooks */ 810 /* recreate the die/warn hooks */
743 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 );
744 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);
745 813
746 GvSV (PL_defgv) = newSV (0); 814 GvSV (PL_defgv) = newSV (0);
747 GvAV (PL_defgv) = coro->args; coro->args = 0; 815 GvAV (PL_defgv) = coro->args; coro->args = 0;
748 GvSV (PL_errgv) = newSV (0); 816 GvSV (PL_errgv) = newSV (0);
749 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);
750 PL_rs = newSVsv (GvSV (irsgv)); 818 PL_rs = newSVsv (GvSV (irsgv));
751 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 819 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
752 820
753 { 821 {
754 dSP; 822 dSP;
755 LOGOP myop; 823 LOGOP myop;
756 824
852 : cx->blk_gimme == G_SCALAR ? bot + 1 920 : cx->blk_gimme == G_SCALAR ? bot + 1
853 : bot; 921 : bot;
854 922
855 av_extend (av, top - bot); 923 av_extend (av, top - bot);
856 while (bot < top) 924 while (bot < top)
857 av_push (av, SvREFCNT_inc (*bot++)); 925 av_push (av, SvREFCNT_inc_NN (*bot++));
858 926
859 PL_runops = RUNOPS_DEFAULT; 927 PL_runops = RUNOPS_DEFAULT;
860 ENTER; 928 ENTER;
861 SAVETMPS; 929 SAVETMPS;
862 EXTEND (SP, 3); 930 EXTEND (SP, 3);
1126 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");
1127 1195
1128 if (expect_false (next->flags & CF_DESTROYED)) 1196 if (expect_false (next->flags & CF_DESTROYED))
1129 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");
1130 1198
1131 if (
1132#if PERL_VERSION_ATLEAST (5,9,0) 1199#if !PERL_VERSION_ATLEAST (5,10,0)
1133 expect_false (PL_parser && PL_parser->lex_state != LEX_NOTPARSING)
1134#else
1135 expect_false (PL_lex_state != LEX_NOTPARSING) 1200 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1136#endif
1137 )
1138 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
1139 } 1203 }
1140} 1204}
1141 1205
1142/* always use the TRANSFER macro */ 1206/* always use the TRANSFER macro */
1143static void NOINLINE 1207static void NOINLINE
1144transfer (pTHX_ struct coro *prev, struct coro *next) 1208transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1145{ 1209{
1146 dSTACKLEVEL; 1210 dSTACKLEVEL;
1147 static volatile int has_throw; 1211 static volatile int has_throw;
1148 1212
1149 /* sometimes transfer is only called to set idle_sp */ 1213 /* sometimes transfer is only called to set idle_sp */
1183 load_perl (aTHX_ next); 1247 load_perl (aTHX_ next);
1184 1248
1185 prev__cctx = prev->cctx; 1249 prev__cctx = prev->cctx;
1186 1250
1187 /* possibly "free" the cctx */ 1251 /* possibly "free" the cctx */
1188 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 ))
1189 { 1257 {
1190 /* 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 */
1191 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));
1192 1260
1193 prev->cctx = 0; 1261 prev->cctx = 0;
1236struct transfer_args 1304struct transfer_args
1237{ 1305{
1238 struct coro *prev, *next; 1306 struct coro *prev, *next;
1239}; 1307};
1240 1308
1241#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1309#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1242#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1310#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1243 1311
1244/** high level stuff ********************************************************/ 1312/** high level stuff ********************************************************/
1245 1313
1246static int 1314static int
1340{ 1408{
1341 dTHX; 1409 dTHX;
1342 struct transfer_args ta; 1410 struct transfer_args ta;
1343 1411
1344 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1412 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1345 TRANSFER (ta); 1413 TRANSFER (ta, 1);
1346} 1414}
1347 1415
1348/** Coro ********************************************************************/ 1416/** Coro ********************************************************************/
1349 1417
1350static void 1418static void
1368static int 1436static int
1369api_ready (SV *coro_sv) 1437api_ready (SV *coro_sv)
1370{ 1438{
1371 dTHX; 1439 dTHX;
1372 struct coro *coro; 1440 struct coro *coro;
1441 SV *sv_hook;
1442 void (*xs_hook)(void);
1373 1443
1374 if (SvROK (coro_sv)) 1444 if (SvROK (coro_sv))
1375 coro_sv = SvRV (coro_sv); 1445 coro_sv = SvRV (coro_sv);
1376 1446
1377 coro = SvSTATE (coro_sv); 1447 coro = SvSTATE (coro_sv);
1380 return 0; 1450 return 0;
1381 1451
1382 coro->flags |= CF_READY; 1452 coro->flags |= CF_READY;
1383 1453
1384 LOCK; 1454 LOCK;
1455
1456 sv_hook = coro_nready ? 0 : coro_readyhook;
1457 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1458
1385 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1459 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1386 ++coro_nready; 1460 ++coro_nready;
1461
1387 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 ();
1388 1482
1389 return 1; 1483 return 1;
1390} 1484}
1391 1485
1392static int 1486static int
1481{ 1575{
1482 dTHX; 1576 dTHX;
1483 struct transfer_args ta; 1577 struct transfer_args ta;
1484 1578
1485 prepare_schedule (aTHX_ &ta); 1579 prepare_schedule (aTHX_ &ta);
1486 TRANSFER (ta); 1580 TRANSFER (ta, 1);
1487} 1581}
1488 1582
1489static int 1583static int
1490api_cede (void) 1584api_cede (void)
1491{ 1585{
1494 1588
1495 prepare_cede (aTHX_ &ta); 1589 prepare_cede (aTHX_ &ta);
1496 1590
1497 if (expect_true (ta.prev != ta.next)) 1591 if (expect_true (ta.prev != ta.next))
1498 { 1592 {
1499 TRANSFER (ta); 1593 TRANSFER (ta, 1);
1500 return 1; 1594 return 1;
1501 } 1595 }
1502 else 1596 else
1503 return 0; 1597 return 0;
1504} 1598}
1509 dTHX; 1603 dTHX;
1510 struct transfer_args ta; 1604 struct transfer_args ta;
1511 1605
1512 if (prepare_cede_notself (aTHX_ &ta)) 1606 if (prepare_cede_notself (aTHX_ &ta))
1513 { 1607 {
1514 TRANSFER (ta); 1608 TRANSFER (ta, 1);
1515 return 1; 1609 return 1;
1516 } 1610 }
1517 else 1611 else
1518 return 0; 1612 return 0;
1519} 1613}
1542 else 1636 else
1543 coro->slot->runops = RUNOPS_DEFAULT; 1637 coro->slot->runops = RUNOPS_DEFAULT;
1544 } 1638 }
1545} 1639}
1546 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
1547MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1733MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1548 1734
1549PROTOTYPES: DISABLE 1735PROTOTYPES: DISABLE
1550 1736
1551BOOT: 1737BOOT:
1556 BOOT_PAGESIZE; 1742 BOOT_PAGESIZE;
1557 1743
1558 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1744 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1559 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1745 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1560 1746
1561 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;
1562 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;
1563 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;
1564 PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1565 1750
1566 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); 1751 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1567 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));
1568 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));
1569 1754
1578 main_top_env = PL_top_env; 1763 main_top_env = PL_top_env;
1579 1764
1580 while (main_top_env->je_prev) 1765 while (main_top_env->je_prev)
1581 main_top_env = main_top_env->je_prev; 1766 main_top_env = main_top_env->je_prev;
1582 1767
1583 coroapi.ver = CORO_API_VERSION; 1768 coroapi.ver = CORO_API_VERSION;
1584 coroapi.rev = CORO_API_REVISION; 1769 coroapi.rev = CORO_API_REVISION;
1585 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 }
1586 1780
1587 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1781 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1588} 1782}
1589 1783
1590SV * 1784SV *
1661 } 1855 }
1662 SPAGAIN; 1856 SPAGAIN;
1663 1857
1664 BARRIER; 1858 BARRIER;
1665 PUTBACK; 1859 PUTBACK;
1666 TRANSFER (ta); 1860 TRANSFER (ta, 0);
1667 SPAGAIN; /* might be the sp of a different coroutine now */ 1861 SPAGAIN; /* might be the sp of a different coroutine now */
1668 /* be extra careful not to ever do anything after TRANSFER */ 1862 /* be extra careful not to ever do anything after TRANSFER */
1669} 1863}
1670 1864
1671bool 1865bool
1674 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1868 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1675 OUTPUT: 1869 OUTPUT:
1676 RETVAL 1870 RETVAL
1677 1871
1678void 1872void
1679_exit (code) 1873_exit (int code)
1680 int code
1681 PROTOTYPE: $ 1874 PROTOTYPE: $
1682 CODE: 1875 CODE:
1683 _exit (code); 1876 _exit (code);
1684 1877
1685int 1878int
1719call (Coro::State coro, SV *coderef) 1912call (Coro::State coro, SV *coderef)
1720 ALIAS: 1913 ALIAS:
1721 eval = 1 1914 eval = 1
1722 CODE: 1915 CODE:
1723{ 1916{
1724 if (coro->mainstack) 1917 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1725 { 1918 {
1726 struct coro temp; 1919 struct coro temp;
1727 1920
1728 if (!(coro->flags & CF_RUNNING)) 1921 if (!(coro->flags & CF_RUNNING))
1729 { 1922 {
1776 1969
1777void 1970void
1778api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1971api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1779 1972
1780SV * 1973SV *
1781has_stack (Coro::State coro) 1974has_cctx (Coro::State coro)
1782 PROTOTYPE: $ 1975 PROTOTYPE: $
1783 CODE: 1976 CODE:
1784 RETVAL = boolSV (!!coro->cctx); 1977 RETVAL = boolSV (!!coro->cctx);
1785 OUTPUT: 1978 OUTPUT:
1786 RETVAL 1979 RETVAL
1805 case 1: RETVAL = coro->usecount; break; 1998 case 1: RETVAL = coro->usecount; break;
1806 } 1999 }
1807 OUTPUT: 2000 OUTPUT:
1808 RETVAL 2001 RETVAL
1809 2002
1810
1811MODULE = Coro::State PACKAGE = Coro
1812
1813BOOT:
1814{
1815 int i;
1816
1817 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1818 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1819 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1820
1821 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1822 SvREADONLY_on (coro_current);
1823
1824 coro_stash = gv_stashpv ("Coro", TRUE);
1825
1826 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1827 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1828 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1829 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1830 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1831 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1832
1833 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1834 coro_ready[i] = newAV ();
1835
1836 {
1837 SV *sv = perl_get_sv ("Coro::API", TRUE);
1838 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1839
1840 coroapi.schedule = api_schedule;
1841 coroapi.cede = api_cede;
1842 coroapi.cede_notself = api_cede_notself;
1843 coroapi.ready = api_ready;
1844 coroapi.is_ready = api_is_ready;
1845 coroapi.nready = &coro_nready;
1846 coroapi.current = coro_current;
1847
1848 GCoroAPI = &coroapi;
1849 sv_setiv (sv, (IV)&coroapi);
1850 SvREADONLY_on (sv);
1851 }
1852}
1853
1854void 2003void
1855_set_current (SV *current) 2004force_cctx ()
1856 PROTOTYPE: $
1857 CODE: 2005 CODE:
1858 SvREFCNT_dec (SvRV (coro_current)); 2006 struct coro *coro = SvSTATE (coro_current);
1859 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2007 coro->cctx->idle_sp = 0;
1860
1861int
1862prio (Coro::State coro, int newprio = 0)
1863 ALIAS:
1864 nice = 1
1865 CODE:
1866{
1867 RETVAL = coro->prio;
1868
1869 if (items > 1)
1870 {
1871 if (ix)
1872 newprio = coro->prio - newprio;
1873
1874 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1875 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1876
1877 coro->prio = newprio;
1878 }
1879}
1880 OUTPUT:
1881 RETVAL
1882
1883SV *
1884ready (SV *self)
1885 PROTOTYPE: $
1886 CODE:
1887 RETVAL = boolSV (api_ready (self));
1888 OUTPUT:
1889 RETVAL
1890
1891int
1892nready (...)
1893 PROTOTYPE:
1894 CODE:
1895 RETVAL = coro_nready;
1896 OUTPUT:
1897 RETVAL
1898 2008
1899void 2009void
1900throw (Coro::State self, SV *throw = &PL_sv_undef) 2010throw (Coro::State self, SV *throw = &PL_sv_undef)
1901 PROTOTYPE: $;$ 2011 PROTOTYPE: $;$
1902 CODE: 2012 CODE:
1917 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2027 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
1918 2028
1919 SV *tmp = *src; *src = *dst; *dst = tmp; 2029 SV *tmp = *src; *src = *dst; *dst = tmp;
1920 } 2030 }
1921 2031
2032MODULE = Coro::State PACKAGE = Coro
2033
2034BOOT:
2035{
2036 int i;
2037
2038 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2039 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2040 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2041
2042 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2043 SvREADONLY_on (coro_current);
2044
2045 coro_stash = gv_stashpv ("Coro", TRUE);
2046
2047 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2048 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2049 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2050 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2051 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2052 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2053
2054 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2055 coro_ready[i] = newAV ();
2056
2057 {
2058 SV *sv = perl_get_sv ("Coro::API", TRUE);
2059 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2060
2061 coroapi.schedule = api_schedule;
2062 coroapi.cede = api_cede;
2063 coroapi.cede_notself = api_cede_notself;
2064 coroapi.ready = api_ready;
2065 coroapi.is_ready = api_is_ready;
2066 coroapi.nready = &coro_nready;
2067 coroapi.current = coro_current;
2068
2069 GCoroAPI = &coroapi;
2070 sv_setiv (sv, (IV)&coroapi);
2071 SvREADONLY_on (sv);
2072 }
2073}
2074
2075void
2076_set_current (SV *current)
2077 PROTOTYPE: $
2078 CODE:
2079 SvREFCNT_dec (SvRV (coro_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;
2090
2091int
2092prio (Coro::State coro, int newprio = 0)
2093 ALIAS:
2094 nice = 1
2095 CODE:
2096{
2097 RETVAL = coro->prio;
2098
2099 if (items > 1)
2100 {
2101 if (ix)
2102 newprio = coro->prio - newprio;
2103
2104 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2105 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2106
2107 coro->prio = newprio;
2108 }
2109}
2110 OUTPUT:
2111 RETVAL
2112
2113SV *
2114ready (SV *self)
2115 PROTOTYPE: $
2116 CODE:
2117 RETVAL = boolSV (api_ready (self));
2118 OUTPUT:
2119 RETVAL
2120
2121int
2122nready (...)
2123 PROTOTYPE:
2124 CODE:
2125 RETVAL = coro_nready;
2126 OUTPUT:
2127 RETVAL
2128
1922# for async_pool speedup 2129# for async_pool speedup
1923void 2130void
1924_pool_1 (SV *cb) 2131_pool_1 (SV *cb)
1925 CODE: 2132 CODE:
1926{ 2133{
1931 AV *invoke_av; 2138 AV *invoke_av;
1932 int i, len; 2139 int i, len;
1933 2140
1934 if (!invoke) 2141 if (!invoke)
1935 { 2142 {
1936 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2143 SV *old = PL_diehook;
2144 PL_diehook = 0;
2145 SvREFCNT_dec (old);
1937 croak ("\3async_pool terminate\2\n"); 2146 croak ("\3async_pool terminate\2\n");
1938 } 2147 }
1939 2148
1940 SvREFCNT_dec (coro->saved_deffh); 2149 SvREFCNT_dec (coro->saved_deffh);
1941 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2150 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1942 2151
1943 hv_store (hv, "desc", sizeof ("desc") - 1, 2152 hv_store (hv, "desc", sizeof ("desc") - 1,
1944 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2153 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1945 2154
1946 invoke_av = (AV *)SvRV (invoke); 2155 invoke_av = (AV *)SvRV (invoke);
1950 2159
1951 if (len > 0) 2160 if (len > 0)
1952 { 2161 {
1953 av_fill (defav, len - 1); 2162 av_fill (defav, len - 1);
1954 for (i = 0; i < len; ++i) 2163 for (i = 0; i < len; ++i)
1955 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2164 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1956 } 2165 }
1957 2166
1958 SvREFCNT_dec (invoke); 2167 SvREFCNT_dec (invoke);
1959} 2168}
1960 2169
1970 coro->saved_deffh = 0; 2179 coro->saved_deffh = 0;
1971 2180
1972 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2181 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1973 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2182 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1974 { 2183 {
1975 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2184 SV *old = PL_diehook;
2185 PL_diehook = 0;
2186 SvREFCNT_dec (old);
1976 croak ("\3async_pool terminate\2\n"); 2187 croak ("\3async_pool terminate\2\n");
1977 } 2188 }
1978 2189
1979 av_clear (GvAV (PL_defgv)); 2190 av_clear (GvAV (PL_defgv));
1980 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 2191 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1986 api_trace (coro_current, 0); 2197 api_trace (coro_current, 0);
1987 2198
1988 av_push (av_async_pool, newSVsv (coro_current)); 2199 av_push (av_async_pool, newSVsv (coro_current));
1989} 2200}
1990 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
1991 2250
1992MODULE = Coro::State PACKAGE = Coro::AIO 2251MODULE = Coro::State PACKAGE = Coro::AIO
1993 2252
1994SV * 2253void
1995_get_state () 2254_get_state (SV *self)
1996 CODE: 2255 PPCODE:
1997{ 2256{
1998 struct io_state *data; 2257 AV *defav = GvAV (PL_defgv);
1999 2258 AV *av = newAV ();
2259 int i;
2000 RETVAL = newSV (sizeof (struct io_state)); 2260 SV *data_sv = newSV (sizeof (struct io_state));
2001 data = (struct io_state *)SvPVX (RETVAL); 2261 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2002 SvCUR_set (RETVAL, sizeof (struct io_state)); 2262 SvCUR_set (data_sv, sizeof (struct io_state));
2003 SvPOK_only (RETVAL); 2263 SvPOK_only (data_sv);
2004 2264
2005 data->errorno = errno; 2265 data->errorno = errno;
2006 data->laststype = PL_laststype; 2266 data->laststype = PL_laststype;
2007 data->laststatval = PL_laststatval; 2267 data->laststatval = PL_laststatval;
2008 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);
2009} 2280}
2010 OUTPUT:
2011 RETVAL
2012 2281
2013void 2282void
2014_set_state (char *data_) 2283_set_state (SV *state)
2015 PROTOTYPE: $ 2284 PROTOTYPE: $
2016 CODE: 2285 PPCODE:
2017{ 2286{
2018 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;
2019 2290
2020 errno = data->errorno; 2291 errno = data->errorno;
2021 PL_laststype = data->laststype; 2292 PL_laststype = data->laststype;
2022 PL_laststatval = data->laststatval; 2293 PL_laststatval = data->laststatval;
2023 PL_statcache = data->statcache; 2294 PL_statcache = data->statcache;
2024}
2025 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