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.401 by root, Mon May 16 22:08:24 2011 UTC vs.
Revision 1.414 by root, Wed Jun 29 16:08:31 2011 UTC

12#include "perl.h" 12#include "perl.h"
13#include "XSUB.h" 13#include "XSUB.h"
14#include "perliol.h" 14#include "perliol.h"
15 15
16#include "schmorp.h" 16#include "schmorp.h"
17#include "ecb.h"
17 18
19#include <stddef.h>
18#include <stdio.h> 20#include <stdio.h>
19#include <errno.h> 21#include <errno.h>
20#include <assert.h> 22#include <assert.h>
21 23
22#ifndef SVs_PADSTALE 24#ifndef SVs_PADSTALE
31# define setjmp _setjmp /* deep magic */ 33# define setjmp _setjmp /* deep magic */
32#else 34#else
33# include <inttypes.h> /* most portable stdint.h */ 35# include <inttypes.h> /* most portable stdint.h */
34#endif 36#endif
35 37
36#ifdef HAVE_MMAP 38#if HAVE_MMAP
37# include <unistd.h> 39# include <unistd.h>
38# include <sys/mman.h> 40# include <sys/mman.h>
39# ifndef MAP_ANONYMOUS 41# ifndef MAP_ANONYMOUS
40# ifdef MAP_ANON 42# ifdef MAP_ANON
41# define MAP_ANONYMOUS MAP_ANON 43# define MAP_ANONYMOUS MAP_ANON
61#endif 63#endif
62 64
63/* the maximum number of idle cctx that will be pooled */ 65/* the maximum number of idle cctx that will be pooled */
64static int cctx_max_idle = 4; 66static int cctx_max_idle = 4;
65 67
68#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
69# define HAS_SCOPESTACK_NAME 1
70#endif
71
66#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 72#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
67# undef CORO_STACKGUARD 73# undef CORO_STACKGUARD
68#endif 74#endif
69 75
70#ifndef CORO_STACKGUARD 76#ifndef CORO_STACKGUARD
86# define STACKLEVEL ((void *)&stacklevel) 92# define STACKLEVEL ((void *)&stacklevel)
87#endif 93#endif
88 94
89#define IN_DESTRUCT PL_dirty 95#define IN_DESTRUCT PL_dirty
90 96
91#if __GNUC__ >= 3
92# define attribute(x) __attribute__(x)
93# define expect(expr,value) __builtin_expect ((expr), (value))
94# define INLINE static inline
95#else
96# define attribute(x)
97# define expect(expr,value) (expr)
98# define INLINE static
99#endif
100
101#define expect_false(expr) expect ((expr) != 0, 0)
102#define expect_true(expr) expect ((expr) != 0, 1)
103
104#define NOINLINE attribute ((noinline))
105
106#include "CoroAPI.h" 97#include "CoroAPI.h"
107#define GCoroAPI (&coroapi) /* very sneaky */ 98#define GCoroAPI (&coroapi) /* very sneaky */
108 99
109#ifdef USE_ITHREADS 100#ifdef USE_ITHREADS
110# if CORO_PTHREAD 101# if CORO_PTHREAD
111static void *coro_thx; 102static void *coro_thx;
112# endif 103# endif
113#endif 104#endif
105
106/* used in state.h */
107#define VAR(name,type) VARx(name, PL_ ## name, type)
114 108
115#ifdef __linux 109#ifdef __linux
116# include <time.h> /* for timespec */ 110# include <time.h> /* for timespec */
117# include <syscall.h> /* for SYS_* */ 111# include <syscall.h> /* for SYS_* */
118# ifdef SYS_clock_gettime 112# ifdef SYS_clock_gettime
217}; 211};
218 212
219/* the structure where most of the perl state is stored, overlaid on the cxstack */ 213/* the structure where most of the perl state is stored, overlaid on the cxstack */
220typedef struct 214typedef struct
221{ 215{
222 SV *defsv;
223 AV *defav;
224 SV *errsv;
225 SV *irsgv;
226 HV *hinthv;
227#define VAR(name,type) type name; 216#define VARx(name,expr,type) type name;
228# include "state.h" 217# include "state.h"
229#undef VAR 218#undef VARx
230} perl_slots; 219} perl_slots;
231 220
232// how many context stack entries do we need for perl_slots 221/* how many context stack entries do we need for perl_slots */
233#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 222#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
234 223
235/* this is a structure representing a perl-level coroutine */ 224/* this is a structure representing a perl-level coroutine */
236struct coro 225struct coro
237{ 226{
303static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 292static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
304static CV *cv_coro_run; 293static CV *cv_coro_run;
305static struct coro *coro_first; 294static struct coro *coro_first;
306#define coro_nready coroapi.nready 295#define coro_nready coroapi.nready
307 296
297/** JIT *********************************************************************/
298
299#if CORO_JIT
300 /* APPLE doesn't have HAVE_MMAP though */
301 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
302 #ifndef CORO_JIT_TYPE
303 #if __x86_64 && CORO_JIT_UNIXY
304 #define CORO_JIT_TYPE "amd64-unix"
305 #elif __i386 && CORO_JIT_UNIXY
306 #define CORO_JIT_TYPE "x86-unix"
307 #endif
308 #endif
309#endif
310
311#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP
312 #undef CORO_JIT
313#endif
314
315#if CORO_JIT
316 typedef void (*load_save_perl_slots_type)(perl_slots *);
317 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
318#endif
319
308/** Coro::Select ************************************************************/ 320/** Coro::Select ************************************************************/
309 321
310static OP *(*coro_old_pp_sselect) (pTHX); 322static OP *(*coro_old_pp_sselect) (pTHX);
311static SV *coro_select_select; 323static SV *coro_select_select;
312 324
327 339
328/** time stuff **************************************************************/ 340/** time stuff **************************************************************/
329 341
330#ifdef HAS_GETTIMEOFDAY 342#ifdef HAS_GETTIMEOFDAY
331 343
332static void 344ecb_inline void
333coro_u2time (pTHX_ UV ret[2]) 345coro_u2time (pTHX_ UV ret[2])
334{ 346{
335 struct timeval tv; 347 struct timeval tv;
336 gettimeofday (&tv, 0); 348 gettimeofday (&tv, 0);
337 349
338 ret [0] = tv.tv_sec; 350 ret [0] = tv.tv_sec;
339 ret [1] = tv.tv_usec; 351 ret [1] = tv.tv_usec;
340} 352}
341 353
342static double 354ecb_inline double
343coro_nvtime () 355coro_nvtime (void)
344{ 356{
345 struct timeval tv; 357 struct timeval tv;
346 gettimeofday (&tv, 0); 358 gettimeofday (&tv, 0);
347 359
348 return tv.tv_sec + tv.tv_usec * 1e-6; 360 return tv.tv_sec + tv.tv_usec * 1e-6;
349} 361}
350 362
351static void 363ecb_inline void
352time_init (pTHX) 364time_init (pTHX)
353{ 365{
354 nvtime = coro_nvtime; 366 nvtime = coro_nvtime;
355 u2time = coro_u2time; 367 u2time = coro_u2time;
356} 368}
357 369
358#else 370#else
359 371
360static void 372ecb_inline void
361time_init (pTHX) 373time_init (pTHX)
362{ 374{
363 SV **svp; 375 SV **svp;
364 376
365 require_pv ("Time/HiRes.pm"); 377 require_pv ("Time/HiRes.pm");
377 389
378#endif 390#endif
379 391
380/** lowlevel stuff **********************************************************/ 392/** lowlevel stuff **********************************************************/
381 393
382static SV * 394static SV * ecb_noinline
383coro_get_sv (pTHX_ const char *name, int create) 395coro_get_sv (pTHX_ const char *name, int create)
384{ 396{
385#if PERL_VERSION_ATLEAST (5,10,0) 397#if PERL_VERSION_ATLEAST (5,10,0)
386 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 398 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
387 get_sv (name, create); 399 get_sv (name, create);
388#endif 400#endif
389 return get_sv (name, create); 401 return get_sv (name, create);
390} 402}
391 403
392static AV * 404static AV * ecb_noinline
393coro_get_av (pTHX_ const char *name, int create) 405coro_get_av (pTHX_ const char *name, int create)
394{ 406{
395#if PERL_VERSION_ATLEAST (5,10,0) 407#if PERL_VERSION_ATLEAST (5,10,0)
396 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 408 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
397 get_av (name, create); 409 get_av (name, create);
398#endif 410#endif
399 return get_av (name, create); 411 return get_av (name, create);
400} 412}
401 413
402static HV * 414static HV * ecb_noinline
403coro_get_hv (pTHX_ const char *name, int create) 415coro_get_hv (pTHX_ const char *name, int create)
404{ 416{
405#if PERL_VERSION_ATLEAST (5,10,0) 417#if PERL_VERSION_ATLEAST (5,10,0)
406 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 418 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
407 get_hv (name, create); 419 get_hv (name, create);
408#endif 420#endif
409 return get_hv (name, create); 421 return get_hv (name, create);
410} 422}
411 423
412INLINE void 424ecb_inline void
413coro_times_update () 425coro_times_update (void)
414{ 426{
415#ifdef coro_clock_gettime 427#ifdef coro_clock_gettime
416 struct timespec ts; 428 struct timespec ts;
417 429
418 ts.tv_sec = ts.tv_nsec = 0; 430 ts.tv_sec = ts.tv_nsec = 0;
430 time_real [0] = tv [0]; 442 time_real [0] = tv [0];
431 time_real [1] = tv [1] * 1000; 443 time_real [1] = tv [1] * 1000;
432#endif 444#endif
433} 445}
434 446
435INLINE void 447ecb_inline void
436coro_times_add (struct coro *c) 448coro_times_add (struct coro *c)
437{ 449{
438 c->t_real [1] += time_real [1]; 450 c->t_real [1] += time_real [1];
439 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } 451 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
440 c->t_real [0] += time_real [0]; 452 c->t_real [0] += time_real [0];
442 c->t_cpu [1] += time_cpu [1]; 454 c->t_cpu [1] += time_cpu [1];
443 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } 455 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
444 c->t_cpu [0] += time_cpu [0]; 456 c->t_cpu [0] += time_cpu [0];
445} 457}
446 458
447INLINE void 459ecb_inline void
448coro_times_sub (struct coro *c) 460coro_times_sub (struct coro *c)
449{ 461{
450 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } 462 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
451 c->t_real [1] -= time_real [1]; 463 c->t_real [1] -= time_real [1];
452 c->t_real [0] -= time_real [0]; 464 c->t_real [0] -= time_real [0];
460/* magic glue */ 472/* magic glue */
461 473
462#define CORO_MAGIC_type_cv 26 474#define CORO_MAGIC_type_cv 26
463#define CORO_MAGIC_type_state PERL_MAGIC_ext 475#define CORO_MAGIC_type_state PERL_MAGIC_ext
464 476
465#define CORO_MAGIC_NN(sv, type) \ 477#define CORO_MAGIC_NN(sv, type) \
466 (expect_true (SvMAGIC (sv)->mg_type == type) \ 478 (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \
467 ? SvMAGIC (sv) \ 479 ? SvMAGIC (sv) \
468 : mg_find (sv, type)) 480 : mg_find (sv, type))
469 481
470#define CORO_MAGIC(sv, type) \ 482#define CORO_MAGIC(sv, type) \
471 (expect_true (SvMAGIC (sv)) \ 483 (ecb_expect_true (SvMAGIC (sv)) \
472 ? CORO_MAGIC_NN (sv, type) \ 484 ? CORO_MAGIC_NN (sv, type) \
473 : 0) 485 : 0)
474 486
475#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 487#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
476#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) 488#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
477 489
478INLINE MAGIC * 490ecb_inline MAGIC *
479SvSTATEhv_p (pTHX_ SV *coro) 491SvSTATEhv_p (pTHX_ SV *coro)
480{ 492{
481 MAGIC *mg; 493 MAGIC *mg;
482 494
483 if (expect_true ( 495 if (ecb_expect_true (
484 SvTYPE (coro) == SVt_PVHV 496 SvTYPE (coro) == SVt_PVHV
485 && (mg = CORO_MAGIC_state (coro)) 497 && (mg = CORO_MAGIC_state (coro))
486 && mg->mg_virtual == &coro_state_vtbl 498 && mg->mg_virtual == &coro_state_vtbl
487 )) 499 ))
488 return mg; 500 return mg;
489 501
490 return 0; 502 return 0;
491} 503}
492 504
493INLINE struct coro * 505ecb_inline struct coro *
494SvSTATE_ (pTHX_ SV *coro) 506SvSTATE_ (pTHX_ SV *coro)
495{ 507{
496 MAGIC *mg; 508 MAGIC *mg;
497 509
498 if (SvROK (coro)) 510 if (SvROK (coro))
499 coro = SvRV (coro); 511 coro = SvRV (coro);
500 512
501 mg = SvSTATEhv_p (coro); 513 mg = SvSTATEhv_p (aTHX_ coro);
502 if (!mg) 514 if (!mg)
503 croak ("Coro::State object required"); 515 croak ("Coro::State object required");
504 516
505 return (struct coro *)mg->mg_ptr; 517 return (struct coro *)mg->mg_ptr;
506} 518}
512#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 524#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
513 525
514/*****************************************************************************/ 526/*****************************************************************************/
515/* padlist management and caching */ 527/* padlist management and caching */
516 528
517static AV * 529ecb_inline AV *
518coro_derive_padlist (pTHX_ CV *cv) 530coro_derive_padlist (pTHX_ CV *cv)
519{ 531{
520 AV *padlist = CvPADLIST (cv); 532 AV *padlist = CvPADLIST (cv);
521 AV *newpadlist, *newpad; 533 AV *newpadlist, *newpad;
522 534
534 av_store (newpadlist, 1, (SV *)newpad); 546 av_store (newpadlist, 1, (SV *)newpad);
535 547
536 return newpadlist; 548 return newpadlist;
537} 549}
538 550
539static void 551ecb_inline void
540free_padlist (pTHX_ AV *padlist) 552free_padlist (pTHX_ AV *padlist)
541{ 553{
542 /* may be during global destruction */ 554 /* may be during global destruction */
543 if (!IN_DESTRUCT) 555 if (!IN_DESTRUCT)
544 { 556 {
587 0, 0, 0, 0, 599 0, 0, 0, 0,
588 coro_cv_free 600 coro_cv_free
589}; 601};
590 602
591/* the next two functions merely cache the padlists */ 603/* the next two functions merely cache the padlists */
592static void 604ecb_inline void
593get_padlist (pTHX_ CV *cv) 605get_padlist (pTHX_ CV *cv)
594{ 606{
595 MAGIC *mg = CORO_MAGIC_cv (cv); 607 MAGIC *mg = CORO_MAGIC_cv (cv);
596 AV *av; 608 AV *av;
597 609
598 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 610 if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
599 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 611 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
600 else 612 else
601 { 613 {
602#if CORO_PREFER_PERL_FUNCTIONS 614#if CORO_PREFER_PERL_FUNCTIONS
603 /* this is probably cleaner? but also slower! */ 615 /* this is probably cleaner? but also slower! */
610 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); 622 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
611#endif 623#endif
612 } 624 }
613} 625}
614 626
615static void 627ecb_inline void
616put_padlist (pTHX_ CV *cv) 628put_padlist (pTHX_ CV *cv)
617{ 629{
618 MAGIC *mg = CORO_MAGIC_cv (cv); 630 MAGIC *mg = CORO_MAGIC_cv (cv);
619 AV *av; 631 AV *av;
620 632
621 if (expect_false (!mg)) 633 if (ecb_expect_false (!mg))
622 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 634 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
623 635
624 av = (AV *)mg->mg_obj; 636 av = (AV *)mg->mg_obj;
625 637
626 if (expect_false (AvFILLp (av) >= AvMAX (av))) 638 if (ecb_expect_false (AvFILLp (av) >= AvMAX (av)))
627 av_extend (av, AvFILLp (av) + 1); 639 av_extend (av, AvFILLp (av) + 1);
628 640
629 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 641 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
630} 642}
631 643
676 } 688 }
677#endif 689#endif
678 } 690 }
679} 691}
680 692
681#define SWAP_SVS(coro) \ 693#define SWAP_SVS(coro) \
682 if (expect_false ((coro)->swap_sv)) \ 694 if (ecb_expect_false ((coro)->swap_sv)) \
683 swap_svs (aTHX_ (coro)) 695 swap_svs (aTHX_ (coro))
684 696
685static void 697static void
686on_enterleave_call (pTHX_ SV *cb); 698on_enterleave_call (pTHX_ SV *cb);
687 699
691 perl_slots *slot = c->slot; 703 perl_slots *slot = c->slot;
692 c->slot = 0; 704 c->slot = 0;
693 705
694 PL_mainstack = c->mainstack; 706 PL_mainstack = c->mainstack;
695 707
696 GvSV (PL_defgv) = slot->defsv; 708#if CORO_JIT
697 GvAV (PL_defgv) = slot->defav; 709 load_perl_slots (slot);
698 GvSV (PL_errgv) = slot->errsv; 710#else
699 GvSV (irsgv) = slot->irsgv;
700 GvHV (PL_hintgv) = slot->hinthv;
701
702 #define VAR(name,type) PL_ ## name = slot->name; 711 #define VARx(name,expr,type) expr = slot->name;
703 # include "state.h" 712 # include "state.h"
704 #undef VAR 713 #undef VARx
714#endif
705 715
706 { 716 {
707 dSP; 717 dSP;
708 718
709 CV *cv; 719 CV *cv;
710 720
711 /* now do the ugly restore mess */ 721 /* now do the ugly restore mess */
712 while (expect_true (cv = (CV *)POPs)) 722 while (ecb_expect_true (cv = (CV *)POPs))
713 { 723 {
714 put_padlist (aTHX_ cv); /* mark this padlist as available */ 724 put_padlist (aTHX_ cv); /* mark this padlist as available */
715 CvDEPTH (cv) = PTR2IV (POPs); 725 CvDEPTH (cv) = PTR2IV (POPs);
716 CvPADLIST (cv) = (AV *)POPs; 726 CvPADLIST (cv) = (AV *)POPs;
717 } 727 }
720 } 730 }
721 731
722 slf_frame = c->slf_frame; 732 slf_frame = c->slf_frame;
723 CORO_THROW = c->except; 733 CORO_THROW = c->except;
724 734
725 if (expect_false (enable_times)) 735 if (ecb_expect_false (enable_times))
726 { 736 {
727 if (expect_false (!times_valid)) 737 if (ecb_expect_false (!times_valid))
728 coro_times_update (); 738 coro_times_update ();
729 739
730 coro_times_sub (c); 740 coro_times_sub (c);
731 } 741 }
732 742
733 if (expect_false (c->on_enter)) 743 if (ecb_expect_false (c->on_enter))
734 { 744 {
735 int i; 745 int i;
736 746
737 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 747 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
738 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 748 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
744static void 754static void
745save_perl (pTHX_ Coro__State c) 755save_perl (pTHX_ Coro__State c)
746{ 756{
747 SWAP_SVS (c); 757 SWAP_SVS (c);
748 758
749 if (expect_false (c->on_leave)) 759 if (ecb_expect_false (c->on_leave))
750 { 760 {
751 int i; 761 int i;
752 762
753 for (i = AvFILLp (c->on_leave); i >= 0; --i) 763 for (i = AvFILLp (c->on_leave); i >= 0; --i)
754 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); 764 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
755 } 765 }
756 766
757 times_valid = 0; 767 times_valid = 0;
758 768
759 if (expect_false (enable_times)) 769 if (ecb_expect_false (enable_times))
760 { 770 {
761 coro_times_update (); times_valid = 1; 771 coro_times_update (); times_valid = 1;
762 coro_times_add (c); 772 coro_times_add (c);
763 } 773 }
764 774
778 788
779 XPUSHs (Nullsv); 789 XPUSHs (Nullsv);
780 /* this loop was inspired by pp_caller */ 790 /* this loop was inspired by pp_caller */
781 for (;;) 791 for (;;)
782 { 792 {
783 while (expect_true (cxix >= 0)) 793 while (ecb_expect_true (cxix >= 0))
784 { 794 {
785 PERL_CONTEXT *cx = &ccstk[cxix--]; 795 PERL_CONTEXT *cx = &ccstk[cxix--];
786 796
787 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) 797 if (ecb_expect_true (CxTYPE (cx) == CXt_SUB) || ecb_expect_false (CxTYPE (cx) == CXt_FORMAT))
788 { 798 {
789 CV *cv = cx->blk_sub.cv; 799 CV *cv = cx->blk_sub.cv;
790 800
791 if (expect_true (CvDEPTH (cv))) 801 if (ecb_expect_true (CvDEPTH (cv)))
792 { 802 {
793 EXTEND (SP, 3); 803 EXTEND (SP, 3);
794 PUSHs ((SV *)CvPADLIST (cv)); 804 PUSHs ((SV *)CvPADLIST (cv));
795 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 805 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
796 PUSHs ((SV *)cv); 806 PUSHs ((SV *)cv);
799 get_padlist (aTHX_ cv); 809 get_padlist (aTHX_ cv);
800 } 810 }
801 } 811 }
802 } 812 }
803 813
804 if (expect_true (top_si->si_type == PERLSI_MAIN)) 814 if (ecb_expect_true (top_si->si_type == PERLSI_MAIN))
805 break; 815 break;
806 816
807 top_si = top_si->si_prev; 817 top_si = top_si->si_prev;
808 ccstk = top_si->si_cxstack; 818 ccstk = top_si->si_cxstack;
809 cxix = top_si->si_cxix; 819 cxix = top_si->si_cxix;
811 821
812 PUTBACK; 822 PUTBACK;
813 } 823 }
814 824
815 /* allocate some space on the context stack for our purposes */ 825 /* allocate some space on the context stack for our purposes */
816 if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) 826 if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max))
817 { 827 {
818 unsigned int i; 828 unsigned int i;
819 829
820 for (i = 0; i < SLOT_COUNT; ++i) 830 for (i = 0; i < SLOT_COUNT; ++i)
821 CXINC; 831 CXINC;
826 c->mainstack = PL_mainstack; 836 c->mainstack = PL_mainstack;
827 837
828 { 838 {
829 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 839 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
830 840
831 slot->defav = GvAV (PL_defgv); 841#if CORO_JIT
832 slot->defsv = DEFSV; 842 save_perl_slots (slot);
833 slot->errsv = ERRSV; 843#else
834 slot->irsgv = GvSV (irsgv);
835 slot->hinthv = GvHV (PL_hintgv);
836
837 #define VAR(name,type) slot->name = PL_ ## name; 844 #define VARx(name,expr,type) slot->name = expr;
838 # include "state.h" 845 # include "state.h"
839 #undef VAR 846 #undef VARx
847#endif
840 } 848 }
841} 849}
842 850
843/* 851/*
844 * allocate various perl stacks. This is almost an exact copy 852 * allocate various perl stacks. This is almost an exact copy
875#endif 883#endif
876 884
877 New(54,PL_scopestack,8,I32); 885 New(54,PL_scopestack,8,I32);
878 PL_scopestack_ix = 0; 886 PL_scopestack_ix = 0;
879 PL_scopestack_max = 8; 887 PL_scopestack_max = 8;
888#if HAS_SCOPESTACK_NAME
889 New(54,PL_scopestack_name,8,const char*);
890#endif
880 891
881 New(54,PL_savestack,24,ANY); 892 New(54,PL_savestack,24,ANY);
882 PL_savestack_ix = 0; 893 PL_savestack_ix = 0;
883 PL_savestack_max = 24; 894 PL_savestack_max = 24;
884 895
912 } 923 }
913 924
914 Safefree (PL_tmps_stack); 925 Safefree (PL_tmps_stack);
915 Safefree (PL_markstack); 926 Safefree (PL_markstack);
916 Safefree (PL_scopestack); 927 Safefree (PL_scopestack);
928#if HAS_SCOPESTACK_NAME
929 Safefree (PL_scopestack_name);
930#endif
917 Safefree (PL_savestack); 931 Safefree (PL_savestack);
918#if !PERL_VERSION_ATLEAST (5,10,0) 932#if !PERL_VERSION_ATLEAST (5,10,0)
919 Safefree (PL_retstack); 933 Safefree (PL_retstack);
920#endif 934#endif
921} 935}
970 * This overrides the default magic get method of %SIG elements. 984 * This overrides the default magic get method of %SIG elements.
971 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 985 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
972 * and instead of trying to save and restore the hash elements (extremely slow), 986 * and instead of trying to save and restore the hash elements (extremely slow),
973 * we just provide our own readback here. 987 * we just provide our own readback here.
974 */ 988 */
975static int 989static int ecb_cold
976coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 990coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
977{ 991{
978 const char *s = MgPV_nolen_const (mg); 992 const char *s = MgPV_nolen_const (mg);
979 993
980 if (*s == '_') 994 if (*s == '_')
992 } 1006 }
993 1007
994 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1008 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
995} 1009}
996 1010
997static int 1011static int ecb_cold
998coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1012coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
999{ 1013{
1000 const char *s = MgPV_nolen_const (mg); 1014 const char *s = MgPV_nolen_const (mg);
1001 1015
1002 if (*s == '_') 1016 if (*s == '_')
1016 } 1030 }
1017 1031
1018 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; 1032 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
1019} 1033}
1020 1034
1021static int 1035static int ecb_cold
1022coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1036coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
1023{ 1037{
1024 const char *s = MgPV_nolen_const (mg); 1038 const char *s = MgPV_nolen_const (mg);
1025 1039
1026 if (*s == '_') 1040 if (*s == '_')
1061 return 1; 1075 return 1;
1062} 1076}
1063 1077
1064static UNOP init_perl_op; 1078static UNOP init_perl_op;
1065 1079
1066static void NOINLINE /* noinline to keep it out of the transfer fast path */ 1080ecb_noinline static void /* noinline to keep it out of the transfer fast path */
1067init_perl (pTHX_ struct coro *coro) 1081init_perl (pTHX_ struct coro *coro)
1068{ 1082{
1069 /* 1083 /*
1070 * emulate part of the perl startup here. 1084 * emulate part of the perl startup here.
1071 */ 1085 */
1133 /* copy throw, in case it was set before init_perl */ 1147 /* copy throw, in case it was set before init_perl */
1134 CORO_THROW = coro->except; 1148 CORO_THROW = coro->except;
1135 1149
1136 SWAP_SVS (coro); 1150 SWAP_SVS (coro);
1137 1151
1138 if (expect_false (enable_times)) 1152 if (ecb_expect_false (enable_times))
1139 { 1153 {
1140 coro_times_update (); 1154 coro_times_update ();
1141 coro_times_sub (coro); 1155 coro_times_sub (coro);
1142 } 1156 }
1143} 1157}
1186 /* restore swapped sv's */ 1200 /* restore swapped sv's */
1187 SWAP_SVS (coro); 1201 SWAP_SVS (coro);
1188 1202
1189 coro_destruct_stacks (aTHX); 1203 coro_destruct_stacks (aTHX);
1190 1204
1191 // now save some sv's to be free'd later 1205 /* now save some sv's to be free'd later */
1192 svf [0] = GvSV (PL_defgv); 1206 svf [0] = GvSV (PL_defgv);
1193 svf [1] = (SV *)GvAV (PL_defgv); 1207 svf [1] = (SV *)GvAV (PL_defgv);
1194 svf [2] = GvSV (PL_errgv); 1208 svf [2] = GvSV (PL_errgv);
1195 svf [3] = (SV *)PL_defoutgv; 1209 svf [3] = (SV *)PL_defoutgv;
1196 svf [4] = PL_rs; 1210 svf [4] = PL_rs;
1216 SvREFCNT_dec (coro->invoke_cb); 1230 SvREFCNT_dec (coro->invoke_cb);
1217 SvREFCNT_dec (coro->invoke_av); 1231 SvREFCNT_dec (coro->invoke_av);
1218 } 1232 }
1219} 1233}
1220 1234
1221INLINE void 1235ecb_inline void
1222free_coro_mortal (pTHX) 1236free_coro_mortal (pTHX)
1223{ 1237{
1224 if (expect_true (coro_mortal)) 1238 if (ecb_expect_true (coro_mortal))
1225 { 1239 {
1226 SvREFCNT_dec ((SV *)coro_mortal); 1240 SvREFCNT_dec ((SV *)coro_mortal);
1227 coro_mortal = 0; 1241 coro_mortal = 0;
1228 } 1242 }
1229} 1243}
1364 1378
1365 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1379 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1366} 1380}
1367 1381
1368/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1382/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1369static void NOINLINE 1383static void ecb_noinline
1370cctx_prepare (pTHX) 1384cctx_prepare (pTHX)
1371{ 1385{
1372 PL_top_env = &PL_start_env; 1386 PL_top_env = &PL_start_env;
1373 1387
1374 if (cctx_current->flags & CC_TRACE) 1388 if (cctx_current->flags & CC_TRACE)
1385 slf_frame.prepare = slf_prepare_set_stacklevel; 1399 slf_frame.prepare = slf_prepare_set_stacklevel;
1386 slf_frame.check = slf_check_set_stacklevel; 1400 slf_frame.check = slf_check_set_stacklevel;
1387} 1401}
1388 1402
1389/* the tail of transfer: execute stuff we can only do after a transfer */ 1403/* the tail of transfer: execute stuff we can only do after a transfer */
1390INLINE void 1404ecb_inline void
1391transfer_tail (pTHX) 1405transfer_tail (pTHX)
1392{ 1406{
1393 free_coro_mortal (aTHX); 1407 free_coro_mortal (aTHX);
1394} 1408}
1395 1409
1439 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1453 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1440 } 1454 }
1441} 1455}
1442 1456
1443static coro_cctx * 1457static coro_cctx *
1444cctx_new () 1458cctx_new (void)
1445{ 1459{
1446 coro_cctx *cctx; 1460 coro_cctx *cctx;
1447 1461
1448 ++cctx_count; 1462 ++cctx_count;
1449 New (0, cctx, 1, coro_cctx); 1463 New (0, cctx, 1, coro_cctx);
1455 return cctx; 1469 return cctx;
1456} 1470}
1457 1471
1458/* create a new cctx only suitable as source */ 1472/* create a new cctx only suitable as source */
1459static coro_cctx * 1473static coro_cctx *
1460cctx_new_empty () 1474cctx_new_empty (void)
1461{ 1475{
1462 coro_cctx *cctx = cctx_new (); 1476 coro_cctx *cctx = cctx_new ();
1463 1477
1464 cctx->sptr = 0; 1478 cctx->sptr = 0;
1465 coro_create (&cctx->cctx, 0, 0, 0, 0); 1479 coro_create (&cctx->cctx, 0, 0, 0, 0);
1467 return cctx; 1481 return cctx;
1468} 1482}
1469 1483
1470/* create a new cctx suitable as destination/running a perl interpreter */ 1484/* create a new cctx suitable as destination/running a perl interpreter */
1471static coro_cctx * 1485static coro_cctx *
1472cctx_new_run () 1486cctx_new_run (void)
1473{ 1487{
1474 coro_cctx *cctx = cctx_new (); 1488 coro_cctx *cctx = cctx_new ();
1475 void *stack_start; 1489 void *stack_start;
1476 size_t stack_size; 1490 size_t stack_size;
1477 1491
1478#if HAVE_MMAP 1492#if HAVE_MMAP
1479 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1493 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1480 /* mmap supposedly does allocate-on-write for us */ 1494 /* mmap supposedly does allocate-on-write for us */
1481 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1495 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0);
1482 1496
1483 if (cctx->sptr != (void *)-1) 1497 if (cctx->sptr != (void *)-1)
1484 { 1498 {
1485 #if CORO_STACKGUARD 1499 #if CORO_STACKGUARD
1486 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1500 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1547#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) 1561#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1548 1562
1549static coro_cctx * 1563static coro_cctx *
1550cctx_get (pTHX) 1564cctx_get (pTHX)
1551{ 1565{
1552 while (expect_true (cctx_first)) 1566 while (ecb_expect_true (cctx_first))
1553 { 1567 {
1554 coro_cctx *cctx = cctx_first; 1568 coro_cctx *cctx = cctx_first;
1555 cctx_first = cctx->next; 1569 cctx_first = cctx->next;
1556 --cctx_idle; 1570 --cctx_idle;
1557 1571
1558 if (expect_true (!CCTX_EXPIRED (cctx))) 1572 if (ecb_expect_true (!CCTX_EXPIRED (cctx)))
1559 return cctx; 1573 return cctx;
1560 1574
1561 cctx_destroy (cctx); 1575 cctx_destroy (cctx);
1562 } 1576 }
1563 1577
1568cctx_put (coro_cctx *cctx) 1582cctx_put (coro_cctx *cctx)
1569{ 1583{
1570 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); 1584 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1571 1585
1572 /* free another cctx if overlimit */ 1586 /* free another cctx if overlimit */
1573 if (expect_false (cctx_idle >= cctx_max_idle)) 1587 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
1574 { 1588 {
1575 coro_cctx *first = cctx_first; 1589 coro_cctx *first = cctx_first;
1576 cctx_first = first->next; 1590 cctx_first = first->next;
1577 --cctx_idle; 1591 --cctx_idle;
1578 1592
1589static void 1603static void
1590transfer_check (pTHX_ struct coro *prev, struct coro *next) 1604transfer_check (pTHX_ struct coro *prev, struct coro *next)
1591{ 1605{
1592 /* TODO: throwing up here is considered harmful */ 1606 /* TODO: throwing up here is considered harmful */
1593 1607
1594 if (expect_true (prev != next)) 1608 if (ecb_expect_true (prev != next))
1595 { 1609 {
1596 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1610 if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1597 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); 1611 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1598 1612
1599 if (expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) 1613 if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED)))
1600 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); 1614 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1601 1615
1602#if !PERL_VERSION_ATLEAST (5,10,0) 1616#if !PERL_VERSION_ATLEAST (5,10,0)
1603 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1617 if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING))
1604 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1618 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1605#endif 1619#endif
1606 } 1620 }
1607} 1621}
1608 1622
1609/* always use the TRANSFER macro */ 1623/* always use the TRANSFER macro */
1610static void NOINLINE /* noinline so we have a fixed stackframe */ 1624static void ecb_noinline /* noinline so we have a fixed stackframe */
1611transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1625transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1612{ 1626{
1613 dSTACKLEVEL; 1627 dSTACKLEVEL;
1614 1628
1615 /* sometimes transfer is only called to set idle_sp */ 1629 /* sometimes transfer is only called to set idle_sp */
1616 if (expect_false (!prev)) 1630 if (ecb_expect_false (!prev))
1617 { 1631 {
1618 cctx_current->idle_sp = STACKLEVEL; 1632 cctx_current->idle_sp = STACKLEVEL;
1619 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1633 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1620 } 1634 }
1621 else if (expect_true (prev != next)) 1635 else if (ecb_expect_true (prev != next))
1622 { 1636 {
1623 coro_cctx *cctx_prev; 1637 coro_cctx *cctx_prev;
1624 1638
1625 if (expect_false (prev->flags & CF_NEW)) 1639 if (ecb_expect_false (prev->flags & CF_NEW))
1626 { 1640 {
1627 /* create a new empty/source context */ 1641 /* create a new empty/source context */
1628 prev->flags &= ~CF_NEW; 1642 prev->flags &= ~CF_NEW;
1629 prev->flags |= CF_RUNNING; 1643 prev->flags |= CF_RUNNING;
1630 } 1644 }
1633 next->flags |= CF_RUNNING; 1647 next->flags |= CF_RUNNING;
1634 1648
1635 /* first get rid of the old state */ 1649 /* first get rid of the old state */
1636 save_perl (aTHX_ prev); 1650 save_perl (aTHX_ prev);
1637 1651
1638 if (expect_false (next->flags & CF_NEW)) 1652 if (ecb_expect_false (next->flags & CF_NEW))
1639 { 1653 {
1640 /* need to start coroutine */ 1654 /* need to start coroutine */
1641 next->flags &= ~CF_NEW; 1655 next->flags &= ~CF_NEW;
1642 /* setup coroutine call */ 1656 /* setup coroutine call */
1643 init_perl (aTHX_ next); 1657 init_perl (aTHX_ next);
1644 } 1658 }
1645 else 1659 else
1646 load_perl (aTHX_ next); 1660 load_perl (aTHX_ next);
1647 1661
1648 /* possibly untie and reuse the cctx */ 1662 /* possibly untie and reuse the cctx */
1649 if (expect_true ( 1663 if (ecb_expect_true (
1650 cctx_current->idle_sp == STACKLEVEL 1664 cctx_current->idle_sp == STACKLEVEL
1651 && !(cctx_current->flags & CC_TRACE) 1665 && !(cctx_current->flags & CC_TRACE)
1652 && !force_cctx 1666 && !force_cctx
1653 )) 1667 ))
1654 { 1668 {
1655 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1669 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1656 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); 1670 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1657 1671
1658 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ 1672 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1659 /* without this the next cctx_get might destroy the running cctx while still in use */ 1673 /* without this the next cctx_get might destroy the running cctx while still in use */
1660 if (expect_false (CCTX_EXPIRED (cctx_current))) 1674 if (ecb_expect_false (CCTX_EXPIRED (cctx_current)))
1661 if (expect_true (!next->cctx)) 1675 if (ecb_expect_true (!next->cctx))
1662 next->cctx = cctx_get (aTHX); 1676 next->cctx = cctx_get (aTHX);
1663 1677
1664 cctx_put (cctx_current); 1678 cctx_put (cctx_current);
1665 } 1679 }
1666 else 1680 else
1667 prev->cctx = cctx_current; 1681 prev->cctx = cctx_current;
1668 1682
1669 ++next->usecount; 1683 ++next->usecount;
1670 1684
1671 cctx_prev = cctx_current; 1685 cctx_prev = cctx_current;
1672 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); 1686 cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1673 1687
1674 next->cctx = 0; 1688 next->cctx = 0;
1675 1689
1676 if (expect_false (cctx_prev != cctx_current)) 1690 if (ecb_expect_false (cctx_prev != cctx_current))
1677 { 1691 {
1678 cctx_prev->top_env = PL_top_env; 1692 cctx_prev->top_env = PL_top_env;
1679 PL_top_env = cctx_current->top_env; 1693 PL_top_env = cctx_current->top_env;
1680 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); 1694 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1681 } 1695 }
1727 SvREFCNT_dec (coro->startcv); 1741 SvREFCNT_dec (coro->startcv);
1728 SvREFCNT_dec (coro->args); 1742 SvREFCNT_dec (coro->args);
1729 SvREFCNT_dec (coro->swap_sv); 1743 SvREFCNT_dec (coro->swap_sv);
1730 SvREFCNT_dec (CORO_THROW); 1744 SvREFCNT_dec (CORO_THROW);
1731 1745
1732 coro_call_on_destroy (coro); 1746 coro_call_on_destroy (aTHX_ coro);
1733 1747
1734 /* more destruction mayhem in coro_state_free */ 1748 /* more destruction mayhem in coro_state_free */
1735} 1749}
1736 1750
1737static int 1751static int
1738coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1752coro_state_free (pTHX_ SV *sv, MAGIC *mg)
1739{ 1753{
1740 struct coro *coro = (struct coro *)mg->mg_ptr; 1754 struct coro *coro = (struct coro *)mg->mg_ptr;
1741 mg->mg_ptr = 0; 1755 mg->mg_ptr = 0;
1742 1756
1743 coro_state_destroy (coro); 1757 coro_state_destroy (aTHX_ coro);
1744 SvREFCNT_dec (coro->on_destroy); 1758 SvREFCNT_dec (coro->on_destroy);
1745 SvREFCNT_dec (coro->status); 1759 SvREFCNT_dec (coro->status);
1746 1760
1747 Safefree (coro); 1761 Safefree (coro);
1748 1762
1749 return 0; 1763 return 0;
1750} 1764}
1751 1765
1752static int 1766static int ecb_cold
1753coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) 1767coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
1754{ 1768{
1755 /* called when perl clones the current process the slow way (windows process emulation) */ 1769 /* called when perl clones the current process the slow way (windows process emulation) */
1756 /* WE SIMply nuke the pointers in the copy, causing perl to croak */ 1770 /* WE SIMply nuke the pointers in the copy, causing perl to croak */
1757 mg->mg_ptr = 0; 1771 mg->mg_ptr = 0;
1788 TRANSFER (ta, 1); 1802 TRANSFER (ta, 1);
1789} 1803}
1790 1804
1791/** Coro ********************************************************************/ 1805/** Coro ********************************************************************/
1792 1806
1793INLINE void 1807ecb_inline void
1794coro_enq (pTHX_ struct coro *coro) 1808coro_enq (pTHX_ struct coro *coro)
1795{ 1809{
1796 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; 1810 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1797 1811
1798 SvREFCNT_inc_NN (coro->hv); 1812 SvREFCNT_inc_NN (coro->hv);
1800 coro->next_ready = 0; 1814 coro->next_ready = 0;
1801 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; 1815 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1802 ready [1] = coro; 1816 ready [1] = coro;
1803} 1817}
1804 1818
1805INLINE struct coro * 1819ecb_inline struct coro *
1806coro_deq (pTHX) 1820coro_deq (pTHX)
1807{ 1821{
1808 int prio; 1822 int prio;
1809 1823
1810 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) 1824 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1863{ 1877{
1864 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1878 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1865} 1879}
1866 1880
1867/* expects to own a reference to next->hv */ 1881/* expects to own a reference to next->hv */
1868INLINE void 1882ecb_inline void
1869prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1883prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1870{ 1884{
1871 SV *prev_sv = SvRV (coro_current); 1885 SV *prev_sv = SvRV (coro_current);
1872 1886
1873 ta->prev = SvSTATE_hv (prev_sv); 1887 ta->prev = SvSTATE_hv (prev_sv);
1886{ 1900{
1887 for (;;) 1901 for (;;)
1888 { 1902 {
1889 struct coro *next = coro_deq (aTHX); 1903 struct coro *next = coro_deq (aTHX);
1890 1904
1891 if (expect_true (next)) 1905 if (ecb_expect_true (next))
1892 { 1906 {
1893 /* cannot transfer to destroyed coros, skip and look for next */ 1907 /* cannot transfer to destroyed coros, skip and look for next */
1894 if (expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) 1908 if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED)))
1895 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ 1909 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1896 else 1910 else
1897 { 1911 {
1898 next->flags &= ~CF_READY; 1912 next->flags &= ~CF_READY;
1899 --coro_nready; 1913 --coro_nready;
1932 } 1946 }
1933 } 1947 }
1934 } 1948 }
1935} 1949}
1936 1950
1937INLINE void 1951ecb_inline void
1938prepare_cede (pTHX_ struct coro_transfer_args *ta) 1952prepare_cede (pTHX_ struct coro_transfer_args *ta)
1939{ 1953{
1940 api_ready (aTHX_ coro_current); 1954 api_ready (aTHX_ coro_current);
1941 prepare_schedule (aTHX_ ta); 1955 prepare_schedule (aTHX_ ta);
1942} 1956}
1943 1957
1944INLINE void 1958ecb_inline void
1945prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) 1959prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1946{ 1960{
1947 SV *prev = SvRV (coro_current); 1961 SV *prev = SvRV (coro_current);
1948 1962
1949 if (coro_nready) 1963 if (coro_nready)
1979{ 1993{
1980 struct coro_transfer_args ta; 1994 struct coro_transfer_args ta;
1981 1995
1982 prepare_cede (aTHX_ &ta); 1996 prepare_cede (aTHX_ &ta);
1983 1997
1984 if (expect_true (ta.prev != ta.next)) 1998 if (ecb_expect_true (ta.prev != ta.next))
1985 { 1999 {
1986 TRANSFER (ta, 1); 2000 TRANSFER (ta, 1);
1987 return 1; 2001 return 1;
1988 } 2002 }
1989 else 2003 else
2054 PUTBACK; 2068 PUTBACK;
2055 } 2069 }
2056} 2070}
2057 2071
2058static void 2072static void
2059coro_push_on_destroy (aTHX_ struct coro *coro, SV *cb) 2073coro_push_on_destroy (pTHX_ struct coro *coro, SV *cb)
2060{ 2074{
2061 if (!coro->on_destroy) 2075 if (!coro->on_destroy)
2062 coro->on_destroy = newAV (); 2076 coro->on_destroy = newAV ();
2063 2077
2064 av_push (coro->on_destroy, cb); 2078 av_push (coro->on_destroy, cb);
2078 if (!coro->status) 2092 if (!coro->status)
2079 return 1; 2093 return 1;
2080 2094
2081 frame->destroy = 0; 2095 frame->destroy = 0;
2082 2096
2083 coro_push_av (coro->status, GIMME_V); 2097 coro_push_av (aTHX_ coro->status, GIMME_V);
2084 2098
2085 SvREFCNT_dec ((SV *)coro->hv); 2099 SvREFCNT_dec ((SV *)coro->hv);
2086 2100
2087 return 0; 2101 return 0;
2088} 2102}
2202 coro = SvSTATE (arg [0]); 2216 coro = SvSTATE (arg [0]);
2203 coro_hv = coro->hv; 2217 coro_hv = coro->hv;
2204 2218
2205 coro_set_status (aTHX_ coro, arg + 1, items - 1); 2219 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2206 2220
2207 if (expect_false (coro->flags & CF_NOCANCEL)) 2221 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2208 { 2222 {
2209 /* coro currently busy cancelling something, so just notify it */ 2223 /* coro currently busy cancelling something, so just notify it */
2210 coro->slf_frame.data = (void *)coro; 2224 coro->slf_frame.data = (void *)coro;
2211 2225
2212 frame->prepare = prepare_nop; 2226 frame->prepare = prepare_nop;
2320slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2334slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2321{ 2335{
2322 HV *hv = (HV *)SvRV (coro_current); 2336 HV *hv = (HV *)SvRV (coro_current);
2323 struct coro *coro = SvSTATE_hv ((SV *)hv); 2337 struct coro *coro = SvSTATE_hv ((SV *)hv);
2324 2338
2325 if (expect_true (coro->saved_deffh)) 2339 if (ecb_expect_true (coro->saved_deffh))
2326 { 2340 {
2327 /* subsequent iteration */ 2341 /* subsequent iteration */
2328 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2342 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2329 coro->saved_deffh = 0; 2343 coro->saved_deffh = 0;
2330 2344
2596 I32 checkmark; /* mark SP to see how many elements check has pushed */ 2610 I32 checkmark; /* mark SP to see how many elements check has pushed */
2597 2611
2598 /* set up the slf frame, unless it has already been set-up */ 2612 /* set up the slf frame, unless it has already been set-up */
2599 /* the latter happens when a new coro has been started */ 2613 /* the latter happens when a new coro has been started */
2600 /* or when a new cctx was attached to an existing coroutine */ 2614 /* or when a new cctx was attached to an existing coroutine */
2601 if (expect_true (!slf_frame.prepare)) 2615 if (ecb_expect_true (!slf_frame.prepare))
2602 { 2616 {
2603 /* first iteration */ 2617 /* first iteration */
2604 dSP; 2618 dSP;
2605 SV **arg = PL_stack_base + TOPMARK + 1; 2619 SV **arg = PL_stack_base + TOPMARK + 1;
2606 int items = SP - arg; /* args without function object */ 2620 int items = SP - arg; /* args without function object */
2647 while (slf_frame.check (aTHX_ &slf_frame)); 2661 while (slf_frame.check (aTHX_ &slf_frame));
2648 2662
2649 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 2663 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
2650 2664
2651 /* exception handling */ 2665 /* exception handling */
2652 if (expect_false (CORO_THROW)) 2666 if (ecb_expect_false (CORO_THROW))
2653 { 2667 {
2654 SV *exception = sv_2mortal (CORO_THROW); 2668 SV *exception = sv_2mortal (CORO_THROW);
2655 2669
2656 CORO_THROW = 0; 2670 CORO_THROW = 0;
2657 sv_setsv (ERRSV, exception); 2671 sv_setsv (ERRSV, exception);
2659 } 2673 }
2660 2674
2661 /* return value handling - mostly like entersub */ 2675 /* return value handling - mostly like entersub */
2662 /* make sure we put something on the stack in scalar context */ 2676 /* make sure we put something on the stack in scalar context */
2663 if (GIMME_V == G_SCALAR 2677 if (GIMME_V == G_SCALAR
2664 && expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) 2678 && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1))
2665 { 2679 {
2666 dSP; 2680 dSP;
2667 SV **bot = PL_stack_base + checkmark; 2681 SV **bot = PL_stack_base + checkmark;
2668 2682
2669 if (sp == bot) /* too few, push undef */ 2683 if (sp == bot) /* too few, push undef */
2783{ 2797{
2784 PerlIOBuf base; 2798 PerlIOBuf base;
2785 NV next, every; 2799 NV next, every;
2786} PerlIOCede; 2800} PerlIOCede;
2787 2801
2788static IV 2802static IV ecb_cold
2789PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) 2803PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2790{ 2804{
2791 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2805 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2792 2806
2793 self->every = SvCUR (arg) ? SvNV (arg) : 0.01; 2807 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
2794 self->next = nvtime () + self->every; 2808 self->next = nvtime () + self->every;
2795 2809
2796 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); 2810 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
2797} 2811}
2798 2812
2799static SV * 2813static SV * ecb_cold
2800PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) 2814PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
2801{ 2815{
2802 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2816 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2803 2817
2804 return newSVnv (self->every); 2818 return newSVnv (self->every);
2949 { 2963 {
2950 int i; 2964 int i;
2951 /* if we were woken up but can't down, we look through the whole */ 2965 /* if we were woken up but can't down, we look through the whole */
2952 /* waiters list and only add us if we aren't in there already */ 2966 /* waiters list and only add us if we aren't in there already */
2953 /* this avoids some degenerate memory usage cases */ 2967 /* this avoids some degenerate memory usage cases */
2954 for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug 2968 for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */
2955 if (AvARRAY (av)[i] == coro_hv) 2969 if (AvARRAY (av)[i] == coro_hv)
2956 return 1; 2970 return 1;
2957 2971
2958 av_push (av, SvREFCNT_inc (coro_hv)); 2972 av_push (av, SvREFCNT_inc (coro_hv));
2959 return 1; 2973 return 1;
3216 dSP; 3230 dSP;
3217 3231
3218 static SV *prio_cv; 3232 static SV *prio_cv;
3219 static SV *prio_sv; 3233 static SV *prio_sv;
3220 3234
3221 if (expect_false (!prio_cv)) 3235 if (ecb_expect_false (!prio_cv))
3222 { 3236 {
3223 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); 3237 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0);
3224 prio_sv = newSViv (0); 3238 prio_sv = newSViv (0);
3225 } 3239 }
3226 3240
3332 } 3346 }
3333 3347
3334 return coro_sv; 3348 return coro_sv;
3335} 3349}
3336 3350
3351#ifndef __cplusplus
3352ecb_cold XS(boot_Coro__State);
3353#endif
3354
3355#if CORO_JIT
3356
3357static void ecb_noinline ecb_cold
3358pushav_4uv (pTHX_ UV a, UV b, UV c, UV d)
3359{
3360 dSP;
3361 AV *av = newAV ();
3362
3363 av_store (av, 3, newSVuv (d));
3364 av_store (av, 2, newSVuv (c));
3365 av_store (av, 1, newSVuv (b));
3366 av_store (av, 0, newSVuv (a));
3367
3368 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3369
3370 PUTBACK;
3371}
3372
3373static void ecb_noinline ecb_cold
3374jit_init (pTHX)
3375{
3376 dSP;
3377 SV *load, *save;
3378 char *map_base;
3379 char *load_ptr, *save_ptr;
3380 STRLEN load_len, save_len, map_len;
3381 int count;
3382
3383 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3384
3385 PUSHMARK (SP);
3386#define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3387# include "state.h"
3388#undef VARx
3389 count = call_pv ("Coro::State::_jit", G_ARRAY);
3390 SPAGAIN;
3391
3392 save = POPs; save_ptr = SvPVbyte (save, save_len);
3393 load = POPs; load_ptr = SvPVbyte (load, load_len);
3394
3395 map_len = load_len + save_len + 16;
3396
3397 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3398
3399 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3400
3401 load_perl_slots = (load_save_perl_slots_type)map_base;
3402 memcpy (map_base, load_ptr, load_len);
3403
3404 map_base += (load_len + 15) & ~15;
3405
3406 save_perl_slots = (load_save_perl_slots_type)map_base;
3407 memcpy (map_base, save_ptr, save_len);
3408
3409 /* we are good citizens and try to make the page read-only, so the evil evil */
3410 /* hackers might have it a bit more difficult */
3411 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3412
3413 PUTBACK;
3414 eval_pv ("undef &Coro::State::_jit", 1);
3415}
3416
3417#endif
3418
3337MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3419MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
3338 3420
3339PROTOTYPES: DISABLE 3421PROTOTYPES: DISABLE
3340 3422
3341BOOT: 3423BOOT:
3344# if CORO_PTHREAD 3426# if CORO_PTHREAD
3345 coro_thx = PERL_GET_CONTEXT; 3427 coro_thx = PERL_GET_CONTEXT;
3346# endif 3428# endif
3347#endif 3429#endif
3348 BOOT_PAGESIZE; 3430 BOOT_PAGESIZE;
3431
3432 /* perl defines these to check for existance first, but why it doesn't */
3433 /* just create them one at init time is not clear to me, except for */
3434 /* programs trying to delete them, but... */
3435 /* anyway, we declare this as invalid and make sure they are initialised here */
3436 DEFSV;
3437 ERRSV;
3349 3438
3350 cctx_current = cctx_new_empty (); 3439 cctx_current = cctx_new_empty ();
3351 3440
3352 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3441 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3353 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3442 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3396 coroapi.prepare_cede_notself = prepare_cede_notself; 3485 coroapi.prepare_cede_notself = prepare_cede_notself;
3397 3486
3398 time_init (aTHX); 3487 time_init (aTHX);
3399 3488
3400 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3489 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3490#if CORO_JIT
3491 PUTBACK;
3492 jit_init (aTHX);
3493 SPAGAIN;
3494#endif
3401} 3495}
3402 3496
3403SV * 3497SV *
3404new (SV *klass, ...) 3498new (SV *klass, ...)
3405 ALIAS: 3499 ALIAS:
3653times (Coro::State self) 3747times (Coro::State self)
3654 PPCODE: 3748 PPCODE:
3655{ 3749{
3656 struct coro *current = SvSTATE (coro_current); 3750 struct coro *current = SvSTATE (coro_current);
3657 3751
3658 if (expect_false (current == self)) 3752 if (ecb_expect_false (current == self))
3659 { 3753 {
3660 coro_times_update (); 3754 coro_times_update ();
3661 coro_times_add (SvSTATE (coro_current)); 3755 coro_times_add (SvSTATE (coro_current));
3662 } 3756 }
3663 3757
3664 EXTEND (SP, 2); 3758 EXTEND (SP, 2);
3665 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); 3759 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9)));
3666 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); 3760 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3667 3761
3668 if (expect_false (current == self)) 3762 if (ecb_expect_false (current == self))
3669 coro_times_sub (SvSTATE (coro_current)); 3763 coro_times_sub (SvSTATE (coro_current));
3670} 3764}
3671 3765
3672void 3766void
3673swap_sv (Coro::State coro, SV *sv, SV *swapsv) 3767swap_sv (Coro::State coro, SV *sv, SV *swapsv)
4055} 4149}
4056 4150
4057MODULE = Coro::State PACKAGE = Coro::SemaphoreSet 4151MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
4058 4152
4059void 4153void
4060_may_delete (SV *sem, int count, int extra_refs) 4154_may_delete (SV *sem, int count, unsigned int extra_refs)
4061 PPCODE: 4155 PPCODE:
4062{ 4156{
4063 AV *av = (AV *)SvRV (sem); 4157 AV *av = (AV *)SvRV (sem);
4064 4158
4065 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4159 if (SvREFCNT ((SV *)av) == 1 + extra_refs

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines