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.404 by root, Fri Jun 10 12:27:02 2011 UTC vs.
Revision 1.416 by root, Mon Dec 12 15:19:56 2011 UTC

33# define setjmp _setjmp /* deep magic */ 33# define setjmp _setjmp /* deep magic */
34#else 34#else
35# include <inttypes.h> /* most portable stdint.h */ 35# include <inttypes.h> /* most portable stdint.h */
36#endif 36#endif
37 37
38#ifdef HAVE_MMAP 38#if HAVE_MMAP
39# include <unistd.h> 39# include <unistd.h>
40# include <sys/mman.h> 40# include <sys/mman.h>
41# ifndef MAP_ANONYMOUS 41# ifndef MAP_ANONYMOUS
42# ifdef MAP_ANON 42# ifdef MAP_ANON
43# define MAP_ANONYMOUS MAP_ANON 43# define MAP_ANONYMOUS MAP_ANON
63#endif 63#endif
64 64
65/* the maximum number of idle cctx that will be pooled */ 65/* the maximum number of idle cctx that will be pooled */
66static int cctx_max_idle = 4; 66static int cctx_max_idle = 4;
67 67
68#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
69# define HAS_SCOPESTACK_NAME 1
70#endif
71
68#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 72#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
69# undef CORO_STACKGUARD 73# undef CORO_STACKGUARD
70#endif 74#endif
71 75
72#ifndef CORO_STACKGUARD 76#ifndef CORO_STACKGUARD
97# if CORO_PTHREAD 101# if CORO_PTHREAD
98static void *coro_thx; 102static void *coro_thx;
99# endif 103# endif
100#endif 104#endif
101 105
106/* used in state.h */
107#define VAR(name,type) VARx(name, PL_ ## name, type)
108
102#ifdef __linux 109#ifdef __linux
103# include <time.h> /* for timespec */ 110# include <time.h> /* for timespec */
104# include <syscall.h> /* for SYS_* */ 111# include <syscall.h> /* for SYS_* */
105# ifdef SYS_clock_gettime 112# ifdef SYS_clock_gettime
106# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 113# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
174 void *sptr; 181 void *sptr;
175 size_t ssize; 182 size_t ssize;
176 183
177 /* cpu state */ 184 /* cpu state */
178 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 185 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
186#ifndef NDEBUG
179 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 187 JMPENV *idle_te; /* same as idle_sp, but for top_env */
188#endif
180 JMPENV *top_env; 189 JMPENV *top_env;
181 coro_context cctx; 190 coro_context cctx;
182 191
183 U32 gen; 192 U32 gen;
184#if CORO_USE_VALGRIND 193#if CORO_USE_VALGRIND
204}; 213};
205 214
206/* the structure where most of the perl state is stored, overlaid on the cxstack */ 215/* the structure where most of the perl state is stored, overlaid on the cxstack */
207typedef struct 216typedef struct
208{ 217{
209 SV *defsv;
210 AV *defav;
211 SV *errsv;
212 SV *irsgv;
213 HV *hinthv;
214#define VAR(name,type) type name; 218#define VARx(name,expr,type) type name;
215# include "state.h" 219# include "state.h"
216#undef VAR 220#undef VARx
217} perl_slots; 221} perl_slots;
218 222
219// how many context stack entries do we need for perl_slots 223/* how many context stack entries do we need for perl_slots */
220#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 224#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
221 225
222/* this is a structure representing a perl-level coroutine */ 226/* this is a structure representing a perl-level coroutine */
223struct coro 227struct coro
224{ 228{
290static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 294static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
291static CV *cv_coro_run; 295static CV *cv_coro_run;
292static struct coro *coro_first; 296static struct coro *coro_first;
293#define coro_nready coroapi.nready 297#define coro_nready coroapi.nready
294 298
299/** JIT *********************************************************************/
300
301#if CORO_JIT
302 /* APPLE doesn't have HAVE_MMAP though */
303 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
304 #ifndef CORO_JIT_TYPE
305 #if __x86_64 && CORO_JIT_UNIXY
306 #define CORO_JIT_TYPE "amd64-unix"
307 #elif __i386 && CORO_JIT_UNIXY
308 #define CORO_JIT_TYPE "x86-unix"
309 #endif
310 #endif
311#endif
312
313#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP
314 #undef CORO_JIT
315#endif
316
317#if CORO_JIT
318 typedef void (*load_save_perl_slots_type)(perl_slots *);
319 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
320#endif
321
295/** Coro::Select ************************************************************/ 322/** Coro::Select ************************************************************/
296 323
297static OP *(*coro_old_pp_sselect) (pTHX); 324static OP *(*coro_old_pp_sselect) (pTHX);
298static SV *coro_select_select; 325static SV *coro_select_select;
299 326
314 341
315/** time stuff **************************************************************/ 342/** time stuff **************************************************************/
316 343
317#ifdef HAS_GETTIMEOFDAY 344#ifdef HAS_GETTIMEOFDAY
318 345
319ECB_INLINE void 346ecb_inline void
320coro_u2time (pTHX_ UV ret[2]) 347coro_u2time (pTHX_ UV ret[2])
321{ 348{
322 struct timeval tv; 349 struct timeval tv;
323 gettimeofday (&tv, 0); 350 gettimeofday (&tv, 0);
324 351
325 ret [0] = tv.tv_sec; 352 ret [0] = tv.tv_sec;
326 ret [1] = tv.tv_usec; 353 ret [1] = tv.tv_usec;
327} 354}
328 355
329ECB_INLINE double 356ecb_inline double
330coro_nvtime () 357coro_nvtime (void)
331{ 358{
332 struct timeval tv; 359 struct timeval tv;
333 gettimeofday (&tv, 0); 360 gettimeofday (&tv, 0);
334 361
335 return tv.tv_sec + tv.tv_usec * 1e-6; 362 return tv.tv_sec + tv.tv_usec * 1e-6;
336} 363}
337 364
338ECB_INLINE void 365ecb_inline void
339time_init (pTHX) 366time_init (pTHX)
340{ 367{
341 nvtime = coro_nvtime; 368 nvtime = coro_nvtime;
342 u2time = coro_u2time; 369 u2time = coro_u2time;
343} 370}
344 371
345#else 372#else
346 373
347ECB_INLINE void 374ecb_inline void
348time_init (pTHX) 375time_init (pTHX)
349{ 376{
350 SV **svp; 377 SV **svp;
351 378
352 require_pv ("Time/HiRes.pm"); 379 require_pv ("Time/HiRes.pm");
394 get_hv (name, create); 421 get_hv (name, create);
395#endif 422#endif
396 return get_hv (name, create); 423 return get_hv (name, create);
397} 424}
398 425
399ECB_INLINE void 426ecb_inline void
400coro_times_update () 427coro_times_update (void)
401{ 428{
402#ifdef coro_clock_gettime 429#ifdef coro_clock_gettime
403 struct timespec ts; 430 struct timespec ts;
404 431
405 ts.tv_sec = ts.tv_nsec = 0; 432 ts.tv_sec = ts.tv_nsec = 0;
417 time_real [0] = tv [0]; 444 time_real [0] = tv [0];
418 time_real [1] = tv [1] * 1000; 445 time_real [1] = tv [1] * 1000;
419#endif 446#endif
420} 447}
421 448
422ECB_INLINE void 449ecb_inline void
423coro_times_add (struct coro *c) 450coro_times_add (struct coro *c)
424{ 451{
425 c->t_real [1] += time_real [1]; 452 c->t_real [1] += time_real [1];
426 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } 453 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
427 c->t_real [0] += time_real [0]; 454 c->t_real [0] += time_real [0];
429 c->t_cpu [1] += time_cpu [1]; 456 c->t_cpu [1] += time_cpu [1];
430 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } 457 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
431 c->t_cpu [0] += time_cpu [0]; 458 c->t_cpu [0] += time_cpu [0];
432} 459}
433 460
434ECB_INLINE void 461ecb_inline void
435coro_times_sub (struct coro *c) 462coro_times_sub (struct coro *c)
436{ 463{
437 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } 464 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
438 c->t_real [1] -= time_real [1]; 465 c->t_real [1] -= time_real [1];
439 c->t_real [0] -= time_real [0]; 466 c->t_real [0] -= time_real [0];
460 : 0) 487 : 0)
461 488
462#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 489#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
463#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) 490#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
464 491
465ECB_INLINE MAGIC * 492ecb_inline MAGIC *
466SvSTATEhv_p (pTHX_ SV *coro) 493SvSTATEhv_p (pTHX_ SV *coro)
467{ 494{
468 MAGIC *mg; 495 MAGIC *mg;
469 496
470 if (ecb_expect_true ( 497 if (ecb_expect_true (
475 return mg; 502 return mg;
476 503
477 return 0; 504 return 0;
478} 505}
479 506
480ECB_INLINE struct coro * 507ecb_inline struct coro *
481SvSTATE_ (pTHX_ SV *coro) 508SvSTATE_ (pTHX_ SV *coro)
482{ 509{
483 MAGIC *mg; 510 MAGIC *mg;
484 511
485 if (SvROK (coro)) 512 if (SvROK (coro))
499#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 526#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
500 527
501/*****************************************************************************/ 528/*****************************************************************************/
502/* padlist management and caching */ 529/* padlist management and caching */
503 530
504ECB_INLINE AV * 531ecb_inline AV *
505coro_derive_padlist (pTHX_ CV *cv) 532coro_derive_padlist (pTHX_ CV *cv)
506{ 533{
507 AV *padlist = CvPADLIST (cv); 534 AV *padlist = CvPADLIST (cv);
508 AV *newpadlist, *newpad; 535 AV *newpadlist, *newpad;
509 536
521 av_store (newpadlist, 1, (SV *)newpad); 548 av_store (newpadlist, 1, (SV *)newpad);
522 549
523 return newpadlist; 550 return newpadlist;
524} 551}
525 552
526ECB_INLINE void 553ecb_inline void
527free_padlist (pTHX_ AV *padlist) 554free_padlist (pTHX_ AV *padlist)
528{ 555{
529 /* may be during global destruction */ 556 /* may be during global destruction */
530 if (!IN_DESTRUCT) 557 if (!IN_DESTRUCT)
531 { 558 {
574 0, 0, 0, 0, 601 0, 0, 0, 0,
575 coro_cv_free 602 coro_cv_free
576}; 603};
577 604
578/* the next two functions merely cache the padlists */ 605/* the next two functions merely cache the padlists */
579ECB_INLINE void 606ecb_inline void
580get_padlist (pTHX_ CV *cv) 607get_padlist (pTHX_ CV *cv)
581{ 608{
582 MAGIC *mg = CORO_MAGIC_cv (cv); 609 MAGIC *mg = CORO_MAGIC_cv (cv);
583 AV *av; 610 AV *av;
584 611
597 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); 624 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
598#endif 625#endif
599 } 626 }
600} 627}
601 628
602ECB_INLINE void 629ecb_inline void
603put_padlist (pTHX_ CV *cv) 630put_padlist (pTHX_ CV *cv)
604{ 631{
605 MAGIC *mg = CORO_MAGIC_cv (cv); 632 MAGIC *mg = CORO_MAGIC_cv (cv);
606 AV *av; 633 AV *av;
607 634
616 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 643 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
617} 644}
618 645
619/** load & save, init *******************************************************/ 646/** load & save, init *******************************************************/
620 647
648ecb_inline void
649swap_sv (SV *a, SV *b)
650{
651 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
652 SV tmp;
653
654 /* swap sv_any */
655 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
656
657 /* swap sv_flags */
658 SvFLAGS (&tmp) = SvFLAGS (a);
659 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
660 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
661
662#if PERL_VERSION_ATLEAST (5,10,0)
663 /* perl 5.10 and later complicates this _quite_ a bit, but it also
664 * is much faster, so no quarrels here. alternatively, we could
665 * sv_upgrade to avoid this.
666 */
667 {
668 /* swap sv_u */
669 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
670
671 /* if SvANY points to the head, we need to adjust the pointers,
672 * as the pointer for a still points to b, and maybe vice versa.
673 */
674 #define svany_in_head(type) \
675 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
676
677 if (svany_in_head (SvTYPE (a)))
678 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
679
680 if (svany_in_head (SvTYPE (b)))
681 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
682 }
683#endif
684}
685
621/* swap sv heads, at least logically */ 686/* swap sv heads, at least logically */
622static void 687static void
623swap_svs (pTHX_ Coro__State c) 688swap_svs (pTHX_ Coro__State c)
624{ 689{
625 int i; 690 int i;
626 691
627 for (i = 0; i <= AvFILLp (c->swap_sv); ) 692 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
628 { 693 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
629 SV *a = AvARRAY (c->swap_sv)[i++];
630 SV *b = AvARRAY (c->swap_sv)[i++];
631
632 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
633 SV tmp;
634
635 /* swap sv_any */
636 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
637
638 /* swap sv_flags */
639 SvFLAGS (&tmp) = SvFLAGS (a);
640 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
641 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
642
643#if PERL_VERSION_ATLEAST (5,10,0)
644 /* perl 5.10 complicates this _quite_ a bit, but it also is
645 * much faster, so no quarrels here. alternatively, we could
646 * sv_upgrade to avoid this.
647 */
648 {
649 /* swap sv_u */
650 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
651
652 /* if SvANY points to the head, we need to adjust the pointers,
653 * as the pointer for a still points to b, and maybe vice versa.
654 */
655 #define svany_in_head(type) \
656 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
657
658 if (svany_in_head (SvTYPE (a)))
659 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
660
661 if (svany_in_head (SvTYPE (b)))
662 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
663 }
664#endif
665 }
666} 694}
667 695
668#define SWAP_SVS(coro) \ 696#define SWAP_SVS(coro) \
669 if (ecb_expect_false ((coro)->swap_sv)) \ 697 if (ecb_expect_false ((coro)->swap_sv)) \
670 swap_svs (aTHX_ (coro)) 698 swap_svs (aTHX_ (coro))
678 perl_slots *slot = c->slot; 706 perl_slots *slot = c->slot;
679 c->slot = 0; 707 c->slot = 0;
680 708
681 PL_mainstack = c->mainstack; 709 PL_mainstack = c->mainstack;
682 710
683 GvSV (PL_defgv) = slot->defsv; 711#if CORO_JIT
684 GvAV (PL_defgv) = slot->defav; 712 load_perl_slots (slot);
685 GvSV (PL_errgv) = slot->errsv; 713#else
686 GvSV (irsgv) = slot->irsgv;
687 GvHV (PL_hintgv) = slot->hinthv;
688
689 #define VAR(name,type) PL_ ## name = slot->name; 714 #define VARx(name,expr,type) expr = slot->name;
690 # include "state.h" 715 # include "state.h"
691 #undef VAR 716 #undef VARx
717#endif
692 718
693 { 719 {
694 dSP; 720 dSP;
695 721
696 CV *cv; 722 CV *cv;
798 824
799 PUTBACK; 825 PUTBACK;
800 } 826 }
801 827
802 /* allocate some space on the context stack for our purposes */ 828 /* allocate some space on the context stack for our purposes */
803 if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) 829 if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max))
804 { 830 {
805 unsigned int i; 831 unsigned int i;
806 832
807 for (i = 0; i < SLOT_COUNT; ++i) 833 for (i = 0; i < SLOT_COUNT; ++i)
808 CXINC; 834 CXINC;
813 c->mainstack = PL_mainstack; 839 c->mainstack = PL_mainstack;
814 840
815 { 841 {
816 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 842 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
817 843
818 slot->defav = GvAV (PL_defgv); 844#if CORO_JIT
819 slot->defsv = DEFSV; 845 save_perl_slots (slot);
820 slot->errsv = ERRSV; 846#else
821 slot->irsgv = GvSV (irsgv);
822 slot->hinthv = GvHV (PL_hintgv);
823
824 #define VAR(name,type) slot->name = PL_ ## name; 847 #define VARx(name,expr,type) slot->name = expr;
825 # include "state.h" 848 # include "state.h"
826 #undef VAR 849 #undef VARx
850#endif
827 } 851 }
828} 852}
829 853
830/* 854/*
831 * allocate various perl stacks. This is almost an exact copy 855 * allocate various perl stacks. This is almost an exact copy
862#endif 886#endif
863 887
864 New(54,PL_scopestack,8,I32); 888 New(54,PL_scopestack,8,I32);
865 PL_scopestack_ix = 0; 889 PL_scopestack_ix = 0;
866 PL_scopestack_max = 8; 890 PL_scopestack_max = 8;
891#if HAS_SCOPESTACK_NAME
892 New(54,PL_scopestack_name,8,const char*);
893#endif
867 894
868 New(54,PL_savestack,24,ANY); 895 New(54,PL_savestack,24,ANY);
869 PL_savestack_ix = 0; 896 PL_savestack_ix = 0;
870 PL_savestack_max = 24; 897 PL_savestack_max = 24;
871 898
899 } 926 }
900 927
901 Safefree (PL_tmps_stack); 928 Safefree (PL_tmps_stack);
902 Safefree (PL_markstack); 929 Safefree (PL_markstack);
903 Safefree (PL_scopestack); 930 Safefree (PL_scopestack);
931#if HAS_SCOPESTACK_NAME
932 Safefree (PL_scopestack_name);
933#endif
904 Safefree (PL_savestack); 934 Safefree (PL_savestack);
905#if !PERL_VERSION_ATLEAST (5,10,0) 935#if !PERL_VERSION_ATLEAST (5,10,0)
906 Safefree (PL_retstack); 936 Safefree (PL_retstack);
907#endif 937#endif
908} 938}
971 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 1001 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
972 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 1002 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
973 1003
974 if (svp) 1004 if (svp)
975 { 1005 {
976 sv_setsv (sv, *svp ? *svp : &PL_sv_undef); 1006 SV *ssv;
1007
1008 if (!*svp)
1009 ssv = &PL_sv_undef;
1010 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1011 ssv = sv_2mortal (newRV_inc (*svp));
1012 else
1013 ssv = *svp;
1014
1015 sv_setsv (sv, ssv);
977 return 0; 1016 return 0;
978 } 1017 }
979 } 1018 }
980 1019
981 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1020 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
1173 /* restore swapped sv's */ 1212 /* restore swapped sv's */
1174 SWAP_SVS (coro); 1213 SWAP_SVS (coro);
1175 1214
1176 coro_destruct_stacks (aTHX); 1215 coro_destruct_stacks (aTHX);
1177 1216
1178 // now save some sv's to be free'd later 1217 /* now save some sv's to be free'd later */
1179 svf [0] = GvSV (PL_defgv); 1218 svf [0] = GvSV (PL_defgv);
1180 svf [1] = (SV *)GvAV (PL_defgv); 1219 svf [1] = (SV *)GvAV (PL_defgv);
1181 svf [2] = GvSV (PL_errgv); 1220 svf [2] = GvSV (PL_errgv);
1182 svf [3] = (SV *)PL_defoutgv; 1221 svf [3] = (SV *)PL_defoutgv;
1183 svf [4] = PL_rs; 1222 svf [4] = PL_rs;
1203 SvREFCNT_dec (coro->invoke_cb); 1242 SvREFCNT_dec (coro->invoke_cb);
1204 SvREFCNT_dec (coro->invoke_av); 1243 SvREFCNT_dec (coro->invoke_av);
1205 } 1244 }
1206} 1245}
1207 1246
1208ECB_INLINE void 1247ecb_inline void
1209free_coro_mortal (pTHX) 1248free_coro_mortal (pTHX)
1210{ 1249{
1211 if (ecb_expect_true (coro_mortal)) 1250 if (ecb_expect_true (coro_mortal))
1212 { 1251 {
1213 SvREFCNT_dec ((SV *)coro_mortal); 1252 SvREFCNT_dec ((SV *)coro_mortal);
1372 slf_frame.prepare = slf_prepare_set_stacklevel; 1411 slf_frame.prepare = slf_prepare_set_stacklevel;
1373 slf_frame.check = slf_check_set_stacklevel; 1412 slf_frame.check = slf_check_set_stacklevel;
1374} 1413}
1375 1414
1376/* the tail of transfer: execute stuff we can only do after a transfer */ 1415/* the tail of transfer: execute stuff we can only do after a transfer */
1377ECB_INLINE void 1416ecb_inline void
1378transfer_tail (pTHX) 1417transfer_tail (pTHX)
1379{ 1418{
1380 free_coro_mortal (aTHX); 1419 free_coro_mortal (aTHX);
1381} 1420}
1382 1421
1426 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1465 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1427 } 1466 }
1428} 1467}
1429 1468
1430static coro_cctx * 1469static coro_cctx *
1431cctx_new () 1470cctx_new (void)
1432{ 1471{
1433 coro_cctx *cctx; 1472 coro_cctx *cctx;
1434 1473
1435 ++cctx_count; 1474 ++cctx_count;
1436 New (0, cctx, 1, coro_cctx); 1475 New (0, cctx, 1, coro_cctx);
1442 return cctx; 1481 return cctx;
1443} 1482}
1444 1483
1445/* create a new cctx only suitable as source */ 1484/* create a new cctx only suitable as source */
1446static coro_cctx * 1485static coro_cctx *
1447cctx_new_empty () 1486cctx_new_empty (void)
1448{ 1487{
1449 coro_cctx *cctx = cctx_new (); 1488 coro_cctx *cctx = cctx_new ();
1450 1489
1451 cctx->sptr = 0; 1490 cctx->sptr = 0;
1452 coro_create (&cctx->cctx, 0, 0, 0, 0); 1491 coro_create (&cctx->cctx, 0, 0, 0, 0);
1454 return cctx; 1493 return cctx;
1455} 1494}
1456 1495
1457/* create a new cctx suitable as destination/running a perl interpreter */ 1496/* create a new cctx suitable as destination/running a perl interpreter */
1458static coro_cctx * 1497static coro_cctx *
1459cctx_new_run () 1498cctx_new_run (void)
1460{ 1499{
1461 coro_cctx *cctx = cctx_new (); 1500 coro_cctx *cctx = cctx_new ();
1462 void *stack_start; 1501 void *stack_start;
1463 size_t stack_size; 1502 size_t stack_size;
1464 1503
1465#if HAVE_MMAP 1504#if HAVE_MMAP
1466 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1505 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1467 /* mmap supposedly does allocate-on-write for us */ 1506 /* mmap supposedly does allocate-on-write for us */
1468 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1507 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0);
1469 1508
1470 if (cctx->sptr != (void *)-1) 1509 if (cctx->sptr != (void *)-1)
1471 { 1510 {
1472 #if CORO_STACKGUARD 1511 #if CORO_STACKGUARD
1473 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1512 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1775 TRANSFER (ta, 1); 1814 TRANSFER (ta, 1);
1776} 1815}
1777 1816
1778/** Coro ********************************************************************/ 1817/** Coro ********************************************************************/
1779 1818
1780ECB_INLINE void 1819ecb_inline void
1781coro_enq (pTHX_ struct coro *coro) 1820coro_enq (pTHX_ struct coro *coro)
1782{ 1821{
1783 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; 1822 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1784 1823
1785 SvREFCNT_inc_NN (coro->hv); 1824 SvREFCNT_inc_NN (coro->hv);
1787 coro->next_ready = 0; 1826 coro->next_ready = 0;
1788 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; 1827 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1789 ready [1] = coro; 1828 ready [1] = coro;
1790} 1829}
1791 1830
1792ECB_INLINE struct coro * 1831ecb_inline struct coro *
1793coro_deq (pTHX) 1832coro_deq (pTHX)
1794{ 1833{
1795 int prio; 1834 int prio;
1796 1835
1797 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) 1836 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1850{ 1889{
1851 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1890 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1852} 1891}
1853 1892
1854/* expects to own a reference to next->hv */ 1893/* expects to own a reference to next->hv */
1855ECB_INLINE void 1894ecb_inline void
1856prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1895prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1857{ 1896{
1858 SV *prev_sv = SvRV (coro_current); 1897 SV *prev_sv = SvRV (coro_current);
1859 1898
1860 ta->prev = SvSTATE_hv (prev_sv); 1899 ta->prev = SvSTATE_hv (prev_sv);
1919 } 1958 }
1920 } 1959 }
1921 } 1960 }
1922} 1961}
1923 1962
1924ECB_INLINE void 1963ecb_inline void
1925prepare_cede (pTHX_ struct coro_transfer_args *ta) 1964prepare_cede (pTHX_ struct coro_transfer_args *ta)
1926{ 1965{
1927 api_ready (aTHX_ coro_current); 1966 api_ready (aTHX_ coro_current);
1928 prepare_schedule (aTHX_ ta); 1967 prepare_schedule (aTHX_ ta);
1929} 1968}
1930 1969
1931ECB_INLINE void 1970ecb_inline void
1932prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) 1971prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1933{ 1972{
1934 SV *prev = SvRV (coro_current); 1973 SV *prev = SvRV (coro_current);
1935 1974
1936 if (coro_nready) 1975 if (coro_nready)
2936 { 2975 {
2937 int i; 2976 int i;
2938 /* if we were woken up but can't down, we look through the whole */ 2977 /* if we were woken up but can't down, we look through the whole */
2939 /* waiters list and only add us if we aren't in there already */ 2978 /* waiters list and only add us if we aren't in there already */
2940 /* this avoids some degenerate memory usage cases */ 2979 /* this avoids some degenerate memory usage cases */
2941 for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug 2980 for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */
2942 if (AvARRAY (av)[i] == coro_hv) 2981 if (AvARRAY (av)[i] == coro_hv)
2943 return 1; 2982 return 1;
2944 2983
2945 av_push (av, SvREFCNT_inc (coro_hv)); 2984 av_push (av, SvREFCNT_inc (coro_hv));
2946 return 1; 2985 return 1;
3323 3362
3324#ifndef __cplusplus 3363#ifndef __cplusplus
3325ecb_cold XS(boot_Coro__State); 3364ecb_cold XS(boot_Coro__State);
3326#endif 3365#endif
3327 3366
3367#if CORO_JIT
3368
3369static void ecb_noinline ecb_cold
3370pushav_4uv (pTHX_ UV a, UV b, UV c, UV d)
3371{
3372 dSP;
3373 AV *av = newAV ();
3374
3375 av_store (av, 3, newSVuv (d));
3376 av_store (av, 2, newSVuv (c));
3377 av_store (av, 1, newSVuv (b));
3378 av_store (av, 0, newSVuv (a));
3379
3380 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3381
3382 PUTBACK;
3383}
3384
3385static void ecb_noinline ecb_cold
3386jit_init (pTHX)
3387{
3388 dSP;
3389 SV *load, *save;
3390 char *map_base;
3391 char *load_ptr, *save_ptr;
3392 STRLEN load_len, save_len, map_len;
3393 int count;
3394
3395 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3396
3397 PUSHMARK (SP);
3398#define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3399# include "state.h"
3400#undef VARx
3401 count = call_pv ("Coro::State::_jit", G_ARRAY);
3402 SPAGAIN;
3403
3404 save = POPs; save_ptr = SvPVbyte (save, save_len);
3405 load = POPs; load_ptr = SvPVbyte (load, load_len);
3406
3407 map_len = load_len + save_len + 16;
3408
3409 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3410
3411 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3412
3413 load_perl_slots = (load_save_perl_slots_type)map_base;
3414 memcpy (map_base, load_ptr, load_len);
3415
3416 map_base += (load_len + 15) & ~15;
3417
3418 save_perl_slots = (load_save_perl_slots_type)map_base;
3419 memcpy (map_base, save_ptr, save_len);
3420
3421 /* we are good citizens and try to make the page read-only, so the evil evil */
3422 /* hackers might have it a bit more difficult */
3423 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3424
3425 PUTBACK;
3426 eval_pv ("undef &Coro::State::_jit", 1);
3427}
3428
3429#endif
3430
3328MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3431MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
3329 3432
3330PROTOTYPES: DISABLE 3433PROTOTYPES: DISABLE
3331 3434
3332BOOT: 3435BOOT:
3335# if CORO_PTHREAD 3438# if CORO_PTHREAD
3336 coro_thx = PERL_GET_CONTEXT; 3439 coro_thx = PERL_GET_CONTEXT;
3337# endif 3440# endif
3338#endif 3441#endif
3339 BOOT_PAGESIZE; 3442 BOOT_PAGESIZE;
3443
3444 /* perl defines these to check for existance first, but why it doesn't */
3445 /* just create them one at init time is not clear to me, except for */
3446 /* programs trying to delete them, but... */
3447 /* anyway, we declare this as invalid and make sure they are initialised here */
3448 DEFSV;
3449 ERRSV;
3340 3450
3341 cctx_current = cctx_new_empty (); 3451 cctx_current = cctx_new_empty ();
3342 3452
3343 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3453 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3344 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3454 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3387 coroapi.prepare_cede_notself = prepare_cede_notself; 3497 coroapi.prepare_cede_notself = prepare_cede_notself;
3388 3498
3389 time_init (aTHX); 3499 time_init (aTHX);
3390 3500
3391 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3501 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3502#if CORO_JIT
3503 PUTBACK;
3504 jit_init (aTHX);
3505 SPAGAIN;
3506#endif
3392} 3507}
3393 3508
3394SV * 3509SV *
3395new (SV *klass, ...) 3510new (SV *klass, ...)
3396 ALIAS: 3511 ALIAS:
4046} 4161}
4047 4162
4048MODULE = Coro::State PACKAGE = Coro::SemaphoreSet 4163MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
4049 4164
4050void 4165void
4051_may_delete (SV *sem, int count, int extra_refs) 4166_may_delete (SV *sem, int count, unsigned int extra_refs)
4052 PPCODE: 4167 PPCODE:
4053{ 4168{
4054 AV *av = (AV *)SvRV (sem); 4169 AV *av = (AV *)SvRV (sem);
4055 4170
4056 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4171 if (SvREFCNT ((SV *)av) == 1 + extra_refs

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines