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.409 by root, Sun Jun 12 04:29:15 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines