ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.403 by root, Sun May 22 20:13:33 2011 UTC vs.
Revision 1.412 by root, Mon Jun 13 09:00:15 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines