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.409 by root, Sun Jun 12 04:29:15 2011 UTC vs.
Revision 1.421 by root, Sun May 27 16:36:05 2012 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
102/* used in state.h */
103#define VAR(name,type) VARx(name, PL_ ## name, type)
104
105#ifdef __linux 106#ifdef __linux
106# include <time.h> /* for timespec */ 107# include <time.h> /* for timespec */
107# include <syscall.h> /* for SYS_* */ 108# include <syscall.h> /* for SYS_* */
108# ifdef SYS_clock_gettime 109# ifdef SYS_clock_gettime
109# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 110# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
177 void *sptr; 178 void *sptr;
178 size_t ssize; 179 size_t ssize;
179 180
180 /* cpu state */ 181 /* cpu state */
181 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 182 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
183#ifndef NDEBUG
182 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 184 JMPENV *idle_te; /* same as idle_sp, but for top_env */
185#endif
183 JMPENV *top_env; 186 JMPENV *top_env;
184 coro_context cctx; 187 coro_context cctx;
185 188
186 U32 gen; 189 U32 gen;
187#if CORO_USE_VALGRIND 190#if CORO_USE_VALGRIND
207}; 210};
208 211
209/* the structure where most of the perl state is stored, overlaid on the cxstack */ 212/* the structure where most of the perl state is stored, overlaid on the cxstack */
210typedef struct 213typedef struct
211{ 214{
212#define VARx(name,expr,type) type name; 215 #define VARx(name,expr,type) type name;
213# include "state.h" 216 #include "state.h"
214#undef VARx
215} perl_slots; 217} perl_slots;
216 218
217// how many context stack entries do we need for perl_slots 219/* how many context stack entries do we need for perl_slots */
218#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 220#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
219 221
220/* this is a structure representing a perl-level coroutine */ 222/* this is a structure representing a perl-level coroutine */
221struct coro 223struct coro
222{ 224{
291#define coro_nready coroapi.nready 293#define coro_nready coroapi.nready
292 294
293/** JIT *********************************************************************/ 295/** JIT *********************************************************************/
294 296
295#if CORO_JIT 297#if CORO_JIT
298 /* APPLE doesn't have HAVE_MMAP though */
299 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
296 #ifndef CORO_JIT_TYPE 300 #ifndef CORO_JIT_TYPE
297 #if __x86_64 && (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris) 301 #if __x86_64 && CORO_JIT_UNIXY
298 #define CORO_JIT_TYPE "amd64-unix" 302 #define CORO_JIT_TYPE "amd64-unix"
299 typedef void (*load_save_perl_slots_type)(perl_slots *); 303 #elif __i386 && CORO_JIT_UNIXY
300 #elif __i386 && (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris)
301 #define CORO_JIT_TYPE "x86-unix" 304 #define CORO_JIT_TYPE "x86-unix"
302 typedef void (*load_save_perl_slots_type)(perl_slots *);
303 #else
304 x x x x
305 #undef CORO_JIT
306 #endif 305 #endif
307 #endif 306 #endif
308#endif 307#endif
309 308
309#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP
310 #undef CORO_JIT
311#endif
312
310#if CORO_JIT 313#if CORO_JIT
314 typedef void (*load_save_perl_slots_type)(perl_slots *);
311static load_save_perl_slots_type load_perl_slots, save_perl_slots; 315 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
312#endif 316#endif
313 317
314/** Coro::Select ************************************************************/ 318/** Coro::Select ************************************************************/
315 319
316static OP *(*coro_old_pp_sselect) (pTHX); 320static OP *(*coro_old_pp_sselect) (pTHX);
333 337
334/** time stuff **************************************************************/ 338/** time stuff **************************************************************/
335 339
336#ifdef HAS_GETTIMEOFDAY 340#ifdef HAS_GETTIMEOFDAY
337 341
338ECB_INLINE void 342ecb_inline void
339coro_u2time (pTHX_ UV ret[2]) 343coro_u2time (pTHX_ UV ret[2])
340{ 344{
341 struct timeval tv; 345 struct timeval tv;
342 gettimeofday (&tv, 0); 346 gettimeofday (&tv, 0);
343 347
344 ret [0] = tv.tv_sec; 348 ret [0] = tv.tv_sec;
345 ret [1] = tv.tv_usec; 349 ret [1] = tv.tv_usec;
346} 350}
347 351
348ECB_INLINE double 352ecb_inline double
349coro_nvtime () 353coro_nvtime (void)
350{ 354{
351 struct timeval tv; 355 struct timeval tv;
352 gettimeofday (&tv, 0); 356 gettimeofday (&tv, 0);
353 357
354 return tv.tv_sec + tv.tv_usec * 1e-6; 358 return tv.tv_sec + tv.tv_usec * 1e-6;
355} 359}
356 360
357ECB_INLINE void 361ecb_inline void
358time_init (pTHX) 362time_init (pTHX)
359{ 363{
360 nvtime = coro_nvtime; 364 nvtime = coro_nvtime;
361 u2time = coro_u2time; 365 u2time = coro_u2time;
362} 366}
363 367
364#else 368#else
365 369
366ECB_INLINE void 370ecb_inline void
367time_init (pTHX) 371time_init (pTHX)
368{ 372{
369 SV **svp; 373 SV **svp;
370 374
371 require_pv ("Time/HiRes.pm"); 375 require_pv ("Time/HiRes.pm");
413 get_hv (name, create); 417 get_hv (name, create);
414#endif 418#endif
415 return get_hv (name, create); 419 return get_hv (name, create);
416} 420}
417 421
418ECB_INLINE void 422ecb_inline void
419coro_times_update () 423coro_times_update (void)
420{ 424{
421#ifdef coro_clock_gettime 425#ifdef coro_clock_gettime
422 struct timespec ts; 426 struct timespec ts;
423 427
424 ts.tv_sec = ts.tv_nsec = 0; 428 ts.tv_sec = ts.tv_nsec = 0;
436 time_real [0] = tv [0]; 440 time_real [0] = tv [0];
437 time_real [1] = tv [1] * 1000; 441 time_real [1] = tv [1] * 1000;
438#endif 442#endif
439} 443}
440 444
441ECB_INLINE void 445ecb_inline void
442coro_times_add (struct coro *c) 446coro_times_add (struct coro *c)
443{ 447{
444 c->t_real [1] += time_real [1]; 448 c->t_real [1] += time_real [1];
445 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } 449 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
446 c->t_real [0] += time_real [0]; 450 c->t_real [0] += time_real [0];
448 c->t_cpu [1] += time_cpu [1]; 452 c->t_cpu [1] += time_cpu [1];
449 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } 453 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
450 c->t_cpu [0] += time_cpu [0]; 454 c->t_cpu [0] += time_cpu [0];
451} 455}
452 456
453ECB_INLINE void 457ecb_inline void
454coro_times_sub (struct coro *c) 458coro_times_sub (struct coro *c)
455{ 459{
456 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } 460 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
457 c->t_real [1] -= time_real [1]; 461 c->t_real [1] -= time_real [1];
458 c->t_real [0] -= time_real [0]; 462 c->t_real [0] -= time_real [0];
479 : 0) 483 : 0)
480 484
481#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 485#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
482#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) 486#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
483 487
484ECB_INLINE MAGIC * 488ecb_inline MAGIC *
485SvSTATEhv_p (pTHX_ SV *coro) 489SvSTATEhv_p (pTHX_ SV *coro)
486{ 490{
487 MAGIC *mg; 491 MAGIC *mg;
488 492
489 if (ecb_expect_true ( 493 if (ecb_expect_true (
494 return mg; 498 return mg;
495 499
496 return 0; 500 return 0;
497} 501}
498 502
499ECB_INLINE struct coro * 503ecb_inline struct coro *
500SvSTATE_ (pTHX_ SV *coro) 504SvSTATE_ (pTHX_ SV *coro)
501{ 505{
502 MAGIC *mg; 506 MAGIC *mg;
503 507
504 if (SvROK (coro)) 508 if (SvROK (coro))
518#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 522#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
519 523
520/*****************************************************************************/ 524/*****************************************************************************/
521/* padlist management and caching */ 525/* padlist management and caching */
522 526
523ECB_INLINE AV * 527ecb_inline AV *
524coro_derive_padlist (pTHX_ CV *cv) 528coro_derive_padlist (pTHX_ CV *cv)
525{ 529{
526 AV *padlist = CvPADLIST (cv); 530 AV *padlist = CvPADLIST (cv);
527 AV *newpadlist, *newpad; 531 AV *newpadlist, *newpad;
528 532
540 av_store (newpadlist, 1, (SV *)newpad); 544 av_store (newpadlist, 1, (SV *)newpad);
541 545
542 return newpadlist; 546 return newpadlist;
543} 547}
544 548
545ECB_INLINE void 549ecb_inline void
546free_padlist (pTHX_ AV *padlist) 550free_padlist (pTHX_ AV *padlist)
547{ 551{
548 /* may be during global destruction */ 552 /* may be during global destruction */
549 if (!IN_DESTRUCT) 553 if (!IN_DESTRUCT)
550 { 554 {
593 0, 0, 0, 0, 597 0, 0, 0, 0,
594 coro_cv_free 598 coro_cv_free
595}; 599};
596 600
597/* the next two functions merely cache the padlists */ 601/* the next two functions merely cache the padlists */
598ECB_INLINE void 602ecb_inline void
599get_padlist (pTHX_ CV *cv) 603get_padlist (pTHX_ CV *cv)
600{ 604{
601 MAGIC *mg = CORO_MAGIC_cv (cv); 605 MAGIC *mg = CORO_MAGIC_cv (cv);
602 AV *av; 606 AV *av;
603 607
616 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); 620 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
617#endif 621#endif
618 } 622 }
619} 623}
620 624
621ECB_INLINE void 625ecb_inline void
622put_padlist (pTHX_ CV *cv) 626put_padlist (pTHX_ CV *cv)
623{ 627{
624 MAGIC *mg = CORO_MAGIC_cv (cv); 628 MAGIC *mg = CORO_MAGIC_cv (cv);
625 AV *av; 629 AV *av;
626 630
635 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 639 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
636} 640}
637 641
638/** load & save, init *******************************************************/ 642/** load & save, init *******************************************************/
639 643
644ecb_inline void
645swap_sv (SV *a, SV *b)
646{
647 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
648 SV tmp;
649
650 /* swap sv_any */
651 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
652
653 /* swap sv_flags */
654 SvFLAGS (&tmp) = SvFLAGS (a);
655 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
656 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
657
658#if PERL_VERSION_ATLEAST (5,10,0)
659 /* perl 5.10 and later complicates this _quite_ a bit, but it also
660 * is much faster, so no quarrels here. alternatively, we could
661 * sv_upgrade to avoid this.
662 */
663 {
664 /* swap sv_u */
665 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
666
667 /* if SvANY points to the head, we need to adjust the pointers,
668 * as the pointer for a still points to b, and maybe vice versa.
669 */
670 #define svany_in_head(type) \
671 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
672
673 if (svany_in_head (SvTYPE (a)))
674 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
675
676 if (svany_in_head (SvTYPE (b)))
677 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
678 }
679#endif
680}
681
640/* swap sv heads, at least logically */ 682/* swap sv heads, at least logically */
641static void 683static void
642swap_svs (pTHX_ Coro__State c) 684swap_svs (pTHX_ Coro__State c)
643{ 685{
644 int i; 686 int i;
645 687
646 for (i = 0; i <= AvFILLp (c->swap_sv); ) 688 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
647 { 689 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
648 SV *a = AvARRAY (c->swap_sv)[i++];
649 SV *b = AvARRAY (c->swap_sv)[i++];
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 complicates this _quite_ a bit, but it also is
664 * 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} 690}
686 691
687#define SWAP_SVS(coro) \ 692#define SWAP_SVS(coro) \
688 if (ecb_expect_false ((coro)->swap_sv)) \ 693 if (ecb_expect_false ((coro)->swap_sv)) \
689 swap_svs (aTHX_ (coro)) 694 swap_svs (aTHX_ (coro))
701 706
702#if CORO_JIT 707#if CORO_JIT
703 load_perl_slots (slot); 708 load_perl_slots (slot);
704#else 709#else
705 #define VARx(name,expr,type) expr = slot->name; 710 #define VARx(name,expr,type) expr = slot->name;
706 # include "state.h" 711 #include "state.h"
707 #undef VARx
708#endif 712#endif
709 713
710 { 714 {
711 dSP; 715 dSP;
712 716
815 819
816 PUTBACK; 820 PUTBACK;
817 } 821 }
818 822
819 /* allocate some space on the context stack for our purposes */ 823 /* allocate some space on the context stack for our purposes */
820 if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) 824 if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max))
821 { 825 {
822 unsigned int i; 826 unsigned int i;
823 827
824 for (i = 0; i < SLOT_COUNT; ++i) 828 for (i = 0; i < SLOT_COUNT; ++i)
825 CXINC; 829 CXINC;
834 838
835#if CORO_JIT 839#if CORO_JIT
836 save_perl_slots (slot); 840 save_perl_slots (slot);
837#else 841#else
838 #define VARx(name,expr,type) slot->name = expr; 842 #define VARx(name,expr,type) slot->name = expr;
839 # include "state.h" 843 #include "state.h"
840 #undef VARx
841#endif 844#endif
842 } 845 }
843} 846}
844 847
845/* 848/*
877#endif 880#endif
878 881
879 New(54,PL_scopestack,8,I32); 882 New(54,PL_scopestack,8,I32);
880 PL_scopestack_ix = 0; 883 PL_scopestack_ix = 0;
881 PL_scopestack_max = 8; 884 PL_scopestack_max = 8;
885#if HAS_SCOPESTACK_NAME
886 New(54,PL_scopestack_name,8,const char*);
887#endif
882 888
883 New(54,PL_savestack,24,ANY); 889 New(54,PL_savestack,24,ANY);
884 PL_savestack_ix = 0; 890 PL_savestack_ix = 0;
885 PL_savestack_max = 24; 891 PL_savestack_max = 24;
886 892
914 } 920 }
915 921
916 Safefree (PL_tmps_stack); 922 Safefree (PL_tmps_stack);
917 Safefree (PL_markstack); 923 Safefree (PL_markstack);
918 Safefree (PL_scopestack); 924 Safefree (PL_scopestack);
925#if HAS_SCOPESTACK_NAME
926 Safefree (PL_scopestack_name);
927#endif
919 Safefree (PL_savestack); 928 Safefree (PL_savestack);
920#if !PERL_VERSION_ATLEAST (5,10,0) 929#if !PERL_VERSION_ATLEAST (5,10,0)
921 Safefree (PL_retstack); 930 Safefree (PL_retstack);
922#endif 931#endif
923} 932}
986 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 995 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
987 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 996 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
988 997
989 if (svp) 998 if (svp)
990 { 999 {
991 sv_setsv (sv, *svp ? *svp : &PL_sv_undef); 1000 SV *ssv;
1001
1002 if (!*svp)
1003 ssv = &PL_sv_undef;
1004 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1005 ssv = sv_2mortal (newRV_inc (*svp));
1006 else
1007 ssv = *svp;
1008
1009 sv_setsv (sv, ssv);
992 return 0; 1010 return 0;
993 } 1011 }
994 } 1012 }
995 1013
996 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1014 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
1188 /* restore swapped sv's */ 1206 /* restore swapped sv's */
1189 SWAP_SVS (coro); 1207 SWAP_SVS (coro);
1190 1208
1191 coro_destruct_stacks (aTHX); 1209 coro_destruct_stacks (aTHX);
1192 1210
1193 // now save some sv's to be free'd later 1211 /* now save some sv's to be free'd later */
1194 svf [0] = GvSV (PL_defgv); 1212 svf [0] = GvSV (PL_defgv);
1195 svf [1] = (SV *)GvAV (PL_defgv); 1213 svf [1] = (SV *)GvAV (PL_defgv);
1196 svf [2] = GvSV (PL_errgv); 1214 svf [2] = GvSV (PL_errgv);
1197 svf [3] = (SV *)PL_defoutgv; 1215 svf [3] = (SV *)PL_defoutgv;
1198 svf [4] = PL_rs; 1216 svf [4] = PL_rs;
1218 SvREFCNT_dec (coro->invoke_cb); 1236 SvREFCNT_dec (coro->invoke_cb);
1219 SvREFCNT_dec (coro->invoke_av); 1237 SvREFCNT_dec (coro->invoke_av);
1220 } 1238 }
1221} 1239}
1222 1240
1223ECB_INLINE void 1241ecb_inline void
1224free_coro_mortal (pTHX) 1242free_coro_mortal (pTHX)
1225{ 1243{
1226 if (ecb_expect_true (coro_mortal)) 1244 if (ecb_expect_true (coro_mortal))
1227 { 1245 {
1228 SvREFCNT_dec ((SV *)coro_mortal); 1246 SvREFCNT_dec ((SV *)coro_mortal);
1387 slf_frame.prepare = slf_prepare_set_stacklevel; 1405 slf_frame.prepare = slf_prepare_set_stacklevel;
1388 slf_frame.check = slf_check_set_stacklevel; 1406 slf_frame.check = slf_check_set_stacklevel;
1389} 1407}
1390 1408
1391/* the tail of transfer: execute stuff we can only do after a transfer */ 1409/* the tail of transfer: execute stuff we can only do after a transfer */
1392ECB_INLINE void 1410ecb_inline void
1393transfer_tail (pTHX) 1411transfer_tail (pTHX)
1394{ 1412{
1395 free_coro_mortal (aTHX); 1413 free_coro_mortal (aTHX);
1414}
1415
1416/* try to exit the same way perl's main function would do */
1417/* we do not bother resetting the environment or other things *7
1418/* that are not, uhm, essential */
1419/* this obviously also doesn't work when perl is embedded */
1420static void ecb_noinline ecb_cold
1421perlish_exit (pTHX)
1422{
1423 int exitstatus = perl_destruct (PL_curinterp);
1424 perl_free (PL_curinterp);
1425 exit (exitstatus);
1396} 1426}
1397 1427
1398/* 1428/*
1399 * this is a _very_ stripped down perl interpreter ;) 1429 * this is a _very_ stripped down perl interpreter ;)
1400 */ 1430 */
1429 */ 1459 */
1430 1460
1431 /* 1461 /*
1432 * If perl-run returns we assume exit() was being called or the coro 1462 * If perl-run returns we assume exit() was being called or the coro
1433 * fell off the end, which seems to be the only valid (non-bug) 1463 * fell off the end, which seems to be the only valid (non-bug)
1434 * reason for perl_run to return. We try to exit by jumping to the 1464 * reason for perl_run to return. We try to mimic whatever perl is normally
1435 * bootstrap-time "top" top_env, as we cannot restore the "main" 1465 * doing in that case. YMMV.
1436 * coroutine as Coro has no such concept.
1437 * This actually isn't valid with the pthread backend, but OSes requiring
1438 * that backend are too broken to do it in a standards-compliant way.
1439 */ 1466 */
1440 PL_top_env = main_top_env; 1467 perlish_exit (aTHX);
1441 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1442 } 1468 }
1443} 1469}
1444 1470
1445static coro_cctx * 1471static coro_cctx *
1446cctx_new () 1472cctx_new (void)
1447{ 1473{
1448 coro_cctx *cctx; 1474 coro_cctx *cctx;
1449 1475
1450 ++cctx_count; 1476 ++cctx_count;
1451 New (0, cctx, 1, coro_cctx); 1477 New (0, cctx, 1, coro_cctx);
1457 return cctx; 1483 return cctx;
1458} 1484}
1459 1485
1460/* create a new cctx only suitable as source */ 1486/* create a new cctx only suitable as source */
1461static coro_cctx * 1487static coro_cctx *
1462cctx_new_empty () 1488cctx_new_empty (void)
1463{ 1489{
1464 coro_cctx *cctx = cctx_new (); 1490 coro_cctx *cctx = cctx_new ();
1465 1491
1466 cctx->sptr = 0; 1492 cctx->sptr = 0;
1467 coro_create (&cctx->cctx, 0, 0, 0, 0); 1493 coro_create (&cctx->cctx, 0, 0, 0, 0);
1469 return cctx; 1495 return cctx;
1470} 1496}
1471 1497
1472/* create a new cctx suitable as destination/running a perl interpreter */ 1498/* create a new cctx suitable as destination/running a perl interpreter */
1473static coro_cctx * 1499static coro_cctx *
1474cctx_new_run () 1500cctx_new_run (void)
1475{ 1501{
1476 coro_cctx *cctx = cctx_new (); 1502 coro_cctx *cctx = cctx_new ();
1477 void *stack_start; 1503 void *stack_start;
1478 size_t stack_size; 1504 size_t stack_size;
1479 1505
1480#if HAVE_MMAP 1506#if HAVE_MMAP
1481 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1507 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1482 /* mmap supposedly does allocate-on-write for us */ 1508 /* mmap supposedly does allocate-on-write for us */
1483 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1509 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1484 1510
1485 if (cctx->sptr != (void *)-1) 1511 if (cctx->sptr != (void *)-1)
1486 { 1512 {
1487 #if CORO_STACKGUARD 1513 #if CORO_STACKGUARD
1488 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1514 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1790 TRANSFER (ta, 1); 1816 TRANSFER (ta, 1);
1791} 1817}
1792 1818
1793/** Coro ********************************************************************/ 1819/** Coro ********************************************************************/
1794 1820
1795ECB_INLINE void 1821ecb_inline void
1796coro_enq (pTHX_ struct coro *coro) 1822coro_enq (pTHX_ struct coro *coro)
1797{ 1823{
1798 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; 1824 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1799 1825
1800 SvREFCNT_inc_NN (coro->hv); 1826 SvREFCNT_inc_NN (coro->hv);
1802 coro->next_ready = 0; 1828 coro->next_ready = 0;
1803 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; 1829 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1804 ready [1] = coro; 1830 ready [1] = coro;
1805} 1831}
1806 1832
1807ECB_INLINE struct coro * 1833ecb_inline struct coro *
1808coro_deq (pTHX) 1834coro_deq (pTHX)
1809{ 1835{
1810 int prio; 1836 int prio;
1811 1837
1812 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) 1838 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1865{ 1891{
1866 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1892 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1867} 1893}
1868 1894
1869/* expects to own a reference to next->hv */ 1895/* expects to own a reference to next->hv */
1870ECB_INLINE void 1896ecb_inline void
1871prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1897prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1872{ 1898{
1873 SV *prev_sv = SvRV (coro_current); 1899 SV *prev_sv = SvRV (coro_current);
1874 1900
1875 ta->prev = SvSTATE_hv (prev_sv); 1901 ta->prev = SvSTATE_hv (prev_sv);
1934 } 1960 }
1935 } 1961 }
1936 } 1962 }
1937} 1963}
1938 1964
1939ECB_INLINE void 1965ecb_inline void
1940prepare_cede (pTHX_ struct coro_transfer_args *ta) 1966prepare_cede (pTHX_ struct coro_transfer_args *ta)
1941{ 1967{
1942 api_ready (aTHX_ coro_current); 1968 api_ready (aTHX_ coro_current);
1943 prepare_schedule (aTHX_ ta); 1969 prepare_schedule (aTHX_ ta);
1944} 1970}
1945 1971
1946ECB_INLINE void 1972ecb_inline void
1947prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) 1973prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1948{ 1974{
1949 SV *prev = SvRV (coro_current); 1975 SV *prev = SvRV (coro_current);
1950 1976
1951 if (coro_nready) 1977 if (coro_nready)
2951 { 2977 {
2952 int i; 2978 int i;
2953 /* if we were woken up but can't down, we look through the whole */ 2979 /* if we were woken up but can't down, we look through the whole */
2954 /* waiters list and only add us if we aren't in there already */ 2980 /* waiters list and only add us if we aren't in there already */
2955 /* this avoids some degenerate memory usage cases */ 2981 /* this avoids some degenerate memory usage cases */
2956 for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug 2982 for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */
2957 if (AvARRAY (av)[i] == coro_hv) 2983 if (AvARRAY (av)[i] == coro_hv)
2958 return 1; 2984 return 1;
2959 2985
2960 av_push (av, SvREFCNT_inc (coro_hv)); 2986 av_push (av, SvREFCNT_inc (coro_hv));
2961 return 1; 2987 return 1;
3341#endif 3367#endif
3342 3368
3343#if CORO_JIT 3369#if CORO_JIT
3344 3370
3345static void ecb_noinline ecb_cold 3371static void ecb_noinline ecb_cold
3346pushav_3uv (pTHX_ UV a, UV b, UV c) 3372pushav_4uv (pTHX_ UV a, UV b, UV c, UV d)
3347{ 3373{
3348 dSP; 3374 dSP;
3349 AV *av = newAV (); 3375 AV *av = newAV ();
3350 3376
3377 av_store (av, 3, newSVuv (d));
3351 av_store (av, 2, newSVuv (c)); 3378 av_store (av, 2, newSVuv (c));
3352 av_store (av, 1, newSVuv (b)); 3379 av_store (av, 1, newSVuv (b));
3353 av_store (av, 0, newSVuv (a)); 3380 av_store (av, 0, newSVuv (a));
3354 3381
3355 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 3382 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3362{ 3389{
3363 dSP; 3390 dSP;
3364 SV *load, *save; 3391 SV *load, *save;
3365 char *map_base; 3392 char *map_base;
3366 char *load_ptr, *save_ptr; 3393 char *load_ptr, *save_ptr;
3367 STRLEN load_len, save_len; 3394 STRLEN load_len, save_len, map_len;
3368 int count; 3395 int count;
3369 3396
3370 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); 3397 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3371 3398
3372 PUSHMARK (SP); 3399 PUSHMARK (SP);
3373#define VARx(name,expr,type) pushav_3uv (aTHX_ (UV)&(expr), offsetof (perl_slots, name), sizeof (type)); 3400 #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3374# include "state.h" 3401 #include "state.h"
3375#undef VARx
3376 count = call_pv ("Coro::State::_jit", G_ARRAY); 3402 count = call_pv ("Coro::State::_jit", G_ARRAY);
3377 SPAGAIN; 3403 SPAGAIN;
3378 3404
3379 save = POPs; save_ptr = SvPVbyte (save, save_len); 3405 save = POPs; save_ptr = SvPVbyte (save, save_len);
3380 load = POPs; load_ptr = SvPVbyte (load, load_len); 3406 load = POPs; load_ptr = SvPVbyte (load, load_len);
3381 3407
3408 map_len = load_len + save_len + 16;
3409
3382 map_base = mmap (0, load_len + save_len + 16, PROT_EXEC | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 3410 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3383 3411
3384 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); 3412 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3385 3413
3386 load_perl_slots = (load_save_perl_slots_type)map_base; 3414 load_perl_slots = (load_save_perl_slots_type)map_base;
3387 memcpy (map_base, load_ptr, load_len); 3415 memcpy (map_base, load_ptr, load_len);
3388 3416
3389 map_base += (load_len + 15) & ~15; 3417 map_base += (load_len + 15) & ~15;
3390 3418
3391 save_perl_slots = (load_save_perl_slots_type)map_base; 3419 save_perl_slots = (load_save_perl_slots_type)map_base;
3392 memcpy (map_base, save_ptr, save_len); 3420 memcpy (map_base, save_ptr, save_len);
3421
3422 /* we are good citizens and try to make the page read-only, so the evil evil */
3423 /* hackers might have it a bit more difficult */
3424 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3393 3425
3394 PUTBACK; 3426 PUTBACK;
3395 eval_pv ("undef &Coro::State::_jit", 1); 3427 eval_pv ("undef &Coro::State::_jit", 1);
3396} 3428}
3397 3429
4130} 4162}
4131 4163
4132MODULE = Coro::State PACKAGE = Coro::SemaphoreSet 4164MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
4133 4165
4134void 4166void
4135_may_delete (SV *sem, int count, int extra_refs) 4167_may_delete (SV *sem, int count, unsigned int extra_refs)
4136 PPCODE: 4168 PPCODE:
4137{ 4169{
4138 AV *av = (AV *)SvRV (sem); 4170 AV *av = (AV *)SvRV (sem);
4139 4171
4140 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4172 if (SvREFCNT ((SV *)av) == 1 + extra_refs

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines