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.199 by root, Sun Oct 7 13:53:37 2007 UTC vs.
Revision 1.252 by root, Tue Nov 4 12:08:44 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
133#endif 153#endif
134 154
135/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
136struct io_state 156struct io_state
137{ 157{
158 AV *res;
138 int errorno; 159 int errorno;
139 I32 laststype; 160 I32 laststype;
140 int laststatval; 161 int laststatval;
141 Stat_t statcache; 162 Stat_t statcache;
142}; 163};
143 164
165static double (*nvtime)(); /* so why doesn't it take void? */
166
144static size_t coro_stacksize = CORO_STACKSIZE; 167static size_t coro_stacksize = CORO_STACKSIZE;
145static struct CoroAPI coroapi; 168static struct CoroAPI coroapi;
146static AV *main_mainstack; /* used to differentiate between $main and others */ 169static AV *main_mainstack; /* used to differentiate between $main and others */
147static JMPENV *main_top_env; 170static JMPENV *main_top_env;
148static HV *coro_state_stash, *coro_stash; 171static HV *coro_state_stash, *coro_stash;
149static SV *coro_mortal; /* will be freed after next transfer */ 172static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
150 173
151static GV *irsgv; /* $/ */ 174static GV *irsgv; /* $/ */
152static GV *stdoutgv; /* *STDOUT */ 175static GV *stdoutgv; /* *STDOUT */
153static SV *sv_diehook; 176static SV *rv_diehook;
154static SV *sv_warnhook; 177static SV *rv_warnhook;
178static HV *hv_sig; /* %SIG */
155 179
156/* async_pool helper stuff */ 180/* async_pool helper stuff */
157static SV *sv_pool_rss; 181static SV *sv_pool_rss;
158static SV *sv_pool_size; 182static SV *sv_pool_size;
159static AV *av_async_pool; 183static AV *av_async_pool;
184
185/* Coro::AnyEvent */
186static SV *sv_activity;
160 187
161static struct coro_cctx *cctx_first; 188static struct coro_cctx *cctx_first;
162static int cctx_count, cctx_idle; 189static int cctx_count, cctx_idle;
163 190
164enum { 191enum {
217 244
218 /* process data */ 245 /* process data */
219 AV *mainstack; 246 AV *mainstack;
220 perl_slots *slot; /* basically the saved sp */ 247 perl_slots *slot; /* basically the saved sp */
221 248
222 /* data associated with this coroutine (initial args) */ 249 AV *args; /* data associated with this coroutine (initial args) */
223 AV *args; 250 int refcnt; /* coroutines are refcounted, yes */
224 int refcnt;
225 int flags; /* CF_ flags */ 251 int flags; /* CF_ flags */
252 HV *hv; /* the perl hash associated with this coro, if any */
226 253
227 /* statistics */ 254 /* statistics */
228 int usecount; /* number of transfers to this coro */ 255 int usecount; /* number of transfers to this coro */
229 256
230 /* coro process data */ 257 /* coro process data */
231 int prio; 258 int prio;
232 SV *throw; 259 SV *throw; /* exception to be thrown */
233 260
234 /* async_pool */ 261 /* async_pool */
235 SV *saved_deffh; 262 SV *saved_deffh;
236 263
237 /* linked list */ 264 /* linked list */
238 struct coro *next, *prev; 265 struct coro *next, *prev;
239 HV *hv; /* the perl hash associated with this coro, if any */
240}; 266};
241 267
242typedef struct coro *Coro__State; 268typedef struct coro *Coro__State;
243typedef struct coro *Coro__State_or_hashref; 269typedef struct coro *Coro__State_or_hashref;
244 270
251#define PRIO_IDLE -3 277#define PRIO_IDLE -3
252#define PRIO_MIN -4 278#define PRIO_MIN -4
253 279
254/* for Coro.pm */ 280/* for Coro.pm */
255static SV *coro_current; 281static SV *coro_current;
282static SV *coro_readyhook;
256static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 283static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
257static int coro_nready; 284static int coro_nready;
258static struct coro *coro_first; 285static struct coro *coro_first;
259 286
260/** lowlevel stuff **********************************************************/ 287/** lowlevel stuff **********************************************************/
261 288
262static SV * 289static SV *
263coro_get_sv (const char *name, int create) 290coro_get_sv (pTHX_ const char *name, int create)
264{ 291{
265#if PERL_VERSION_ATLEAST (5,9,0) 292#if PERL_VERSION_ATLEAST (5,10,0)
266 /* 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 */
267 get_sv (name, create); 294 get_sv (name, create);
268#endif 295#endif
269 return get_sv (name, create); 296 return get_sv (name, create);
270} 297}
271 298
272static SV * 299static AV *
273coro_get_av (const char *name, int create) 300coro_get_av (pTHX_ const char *name, int create)
274{ 301{
275#if PERL_VERSION_ATLEAST (5,9,0) 302#if PERL_VERSION_ATLEAST (5,10,0)
276 /* 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 */
277 get_av (name, create); 304 get_av (name, create);
278#endif 305#endif
279 return get_av (name, create); 306 return get_av (name, create);
280} 307}
281 308
309static HV *
310coro_get_hv (pTHX_ const char *name, int create)
311{
312#if PERL_VERSION_ATLEAST (5,10,0)
313 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
314 get_hv (name, create);
315#endif
316 return get_hv (name, create);
317}
318
282static AV * 319static AV *
283coro_clone_padlist (pTHX_ CV *cv) 320coro_clone_padlist (pTHX_ CV *cv)
284{ 321{
285 AV *padlist = CvPADLIST (cv); 322 AV *padlist = CvPADLIST (cv);
286 AV *newpadlist, *newpad; 323 AV *newpadlist, *newpad;
287 324
288 newpadlist = newAV (); 325 newpadlist = newAV ();
289 AvREAL_off (newpadlist); 326 AvREAL_off (newpadlist);
290#if PERL_VERSION_ATLEAST (5,9,0) 327#if PERL_VERSION_ATLEAST (5,10,0)
291 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 328 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
292#else 329#else
293 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 330 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
294#endif 331#endif
295 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 332 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
296 --AvFILLp (padlist); 333 --AvFILLp (padlist);
297 334
298 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 335 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
299 av_store (newpadlist, 1, (SV *)newpad); 336 av_store (newpadlist, 1, (SV *)newpad);
300 337
301 return newpadlist; 338 return newpadlist;
302} 339}
303 340
333 370
334 /* casting is fun. */ 371 /* casting is fun. */
335 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 372 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
336 free_padlist (aTHX_ padlist); 373 free_padlist (aTHX_ padlist);
337 374
338 SvREFCNT_dec (av); 375 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
339 376
340 return 0; 377 return 0;
341} 378}
342 379
343#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
344 382
345static 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};
346 387
347#define CORO_MAGIC(cv) \ 388#define CORO_MAGIC(sv,type) \
348 SvMAGIC (cv) \ 389 SvMAGIC (sv) \
349 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 390 ? SvMAGIC (sv)->mg_type == type \
350 ? SvMAGIC (cv) \ 391 ? SvMAGIC (sv) \
351 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 392 : mg_find (sv, type) \
352 : 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)
353 397
354static struct coro * 398static struct coro *
355SvSTATE_ (pTHX_ SV *coro) 399SvSTATE_ (pTHX_ SV *coro)
356{ 400{
357 HV *stash; 401 HV *stash;
369 /* very slow, but rare, check */ 413 /* very slow, but rare, check */
370 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 414 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
371 croak ("Coro::State object required"); 415 croak ("Coro::State object required");
372 } 416 }
373 417
374 mg = CORO_MAGIC (coro); 418 mg = CORO_MAGIC_state (coro);
375 return (struct coro *)mg->mg_ptr; 419 return (struct coro *)mg->mg_ptr;
376} 420}
377 421
378#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 422#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
379 423
380/* the next two functions merely cache the padlists */ 424/* the next two functions merely cache the padlists */
381static void 425static void
382get_padlist (pTHX_ CV *cv) 426get_padlist (pTHX_ CV *cv)
383{ 427{
384 MAGIC *mg = CORO_MAGIC (cv); 428 MAGIC *mg = CORO_MAGIC_cv (cv);
385 AV *av; 429 AV *av;
386 430
387 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 431 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
388 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 432 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
389 else 433 else
390 { 434 {
391#if CORO_PREFER_PERL_FUNCTIONS 435#if CORO_PREFER_PERL_FUNCTIONS
392 /* this is probably cleaner, but also slower? */ 436 /* this is probably cleaner? but also slower! */
437 /* in practise, it seems to be less stable */
393 CV *cp = Perl_cv_clone (cv); 438 CV *cp = Perl_cv_clone (cv);
394 CvPADLIST (cv) = CvPADLIST (cp); 439 CvPADLIST (cv) = CvPADLIST (cp);
395 CvPADLIST (cp) = 0; 440 CvPADLIST (cp) = 0;
396 SvREFCNT_dec (cp); 441 SvREFCNT_dec (cp);
397#else 442#else
401} 446}
402 447
403static void 448static void
404put_padlist (pTHX_ CV *cv) 449put_padlist (pTHX_ CV *cv)
405{ 450{
406 MAGIC *mg = CORO_MAGIC (cv); 451 MAGIC *mg = CORO_MAGIC_cv (cv);
407 AV *av; 452 AV *av;
408 453
409 if (expect_false (!mg)) 454 if (expect_false (!mg))
410 { 455 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
411 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
412 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
413 mg->mg_virtual = &vtbl_coro;
414 mg->mg_obj = (SV *)newAV ();
415 }
416 456
417 av = (AV *)mg->mg_obj; 457 av = (AV *)mg->mg_obj;
418 458
419 if (expect_false (AvFILLp (av) >= AvMAX (av))) 459 if (expect_false (AvFILLp (av) >= AvMAX (av)))
420 av_extend (av, AvMAX (av) + 1); 460 av_extend (av, AvMAX (av) + 1);
486 526
487 if (expect_true (CvDEPTH (cv))) 527 if (expect_true (CvDEPTH (cv)))
488 { 528 {
489 EXTEND (SP, 3); 529 EXTEND (SP, 3);
490 PUSHs ((SV *)CvPADLIST (cv)); 530 PUSHs ((SV *)CvPADLIST (cv));
491 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 531 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
492 PUSHs ((SV *)cv); 532 PUSHs ((SV *)cv);
493 533
494 CvDEPTH (cv) = 0; 534 CvDEPTH (cv) = 0;
495 get_padlist (aTHX_ cv); 535 get_padlist (aTHX_ cv);
496 } 536 }
507 547
508 PUTBACK; 548 PUTBACK;
509 } 549 }
510 550
511 /* allocate some space on the context stack for our purposes */ 551 /* allocate some space on the context stack for our purposes */
552 /* we manually unroll here, as usually 2 slots is enough */
553 if (SLOT_COUNT >= 1) CXINC;
554 if (SLOT_COUNT >= 2) CXINC;
555 if (SLOT_COUNT >= 3) CXINC;
512 { 556 {
513 /* we manually unroll here, as usually 2 slots is enough */
514 int i; 557 int i;
515 if (SLOT_COUNT >= 1) CXINC;
516 if (SLOT_COUNT >= 2) CXINC;
517 if (SLOT_COUNT >= 3) CXINC;
518 for (i = 3; i < SLOT_COUNT; ++i) 558 for (i = 3; i < SLOT_COUNT; ++i)
519 CXINC; 559 CXINC;
520
521 cxstack_ix -= SLOT_COUNT; /* undo allocation */
522 } 560 }
561 cxstack_ix -= SLOT_COUNT; /* undo allocation */
523 562
524 c->mainstack = PL_mainstack; 563 c->mainstack = PL_mainstack;
525 564
526 { 565 {
527 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 566 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
577 616
578 New(54,PL_savestack,24,ANY); 617 New(54,PL_savestack,24,ANY);
579 PL_savestack_ix = 0; 618 PL_savestack_ix = 0;
580 PL_savestack_max = 24; 619 PL_savestack_max = 24;
581 620
582#if !PERL_VERSION_ATLEAST (5,9,0) 621#if !PERL_VERSION_ATLEAST (5,10,0)
583 New(54,PL_retstack,4,OP*); 622 New(54,PL_retstack,4,OP*);
584 PL_retstack_ix = 0; 623 PL_retstack_ix = 0;
585 PL_retstack_max = 4; 624 PL_retstack_max = 4;
586#endif 625#endif
587} 626}
610 649
611 Safefree (PL_tmps_stack); 650 Safefree (PL_tmps_stack);
612 Safefree (PL_markstack); 651 Safefree (PL_markstack);
613 Safefree (PL_scopestack); 652 Safefree (PL_scopestack);
614 Safefree (PL_savestack); 653 Safefree (PL_savestack);
615#if !PERL_VERSION_ATLEAST (5,9,0) 654#if !PERL_VERSION_ATLEAST (5,10,0)
616 Safefree (PL_retstack); 655 Safefree (PL_retstack);
617#endif 656#endif
618} 657}
619 658
620static size_t 659static size_t
636 #undef VAR 675 #undef VAR
637 } 676 }
638 else 677 else
639 slot = coro->slot; 678 slot = coro->slot;
640 679
680 if (slot)
681 {
641 rss += sizeof (slot->curstackinfo); 682 rss += sizeof (slot->curstackinfo);
642 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 683 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
643 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 *);
644 rss += slot->tmps_max * sizeof (SV *); 685 rss += slot->tmps_max * sizeof (SV *);
645 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 686 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
646 rss += slot->scopestack_max * sizeof (I32); 687 rss += slot->scopestack_max * sizeof (I32);
647 rss += slot->savestack_max * sizeof (ANY); 688 rss += slot->savestack_max * sizeof (ANY);
648 689
649#if !PERL_VERSION_ATLEAST (5,9,0) 690#if !PERL_VERSION_ATLEAST (5,10,0)
650 rss += slot->retstack_max * sizeof (OP *); 691 rss += slot->retstack_max * sizeof (OP *);
651#endif 692#endif
693 }
652 } 694 }
653 695
654 return rss; 696 return rss;
655} 697}
656 698
657/** 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}
658 790
659static void 791static void
660coro_setup (pTHX_ struct coro *coro) 792coro_setup (pTHX_ struct coro *coro)
661{ 793{
662 /* 794 /*
671 PL_curpm = 0; 803 PL_curpm = 0;
672 PL_curpad = 0; 804 PL_curpad = 0;
673 PL_localizing = 0; 805 PL_localizing = 0;
674 PL_dirty = 0; 806 PL_dirty = 0;
675 PL_restartop = 0; 807 PL_restartop = 0;
676 PL_diehook = SvREFCNT_inc (sv_diehook); 808#if PERL_VERSION_ATLEAST (5,10,0)
677 PL_warnhook = SvREFCNT_inc (sv_warnhook); 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);
678 815
679 GvSV (PL_defgv) = newSV (0); 816 GvSV (PL_defgv) = newSV (0);
680 GvAV (PL_defgv) = coro->args; coro->args = 0; 817 GvAV (PL_defgv) = coro->args; coro->args = 0;
681 GvSV (PL_errgv) = newSV (0); 818 GvSV (PL_errgv) = newSV (0);
682 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);
683 PL_rs = newSVsv (GvSV (irsgv)); 820 PL_rs = newSVsv (GvSV (irsgv));
684 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
685 822
686 { 823 {
687 dSP; 824 dSP;
688 LOGOP myop; 825 LOGOP myop;
689 826
697 PL_op = (OP *)&myop; 834 PL_op = (OP *)&myop;
698 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 835 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
699 SPAGAIN; 836 SPAGAIN;
700 } 837 }
701 838
702 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;
703} 845}
704 846
705static void 847static void
706coro_destroy (pTHX_ struct coro *coro) 848coro_destroy (pTHX_ struct coro *coro)
707{ 849{
780 : cx->blk_gimme == G_SCALAR ? bot + 1 922 : cx->blk_gimme == G_SCALAR ? bot + 1
781 : bot; 923 : bot;
782 924
783 av_extend (av, top - bot); 925 av_extend (av, top - bot);
784 while (bot < top) 926 while (bot < top)
785 av_push (av, SvREFCNT_inc (*bot++)); 927 av_push (av, SvREFCNT_inc_NN (*bot++));
786 928
787 PL_runops = RUNOPS_DEFAULT; 929 PL_runops = RUNOPS_DEFAULT;
788 ENTER; 930 ENTER;
789 SAVETMPS; 931 SAVETMPS;
790 EXTEND (SP, 3); 932 EXTEND (SP, 3);
828 SAVETMPS; 970 SAVETMPS;
829 EXTEND (SP, 3); 971 EXTEND (SP, 3);
830 PUSHMARK (SP); 972 PUSHMARK (SP);
831 PUSHs (&PL_sv_yes); 973 PUSHs (&PL_sv_yes);
832 PUSHs (fullname); 974 PUSHs (fullname);
833 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);
834 PUTBACK; 976 PUTBACK;
835 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 977 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
836 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);
837 SPAGAIN; 979 SPAGAIN;
838 FREETMPS; 980 FREETMPS;
874 1016
875/* inject a fake call to Coro::State::_cctx_init into the execution */ 1017/* inject a fake call to Coro::State::_cctx_init into the execution */
876/* _cctx_init should be careful, as it could be called at almost any time */ 1018/* _cctx_init should be careful, as it could be called at almost any time */
877/* during execution of a perl program */ 1019/* during execution of a perl program */
878static void NOINLINE 1020static void NOINLINE
879prepare_cctx (pTHX_ coro_cctx *cctx) 1021cctx_prepare (pTHX_ coro_cctx *cctx)
880{ 1022{
881 dSP; 1023 dSP;
882 LOGOP myop; 1024 LOGOP myop;
883 1025
884 PL_top_env = &PL_start_env; 1026 PL_top_env = &PL_start_env;
902 1044
903/* 1045/*
904 * this is a _very_ stripped down perl interpreter ;) 1046 * this is a _very_ stripped down perl interpreter ;)
905 */ 1047 */
906static void 1048static void
907coro_run (void *arg) 1049cctx_run (void *arg)
908{ 1050{
909 dTHX; 1051 dTHX;
910 1052
911 /* coro_run is the alternative tail of transfer(), so unlock here. */ 1053 /* cctx_run is the alternative tail of transfer(), so unlock here. */
912 UNLOCK; 1054 UNLOCK;
913 1055
914 /* we now skip the entersub that lead to transfer() */ 1056 /* we now skip the entersub that lead to transfer() */
915 PL_op = PL_op->op_next; 1057 PL_op = PL_op->op_next;
916 1058
917 /* inject a fake subroutine call to cctx_init */ 1059 /* inject a fake subroutine call to cctx_init */
918 prepare_cctx (aTHX_ (coro_cctx *)arg); 1060 cctx_prepare (aTHX_ (coro_cctx *)arg);
919 1061
920 /* somebody or something will hit me for both perl_run and PL_restartop */ 1062 /* somebody or something will hit me for both perl_run and PL_restartop */
921 PL_restartop = PL_op; 1063 PL_restartop = PL_op;
922 perl_run (PL_curinterp); 1064 perl_run (PL_curinterp);
923 1065
942 ++cctx_count; 1084 ++cctx_count;
943 1085
944 Newz (0, cctx, 1, coro_cctx); 1086 Newz (0, cctx, 1, coro_cctx);
945 1087
946#if HAVE_MMAP 1088#if HAVE_MMAP
947
948 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1089 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
949 /* mmap supposedly does allocate-on-write for us */ 1090 /* mmap supposedly does allocate-on-write for us */
950 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1091 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
951 1092
952 if (cctx->sptr != (void *)-1) 1093 if (cctx->sptr != (void *)-1)
973 stack_start = cctx->sptr; 1114 stack_start = cctx->sptr;
974 stack_size = cctx->ssize; 1115 stack_size = cctx->ssize;
975 } 1116 }
976 1117
977 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1118 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
978 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); 1119 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
979 1120
980 return cctx; 1121 return cctx;
981} 1122}
982 1123
983static void 1124static void
1055 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");
1056 1197
1057 if (expect_false (next->flags & CF_DESTROYED)) 1198 if (expect_false (next->flags & CF_DESTROYED))
1058 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");
1059 1200
1060 if (
1061#if PERL_VERSION_ATLEAST (5,9,0) 1201#if !PERL_VERSION_ATLEAST (5,10,0)
1062 expect_false (PL_parser)
1063#else
1064 expect_false (PL_lex_state != LEX_NOTPARSING) 1202 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1065#endif
1066 )
1067 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
1068 } 1205 }
1069} 1206}
1070 1207
1071/* always use the TRANSFER macro */ 1208/* always use the TRANSFER macro */
1072static void NOINLINE 1209static void NOINLINE
1073transfer (pTHX_ struct coro *prev, struct coro *next) 1210transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1074{ 1211{
1075 dSTACKLEVEL; 1212 dSTACKLEVEL;
1076 1213
1077 /* sometimes transfer is only called to set idle_sp */ 1214 /* sometimes transfer is only called to set idle_sp */
1078 if (expect_false (!next)) 1215 if (expect_false (!next))
1080 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1217 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1081 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1218 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1082 } 1219 }
1083 else if (expect_true (prev != next)) 1220 else if (expect_true (prev != next))
1084 { 1221 {
1222 static volatile int has_throw;
1085 coro_cctx *prev__cctx; 1223 coro_cctx *prev__cctx;
1086 1224
1087 if (expect_false (prev->flags & CF_NEW)) 1225 if (expect_false (prev->flags & CF_NEW))
1088 { 1226 {
1089 /* create a new empty context */ 1227 /* create a new empty context */
1095 prev->flags &= ~CF_RUNNING; 1233 prev->flags &= ~CF_RUNNING;
1096 next->flags |= CF_RUNNING; 1234 next->flags |= CF_RUNNING;
1097 1235
1098 LOCK; 1236 LOCK;
1099 1237
1238 /* first get rid of the old state */
1239 save_perl (aTHX_ prev);
1240
1100 if (expect_false (next->flags & CF_NEW)) 1241 if (expect_false (next->flags & CF_NEW))
1101 { 1242 {
1102 /* need to start coroutine */ 1243 /* need to start coroutine */
1103 next->flags &= ~CF_NEW; 1244 next->flags &= ~CF_NEW;
1104 /* first get rid of the old state */
1105 save_perl (aTHX_ prev);
1106 /* setup coroutine call */ 1245 /* setup coroutine call */
1107 coro_setup (aTHX_ next); 1246 coro_setup (aTHX_ next);
1108 } 1247 }
1109 else 1248 else
1110 {
1111 /* coroutine already started */
1112 save_perl (aTHX_ prev);
1113 load_perl (aTHX_ next); 1249 load_perl (aTHX_ next);
1114 }
1115 1250
1116 prev__cctx = prev->cctx; 1251 prev__cctx = prev->cctx;
1117 1252
1118 /* possibly "free" the cctx */ 1253 /* possibly "free" the cctx */
1119 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 ))
1120 { 1259 {
1121 /* 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 */
1122 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));
1123 1262
1124 prev->cctx = 0; 1263 prev->cctx = 0;
1125 1264
1126 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1265 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1127 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1266 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1128 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1267 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1268 if (!next->cctx)
1129 next->cctx = cctx_get (aTHX); 1269 next->cctx = cctx_get (aTHX);
1130 1270
1131 cctx_put (prev__cctx); 1271 cctx_put (prev__cctx);
1132 } 1272 }
1133 1273
1134 ++next->usecount; 1274 ++next->usecount;
1135 1275
1136 if (expect_true (!next->cctx)) 1276 if (expect_true (!next->cctx))
1137 next->cctx = cctx_get (aTHX); 1277 next->cctx = cctx_get (aTHX);
1278
1279 has_throw = !!next->throw;
1138 1280
1139 if (expect_false (prev__cctx != next->cctx)) 1281 if (expect_false (prev__cctx != next->cctx))
1140 { 1282 {
1141 prev__cctx->top_env = PL_top_env; 1283 prev__cctx->top_env = PL_top_env;
1142 PL_top_env = next->cctx->top_env; 1284 PL_top_env = next->cctx->top_env;
1144 } 1286 }
1145 1287
1146 free_coro_mortal (aTHX); 1288 free_coro_mortal (aTHX);
1147 UNLOCK; 1289 UNLOCK;
1148 1290
1149 if (expect_false (prev->throw || next->throw)) 1291 if (expect_false (has_throw))
1150 { 1292 {
1151 struct coro *coro = SvSTATE (coro_current); 1293 struct coro *coro = SvSTATE (coro_current);
1152 1294
1153 if (coro->throw) 1295 if (coro->throw)
1154 { 1296 {
1164struct transfer_args 1306struct transfer_args
1165{ 1307{
1166 struct coro *prev, *next; 1308 struct coro *prev, *next;
1167}; 1309};
1168 1310
1169#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1311#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1170#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1312#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1171 1313
1172/** high level stuff ********************************************************/ 1314/** high level stuff ********************************************************/
1173 1315
1174static int 1316static int
1268{ 1410{
1269 dTHX; 1411 dTHX;
1270 struct transfer_args ta; 1412 struct transfer_args ta;
1271 1413
1272 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1414 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1273 TRANSFER (ta); 1415 TRANSFER (ta, 1);
1274} 1416}
1275 1417
1276/** Coro ********************************************************************/ 1418/** Coro ********************************************************************/
1277 1419
1278static void 1420static void
1280{ 1422{
1281 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);
1282} 1424}
1283 1425
1284static SV * 1426static SV *
1285coro_deq (pTHX_ int min_prio) 1427coro_deq (pTHX)
1286{ 1428{
1287 int prio = PRIO_MAX - PRIO_MIN; 1429 int prio;
1288 1430
1289 min_prio -= PRIO_MIN;
1290 if (min_prio < 0)
1291 min_prio = 0;
1292
1293 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 1431 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1294 if (AvFILLp (coro_ready [prio]) >= 0) 1432 if (AvFILLp (coro_ready [prio]) >= 0)
1295 return av_shift (coro_ready [prio]); 1433 return av_shift (coro_ready [prio]);
1296 1434
1297 return 0; 1435 return 0;
1298} 1436}
1300static int 1438static int
1301api_ready (SV *coro_sv) 1439api_ready (SV *coro_sv)
1302{ 1440{
1303 dTHX; 1441 dTHX;
1304 struct coro *coro; 1442 struct coro *coro;
1443 SV *sv_hook;
1444 void (*xs_hook)(void);
1305 1445
1306 if (SvROK (coro_sv)) 1446 if (SvROK (coro_sv))
1307 coro_sv = SvRV (coro_sv); 1447 coro_sv = SvRV (coro_sv);
1308 1448
1309 coro = SvSTATE (coro_sv); 1449 coro = SvSTATE (coro_sv);
1312 return 0; 1452 return 0;
1313 1453
1314 coro->flags |= CF_READY; 1454 coro->flags |= CF_READY;
1315 1455
1316 LOCK; 1456 LOCK;
1457
1458 sv_hook = coro_nready ? 0 : coro_readyhook;
1459 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1460
1317 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1461 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1318 ++coro_nready; 1462 ++coro_nready;
1463
1319 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 ();
1320 1484
1321 return 1; 1485 return 1;
1322} 1486}
1323 1487
1324static int 1488static int
1334 SV *prev_sv, *next_sv; 1498 SV *prev_sv, *next_sv;
1335 1499
1336 for (;;) 1500 for (;;)
1337 { 1501 {
1338 LOCK; 1502 LOCK;
1339 next_sv = coro_deq (aTHX_ PRIO_MIN); 1503 next_sv = coro_deq (aTHX);
1340 1504
1341 /* nothing to schedule: call the idle handler */ 1505 /* nothing to schedule: call the idle handler */
1342 if (expect_false (!next_sv)) 1506 if (expect_false (!next_sv))
1343 { 1507 {
1344 dSP; 1508 dSP;
1348 SAVETMPS; 1512 SAVETMPS;
1349 1513
1350 PUSHMARK (SP); 1514 PUSHMARK (SP);
1351 PUTBACK; 1515 PUTBACK;
1352 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1516 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1517 SPAGAIN;
1353 1518
1354 FREETMPS; 1519 FREETMPS;
1355 LEAVE; 1520 LEAVE;
1356 continue; 1521 continue;
1357 } 1522 }
1412{ 1577{
1413 dTHX; 1578 dTHX;
1414 struct transfer_args ta; 1579 struct transfer_args ta;
1415 1580
1416 prepare_schedule (aTHX_ &ta); 1581 prepare_schedule (aTHX_ &ta);
1417 TRANSFER (ta); 1582 TRANSFER (ta, 1);
1418} 1583}
1419 1584
1420static int 1585static int
1421api_cede (void) 1586api_cede (void)
1422{ 1587{
1425 1590
1426 prepare_cede (aTHX_ &ta); 1591 prepare_cede (aTHX_ &ta);
1427 1592
1428 if (expect_true (ta.prev != ta.next)) 1593 if (expect_true (ta.prev != ta.next))
1429 { 1594 {
1430 TRANSFER (ta); 1595 TRANSFER (ta, 1);
1431 return 1; 1596 return 1;
1432 } 1597 }
1433 else 1598 else
1434 return 0; 1599 return 0;
1435} 1600}
1440 dTHX; 1605 dTHX;
1441 struct transfer_args ta; 1606 struct transfer_args ta;
1442 1607
1443 if (prepare_cede_notself (aTHX_ &ta)) 1608 if (prepare_cede_notself (aTHX_ &ta))
1444 { 1609 {
1445 TRANSFER (ta); 1610 TRANSFER (ta, 1);
1446 return 1; 1611 return 1;
1447 } 1612 }
1448 else 1613 else
1449 return 0; 1614 return 0;
1450} 1615}
1473 else 1638 else
1474 coro->slot->runops = RUNOPS_DEFAULT; 1639 coro->slot->runops = RUNOPS_DEFAULT;
1475 } 1640 }
1476} 1641}
1477 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
1478MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1735MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1479 1736
1480PROTOTYPES: DISABLE 1737PROTOTYPES: DISABLE
1481 1738
1482BOOT: 1739BOOT:
1487 BOOT_PAGESIZE; 1744 BOOT_PAGESIZE;
1488 1745
1489 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1746 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1490 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1747 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1491 1748
1492 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); 1749 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1493 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); 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;
1494 1752
1495 if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook); 1753 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1496 if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook); 1754 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1755 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1497 1756
1498 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1757 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1499 1758
1500 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1759 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1501 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1760 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1506 main_top_env = PL_top_env; 1765 main_top_env = PL_top_env;
1507 1766
1508 while (main_top_env->je_prev) 1767 while (main_top_env->je_prev)
1509 main_top_env = main_top_env->je_prev; 1768 main_top_env = main_top_env->je_prev;
1510 1769
1511 coroapi.ver = CORO_API_VERSION; 1770 coroapi.ver = CORO_API_VERSION;
1771 coroapi.rev = CORO_API_REVISION;
1512 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 }
1513 1782
1514 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1783 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1515} 1784}
1516 1785
1517SV * 1786SV *
1518new (char *klass, ...) 1787new (char *klass, ...)
1519 CODE: 1788 CODE:
1520{ 1789{
1521 struct coro *coro; 1790 struct coro *coro;
1791 MAGIC *mg;
1522 HV *hv; 1792 HV *hv;
1523 int i; 1793 int i;
1524 1794
1525 Newz (0, coro, 1, struct coro); 1795 Newz (0, coro, 1, struct coro);
1526 coro->args = newAV (); 1796 coro->args = newAV ();
1529 if (coro_first) coro_first->prev = coro; 1799 if (coro_first) coro_first->prev = coro;
1530 coro->next = coro_first; 1800 coro->next = coro_first;
1531 coro_first = coro; 1801 coro_first = coro;
1532 1802
1533 coro->hv = hv = newHV (); 1803 coro->hv = hv = newHV ();
1534 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;
1535 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1806 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1536 1807
1537 av_extend (coro->args, items - 1); 1808 av_extend (coro->args, items - 1);
1538 for (i = 1; i < items; i++) 1809 for (i = 1; i < items; i++)
1539 av_push (coro->args, newSVsv (ST (i))); 1810 av_push (coro->args, newSVsv (ST (i)));
1553 Coro::cede_notself = 4 1824 Coro::cede_notself = 4
1554 CODE: 1825 CODE:
1555{ 1826{
1556 struct transfer_args ta; 1827 struct transfer_args ta;
1557 1828
1829 PUTBACK;
1558 switch (ix) 1830 switch (ix)
1559 { 1831 {
1560 case 0: 1832 case 0:
1561 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1833 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1562 ta.next = 0; 1834 ta.next = 0;
1563 break; 1835 break;
1564 1836
1565 case 1: 1837 case 1:
1566 if (items != 2) 1838 if (items != 2)
1567 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);
1568 1840
1569 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1841 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1570 break; 1842 break;
1571 1843
1572 case 2: 1844 case 2:
1581 if (!prepare_cede_notself (aTHX_ &ta)) 1853 if (!prepare_cede_notself (aTHX_ &ta))
1582 XSRETURN_EMPTY; 1854 XSRETURN_EMPTY;
1583 1855
1584 break; 1856 break;
1585 } 1857 }
1858 SPAGAIN;
1586 1859
1587 BARRIER; 1860 BARRIER;
1861 PUTBACK;
1588 TRANSFER (ta); 1862 TRANSFER (ta, 0);
1589 1863 SPAGAIN; /* might be the sp of a different coroutine now */
1590 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1864 /* be extra careful not to ever do anything after TRANSFER */
1591 XSRETURN_YES;
1592} 1865}
1593 1866
1594bool 1867bool
1595_destroy (SV *coro_sv) 1868_destroy (SV *coro_sv)
1596 CODE: 1869 CODE:
1597 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1870 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1598 OUTPUT: 1871 OUTPUT:
1599 RETVAL 1872 RETVAL
1600 1873
1601void 1874void
1602_exit (code) 1875_exit (int code)
1603 int code
1604 PROTOTYPE: $ 1876 PROTOTYPE: $
1605 CODE: 1877 CODE:
1606 _exit (code); 1878 _exit (code);
1607 1879
1608int 1880int
1642call (Coro::State coro, SV *coderef) 1914call (Coro::State coro, SV *coderef)
1643 ALIAS: 1915 ALIAS:
1644 eval = 1 1916 eval = 1
1645 CODE: 1917 CODE:
1646{ 1918{
1647 if (coro->mainstack) 1919 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1648 { 1920 {
1649 struct coro temp; 1921 struct coro temp;
1650 1922
1651 if (!(coro->flags & CF_RUNNING)) 1923 if (!(coro->flags & CF_RUNNING))
1652 { 1924 {
1925 PUTBACK;
1653 save_perl (aTHX_ &temp); 1926 save_perl (aTHX_ &temp);
1654 load_perl (aTHX_ coro); 1927 load_perl (aTHX_ coro);
1655 } 1928 }
1656 1929
1657 { 1930 {
1658 dSP; 1931 dSP;
1659 ENTER; 1932 ENTER;
1660 SAVETMPS; 1933 SAVETMPS;
1934 PUTBACK;
1935 PUSHSTACK;
1661 PUSHMARK (SP); 1936 PUSHMARK (SP);
1662 PUTBACK;
1663 1937
1664 if (ix) 1938 if (ix)
1665 eval_sv (coderef, 0); 1939 eval_sv (coderef, 0);
1666 else 1940 else
1667 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1941 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1668 1942
1943 POPSTACK;
1669 SPAGAIN; 1944 SPAGAIN;
1670 FREETMPS; 1945 FREETMPS;
1671 LEAVE; 1946 LEAVE;
1672 PUTBACK; 1947 PUTBACK;
1673 } 1948 }
1674 1949
1675 if (!(coro->flags & CF_RUNNING)) 1950 if (!(coro->flags & CF_RUNNING))
1676 { 1951 {
1677 save_perl (aTHX_ coro); 1952 save_perl (aTHX_ coro);
1678 load_perl (aTHX_ &temp); 1953 load_perl (aTHX_ &temp);
1954 SPAGAIN;
1679 } 1955 }
1680 } 1956 }
1681} 1957}
1682 1958
1683SV * 1959SV *
1695 1971
1696void 1972void
1697api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1973api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1698 1974
1699SV * 1975SV *
1700has_stack (Coro::State coro) 1976has_cctx (Coro::State coro)
1701 PROTOTYPE: $ 1977 PROTOTYPE: $
1702 CODE: 1978 CODE:
1703 RETVAL = boolSV (!!coro->cctx); 1979 RETVAL = boolSV (!!coro->cctx);
1704 OUTPUT: 1980 OUTPUT:
1705 RETVAL 1981 RETVAL
1724 case 1: RETVAL = coro->usecount; break; 2000 case 1: RETVAL = coro->usecount; break;
1725 } 2001 }
1726 OUTPUT: 2002 OUTPUT:
1727 RETVAL 2003 RETVAL
1728 2004
2005void
2006force_cctx ()
2007 CODE:
2008 struct coro *coro = SvSTATE (coro_current);
2009 coro->cctx->idle_sp = 0;
2010
2011void
2012swap_defsv (Coro::State self)
2013 PROTOTYPE: $
2014 ALIAS:
2015 swap_defav = 1
2016 CODE:
2017 if (!self->slot)
2018 croak ("cannot swap state with coroutine that has no saved state");
2019 else
2020 {
2021 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv);
2022 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2023
2024 SV *tmp = *src; *src = *dst; *dst = tmp;
2025 }
1729 2026
1730MODULE = Coro::State PACKAGE = Coro 2027MODULE = Coro::State PACKAGE = Coro
1731 2028
1732BOOT: 2029BOOT:
1733{ 2030{
1734 int i; 2031 int i;
1735 2032
1736 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE);
1737 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE);
1738 av_async_pool = coro_get_av ("Coro::async_pool", TRUE); 2033 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2034 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2035 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1739 2036
1740 coro_current = coro_get_sv ("Coro::current", FALSE); 2037 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1741 SvREADONLY_on (coro_current); 2038 SvREADONLY_on (coro_current);
1742 2039
1743 coro_stash = gv_stashpv ("Coro", TRUE); 2040 coro_stash = gv_stashpv ("Coro", TRUE);
1744 2041
1745 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2042 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1773void 2070void
1774_set_current (SV *current) 2071_set_current (SV *current)
1775 PROTOTYPE: $ 2072 PROTOTYPE: $
1776 CODE: 2073 CODE:
1777 SvREFCNT_dec (SvRV (coro_current)); 2074 SvREFCNT_dec (SvRV (coro_current));
1778 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2075 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2076
2077void
2078_set_readyhook (SV *hook)
2079 PROTOTYPE: $
2080 CODE:
2081 LOCK;
2082 SvREFCNT_dec (coro_readyhook);
2083 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2084 UNLOCK;
1779 2085
1780int 2086int
1781prio (Coro::State coro, int newprio = 0) 2087prio (Coro::State coro, int newprio = 0)
1782 ALIAS: 2088 ALIAS:
1783 nice = 1 2089 nice = 1
1833 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 2139 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1834 AV *invoke_av; 2140 AV *invoke_av;
1835 int i, len; 2141 int i, len;
1836 2142
1837 if (!invoke) 2143 if (!invoke)
2144 {
2145 SV *old = PL_diehook;
2146 PL_diehook = 0;
2147 SvREFCNT_dec (old);
1838 croak ("\3async_pool terminate\2\n"); 2148 croak ("\3async_pool terminate\2\n");
2149 }
1839 2150
1840 SvREFCNT_dec (coro->saved_deffh); 2151 SvREFCNT_dec (coro->saved_deffh);
1841 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2152 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1842 2153
1843 hv_store (hv, "desc", sizeof ("desc") - 1, 2154 hv_store (hv, "desc", sizeof ("desc") - 1,
1844 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2155 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1845 2156
1846 invoke_av = (AV *)SvRV (invoke); 2157 invoke_av = (AV *)SvRV (invoke);
1850 2161
1851 if (len > 0) 2162 if (len > 0)
1852 { 2163 {
1853 av_fill (defav, len - 1); 2164 av_fill (defav, len - 1);
1854 for (i = 0; i < len; ++i) 2165 for (i = 0; i < len; ++i)
1855 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2166 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1856 } 2167 }
1857 2168
1858 SvREFCNT_dec (invoke); 2169 SvREFCNT_dec (invoke);
1859} 2170}
1860 2171
1869 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2180 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1870 coro->saved_deffh = 0; 2181 coro->saved_deffh = 0;
1871 2182
1872 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2183 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1873 || 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);
1874 croak ("\3async_pool terminate\2\n"); 2189 croak ("\3async_pool terminate\2\n");
2190 }
1875 2191
1876 av_clear (GvAV (PL_defgv)); 2192 av_clear (GvAV (PL_defgv));
1877 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 2193 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1878 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 2194 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1879 2195
1883 api_trace (coro_current, 0); 2199 api_trace (coro_current, 0);
1884 2200
1885 av_push (av_async_pool, newSVsv (coro_current)); 2201 av_push (av_async_pool, newSVsv (coro_current));
1886} 2202}
1887 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
1888 2252
1889MODULE = Coro::State PACKAGE = Coro::AIO 2253MODULE = Coro::State PACKAGE = Coro::AIO
1890 2254
1891SV * 2255void
1892_get_state () 2256_get_state (SV *self)
1893 CODE: 2257 PPCODE:
1894{ 2258{
1895 struct io_state *data; 2259 AV *defav = GvAV (PL_defgv);
1896 2260 AV *av = newAV ();
2261 int i;
1897 RETVAL = newSV (sizeof (struct io_state)); 2262 SV *data_sv = newSV (sizeof (struct io_state));
1898 data = (struct io_state *)SvPVX (RETVAL); 2263 struct io_state *data = (struct io_state *)SvPVX (data_sv);
1899 SvCUR_set (RETVAL, sizeof (struct io_state)); 2264 SvCUR_set (data_sv, sizeof (struct io_state));
1900 SvPOK_only (RETVAL); 2265 SvPOK_only (data_sv);
1901 2266
1902 data->errorno = errno; 2267 data->errorno = errno;
1903 data->laststype = PL_laststype; 2268 data->laststype = PL_laststype;
1904 data->laststatval = PL_laststatval; 2269 data->laststatval = PL_laststatval;
1905 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);
1906} 2282}
1907 OUTPUT:
1908 RETVAL
1909 2283
1910void 2284void
1911_set_state (char *data_) 2285_set_state (SV *state)
1912 PROTOTYPE: $ 2286 PROTOTYPE: $
1913 CODE: 2287 PPCODE:
1914{ 2288{
1915 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;
1916 2292
1917 errno = data->errorno; 2293 errno = data->errorno;
1918 PL_laststype = data->laststype; 2294 PL_laststype = data->laststype;
1919 PL_laststatval = data->laststatval; 2295 PL_laststatval = data->laststatval;
1920 PL_statcache = data->statcache; 2296 PL_statcache = data->statcache;
1921}
1922 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