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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines