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.227 by root, Sat Apr 5 22:29:54 2008 UTC vs.
Revision 1.253 by root, Fri Nov 7 20:12:26 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)
128#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
129 143
130#include "CoroAPI.h" 144#include "CoroAPI.h"
131 145
132#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
133static perl_mutex coro_mutex; 147static perl_mutex coro_lock;
134# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
135# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
136#else 150#else
137# define LOCK (void)0 151# define LOCK (void)0
138# define UNLOCK (void)0 152# define UNLOCK (void)0
139#endif 153#endif
140 154
141/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
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};
149 164
165static double (*nvtime)(); /* so why doesn't it take void? */
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;
154static HV *coro_state_stash, *coro_stash; 171static HV *coro_state_stash, *coro_stash;
155static volatile SV *coro_mortal; /* will be freed after next transfer */ 172static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
156 173
157static GV *irsgv; /* $/ */ 174static GV *irsgv; /* $/ */
158static GV *stdoutgv; /* *STDOUT */ 175static GV *stdoutgv; /* *STDOUT */
159static SV *rv_diehook; 176static SV *rv_diehook;
160static SV *rv_warnhook; 177static SV *rv_warnhook;
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
360static MGVTBL coro_cv_vtbl = { 383static MGVTBL coro_cv_vtbl = {
361 0, 0, 0, 0, 384 0, 0, 0, 0,
362 coro_cv_free 385 coro_cv_free
363}; 386};
364 387
365#define CORO_MAGIC(sv,type) \ 388#define CORO_MAGIC(sv, type) \
366 SvMAGIC (sv) \ 389 SvMAGIC (sv) \
367 ? SvMAGIC (sv)->mg_type == type \ 390 ? SvMAGIC (sv)->mg_type == type \
368 ? SvMAGIC (sv) \ 391 ? SvMAGIC (sv) \
369 : mg_find (sv, type) \ 392 : mg_find (sv, type) \
370 : 0 393 : 0
371 394
372#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 395#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
373#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 396#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
374 397
375static struct coro * 398static struct coro *
408 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 431 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
409 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 432 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
410 else 433 else
411 { 434 {
412#if CORO_PREFER_PERL_FUNCTIONS 435#if CORO_PREFER_PERL_FUNCTIONS
413 /* this is probably cleaner, but also slower? */ 436 /* this is probably cleaner? but also slower! */
437 /* in practise, it seems to be less stable */
414 CV *cp = Perl_cv_clone (cv); 438 CV *cp = Perl_cv_clone (cv);
415 CvPADLIST (cv) = CvPADLIST (cp); 439 CvPADLIST (cv) = CvPADLIST (cp);
416 CvPADLIST (cp) = 0; 440 CvPADLIST (cp) = 0;
417 SvREFCNT_dec (cp); 441 SvREFCNT_dec (cp);
418#else 442#else
502 526
503 if (expect_true (CvDEPTH (cv))) 527 if (expect_true (CvDEPTH (cv)))
504 { 528 {
505 EXTEND (SP, 3); 529 EXTEND (SP, 3);
506 PUSHs ((SV *)CvPADLIST (cv)); 530 PUSHs ((SV *)CvPADLIST (cv));
507 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 531 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
508 PUSHs ((SV *)cv); 532 PUSHs ((SV *)cv);
509 533
510 CvDEPTH (cv) = 0; 534 CvDEPTH (cv) = 0;
511 get_padlist (aTHX_ cv); 535 get_padlist (aTHX_ cv);
512 } 536 }
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}
604 628
605/* 629/*
606 * destroy the stacks, the callchain etc... 630 * destroy the stacks, the callchain etc...
607 */ 631 */
608static void 632static void
609coro_destroy_stacks (pTHX) 633coro_destruct_stacks (pTHX)
610{ 634{
611 while (PL_curstackinfo->si_next) 635 while (PL_curstackinfo->si_next)
612 PL_curstackinfo = PL_curstackinfo->si_next; 636 PL_curstackinfo = PL_curstackinfo->si_next;
613 637
614 while (PL_curstackinfo) 638 while (PL_curstackinfo)
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
773 */ 843 */
774 ENTER; 844 ENTER;
775} 845}
776 846
777static void 847static void
778coro_destroy (pTHX_ struct coro *coro) 848coro_destruct (pTHX_ struct coro *coro)
779{ 849{
780 if (!IN_DESTRUCT) 850 if (!IN_DESTRUCT)
781 { 851 {
782 /* restore all saved variables and stuff */ 852 /* restore all saved variables and stuff */
783 LEAVE_SCOPE (0); 853 LEAVE_SCOPE (0);
805 SvREFCNT_dec (PL_warnhook); 875 SvREFCNT_dec (PL_warnhook);
806 876
807 SvREFCNT_dec (coro->saved_deffh); 877 SvREFCNT_dec (coro->saved_deffh);
808 SvREFCNT_dec (coro->throw); 878 SvREFCNT_dec (coro->throw);
809 879
810 coro_destroy_stacks (aTHX); 880 coro_destruct_stacks (aTHX);
811} 881}
812 882
813static void 883static void
814free_coro_mortal (pTHX) 884free_coro_mortal (pTHX)
815{ 885{
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);
1010 coro_cctx *cctx; 1080 coro_cctx *cctx;
1011 void *stack_start; 1081 void *stack_start;
1012 size_t stack_size; 1082 size_t stack_size;
1013 1083
1014 ++cctx_count; 1084 ++cctx_count;
1015
1016 Newz (0, cctx, 1, coro_cctx); 1085 Newz (0, cctx, 1, coro_cctx);
1017 1086
1018#if HAVE_MMAP 1087#if HAVE_MMAP
1019 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1088 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1020 /* mmap supposedly does allocate-on-write for us */ 1089 /* mmap supposedly does allocate-on-write for us */
1056{ 1125{
1057 if (!cctx) 1126 if (!cctx)
1058 return; 1127 return;
1059 1128
1060 --cctx_count; 1129 --cctx_count;
1130 coro_destroy (&cctx->cctx);
1061 1131
1132 /* coro_transfer creates new, empty cctx's */
1133 if (cctx->sptr)
1134 {
1062#if CORO_USE_VALGRIND 1135#if CORO_USE_VALGRIND
1063 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1136 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1064#endif 1137#endif
1065 1138
1066#if HAVE_MMAP 1139#if HAVE_MMAP
1067 if (cctx->flags & CC_MAPPED) 1140 if (cctx->flags & CC_MAPPED)
1068 munmap (cctx->sptr, cctx->ssize); 1141 munmap (cctx->sptr, cctx->ssize);
1069 else 1142 else
1070#endif 1143#endif
1071 Safefree (cctx->sptr); 1144 Safefree (cctx->sptr);
1145 }
1072 1146
1073 Safefree (cctx); 1147 Safefree (cctx);
1074} 1148}
1075 1149
1076/* wether this cctx should be destructed */ 1150/* wether this cctx should be destructed */
1095} 1169}
1096 1170
1097static void 1171static void
1098cctx_put (coro_cctx *cctx) 1172cctx_put (coro_cctx *cctx)
1099{ 1173{
1174 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1175
1100 /* free another cctx if overlimit */ 1176 /* free another cctx if overlimit */
1101 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1177 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
1102 { 1178 {
1103 coro_cctx *first = cctx_first; 1179 coro_cctx *first = cctx_first;
1104 cctx_first = first->next; 1180 cctx_first = first->next;
1126 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1202 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
1127 1203
1128 if (expect_false (next->flags & CF_DESTROYED)) 1204 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"); 1205 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
1130 1206
1131 if (
1132#if PERL_VERSION_ATLEAST (5,9,0) 1207#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) 1208 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1136#endif
1137 )
1138 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1209 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version");
1210#endif
1139 } 1211 }
1140} 1212}
1141 1213
1142/* always use the TRANSFER macro */ 1214/* always use the TRANSFER macro */
1143static void NOINLINE 1215static void NOINLINE
1144transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1216transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1145{ 1217{
1146 dSTACKLEVEL; 1218 dSTACKLEVEL;
1147 static volatile int has_throw;
1148 1219
1149 /* sometimes transfer is only called to set idle_sp */ 1220 /* sometimes transfer is only called to set idle_sp */
1150 if (expect_false (!next)) 1221 if (expect_false (!next))
1151 { 1222 {
1152 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1223 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1153 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1224 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1154 } 1225 }
1155 else if (expect_true (prev != next)) 1226 else if (expect_true (prev != next))
1156 { 1227 {
1228 static volatile int has_throw;
1157 coro_cctx *prev__cctx; 1229 coro_cctx *prev__cctx;
1158 1230
1159 if (expect_false (prev->flags & CF_NEW)) 1231 if (expect_false (prev->flags & CF_NEW))
1160 { 1232 {
1161 /* create a new empty context */ 1233 /* create a new empty/source context */
1234 ++cctx_count;
1162 Newz (0, prev->cctx, 1, coro_cctx); 1235 New (0, prev->cctx, 1, coro_cctx);
1236 prev->cctx->sptr = 0;
1237 coro_create (&prev->cctx->cctx, 0, 0, 0, 0);
1238
1163 prev->flags &= ~CF_NEW; 1239 prev->flags &= ~CF_NEW;
1164 prev->flags |= CF_RUNNING; 1240 prev->flags |= CF_RUNNING;
1165 } 1241 }
1166 1242
1167 prev->flags &= ~CF_RUNNING; 1243 prev->flags &= ~CF_RUNNING;
1274 croak ("FATAL: tried to destroy currently running coroutine"); 1350 croak ("FATAL: tried to destroy currently running coroutine");
1275 1351
1276 save_perl (aTHX_ &temp); 1352 save_perl (aTHX_ &temp);
1277 load_perl (aTHX_ coro); 1353 load_perl (aTHX_ coro);
1278 1354
1279 coro_destroy (aTHX_ coro); 1355 coro_destruct (aTHX_ coro);
1280 1356
1281 load_perl (aTHX_ &temp); 1357 load_perl (aTHX_ &temp);
1282 1358
1283 coro->slot = 0; 1359 coro->slot = 0;
1284 } 1360 }
1372static int 1448static int
1373api_ready (SV *coro_sv) 1449api_ready (SV *coro_sv)
1374{ 1450{
1375 dTHX; 1451 dTHX;
1376 struct coro *coro; 1452 struct coro *coro;
1453 SV *sv_hook;
1454 void (*xs_hook)(void);
1377 1455
1378 if (SvROK (coro_sv)) 1456 if (SvROK (coro_sv))
1379 coro_sv = SvRV (coro_sv); 1457 coro_sv = SvRV (coro_sv);
1380 1458
1381 coro = SvSTATE (coro_sv); 1459 coro = SvSTATE (coro_sv);
1384 return 0; 1462 return 0;
1385 1463
1386 coro->flags |= CF_READY; 1464 coro->flags |= CF_READY;
1387 1465
1388 LOCK; 1466 LOCK;
1467
1468 sv_hook = coro_nready ? 0 : coro_readyhook;
1469 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1470
1389 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1471 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1390 ++coro_nready; 1472 ++coro_nready;
1473
1391 UNLOCK; 1474 UNLOCK;
1475
1476 if (sv_hook)
1477 {
1478 dSP;
1479
1480 ENTER;
1481 SAVETMPS;
1482
1483 PUSHMARK (SP);
1484 PUTBACK;
1485 call_sv (sv_hook, G_DISCARD);
1486 SPAGAIN;
1487
1488 FREETMPS;
1489 LEAVE;
1490 }
1491
1492 if (xs_hook)
1493 xs_hook ();
1392 1494
1393 return 1; 1495 return 1;
1394} 1496}
1395 1497
1396static int 1498static int
1546 else 1648 else
1547 coro->slot->runops = RUNOPS_DEFAULT; 1649 coro->slot->runops = RUNOPS_DEFAULT;
1548 } 1650 }
1549} 1651}
1550 1652
1653static int
1654coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1655{
1656 AV *padlist;
1657 AV *av = (AV *)mg->mg_obj;
1658
1659 abort ();
1660
1661 return 0;
1662}
1663
1664static MGVTBL coro_gensub_vtbl = {
1665 0, 0, 0, 0,
1666 coro_gensub_free
1667};
1668
1669/*****************************************************************************/
1670/* PerlIO::cede */
1671
1672typedef struct
1673{
1674 PerlIOBuf base;
1675 NV next, every;
1676} PerlIOCede;
1677
1678static IV
1679PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1680{
1681 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1682
1683 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1684 self->next = nvtime () + self->every;
1685
1686 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1687}
1688
1689static SV *
1690PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1691{
1692 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1693
1694 return newSVnv (self->every);
1695}
1696
1697static IV
1698PerlIOCede_flush (pTHX_ PerlIO *f)
1699{
1700 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1701 double now = nvtime ();
1702
1703 if (now >= self->next)
1704 {
1705 api_cede ();
1706 self->next = now + self->every;
1707 }
1708
1709 return PerlIOBuf_flush (aTHX_ f);
1710}
1711
1712static PerlIO_funcs PerlIO_cede =
1713{
1714 sizeof(PerlIO_funcs),
1715 "cede",
1716 sizeof(PerlIOCede),
1717 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1718 PerlIOCede_pushed,
1719 PerlIOBuf_popped,
1720 PerlIOBuf_open,
1721 PerlIOBase_binmode,
1722 PerlIOCede_getarg,
1723 PerlIOBase_fileno,
1724 PerlIOBuf_dup,
1725 PerlIOBuf_read,
1726 PerlIOBuf_unread,
1727 PerlIOBuf_write,
1728 PerlIOBuf_seek,
1729 PerlIOBuf_tell,
1730 PerlIOBuf_close,
1731 PerlIOCede_flush,
1732 PerlIOBuf_fill,
1733 PerlIOBase_eof,
1734 PerlIOBase_error,
1735 PerlIOBase_clearerr,
1736 PerlIOBase_setlinebuf,
1737 PerlIOBuf_get_base,
1738 PerlIOBuf_bufsiz,
1739 PerlIOBuf_get_ptr,
1740 PerlIOBuf_get_cnt,
1741 PerlIOBuf_set_ptrcnt,
1742};
1743
1744
1551MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1745MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1552 1746
1553PROTOTYPES: DISABLE 1747PROTOTYPES: DISABLE
1554 1748
1555BOOT: 1749BOOT:
1556{ 1750{
1557#ifdef USE_ITHREADS 1751#ifdef USE_ITHREADS
1558 MUTEX_INIT (&coro_mutex); 1752 MUTEX_INIT (&coro_lock);
1559#endif 1753#endif
1560 BOOT_PAGESIZE; 1754 BOOT_PAGESIZE;
1561 1755
1562 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1756 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1563 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1757 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1564 1758
1565 orig_sigelem_get = PL_vtbl_sigelem.svt_get; 1759 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1566 PL_vtbl_sigelem.svt_get = coro_sigelem_get; 1760 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1567 orig_sigelem_set = PL_vtbl_sigelem.svt_set; 1761 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1568 PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1569 1762
1570 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); 1763 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1571 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 1764 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1572 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); 1765 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1573 1766
1582 main_top_env = PL_top_env; 1775 main_top_env = PL_top_env;
1583 1776
1584 while (main_top_env->je_prev) 1777 while (main_top_env->je_prev)
1585 main_top_env = main_top_env->je_prev; 1778 main_top_env = main_top_env->je_prev;
1586 1779
1587 coroapi.ver = CORO_API_VERSION; 1780 coroapi.ver = CORO_API_VERSION;
1588 coroapi.rev = CORO_API_REVISION; 1781 coroapi.rev = CORO_API_REVISION;
1589 coroapi.transfer = api_transfer; 1782 coroapi.transfer = api_transfer;
1783
1784 {
1785 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1786
1787 if (!svp) croak ("Time::HiRes is required");
1788 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1789
1790 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1791 }
1590 1792
1591 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1793 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1592} 1794}
1593 1795
1594SV * 1796SV *
1642 ta.next = 0; 1844 ta.next = 0;
1643 break; 1845 break;
1644 1846
1645 case 1: 1847 case 1:
1646 if (items != 2) 1848 if (items != 2)
1647 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1849 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1648 1850
1649 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1851 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1650 break; 1852 break;
1651 1853
1652 case 2: 1854 case 2:
1678 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1880 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1679 OUTPUT: 1881 OUTPUT:
1680 RETVAL 1882 RETVAL
1681 1883
1682void 1884void
1683_exit (code) 1885_exit (int code)
1684 int code
1685 PROTOTYPE: $ 1886 PROTOTYPE: $
1686 CODE: 1887 CODE:
1687 _exit (code); 1888 _exit (code);
1688 1889
1689int 1890int
1723call (Coro::State coro, SV *coderef) 1924call (Coro::State coro, SV *coderef)
1724 ALIAS: 1925 ALIAS:
1725 eval = 1 1926 eval = 1
1726 CODE: 1927 CODE:
1727{ 1928{
1728 if (coro->mainstack) 1929 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1729 { 1930 {
1730 struct coro temp; 1931 struct coro temp;
1731 1932
1732 if (!(coro->flags & CF_RUNNING)) 1933 if (!(coro->flags & CF_RUNNING))
1733 { 1934 {
1780 1981
1781void 1982void
1782api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1983api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1783 1984
1784SV * 1985SV *
1785has_stack (Coro::State coro) 1986has_cctx (Coro::State coro)
1786 PROTOTYPE: $ 1987 PROTOTYPE: $
1787 CODE: 1988 CODE:
1788 RETVAL = boolSV (!!coro->cctx); 1989 RETVAL = boolSV (!!coro->cctx);
1789 OUTPUT: 1990 OUTPUT:
1790 RETVAL 1991 RETVAL
1815force_cctx () 2016force_cctx ()
1816 CODE: 2017 CODE:
1817 struct coro *coro = SvSTATE (coro_current); 2018 struct coro *coro = SvSTATE (coro_current);
1818 coro->cctx->idle_sp = 0; 2019 coro->cctx->idle_sp = 0;
1819 2020
1820MODULE = Coro::State PACKAGE = Coro
1821
1822BOOT:
1823{
1824 int i;
1825
1826 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1827 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1828 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1829
1830 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1831 SvREADONLY_on (coro_current);
1832
1833 coro_stash = gv_stashpv ("Coro", TRUE);
1834
1835 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1836 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1837 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1838 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1839 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1840 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1841
1842 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1843 coro_ready[i] = newAV ();
1844
1845 {
1846 SV *sv = perl_get_sv ("Coro::API", TRUE);
1847 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1848
1849 coroapi.schedule = api_schedule;
1850 coroapi.cede = api_cede;
1851 coroapi.cede_notself = api_cede_notself;
1852 coroapi.ready = api_ready;
1853 coroapi.is_ready = api_is_ready;
1854 coroapi.nready = &coro_nready;
1855 coroapi.current = coro_current;
1856
1857 GCoroAPI = &coroapi;
1858 sv_setiv (sv, (IV)&coroapi);
1859 SvREADONLY_on (sv);
1860 }
1861}
1862
1863void
1864_set_current (SV *current)
1865 PROTOTYPE: $
1866 CODE:
1867 SvREFCNT_dec (SvRV (coro_current));
1868 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1869
1870int
1871prio (Coro::State coro, int newprio = 0)
1872 ALIAS:
1873 nice = 1
1874 CODE:
1875{
1876 RETVAL = coro->prio;
1877
1878 if (items > 1)
1879 {
1880 if (ix)
1881 newprio = coro->prio - newprio;
1882
1883 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1884 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1885
1886 coro->prio = newprio;
1887 }
1888}
1889 OUTPUT:
1890 RETVAL
1891
1892SV *
1893ready (SV *self)
1894 PROTOTYPE: $
1895 CODE:
1896 RETVAL = boolSV (api_ready (self));
1897 OUTPUT:
1898 RETVAL
1899
1900int
1901nready (...)
1902 PROTOTYPE:
1903 CODE:
1904 RETVAL = coro_nready;
1905 OUTPUT:
1906 RETVAL
1907
1908void
1909throw (Coro::State self, SV *throw = &PL_sv_undef)
1910 PROTOTYPE: $;$
1911 CODE:
1912 SvREFCNT_dec (self->throw);
1913 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1914
1915void 2021void
1916swap_defsv (Coro::State self) 2022swap_defsv (Coro::State self)
1917 PROTOTYPE: $ 2023 PROTOTYPE: $
1918 ALIAS: 2024 ALIAS:
1919 swap_defav = 1 2025 swap_defav = 1
1926 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2032 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
1927 2033
1928 SV *tmp = *src; *src = *dst; *dst = tmp; 2034 SV *tmp = *src; *src = *dst; *dst = tmp;
1929 } 2035 }
1930 2036
2037MODULE = Coro::State PACKAGE = Coro
2038
2039BOOT:
2040{
2041 int i;
2042
2043 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2044 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2045 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2046
2047 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2048 SvREADONLY_on (coro_current);
2049
2050 coro_stash = gv_stashpv ("Coro", TRUE);
2051
2052 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2053 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2054 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2055 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2056 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2057 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2058
2059 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2060 coro_ready[i] = newAV ();
2061
2062 {
2063 SV *sv = perl_get_sv ("Coro::API", TRUE);
2064 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2065
2066 coroapi.schedule = api_schedule;
2067 coroapi.cede = api_cede;
2068 coroapi.cede_notself = api_cede_notself;
2069 coroapi.ready = api_ready;
2070 coroapi.is_ready = api_is_ready;
2071 coroapi.nready = &coro_nready;
2072 coroapi.current = coro_current;
2073
2074 GCoroAPI = &coroapi;
2075 sv_setiv (sv, (IV)&coroapi);
2076 SvREADONLY_on (sv);
2077 }
2078}
2079
2080void
2081_set_current (SV *current)
2082 PROTOTYPE: $
2083 CODE:
2084 SvREFCNT_dec (SvRV (coro_current));
2085 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2086
2087void
2088_set_readyhook (SV *hook)
2089 PROTOTYPE: $
2090 CODE:
2091 LOCK;
2092 SvREFCNT_dec (coro_readyhook);
2093 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2094 UNLOCK;
2095
2096int
2097prio (Coro::State coro, int newprio = 0)
2098 ALIAS:
2099 nice = 1
2100 CODE:
2101{
2102 RETVAL = coro->prio;
2103
2104 if (items > 1)
2105 {
2106 if (ix)
2107 newprio = coro->prio - newprio;
2108
2109 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2110 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2111
2112 coro->prio = newprio;
2113 }
2114}
2115 OUTPUT:
2116 RETVAL
2117
2118SV *
2119ready (SV *self)
2120 PROTOTYPE: $
2121 CODE:
2122 RETVAL = boolSV (api_ready (self));
2123 OUTPUT:
2124 RETVAL
2125
2126int
2127nready (...)
2128 PROTOTYPE:
2129 CODE:
2130 RETVAL = coro_nready;
2131 OUTPUT:
2132 RETVAL
2133
2134void
2135throw (Coro::State self, SV *throw = &PL_sv_undef)
2136 PROTOTYPE: $;$
2137 CODE:
2138 SvREFCNT_dec (self->throw);
2139 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2140
1931# for async_pool speedup 2141# for async_pool speedup
1932void 2142void
1933_pool_1 (SV *cb) 2143_pool_1 (SV *cb)
1934 CODE: 2144 CODE:
1935{ 2145{
1940 AV *invoke_av; 2150 AV *invoke_av;
1941 int i, len; 2151 int i, len;
1942 2152
1943 if (!invoke) 2153 if (!invoke)
1944 { 2154 {
1945 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2155 SV *old = PL_diehook;
2156 PL_diehook = 0;
2157 SvREFCNT_dec (old);
1946 croak ("\3async_pool terminate\2\n"); 2158 croak ("\3async_pool terminate\2\n");
1947 } 2159 }
1948 2160
1949 SvREFCNT_dec (coro->saved_deffh); 2161 SvREFCNT_dec (coro->saved_deffh);
1950 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2162 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1951 2163
1952 hv_store (hv, "desc", sizeof ("desc") - 1, 2164 hv_store (hv, "desc", sizeof ("desc") - 1,
1953 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2165 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1954 2166
1955 invoke_av = (AV *)SvRV (invoke); 2167 invoke_av = (AV *)SvRV (invoke);
1959 2171
1960 if (len > 0) 2172 if (len > 0)
1961 { 2173 {
1962 av_fill (defav, len - 1); 2174 av_fill (defav, len - 1);
1963 for (i = 0; i < len; ++i) 2175 for (i = 0; i < len; ++i)
1964 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2176 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1965 } 2177 }
1966 2178
1967 SvREFCNT_dec (invoke); 2179 SvREFCNT_dec (invoke);
1968} 2180}
1969 2181
1979 coro->saved_deffh = 0; 2191 coro->saved_deffh = 0;
1980 2192
1981 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2193 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1982 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2194 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1983 { 2195 {
1984 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2196 SV *old = PL_diehook;
2197 PL_diehook = 0;
2198 SvREFCNT_dec (old);
1985 croak ("\3async_pool terminate\2\n"); 2199 croak ("\3async_pool terminate\2\n");
1986 } 2200 }
1987 2201
1988 av_clear (GvAV (PL_defgv)); 2202 av_clear (GvAV (PL_defgv));
1989 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 2203 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1995 api_trace (coro_current, 0); 2209 api_trace (coro_current, 0);
1996 2210
1997 av_push (av_async_pool, newSVsv (coro_current)); 2211 av_push (av_async_pool, newSVsv (coro_current));
1998} 2212}
1999 2213
2214#if 0
2215
2216void
2217_generator_call (...)
2218 PROTOTYPE: @
2219 PPCODE:
2220 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2221 xxxx
2222 abort ();
2223
2224SV *
2225gensub (SV *sub, ...)
2226 PROTOTYPE: &;@
2227 CODE:
2228{
2229 struct coro *coro;
2230 MAGIC *mg;
2231 CV *xcv;
2232 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2233 int i;
2234
2235 CvGV (ncv) = CvGV (cv);
2236 CvFILE (ncv) = CvFILE (cv);
2237
2238 Newz (0, coro, 1, struct coro);
2239 coro->args = newAV ();
2240 coro->flags = CF_NEW;
2241
2242 av_extend (coro->args, items - 1);
2243 for (i = 1; i < items; i++)
2244 av_push (coro->args, newSVsv (ST (i)));
2245
2246 CvISXSUB_on (ncv);
2247 CvXSUBANY (ncv).any_ptr = (void *)coro;
2248
2249 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2250
2251 CvXSUB (ncv) = CvXSUB (xcv);
2252 CvANON_on (ncv);
2253
2254 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2255 RETVAL = newRV_noinc ((SV *)ncv);
2256}
2257 OUTPUT:
2258 RETVAL
2259
2260#endif
2261
2000 2262
2001MODULE = Coro::State PACKAGE = Coro::AIO 2263MODULE = Coro::State PACKAGE = Coro::AIO
2002 2264
2003SV * 2265void
2004_get_state () 2266_get_state (SV *self)
2005 CODE: 2267 PPCODE:
2006{ 2268{
2007 struct io_state *data; 2269 AV *defav = GvAV (PL_defgv);
2008 2270 AV *av = newAV ();
2271 int i;
2009 RETVAL = newSV (sizeof (struct io_state)); 2272 SV *data_sv = newSV (sizeof (struct io_state));
2010 data = (struct io_state *)SvPVX (RETVAL); 2273 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2011 SvCUR_set (RETVAL, sizeof (struct io_state)); 2274 SvCUR_set (data_sv, sizeof (struct io_state));
2012 SvPOK_only (RETVAL); 2275 SvPOK_only (data_sv);
2013 2276
2014 data->errorno = errno; 2277 data->errorno = errno;
2015 data->laststype = PL_laststype; 2278 data->laststype = PL_laststype;
2016 data->laststatval = PL_laststatval; 2279 data->laststatval = PL_laststatval;
2017 data->statcache = PL_statcache; 2280 data->statcache = PL_statcache;
2281
2282 av_extend (av, AvFILLp (defav) + 1 + 1);
2283
2284 for (i = 0; i <= AvFILLp (defav); ++i)
2285 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2286
2287 av_push (av, data_sv);
2288
2289 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2290
2291 api_ready (self);
2018} 2292}
2019 OUTPUT:
2020 RETVAL
2021 2293
2022void 2294void
2023_set_state (char *data_) 2295_set_state (SV *state)
2024 PROTOTYPE: $ 2296 PROTOTYPE: $
2025 CODE: 2297 PPCODE:
2026{ 2298{
2027 struct io_state *data = (void *)data_; 2299 AV *av = (AV *)SvRV (state);
2300 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2301 int i;
2028 2302
2029 errno = data->errorno; 2303 errno = data->errorno;
2030 PL_laststype = data->laststype; 2304 PL_laststype = data->laststype;
2031 PL_laststatval = data->laststatval; 2305 PL_laststatval = data->laststatval;
2032 PL_statcache = data->statcache; 2306 PL_statcache = data->statcache;
2033}
2034 2307
2308 EXTEND (SP, AvFILLp (av));
2309 for (i = 0; i < AvFILLp (av); ++i)
2310 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2311}
2312
2313
2314MODULE = Coro::State PACKAGE = Coro::AnyEvent
2315
2316BOOT:
2317 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2318
2319SV *
2320_schedule (...)
2321 PROTOTYPE: @
2322 CODE:
2323{
2324 static int incede;
2325
2326 api_cede_notself ();
2327
2328 ++incede;
2329 while (coro_nready >= incede && api_cede ())
2330 ;
2331
2332 sv_setsv (sv_activity, &PL_sv_undef);
2333 if (coro_nready >= incede)
2334 {
2335 PUSHMARK (SP);
2336 PUTBACK;
2337 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2338 SPAGAIN;
2339 }
2340
2341 --incede;
2342}
2343
2344
2345MODULE = Coro::State PACKAGE = PerlIO::cede
2346
2347BOOT:
2348 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines