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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines