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.204 by root, Mon Oct 8 03:03:39 2007 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
23# include <inttypes.h> /* most portable stdint.h */
24#endif
15 25
16#ifdef HAVE_MMAP 26#ifdef HAVE_MMAP
17# include <unistd.h> 27# include <unistd.h>
18# include <sys/mman.h> 28# include <sys/mman.h>
19# ifndef MAP_ANONYMOUS 29# ifndef MAP_ANONYMOUS
71# ifndef IS_PADCONST 81# ifndef IS_PADCONST
72# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
73# endif 83# endif
74#endif 84#endif
75 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
96/* 5.8.8 */
97#ifndef GV_NOTQUAL
98# define GV_NOTQUAL 0
99#endif
100#ifndef newSV
101# define newSV(l) NEWSV(0,l)
102#endif
103
76/* 5.8.7 */ 104/* 5.8.7 */
77#ifndef SvRV_set 105#ifndef SvRV_set
78# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
79#endif
80
81/* 5.8.8 */
82#ifndef GV_NOTQUAL
83# define GV_NOTQUAL 0
84#endif
85#ifndef newSV
86# define newSV(l) NEWSV(0,l)
87#endif 107#endif
88 108
89#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 109#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
90# undef CORO_STACKGUARD 110# undef CORO_STACKGUARD
91#endif 111#endif
122#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
123 143
124#include "CoroAPI.h" 144#include "CoroAPI.h"
125 145
126#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
127static perl_mutex coro_mutex; 147static perl_mutex coro_lock;
128# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
129# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
130#else 150#else
131# define LOCK (void)0 151# define LOCK (void)0
132# define UNLOCK (void)0 152# define UNLOCK (void)0
133#endif 153#endif
134 154
135#define strpair(const) const, sizeof (const) - 1
136
137/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
138struct io_state 156struct io_state
139{ 157{
158 AV *res;
140 int errorno; 159 int errorno;
141 I32 laststype; 160 I32 laststype;
142 int laststatval; 161 int laststatval;
143 Stat_t statcache; 162 Stat_t statcache;
144}; 163};
145 164
165static double (*nvtime)(); /* so why doesn't it take void? */
166
146static size_t coro_stacksize = CORO_STACKSIZE; 167static size_t coro_stacksize = CORO_STACKSIZE;
147static struct CoroAPI coroapi; 168static struct CoroAPI coroapi;
148static AV *main_mainstack; /* used to differentiate between $main and others */ 169static AV *main_mainstack; /* used to differentiate between $main and others */
149static JMPENV *main_top_env; 170static JMPENV *main_top_env;
150static HV *coro_state_stash, *coro_stash; 171static HV *coro_state_stash, *coro_stash;
151static SV *coro_mortal; /* will be freed after next transfer */ 172static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
152 173
153static GV *irsgv; /* $/ */ 174static GV *irsgv; /* $/ */
154static GV *stdoutgv; /* *STDOUT */ 175static GV *stdoutgv; /* *STDOUT */
155 176static SV *rv_diehook;
177static SV *rv_warnhook;
156static HV *hv_sig; /* %SIG */ 178static HV *hv_sig; /* %SIG */
157static SV *sv_diehook;
158static SV *sv_warnhook;
159 179
160/* async_pool helper stuff */ 180/* async_pool helper stuff */
161static SV *sv_pool_rss; 181static SV *sv_pool_rss;
162static SV *sv_pool_size; 182static SV *sv_pool_size;
163static AV *av_async_pool; 183static AV *av_async_pool;
184
185/* Coro::AnyEvent */
186static SV *sv_activity;
164 187
165static struct coro_cctx *cctx_first; 188static struct coro_cctx *cctx_first;
166static int cctx_count, cctx_idle; 189static int cctx_count, cctx_idle;
167 190
168enum { 191enum {
254#define PRIO_IDLE -3 277#define PRIO_IDLE -3
255#define PRIO_MIN -4 278#define PRIO_MIN -4
256 279
257/* for Coro.pm */ 280/* for Coro.pm */
258static SV *coro_current; 281static SV *coro_current;
282static SV *coro_readyhook;
259static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 283static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
260static int coro_nready; 284static int coro_nready;
261static struct coro *coro_first; 285static struct coro *coro_first;
262 286
263/** lowlevel stuff **********************************************************/ 287/** lowlevel stuff **********************************************************/
264 288
265static SV * 289static SV *
266coro_get_sv (const char *name, int create) 290coro_get_sv (pTHX_ const char *name, int create)
267{ 291{
268#if PERL_VERSION_ATLEAST (5,9,0) 292#if PERL_VERSION_ATLEAST (5,10,0)
269 /* 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 */
270 get_sv (name, create); 294 get_sv (name, create);
271#endif 295#endif
272 return get_sv (name, create); 296 return get_sv (name, create);
273} 297}
274 298
275static AV * 299static AV *
276coro_get_av (const char *name, int create) 300coro_get_av (pTHX_ const char *name, int create)
277{ 301{
278#if PERL_VERSION_ATLEAST (5,9,0) 302#if PERL_VERSION_ATLEAST (5,10,0)
279 /* 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 */
280 get_av (name, create); 304 get_av (name, create);
281#endif 305#endif
282 return get_av (name, create); 306 return get_av (name, create);
283} 307}
284 308
285static HV * 309static HV *
286coro_get_hv (const char *name, int create) 310coro_get_hv (pTHX_ const char *name, int create)
287{ 311{
288#if PERL_VERSION_ATLEAST (5,9,0) 312#if PERL_VERSION_ATLEAST (5,10,0)
289 /* 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 */
290 get_hv (name, create); 314 get_hv (name, create);
291#endif 315#endif
292 return get_hv (name, create); 316 return get_hv (name, create);
293} 317}
298 AV *padlist = CvPADLIST (cv); 322 AV *padlist = CvPADLIST (cv);
299 AV *newpadlist, *newpad; 323 AV *newpadlist, *newpad;
300 324
301 newpadlist = newAV (); 325 newpadlist = newAV ();
302 AvREAL_off (newpadlist); 326 AvREAL_off (newpadlist);
303#if PERL_VERSION_ATLEAST (5,9,0) 327#if PERL_VERSION_ATLEAST (5,10,0)
304 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 328 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
305#else 329#else
306 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 330 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
307#endif 331#endif
308 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 332 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
309 --AvFILLp (padlist); 333 --AvFILLp (padlist);
310 334
311 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 335 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
312 av_store (newpadlist, 1, (SV *)newpad); 336 av_store (newpadlist, 1, (SV *)newpad);
313 337
314 return newpadlist; 338 return newpadlist;
315} 339}
316 340
346 370
347 /* casting is fun. */ 371 /* casting is fun. */
348 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 372 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
349 free_padlist (aTHX_ padlist); 373 free_padlist (aTHX_ padlist);
350 374
351 SvREFCNT_dec (av); 375 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
352 376
353 return 0; 377 return 0;
354} 378}
355 379
356#define PERL_MAGIC_coro PERL_MAGIC_ext 380#define CORO_MAGIC_type_cv PERL_MAGIC_ext
381#define CORO_MAGIC_type_state PERL_MAGIC_ext
357 382
358static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 383static MGVTBL coro_cv_vtbl = {
384 0, 0, 0, 0,
385 coro_cv_free
386};
359 387
360#define CORO_MAGIC(cv) \ 388#define CORO_MAGIC(sv, type) \
361 SvMAGIC (cv) \ 389 SvMAGIC (sv) \
362 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 390 ? SvMAGIC (sv)->mg_type == type \
363 ? SvMAGIC (cv) \ 391 ? SvMAGIC (sv) \
364 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 392 : mg_find (sv, type) \
365 : 0 393 : 0
394
395#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
396#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
366 397
367static struct coro * 398static struct coro *
368SvSTATE_ (pTHX_ SV *coro) 399SvSTATE_ (pTHX_ SV *coro)
369{ 400{
370 HV *stash; 401 HV *stash;
382 /* very slow, but rare, check */ 413 /* very slow, but rare, check */
383 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 414 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
384 croak ("Coro::State object required"); 415 croak ("Coro::State object required");
385 } 416 }
386 417
387 mg = CORO_MAGIC (coro); 418 mg = CORO_MAGIC_state (coro);
388 return (struct coro *)mg->mg_ptr; 419 return (struct coro *)mg->mg_ptr;
389} 420}
390 421
391#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 422#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
392 423
393/* the next two functions merely cache the padlists */ 424/* the next two functions merely cache the padlists */
394static void 425static void
395get_padlist (pTHX_ CV *cv) 426get_padlist (pTHX_ CV *cv)
396{ 427{
397 MAGIC *mg = CORO_MAGIC (cv); 428 MAGIC *mg = CORO_MAGIC_cv (cv);
398 AV *av; 429 AV *av;
399 430
400 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 431 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
401 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 432 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
402 else 433 else
403 { 434 {
404#if CORO_PREFER_PERL_FUNCTIONS 435#if CORO_PREFER_PERL_FUNCTIONS
405 /* this is probably cleaner, but also slower? */ 436 /* this is probably cleaner? but also slower! */
437 /* in practise, it seems to be less stable */
406 CV *cp = Perl_cv_clone (cv); 438 CV *cp = Perl_cv_clone (cv);
407 CvPADLIST (cv) = CvPADLIST (cp); 439 CvPADLIST (cv) = CvPADLIST (cp);
408 CvPADLIST (cp) = 0; 440 CvPADLIST (cp) = 0;
409 SvREFCNT_dec (cp); 441 SvREFCNT_dec (cp);
410#else 442#else
414} 446}
415 447
416static void 448static void
417put_padlist (pTHX_ CV *cv) 449put_padlist (pTHX_ CV *cv)
418{ 450{
419 MAGIC *mg = CORO_MAGIC (cv); 451 MAGIC *mg = CORO_MAGIC_cv (cv);
420 AV *av; 452 AV *av;
421 453
422 if (expect_false (!mg)) 454 if (expect_false (!mg))
423 { 455 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
424 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
425 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
426 mg->mg_virtual = &vtbl_coro;
427 mg->mg_obj = (SV *)newAV ();
428 }
429 456
430 av = (AV *)mg->mg_obj; 457 av = (AV *)mg->mg_obj;
431 458
432 if (expect_false (AvFILLp (av) >= AvMAX (av))) 459 if (expect_false (AvFILLp (av) >= AvMAX (av)))
433 av_extend (av, AvMAX (av) + 1); 460 av_extend (av, AvMAX (av) + 1);
451 GvSV (irsgv) = slot->irsgv; 478 GvSV (irsgv) = slot->irsgv;
452 479
453 #define VAR(name,type) PL_ ## name = slot->name; 480 #define VAR(name,type) PL_ ## name = slot->name;
454 # include "state.h" 481 # include "state.h"
455 #undef VAR 482 #undef VAR
456
457 /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/
458 /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/
459 483
460 { 484 {
461 dSP; 485 dSP;
462 486
463 CV *cv; 487 CV *cv;
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 ************************************************/
700
701static int (*orig_sigelem_get) (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
711
712/*
713 * This overrides the default magic get method of %SIG elements.
714 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
715 * and instead of tryign to save and restore the hash elements, we just provide
716 * readback here.
717 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
718 * not expecting this to actually change the hook. This is a potential problem
719 * when a schedule happens then, but we ignore this.
720 */
721static int
722coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
723{
724 const char *s = MgPV_nolen_const (mg);
725
726 if (*s == '_')
727 {
728 SV **svp = 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 }
738 }
739
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;
765}
766
767static int
768coro_sigelem_set (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 = newSVsv (sv);
783 SvREFCNT_dec (old);
784 return 0;
785 }
786 }
787
788 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
789}
673 790
674static void 791static void
675coro_setup (pTHX_ struct coro *coro) 792coro_setup (pTHX_ struct coro *coro)
676{ 793{
677 /* 794 /*
686 PL_curpm = 0; 803 PL_curpm = 0;
687 PL_curpad = 0; 804 PL_curpad = 0;
688 PL_localizing = 0; 805 PL_localizing = 0;
689 PL_dirty = 0; 806 PL_dirty = 0;
690 PL_restartop = 0; 807 PL_restartop = 0;
691 PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); 808#if PERL_VERSION_ATLEAST (5,10,0)
692 PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); 809 PL_parser = 0;
810#endif
811
812 /* recreate the die/warn hooks */
813 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
814 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
693 815
694 GvSV (PL_defgv) = newSV (0); 816 GvSV (PL_defgv) = newSV (0);
695 GvAV (PL_defgv) = coro->args; coro->args = 0; 817 GvAV (PL_defgv) = coro->args; coro->args = 0;
696 GvSV (PL_errgv) = newSV (0); 818 GvSV (PL_errgv) = newSV (0);
697 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);
698 PL_rs = newSVsv (GvSV (irsgv)); 820 PL_rs = newSVsv (GvSV (irsgv));
699 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
700 822
701 { 823 {
702 dSP; 824 dSP;
703 LOGOP myop; 825 LOGOP myop;
704 826
712 PL_op = (OP *)&myop; 834 PL_op = (OP *)&myop;
713 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 835 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
714 SPAGAIN; 836 SPAGAIN;
715 } 837 }
716 838
717 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ 839 /* this newly created coroutine might be run on an existing cctx which most
840 * likely was suspended in set_stacklevel, called from entersub.
841 * set_stacklevl doesn't do anything on return, but entersub does LEAVE,
842 * so we ENTER here for symmetry
843 */
844 ENTER;
718} 845}
719 846
720static void 847static void
721coro_destroy (pTHX_ struct coro *coro) 848coro_destruct (pTHX_ struct coro *coro)
722{ 849{
723 if (!IN_DESTRUCT) 850 if (!IN_DESTRUCT)
724 { 851 {
725 /* restore all saved variables and stuff */ 852 /* restore all saved variables and stuff */
726 LEAVE_SCOPE (0); 853 LEAVE_SCOPE (0);
748 SvREFCNT_dec (PL_warnhook); 875 SvREFCNT_dec (PL_warnhook);
749 876
750 SvREFCNT_dec (coro->saved_deffh); 877 SvREFCNT_dec (coro->saved_deffh);
751 SvREFCNT_dec (coro->throw); 878 SvREFCNT_dec (coro->throw);
752 879
753 coro_destroy_stacks (aTHX); 880 coro_destruct_stacks (aTHX);
754} 881}
755 882
756static void 883static void
757free_coro_mortal (pTHX) 884free_coro_mortal (pTHX)
758{ 885{
795 : cx->blk_gimme == G_SCALAR ? bot + 1 922 : cx->blk_gimme == G_SCALAR ? bot + 1
796 : bot; 923 : bot;
797 924
798 av_extend (av, top - bot); 925 av_extend (av, top - bot);
799 while (bot < top) 926 while (bot < top)
800 av_push (av, SvREFCNT_inc (*bot++)); 927 av_push (av, SvREFCNT_inc_NN (*bot++));
801 928
802 PL_runops = RUNOPS_DEFAULT; 929 PL_runops = RUNOPS_DEFAULT;
803 ENTER; 930 ENTER;
804 SAVETMPS; 931 SAVETMPS;
805 EXTEND (SP, 3); 932 EXTEND (SP, 3);
806 PUSHMARK (SP); 933 PUSHMARK (SP);
807 PUSHs (&PL_sv_no); 934 PUSHs (&PL_sv_no);
808 PUSHs (fullname); 935 PUSHs (fullname);
809 PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 936 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
810 PUTBACK; 937 PUTBACK;
811 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 938 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
812 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 939 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
813 SPAGAIN; 940 SPAGAIN;
814 FREETMPS; 941 FREETMPS;
815 LEAVE; 942 LEAVE;
816 PL_runops = runops_trace; 943 PL_runops = runops_trace;
843 SAVETMPS; 970 SAVETMPS;
844 EXTEND (SP, 3); 971 EXTEND (SP, 3);
845 PUSHMARK (SP); 972 PUSHMARK (SP);
846 PUSHs (&PL_sv_yes); 973 PUSHs (&PL_sv_yes);
847 PUSHs (fullname); 974 PUSHs (fullname);
848 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 975 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
849 PUTBACK; 976 PUTBACK;
850 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 977 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
851 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 978 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
852 SPAGAIN; 979 SPAGAIN;
853 FREETMPS; 980 FREETMPS;
854 LEAVE; 981 LEAVE;
855 PL_runops = runops_trace; 982 PL_runops = runops_trace;
869 PL_runops = RUNOPS_DEFAULT; 996 PL_runops = RUNOPS_DEFAULT;
870 PUSHMARK (SP); 997 PUSHMARK (SP);
871 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); 998 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
872 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); 999 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
873 PUTBACK; 1000 PUTBACK;
874 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); 1001 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
875 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1002 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
876 SPAGAIN; 1003 SPAGAIN;
877 FREETMPS; 1004 FREETMPS;
878 LEAVE; 1005 LEAVE;
879 PL_runops = runops_trace; 1006 PL_runops = runops_trace;
953 coro_cctx *cctx; 1080 coro_cctx *cctx;
954 void *stack_start; 1081 void *stack_start;
955 size_t stack_size; 1082 size_t stack_size;
956 1083
957 ++cctx_count; 1084 ++cctx_count;
958
959 Newz (0, cctx, 1, coro_cctx); 1085 Newz (0, cctx, 1, coro_cctx);
960 1086
961#if HAVE_MMAP 1087#if HAVE_MMAP
962 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;
963 /* mmap supposedly does allocate-on-write for us */ 1089 /* mmap supposedly does allocate-on-write for us */
999{ 1125{
1000 if (!cctx) 1126 if (!cctx)
1001 return; 1127 return;
1002 1128
1003 --cctx_count; 1129 --cctx_count;
1130 coro_destroy (&cctx->cctx);
1004 1131
1132 /* coro_transfer creates new, empty cctx's */
1133 if (cctx->sptr)
1134 {
1005#if CORO_USE_VALGRIND 1135#if CORO_USE_VALGRIND
1006 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1136 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1007#endif 1137#endif
1008 1138
1009#if HAVE_MMAP 1139#if HAVE_MMAP
1010 if (cctx->flags & CC_MAPPED) 1140 if (cctx->flags & CC_MAPPED)
1011 munmap (cctx->sptr, cctx->ssize); 1141 munmap (cctx->sptr, cctx->ssize);
1012 else 1142 else
1013#endif 1143#endif
1014 Safefree (cctx->sptr); 1144 Safefree (cctx->sptr);
1145 }
1015 1146
1016 Safefree (cctx); 1147 Safefree (cctx);
1017} 1148}
1018 1149
1019/* wether this cctx should be destructed */ 1150/* wether this cctx should be destructed */
1038} 1169}
1039 1170
1040static void 1171static void
1041cctx_put (coro_cctx *cctx) 1172cctx_put (coro_cctx *cctx)
1042{ 1173{
1174 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1175
1043 /* free another cctx if overlimit */ 1176 /* free another cctx if overlimit */
1044 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1177 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
1045 { 1178 {
1046 coro_cctx *first = cctx_first; 1179 coro_cctx *first = cctx_first;
1047 cctx_first = first->next; 1180 cctx_first = first->next;
1069 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");
1070 1203
1071 if (expect_false (next->flags & CF_DESTROYED)) 1204 if (expect_false (next->flags & CF_DESTROYED))
1072 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");
1073 1206
1074 if (
1075#if PERL_VERSION_ATLEAST (5,9,0) 1207#if !PERL_VERSION_ATLEAST (5,10,0)
1076 expect_false (PL_parser)
1077#else
1078 expect_false (PL_lex_state != LEX_NOTPARSING) 1208 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1079#endif
1080 )
1081 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
1082 } 1211 }
1083} 1212}
1084 1213
1085/* always use the TRANSFER macro */ 1214/* always use the TRANSFER macro */
1086static void NOINLINE 1215static void NOINLINE
1087transfer (pTHX_ struct coro *prev, struct coro *next) 1216transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1088{ 1217{
1089 dSTACKLEVEL; 1218 dSTACKLEVEL;
1090 1219
1091 /* sometimes transfer is only called to set idle_sp */ 1220 /* sometimes transfer is only called to set idle_sp */
1092 if (expect_false (!next)) 1221 if (expect_false (!next))
1094 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1223 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1095 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 */
1096 } 1225 }
1097 else if (expect_true (prev != next)) 1226 else if (expect_true (prev != next))
1098 { 1227 {
1228 static volatile int has_throw;
1099 coro_cctx *prev__cctx; 1229 coro_cctx *prev__cctx;
1100 1230
1101 if (expect_false (prev->flags & CF_NEW)) 1231 if (expect_false (prev->flags & CF_NEW))
1102 { 1232 {
1103 /* create a new empty context */ 1233 /* create a new empty/source context */
1234 ++cctx_count;
1104 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
1105 prev->flags &= ~CF_NEW; 1239 prev->flags &= ~CF_NEW;
1106 prev->flags |= CF_RUNNING; 1240 prev->flags |= CF_RUNNING;
1107 } 1241 }
1108 1242
1109 prev->flags &= ~CF_RUNNING; 1243 prev->flags &= ~CF_RUNNING;
1110 next->flags |= CF_RUNNING; 1244 next->flags |= CF_RUNNING;
1111 1245
1112 LOCK; 1246 LOCK;
1247
1248 /* first get rid of the old state */
1249 save_perl (aTHX_ prev);
1113 1250
1114 if (expect_false (next->flags & CF_NEW)) 1251 if (expect_false (next->flags & CF_NEW))
1115 { 1252 {
1116 /* need to start coroutine */ 1253 /* need to start coroutine */
1117 next->flags &= ~CF_NEW; 1254 next->flags &= ~CF_NEW;
1118 /* first get rid of the old state */
1119 save_perl (aTHX_ prev);
1120 /* setup coroutine call */ 1255 /* setup coroutine call */
1121 coro_setup (aTHX_ next); 1256 coro_setup (aTHX_ next);
1122 } 1257 }
1123 else 1258 else
1124 {
1125 /* coroutine already started */
1126 save_perl (aTHX_ prev);
1127 load_perl (aTHX_ next); 1259 load_perl (aTHX_ next);
1128 }
1129 1260
1130 prev__cctx = prev->cctx; 1261 prev__cctx = prev->cctx;
1131 1262
1132 /* possibly "free" the cctx */ 1263 /* possibly "free" the cctx */
1133 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) 1264 if (expect_true (
1265 prev__cctx->idle_sp == STACKLEVEL
1266 && !(prev__cctx->flags & CC_TRACE)
1267 && !force_cctx
1268 ))
1134 { 1269 {
1135 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1270 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1136 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1271 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
1137 1272
1138 prev->cctx = 0; 1273 prev->cctx = 0;
1148 1283
1149 ++next->usecount; 1284 ++next->usecount;
1150 1285
1151 if (expect_true (!next->cctx)) 1286 if (expect_true (!next->cctx))
1152 next->cctx = cctx_get (aTHX); 1287 next->cctx = cctx_get (aTHX);
1288
1289 has_throw = !!next->throw;
1153 1290
1154 if (expect_false (prev__cctx != next->cctx)) 1291 if (expect_false (prev__cctx != next->cctx))
1155 { 1292 {
1156 prev__cctx->top_env = PL_top_env; 1293 prev__cctx->top_env = PL_top_env;
1157 PL_top_env = next->cctx->top_env; 1294 PL_top_env = next->cctx->top_env;
1159 } 1296 }
1160 1297
1161 free_coro_mortal (aTHX); 1298 free_coro_mortal (aTHX);
1162 UNLOCK; 1299 UNLOCK;
1163 1300
1164 if (expect_false (prev->throw || next->throw)) 1301 if (expect_false (has_throw))
1165 { 1302 {
1166 struct coro *coro = SvSTATE (coro_current); 1303 struct coro *coro = SvSTATE (coro_current);
1167 1304
1168 if (coro->throw) 1305 if (coro->throw)
1169 { 1306 {
1179struct transfer_args 1316struct transfer_args
1180{ 1317{
1181 struct coro *prev, *next; 1318 struct coro *prev, *next;
1182}; 1319};
1183 1320
1184#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1321#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1185#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1322#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1186 1323
1187/** high level stuff ********************************************************/ 1324/** high level stuff ********************************************************/
1188 1325
1189static int 1326static int
1213 croak ("FATAL: tried to destroy currently running coroutine"); 1350 croak ("FATAL: tried to destroy currently running coroutine");
1214 1351
1215 save_perl (aTHX_ &temp); 1352 save_perl (aTHX_ &temp);
1216 load_perl (aTHX_ coro); 1353 load_perl (aTHX_ coro);
1217 1354
1218 coro_destroy (aTHX_ coro); 1355 coro_destruct (aTHX_ coro);
1219 1356
1220 load_perl (aTHX_ &temp); 1357 load_perl (aTHX_ &temp);
1221 1358
1222 coro->slot = 0; 1359 coro->slot = 0;
1223 } 1360 }
1283{ 1420{
1284 dTHX; 1421 dTHX;
1285 struct transfer_args ta; 1422 struct transfer_args ta;
1286 1423
1287 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1424 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1288 TRANSFER (ta); 1425 TRANSFER (ta, 1);
1289} 1426}
1290 1427
1291/** Coro ********************************************************************/ 1428/** Coro ********************************************************************/
1292 1429
1293static void 1430static void
1295{ 1432{
1296 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1433 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
1297} 1434}
1298 1435
1299static SV * 1436static SV *
1300coro_deq (pTHX_ int min_prio) 1437coro_deq (pTHX)
1301{ 1438{
1302 int prio = PRIO_MAX - PRIO_MIN; 1439 int prio;
1303 1440
1304 min_prio -= PRIO_MIN;
1305 if (min_prio < 0)
1306 min_prio = 0;
1307
1308 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 1441 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1309 if (AvFILLp (coro_ready [prio]) >= 0) 1442 if (AvFILLp (coro_ready [prio]) >= 0)
1310 return av_shift (coro_ready [prio]); 1443 return av_shift (coro_ready [prio]);
1311 1444
1312 return 0; 1445 return 0;
1313} 1446}
1315static int 1448static int
1316api_ready (SV *coro_sv) 1449api_ready (SV *coro_sv)
1317{ 1450{
1318 dTHX; 1451 dTHX;
1319 struct coro *coro; 1452 struct coro *coro;
1453 SV *sv_hook;
1454 void (*xs_hook)(void);
1320 1455
1321 if (SvROK (coro_sv)) 1456 if (SvROK (coro_sv))
1322 coro_sv = SvRV (coro_sv); 1457 coro_sv = SvRV (coro_sv);
1323 1458
1324 coro = SvSTATE (coro_sv); 1459 coro = SvSTATE (coro_sv);
1327 return 0; 1462 return 0;
1328 1463
1329 coro->flags |= CF_READY; 1464 coro->flags |= CF_READY;
1330 1465
1331 LOCK; 1466 LOCK;
1467
1468 sv_hook = coro_nready ? 0 : coro_readyhook;
1469 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1470
1332 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1471 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1333 ++coro_nready; 1472 ++coro_nready;
1473
1334 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 ();
1335 1494
1336 return 1; 1495 return 1;
1337} 1496}
1338 1497
1339static int 1498static int
1349 SV *prev_sv, *next_sv; 1508 SV *prev_sv, *next_sv;
1350 1509
1351 for (;;) 1510 for (;;)
1352 { 1511 {
1353 LOCK; 1512 LOCK;
1354 next_sv = coro_deq (aTHX_ PRIO_MIN); 1513 next_sv = coro_deq (aTHX);
1355 1514
1356 /* nothing to schedule: call the idle handler */ 1515 /* nothing to schedule: call the idle handler */
1357 if (expect_false (!next_sv)) 1516 if (expect_false (!next_sv))
1358 { 1517 {
1359 dSP; 1518 dSP;
1363 SAVETMPS; 1522 SAVETMPS;
1364 1523
1365 PUSHMARK (SP); 1524 PUSHMARK (SP);
1366 PUTBACK; 1525 PUTBACK;
1367 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1526 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1527 SPAGAIN;
1368 1528
1369 FREETMPS; 1529 FREETMPS;
1370 LEAVE; 1530 LEAVE;
1371 continue; 1531 continue;
1372 } 1532 }
1427{ 1587{
1428 dTHX; 1588 dTHX;
1429 struct transfer_args ta; 1589 struct transfer_args ta;
1430 1590
1431 prepare_schedule (aTHX_ &ta); 1591 prepare_schedule (aTHX_ &ta);
1432 TRANSFER (ta); 1592 TRANSFER (ta, 1);
1433} 1593}
1434 1594
1435static int 1595static int
1436api_cede (void) 1596api_cede (void)
1437{ 1597{
1440 1600
1441 prepare_cede (aTHX_ &ta); 1601 prepare_cede (aTHX_ &ta);
1442 1602
1443 if (expect_true (ta.prev != ta.next)) 1603 if (expect_true (ta.prev != ta.next))
1444 { 1604 {
1445 TRANSFER (ta); 1605 TRANSFER (ta, 1);
1446 return 1; 1606 return 1;
1447 } 1607 }
1448 else 1608 else
1449 return 0; 1609 return 0;
1450} 1610}
1455 dTHX; 1615 dTHX;
1456 struct transfer_args ta; 1616 struct transfer_args ta;
1457 1617
1458 if (prepare_cede_notself (aTHX_ &ta)) 1618 if (prepare_cede_notself (aTHX_ &ta))
1459 { 1619 {
1460 TRANSFER (ta); 1620 TRANSFER (ta, 1);
1461 return 1; 1621 return 1;
1462 } 1622 }
1463 else 1623 else
1464 return 0; 1624 return 0;
1465} 1625}
1488 else 1648 else
1489 coro->slot->runops = RUNOPS_DEFAULT; 1649 coro->slot->runops = RUNOPS_DEFAULT;
1490 } 1650 }
1491} 1651}
1492 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
1493MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1745MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1494 1746
1495PROTOTYPES: DISABLE 1747PROTOTYPES: DISABLE
1496 1748
1497BOOT: 1749BOOT:
1498{ 1750{
1499#ifdef USE_ITHREADS 1751#ifdef USE_ITHREADS
1500 MUTEX_INIT (&coro_mutex); 1752 MUTEX_INIT (&coro_lock);
1501#endif 1753#endif
1502 BOOT_PAGESIZE; 1754 BOOT_PAGESIZE;
1503 1755
1504 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1756 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1505 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1757 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1506 1758
1759 orig_sigelem_get = PL_vtbl_sigelem.svt_get; 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;
1761 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1762
1507 hv_sig = coro_get_hv ("SIG", TRUE); 1763 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1508 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); 1764 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1509 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); 1765 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1510
1511 if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);
1512 if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);
1513 1766
1514 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1767 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1515 1768
1516 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1769 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1517 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1770 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1522 main_top_env = PL_top_env; 1775 main_top_env = PL_top_env;
1523 1776
1524 while (main_top_env->je_prev) 1777 while (main_top_env->je_prev)
1525 main_top_env = main_top_env->je_prev; 1778 main_top_env = main_top_env->je_prev;
1526 1779
1527 coroapi.ver = CORO_API_VERSION; 1780 coroapi.ver = CORO_API_VERSION;
1781 coroapi.rev = CORO_API_REVISION;
1528 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 }
1529 1792
1530 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1793 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1531} 1794}
1532 1795
1533SV * 1796SV *
1534new (char *klass, ...) 1797new (char *klass, ...)
1535 CODE: 1798 CODE:
1536{ 1799{
1537 struct coro *coro; 1800 struct coro *coro;
1801 MAGIC *mg;
1538 HV *hv; 1802 HV *hv;
1539 int i; 1803 int i;
1540 1804
1541 Newz (0, coro, 1, struct coro); 1805 Newz (0, coro, 1, struct coro);
1542 coro->args = newAV (); 1806 coro->args = newAV ();
1545 if (coro_first) coro_first->prev = coro; 1809 if (coro_first) coro_first->prev = coro;
1546 coro->next = coro_first; 1810 coro->next = coro_first;
1547 coro_first = coro; 1811 coro_first = coro;
1548 1812
1549 coro->hv = hv = newHV (); 1813 coro->hv = hv = newHV ();
1550 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1814 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
1815 mg->mg_flags |= MGf_DUP;
1551 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1816 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1552 1817
1553 av_extend (coro->args, items - 1); 1818 av_extend (coro->args, items - 1);
1554 for (i = 1; i < items; i++) 1819 for (i = 1; i < items; i++)
1555 av_push (coro->args, newSVsv (ST (i))); 1820 av_push (coro->args, newSVsv (ST (i)));
1569 Coro::cede_notself = 4 1834 Coro::cede_notself = 4
1570 CODE: 1835 CODE:
1571{ 1836{
1572 struct transfer_args ta; 1837 struct transfer_args ta;
1573 1838
1839 PUTBACK;
1574 switch (ix) 1840 switch (ix)
1575 { 1841 {
1576 case 0: 1842 case 0:
1577 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1843 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1578 ta.next = 0; 1844 ta.next = 0;
1579 break; 1845 break;
1580 1846
1581 case 1: 1847 case 1:
1582 if (items != 2) 1848 if (items != 2)
1583 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);
1584 1850
1585 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1851 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1586 break; 1852 break;
1587 1853
1588 case 2: 1854 case 2:
1597 if (!prepare_cede_notself (aTHX_ &ta)) 1863 if (!prepare_cede_notself (aTHX_ &ta))
1598 XSRETURN_EMPTY; 1864 XSRETURN_EMPTY;
1599 1865
1600 break; 1866 break;
1601 } 1867 }
1868 SPAGAIN;
1602 1869
1603 BARRIER; 1870 BARRIER;
1871 PUTBACK;
1604 TRANSFER (ta); 1872 TRANSFER (ta, 0);
1605 1873 SPAGAIN; /* might be the sp of a different coroutine now */
1606 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1874 /* be extra careful not to ever do anything after TRANSFER */
1607 XSRETURN_YES;
1608} 1875}
1609 1876
1610bool 1877bool
1611_destroy (SV *coro_sv) 1878_destroy (SV *coro_sv)
1612 CODE: 1879 CODE:
1613 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1880 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1614 OUTPUT: 1881 OUTPUT:
1615 RETVAL 1882 RETVAL
1616 1883
1617void 1884void
1618_exit (code) 1885_exit (int code)
1619 int code
1620 PROTOTYPE: $ 1886 PROTOTYPE: $
1621 CODE: 1887 CODE:
1622 _exit (code); 1888 _exit (code);
1623 1889
1624int 1890int
1658call (Coro::State coro, SV *coderef) 1924call (Coro::State coro, SV *coderef)
1659 ALIAS: 1925 ALIAS:
1660 eval = 1 1926 eval = 1
1661 CODE: 1927 CODE:
1662{ 1928{
1663 if (coro->mainstack) 1929 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1664 { 1930 {
1665 struct coro temp; 1931 struct coro temp;
1666 1932
1667 if (!(coro->flags & CF_RUNNING)) 1933 if (!(coro->flags & CF_RUNNING))
1668 { 1934 {
1935 PUTBACK;
1669 save_perl (aTHX_ &temp); 1936 save_perl (aTHX_ &temp);
1670 load_perl (aTHX_ coro); 1937 load_perl (aTHX_ coro);
1671 } 1938 }
1672 1939
1673 { 1940 {
1674 dSP; 1941 dSP;
1675 ENTER; 1942 ENTER;
1676 SAVETMPS; 1943 SAVETMPS;
1944 PUTBACK;
1945 PUSHSTACK;
1677 PUSHMARK (SP); 1946 PUSHMARK (SP);
1678 PUTBACK;
1679 1947
1680 if (ix) 1948 if (ix)
1681 eval_sv (coderef, 0); 1949 eval_sv (coderef, 0);
1682 else 1950 else
1683 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1951 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1684 1952
1953 POPSTACK;
1685 SPAGAIN; 1954 SPAGAIN;
1686 FREETMPS; 1955 FREETMPS;
1687 LEAVE; 1956 LEAVE;
1688 PUTBACK; 1957 PUTBACK;
1689 } 1958 }
1690 1959
1691 if (!(coro->flags & CF_RUNNING)) 1960 if (!(coro->flags & CF_RUNNING))
1692 { 1961 {
1693 save_perl (aTHX_ coro); 1962 save_perl (aTHX_ coro);
1694 load_perl (aTHX_ &temp); 1963 load_perl (aTHX_ &temp);
1964 SPAGAIN;
1695 } 1965 }
1696 } 1966 }
1697} 1967}
1698 1968
1699SV * 1969SV *
1711 1981
1712void 1982void
1713api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1983api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1714 1984
1715SV * 1985SV *
1716has_stack (Coro::State coro) 1986has_cctx (Coro::State coro)
1717 PROTOTYPE: $ 1987 PROTOTYPE: $
1718 CODE: 1988 CODE:
1719 RETVAL = boolSV (!!coro->cctx); 1989 RETVAL = boolSV (!!coro->cctx);
1720 OUTPUT: 1990 OUTPUT:
1721 RETVAL 1991 RETVAL
1740 case 1: RETVAL = coro->usecount; break; 2010 case 1: RETVAL = coro->usecount; break;
1741 } 2011 }
1742 OUTPUT: 2012 OUTPUT:
1743 RETVAL 2013 RETVAL
1744 2014
2015void
2016force_cctx ()
2017 CODE:
2018 struct coro *coro = SvSTATE (coro_current);
2019 coro->cctx->idle_sp = 0;
2020
2021void
2022swap_defsv (Coro::State self)
2023 PROTOTYPE: $
2024 ALIAS:
2025 swap_defav = 1
2026 CODE:
2027 if (!self->slot)
2028 croak ("cannot swap state with coroutine that has no saved state");
2029 else
2030 {
2031 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv);
2032 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2033
2034 SV *tmp = *src; *src = *dst; *dst = tmp;
2035 }
1745 2036
1746MODULE = Coro::State PACKAGE = Coro 2037MODULE = Coro::State PACKAGE = Coro
1747 2038
1748BOOT: 2039BOOT:
1749{ 2040{
1750 int i; 2041 int i;
1751 2042
1752 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE);
1753 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE);
1754 av_async_pool = coro_get_av ("Coro::async_pool", TRUE); 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);
1755 2046
1756 coro_current = coro_get_sv ("Coro::current", FALSE); 2047 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1757 SvREADONLY_on (coro_current); 2048 SvREADONLY_on (coro_current);
1758 2049
1759 coro_stash = gv_stashpv ("Coro", TRUE); 2050 coro_stash = gv_stashpv ("Coro", TRUE);
1760 2051
1761 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2052 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1789void 2080void
1790_set_current (SV *current) 2081_set_current (SV *current)
1791 PROTOTYPE: $ 2082 PROTOTYPE: $
1792 CODE: 2083 CODE:
1793 SvREFCNT_dec (SvRV (coro_current)); 2084 SvREFCNT_dec (SvRV (coro_current));
1794 SvRV_set (coro_current, SvREFCNT_inc (SvRV (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;
1795 2095
1796int 2096int
1797prio (Coro::State coro, int newprio = 0) 2097prio (Coro::State coro, int newprio = 0)
1798 ALIAS: 2098 ALIAS:
1799 nice = 1 2099 nice = 1
1844 CODE: 2144 CODE:
1845{ 2145{
1846 struct coro *coro = SvSTATE (coro_current); 2146 struct coro *coro = SvSTATE (coro_current);
1847 HV *hv = (HV *)SvRV (coro_current); 2147 HV *hv = (HV *)SvRV (coro_current);
1848 AV *defav = GvAV (PL_defgv); 2148 AV *defav = GvAV (PL_defgv);
1849 SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); 2149 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1850 AV *invoke_av; 2150 AV *invoke_av;
1851 int i, len; 2151 int i, len;
1852 2152
1853 if (!invoke) 2153 if (!invoke)
2154 {
2155 SV *old = PL_diehook;
2156 PL_diehook = 0;
2157 SvREFCNT_dec (old);
1854 croak ("\3async_pool terminate\2\n"); 2158 croak ("\3async_pool terminate\2\n");
2159 }
1855 2160
1856 SvREFCNT_dec (coro->saved_deffh); 2161 SvREFCNT_dec (coro->saved_deffh);
1857 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2162 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1858 2163
1859 hv_store (hv, "desc", sizeof ("desc") - 1, 2164 hv_store (hv, "desc", sizeof ("desc") - 1,
1860 newSVpvn (strpair ("[async_pool]")), 0); 2165 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1861 2166
1862 invoke_av = (AV *)SvRV (invoke); 2167 invoke_av = (AV *)SvRV (invoke);
1863 len = av_len (invoke_av); 2168 len = av_len (invoke_av);
1864 2169
1865 sv_setsv (cb, AvARRAY (invoke_av)[0]); 2170 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1866 2171
1867 if (len > 0) 2172 if (len > 0)
1868 { 2173 {
1869 av_fill (defav, len - 1); 2174 av_fill (defav, len - 1);
1870 for (i = 0; i < len; ++i) 2175 for (i = 0; i < len; ++i)
1871 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2176 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1872 } 2177 }
1873 2178
1874 SvREFCNT_dec (invoke); 2179 SvREFCNT_dec (invoke);
1875} 2180}
1876 2181
1885 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2190 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1886 coro->saved_deffh = 0; 2191 coro->saved_deffh = 0;
1887 2192
1888 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2193 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1889 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2194 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2195 {
2196 SV *old = PL_diehook;
2197 PL_diehook = 0;
2198 SvREFCNT_dec (old);
1890 croak ("\3async_pool terminate\2\n"); 2199 croak ("\3async_pool terminate\2\n");
2200 }
1891 2201
1892 av_clear (GvAV (PL_defgv)); 2202 av_clear (GvAV (PL_defgv));
1893 hv_store ((HV *)SvRV (coro_current), strpair ("desc"), 2203 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1894 newSVpvn (strpair ("[async_pool idle]")), 0); 2204 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1895 2205
1896 coro->prio = 0; 2206 coro->prio = 0;
1897 2207
1898 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2208 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1899 api_trace (coro_current, 0); 2209 api_trace (coro_current, 0);
1900 2210
1901 av_push (av_async_pool, newSVsv (coro_current)); 2211 av_push (av_async_pool, newSVsv (coro_current));
1902} 2212}
1903 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
1904 2262
1905MODULE = Coro::State PACKAGE = Coro::AIO 2263MODULE = Coro::State PACKAGE = Coro::AIO
1906 2264
1907SV * 2265void
1908_get_state () 2266_get_state (SV *self)
1909 CODE: 2267 PPCODE:
1910{ 2268{
1911 struct io_state *data; 2269 AV *defav = GvAV (PL_defgv);
1912 2270 AV *av = newAV ();
2271 int i;
1913 RETVAL = newSV (sizeof (struct io_state)); 2272 SV *data_sv = newSV (sizeof (struct io_state));
1914 data = (struct io_state *)SvPVX (RETVAL); 2273 struct io_state *data = (struct io_state *)SvPVX (data_sv);
1915 SvCUR_set (RETVAL, sizeof (struct io_state)); 2274 SvCUR_set (data_sv, sizeof (struct io_state));
1916 SvPOK_only (RETVAL); 2275 SvPOK_only (data_sv);
1917 2276
1918 data->errorno = errno; 2277 data->errorno = errno;
1919 data->laststype = PL_laststype; 2278 data->laststype = PL_laststype;
1920 data->laststatval = PL_laststatval; 2279 data->laststatval = PL_laststatval;
1921 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);
1922} 2292}
1923 OUTPUT:
1924 RETVAL
1925 2293
1926void 2294void
1927_set_state (char *data_) 2295_set_state (SV *state)
1928 PROTOTYPE: $ 2296 PROTOTYPE: $
1929 CODE: 2297 PPCODE:
1930{ 2298{
1931 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;
1932 2302
1933 errno = data->errorno; 2303 errno = data->errorno;
1934 PL_laststype = data->laststype; 2304 PL_laststype = data->laststype;
1935 PL_laststatval = data->laststatval; 2305 PL_laststatval = data->laststatval;
1936 PL_statcache = data->statcache; 2306 PL_statcache = data->statcache;
1937}
1938 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