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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines