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.207 by root, Tue Oct 9 22:29:27 2007 UTC vs.
Revision 1.251 by root, Thu Oct 30 09:44:31 2008 UTC

4#define PERL_EXT 4#define PERL_EXT
5 5
6#include "EXTERN.h" 6#include "EXTERN.h"
7#include "perl.h" 7#include "perl.h"
8#include "XSUB.h" 8#include "XSUB.h"
9#include "perliol.h"
9 10
10#include "patchlevel.h" 11#include "patchlevel.h"
11 12
12#include <stdio.h> 13#include <stdio.h>
13#include <errno.h> 14#include <errno.h>
14#include <assert.h> 15#include <assert.h>
16
17#ifdef WIN32
18# undef setjmp
19# undef longjmp
20# undef _exit
21# define setjmp _setjmp // deep magic, don't ask
22#else
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
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 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}
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_destroy (pTHX_ struct coro *coro)
722{ 849{
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;
1069 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1196 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
1070 1197
1071 if (expect_false (next->flags & CF_DESTROYED)) 1198 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"); 1199 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
1073 1200
1074 if (
1075#if PERL_VERSION_ATLEAST (5,9,0) 1201#if !PERL_VERSION_ATLEAST (5,10,0)
1076 expect_false (PL_parser)
1077#else
1078 expect_false (PL_lex_state != LEX_NOTPARSING) 1202 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1079#endif
1080 )
1081 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1203 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version");
1204#endif
1082 } 1205 }
1083} 1206}
1084 1207
1085/* always use the TRANSFER macro */ 1208/* always use the TRANSFER macro */
1086static void NOINLINE 1209static void NOINLINE
1087transfer (pTHX_ struct coro *prev, struct coro *next) 1210transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1088{ 1211{
1089 dSTACKLEVEL; 1212 dSTACKLEVEL;
1213 static volatile int has_throw;
1090 1214
1091 /* sometimes transfer is only called to set idle_sp */ 1215 /* sometimes transfer is only called to set idle_sp */
1092 if (expect_false (!next)) 1216 if (expect_false (!next))
1093 { 1217 {
1094 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1218 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1109 prev->flags &= ~CF_RUNNING; 1233 prev->flags &= ~CF_RUNNING;
1110 next->flags |= CF_RUNNING; 1234 next->flags |= CF_RUNNING;
1111 1235
1112 LOCK; 1236 LOCK;
1113 1237
1238 /* first get rid of the old state */
1239 save_perl (aTHX_ prev);
1240
1114 if (expect_false (next->flags & CF_NEW)) 1241 if (expect_false (next->flags & CF_NEW))
1115 { 1242 {
1116 /* need to start coroutine */ 1243 /* need to start coroutine */
1117 next->flags &= ~CF_NEW; 1244 next->flags &= ~CF_NEW;
1118 /* first get rid of the old state */
1119 save_perl (aTHX_ prev);
1120 /* setup coroutine call */ 1245 /* setup coroutine call */
1121 coro_setup (aTHX_ next); 1246 coro_setup (aTHX_ next);
1122 } 1247 }
1123 else 1248 else
1124 {
1125 /* coroutine already started */
1126 save_perl (aTHX_ prev);
1127 load_perl (aTHX_ next); 1249 load_perl (aTHX_ next);
1128 }
1129 1250
1130 prev__cctx = prev->cctx; 1251 prev__cctx = prev->cctx;
1131 1252
1132 /* possibly "free" the cctx */ 1253 /* possibly "free" the cctx */
1133 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) 1254 if (expect_true (
1255 prev__cctx->idle_sp == STACKLEVEL
1256 && !(prev__cctx->flags & CC_TRACE)
1257 && !force_cctx
1258 ))
1134 { 1259 {
1135 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1260 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1136 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1261 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
1137 1262
1138 prev->cctx = 0; 1263 prev->cctx = 0;
1148 1273
1149 ++next->usecount; 1274 ++next->usecount;
1150 1275
1151 if (expect_true (!next->cctx)) 1276 if (expect_true (!next->cctx))
1152 next->cctx = cctx_get (aTHX); 1277 next->cctx = cctx_get (aTHX);
1278
1279 has_throw = !!next->throw;
1153 1280
1154 if (expect_false (prev__cctx != next->cctx)) 1281 if (expect_false (prev__cctx != next->cctx))
1155 { 1282 {
1156 prev__cctx->top_env = PL_top_env; 1283 prev__cctx->top_env = PL_top_env;
1157 PL_top_env = next->cctx->top_env; 1284 PL_top_env = next->cctx->top_env;
1159 } 1286 }
1160 1287
1161 free_coro_mortal (aTHX); 1288 free_coro_mortal (aTHX);
1162 UNLOCK; 1289 UNLOCK;
1163 1290
1164 if (expect_false (prev->throw || next->throw)) 1291 if (expect_false (has_throw))
1165 { 1292 {
1166 struct coro *coro = SvSTATE (coro_current); 1293 struct coro *coro = SvSTATE (coro_current);
1167 1294
1168 if (coro->throw) 1295 if (coro->throw)
1169 { 1296 {
1179struct transfer_args 1306struct transfer_args
1180{ 1307{
1181 struct coro *prev, *next; 1308 struct coro *prev, *next;
1182}; 1309};
1183 1310
1184#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1311#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) 1312#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1186 1313
1187/** high level stuff ********************************************************/ 1314/** high level stuff ********************************************************/
1188 1315
1189static int 1316static int
1283{ 1410{
1284 dTHX; 1411 dTHX;
1285 struct transfer_args ta; 1412 struct transfer_args ta;
1286 1413
1287 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1414 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1288 TRANSFER (ta); 1415 TRANSFER (ta, 1);
1289} 1416}
1290 1417
1291/** Coro ********************************************************************/ 1418/** Coro ********************************************************************/
1292 1419
1293static void 1420static void
1295{ 1422{
1296 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1423 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
1297} 1424}
1298 1425
1299static SV * 1426static SV *
1300coro_deq (pTHX_ int min_prio) 1427coro_deq (pTHX)
1301{ 1428{
1302 int prio = PRIO_MAX - PRIO_MIN; 1429 int prio;
1303 1430
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; ) 1431 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1309 if (AvFILLp (coro_ready [prio]) >= 0) 1432 if (AvFILLp (coro_ready [prio]) >= 0)
1310 return av_shift (coro_ready [prio]); 1433 return av_shift (coro_ready [prio]);
1311 1434
1312 return 0; 1435 return 0;
1313} 1436}
1315static int 1438static int
1316api_ready (SV *coro_sv) 1439api_ready (SV *coro_sv)
1317{ 1440{
1318 dTHX; 1441 dTHX;
1319 struct coro *coro; 1442 struct coro *coro;
1443 SV *sv_hook;
1444 void (*xs_hook)(void);
1320 1445
1321 if (SvROK (coro_sv)) 1446 if (SvROK (coro_sv))
1322 coro_sv = SvRV (coro_sv); 1447 coro_sv = SvRV (coro_sv);
1323 1448
1324 coro = SvSTATE (coro_sv); 1449 coro = SvSTATE (coro_sv);
1327 return 0; 1452 return 0;
1328 1453
1329 coro->flags |= CF_READY; 1454 coro->flags |= CF_READY;
1330 1455
1331 LOCK; 1456 LOCK;
1457
1458 sv_hook = coro_nready ? 0 : coro_readyhook;
1459 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1460
1332 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1461 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1333 ++coro_nready; 1462 ++coro_nready;
1463
1334 UNLOCK; 1464 UNLOCK;
1465
1466 if (sv_hook)
1467 {
1468 dSP;
1469
1470 ENTER;
1471 SAVETMPS;
1472
1473 PUSHMARK (SP);
1474 PUTBACK;
1475 call_sv (sv_hook, G_DISCARD);
1476 SPAGAIN;
1477
1478 FREETMPS;
1479 LEAVE;
1480 }
1481
1482 if (xs_hook)
1483 xs_hook ();
1335 1484
1336 return 1; 1485 return 1;
1337} 1486}
1338 1487
1339static int 1488static int
1349 SV *prev_sv, *next_sv; 1498 SV *prev_sv, *next_sv;
1350 1499
1351 for (;;) 1500 for (;;)
1352 { 1501 {
1353 LOCK; 1502 LOCK;
1354 next_sv = coro_deq (aTHX_ PRIO_MIN); 1503 next_sv = coro_deq (aTHX);
1355 1504
1356 /* nothing to schedule: call the idle handler */ 1505 /* nothing to schedule: call the idle handler */
1357 if (expect_false (!next_sv)) 1506 if (expect_false (!next_sv))
1358 { 1507 {
1359 dSP; 1508 dSP;
1363 SAVETMPS; 1512 SAVETMPS;
1364 1513
1365 PUSHMARK (SP); 1514 PUSHMARK (SP);
1366 PUTBACK; 1515 PUTBACK;
1367 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1516 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1517 SPAGAIN;
1368 1518
1369 FREETMPS; 1519 FREETMPS;
1370 LEAVE; 1520 LEAVE;
1371 continue; 1521 continue;
1372 } 1522 }
1427{ 1577{
1428 dTHX; 1578 dTHX;
1429 struct transfer_args ta; 1579 struct transfer_args ta;
1430 1580
1431 prepare_schedule (aTHX_ &ta); 1581 prepare_schedule (aTHX_ &ta);
1432 TRANSFER (ta); 1582 TRANSFER (ta, 1);
1433} 1583}
1434 1584
1435static int 1585static int
1436api_cede (void) 1586api_cede (void)
1437{ 1587{
1440 1590
1441 prepare_cede (aTHX_ &ta); 1591 prepare_cede (aTHX_ &ta);
1442 1592
1443 if (expect_true (ta.prev != ta.next)) 1593 if (expect_true (ta.prev != ta.next))
1444 { 1594 {
1445 TRANSFER (ta); 1595 TRANSFER (ta, 1);
1446 return 1; 1596 return 1;
1447 } 1597 }
1448 else 1598 else
1449 return 0; 1599 return 0;
1450} 1600}
1455 dTHX; 1605 dTHX;
1456 struct transfer_args ta; 1606 struct transfer_args ta;
1457 1607
1458 if (prepare_cede_notself (aTHX_ &ta)) 1608 if (prepare_cede_notself (aTHX_ &ta))
1459 { 1609 {
1460 TRANSFER (ta); 1610 TRANSFER (ta, 1);
1461 return 1; 1611 return 1;
1462 } 1612 }
1463 else 1613 else
1464 return 0; 1614 return 0;
1465} 1615}
1488 else 1638 else
1489 coro->slot->runops = RUNOPS_DEFAULT; 1639 coro->slot->runops = RUNOPS_DEFAULT;
1490 } 1640 }
1491} 1641}
1492 1642
1643static int
1644coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1645{
1646 AV *padlist;
1647 AV *av = (AV *)mg->mg_obj;
1648
1649 abort ();
1650
1651 return 0;
1652}
1653
1654static MGVTBL coro_gensub_vtbl = {
1655 0, 0, 0, 0,
1656 coro_gensub_free
1657};
1658
1659/*****************************************************************************/
1660/* PerlIO::cede */
1661
1662typedef struct
1663{
1664 PerlIOBuf base;
1665 NV next, every;
1666} PerlIOCede;
1667
1668static IV
1669PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1670{
1671 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1672
1673 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1674 self->next = nvtime () + self->every;
1675
1676 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1677}
1678
1679static SV *
1680PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1681{
1682 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1683
1684 return newSVnv (self->every);
1685}
1686
1687static IV
1688PerlIOCede_flush (pTHX_ PerlIO *f)
1689{
1690 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1691 double now = nvtime ();
1692
1693 if (now >= self->next)
1694 {
1695 api_cede ();
1696 self->next = now + self->every;
1697 }
1698
1699 return PerlIOBuf_flush (aTHX_ f);
1700}
1701
1702static PerlIO_funcs PerlIO_cede =
1703{
1704 sizeof(PerlIO_funcs),
1705 "cede",
1706 sizeof(PerlIOCede),
1707 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1708 PerlIOCede_pushed,
1709 PerlIOBuf_popped,
1710 PerlIOBuf_open,
1711 PerlIOBase_binmode,
1712 PerlIOCede_getarg,
1713 PerlIOBase_fileno,
1714 PerlIOBuf_dup,
1715 PerlIOBuf_read,
1716 PerlIOBuf_unread,
1717 PerlIOBuf_write,
1718 PerlIOBuf_seek,
1719 PerlIOBuf_tell,
1720 PerlIOBuf_close,
1721 PerlIOCede_flush,
1722 PerlIOBuf_fill,
1723 PerlIOBase_eof,
1724 PerlIOBase_error,
1725 PerlIOBase_clearerr,
1726 PerlIOBase_setlinebuf,
1727 PerlIOBuf_get_base,
1728 PerlIOBuf_bufsiz,
1729 PerlIOBuf_get_ptr,
1730 PerlIOBuf_get_cnt,
1731 PerlIOBuf_set_ptrcnt,
1732};
1733
1734
1493MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1735MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1494 1736
1495PROTOTYPES: DISABLE 1737PROTOTYPES: DISABLE
1496 1738
1497BOOT: 1739BOOT:
1502 BOOT_PAGESIZE; 1744 BOOT_PAGESIZE;
1503 1745
1504 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1746 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1505 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1747 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1506 1748
1749 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1750 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1751 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1752
1507 hv_sig = coro_get_hv ("SIG", TRUE); 1753 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1508 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); 1754 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1509 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); 1755 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 1756
1514 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1757 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1515 1758
1516 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1759 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1517 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1760 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1522 main_top_env = PL_top_env; 1765 main_top_env = PL_top_env;
1523 1766
1524 while (main_top_env->je_prev) 1767 while (main_top_env->je_prev)
1525 main_top_env = main_top_env->je_prev; 1768 main_top_env = main_top_env->je_prev;
1526 1769
1527 coroapi.ver = CORO_API_VERSION; 1770 coroapi.ver = CORO_API_VERSION;
1771 coroapi.rev = CORO_API_REVISION;
1528 coroapi.transfer = api_transfer; 1772 coroapi.transfer = api_transfer;
1773
1774 {
1775 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1776
1777 if (!svp) croak ("Time::HiRes is required");
1778 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1779
1780 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1781 }
1529 1782
1530 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1783 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1531} 1784}
1532 1785
1533SV * 1786SV *
1534new (char *klass, ...) 1787new (char *klass, ...)
1535 CODE: 1788 CODE:
1536{ 1789{
1537 struct coro *coro; 1790 struct coro *coro;
1791 MAGIC *mg;
1538 HV *hv; 1792 HV *hv;
1539 int i; 1793 int i;
1540 1794
1541 Newz (0, coro, 1, struct coro); 1795 Newz (0, coro, 1, struct coro);
1542 coro->args = newAV (); 1796 coro->args = newAV ();
1545 if (coro_first) coro_first->prev = coro; 1799 if (coro_first) coro_first->prev = coro;
1546 coro->next = coro_first; 1800 coro->next = coro_first;
1547 coro_first = coro; 1801 coro_first = coro;
1548 1802
1549 coro->hv = hv = newHV (); 1803 coro->hv = hv = newHV ();
1550 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1804 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
1805 mg->mg_flags |= MGf_DUP;
1551 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1806 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1552 1807
1553 av_extend (coro->args, items - 1); 1808 av_extend (coro->args, items - 1);
1554 for (i = 1; i < items; i++) 1809 for (i = 1; i < items; i++)
1555 av_push (coro->args, newSVsv (ST (i))); 1810 av_push (coro->args, newSVsv (ST (i)));
1561# function to increase chances that they all will call transfer with the same 1816# function to increase chances that they all will call transfer with the same
1562# stack offset 1817# stack offset
1563void 1818void
1564_set_stacklevel (...) 1819_set_stacklevel (...)
1565 ALIAS: 1820 ALIAS:
1566 Coro::State::transfer = 1 1821 Coro::State::transfer = 1
1567 Coro::schedule = 2 1822 Coro::schedule = 2
1568 Coro::cede = 3 1823 Coro::cede = 3
1569 Coro::cede_notself = 4 1824 Coro::cede_notself = 4
1570 Coro::Event::next = 5
1571 Coro::Event::next_cancel = 6
1572 PPCODE: 1825 CODE:
1573{ 1826{
1574 struct transfer_args ta; 1827 struct transfer_args ta;
1575 int repeat = 0;
1576 1828
1577 do 1829 PUTBACK;
1830 switch (ix)
1578 { 1831 {
1579 switch (ix)
1580 {
1581 case 0: 1832 case 0:
1582 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1833 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1583 ta.next = 0; 1834 ta.next = 0;
1584 break; 1835 break;
1585 1836
1586 case 1: 1837 case 1:
1587 if (items != 2) 1838 if (items != 2)
1588 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1839 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1589 1840
1590 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1841 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1591 break; 1842 break;
1592 1843
1593 case 2: 1844 case 2:
1594 prepare_schedule (aTHX_ &ta); 1845 prepare_schedule (aTHX_ &ta);
1595 break; 1846 break;
1596 1847
1597 case 3: 1848 case 3:
1598 prepare_cede (aTHX_ &ta); 1849 prepare_cede (aTHX_ &ta);
1599 break; 1850 break;
1600 1851
1601 case 4: 1852 case 4:
1602 if (!prepare_cede_notself (aTHX_ &ta)) 1853 if (!prepare_cede_notself (aTHX_ &ta))
1603 XSRETURN_EMPTY; 1854 XSRETURN_EMPTY;
1604 1855
1605 break; 1856 break;
1606
1607 case 5:
1608 case 6:
1609 if (items != 1)
1610 croak ("Coro::Event::next (watcher) expects one argument, not %d", items);
1611
1612 {
1613 SV *ev = coroapi.coro_event_next (ST (0), ix == 6, GIMME_V != G_VOID);
1614
1615 if (ev)
1616 {
1617 if (GIMME_V != G_VOID)
1618 {
1619 XPUSHs (ev);
1620 XSRETURN (1);
1621 }
1622 else
1623 XSRETURN_EMPTY;
1624 }
1625 }
1626
1627 prepare_schedule (aTHX_ &ta);
1628 repeat = 1;
1629 break;
1630 }
1631
1632 BARRIER;
1633 TRANSFER (ta);
1634 BARRIER;
1635 } 1857 }
1636 while (repeat); 1858 SPAGAIN;
1637 1859
1638 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1860 BARRIER;
1639 XSRETURN_YES; 1861 PUTBACK;
1862 TRANSFER (ta, 0);
1863 SPAGAIN; /* might be the sp of a different coroutine now */
1864 /* be extra careful not to ever do anything after TRANSFER */
1640} 1865}
1641 1866
1642bool 1867bool
1643_destroy (SV *coro_sv) 1868_destroy (SV *coro_sv)
1644 CODE: 1869 CODE:
1645 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1870 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1646 OUTPUT: 1871 OUTPUT:
1647 RETVAL 1872 RETVAL
1648 1873
1649void 1874void
1650_exit (code) 1875_exit (int code)
1651 int code
1652 PROTOTYPE: $ 1876 PROTOTYPE: $
1653 CODE: 1877 CODE:
1654 _exit (code); 1878 _exit (code);
1655 1879
1656int 1880int
1690call (Coro::State coro, SV *coderef) 1914call (Coro::State coro, SV *coderef)
1691 ALIAS: 1915 ALIAS:
1692 eval = 1 1916 eval = 1
1693 CODE: 1917 CODE:
1694{ 1918{
1695 if (coro->mainstack) 1919 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1696 { 1920 {
1697 struct coro temp; 1921 struct coro temp;
1698 1922
1699 if (!(coro->flags & CF_RUNNING)) 1923 if (!(coro->flags & CF_RUNNING))
1700 { 1924 {
1925 PUTBACK;
1701 save_perl (aTHX_ &temp); 1926 save_perl (aTHX_ &temp);
1702 load_perl (aTHX_ coro); 1927 load_perl (aTHX_ coro);
1703 } 1928 }
1704 1929
1705 { 1930 {
1706 dSP; 1931 dSP;
1707 ENTER; 1932 ENTER;
1708 SAVETMPS; 1933 SAVETMPS;
1934 PUTBACK;
1935 PUSHSTACK;
1709 PUSHMARK (SP); 1936 PUSHMARK (SP);
1710 PUTBACK;
1711 1937
1712 if (ix) 1938 if (ix)
1713 eval_sv (coderef, 0); 1939 eval_sv (coderef, 0);
1714 else 1940 else
1715 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1941 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1716 1942
1943 POPSTACK;
1717 SPAGAIN; 1944 SPAGAIN;
1718 FREETMPS; 1945 FREETMPS;
1719 LEAVE; 1946 LEAVE;
1720 PUTBACK; 1947 PUTBACK;
1721 } 1948 }
1722 1949
1723 if (!(coro->flags & CF_RUNNING)) 1950 if (!(coro->flags & CF_RUNNING))
1724 { 1951 {
1725 save_perl (aTHX_ coro); 1952 save_perl (aTHX_ coro);
1726 load_perl (aTHX_ &temp); 1953 load_perl (aTHX_ &temp);
1954 SPAGAIN;
1727 } 1955 }
1728 } 1956 }
1729} 1957}
1730 1958
1731SV * 1959SV *
1743 1971
1744void 1972void
1745api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1973api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1746 1974
1747SV * 1975SV *
1748has_stack (Coro::State coro) 1976has_cctx (Coro::State coro)
1749 PROTOTYPE: $ 1977 PROTOTYPE: $
1750 CODE: 1978 CODE:
1751 RETVAL = boolSV (!!coro->cctx); 1979 RETVAL = boolSV (!!coro->cctx);
1752 OUTPUT: 1980 OUTPUT:
1753 RETVAL 1981 RETVAL
1772 case 1: RETVAL = coro->usecount; break; 2000 case 1: RETVAL = coro->usecount; break;
1773 } 2001 }
1774 OUTPUT: 2002 OUTPUT:
1775 RETVAL 2003 RETVAL
1776 2004
2005void
2006force_cctx ()
2007 CODE:
2008 struct coro *coro = SvSTATE (coro_current);
2009 coro->cctx->idle_sp = 0;
2010
2011void
2012throw (Coro::State self, SV *throw = &PL_sv_undef)
2013 PROTOTYPE: $;$
2014 CODE:
2015 SvREFCNT_dec (self->throw);
2016 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2017
2018void
2019swap_defsv (Coro::State self)
2020 PROTOTYPE: $
2021 ALIAS:
2022 swap_defav = 1
2023 CODE:
2024 if (!self->slot)
2025 croak ("cannot swap state with coroutine that has no saved state");
2026 else
2027 {
2028 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv);
2029 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2030
2031 SV *tmp = *src; *src = *dst; *dst = tmp;
2032 }
1777 2033
1778MODULE = Coro::State PACKAGE = Coro 2034MODULE = Coro::State PACKAGE = Coro
1779 2035
1780BOOT: 2036BOOT:
1781{ 2037{
1782 int i; 2038 int i;
1783 2039
1784 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE);
1785 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE);
1786 av_async_pool = coro_get_av ("Coro::async_pool", TRUE); 2040 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2041 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2042 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1787 2043
1788 coro_current = coro_get_sv ("Coro::current", FALSE); 2044 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1789 SvREADONLY_on (coro_current); 2045 SvREADONLY_on (coro_current);
1790 2046
1791 coro_stash = gv_stashpv ("Coro", TRUE); 2047 coro_stash = gv_stashpv ("Coro", TRUE);
1792 2048
1793 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2049 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1821void 2077void
1822_set_current (SV *current) 2078_set_current (SV *current)
1823 PROTOTYPE: $ 2079 PROTOTYPE: $
1824 CODE: 2080 CODE:
1825 SvREFCNT_dec (SvRV (coro_current)); 2081 SvREFCNT_dec (SvRV (coro_current));
1826 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2082 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2083
2084void
2085_set_readyhook (SV *hook)
2086 PROTOTYPE: $
2087 CODE:
2088 LOCK;
2089 SvREFCNT_dec (coro_readyhook);
2090 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2091 UNLOCK;
1827 2092
1828int 2093int
1829prio (Coro::State coro, int newprio = 0) 2094prio (Coro::State coro, int newprio = 0)
1830 ALIAS: 2095 ALIAS:
1831 nice = 1 2096 nice = 1
1861 CODE: 2126 CODE:
1862 RETVAL = coro_nready; 2127 RETVAL = coro_nready;
1863 OUTPUT: 2128 OUTPUT:
1864 RETVAL 2129 RETVAL
1865 2130
1866void
1867throw (Coro::State self, SV *throw = &PL_sv_undef)
1868 PROTOTYPE: $;$
1869 CODE:
1870 SvREFCNT_dec (self->throw);
1871 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1872
1873# for async_pool speedup 2131# for async_pool speedup
1874void 2132void
1875_pool_1 (SV *cb) 2133_pool_1 (SV *cb)
1876 CODE: 2134 CODE:
1877{ 2135{
1878 struct coro *coro = SvSTATE (coro_current); 2136 struct coro *coro = SvSTATE (coro_current);
1879 HV *hv = (HV *)SvRV (coro_current); 2137 HV *hv = (HV *)SvRV (coro_current);
1880 AV *defav = GvAV (PL_defgv); 2138 AV *defav = GvAV (PL_defgv);
1881 SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); 2139 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1882 AV *invoke_av; 2140 AV *invoke_av;
1883 int i, len; 2141 int i, len;
1884 2142
1885 if (!invoke) 2143 if (!invoke)
2144 {
2145 SV *old = PL_diehook;
2146 PL_diehook = 0;
2147 SvREFCNT_dec (old);
1886 croak ("\3async_pool terminate\2\n"); 2148 croak ("\3async_pool terminate\2\n");
2149 }
1887 2150
1888 SvREFCNT_dec (coro->saved_deffh); 2151 SvREFCNT_dec (coro->saved_deffh);
1889 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2152 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1890 2153
1891 hv_store (hv, "desc", sizeof ("desc") - 1, 2154 hv_store (hv, "desc", sizeof ("desc") - 1,
1892 newSVpvn (strpair ("[async_pool]")), 0); 2155 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1893 2156
1894 invoke_av = (AV *)SvRV (invoke); 2157 invoke_av = (AV *)SvRV (invoke);
1895 len = av_len (invoke_av); 2158 len = av_len (invoke_av);
1896 2159
1897 sv_setsv (cb, AvARRAY (invoke_av)[0]); 2160 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1898 2161
1899 if (len > 0) 2162 if (len > 0)
1900 { 2163 {
1901 av_fill (defav, len - 1); 2164 av_fill (defav, len - 1);
1902 for (i = 0; i < len; ++i) 2165 for (i = 0; i < len; ++i)
1903 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2166 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1904 } 2167 }
1905 2168
1906 SvREFCNT_dec (invoke); 2169 SvREFCNT_dec (invoke);
1907} 2170}
1908 2171
1917 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2180 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1918 coro->saved_deffh = 0; 2181 coro->saved_deffh = 0;
1919 2182
1920 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2183 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1921 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2184 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2185 {
2186 SV *old = PL_diehook;
2187 PL_diehook = 0;
2188 SvREFCNT_dec (old);
1922 croak ("\3async_pool terminate\2\n"); 2189 croak ("\3async_pool terminate\2\n");
2190 }
1923 2191
1924 av_clear (GvAV (PL_defgv)); 2192 av_clear (GvAV (PL_defgv));
1925 hv_store ((HV *)SvRV (coro_current), strpair ("desc"), 2193 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1926 newSVpvn (strpair ("[async_pool idle]")), 0); 2194 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1927 2195
1928 coro->prio = 0; 2196 coro->prio = 0;
1929 2197
1930 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2198 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1931 api_trace (coro_current, 0); 2199 api_trace (coro_current, 0);
1932 2200
1933 av_push (av_async_pool, newSVsv (coro_current)); 2201 av_push (av_async_pool, newSVsv (coro_current));
1934} 2202}
1935 2203
2204#if 0
2205
2206void
2207_generator_call (...)
2208 PROTOTYPE: @
2209 PPCODE:
2210 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2211 xxxx
2212 abort ();
2213
2214SV *
2215gensub (SV *sub, ...)
2216 PROTOTYPE: &;@
2217 CODE:
2218{
2219 struct coro *coro;
2220 MAGIC *mg;
2221 CV *xcv;
2222 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2223 int i;
2224
2225 CvGV (ncv) = CvGV (cv);
2226 CvFILE (ncv) = CvFILE (cv);
2227
2228 Newz (0, coro, 1, struct coro);
2229 coro->args = newAV ();
2230 coro->flags = CF_NEW;
2231
2232 av_extend (coro->args, items - 1);
2233 for (i = 1; i < items; i++)
2234 av_push (coro->args, newSVsv (ST (i)));
2235
2236 CvISXSUB_on (ncv);
2237 CvXSUBANY (ncv).any_ptr = (void *)coro;
2238
2239 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2240
2241 CvXSUB (ncv) = CvXSUB (xcv);
2242 CvANON_on (ncv);
2243
2244 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2245 RETVAL = newRV_noinc ((SV *)ncv);
2246}
2247 OUTPUT:
2248 RETVAL
2249
2250#endif
2251
1936 2252
1937MODULE = Coro::State PACKAGE = Coro::AIO 2253MODULE = Coro::State PACKAGE = Coro::AIO
1938 2254
1939SV * 2255void
1940_get_state () 2256_get_state (SV *self)
1941 CODE: 2257 PPCODE:
1942{ 2258{
1943 struct io_state *data; 2259 AV *defav = GvAV (PL_defgv);
1944 2260 AV *av = newAV ();
2261 int i;
1945 RETVAL = newSV (sizeof (struct io_state)); 2262 SV *data_sv = newSV (sizeof (struct io_state));
1946 data = (struct io_state *)SvPVX (RETVAL); 2263 struct io_state *data = (struct io_state *)SvPVX (data_sv);
1947 SvCUR_set (RETVAL, sizeof (struct io_state)); 2264 SvCUR_set (data_sv, sizeof (struct io_state));
1948 SvPOK_only (RETVAL); 2265 SvPOK_only (data_sv);
1949 2266
1950 data->errorno = errno; 2267 data->errorno = errno;
1951 data->laststype = PL_laststype; 2268 data->laststype = PL_laststype;
1952 data->laststatval = PL_laststatval; 2269 data->laststatval = PL_laststatval;
1953 data->statcache = PL_statcache; 2270 data->statcache = PL_statcache;
2271
2272 av_extend (av, AvFILLp (defav) + 1 + 1);
2273
2274 for (i = 0; i <= AvFILLp (defav); ++i)
2275 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2276
2277 av_push (av, data_sv);
2278
2279 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2280
2281 api_ready (self);
1954} 2282}
1955 OUTPUT:
1956 RETVAL
1957 2283
1958void 2284void
1959_set_state (char *data_) 2285_set_state (SV *state)
1960 PROTOTYPE: $ 2286 PROTOTYPE: $
1961 CODE: 2287 PPCODE:
1962{ 2288{
1963 struct io_state *data = (void *)data_; 2289 AV *av = (AV *)SvRV (state);
2290 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2291 int i;
1964 2292
1965 errno = data->errorno; 2293 errno = data->errorno;
1966 PL_laststype = data->laststype; 2294 PL_laststype = data->laststype;
1967 PL_laststatval = data->laststatval; 2295 PL_laststatval = data->laststatval;
1968 PL_statcache = data->statcache; 2296 PL_statcache = data->statcache;
1969}
1970 2297
2298 EXTEND (SP, AvFILLp (av));
2299 for (i = 0; i < AvFILLp (av); ++i)
2300 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2301}
2302
2303
2304MODULE = Coro::State PACKAGE = Coro::AnyEvent
2305
2306BOOT:
2307 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2308
2309SV *
2310_schedule (...)
2311 PROTOTYPE: @
2312 CODE:
2313{
2314 static int incede;
2315
2316 api_cede_notself ();
2317
2318 ++incede;
2319 while (coro_nready >= incede && api_cede ())
2320 ;
2321
2322 sv_setsv (sv_activity, &PL_sv_undef);
2323 if (coro_nready >= incede)
2324 {
2325 PUSHMARK (SP);
2326 PUTBACK;
2327 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2328 SPAGAIN;
2329 }
2330
2331 --incede;
2332}
2333
2334
2335MODULE = Coro::State PACKAGE = PerlIO::cede
2336
2337BOOT:
2338 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines