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.263 by root, Wed Nov 12 04:49:06 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
37# define BOOT_PAGESIZE (void)0 46# define BOOT_PAGESIZE (void)0
38#endif 47#endif
39 48
40#if CORO_USE_VALGRIND 49#if CORO_USE_VALGRIND
41# include <valgrind/valgrind.h> 50# include <valgrind/valgrind.h>
42# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
43#else
44# define REGISTER_STACK(cctx,start,end)
45#endif 51#endif
46 52
47/* the maximum number of idle cctx that will be pooled */ 53/* the maximum number of idle cctx that will be pooled */
48#define MAX_IDLE_CCTX 8 54static int cctx_max_idle = 4;
49 55
50#define PERL_VERSION_ATLEAST(a,b,c) \ 56#define PERL_VERSION_ATLEAST(a,b,c) \
51 (PERL_REVISION > (a) \ 57 (PERL_REVISION > (a) \
52 || (PERL_REVISION == (a) \ 58 || (PERL_REVISION == (a) \
53 && (PERL_VERSION > (b) \ 59 && (PERL_VERSION > (b) \
72# ifndef IS_PADCONST 78# ifndef IS_PADCONST
73# define IS_PADCONST(v) 0 79# define IS_PADCONST(v) 0
74# endif 80# endif
75#endif 81#endif
76 82
83/* 5.11 */
84#ifndef CxHASARGS
85# define CxHASARGS(cx) (cx)->blk_sub.hasargs
86#endif
87
88/* 5.10.0 */
89#ifndef SvREFCNT_inc_NN
90# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
91#endif
92
77/* 5.8.8 */ 93/* 5.8.8 */
78#ifndef GV_NOTQUAL 94#ifndef GV_NOTQUAL
79# define GV_NOTQUAL 0 95# define GV_NOTQUAL 0
80#endif 96#endif
81#ifndef newSV 97#ifndef newSV
82# define newSV(l) NEWSV(0,l) 98# 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 99#endif
89 100
90/* 5.8.7 */ 101/* 5.8.7 */
91#ifndef SvRV_set 102#ifndef SvRV_set
92# define SvRV_set(s,v) SvRV(s) = (v) 103# define SvRV_set(s,v) SvRV(s) = (v)
114 125
115#if __GNUC__ >= 3 126#if __GNUC__ >= 3
116# define attribute(x) __attribute__(x) 127# define attribute(x) __attribute__(x)
117# define BARRIER __asm__ __volatile__ ("" : : : "memory") 128# define BARRIER __asm__ __volatile__ ("" : : : "memory")
118# define expect(expr,value) __builtin_expect ((expr),(value)) 129# define expect(expr,value) __builtin_expect ((expr),(value))
130# define INLINE static inline
119#else 131#else
120# define attribute(x) 132# define attribute(x)
121# define BARRIER 133# define BARRIER
122# define expect(expr,value) (expr) 134# define expect(expr,value) (expr)
135# define INLINE static
123#endif 136#endif
124 137
125#define expect_false(expr) expect ((expr) != 0, 0) 138#define expect_false(expr) expect ((expr) != 0, 0)
126#define expect_true(expr) expect ((expr) != 0, 1) 139#define expect_true(expr) expect ((expr) != 0, 1)
127 140
128#define NOINLINE attribute ((noinline)) 141#define NOINLINE attribute ((noinline))
129 142
130#include "CoroAPI.h" 143#include "CoroAPI.h"
131 144
132#ifdef USE_ITHREADS 145#ifdef USE_ITHREADS
146
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)
150# if CORO_PTHREAD
151static void *coro_thx;
152# endif
153
136#else 154#else
155
137# define LOCK (void)0 156# define LOCK (void)0
138# define UNLOCK (void)0 157# define UNLOCK (void)0
158
139#endif 159#endif
160
161# undef LOCK
162# define LOCK (void)0
163# undef UNLOCK
164# define UNLOCK (void)0
140 165
141/* helper storage struct for Coro::AIO */ 166/* helper storage struct for Coro::AIO */
142struct io_state 167struct io_state
143{ 168{
169 AV *res;
144 int errorno; 170 int errorno;
145 I32 laststype; 171 I32 laststype; /* U16 in 5.10.0 */
146 int laststatval; 172 int laststatval;
147 Stat_t statcache; 173 Stat_t statcache;
148}; 174};
149 175
176static double (*nvtime)(); /* so why doesn't it take void? */
177
178static U32 cctx_gen;
150static size_t coro_stacksize = CORO_STACKSIZE; 179static size_t cctx_stacksize = CORO_STACKSIZE;
151static struct CoroAPI coroapi; 180static struct CoroAPI coroapi;
152static AV *main_mainstack; /* used to differentiate between $main and others */ 181static AV *main_mainstack; /* used to differentiate between $main and others */
153static JMPENV *main_top_env; 182static JMPENV *main_top_env;
154static HV *coro_state_stash, *coro_stash; 183static HV *coro_state_stash, *coro_stash;
155static volatile SV *coro_mortal; /* will be freed after next transfer */ 184static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
185static volatile struct coro *transfer_next;
186
187struct transfer_args
188{
189 struct coro *prev, *next;
190};
156 191
157static GV *irsgv; /* $/ */ 192static GV *irsgv; /* $/ */
158static GV *stdoutgv; /* *STDOUT */ 193static GV *stdoutgv; /* *STDOUT */
159static SV *rv_diehook; 194static SV *rv_diehook;
160static SV *rv_warnhook; 195static SV *rv_warnhook;
162 197
163/* async_pool helper stuff */ 198/* async_pool helper stuff */
164static SV *sv_pool_rss; 199static SV *sv_pool_rss;
165static SV *sv_pool_size; 200static SV *sv_pool_size;
166static AV *av_async_pool; 201static AV *av_async_pool;
202
203/* Coro::AnyEvent */
204static SV *sv_activity;
167 205
168static struct coro_cctx *cctx_first; 206static struct coro_cctx *cctx_first;
169static int cctx_count, cctx_idle; 207static int cctx_count, cctx_idle;
170 208
171enum { 209enum {
189 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 227 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
190 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 228 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
191 JMPENV *top_env; 229 JMPENV *top_env;
192 coro_context cctx; 230 coro_context cctx;
193 231
232 U32 gen;
194#if CORO_USE_VALGRIND 233#if CORO_USE_VALGRIND
195 int valgrind_id; 234 int valgrind_id;
196#endif 235#endif
197 unsigned char flags; 236 unsigned char flags;
198} coro_cctx; 237} coro_cctx;
257#define PRIO_IDLE -3 296#define PRIO_IDLE -3
258#define PRIO_MIN -4 297#define PRIO_MIN -4
259 298
260/* for Coro.pm */ 299/* for Coro.pm */
261static SV *coro_current; 300static SV *coro_current;
301static SV *coro_readyhook;
262static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 302static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
263static int coro_nready; 303static int coro_nready;
264static struct coro *coro_first; 304static struct coro *coro_first;
265 305
266/** lowlevel stuff **********************************************************/ 306/** lowlevel stuff **********************************************************/
267 307
268static SV * 308static SV *
269coro_get_sv (pTHX_ const char *name, int create) 309coro_get_sv (pTHX_ const char *name, int create)
270{ 310{
271#if PERL_VERSION_ATLEAST (5,9,0) 311#if PERL_VERSION_ATLEAST (5,10,0)
272 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 312 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
273 get_sv (name, create); 313 get_sv (name, create);
274#endif 314#endif
275 return get_sv (name, create); 315 return get_sv (name, create);
276} 316}
277 317
278static AV * 318static AV *
279coro_get_av (pTHX_ const char *name, int create) 319coro_get_av (pTHX_ const char *name, int create)
280{ 320{
281#if PERL_VERSION_ATLEAST (5,9,0) 321#if PERL_VERSION_ATLEAST (5,10,0)
282 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 322 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
283 get_av (name, create); 323 get_av (name, create);
284#endif 324#endif
285 return get_av (name, create); 325 return get_av (name, create);
286} 326}
287 327
288static HV * 328static HV *
289coro_get_hv (pTHX_ const char *name, int create) 329coro_get_hv (pTHX_ const char *name, int create)
290{ 330{
291#if PERL_VERSION_ATLEAST (5,9,0) 331#if PERL_VERSION_ATLEAST (5,10,0)
292 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 332 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
293 get_hv (name, create); 333 get_hv (name, create);
294#endif 334#endif
295 return get_hv (name, create); 335 return get_hv (name, create);
296} 336}
301 AV *padlist = CvPADLIST (cv); 341 AV *padlist = CvPADLIST (cv);
302 AV *newpadlist, *newpad; 342 AV *newpadlist, *newpad;
303 343
304 newpadlist = newAV (); 344 newpadlist = newAV ();
305 AvREAL_off (newpadlist); 345 AvREAL_off (newpadlist);
306#if PERL_VERSION_ATLEAST (5,9,0) 346#if PERL_VERSION_ATLEAST (5,10,0)
307 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 347 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
308#else 348#else
309 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 349 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
310#endif 350#endif
311 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 351 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
312 --AvFILLp (padlist); 352 --AvFILLp (padlist);
313 353
314 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 354 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
315 av_store (newpadlist, 1, (SV *)newpad); 355 av_store (newpadlist, 1, (SV *)newpad);
316 356
317 return newpadlist; 357 return newpadlist;
318} 358}
319 359
349 389
350 /* casting is fun. */ 390 /* casting is fun. */
351 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 391 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
352 free_padlist (aTHX_ padlist); 392 free_padlist (aTHX_ padlist);
353 393
394 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
395
354 return 0; 396 return 0;
355} 397}
356 398
357#define CORO_MAGIC_type_cv PERL_MAGIC_ext 399#define CORO_MAGIC_type_cv PERL_MAGIC_ext
358#define CORO_MAGIC_type_state PERL_MAGIC_ext 400#define CORO_MAGIC_type_state PERL_MAGIC_ext
360static MGVTBL coro_cv_vtbl = { 402static MGVTBL coro_cv_vtbl = {
361 0, 0, 0, 0, 403 0, 0, 0, 0,
362 coro_cv_free 404 coro_cv_free
363}; 405};
364 406
365#define CORO_MAGIC(sv,type) \ 407#define CORO_MAGIC(sv, type) \
366 SvMAGIC (sv) \ 408 SvMAGIC (sv) \
367 ? SvMAGIC (sv)->mg_type == type \ 409 ? SvMAGIC (sv)->mg_type == type \
368 ? SvMAGIC (sv) \ 410 ? SvMAGIC (sv) \
369 : mg_find (sv, type) \ 411 : mg_find (sv, type) \
370 : 0 412 : 0
371 413
372#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 414#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) 415#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
374 416
375static struct coro * 417INLINE struct coro *
376SvSTATE_ (pTHX_ SV *coro) 418SvSTATE_ (pTHX_ SV *coro)
377{ 419{
378 HV *stash; 420 HV *stash;
379 MAGIC *mg; 421 MAGIC *mg;
380 422
408 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 450 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
409 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 451 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
410 else 452 else
411 { 453 {
412#if CORO_PREFER_PERL_FUNCTIONS 454#if CORO_PREFER_PERL_FUNCTIONS
413 /* this is probably cleaner, but also slower? */ 455 /* this is probably cleaner? but also slower! */
456 /* in practise, it seems to be less stable */
414 CV *cp = Perl_cv_clone (cv); 457 CV *cp = Perl_cv_clone (cv);
415 CvPADLIST (cv) = CvPADLIST (cp); 458 CvPADLIST (cv) = CvPADLIST (cp);
416 CvPADLIST (cp) = 0; 459 CvPADLIST (cp) = 0;
417 SvREFCNT_dec (cp); 460 SvREFCNT_dec (cp);
418#else 461#else
502 545
503 if (expect_true (CvDEPTH (cv))) 546 if (expect_true (CvDEPTH (cv)))
504 { 547 {
505 EXTEND (SP, 3); 548 EXTEND (SP, 3);
506 PUSHs ((SV *)CvPADLIST (cv)); 549 PUSHs ((SV *)CvPADLIST (cv));
507 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 550 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
508 PUSHs ((SV *)cv); 551 PUSHs ((SV *)cv);
509 552
510 CvDEPTH (cv) = 0; 553 CvDEPTH (cv) = 0;
511 get_padlist (aTHX_ cv); 554 get_padlist (aTHX_ cv);
512 } 555 }
592 635
593 New(54,PL_savestack,24,ANY); 636 New(54,PL_savestack,24,ANY);
594 PL_savestack_ix = 0; 637 PL_savestack_ix = 0;
595 PL_savestack_max = 24; 638 PL_savestack_max = 24;
596 639
597#if !PERL_VERSION_ATLEAST (5,9,0) 640#if !PERL_VERSION_ATLEAST (5,10,0)
598 New(54,PL_retstack,4,OP*); 641 New(54,PL_retstack,4,OP*);
599 PL_retstack_ix = 0; 642 PL_retstack_ix = 0;
600 PL_retstack_max = 4; 643 PL_retstack_max = 4;
601#endif 644#endif
602} 645}
604 647
605/* 648/*
606 * destroy the stacks, the callchain etc... 649 * destroy the stacks, the callchain etc...
607 */ 650 */
608static void 651static void
609coro_destroy_stacks (pTHX) 652coro_destruct_stacks (pTHX)
610{ 653{
611 while (PL_curstackinfo->si_next) 654 while (PL_curstackinfo->si_next)
612 PL_curstackinfo = PL_curstackinfo->si_next; 655 PL_curstackinfo = PL_curstackinfo->si_next;
613 656
614 while (PL_curstackinfo) 657 while (PL_curstackinfo)
625 668
626 Safefree (PL_tmps_stack); 669 Safefree (PL_tmps_stack);
627 Safefree (PL_markstack); 670 Safefree (PL_markstack);
628 Safefree (PL_scopestack); 671 Safefree (PL_scopestack);
629 Safefree (PL_savestack); 672 Safefree (PL_savestack);
630#if !PERL_VERSION_ATLEAST (5,9,0) 673#if !PERL_VERSION_ATLEAST (5,10,0)
631 Safefree (PL_retstack); 674 Safefree (PL_retstack);
632#endif 675#endif
633} 676}
634 677
635static size_t 678static size_t
651 #undef VAR 694 #undef VAR
652 } 695 }
653 else 696 else
654 slot = coro->slot; 697 slot = coro->slot;
655 698
699 if (slot)
700 {
656 rss += sizeof (slot->curstackinfo); 701 rss += sizeof (slot->curstackinfo);
657 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 702 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
658 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 703 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
659 rss += slot->tmps_max * sizeof (SV *); 704 rss += slot->tmps_max * sizeof (SV *);
660 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 705 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
661 rss += slot->scopestack_max * sizeof (I32); 706 rss += slot->scopestack_max * sizeof (I32);
662 rss += slot->savestack_max * sizeof (ANY); 707 rss += slot->savestack_max * sizeof (ANY);
663 708
664#if !PERL_VERSION_ATLEAST (5,9,0) 709#if !PERL_VERSION_ATLEAST (5,10,0)
665 rss += slot->retstack_max * sizeof (OP *); 710 rss += slot->retstack_max * sizeof (OP *);
666#endif 711#endif
712 }
667 } 713 }
668 714
669 return rss; 715 return rss;
670} 716}
671 717
672/** coroutine stack handling ************************************************/ 718/** coroutine stack handling ************************************************/
673 719
674static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 720static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
675static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 721static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
722static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
723
724/* apparently < 5.8.8 */
725#ifndef MgPV_nolen_const
726#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
727 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
728 (const char*)(mg)->mg_ptr)
729#endif
730
731/* we sometimes need to create the effect of entersub calling us */
732#define ENTERSUB_HEAD ENTER; SAVETMPS
733/* we somtimes need to create the effect of leaving via entersub */
734#define ENTERSUB_TAIL LEAVE
676 735
677/* 736/*
678 * This overrides the default magic get method of %SIG elements. 737 * 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 738 * 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 739 * and instead of tryign to save and restore the hash elements, we just provide
688{ 747{
689 const char *s = MgPV_nolen_const (mg); 748 const char *s = MgPV_nolen_const (mg);
690 749
691 if (*s == '_') 750 if (*s == '_')
692 { 751 {
693 if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; 752 SV **svp = 0;
694 if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; 753
754 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
755 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
756
757 if (svp)
758 {
759 sv_setsv (sv, *svp ? *svp : &PL_sv_undef);
760 return 0;
761 }
695 } 762 }
696 763
697 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 764 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
765}
766
767static int
768coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
769{
770 const char *s = MgPV_nolen_const (mg);
771
772 if (*s == '_')
773 {
774 SV **svp = 0;
775
776 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
777 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
778
779 if (svp)
780 {
781 SV *old = *svp;
782 *svp = 0;
783 SvREFCNT_dec (old);
784 return 0;
785 }
786 }
787
788 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
698} 789}
699 790
700static int 791static int
701coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 792coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
702{ 793{
712 if (svp) 803 if (svp)
713 { 804 {
714 SV *old = *svp; 805 SV *old = *svp;
715 *svp = newSVsv (sv); 806 *svp = newSVsv (sv);
716 SvREFCNT_dec (old); 807 SvREFCNT_dec (old);
717 return; 808 return 0;
718 } 809 }
719 } 810 }
720 811
721 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 812 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
722} 813}
736 PL_curpm = 0; 827 PL_curpm = 0;
737 PL_curpad = 0; 828 PL_curpad = 0;
738 PL_localizing = 0; 829 PL_localizing = 0;
739 PL_dirty = 0; 830 PL_dirty = 0;
740 PL_restartop = 0; 831 PL_restartop = 0;
832#if PERL_VERSION_ATLEAST (5,10,0)
833 PL_parser = 0;
834#endif
741 835
742 /* recreate the die/warn hooks */ 836 /* recreate the die/warn hooks */
743 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 837 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); 838 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
745 839
746 GvSV (PL_defgv) = newSV (0); 840 GvSV (PL_defgv) = newSV (0);
747 GvAV (PL_defgv) = coro->args; coro->args = 0; 841 GvAV (PL_defgv) = coro->args; coro->args = 0;
748 GvSV (PL_errgv) = newSV (0); 842 GvSV (PL_errgv) = newSV (0);
749 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 843 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
750 PL_rs = newSVsv (GvSV (irsgv)); 844 PL_rs = newSVsv (GvSV (irsgv));
751 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 845 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
752 846
753 { 847 {
754 dSP; 848 dSP;
755 LOGOP myop; 849 UNOP myop;
756 850
757 Zero (&myop, 1, LOGOP); 851 Zero (&myop, 1, UNOP);
758 myop.op_next = Nullop; 852 myop.op_next = Nullop;
759 myop.op_flags = OPf_WANT_VOID; 853 myop.op_flags = OPf_WANT_VOID;
760 854
761 PUSHMARK (SP); 855 PUSHMARK (SP);
762 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 856 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
766 SPAGAIN; 860 SPAGAIN;
767 } 861 }
768 862
769 /* this newly created coroutine might be run on an existing cctx which most 863 /* this newly created coroutine might be run on an existing cctx which most
770 * likely was suspended in set_stacklevel, called from entersub. 864 * likely was suspended in set_stacklevel, called from entersub.
771 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 865 * set_stacklevel doesn't do anything on return, but entersub does LEAVE,
772 * so we ENTER here for symmetry 866 * so we ENTER here for symmetry.
773 */ 867 */
774 ENTER; 868 ENTERSUB_HEAD;
775} 869}
776 870
777static void 871static void
778coro_destroy (pTHX_ struct coro *coro) 872coro_destruct (pTHX_ struct coro *coro)
779{ 873{
780 if (!IN_DESTRUCT) 874 if (!IN_DESTRUCT)
781 { 875 {
782 /* restore all saved variables and stuff */ 876 /* restore all saved variables and stuff */
783 LEAVE_SCOPE (0); 877 LEAVE_SCOPE (0);
805 SvREFCNT_dec (PL_warnhook); 899 SvREFCNT_dec (PL_warnhook);
806 900
807 SvREFCNT_dec (coro->saved_deffh); 901 SvREFCNT_dec (coro->saved_deffh);
808 SvREFCNT_dec (coro->throw); 902 SvREFCNT_dec (coro->throw);
809 903
810 coro_destroy_stacks (aTHX); 904 coro_destruct_stacks (aTHX);
811} 905}
812 906
813static void 907INLINE void
814free_coro_mortal (pTHX) 908free_coro_mortal (pTHX)
815{ 909{
816 if (expect_true (coro_mortal)) 910 if (expect_true (coro_mortal))
817 { 911 {
818 SvREFCNT_dec (coro_mortal); 912 SvREFCNT_dec (coro_mortal);
852 : cx->blk_gimme == G_SCALAR ? bot + 1 946 : cx->blk_gimme == G_SCALAR ? bot + 1
853 : bot; 947 : bot;
854 948
855 av_extend (av, top - bot); 949 av_extend (av, top - bot);
856 while (bot < top) 950 while (bot < top)
857 av_push (av, SvREFCNT_inc (*bot++)); 951 av_push (av, SvREFCNT_inc_NN (*bot++));
858 952
859 PL_runops = RUNOPS_DEFAULT; 953 PL_runops = RUNOPS_DEFAULT;
860 ENTER; 954 ENTER;
861 SAVETMPS; 955 SAVETMPS;
862 EXTEND (SP, 3); 956 EXTEND (SP, 3);
942 1036
943 TAINT_NOT; 1037 TAINT_NOT;
944 return 0; 1038 return 0;
945} 1039}
946 1040
1041static void
1042prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1043{
1044 ta->prev = (struct coro *)cctx;
1045 ta->next = 0;
1046}
1047
947/* inject a fake call to Coro::State::_cctx_init into the execution */ 1048/* inject a fake call to Coro::State::_cctx_init into the execution */
948/* _cctx_init should be careful, as it could be called at almost any time */ 1049/* _cctx_init should be careful, as it could be called at almost any time */
949/* during execution of a perl program */ 1050/* during execution of a perl program */
1051/* also initialises PL_top_env */
950static void NOINLINE 1052static void NOINLINE
951cctx_prepare (pTHX_ coro_cctx *cctx) 1053cctx_prepare (pTHX_ coro_cctx *cctx)
952{ 1054{
953 dSP; 1055 dSP;
954 LOGOP myop; 1056 UNOP myop;
955 1057
956 PL_top_env = &PL_start_env; 1058 PL_top_env = &PL_start_env;
957 1059
958 if (cctx->flags & CC_TRACE) 1060 if (cctx->flags & CC_TRACE)
959 PL_runops = runops_trace; 1061 PL_runops = runops_trace;
960 1062
961 Zero (&myop, 1, LOGOP); 1063 Zero (&myop, 1, UNOP);
962 myop.op_next = PL_op; 1064 myop.op_next = PL_op;
963 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1065 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
964 1066
965 PUSHMARK (SP); 1067 PUSHMARK (SP);
966 EXTEND (SP, 2); 1068 EXTEND (SP, 2);
967 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1069 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
968 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1070 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
969 PUTBACK; 1071 PUTBACK;
970 PL_op = (OP *)&myop; 1072 PL_op = (OP *)&myop;
971 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1073 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
972 SPAGAIN; 1074 SPAGAIN;
973} 1075}
974 1076
1077/* the tail of transfer: execute stuff we can only do after a transfer */
1078INLINE void
1079transfer_tail (pTHX)
1080{
1081 struct coro *next = (struct coro *)transfer_next;
1082 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1083 assert (("FATAL: transfer_next was zero in transfer_tail (please report)", next));
1084
1085 free_coro_mortal (aTHX);
1086 UNLOCK;
1087
1088 if (expect_false (next->throw))
1089 {
1090 SV *exception = sv_2mortal (next->throw);
1091
1092 next->throw = 0;
1093 sv_setsv (ERRSV, exception);
1094 croak (0);
1095 }
1096}
1097
975/* 1098/*
976 * this is a _very_ stripped down perl interpreter ;) 1099 * this is a _very_ stripped down perl interpreter ;)
977 */ 1100 */
978static void 1101static void
979cctx_run (void *arg) 1102cctx_run (void *arg)
980{ 1103{
1104#ifdef USE_ITHREADS
1105# if CORO_PTHREAD
1106 PERL_SET_CONTEXT (coro_thx);
1107# endif
1108#endif
1109 {
981 dTHX; 1110 dTHX;
982 1111
983 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1112 /* entersub called ENTER, but we never 'returned', undo that here */
984 UNLOCK; 1113 ENTERSUB_TAIL;
985 1114
986 /* we now skip the entersub that lead to transfer() */ 1115 /* we now skip the entersub that did lead to transfer() */
987 PL_op = PL_op->op_next; 1116 PL_op = PL_op->op_next;
988 1117
989 /* inject a fake subroutine call to cctx_init */ 1118 /* inject a fake subroutine call to cctx_init */
990 cctx_prepare (aTHX_ (coro_cctx *)arg); 1119 cctx_prepare (aTHX_ (coro_cctx *)arg);
991 1120
1121 /* cctx_run is the alternative tail of transfer() */
1122 transfer_tail (aTHX);
1123
992 /* somebody or something will hit me for both perl_run and PL_restartop */ 1124 /* somebody or something will hit me for both perl_run and PL_restartop */
993 PL_restartop = PL_op; 1125 PL_restartop = PL_op;
994 perl_run (PL_curinterp); 1126 perl_run (PL_curinterp);
995 1127
996 /* 1128 /*
997 * If perl-run returns we assume exit() was being called or the coro 1129 * If perl-run returns we assume exit() was being called or the coro
998 * fell off the end, which seems to be the only valid (non-bug) 1130 * fell off the end, which seems to be the only valid (non-bug)
999 * reason for perl_run to return. We try to exit by jumping to the 1131 * reason for perl_run to return. We try to exit by jumping to the
1000 * bootstrap-time "top" top_env, as we cannot restore the "main" 1132 * bootstrap-time "top" top_env, as we cannot restore the "main"
1001 * coroutine as Coro has no such concept 1133 * coroutine as Coro has no such concept
1002 */ 1134 */
1003 PL_top_env = main_top_env; 1135 PL_top_env = main_top_env;
1004 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1136 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1137 }
1005} 1138}
1006 1139
1007static coro_cctx * 1140static coro_cctx *
1008cctx_new () 1141cctx_new ()
1009{ 1142{
1010 coro_cctx *cctx; 1143 coro_cctx *cctx;
1144
1145 ++cctx_count;
1146 New (0, cctx, 1, coro_cctx);
1147
1148 cctx->gen = cctx_gen;
1149 cctx->flags = 0;
1150 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1151
1152 return cctx;
1153}
1154
1155/* create a new cctx only suitable as source */
1156static coro_cctx *
1157cctx_new_empty ()
1158{
1159 coro_cctx *cctx = cctx_new ();
1160
1161 cctx->sptr = 0;
1162 coro_create (&cctx->cctx, 0, 0, 0, 0);
1163
1164 return cctx;
1165}
1166
1167/* create a new cctx suitable as destination/running a perl interpreter */
1168static coro_cctx *
1169cctx_new_run ()
1170{
1171 coro_cctx *cctx = cctx_new ();
1011 void *stack_start; 1172 void *stack_start;
1012 size_t stack_size; 1173 size_t stack_size;
1013 1174
1014 ++cctx_count;
1015
1016 Newz (0, cctx, 1, coro_cctx);
1017
1018#if HAVE_MMAP 1175#if HAVE_MMAP
1019 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1176 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1020 /* mmap supposedly does allocate-on-write for us */ 1177 /* mmap supposedly does allocate-on-write for us */
1021 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1178 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1022 1179
1023 if (cctx->sptr != (void *)-1) 1180 if (cctx->sptr != (void *)-1)
1024 { 1181 {
1025# if CORO_STACKGUARD 1182 #if CORO_STACKGUARD
1026 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1183 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1027# endif 1184 #endif
1028 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1185 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1029 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1186 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1030 cctx->flags |= CC_MAPPED; 1187 cctx->flags |= CC_MAPPED;
1031 } 1188 }
1032 else 1189 else
1033#endif 1190#endif
1034 { 1191 {
1035 cctx->ssize = coro_stacksize * (long)sizeof (long); 1192 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1036 New (0, cctx->sptr, coro_stacksize, long); 1193 New (0, cctx->sptr, cctx_stacksize, long);
1037 1194
1038 if (!cctx->sptr) 1195 if (!cctx->sptr)
1039 { 1196 {
1040 perror ("FATAL: unable to allocate stack for coroutine"); 1197 perror ("FATAL: unable to allocate stack for coroutine");
1041 _exit (EXIT_FAILURE); 1198 _exit (EXIT_FAILURE);
1043 1200
1044 stack_start = cctx->sptr; 1201 stack_start = cctx->sptr;
1045 stack_size = cctx->ssize; 1202 stack_size = cctx->ssize;
1046 } 1203 }
1047 1204
1048 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1205 #if CORO_USE_VALGRIND
1206 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1207 #endif
1208
1049 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1209 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1050 1210
1051 return cctx; 1211 return cctx;
1052} 1212}
1053 1213
1056{ 1216{
1057 if (!cctx) 1217 if (!cctx)
1058 return; 1218 return;
1059 1219
1060 --cctx_count; 1220 --cctx_count;
1221 coro_destroy (&cctx->cctx);
1061 1222
1223 /* coro_transfer creates new, empty cctx's */
1224 if (cctx->sptr)
1225 {
1062#if CORO_USE_VALGRIND 1226 #if CORO_USE_VALGRIND
1063 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1227 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1064#endif 1228 #endif
1065 1229
1066#if HAVE_MMAP 1230#if HAVE_MMAP
1067 if (cctx->flags & CC_MAPPED) 1231 if (cctx->flags & CC_MAPPED)
1068 munmap (cctx->sptr, cctx->ssize); 1232 munmap (cctx->sptr, cctx->ssize);
1069 else 1233 else
1070#endif 1234#endif
1071 Safefree (cctx->sptr); 1235 Safefree (cctx->sptr);
1236 }
1072 1237
1073 Safefree (cctx); 1238 Safefree (cctx);
1074} 1239}
1075 1240
1076/* wether this cctx should be destructed */ 1241/* wether this cctx should be destructed */
1077#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1242#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1078 1243
1079static coro_cctx * 1244static coro_cctx *
1080cctx_get (pTHX) 1245cctx_get (pTHX)
1081{ 1246{
1082 while (expect_true (cctx_first)) 1247 while (expect_true (cctx_first))
1089 return cctx; 1254 return cctx;
1090 1255
1091 cctx_destroy (cctx); 1256 cctx_destroy (cctx);
1092 } 1257 }
1093 1258
1094 return cctx_new (); 1259 return cctx_new_run ();
1095} 1260}
1096 1261
1097static void 1262static void
1098cctx_put (coro_cctx *cctx) 1263cctx_put (coro_cctx *cctx)
1099{ 1264{
1265 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1266
1100 /* free another cctx if overlimit */ 1267 /* free another cctx if overlimit */
1101 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1268 if (expect_false (cctx_idle >= cctx_max_idle))
1102 { 1269 {
1103 coro_cctx *first = cctx_first; 1270 coro_cctx *first = cctx_first;
1104 cctx_first = first->next; 1271 cctx_first = first->next;
1105 --cctx_idle; 1272 --cctx_idle;
1106 1273
1126 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1293 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
1127 1294
1128 if (expect_false (next->flags & CF_DESTROYED)) 1295 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"); 1296 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
1130 1297
1131 if (
1132#if PERL_VERSION_ATLEAST (5,9,0) 1298#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) 1299 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1136#endif
1137 )
1138 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1300 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version");
1301#endif
1139 } 1302 }
1140} 1303}
1141 1304
1142/* always use the TRANSFER macro */ 1305/* always use the TRANSFER macro */
1143static void NOINLINE 1306static void NOINLINE
1144transfer (pTHX_ struct coro *prev, struct coro *next) 1307transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1145{ 1308{
1146 dSTACKLEVEL; 1309 dSTACKLEVEL;
1147 static volatile int has_throw;
1148 1310
1149 /* sometimes transfer is only called to set idle_sp */ 1311 /* sometimes transfer is only called to set idle_sp */
1150 if (expect_false (!next)) 1312 if (expect_false (!next))
1151 { 1313 {
1152 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1314 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1156 { 1318 {
1157 coro_cctx *prev__cctx; 1319 coro_cctx *prev__cctx;
1158 1320
1159 if (expect_false (prev->flags & CF_NEW)) 1321 if (expect_false (prev->flags & CF_NEW))
1160 { 1322 {
1161 /* create a new empty context */ 1323 /* create a new empty/source context */
1162 Newz (0, prev->cctx, 1, coro_cctx); 1324 prev->cctx = cctx_new_empty ();
1163 prev->flags &= ~CF_NEW; 1325 prev->flags &= ~CF_NEW;
1164 prev->flags |= CF_RUNNING; 1326 prev->flags |= CF_RUNNING;
1165 } 1327 }
1166 1328
1167 prev->flags &= ~CF_RUNNING; 1329 prev->flags &= ~CF_RUNNING;
1182 else 1344 else
1183 load_perl (aTHX_ next); 1345 load_perl (aTHX_ next);
1184 1346
1185 prev__cctx = prev->cctx; 1347 prev__cctx = prev->cctx;
1186 1348
1187 /* possibly "free" the cctx */ 1349 /* possibly untie and reuse the cctx */
1188 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) 1350 if (expect_true (
1351 prev__cctx->idle_sp == STACKLEVEL
1352 && !(prev__cctx->flags & CC_TRACE)
1353 && !force_cctx
1354 ))
1189 { 1355 {
1190 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1356 /* 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)); 1357 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1192 1358
1193 prev->cctx = 0; 1359 prev->cctx = 0;
1194 1360
1195 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1361 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1196 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1362 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1204 ++next->usecount; 1370 ++next->usecount;
1205 1371
1206 if (expect_true (!next->cctx)) 1372 if (expect_true (!next->cctx))
1207 next->cctx = cctx_get (aTHX); 1373 next->cctx = cctx_get (aTHX);
1208 1374
1209 has_throw = !!next->throw; 1375 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1376 transfer_next = next;
1210 1377
1211 if (expect_false (prev__cctx != next->cctx)) 1378 if (expect_false (prev__cctx != next->cctx))
1212 { 1379 {
1213 prev__cctx->top_env = PL_top_env; 1380 prev__cctx->top_env = PL_top_env;
1214 PL_top_env = next->cctx->top_env; 1381 PL_top_env = next->cctx->top_env;
1215 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1382 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1216 } 1383 }
1217 1384
1218 free_coro_mortal (aTHX); 1385 transfer_tail (aTHX);
1219 UNLOCK;
1220
1221 if (expect_false (has_throw))
1222 {
1223 struct coro *coro = SvSTATE (coro_current);
1224
1225 if (coro->throw)
1226 {
1227 SV *exception = coro->throw;
1228 coro->throw = 0;
1229 sv_setsv (ERRSV, exception);
1230 croak (0);
1231 }
1232 }
1233 } 1386 }
1234} 1387}
1235 1388
1236struct transfer_args
1237{
1238 struct coro *prev, *next;
1239};
1240
1241#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1389#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) 1390#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1243 1391
1244/** high level stuff ********************************************************/ 1392/** high level stuff ********************************************************/
1245 1393
1246static int 1394static int
1270 croak ("FATAL: tried to destroy currently running coroutine"); 1418 croak ("FATAL: tried to destroy currently running coroutine");
1271 1419
1272 save_perl (aTHX_ &temp); 1420 save_perl (aTHX_ &temp);
1273 load_perl (aTHX_ coro); 1421 load_perl (aTHX_ coro);
1274 1422
1275 coro_destroy (aTHX_ coro); 1423 coro_destruct (aTHX_ coro);
1276 1424
1277 load_perl (aTHX_ &temp); 1425 load_perl (aTHX_ &temp);
1278 1426
1279 coro->slot = 0; 1427 coro->slot = 0;
1280 } 1428 }
1340{ 1488{
1341 dTHX; 1489 dTHX;
1342 struct transfer_args ta; 1490 struct transfer_args ta;
1343 1491
1344 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1492 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1345 TRANSFER (ta); 1493 TRANSFER (ta, 1);
1346} 1494}
1347 1495
1348/** Coro ********************************************************************/ 1496/** Coro ********************************************************************/
1349 1497
1350static void 1498static void
1368static int 1516static int
1369api_ready (SV *coro_sv) 1517api_ready (SV *coro_sv)
1370{ 1518{
1371 dTHX; 1519 dTHX;
1372 struct coro *coro; 1520 struct coro *coro;
1521 SV *sv_hook;
1522 void (*xs_hook)(void);
1373 1523
1374 if (SvROK (coro_sv)) 1524 if (SvROK (coro_sv))
1375 coro_sv = SvRV (coro_sv); 1525 coro_sv = SvRV (coro_sv);
1376 1526
1377 coro = SvSTATE (coro_sv); 1527 coro = SvSTATE (coro_sv);
1380 return 0; 1530 return 0;
1381 1531
1382 coro->flags |= CF_READY; 1532 coro->flags |= CF_READY;
1383 1533
1384 LOCK; 1534 LOCK;
1535
1536 sv_hook = coro_nready ? 0 : coro_readyhook;
1537 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1538
1385 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1539 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1386 ++coro_nready; 1540 ++coro_nready;
1541
1387 UNLOCK; 1542 UNLOCK;
1543
1544 if (sv_hook)
1545 {
1546 dSP;
1547
1548 ENTER;
1549 SAVETMPS;
1550
1551 PUSHMARK (SP);
1552 PUTBACK;
1553 call_sv (sv_hook, G_DISCARD);
1554 SPAGAIN;
1555
1556 FREETMPS;
1557 LEAVE;
1558 }
1559
1560 if (xs_hook)
1561 xs_hook ();
1388 1562
1389 return 1; 1563 return 1;
1390} 1564}
1391 1565
1392static int 1566static int
1393api_is_ready (SV *coro_sv) 1567api_is_ready (SV *coro_sv)
1394{ 1568{
1395 dTHX; 1569 dTHX;
1570
1396 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1571 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1397} 1572}
1398 1573
1399static void 1574INLINE void
1400prepare_schedule (pTHX_ struct transfer_args *ta) 1575prepare_schedule (pTHX_ struct transfer_args *ta)
1401{ 1576{
1402 SV *prev_sv, *next_sv; 1577 SV *prev_sv, *next_sv;
1403 1578
1404 for (;;) 1579 for (;;)
1430 /* cannot transfer to destroyed coros, skip and look for next */ 1605 /* cannot transfer to destroyed coros, skip and look for next */
1431 if (expect_false (ta->next->flags & CF_DESTROYED)) 1606 if (expect_false (ta->next->flags & CF_DESTROYED))
1432 { 1607 {
1433 UNLOCK; 1608 UNLOCK;
1434 SvREFCNT_dec (next_sv); 1609 SvREFCNT_dec (next_sv);
1435 /* coro_nready is already taken care of by destroy */ 1610 /* coro_nready has already been taken care of by destroy */
1436 continue; 1611 continue;
1437 } 1612 }
1438 1613
1439 --coro_nready; 1614 --coro_nready;
1440 UNLOCK; 1615 UNLOCK;
1443 1618
1444 /* free this only after the transfer */ 1619 /* free this only after the transfer */
1445 prev_sv = SvRV (coro_current); 1620 prev_sv = SvRV (coro_current);
1446 ta->prev = SvSTATE (prev_sv); 1621 ta->prev = SvSTATE (prev_sv);
1447 TRANSFER_CHECK (*ta); 1622 TRANSFER_CHECK (*ta);
1448 assert (ta->next->flags & CF_READY); 1623 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1449 ta->next->flags &= ~CF_READY; 1624 ta->next->flags &= ~CF_READY;
1450 SvRV_set (coro_current, next_sv); 1625 SvRV_set (coro_current, next_sv);
1451 1626
1452 LOCK; 1627 LOCK;
1453 free_coro_mortal (aTHX); 1628 free_coro_mortal (aTHX);
1454 coro_mortal = prev_sv; 1629 coro_mortal = prev_sv;
1455 UNLOCK; 1630 UNLOCK;
1456} 1631}
1457 1632
1458static void 1633INLINE void
1459prepare_cede (pTHX_ struct transfer_args *ta) 1634prepare_cede (pTHX_ struct transfer_args *ta)
1460{ 1635{
1461 api_ready (coro_current); 1636 api_ready (coro_current);
1462 prepare_schedule (aTHX_ ta); 1637 prepare_schedule (aTHX_ ta);
1463} 1638}
1481{ 1656{
1482 dTHX; 1657 dTHX;
1483 struct transfer_args ta; 1658 struct transfer_args ta;
1484 1659
1485 prepare_schedule (aTHX_ &ta); 1660 prepare_schedule (aTHX_ &ta);
1486 TRANSFER (ta); 1661 TRANSFER (ta, 1);
1487} 1662}
1488 1663
1489static int 1664static int
1490api_cede (void) 1665api_cede (void)
1491{ 1666{
1494 1669
1495 prepare_cede (aTHX_ &ta); 1670 prepare_cede (aTHX_ &ta);
1496 1671
1497 if (expect_true (ta.prev != ta.next)) 1672 if (expect_true (ta.prev != ta.next))
1498 { 1673 {
1499 TRANSFER (ta); 1674 TRANSFER (ta, 1);
1500 return 1; 1675 return 1;
1501 } 1676 }
1502 else 1677 else
1503 return 0; 1678 return 0;
1504} 1679}
1509 dTHX; 1684 dTHX;
1510 struct transfer_args ta; 1685 struct transfer_args ta;
1511 1686
1512 if (prepare_cede_notself (aTHX_ &ta)) 1687 if (prepare_cede_notself (aTHX_ &ta))
1513 { 1688 {
1514 TRANSFER (ta); 1689 TRANSFER (ta, 1);
1515 return 1; 1690 return 1;
1516 } 1691 }
1517 else 1692 else
1518 return 0; 1693 return 0;
1519} 1694}
1525 struct coro *coro = SvSTATE (coro_sv); 1700 struct coro *coro = SvSTATE (coro_sv);
1526 1701
1527 if (flags & CC_TRACE) 1702 if (flags & CC_TRACE)
1528 { 1703 {
1529 if (!coro->cctx) 1704 if (!coro->cctx)
1530 coro->cctx = cctx_new (); 1705 coro->cctx = cctx_new_run ();
1531 else if (!(coro->cctx->flags & CC_TRACE)) 1706 else if (!(coro->cctx->flags & CC_TRACE))
1532 croak ("cannot enable tracing on coroutine with custom stack"); 1707 croak ("cannot enable tracing on coroutine with custom stack");
1533 1708
1534 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1709 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1535 } 1710 }
1542 else 1717 else
1543 coro->slot->runops = RUNOPS_DEFAULT; 1718 coro->slot->runops = RUNOPS_DEFAULT;
1544 } 1719 }
1545} 1720}
1546 1721
1722#if 0
1723static int
1724coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1725{
1726 AV *padlist;
1727 AV *av = (AV *)mg->mg_obj;
1728
1729 abort ();
1730
1731 return 0;
1732}
1733
1734static MGVTBL coro_gensub_vtbl = {
1735 0, 0, 0, 0,
1736 coro_gensub_free
1737};
1738#endif
1739
1740/*****************************************************************************/
1741/* PerlIO::cede */
1742
1743typedef struct
1744{
1745 PerlIOBuf base;
1746 NV next, every;
1747} PerlIOCede;
1748
1749static IV
1750PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1751{
1752 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1753
1754 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1755 self->next = nvtime () + self->every;
1756
1757 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1758}
1759
1760static SV *
1761PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1762{
1763 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1764
1765 return newSVnv (self->every);
1766}
1767
1768static IV
1769PerlIOCede_flush (pTHX_ PerlIO *f)
1770{
1771 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1772 double now = nvtime ();
1773
1774 if (now >= self->next)
1775 {
1776 api_cede ();
1777 self->next = now + self->every;
1778 }
1779
1780 return PerlIOBuf_flush (aTHX_ f);
1781}
1782
1783static PerlIO_funcs PerlIO_cede =
1784{
1785 sizeof(PerlIO_funcs),
1786 "cede",
1787 sizeof(PerlIOCede),
1788 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1789 PerlIOCede_pushed,
1790 PerlIOBuf_popped,
1791 PerlIOBuf_open,
1792 PerlIOBase_binmode,
1793 PerlIOCede_getarg,
1794 PerlIOBase_fileno,
1795 PerlIOBuf_dup,
1796 PerlIOBuf_read,
1797 PerlIOBuf_unread,
1798 PerlIOBuf_write,
1799 PerlIOBuf_seek,
1800 PerlIOBuf_tell,
1801 PerlIOBuf_close,
1802 PerlIOCede_flush,
1803 PerlIOBuf_fill,
1804 PerlIOBase_eof,
1805 PerlIOBase_error,
1806 PerlIOBase_clearerr,
1807 PerlIOBase_setlinebuf,
1808 PerlIOBuf_get_base,
1809 PerlIOBuf_bufsiz,
1810 PerlIOBuf_get_ptr,
1811 PerlIOBuf_get_cnt,
1812 PerlIOBuf_set_ptrcnt,
1813};
1814
1815
1547MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1816MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1548 1817
1549PROTOTYPES: DISABLE 1818PROTOTYPES: DISABLE
1550 1819
1551BOOT: 1820BOOT:
1552{ 1821{
1553#ifdef USE_ITHREADS 1822#ifdef USE_ITHREADS
1554 MUTEX_INIT (&coro_mutex); 1823 MUTEX_INIT (&coro_lock);
1824# if CORO_PTHREAD
1825 coro_thx = PERL_GET_CONTEXT;
1826# endif
1555#endif 1827#endif
1556 BOOT_PAGESIZE; 1828 BOOT_PAGESIZE;
1557 1829
1558 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1830 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1559 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1831 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1560 1832
1561 orig_sigelem_get = PL_vtbl_sigelem.svt_get; 1833 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; 1834 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; 1835 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 1836
1566 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); 1837 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1567 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 1838 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)); 1839 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1569 1840
1578 main_top_env = PL_top_env; 1849 main_top_env = PL_top_env;
1579 1850
1580 while (main_top_env->je_prev) 1851 while (main_top_env->je_prev)
1581 main_top_env = main_top_env->je_prev; 1852 main_top_env = main_top_env->je_prev;
1582 1853
1583 coroapi.ver = CORO_API_VERSION; 1854 coroapi.ver = CORO_API_VERSION;
1584 coroapi.rev = CORO_API_REVISION; 1855 coroapi.rev = CORO_API_REVISION;
1585 coroapi.transfer = api_transfer; 1856 coroapi.transfer = api_transfer;
1857
1858 {
1859 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1860
1861 if (!svp) croak ("Time::HiRes is required");
1862 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1863
1864 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1865 }
1586 1866
1587 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1867 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1588} 1868}
1589 1869
1590SV * 1870SV *
1632 1912
1633 PUTBACK; 1913 PUTBACK;
1634 switch (ix) 1914 switch (ix)
1635 { 1915 {
1636 case 0: 1916 case 0:
1637 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1917 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0)));
1638 ta.next = 0;
1639 break; 1918 break;
1640 1919
1641 case 1: 1920 case 1:
1642 if (items != 2) 1921 if (items != 2)
1643 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1922 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1644 1923
1645 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1924 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1646 break; 1925 break;
1647 1926
1648 case 2: 1927 case 2:
1661 } 1940 }
1662 SPAGAIN; 1941 SPAGAIN;
1663 1942
1664 BARRIER; 1943 BARRIER;
1665 PUTBACK; 1944 PUTBACK;
1666 TRANSFER (ta); 1945 TRANSFER (ta, 0);
1667 SPAGAIN; /* might be the sp of a different coroutine now */ 1946 SPAGAIN; /* might be the sp of a different coroutine now */
1668 /* be extra careful not to ever do anything after TRANSFER */ 1947 /* be extra careful not to ever do anything after TRANSFER */
1669} 1948}
1670 1949
1671bool 1950bool
1674 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1953 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1675 OUTPUT: 1954 OUTPUT:
1676 RETVAL 1955 RETVAL
1677 1956
1678void 1957void
1679_exit (code) 1958_exit (int code)
1680 int code
1681 PROTOTYPE: $ 1959 PROTOTYPE: $
1682 CODE: 1960 CODE:
1683 _exit (code); 1961 _exit (code);
1684 1962
1685int 1963int
1686cctx_stacksize (int new_stacksize = 0) 1964cctx_stacksize (int new_stacksize = 0)
1687 CODE: 1965 CODE:
1688 RETVAL = coro_stacksize; 1966 RETVAL = cctx_stacksize;
1689 if (new_stacksize) 1967 if (new_stacksize)
1968 {
1690 coro_stacksize = new_stacksize; 1969 cctx_stacksize = new_stacksize;
1970 ++cctx_gen;
1971 }
1972 OUTPUT:
1973 RETVAL
1974
1975int
1976cctx_max_idle (int max_idle = 0)
1977 CODE:
1978 RETVAL = cctx_max_idle;
1979 if (max_idle > 1)
1980 cctx_max_idle = max_idle;
1691 OUTPUT: 1981 OUTPUT:
1692 RETVAL 1982 RETVAL
1693 1983
1694int 1984int
1695cctx_count () 1985cctx_count ()
1719call (Coro::State coro, SV *coderef) 2009call (Coro::State coro, SV *coderef)
1720 ALIAS: 2010 ALIAS:
1721 eval = 1 2011 eval = 1
1722 CODE: 2012 CODE:
1723{ 2013{
1724 if (coro->mainstack) 2014 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1725 { 2015 {
1726 struct coro temp; 2016 struct coro temp;
1727 2017
1728 if (!(coro->flags & CF_RUNNING)) 2018 if (!(coro->flags & CF_RUNNING))
1729 { 2019 {
1773 RETVAL = boolSV (coro->flags & ix); 2063 RETVAL = boolSV (coro->flags & ix);
1774 OUTPUT: 2064 OUTPUT:
1775 RETVAL 2065 RETVAL
1776 2066
1777void 2067void
2068throw (Coro::State self, SV *throw = &PL_sv_undef)
2069 PROTOTYPE: $;$
2070 CODE:
2071 SvREFCNT_dec (self->throw);
2072 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2073
2074void
1778api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2075api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1779 2076
1780SV * 2077SV *
1781has_stack (Coro::State coro) 2078has_cctx (Coro::State coro)
1782 PROTOTYPE: $ 2079 PROTOTYPE: $
1783 CODE: 2080 CODE:
1784 RETVAL = boolSV (!!coro->cctx); 2081 RETVAL = boolSV (!!coro->cctx);
1785 OUTPUT: 2082 OUTPUT:
1786 RETVAL 2083 RETVAL
1791 CODE: 2088 CODE:
1792 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2089 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1793 OUTPUT: 2090 OUTPUT:
1794 RETVAL 2091 RETVAL
1795 2092
1796IV 2093UV
1797rss (Coro::State coro) 2094rss (Coro::State coro)
1798 PROTOTYPE: $ 2095 PROTOTYPE: $
1799 ALIAS: 2096 ALIAS:
1800 usecount = 1 2097 usecount = 1
1801 CODE: 2098 CODE:
1805 case 1: RETVAL = coro->usecount; break; 2102 case 1: RETVAL = coro->usecount; break;
1806 } 2103 }
1807 OUTPUT: 2104 OUTPUT:
1808 RETVAL 2105 RETVAL
1809 2106
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 2107void
1855_set_current (SV *current) 2108force_cctx ()
1856 PROTOTYPE: $
1857 CODE: 2109 CODE:
1858 SvREFCNT_dec (SvRV (coro_current)); 2110 struct coro *coro = SvSTATE (coro_current);
1859 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2111 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
1899void
1900throw (Coro::State self, SV *throw = &PL_sv_undef)
1901 PROTOTYPE: $;$
1902 CODE:
1903 SvREFCNT_dec (self->throw);
1904 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1905 2112
1906void 2113void
1907swap_defsv (Coro::State self) 2114swap_defsv (Coro::State self)
1908 PROTOTYPE: $ 2115 PROTOTYPE: $
1909 ALIAS: 2116 ALIAS:
1917 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2124 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
1918 2125
1919 SV *tmp = *src; *src = *dst; *dst = tmp; 2126 SV *tmp = *src; *src = *dst; *dst = tmp;
1920 } 2127 }
1921 2128
2129MODULE = Coro::State PACKAGE = Coro
2130
2131BOOT:
2132{
2133 int i;
2134
2135 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2136 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2137 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2138
2139 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2140 SvREADONLY_on (coro_current);
2141
2142 coro_stash = gv_stashpv ("Coro", TRUE);
2143
2144 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2145 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2146 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2147 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2148 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2149 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2150
2151 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2152 coro_ready[i] = newAV ();
2153
2154 {
2155 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2156
2157 coroapi.schedule = api_schedule;
2158 coroapi.cede = api_cede;
2159 coroapi.cede_notself = api_cede_notself;
2160 coroapi.ready = api_ready;
2161 coroapi.is_ready = api_is_ready;
2162 coroapi.nready = &coro_nready;
2163 coroapi.current = coro_current;
2164
2165 GCoroAPI = &coroapi;
2166 sv_setiv (sv, (IV)&coroapi);
2167 SvREADONLY_on (sv);
2168 }
2169}
2170
2171void
2172_set_current (SV *current)
2173 PROTOTYPE: $
2174 CODE:
2175 SvREFCNT_dec (SvRV (coro_current));
2176 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2177
2178void
2179_set_readyhook (SV *hook)
2180 PROTOTYPE: $
2181 CODE:
2182 LOCK;
2183 SvREFCNT_dec (coro_readyhook);
2184 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2185 UNLOCK;
2186
2187int
2188prio (Coro::State coro, int newprio = 0)
2189 ALIAS:
2190 nice = 1
2191 CODE:
2192{
2193 RETVAL = coro->prio;
2194
2195 if (items > 1)
2196 {
2197 if (ix)
2198 newprio = coro->prio - newprio;
2199
2200 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2201 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2202
2203 coro->prio = newprio;
2204 }
2205}
2206 OUTPUT:
2207 RETVAL
2208
2209SV *
2210ready (SV *self)
2211 PROTOTYPE: $
2212 CODE:
2213 RETVAL = boolSV (api_ready (self));
2214 OUTPUT:
2215 RETVAL
2216
2217int
2218nready (...)
2219 PROTOTYPE:
2220 CODE:
2221 RETVAL = coro_nready;
2222 OUTPUT:
2223 RETVAL
2224
1922# for async_pool speedup 2225# for async_pool speedup
1923void 2226void
1924_pool_1 (SV *cb) 2227_pool_1 (SV *cb)
1925 CODE: 2228 CODE:
1926{ 2229{
1931 AV *invoke_av; 2234 AV *invoke_av;
1932 int i, len; 2235 int i, len;
1933 2236
1934 if (!invoke) 2237 if (!invoke)
1935 { 2238 {
1936 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2239 SV *old = PL_diehook;
2240 PL_diehook = 0;
2241 SvREFCNT_dec (old);
1937 croak ("\3async_pool terminate\2\n"); 2242 croak ("\3async_pool terminate\2\n");
1938 } 2243 }
1939 2244
1940 SvREFCNT_dec (coro->saved_deffh); 2245 SvREFCNT_dec (coro->saved_deffh);
1941 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2246 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1942 2247
1943 hv_store (hv, "desc", sizeof ("desc") - 1, 2248 hv_store (hv, "desc", sizeof ("desc") - 1,
1944 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2249 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1945 2250
1946 invoke_av = (AV *)SvRV (invoke); 2251 invoke_av = (AV *)SvRV (invoke);
1950 2255
1951 if (len > 0) 2256 if (len > 0)
1952 { 2257 {
1953 av_fill (defav, len - 1); 2258 av_fill (defav, len - 1);
1954 for (i = 0; i < len; ++i) 2259 for (i = 0; i < len; ++i)
1955 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2260 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1956 } 2261 }
1957 2262
1958 SvREFCNT_dec (invoke); 2263 SvREFCNT_dec (invoke);
1959} 2264}
1960 2265
1967 sv_setsv (cb, &PL_sv_undef); 2272 sv_setsv (cb, &PL_sv_undef);
1968 2273
1969 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2274 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1970 coro->saved_deffh = 0; 2275 coro->saved_deffh = 0;
1971 2276
1972 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2277 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1973 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2278 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1974 { 2279 {
1975 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2280 SV *old = PL_diehook;
2281 PL_diehook = 0;
2282 SvREFCNT_dec (old);
1976 croak ("\3async_pool terminate\2\n"); 2283 croak ("\3async_pool terminate\2\n");
1977 } 2284 }
1978 2285
1979 av_clear (GvAV (PL_defgv)); 2286 av_clear (GvAV (PL_defgv));
1980 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 2287 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1986 api_trace (coro_current, 0); 2293 api_trace (coro_current, 0);
1987 2294
1988 av_push (av_async_pool, newSVsv (coro_current)); 2295 av_push (av_async_pool, newSVsv (coro_current));
1989} 2296}
1990 2297
2298#if 0
2299
2300void
2301_generator_call (...)
2302 PROTOTYPE: @
2303 PPCODE:
2304 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2305 xxxx
2306 abort ();
2307
2308SV *
2309gensub (SV *sub, ...)
2310 PROTOTYPE: &;@
2311 CODE:
2312{
2313 struct coro *coro;
2314 MAGIC *mg;
2315 CV *xcv;
2316 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2317 int i;
2318
2319 CvGV (ncv) = CvGV (cv);
2320 CvFILE (ncv) = CvFILE (cv);
2321
2322 Newz (0, coro, 1, struct coro);
2323 coro->args = newAV ();
2324 coro->flags = CF_NEW;
2325
2326 av_extend (coro->args, items - 1);
2327 for (i = 1; i < items; i++)
2328 av_push (coro->args, newSVsv (ST (i)));
2329
2330 CvISXSUB_on (ncv);
2331 CvXSUBANY (ncv).any_ptr = (void *)coro;
2332
2333 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2334
2335 CvXSUB (ncv) = CvXSUB (xcv);
2336 CvANON_on (ncv);
2337
2338 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2339 RETVAL = newRV_noinc ((SV *)ncv);
2340}
2341 OUTPUT:
2342 RETVAL
2343
2344#endif
2345
1991 2346
1992MODULE = Coro::State PACKAGE = Coro::AIO 2347MODULE = Coro::State PACKAGE = Coro::AIO
1993 2348
1994SV * 2349void
1995_get_state () 2350_get_state (SV *self)
1996 CODE: 2351 PPCODE:
1997{ 2352{
1998 struct io_state *data; 2353 AV *defav = GvAV (PL_defgv);
1999 2354 AV *av = newAV ();
2355 int i;
2000 RETVAL = newSV (sizeof (struct io_state)); 2356 SV *data_sv = newSV (sizeof (struct io_state));
2001 data = (struct io_state *)SvPVX (RETVAL); 2357 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2002 SvCUR_set (RETVAL, sizeof (struct io_state)); 2358 SvCUR_set (data_sv, sizeof (struct io_state));
2003 SvPOK_only (RETVAL); 2359 SvPOK_only (data_sv);
2004 2360
2005 data->errorno = errno; 2361 data->errorno = errno;
2006 data->laststype = PL_laststype; 2362 data->laststype = PL_laststype;
2007 data->laststatval = PL_laststatval; 2363 data->laststatval = PL_laststatval;
2008 data->statcache = PL_statcache; 2364 data->statcache = PL_statcache;
2365
2366 av_extend (av, AvFILLp (defav) + 1 + 1);
2367
2368 for (i = 0; i <= AvFILLp (defav); ++i)
2369 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2370
2371 av_push (av, data_sv);
2372
2373 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2374
2375 api_ready (self);
2009} 2376}
2010 OUTPUT:
2011 RETVAL
2012 2377
2013void 2378void
2014_set_state (char *data_) 2379_set_state (SV *state)
2015 PROTOTYPE: $ 2380 PROTOTYPE: $
2016 CODE: 2381 PPCODE:
2017{ 2382{
2018 struct io_state *data = (void *)data_; 2383 AV *av = (AV *)SvRV (state);
2384 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2385 int i;
2019 2386
2020 errno = data->errorno; 2387 errno = data->errorno;
2021 PL_laststype = data->laststype; 2388 PL_laststype = data->laststype;
2022 PL_laststatval = data->laststatval; 2389 PL_laststatval = data->laststatval;
2023 PL_statcache = data->statcache; 2390 PL_statcache = data->statcache;
2024}
2025 2391
2392 EXTEND (SP, AvFILLp (av));
2393 for (i = 0; i < AvFILLp (av); ++i)
2394 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2395}
2396
2397
2398MODULE = Coro::State PACKAGE = Coro::AnyEvent
2399
2400BOOT:
2401 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2402
2403SV *
2404_schedule (...)
2405 PROTOTYPE: @
2406 CODE:
2407{
2408 static int incede;
2409
2410 api_cede_notself ();
2411
2412 ++incede;
2413 while (coro_nready >= incede && api_cede ())
2414 ;
2415
2416 sv_setsv (sv_activity, &PL_sv_undef);
2417 if (coro_nready >= incede)
2418 {
2419 PUSHMARK (SP);
2420 PUTBACK;
2421 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2422 SPAGAIN;
2423 }
2424
2425 --incede;
2426}
2427
2428
2429MODULE = Coro::State PACKAGE = PerlIO::cede
2430
2431BOOT:
2432 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2433

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines