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.403 by root, Sun May 22 20:13:33 2011 UTC vs.
Revision 1.409 by root, Sun Jun 12 04:29:15 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
18#include <stddef.h> 19#include <stddef.h>
19#include <stdio.h> 20#include <stdio.h>
20#include <errno.h> 21#include <errno.h>
21#include <assert.h> 22#include <assert.h>
87# define STACKLEVEL ((void *)&stacklevel) 88# define STACKLEVEL ((void *)&stacklevel)
88#endif 89#endif
89 90
90#define IN_DESTRUCT PL_dirty 91#define IN_DESTRUCT PL_dirty
91 92
92#if __GNUC__ >= 3
93# define attribute(x) __attribute__(x)
94# define expect(expr,value) __builtin_expect ((expr), (value))
95# define INLINE static inline
96#else
97# define attribute(x)
98# define expect(expr,value) (expr)
99# define INLINE static
100#endif
101
102#define expect_false(expr) expect ((expr) != 0, 0)
103#define expect_true(expr) expect ((expr) != 0, 1)
104
105#define NOINLINE attribute ((noinline))
106
107#include "CoroAPI.h" 93#include "CoroAPI.h"
108#define GCoroAPI (&coroapi) /* very sneaky */ 94#define GCoroAPI (&coroapi) /* very sneaky */
109 95
110#ifdef USE_ITHREADS 96#ifdef USE_ITHREADS
111# if CORO_PTHREAD 97# if CORO_PTHREAD
112static void *coro_thx; 98static void *coro_thx;
113# endif 99# endif
114#endif 100#endif
101
102/* used in state.h */
103#define VAR(name,type) VARx(name, PL_ ## name, type)
115 104
116#ifdef __linux 105#ifdef __linux
117# include <time.h> /* for timespec */ 106# include <time.h> /* for timespec */
118# include <syscall.h> /* for SYS_* */ 107# include <syscall.h> /* for SYS_* */
119# ifdef SYS_clock_gettime 108# ifdef SYS_clock_gettime
218}; 207};
219 208
220/* the structure where most of the perl state is stored, overlaid on the cxstack */ 209/* the structure where most of the perl state is stored, overlaid on the cxstack */
221typedef struct 210typedef struct
222{ 211{
223 SV *defsv;
224 AV *defav;
225 SV *errsv;
226 SV *irsgv;
227 HV *hinthv;
228#define VAR(name,type) type name; 212#define VARx(name,expr,type) type name;
229# include "state.h" 213# include "state.h"
230#undef VAR 214#undef VARx
231} perl_slots; 215} perl_slots;
232 216
233// how many context stack entries do we need for perl_slots 217// how many context stack entries do we need for perl_slots
234#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 218#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
235 219
304static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 288static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
305static CV *cv_coro_run; 289static CV *cv_coro_run;
306static struct coro *coro_first; 290static struct coro *coro_first;
307#define coro_nready coroapi.nready 291#define coro_nready coroapi.nready
308 292
293/** JIT *********************************************************************/
294
295#if CORO_JIT
296 #ifndef CORO_JIT_TYPE
297 #if __x86_64 && (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris)
298 #define CORO_JIT_TYPE "amd64-unix"
299 typedef void (*load_save_perl_slots_type)(perl_slots *);
300 #elif __i386 && (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris)
301 #define CORO_JIT_TYPE "x86-unix"
302 typedef void (*load_save_perl_slots_type)(perl_slots *);
303 #else
304 x x x x
305 #undef CORO_JIT
306 #endif
307 #endif
308#endif
309
310#if CORO_JIT
311static load_save_perl_slots_type load_perl_slots, save_perl_slots;
312#endif
313
309/** Coro::Select ************************************************************/ 314/** Coro::Select ************************************************************/
310 315
311static OP *(*coro_old_pp_sselect) (pTHX); 316static OP *(*coro_old_pp_sselect) (pTHX);
312static SV *coro_select_select; 317static SV *coro_select_select;
313 318
328 333
329/** time stuff **************************************************************/ 334/** time stuff **************************************************************/
330 335
331#ifdef HAS_GETTIMEOFDAY 336#ifdef HAS_GETTIMEOFDAY
332 337
333static void 338ECB_INLINE void
334coro_u2time (pTHX_ UV ret[2]) 339coro_u2time (pTHX_ UV ret[2])
335{ 340{
336 struct timeval tv; 341 struct timeval tv;
337 gettimeofday (&tv, 0); 342 gettimeofday (&tv, 0);
338 343
339 ret [0] = tv.tv_sec; 344 ret [0] = tv.tv_sec;
340 ret [1] = tv.tv_usec; 345 ret [1] = tv.tv_usec;
341} 346}
342 347
343static double 348ECB_INLINE double
344coro_nvtime () 349coro_nvtime ()
345{ 350{
346 struct timeval tv; 351 struct timeval tv;
347 gettimeofday (&tv, 0); 352 gettimeofday (&tv, 0);
348 353
349 return tv.tv_sec + tv.tv_usec * 1e-6; 354 return tv.tv_sec + tv.tv_usec * 1e-6;
350} 355}
351 356
352static void 357ECB_INLINE void
353time_init (pTHX) 358time_init (pTHX)
354{ 359{
355 nvtime = coro_nvtime; 360 nvtime = coro_nvtime;
356 u2time = coro_u2time; 361 u2time = coro_u2time;
357} 362}
358 363
359#else 364#else
360 365
361static void 366ECB_INLINE void
362time_init (pTHX) 367time_init (pTHX)
363{ 368{
364 SV **svp; 369 SV **svp;
365 370
366 require_pv ("Time/HiRes.pm"); 371 require_pv ("Time/HiRes.pm");
378 383
379#endif 384#endif
380 385
381/** lowlevel stuff **********************************************************/ 386/** lowlevel stuff **********************************************************/
382 387
383static SV * 388static SV * ecb_noinline
384coro_get_sv (pTHX_ const char *name, int create) 389coro_get_sv (pTHX_ const char *name, int create)
385{ 390{
386#if PERL_VERSION_ATLEAST (5,10,0) 391#if PERL_VERSION_ATLEAST (5,10,0)
387 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 392 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
388 get_sv (name, create); 393 get_sv (name, create);
389#endif 394#endif
390 return get_sv (name, create); 395 return get_sv (name, create);
391} 396}
392 397
393static AV * 398static AV * ecb_noinline
394coro_get_av (pTHX_ const char *name, int create) 399coro_get_av (pTHX_ const char *name, int create)
395{ 400{
396#if PERL_VERSION_ATLEAST (5,10,0) 401#if PERL_VERSION_ATLEAST (5,10,0)
397 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 402 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
398 get_av (name, create); 403 get_av (name, create);
399#endif 404#endif
400 return get_av (name, create); 405 return get_av (name, create);
401} 406}
402 407
403static HV * 408static HV * ecb_noinline
404coro_get_hv (pTHX_ const char *name, int create) 409coro_get_hv (pTHX_ const char *name, int create)
405{ 410{
406#if PERL_VERSION_ATLEAST (5,10,0) 411#if PERL_VERSION_ATLEAST (5,10,0)
407 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 412 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
408 get_hv (name, create); 413 get_hv (name, create);
409#endif 414#endif
410 return get_hv (name, create); 415 return get_hv (name, create);
411} 416}
412 417
413INLINE void 418ECB_INLINE void
414coro_times_update () 419coro_times_update ()
415{ 420{
416#ifdef coro_clock_gettime 421#ifdef coro_clock_gettime
417 struct timespec ts; 422 struct timespec ts;
418 423
431 time_real [0] = tv [0]; 436 time_real [0] = tv [0];
432 time_real [1] = tv [1] * 1000; 437 time_real [1] = tv [1] * 1000;
433#endif 438#endif
434} 439}
435 440
436INLINE void 441ECB_INLINE void
437coro_times_add (struct coro *c) 442coro_times_add (struct coro *c)
438{ 443{
439 c->t_real [1] += time_real [1]; 444 c->t_real [1] += time_real [1];
440 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } 445 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
441 c->t_real [0] += time_real [0]; 446 c->t_real [0] += time_real [0];
443 c->t_cpu [1] += time_cpu [1]; 448 c->t_cpu [1] += time_cpu [1];
444 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } 449 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
445 c->t_cpu [0] += time_cpu [0]; 450 c->t_cpu [0] += time_cpu [0];
446} 451}
447 452
448INLINE void 453ECB_INLINE void
449coro_times_sub (struct coro *c) 454coro_times_sub (struct coro *c)
450{ 455{
451 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } 456 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
452 c->t_real [1] -= time_real [1]; 457 c->t_real [1] -= time_real [1];
453 c->t_real [0] -= time_real [0]; 458 c->t_real [0] -= time_real [0];
461/* magic glue */ 466/* magic glue */
462 467
463#define CORO_MAGIC_type_cv 26 468#define CORO_MAGIC_type_cv 26
464#define CORO_MAGIC_type_state PERL_MAGIC_ext 469#define CORO_MAGIC_type_state PERL_MAGIC_ext
465 470
466#define CORO_MAGIC_NN(sv, type) \ 471#define CORO_MAGIC_NN(sv, type) \
467 (expect_true (SvMAGIC (sv)->mg_type == type) \ 472 (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \
468 ? SvMAGIC (sv) \ 473 ? SvMAGIC (sv) \
469 : mg_find (sv, type)) 474 : mg_find (sv, type))
470 475
471#define CORO_MAGIC(sv, type) \ 476#define CORO_MAGIC(sv, type) \
472 (expect_true (SvMAGIC (sv)) \ 477 (ecb_expect_true (SvMAGIC (sv)) \
473 ? CORO_MAGIC_NN (sv, type) \ 478 ? CORO_MAGIC_NN (sv, type) \
474 : 0) 479 : 0)
475 480
476#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 481#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
477#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) 482#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
478 483
479INLINE MAGIC * 484ECB_INLINE MAGIC *
480SvSTATEhv_p (pTHX_ SV *coro) 485SvSTATEhv_p (pTHX_ SV *coro)
481{ 486{
482 MAGIC *mg; 487 MAGIC *mg;
483 488
484 if (expect_true ( 489 if (ecb_expect_true (
485 SvTYPE (coro) == SVt_PVHV 490 SvTYPE (coro) == SVt_PVHV
486 && (mg = CORO_MAGIC_state (coro)) 491 && (mg = CORO_MAGIC_state (coro))
487 && mg->mg_virtual == &coro_state_vtbl 492 && mg->mg_virtual == &coro_state_vtbl
488 )) 493 ))
489 return mg; 494 return mg;
490 495
491 return 0; 496 return 0;
492} 497}
493 498
494INLINE struct coro * 499ECB_INLINE struct coro *
495SvSTATE_ (pTHX_ SV *coro) 500SvSTATE_ (pTHX_ SV *coro)
496{ 501{
497 MAGIC *mg; 502 MAGIC *mg;
498 503
499 if (SvROK (coro)) 504 if (SvROK (coro))
513#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 518#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
514 519
515/*****************************************************************************/ 520/*****************************************************************************/
516/* padlist management and caching */ 521/* padlist management and caching */
517 522
518static AV * 523ECB_INLINE AV *
519coro_derive_padlist (pTHX_ CV *cv) 524coro_derive_padlist (pTHX_ CV *cv)
520{ 525{
521 AV *padlist = CvPADLIST (cv); 526 AV *padlist = CvPADLIST (cv);
522 AV *newpadlist, *newpad; 527 AV *newpadlist, *newpad;
523 528
535 av_store (newpadlist, 1, (SV *)newpad); 540 av_store (newpadlist, 1, (SV *)newpad);
536 541
537 return newpadlist; 542 return newpadlist;
538} 543}
539 544
540static void 545ECB_INLINE void
541free_padlist (pTHX_ AV *padlist) 546free_padlist (pTHX_ AV *padlist)
542{ 547{
543 /* may be during global destruction */ 548 /* may be during global destruction */
544 if (!IN_DESTRUCT) 549 if (!IN_DESTRUCT)
545 { 550 {
588 0, 0, 0, 0, 593 0, 0, 0, 0,
589 coro_cv_free 594 coro_cv_free
590}; 595};
591 596
592/* the next two functions merely cache the padlists */ 597/* the next two functions merely cache the padlists */
593static void 598ECB_INLINE void
594get_padlist (pTHX_ CV *cv) 599get_padlist (pTHX_ CV *cv)
595{ 600{
596 MAGIC *mg = CORO_MAGIC_cv (cv); 601 MAGIC *mg = CORO_MAGIC_cv (cv);
597 AV *av; 602 AV *av;
598 603
599 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 604 if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
600 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 605 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
601 else 606 else
602 { 607 {
603#if CORO_PREFER_PERL_FUNCTIONS 608#if CORO_PREFER_PERL_FUNCTIONS
604 /* this is probably cleaner? but also slower! */ 609 /* this is probably cleaner? but also slower! */
611 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); 616 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
612#endif 617#endif
613 } 618 }
614} 619}
615 620
616static void 621ECB_INLINE void
617put_padlist (pTHX_ CV *cv) 622put_padlist (pTHX_ CV *cv)
618{ 623{
619 MAGIC *mg = CORO_MAGIC_cv (cv); 624 MAGIC *mg = CORO_MAGIC_cv (cv);
620 AV *av; 625 AV *av;
621 626
622 if (expect_false (!mg)) 627 if (ecb_expect_false (!mg))
623 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 628 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
624 629
625 av = (AV *)mg->mg_obj; 630 av = (AV *)mg->mg_obj;
626 631
627 if (expect_false (AvFILLp (av) >= AvMAX (av))) 632 if (ecb_expect_false (AvFILLp (av) >= AvMAX (av)))
628 av_extend (av, AvFILLp (av) + 1); 633 av_extend (av, AvFILLp (av) + 1);
629 634
630 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 635 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
631} 636}
632 637
677 } 682 }
678#endif 683#endif
679 } 684 }
680} 685}
681 686
682#define SWAP_SVS(coro) \ 687#define SWAP_SVS(coro) \
683 if (expect_false ((coro)->swap_sv)) \ 688 if (ecb_expect_false ((coro)->swap_sv)) \
684 swap_svs (aTHX_ (coro)) 689 swap_svs (aTHX_ (coro))
685 690
686static void 691static void
687on_enterleave_call (pTHX_ SV *cb); 692on_enterleave_call (pTHX_ SV *cb);
688 693
692 perl_slots *slot = c->slot; 697 perl_slots *slot = c->slot;
693 c->slot = 0; 698 c->slot = 0;
694 699
695 PL_mainstack = c->mainstack; 700 PL_mainstack = c->mainstack;
696 701
697 GvSV (PL_defgv) = slot->defsv; 702#if CORO_JIT
698 GvAV (PL_defgv) = slot->defav; 703 load_perl_slots (slot);
699 GvSV (PL_errgv) = slot->errsv; 704#else
700 GvSV (irsgv) = slot->irsgv;
701 GvHV (PL_hintgv) = slot->hinthv;
702
703 #define VAR(name,type) PL_ ## name = slot->name; 705 #define VARx(name,expr,type) expr = slot->name;
704 # include "state.h" 706 # include "state.h"
705 #undef VAR 707 #undef VARx
708#endif
706 709
707 { 710 {
708 dSP; 711 dSP;
709 712
710 CV *cv; 713 CV *cv;
711 714
712 /* now do the ugly restore mess */ 715 /* now do the ugly restore mess */
713 while (expect_true (cv = (CV *)POPs)) 716 while (ecb_expect_true (cv = (CV *)POPs))
714 { 717 {
715 put_padlist (aTHX_ cv); /* mark this padlist as available */ 718 put_padlist (aTHX_ cv); /* mark this padlist as available */
716 CvDEPTH (cv) = PTR2IV (POPs); 719 CvDEPTH (cv) = PTR2IV (POPs);
717 CvPADLIST (cv) = (AV *)POPs; 720 CvPADLIST (cv) = (AV *)POPs;
718 } 721 }
721 } 724 }
722 725
723 slf_frame = c->slf_frame; 726 slf_frame = c->slf_frame;
724 CORO_THROW = c->except; 727 CORO_THROW = c->except;
725 728
726 if (expect_false (enable_times)) 729 if (ecb_expect_false (enable_times))
727 { 730 {
728 if (expect_false (!times_valid)) 731 if (ecb_expect_false (!times_valid))
729 coro_times_update (); 732 coro_times_update ();
730 733
731 coro_times_sub (c); 734 coro_times_sub (c);
732 } 735 }
733 736
734 if (expect_false (c->on_enter)) 737 if (ecb_expect_false (c->on_enter))
735 { 738 {
736 int i; 739 int i;
737 740
738 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 741 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
739 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 742 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
745static void 748static void
746save_perl (pTHX_ Coro__State c) 749save_perl (pTHX_ Coro__State c)
747{ 750{
748 SWAP_SVS (c); 751 SWAP_SVS (c);
749 752
750 if (expect_false (c->on_leave)) 753 if (ecb_expect_false (c->on_leave))
751 { 754 {
752 int i; 755 int i;
753 756
754 for (i = AvFILLp (c->on_leave); i >= 0; --i) 757 for (i = AvFILLp (c->on_leave); i >= 0; --i)
755 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); 758 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
756 } 759 }
757 760
758 times_valid = 0; 761 times_valid = 0;
759 762
760 if (expect_false (enable_times)) 763 if (ecb_expect_false (enable_times))
761 { 764 {
762 coro_times_update (); times_valid = 1; 765 coro_times_update (); times_valid = 1;
763 coro_times_add (c); 766 coro_times_add (c);
764 } 767 }
765 768
779 782
780 XPUSHs (Nullsv); 783 XPUSHs (Nullsv);
781 /* this loop was inspired by pp_caller */ 784 /* this loop was inspired by pp_caller */
782 for (;;) 785 for (;;)
783 { 786 {
784 while (expect_true (cxix >= 0)) 787 while (ecb_expect_true (cxix >= 0))
785 { 788 {
786 PERL_CONTEXT *cx = &ccstk[cxix--]; 789 PERL_CONTEXT *cx = &ccstk[cxix--];
787 790
788 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) 791 if (ecb_expect_true (CxTYPE (cx) == CXt_SUB) || ecb_expect_false (CxTYPE (cx) == CXt_FORMAT))
789 { 792 {
790 CV *cv = cx->blk_sub.cv; 793 CV *cv = cx->blk_sub.cv;
791 794
792 if (expect_true (CvDEPTH (cv))) 795 if (ecb_expect_true (CvDEPTH (cv)))
793 { 796 {
794 EXTEND (SP, 3); 797 EXTEND (SP, 3);
795 PUSHs ((SV *)CvPADLIST (cv)); 798 PUSHs ((SV *)CvPADLIST (cv));
796 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 799 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
797 PUSHs ((SV *)cv); 800 PUSHs ((SV *)cv);
800 get_padlist (aTHX_ cv); 803 get_padlist (aTHX_ cv);
801 } 804 }
802 } 805 }
803 } 806 }
804 807
805 if (expect_true (top_si->si_type == PERLSI_MAIN)) 808 if (ecb_expect_true (top_si->si_type == PERLSI_MAIN))
806 break; 809 break;
807 810
808 top_si = top_si->si_prev; 811 top_si = top_si->si_prev;
809 ccstk = top_si->si_cxstack; 812 ccstk = top_si->si_cxstack;
810 cxix = top_si->si_cxix; 813 cxix = top_si->si_cxix;
812 815
813 PUTBACK; 816 PUTBACK;
814 } 817 }
815 818
816 /* allocate some space on the context stack for our purposes */ 819 /* allocate some space on the context stack for our purposes */
817 if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) 820 if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max))
818 { 821 {
819 unsigned int i; 822 unsigned int i;
820 823
821 for (i = 0; i < SLOT_COUNT; ++i) 824 for (i = 0; i < SLOT_COUNT; ++i)
822 CXINC; 825 CXINC;
827 c->mainstack = PL_mainstack; 830 c->mainstack = PL_mainstack;
828 831
829 { 832 {
830 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 833 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
831 834
832 slot->defav = GvAV (PL_defgv); 835#if CORO_JIT
833 slot->defsv = DEFSV; 836 save_perl_slots (slot);
834 slot->errsv = ERRSV; 837#else
835 slot->irsgv = GvSV (irsgv);
836 slot->hinthv = GvHV (PL_hintgv);
837
838 #define VAR(name,type) slot->name = PL_ ## name; 838 #define VARx(name,expr,type) slot->name = expr;
839 # include "state.h" 839 # include "state.h"
840 #undef VAR 840 #undef VARx
841#endif
841 } 842 }
842} 843}
843 844
844/* 845/*
845 * allocate various perl stacks. This is almost an exact copy 846 * allocate various perl stacks. This is almost an exact copy
971 * This overrides the default magic get method of %SIG elements. 972 * This overrides the default magic get method of %SIG elements.
972 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 973 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
973 * and instead of trying to save and restore the hash elements (extremely slow), 974 * and instead of trying to save and restore the hash elements (extremely slow),
974 * we just provide our own readback here. 975 * we just provide our own readback here.
975 */ 976 */
976static int 977static int ecb_cold
977coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 978coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
978{ 979{
979 const char *s = MgPV_nolen_const (mg); 980 const char *s = MgPV_nolen_const (mg);
980 981
981 if (*s == '_') 982 if (*s == '_')
993 } 994 }
994 995
995 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 996 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
996} 997}
997 998
998static int 999static int ecb_cold
999coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1000coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
1000{ 1001{
1001 const char *s = MgPV_nolen_const (mg); 1002 const char *s = MgPV_nolen_const (mg);
1002 1003
1003 if (*s == '_') 1004 if (*s == '_')
1017 } 1018 }
1018 1019
1019 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; 1020 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
1020} 1021}
1021 1022
1022static int 1023static int ecb_cold
1023coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1024coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
1024{ 1025{
1025 const char *s = MgPV_nolen_const (mg); 1026 const char *s = MgPV_nolen_const (mg);
1026 1027
1027 if (*s == '_') 1028 if (*s == '_')
1062 return 1; 1063 return 1;
1063} 1064}
1064 1065
1065static UNOP init_perl_op; 1066static UNOP init_perl_op;
1066 1067
1067static void NOINLINE /* noinline to keep it out of the transfer fast path */ 1068ecb_noinline static void /* noinline to keep it out of the transfer fast path */
1068init_perl (pTHX_ struct coro *coro) 1069init_perl (pTHX_ struct coro *coro)
1069{ 1070{
1070 /* 1071 /*
1071 * emulate part of the perl startup here. 1072 * emulate part of the perl startup here.
1072 */ 1073 */
1134 /* copy throw, in case it was set before init_perl */ 1135 /* copy throw, in case it was set before init_perl */
1135 CORO_THROW = coro->except; 1136 CORO_THROW = coro->except;
1136 1137
1137 SWAP_SVS (coro); 1138 SWAP_SVS (coro);
1138 1139
1139 if (expect_false (enable_times)) 1140 if (ecb_expect_false (enable_times))
1140 { 1141 {
1141 coro_times_update (); 1142 coro_times_update ();
1142 coro_times_sub (coro); 1143 coro_times_sub (coro);
1143 } 1144 }
1144} 1145}
1217 SvREFCNT_dec (coro->invoke_cb); 1218 SvREFCNT_dec (coro->invoke_cb);
1218 SvREFCNT_dec (coro->invoke_av); 1219 SvREFCNT_dec (coro->invoke_av);
1219 } 1220 }
1220} 1221}
1221 1222
1222INLINE void 1223ECB_INLINE void
1223free_coro_mortal (pTHX) 1224free_coro_mortal (pTHX)
1224{ 1225{
1225 if (expect_true (coro_mortal)) 1226 if (ecb_expect_true (coro_mortal))
1226 { 1227 {
1227 SvREFCNT_dec ((SV *)coro_mortal); 1228 SvREFCNT_dec ((SV *)coro_mortal);
1228 coro_mortal = 0; 1229 coro_mortal = 0;
1229 } 1230 }
1230} 1231}
1365 1366
1366 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1367 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1367} 1368}
1368 1369
1369/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1370/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1370static void NOINLINE 1371static void ecb_noinline
1371cctx_prepare (pTHX) 1372cctx_prepare (pTHX)
1372{ 1373{
1373 PL_top_env = &PL_start_env; 1374 PL_top_env = &PL_start_env;
1374 1375
1375 if (cctx_current->flags & CC_TRACE) 1376 if (cctx_current->flags & CC_TRACE)
1386 slf_frame.prepare = slf_prepare_set_stacklevel; 1387 slf_frame.prepare = slf_prepare_set_stacklevel;
1387 slf_frame.check = slf_check_set_stacklevel; 1388 slf_frame.check = slf_check_set_stacklevel;
1388} 1389}
1389 1390
1390/* the tail of transfer: execute stuff we can only do after a transfer */ 1391/* the tail of transfer: execute stuff we can only do after a transfer */
1391INLINE void 1392ECB_INLINE void
1392transfer_tail (pTHX) 1393transfer_tail (pTHX)
1393{ 1394{
1394 free_coro_mortal (aTHX); 1395 free_coro_mortal (aTHX);
1395} 1396}
1396 1397
1548#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) 1549#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1549 1550
1550static coro_cctx * 1551static coro_cctx *
1551cctx_get (pTHX) 1552cctx_get (pTHX)
1552{ 1553{
1553 while (expect_true (cctx_first)) 1554 while (ecb_expect_true (cctx_first))
1554 { 1555 {
1555 coro_cctx *cctx = cctx_first; 1556 coro_cctx *cctx = cctx_first;
1556 cctx_first = cctx->next; 1557 cctx_first = cctx->next;
1557 --cctx_idle; 1558 --cctx_idle;
1558 1559
1559 if (expect_true (!CCTX_EXPIRED (cctx))) 1560 if (ecb_expect_true (!CCTX_EXPIRED (cctx)))
1560 return cctx; 1561 return cctx;
1561 1562
1562 cctx_destroy (cctx); 1563 cctx_destroy (cctx);
1563 } 1564 }
1564 1565
1569cctx_put (coro_cctx *cctx) 1570cctx_put (coro_cctx *cctx)
1570{ 1571{
1571 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); 1572 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1572 1573
1573 /* free another cctx if overlimit */ 1574 /* free another cctx if overlimit */
1574 if (expect_false (cctx_idle >= cctx_max_idle)) 1575 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
1575 { 1576 {
1576 coro_cctx *first = cctx_first; 1577 coro_cctx *first = cctx_first;
1577 cctx_first = first->next; 1578 cctx_first = first->next;
1578 --cctx_idle; 1579 --cctx_idle;
1579 1580
1590static void 1591static void
1591transfer_check (pTHX_ struct coro *prev, struct coro *next) 1592transfer_check (pTHX_ struct coro *prev, struct coro *next)
1592{ 1593{
1593 /* TODO: throwing up here is considered harmful */ 1594 /* TODO: throwing up here is considered harmful */
1594 1595
1595 if (expect_true (prev != next)) 1596 if (ecb_expect_true (prev != next))
1596 { 1597 {
1597 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1598 if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1598 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); 1599 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1599 1600
1600 if (expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) 1601 if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED)))
1601 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); 1602 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1602 1603
1603#if !PERL_VERSION_ATLEAST (5,10,0) 1604#if !PERL_VERSION_ATLEAST (5,10,0)
1604 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1605 if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING))
1605 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1606 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1606#endif 1607#endif
1607 } 1608 }
1608} 1609}
1609 1610
1610/* always use the TRANSFER macro */ 1611/* always use the TRANSFER macro */
1611static void NOINLINE /* noinline so we have a fixed stackframe */ 1612static void ecb_noinline /* noinline so we have a fixed stackframe */
1612transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1613transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1613{ 1614{
1614 dSTACKLEVEL; 1615 dSTACKLEVEL;
1615 1616
1616 /* sometimes transfer is only called to set idle_sp */ 1617 /* sometimes transfer is only called to set idle_sp */
1617 if (expect_false (!prev)) 1618 if (ecb_expect_false (!prev))
1618 { 1619 {
1619 cctx_current->idle_sp = STACKLEVEL; 1620 cctx_current->idle_sp = STACKLEVEL;
1620 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1621 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1621 } 1622 }
1622 else if (expect_true (prev != next)) 1623 else if (ecb_expect_true (prev != next))
1623 { 1624 {
1624 coro_cctx *cctx_prev; 1625 coro_cctx *cctx_prev;
1625 1626
1626 if (expect_false (prev->flags & CF_NEW)) 1627 if (ecb_expect_false (prev->flags & CF_NEW))
1627 { 1628 {
1628 /* create a new empty/source context */ 1629 /* create a new empty/source context */
1629 prev->flags &= ~CF_NEW; 1630 prev->flags &= ~CF_NEW;
1630 prev->flags |= CF_RUNNING; 1631 prev->flags |= CF_RUNNING;
1631 } 1632 }
1634 next->flags |= CF_RUNNING; 1635 next->flags |= CF_RUNNING;
1635 1636
1636 /* first get rid of the old state */ 1637 /* first get rid of the old state */
1637 save_perl (aTHX_ prev); 1638 save_perl (aTHX_ prev);
1638 1639
1639 if (expect_false (next->flags & CF_NEW)) 1640 if (ecb_expect_false (next->flags & CF_NEW))
1640 { 1641 {
1641 /* need to start coroutine */ 1642 /* need to start coroutine */
1642 next->flags &= ~CF_NEW; 1643 next->flags &= ~CF_NEW;
1643 /* setup coroutine call */ 1644 /* setup coroutine call */
1644 init_perl (aTHX_ next); 1645 init_perl (aTHX_ next);
1645 } 1646 }
1646 else 1647 else
1647 load_perl (aTHX_ next); 1648 load_perl (aTHX_ next);
1648 1649
1649 /* possibly untie and reuse the cctx */ 1650 /* possibly untie and reuse the cctx */
1650 if (expect_true ( 1651 if (ecb_expect_true (
1651 cctx_current->idle_sp == STACKLEVEL 1652 cctx_current->idle_sp == STACKLEVEL
1652 && !(cctx_current->flags & CC_TRACE) 1653 && !(cctx_current->flags & CC_TRACE)
1653 && !force_cctx 1654 && !force_cctx
1654 )) 1655 ))
1655 { 1656 {
1656 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1657 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1657 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); 1658 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1658 1659
1659 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ 1660 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1660 /* without this the next cctx_get might destroy the running cctx while still in use */ 1661 /* without this the next cctx_get might destroy the running cctx while still in use */
1661 if (expect_false (CCTX_EXPIRED (cctx_current))) 1662 if (ecb_expect_false (CCTX_EXPIRED (cctx_current)))
1662 if (expect_true (!next->cctx)) 1663 if (ecb_expect_true (!next->cctx))
1663 next->cctx = cctx_get (aTHX); 1664 next->cctx = cctx_get (aTHX);
1664 1665
1665 cctx_put (cctx_current); 1666 cctx_put (cctx_current);
1666 } 1667 }
1667 else 1668 else
1668 prev->cctx = cctx_current; 1669 prev->cctx = cctx_current;
1669 1670
1670 ++next->usecount; 1671 ++next->usecount;
1671 1672
1672 cctx_prev = cctx_current; 1673 cctx_prev = cctx_current;
1673 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); 1674 cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1674 1675
1675 next->cctx = 0; 1676 next->cctx = 0;
1676 1677
1677 if (expect_false (cctx_prev != cctx_current)) 1678 if (ecb_expect_false (cctx_prev != cctx_current))
1678 { 1679 {
1679 cctx_prev->top_env = PL_top_env; 1680 cctx_prev->top_env = PL_top_env;
1680 PL_top_env = cctx_current->top_env; 1681 PL_top_env = cctx_current->top_env;
1681 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); 1682 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1682 } 1683 }
1748 Safefree (coro); 1749 Safefree (coro);
1749 1750
1750 return 0; 1751 return 0;
1751} 1752}
1752 1753
1753static int 1754static int ecb_cold
1754coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) 1755coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
1755{ 1756{
1756 /* called when perl clones the current process the slow way (windows process emulation) */ 1757 /* called when perl clones the current process the slow way (windows process emulation) */
1757 /* WE SIMply nuke the pointers in the copy, causing perl to croak */ 1758 /* WE SIMply nuke the pointers in the copy, causing perl to croak */
1758 mg->mg_ptr = 0; 1759 mg->mg_ptr = 0;
1789 TRANSFER (ta, 1); 1790 TRANSFER (ta, 1);
1790} 1791}
1791 1792
1792/** Coro ********************************************************************/ 1793/** Coro ********************************************************************/
1793 1794
1794INLINE void 1795ECB_INLINE void
1795coro_enq (pTHX_ struct coro *coro) 1796coro_enq (pTHX_ struct coro *coro)
1796{ 1797{
1797 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; 1798 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1798 1799
1799 SvREFCNT_inc_NN (coro->hv); 1800 SvREFCNT_inc_NN (coro->hv);
1801 coro->next_ready = 0; 1802 coro->next_ready = 0;
1802 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; 1803 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1803 ready [1] = coro; 1804 ready [1] = coro;
1804} 1805}
1805 1806
1806INLINE struct coro * 1807ECB_INLINE struct coro *
1807coro_deq (pTHX) 1808coro_deq (pTHX)
1808{ 1809{
1809 int prio; 1810 int prio;
1810 1811
1811 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) 1812 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1864{ 1865{
1865 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1866 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1866} 1867}
1867 1868
1868/* expects to own a reference to next->hv */ 1869/* expects to own a reference to next->hv */
1869INLINE void 1870ECB_INLINE void
1870prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1871prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1871{ 1872{
1872 SV *prev_sv = SvRV (coro_current); 1873 SV *prev_sv = SvRV (coro_current);
1873 1874
1874 ta->prev = SvSTATE_hv (prev_sv); 1875 ta->prev = SvSTATE_hv (prev_sv);
1887{ 1888{
1888 for (;;) 1889 for (;;)
1889 { 1890 {
1890 struct coro *next = coro_deq (aTHX); 1891 struct coro *next = coro_deq (aTHX);
1891 1892
1892 if (expect_true (next)) 1893 if (ecb_expect_true (next))
1893 { 1894 {
1894 /* cannot transfer to destroyed coros, skip and look for next */ 1895 /* cannot transfer to destroyed coros, skip and look for next */
1895 if (expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) 1896 if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED)))
1896 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ 1897 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1897 else 1898 else
1898 { 1899 {
1899 next->flags &= ~CF_READY; 1900 next->flags &= ~CF_READY;
1900 --coro_nready; 1901 --coro_nready;
1933 } 1934 }
1934 } 1935 }
1935 } 1936 }
1936} 1937}
1937 1938
1938INLINE void 1939ECB_INLINE void
1939prepare_cede (pTHX_ struct coro_transfer_args *ta) 1940prepare_cede (pTHX_ struct coro_transfer_args *ta)
1940{ 1941{
1941 api_ready (aTHX_ coro_current); 1942 api_ready (aTHX_ coro_current);
1942 prepare_schedule (aTHX_ ta); 1943 prepare_schedule (aTHX_ ta);
1943} 1944}
1944 1945
1945INLINE void 1946ECB_INLINE void
1946prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) 1947prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1947{ 1948{
1948 SV *prev = SvRV (coro_current); 1949 SV *prev = SvRV (coro_current);
1949 1950
1950 if (coro_nready) 1951 if (coro_nready)
1980{ 1981{
1981 struct coro_transfer_args ta; 1982 struct coro_transfer_args ta;
1982 1983
1983 prepare_cede (aTHX_ &ta); 1984 prepare_cede (aTHX_ &ta);
1984 1985
1985 if (expect_true (ta.prev != ta.next)) 1986 if (ecb_expect_true (ta.prev != ta.next))
1986 { 1987 {
1987 TRANSFER (ta, 1); 1988 TRANSFER (ta, 1);
1988 return 1; 1989 return 1;
1989 } 1990 }
1990 else 1991 else
2203 coro = SvSTATE (arg [0]); 2204 coro = SvSTATE (arg [0]);
2204 coro_hv = coro->hv; 2205 coro_hv = coro->hv;
2205 2206
2206 coro_set_status (aTHX_ coro, arg + 1, items - 1); 2207 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2207 2208
2208 if (expect_false (coro->flags & CF_NOCANCEL)) 2209 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2209 { 2210 {
2210 /* coro currently busy cancelling something, so just notify it */ 2211 /* coro currently busy cancelling something, so just notify it */
2211 coro->slf_frame.data = (void *)coro; 2212 coro->slf_frame.data = (void *)coro;
2212 2213
2213 frame->prepare = prepare_nop; 2214 frame->prepare = prepare_nop;
2321slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2322slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2322{ 2323{
2323 HV *hv = (HV *)SvRV (coro_current); 2324 HV *hv = (HV *)SvRV (coro_current);
2324 struct coro *coro = SvSTATE_hv ((SV *)hv); 2325 struct coro *coro = SvSTATE_hv ((SV *)hv);
2325 2326
2326 if (expect_true (coro->saved_deffh)) 2327 if (ecb_expect_true (coro->saved_deffh))
2327 { 2328 {
2328 /* subsequent iteration */ 2329 /* subsequent iteration */
2329 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2330 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2330 coro->saved_deffh = 0; 2331 coro->saved_deffh = 0;
2331 2332
2597 I32 checkmark; /* mark SP to see how many elements check has pushed */ 2598 I32 checkmark; /* mark SP to see how many elements check has pushed */
2598 2599
2599 /* set up the slf frame, unless it has already been set-up */ 2600 /* set up the slf frame, unless it has already been set-up */
2600 /* the latter happens when a new coro has been started */ 2601 /* the latter happens when a new coro has been started */
2601 /* or when a new cctx was attached to an existing coroutine */ 2602 /* or when a new cctx was attached to an existing coroutine */
2602 if (expect_true (!slf_frame.prepare)) 2603 if (ecb_expect_true (!slf_frame.prepare))
2603 { 2604 {
2604 /* first iteration */ 2605 /* first iteration */
2605 dSP; 2606 dSP;
2606 SV **arg = PL_stack_base + TOPMARK + 1; 2607 SV **arg = PL_stack_base + TOPMARK + 1;
2607 int items = SP - arg; /* args without function object */ 2608 int items = SP - arg; /* args without function object */
2648 while (slf_frame.check (aTHX_ &slf_frame)); 2649 while (slf_frame.check (aTHX_ &slf_frame));
2649 2650
2650 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 2651 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
2651 2652
2652 /* exception handling */ 2653 /* exception handling */
2653 if (expect_false (CORO_THROW)) 2654 if (ecb_expect_false (CORO_THROW))
2654 { 2655 {
2655 SV *exception = sv_2mortal (CORO_THROW); 2656 SV *exception = sv_2mortal (CORO_THROW);
2656 2657
2657 CORO_THROW = 0; 2658 CORO_THROW = 0;
2658 sv_setsv (ERRSV, exception); 2659 sv_setsv (ERRSV, exception);
2660 } 2661 }
2661 2662
2662 /* return value handling - mostly like entersub */ 2663 /* return value handling - mostly like entersub */
2663 /* make sure we put something on the stack in scalar context */ 2664 /* make sure we put something on the stack in scalar context */
2664 if (GIMME_V == G_SCALAR 2665 if (GIMME_V == G_SCALAR
2665 && expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) 2666 && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1))
2666 { 2667 {
2667 dSP; 2668 dSP;
2668 SV **bot = PL_stack_base + checkmark; 2669 SV **bot = PL_stack_base + checkmark;
2669 2670
2670 if (sp == bot) /* too few, push undef */ 2671 if (sp == bot) /* too few, push undef */
2784{ 2785{
2785 PerlIOBuf base; 2786 PerlIOBuf base;
2786 NV next, every; 2787 NV next, every;
2787} PerlIOCede; 2788} PerlIOCede;
2788 2789
2789static IV 2790static IV ecb_cold
2790PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) 2791PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2791{ 2792{
2792 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2793 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2793 2794
2794 self->every = SvCUR (arg) ? SvNV (arg) : 0.01; 2795 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
2795 self->next = nvtime () + self->every; 2796 self->next = nvtime () + self->every;
2796 2797
2797 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); 2798 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
2798} 2799}
2799 2800
2800static SV * 2801static SV * ecb_cold
2801PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) 2802PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
2802{ 2803{
2803 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2804 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2804 2805
2805 return newSVnv (self->every); 2806 return newSVnv (self->every);
3217 dSP; 3218 dSP;
3218 3219
3219 static SV *prio_cv; 3220 static SV *prio_cv;
3220 static SV *prio_sv; 3221 static SV *prio_sv;
3221 3222
3222 if (expect_false (!prio_cv)) 3223 if (ecb_expect_false (!prio_cv))
3223 { 3224 {
3224 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); 3225 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0);
3225 prio_sv = newSViv (0); 3226 prio_sv = newSViv (0);
3226 } 3227 }
3227 3228
3333 } 3334 }
3334 3335
3335 return coro_sv; 3336 return coro_sv;
3336} 3337}
3337 3338
3339#ifndef __cplusplus
3340ecb_cold XS(boot_Coro__State);
3341#endif
3342
3343#if CORO_JIT
3344
3345static void ecb_noinline ecb_cold
3346pushav_3uv (pTHX_ UV a, UV b, UV c)
3347{
3348 dSP;
3349 AV *av = newAV ();
3350
3351 av_store (av, 2, newSVuv (c));
3352 av_store (av, 1, newSVuv (b));
3353 av_store (av, 0, newSVuv (a));
3354
3355 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3356
3357 PUTBACK;
3358}
3359
3360static void ecb_noinline ecb_cold
3361jit_init (pTHX)
3362{
3363 dSP;
3364 SV *load, *save;
3365 char *map_base;
3366 char *load_ptr, *save_ptr;
3367 STRLEN load_len, save_len;
3368 int count;
3369
3370 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3371
3372 PUSHMARK (SP);
3373#define VARx(name,expr,type) pushav_3uv (aTHX_ (UV)&(expr), offsetof (perl_slots, name), sizeof (type));
3374# include "state.h"
3375#undef VARx
3376 count = call_pv ("Coro::State::_jit", G_ARRAY);
3377 SPAGAIN;
3378
3379 save = POPs; save_ptr = SvPVbyte (save, save_len);
3380 load = POPs; load_ptr = SvPVbyte (load, load_len);
3381
3382 map_base = mmap (0, load_len + save_len + 16, PROT_EXEC | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3383
3384 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3385
3386 load_perl_slots = (load_save_perl_slots_type)map_base;
3387 memcpy (map_base, load_ptr, load_len);
3388
3389 map_base += (load_len + 15) & ~15;
3390
3391 save_perl_slots = (load_save_perl_slots_type)map_base;
3392 memcpy (map_base, save_ptr, save_len);
3393
3394 PUTBACK;
3395 eval_pv ("undef &Coro::State::_jit", 1);
3396}
3397
3398#endif
3399
3338MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3400MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
3339 3401
3340PROTOTYPES: DISABLE 3402PROTOTYPES: DISABLE
3341 3403
3342BOOT: 3404BOOT:
3346 coro_thx = PERL_GET_CONTEXT; 3408 coro_thx = PERL_GET_CONTEXT;
3347# endif 3409# endif
3348#endif 3410#endif
3349 BOOT_PAGESIZE; 3411 BOOT_PAGESIZE;
3350 3412
3413 /* perl defines these to check for existance first, but why it doesn't */
3414 /* just create them one at init time is not clear to me, except for */
3415 /* programs trying to delete them, but... */
3416 /* anyway, we declare this as invalid and make sure they are initialised here */
3417 DEFSV;
3418 ERRSV;
3419
3351 cctx_current = cctx_new_empty (); 3420 cctx_current = cctx_new_empty ();
3352 3421
3353 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3422 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3354 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3423 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3355 3424
3397 coroapi.prepare_cede_notself = prepare_cede_notself; 3466 coroapi.prepare_cede_notself = prepare_cede_notself;
3398 3467
3399 time_init (aTHX); 3468 time_init (aTHX);
3400 3469
3401 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3470 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3471#if CORO_JIT
3472 PUTBACK;
3473 jit_init (aTHX);
3474 SPAGAIN;
3475#endif
3402} 3476}
3403 3477
3404SV * 3478SV *
3405new (SV *klass, ...) 3479new (SV *klass, ...)
3406 ALIAS: 3480 ALIAS:
3654times (Coro::State self) 3728times (Coro::State self)
3655 PPCODE: 3729 PPCODE:
3656{ 3730{
3657 struct coro *current = SvSTATE (coro_current); 3731 struct coro *current = SvSTATE (coro_current);
3658 3732
3659 if (expect_false (current == self)) 3733 if (ecb_expect_false (current == self))
3660 { 3734 {
3661 coro_times_update (); 3735 coro_times_update ();
3662 coro_times_add (SvSTATE (coro_current)); 3736 coro_times_add (SvSTATE (coro_current));
3663 } 3737 }
3664 3738
3665 EXTEND (SP, 2); 3739 EXTEND (SP, 2);
3666 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); 3740 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9)));
3667 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); 3741 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3668 3742
3669 if (expect_false (current == self)) 3743 if (ecb_expect_false (current == self))
3670 coro_times_sub (SvSTATE (coro_current)); 3744 coro_times_sub (SvSTATE (coro_current));
3671} 3745}
3672 3746
3673void 3747void
3674swap_sv (Coro::State coro, SV *sv, SV *swapsv) 3748swap_sv (Coro::State coro, SV *sv, SV *swapsv)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines