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.226 by root, Fri Apr 4 20:07:35 2008 UTC vs.
Revision 1.251 by root, Thu Oct 30 09:44:31 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.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)
139#endif 153#endif
140 154
141/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
142struct io_state 156struct 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};
164
165static double (*nvtime)(); /* so why doesn't it take void? */
149 166
150static size_t coro_stacksize = CORO_STACKSIZE; 167static size_t coro_stacksize = CORO_STACKSIZE;
151static struct CoroAPI coroapi; 168static struct CoroAPI coroapi;
152static AV *main_mainstack; /* used to differentiate between $main and others */ 169static AV *main_mainstack; /* used to differentiate between $main and others */
153static JMPENV *main_top_env; 170static JMPENV *main_top_env;
162 179
163/* async_pool helper stuff */ 180/* async_pool helper stuff */
164static SV *sv_pool_rss; 181static SV *sv_pool_rss;
165static SV *sv_pool_size; 182static SV *sv_pool_size;
166static AV *av_async_pool; 183static AV *av_async_pool;
184
185/* Coro::AnyEvent */
186static SV *sv_activity;
167 187
168static struct coro_cctx *cctx_first; 188static struct coro_cctx *cctx_first;
169static int cctx_count, cctx_idle; 189static int cctx_count, cctx_idle;
170 190
171enum { 191enum {
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 */
261static SV *coro_current; 281static SV *coro_current;
282static SV *coro_readyhook;
262static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 283static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
263static int coro_nready; 284static int coro_nready;
264static struct coro *coro_first; 285static struct coro *coro_first;
265 286
266/** lowlevel stuff **********************************************************/ 287/** lowlevel stuff **********************************************************/
267 288
268static SV * 289static SV *
269coro_get_sv (pTHX_ const char *name, int create) 290coro_get_sv (pTHX_ const char *name, int create)
270{ 291{
271#if PERL_VERSION_ATLEAST (5,9,0) 292#if PERL_VERSION_ATLEAST (5,10,0)
272 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 293 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
273 get_sv (name, create); 294 get_sv (name, create);
274#endif 295#endif
275 return get_sv (name, create); 296 return get_sv (name, create);
276} 297}
277 298
278static AV * 299static AV *
279coro_get_av (pTHX_ const char *name, int create) 300coro_get_av (pTHX_ const char *name, int create)
280{ 301{
281#if PERL_VERSION_ATLEAST (5,9,0) 302#if PERL_VERSION_ATLEAST (5,10,0)
282 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 303 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
283 get_av (name, create); 304 get_av (name, create);
284#endif 305#endif
285 return get_av (name, create); 306 return get_av (name, create);
286} 307}
287 308
288static HV * 309static HV *
289coro_get_hv (pTHX_ const char *name, int create) 310coro_get_hv (pTHX_ const char *name, int create)
290{ 311{
291#if PERL_VERSION_ATLEAST (5,9,0) 312#if PERL_VERSION_ATLEAST (5,10,0)
292 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 313 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
293 get_hv (name, create); 314 get_hv (name, create);
294#endif 315#endif
295 return get_hv (name, create); 316 return get_hv (name, create);
296} 317}
301 AV *padlist = CvPADLIST (cv); 322 AV *padlist = CvPADLIST (cv);
302 AV *newpadlist, *newpad; 323 AV *newpadlist, *newpad;
303 324
304 newpadlist = newAV (); 325 newpadlist = newAV ();
305 AvREAL_off (newpadlist); 326 AvREAL_off (newpadlist);
306#if PERL_VERSION_ATLEAST (5,9,0) 327#if PERL_VERSION_ATLEAST (5,10,0)
307 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 328 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
308#else 329#else
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
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 }
592 616
593 New(54,PL_savestack,24,ANY); 617 New(54,PL_savestack,24,ANY);
594 PL_savestack_ix = 0; 618 PL_savestack_ix = 0;
595 PL_savestack_max = 24; 619 PL_savestack_max = 24;
596 620
597#if !PERL_VERSION_ATLEAST (5,9,0) 621#if !PERL_VERSION_ATLEAST (5,10,0)
598 New(54,PL_retstack,4,OP*); 622 New(54,PL_retstack,4,OP*);
599 PL_retstack_ix = 0; 623 PL_retstack_ix = 0;
600 PL_retstack_max = 4; 624 PL_retstack_max = 4;
601#endif 625#endif
602} 626}
625 649
626 Safefree (PL_tmps_stack); 650 Safefree (PL_tmps_stack);
627 Safefree (PL_markstack); 651 Safefree (PL_markstack);
628 Safefree (PL_scopestack); 652 Safefree (PL_scopestack);
629 Safefree (PL_savestack); 653 Safefree (PL_savestack);
630#if !PERL_VERSION_ATLEAST (5,9,0) 654#if !PERL_VERSION_ATLEAST (5,10,0)
631 Safefree (PL_retstack); 655 Safefree (PL_retstack);
632#endif 656#endif
633} 657}
634 658
635static size_t 659static size_t
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,9,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
674static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 701static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
675static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 702static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
703static 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
743static int
744coro_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
700static int 767static int
701coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 768coro_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}
736 PL_curpm = 0; 803 PL_curpm = 0;
737 PL_curpad = 0; 804 PL_curpad = 0;
738 PL_localizing = 0; 805 PL_localizing = 0;
739 PL_dirty = 0; 806 PL_dirty = 0;
740 PL_restartop = 0; 807 PL_restartop = 0;
808#if PERL_VERSION_ATLEAST (5,10,0)
809 PL_parser = 0;
810#endif
741 811
742 /* recreate the die/warn hooks */ 812 /* recreate the die/warn hooks */
743 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 813 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); 814 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
745 815
746 GvSV (PL_defgv) = newSV (0); 816 GvSV (PL_defgv) = newSV (0);
747 GvAV (PL_defgv) = coro->args; coro->args = 0; 817 GvAV (PL_defgv) = coro->args; coro->args = 0;
748 GvSV (PL_errgv) = newSV (0); 818 GvSV (PL_errgv) = newSV (0);
749 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);
750 PL_rs = newSVsv (GvSV (irsgv)); 820 PL_rs = newSVsv (GvSV (irsgv));
751 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
752 822
753 { 823 {
754 dSP; 824 dSP;
755 LOGOP myop; 825 LOGOP myop;
756 826
852 : cx->blk_gimme == G_SCALAR ? bot + 1 922 : cx->blk_gimme == G_SCALAR ? bot + 1
853 : bot; 923 : bot;
854 924
855 av_extend (av, top - bot); 925 av_extend (av, top - bot);
856 while (bot < top) 926 while (bot < top)
857 av_push (av, SvREFCNT_inc (*bot++)); 927 av_push (av, SvREFCNT_inc_NN (*bot++));
858 928
859 PL_runops = RUNOPS_DEFAULT; 929 PL_runops = RUNOPS_DEFAULT;
860 ENTER; 930 ENTER;
861 SAVETMPS; 931 SAVETMPS;
862 EXTEND (SP, 3); 932 EXTEND (SP, 3);
1126 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1196 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
1127 1197
1128 if (expect_false (next->flags & CF_DESTROYED)) 1198 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"); 1199 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
1130 1200
1131 if (
1132#if PERL_VERSION_ATLEAST (5,9,0) 1201#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) 1202 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1136#endif
1137 )
1138 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1203 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version");
1204#endif
1139 } 1205 }
1140} 1206}
1141 1207
1142/* always use the TRANSFER macro */ 1208/* always use the TRANSFER macro */
1143static void NOINLINE 1209static void NOINLINE
1144transfer (pTHX_ struct coro *prev, struct coro *next) 1210transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1145{ 1211{
1146 dSTACKLEVEL; 1212 dSTACKLEVEL;
1147 static volatile int has_throw; 1213 static volatile int has_throw;
1148 1214
1149 /* sometimes transfer is only called to set idle_sp */ 1215 /* sometimes transfer is only called to set idle_sp */
1183 load_perl (aTHX_ next); 1249 load_perl (aTHX_ next);
1184 1250
1185 prev__cctx = prev->cctx; 1251 prev__cctx = prev->cctx;
1186 1252
1187 /* possibly "free" the cctx */ 1253 /* possibly "free" the cctx */
1188 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) 1254 if (expect_true (
1255 prev__cctx->idle_sp == STACKLEVEL
1256 && !(prev__cctx->flags & CC_TRACE)
1257 && !force_cctx
1258 ))
1189 { 1259 {
1190 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1260 /* 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)); 1261 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
1192 1262
1193 prev->cctx = 0; 1263 prev->cctx = 0;
1236struct transfer_args 1306struct transfer_args
1237{ 1307{
1238 struct coro *prev, *next; 1308 struct coro *prev, *next;
1239}; 1309};
1240 1310
1241#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1311#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) 1312#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1243 1313
1244/** high level stuff ********************************************************/ 1314/** high level stuff ********************************************************/
1245 1315
1246static int 1316static int
1340{ 1410{
1341 dTHX; 1411 dTHX;
1342 struct transfer_args ta; 1412 struct transfer_args ta;
1343 1413
1344 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1414 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1345 TRANSFER (ta); 1415 TRANSFER (ta, 1);
1346} 1416}
1347 1417
1348/** Coro ********************************************************************/ 1418/** Coro ********************************************************************/
1349 1419
1350static void 1420static void
1368static int 1438static int
1369api_ready (SV *coro_sv) 1439api_ready (SV *coro_sv)
1370{ 1440{
1371 dTHX; 1441 dTHX;
1372 struct coro *coro; 1442 struct coro *coro;
1443 SV *sv_hook;
1444 void (*xs_hook)(void);
1373 1445
1374 if (SvROK (coro_sv)) 1446 if (SvROK (coro_sv))
1375 coro_sv = SvRV (coro_sv); 1447 coro_sv = SvRV (coro_sv);
1376 1448
1377 coro = SvSTATE (coro_sv); 1449 coro = SvSTATE (coro_sv);
1380 return 0; 1452 return 0;
1381 1453
1382 coro->flags |= CF_READY; 1454 coro->flags |= CF_READY;
1383 1455
1384 LOCK; 1456 LOCK;
1457
1458 sv_hook = coro_nready ? 0 : coro_readyhook;
1459 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1460
1385 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1461 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1386 ++coro_nready; 1462 ++coro_nready;
1463
1387 UNLOCK; 1464 UNLOCK;
1465
1466 if (sv_hook)
1467 {
1468 dSP;
1469
1470 ENTER;
1471 SAVETMPS;
1472
1473 PUSHMARK (SP);
1474 PUTBACK;
1475 call_sv (sv_hook, G_DISCARD);
1476 SPAGAIN;
1477
1478 FREETMPS;
1479 LEAVE;
1480 }
1481
1482 if (xs_hook)
1483 xs_hook ();
1388 1484
1389 return 1; 1485 return 1;
1390} 1486}
1391 1487
1392static int 1488static int
1481{ 1577{
1482 dTHX; 1578 dTHX;
1483 struct transfer_args ta; 1579 struct transfer_args ta;
1484 1580
1485 prepare_schedule (aTHX_ &ta); 1581 prepare_schedule (aTHX_ &ta);
1486 TRANSFER (ta); 1582 TRANSFER (ta, 1);
1487} 1583}
1488 1584
1489static int 1585static int
1490api_cede (void) 1586api_cede (void)
1491{ 1587{
1494 1590
1495 prepare_cede (aTHX_ &ta); 1591 prepare_cede (aTHX_ &ta);
1496 1592
1497 if (expect_true (ta.prev != ta.next)) 1593 if (expect_true (ta.prev != ta.next))
1498 { 1594 {
1499 TRANSFER (ta); 1595 TRANSFER (ta, 1);
1500 return 1; 1596 return 1;
1501 } 1597 }
1502 else 1598 else
1503 return 0; 1599 return 0;
1504} 1600}
1509 dTHX; 1605 dTHX;
1510 struct transfer_args ta; 1606 struct transfer_args ta;
1511 1607
1512 if (prepare_cede_notself (aTHX_ &ta)) 1608 if (prepare_cede_notself (aTHX_ &ta))
1513 { 1609 {
1514 TRANSFER (ta); 1610 TRANSFER (ta, 1);
1515 return 1; 1611 return 1;
1516 } 1612 }
1517 else 1613 else
1518 return 0; 1614 return 0;
1519} 1615}
1542 else 1638 else
1543 coro->slot->runops = RUNOPS_DEFAULT; 1639 coro->slot->runops = RUNOPS_DEFAULT;
1544 } 1640 }
1545} 1641}
1546 1642
1643static int
1644coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1645{
1646 AV *padlist;
1647 AV *av = (AV *)mg->mg_obj;
1648
1649 abort ();
1650
1651 return 0;
1652}
1653
1654static MGVTBL coro_gensub_vtbl = {
1655 0, 0, 0, 0,
1656 coro_gensub_free
1657};
1658
1659/*****************************************************************************/
1660/* PerlIO::cede */
1661
1662typedef struct
1663{
1664 PerlIOBuf base;
1665 NV next, every;
1666} PerlIOCede;
1667
1668static IV
1669PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1670{
1671 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1672
1673 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1674 self->next = nvtime () + self->every;
1675
1676 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1677}
1678
1679static SV *
1680PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1681{
1682 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1683
1684 return newSVnv (self->every);
1685}
1686
1687static IV
1688PerlIOCede_flush (pTHX_ PerlIO *f)
1689{
1690 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1691 double now = nvtime ();
1692
1693 if (now >= self->next)
1694 {
1695 api_cede ();
1696 self->next = now + self->every;
1697 }
1698
1699 return PerlIOBuf_flush (aTHX_ f);
1700}
1701
1702static PerlIO_funcs PerlIO_cede =
1703{
1704 sizeof(PerlIO_funcs),
1705 "cede",
1706 sizeof(PerlIOCede),
1707 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1708 PerlIOCede_pushed,
1709 PerlIOBuf_popped,
1710 PerlIOBuf_open,
1711 PerlIOBase_binmode,
1712 PerlIOCede_getarg,
1713 PerlIOBase_fileno,
1714 PerlIOBuf_dup,
1715 PerlIOBuf_read,
1716 PerlIOBuf_unread,
1717 PerlIOBuf_write,
1718 PerlIOBuf_seek,
1719 PerlIOBuf_tell,
1720 PerlIOBuf_close,
1721 PerlIOCede_flush,
1722 PerlIOBuf_fill,
1723 PerlIOBase_eof,
1724 PerlIOBase_error,
1725 PerlIOBase_clearerr,
1726 PerlIOBase_setlinebuf,
1727 PerlIOBuf_get_base,
1728 PerlIOBuf_bufsiz,
1729 PerlIOBuf_get_ptr,
1730 PerlIOBuf_get_cnt,
1731 PerlIOBuf_set_ptrcnt,
1732};
1733
1734
1547MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1735MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1548 1736
1549PROTOTYPES: DISABLE 1737PROTOTYPES: DISABLE
1550 1738
1551BOOT: 1739BOOT:
1556 BOOT_PAGESIZE; 1744 BOOT_PAGESIZE;
1557 1745
1558 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1746 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1559 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1747 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1560 1748
1561 orig_sigelem_get = PL_vtbl_sigelem.svt_get; 1749 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; 1750 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; 1751 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 1752
1566 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); 1753 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1567 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 1754 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)); 1755 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1569 1756
1578 main_top_env = PL_top_env; 1765 main_top_env = PL_top_env;
1579 1766
1580 while (main_top_env->je_prev) 1767 while (main_top_env->je_prev)
1581 main_top_env = main_top_env->je_prev; 1768 main_top_env = main_top_env->je_prev;
1582 1769
1583 coroapi.ver = CORO_API_VERSION; 1770 coroapi.ver = CORO_API_VERSION;
1584 coroapi.rev = CORO_API_REVISION; 1771 coroapi.rev = CORO_API_REVISION;
1585 coroapi.transfer = api_transfer; 1772 coroapi.transfer = api_transfer;
1773
1774 {
1775 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1776
1777 if (!svp) croak ("Time::HiRes is required");
1778 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1779
1780 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1781 }
1586 1782
1587 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1783 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1588} 1784}
1589 1785
1590SV * 1786SV *
1661 } 1857 }
1662 SPAGAIN; 1858 SPAGAIN;
1663 1859
1664 BARRIER; 1860 BARRIER;
1665 PUTBACK; 1861 PUTBACK;
1666 TRANSFER (ta); 1862 TRANSFER (ta, 0);
1667 SPAGAIN; /* might be the sp of a different coroutine now */ 1863 SPAGAIN; /* might be the sp of a different coroutine now */
1668 /* be extra careful not to ever do anything after TRANSFER */ 1864 /* be extra careful not to ever do anything after TRANSFER */
1669} 1865}
1670 1866
1671bool 1867bool
1674 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1870 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1675 OUTPUT: 1871 OUTPUT:
1676 RETVAL 1872 RETVAL
1677 1873
1678void 1874void
1679_exit (code) 1875_exit (int code)
1680 int code
1681 PROTOTYPE: $ 1876 PROTOTYPE: $
1682 CODE: 1877 CODE:
1683 _exit (code); 1878 _exit (code);
1684 1879
1685int 1880int
1719call (Coro::State coro, SV *coderef) 1914call (Coro::State coro, SV *coderef)
1720 ALIAS: 1915 ALIAS:
1721 eval = 1 1916 eval = 1
1722 CODE: 1917 CODE:
1723{ 1918{
1724 if (coro->mainstack) 1919 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1725 { 1920 {
1726 struct coro temp; 1921 struct coro temp;
1727 1922
1728 if (!(coro->flags & CF_RUNNING)) 1923 if (!(coro->flags & CF_RUNNING))
1729 { 1924 {
1776 1971
1777void 1972void
1778api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1973api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1779 1974
1780SV * 1975SV *
1781has_stack (Coro::State coro) 1976has_cctx (Coro::State coro)
1782 PROTOTYPE: $ 1977 PROTOTYPE: $
1783 CODE: 1978 CODE:
1784 RETVAL = boolSV (!!coro->cctx); 1979 RETVAL = boolSV (!!coro->cctx);
1785 OUTPUT: 1980 OUTPUT:
1786 RETVAL 1981 RETVAL
1811force_cctx () 2006force_cctx ()
1812 CODE: 2007 CODE:
1813 struct coro *coro = SvSTATE (coro_current); 2008 struct coro *coro = SvSTATE (coro_current);
1814 coro->cctx->idle_sp = 0; 2009 coro->cctx->idle_sp = 0;
1815 2010
1816MODULE = Coro::State PACKAGE = Coro
1817
1818BOOT:
1819{
1820 int i;
1821
1822 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1823 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1824 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1825
1826 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1827 SvREADONLY_on (coro_current);
1828
1829 coro_stash = gv_stashpv ("Coro", TRUE);
1830
1831 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1832 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1833 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1834 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1835 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1836 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1837
1838 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1839 coro_ready[i] = newAV ();
1840
1841 {
1842 SV *sv = perl_get_sv ("Coro::API", TRUE);
1843 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1844
1845 coroapi.schedule = api_schedule;
1846 coroapi.cede = api_cede;
1847 coroapi.cede_notself = api_cede_notself;
1848 coroapi.ready = api_ready;
1849 coroapi.is_ready = api_is_ready;
1850 coroapi.nready = &coro_nready;
1851 coroapi.current = coro_current;
1852
1853 GCoroAPI = &coroapi;
1854 sv_setiv (sv, (IV)&coroapi);
1855 SvREADONLY_on (sv);
1856 }
1857}
1858
1859void
1860_set_current (SV *current)
1861 PROTOTYPE: $
1862 CODE:
1863 SvREFCNT_dec (SvRV (coro_current));
1864 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1865
1866int
1867prio (Coro::State coro, int newprio = 0)
1868 ALIAS:
1869 nice = 1
1870 CODE:
1871{
1872 RETVAL = coro->prio;
1873
1874 if (items > 1)
1875 {
1876 if (ix)
1877 newprio = coro->prio - newprio;
1878
1879 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1880 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1881
1882 coro->prio = newprio;
1883 }
1884}
1885 OUTPUT:
1886 RETVAL
1887
1888SV *
1889ready (SV *self)
1890 PROTOTYPE: $
1891 CODE:
1892 RETVAL = boolSV (api_ready (self));
1893 OUTPUT:
1894 RETVAL
1895
1896int
1897nready (...)
1898 PROTOTYPE:
1899 CODE:
1900 RETVAL = coro_nready;
1901 OUTPUT:
1902 RETVAL
1903
1904void 2011void
1905throw (Coro::State self, SV *throw = &PL_sv_undef) 2012throw (Coro::State self, SV *throw = &PL_sv_undef)
1906 PROTOTYPE: $;$ 2013 PROTOTYPE: $;$
1907 CODE: 2014 CODE:
1908 SvREFCNT_dec (self->throw); 2015 SvREFCNT_dec (self->throw);
1922 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2029 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
1923 2030
1924 SV *tmp = *src; *src = *dst; *dst = tmp; 2031 SV *tmp = *src; *src = *dst; *dst = tmp;
1925 } 2032 }
1926 2033
2034MODULE = Coro::State PACKAGE = Coro
2035
2036BOOT:
2037{
2038 int i;
2039
2040 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2041 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2042 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2043
2044 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2045 SvREADONLY_on (coro_current);
2046
2047 coro_stash = gv_stashpv ("Coro", TRUE);
2048
2049 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2050 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2051 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2052 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2053 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2054 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2055
2056 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2057 coro_ready[i] = newAV ();
2058
2059 {
2060 SV *sv = perl_get_sv ("Coro::API", TRUE);
2061 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2062
2063 coroapi.schedule = api_schedule;
2064 coroapi.cede = api_cede;
2065 coroapi.cede_notself = api_cede_notself;
2066 coroapi.ready = api_ready;
2067 coroapi.is_ready = api_is_ready;
2068 coroapi.nready = &coro_nready;
2069 coroapi.current = coro_current;
2070
2071 GCoroAPI = &coroapi;
2072 sv_setiv (sv, (IV)&coroapi);
2073 SvREADONLY_on (sv);
2074 }
2075}
2076
2077void
2078_set_current (SV *current)
2079 PROTOTYPE: $
2080 CODE:
2081 SvREFCNT_dec (SvRV (coro_current));
2082 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2083
2084void
2085_set_readyhook (SV *hook)
2086 PROTOTYPE: $
2087 CODE:
2088 LOCK;
2089 SvREFCNT_dec (coro_readyhook);
2090 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2091 UNLOCK;
2092
2093int
2094prio (Coro::State coro, int newprio = 0)
2095 ALIAS:
2096 nice = 1
2097 CODE:
2098{
2099 RETVAL = coro->prio;
2100
2101 if (items > 1)
2102 {
2103 if (ix)
2104 newprio = coro->prio - newprio;
2105
2106 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2107 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2108
2109 coro->prio = newprio;
2110 }
2111}
2112 OUTPUT:
2113 RETVAL
2114
2115SV *
2116ready (SV *self)
2117 PROTOTYPE: $
2118 CODE:
2119 RETVAL = boolSV (api_ready (self));
2120 OUTPUT:
2121 RETVAL
2122
2123int
2124nready (...)
2125 PROTOTYPE:
2126 CODE:
2127 RETVAL = coro_nready;
2128 OUTPUT:
2129 RETVAL
2130
1927# for async_pool speedup 2131# for async_pool speedup
1928void 2132void
1929_pool_1 (SV *cb) 2133_pool_1 (SV *cb)
1930 CODE: 2134 CODE:
1931{ 2135{
1936 AV *invoke_av; 2140 AV *invoke_av;
1937 int i, len; 2141 int i, len;
1938 2142
1939 if (!invoke) 2143 if (!invoke)
1940 { 2144 {
1941 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2145 SV *old = PL_diehook;
2146 PL_diehook = 0;
2147 SvREFCNT_dec (old);
1942 croak ("\3async_pool terminate\2\n"); 2148 croak ("\3async_pool terminate\2\n");
1943 } 2149 }
1944 2150
1945 SvREFCNT_dec (coro->saved_deffh); 2151 SvREFCNT_dec (coro->saved_deffh);
1946 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2152 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1947 2153
1948 hv_store (hv, "desc", sizeof ("desc") - 1, 2154 hv_store (hv, "desc", sizeof ("desc") - 1,
1949 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2155 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1950 2156
1951 invoke_av = (AV *)SvRV (invoke); 2157 invoke_av = (AV *)SvRV (invoke);
1955 2161
1956 if (len > 0) 2162 if (len > 0)
1957 { 2163 {
1958 av_fill (defav, len - 1); 2164 av_fill (defav, len - 1);
1959 for (i = 0; i < len; ++i) 2165 for (i = 0; i < len; ++i)
1960 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2166 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1961 } 2167 }
1962 2168
1963 SvREFCNT_dec (invoke); 2169 SvREFCNT_dec (invoke);
1964} 2170}
1965 2171
1975 coro->saved_deffh = 0; 2181 coro->saved_deffh = 0;
1976 2182
1977 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2183 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1978 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2184 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1979 { 2185 {
1980 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2186 SV *old = PL_diehook;
2187 PL_diehook = 0;
2188 SvREFCNT_dec (old);
1981 croak ("\3async_pool terminate\2\n"); 2189 croak ("\3async_pool terminate\2\n");
1982 } 2190 }
1983 2191
1984 av_clear (GvAV (PL_defgv)); 2192 av_clear (GvAV (PL_defgv));
1985 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 2193 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1991 api_trace (coro_current, 0); 2199 api_trace (coro_current, 0);
1992 2200
1993 av_push (av_async_pool, newSVsv (coro_current)); 2201 av_push (av_async_pool, newSVsv (coro_current));
1994} 2202}
1995 2203
2204#if 0
2205
2206void
2207_generator_call (...)
2208 PROTOTYPE: @
2209 PPCODE:
2210 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2211 xxxx
2212 abort ();
2213
2214SV *
2215gensub (SV *sub, ...)
2216 PROTOTYPE: &;@
2217 CODE:
2218{
2219 struct coro *coro;
2220 MAGIC *mg;
2221 CV *xcv;
2222 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2223 int i;
2224
2225 CvGV (ncv) = CvGV (cv);
2226 CvFILE (ncv) = CvFILE (cv);
2227
2228 Newz (0, coro, 1, struct coro);
2229 coro->args = newAV ();
2230 coro->flags = CF_NEW;
2231
2232 av_extend (coro->args, items - 1);
2233 for (i = 1; i < items; i++)
2234 av_push (coro->args, newSVsv (ST (i)));
2235
2236 CvISXSUB_on (ncv);
2237 CvXSUBANY (ncv).any_ptr = (void *)coro;
2238
2239 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2240
2241 CvXSUB (ncv) = CvXSUB (xcv);
2242 CvANON_on (ncv);
2243
2244 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2245 RETVAL = newRV_noinc ((SV *)ncv);
2246}
2247 OUTPUT:
2248 RETVAL
2249
2250#endif
2251
1996 2252
1997MODULE = Coro::State PACKAGE = Coro::AIO 2253MODULE = Coro::State PACKAGE = Coro::AIO
1998 2254
1999SV * 2255void
2000_get_state () 2256_get_state (SV *self)
2001 CODE: 2257 PPCODE:
2002{ 2258{
2003 struct io_state *data; 2259 AV *defav = GvAV (PL_defgv);
2004 2260 AV *av = newAV ();
2261 int i;
2005 RETVAL = newSV (sizeof (struct io_state)); 2262 SV *data_sv = newSV (sizeof (struct io_state));
2006 data = (struct io_state *)SvPVX (RETVAL); 2263 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2007 SvCUR_set (RETVAL, sizeof (struct io_state)); 2264 SvCUR_set (data_sv, sizeof (struct io_state));
2008 SvPOK_only (RETVAL); 2265 SvPOK_only (data_sv);
2009 2266
2010 data->errorno = errno; 2267 data->errorno = errno;
2011 data->laststype = PL_laststype; 2268 data->laststype = PL_laststype;
2012 data->laststatval = PL_laststatval; 2269 data->laststatval = PL_laststatval;
2013 data->statcache = PL_statcache; 2270 data->statcache = PL_statcache;
2271
2272 av_extend (av, AvFILLp (defav) + 1 + 1);
2273
2274 for (i = 0; i <= AvFILLp (defav); ++i)
2275 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2276
2277 av_push (av, data_sv);
2278
2279 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2280
2281 api_ready (self);
2014} 2282}
2015 OUTPUT:
2016 RETVAL
2017 2283
2018void 2284void
2019_set_state (char *data_) 2285_set_state (SV *state)
2020 PROTOTYPE: $ 2286 PROTOTYPE: $
2021 CODE: 2287 PPCODE:
2022{ 2288{
2023 struct io_state *data = (void *)data_; 2289 AV *av = (AV *)SvRV (state);
2290 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2291 int i;
2024 2292
2025 errno = data->errorno; 2293 errno = data->errorno;
2026 PL_laststype = data->laststype; 2294 PL_laststype = data->laststype;
2027 PL_laststatval = data->laststatval; 2295 PL_laststatval = data->laststatval;
2028 PL_statcache = data->statcache; 2296 PL_statcache = data->statcache;
2029}
2030 2297
2298 EXTEND (SP, AvFILLp (av));
2299 for (i = 0; i < AvFILLp (av); ++i)
2300 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2301}
2302
2303
2304MODULE = Coro::State PACKAGE = Coro::AnyEvent
2305
2306BOOT:
2307 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2308
2309SV *
2310_schedule (...)
2311 PROTOTYPE: @
2312 CODE:
2313{
2314 static int incede;
2315
2316 api_cede_notself ();
2317
2318 ++incede;
2319 while (coro_nready >= incede && api_cede ())
2320 ;
2321
2322 sv_setsv (sv_activity, &PL_sv_undef);
2323 if (coro_nready >= incede)
2324 {
2325 PUSHMARK (SP);
2326 PUTBACK;
2327 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2328 SPAGAIN;
2329 }
2330
2331 --incede;
2332}
2333
2334
2335MODULE = Coro::State PACKAGE = PerlIO::cede
2336
2337BOOT:
2338 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines